@yume-chan/stream-extra 0.0.0-20240714132542
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/CHANGELOG.md +76 -0
- package/LICENSE +21 -0
- package/README.md +43 -0
- package/esm/buffered-transform.d.ts +11 -0
- package/esm/buffered-transform.d.ts.map +1 -0
- package/esm/buffered-transform.js +57 -0
- package/esm/buffered-transform.js.map +1 -0
- package/esm/buffered.d.ts +23 -0
- package/esm/buffered.d.ts.map +1 -0
- package/esm/buffered.js +138 -0
- package/esm/buffered.js.map +1 -0
- package/esm/concat.d.ts +36 -0
- package/esm/concat.d.ts.map +1 -0
- package/esm/concat.js +130 -0
- package/esm/concat.js.map +1 -0
- package/esm/consumable.d.ts +40 -0
- package/esm/consumable.d.ts.map +1 -0
- package/esm/consumable.js +130 -0
- package/esm/consumable.js.map +1 -0
- package/esm/distribution.d.ts +22 -0
- package/esm/distribution.d.ts.map +1 -0
- package/esm/distribution.js +103 -0
- package/esm/distribution.js.map +1 -0
- package/esm/duplex.d.ts +45 -0
- package/esm/duplex.d.ts.map +1 -0
- package/esm/duplex.js +93 -0
- package/esm/duplex.js.map +1 -0
- package/esm/encoding.d.ts +21 -0
- package/esm/encoding.d.ts.map +1 -0
- package/esm/encoding.js +4 -0
- package/esm/encoding.js.map +1 -0
- package/esm/index.d.ts +19 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +19 -0
- package/esm/index.js.map +1 -0
- package/esm/inspect.d.ts +5 -0
- package/esm/inspect.d.ts.map +1 -0
- package/esm/inspect.js +12 -0
- package/esm/inspect.js.map +1 -0
- package/esm/maybe-consumable.d.ts +21 -0
- package/esm/maybe-consumable.d.ts.map +1 -0
- package/esm/maybe-consumable.js +62 -0
- package/esm/maybe-consumable.js.map +1 -0
- package/esm/pipe-from.d.ts +13 -0
- package/esm/pipe-from.d.ts.map +1 -0
- package/esm/pipe-from.js +24 -0
- package/esm/pipe-from.js.map +1 -0
- package/esm/push-readable.d.ts +35 -0
- package/esm/push-readable.d.ts.map +1 -0
- package/esm/push-readable.js +240 -0
- package/esm/push-readable.js.map +1 -0
- package/esm/split-string.d.ts +5 -0
- package/esm/split-string.d.ts.map +1 -0
- package/esm/split-string.js +25 -0
- package/esm/split-string.js.map +1 -0
- package/esm/stream.d.ts +25 -0
- package/esm/stream.d.ts.map +1 -0
- package/esm/stream.js +7 -0
- package/esm/stream.js.map +1 -0
- package/esm/struct-deserialize.d.ts +7 -0
- package/esm/struct-deserialize.d.ts.map +1 -0
- package/esm/struct-deserialize.js +9 -0
- package/esm/struct-deserialize.js.map +1 -0
- package/esm/struct-serialize.d.ts +6 -0
- package/esm/struct-serialize.d.ts.map +1 -0
- package/esm/struct-serialize.js +11 -0
- package/esm/struct-serialize.js.map +1 -0
- package/esm/task.d.ts +5 -0
- package/esm/task.d.ts.map +1 -0
- package/esm/task.js +9 -0
- package/esm/task.js.map +1 -0
- package/esm/types.d.ts +777 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/types.js +3 -0
- package/esm/types.js.map +1 -0
- package/esm/wrap-readable.d.ts +22 -0
- package/esm/wrap-readable.d.ts.map +1 -0
- package/esm/wrap-readable.js +57 -0
- package/esm/wrap-readable.js.map +1 -0
- package/esm/wrap-writable.d.ts +15 -0
- package/esm/wrap-writable.d.ts.map +1 -0
- package/esm/wrap-writable.js +63 -0
- package/esm/wrap-writable.js.map +1 -0
- package/package.json +48 -0
- package/src/buffered-transform.ts +71 -0
- package/src/buffered.ts +160 -0
- package/src/concat.ts +162 -0
- package/src/consumable.ts +205 -0
- package/src/distribution.ts +123 -0
- package/src/duplex.ts +146 -0
- package/src/encoding.ts +39 -0
- package/src/index.ts +18 -0
- package/src/inspect.ts +12 -0
- package/src/maybe-consumable.ts +101 -0
- package/src/pipe-from.ts +28 -0
- package/src/push-readable.ts +307 -0
- package/src/split-string.ts +32 -0
- package/src/stream.ts +46 -0
- package/src/struct-deserialize.ts +14 -0
- package/src/struct-serialize.ts +15 -0
- package/src/task.ts +21 -0
- package/src/types.ts +881 -0
- package/src/wrap-readable.ts +93 -0
- package/src/wrap-writable.ts +88 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.build.tsbuildinfo +1 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# Change Log - @yume-chan/stream-extra
|
|
2
|
+
|
|
3
|
+
## 0.0.0-20240714132542
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Switch to PNPM workspace and changesets
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @yume-chan/struct@0.0.0-20240714132542
|
|
10
|
+
|
|
11
|
+
This log was last generated on Tue, 18 Jun 2024 02:49:43 GMT and should not be manually modified.
|
|
12
|
+
|
|
13
|
+
## 0.0.24
|
|
14
|
+
|
|
15
|
+
Tue, 18 Jun 2024 02:49:43 GMT
|
|
16
|
+
|
|
17
|
+
### Updates
|
|
18
|
+
|
|
19
|
+
- Add `MaybeConsumable` type. It's also a namespace containing related types.
|
|
20
|
+
- Re-export global `TextDecoderStream` to replace `DecodeUtf8Stream` which doesn't work correctly in stream mode
|
|
21
|
+
- Move `Consumable` related types to the `Consumable` namespace. In future, more types will be moved to namespaces.
|
|
22
|
+
|
|
23
|
+
## 0.0.23
|
|
24
|
+
|
|
25
|
+
Thu, 21 Mar 2024 03:15:10 GMT
|
|
26
|
+
|
|
27
|
+
### Updates
|
|
28
|
+
|
|
29
|
+
- Fix `ConsumableWritableStream.write` calls `chunk.consume` twice. (doesn't cause any issue)
|
|
30
|
+
- Fix `WrapWritableStream` might close the inner stream twice. (and throw an error)
|
|
31
|
+
- Remove web-streams-polyfill dependency. The runtime must have global stream implementations (or you can add a polyfill yourself).
|
|
32
|
+
|
|
33
|
+
## 0.0.22
|
|
34
|
+
|
|
35
|
+
Wed, 13 Dec 2023 05:57:27 GMT
|
|
36
|
+
|
|
37
|
+
_Version update only_
|
|
38
|
+
|
|
39
|
+
## 0.0.21
|
|
40
|
+
|
|
41
|
+
Fri, 25 Aug 2023 14:05:18 GMT
|
|
42
|
+
|
|
43
|
+
### Updates
|
|
44
|
+
|
|
45
|
+
- Replace `GatherStringStream` with `ConcatStringStream` which can be treated as a Promise
|
|
46
|
+
|
|
47
|
+
## 0.0.20
|
|
48
|
+
|
|
49
|
+
Mon, 05 Jun 2023 02:51:41 GMT
|
|
50
|
+
|
|
51
|
+
### Updates
|
|
52
|
+
|
|
53
|
+
- Fix a bug where `BufferedReadableStream#release` might output duplicate data.
|
|
54
|
+
- Use ECMAScript private class fields syntax (supported by Chrome 74, Firefox 90, Safari 14.1 and Node.js 12.0.0).
|
|
55
|
+
|
|
56
|
+
## 0.0.19
|
|
57
|
+
|
|
58
|
+
Sun, 09 Apr 2023 05:55:33 GMT
|
|
59
|
+
|
|
60
|
+
### Updates
|
|
61
|
+
|
|
62
|
+
- Add an option to combine small chunks into target size in `ChunkStream`, and rename it to `DistributionStream`
|
|
63
|
+
|
|
64
|
+
## 0.0.18
|
|
65
|
+
|
|
66
|
+
Wed, 25 Jan 2023 21:33:49 GMT
|
|
67
|
+
|
|
68
|
+
### Updates
|
|
69
|
+
|
|
70
|
+
- Change to load native Web Streams API implementation from `globalThis` if available
|
|
71
|
+
|
|
72
|
+
## 0.0.17
|
|
73
|
+
|
|
74
|
+
Tue, 18 Oct 2022 09:32:30 GMT
|
|
75
|
+
|
|
76
|
+
_Initial release_
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2024 Simon Chan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @yume-chan/stream-extra
|
|
2
|
+
|
|
3
|
+
Some useful extensions for working with binary streams. Conforms to the [Web Streams API](https://streams.spec.whatwg.org/).
|
|
4
|
+
|
|
5
|
+
## Find an implementation
|
|
6
|
+
|
|
7
|
+
If all of `ReadableStream`, `WritableStream` and `TransformStream` fields are available on `globalThis`, they will be used. Otherwise, the [web-streams-polyfill](https://github.com/MattiasBuelens/web-streams-polyfill) package will be used.
|
|
8
|
+
|
|
9
|
+
Google Chrome 89 and Mozilla Firefox 102 provide full support for Web Streams API natively.
|
|
10
|
+
|
|
11
|
+
In Node.js, it's not possible to load the `stream/web` module while keeping the compatibility with both Web and bundlers:
|
|
12
|
+
|
|
13
|
+
- Webpack has poor support with Top Level Await, for example, Hot Module Replacement doesn't work when any module is using TLA.
|
|
14
|
+
- Web doesn't have the `module` module, thus requires a shim in import map.
|
|
15
|
+
|
|
16
|
+
Assigning `ReadableStream`, `WritableStream` and `TransformStream` from `stream/web` module to `globalThis`, before loading this library, will still work. Other custom polyfill can also be loaded this way.
|
|
17
|
+
|
|
18
|
+
## Compatibility issue with `ReadableStream#pipeTo` and `ReadableStream#pipeThrough`
|
|
19
|
+
|
|
20
|
+
The [Web Streams API spec](https://streams.spec.whatwg.org/#readable-stream-pipe-to) specifies that `ReadableStream#pipeTo` must check the argument to be an instance of `WritableStream`, so it can optimize the performance by calling internal methods directly.
|
|
21
|
+
|
|
22
|
+
Native implementations will perform this check, so `new globalThis.ReadableStream().pipeTo(new Polyfill.WritableStream())` will throw an error.
|
|
23
|
+
|
|
24
|
+
The `WrapReadableStream` class can be used to bypass this check:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { WrapReadableStream } from "@yume-chan/stream-extra";
|
|
28
|
+
import { WritableStream as PolyfillWritableStream } from "web-streams-polyfill";
|
|
29
|
+
|
|
30
|
+
const nativeReadable = new globalThis.ReadableStream();
|
|
31
|
+
const wrappedReadable = new WrapReadableStream(new globalThis.ReadableStream());
|
|
32
|
+
|
|
33
|
+
nativeReadable.pipeTo(new PolyfillWritableStream()); // Error
|
|
34
|
+
wrappedReadable.pipeTo(new PolyfillWritableStream()); // OK
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
web-streams-polyfill package's `ReadableStream#pipeTo` only uses public methods, so it can be used with any `WritableStream` implementation.
|
|
38
|
+
|
|
39
|
+
## `BufferedReadableStream`
|
|
40
|
+
|
|
41
|
+
Allowing reading specified amount of data by buffering incoming data.
|
|
42
|
+
|
|
43
|
+
It's not a Web Streams API `ReadableStream`, because `ReadableStream` doesn't allow hinting the desired read size (except using BYOB readable, but causes extra allocations for small reads).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
+
import { BufferedReadableStream } from "./buffered.js";
|
|
3
|
+
import type { ReadableWritablePair } from "./stream.js";
|
|
4
|
+
import { ReadableStream, WritableStream } from "./stream.js";
|
|
5
|
+
export declare class BufferedTransformStream<T> implements ReadableWritablePair<T, Uint8Array> {
|
|
6
|
+
#private;
|
|
7
|
+
get readable(): ReadableStream<T>;
|
|
8
|
+
get writable(): WritableStream<Uint8Array>;
|
|
9
|
+
constructor(transform: (stream: BufferedReadableStream) => ValueOrPromise<T>);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=buffered-transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffered-transform.d.ts","sourceRoot":"","sources":["../src/buffered-transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGxD,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAGvD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAG7D,qBAAa,uBAAuB,CAAC,CAAC,CAClC,YAAW,oBAAoB,CAAC,CAAC,EAAE,UAAU,CAAC;;IAG9C,IAAI,QAAQ,sBAEX;IAGD,IAAI,QAAQ,+BAEX;gBAGG,SAAS,EAAE,CAAC,MAAM,EAAE,sBAAsB,KAAK,cAAc,CAAC,CAAC,CAAC;CA8CvE"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { StructEmptyError } from "@yume-chan/struct";
|
|
2
|
+
import { BufferedReadableStream } from "./buffered.js";
|
|
3
|
+
import { PushReadableStream } from "./push-readable.js";
|
|
4
|
+
import { ReadableStream, WritableStream } from "./stream.js";
|
|
5
|
+
// TODO: BufferedTransformStream: find better implementation
|
|
6
|
+
export class BufferedTransformStream {
|
|
7
|
+
#readable;
|
|
8
|
+
get readable() {
|
|
9
|
+
return this.#readable;
|
|
10
|
+
}
|
|
11
|
+
#writable;
|
|
12
|
+
get writable() {
|
|
13
|
+
return this.#writable;
|
|
14
|
+
}
|
|
15
|
+
constructor(transform) {
|
|
16
|
+
// Convert incoming chunks to a `BufferedReadableStream`
|
|
17
|
+
let sourceStreamController;
|
|
18
|
+
const buffered = new BufferedReadableStream(new PushReadableStream((controller) => {
|
|
19
|
+
sourceStreamController = controller;
|
|
20
|
+
}));
|
|
21
|
+
this.#readable = new ReadableStream({
|
|
22
|
+
async pull(controller) {
|
|
23
|
+
try {
|
|
24
|
+
const value = await transform(buffered);
|
|
25
|
+
controller.enqueue(value);
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
// Treat `StructEmptyError` as a normal end.
|
|
29
|
+
// If the `transform` method doesn't have enough data to return a value,
|
|
30
|
+
// it should throw another error to indicate that.
|
|
31
|
+
if (e instanceof StructEmptyError) {
|
|
32
|
+
controller.close();
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
cancel: (reason) => {
|
|
39
|
+
// Propagate cancel to the source stream
|
|
40
|
+
// So future writes will be rejected
|
|
41
|
+
return buffered.cancel(reason);
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
this.#writable = new WritableStream({
|
|
45
|
+
async write(chunk) {
|
|
46
|
+
await sourceStreamController.enqueue(chunk);
|
|
47
|
+
},
|
|
48
|
+
abort() {
|
|
49
|
+
sourceStreamController.close();
|
|
50
|
+
},
|
|
51
|
+
close() {
|
|
52
|
+
sourceStreamController.close();
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=buffered-transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffered-transform.js","sourceRoot":"","sources":["../src/buffered-transform.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAEvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAExD,OAAO,EAAE,cAAc,EAAE,cA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YACD,KAAK;gBACD,sBAAsB,CAAC,KAAK,EAAE,CAAC;YACnC,CAAC;YACD,KAAK;gBACD,sBAAsB,CAAC,KAAK,EAAE,CAAC;YACnC,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;CACJ"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AsyncExactReadable } from "@yume-chan/struct";
|
|
2
|
+
import type { ReadableStream, ReadableStreamDefaultReader } from "./stream.js";
|
|
3
|
+
export declare class BufferedReadableStream implements AsyncExactReadable {
|
|
4
|
+
#private;
|
|
5
|
+
get position(): number;
|
|
6
|
+
protected readonly stream: ReadableStream<Uint8Array>;
|
|
7
|
+
protected readonly reader: ReadableStreamDefaultReader<Uint8Array>;
|
|
8
|
+
constructor(stream: ReadableStream<Uint8Array>);
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param length
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
readExactly(length: number): Uint8Array | Promise<Uint8Array>;
|
|
15
|
+
/**
|
|
16
|
+
* Return a readable stream with unconsumed data (if any) and
|
|
17
|
+
* all data from the wrapped stream.
|
|
18
|
+
* @returns A `ReadableStream`
|
|
19
|
+
*/
|
|
20
|
+
release(): ReadableStream<Uint8Array>;
|
|
21
|
+
cancel(reason?: unknown): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=buffered.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffered.d.ts","sourceRoot":"","sources":["../src/buffered.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAI5D,OAAO,KAAK,EAAE,cAAc,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAM/E,qBAAa,sBAAuB,YAAW,kBAAkB;;IAM7D,IAAI,QAAQ,WAEX;IAED,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IACtD,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,2BAA2B,CAAC,UAAU,CAAC,CAAC;gBAEvD,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC;IAsE9C;;;;OAIG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAwB7D;;;;OAIG;IACH,OAAO,IAAI,cAAc,CAAC,UAAU,CAAC;IA6B/B,MAAM,CAAC,MAAM,CAAC,EAAE,OAAO;CAGhC"}
|
package/esm/buffered.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ExactReadableEndedError } from "@yume-chan/struct";
|
|
2
|
+
import { PushReadableStream } from "./push-readable.js";
|
|
3
|
+
const NOOP = () => {
|
|
4
|
+
// no-op
|
|
5
|
+
};
|
|
6
|
+
export class BufferedReadableStream {
|
|
7
|
+
#buffered;
|
|
8
|
+
#bufferedOffset = 0;
|
|
9
|
+
#bufferedLength = 0;
|
|
10
|
+
#position = 0;
|
|
11
|
+
get position() {
|
|
12
|
+
return this.#position;
|
|
13
|
+
}
|
|
14
|
+
stream;
|
|
15
|
+
reader;
|
|
16
|
+
constructor(stream) {
|
|
17
|
+
this.stream = stream;
|
|
18
|
+
this.reader = stream.getReader();
|
|
19
|
+
}
|
|
20
|
+
async #readSource() {
|
|
21
|
+
const { done, value } = await this.reader.read();
|
|
22
|
+
if (done) {
|
|
23
|
+
throw new ExactReadableEndedError();
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
async #readAsync(length, initial) {
|
|
28
|
+
let result;
|
|
29
|
+
let index;
|
|
30
|
+
if (initial) {
|
|
31
|
+
result = new Uint8Array(length);
|
|
32
|
+
result.set(initial);
|
|
33
|
+
index = initial.length;
|
|
34
|
+
length -= initial.length;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const array = await this.#readSource();
|
|
38
|
+
if (array.length === length) {
|
|
39
|
+
this.#position += length;
|
|
40
|
+
return array;
|
|
41
|
+
}
|
|
42
|
+
if (array.length > length) {
|
|
43
|
+
this.#buffered = array;
|
|
44
|
+
this.#bufferedOffset = length;
|
|
45
|
+
this.#bufferedLength = array.length - length;
|
|
46
|
+
this.#position += length;
|
|
47
|
+
return array.subarray(0, length);
|
|
48
|
+
}
|
|
49
|
+
result = new Uint8Array(length);
|
|
50
|
+
result.set(array);
|
|
51
|
+
index = array.length;
|
|
52
|
+
length -= array.length;
|
|
53
|
+
this.#position += array.length;
|
|
54
|
+
}
|
|
55
|
+
while (length > 0) {
|
|
56
|
+
const array = await this.#readSource();
|
|
57
|
+
if (array.length === length) {
|
|
58
|
+
result.set(array, index);
|
|
59
|
+
this.#position += length;
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
if (array.length > length) {
|
|
63
|
+
this.#buffered = array;
|
|
64
|
+
this.#bufferedOffset = length;
|
|
65
|
+
this.#bufferedLength = array.length - length;
|
|
66
|
+
result.set(array.subarray(0, length), index);
|
|
67
|
+
this.#position += length;
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
result.set(array, index);
|
|
71
|
+
index += array.length;
|
|
72
|
+
length -= array.length;
|
|
73
|
+
this.#position += array.length;
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
*
|
|
79
|
+
* @param length
|
|
80
|
+
* @returns
|
|
81
|
+
*/
|
|
82
|
+
readExactly(length) {
|
|
83
|
+
// PERF: Add a synchronous path for reading from internal buffer
|
|
84
|
+
if (this.#buffered) {
|
|
85
|
+
const array = this.#buffered;
|
|
86
|
+
const offset = this.#bufferedOffset;
|
|
87
|
+
if (this.#bufferedLength > length) {
|
|
88
|
+
// PERF: `subarray` is slow
|
|
89
|
+
// don't use it until absolutely necessary
|
|
90
|
+
this.#bufferedOffset += length;
|
|
91
|
+
this.#bufferedLength -= length;
|
|
92
|
+
this.#position += length;
|
|
93
|
+
return array.subarray(offset, offset + length);
|
|
94
|
+
}
|
|
95
|
+
this.#buffered = undefined;
|
|
96
|
+
this.#bufferedLength = 0;
|
|
97
|
+
this.#bufferedOffset = 0;
|
|
98
|
+
this.#position += array.length - offset;
|
|
99
|
+
return this.#readAsync(length, array.subarray(offset));
|
|
100
|
+
}
|
|
101
|
+
return this.#readAsync(length);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Return a readable stream with unconsumed data (if any) and
|
|
105
|
+
* all data from the wrapped stream.
|
|
106
|
+
* @returns A `ReadableStream`
|
|
107
|
+
*/
|
|
108
|
+
release() {
|
|
109
|
+
if (this.#bufferedLength > 0) {
|
|
110
|
+
return new PushReadableStream(async (controller) => {
|
|
111
|
+
// Put the remaining data back to the stream
|
|
112
|
+
const buffered = this.#buffered.subarray(this.#bufferedOffset);
|
|
113
|
+
await controller.enqueue(buffered);
|
|
114
|
+
controller.abortSignal.addEventListener("abort", () => {
|
|
115
|
+
// NOOP: the reader might already be released
|
|
116
|
+
this.reader.cancel().catch(NOOP);
|
|
117
|
+
});
|
|
118
|
+
// Manually pipe the stream
|
|
119
|
+
while (true) {
|
|
120
|
+
const { done, value } = await this.reader.read();
|
|
121
|
+
if (done) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
await controller.enqueue(value);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// Simply release the reader and return the stream
|
|
130
|
+
this.reader.releaseLock();
|
|
131
|
+
return this.stream;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async cancel(reason) {
|
|
135
|
+
await this.reader.cancel(reason);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=buffered.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buffered.js","sourceRoot":"","sources":["../src/buffered.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAm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;QACH,IAAI,IAAI,CAAC,eAAe,GAAG,CAAC,EAAE,CAAC;YAC3B,OAAO,IAAI,kBAAkB,CAAa,KAAK,EAAE,UAAU,EAAE,EAAE;gBAC3D,4CAA4C;gBAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;gBAChE,MAAM,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;gBAEnC,UAAU,CAAC,WAAW,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE;oBAClD,6CAA6C;oBAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;gBACrC,CAAC,CAAC,CAAC;gBAEH,2BAA2B;gBAC3B,OAAO,IAAI,EAAE,CAAC;oBACV,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;oBACjD,IAAI,IAAI,EAAE,CAAC;wBACP,OAAO;oBACX,CAAC;oBAED,MAAM,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;gBACpC,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC;aAAM,CAAC;YACJ,kDAAkD;YAClD,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,MAAM,CAAC;QACvB,CAAC;IACL,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,MAAgB;QACzB,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACrC,CAAC;CACJ"}
|
package/esm/concat.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ReadableStream, WritableStream } from "./stream.js";
|
|
2
|
+
export interface ConcatStringReadableStream extends ReadableStream<string>, Promise<string> {
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* A `TransformStream` that concatenates strings.
|
|
6
|
+
*
|
|
7
|
+
* Its `readable` is also a `Promise<string>`, so it's possible to `await` it to get the result.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const result: string = await readable.pipeThrough(new ConcatStringStream());
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class ConcatStringStream {
|
|
14
|
+
#private;
|
|
15
|
+
get writable(): WritableStream<string>;
|
|
16
|
+
get readable(): ConcatStringReadableStream;
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
export interface ConcatBufferReadableStream extends ReadableStream<Uint8Array>, Promise<Uint8Array> {
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A `TransformStream` that concatenates `Uint8Array`s.
|
|
23
|
+
*
|
|
24
|
+
* If you want to decode the result as string,
|
|
25
|
+
* prefer `.pipeThrough(new TextDecoderStream()).pipeThrough(new ConcatStringStream())`,
|
|
26
|
+
* than `.pipeThough(new ConcatBufferStream()).pipeThrough(new TextDecoderStream())`,
|
|
27
|
+
* because of JavaScript engine optimizations,
|
|
28
|
+
* concatenating strings is faster than concatenating `Uint8Array`s.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ConcatBufferStream {
|
|
31
|
+
#private;
|
|
32
|
+
get writable(): WritableStream<Uint8Array>;
|
|
33
|
+
get readable(): ConcatBufferReadableStream;
|
|
34
|
+
constructor();
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=concat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concat.d.ts","sourceRoot":"","sources":["../src/concat.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7D,MAAM,WAAW,0BACb,SAAQ,cAAc,CAAC,MAAM,CAAC,EAC1B,OAAO,CAAC,MAAM,CAAC;CAAG;AAO1B;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;;IAoB3B,IAAI,QAAQ,IAAI,cAAc,CAAC,MAAM,CAAC,CAErC;IAQD,IAAI,QAAQ,IAAI,0BAA0B,CAEzC;;CAkBJ;AAED,MAAM,WAAW,0BACb,SAAQ,cAAc,CAAC,UAAU,CAAC,EAC9B,OAAO,CAAC,UAAU,CAAC;CAAG;AAE9B;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;;IA0C3B,IAAI,QAAQ,IAAI,cAAc,CAAC,UAAU,CAAC,CAEzC;IAQD,IAAI,QAAQ,IAAI,0BAA0B,CAEzC;;CAkBJ"}
|
package/esm/concat.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { PromiseResolver } from "@yume-chan/async";
|
|
2
|
+
import { EMPTY_UINT8_ARRAY } from "@yume-chan/struct";
|
|
3
|
+
import { ReadableStream, WritableStream } from "./stream.js";
|
|
4
|
+
// `TransformStream` only calls its `source.flush` method when its `readable` is being read.
|
|
5
|
+
// If the user want to use the `Promise` interface, the `flush` method will never be called,
|
|
6
|
+
// so the `PromiseResolver` will never be resolved.
|
|
7
|
+
// Thus we need to implement our own `TransformStream` using a `WritableStream` and a `ReadableStream`.
|
|
8
|
+
/**
|
|
9
|
+
* A `TransformStream` that concatenates strings.
|
|
10
|
+
*
|
|
11
|
+
* Its `readable` is also a `Promise<string>`, so it's possible to `await` it to get the result.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* const result: string = await readable.pipeThrough(new ConcatStringStream());
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class ConcatStringStream {
|
|
18
|
+
// PERF: rope (concat strings) is faster than `[].join('')`
|
|
19
|
+
#result = "";
|
|
20
|
+
#resolver = new PromiseResolver();
|
|
21
|
+
#writable = new WritableStream({
|
|
22
|
+
write: (chunk) => {
|
|
23
|
+
this.#result += chunk;
|
|
24
|
+
},
|
|
25
|
+
close: () => {
|
|
26
|
+
this.#resolver.resolve(this.#result);
|
|
27
|
+
this.#readableController.enqueue(this.#result);
|
|
28
|
+
this.#readableController.close();
|
|
29
|
+
},
|
|
30
|
+
abort: (reason) => {
|
|
31
|
+
this.#resolver.reject(reason);
|
|
32
|
+
this.#readableController.error(reason);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
get writable() {
|
|
36
|
+
return this.#writable;
|
|
37
|
+
}
|
|
38
|
+
#readableController;
|
|
39
|
+
#readable = new ReadableStream({
|
|
40
|
+
start: (controller) => {
|
|
41
|
+
this.#readableController = controller;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
get readable() {
|
|
45
|
+
return this.#readable;
|
|
46
|
+
}
|
|
47
|
+
constructor() {
|
|
48
|
+
void Object.defineProperties(this.#readable, {
|
|
49
|
+
then: {
|
|
50
|
+
get: () => this.#resolver.promise.then.bind(this.#resolver.promise),
|
|
51
|
+
},
|
|
52
|
+
catch: {
|
|
53
|
+
get: () => this.#resolver.promise.catch.bind(this.#resolver.promise),
|
|
54
|
+
},
|
|
55
|
+
finally: {
|
|
56
|
+
get: () => this.#resolver.promise.finally.bind(this.#resolver.promise),
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* A `TransformStream` that concatenates `Uint8Array`s.
|
|
63
|
+
*
|
|
64
|
+
* If you want to decode the result as string,
|
|
65
|
+
* prefer `.pipeThrough(new TextDecoderStream()).pipeThrough(new ConcatStringStream())`,
|
|
66
|
+
* than `.pipeThough(new ConcatBufferStream()).pipeThrough(new TextDecoderStream())`,
|
|
67
|
+
* because of JavaScript engine optimizations,
|
|
68
|
+
* concatenating strings is faster than concatenating `Uint8Array`s.
|
|
69
|
+
*/
|
|
70
|
+
export class ConcatBufferStream {
|
|
71
|
+
#segments = [];
|
|
72
|
+
#resolver = new PromiseResolver();
|
|
73
|
+
#writable = new WritableStream({
|
|
74
|
+
write: (chunk) => {
|
|
75
|
+
this.#segments.push(chunk);
|
|
76
|
+
},
|
|
77
|
+
close: () => {
|
|
78
|
+
let result;
|
|
79
|
+
let offset = 0;
|
|
80
|
+
switch (this.#segments.length) {
|
|
81
|
+
case 0:
|
|
82
|
+
result = EMPTY_UINT8_ARRAY;
|
|
83
|
+
break;
|
|
84
|
+
case 1:
|
|
85
|
+
result = this.#segments[0];
|
|
86
|
+
break;
|
|
87
|
+
default:
|
|
88
|
+
result = new Uint8Array(this.#segments.reduce((prev, item) => prev + item.length, 0));
|
|
89
|
+
for (const segment of this.#segments) {
|
|
90
|
+
result.set(segment, offset);
|
|
91
|
+
offset += segment.length;
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
this.#resolver.resolve(result);
|
|
96
|
+
this.#readableController.enqueue(result);
|
|
97
|
+
this.#readableController.close();
|
|
98
|
+
},
|
|
99
|
+
abort: (reason) => {
|
|
100
|
+
this.#resolver.reject(reason);
|
|
101
|
+
this.#readableController.error(reason);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
get writable() {
|
|
105
|
+
return this.#writable;
|
|
106
|
+
}
|
|
107
|
+
#readableController;
|
|
108
|
+
#readable = new ReadableStream({
|
|
109
|
+
start: (controller) => {
|
|
110
|
+
this.#readableController = controller;
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
get readable() {
|
|
114
|
+
return this.#readable;
|
|
115
|
+
}
|
|
116
|
+
constructor() {
|
|
117
|
+
void Object.defineProperties(this.#readable, {
|
|
118
|
+
then: {
|
|
119
|
+
get: () => this.#resolver.promise.then.bind(this.#resolver.promise),
|
|
120
|
+
},
|
|
121
|
+
catch: {
|
|
122
|
+
get: () => this.#resolver.promise.catch.bind(this.#resolver.promise),
|
|
123
|
+
},
|
|
124
|
+
finally: {
|
|
125
|
+
get: () => this.#resolver.promise.finally.bind(this.#resolver.promise),
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=concat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version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AAC,SAAS,CAAC,OAAO,CAAC;aAC/D;YACD,KAAK,EAAE;gBACH,GAAG,EAAE,GAAG,EAAE,CACN,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;aAChE;YACD,OAAO,EAAE;gBACL,GAAG,EAAE,GAAG,EAAE,CACN,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;aAClE;SACJ,CAAC,CAAC;IACP,CAAC;CACJ"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { QueuingStrategy, WritableStreamDefaultController, WritableStreamDefaultWriter } from "./stream.js";
|
|
2
|
+
import { ReadableStream as NativeReadableStream, WritableStream as NativeWritableStream } from "./stream.js";
|
|
3
|
+
export declare class Consumable<T> {
|
|
4
|
+
#private;
|
|
5
|
+
readonly value: T;
|
|
6
|
+
readonly consumed: Promise<void>;
|
|
7
|
+
constructor(value: T);
|
|
8
|
+
consume(): void;
|
|
9
|
+
error(error: unknown): void;
|
|
10
|
+
tryConsume<U>(callback: (value: T) => U): U;
|
|
11
|
+
}
|
|
12
|
+
export declare namespace Consumable {
|
|
13
|
+
interface WritableStreamSink<in T> {
|
|
14
|
+
start?(controller: WritableStreamDefaultController): void | PromiseLike<void>;
|
|
15
|
+
write?(chunk: T, controller: WritableStreamDefaultController): void | PromiseLike<void>;
|
|
16
|
+
abort?(reason: unknown): void | PromiseLike<void>;
|
|
17
|
+
close?(): void | PromiseLike<void>;
|
|
18
|
+
}
|
|
19
|
+
class WritableStream<in T> extends NativeWritableStream<Consumable<T>> {
|
|
20
|
+
static write<T>(writer: WritableStreamDefaultWriter<Consumable<T>>, value: T): Promise<void>;
|
|
21
|
+
constructor(sink: WritableStreamSink<T>, strategy?: QueuingStrategy<T>);
|
|
22
|
+
}
|
|
23
|
+
interface ReadableStreamController<T> {
|
|
24
|
+
enqueue(chunk: T): Promise<void>;
|
|
25
|
+
close(): void;
|
|
26
|
+
error(reason: unknown): void;
|
|
27
|
+
}
|
|
28
|
+
interface ReadableStreamSource<T> {
|
|
29
|
+
start?(controller: ReadableStreamController<T>): void | PromiseLike<void>;
|
|
30
|
+
pull?(controller: ReadableStreamController<T>): void | PromiseLike<void>;
|
|
31
|
+
cancel?(reason: unknown): void | PromiseLike<void>;
|
|
32
|
+
}
|
|
33
|
+
class ReadableStream<T> extends NativeReadableStream<Consumable<T>> {
|
|
34
|
+
static enqueue<T>(controller: {
|
|
35
|
+
enqueue: (chunk: Consumable<T>) => void;
|
|
36
|
+
}, chunk: T): Promise<void>;
|
|
37
|
+
constructor(source: ReadableStreamSource<T>, strategy?: QueuingStrategy<T>);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=consumable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consumable.d.ts","sourceRoot":"","sources":["../src/consumable.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACR,eAAe,EACf,+BAA+B,EAC/B,2BAA2B,EAC9B,MAAM,aAAa,CAAC;AACrB,OAAO,EACH,cAAc,IAAI,oBAAoB,EACtC,cAAc,IAAI,oBAAoB,EACzC,MAAM,aAAa,CAAC;AAQrB,qBAAa,UAAU,CAAC,CAAC;;IAIrB,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;IAClB,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;gBAErB,KAAK,EAAE,CAAC;IAOpB,OAAO;IAIP,KAAK,CAAC,KAAK,EAAE,OAAO;IAIpB,UAAU,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC;CAuB1C;AAED,yBAAiB,UAAU,CAAC;IACxB,UAAiB,kBAAkB,CAAC,EAAE,CAAC,CAAC;QACpC,KAAK,CAAC,CACF,UAAU,EAAE,+B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