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