• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21'use strict';
22
23const common = require('../common');
24const assert = require('assert');
25const Duplex = require('stream').Duplex;
26const { ReadableStream, WritableStream } = require('stream/web');
27
28const stream = new Duplex({ objectMode: true });
29
30assert(Duplex() instanceof Duplex);
31assert(stream._readableState.objectMode);
32assert(stream._writableState.objectMode);
33assert(stream.allowHalfOpen);
34assert.strictEqual(stream.listenerCount('end'), 0);
35
36let written;
37let read;
38
39stream._write = (obj, _, cb) => {
40  written = obj;
41  cb();
42};
43
44stream._read = () => {};
45
46stream.on('data', (obj) => {
47  read = obj;
48});
49
50stream.push({ val: 1 });
51stream.end({ val: 2 });
52
53process.on('exit', () => {
54  assert.strictEqual(read.val, 1);
55  assert.strictEqual(written.val, 2);
56});
57
58// Duplex.fromWeb
59{
60  const dataToRead = Buffer.from('hello');
61  const dataToWrite = Buffer.from('world');
62
63  const readable = new ReadableStream({
64    start(controller) {
65      controller.enqueue(dataToRead);
66    },
67  });
68
69  const writable = new WritableStream({
70    write: common.mustCall((chunk) => {
71      assert.strictEqual(chunk, dataToWrite);
72    })
73  });
74
75  const pair = { readable, writable };
76  const duplex = Duplex.fromWeb(pair);
77
78  duplex.write(dataToWrite);
79  duplex.once('data', common.mustCall((chunk) => {
80    assert.strictEqual(chunk, dataToRead);
81  }));
82}
83
84// Duplex.fromWeb - using utf8 and objectMode
85{
86  const dataToRead = 'hello';
87  const dataToWrite = 'world';
88
89  const readable = new ReadableStream({
90    start(controller) {
91      controller.enqueue(dataToRead);
92    },
93  });
94
95  const writable = new WritableStream({
96    write: common.mustCall((chunk) => {
97      assert.strictEqual(chunk, dataToWrite);
98    })
99  });
100
101  const pair = {
102    readable,
103    writable
104  };
105  const duplex = Duplex.fromWeb(pair, { encoding: 'utf8', objectMode: true });
106
107  duplex.write(dataToWrite);
108  duplex.once('data', common.mustCall((chunk) => {
109    assert.strictEqual(chunk, dataToRead);
110  }));
111}
112// Duplex.toWeb
113{
114  const dataToRead = Buffer.from('hello');
115  const dataToWrite = Buffer.from('world');
116
117  const duplex = Duplex({
118    read() {
119      this.push(dataToRead);
120      this.push(null);
121    },
122    write: common.mustCall((chunk) => {
123      assert.strictEqual(chunk, dataToWrite);
124    })
125  });
126
127  const { writable, readable } = Duplex.toWeb(duplex);
128  writable.getWriter().write(dataToWrite);
129
130  readable.getReader().read().then(common.mustCall((result) => {
131    assert.deepStrictEqual(Buffer.from(result.value), dataToRead);
132  }));
133}
134