* Fix body read (#117) * Minor code cleanup
This commit is contained in:
parent
f4aeb74b72
commit
30654a62a3
17
src/HTTPure/Body.js
Normal file
17
src/HTTPure/Body.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
/* global exports */
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
exports.aggregateChunks = function (stream) {
|
||||||
|
return function(onEnd) {
|
||||||
|
return function() {
|
||||||
|
var chunks = [];
|
||||||
|
stream.on("data", function (chunk) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
});
|
||||||
|
stream.on("end", function() {
|
||||||
|
onEnd(chunks)();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
@ -8,17 +8,16 @@ module HTTPure.Body
|
|||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
import Data.Either as Either
|
import Data.Either as Either
|
||||||
|
import Data.Foldable as Foldable
|
||||||
import Effect as Effect
|
import Effect as Effect
|
||||||
import Effect.Aff as Aff
|
import Effect.Aff as Aff
|
||||||
import Effect.Ref as Ref
|
import HTTPure.Headers as Headers
|
||||||
import Node.Buffer as Buffer
|
import Node.Buffer as Buffer
|
||||||
import Node.Encoding as Encoding
|
import Node.Encoding as Encoding
|
||||||
import Node.HTTP as HTTP
|
import Node.HTTP as HTTP
|
||||||
import Node.Stream as Stream
|
import Node.Stream as Stream
|
||||||
import Type.Equality as TypeEquals
|
import Type.Equality as TypeEquals
|
||||||
|
|
||||||
import HTTPure.Headers as Headers
|
|
||||||
|
|
||||||
-- | Types that implement the `Body` class can be used as a body to an HTTPure
|
-- | Types that implement the `Body` class can be used as a body to an HTTPure
|
||||||
-- | response, and can be used with all the response helpers.
|
-- | response, and can be used with all the response helpers.
|
||||||
class Body b where
|
class Body b where
|
||||||
@ -79,12 +78,16 @@ instance bodyChunked ::
|
|||||||
Stream.onEnd stream $ done $ Either.Right unit
|
Stream.onEnd stream $ done $ Either.Right unit
|
||||||
pure Aff.nonCanceler
|
pure Aff.nonCanceler
|
||||||
|
|
||||||
|
foreign import aggregateChunks :: forall w. Stream.Readable w ->
|
||||||
|
(Array Buffer.Buffer -> Effect.Effect Unit) ->
|
||||||
|
Effect.Effect Unit
|
||||||
|
|
||||||
-- | Extract the contents of the body of the HTTP `Request`.
|
-- | Extract the contents of the body of the HTTP `Request`.
|
||||||
read :: HTTP.Request -> Aff.Aff String
|
read :: HTTP.Request -> Aff.Aff String
|
||||||
read request = Aff.makeAff \done -> do
|
read request = Aff.makeAff \done -> do
|
||||||
let stream = HTTP.requestAsStream request
|
let
|
||||||
buf <- Ref.new ""
|
stream = HTTP.requestAsStream request
|
||||||
Stream.onDataString stream Encoding.UTF8 \str ->
|
decode = Buffer.toString Encoding.UTF8
|
||||||
void $ Ref.modify ((<>) str) buf
|
aggregateChunks stream $
|
||||||
Stream.onEnd stream $ Ref.read buf >>= Either.Right >>> done
|
Foldable.foldMap decode >=> Either.Right >>> pure >=> done
|
||||||
pure Aff.nonCanceler
|
pure Aff.nonCanceler
|
||||||
|
Loading…
Reference in New Issue
Block a user