• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --no-warnings --expose-internals
2'use strict';
3
4const common = require('../common');
5
6const assert = require('assert');
7
8const {
9  TransformStream,
10} = require('stream/web');
11
12const {
13  newStreamDuplexFromReadableWritablePair,
14} = require('internal/webstreams/adapters');
15
16const {
17  finished,
18  pipeline,
19  Readable,
20  Writable,
21} = require('stream');
22
23const {
24  kState,
25} = require('internal/webstreams/util');
26
27{
28  const transform = new TransformStream();
29  const duplex = newStreamDuplexFromReadableWritablePair(transform);
30
31  assert(transform.readable.locked);
32  assert(transform.writable.locked);
33
34  duplex.destroy();
35
36  duplex.on('close', common.mustCall(() => {
37    assert.strictEqual(transform.readable[kState].state, 'closed');
38    assert.strictEqual(transform.writable[kState].state, 'errored');
39  }));
40}
41
42{
43  const error = new Error('boom');
44  const transform = new TransformStream();
45  const duplex = newStreamDuplexFromReadableWritablePair(transform);
46
47  assert(transform.readable.locked);
48  assert(transform.writable.locked);
49
50  duplex.destroy(error);
51  duplex.on('error', common.mustCall((reason) => {
52    assert.strictEqual(reason, error);
53  }));
54
55  duplex.on('close', common.mustCall(() => {
56    assert.strictEqual(transform.readable[kState].state, 'closed');
57    assert.strictEqual(transform.writable[kState].state, 'errored');
58    assert.strictEqual(transform.writable[kState].storedError, error);
59  }));
60}
61
62{
63  const transform = new TransformStream();
64  const duplex = new newStreamDuplexFromReadableWritablePair(transform);
65
66  duplex.end();
67  duplex.resume();
68
69  duplex.on('close', common.mustCall(() => {
70    assert.strictEqual(transform.readable[kState].state, 'closed');
71    assert.strictEqual(transform.writable[kState].state, 'closed');
72  }));
73}
74
75{
76  const ec = new TextEncoder();
77  const dc = new TextDecoder();
78  const transform = new TransformStream({
79    transform(chunk, controller) {
80      const text = dc.decode(chunk);
81      controller.enqueue(ec.encode(text.toUpperCase()));
82    }
83  });
84  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
85    encoding: 'utf8',
86  });
87
88  duplex.end('hello');
89  duplex.on('data', common.mustCall((chunk) => {
90    assert.strictEqual(chunk, 'HELLO');
91  }));
92  duplex.on('end', common.mustCall());
93
94  duplex.on('close', common.mustCall(() => {
95    assert.strictEqual(transform.readable[kState].state, 'closed');
96    assert.strictEqual(transform.writable[kState].state, 'closed');
97  }));
98}
99
100{
101  const ec = new TextEncoder();
102  const dc = new TextDecoder();
103  const transform = new TransformStream({
104    transform: common.mustCall((chunk, controller) => {
105      const text = dc.decode(chunk);
106      controller.enqueue(ec.encode(text.toUpperCase()));
107    })
108  });
109  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
110    encoding: 'utf8',
111  });
112
113  finished(duplex, common.mustCall());
114
115  duplex.end('hello');
116  duplex.resume();
117}
118
119{
120  const ec = new TextEncoder();
121  const dc = new TextDecoder();
122  const transform = new TransformStream({
123    transform: common.mustCall((chunk, controller) => {
124      const text = dc.decode(chunk);
125      controller.enqueue(ec.encode(text.toUpperCase()));
126    })
127  });
128  const duplex = new newStreamDuplexFromReadableWritablePair(transform, {
129    encoding: 'utf8',
130  });
131
132  const readable = new Readable({
133    read() {
134      readable.push(Buffer.from('hello'));
135      readable.push(null);
136    }
137  });
138
139  const writable = new Writable({
140    write: common.mustCall((chunk, encoding, callback) => {
141      assert.strictEqual(dc.decode(chunk), 'HELLO');
142      assert.strictEqual(encoding, 'buffer');
143      callback();
144    })
145  });
146
147  finished(duplex, common.mustCall());
148  pipeline(readable, duplex, writable, common.mustCall());
149}
150
151{
152  const transform = new TransformStream();
153  const duplex = newStreamDuplexFromReadableWritablePair(transform);
154  duplex.setEncoding('utf-8');
155  duplex.on('data', common.mustCall((data) => {
156    assert.strictEqual(data, 'hello');
157  }, 5));
158
159  duplex.write(Buffer.from('hello'));
160  duplex.write(Buffer.from('hello'));
161  duplex.write(Buffer.from('hello'));
162  duplex.write(Buffer.from('hello'));
163  duplex.write(Buffer.from('hello'));
164
165  duplex.end();
166}
167