Skip to content

Commit 6c871e1

Browse files
committed
Adapt writeString to aff-4.0.0
1 parent b0443cf commit 6c871e1

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

src/Hyper/Node/Server.purs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,24 @@ derive instance newtypeNodeResponse ∷ Newtype (NodeResponse m e) _
7171

7272
writeString :: forall m e. MonadAff e m => Encoding -> String -> NodeResponse m e
7373
writeString enc str =
74-
NodeResponse $ \w -> liftAff (makeAff (writeChunk w 0))
74+
NodeResponse $ \w -> liftAff (writeString' w 0)
7575
where
76-
-- We operate on a (Array Char), instead of the String, to slice
77-
-- it into chunks:
7876
chars = String.toCharArray str
7977
totalLength = Array.length chars
80-
-- This function writes a chunk at the given offset, and recurses
81-
-- if there are more chunks to be written.
82-
writeChunk w offset fail succeed = do
83-
-- We ignore the result of write as we cannot access the 'drain'
84-
-- event anyway:
85-
void $
86-
Stream.writeString w enc (String.fromCharArray chunk) $
87-
-- In the callback from `writeString`, we succeed the Aff
88-
-- value if we have just written the last chunk, otherwise we
89-
-- keep on writing chunks.
90-
if isLastChunk
91-
then succeed unit
92-
else writeChunk w nextOffset fail succeed
93-
where
94-
chunkSize = 1024 * 8
78+
chunkSize = 1024 * 8
79+
80+
writeString' w offset = do
81+
let
9582
nextOffset = min totalLength (offset + chunkSize)
9683
isLastChunk = nextOffset == totalLength
9784
chunk = Array.slice offset nextOffset chars
85+
writeChunk w chunk
86+
if isLastChunk
87+
then pure unit
88+
else writeString' w nextOffset
89+
90+
writeChunk w chunk = makeAff (\k → do
91+
Stream.writeString w enc (String.fromCharArray chunk) (k (Right unit)) *> pure nonCanceler)
9892

9993
write :: forall m e. MonadAff e m => Buffer -> NodeResponse m e
10094
write buffer = NodeResponse $ \w ->

test/Hyper/Node/ServerSpec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var stream = require('stream'),
77
function MemoryWritableStream(writeDelay, options) {
88
if (!(this instanceof MemoryWritableStream))
99
return new MemoryWritableStream(options);
10-
this.writeDelay = 500;
10+
this.writeDelay = writeDelay;
1111
this.output = new buffer.Buffer([]);
1212
stream.Writable.call(this, options);
1313
}
@@ -16,7 +16,8 @@ util.inherits(MemoryWritableStream, stream.Writable);
1616
MemoryWritableStream.prototype._write = function(chunk, encoding, cb) {
1717
var that = this;
1818
setTimeout(function() {
19-
that.output.concat([chunk]);
19+
that.output = Buffer.concat([that.output, chunk]);
20+
cb();
2021
}, this.writeDelay);
2122
};
2223

test/Hyper/Node/ServerSpec.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ foreign import streamBufferImpl ∷ ∀ eff. EffFn1 eff (WritableStream eff) Buf
3535
streamBuffer eff. WritableStream eff Eff eff Buffer
3636
streamBuffer = runEffFn1 streamBufferImpl
3737

38-
testString :: forall eff. Int -> Aff eff String
39-
testString totalLength = do
38+
buildTestString :: forall eff. Int -> Aff eff String
39+
buildTestString totalLength = do
4040
let
4141
str = go "a" 1
4242
length str `shouldEqual` totalLength
@@ -51,18 +51,18 @@ spec =
5151
describe "Hyper.Node.Server" do
5252
describe "writeString" do
5353
it "handles empty string" do
54-
output ← liftEff $ memoryWritableStream 100
54+
output ← liftEff $ memoryWritableStream 10
5555
let writer = unwrap $ writeString UTF8 ""
5656
writer (toWritable output)
5757

5858
it "handles short (< 16 KB) string" do
59-
output ← liftEff $ memoryWritableStream 100
60-
str ← testString 3000
59+
output ← liftEff $ memoryWritableStream 10
60+
str ← buildTestString 3000
6161
let writer = unwrap $ writeString UTF8 str
6262
writer (toWritable output)
6363

6464
it "handles long (> 64 KB) string" do
65-
str ← testString 65000
66-
output ← liftEff $ memoryWritableStream 100
65+
str ← buildTestString 65000
66+
output ← liftEff $ memoryWritableStream 10
6767
let writer = unwrap $ writeString UTF8 str
6868
writer (toWritable output)

0 commit comments

Comments
 (0)