forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-stream-pipe-objectmode-to-non-objectmode.js
More file actions
73 lines (59 loc) · 1.88 KB
/
test-stream-pipe-objectmode-to-non-objectmode.js
File metadata and controls
73 lines (59 loc) · 1.88 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
67
68
69
70
71
72
73
'use strict';
const common = require('../common');
const assert = require('node:assert');
const { Readable, Transform, Writable } = require('node:stream');
// Piping objects from object mode to non-object mode in a pipeline should throw
// an error and catch by the consumer
{
const objectReadable = Readable.from([
{ hello: 'hello' },
{ world: 'world' },
]);
const passThrough = new Transform({
transform(chunk, _encoding, cb) {
this.push(chunk);
cb(null);
},
});
passThrough.on('error', common.mustCall());
objectReadable.pipe(passThrough);
assert.rejects(async () => {
// eslint-disable-next-line no-unused-vars
for await (const _ of passThrough);
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall());
}
// The error should be properly forwarded when the readable stream is in object mode,
// the writable stream is in non-object mode, and the data is string.
{
const stringReadable = Readable.from(['hello', 'world']);
const passThrough = new Transform({
transform(chunk, _encoding, cb) {
this.push(chunk);
throw new Error('something went wrong');
},
});
passThrough.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'something went wrong');
}));
stringReadable.pipe(passThrough);
}
// The error should be properly forwarded when the readable stream is in object mode,
// the writable stream is in non-object mode, and the data is buffer.
{
const binaryData = Buffer.from('binary data');
const binaryReadable = new Readable({
read() {
this.push(binaryData);
this.push(null);
}
});
const binaryWritable = new Writable({
write(chunk, _encoding, cb) {
throw new Error('something went wrong');
}
});
binaryWritable.on('error', common.mustCall((err) => {
assert.strictEqual(err.message, 'something went wrong');
}));
binaryReadable.pipe(binaryWritable);
}