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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
require('../common');
const assert = require('assert');
const PassThrough = require('_stream_passthrough');
const Transform = require('_stream_transform');
// tiny node-tap lookalike.
const tests = [];
let count = 0;
function test(name, fn) {
count++;
tests.push([name, fn]);
}
function run() {
const next = tests.shift();
if (!next)
return console.error('ok');
const name = next[0];
const fn = next[1];
console.log('# %s', name);
fn({
same: assert.deepStrictEqual,
equal: assert.strictEqual,
ok: assert,
end: function() {
count--;
run();
}
});
}
// ensure all tests have run
process.on('exit', function() {
assert.strictEqual(count, 0);
});
process.nextTick(run);
/////
test('writable side consumption', function(t) {
const tx = new Transform({
highWaterMark: 10
});
let transformed = 0;
tx._transform = function(chunk, encoding, cb) {
transformed += chunk.length;
tx.push(chunk);
cb();
};
for (let i = 1; i <= 10; i++) {
tx.write(Buffer.allocUnsafe(i));
}
tx.end();
t.equal(tx._readableState.length, 10);
t.equal(transformed, 10);
t.equal(tx._transformState.writechunk.length, 5);
t.same(tx._writableState.getBuffer().map(function(c) {
return c.chunk.length;
}), [6, 7, 8, 9, 10]);
t.end();
});
test('passthrough', function(t) {
const pt = new PassThrough();
pt.write(Buffer.from('foog'));
pt.write(Buffer.from('bark'));
pt.write(Buffer.from('bazy'));
pt.write(Buffer.from('kuel'));
pt.end();
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'l');
t.end();
});
test('object passthrough', function(t) {
const pt = new PassThrough({ objectMode: true });
pt.write(1);
pt.write(true);
pt.write(false);
pt.write(0);
pt.write('foo');
pt.write('');
pt.write({ a: 'b'});
pt.end();
t.equal(pt.read(), 1);
t.equal(pt.read(), true);
t.equal(pt.read(), false);
t.equal(pt.read(), 0);
t.equal(pt.read(), 'foo');
t.equal(pt.read(), '');
t.same(pt.read(), { a: 'b'});
t.end();
});
test('passthrough constructor', function(t) {
const pt = PassThrough();
assert(pt instanceof PassThrough);
t.end();
});
test('simple transform', function(t) {
const pt = new Transform();
pt._transform = function(c, e, cb) {
const ret = Buffer.alloc(c.length, 'x');
pt.push(ret);
cb();
};
pt.write(Buffer.from('foog'));
pt.write(Buffer.from('bark'));
pt.write(Buffer.from('bazy'));
pt.write(Buffer.from('kuel'));
pt.end();
t.equal(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'xxxxx');
t.equal(pt.read(5).toString(), 'x');
t.end();
});
test('simple object transform', function(t) {
const pt = new Transform({ objectMode: true });
pt._transform = function(c, e, cb) {
pt.push(JSON.stringify(c));
cb();
};
pt.write(1);
pt.write(true);
pt.write(false);
pt.write(0);
pt.write('foo');
pt.write('');
pt.write({ a: 'b'});
pt.end();
t.equal(pt.read(), '1');
t.equal(pt.read(), 'true');
t.equal(pt.read(), 'false');
t.equal(pt.read(), '0');
t.equal(pt.read(), '"foo"');
t.equal(pt.read(), '""');
t.equal(pt.read(), '{"a":"b"}');
t.end();
});
test('async passthrough', function(t) {
const pt = new Transform();
pt._transform = function(chunk, encoding, cb) {
setTimeout(function() {
pt.push(chunk);
cb();
}, 10);
};
pt.write(Buffer.from('foog'));
pt.write(Buffer.from('bark'));
pt.write(Buffer.from('bazy'));
pt.write(Buffer.from('kuel'));
pt.end();
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5).toString(), 'l');
t.end();
});
});
test('assymetric transform (expand)', function(t) {
const pt = new Transform();
// emit each chunk 2 times.
pt._transform = function(chunk, encoding, cb) {
setTimeout(function() {
pt.push(chunk);
setTimeout(function() {
pt.push(chunk);
cb();
}, 10);
}, 10);
};
pt.write(Buffer.from('foog'));
pt.write(Buffer.from('bark'));
pt.write(Buffer.from('bazy'));
pt.write(Buffer.from('kuel'));
pt.end();
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'foogf');
t.equal(pt.read(5).toString(), 'oogba');
t.equal(pt.read(5).toString(), 'rkbar');
t.equal(pt.read(5).toString(), 'kbazy');
t.equal(pt.read(5).toString(), 'bazyk');
t.equal(pt.read(5).toString(), 'uelku');
t.equal(pt.read(5).toString(), 'el');
t.end();
});
});
test('assymetric transform (compress)', function(t) {
const pt = new Transform();
// each output is the first char of 3 consecutive chunks,
// or whatever's left.
pt.state = '';
pt._transform = function(chunk, encoding, cb) {
if (!chunk)
chunk = '';
const s = chunk.toString();
setTimeout(function() {
this.state += s.charAt(0);
if (this.state.length === 3) {
pt.push(Buffer.from(this.state));
this.state = '';
}
cb();
}.bind(this), 10);
};
pt._flush = function(cb) {
// just output whatever we have.
pt.push(Buffer.from(this.state));
this.state = '';
cb();
};
pt.write(Buffer.from('aaaa'));
pt.write(Buffer.from('bbbb'));
pt.write(Buffer.from('cccc'));
pt.write(Buffer.from('dddd'));
pt.write(Buffer.from('eeee'));
pt.write(Buffer.from('aaaa'));
pt.write(Buffer.from('bbbb'));
pt.write(Buffer.from('cccc'));
pt.write(Buffer.from('dddd'));
pt.write(Buffer.from('eeee'));
pt.write(Buffer.from('aaaa'));
pt.write(Buffer.from('bbbb'));
pt.write(Buffer.from('cccc'));
pt.write(Buffer.from('dddd'));
pt.end();
// 'abcdeabcdeabcd'
pt.on('finish', function() {
t.equal(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcde');
t.equal(pt.read(5).toString(), 'abcd');
t.end();
});
});
// this tests for a stall when data is written to a full stream
// that has empty transforms.
test('complex transform', function(t) {
let count = 0;
let saved = null;
const pt = new Transform({highWaterMark: 3});
pt._transform = function(c, e, cb) {
if (count++ === 1)
saved = c;
else {
if (saved) {
pt.push(saved);
saved = null;
}
pt.push(c);
}
cb();
};
pt.once('readable', function() {
process.nextTick(function() {
pt.write(Buffer.from('d'));
pt.write(Buffer.from('ef'), function() {
pt.end();
t.end();
});
t.equal(pt.read().toString(), 'abcdef');
t.equal(pt.read(), null);
});
});
pt.write(Buffer.from('abc'));
});
test('passthrough event emission', function(t) {
const pt = new PassThrough();
let emits = 0;
pt.on('readable', function() {
console.error('>>> emit readable %d', emits);
emits++;
});
pt.write(Buffer.from('foog'));
console.error('need emit 0');
pt.write(Buffer.from('bark'));
console.error('should have emitted readable now 1 === %d', emits);
t.equal(emits, 1);
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5) + '', 'null');
console.error('need emit 1');
pt.write(Buffer.from('bazy'));
console.error('should have emitted, but not again');
pt.write(Buffer.from('kuel'));
console.error('should have emitted readable now 2 === %d', emits);
t.equal(emits, 2);
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5), null);
console.error('need emit 2');
pt.end();
t.equal(emits, 3);
t.equal(pt.read(5).toString(), 'l');
t.equal(pt.read(5), null);
console.error('should not have emitted again');
t.equal(emits, 3);
t.end();
});
test('passthrough event emission reordered', function(t) {
const pt = new PassThrough();
let emits = 0;
pt.on('readable', function() {
console.error('emit readable', emits);
emits++;
});
pt.write(Buffer.from('foog'));
console.error('need emit 0');
pt.write(Buffer.from('bark'));
console.error('should have emitted readable now 1 === %d', emits);
t.equal(emits, 1);
t.equal(pt.read(5).toString(), 'foogb');
t.equal(pt.read(5), null);
console.error('need emit 1');
pt.once('readable', function() {
t.equal(pt.read(5).toString(), 'arkba');
t.equal(pt.read(5), null);
console.error('need emit 2');
pt.once('readable', function() {
t.equal(pt.read(5).toString(), 'zykue');
t.equal(pt.read(5), null);
pt.once('readable', function() {
t.equal(pt.read(5).toString(), 'l');
t.equal(pt.read(5), null);
t.equal(emits, 4);
t.end();
});
pt.end();
});
pt.write(Buffer.from('kuel'));
});
pt.write(Buffer.from('bazy'));
});
test('passthrough facaded', function(t) {
console.error('passthrough facaded');
const pt = new PassThrough();
const datas = [];
pt.on('data', function(chunk) {
datas.push(chunk.toString());
});
pt.on('end', function() {
t.same(datas, ['foog', 'bark', 'bazy', 'kuel']);
t.end();
});
pt.write(Buffer.from('foog'));
setTimeout(function() {
pt.write(Buffer.from('bark'));
setTimeout(function() {
pt.write(Buffer.from('bazy'));
setTimeout(function() {
pt.write(Buffer.from('kuel'));
setTimeout(function() {
pt.end();
}, 10);
}, 10);
}, 10);
}, 10);
});
test('object transform (json parse)', function(t) {
console.error('json parse stream');
const jp = new Transform({ objectMode: true });
jp._transform = function(data, encoding, cb) {
try {
jp.push(JSON.parse(data));
cb();
} catch (er) {
cb(er);
}
};
// anything except null/undefined is fine.
// those are "magic" in the stream API, because they signal EOF.
const objects = [
{ foo: 'bar' },
100,
'string',
{ nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }
];
let ended = false;
jp.on('end', function() {
ended = true;
});
objects.forEach(function(obj) {
jp.write(JSON.stringify(obj));
const res = jp.read();
t.same(res, obj);
});
jp.end();
// read one more time to get the 'end' event
jp.read();
process.nextTick(function() {
t.ok(ended);
t.end();
});
});
test('object transform (json stringify)', function(t) {
console.error('json parse stream');
const js = new Transform({ objectMode: true });
js._transform = function(data, encoding, cb) {
try {
js.push(JSON.stringify(data));
cb();
} catch (er) {
cb(er);
}
};
// anything except null/undefined is fine.
// those are "magic" in the stream API, because they signal EOF.
const objects = [
{ foo: 'bar' },
100,
'string',
{ nested: { things: [ { foo: 'bar' }, 100, 'string' ] } }
];
let ended = false;
js.on('end', function() {
ended = true;
});
objects.forEach(function(obj) {
js.write(obj);
const res = js.read();
t.equal(res, JSON.stringify(obj));
});
js.end();
// read one more time to get the 'end' event
js.read();
process.nextTick(function() {
t.ok(ended);
t.end();
});
});
|