1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Flags: --expose-internals --no-warnings
'use strict';
const common = require('../common');
const assert = require('assert');
const {
ReadableStream,
TransformStream,
TransformStreamDefaultController,
} = require('stream/web');
const {
createReadStream,
readFileSync,
} = require('fs');
const {
kTransfer,
} = require('internal/worker/js_transferable');
const {
inspect,
} = require('util');
assert.throws(() => new TransformStream({ readableType: 1 }), {
code: 'ERR_INVALID_ARG_VALUE',
});
assert.throws(() => new TransformStream({ writableType: 1 }), {
code: 'ERR_INVALID_ARG_VALUE',
});
{
const stream = new TransformStream();
async function test(stream) {
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
const { 1: result } = await Promise.all([
writer.write('hello'),
reader.read(),
]);
assert.strictEqual(result.value, 'hello');
}
test(stream).then(common.mustCall());
}
class Transform {
start(controller) {
this.started = true;
}
async transform(chunk, controller) {
controller.enqueue(chunk.toUpperCase());
}
async flush() {
this.flushed = true;
}
}
{
const transform = new Transform();
const stream = new TransformStream(transform);
assert(transform.started);
async function test(stream) {
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
const { 1: result } = await Promise.all([
writer.write('hello'),
reader.read(),
]);
assert.strictEqual(result.value, 'HELLO');
await writer.close();
}
test(stream).then(common.mustCall(() => {
assert(transform.flushed);
}));
}
class Source {
constructor() {
this.cancelCalled = false;
}
start(controller) {
this.stream = createReadStream(__filename);
this.stream.on('data', (chunk) => {
controller.enqueue(chunk.toString());
});
this.stream.once('end', () => {
if (!this.cancelCalled)
controller.close();
});
this.stream.once('error', (error) => {
controller.error(error);
});
}
cancel() {
this.cancelCalled = true;
}
}
{
const instream = new ReadableStream(new Source());
const tstream = new TransformStream(new Transform());
const r = instream.pipeThrough(tstream);
async function read(stream) {
let res = '';
for await (const chunk of stream)
res += chunk;
return res;
}
read(r).then(common.mustCall((data) => {
const check = readFileSync(__filename);
assert.strictEqual(check.toString().toUpperCase(), data);
}));
}
{
assert.throws(() => Reflect.get(TransformStream.prototype, 'readable', {}), {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => Reflect.get(TransformStream.prototype, 'writable', {}), {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => TransformStream.prototype[kTransfer]({}), {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => {
Reflect.get(TransformStreamDefaultController.prototype, 'desiredSize', {});
}, {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => {
TransformStreamDefaultController.prototype.enqueue({});
}, {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => {
TransformStreamDefaultController.prototype.error({});
}, {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => {
TransformStreamDefaultController.prototype.terminate({});
}, {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => new TransformStreamDefaultController(), {
code: 'ERR_ILLEGAL_CONSTRUCTOR',
});
}
{
let controller;
const transform = new TransformStream({
start(c) {
controller = c;
}
});
assert.match(inspect(transform), /TransformStream/);
assert.match(inspect(transform, { depth: null }), /TransformStream/);
assert.match(inspect(transform, { depth: 0 }), /TransformStream \[/);
assert.match(inspect(controller), /TransformStreamDefaultController/);
assert.match(
inspect(controller, { depth: null }),
/TransformStreamDefaultController/);
assert.match(
inspect(controller, { depth: 0 }),
/TransformStreamDefaultController \[/);
}
{
Object.defineProperty(Object.prototype, 'type', {
get: common.mustNotCall('get %Object.prototype%.type'),
set: common.mustNotCall('set %Object.prototype%.type'),
configurable: true,
});
new TransformStream({
transform(chunk, controller) {
controller.enqueue(chunk);
},
flush(controller) {
controller.terminate();
}
});
delete Object.prototype.type;
}
|