11cb0ef41Sopenharmony_ci// Flags: --no-warnings --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  TransformStream,
101cb0ef41Sopenharmony_ci} = require('stream/web');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst {
131cb0ef41Sopenharmony_ci  newStreamDuplexFromReadableWritablePair,
141cb0ef41Sopenharmony_ci} = require('internal/webstreams/adapters');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst {
171cb0ef41Sopenharmony_ci  finished,
181cb0ef41Sopenharmony_ci  pipeline,
191cb0ef41Sopenharmony_ci  Readable,
201cb0ef41Sopenharmony_ci  Writable,
211cb0ef41Sopenharmony_ci} = require('stream');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst {
241cb0ef41Sopenharmony_ci  kState,
251cb0ef41Sopenharmony_ci} = require('internal/webstreams/util');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci{
281cb0ef41Sopenharmony_ci  const transform = new TransformStream();
291cb0ef41Sopenharmony_ci  const duplex = newStreamDuplexFromReadableWritablePair(transform);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  assert(transform.readable.locked);
321cb0ef41Sopenharmony_ci  assert(transform.writable.locked);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  duplex.destroy();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall(() => {
371cb0ef41Sopenharmony_ci    assert.strictEqual(transform.readable[kState].state, 'closed');
381cb0ef41Sopenharmony_ci    assert.strictEqual(transform.writable[kState].state, 'errored');
391cb0ef41Sopenharmony_ci  }));
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci  const error = new Error('boom');
441cb0ef41Sopenharmony_ci  const transform = new TransformStream();
451cb0ef41Sopenharmony_ci  const duplex = newStreamDuplexFromReadableWritablePair(transform);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  assert(transform.readable.locked);
481cb0ef41Sopenharmony_ci  assert(transform.writable.locked);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  duplex.destroy(error);
511cb0ef41Sopenharmony_ci  duplex.on('error', common.mustCall((reason) => {
521cb0ef41Sopenharmony_ci    assert.strictEqual(reason, error);
531cb0ef41Sopenharmony_ci  }));
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall(() => {
561cb0ef41Sopenharmony_ci    assert.strictEqual(transform.readable[kState].state, 'closed');
571cb0ef41Sopenharmony_ci    assert.strictEqual(transform.writable[kState].state, 'errored');
581cb0ef41Sopenharmony_ci    assert.strictEqual(transform.writable[kState].storedError, error);
591cb0ef41Sopenharmony_ci  }));
601cb0ef41Sopenharmony_ci}
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci{
631cb0ef41Sopenharmony_ci  const transform = new TransformStream();
641cb0ef41Sopenharmony_ci  const duplex = new newStreamDuplexFromReadableWritablePair(transform);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  duplex.end();
671cb0ef41Sopenharmony_ci  duplex.resume();
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall(() => {
701cb0ef41Sopenharmony_ci    assert.strictEqual(transform.readable[kState].state, 'closed');
711cb0ef41Sopenharmony_ci    assert.strictEqual(transform.writable[kState].state, 'closed');
721cb0ef41Sopenharmony_ci  }));
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci{
761cb0ef41Sopenharmony_ci  const ec = new TextEncoder();
771cb0ef41Sopenharmony_ci  const dc = new TextDecoder();
781cb0ef41Sopenharmony_ci  const transform = new TransformStream({
791cb0ef41Sopenharmony_ci    transform(chunk, controller) {
801cb0ef41Sopenharmony_ci      const text = dc.decode(chunk);
811cb0ef41Sopenharmony_ci      controller.enqueue(ec.encode(text.toUpperCase()));
821cb0ef41Sopenharmony_ci    }
831cb0ef41Sopenharmony_ci  });
841cb0ef41Sopenharmony_ci  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
851cb0ef41Sopenharmony_ci    encoding: 'utf8',
861cb0ef41Sopenharmony_ci  });
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci  duplex.end('hello');
891cb0ef41Sopenharmony_ci  duplex.on('data', common.mustCall((chunk) => {
901cb0ef41Sopenharmony_ci    assert.strictEqual(chunk, 'HELLO');
911cb0ef41Sopenharmony_ci  }));
921cb0ef41Sopenharmony_ci  duplex.on('end', common.mustCall());
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  duplex.on('close', common.mustCall(() => {
951cb0ef41Sopenharmony_ci    assert.strictEqual(transform.readable[kState].state, 'closed');
961cb0ef41Sopenharmony_ci    assert.strictEqual(transform.writable[kState].state, 'closed');
971cb0ef41Sopenharmony_ci  }));
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci{
1011cb0ef41Sopenharmony_ci  const ec = new TextEncoder();
1021cb0ef41Sopenharmony_ci  const dc = new TextDecoder();
1031cb0ef41Sopenharmony_ci  const transform = new TransformStream({
1041cb0ef41Sopenharmony_ci    transform: common.mustCall((chunk, controller) => {
1051cb0ef41Sopenharmony_ci      const text = dc.decode(chunk);
1061cb0ef41Sopenharmony_ci      controller.enqueue(ec.encode(text.toUpperCase()));
1071cb0ef41Sopenharmony_ci    })
1081cb0ef41Sopenharmony_ci  });
1091cb0ef41Sopenharmony_ci  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
1101cb0ef41Sopenharmony_ci    encoding: 'utf8',
1111cb0ef41Sopenharmony_ci  });
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  finished(duplex, common.mustCall());
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  duplex.end('hello');
1161cb0ef41Sopenharmony_ci  duplex.resume();
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci{
1201cb0ef41Sopenharmony_ci  const ec = new TextEncoder();
1211cb0ef41Sopenharmony_ci  const dc = new TextDecoder();
1221cb0ef41Sopenharmony_ci  const transform = new TransformStream({
1231cb0ef41Sopenharmony_ci    transform: common.mustCall((chunk, controller) => {
1241cb0ef41Sopenharmony_ci      const text = dc.decode(chunk);
1251cb0ef41Sopenharmony_ci      controller.enqueue(ec.encode(text.toUpperCase()));
1261cb0ef41Sopenharmony_ci    })
1271cb0ef41Sopenharmony_ci  });
1281cb0ef41Sopenharmony_ci  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
1291cb0ef41Sopenharmony_ci    encoding: 'utf8',
1301cb0ef41Sopenharmony_ci  });
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  const readable = new Readable({
1331cb0ef41Sopenharmony_ci    read() {
1341cb0ef41Sopenharmony_ci      readable.push(Buffer.from('hello'));
1351cb0ef41Sopenharmony_ci      readable.push(null);
1361cb0ef41Sopenharmony_ci    }
1371cb0ef41Sopenharmony_ci  });
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  const writable = new Writable({
1401cb0ef41Sopenharmony_ci    write: common.mustCall((chunk, encoding, callback) => {
1411cb0ef41Sopenharmony_ci      assert.strictEqual(dc.decode(chunk), 'HELLO');
1421cb0ef41Sopenharmony_ci      assert.strictEqual(encoding, 'buffer');
1431cb0ef41Sopenharmony_ci      callback();
1441cb0ef41Sopenharmony_ci    })
1451cb0ef41Sopenharmony_ci  });
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci  finished(duplex, common.mustCall());
1481cb0ef41Sopenharmony_ci  pipeline(readable, duplex, writable, common.mustCall());
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci{
1521cb0ef41Sopenharmony_ci  const transform = new TransformStream();
1531cb0ef41Sopenharmony_ci  const duplex = newStreamDuplexFromReadableWritablePair(transform);
1541cb0ef41Sopenharmony_ci  duplex.setEncoding('utf-8');
1551cb0ef41Sopenharmony_ci  duplex.on('data', common.mustCall((data) => {
1561cb0ef41Sopenharmony_ci    assert.strictEqual(data, 'hello');
1571cb0ef41Sopenharmony_ci  }, 5));
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci  duplex.write(Buffer.from('hello'));
1601cb0ef41Sopenharmony_ci  duplex.write(Buffer.from('hello'));
1611cb0ef41Sopenharmony_ci  duplex.write(Buffer.from('hello'));
1621cb0ef41Sopenharmony_ci  duplex.write(Buffer.from('hello'));
1631cb0ef41Sopenharmony_ci  duplex.write(Buffer.from('hello'));
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  duplex.end();
1661cb0ef41Sopenharmony_ci}
167