Ensure correct ordering on stream operations (#176)

This commit is contained in:
Connor Prussin 2021-06-25 10:58:16 -07:00 committed by GitHub
parent 1da43a09c4
commit c075df65d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 14 deletions

View File

@ -42,9 +42,10 @@ instance bodyString :: Body String where
Aff.makeAff \done -> do
let
stream = HTTP.responseAsStream response
_ <- Stream.writeString stream Encoding.UTF8 body $ pure unit
_ <- Stream.end stream $ pure unit
done $ Either.Right unit
void $ Stream.writeString stream Encoding.UTF8 body
$ Stream.end stream
$ done
$ Either.Right unit
pure Aff.nonCanceler
-- | The instance for `Buffer` is trivial--we add a `Content-Length` header
@ -56,9 +57,10 @@ instance bodyBuffer :: Body Buffer.Buffer where
Aff.makeAff \done -> do
let
stream = HTTP.responseAsStream response
_ <- Stream.write stream body $ pure unit
_ <- Stream.end stream $ pure unit
done $ Either.Right unit
void $ Stream.write stream body
$ Stream.end stream
$ done
$ Either.Right unit
pure Aff.nonCanceler
-- | This instance can be used to send chunked data. Here, we add a
@ -72,7 +74,7 @@ instance bodyChunked ::
Aff.makeAff \done -> do
let
stream = TypeEquals.to body
_ <- Stream.pipe stream $ HTTP.responseAsStream response
void $ Stream.pipe stream $ HTTP.responseAsStream response
Stream.onEnd stream $ done $ Either.Right unit
pure Aff.nonCanceler

View File

@ -45,9 +45,11 @@ sendSpec =
\response ->
Aff.makeAff \done -> do
stream <- pure $ HTTP.responseAsStream response
_ <- Stream.writeString stream Encoding.UTF8 "test" $ pure unit
_ <- Stream.end stream $ pure unit
done $ Either.Right unit
void
$ Stream.writeString stream Encoding.UTF8 "test"
$ Stream.end stream
$ done
$ Either.Right unit
pure Aff.nonCanceler
}

View File

@ -30,11 +30,19 @@ exports.mockResponse = function() {
body: "",
headers: {},
write: function(str) {
write: function(str, encoding, callback) {
this.body = this.body + str;
if (callback) {
callback();
}
},
end: function(str, encoding, callback) {
if (callback) {
callback();
}
},
end: function() { },
on: function() { },
once: function() { },
emit: function() { },

View File

@ -47,8 +47,10 @@ request secure port method headers path body =
req <- HTTPClient.request options $ Either.Right >>> done
let
stream = HTTPClient.requestAsStream req
_ <- Stream.writeString stream Encoding.UTF8 body $ pure unit
Stream.end stream $ pure unit
void
$ Stream.writeString stream Encoding.UTF8 body
$ Stream.end stream
$ pure unit
pure Aff.nonCanceler
where
options =