All files / streams/src utils.ts

100% Statements 29/29
100% Branches 20/20
100% Functions 8/8
100% Lines 24/24

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                                    71x         71x   71x       71x                     3736x 215x       71x         119x                                                 1160x 1x   1159x 26x         1133x                   3877x 24x 17x   7x 6x   1x   3853x                 71x 1023x                       71x     4986x        
import type { Reader, Writer } from '@endo/stream';
import {
  isMarshaledError,
  marshalError,
  unmarshalError,
} from '@metamask/kernel-errors';
import { stringify } from '@metamask/kernel-utils';
import type { Infer } from '@metamask/superstruct';
import { is, literal } from '@metamask/superstruct';
import {
  hasProperty,
  isObject,
  object,
  UnsafeJsonStruct,
} from '@metamask/utils';
 
export type { Reader, Writer };
 
export const StreamSentinel = {
  Error: '@@StreamError',
  Done: '@@StreamDone',
} as const;
 
export const StreamDoneSymbol = Symbol('StreamDone');
 
const StreamDoneStruct = object({
  [StreamSentinel.Done]: literal(true),
});
 
const StreamErrorStruct = object({
  [StreamSentinel.Error]: literal(true),
  error: UnsafeJsonStruct,
});
 
type StreamDone = Infer<typeof StreamDoneStruct>;
 
type StreamError = Infer<typeof StreamErrorStruct>;
 
export type StreamSignal = StreamError | StreamDone;
 
export const isSignalLike = (value: unknown): value is StreamSignal =>
  isObject(value) &&
  (hasProperty(value, StreamSentinel.Error) ||
    hasProperty(value, StreamSentinel.Done));
 
export const makeStreamErrorSignal = (error: Error): StreamError => ({
  [StreamSentinel.Error]: true,
  error: marshalError(error),
});
 
export const makeStreamDoneSignal = (): StreamDone => ({
  [StreamSentinel.Done]: true,
});
 
/**
 * A value that can be written to a stream.
 *
 * @template Yield - The type of the values yielded by the iterator.
 */
export type Writable<Yield> = Yield | Error | typeof StreamDoneSymbol;
 
/**
 * A value that can be dispatched to the internal transport mechanism of a stream.
 *
 * @template Yield - The type of the values yielded by the stream.
 */
export type Dispatchable<Yield> = Yield | StreamSignal;
 
/**
 * Marshals a {@link Writable} into a {@link Dispatchable}.
 *
 * @param value - The value to marshal.
 * @returns The marshaled value.
 */
export function marshal<Yield>(value: Writable<Yield>): Dispatchable<Yield> {
  if (value === StreamDoneSymbol) {
    return { [StreamSentinel.Done]: true };
  }
  if (value instanceof Error) {
    return {
      [StreamSentinel.Error]: true,
      error: marshalError(value),
    };
  }
  return value;
}
 
/**
 * Unmarshals a {@link Dispatchable} into a {@link Writable}.
 *
 * @param value - The value to unmarshal.
 * @returns The unmarshaled value.
 */
export function unmarshal<Yield>(value: Dispatchable<Yield>): Writable<Yield> {
  if (isSignalLike(value)) {
    if (is(value, StreamDoneStruct)) {
      return StreamDoneSymbol;
    }
    if (is(value, StreamErrorStruct) && isMarshaledError(value.error)) {
      return unmarshalError(value.error);
    }
    throw new Error(`Invalid stream signal: ${stringify(value)}`);
  }
  return value;
}
 
/**
 * Creates a {@link IteratorResult} with `{ done: true, value: undefined }`.
 *
 * @template Yield - The type of the values yielded by the iterator.
 * @returns A {@link IteratorResult} with `{ done: true, value: undefined }`.
 */
export const makeDoneResult = <Yield>(): IteratorResult<Yield, undefined> =>
  harden({
    done: true,
    value: undefined,
  });
 
/**
 * Creates a {@link IteratorResult} with `{ done: false, value }`.
 *
 * @template Yield - The type of the values yielded by the iterator.
 * @param value - The value of the iterator result.
 * @returns A {@link IteratorResult} with `{ done: false, value }`.
 */
export const makePendingResult = <Yield>(
  value: Yield,
): IteratorResult<Yield, undefined> =>
  harden({
    done: false,
    value,
  });