deno.land / std@0.166.0 / node / _tools / test / parallel / test-stream-duplex-from.js

test-stream-duplex-from.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
// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 18.12.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
'use strict';
const common = require('../common');const assert = require('assert');const { Duplex, Readable, Writable, pipeline } = require('stream');const { Blob } = require('buffer');
{ const d = Duplex.from({ readable: new Readable({ read() { this.push('asd'); this.push(null); } }) }); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ const d = Duplex.from(new Readable({ read() { this.push('asd'); this.push(null); } })); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ let ret = ''; const d = Duplex.from(new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } })); assert.strictEqual(d.readable, false); assert.strictEqual(d.writable, true); d.end('asd'); d.on('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ let ret = ''; const d = Duplex.from({ writable: new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } }) }); assert.strictEqual(d.readable, false); assert.strictEqual(d.writable, true); d.end('asd'); d.on('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ let ret = ''; const d = Duplex.from({ readable: new Readable({ read() { this.push('asd'); this.push(null); } }), writable: new Writable({ write(chunk, encoding, callback) { ret += chunk; callback(); } }) }); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, true); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); })); d.end('asd'); d.once('finish', common.mustCall(function() { assert.strictEqual(d.writable, false); assert.strictEqual(ret, 'asd'); }));}
{ const d = Duplex.from(Promise.resolve('asd')); assert.strictEqual(d.readable, true); assert.strictEqual(d.writable, false); d.once('readable', common.mustCall(function() { assert.strictEqual(d.read().toString(), 'asd'); })); d.once('end', common.mustCall(function() { assert.strictEqual(d.readable, false); }));}
{ // https://github.com/nodejs/node/issues/40497 pipeline( ['abc\ndef\nghi'], Duplex.from(async function * (source) { let rest = ''; for await (const chunk of source) { const lines = (rest + chunk.toString()).split('\n'); rest = lines.pop(); for (const line of lines) { yield line; } } yield rest; }), async function * (source) { // eslint-disable-line require-yield let ret = ''; for await (const x of source) { ret += x; } assert.strictEqual(ret, 'abcdefghi'); }, common.mustCall(() => {}), );}
// Ensure that isDuplexNodeStream was called{ const duplex = new Duplex(); assert.strictEqual(Duplex.from(duplex), duplex);}
// Ensure that Duplex.from works for blobs{ const blob = new Blob(['blob']); const expectedByteLength = blob.size; const duplex = Duplex.from(blob); duplex.on('data', common.mustCall((arrayBuffer) => { assert.strictEqual(arrayBuffer.byteLength, expectedByteLength); }));}
// Ensure that given a promise rejection it emits an error{ const myErrorMessage = 'myCustomError'; Duplex.from(Promise.reject(myErrorMessage)) .on('error', common.mustCall((error) => { assert.strictEqual(error, myErrorMessage); }));}
// Ensure that given a promise rejection on an async function it emits an error{ const myErrorMessage = 'myCustomError'; async function asyncFn() { return Promise.reject(myErrorMessage); }
Duplex.from(asyncFn) .on('error', common.mustCall((error) => { assert.strictEqual(error, myErrorMessage); }));}
// Ensure that Duplex.from throws an Invalid return value when function is void{ assert.throws(() => Duplex.from(() => {}), { code: 'ERR_INVALID_RETURN_VALUE', });}
// Ensure data if a sub object has a readable stream it's duplexified{ const msg = Buffer.from('hello'); const duplex = Duplex.from({ readable: Readable({ read() { this.push(msg); this.push(null); } }) }).on('data', common.mustCall((data) => { assert.strictEqual(data, msg); }));
assert.strictEqual(duplex.writable, false);}
// Ensure data if a sub object has a writable stream it's duplexified{ const msg = Buffer.from('hello'); const duplex = Duplex.from({ writable: Writable({ write: common.mustCall((data) => { assert.strictEqual(data, msg); }) }) });
duplex.write(msg); assert.strictEqual(duplex.readable, false);}
// Ensure data if a sub object has a writable and readable stream it's duplexified{ const msg = Buffer.from('hello');
const duplex = Duplex.from({ readable: Readable({ read() { this.push(msg); this.push(null); } }), writable: Writable({ write: common.mustCall((data) => { assert.strictEqual(data, msg); }) }) });
duplex.pipe(duplex) .on('data', common.mustCall((data) => { assert.strictEqual(data, msg); assert.strictEqual(duplex.readable, true); assert.strictEqual(duplex.writable, true); })) .on('end', common.mustCall());}
// Ensure that given readable stream that throws an error it calls destroy{ const myErrorMessage = 'error!'; const duplex = Duplex.from(Readable({ read() { throw new Error(myErrorMessage); } })); duplex.on('error', common.mustCall((msg) => { assert.strictEqual(msg.message, myErrorMessage); }));}
// Ensure that given writable stream that throws an error it calls destroy{ const myErrorMessage = 'error!'; const duplex = Duplex.from(Writable({ write(chunk, enc, cb) { cb(myErrorMessage); } }));
duplex.on('error', common.mustCall((msg) => { assert.strictEqual(msg, myErrorMessage); }));
duplex.write('test');}
std

Version Info

Tagged at
a year ago