diff --git a/src/HTTPure/Body.js b/src/HTTPure/Body.js deleted file mode 100644 index 89e6c74..0000000 --- a/src/HTTPure/Body.js +++ /dev/null @@ -1,17 +0,0 @@ -/* 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)(); - }); - }; - } -}; diff --git a/src/HTTPure/Body.purs b/src/HTTPure/Body.purs index f5861fb..affc010 100644 --- a/src/HTTPure/Body.purs +++ b/src/HTTPure/Body.purs @@ -8,9 +8,9 @@ module HTTPure.Body import Prelude import Data.Either as Either -import Data.Foldable as Foldable import Effect as Effect import Effect.Aff as Aff +import Effect.Ref as Ref import HTTPure.Headers as Headers import Node.Buffer as Buffer import Node.Encoding as Encoding @@ -78,26 +78,12 @@ instance bodyChunked :: Stream.onEnd stream $ done $ Either.Right unit pure Aff.nonCanceler --- | Given a readable stream accumulates incomming chunks preserving --- | their original order. --- | --- | We want to append every chunk using single function call --- | to ensure "atomicity" of this action. If we use any additional --- | function calls inside our data handler Javascript scheduler can interleave --- | processing of multiple chunks and we can't be sure if they end up --- | in correct order. --- | --- | Please refer to #117 for more details. -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`. read :: HTTP.Request -> Aff.Aff String read request = Aff.makeAff \done -> do - let - stream = HTTP.requestAsStream request - decode = Buffer.toString Encoding.UTF8 - aggregateChunks stream $ - Foldable.foldMap decode >=> Either.Right >>> pure >=> done + let stream = HTTP.requestAsStream request + buf <- Ref.new "" + Stream.onDataString stream Encoding.UTF8 \str -> + void $ Ref.modify (_ <> str) buf + Stream.onEnd stream $ Ref.read buf >>= Either.Right >>> done pure Aff.nonCanceler