Actually fix a really minor bug in body read function (#121)

This commit is contained in:
paluh 2018-09-25 03:36:06 +02:00 committed by Connor Prussin
parent c094792be6
commit 2f335e9fc0
2 changed files with 6 additions and 37 deletions

View File

@ -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)();
});
};
}
};

View File

@ -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