This commit is contained in:
2025-05-12 05:38:44 +09:00
parent dced21c3f8
commit 6d78bfa46e
8120 changed files with 1161564 additions and 0 deletions

21
book/node_modules/@sec-ant/readable-stream/LICENSE generated vendored Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Ze-Zheng Wu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

230
book/node_modules/@sec-ant/readable-stream/README.md generated vendored Normal file
View File

@ -0,0 +1,230 @@
# @sec-ant/readable-stream
[![npm version](https://img.shields.io/npm/v/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest) [![npm downloads](https://img.shields.io/npm/dm/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest) [![](https://img.shields.io/jsdelivr/npm/hm/@sec-ant/readable-stream?cacheSeconds=300&color=ff5627)](https://www.jsdelivr.com/package/npm/@sec-ant/readable-stream) [![bundlephobia minzipped](https://img.shields.io/bundlephobia/minzip/@sec-ant/readable-stream?cacheSeconds=300)](https://bundlephobia.com/package/@sec-ant/readable-stream@latest) [![npm license](https://img.shields.io/npm/l/@sec-ant/readable-stream?cacheSeconds=300)](https://www.npmjs.com/package/@sec-ant/readable-stream/v/latest)
A tiny, zero-dependency yet spec-compliant asynchronous iterator polyfill/ponyfill for [`ReadableStream`](https://developer.mozilla.org/docs/Web/API/ReadableStream)s.
## Features
### Asynchronously iterate a `ReadableStream`
With this package, you can consume a `ReadableStream` as an `AsyncIterable`.
- spec: https://streams.spec.whatwg.org/#rs-asynciterator
- tests: https://github.com/Sec-ant/readable-stream/blob/main/tests/asyncIterator.spec.ts (copied from [wpt](https://github.com/web-platform-tests/wpt/blob/309231a7f3e900d04914bc4963b016efd9989a00/streams/readable-streams/async-iterator.any.js))
### Convert an `AsyncIterable` or an `Iterable` into a `ReadableStream`
With this package, you can construct a `ReadableStream` from an `AsyncIterable` or an `Iterable`.
- spec: https://streams.spec.whatwg.org/#rs-from
- tests: https://github.com/Sec-ant/readable-stream/blob/main/tests/fromAnyIterable.spec.ts (copied from [wpt](https://github.com/web-platform-tests/wpt/blob/309231a7f3e900d04914bc4963b016efd9989a00/streams/readable-streams/from.any.js))
This package passes all the aforementioned tests.
## Install
```bash
npm i @sec-ant/readable-stream
```
## Usage
### Ponyfill
This package can be imported as a _ponyfill_ to avoid side effects:
#### `asyncIterator`
Path:
```
@sec-ant/readable-stream/ponyfill/asyncIterator
```
Example:
```ts
import {
asyncIterator,
type ReadableStreamIteratorOptions,
} from "@sec-ant/readable-stream/ponyfill/asyncIterator";
const readableStream = (await fetch("https://www.example.org/")).body;
let total = 0;
for await (const chunk of asyncIterator.call(readableStream)) {
total += chunk.length;
}
console.log(total);
```
Check https://streams.spec.whatwg.org/#rs-class-definition and https://streams.spec.whatwg.org/#rs-asynciterator for further explanation on `ReadableStreamIteratorOptions`.
#### `fromAnyIterable`
Path:
```
@sec-ant/readable-stream/ponyfill/fromAnyIterable
```
Example:
```ts
import { fromAnyIterable } from "@sec-ant/readable-stream/ponyfill/fromAnyIterable";
const readableStream = fromAnyIterable(["a", "b"]);
```
#### All-in-One
Path:
```
@sec-ant/readable-stream/ponyfill
```
Example:
```ts
import {
fromAnyIterable,
asyncIterator,
type ReadableStreamIteratorOptions,
} from "@sec-ant/readable-stream/ponyfill";
```
### Polyfill
This package can be imported as a drop-in _polyfill_ with side effects.
#### `ReadableStream.prototype[Symbol.asyncIterator]` and `ReadableStream.prototype.values`
Path:
```
@sec-ant/readable-stream/polyfill/asyncIterator
```
Example:
```ts
import "@sec-ant/readable-stream/polyfill/asyncIterator";
const readableStream = (await fetch("https://www.example.org/")).body;
let total = 0;
for await (const chunk of readableStream) {
total += chunk.length;
}
console.log(total);
```
#### `ReadableStream.from`
Path:
```
@sec-ant/readable-stream/polyfill/fromAnyIterable
```
Example:
```js
import "@sec-ant/readable-stream/polyfill/fromAnyIterable";
const readableStream = ReadableStream.from(["a", "b"]);
```
Note that `ReadableStream.from` is not typed because [declared vars cannot be overridden](https://github.com/microsoft/TypeScript/issues/36146).
#### All-in-One
Path:
```
@sec-ant/readable-stream/polyfill
```
Example:
```ts
import "@sec-ant/readable-stream/polyfill";
```
### Ponyfill + Polyfill
#### `asyncIterator`
Path:
```
@sec-ant/readable-stream/asyncIterator
```
Example:
```ts
import {
asyncIterator,
type ReadableStreamIteratorOptions,
} from "@sec-ant/readable-stream/asyncIterator";
// also with side effects
```
#### `fromAnyIterable`
Path:
```
@sec-ant/readable-stream/fromAnyIterable
```
Example:
```ts
import { fromAnyIterable } from "@sec-ant/readable-stream/fromAnyIterable";
// also with side effects
```
#### All-in-One
Path:
```
@sec-ant/readable-stream
```
Example:
```ts
import {
fromAnyIterable,
asyncIterator,
type ReadableStreamIteratorOptions,
} from "@sec-ant/readable-stream";
// also with side effects
```
### Types
You can also use this package to augment the `ReadableStream` type for async iteration if the runtime already supports it but the type system does not.
Path:
```
@sec-ant/readable-stream/async-iterator
```
Example:
```ts
/// <reference types="@sec-ant/readable-stream/async-iterator" />
```
## License
MIT

View File

@ -0,0 +1 @@
export declare const AsyncIterablePrototype: object;

View File

@ -0,0 +1,27 @@
/**
* the implementer that does all the heavy works
*/
declare class ReadableStreamAsyncIterableIteratorImpl<R, TReturn> implements AsyncIterator<R> {
#private;
constructor(reader: ReadableStreamDefaultReader<R>, preventCancel: boolean);
next(): Promise<IteratorResult<R, undefined>>;
return(value?: TReturn): Promise<IteratorReturnResult<TReturn>>;
}
declare const implementSymbol: unique symbol;
/**
* declare `ReadableStreamAsyncIterableIterator` interaface
*/
interface ReadableStreamAsyncIterableIterator<R, TReturn> extends AsyncIterableIterator<R> {
[implementSymbol]: ReadableStreamAsyncIterableIteratorImpl<R, TReturn>;
}
export interface ReadableStreamIteratorOptions {
preventCancel?: boolean;
}
/**
* Get an async iterable iterator from a readable stream
* @param this
* @param readableStreamIteratorOptions
* @returns
*/
export declare function asyncIterator<R, TReturn>(this: ReadableStream<R>, { preventCancel }?: ReadableStreamIteratorOptions): ReadableStreamAsyncIterableIterator<R, TReturn>;
export {};

View File

@ -0,0 +1,6 @@
/**
* Create a new readable stream from an async iterable or a sync iterable
* @param iterable
* @returns a readable stream
*/
export declare function fromAnyIterable<R>(iterable: Iterable<R> | AsyncIterable<R>): ReadableStream<R>;

View File

@ -0,0 +1,2 @@
import "../polyfill/asyncIterator.js";
export * from "../ponyfill/asyncIterator.js";

View File

@ -0,0 +1,5 @@
import "../polyfill/asyncIterator.js";
import { asyncIterator as a } from "../ponyfill/asyncIterator.js";
export {
a as asyncIterator
};

View File

@ -0,0 +1,2 @@
import "../polyfill/fromAnyIterable.js";
export * from "../ponyfill/fromAnyIterable.js";

View File

@ -0,0 +1,5 @@
import "../polyfill/fromAnyIterable.js";
import { fromAnyIterable as m } from "../ponyfill/fromAnyIterable.js";
export {
m as fromAnyIterable
};

View File

@ -0,0 +1,2 @@
export * from "./asyncIterator.js";
export * from "./fromAnyIterable.js";

View File

@ -0,0 +1,8 @@
import "../polyfill/asyncIterator.js";
import { asyncIterator as m } from "../ponyfill/asyncIterator.js";
import "../polyfill/fromAnyIterable.js";
import { fromAnyIterable as a } from "../ponyfill/fromAnyIterable.js";
export {
m as asyncIterator,
a as fromAnyIterable
};

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,3 @@
import { asyncIterator as e } from "../ponyfill/asyncIterator.js";
ReadableStream.prototype.values ??= ReadableStream.prototype[Symbol.asyncIterator] ??= e;
ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;

View File

@ -0,0 +1 @@
export {};

View File

@ -0,0 +1,2 @@
import { fromAnyIterable as r } from "../ponyfill/fromAnyIterable.js";
ReadableStream.from ??= r;

View File

@ -0,0 +1,2 @@
import "./asyncIterator.js";
import "./fromAnyIterable.js";

View File

@ -0,0 +1,4 @@
import "./asyncIterator.js";
import "./fromAnyIterable.js";
import "../ponyfill/asyncIterator.js";
import "../ponyfill/fromAnyIterable.js";

View File

@ -0,0 +1 @@
export { asyncIterator, type ReadableStreamIteratorOptions, } from "../core/asyncIterator.js";

View File

@ -0,0 +1,89 @@
const a = Object.getPrototypeOf(
Object.getPrototypeOf(
/* istanbul ignore next */
async function* () {
}
).prototype
);
class c {
#t;
#n;
#r = !1;
#e = void 0;
constructor(e, t) {
this.#t = e, this.#n = t;
}
next() {
const e = () => this.#s();
return this.#e = this.#e ? this.#e.then(e, e) : e(), this.#e;
}
return(e) {
const t = () => this.#i(e);
return this.#e ? this.#e.then(t, t) : t();
}
async #s() {
if (this.#r)
return {
done: !0,
value: void 0
};
let e;
try {
e = await this.#t.read();
} catch (t) {
throw this.#e = void 0, this.#r = !0, this.#t.releaseLock(), t;
}
return e.done && (this.#e = void 0, this.#r = !0, this.#t.releaseLock()), e;
}
async #i(e) {
if (this.#r)
return {
done: !0,
value: e
};
if (this.#r = !0, !this.#n) {
const t = this.#t.cancel(e);
return this.#t.releaseLock(), await t, {
done: !0,
value: e
};
}
return this.#t.releaseLock(), {
done: !0,
value: e
};
}
}
const n = Symbol();
function i() {
return this[n].next();
}
Object.defineProperty(i, "name", { value: "next" });
function o(r) {
return this[n].return(r);
}
Object.defineProperty(o, "name", { value: "return" });
const u = Object.create(a, {
next: {
enumerable: !0,
configurable: !0,
writable: !0,
value: i
},
return: {
enumerable: !0,
configurable: !0,
writable: !0,
value: o
}
});
function h({ preventCancel: r = !1 } = {}) {
const e = this.getReader(), t = new c(
e,
r
), s = Object.create(u);
return s[n] = t, s;
}
export {
h as asyncIterator
};

View File

@ -0,0 +1 @@
export { fromAnyIterable } from "../core/fromAnyIterable.js";

View File

@ -0,0 +1,34 @@
function c(n) {
const t = a(n);
return new ReadableStream(
{
async pull(e) {
const { value: r, done: o } = await t.next();
o ? e.close() : e.enqueue(r);
},
async cancel(e) {
if (typeof t.return == "function" && typeof await t.return(e) != "object")
throw new TypeError("return() fulfills with a non-object.");
return e;
}
},
new CountQueuingStrategy({
highWaterMark: 0
})
);
}
function a(n) {
let t = n[Symbol.asyncIterator]?.bind(n);
if (t === void 0) {
const r = n[Symbol.iterator](), o = {
[Symbol.iterator]: () => r
};
t = async function* () {
return yield* o;
};
}
return t();
}
export {
c as fromAnyIterable
};

View File

@ -0,0 +1,2 @@
export * from "./asyncIterator.js";
export * from "./fromAnyIterable.js";

View File

@ -0,0 +1,6 @@
import { asyncIterator as e } from "./asyncIterator.js";
import { fromAnyIterable as a } from "./fromAnyIterable.js";
export {
e as asyncIterator,
a as fromAnyIterable
};

View File

@ -0,0 +1,11 @@
import type { ReadableStreamIteratorOptions } from "../core/asyncIterator.js";
/**
* augment global readable stream interface
*/
declare global {
// biome-ignore lint/suspicious/noExplicitAny: to be compatible with lib.dom.d.ts
interface ReadableStream<R = any> {
[Symbol.asyncIterator](): AsyncIterableIterator<R>;
values(options?: ReadableStreamIteratorOptions): AsyncIterableIterator<R>;
}
}

View File

@ -0,0 +1,96 @@
{
"name": "@sec-ant/readable-stream",
"description": "A tiny, zero-dependency yet spec-compliant asynchronous iterator polyfill/ponyfill for ReadableStreams.",
"private": false,
"version": "0.4.1",
"type": "module",
"files": ["./dist"],
"main": "./dist/index/index.js",
"module": "./dist/index/index.js",
"exports": {
".": "./dist/index/index.js",
"./asyncIterator": "./dist/index/asyncIterator.js",
"./fromAnyIterable": "./dist/index/fromAnyIterable.js",
"./ponyfill": "./dist/ponyfill/index.js",
"./ponyfill/asyncIterator": "./dist/ponyfill/asyncIterator.js",
"./ponyfill/fromAnyIterable": "./dist/ponyfill/fromAnyIterable.js",
"./polyfill": "./dist/polyfill/index.js",
"./polyfill/asyncIterator": "./dist/polyfill/asyncIterator.js",
"./polyfill/fromAnyIterable": "./dist/polyfill/fromAnyIterable.js",
"./async-iterator": {
"types": "./dist/types/async-iterator.d.ts"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/Sec-ant/readable-stream.git"
},
"homepage": "https://github.com/Sec-ant/readable-stream",
"bugs": {
"url": "https://github.com/Sec-ant/readable-stream/issues",
"email": "zezhengwu@proton.me"
},
"keywords": [
"stream",
"web-streams",
"readablestream",
"async",
"asynchronous",
"iterator",
"iteration",
"async-iterator",
"polyfill",
"esm",
"from-iterable"
],
"author": {
"name": "Ze-Zheng Wu"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"scripts": {
"install:ci": "npm ci && npx playwright install --with-deps",
"install:codesandbox": "npm ci && ./scripts/prepare.sh",
"update-hooks": "simple-git-hooks",
"changeset": "changeset",
"bump": "changeset version 2>/dev/null | grep . && npm i; exit 0",
"type-check": "tsc --noEmit --emitDeclarationOnly false",
"format:prettier": "prettier . --write",
"format:biome": "biome format . --write",
"format": "conc \"npm:format:prettier\" \"npm:format:biome\"",
"check:biome": "biome check --apply .",
"check": "conc \"npm:format:prettier\" \"npm:check:biome\"",
"prebuild": "npm run check && npm run type-check",
"build": "vite build",
"copy": "copy-files-from-to",
"postbuild": "tsc && npm run copy",
"test:chromium": "vitest run --browser.name=chromium",
"test:firefox": "vitest run --browser.name=firefox",
"test": "npm run test:chromium && npm run test:firefox",
"test:coverage": "vitest run --coverage",
"test:ui": "vitest --ui --coverage",
"prepublishOnly": "npm run build",
"bump-biome:latest": "npm i -DE @biomejs/biome@latest",
"bump-biome:nightly": "npm i -DE @biomejs/biome@nightly"
},
"devDependencies": {
"@biomejs/biome": "1.6.1",
"@changesets/cli": "^2.27.1",
"@commitlint/cli": "^19.1.0",
"@commitlint/config-conventional": "^19.1.0",
"@vitest/browser": "^1.3.1",
"@vitest/coverage-istanbul": "^1.3.1",
"@vitest/ui": "^1.3.1",
"concurrently": "^8.2.2",
"copy-files-from-to": "^3.9.1",
"lint-staged": "^15.2.2",
"playwright": "^1.42.1",
"prettier": "^3.2.5",
"simple-git-hooks": "^2.10.0",
"typescript": "^5.4.2",
"vite": "^5.1.6",
"vitest": "^1.3.1"
}
}