-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathcontroller.js
More file actions
277 lines (266 loc) · 8.77 KB
/
Copy pathcontroller.js
File metadata and controls
277 lines (266 loc) · 8.77 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0
// ReadableByteStreamController and its byobRequest: auto-allocation,
// request lifecycle around enqueue(), and close() interactions with
// partially-filled BYOB reads.
import { strictEqual, ok, throws } from 'node:assert';
import { usingTsImpl } from 'which-impl';
import { rejectionOf } from 'helpers';
// DIVERGENCE (the subject of the streams_no_default_auto_allocate_
// chunk_size flag): with a DEFAULT reader and no autoAllocateChunkSize,
// C++ auto-allocates anyway — pull() sees a byobRequest with a real
// view; TypeScript follows the spec and reports byobRequest null. The
// C++ default allocation is 4096 bytes, or 16384 with the
// UPDATED_AUTO_ALLOCATE_CHUNK_SIZE autogate on (the @all-autogates
// variant), so both exact sizes are pinned.
export const byobRequestOnDefaultRead = {
async test() {
let seen = 'not-pulled';
const rs = new ReadableStream({
type: 'bytes',
pull(c) {
seen =
c.byobRequest === null
? 'null'
: `view(${c.byobRequest.view.byteLength})`;
c.enqueue(new Uint8Array([1]));
},
});
strictEqual((await rs.getReader().read()).value[0], 1);
if (usingTsImpl) {
strictEqual(seen, 'null');
} else {
ok(
seen === 'view(4096)' || seen === 'view(16384)',
`unexpected allocation: ${seen}`
);
}
},
};
// enqueue() discards the outstanding (auto-allocated) byobRequest: it is
// null right after the enqueue (pinned per implementation; under
// TypeScript there was no request to begin with — see
// byobRequestOnDefaultRead).
export const enqueueDiscardsByobRequest = {
async test() {
let states;
const rs = new ReadableStream({
type: 'bytes',
pull(c) {
const before = c.byobRequest === null ? 'null' : 'req';
c.enqueue(new Uint8Array([7]));
const after = c.byobRequest === null ? 'null' : 'req';
states = `${before},${after}`;
},
});
strictEqual((await rs.getReader().read()).value[0], 7);
strictEqual(states, usingTsImpl ? 'null,null' : 'req,null');
},
};
// DIVERGENCE (the WPT general.any close-with-partial seed): closing with
// a partially-filled pending read(view) must error the stream under the
// spec — TypeScript throws from close() and rejects the read and closed
// with the same TypeError. C++ lets close() succeed, resolves the read
// with an EMPTY view and done=false, and fulfills closed.
export const closeWithPartiallyFilledView = {
async test() {
let controller;
const rs = new ReadableStream({
type: 'bytes',
start(c) {
controller = c;
},
});
const reader = rs.getReader({ mode: 'byob' });
const readP = reader.read(new Uint16Array(1));
controller.enqueue(new Uint8Array([1])); // 1 of 2 bytes: partial
if (usingTsImpl) {
const expected = {
name: 'TypeError',
message: 'Insufficient bytes to fill elements in the given view',
};
throws(() => controller.close(), expected);
const readErr = await rejectionOf(readP);
strictEqual(readErr.message, expected.message);
const closedErr = await rejectionOf(reader.closed);
strictEqual(closedErr.message, expected.message);
} else {
controller.close();
const r = await readP;
strictEqual(r.done, false);
ok(r.value instanceof Uint16Array);
strictEqual(r.value.byteLength, 0);
strictEqual(await reader.closed, undefined);
}
},
};
// read(view) against a closed stream resolves done with an EMPTY view
// over the same-sized buffer (parity under the pinned
// internal_stream_byob_return_view flag).
export const readAfterCloseReturnsEmptyView = {
async test() {
let controller;
const rs = new ReadableStream({
type: 'bytes',
start(c) {
controller = c;
},
});
controller.close();
const { value, done } = await rs
.getReader({ mode: 'byob' })
.read(new Uint8Array(4));
ok(done);
ok(value instanceof Uint8Array);
strictEqual(value.byteLength, 0);
strictEqual(value.buffer.byteLength, 4);
},
};
// read(view) transfers the caller's buffer immediately (the pinned
// streams_byob_reader_detaches_buffer behavior) and delivers the bytes
// in a fresh view over the transferred buffer (parity).
export const readDetachesCallerBuffer = {
async test() {
let controller;
const rs = new ReadableStream({
type: 'bytes',
start(c) {
controller = c;
},
});
const reader = rs.getReader({ mode: 'byob' });
const view = new Uint8Array(4);
const readP = reader.read(view);
strictEqual(view.byteLength, 0); // detached at call time
controller.enqueue(new Uint8Array([1, 2]));
const { value, done } = await readP;
strictEqual(done, false);
strictEqual(value.byteLength, 2);
strictEqual(value[0], 1);
strictEqual(value[1], 2);
},
};
// close() while an UNFILLED BYOB read is pending. DIVERGENCE: C++
// resolves the read done with an empty view; the TypeScript read PENDS
// FOREVER while close() itself succeeds (bounded observation — the
// close-below-min defect family, without any min involved).
export const closeWithPendingUnfilledByobRead = {
async test() {
let controller;
const rs = new ReadableStream({
type: 'bytes',
start(c) {
controller = c;
},
});
const reader = rs.getReader({ mode: 'byob' });
const readP = reader.read(new Uint8Array(16));
controller.close();
const outcome = await Promise.race([
readP.then(
(r) => ({ state: 'fulfilled', r }),
() => ({ state: 'rejected' })
),
scheduler.wait(250).then(() => ({ state: 'pending' })),
]);
if (usingTsImpl) {
strictEqual(outcome.state, 'pending');
} else {
strictEqual(outcome.state, 'fulfilled');
strictEqual(outcome.r.done, true);
strictEqual(outcome.r.value.byteLength, 0);
}
await reader.closed;
},
};
export const controllerType = {
async test() {
let c;
new ReadableStream({
type: 'bytes',
start(ctrl) {
c = ctrl;
},
});
strictEqual(c instanceof ReadableByteStreamController, true);
},
};
// cancel() while a partially filled pull-into is pending (WPT
// 'cancel() with partially filled pending pull() request'): the read
// resolves done with the partial bytes DISCARDED on both sides —
// DIVERGENCE only in the done shape (C++ an empty view, TypeScript
// undefined; the done-read family). The cancel hook gets the reason
// and the cancel fulfills on both.
export const cancelWithPartiallyFilledPull = {
async test() {
const events = [];
let controller;
const rs = new ReadableStream({
type: 'bytes',
start(c) {
controller = c;
},
cancel(reason) {
events.push(`cancel:${reason}`);
},
});
const reader = rs.getReader({ mode: 'byob' });
const readP = reader.read(new Uint16Array(1)); // wants 2 bytes
controller.enqueue(new Uint8Array([0x11])); // partial: 1 byte
await scheduler.wait(1);
const cancelP = reader.cancel('why');
const read = await Promise.race([
readP.then(
(r) =>
`read:done=${r.done},len=${r.value ? r.value.byteLength : 'undef'}`,
(e) => `read-rejected:${e.name}`
),
scheduler.wait(200).then(() => 'read:pending'),
]);
const cancel = await Promise.race([
cancelP.then(
() => 'cancel:fulfilled',
(e) => `cancel-rejected:${e.name}`
),
scheduler.wait(200).then(() => 'cancel:pending'),
]);
strictEqual(
read,
usingTsImpl ? 'read:done=true,len=undef' : 'read:done=true,len=0'
);
strictEqual(cancel, 'cancel:fulfilled');
strictEqual(events.join(','), 'cancel:why');
},
};
// read(view) then immediate cancel() (WPT 'getReader(), read(view),
// then cancel()'): DIVERGENCE — C++ pulls proactively on the read, so
// pull runs BEFORE the cancel hook; TypeScript never pulls (spec: the
// cancel wins). The read resolves done on both.
export const readViewThenCancelOrdering = {
async test() {
const events = [];
const rs = new ReadableStream({
type: 'bytes',
pull() {
events.push('pull');
},
cancel(reason) {
events.push(`cancel:${reason}`);
},
});
const reader = rs.getReader({ mode: 'byob' });
const readP = reader.read(new Uint8Array(4));
const cancelP = reader.cancel('stop');
await Promise.all([
readP.then((r) => events.push(`read:done=${r.done}`)),
cancelP,
]);
strictEqual(
events.join(','),
usingTsImpl
? 'cancel:stop,read:done=true'
: 'pull,cancel:stop,read:done=true'
);
},
};