forked from nodejs/node
/
test-whatwg-readablebytestream.js
264 lines (218 loc) · 5.98 KB
/
test-whatwg-readablebytestream.js
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Flags: --expose-internals --no-warnings
'use strict';
const common = require('../common');
const assert = require('assert');
const {
ReadableStream,
ReadableByteStreamController,
ReadableStreamDefaultReader,
ReadableStreamBYOBReader,
ReadableStreamBYOBRequest,
} = require('stream/web');
const {
kState,
} = require('internal/webstreams/util');
const {
open,
} = require('fs/promises');
const {
readFileSync,
} = require('fs');
const {
Buffer,
} = require('buffer');
const {
inspect,
} = require('util');
{
const r = new ReadableStream({
type: 'bytes',
});
assert(r[kState].controller instanceof ReadableByteStreamController);
assert.strictEqual(typeof r.locked, 'boolean');
assert.strictEqual(typeof r.cancel, 'function');
assert.strictEqual(typeof r.getReader, 'function');
assert.strictEqual(typeof r.pipeThrough, 'function');
assert.strictEqual(typeof r.pipeTo, 'function');
assert.strictEqual(typeof r.tee, 'function');
['', null, 'asdf'].forEach((mode) => {
assert.throws(() => r.getReader({ mode }), {
code: 'ERR_INVALID_ARG_VALUE',
});
});
[1, 'asdf'].forEach((options) => {
assert.throws(() => r.getReader(options), {
code: 'ERR_INVALID_ARG_TYPE',
});
});
assert(!r.locked);
const defaultReader = r.getReader();
assert(r.locked);
assert(defaultReader instanceof ReadableStreamDefaultReader);
defaultReader.releaseLock();
const byobReader = r.getReader({ mode: 'byob' });
assert(byobReader instanceof ReadableStreamBYOBReader);
}
class Source {
constructor() {
this.controllerClosed = false;
}
async start(controller) {
this.file = await open(__filename);
this.controller = controller;
}
async pull(controller) {
const byobRequest = controller.byobRequest;
assert.match(inspect(byobRequest), /ReadableStreamBYOBRequest/);
const view = byobRequest.view;
const {
bytesRead,
} = await this.file.read({
buffer: view,
offset: view.byteOffset,
length: view.byteLength
});
if (bytesRead === 0) {
await this.file.close();
this.controller.close();
}
assert.throws(() => byobRequest.respondWithNewView({}), {
code: 'ERR_INVALID_ARG_TYPE',
});
byobRequest.respond(bytesRead);
assert.throws(() => byobRequest.respond(bytesRead), {
code: 'ERR_INVALID_STATE',
});
assert.throws(() => byobRequest.respondWithNewView(view), {
code: 'ERR_INVALID_STATE',
});
}
get type() { return 'bytes'; }
get autoAllocateChunkSize() { return 1024; }
}
{
const stream = new ReadableStream(new Source());
assert(stream[kState].controller instanceof ReadableByteStreamController);
async function read(stream) {
const reader = stream.getReader({ mode: 'byob' });
const chunks = [];
let result;
do {
result = await reader.read(Buffer.alloc(100));
if (result.value !== undefined)
chunks.push(Buffer.from(result.value));
} while (!result.done);
return Buffer.concat(chunks);
}
read(stream).then(common.mustCall((data) => {
const check = readFileSync(__filename);
assert.deepStrictEqual(check, data);
}));
}
{
const stream = new ReadableStream(new Source());
assert(stream[kState].controller instanceof ReadableByteStreamController);
async function read(stream) {
const chunks = [];
for await (const chunk of stream)
chunks.push(chunk);
return Buffer.concat(chunks);
}
read(stream).then(common.mustCall((data) => {
const check = readFileSync(__filename);
assert.deepStrictEqual(check, data);
}));
}
{
const stream = new ReadableStream(new Source());
assert(stream[kState].controller instanceof ReadableByteStreamController);
async function read(stream) {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream)
break;
}
read(stream).then(common.mustCall());
}
{
const stream = new ReadableStream(new Source());
assert(stream[kState].controller instanceof ReadableByteStreamController);
const error = new Error('boom');
async function read(stream) {
// eslint-disable-next-line no-unused-vars
for await (const _ of stream)
throw error;
}
assert.rejects(read(stream), error);
}
{
assert.throws(() => {
Reflect.get(ReadableStreamBYOBRequest.prototype, 'view', {});
}, {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => ReadableStreamBYOBRequest.prototype.respond.call({}), {
code: 'ERR_INVALID_THIS',
});
assert.throws(() => {
ReadableStreamBYOBRequest.prototype.respondWithNewView.call({});
}, {
code: 'ERR_INVALID_THIS',
});
}
{
const readable = new ReadableStream({ type: 'bytes' });
const reader = readable.getReader({ mode: 'byob' });
reader.releaseLock();
reader.releaseLock();
assert.rejects(reader.read(new Uint8Array(10)), {
code: 'ERR_INVALID_STATE',
});
assert.rejects(reader.cancel(), {
code: 'ERR_INVALID_STATE',
});
}
{
let controller;
new ReadableStream({
type: 'bytes',
start(c) { controller = c; }
});
assert.throws(() => controller.enqueue(1), {
code: 'ERR_INVALID_ARG_TYPE',
});
controller.close();
assert.throws(() => controller.enqueue(new Uint8Array(10)), {
code: 'ERR_INVALID_STATE',
});
assert.throws(() => controller.close(), {
code: 'ERR_INVALID_STATE',
});
}
{
const stream = new ReadableStream({
type: 'bytes',
pull(c) {
const v = new Uint8Array(c.byobRequest.view.buffer, 0, 3);
v.set([20, 21, 22]);
c.byobRequest.respondWithNewView(v);
},
});
const buffer = new ArrayBuffer(10);
const view = new Uint8Array(buffer, 0, 3);
view.set([10, 11, 12]);
const reader = stream.getReader({ mode: 'byob' });
reader.read(view);
}
{
const stream = new ReadableStream({
type: 'bytes',
autoAllocateChunkSize: 10,
pull(c) {
const v = new Uint8Array(c.byobRequest.view.buffer, 0, 3);
v.set([20, 21, 22]);
c.byobRequest.respondWithNewView(v);
},
});
const reader = stream.getReader();
reader.read();
}