11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci'use strict'; 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst Duplex = require('stream').Duplex; 261cb0ef41Sopenharmony_ciconst { ReadableStream, WritableStream } = require('stream/web'); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciconst stream = new Duplex({ objectMode: true }); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciassert(Duplex() instanceof Duplex); 311cb0ef41Sopenharmony_ciassert(stream._readableState.objectMode); 321cb0ef41Sopenharmony_ciassert(stream._writableState.objectMode); 331cb0ef41Sopenharmony_ciassert(stream.allowHalfOpen); 341cb0ef41Sopenharmony_ciassert.strictEqual(stream.listenerCount('end'), 0); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cilet written; 371cb0ef41Sopenharmony_cilet read; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_cistream._write = (obj, _, cb) => { 401cb0ef41Sopenharmony_ci written = obj; 411cb0ef41Sopenharmony_ci cb(); 421cb0ef41Sopenharmony_ci}; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_cistream._read = () => {}; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cistream.on('data', (obj) => { 471cb0ef41Sopenharmony_ci read = obj; 481cb0ef41Sopenharmony_ci}); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cistream.push({ val: 1 }); 511cb0ef41Sopenharmony_cistream.end({ val: 2 }); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ciprocess.on('exit', () => { 541cb0ef41Sopenharmony_ci assert.strictEqual(read.val, 1); 551cb0ef41Sopenharmony_ci assert.strictEqual(written.val, 2); 561cb0ef41Sopenharmony_ci}); 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci// Duplex.fromWeb 591cb0ef41Sopenharmony_ci{ 601cb0ef41Sopenharmony_ci const dataToRead = Buffer.from('hello'); 611cb0ef41Sopenharmony_ci const dataToWrite = Buffer.from('world'); 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci const readable = new ReadableStream({ 641cb0ef41Sopenharmony_ci start(controller) { 651cb0ef41Sopenharmony_ci controller.enqueue(dataToRead); 661cb0ef41Sopenharmony_ci }, 671cb0ef41Sopenharmony_ci }); 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci const writable = new WritableStream({ 701cb0ef41Sopenharmony_ci write: common.mustCall((chunk) => { 711cb0ef41Sopenharmony_ci assert.strictEqual(chunk, dataToWrite); 721cb0ef41Sopenharmony_ci }) 731cb0ef41Sopenharmony_ci }); 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ci const pair = { readable, writable }; 761cb0ef41Sopenharmony_ci const duplex = Duplex.fromWeb(pair); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ci duplex.write(dataToWrite); 791cb0ef41Sopenharmony_ci duplex.once('data', common.mustCall((chunk) => { 801cb0ef41Sopenharmony_ci assert.strictEqual(chunk, dataToRead); 811cb0ef41Sopenharmony_ci })); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci// Duplex.fromWeb - using utf8 and objectMode 851cb0ef41Sopenharmony_ci{ 861cb0ef41Sopenharmony_ci const dataToRead = 'hello'; 871cb0ef41Sopenharmony_ci const dataToWrite = 'world'; 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci const readable = new ReadableStream({ 901cb0ef41Sopenharmony_ci start(controller) { 911cb0ef41Sopenharmony_ci controller.enqueue(dataToRead); 921cb0ef41Sopenharmony_ci }, 931cb0ef41Sopenharmony_ci }); 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci const writable = new WritableStream({ 961cb0ef41Sopenharmony_ci write: common.mustCall((chunk) => { 971cb0ef41Sopenharmony_ci assert.strictEqual(chunk, dataToWrite); 981cb0ef41Sopenharmony_ci }) 991cb0ef41Sopenharmony_ci }); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci const pair = { 1021cb0ef41Sopenharmony_ci readable, 1031cb0ef41Sopenharmony_ci writable 1041cb0ef41Sopenharmony_ci }; 1051cb0ef41Sopenharmony_ci const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true }); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci duplex.write(dataToWrite); 1081cb0ef41Sopenharmony_ci duplex.once('data', common.mustCall((chunk) => { 1091cb0ef41Sopenharmony_ci assert.strictEqual(chunk, dataToRead); 1101cb0ef41Sopenharmony_ci })); 1111cb0ef41Sopenharmony_ci} 1121cb0ef41Sopenharmony_ci// Duplex.toWeb 1131cb0ef41Sopenharmony_ci{ 1141cb0ef41Sopenharmony_ci const dataToRead = Buffer.from('hello'); 1151cb0ef41Sopenharmony_ci const dataToWrite = Buffer.from('world'); 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci const duplex = Duplex({ 1181cb0ef41Sopenharmony_ci read() { 1191cb0ef41Sopenharmony_ci this.push(dataToRead); 1201cb0ef41Sopenharmony_ci this.push(null); 1211cb0ef41Sopenharmony_ci }, 1221cb0ef41Sopenharmony_ci write: common.mustCall((chunk) => { 1231cb0ef41Sopenharmony_ci assert.strictEqual(chunk, dataToWrite); 1241cb0ef41Sopenharmony_ci }) 1251cb0ef41Sopenharmony_ci }); 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci const { writable, readable } = Duplex.toWeb(duplex); 1281cb0ef41Sopenharmony_ci writable.getWriter().write(dataToWrite); 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci readable.getReader().read().then(common.mustCall((result) => { 1311cb0ef41Sopenharmony_ci assert.deepStrictEqual(Buffer.from(result.value), dataToRead); 1321cb0ef41Sopenharmony_ci })); 1331cb0ef41Sopenharmony_ci} 134