-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathtest-stream-transform-callback-null.js
More file actions
66 lines (55 loc) · 1.53 KB
/
test-stream-transform-callback-null.js
File metadata and controls
66 lines (55 loc) · 1.53 KB
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
'use strict';
const common = require('../common');
const assert = require('assert');
const { Transform } = require('stream');
// Passing null as the second argument to the transform callback should be
// equivalent to calling this.push(null), signaling the end of the readable
// side. Refs: https://github.com/nodejs/node/issues/62769
{
// Calling callback(null, null) should end the readable side of the transform stream.
const t = new Transform({
transform(chunk, encoding, callback) {
callback(null, null);
},
});
t.on('end', common.mustCall());
t.on('data', (chunk) => {
// Null sentinel should not appear as a data chunk.
assert.fail('unexpected data event');
});
t.write('hello');
t.end();
}
{
// Verify callback(null, data) still works normally.
const t = new Transform({
transform(chunk, encoding, callback) {
callback(null, chunk);
},
});
const received = [];
t.on('data', (chunk) => received.push(chunk.toString()));
t.on('end', common.mustCall(() => {
assert.deepStrictEqual(received, ['hello']);
}));
t.write('hello');
t.end();
}
{
// Verify callback() with no second arg still works (no push).
const t = new Transform({
transform(chunk, encoding, callback) {
callback();
},
flush(callback) {
callback(null, 'flushed');
},
});
const received = [];
t.on('data', (chunk) => received.push(chunk.toString()));
t.on('end', common.mustCall(() => {
assert.deepStrictEqual(received, ['flushed']);
}));
t.write('hello');
t.end();
}