Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | 418x 418x 418x 69x 418x 418x 418x 418x 418x 418x 16x 485x 319x 1645x 2x 2x 1x 1x 1x 1x 218x 218x 2x 69x 209x 418x 209x 209x 209x 1675x 1675x 2395x 1674x 1674x 1674x 1675x 1x 78x 206x 418x | import { stringify } from '@metamask/kernel-utils';
import type { DuplexStream } from './BaseDuplexStream.ts';
import { BaseReader } from './BaseStream.ts';
import type { BaseReaderArgs, ReceiveInput } from './BaseStream.ts';
/**
* A reader for use within {@link split} that reads from a reader and forwards
* writes to a parent. The reader should output a subset of the parent stream's values based
* on some predicate.
*/
class SplitReader<Read> extends BaseReader<Read> {
/**
* Constructs a new {@link SplitReader}.
*
* @param args - The arguments to pass to the base reader.
*/
// eslint-disable-next-line no-restricted-syntax
private constructor(args: BaseReaderArgs<Read>) {
super(args);
}
/**
* Creates a new {@link SplitReader}.
*
* @param args - The arguments to pass to the base reader.
* @returns A new {@link SplitReader} and the receive input function.
*/
static make<Read>(
args: BaseReaderArgs<Read>,
): [SplitReader<Read>, ReceiveInput] {
const reader = new SplitReader<Read>(args);
return [reader, reader.getReceiveInput()] as const;
}
}
harden(SplitReader);
/**
* A {@link DuplexStream} for use within {@link split} that reads from a reader and forwards
* writes to a parent. The reader should output a subset of the parent stream's values based
* on some predicate.
*/
class SplitStream<ParentRead, Read extends ParentRead, Write>
implements DuplexStream<Read, Write>
{
readonly #parent: DuplexStream<ParentRead, Write>;
readonly #reader: SplitReader<Read>;
/**
* Constructs a new {@link SplitStream}.
*
* @param parent - The parent stream to read from.
* @param reader - The reader to use to read from the parent stream.
*/
constructor(
parent: DuplexStream<ParentRead, Write>,
reader: SplitReader<Read>,
) {
this.#parent = parent;
this.#reader = reader;
harden(this);
}
/**
* Constructs a new {@link SplitStream}.
*
* @param parent - The parent stream to read from.
* @returns A new {@link SplitStream} and the receive input function.
*/
static make<ParentRead, Read extends ParentRead, Write>(
parent: DuplexStream<ParentRead, Write>,
): {
stream: SplitStream<ParentRead, Read, Write>;
receiveInput: ReceiveInput;
} {
const [reader, receiveInput] = SplitReader.make<Read>({
name: this.constructor.name,
});
const stream = new SplitStream(parent, reader);
return { stream, receiveInput };
}
/**
* Reads the next value from the stream.
*
* @returns The next value from the stream.
*/
async next(): Promise<IteratorResult<Read, undefined>> {
return this.#reader.next();
}
/**
* Writes a value to the stream.
*
* @param value - The value to write to the stream.
* @returns The result of writing the value.
*/
async write(value: Write): Promise<IteratorResult<undefined, undefined>> {
return this.#parent.write(value);
}
/**
* Drains the stream by passing each value to a handler function.
*
* @param handler - The function that will receive each value from the stream.
*/
async drain(handler: (value: Read) => void | Promise<void>): Promise<void> {
for await (const value of this.#reader) {
await handler(value);
}
}
/**
* Pipes the stream to another duplex stream.
*
* @param sink - The duplex stream to pipe to.
*/
async pipe<Read2>(sink: DuplexStream<Read2, Read>): Promise<void> {
await this.drain(async (value) => {
await sink.write(value);
});
}
/**
* Closes the stream. Idempotent.
*
* @returns The final result for this stream.
*/
async return(): Promise<IteratorResult<Read, undefined>> {
await this.#parent.return();
return this.#reader.return();
}
/**
* Closes the stream with an error. Idempotent.
*
* @param error - The error to close the stream with.
* @returns The final result for this stream.
*/
async throw(error: Error): Promise<IteratorResult<Read, undefined>> {
await this.#parent.throw(error);
return this.#reader.throw(error);
}
/**
* Closes the stream. Syntactic sugar for `throw(error)` or `return()`. Idempotent.
*
* @param error - The error to close the stream with.
* @returns The final result for this stream.
*/
async end(error?: Error): Promise<IteratorResult<Read, undefined>> {
await this.#parent.end(error);
return this.#reader.end(error);
}
/**
* Returns the async iterator for this stream.
*
* @returns This stream as an async iterator.
*/
[Symbol.asyncIterator](): typeof this {
return this;
}
}
harden(SplitStream);
// There's no reason to do this but we leave it in for the sake of completeness.
export function split<Read, Write, ReadA extends Read>(
stream: DuplexStream<Read, Write>,
predicateA: (value: Read) => value is ReadA,
): [DuplexStream<ReadA, Write>];
export function split<Read, Write, ReadA extends Read, ReadB extends Read>(
stream: DuplexStream<Read, Write>,
predicateA: (value: Read) => value is ReadA,
predicateB: (value: Read) => value is ReadB,
): [DuplexStream<ReadA, Write>, DuplexStream<ReadB, Write>];
export function split<
Read,
Write,
ReadA extends Read,
ReadB extends Read,
ReadC extends Read,
>(
stream: DuplexStream<Read, Write>,
predicateA: (value: Read) => value is ReadA,
predicateB: (value: Read) => value is ReadB,
predicateC: (value: Read) => value is ReadC,
): [
DuplexStream<ReadA, Write>,
DuplexStream<ReadB, Write>,
DuplexStream<ReadC, Write>,
];
export function split<
Read,
Write,
ReadA extends Read,
ReadB extends Read,
ReadC extends Read,
ReadD extends Read,
>(
stream: DuplexStream<Read, Write>,
predicateA: (value: Read) => value is ReadA,
predicateB: (value: Read) => value is ReadB,
predicateC: (value: Read) => value is ReadC,
predicateD: (value: Read) => value is ReadD,
): [
DuplexStream<ReadA, Write>,
DuplexStream<ReadB, Write>,
DuplexStream<ReadC, Write>,
DuplexStream<ReadD, Write>,
];
/**
* Splits a stream into multiple streams based on a list of predicates.
* Supports up to 4 predicates with type checking, and any number without!
*
* @param parentStream - The stream to split.
* @param predicates - The predicates to use to split the stream.
* @returns An array of "splits" of the parent stream.
*/
export function split<Read, Write>(
parentStream: DuplexStream<Read, Write>,
...predicates: ((value: Read) => boolean)[]
): DuplexStream<Read, Write>[] {
const splits = predicates.map(
(predicate) => [predicate, SplitStream.make(parentStream)] as const,
);
// eslint-disable-next-line no-void
void (async () => {
let error: Error | undefined;
try {
for await (const value of parentStream) {
let matched = false;
for (const [predicate, { receiveInput }] of splits) {
if (predicate(value)) {
matched = true;
await receiveInput(value);
break;
}
}
if (!matched) {
throw new Error(
`Failed to match any predicate for value: ${stringify(value)}`,
);
}
}
} catch (caughtError) {
error = caughtError as Error;
}
await Promise.all(splits.map(async ([, { stream }]) => stream.end(error)));
})();
return splits.map(([, { stream }]) => stream);
}
|