* 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 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
|
||||
import Node.HTTP as HTTP
|
||||
import Node.Stream as Stream
|
||||
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
|
||||
-- | response, and can be used with all the response helpers.
|
||||
class Body b where
|
||||
@ -79,12 +78,16 @@ instance bodyChunked ::
|
||||
Stream.onEnd stream $ done $ Either.Right unit
|
||||
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`.
|
||||
read :: HTTP.Request -> Aff.Aff String
|
||||
read request = Aff.makeAff \done -> do
|
||||
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
|
||||
let
|
||||
stream = HTTP.requestAsStream request
|
||||
decode = Buffer.toString Encoding.UTF8
|
||||
aggregateChunks stream $
|
||||
Foldable.foldMap decode >=> Either.Right >>> pure >=> done
|
||||
pure Aff.nonCanceler
|
||||
|
Loading…
Reference in New Issue
Block a user