-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.ts
177 lines (159 loc) · 6.13 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Converts a ReadableStream into an AsyncIterable so the stream can be consumed using "for await".
* In the latest Streams API, ReadableStream is an AsyncIterable, but not all browsers support this yet.
*/
export async function* streamToIterable<T>(stream: ReadableStream<T>): AsyncGenerator<T, void, undefined> {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
return;
} else {
yield value;
}
}
} finally {
// Is also called if iterator is quit early (by using break;)
reader.cancel().catch(() => undefined);
}
}
/**
* Converts a sync/async iterable into an UnderlyingDefaultSource, which can be used as the argument to construct a ReadableStream.
*/
export function iterableToSource<T>(iterable: AsyncIterable<T> | Iterable<T>): UnderlyingDefaultSource<T> {
const iterator = Symbol.asyncIterator in iterable ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
return {
async pull(controller) {
const { value, done } = await iterator.next();
if (done) {
controller.close();
} else {
controller.enqueue(value);
}
},
};
}
export function iterableToStream<T>(iterable: AsyncIterable<T> | Iterable<T>, strategy?: QueuingStrategy<T>): ReadableStream<T> {
return new ReadableStream<T>(iterableToSource(iterable), strategy);
}
export async function streamToArray<T>(stream: ReadableStream<T>): Promise<T[]> {
const reader = stream.getReader();
const result: T[] = [];
// eslint-disable-next-line no-constant-condition
while (true) {
const { done, value } = await reader.read();
if (done) {
return result;
} else {
result.push(value);
}
}
}
export async function streamToString(stream: ReadableStream<string>): Promise<string> {
return (await streamToArray(stream)).join("");
}
export function stringToStream(string: string): ReadableStream<string> {
return iterableToStream([string]);
}
export function concatStreams<T>(...streams: Array<ReadableStream<T> | (() => ReadableStream<T>)>): ReadableStream<T> {
const transform = new TransformStream();
(async () => {
for (const stream of streams) {
await (typeof stream === "function" ? stream() : stream).pipeTo(transform.writable, { preventClose: true });
}
await transform.writable.close();
})().catch(async (err) => {
await transform.writable.abort(err);
});
return transform.readable;
}
export type TransformerAbortCallback<O> = (reason: any, controller: TransformStreamDefaultController<O>) => void | PromiseLike<void>;
/**
* A transform stream that provides an abort() handler to transform stream abortions.
* More info can be found on https://stackoverflow.com/a/78489418/242365.
*/
export class AbortHandlingTransformStream<I, O> extends TransformStream<I, O> {
constructor(
transformer?: Transformer<I, O> & {
abort?: TransformerAbortCallback<O>;
},
writableStrategy?: QueuingStrategy<I>,
readableStrategy?: QueuingStrategy<O>
) {
const { abort, start, ...rest } = transformer ?? {};
let controller: TransformStreamDefaultController<O>;
super({
...rest,
start: (c) => {
controller = c;
start?.(c);
}
}, writableStrategy, readableStrategy);
const writer = this.writable.getWriter();
const writable = new WritableStream({
write: (chunk) => writer.write(chunk),
close: () => writer.close(),
abort: async (reason) => {
if (abort) {
try {
await abort(reason, controller);
} catch (err: any) {
await writer.abort(err);
}
} else {
await writer.abort(reason);
}
}
});
Object.defineProperty(this, "writable", {
get: () => writable,
configurable: true
});
}
}
/**
* A transform stream where rather than specifying a transformer as a constructor argument, you override its methods to implement the
* transformation.
*/
export abstract class AbstractTransformStream<I, O> extends AbortHandlingTransformStream<I, O> {
constructor(writableStrategy?: QueuingStrategy<I>, readableStrategy?: QueuingStrategy<O>) {
super({
transform: (chunk, controller) => {
return this.transform(chunk, controller);
},
flush: (controller) => {
return this.flush(controller);
},
abort: (reason, controller) => {
return this.abort(reason, controller);
}
}, writableStrategy, readableStrategy);
}
protected abstract transform(chunk: I, controller: TransformStreamDefaultController<O>): void | Promise<void>;
protected flush(controller: TransformStreamDefaultController<O>): void | Promise<void> {
controller.terminate();
}
protected abort(reason: any, controller: TransformStreamDefaultController<O>): void | Promise<void> {
controller.error(reason);
}
}
/**
* A TransformStream that is set up by providing a ReadableStream mapper rather than transforming individual chunks using
* start(), transform() and flush().
* This allows access to ReadableStream methods such as pipeThrough(), which makes it easy to reuse other TransformStreams
* in the implementation.
* @param transformReadable Retrieves one parameter with a ReadableStream that emits the input data of the TransformStream.
* Should return a ReadableStream whose output will become the output data of the TransformStream.
*/
// https://stackoverflow.com/a/78404600/242365
export class PipeableTransformStream<I, O> extends TransformStream<I, O> {
constructor(transformReadable: (readable: ReadableStream<I>) => ReadableStream<O>, writableStrategy?: QueuingStrategy<I>, readableStrategy?: QueuingStrategy<O>) {
super({}, writableStrategy);
const readable = transformReadable(this.readable as any).pipeThrough(new TransformStream({}, undefined, readableStrategy));
Object.defineProperty(this, "readable", { get: () => readable });
}
}
export function arrayStartsWith<T>(array: T[], startsWith: T[]): boolean {
return array.length >= startsWith.length && startsWith.every((v, i) => array[i] === v);
}