create-web-stream 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -10
- package/dist/index.cjs +173 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +71 -9
- package/dist/index.js +173 -12
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,16 +1,92 @@
|
|
|
1
1
|
Note: **I’m using a translation tool, so there may be some inappropriate expressions.**
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
This library provides functions for creating Web API `ReadableStream` and `WritableStream` instances from simple handlers, with support for BYOB readers, buffered writers, AbortSignal integration, and guaranteed cleanup callbacks.
|
|
3
|
+
# create-web-stream
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
- `createReadableStream`
|
|
8
|
-
- `createWriteableFileStream`
|
|
5
|
+
A library for creating Web API `ReadableStream` and `WritableStream` instances from simple handlers and options. Supports BYOB readers, buffered writers, `AbortSignal` integration, and guaranteed cleanup callbacks.
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
This project is licensed under either of
|
|
7
|
+
## Installation
|
|
12
8
|
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
```bash
|
|
10
|
+
npm install create-web-stream
|
|
11
|
+
# or
|
|
12
|
+
pnpm add create-web-stream
|
|
13
|
+
# or
|
|
14
|
+
yarn add create-web-stream
|
|
15
|
+
```
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### createReadableStream
|
|
20
|
+
|
|
21
|
+
Creates a `ReadableStream` that yields byte data from the handler's `read` callback. Use `release` for cleanup when the stream ends.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createReadableStream } from "create-web-stream"
|
|
25
|
+
|
|
26
|
+
const stream = createReadableStream({
|
|
27
|
+
read() {
|
|
28
|
+
// Return the next chunk. Return null, undefined, or empty Uint8Array to end the stream
|
|
29
|
+
return new Uint8Array([1, 2, 3])
|
|
30
|
+
},
|
|
31
|
+
release(type, reason) {
|
|
32
|
+
console.log("end:", type, reason)
|
|
33
|
+
},
|
|
34
|
+
}, { signal: myAbortSignal })
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### createBYOBReadableStream
|
|
38
|
+
|
|
39
|
+
Creates a BYOB-style `ReadableStream` that reads directly into a buffer provided by the consumer. `read(buffer)` returns the number of bytes written.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createBYOBReadableStream } from "create-web-stream"
|
|
43
|
+
|
|
44
|
+
const stream = createBYOBReadableStream(
|
|
45
|
+
{
|
|
46
|
+
read(buffer) {
|
|
47
|
+
// Write into buffer and return the number of bytes written. Return 0 to end the stream
|
|
48
|
+
buffer.set(new Uint8Array([1, 2, 3]), 0)
|
|
49
|
+
return 3
|
|
50
|
+
},
|
|
51
|
+
release(type, reason) {
|
|
52
|
+
console.log("end:", type, reason)
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
4096, // bufferSize for fallback
|
|
56
|
+
{ signal: myAbortSignal }
|
|
57
|
+
)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### createWritableStream
|
|
61
|
+
|
|
62
|
+
Creates a `WritableStream` that passes byte data to the handler's `write` callback.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { createWritableStream } from "create-web-stream"
|
|
66
|
+
|
|
67
|
+
const stream = createWritableStream(
|
|
68
|
+
{
|
|
69
|
+
write(chunk) {
|
|
70
|
+
console.log("write:", chunk.byteLength, "bytes")
|
|
71
|
+
},
|
|
72
|
+
release(type, reason) {
|
|
73
|
+
console.log("end:", type, reason)
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
signal: myAbortSignal,
|
|
78
|
+
bufferSize: 4096, // 0 for no buffering (default)
|
|
79
|
+
strictBufferSize: false, // true to always pass chunks of exactly bufferSize (except the last)
|
|
80
|
+
useBufferView: false, // true to pass a buffer view to write and reduce copies
|
|
81
|
+
}
|
|
82
|
+
)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
This project is licensed under either of:
|
|
88
|
+
|
|
89
|
+
- MIT License
|
|
90
|
+
- Apache License (Version 2.0)
|
|
91
|
+
|
|
92
|
+
at your option.
|
package/dist/index.cjs
CHANGED
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates a `ReadableStream` that yields byte data using the provided custom handler.
|
|
5
5
|
*
|
|
6
|
-
* This function does not throw errors.
|
|
7
6
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
8
7
|
* the caller must check and handle it manually.
|
|
9
|
-
* The resulting stream includes a BYOB (Bring Your Own Buffer) reader if supported by the runtime.
|
|
10
8
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
9
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
10
|
+
* If unsupported, only a default reader is available.
|
|
11
|
+
*
|
|
12
|
+
* @param {CreateReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.
|
|
13
|
+
* @param {CreateReadableStreamOptions} options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.
|
|
14
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
14
15
|
*/
|
|
15
16
|
function createReadableStream(handler, options) {
|
|
16
17
|
let abortListener = null;
|
|
@@ -31,7 +32,10 @@ function createReadableStream(handler, options) {
|
|
|
31
32
|
}
|
|
32
33
|
return cleanupPromise;
|
|
33
34
|
}
|
|
34
|
-
|
|
35
|
+
const useByteStream = (options?.__internal_useByteStream__ !== undefined)
|
|
36
|
+
? options.__internal_useByteStream__
|
|
37
|
+
: isReadableByteStreamAvailable();
|
|
38
|
+
if (!useByteStream) {
|
|
35
39
|
return new ReadableStream({
|
|
36
40
|
start(controller) {
|
|
37
41
|
if (options?.signal != null) {
|
|
@@ -130,19 +134,158 @@ function createReadableStream(handler, options) {
|
|
|
130
134
|
}
|
|
131
135
|
});
|
|
132
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Creates a `ReadableStream` that yields byte data using a BYOB-style handler.
|
|
139
|
+
*
|
|
140
|
+
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
141
|
+
* the caller must check and handle it manually.
|
|
142
|
+
*
|
|
143
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
144
|
+
* If unsupported, only a default reader is available.
|
|
145
|
+
*
|
|
146
|
+
* @param {CreateBYOBReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateBYOBReadableStreamHandler` for details.
|
|
147
|
+
* @param {number} defaultBufferSize The default size of the buffer passed to `handler.read`. Must be a positive safe integer. Used as `autoAllocateChunkSize` when a bytes-type reader is available. If unsupported, used as the size of the internal buffer for a default reader.
|
|
148
|
+
* @param {CreateBYOBReadableStreamOptions} options Optional settings: `signal`. See `CreateBYOBReadableStreamOptions` for details.
|
|
149
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
150
|
+
*/
|
|
151
|
+
function createBYOBReadableStream(handler, defaultBufferSize, options) {
|
|
152
|
+
defaultBufferSize = Math.ceil(defaultBufferSize);
|
|
153
|
+
requiresNonzeroSafeInt(defaultBufferSize, "defaultBufferSize");
|
|
154
|
+
let abortListener = null;
|
|
155
|
+
let buffer = null;
|
|
156
|
+
let cleanupPromise = null;
|
|
157
|
+
function cleanup(type, reason) {
|
|
158
|
+
if (cleanupPromise === null) {
|
|
159
|
+
cleanupPromise = (async () => {
|
|
160
|
+
buffer = null;
|
|
161
|
+
if (options?.signal != null && abortListener != null) {
|
|
162
|
+
options.signal.removeEventListener("abort", abortListener);
|
|
163
|
+
abortListener = null;
|
|
164
|
+
}
|
|
165
|
+
if (handler.release != null) {
|
|
166
|
+
await handler.release(type, reason);
|
|
167
|
+
}
|
|
168
|
+
})();
|
|
169
|
+
}
|
|
170
|
+
return cleanupPromise;
|
|
171
|
+
}
|
|
172
|
+
const useByteStream = (options?.__internal_useByteStream__ !== undefined)
|
|
173
|
+
? options.__internal_useByteStream__
|
|
174
|
+
: isReadableByteStreamAvailable();
|
|
175
|
+
if (!useByteStream) {
|
|
176
|
+
return new ReadableStream({
|
|
177
|
+
start(controller) {
|
|
178
|
+
if (options?.signal != null) {
|
|
179
|
+
abortListener = () => {
|
|
180
|
+
const reason = options?.signal?.reason ?? newAbortSignalDefaultError();
|
|
181
|
+
cleanup("SignalAbort", reason).catch(() => { });
|
|
182
|
+
controller.error(reason);
|
|
183
|
+
};
|
|
184
|
+
options?.signal?.addEventListener("abort", abortListener);
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
async pull(controller) {
|
|
188
|
+
try {
|
|
189
|
+
throwIfAborted(options?.signal);
|
|
190
|
+
if (buffer === null) {
|
|
191
|
+
buffer = new Uint8Array(defaultBufferSize);
|
|
192
|
+
}
|
|
193
|
+
throwIfAborted(options?.signal);
|
|
194
|
+
const nread = await handler.read(buffer);
|
|
195
|
+
throwIfAborted(options?.signal);
|
|
196
|
+
requiresSafeUint(nread, "nread");
|
|
197
|
+
if (nread === 0) {
|
|
198
|
+
await cleanup("Close");
|
|
199
|
+
controller.close();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
controller.enqueue(buffer.slice(0, nread));
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
const isSignalAbort = isThrownByAbortSignal(e, options?.signal);
|
|
206
|
+
await cleanup(isSignalAbort ? "SignalAbort" : "Error", e).catch(() => { });
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
async cancel(reason) {
|
|
211
|
+
await cleanup("Cancel", reason);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return new ReadableStream({
|
|
216
|
+
type: "bytes",
|
|
217
|
+
autoAllocateChunkSize: defaultBufferSize,
|
|
218
|
+
start(controller) {
|
|
219
|
+
if (options?.signal) {
|
|
220
|
+
abortListener = () => {
|
|
221
|
+
const reason = options?.signal?.reason ?? newAbortSignalDefaultError();
|
|
222
|
+
cleanup("SignalAbort", reason).catch(() => { });
|
|
223
|
+
controller.error(reason);
|
|
224
|
+
};
|
|
225
|
+
options?.signal.addEventListener("abort", abortListener);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
async pull(controller) {
|
|
229
|
+
try {
|
|
230
|
+
throwIfAborted(options?.signal);
|
|
231
|
+
const byob = controller.byobRequest;
|
|
232
|
+
if (byob == null) {
|
|
233
|
+
if (buffer == null) {
|
|
234
|
+
buffer = new Uint8Array(defaultBufferSize);
|
|
235
|
+
}
|
|
236
|
+
throwIfAborted(options?.signal);
|
|
237
|
+
const nread = await handler.read(buffer);
|
|
238
|
+
throwIfAborted(options?.signal);
|
|
239
|
+
requiresSafeUint(nread, "nread");
|
|
240
|
+
if (nread === 0) {
|
|
241
|
+
await cleanup("Close");
|
|
242
|
+
controller.close();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
controller.enqueue(buffer.slice(0, nread));
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const v = byob.view;
|
|
249
|
+
const view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
|
|
250
|
+
throwIfAborted(options?.signal);
|
|
251
|
+
const nread = await handler.read(view);
|
|
252
|
+
throwIfAborted(options?.signal);
|
|
253
|
+
requiresSafeUint(nread, "nread");
|
|
254
|
+
if (nread === 0) {
|
|
255
|
+
await cleanup("Close");
|
|
256
|
+
// byobRequest がある場合、respond を呼ばないと promise が解決されない。
|
|
257
|
+
// controller.close() の後だと respond(0) を読んでもエラーにはならない。
|
|
258
|
+
// https://github.com/whatwg/streams/issues/1170
|
|
259
|
+
controller.close();
|
|
260
|
+
controller.byobRequest?.respond(0);
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
byob.respond(nread);
|
|
264
|
+
}
|
|
265
|
+
catch (e) {
|
|
266
|
+
const isSignalAbort = isThrownByAbortSignal(e, options?.signal);
|
|
267
|
+
await cleanup(isSignalAbort ? "SignalAbort" : "Error", e).catch(() => { });
|
|
268
|
+
throw e;
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
async cancel(reason) {
|
|
272
|
+
await cleanup("Cancel", reason);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
133
276
|
/**
|
|
134
277
|
* Creates a `WritableStream` that writes byte data using the provided custom handler.
|
|
135
278
|
*
|
|
136
|
-
* This function itself does not throw errors.
|
|
137
279
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
138
280
|
* the caller must check and handle it manually.
|
|
139
281
|
*
|
|
140
|
-
* @param handler
|
|
141
|
-
* @param options
|
|
142
|
-
* @returns A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
282
|
+
* @param {CreateWritableStreamHandler} handler The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.
|
|
283
|
+
* @param {CreateWritableStreamOptions} options Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.
|
|
284
|
+
* @returns {WritableStream<Uint8Array<ArrayBufferLike>>} A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
143
285
|
*/
|
|
144
286
|
function createWritableStream(handler, options) {
|
|
145
|
-
const bufferSize =
|
|
287
|
+
const bufferSize = options?.bufferSize ?? 0;
|
|
288
|
+
requiresSafeUint(bufferSize, "bufferSize");
|
|
146
289
|
let abortListener = null;
|
|
147
290
|
let buffer = null;
|
|
148
291
|
let bufferOffset = 0;
|
|
@@ -268,7 +411,26 @@ function mapToArrayBuffer(buffer) {
|
|
|
268
411
|
? buffer
|
|
269
412
|
: new Uint8Array(buffer);
|
|
270
413
|
}
|
|
414
|
+
function requiresSafeUint(num, numName) {
|
|
415
|
+
const name = numName ?? "value";
|
|
416
|
+
if (!Number.isSafeInteger(num)) {
|
|
417
|
+
throw new TypeError(`${name} must be a safe integer.`);
|
|
418
|
+
}
|
|
419
|
+
if (num < 0) {
|
|
420
|
+
throw new RangeError(`${name} must be a positive integer.`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function requiresNonzeroSafeInt(num, numName) {
|
|
424
|
+
const name = numName;
|
|
425
|
+
if (!Number.isSafeInteger(num)) {
|
|
426
|
+
throw new TypeError(`${name} must be a safe integer.`);
|
|
427
|
+
}
|
|
428
|
+
if (num <= 0) {
|
|
429
|
+
throw new RangeError(`${name} must be a non-zero positive integer.`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
271
432
|
|
|
433
|
+
exports.createBYOBReadableStream = createBYOBReadableStream;
|
|
272
434
|
exports.createReadableStream = createReadableStream;
|
|
273
435
|
exports.createWritableStream = createWritableStream;
|
|
274
436
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type CreateReadableStreamHandlerSource = Uint8Array<ArrayBuffer> | null | undefined\n\nexport type CreateReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t * \n\t * - **Yielding data**: Return a `Uint8Array` containing the next chunk of bytes.\n\t * - **Ending the stream**: Return `null`, `undefined`, or an empty `Uint8Array` (`byteLength === 0`) to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t */\n\tread: () => PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource,\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t */\n\trelease?: (type: CreateReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using the provided custom handler.\n * \n * This function does not throw errors. \n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * The resulting stream includes a BYOB (Bring Your Own Buffer) reader if supported by the runtime.\n * \n * @param handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.\n * @param options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.\n * @returns A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createReadableStream(\n\thandler: CreateReadableStreamHandler,\n\toptions?: CreateReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tif (!isReadableByteStreamAvailable()) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst data = await handler.read()\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (data == null || data.byteLength === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(data)\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\t// autoAllocateChunkSize を指定すると stream.getReader() でも byob が使われるようになるが、\n\t// この実装で byob を用いてもコピーが増えるだけで恩恵が少ないため指定しない。\n\t// また type: \"bytes\" で strategy を指定すると (正確には size を定義すると) エラーになる点にも注意。\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tbuffer = (await handler.read()) ?? null\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t}\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されないことに注意\n\t\t\t\tif (byob != null) {\n\t\t\t\t\t// respond する前なので null にならない\n\t\t\t\t\tconst v = byob.view!!\n\t\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\t\t\t\t\tconst nread = Math.min(buffer.byteLength, view.byteLength)\n\n\t\t\t\t\tview.set(buffer.subarray(0, nread))\n\t\t\t\t\tbuffer = buffer.subarray(nread)\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tbyob.respond(nread)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tcontroller.enqueue(buffer)\n\t\t\t\t\tbuffer = null\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\n\t\t\t\t// byobRequest が存在する場合、controller.close() を呼んだだけでは\n\t\t\t\t// Promise は解決されず、respond() も呼ぶ必要がある。\n\t\t\t\t// controller.error() も同様の挙動になる可能性がある。(要検証)\n\t\t\t\t// 少なくとも throw すれば Promise は解決されるため、現状はこの実装とする。\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateWritableStreamHandlerReleaseType = \"Close\" | \"Abort\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateWritableStreamHandler = {\n\n\t/**\n\t * A callback invoked when a new chunk of byte data is ready to be written.\n\t * \n\t * - **Data Chunk**: Receives a `Uint8Array` containing the data. The exact size and memory reference of this chunk depend on the stream's `bufferSize` and `strictBufferSize` options.\n\t * - **Data Safety**: If `options.useBufferView` is `true`, the `chunk` might be a direct view (subarray) of the internal buffer. To prevent data corruption, do not retain references to this view outside this callback.\n\t * - **Handling Errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t */\n\twrite: (chunk: Uint8Array<ArrayBuffer>) => PromiseLike<void> | void,\n\n\t/**\n\t * A callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream or its writer is explicitly closed. (type: `\"Close\"`)\n\t * - The stream or its writer is explicitly aborted. (type: `\"Abort\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a write operation. (type: `\"Error\"`)\n\t */\n\trelease?: (\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t) => PromiseLike<void> | void,\n}\n\nexport type CreateWritableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * The size of the internal buffer in bytes.\n\t * \n\t * Defaults to `0` (unbuffered).\n\t */\n\tbufferSize?: number,\n\n\t/**\n\t * If `true`, the stream strictly enforces the `bufferSize` for every chunk passed to the handler.\n\t * Only the final `write` call may receive a chunk smaller than the `bufferSize`.\n\t * \n\t * If `false`, chunks larger than the `bufferSize` will bypass the internal buffer and be processed directly.\n\t * \n\t * Defaults to `false`.\n\t */\n\tstrictBufferSize?: boolean,\n\n\t/**\n\t * If `true`, the handler's `write` method can receive a chunk as a view (subarray) of the internal buffer.\n\t * This reduces the number of memory copies, but the received chunk must not be referenced outside the `write` method.\n\t * If you need to retain the chunk data externally, you must make a copy of it within the `write` method.\n\t * \n\t * Defaults to `false`.\n\t */\n\tuseBufferView?: boolean,\n}\n\n/**\n * Creates a `WritableStream` that writes byte data using the provided custom handler.\n * \n * This function itself does not throw errors.\n * If you need to throw an error early when the provided `options.signal` is already aborted, \n * the caller must check and handle it manually.\n * \n * @param handler - The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.\n * @param options - Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.\n * @returns A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.\n */\nexport function createWritableStream(\n\thandler: CreateWritableStreamHandler,\n\toptions?: CreateWritableStreamOptions,\n): WritableStream<Uint8Array<ArrayBufferLike>> {\n\n\tconst bufferSize = Math.max(0, Math.ceil(options?.bufferSize ?? 0))\n\n\tlet abortListener: (() => void) | null = null;\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null;\n\tlet bufferOffset = 0;\n\n\tlet cleanupPromise: Promise<void> | null = null;\n\tfunction cleanup(\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions?.signal?.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\treturn new WritableStream<Uint8Array<ArrayBufferLike>>({\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal != null) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener);\n\t\t\t}\n\t\t},\n\n\t\tasync write(src) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t// bufferSize が 0 以下の場合や src が buffer より大きい場合は buffer を使わずに処理する。\n\t\t\t\tif (\n\t\t\t\t\tbufferSize <= 0 ||\n\t\t\t\t\t(bufferSize <= src.byteLength && options?.strictBufferSize !== true)\n\t\t\t\t) {\n\n\t\t\t\t\t// buffer に既にデータがある場合、それを処理する。\n\t\t\t\t\tif (buffer !== null && 0 < bufferOffset) {\n\t\t\t\t\t\tconst chunk = options?.useBufferView === true\n\t\t\t\t\t\t\t? buffer.subarray(0, bufferOffset)\n\t\t\t\t\t\t\t: buffer.slice(0, bufferOffset)\n\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tawait handler.write(mapToArrayBuffer(src))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tlet srcOffset = 0;\n\t\t\t\twhile (srcOffset < src.byteLength) {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(bufferSize)\n\t\t\t\t\t}\n\n\t\t\t\t\tconst n = Math.min(bufferSize - bufferOffset, src.byteLength - srcOffset)\n\t\t\t\t\tbuffer.set(src.subarray(srcOffset, srcOffset + n), bufferOffset)\n\t\t\t\t\tbufferOffset += n\n\t\t\t\t\tsrcOffset += n\n\n\t\t\t\t\tif (bufferOffset === bufferSize) {\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\t\tconst chunk = buffer\n\t\t\t\t\t\tif (options?.useBufferView !== true) {\n\t\t\t\t\t\t\tbuffer = null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync close() {\n\t\t\ttry {\n\t\t\t\tif (0 < bufferOffset && buffer != null) {\n\t\t\t\t\tconst view = buffer.subarray(0, bufferOffset)\n\t\t\t\t\tbuffer = null\n\t\t\t\t\tawait handler.write(view)\n\t\t\t\t}\n\t\t\t\tawait cleanup(\"Close\")\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tawait cleanup(\"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync abort(reason) {\n\t\t\tawait cleanup(\"Abort\", reason)\n\t\t}\n\t})\n}\n\n\nlet _isReadableByteStreamAvailable: boolean | null = null\nfunction isReadableByteStreamAvailable() {\n\tif (_isReadableByteStreamAvailable === null) {\n\t\ttry {\n\t\t\tnew ReadableStream({ type: \"bytes\" })\n\t\t\t_isReadableByteStreamAvailable = true\n\t\t}\n\t\tcatch {\n\t\t\t_isReadableByteStreamAvailable = false\n\t\t}\n\t}\n\n\treturn _isReadableByteStreamAvailable\n}\n\nfunction throwIfAborted(signal: AbortSignal | undefined | null) {\n\tif (signal?.aborted === true) {\n\t\tthrow (signal?.reason ?? newAbortSignalDefaultError())\n\t}\n}\n\nfunction newAbortSignalDefaultError(): Error {\n\treturn new DOMException(\"The operation was aborted.\", \"AbortError\")\n}\n\nfunction isThrownByAbortSignal(err: unknown, signal: AbortSignal | null | undefined): boolean {\n\treturn (signal?.aborted === true) &&\n\t\t(err === signal.reason || (err instanceof DOMException && err.name === \"AbortError\"));\n}\n\nfunction mapToArrayBuffer(\n\tbuffer: Uint8Array<ArrayBufferLike>\n): Uint8Array<ArrayBuffer> {\n\n\treturn buffer.buffer instanceof ArrayBuffer\n\t\t? buffer as Uint8Array<ArrayBuffer>\n\t\t: new Uint8Array(buffer)\n}\n"],"names":[],"mappings":";;AAsCA;;;;;;;;;;;AAWG;AACG,SAAU,oBAAoB,CACnC,OAAoC,EACpC,OAAqC,EAAA;IAGrC,IAAI,aAAa,GAAwB,IAAI;IAC7C,IAAI,MAAM,GAAmC,IAAI;IAEjD,IAAI,cAAc,GAAyB,IAAI;AAC/C,IAAA,SAAS,OAAO,CACf,IAA4C,EAC5C,MAAgB,EAAA;AAGhB,QAAA,IAAI,cAAc,KAAK,IAAI,EAAE;AAC5B,YAAA,cAAc,GAAG,CAAC,YAAW;gBAC5B,MAAM,GAAG,IAAI;gBACb,IAAI,OAAO,EAAE,MAAM,IAAI,IAAI,IAAI,aAAa,IAAI,IAAI,EAAE;oBACrD,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC;oBAC1D,aAAa,GAAG,IAAI;gBACrB;AACA,gBAAA,IAAI,OAAO,CAAC,OAAO,IAAI,IAAI,EAAE;oBAC5B,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;gBACpC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EAAE;AAC5C,QAAA,IAAI;YACH,IAAI,cAAc,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;YACrC,8BAA8B,GAAG,IAAI;QACtC;AACA,QAAA,MAAM;YACL,8BAA8B,GAAG,KAAK;QACvC;IACD;AAEA,IAAA,OAAO,8BAA8B;AACtC;AAEA,SAAS,cAAc,CAAC,MAAsC,EAAA;AAC7D,IAAA,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI,EAAE;QAC7B,OAAO,MAAM,EAAE,MAAM,IAAI,0BAA0B,EAAE;IACtD;AACD;AAEA,SAAS,0BAA0B,GAAA;AAClC,IAAA,OAAO,IAAI,YAAY,CAAC,4BAA4B,EAAE,YAAY,CAAC;AACpE;AAEA,SAAS,qBAAqB,CAAC,GAAY,EAAE,MAAsC,EAAA;AAClF,IAAA,OAAO,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI;AAC/B,SAAC,GAAG,KAAK,MAAM,CAAC,MAAM,KAAK,GAAG,YAAY,YAAY,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC;AACvF;AAEA,SAAS,gBAAgB,CACxB,MAAmC,EAAA;AAGnC,IAAA,OAAO,MAAM,CAAC,MAAM,YAAY;AAC/B,UAAE;AACF,UAAE,IAAI,UAAU,CAAC,MAAM,CAAC;AAC1B;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type CreateReadableStreamHandlerSource = Uint8Array<ArrayBuffer> | null | undefined\n\nexport type CreateReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t * \n\t * - **Yielding data**: Return a `Uint8Array` containing the next chunk of bytes.\n\t * - **Ending the stream**: Return `null`, `undefined`, or an empty `Uint8Array` (`byteLength === 0`) to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @returns {PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource} The next chunk of bytes.\n\t */\n\tread: () => PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource,\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateReadableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (type: CreateReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * @internal\n\t * @ignore\n\t */\n\t__internal_useByteStream__?: boolean\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using the provided custom handler.\n * \n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * \n * The resulting stream can provide a BYOB reader if supported by the runtime.\n * If unsupported, only a default reader is available.\n * \n * @param {CreateReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.\n * @param {CreateReadableStreamOptions} options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.\n * @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createReadableStream(\n\thandler: CreateReadableStreamHandler,\n\toptions?: CreateReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tconst useByteStream = (options?.__internal_useByteStream__ !== undefined)\n\t\t? options.__internal_useByteStream__\n\t\t: isReadableByteStreamAvailable()\n\n\tif (!useByteStream) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst data = await handler.read()\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (data == null || data.byteLength === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(data)\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\t// autoAllocateChunkSize を指定すると stream.getReader() でも byob が使われるようになるが、\n\t// この実装で byob を用いてもコピーが増えるだけで恩恵が少ないため指定しない。\n\t// また type: \"bytes\" で strategy を指定すると (正確には size を定義すると) エラーになる点にも注意。\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tbuffer = (await handler.read()) ?? null\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t}\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されないことに注意\n\t\t\t\tif (byob != null) {\n\t\t\t\t\t// respond する前なので null にならない\n\t\t\t\t\tconst v = byob.view!!\n\t\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\t\t\t\t\tconst nread = Math.min(buffer.byteLength, view.byteLength)\n\n\t\t\t\t\tview.set(buffer.subarray(0, nread))\n\t\t\t\t\tbuffer = buffer.subarray(nread)\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tbyob.respond(nread)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tcontroller.enqueue(buffer)\n\t\t\t\t\tbuffer = null\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\n\t\t\t\t// byobRequest が存在する場合、controller.close() を呼んだだけでは\n\t\t\t\t// Promise は解決されず、respond() も呼ぶ必要がある。\n\t\t\t\t// controller.error() も同様の挙動になる可能性がある。(要検証)\n\t\t\t\t// 少なくとも throw すれば Promise は解決されるため、現状はこの実装とする。\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateBYOBReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateBYOBReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t *\n\t * - **Yielding data**: Write bytes into the provided `buffer` and return the number of bytes written (1 or greater). The returned value must not exceed `buffer.byteLength`.\n\t * - **Ending the stream**: Return `0` to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @param {Uint8Array<ArrayBuffer>} buffer The buffer to write the data into.\n\t * @returns {PromiseLike<number> | number} The number of bytes written.\n\t */\n\tread: (buffer: Uint8Array<ArrayBuffer>) => PromiseLike<number> | number\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateBYOBReadableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (type: CreateBYOBReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateBYOBReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * @internal\n\t * @ignore\n\t */\n\t__internal_useByteStream__?: boolean\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using a BYOB-style handler.\n *\n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * \n * The resulting stream can provide a BYOB reader if supported by the runtime.\n * If unsupported, only a default reader is available.\n *\n * @param {CreateBYOBReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateBYOBReadableStreamHandler` for details.\n * @param {number} defaultBufferSize The default size of the buffer passed to `handler.read`. Must be a positive safe integer. Used as `autoAllocateChunkSize` when a bytes-type reader is available. If unsupported, used as the size of the internal buffer for a default reader.\n * @param {CreateBYOBReadableStreamOptions} options Optional settings: `signal`. See `CreateBYOBReadableStreamOptions` for details.\n * @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createBYOBReadableStream(\n\thandler: CreateBYOBReadableStreamHandler,\n\tdefaultBufferSize: number,\n\toptions?: CreateBYOBReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tdefaultBufferSize = Math.ceil(defaultBufferSize)\n\trequiresNonzeroSafeInt(defaultBufferSize, \"defaultBufferSize\")\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateBYOBReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tconst useByteStream = (options?.__internal_useByteStream__ !== undefined)\n\t\t? options.__internal_useByteStream__\n\t\t: isReadableByteStreamAvailable()\n\n\tif (!useByteStream) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(defaultBufferSize)\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst nread = await handler.read(buffer)\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\t\tif (nread === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(buffer.slice(0, nread))\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\t\tautoAllocateChunkSize: defaultBufferSize,\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\tif (byob == null) {\n\t\t\t\t\tif (buffer == null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(defaultBufferSize)\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst nread = await handler.read(buffer)\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\t\tif (nread === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(buffer.slice(0, nread))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst v = byob.view!!\n\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tconst nread = await handler.read(view)\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\tif (nread === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tbyob.respond(nread)\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateWritableStreamHandlerReleaseType = \"Close\" | \"Abort\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateWritableStreamHandler = {\n\n\t/**\n\t * A callback invoked when a new chunk of byte data is ready to be written.\n\t * \n\t * - **Data Chunk**: Receives a `Uint8Array` containing the data. The exact size and memory reference of this chunk depend on the stream's `bufferSize` and `strictBufferSize` options.\n\t * - **Data Safety**: If `options.useBufferView` is `true`, the `chunk` might be a direct view (subarray) of the internal buffer. To prevent data corruption, do not retain references to this view outside this callback.\n\t * - **Handling Errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @param {Uint8Array<ArrayBuffer>} chunk The chunk of byte data to write.\n\t */\n\twrite: (chunk: Uint8Array<ArrayBuffer>) => PromiseLike<void> | void,\n\n\t/**\n\t * A callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream or its writer is explicitly closed. (type: `\"Close\"`)\n\t * - The stream or its writer is explicitly aborted. (type: `\"Abort\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a write operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateWritableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t) => PromiseLike<void> | void,\n}\n\nexport type CreateWritableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * The size of the internal buffer in bytes. \n\t * Must be a zero or positive safe integer. \n\t * \n\t * Defaults to `0` (unbuffered).\n\t */\n\tbufferSize?: number,\n\n\t/**\n\t * If `true`, the stream strictly enforces the `bufferSize` for every chunk passed to the handler.\n\t * Only the final `write` call may receive a chunk smaller than the `bufferSize`.\n\t * \n\t * If `false`, chunks larger than the `bufferSize` will bypass the internal buffer and be processed directly.\n\t * \n\t * Defaults to `false`.\n\t */\n\tstrictBufferSize?: boolean,\n\n\t/**\n\t * If `true`, the handler's `write` method can receive a chunk as a view (subarray) of the internal buffer.\n\t * This reduces the number of memory copies, but the received chunk must not be referenced outside the `write` method.\n\t * If you need to retain the chunk data externally, you must make a copy of it within the `write` method.\n\t * \n\t * Defaults to `false`.\n\t */\n\tuseBufferView?: boolean,\n}\n\n/**\n * Creates a `WritableStream` that writes byte data using the provided custom handler.\n * \n * If you need to throw an error early when the provided `options.signal` is already aborted, \n * the caller must check and handle it manually.\n * \n * @param {CreateWritableStreamHandler} handler The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.\n * @param {CreateWritableStreamOptions} options Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.\n * @returns {WritableStream<Uint8Array<ArrayBufferLike>>} A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.\n */\nexport function createWritableStream(\n\thandler: CreateWritableStreamHandler,\n\toptions?: CreateWritableStreamOptions,\n): WritableStream<Uint8Array<ArrayBufferLike>> {\n\n\tconst bufferSize = options?.bufferSize ?? 0\n\trequiresSafeUint(bufferSize, \"bufferSize\")\n\n\tlet abortListener: (() => void) | null = null;\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null;\n\tlet bufferOffset = 0;\n\n\tlet cleanupPromise: Promise<void> | null = null;\n\tfunction cleanup(\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions?.signal?.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\treturn new WritableStream<Uint8Array<ArrayBufferLike>>({\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal != null) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener);\n\t\t\t}\n\t\t},\n\n\t\tasync write(src) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t// bufferSize が 0 以下の場合や src が buffer より大きい場合は buffer を使わずに処理する。\n\t\t\t\tif (\n\t\t\t\t\tbufferSize <= 0 ||\n\t\t\t\t\t(bufferSize <= src.byteLength && options?.strictBufferSize !== true)\n\t\t\t\t) {\n\n\t\t\t\t\t// buffer に既にデータがある場合、それを処理する。\n\t\t\t\t\tif (buffer !== null && 0 < bufferOffset) {\n\t\t\t\t\t\tconst chunk = options?.useBufferView === true\n\t\t\t\t\t\t\t? buffer.subarray(0, bufferOffset)\n\t\t\t\t\t\t\t: buffer.slice(0, bufferOffset)\n\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tawait handler.write(mapToArrayBuffer(src))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tlet srcOffset = 0;\n\t\t\t\twhile (srcOffset < src.byteLength) {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(bufferSize)\n\t\t\t\t\t}\n\n\t\t\t\t\tconst n = Math.min(bufferSize - bufferOffset, src.byteLength - srcOffset)\n\t\t\t\t\tbuffer.set(src.subarray(srcOffset, srcOffset + n), bufferOffset)\n\t\t\t\t\tbufferOffset += n\n\t\t\t\t\tsrcOffset += n\n\n\t\t\t\t\tif (bufferOffset === bufferSize) {\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\t\tconst chunk = buffer\n\t\t\t\t\t\tif (options?.useBufferView !== true) {\n\t\t\t\t\t\t\tbuffer = null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync close() {\n\t\t\ttry {\n\t\t\t\tif (0 < bufferOffset && buffer != null) {\n\t\t\t\t\tconst view = buffer.subarray(0, bufferOffset)\n\t\t\t\t\tbuffer = null\n\t\t\t\t\tawait handler.write(view)\n\t\t\t\t}\n\t\t\t\tawait cleanup(\"Close\")\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tawait cleanup(\"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync abort(reason) {\n\t\t\tawait cleanup(\"Abort\", reason)\n\t\t}\n\t})\n}\n\n\nlet _isReadableByteStreamAvailable: boolean | null = null\nfunction isReadableByteStreamAvailable(): boolean {\n\tif (_isReadableByteStreamAvailable === null) {\n\t\ttry {\n\t\t\tnew ReadableStream({ type: \"bytes\" })\n\t\t\t_isReadableByteStreamAvailable = true\n\t\t}\n\t\tcatch {\n\t\t\t_isReadableByteStreamAvailable = false\n\t\t}\n\t}\n\treturn _isReadableByteStreamAvailable\n}\n\nfunction throwIfAborted(signal: AbortSignal | undefined | null) {\n\tif (signal?.aborted === true) {\n\t\tthrow (signal?.reason ?? newAbortSignalDefaultError())\n\t}\n}\n\nfunction newAbortSignalDefaultError(): Error {\n\treturn new DOMException(\"The operation was aborted.\", \"AbortError\")\n}\n\nfunction isThrownByAbortSignal(err: unknown, signal: AbortSignal | null | undefined): boolean {\n\treturn (signal?.aborted === true) &&\n\t\t(err === signal.reason || (err instanceof DOMException && err.name === \"AbortError\"));\n}\n\nfunction mapToArrayBuffer(\n\tbuffer: Uint8Array<ArrayBufferLike>\n): Uint8Array<ArrayBuffer> {\n\n\treturn buffer.buffer instanceof ArrayBuffer\n\t\t? buffer as Uint8Array<ArrayBuffer>\n\t\t: new Uint8Array(buffer)\n}\n\nfunction requiresSafeUint(num: number, numName?: string): void {\n\tconst name = numName ?? \"value\";\n\n\tif (!Number.isSafeInteger(num)) {\n\t\tthrow new TypeError(`${name} must be a safe integer.`);\n\t}\n\tif (num < 0) {\n\t\tthrow new RangeError(`${name} must be a positive integer.`);\n\t}\n}\n\nfunction requiresNonzeroSafeInt(num: number, numName?: string): void {\n\tconst name = numName ?? \"value\";\n\n\tif (!Number.isSafeInteger(num)) {\n\t\tthrow new TypeError(`${name} must be a safe integer.`);\n\t}\n\tif (num <= 0) {\n\t\tthrow new RangeError(`${name} must be a non-zero positive integer.`);\n\t}\n}"],"names":[],"mappings":";;AAiDA;;;;;;;;;;;;AAYG;AACG,SAAU,oBAAoB,CACnC,OAAoC,EACpC,OAAqC,EAAA;IAGrC,IAAI,aAAa,GAAwB,IAAI;IAC7C,IAAI,MAAM,GAAmC,IAAI;IAEjD,IAAI,cAAc,GAAyB,IAAI;AAC/C,IAAA,SAAS,OAAO,CACf,IAA4C,EAC5C,MAAgB,EAAA;AAGhB,QAAA,IAAI,cAAc,KAAK,IAAI,EAAE;AAC5B,YAAA,cAAc,GAAG,CAAC,YAAW;gBAC5B,MAAM,GAAG,IAAI;gBACb,IAAI,OAAO,EAAE,MAAM,IAAI,IAAI,IAAI,aAAa,IAAI,IAAI,EAAE;oBACrD,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,aAAa,CAAC;oBAC1D,aAAa,GAAG,IAAI;gBACrB;AACA,gBAAA,IAAI,OAAO,CAAC,OAAO,IAAI,IAAI,EAAE;oBAC5B,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC;gBACpC;YACD,CAAC,GAAG;QACL;AACA,QAAA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AAI,SAAS,CAAC,GAAG,IAAI,CAAA,wBAAA,CAA0B,CAAC;IACvD;AACA,IAAA,IAAI,GAAG,IAAI,CAAC,EAAE;AACb,QAAA,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,CAAA,qCAAA,CAAuC,CAAC;IACrE;AACD;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export type CreateReadableStreamHandler = {
|
|
|
7
7
|
* - **Yielding data**: Return a `Uint8Array` containing the next chunk of bytes.
|
|
8
8
|
* - **Ending the stream**: Return `null`, `undefined`, or an empty `Uint8Array` (`byteLength === 0`) to signal that no more data is available. This will automatically close the stream.
|
|
9
9
|
* - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.
|
|
10
|
+
*
|
|
11
|
+
* @returns {PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource} The next chunk of bytes.
|
|
10
12
|
*/
|
|
11
13
|
read: () => PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource;
|
|
12
14
|
/**
|
|
@@ -18,6 +20,9 @@ export type CreateReadableStreamHandler = {
|
|
|
18
20
|
* - The stream or its reader is explicitly canceled. (type: `"Cancel"`)
|
|
19
21
|
* - The provided `AbortSignal` fires an `abort` event. (type: `"SignalAbort"`)
|
|
20
22
|
* - An error occurs during a read operation. (type: `"Error"`)
|
|
23
|
+
*
|
|
24
|
+
* @param {CreateReadableStreamHandlerReleaseType} type The type of the release operation.
|
|
25
|
+
* @param {unknown} reason The reason for the release operation.
|
|
21
26
|
*/
|
|
22
27
|
release?: (type: CreateReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void;
|
|
23
28
|
};
|
|
@@ -32,16 +37,68 @@ export type CreateReadableStreamOptions = {
|
|
|
32
37
|
/**
|
|
33
38
|
* Creates a `ReadableStream` that yields byte data using the provided custom handler.
|
|
34
39
|
*
|
|
35
|
-
* This function does not throw errors.
|
|
36
40
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
37
41
|
* the caller must check and handle it manually.
|
|
38
|
-
* The resulting stream includes a BYOB (Bring Your Own Buffer) reader if supported by the runtime.
|
|
39
42
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
44
|
+
* If unsupported, only a default reader is available.
|
|
45
|
+
*
|
|
46
|
+
* @param {CreateReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.
|
|
47
|
+
* @param {CreateReadableStreamOptions} options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.
|
|
48
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
43
49
|
*/
|
|
44
50
|
export declare function createReadableStream(handler: CreateReadableStreamHandler, options?: CreateReadableStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
51
|
+
export type CreateBYOBReadableStreamHandlerReleaseType = "Close" | "Cancel" | "SignalAbort" | "Error";
|
|
52
|
+
export type CreateBYOBReadableStreamHandler = {
|
|
53
|
+
/**
|
|
54
|
+
* A callback invoked when the stream's consumer requests more data.
|
|
55
|
+
*
|
|
56
|
+
* - **Yielding data**: Write bytes into the provided `buffer` and return the number of bytes written (1 or greater). The returned value must not exceed `buffer.byteLength`.
|
|
57
|
+
* - **Ending the stream**: Return `0` to signal that no more data is available. This will automatically close the stream.
|
|
58
|
+
* - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.
|
|
59
|
+
*
|
|
60
|
+
* @param {Uint8Array<ArrayBuffer>} buffer The buffer to write the data into.
|
|
61
|
+
* @returns {PromiseLike<number> | number} The number of bytes written.
|
|
62
|
+
*/
|
|
63
|
+
read: (buffer: Uint8Array<ArrayBuffer>) => PromiseLike<number> | number;
|
|
64
|
+
/**
|
|
65
|
+
* An optional callback for performing cleanup operations.
|
|
66
|
+
*
|
|
67
|
+
* This function is guaranteed to be invoked at most once.
|
|
68
|
+
* It is automatically triggered when the stream terminates under any of the following conditions:
|
|
69
|
+
* - The stream's reader is successfully read to the end. (type: `"Close"`)
|
|
70
|
+
* - The stream or its reader is explicitly canceled. (type: `"Cancel"`)
|
|
71
|
+
* - The provided `AbortSignal` fires an `abort` event. (type: `"SignalAbort"`)
|
|
72
|
+
* - An error occurs during a read operation. (type: `"Error"`)
|
|
73
|
+
*
|
|
74
|
+
* @param {CreateBYOBReadableStreamHandlerReleaseType} type The type of the release operation.
|
|
75
|
+
* @param {unknown} reason The reason for the release operation.
|
|
76
|
+
*/
|
|
77
|
+
release?: (type: CreateBYOBReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void;
|
|
78
|
+
};
|
|
79
|
+
export type CreateBYOBReadableStreamOptions = {
|
|
80
|
+
/**
|
|
81
|
+
* An `AbortSignal` to abort the stream.
|
|
82
|
+
*
|
|
83
|
+
* When the abort event is fired, the handler's `release` function will be called.
|
|
84
|
+
*/
|
|
85
|
+
signal?: AbortSignal;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Creates a `ReadableStream` that yields byte data using a BYOB-style handler.
|
|
89
|
+
*
|
|
90
|
+
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
91
|
+
* the caller must check and handle it manually.
|
|
92
|
+
*
|
|
93
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
94
|
+
* If unsupported, only a default reader is available.
|
|
95
|
+
*
|
|
96
|
+
* @param {CreateBYOBReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateBYOBReadableStreamHandler` for details.
|
|
97
|
+
* @param {number} defaultBufferSize The default size of the buffer passed to `handler.read`. Must be a positive safe integer. Used as `autoAllocateChunkSize` when a bytes-type reader is available. If unsupported, used as the size of the internal buffer for a default reader.
|
|
98
|
+
* @param {CreateBYOBReadableStreamOptions} options Optional settings: `signal`. See `CreateBYOBReadableStreamOptions` for details.
|
|
99
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
100
|
+
*/
|
|
101
|
+
export declare function createBYOBReadableStream(handler: CreateBYOBReadableStreamHandler, defaultBufferSize: number, options?: CreateBYOBReadableStreamOptions): ReadableStream<Uint8Array<ArrayBuffer>>;
|
|
45
102
|
export type CreateWritableStreamHandlerReleaseType = "Close" | "Abort" | "SignalAbort" | "Error";
|
|
46
103
|
export type CreateWritableStreamHandler = {
|
|
47
104
|
/**
|
|
@@ -50,6 +107,8 @@ export type CreateWritableStreamHandler = {
|
|
|
50
107
|
* - **Data Chunk**: Receives a `Uint8Array` containing the data. The exact size and memory reference of this chunk depend on the stream's `bufferSize` and `strictBufferSize` options.
|
|
51
108
|
* - **Data Safety**: If `options.useBufferView` is `true`, the `chunk` might be a direct view (subarray) of the internal buffer. To prevent data corruption, do not retain references to this view outside this callback.
|
|
52
109
|
* - **Handling Errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.
|
|
110
|
+
*
|
|
111
|
+
* @param {Uint8Array<ArrayBuffer>} chunk The chunk of byte data to write.
|
|
53
112
|
*/
|
|
54
113
|
write: (chunk: Uint8Array<ArrayBuffer>) => PromiseLike<void> | void;
|
|
55
114
|
/**
|
|
@@ -61,6 +120,9 @@ export type CreateWritableStreamHandler = {
|
|
|
61
120
|
* - The stream or its writer is explicitly aborted. (type: `"Abort"`)
|
|
62
121
|
* - The provided `AbortSignal` fires an `abort` event. (type: `"SignalAbort"`)
|
|
63
122
|
* - An error occurs during a write operation. (type: `"Error"`)
|
|
123
|
+
*
|
|
124
|
+
* @param {CreateWritableStreamHandlerReleaseType} type The type of the release operation.
|
|
125
|
+
* @param {unknown} reason The reason for the release operation.
|
|
64
126
|
*/
|
|
65
127
|
release?: (type: CreateWritableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void;
|
|
66
128
|
};
|
|
@@ -73,6 +135,7 @@ export type CreateWritableStreamOptions = {
|
|
|
73
135
|
signal?: AbortSignal;
|
|
74
136
|
/**
|
|
75
137
|
* The size of the internal buffer in bytes.
|
|
138
|
+
* Must be a zero or positive safe integer.
|
|
76
139
|
*
|
|
77
140
|
* Defaults to `0` (unbuffered).
|
|
78
141
|
*/
|
|
@@ -98,12 +161,11 @@ export type CreateWritableStreamOptions = {
|
|
|
98
161
|
/**
|
|
99
162
|
* Creates a `WritableStream` that writes byte data using the provided custom handler.
|
|
100
163
|
*
|
|
101
|
-
* This function itself does not throw errors.
|
|
102
164
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
103
165
|
* the caller must check and handle it manually.
|
|
104
166
|
*
|
|
105
|
-
* @param handler
|
|
106
|
-
* @param options
|
|
107
|
-
* @returns A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
167
|
+
* @param {CreateWritableStreamHandler} handler The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.
|
|
168
|
+
* @param {CreateWritableStreamOptions} options Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.
|
|
169
|
+
* @returns {WritableStream<Uint8Array<ArrayBufferLike>>} A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
108
170
|
*/
|
|
109
171
|
export declare function createWritableStream(handler: CreateWritableStreamHandler, options?: CreateWritableStreamOptions): WritableStream<Uint8Array<ArrayBufferLike>>;
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Creates a `ReadableStream` that yields byte data using the provided custom handler.
|
|
3
3
|
*
|
|
4
|
-
* This function does not throw errors.
|
|
5
4
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
6
5
|
* the caller must check and handle it manually.
|
|
7
|
-
* The resulting stream includes a BYOB (Bring Your Own Buffer) reader if supported by the runtime.
|
|
8
6
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
7
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
8
|
+
* If unsupported, only a default reader is available.
|
|
9
|
+
*
|
|
10
|
+
* @param {CreateReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.
|
|
11
|
+
* @param {CreateReadableStreamOptions} options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.
|
|
12
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
12
13
|
*/
|
|
13
14
|
function createReadableStream(handler, options) {
|
|
14
15
|
let abortListener = null;
|
|
@@ -29,7 +30,10 @@ function createReadableStream(handler, options) {
|
|
|
29
30
|
}
|
|
30
31
|
return cleanupPromise;
|
|
31
32
|
}
|
|
32
|
-
|
|
33
|
+
const useByteStream = (options?.__internal_useByteStream__ !== undefined)
|
|
34
|
+
? options.__internal_useByteStream__
|
|
35
|
+
: isReadableByteStreamAvailable();
|
|
36
|
+
if (!useByteStream) {
|
|
33
37
|
return new ReadableStream({
|
|
34
38
|
start(controller) {
|
|
35
39
|
if (options?.signal != null) {
|
|
@@ -128,19 +132,158 @@ function createReadableStream(handler, options) {
|
|
|
128
132
|
}
|
|
129
133
|
});
|
|
130
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Creates a `ReadableStream` that yields byte data using a BYOB-style handler.
|
|
137
|
+
*
|
|
138
|
+
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
139
|
+
* the caller must check and handle it manually.
|
|
140
|
+
*
|
|
141
|
+
* The resulting stream can provide a BYOB reader if supported by the runtime.
|
|
142
|
+
* If unsupported, only a default reader is available.
|
|
143
|
+
*
|
|
144
|
+
* @param {CreateBYOBReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateBYOBReadableStreamHandler` for details.
|
|
145
|
+
* @param {number} defaultBufferSize The default size of the buffer passed to `handler.read`. Must be a positive safe integer. Used as `autoAllocateChunkSize` when a bytes-type reader is available. If unsupported, used as the size of the internal buffer for a default reader.
|
|
146
|
+
* @param {CreateBYOBReadableStreamOptions} options Optional settings: `signal`. See `CreateBYOBReadableStreamOptions` for details.
|
|
147
|
+
* @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.
|
|
148
|
+
*/
|
|
149
|
+
function createBYOBReadableStream(handler, defaultBufferSize, options) {
|
|
150
|
+
defaultBufferSize = Math.ceil(defaultBufferSize);
|
|
151
|
+
requiresNonzeroSafeInt(defaultBufferSize, "defaultBufferSize");
|
|
152
|
+
let abortListener = null;
|
|
153
|
+
let buffer = null;
|
|
154
|
+
let cleanupPromise = null;
|
|
155
|
+
function cleanup(type, reason) {
|
|
156
|
+
if (cleanupPromise === null) {
|
|
157
|
+
cleanupPromise = (async () => {
|
|
158
|
+
buffer = null;
|
|
159
|
+
if (options?.signal != null && abortListener != null) {
|
|
160
|
+
options.signal.removeEventListener("abort", abortListener);
|
|
161
|
+
abortListener = null;
|
|
162
|
+
}
|
|
163
|
+
if (handler.release != null) {
|
|
164
|
+
await handler.release(type, reason);
|
|
165
|
+
}
|
|
166
|
+
})();
|
|
167
|
+
}
|
|
168
|
+
return cleanupPromise;
|
|
169
|
+
}
|
|
170
|
+
const useByteStream = (options?.__internal_useByteStream__ !== undefined)
|
|
171
|
+
? options.__internal_useByteStream__
|
|
172
|
+
: isReadableByteStreamAvailable();
|
|
173
|
+
if (!useByteStream) {
|
|
174
|
+
return new ReadableStream({
|
|
175
|
+
start(controller) {
|
|
176
|
+
if (options?.signal != null) {
|
|
177
|
+
abortListener = () => {
|
|
178
|
+
const reason = options?.signal?.reason ?? newAbortSignalDefaultError();
|
|
179
|
+
cleanup("SignalAbort", reason).catch(() => { });
|
|
180
|
+
controller.error(reason);
|
|
181
|
+
};
|
|
182
|
+
options?.signal?.addEventListener("abort", abortListener);
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
async pull(controller) {
|
|
186
|
+
try {
|
|
187
|
+
throwIfAborted(options?.signal);
|
|
188
|
+
if (buffer === null) {
|
|
189
|
+
buffer = new Uint8Array(defaultBufferSize);
|
|
190
|
+
}
|
|
191
|
+
throwIfAborted(options?.signal);
|
|
192
|
+
const nread = await handler.read(buffer);
|
|
193
|
+
throwIfAborted(options?.signal);
|
|
194
|
+
requiresSafeUint(nread, "nread");
|
|
195
|
+
if (nread === 0) {
|
|
196
|
+
await cleanup("Close");
|
|
197
|
+
controller.close();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
controller.enqueue(buffer.slice(0, nread));
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
const isSignalAbort = isThrownByAbortSignal(e, options?.signal);
|
|
204
|
+
await cleanup(isSignalAbort ? "SignalAbort" : "Error", e).catch(() => { });
|
|
205
|
+
throw e;
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
async cancel(reason) {
|
|
209
|
+
await cleanup("Cancel", reason);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return new ReadableStream({
|
|
214
|
+
type: "bytes",
|
|
215
|
+
autoAllocateChunkSize: defaultBufferSize,
|
|
216
|
+
start(controller) {
|
|
217
|
+
if (options?.signal) {
|
|
218
|
+
abortListener = () => {
|
|
219
|
+
const reason = options?.signal?.reason ?? newAbortSignalDefaultError();
|
|
220
|
+
cleanup("SignalAbort", reason).catch(() => { });
|
|
221
|
+
controller.error(reason);
|
|
222
|
+
};
|
|
223
|
+
options?.signal.addEventListener("abort", abortListener);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
async pull(controller) {
|
|
227
|
+
try {
|
|
228
|
+
throwIfAborted(options?.signal);
|
|
229
|
+
const byob = controller.byobRequest;
|
|
230
|
+
if (byob == null) {
|
|
231
|
+
if (buffer == null) {
|
|
232
|
+
buffer = new Uint8Array(defaultBufferSize);
|
|
233
|
+
}
|
|
234
|
+
throwIfAborted(options?.signal);
|
|
235
|
+
const nread = await handler.read(buffer);
|
|
236
|
+
throwIfAborted(options?.signal);
|
|
237
|
+
requiresSafeUint(nread, "nread");
|
|
238
|
+
if (nread === 0) {
|
|
239
|
+
await cleanup("Close");
|
|
240
|
+
controller.close();
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
controller.enqueue(buffer.slice(0, nread));
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const v = byob.view;
|
|
247
|
+
const view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength);
|
|
248
|
+
throwIfAborted(options?.signal);
|
|
249
|
+
const nread = await handler.read(view);
|
|
250
|
+
throwIfAborted(options?.signal);
|
|
251
|
+
requiresSafeUint(nread, "nread");
|
|
252
|
+
if (nread === 0) {
|
|
253
|
+
await cleanup("Close");
|
|
254
|
+
// byobRequest がある場合、respond を呼ばないと promise が解決されない。
|
|
255
|
+
// controller.close() の後だと respond(0) を読んでもエラーにはならない。
|
|
256
|
+
// https://github.com/whatwg/streams/issues/1170
|
|
257
|
+
controller.close();
|
|
258
|
+
controller.byobRequest?.respond(0);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
byob.respond(nread);
|
|
262
|
+
}
|
|
263
|
+
catch (e) {
|
|
264
|
+
const isSignalAbort = isThrownByAbortSignal(e, options?.signal);
|
|
265
|
+
await cleanup(isSignalAbort ? "SignalAbort" : "Error", e).catch(() => { });
|
|
266
|
+
throw e;
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
async cancel(reason) {
|
|
270
|
+
await cleanup("Cancel", reason);
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
131
274
|
/**
|
|
132
275
|
* Creates a `WritableStream` that writes byte data using the provided custom handler.
|
|
133
276
|
*
|
|
134
|
-
* This function itself does not throw errors.
|
|
135
277
|
* If you need to throw an error early when the provided `options.signal` is already aborted,
|
|
136
278
|
* the caller must check and handle it manually.
|
|
137
279
|
*
|
|
138
|
-
* @param handler
|
|
139
|
-
* @param options
|
|
140
|
-
* @returns A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
280
|
+
* @param {CreateWritableStreamHandler} handler The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.
|
|
281
|
+
* @param {CreateWritableStreamOptions} options Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.
|
|
282
|
+
* @returns {WritableStream<Uint8Array<ArrayBufferLike>>} A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.
|
|
141
283
|
*/
|
|
142
284
|
function createWritableStream(handler, options) {
|
|
143
|
-
const bufferSize =
|
|
285
|
+
const bufferSize = options?.bufferSize ?? 0;
|
|
286
|
+
requiresSafeUint(bufferSize, "bufferSize");
|
|
144
287
|
let abortListener = null;
|
|
145
288
|
let buffer = null;
|
|
146
289
|
let bufferOffset = 0;
|
|
@@ -266,6 +409,24 @@ function mapToArrayBuffer(buffer) {
|
|
|
266
409
|
? buffer
|
|
267
410
|
: new Uint8Array(buffer);
|
|
268
411
|
}
|
|
412
|
+
function requiresSafeUint(num, numName) {
|
|
413
|
+
const name = numName ?? "value";
|
|
414
|
+
if (!Number.isSafeInteger(num)) {
|
|
415
|
+
throw new TypeError(`${name} must be a safe integer.`);
|
|
416
|
+
}
|
|
417
|
+
if (num < 0) {
|
|
418
|
+
throw new RangeError(`${name} must be a positive integer.`);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
function requiresNonzeroSafeInt(num, numName) {
|
|
422
|
+
const name = numName;
|
|
423
|
+
if (!Number.isSafeInteger(num)) {
|
|
424
|
+
throw new TypeError(`${name} must be a safe integer.`);
|
|
425
|
+
}
|
|
426
|
+
if (num <= 0) {
|
|
427
|
+
throw new RangeError(`${name} must be a non-zero positive integer.`);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
269
430
|
|
|
270
|
-
export { createReadableStream, createWritableStream };
|
|
431
|
+
export { createBYOBReadableStream, createReadableStream, createWritableStream };
|
|
271
432
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["export type CreateReadableStreamHandlerSource = Uint8Array<ArrayBuffer> | null | undefined\n\nexport type CreateReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t * \n\t * - **Yielding data**: Return a `Uint8Array` containing the next chunk of bytes.\n\t * - **Ending the stream**: Return `null`, `undefined`, or an empty `Uint8Array` (`byteLength === 0`) to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t */\n\tread: () => PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource,\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t */\n\trelease?: (type: CreateReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using the provided custom handler.\n * \n * This function does not throw errors. \n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * The resulting stream includes a BYOB (Bring Your Own Buffer) reader if supported by the runtime.\n * \n * @param handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.\n * @param options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.\n * @returns A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createReadableStream(\n\thandler: CreateReadableStreamHandler,\n\toptions?: CreateReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tif (!isReadableByteStreamAvailable()) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst data = await handler.read()\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (data == null || data.byteLength === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(data)\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\t// autoAllocateChunkSize を指定すると stream.getReader() でも byob が使われるようになるが、\n\t// この実装で byob を用いてもコピーが増えるだけで恩恵が少ないため指定しない。\n\t// また type: \"bytes\" で strategy を指定すると (正確には size を定義すると) エラーになる点にも注意。\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tbuffer = (await handler.read()) ?? null\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t}\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されないことに注意\n\t\t\t\tif (byob != null) {\n\t\t\t\t\t// respond する前なので null にならない\n\t\t\t\t\tconst v = byob.view!!\n\t\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\t\t\t\t\tconst nread = Math.min(buffer.byteLength, view.byteLength)\n\n\t\t\t\t\tview.set(buffer.subarray(0, nread))\n\t\t\t\t\tbuffer = buffer.subarray(nread)\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tbyob.respond(nread)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tcontroller.enqueue(buffer)\n\t\t\t\t\tbuffer = null\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\n\t\t\t\t// byobRequest が存在する場合、controller.close() を呼んだだけでは\n\t\t\t\t// Promise は解決されず、respond() も呼ぶ必要がある。\n\t\t\t\t// controller.error() も同様の挙動になる可能性がある。(要検証)\n\t\t\t\t// 少なくとも throw すれば Promise は解決されるため、現状はこの実装とする。\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateWritableStreamHandlerReleaseType = \"Close\" | \"Abort\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateWritableStreamHandler = {\n\n\t/**\n\t * A callback invoked when a new chunk of byte data is ready to be written.\n\t * \n\t * - **Data Chunk**: Receives a `Uint8Array` containing the data. The exact size and memory reference of this chunk depend on the stream's `bufferSize` and `strictBufferSize` options.\n\t * - **Data Safety**: If `options.useBufferView` is `true`, the `chunk` might be a direct view (subarray) of the internal buffer. To prevent data corruption, do not retain references to this view outside this callback.\n\t * - **Handling Errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t */\n\twrite: (chunk: Uint8Array<ArrayBuffer>) => PromiseLike<void> | void,\n\n\t/**\n\t * A callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream or its writer is explicitly closed. (type: `\"Close\"`)\n\t * - The stream or its writer is explicitly aborted. (type: `\"Abort\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a write operation. (type: `\"Error\"`)\n\t */\n\trelease?: (\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t) => PromiseLike<void> | void,\n}\n\nexport type CreateWritableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * The size of the internal buffer in bytes.\n\t * \n\t * Defaults to `0` (unbuffered).\n\t */\n\tbufferSize?: number,\n\n\t/**\n\t * If `true`, the stream strictly enforces the `bufferSize` for every chunk passed to the handler.\n\t * Only the final `write` call may receive a chunk smaller than the `bufferSize`.\n\t * \n\t * If `false`, chunks larger than the `bufferSize` will bypass the internal buffer and be processed directly.\n\t * \n\t * Defaults to `false`.\n\t */\n\tstrictBufferSize?: boolean,\n\n\t/**\n\t * If `true`, the handler's `write` method can receive a chunk as a view (subarray) of the internal buffer.\n\t * This reduces the number of memory copies, but the received chunk must not be referenced outside the `write` method.\n\t * If you need to retain the chunk data externally, you must make a copy of it within the `write` method.\n\t * \n\t * Defaults to `false`.\n\t */\n\tuseBufferView?: boolean,\n}\n\n/**\n * Creates a `WritableStream` that writes byte data using the provided custom handler.\n * \n * This function itself does not throw errors.\n * If you need to throw an error early when the provided `options.signal` is already aborted, \n * the caller must check and handle it manually.\n * \n * @param handler - The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.\n * @param options - Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.\n * @returns A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.\n */\nexport function createWritableStream(\n\thandler: CreateWritableStreamHandler,\n\toptions?: CreateWritableStreamOptions,\n): WritableStream<Uint8Array<ArrayBufferLike>> {\n\n\tconst bufferSize = Math.max(0, Math.ceil(options?.bufferSize ?? 0))\n\n\tlet abortListener: (() => void) | null = null;\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null;\n\tlet bufferOffset = 0;\n\n\tlet cleanupPromise: Promise<void> | null = null;\n\tfunction cleanup(\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions?.signal?.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\treturn new WritableStream<Uint8Array<ArrayBufferLike>>({\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal != null) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener);\n\t\t\t}\n\t\t},\n\n\t\tasync write(src) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t// bufferSize が 0 以下の場合や src が buffer より大きい場合は buffer を使わずに処理する。\n\t\t\t\tif (\n\t\t\t\t\tbufferSize <= 0 ||\n\t\t\t\t\t(bufferSize <= src.byteLength && options?.strictBufferSize !== true)\n\t\t\t\t) {\n\n\t\t\t\t\t// buffer に既にデータがある場合、それを処理する。\n\t\t\t\t\tif (buffer !== null && 0 < bufferOffset) {\n\t\t\t\t\t\tconst chunk = options?.useBufferView === true\n\t\t\t\t\t\t\t? buffer.subarray(0, bufferOffset)\n\t\t\t\t\t\t\t: buffer.slice(0, bufferOffset)\n\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tawait handler.write(mapToArrayBuffer(src))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tlet srcOffset = 0;\n\t\t\t\twhile (srcOffset < src.byteLength) {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(bufferSize)\n\t\t\t\t\t}\n\n\t\t\t\t\tconst n = Math.min(bufferSize - bufferOffset, src.byteLength - srcOffset)\n\t\t\t\t\tbuffer.set(src.subarray(srcOffset, srcOffset + n), bufferOffset)\n\t\t\t\t\tbufferOffset += n\n\t\t\t\t\tsrcOffset += n\n\n\t\t\t\t\tif (bufferOffset === bufferSize) {\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\t\tconst chunk = buffer\n\t\t\t\t\t\tif (options?.useBufferView !== true) {\n\t\t\t\t\t\t\tbuffer = null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync close() {\n\t\t\ttry {\n\t\t\t\tif (0 < bufferOffset && buffer != null) {\n\t\t\t\t\tconst view = buffer.subarray(0, bufferOffset)\n\t\t\t\t\tbuffer = null\n\t\t\t\t\tawait handler.write(view)\n\t\t\t\t}\n\t\t\t\tawait cleanup(\"Close\")\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tawait cleanup(\"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync abort(reason) {\n\t\t\tawait cleanup(\"Abort\", reason)\n\t\t}\n\t})\n}\n\n\nlet _isReadableByteStreamAvailable: boolean | null = null\nfunction isReadableByteStreamAvailable() {\n\tif (_isReadableByteStreamAvailable === null) {\n\t\ttry {\n\t\t\tnew ReadableStream({ type: \"bytes\" })\n\t\t\t_isReadableByteStreamAvailable = true\n\t\t}\n\t\tcatch {\n\t\t\t_isReadableByteStreamAvailable = false\n\t\t}\n\t}\n\n\treturn _isReadableByteStreamAvailable\n}\n\nfunction throwIfAborted(signal: AbortSignal | undefined | null) {\n\tif (signal?.aborted === true) {\n\t\tthrow (signal?.reason ?? newAbortSignalDefaultError())\n\t}\n}\n\nfunction newAbortSignalDefaultError(): Error {\n\treturn new DOMException(\"The operation was aborted.\", \"AbortError\")\n}\n\nfunction isThrownByAbortSignal(err: unknown, signal: AbortSignal | null | undefined): boolean {\n\treturn (signal?.aborted === true) &&\n\t\t(err === signal.reason || (err instanceof DOMException && err.name === \"AbortError\"));\n}\n\nfunction mapToArrayBuffer(\n\tbuffer: Uint8Array<ArrayBufferLike>\n): Uint8Array<ArrayBuffer> {\n\n\treturn buffer.buffer instanceof ArrayBuffer\n\t\t? buffer as Uint8Array<ArrayBuffer>\n\t\t: new Uint8Array(buffer)\n}\n"],"names":[],"mappings":"AAsCA;;;;;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,GAAG,YAAY,YAAY,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC;AACvF;AAEA,SAAS,gBAAgB,CACxB,MAAmC,EAAA;AAGnC,IAAA,OAAO,MAAM,CAAC,MAAM,YAAY;AAC/B,UAAE;AACF,UAAE,IAAI,UAAU,CAAC,MAAM,CAAC;AAC1B;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["export type CreateReadableStreamHandlerSource = Uint8Array<ArrayBuffer> | null | undefined\n\nexport type CreateReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t * \n\t * - **Yielding data**: Return a `Uint8Array` containing the next chunk of bytes.\n\t * - **Ending the stream**: Return `null`, `undefined`, or an empty `Uint8Array` (`byteLength === 0`) to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @returns {PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource} The next chunk of bytes.\n\t */\n\tread: () => PromiseLike<CreateReadableStreamHandlerSource> | CreateReadableStreamHandlerSource,\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateReadableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (type: CreateReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * @internal\n\t * @ignore\n\t */\n\t__internal_useByteStream__?: boolean\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using the provided custom handler.\n * \n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * \n * The resulting stream can provide a BYOB reader if supported by the runtime.\n * If unsupported, only a default reader is available.\n * \n * @param {CreateReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateReadableStreamHandler` for details.\n * @param {CreateReadableStreamOptions} options Optional settings: `signal`. See `CreateReadableStreamOptions` for details.\n * @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createReadableStream(\n\thandler: CreateReadableStreamHandler,\n\toptions?: CreateReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tconst useByteStream = (options?.__internal_useByteStream__ !== undefined)\n\t\t? options.__internal_useByteStream__\n\t\t: isReadableByteStreamAvailable()\n\n\tif (!useByteStream) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst data = await handler.read()\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (data == null || data.byteLength === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(data)\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\t// autoAllocateChunkSize を指定すると stream.getReader() でも byob が使われるようになるが、\n\t// この実装で byob を用いてもコピーが増えるだけで恩恵が少ないため指定しない。\n\t// また type: \"bytes\" で strategy を指定すると (正確には size を定義すると) エラーになる点にも注意。\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tbuffer = (await handler.read()) ?? null\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t}\n\t\t\t\tif (buffer == null || buffer.byteLength === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されないことに注意\n\t\t\t\tif (byob != null) {\n\t\t\t\t\t// respond する前なので null にならない\n\t\t\t\t\tconst v = byob.view!!\n\t\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\t\t\t\t\tconst nread = Math.min(buffer.byteLength, view.byteLength)\n\n\t\t\t\t\tview.set(buffer.subarray(0, nread))\n\t\t\t\t\tbuffer = buffer.subarray(nread)\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tbyob.respond(nread)\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tcontroller.enqueue(buffer)\n\t\t\t\t\tbuffer = null\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\n\t\t\t\t// byobRequest が存在する場合、controller.close() を呼んだだけでは\n\t\t\t\t// Promise は解決されず、respond() も呼ぶ必要がある。\n\t\t\t\t// controller.error() も同様の挙動になる可能性がある。(要検証)\n\t\t\t\t// 少なくとも throw すれば Promise は解決されるため、現状はこの実装とする。\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateBYOBReadableStreamHandlerReleaseType = \"Close\" | \"Cancel\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateBYOBReadableStreamHandler = {\n\n\t/**\n\t * A callback invoked when the stream's consumer requests more data.\n\t *\n\t * - **Yielding data**: Write bytes into the provided `buffer` and return the number of bytes written (1 or greater). The returned value must not exceed `buffer.byteLength`.\n\t * - **Ending the stream**: Return `0` to signal that no more data is available. This will automatically close the stream.\n\t * - **Handling errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @param {Uint8Array<ArrayBuffer>} buffer The buffer to write the data into.\n\t * @returns {PromiseLike<number> | number} The number of bytes written.\n\t */\n\tread: (buffer: Uint8Array<ArrayBuffer>) => PromiseLike<number> | number\n\n\t/**\n\t * An optional callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream's reader is successfully read to the end. (type: `\"Close\"`)\n\t * - The stream or its reader is explicitly canceled. (type: `\"Cancel\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a read operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateBYOBReadableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (type: CreateBYOBReadableStreamHandlerReleaseType, reason?: unknown) => PromiseLike<void> | void,\n}\n\nexport type CreateBYOBReadableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * @internal\n\t * @ignore\n\t */\n\t__internal_useByteStream__?: boolean\n}\n\n/**\n * Creates a `ReadableStream` that yields byte data using a BYOB-style handler.\n *\n * If you need to throw an error early when the provided `options.signal` is already aborted,\n * the caller must check and handle it manually.\n * \n * The resulting stream can provide a BYOB reader if supported by the runtime.\n * If unsupported, only a default reader is available.\n *\n * @param {CreateBYOBReadableStreamHandler} handler The stream handler: `read`, `release`. See `CreateBYOBReadableStreamHandler` for details.\n * @param {number} defaultBufferSize The default size of the buffer passed to `handler.read`. Must be a positive safe integer. Used as `autoAllocateChunkSize` when a bytes-type reader is available. If unsupported, used as the size of the internal buffer for a default reader.\n * @param {CreateBYOBReadableStreamOptions} options Optional settings: `signal`. See `CreateBYOBReadableStreamOptions` for details.\n * @returns {ReadableStream<Uint8Array<ArrayBuffer>>} A `ReadableStream<Uint8Array<ArrayBuffer>>` configured with the provided handler and options.\n */\nexport function createBYOBReadableStream(\n\thandler: CreateBYOBReadableStreamHandler,\n\tdefaultBufferSize: number,\n\toptions?: CreateBYOBReadableStreamOptions,\n): ReadableStream<Uint8Array<ArrayBuffer>> {\n\n\tdefaultBufferSize = Math.ceil(defaultBufferSize)\n\trequiresNonzeroSafeInt(defaultBufferSize, \"defaultBufferSize\")\n\n\tlet abortListener: (() => void) | null = null\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null\n\n\tlet cleanupPromise: Promise<void> | null = null\n\tfunction cleanup(\n\t\ttype: CreateBYOBReadableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\tconst useByteStream = (options?.__internal_useByteStream__ !== undefined)\n\t\t? options.__internal_useByteStream__\n\t\t: isReadableByteStreamAvailable()\n\n\tif (!useByteStream) {\n\t\treturn new ReadableStream({\n\n\t\t\tstart(controller) {\n\t\t\t\tif (options?.signal != null) {\n\t\t\t\t\tabortListener = () => {\n\t\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t\t}\n\t\t\t\t\toptions?.signal?.addEventListener(\"abort\", abortListener);\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync pull(controller) {\n\t\t\t\ttry {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(defaultBufferSize)\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst nread = await handler.read(buffer)\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\t\tif (nread === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(buffer.slice(0, nread))\n\t\t\t\t}\n\t\t\t\tcatch (e) {\n\t\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\t\tthrow e\n\t\t\t\t}\n\t\t\t},\n\n\t\t\tasync cancel(reason) {\n\t\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t\t}\n\t\t})\n\t}\n\n\treturn new ReadableStream({\n\t\ttype: \"bytes\",\n\t\tautoAllocateChunkSize: defaultBufferSize,\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener)\n\t\t\t}\n\t\t},\n\n\t\tasync pull(controller) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tconst byob = controller.byobRequest\n\t\t\t\tif (byob == null) {\n\t\t\t\t\tif (buffer == null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(defaultBufferSize)\n\t\t\t\t\t}\n\t\t\t\t\t\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tconst nread = await handler.read(buffer)\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\t\tif (nread === 0) {\n\t\t\t\t\t\tawait cleanup(\"Close\")\n\t\t\t\t\t\tcontroller.close()\n\t\t\t\t\t\treturn\n\t\t\t\t\t}\n\n\t\t\t\t\tcontroller.enqueue(buffer.slice(0, nread))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tconst v = byob.view!!\n\t\t\t\tconst view = new Uint8Array(v.buffer, v.byteOffset, v.byteLength)\n\n\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\tconst nread = await handler.read(view)\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\trequiresSafeUint(nread, \"nread\")\n\t\t\t\tif (nread === 0) {\n\t\t\t\t\tawait cleanup(\"Close\")\n\n\t\t\t\t\t// byobRequest がある場合、respond を呼ばないと promise が解決されない。\n\t\t\t\t\t// controller.close() の後だと respond(0) を読んでもエラーにはならない。\n\t\t\t\t\t// https://github.com/whatwg/streams/issues/1170\n\t\t\t\t\tcontroller.close()\n\t\t\t\t\tcontroller.byobRequest?.respond(0)\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tbyob.respond(nread)\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync cancel(reason) {\n\t\t\tawait cleanup(\"Cancel\", reason)\n\t\t}\n\t})\n}\n\n\nexport type CreateWritableStreamHandlerReleaseType = \"Close\" | \"Abort\" | \"SignalAbort\" | \"Error\"\n\nexport type CreateWritableStreamHandler = {\n\n\t/**\n\t * A callback invoked when a new chunk of byte data is ready to be written.\n\t * \n\t * - **Data Chunk**: Receives a `Uint8Array` containing the data. The exact size and memory reference of this chunk depend on the stream's `bufferSize` and `strictBufferSize` options.\n\t * - **Data Safety**: If `options.useBufferView` is `true`, the `chunk` might be a direct view (subarray) of the internal buffer. To prevent data corruption, do not retain references to this view outside this callback.\n\t * - **Handling Errors**: If an error is thrown inside this function, the stream will enter an error state and terminate.\n\t * \n\t * @param {Uint8Array<ArrayBuffer>} chunk The chunk of byte data to write.\n\t */\n\twrite: (chunk: Uint8Array<ArrayBuffer>) => PromiseLike<void> | void,\n\n\t/**\n\t * A callback for performing cleanup operations.\n\t * \n\t * This function is guaranteed to be invoked at most once. \n\t * It is automatically triggered when the stream terminates under any of the following conditions:\n\t * - The stream or its writer is explicitly closed. (type: `\"Close\"`)\n\t * - The stream or its writer is explicitly aborted. (type: `\"Abort\"`)\n\t * - The provided `AbortSignal` fires an `abort` event. (type: `\"SignalAbort\"`)\n\t * - An error occurs during a write operation. (type: `\"Error\"`)\n\t * \n\t * @param {CreateWritableStreamHandlerReleaseType} type The type of the release operation.\n\t * @param {unknown} reason The reason for the release operation.\n\t */\n\trelease?: (\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t) => PromiseLike<void> | void,\n}\n\nexport type CreateWritableStreamOptions = {\n\n\t/**\n\t * An `AbortSignal` to abort the stream.\n\t * \n\t * When the abort event is fired, the handler's `release` function will be called.\n\t */\n\tsignal?: AbortSignal,\n\n\t/**\n\t * The size of the internal buffer in bytes. \n\t * Must be a zero or positive safe integer. \n\t * \n\t * Defaults to `0` (unbuffered).\n\t */\n\tbufferSize?: number,\n\n\t/**\n\t * If `true`, the stream strictly enforces the `bufferSize` for every chunk passed to the handler.\n\t * Only the final `write` call may receive a chunk smaller than the `bufferSize`.\n\t * \n\t * If `false`, chunks larger than the `bufferSize` will bypass the internal buffer and be processed directly.\n\t * \n\t * Defaults to `false`.\n\t */\n\tstrictBufferSize?: boolean,\n\n\t/**\n\t * If `true`, the handler's `write` method can receive a chunk as a view (subarray) of the internal buffer.\n\t * This reduces the number of memory copies, but the received chunk must not be referenced outside the `write` method.\n\t * If you need to retain the chunk data externally, you must make a copy of it within the `write` method.\n\t * \n\t * Defaults to `false`.\n\t */\n\tuseBufferView?: boolean,\n}\n\n/**\n * Creates a `WritableStream` that writes byte data using the provided custom handler.\n * \n * If you need to throw an error early when the provided `options.signal` is already aborted, \n * the caller must check and handle it manually.\n * \n * @param {CreateWritableStreamHandler} handler The stream handler: `write`, `release`. See `CreateWritableStreamHandler` for details.\n * @param {CreateWritableStreamOptions} options Optional settings: `signal`, `bufferSize`, `strictBufferSize`, `useBufferView`. See `CreateWritableStreamOptions` for details.\n * @returns {WritableStream<Uint8Array<ArrayBufferLike>>} A `WritableStream<Uint8Array<ArrayBufferLike>>` instance configured with the provided handler and options.\n */\nexport function createWritableStream(\n\thandler: CreateWritableStreamHandler,\n\toptions?: CreateWritableStreamOptions,\n): WritableStream<Uint8Array<ArrayBufferLike>> {\n\n\tconst bufferSize = options?.bufferSize ?? 0\n\trequiresSafeUint(bufferSize, \"bufferSize\")\n\n\tlet abortListener: (() => void) | null = null;\n\tlet buffer: Uint8Array<ArrayBuffer> | null = null;\n\tlet bufferOffset = 0;\n\n\tlet cleanupPromise: Promise<void> | null = null;\n\tfunction cleanup(\n\t\ttype: CreateWritableStreamHandlerReleaseType,\n\t\treason?: unknown\n\t): Promise<void> {\n\n\t\tif (cleanupPromise === null) {\n\t\t\tcleanupPromise = (async () => {\n\t\t\t\tbuffer = null\n\t\t\t\tif (options?.signal != null && abortListener != null) {\n\t\t\t\t\toptions?.signal?.removeEventListener(\"abort\", abortListener)\n\t\t\t\t\tabortListener = null\n\t\t\t\t}\n\t\t\t\tif (handler.release != null) {\n\t\t\t\t\tawait handler.release(type, reason)\n\t\t\t\t}\n\t\t\t})()\n\t\t}\n\t\treturn cleanupPromise\n\t}\n\n\treturn new WritableStream<Uint8Array<ArrayBufferLike>>({\n\n\t\tstart(controller) {\n\t\t\tif (options?.signal != null) {\n\t\t\t\tabortListener = () => {\n\t\t\t\t\tconst reason = options?.signal?.reason ?? newAbortSignalDefaultError()\n\t\t\t\t\tcleanup(\"SignalAbort\", reason).catch(() => { })\n\t\t\t\t\tcontroller.error(reason)\n\t\t\t\t}\n\t\t\t\toptions?.signal.addEventListener(\"abort\", abortListener);\n\t\t\t}\n\t\t},\n\n\t\tasync write(src) {\n\t\t\ttry {\n\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t// bufferSize が 0 以下の場合や src が buffer より大きい場合は buffer を使わずに処理する。\n\t\t\t\tif (\n\t\t\t\t\tbufferSize <= 0 ||\n\t\t\t\t\t(bufferSize <= src.byteLength && options?.strictBufferSize !== true)\n\t\t\t\t) {\n\n\t\t\t\t\t// buffer に既にデータがある場合、それを処理する。\n\t\t\t\t\tif (buffer !== null && 0 < bufferOffset) {\n\t\t\t\t\t\tconst chunk = options?.useBufferView === true\n\t\t\t\t\t\t\t? buffer.subarray(0, bufferOffset)\n\t\t\t\t\t\t\t: buffer.slice(0, bufferOffset)\n\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tawait handler.write(mapToArrayBuffer(src))\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tlet srcOffset = 0;\n\t\t\t\twhile (srcOffset < src.byteLength) {\n\t\t\t\t\tthrowIfAborted(options?.signal)\n\t\t\t\t\tif (buffer === null) {\n\t\t\t\t\t\tbuffer = new Uint8Array(bufferSize)\n\t\t\t\t\t}\n\n\t\t\t\t\tconst n = Math.min(bufferSize - bufferOffset, src.byteLength - srcOffset)\n\t\t\t\t\tbuffer.set(src.subarray(srcOffset, srcOffset + n), bufferOffset)\n\t\t\t\t\tbufferOffset += n\n\t\t\t\t\tsrcOffset += n\n\n\t\t\t\t\tif (bufferOffset === bufferSize) {\n\t\t\t\t\t\tthrowIfAborted(options?.signal)\n\n\t\t\t\t\t\tconst chunk = buffer\n\t\t\t\t\t\tif (options?.useBufferView !== true) {\n\t\t\t\t\t\t\tbuffer = null\n\t\t\t\t\t\t}\n\t\t\t\t\t\tawait handler.write(chunk)\n\t\t\t\t\t\tbufferOffset = 0\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tconst isSignalAbort = isThrownByAbortSignal(e, options?.signal)\n\t\t\t\tawait cleanup(isSignalAbort ? \"SignalAbort\" : \"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync close() {\n\t\t\ttry {\n\t\t\t\tif (0 < bufferOffset && buffer != null) {\n\t\t\t\t\tconst view = buffer.subarray(0, bufferOffset)\n\t\t\t\t\tbuffer = null\n\t\t\t\t\tawait handler.write(view)\n\t\t\t\t}\n\t\t\t\tawait cleanup(\"Close\")\n\t\t\t}\n\t\t\tcatch (e) {\n\t\t\t\tawait cleanup(\"Error\", e).catch(() => { })\n\t\t\t\tthrow e\n\t\t\t}\n\t\t},\n\n\t\tasync abort(reason) {\n\t\t\tawait cleanup(\"Abort\", reason)\n\t\t}\n\t})\n}\n\n\nlet _isReadableByteStreamAvailable: boolean | null = null\nfunction isReadableByteStreamAvailable(): boolean {\n\tif (_isReadableByteStreamAvailable === null) {\n\t\ttry {\n\t\t\tnew ReadableStream({ type: \"bytes\" })\n\t\t\t_isReadableByteStreamAvailable = true\n\t\t}\n\t\tcatch {\n\t\t\t_isReadableByteStreamAvailable = false\n\t\t}\n\t}\n\treturn _isReadableByteStreamAvailable\n}\n\nfunction throwIfAborted(signal: AbortSignal | undefined | null) {\n\tif (signal?.aborted === true) {\n\t\tthrow (signal?.reason ?? newAbortSignalDefaultError())\n\t}\n}\n\nfunction newAbortSignalDefaultError(): Error {\n\treturn new DOMException(\"The operation was aborted.\", \"AbortError\")\n}\n\nfunction isThrownByAbortSignal(err: unknown, signal: AbortSignal | null | undefined): boolean {\n\treturn (signal?.aborted === true) &&\n\t\t(err === signal.reason || (err instanceof DOMException && err.name === \"AbortError\"));\n}\n\nfunction mapToArrayBuffer(\n\tbuffer: Uint8Array<ArrayBufferLike>\n): Uint8Array<ArrayBuffer> {\n\n\treturn buffer.buffer instanceof ArrayBuffer\n\t\t? buffer as Uint8Array<ArrayBuffer>\n\t\t: new Uint8Array(buffer)\n}\n\nfunction requiresSafeUint(num: number, numName?: string): void {\n\tconst name = numName ?? \"value\";\n\n\tif (!Number.isSafeInteger(num)) {\n\t\tthrow new TypeError(`${name} must be a safe integer.`);\n\t}\n\tif (num < 0) {\n\t\tthrow new RangeError(`${name} must be a positive integer.`);\n\t}\n}\n\nfunction requiresNonzeroSafeInt(num: number, numName?: string): void {\n\tconst name = numName ?? \"value\";\n\n\tif (!Number.isSafeInteger(num)) {\n\t\tthrow new TypeError(`${name} must be a safe integer.`);\n\t}\n\tif (num <= 0) {\n\t\tthrow new RangeError(`${name} must be a non-zero positive integer.`);\n\t}\n}"],"names":[],"mappings":"AAiDA;;;;;;;;;;;;AAYG;AACG,SAAU,oBAAoB,CACnC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AAC,MAAM,YAAY;AAC/B,UAAE;AACF,UAAE,IAAI,UAAU,CAAC,MAAM,CAAC;AAC1B;AAEA,SAAS,gBAAgB,CAAC,GAAW,EAAE,OAAgB,EAAA;AACtD,IAAA,MAAM,IAAI,GAAG,OAAO,IAAI,OAAO;IAE/B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE;AAC/B,QAAA,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,CAAA,wBAAA,CAA0B,CAAC;IACvD;AACA,IAAA,IAAI,GAAG,GAAG,CAAC,EAAE;AACZ,QAAA,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,CAAA,4BAAA,CAA8B,CAAC;IAC5D;AACD;AAEA,SAAS,sBAAsB,CAAC,GAAW,EAAE,OAAgB,EAAA;AAC5D,IAAA,MAAM,IAAI,GAAG,OAAkB;IAE/B,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE;AAC/B,QAAA,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,CAAA,wBAAA,CAA0B,CAAC;IACvD;AACA,IAAA,IAAI,GAAG,IAAI,CAAC,EAAE;AACb,QAAA,MAAM,IAAI,UAAU,CAAC,GAAG,IAAI,CAAA,qCAAA,CAAuC,CAAC;IACrE;AACD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-web-stream",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"author": "OkaYu",
|
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
|
6
6
|
"description": "Create Web Readable/Writable streams from simple handlers and options.",
|
|
@@ -34,7 +34,8 @@
|
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "rollup -c",
|
|
36
36
|
"pretest": "pnpm build",
|
|
37
|
-
"prepublishOnly": "pnpm run build"
|
|
37
|
+
"prepublishOnly": "pnpm run build",
|
|
38
|
+
"test": "node --test"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"rollup": "^4.0.0",
|
|
@@ -43,7 +44,7 @@
|
|
|
43
44
|
"typescript": "^5.0.0"
|
|
44
45
|
},
|
|
45
46
|
"engines": {
|
|
46
|
-
"node": ">=18.
|
|
47
|
+
"node": ">=18.9.0"
|
|
47
48
|
},
|
|
48
49
|
"sideEffects": false
|
|
49
50
|
}
|