-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathtransform-stream.js
395 lines (312 loc) · 11.6 KB
/
transform-stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
const test = require('tape-catch');
const readableStreamToArray = require('./utils/readable-stream-to-array.js');
test('TransformStream can be constructed with a transform function', t => {
t.plan(1);
t.doesNotThrow(() => new TransformStream({ transform() { } }), 'TransformStream constructed with no errors');
});
test('TransformStream cannot be constructed with no transform function', t => {
t.plan(2);
t.throws(() => new TransformStream(), /TypeError/, 'TransformStream cannot be constructed with no arguments');
t.throws(() => new TransformStream({}), /TypeError/, 'TransformStream cannot be constructed with an empty object');
});
test('TransformStream instances must have writable and readable properties of the correct types', t => {
t.plan(4);
const ts = new TransformStream({ transform() { } });
t.ok(Object.prototype.hasOwnProperty.call(ts, 'writable'), 'it has a writable property');
t.ok(ts.writable instanceof WritableStream, 'writable is an instance of WritableStream');
t.ok(Object.prototype.hasOwnProperty.call(ts, 'readable'), 'it has a readable property');
t.ok(ts.readable instanceof ReadableStream, 'readable is an instance of ReadableStream');
});
test('TransformStream writable starts in the writable state', t => {
t.plan(1);
const ts = new TransformStream({ transform() { } });
t.equal(ts.writable.state, 'writable', 'writable starts writable');
});
test('Pass-through sync TransformStream: can read from readable what is put into writable', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
enqueue(chunk);
done();
}
});
ts.writable.write('a');
t.equal(ts.writable.state, 'waiting', 'writable is waiting after one write');
ts.readable.getReader().read().then(result => {
t.deepEqual(result, { value: 'a', done: false },
'result from reading the readable is the same as was written to writable');
return ts.writable.ready.then(() => {
t.equal(ts.writable.state, 'writable', 'writable becomes writable again');
});
})
.catch(e => t.error(e));
});
test('Uppercaser sync TransformStream: can read from readable transformed version of what is put into writable', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
enqueue(chunk.toUpperCase());
done();
}
});
ts.writable.write('a');
t.equal(ts.writable.state, 'waiting', 'writable is waiting after one write');
ts.readable.getReader().read().then(result => {
t.deepEqual(result, { value: 'A', done: false },
'result from reading the readable is the transformation of what was written to writable');
return ts.writable.ready.then(() => {
t.equal(ts.writable.state, 'writable', 'writable becomes writable again');
});
})
.catch(e => t.error(e));
});
test('Uppercaser-doubler sync TransformStream: can read both chunks put into the readable', t => {
t.plan(4);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
enqueue(chunk.toUpperCase());
enqueue(chunk.toUpperCase());
done();
}
});
ts.writable.write('a');
t.equal(ts.writable.state, 'waiting', 'writable is waiting after one write');
const reader = ts.readable.getReader();
reader.read().then(result1 => {
t.deepEqual(result1, { value: 'A', done: false },
'the first chunk read is the transformation of the single chunk written');
return reader.read().then(result2 => {
t.deepEqual(result2, { value: 'A', done: false },
'the second chunk read is also the transformation of the single chunk written');
return ts.writable.ready.then(() => {
t.equal(ts.writable.state, 'writable', 'writable becomes writable again');
});
});
})
.catch(e => t.error(e));
});
test('Uppercaser async TransformStream: can read from readable transformed version of what is put into writable', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
setTimeout(() => enqueue(chunk.toUpperCase()), 10);
setTimeout(done, 50);
}
});
ts.writable.write('a');
t.equal(ts.writable.state, 'waiting', 'writable is waiting after one write');
ts.readable.getReader().read().then(result => {
t.deepEqual(result, { value: 'A', done: false },
'result from reading the readable is the transformation of what was written to writable');
return ts.writable.ready.then(() => {
t.equal(ts.writable.state, 'writable', 'writable becomes writable again');
});
})
.catch(e => t.error(e));
});
test('Uppercaser-doubler async TransformStream: can read both chunks put into the readable', t => {
t.plan(4);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
setTimeout(() => enqueue(chunk.toUpperCase()), 10);
setTimeout(() => enqueue(chunk.toUpperCase()), 50);
setTimeout(done, 90);
}
});
const reader = ts.readable.getReader();
ts.writable.write('a');
t.equal(ts.writable.state, 'waiting', 'writable is waiting after one write');
reader.read().then(result1 => {
t.deepEqual(result1, { value: 'A', done: false },
'the first chunk read is the transformation of the single chunk written');
return reader.read().then(result2 => {
t.deepEqual(result2, { value: 'A', done: false },
'the second chunk read is also the transformation of the single chunk written');
return ts.writable.ready.then(() => {
t.equal(ts.writable.state, 'writable', 'writable becomes writable again');
});
});
})
.catch(e => t.error(e));
});
test('TransformStream: by default, closing the writable closes the readable (when there are no queued writes)', t => {
t.plan(3);
const ts = new TransformStream({ transform() { } });
ts.writable.close();
t.equal(ts.writable.state, 'closing', 'writable is closing');
Promise.all([ts.writable.closed, ts.readable.getReader().closed]).then(() => {
t.pass('both writable and readable closed promises fulfill');
t.equal(ts.writable.state, 'closed', 'writable state becomes closed eventually');
})
.catch(e => t.error(e));
});
test('TransformStream: by default, closing the writable waits for transforms to finish before closing both', t => {
t.plan(4);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
setTimeout(done, 50);
}
});
ts.writable.write('a');
ts.writable.close();
t.equal(ts.writable.state, 'closing', 'writable is closing');
let rsClosed = false;
ts.readable.getReader().closed.then(() => {
rsClosed = true;
});
setTimeout(() => {
t.equal(rsClosed, false, 'readable is not closed after a tick');
ts.writable.closed.then(() => {
t.equal(ts.writable.state, 'closed', 'writable becomes closed eventually');
t.equal(rsClosed, true, 'readable is closed at that point');
})
.catch(e => t.error(e));
}, 0);
});
test('TransformStream: by default, closing the writable closes the readable after sync enqueues and async done', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
enqueue('x');
enqueue('y');
setTimeout(done, 50);
}
});
ts.writable.write('a');
ts.writable.close();
t.equal(ts.writable.state, 'closing', 'writable is closing');
ts.writable.closed.then(() => {
t.equal(ts.writable.state, 'closed', 'writable becomes closed eventually');
return readableStreamToArray(ts.readable).then(chunks => {
t.deepEqual(chunks, ['x', 'y'], 'both enqueued chunks can be read from the readable');
});
})
.catch(e => t.error(e));
});
test('TransformStream: by default, closing the writable closes the readable after async enqueues and async done', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
setTimeout(() => enqueue('x'), 10);
setTimeout(() => enqueue('y'), 50);
setTimeout(done, 90);
}
});
ts.writable.write('a');
ts.writable.close();
t.equal(ts.writable.state, 'closing', 'writable is closing');
ts.writable.closed.then(() => {
t.equal(ts.writable.state, 'closed', 'writable becomes closed eventually');
return readableStreamToArray(ts.readable).then(chunks => {
t.deepEqual(chunks, ['x', 'y'], 'both enqueued chunks can be read from the readable');
});
})
.catch(e => t.error(e));
});
test('TransformStream flush is called immediately when the writable is closed, if no writes are queued', t => {
t.plan(1);
let flushCalled = false;
const ts = new TransformStream({
transform() { },
flush(enqueue) {
flushCalled = true;
}
});
ts.writable.close().then(() => {
t.ok(flushCalled, 'closing the writable triggers the transform flush immediately');
});
});
test('TransformStream flush is called after all queued writes finish, once the writable is closed', t => {
t.plan(3);
let flushCalled = false;
const ts = new TransformStream({
transform(chunk, enqueue, done) {
setTimeout(done, 10);
},
flush(enqueue) {
flushCalled = true;
}
});
ts.writable.write('a');
ts.writable.close();
t.notOk(flushCalled, 'closing the writable does not immediately call flush if writes are not finished');
let rsClosed = false;
ts.readable.getReader().closed.then(() => {
rsClosed = true;
});
setTimeout(() => {
t.ok(flushCalled, 'flush is eventually called');
t.equal(rsClosed, false, 'if flush does not call close, the readable does not become closed');
}, 50);
});
test('TransformStream flush gets a chance to enqueue more into the readable', t => {
t.plan(2);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
done();
},
flush(enqueue) {
enqueue('x');
enqueue('y');
}
});
const reader = ts.readable.getReader();
ts.writable.write('a');
ts.writable.close();
reader.read().then(result1 => {
t.deepEqual(result1, { value: 'x', done: false }, 'the first chunk read is the first one enqueued in flush');
return reader.read().then(result2 => {
t.deepEqual(result2, { value: 'y', done: false }, 'the second chunk read is the second one enqueued in flush');
});
})
.catch(e => t.error(e));
});
test('TransformStream flush gets a chance to enqueue more into the readable, and can then async close', t => {
t.plan(3);
const ts = new TransformStream({
transform(chunk, enqueue, done) {
done();
},
flush(enqueue, close) {
enqueue('x');
enqueue('y');
setTimeout(close, 10);
}
});
const reader = ts.readable.getReader();
ts.writable.write('a');
ts.writable.close();
reader.read().then(result1 => {
t.deepEqual(result1, { value: 'x', done: false }, 'the first chunk read is the first one enqueued in flush');
return reader.read().then(result2 => {
t.deepEqual(result2, { value: 'y', done: false }, 'the second chunk read is the second one enqueued in flush');
});
})
.catch(e => t.error(e));
reader.closed.then(() => {
t.pass('readable reader becomes closed');
})
.catch(e => t.error(e));
});
test('Transform stream should call transformer methods as methods', t => {
t.plan(1);
const ts = new TransformStream({
suffix: '-suffix',
transform(chunk, enqueue, done) {
enqueue(chunk + this.suffix);
done();
},
flush(enqueue, close) {
enqueue('flushed' + this.suffix);
close();
}
});
ts.writable.write('a');
ts.writable.close();
ts.writable.closed.then(() => {
return readableStreamToArray(ts.readable).then(chunks => {
t.deepEqual(chunks, ['a-suffix', 'flushed-suffix'], 'both enqueued chunks have suffixes');
});
}, e => t.error(e));
});