|
3 | 3 | const common = require('../common');
|
4 | 4 | const assert = require('assert');
|
5 | 5 | const { Duplex, Readable, Writable, pipeline, PassThrough } = require('stream');
|
| 6 | +const { ReadableStream, WritableStream } = require('stream/web'); |
6 | 7 | const { Blob } = require('buffer');
|
7 | 8 |
|
8 | 9 | {
|
@@ -299,3 +300,104 @@ const { Blob } = require('buffer');
|
299 | 300 | assert.strictEqual(res, 'foobar');
|
300 | 301 | })).on('close', common.mustCall());
|
301 | 302 | }
|
| 303 | + |
| 304 | +function makeATestReadableStream(value) { |
| 305 | + return new ReadableStream({ |
| 306 | + start(controller) { |
| 307 | + controller.enqueue(value); |
| 308 | + controller.close(); |
| 309 | + } |
| 310 | + }); |
| 311 | +} |
| 312 | + |
| 313 | +function makeATestWritableStream(writeFunc) { |
| 314 | + return new WritableStream({ |
| 315 | + write(chunk) { |
| 316 | + writeFunc(chunk); |
| 317 | + } |
| 318 | + }); |
| 319 | +} |
| 320 | + |
| 321 | +{ |
| 322 | + const d = Duplex.from({ |
| 323 | + readable: makeATestReadableStream('foo'), |
| 324 | + }); |
| 325 | + assert.strictEqual(d.readable, true); |
| 326 | + assert.strictEqual(d.writable, false); |
| 327 | + |
| 328 | + d.on('data', common.mustCall((data) => { |
| 329 | + assert.strictEqual(data.toString(), 'foo'); |
| 330 | + })); |
| 331 | + |
| 332 | + d.on('end', common.mustCall(() => { |
| 333 | + assert.strictEqual(d.readable, false); |
| 334 | + })); |
| 335 | +} |
| 336 | + |
| 337 | +{ |
| 338 | + const d = Duplex.from(makeATestReadableStream('foo')); |
| 339 | + |
| 340 | + assert.strictEqual(d.readable, true); |
| 341 | + assert.strictEqual(d.writable, false); |
| 342 | + |
| 343 | + d.on('data', common.mustCall((data) => { |
| 344 | + assert.strictEqual(data.toString(), 'foo'); |
| 345 | + })); |
| 346 | + |
| 347 | + d.on('end', common.mustCall(() => { |
| 348 | + assert.strictEqual(d.readable, false); |
| 349 | + })); |
| 350 | +} |
| 351 | + |
| 352 | +{ |
| 353 | + let ret = ''; |
| 354 | + const d = Duplex.from({ |
| 355 | + writable: makeATestWritableStream((chunk) => ret += chunk), |
| 356 | + }); |
| 357 | + |
| 358 | + assert.strictEqual(d.readable, false); |
| 359 | + assert.strictEqual(d.writable, true); |
| 360 | + |
| 361 | + d.end('foo'); |
| 362 | + d.on('finish', common.mustCall(() => { |
| 363 | + assert.strictEqual(ret, 'foo'); |
| 364 | + assert.strictEqual(d.writable, false); |
| 365 | + })); |
| 366 | +} |
| 367 | + |
| 368 | +{ |
| 369 | + let ret = ''; |
| 370 | + const d = Duplex.from(makeATestWritableStream((chunk) => ret += chunk)); |
| 371 | + |
| 372 | + assert.strictEqual(d.readable, false); |
| 373 | + assert.strictEqual(d.writable, true); |
| 374 | + |
| 375 | + d.end('foo'); |
| 376 | + d.on('finish', common.mustCall(() => { |
| 377 | + assert.strictEqual(ret, 'foo'); |
| 378 | + assert.strictEqual(d.writable, false); |
| 379 | + })); |
| 380 | +} |
| 381 | + |
| 382 | +{ |
| 383 | + let ret = ''; |
| 384 | + const d = Duplex.from({ |
| 385 | + readable: makeATestReadableStream('foo'), |
| 386 | + writable: makeATestWritableStream((chunk) => ret += chunk), |
| 387 | + }); |
| 388 | + |
| 389 | + d.end('bar'); |
| 390 | + |
| 391 | + d.on('data', common.mustCall((data) => { |
| 392 | + assert.strictEqual(data.toString(), 'foo'); |
| 393 | + })); |
| 394 | + |
| 395 | + d.on('end', common.mustCall(() => { |
| 396 | + assert.strictEqual(d.readable, false); |
| 397 | + })); |
| 398 | + |
| 399 | + d.on('finish', common.mustCall(() => { |
| 400 | + assert.strictEqual(ret, 'bar'); |
| 401 | + assert.strictEqual(d.writable, false); |
| 402 | + })); |
| 403 | +} |
0 commit comments