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

View File

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

View File

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

View File

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