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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | 75x 756x 756x 756x 756x 436x 112x 324x 324x 210x 324x 9x 3891x 2x 3889x 3673x 3673x 3673x 216x 4483x 184x 184x 4299x 44x 4311x 4255x 4255x 75x 756x 756x 756x 756x 756x 756x 757x 1x 756x 756x 3916x 3916x 3916x 3916x 5x 5x 3911x 16x 15x 3895x 4x 4x 3887x 9x 4x 9x 436x 436x 436x 436x 324x 4483x 212x 212x 199x 199x 220x 75x 307x 307x 307x 307x 307x 307x 1167x 1167x 1149x 18x 6x 6x 1x 12x 6x 121x 121x 121x 121x 4x 1148x 6x 1142x 162x 97x 97x 162x 29x 13x 28x 2x 25x 25x 24x 24x 75x | import { makePromiseKit } from '@endo/promise-kit';
import type { Reader, Writer } from '@endo/stream';
import { stringify } from '@metamask/kernel-utils';
import type { PromiseCallbacks } from '@metamask/kernel-utils';
import type { Dispatchable, Writable } from './utils.ts';
import {
makeDoneResult,
makePendingResult,
makeStreamDoneSignal,
makeStreamErrorSignal,
marshal,
StreamDoneSymbol,
unmarshal,
} from './utils.ts';
const makeStreamBuffer = <
Value extends IteratorResult<unknown, undefined>,
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
>() => {
const inputBuffer: (Value | Error)[] = [];
const outputBuffer: PromiseCallbacks[] = [];
let done = false;
return {
/**
* Flushes pending reads with a value or error, and causes subsequent writes to be ignored.
* Subsequent reads will exhaust any puts, then return the error (if any), and finally a `done` result.
* Idempotent.
*
* @param error - The error to end the stream with. A `done` result is used if not provided.
*/
end: (error?: Error): void => {
if (done) {
return;
}
done = true;
for (const { resolve, reject } of outputBuffer) {
error ? reject(error) : resolve(makeDoneResult() as Value);
}
outputBuffer.length = 0;
},
hasPendingReads(): boolean {
return outputBuffer.length > 0;
},
/**
* Puts a value or error into the buffer.
*
* @see `end()` for behavior when the stream ends.
* @param value - The value or error to put.
*/
put(value: Value | Error): void {
if (done) {
return;
}
if (outputBuffer.length > 0) {
const { resolve } = outputBuffer.shift() as PromiseCallbacks;
resolve(value);
return;
}
inputBuffer.push(value);
},
async get(): Promise<Value> {
if (inputBuffer.length > 0) {
const value = inputBuffer.shift() as Value;
return value instanceof Error
? Promise.reject(value)
: Promise.resolve(value);
}
if (done) {
return makeDoneResult() as Value;
}
const { promise, resolve, reject } = makePromiseKit<Value>();
outputBuffer.push({
resolve: resolve as (value: unknown) => void,
reject,
});
return promise;
},
};
};
harden(makeStreamBuffer);
/**
* A function that is called when a stream ends. Useful for cleanup, such as closing a
* message port.
*/
export type OnEnd = (error?: Error) => void | Promise<void>;
/**
* A function that validates input to a readable stream.
*/
export type ValidateInput<Read> = (input: unknown) => input is Read;
/**
* A function that receives input from a transport mechanism to a readable stream.
* Validates that the input is an {@link IteratorResult}, and throws if it is not.
*/
export type ReceiveInput = (input: unknown) => Promise<void>;
export type BaseReaderArgs<Read> = {
name?: string | undefined;
onEnd?: OnEnd | undefined;
validateInput?: ValidateInput<Read> | undefined;
};
/**
* The base of a readable async iterator stream.
*
* Subclasses must forward input received from the transport mechanism via the function
* returned by `getReceiveInput()`. Any cleanup required by subclasses should be performed
* in a callback passed to `setOnEnd()`.
*
* The result of any value received before the stream ends is guaranteed to be observable
* by the consumer.
*/
export class BaseReader<Read> implements Reader<Read> {
/**
* A buffer for managing backpressure (writes > reads) and "suction" (reads > writes) for a stream.
* Modeled on `AsyncQueue` from `@endo/stream`, but with arrays under the hood instead of a promise chain.
*/
readonly #buffer = makeStreamBuffer<IteratorResult<Read, undefined>>();
readonly #name: string;
readonly #validateInput?: ValidateInput<Read> | undefined;
#onEnd?: OnEnd | undefined;
#didExposeReceiveInput: boolean = false;
/**
* Constructs a {@link BaseReader}.
*
* @param options - Options bag for configuring the reader.
* @param options.name - The name of the stream, for logging purposes. Defaults to the class name.
* @param options.onEnd - A function that is called when the stream ends. For any cleanup that
* should happen when the stream ends, such as closing a message port.
* @param options.validateInput - A function that validates input from the transport.
*/
constructor({ name, onEnd, validateInput }: BaseReaderArgs<Read>) {
this.#name = name ?? this.constructor.name;
this.#onEnd = onEnd;
this.#validateInput = validateInput;
harden(this);
}
/**
* Returns the `receiveInput()` method, which is used to receive input from the stream.
* Attempting to call this method more than once will throw an error.
*
* @returns The `receiveInput()` method.
*/
protected getReceiveInput(): ReceiveInput {
if (this.#didExposeReceiveInput) {
throw new Error(
`${this.#name} received multiple calls to getReceiveInput()`,
);
}
this.#didExposeReceiveInput = true;
return this.#receiveInput.bind(this);
}
readonly #receiveInput = async (input: unknown): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/await-thenable
await null;
const unmarshaled = unmarshal(input);
if (unmarshaled instanceof Error) {
await this.#handleInputError(unmarshaled);
return;
}
if (unmarshaled === StreamDoneSymbol) {
await this.#end();
return;
}
if (this.#validateInput?.(unmarshaled) === false) {
await this.#handleInputError(
new Error(
`${this.#name}: Message failed type validation:\n${stringify(unmarshaled)}`,
),
);
return;
}
this.#buffer.put(makePendingResult(unmarshaled));
};
/**
* Handles an input error by putting it into the buffer and ending the stream.
*
* @param error - The error to handle.
*/
async #handleInputError(error: Error): Promise<void> {
if (!this.#buffer.hasPendingReads()) {
this.#buffer.put(error);
}
await this.#end(error);
}
/**
* Ends the stream. Calls and then unsets the `#onEnd` method.
* Idempotent.
*
* @param error - The error to end the stream with. A `done` result is used if not provided.
*/
async #end(error?: Error): Promise<void> {
this.#buffer.end(error);
const onEndP = this.#onEnd?.(error);
this.#onEnd = undefined;
await onEndP;
}
/**
* Returns the async iterator for this stream.
*
* @returns This stream as an async iterator.
*/
[Symbol.asyncIterator](): typeof this {
return this;
}
/**
* Reads the next message from the transport.
*
* @returns The next message from the transport.
*/
async next(): Promise<IteratorResult<Read, undefined>> {
return this.#buffer.get();
}
/**
* Closes the underlying transport and returns. Any unread messages will be lost.
*
* @returns The final result for this stream.
*/
async return(): Promise<IteratorResult<Read, undefined>> {
await this.#end();
return makeDoneResult();
}
/**
* Rejects all pending reads with the specified error, closes the underlying transport,
* and returns.
*
* @param error - The error to reject pending reads with.
* @returns The final result for this stream.
*/
async throw(error: Error): Promise<IteratorResult<Read, undefined>> {
await this.#end(error);
return makeDoneResult();
}
/**
* Closes the stream. Syntactic sugar for `return()` or `throw(error)`. 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>> {
return error ? this.throw(error) : this.return();
}
}
harden(BaseReader);
export type Dispatch<Yield> = (
value: Dispatchable<Yield>,
) => void | Promise<void>;
export type BaseWriterArgs<Write> = {
onDispatch: Dispatch<Write>;
name?: string | undefined;
onEnd?: OnEnd | undefined;
};
/**
* The base of a writable async iterator stream.
*/
export class BaseWriter<Write> implements Writer<Write> {
#isDone: boolean = false;
readonly #name: string = 'BaseWriter';
readonly #onDispatch: Dispatch<Write>;
#onEnd?: OnEnd | undefined;
/**
* Constructs a {@link BaseWriter}.
*
* @param options - Options bag for configuring the writer.
* @param options.onDispatch - A function that dispatches messages over the underlying transport mechanism.
* @param options.name - The name of the stream, for logging purposes. Defaults to the class name.
* @param options.onEnd - A function that is called when the stream ends. For any cleanup that
* should happen when the stream ends, such as closing a message port.
*/
constructor({ name, onDispatch, onEnd }: BaseWriterArgs<Write>) {
this.#name = name ?? this.constructor.name;
this.#onDispatch = onDispatch;
this.#onEnd = onEnd;
harden(this);
}
/**
* Dispatches the value, via the dispatch function registered in the constructor.
* If dispatching fails, calls `#throw()`, and is therefore mutually recursive with
* that method. For this reason, includes a flag indicating past failure to dispatch
* a value, which is used to avoid infinite recursion. If dispatching succeeds, returns a
* `{ done: true }` result if the value was an {@link Error} or itself a `done` result,
* otherwise returns `{ done: false }`.
*
* @param value - The value to dispatch.
* @param hasFailed - Whether dispatching has failed previously.
* @returns The result of dispatching the value.
*/
async #dispatch(
value: Writable<Write>,
hasFailed = false,
): Promise<IteratorResult<undefined, undefined>> {
try {
await this.#onDispatch(marshal(value));
return value === StreamDoneSymbol || value instanceof Error
? makeDoneResult()
: makePendingResult(undefined);
} catch (error) {
if (hasFailed) {
// Break out of repeated failure to dispatch an error. It is unclear how this would occur
// in practice, but it's the kind of failure mode where it's better to be sure.
const repeatedFailureError = new Error(
`${this.#name} experienced repeated dispatch failures.`,
{ cause: error },
);
await this.#onDispatch(makeStreamErrorSignal(repeatedFailureError));
throw repeatedFailureError;
} else {
await this.#throw(
/* istanbul ignore next: The ternary is mostly to please TypeScript */
error instanceof Error ? error : new Error(String(error)),
true,
);
throw new Error(`${this.#name} experienced a dispatch failure`, {
cause: error,
});
}
}
}
/**
* Ends the stream and calls the onEnd callback. Idempotent.
*
* @param error - The error to end the stream with.
*/
async #end(error?: Error): Promise<void> {
this.#isDone = true;
const onEndP = this.#onEnd?.(error);
this.#onEnd = undefined;
await onEndP;
}
/**
* Returns the async iterator for this stream.
*
* @returns This stream as an async iterator.
*/
[Symbol.asyncIterator](): typeof this {
return this;
}
/**
* Writes the next message to the transport.
*
* @param value - The next message to write to the transport.
* @returns The result of writing the message.
*/
async next(value: Write): Promise<IteratorResult<undefined, undefined>> {
if (this.#isDone) {
return makeDoneResult();
}
return this.#dispatch(value);
}
/**
* Closes the underlying transport and returns. Idempotent.
*
* @returns The final result for this stream.
*/
async return(): Promise<IteratorResult<undefined, undefined>> {
if (!this.#isDone) {
await this.#onDispatch(makeStreamDoneSignal());
await this.#end();
}
return makeDoneResult();
}
/**
* Forwards the error to the transport and closes this stream. Idempotent.
*
* @param error - The error to forward to the transport.
* @returns The final result for this stream.
*/
async throw(error: Error): Promise<IteratorResult<undefined, undefined>> {
if (!this.#isDone) {
await this.#throw(error);
}
return makeDoneResult();
}
/**
* Closes the stream. Syntactic sugar for `return()` or `throw(error)`. Idempotent.
*
* @param error - The error to close the stream with.
* @returns The final result for this stream.
*/
async end(error?: Error): Promise<IteratorResult<undefined, undefined>> {
return error ? this.throw(error) : this.return();
}
/**
* Dispatches the error and calls `#end()`. Mutually recursive with `dispatch()`.
* For this reason, includes a flag indicating past failure, so that `dispatch()`
* can avoid infinite recursion. See `dispatch()` for more details.
*
* @param error - The error to forward.
* @param hasFailed - Whether dispatching has failed previously.
* @returns The final result for this stream.
*/
async #throw(
error: Error,
hasFailed = false,
): Promise<IteratorResult<undefined, undefined>> {
const result = this.#dispatch(error, hasFailed);
if (!this.#isDone) {
await this.#end(error);
}
return result;
}
}
harden(BaseWriter);
|