@yume-chan/stream-extra 0.0.18 → 0.0.20
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.json +27 -0
- package/CHANGELOG.md +16 -1
- package/README.md +1 -1
- package/esm/buffered-transform.d.ts +10 -10
- package/esm/buffered-transform.d.ts.map +1 -1
- package/esm/buffered-transform.js +56 -55
- package/esm/buffered-transform.js.map +1 -1
- package/esm/buffered.d.ts +24 -27
- package/esm/buffered.d.ts.map +1 -1
- package/esm/buffered.js +132 -123
- package/esm/buffered.js.map +1 -1
- package/esm/consumable.d.ts +52 -0
- package/esm/consumable.d.ts.map +1 -0
- package/esm/consumable.js +178 -0
- package/esm/consumable.js.map +1 -0
- package/esm/decode-utf8.d.ts +4 -4
- package/esm/decode-utf8.js +11 -11
- package/esm/distribution.d.ts +21 -0
- package/esm/distribution.d.ts.map +1 -0
- package/esm/distribution.js +99 -0
- package/esm/distribution.js.map +1 -0
- package/esm/duplex.d.ts +44 -47
- package/esm/duplex.d.ts.map +1 -1
- package/esm/duplex.js +92 -92
- package/esm/duplex.js.map +1 -1
- package/esm/gather-string.d.ts +6 -6
- package/esm/gather-string.d.ts.map +1 -1
- package/esm/gather-string.js +15 -13
- package/esm/gather-string.js.map +1 -1
- package/esm/index.d.ts +16 -15
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +16 -15
- package/esm/index.js.map +1 -1
- package/esm/inspect.d.ts +4 -4
- package/esm/inspect.js +11 -11
- package/esm/pipe-from.d.ts +12 -11
- package/esm/pipe-from.d.ts.map +1 -1
- package/esm/pipe-from.js +23 -25
- package/esm/pipe-from.js.map +1 -1
- package/esm/push-readable.d.ts +19 -18
- package/esm/push-readable.d.ts.map +1 -1
- package/esm/push-readable.js +66 -61
- package/esm/push-readable.js.map +1 -1
- package/esm/split-string.d.ts +4 -4
- package/esm/split-string.d.ts.map +1 -1
- package/esm/split-string.js +24 -24
- package/esm/split-string.js.map +1 -1
- package/esm/stream.d.ts +25 -24
- package/esm/stream.d.ts.map +1 -1
- package/esm/stream.js +26 -26
- package/esm/stream.js.map +1 -1
- package/esm/struct-deserialize.d.ts +6 -6
- package/esm/struct-deserialize.d.ts.map +1 -1
- package/esm/struct-deserialize.js +8 -8
- package/esm/struct-serialize.d.ts +5 -5
- package/esm/struct-serialize.js +10 -10
- package/esm/wrap-readable.d.ts +21 -20
- package/esm/wrap-readable.d.ts.map +1 -1
- package/esm/wrap-readable.js +56 -56
- package/esm/wrap-readable.js.map +1 -1
- package/esm/wrap-writable.d.ts +14 -12
- package/esm/wrap-writable.d.ts.map +1 -1
- package/esm/wrap-writable.js +62 -52
- package/esm/wrap-writable.js.map +1 -1
- package/package.json +9 -8
- package/src/buffered-transform.ts +15 -22
- package/src/buffered.ts +41 -33
- package/src/consumable.ts +281 -0
- package/src/distribution.ts +113 -0
- package/src/duplex.ts +35 -34
- package/src/gather-string.ts +6 -4
- package/src/index.ts +16 -15
- package/src/inspect.ts +1 -1
- package/src/pipe-from.ts +8 -6
- package/src/push-readable.ts +12 -12
- package/src/split-string.ts +5 -2
- package/src/stream.ts +1 -1
- package/src/struct-deserialize.ts +1 -1
- package/src/wrap-readable.ts +9 -9
- package/src/wrap-writable.ts +21 -9
- package/tsconfig.build.json +6 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/esm/chunk.d.ts +0 -5
- package/esm/chunk.d.ts.map +0 -1
- package/esm/chunk.js +0 -15
- package/esm/chunk.js.map +0 -1
- package/esm/trace.d.ts +0 -16
- package/esm/trace.d.ts.map +0 -1
- package/esm/trace.js +0 -131
- package/esm/trace.js.map +0 -1
- package/src/chunk.ts +0 -15
package/esm/stream.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { ReadableStream as ReadableStreamPolyfill, TransformStream as TransformStreamPolyfill, WritableStream as WritableStreamPolyfill, } from "web-streams-polyfill";
|
|
2
|
-
export * from "web-streams-polyfill";
|
|
3
|
-
const GLOBAL = globalThis;
|
|
4
|
-
export const AbortController = GLOBAL.AbortController;
|
|
5
|
-
export let ReadableStream = ReadableStreamPolyfill;
|
|
6
|
-
export let WritableStream = WritableStreamPolyfill;
|
|
7
|
-
export let TransformStream = TransformStreamPolyfill;
|
|
8
|
-
if (GLOBAL.ReadableStream && GLOBAL.WritableStream && GLOBAL.TransformStream) {
|
|
9
|
-
// Use browser native implementation
|
|
10
|
-
ReadableStream = GLOBAL.ReadableStream;
|
|
11
|
-
WritableStream = GLOBAL.WritableStream;
|
|
12
|
-
TransformStream = GLOBAL.TransformStream;
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
// TODO: enable loading Node.js stream implementation when bundler supports Top Level Await
|
|
16
|
-
// try {
|
|
17
|
-
// // Use Node.js native implementation
|
|
18
|
-
// const MODULE_NAME = "node:stream/web";
|
|
19
|
-
// const StreamWeb = (await import(MODULE_NAME)) as GlobalExtension;
|
|
20
|
-
// ReadableStream = StreamWeb.ReadableStream;
|
|
21
|
-
// WritableStream = StreamWeb.WritableStream;
|
|
22
|
-
// TransformStream = StreamWeb.TransformStream;
|
|
23
|
-
// } catch {
|
|
24
|
-
// // ignore
|
|
25
|
-
// }
|
|
26
|
-
}
|
|
1
|
+
import { ReadableStream as ReadableStreamPolyfill, TransformStream as TransformStreamPolyfill, WritableStream as WritableStreamPolyfill, } from "web-streams-polyfill";
|
|
2
|
+
export * from "web-streams-polyfill";
|
|
3
|
+
const GLOBAL = globalThis;
|
|
4
|
+
export const AbortController = GLOBAL.AbortController;
|
|
5
|
+
export let ReadableStream = ReadableStreamPolyfill;
|
|
6
|
+
export let WritableStream = WritableStreamPolyfill;
|
|
7
|
+
export let TransformStream = TransformStreamPolyfill;
|
|
8
|
+
if (GLOBAL.ReadableStream && GLOBAL.WritableStream && GLOBAL.TransformStream) {
|
|
9
|
+
// Use browser native implementation
|
|
10
|
+
ReadableStream = GLOBAL.ReadableStream;
|
|
11
|
+
WritableStream = GLOBAL.WritableStream;
|
|
12
|
+
TransformStream = GLOBAL.TransformStream;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
// TODO: enable loading Node.js stream implementation when bundler supports Top Level Await
|
|
16
|
+
// try {
|
|
17
|
+
// // Use Node.js native implementation
|
|
18
|
+
// const MODULE_NAME = "node:stream/web";
|
|
19
|
+
// const StreamWeb = (await import(MODULE_NAME)) as GlobalExtension;
|
|
20
|
+
// ReadableStream = StreamWeb.ReadableStream;
|
|
21
|
+
// WritableStream = StreamWeb.WritableStream;
|
|
22
|
+
// TransformStream = StreamWeb.TransformStream;
|
|
23
|
+
// } catch {
|
|
24
|
+
// // ignore
|
|
25
|
+
// }
|
|
26
|
+
}
|
|
27
27
|
//# sourceMappingURL=stream.js.map
|
package/esm/stream.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../src/stream.ts"],"names":[],"mappings":"AACA,OAAO,EACH,cAAc,IAAI,sBAAsB,EACxC,eAAe,IAAI,uBAAuB,EAC1C,cAAc,IAAI,sBAAsB,GAC3C,MAAM,sBAAsB,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AA2BrC,MAAM,MAAM,GAAG,UAAwC,CAAC;AAExD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;AAGtD,MAAM,CAAC,IAAI,cAAc,GAAG,sBAAsB,CAAC;AAGnD,MAAM,CAAC,IAAI,cAAc,GAAG,sBAAsB,CAAC;AAGnD,MAAM,CAAC,IAAI,eAAe,GAAG,uBAAuB,CAAC;AAErD,IAAI,MAAM,CAAC,cAAc,IAAI,MAAM,CAAC,cAAc,IAAI,MAAM,CAAC,eAAe,EAAE;IAC1E,oCAAoC;IACpC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;IACvC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;IACvC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;CAC5C;KAAM;IACH,2FAA2F;IAC3F,QAAQ;IACR,2CAA2C;IAC3C,6CAA6C;IAC7C,wEAAwE;IACxE,iDAAiD;IACjD,iDAAiD;IACjD,mDAAmD;IACnD,YAAY;IACZ,gBAAgB;IAChB,IAAI;CACP"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type Struct from "@yume-chan/struct";
|
|
2
|
-
import {
|
|
3
|
-
import { BufferedTransformStream } from "./buffered-transform.js";
|
|
4
|
-
export declare class StructDeserializeStream<T extends Struct<any, any, any, any>> extends BufferedTransformStream<StructValueType<T>> {
|
|
5
|
-
constructor(struct: T);
|
|
6
|
-
}
|
|
1
|
+
import type Struct from "@yume-chan/struct";
|
|
2
|
+
import type { StructValueType } from "@yume-chan/struct";
|
|
3
|
+
import { BufferedTransformStream } from "./buffered-transform.js";
|
|
4
|
+
export declare class StructDeserializeStream<T extends Struct<any, any, any, any>> extends BufferedTransformStream<StructValueType<T>> {
|
|
5
|
+
constructor(struct: T);
|
|
6
|
+
}
|
|
7
7
|
//# sourceMappingURL=struct-deserialize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"struct-deserialize.d.ts","sourceRoot":"","sources":["../src/struct-deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"struct-deserialize.d.ts","sourceRoot":"","sources":["../src/struct-deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAElE,qBAAa,uBAAuB,CAChC,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CACtC,SAAQ,uBAAuB,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;gBAC9B,MAAM,EAAE,CAAC;CAK/B"}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { BufferedTransformStream } from "./buffered-transform.js";
|
|
2
|
-
export class StructDeserializeStream extends BufferedTransformStream {
|
|
3
|
-
constructor(struct) {
|
|
4
|
-
super((stream) => {
|
|
5
|
-
return struct.deserialize(stream);
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
}
|
|
1
|
+
import { BufferedTransformStream } from "./buffered-transform.js";
|
|
2
|
+
export class StructDeserializeStream extends BufferedTransformStream {
|
|
3
|
+
constructor(struct) {
|
|
4
|
+
super((stream) => {
|
|
5
|
+
return struct.deserialize(stream);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
9
|
//# sourceMappingURL=struct-deserialize.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type Struct from "@yume-chan/struct";
|
|
2
|
-
import { TransformStream } from "./stream.js";
|
|
3
|
-
export declare class StructSerializeStream<T extends Struct<any, any, any, any>> extends TransformStream<T["TInit"], Uint8Array> {
|
|
4
|
-
constructor(struct: T);
|
|
5
|
-
}
|
|
1
|
+
import type Struct from "@yume-chan/struct";
|
|
2
|
+
import { TransformStream } from "./stream.js";
|
|
3
|
+
export declare class StructSerializeStream<T extends Struct<any, any, any, any>> extends TransformStream<T["TInit"], Uint8Array> {
|
|
4
|
+
constructor(struct: T);
|
|
5
|
+
}
|
|
6
6
|
//# sourceMappingURL=struct-serialize.d.ts.map
|
package/esm/struct-serialize.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { TransformStream } from "./stream.js";
|
|
2
|
-
export class StructSerializeStream extends TransformStream {
|
|
3
|
-
constructor(struct) {
|
|
4
|
-
super({
|
|
5
|
-
transform(chunk, controller) {
|
|
6
|
-
controller.enqueue(struct.serialize(chunk));
|
|
7
|
-
},
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
import { TransformStream } from "./stream.js";
|
|
2
|
+
export class StructSerializeStream extends TransformStream {
|
|
3
|
+
constructor(struct) {
|
|
4
|
+
super({
|
|
5
|
+
transform(chunk, controller) {
|
|
6
|
+
controller.enqueue(struct.serialize(chunk));
|
|
7
|
+
},
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
11
|
//# sourceMappingURL=struct-serialize.js.map
|
package/esm/wrap-readable.d.ts
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
private
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
+
import type { ReadableStreamDefaultController } from "./stream.js";
|
|
3
|
+
import { ReadableStream } from "./stream.js";
|
|
4
|
+
export type WrapReadableStreamStart<T> = (controller: ReadableStreamDefaultController<T>) => ValueOrPromise<ReadableStream<T>>;
|
|
5
|
+
export interface ReadableStreamWrapper<T> {
|
|
6
|
+
start: WrapReadableStreamStart<T>;
|
|
7
|
+
cancel?(reason?: unknown): ValueOrPromise<void>;
|
|
8
|
+
close?(): ValueOrPromise<void>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* This class has multiple usages:
|
|
12
|
+
*
|
|
13
|
+
* 1. Get notified when the stream is cancelled or closed.
|
|
14
|
+
* 2. Synchronously create a `ReadableStream` by asynchronously return another `ReadableStream`.
|
|
15
|
+
* 3. Convert native `ReadableStream`s to polyfilled ones so they can `pipe` between.
|
|
16
|
+
*/
|
|
17
|
+
export declare class WrapReadableStream<T> extends ReadableStream<T> {
|
|
18
|
+
#private;
|
|
19
|
+
readable: ReadableStream<T>;
|
|
20
|
+
constructor(wrapper: ReadableStream<T> | WrapReadableStreamStart<T> | ReadableStreamWrapper<T>);
|
|
21
|
+
}
|
|
21
22
|
//# sourceMappingURL=wrap-readable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-readable.d.ts","sourceRoot":"","sources":["../src/wrap-readable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"wrap-readable.d.ts","sourceRoot":"","sources":["../src/wrap-readable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EACR,+BAA+B,EAElC,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,uBAAuB,CAAC,CAAC,IAAI,CACrC,UAAU,EAAE,+BAA+B,CAAC,CAAC,CAAC,KAC7C,cAAc,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;AAEvC,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACpC,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAC;IAClC,MAAM,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IAChD,KAAK,CAAC,IAAI,cAAc,CAAC,IAAI,CAAC,CAAC;CAClC;AAoBD;;;;;;GAMG;AACH,qBAAa,kBAAkB,CAAC,CAAC,CAAE,SAAQ,cAAc,CAAC,CAAC,CAAC;;IACjD,QAAQ,EAAG,cAAc,CAAC,CAAC,CAAC,CAAC;gBAKhC,OAAO,EACD,cAAc,CAAC,CAAC,CAAC,GACjB,uBAAuB,CAAC,CAAC,CAAC,GAC1B,qBAAqB,CAAC,CAAC,CAAC;CAmCrC"}
|
package/esm/wrap-readable.js
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import { ReadableStream
|
|
2
|
-
function getWrappedReadableStream(wrapper, controller) {
|
|
3
|
-
if ("start" in wrapper) {
|
|
4
|
-
return wrapper.start(controller);
|
|
5
|
-
}
|
|
6
|
-
else if (typeof wrapper === "function") {
|
|
7
|
-
return wrapper(controller);
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
// Can't use `wrapper instanceof ReadableStream`
|
|
11
|
-
// Because we want to be compatible with any ReadableStream-like objects
|
|
12
|
-
return wrapper;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* This class has multiple usages:
|
|
17
|
-
*
|
|
18
|
-
* 1. Get notified when the stream is cancelled or closed.
|
|
19
|
-
* 2. Synchronously create a `ReadableStream` by asynchronously return another `ReadableStream`.
|
|
20
|
-
* 3. Convert native `ReadableStream`s to polyfilled ones so they can `pipe` between.
|
|
21
|
-
*/
|
|
22
|
-
export class WrapReadableStream extends ReadableStream {
|
|
23
|
-
readable;
|
|
24
|
-
reader;
|
|
25
|
-
constructor(wrapper) {
|
|
26
|
-
super({
|
|
27
|
-
start: async (controller) => {
|
|
28
|
-
// `start` is invoked before `ReadableStream`'s constructor finish,
|
|
29
|
-
// so using `this` synchronously causes
|
|
30
|
-
// "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
|
|
31
|
-
// Queue a microtask to avoid this.
|
|
32
|
-
await Promise.resolve();
|
|
33
|
-
this.readable = await getWrappedReadableStream(wrapper, controller);
|
|
34
|
-
this
|
|
35
|
-
},
|
|
36
|
-
cancel: async (reason) => {
|
|
37
|
-
await this
|
|
38
|
-
if ("cancel" in wrapper) {
|
|
39
|
-
await wrapper.cancel?.(reason);
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
pull: async (controller) => {
|
|
43
|
-
const result = await this
|
|
44
|
-
if (result.done) {
|
|
45
|
-
controller.close();
|
|
46
|
-
if ("close" in wrapper) {
|
|
47
|
-
await wrapper.close?.();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
controller.enqueue(result.value);
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
}
|
|
1
|
+
import { ReadableStream } from "./stream.js";
|
|
2
|
+
function getWrappedReadableStream(wrapper, controller) {
|
|
3
|
+
if ("start" in wrapper) {
|
|
4
|
+
return wrapper.start(controller);
|
|
5
|
+
}
|
|
6
|
+
else if (typeof wrapper === "function") {
|
|
7
|
+
return wrapper(controller);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
// Can't use `wrapper instanceof ReadableStream`
|
|
11
|
+
// Because we want to be compatible with any ReadableStream-like objects
|
|
12
|
+
return wrapper;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* This class has multiple usages:
|
|
17
|
+
*
|
|
18
|
+
* 1. Get notified when the stream is cancelled or closed.
|
|
19
|
+
* 2. Synchronously create a `ReadableStream` by asynchronously return another `ReadableStream`.
|
|
20
|
+
* 3. Convert native `ReadableStream`s to polyfilled ones so they can `pipe` between.
|
|
21
|
+
*/
|
|
22
|
+
export class WrapReadableStream extends ReadableStream {
|
|
23
|
+
readable;
|
|
24
|
+
#reader;
|
|
25
|
+
constructor(wrapper) {
|
|
26
|
+
super({
|
|
27
|
+
start: async (controller) => {
|
|
28
|
+
// `start` is invoked before `ReadableStream`'s constructor finish,
|
|
29
|
+
// so using `this` synchronously causes
|
|
30
|
+
// "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
|
|
31
|
+
// Queue a microtask to avoid this.
|
|
32
|
+
await Promise.resolve();
|
|
33
|
+
this.readable = await getWrappedReadableStream(wrapper, controller);
|
|
34
|
+
this.#reader = this.readable.getReader();
|
|
35
|
+
},
|
|
36
|
+
cancel: async (reason) => {
|
|
37
|
+
await this.#reader.cancel(reason);
|
|
38
|
+
if ("cancel" in wrapper) {
|
|
39
|
+
await wrapper.cancel?.(reason);
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
pull: async (controller) => {
|
|
43
|
+
const result = await this.#reader.read();
|
|
44
|
+
if (result.done) {
|
|
45
|
+
controller.close();
|
|
46
|
+
if ("close" in wrapper) {
|
|
47
|
+
await wrapper.close?.();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
controller.enqueue(result.value);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
57
|
//# sourceMappingURL=wrap-readable.js.map
|
package/esm/wrap-readable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-readable.js","sourceRoot":"","sources":["../src/wrap-readable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wrap-readable.js","sourceRoot":"","sources":["../src/wrap-readable.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAY7C,SAAS,wBAAwB,CAC7B,OAG8B,EAC9B,UAA8C;IAE9C,IAAI,OAAO,IAAI,OAAO,EAAE;QACpB,OAAO,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;KACpC;SAAM,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;QACtC,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;KAC9B;SAAM;QACH,gDAAgD;QAChD,wEAAwE;QACxE,OAAO,OAAO,CAAC;KAClB;AACL,CAAC;AAED;;;;;;GAMG;AACH,MAAM,OAAO,kBAAsB,SAAQ,cAAiB;IACjD,QAAQ,CAAqB;IAEpC,OAAO,CAAkC;IAEzC,YACI,OAG8B;QAE9B,KAAK,CAAC;YACF,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;gBACxB,mEAAmE;gBACnE,uCAAuC;gBACvC,gHAAgH;gBAChH,mCAAmC;gBACnC,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC;gBAExB,IAAI,CAAC,QAAQ,GAAG,MAAM,wBAAwB,CAC1C,OAAO,EACP,UAAU,CACb,CAAC;gBACF,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;YAC7C,CAAC;YACD,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;gBACrB,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAClC,IAAI,QAAQ,IAAI,OAAO,EAAE;oBACrB,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;iBAClC;YACL,CAAC;YACD,IAAI,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;gBACvB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;gBACzC,IAAI,MAAM,CAAC,IAAI,EAAE;oBACb,UAAU,CAAC,KAAK,EAAE,CAAC;oBACnB,IAAI,OAAO,IAAI,OAAO,EAAE;wBACpB,MAAM,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;qBAC3B;iBACJ;qBAAM;oBACH,UAAU,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;iBACpC;YACL,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;CACJ"}
|
package/esm/wrap-writable.d.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
private
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
+
import type { TransformStream } from "./stream.js";
|
|
3
|
+
import { WritableStream } from "./stream.js";
|
|
4
|
+
export type WrapWritableStreamStart<T> = () => ValueOrPromise<WritableStream<T>>;
|
|
5
|
+
export interface WritableStreamWrapper<T> {
|
|
6
|
+
start: WrapWritableStreamStart<T>;
|
|
7
|
+
close?(): void | Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export declare class WrapWritableStream<T> extends WritableStream<T> {
|
|
10
|
+
#private;
|
|
11
|
+
writable: WritableStream<T>;
|
|
12
|
+
constructor(wrapper: WritableStream<T> | WrapWritableStreamStart<T> | WritableStreamWrapper<T>);
|
|
13
|
+
bePipedThroughFrom<U>(transformer: TransformStream<U, T>): WrapWritableStream<U>;
|
|
14
|
+
}
|
|
13
15
|
//# sourceMappingURL=wrap-writable.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-writable.d.ts","sourceRoot":"","sources":["../src/wrap-writable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"wrap-writable.d.ts","sourceRoot":"","sources":["../src/wrap-writable.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EAAE,eAAe,EAA+B,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,MAAM,MAAM,uBAAuB,CAAC,CAAC,IAAI,MAAM,cAAc,CACzD,cAAc,CAAC,CAAC,CAAC,CACpB,CAAC;AAEF,MAAM,WAAW,qBAAqB,CAAC,CAAC;IACpC,KAAK,EAAE,uBAAuB,CAAC,CAAC,CAAC,CAAC;IAClC,KAAK,CAAC,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAClC;AAmBD,qBAAa,kBAAkB,CAAC,CAAC,CAAE,SAAQ,cAAc,CAAC,CAAC,CAAC;;IACjD,QAAQ,EAAG,cAAc,CAAC,CAAC,CAAC,CAAC;gBAKhC,OAAO,EACD,cAAc,CAAC,CAAC,CAAC,GACjB,uBAAuB,CAAC,CAAC,CAAC,GAC1B,qBAAqB,CAAC,CAAC,CAAC;IAmC3B,kBAAkB,CAAC,CAAC,EAAE,WAAW,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC;CAYlE"}
|
package/esm/wrap-writable.js
CHANGED
|
@@ -1,53 +1,63 @@
|
|
|
1
|
-
import { WritableStream } from "./stream.js";
|
|
2
|
-
async function getWrappedWritableStream(wrapper) {
|
|
3
|
-
if ("start" in wrapper) {
|
|
4
|
-
return await wrapper.start();
|
|
5
|
-
}
|
|
6
|
-
else if (typeof wrapper === "function") {
|
|
7
|
-
return await wrapper();
|
|
8
|
-
}
|
|
9
|
-
else {
|
|
10
|
-
// Can't use `wrapper instanceof WritableStream`
|
|
11
|
-
// Because we want to be compatible with any WritableStream-like objects
|
|
12
|
-
return wrapper;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export class WrapWritableStream extends WritableStream {
|
|
16
|
-
writable;
|
|
17
|
-
writer;
|
|
18
|
-
constructor(wrapper) {
|
|
19
|
-
super({
|
|
20
|
-
start: async () => {
|
|
21
|
-
// `start` is invoked before `ReadableStream`'s constructor finish,
|
|
22
|
-
// so using `this` synchronously causes
|
|
23
|
-
// "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
|
|
24
|
-
// Queue a microtask to avoid this.
|
|
25
|
-
await Promise.resolve();
|
|
26
|
-
this.writable = await getWrappedWritableStream(wrapper);
|
|
27
|
-
this
|
|
28
|
-
},
|
|
29
|
-
write: async (chunk) => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
1
|
+
import { WritableStream } from "./stream.js";
|
|
2
|
+
async function getWrappedWritableStream(wrapper) {
|
|
3
|
+
if ("start" in wrapper) {
|
|
4
|
+
return await wrapper.start();
|
|
5
|
+
}
|
|
6
|
+
else if (typeof wrapper === "function") {
|
|
7
|
+
return await wrapper();
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
// Can't use `wrapper instanceof WritableStream`
|
|
11
|
+
// Because we want to be compatible with any WritableStream-like objects
|
|
12
|
+
return wrapper;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class WrapWritableStream extends WritableStream {
|
|
16
|
+
writable;
|
|
17
|
+
#writer;
|
|
18
|
+
constructor(wrapper) {
|
|
19
|
+
super({
|
|
20
|
+
start: async () => {
|
|
21
|
+
// `start` is invoked before `ReadableStream`'s constructor finish,
|
|
22
|
+
// so using `this` synchronously causes
|
|
23
|
+
// "Must call super constructor in derived class before accessing 'this' or returning from derived constructor".
|
|
24
|
+
// Queue a microtask to avoid this.
|
|
25
|
+
await Promise.resolve();
|
|
26
|
+
this.writable = await getWrappedWritableStream(wrapper);
|
|
27
|
+
this.#writer = this.writable.getWriter();
|
|
28
|
+
},
|
|
29
|
+
write: async (chunk) => {
|
|
30
|
+
await this.#writer.write(chunk);
|
|
31
|
+
},
|
|
32
|
+
abort: async (reason) => {
|
|
33
|
+
await this.#writer.abort(reason);
|
|
34
|
+
if ("close" in wrapper) {
|
|
35
|
+
await wrapper.close?.();
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
close: async () => {
|
|
39
|
+
// Close the inner stream first.
|
|
40
|
+
// Usually the inner stream is a logical sub-stream over the outer stream,
|
|
41
|
+
// closing the outer stream first will make the inner stream incapable of
|
|
42
|
+
// sending data in its `close` handler.
|
|
43
|
+
await this.#writer.close();
|
|
44
|
+
if ("close" in wrapper) {
|
|
45
|
+
await wrapper.close?.();
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
bePipedThroughFrom(transformer) {
|
|
51
|
+
let promise;
|
|
52
|
+
return new WrapWritableStream({
|
|
53
|
+
start: () => {
|
|
54
|
+
promise = transformer.readable.pipeTo(this);
|
|
55
|
+
return transformer.writable;
|
|
56
|
+
},
|
|
57
|
+
async close() {
|
|
58
|
+
await promise;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
53
63
|
//# sourceMappingURL=wrap-writable.js.map
|
package/esm/wrap-writable.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wrap-writable.js","sourceRoot":"","sources":["../src/wrap-writable.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"wrap-writable.js","sourceRoot":"","sources":["../src/wrap-writable.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAW7C,KAAK,UAAU,wBAAwB,CACnC,OAG8B;IAE9B,IAAI,OAAO,IAAI,OAAO,EAAE;QACpB,OAAO,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;KAChC;SAAM,IAAI,OAAO,OAAO,KAAK,UAAU,EAAE;QACtC,OAAO,MAAM,OAAO,EAAE,CAAC;KAC1B;SAAM;QACH,gDAAgD;QAChD,wEAAwE;QACxE,OAAO,OAAO,CAAC;KAClB;AACL,CAAC;AAED,MAAM,OAAO,kBAAsB,SAAQ,cAAiB;IACjD,QAAQ,CAAqB;IAEpC,OAAO,CAAkC;IAEzC,YACI,OAG8B;QAE9B,KAAK,CAAC;YACF,KAAK,EAAE,KAAK,IAAI,EAAE;gBACd,mEAAmE;gBACnE,uCAAuC;gBACvC,gHAAgH;gBAChH,mCAAmC;gBACnC,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC;gBAExB,IAAI,CAAC,QAAQ,GAAG,MAAM,wBAAwB,CAAC,OAAO,CAAC,CAAC;gBACxD,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,CAAC;YAC7C,CAAC;YACD,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;gBACnB,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACpC,CAAC;YACD,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;gBACpB,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;gBACjC,IAAI,OAAO,IAAI,OAAO,EAAE;oBACpB,MAAM,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;iBAC3B;YACL,CAAC;YACD,KAAK,EAAE,KAAK,IAAI,EAAE;gBACd,gCAAgC;gBAChC,0EAA0E;gBAC1E,yEAAyE;gBACzE,uCAAuC;gBACvC,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;gBAC3B,IAAI,OAAO,IAAI,OAAO,EAAE;oBACpB,MAAM,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC;iBAC3B;YACL,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;IAEM,kBAAkB,CAAI,WAAkC;QAC3D,IAAI,OAAsB,CAAC;QAC3B,OAAO,IAAI,kBAAkB,CAAI;YAC7B,KAAK,EAAE,GAAG,EAAE;gBACR,OAAO,GAAG,WAAW,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;gBAC5C,OAAO,WAAW,CAAC,QAAQ,CAAC;YAChC,CAAC;YACD,KAAK,CAAC,KAAK;gBACP,MAAM,OAAO,CAAC;YAClB,CAAC;SACJ,CAAC,CAAC;IACP,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yume-chan/stream-extra",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.20",
|
|
4
4
|
"description": "Extensions to Web Streams API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"stream",
|
|
@@ -26,24 +26,25 @@
|
|
|
26
26
|
"types": "esm/index.d.ts",
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@yume-chan/async": "^2.2.0",
|
|
29
|
-
"@yume-chan/struct": "^0.0.
|
|
30
|
-
"tslib": "^2.
|
|
29
|
+
"@yume-chan/struct": "^0.0.20",
|
|
30
|
+
"tslib": "^2.5.2",
|
|
31
31
|
"web-streams-polyfill": "^4.0.0-beta.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@jest/globals": "^29.
|
|
34
|
+
"@jest/globals": "^29.5.0",
|
|
35
35
|
"@yume-chan/eslint-config": "^1.0.0",
|
|
36
36
|
"@yume-chan/tsconfig": "^1.0.0",
|
|
37
37
|
"cross-env": "^7.0.3",
|
|
38
|
-
"eslint": "^8.
|
|
39
|
-
"jest": "^29.
|
|
38
|
+
"eslint": "^8.41.0",
|
|
39
|
+
"jest": "^29.5.0",
|
|
40
|
+
"prettier": "^2.8.8",
|
|
40
41
|
"ts-jest": "^29.0.4",
|
|
41
|
-
"typescript": "^
|
|
42
|
+
"typescript": "^5.0.3"
|
|
42
43
|
},
|
|
43
44
|
"scripts": {
|
|
44
45
|
"build": "tsc -b tsconfig.build.json",
|
|
45
46
|
"build:watch": "tsc -b tsconfig.build.json",
|
|
46
47
|
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage",
|
|
47
|
-
"lint": "eslint src/**/*.ts --fix"
|
|
48
|
+
"lint": "eslint src/**/*.ts --fix && prettier src/**/*.ts --write --tab-width 4"
|
|
48
49
|
}
|
|
49
50
|
}
|
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ValueOrPromise } from "@yume-chan/struct";
|
|
2
|
+
import { StructEmptyError } from "@yume-chan/struct";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./
|
|
7
|
-
import {
|
|
8
|
-
PushReadableStream,
|
|
9
|
-
type PushReadableStreamController,
|
|
10
|
-
} from "./push-readable.js";
|
|
11
|
-
import {
|
|
12
|
-
ReadableStream,
|
|
13
|
-
WritableStream,
|
|
14
|
-
type ReadableWritablePair,
|
|
15
|
-
} from "./stream.js";
|
|
4
|
+
import { BufferedReadableStream } from "./buffered.js";
|
|
5
|
+
import type { PushReadableStreamController } from "./push-readable.js";
|
|
6
|
+
import { PushReadableStream } from "./push-readable.js";
|
|
7
|
+
import type { ReadableWritablePair } from "./stream.js";
|
|
8
|
+
import { ReadableStream, WritableStream } from "./stream.js";
|
|
16
9
|
|
|
17
10
|
// TODO: BufferedTransformStream: find better implementation
|
|
18
11
|
export class BufferedTransformStream<T>
|
|
19
12
|
implements ReadableWritablePair<T, Uint8Array>
|
|
20
13
|
{
|
|
21
|
-
|
|
14
|
+
#readable: ReadableStream<T>;
|
|
22
15
|
public get readable() {
|
|
23
|
-
return this
|
|
16
|
+
return this.#readable;
|
|
24
17
|
}
|
|
25
18
|
|
|
26
|
-
|
|
19
|
+
#writable: WritableStream<Uint8Array>;
|
|
27
20
|
public get writable() {
|
|
28
|
-
return this
|
|
21
|
+
return this.#writable;
|
|
29
22
|
}
|
|
30
23
|
|
|
31
24
|
constructor(
|
|
@@ -40,16 +33,16 @@ export class BufferedTransformStream<T>
|
|
|
40
33
|
})
|
|
41
34
|
);
|
|
42
35
|
|
|
43
|
-
this
|
|
36
|
+
this.#readable = new ReadableStream<T>({
|
|
44
37
|
async pull(controller) {
|
|
45
38
|
try {
|
|
46
39
|
const value = await transform(buffered);
|
|
47
40
|
controller.enqueue(value);
|
|
48
41
|
} catch (e) {
|
|
49
|
-
// Treat `
|
|
42
|
+
// Treat `StructEmptyError` as a normal end.
|
|
50
43
|
// If the `transform` method doesn't have enough data to return a value,
|
|
51
44
|
// it should throw another error to indicate that.
|
|
52
|
-
if (e instanceof
|
|
45
|
+
if (e instanceof StructEmptyError) {
|
|
53
46
|
controller.close();
|
|
54
47
|
return;
|
|
55
48
|
}
|
|
@@ -63,7 +56,7 @@ export class BufferedTransformStream<T>
|
|
|
63
56
|
},
|
|
64
57
|
});
|
|
65
58
|
|
|
66
|
-
this
|
|
59
|
+
this.#writable = new WritableStream({
|
|
67
60
|
async write(chunk) {
|
|
68
61
|
await sourceStreamController.enqueue(chunk);
|
|
69
62
|
},
|