@yume-chan/stream-extra 0.0.22 → 0.0.24
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 +36 -0
- package/CHANGELOG.md +19 -1
- package/LICENSE +1 -1
- package/esm/concat.d.ts +4 -3
- package/esm/concat.d.ts.map +1 -1
- package/esm/concat.js +6 -4
- package/esm/concat.js.map +1 -1
- package/esm/consumable.d.ts +31 -43
- package/esm/consumable.d.ts.map +1 -1
- package/esm/consumable.js +96 -144
- package/esm/consumable.js.map +1 -1
- package/esm/distribution.d.ts +3 -2
- package/esm/distribution.d.ts.map +1 -1
- package/esm/distribution.js +22 -18
- package/esm/distribution.js.map +1 -1
- 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 +3 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -1
- package/esm/index.js.map +1 -1
- 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 +1 -1
- package/esm/pipe-from.d.ts.map +1 -1
- package/esm/stream.d.ts +9 -10
- package/esm/stream.d.ts.map +1 -1
- package/esm/stream.js +6 -12
- package/esm/stream.js.map +1 -1
- package/esm/struct-deserialize.d.ts +1 -1
- package/esm/struct-deserialize.d.ts.map +1 -1
- package/esm/struct-deserialize.js.map +1 -1
- package/esm/struct-serialize.d.ts +1 -1
- package/esm/struct-serialize.d.ts.map +1 -1
- 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 +778 -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-writable.d.ts +1 -1
- package/esm/wrap-writable.d.ts.map +1 -1
- package/esm/wrap-writable.js +12 -12
- package/esm/wrap-writable.js.map +1 -1
- package/package.json +9 -12
- package/src/concat.ts +6 -4
- package/src/consumable.ts +145 -233
- package/src/distribution.ts +30 -20
- package/src/encoding.ts +39 -0
- package/src/index.ts +3 -1
- package/src/maybe-consumable.ts +101 -0
- package/src/stream.ts +20 -25
- package/src/struct-deserialize.ts +2 -2
- package/src/struct-serialize.ts +1 -1
- package/src/task.ts +21 -0
- package/src/types.ts +881 -0
- package/src/wrap-writable.ts +12 -12
- package/tsconfig.build.tsbuildinfo +1 -1
- package/esm/decode-utf8.d.ts +0 -5
- package/esm/decode-utf8.d.ts.map +0 -1
- package/esm/decode-utf8.js +0 -12
- package/esm/decode-utf8.js.map +0 -1
- package/src/decode-utf8.ts +0 -13
package/src/distribution.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Consumable } from "./consumable.js";
|
|
2
|
+
import { MaybeConsumable } from "./maybe-consumable.js";
|
|
3
|
+
import { TransformStream } from "./stream.js";
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Splits or combines buffers to specified size.
|
|
@@ -25,7 +27,7 @@ export class BufferCombiner {
|
|
|
25
27
|
*/
|
|
26
28
|
*push(data: Uint8Array): Generator<Uint8Array, void, void> {
|
|
27
29
|
let offset = 0;
|
|
28
|
-
let available = data.
|
|
30
|
+
let available = data.length;
|
|
29
31
|
|
|
30
32
|
if (this.#offset !== 0) {
|
|
31
33
|
if (available >= this.#available) {
|
|
@@ -77,34 +79,42 @@ export class BufferCombiner {
|
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
export class DistributionStream extends
|
|
81
|
-
Uint8Array
|
|
82
|
-
Uint8Array
|
|
82
|
+
export class DistributionStream extends TransformStream<
|
|
83
|
+
MaybeConsumable<Uint8Array>,
|
|
84
|
+
MaybeConsumable<Uint8Array>
|
|
83
85
|
> {
|
|
84
86
|
constructor(size: number, combine = false) {
|
|
85
87
|
const combiner = combine ? new BufferCombiner(size) : undefined;
|
|
86
88
|
super({
|
|
87
89
|
async transform(chunk, controller) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
available
|
|
90
|
+
await MaybeConsumable.tryConsume(chunk, async (chunk) => {
|
|
91
|
+
if (combiner) {
|
|
92
|
+
for (const buffer of combiner.push(chunk)) {
|
|
93
|
+
await Consumable.ReadableStream.enqueue(
|
|
94
|
+
controller,
|
|
95
|
+
buffer,
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
let offset = 0;
|
|
100
|
+
let available = chunk.length;
|
|
101
|
+
while (available > 0) {
|
|
102
|
+
const end = offset + size;
|
|
103
|
+
await Consumable.ReadableStream.enqueue(
|
|
104
|
+
controller,
|
|
105
|
+
chunk.subarray(offset, end),
|
|
106
|
+
);
|
|
107
|
+
offset = end;
|
|
108
|
+
available -= size;
|
|
109
|
+
}
|
|
100
110
|
}
|
|
101
|
-
}
|
|
111
|
+
});
|
|
102
112
|
},
|
|
103
|
-
|
|
113
|
+
flush(controller) {
|
|
104
114
|
if (combiner) {
|
|
105
115
|
const data = combiner.flush();
|
|
106
116
|
if (data) {
|
|
107
|
-
|
|
117
|
+
controller.enqueue(data);
|
|
108
118
|
}
|
|
109
119
|
}
|
|
110
120
|
},
|
package/src/encoding.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { TransformStream } from "./stream.js";
|
|
2
|
+
|
|
3
|
+
export interface TextDecoderOptions {
|
|
4
|
+
fatal?: boolean;
|
|
5
|
+
ignoreBOM?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare class TextDecoderStreamType extends TransformStream<
|
|
9
|
+
ArrayBufferView | ArrayBuffer,
|
|
10
|
+
string
|
|
11
|
+
> {
|
|
12
|
+
constructor(label?: string, options?: TextDecoderOptions);
|
|
13
|
+
|
|
14
|
+
readonly encoding: string;
|
|
15
|
+
readonly fatal: boolean;
|
|
16
|
+
readonly ignoreBOM: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare class TextEncoderStreamType extends TransformStream<
|
|
20
|
+
string,
|
|
21
|
+
Uint8Array
|
|
22
|
+
> {
|
|
23
|
+
constructor();
|
|
24
|
+
|
|
25
|
+
readonly encoding: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface GlobalExtension {
|
|
29
|
+
TextDecoderStream: typeof TextDecoderStreamType;
|
|
30
|
+
TextEncoderStream: typeof TextEncoderStreamType;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const Global = globalThis as unknown as GlobalExtension;
|
|
34
|
+
|
|
35
|
+
export const TextDecoderStream = Global.TextDecoderStream;
|
|
36
|
+
export type TextDecoderStream = TextDecoderStreamType;
|
|
37
|
+
|
|
38
|
+
export const TextEncoderStream = Global.TextEncoderStream;
|
|
39
|
+
export type TextEncoderStream = TextEncoderStreamType;
|
package/src/index.ts
CHANGED
|
@@ -2,15 +2,17 @@ export * from "./buffered-transform.js";
|
|
|
2
2
|
export * from "./buffered.js";
|
|
3
3
|
export * from "./concat.js";
|
|
4
4
|
export * from "./consumable.js";
|
|
5
|
-
export * from "./decode-utf8.js";
|
|
6
5
|
export * from "./distribution.js";
|
|
7
6
|
export * from "./duplex.js";
|
|
7
|
+
export * from "./encoding.js";
|
|
8
8
|
export * from "./inspect.js";
|
|
9
|
+
export * from "./maybe-consumable.js";
|
|
9
10
|
export * from "./pipe-from.js";
|
|
10
11
|
export * from "./push-readable.js";
|
|
11
12
|
export * from "./split-string.js";
|
|
12
13
|
export * from "./stream.js";
|
|
13
14
|
export * from "./struct-deserialize.js";
|
|
14
15
|
export * from "./struct-serialize.js";
|
|
16
|
+
export * from "./task.js";
|
|
15
17
|
export * from "./wrap-readable.js";
|
|
16
18
|
export * from "./wrap-writable.js";
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Consumable } from "./consumable.js";
|
|
2
|
+
import type {
|
|
3
|
+
QueuingStrategy,
|
|
4
|
+
WritableStreamDefaultController,
|
|
5
|
+
} from "./stream.js";
|
|
6
|
+
import {
|
|
7
|
+
WritableStream as NativeWritableStream,
|
|
8
|
+
TransformStream,
|
|
9
|
+
} from "./stream.js";
|
|
10
|
+
|
|
11
|
+
export type MaybeConsumable<T> = T | Consumable<T>;
|
|
12
|
+
|
|
13
|
+
export namespace MaybeConsumable {
|
|
14
|
+
export function getValue<T>(value: MaybeConsumable<T>): T {
|
|
15
|
+
return value instanceof Consumable ? value.value : value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function tryConsume<T, R>(
|
|
19
|
+
value: T,
|
|
20
|
+
callback: (value: T extends Consumable<infer U> ? U : T) => R,
|
|
21
|
+
): R {
|
|
22
|
+
if (value instanceof Consumable) {
|
|
23
|
+
return value.tryConsume(callback);
|
|
24
|
+
} else {
|
|
25
|
+
return callback(value as never);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class UnwrapStream<T> extends TransformStream<
|
|
30
|
+
MaybeConsumable<T>,
|
|
31
|
+
T
|
|
32
|
+
> {
|
|
33
|
+
constructor() {
|
|
34
|
+
super({
|
|
35
|
+
transform(chunk, controller) {
|
|
36
|
+
MaybeConsumable.tryConsume(chunk, (chunk) => {
|
|
37
|
+
controller.enqueue(chunk as T);
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface WritableStreamSink<in T> {
|
|
45
|
+
start?(
|
|
46
|
+
controller: WritableStreamDefaultController,
|
|
47
|
+
): void | PromiseLike<void>;
|
|
48
|
+
write?(
|
|
49
|
+
chunk: T,
|
|
50
|
+
controller: WritableStreamDefaultController,
|
|
51
|
+
): void | PromiseLike<void>;
|
|
52
|
+
abort?(reason: unknown): void | PromiseLike<void>;
|
|
53
|
+
close?(): void | PromiseLike<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class WritableStream<in T> extends NativeWritableStream<
|
|
57
|
+
MaybeConsumable<T>
|
|
58
|
+
> {
|
|
59
|
+
constructor(
|
|
60
|
+
sink: WritableStreamSink<T>,
|
|
61
|
+
strategy?: QueuingStrategy<T>,
|
|
62
|
+
) {
|
|
63
|
+
let wrappedStrategy:
|
|
64
|
+
| QueuingStrategy<MaybeConsumable<T>>
|
|
65
|
+
| undefined;
|
|
66
|
+
if (strategy) {
|
|
67
|
+
wrappedStrategy = {};
|
|
68
|
+
if ("highWaterMark" in strategy) {
|
|
69
|
+
wrappedStrategy.highWaterMark = strategy.highWaterMark;
|
|
70
|
+
}
|
|
71
|
+
if ("size" in strategy) {
|
|
72
|
+
wrappedStrategy.size = (chunk) => {
|
|
73
|
+
return strategy.size!(
|
|
74
|
+
chunk instanceof Consumable ? chunk.value : chunk,
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
super(
|
|
81
|
+
{
|
|
82
|
+
start(controller) {
|
|
83
|
+
return sink.start?.(controller);
|
|
84
|
+
},
|
|
85
|
+
async write(chunk, controller) {
|
|
86
|
+
await MaybeConsumable.tryConsume(chunk, (chunk) =>
|
|
87
|
+
sink.write?.(chunk as T, controller),
|
|
88
|
+
);
|
|
89
|
+
},
|
|
90
|
+
abort(reason) {
|
|
91
|
+
return sink.abort?.(reason);
|
|
92
|
+
},
|
|
93
|
+
close() {
|
|
94
|
+
return sink.close?.();
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
wrappedStrategy,
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/stream.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
ReadableStream as
|
|
4
|
-
TransformStream as
|
|
5
|
-
WritableStream as
|
|
6
|
-
} from "
|
|
7
|
-
|
|
1
|
+
import type {
|
|
2
|
+
AbortSignal,
|
|
3
|
+
ReadableStream as ReadableStreamType,
|
|
4
|
+
TransformStream as TransformStreamType,
|
|
5
|
+
WritableStream as WritableStreamType,
|
|
6
|
+
} from "./types.js";
|
|
7
|
+
|
|
8
|
+
export * from "./types.js";
|
|
8
9
|
|
|
9
10
|
/** A controller object that allows you to abort one or more DOM requests as and when desired. */
|
|
10
11
|
export interface AbortController {
|
|
@@ -16,7 +17,7 @@ export interface AbortController {
|
|
|
16
17
|
/**
|
|
17
18
|
* Invoking this method will set this object's AbortSignal's aborted flag and signal to any observers that the associated activity is to be aborted.
|
|
18
19
|
*/
|
|
19
|
-
abort(reason?:
|
|
20
|
+
abort(reason?: unknown): void;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
interface AbortControllerConstructor {
|
|
@@ -26,26 +27,20 @@ interface AbortControllerConstructor {
|
|
|
26
27
|
|
|
27
28
|
interface GlobalExtension {
|
|
28
29
|
AbortController: AbortControllerConstructor;
|
|
29
|
-
ReadableStream: typeof
|
|
30
|
-
WritableStream: typeof
|
|
31
|
-
TransformStream: typeof
|
|
30
|
+
ReadableStream: typeof ReadableStreamType;
|
|
31
|
+
WritableStream: typeof WritableStreamType;
|
|
32
|
+
TransformStream: typeof TransformStreamType;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
export const AbortController = GLOBAL.AbortController;
|
|
35
|
+
const Global = globalThis as unknown as GlobalExtension;
|
|
37
36
|
|
|
38
|
-
export
|
|
39
|
-
export let ReadableStream = ReadableStreamPolyfill;
|
|
37
|
+
export const AbortController = Global.AbortController;
|
|
40
38
|
|
|
41
|
-
export type
|
|
42
|
-
export
|
|
39
|
+
export type ReadableStream<out T> = ReadableStreamType<T>;
|
|
40
|
+
export const ReadableStream = Global.ReadableStream;
|
|
43
41
|
|
|
44
|
-
export type
|
|
45
|
-
export
|
|
42
|
+
export type WritableStream<in T> = WritableStreamType<T>;
|
|
43
|
+
export const WritableStream = Global.WritableStream;
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
WritableStream = GLOBAL.WritableStream;
|
|
50
|
-
TransformStream = GLOBAL.TransformStream;
|
|
51
|
-
}
|
|
45
|
+
export type TransformStream<I, O> = TransformStreamType<I, O>;
|
|
46
|
+
export const TransformStream = Global.TransformStream;
|
|
@@ -4,11 +4,11 @@ import type { StructValueType } from "@yume-chan/struct";
|
|
|
4
4
|
import { BufferedTransformStream } from "./buffered-transform.js";
|
|
5
5
|
|
|
6
6
|
export class StructDeserializeStream<
|
|
7
|
-
T extends Struct<
|
|
7
|
+
T extends Struct<object, PropertyKey, object, unknown>,
|
|
8
8
|
> extends BufferedTransformStream<StructValueType<T>> {
|
|
9
9
|
constructor(struct: T) {
|
|
10
10
|
super((stream) => {
|
|
11
|
-
return struct.deserialize(stream);
|
|
11
|
+
return struct.deserialize(stream) as never;
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
14
|
}
|
package/src/struct-serialize.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type Struct from "@yume-chan/struct";
|
|
|
3
3
|
import { TransformStream } from "./stream.js";
|
|
4
4
|
|
|
5
5
|
export class StructSerializeStream<
|
|
6
|
-
T extends Struct<
|
|
6
|
+
T extends Struct<object, PropertyKey, object, unknown>,
|
|
7
7
|
> extends TransformStream<T["TInit"], Uint8Array> {
|
|
8
8
|
constructor(struct: T) {
|
|
9
9
|
super({
|
package/src/task.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface Task {
|
|
2
|
+
run<T>(callback: () => T): T;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
interface Console {
|
|
6
|
+
createTask(name: string): Task;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
interface GlobalExtension {
|
|
10
|
+
console?: Console;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// `createTask` allows browser DevTools to track the call stack across async boundaries.
|
|
14
|
+
const { console } = globalThis as unknown as GlobalExtension;
|
|
15
|
+
export const createTask: (name: string) => Task =
|
|
16
|
+
console?.createTask?.bind(console) ??
|
|
17
|
+
(() => ({
|
|
18
|
+
run(callback) {
|
|
19
|
+
return callback();
|
|
20
|
+
},
|
|
21
|
+
}));
|