osra 0.4.1 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/connections/index.d.ts +1 -0
- package/build/connections/relay.d.ts +11 -0
- package/build/index.js +338 -325
- package/build/index.js.map +1 -1
- package/package.json +1 -1
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/types.ts","../src/utils/type-guards.ts","../src/utils/transport.ts","../src/revivables/utils.ts","../src/revivables/array-buffer.ts","../src/revivables/date.ts","../src/revivables/headers.ts","../src/revivables/error.ts","../src/revivables/typed-array.ts","../src/utils/event-channel.ts","../src/utils/gc-tracker.ts","../src/revivables/message-port.ts","../src/revivables/promise.ts","../src/revivables/function.ts","../src/revivables/readable-stream.ts","../src/revivables/writable-stream.ts","../src/revivables/abort-signal.ts","../src/revivables/response.ts","../src/revivables/request.ts","../src/revivables/identity.ts","../src/revivables/transfer.ts","../src/revivables/map.ts","../src/revivables/set.ts","../src/revivables/bigint.ts","../src/revivables/event.ts","../src/revivables/event-target.ts","../src/revivables/blob.ts","../src/revivables/symbol.ts","../src/revivables/fallbacks.ts","../src/revivables/index.ts","../src/connections/bidirectional.ts","../src/utils/typed-event-target.ts","../src/utils/transferable.ts","../src/connections/utils.ts","../src/connections/index.ts","../src/index.ts"],"sourcesContent":["import type { ConnectionMessage } from './connections'\nimport type { TypedEventTarget } from './utils'\nimport type { IsJsonOnlyTransport } from './utils/type-guards'\nimport type {\n DefaultRevivableModules, RevivableModule,\n InferMessages, InferRevivables, RevivableContext\n} from './revivables'\n\nexport const OSRA_KEY = '__OSRA_KEY__' as const\nexport const OSRA_DEFAULT_KEY = '__OSRA_DEFAULT_KEY__' as const\nexport const OSRA_BOX = '__OSRA_BOX__' as const\n\nexport type Uuid = `${string}-${string}-${string}-${string}-${string}`\n\nexport type Jsonable =\n | boolean\n | null\n | number\n | string\n | { [key: string]: Jsonable }\n | Array<Jsonable>\n\nexport type Structurable =\n | Jsonable\n /** not really structureable but here for convenience */\n | void\n | undefined\n | bigint\n | Date\n | RegExp\n | Blob\n | File\n | FileList\n | ArrayBuffer\n | ArrayBufferView\n | ImageBitmap\n | ImageData\n | { [key: string]: Structurable }\n | Array<Structurable>\n | Map<Structurable, Structurable>\n | Set<Structurable>\n\nexport type StructurableTransferable =\n | Structurable\n | Transferable\n | { [key: string]: StructurableTransferable }\n | Array<StructurableTransferable>\n | Map<StructurableTransferable, StructurableTransferable>\n | Set<StructurableTransferable>\n\n/** \"Free\" types in `Capable` — narrows to `Jsonable` on JSON transports so\n * user code can't type a `Date`/`Blob`/etc. that JSON would silently coerce.\n * Modules that DO support JSON (date, map, set, bigint, …) put their type\n * back via `InferRevivables`. */\ntype CapableBase<Ctx extends RevivableContext> =\n IsJsonOnlyTransport<Ctx['transport']> extends true\n ? Jsonable | undefined | void\n : StructurableTransferable\n\nexport type Capable<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n Ctx extends RevivableContext = RevivableContext,\n> =\n | CapableBase<Ctx>\n | InferRevivables<TModules, Ctx>\n | { [key: string]: Capable<TModules, Ctx> }\n | Array<Capable<TModules, Ctx>>\n | Map<Capable<TModules, Ctx>, Capable<TModules, Ctx>>\n | Set<Capable<TModules, Ctx>>\n\nexport type MessageFields = {\n type: string\n remoteUuid: Uuid\n}\n\nexport type MessageBase = {\n [OSRA_KEY]: string\n /** UUID of the client that sent the message */\n uuid: Uuid\n name?: string\n}\n\nexport type ProtocolMessage =\n | { type: 'announce', remoteUuid?: Uuid }\n | { type: 'close', remoteUuid: Uuid }\n\nexport type MessageVariant<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n | ProtocolMessage\n | ConnectionMessage<TModules>\n | InferMessages<TModules>\n\nexport type Message<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n & MessageBase\n & MessageVariant<TModules>\n\nexport type MessageEventMap<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n message: CustomEvent<Message<TModules>>\n}\n\nexport type MessageEventTarget<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n > = TypedEventTarget<MessageEventMap<TModules>>\n","import type { Runtime } from 'webextension-polyfill'\nimport type { Message } from '../types'\nimport type {\n CustomEmitTransport, CustomReceiveTransport,\n CustomTransport, EmitJsonPlatformTransport,\n EmitTransport, JsonPlatformTransport,\n ReceiveJsonPlatformTransport,\n ReceiveTransport, Transport\n} from './transport'\n\nimport { OSRA_KEY } from '../types'\nimport { getWebExtensionRuntime } from './transport'\n\n// Pulled from globalThis so module evaluation doesn't crash on platforms\n// that haven't shipped Float16Array yet (Node ≤ 23, Chrome ≤ 134, Firefox\n// ≤ 132). Platforms without it just don't round-trip Float16 values.\nconst Float16ArrayCtor = (globalThis as { Float16Array?: typeof Float16Array }).Float16Array\n\nconst typedArrayConstructorsByName = {\n Int8Array,\n Uint8Array,\n Uint8ClampedArray,\n Int16Array,\n Uint16Array,\n Int32Array,\n Uint32Array,\n Float16Array: Float16ArrayCtor,\n Float32Array,\n Float64Array,\n BigInt64Array,\n BigUint64Array,\n} as const\n\nexport type TypedArrayType = keyof typeof typedArrayConstructorsByName\nexport type TypedArrayConstructor = NonNullable<(typeof typedArrayConstructorsByName)[TypedArrayType]>\nexport type TypedArray = InstanceType<TypedArrayConstructor>\n\nconst typedArrayConstructors = Object.values(typedArrayConstructorsByName)\n\nexport const typedArrayToType = (value: TypedArray): TypedArrayType => {\n const name = value.constructor.name as TypedArrayType\n if (name in typedArrayConstructorsByName) return name\n // Subclasses (e.g. Node's Buffer extends Uint8Array). Find the nearest\n // TypedArray ancestor by walking the prototype chain.\n for (const [ancestorName, ctor] of Object.entries(typedArrayConstructorsByName)) {\n if (ctor && value instanceof ctor) return ancestorName as TypedArrayType\n }\n throw new Error('Unknown typed array type')\n}\n\nexport const typedArrayTypeToTypedArrayConstructor = (value: TypedArrayType): TypedArrayConstructor => {\n const ctor = typedArrayConstructorsByName[value]\n if (!ctor) throw new Error('Unknown typed array type')\n return ctor\n}\n\nexport const isTypedArray = (value: unknown): value is TypedArray =>\n typedArrayConstructors.some(ctor => !!ctor && value instanceof ctor)\nexport const isWebSocket = (value: unknown): value is WebSocket => value instanceof WebSocket\nexport const isServiceWorkerContainer = (value: unknown): value is ServiceWorkerContainer => !!globalThis.ServiceWorkerContainer && value instanceof ServiceWorkerContainer\nexport const isWorker = (value: unknown): value is Worker => !!globalThis.Worker && value instanceof Worker\n// @ts-expect-error DedicatedWorkerGlobalScope is only present in worker scopes\nexport const isDedicatedWorker = (value: unknown): value is DedicatedWorkerGlobalScope => !!globalThis.DedicatedWorkerGlobalScope && value instanceof DedicatedWorkerGlobalScope\nexport const isSharedWorker = (value: unknown): value is SharedWorker => !!globalThis.SharedWorker && value instanceof SharedWorker\nconst isMessagePort = (value: unknown): value is MessagePort => value instanceof MessagePort\n\nexport const isOsraMessage = (value: unknown): value is Message =>\n !!value\n && typeof value === 'object'\n && OSRA_KEY in value\n && !!value[OSRA_KEY]\n\ntype AnyConstructor = abstract new (...args: any[]) => unknown\n\n/** True if `value` is an instance of any of the given constructors.\n * Tolerates undefined entries (constructors missing on this platform). */\nexport const instanceOfAny = (value: unknown, ctors: readonly (AnyConstructor | undefined)[]): boolean => {\n for (const ctor of ctors) if (ctor && value instanceof ctor) return true\n return false\n}\n\nexport const isClonable = (value: unknown): boolean =>\n instanceOfAny(value, [globalThis.SharedArrayBuffer])\n\n// Types eligible for transfer when the user opts in via `transfer()`. Some\n// entries are also clonable (ArrayBuffer, ImageBitmap, …) — outside a\n// `transfer` box they fall back to clone.\nexport const isTransferable = (value: unknown): value is Transferable =>\n instanceOfAny(value, [\n globalThis.ArrayBuffer,\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n (globalThis as { AudioData?: abstract new (...args: any[]) => unknown }).AudioData,\n (globalThis as { VideoFrame?: abstract new (...args: any[]) => unknown }).VideoFrame,\n (globalThis as { MediaSourceHandle?: abstract new (...args: any[]) => unknown }).MediaSourceHandle,\n (globalThis as { MediaStreamTrack?: abstract new (...args: any[]) => unknown }).MediaStreamTrack,\n (globalThis as { MIDIAccess?: abstract new (...args: any[]) => unknown }).MIDIAccess,\n (globalThis as { RTCDataChannel?: abstract new (...args: any[]) => unknown }).RTCDataChannel,\n (globalThis as { WebTransportReceiveStream?: abstract new (...args: any[]) => unknown }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: abstract new (...args: any[]) => unknown }).WebTransportSendStream,\n ])\n\nexport type WebExtRuntime = typeof browser.runtime\nexport const isWebExtensionRuntime = (value: unknown): value is WebExtRuntime => {\n const runtime = getWebExtensionRuntime()\n if (!runtime) return false\n return value === runtime\n}\n\nexport type WebExtPort = ReturnType<WebExtRuntime['connect']> | Runtime.Port\nexport const isWebExtensionPort = (value: unknown, connectPort: boolean = false): value is WebExtPort => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('name' in value) || !('disconnect' in value) || !('postMessage' in value)) return false\n // these properties are only present on WebExtPorts created through runtime.connect()\n if (!connectPort) return true\n return 'sender' in value && 'onMessage' in value && 'onDisconnect' in value\n}\n\nexport type WebExtSender = NonNullable<WebExtPort['sender']>\n\n// Structural guard. Can't distinguish onConnect from onMessage on its own —\n// they share this exact shape.\nconst hasListenerApi = (value: unknown): boolean =>\n !!value\n && typeof value === 'object'\n && !isWindow(value)\n && 'addListener' in value\n && 'hasListener' in value\n && 'removeListener' in value\n\n// Identity-compare against runtime.onConnect — structural checks can't\n// distinguish onConnect from onMessage, and misclassifying causes us to\n// treat each incoming message as a port and crash.\nexport type WebExtOnConnect = WebExtRuntime['onConnect']\nexport const isWebExtensionOnConnect = (value: unknown): value is WebExtOnConnect => {\n const runtime = getWebExtensionRuntime()\n if (!runtime) return false\n return value === runtime.onConnect || value === runtime.onConnectExternal\n}\n\nexport type WebExtOnMessage = WebExtRuntime['onMessage']\nexport const isWebExtensionOnMessage = (value: unknown): value is WebExtOnMessage =>\n hasListenerApi(value)\n\nexport const isWindow = (value: unknown): value is Window => {\n if (!value || typeof value !== 'object') return false\n try {\n return 'window' in value && value.window === value\n } catch {\n // Cross-origin Window access can throw SecurityError — fall back to a\n // shape probe over properties that don't trigger the security check.\n try {\n return 'closed' in value\n && typeof value.closed === 'boolean'\n && 'close' in value\n && typeof value.close === 'function'\n } catch {\n return false\n }\n }\n}\n\nexport const isEmitJsonOnlyTransport = (value: unknown): value is EmitJsonPlatformTransport =>\n isWebSocket(value)\n || isWebExtensionPort(value)\n || isWebExtensionRuntime(value)\n\nexport const isReceiveJsonOnlyTransport = (value: unknown): value is ReceiveJsonPlatformTransport =>\n isWebSocket(value)\n || isWebExtensionPort(value)\n || isWebExtensionOnConnect(value)\n || isWebExtensionOnMessage(value)\n || isWebExtensionRuntime(value)\n\nexport type IsJsonOnlyTransport<T extends Transport> = T extends JsonPlatformTransport ? true : false\nexport const isJsonOnlyTransport = (value: unknown): value is Extract<Transport, JsonPlatformTransport> =>\n (!!value && typeof value === 'object' && 'isJson' in value && value.isJson === true)\n || isEmitJsonOnlyTransport(value)\n || isReceiveJsonOnlyTransport(value)\n\nexport const isEmitTransport = (value: unknown): value is EmitTransport =>\n isWindow(value)\n || isEmitJsonOnlyTransport(value)\n || isServiceWorkerContainer(value)\n || isWorker(value)\n || isDedicatedWorker(value)\n || isSharedWorker(value)\n || isMessagePort(value)\n || isCustomEmitTransport(value)\n\nexport function assertEmitTransport(transport: Transport): asserts transport is EmitTransport {\n if (!isEmitTransport(transport)) throw new Error('Transport is not emitable')\n}\n\nexport const isReceiveTransport = (value: unknown): value is ReceiveTransport =>\n isWindow(value)\n || isReceiveJsonOnlyTransport(value)\n || isServiceWorkerContainer(value)\n || isWorker(value)\n || isDedicatedWorker(value)\n || isSharedWorker(value)\n || isMessagePort(value)\n || isCustomReceiveTransport(value)\n\nexport function assertReceiveTransport(transport: Transport): asserts transport is ReceiveTransport {\n if (!isReceiveTransport(transport)) throw new Error('Transport is not receiveable')\n}\n\nexport const isCustomEmitTransport = (value: unknown): value is CustomEmitTransport => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('emit' in value)) return false\n return isEmitTransport(value.emit) || typeof value.emit === 'function'\n}\n\nexport const isCustomReceiveTransport = (value: unknown): value is CustomReceiveTransport => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('receive' in value)) return false\n return isReceiveTransport(value.receive) || typeof value.receive === 'function'\n}\n\nexport const isCustomTransport = (value: unknown): value is CustomTransport =>\n isCustomEmitTransport(value)\n || isCustomReceiveTransport(value)\n\nexport const isTransport = (value: unknown): value is Transport =>\n isEmitTransport(value)\n || isReceiveTransport(value)\n || isCustomTransport(value)\n || isJsonOnlyTransport(value)\n","import type { Message} from '../types'\nimport type {\n WebExtOnConnect, WebExtOnMessage,\n WebExtPort, WebExtRuntime, WebExtSender\n} from './type-guards'\n\nimport { OSRA_KEY } from '../types'\nimport {\n isOsraMessage, isCustomTransport,\n isWebExtensionOnConnect, isWebExtensionOnMessage,\n isWebExtensionPort, isWebExtensionRuntime, isWebSocket, isWindow, isSharedWorker\n} from './type-guards'\n\nexport type MessageContext = {\n port?: MessagePort | WebExtPort // WebExtension\n sender?: WebExtSender // WebExtension\n receiveTransport?: ReceivePlatformTransport\n source?: MessageEventSource | null // Window, Worker, WebSocket, ect...\n}\n\nexport type ReceiveHandler = (listener: (event: Message, messageContext: MessageContext) => void) => void\nexport type EmitHandler = (message: Message, transferables?: Transferable[]) => void\n\ntype CustomReceive = ReceivePlatformTransport | ReceiveHandler\ntype CustomEmit = EmitPlatformTransport | EmitHandler\n\nexport type CustomTransport =\n { isJson?: boolean }\n & (\n | { receive: CustomReceive, emit: CustomEmit }\n | { receive: CustomReceive }\n | { emit: CustomEmit }\n )\n\nexport type CustomEmitTransport = Extract<CustomTransport, { emit: any }>\nexport type CustomReceiveTransport = Extract<CustomTransport, { receive: any }>\n\nexport type EmitJsonPlatformTransport =\n | WebSocket\n | WebExtPort\n | WebExtRuntime\n\nexport type ReceiveJsonPlatformTransport =\n | WebSocket\n | WebExtPort\n | WebExtOnConnect\n | WebExtOnMessage\n | WebExtRuntime\n\nexport type JsonPlatformTransport =\n | { isJson: true }\n | EmitJsonPlatformTransport\n | ReceiveJsonPlatformTransport\n\ntype StructuredClonePlatformTransport =\n | Window\n | ServiceWorker\n | Worker\n | SharedWorker\n | MessagePort\n\nexport type EmitPlatformTransport =\n | EmitJsonPlatformTransport\n | StructuredClonePlatformTransport\n\nexport type ReceivePlatformTransport =\n | ReceiveJsonPlatformTransport\n | StructuredClonePlatformTransport\n\nexport type PlatformTransport =\n | EmitPlatformTransport\n | ReceivePlatformTransport\n\nexport type EmitTransport = EmitPlatformTransport & Extract<CustomTransport, { emit: any }>\nexport type ReceiveTransport = ReceivePlatformTransport & Extract<CustomTransport, { receive: any }>\n\nexport type Transport =\n | PlatformTransport\n | CustomTransport\n\nexport const getWebExtensionGlobal = () => globalThis.browser ?? globalThis.chrome\nexport const getWebExtensionRuntime = () => getWebExtensionGlobal()?.runtime\n\nexport const checkOsraMessageKey = (message: any, key: string): message is Message =>\n isOsraMessage(message)\n && message[OSRA_KEY] === key\n\nconst onAbort = (signal: AbortSignal | undefined, fn: () => void) =>\n signal?.addEventListener('abort', fn, { once: true })\n\nexport const registerOsraMessageListener = (\n { listener, transport, remoteName, key = OSRA_KEY, unregisterSignal }:\n {\n listener: (message: Message, messageContext: MessageContext) => void\n transport: ReceiveTransport\n remoteName?: string\n key?: string\n unregisterSignal?: AbortSignal\n }\n) => {\n const receiveTransport: Extract<CustomTransport, { receive: any }>['receive'] =\n isCustomTransport(transport) ? transport.receive : transport\n\n // Custom function handler\n if (typeof receiveTransport === 'function') {\n receiveTransport((message, ctx) => {\n if (!checkOsraMessageKey(message, key)) return\n if (remoteName && message.name !== remoteName) return\n listener(message, ctx)\n })\n return\n }\n\n // WebExtension family — subscribe to an `onMessage`-style listener.\n if (\n isWebExtensionRuntime(receiveTransport)\n || isWebExtensionPort(receiveTransport)\n || isWebExtensionOnConnect(receiveTransport)\n || isWebExtensionOnMessage(receiveTransport)\n ) {\n const listenOnWebExtOnMessage = (onMessage: WebExtOnMessage, port?: WebExtPort) => {\n const _listener = (message: object, sender?: WebExtSender) => {\n if (!checkOsraMessageKey(message, key)) return\n if (remoteName && message.name !== remoteName) return\n listener(message, { port, sender })\n }\n onMessage.addListener(_listener)\n onAbort(unregisterSignal, () => onMessage.removeListener(_listener))\n }\n\n if (isWebExtensionRuntime(receiveTransport)) {\n listenOnWebExtOnMessage(receiveTransport.onMessage)\n } else if (isWebExtensionOnConnect(receiveTransport)) {\n const _listener = (port: WebExtPort) =>\n listenOnWebExtOnMessage(port.onMessage as WebExtOnMessage, port)\n receiveTransport.addListener(_listener)\n onAbort(unregisterSignal, () => receiveTransport.removeListener(_listener))\n } else if (isWebExtensionOnMessage(receiveTransport)) {\n listenOnWebExtOnMessage(receiveTransport)\n } else { // WebExtPort\n listenOnWebExtOnMessage(receiveTransport.onMessage as WebExtOnMessage)\n }\n return\n }\n\n // Window, Worker, WebSocket, ServiceWorker, MessagePort, …\n const messageListener = (event: MessageEvent<Message>) => {\n if (!checkOsraMessageKey(event.data, key)) return\n if (remoteName && event.data.name !== remoteName) return\n listener(event.data, { receiveTransport, source: event.source })\n }\n receiveTransport.addEventListener('message', messageListener as EventListener)\n onAbort(unregisterSignal, () =>\n receiveTransport.removeEventListener('message', messageListener as EventListener),\n )\n}\n\nexport const sendOsraMessage = (\n transport: EmitTransport,\n message: Message,\n origin = '*',\n transferables: Transferable[] = []\n) => {\n const emitTransport: Extract<EmitTransport, { emit: any }>['emit'] =\n isCustomTransport(transport) ? transport.emit : transport\n\n if (typeof emitTransport === 'function') {\n emitTransport(message, transferables)\n } else if (isWindow(emitTransport)) {\n // Must check first — cross-origin windows throw on other property access.\n emitTransport.postMessage(message, origin, transferables)\n } else if (isWebExtensionPort(emitTransport)) {\n emitTransport.postMessage(message)\n } else if (isWebExtensionRuntime(emitTransport)) {\n emitTransport.sendMessage(message)\n } else if (isWebSocket(emitTransport)) {\n emitTransport.send(JSON.stringify(message))\n } else if (isSharedWorker(emitTransport)) {\n emitTransport.port.postMessage(message, transferables)\n } else { // MessagePort | ServiceWorker | Worker\n emitTransport.postMessage(message, transferables)\n }\n}\n","import type { DefaultRevivableModules, RevivableModule } from '.'\nimport type {\n MessageEventTarget,\n MessageFields,\n Uuid,\n} from '../types'\nimport type { Transport } from '../utils/transport'\nimport type { IsJsonOnlyTransport } from '../utils/type-guards'\n\nimport { OSRA_BOX } from '../types'\nimport { isJsonOnlyTransport } from '../utils/type-guards'\n\nexport type { UnderlyingType } from '../utils/type'\n\nexport const BoxBase = {\n [OSRA_BOX]: 'revivable',\n} as const\n\nexport type BoxBase<T extends string = string> =\n & typeof BoxBase\n & { type: T }\n\nexport type RevivableContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n remoteUuid: Uuid\n unregisterSignal?: AbortSignal\n /** Typed as a broad dispatcher so revivables can post their own message\n * variants without triggering contravariant function-parameter mismatches\n * across modules. The shape is enforced structurally via `MessageFields`. */\n sendMessage: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n eventTarget: MessageEventTarget<TModules>\n}\n\n/** Extract the type a module's `isType` narrows to. Modules marked\n * `capableOnly: true` (clonable, transferable) contribute `never` on JSON\n * transports so users can't type values JSON would silently drop. */\nexport type ExtractType<T, Ctx extends RevivableContext = RevivableContext> =\n T extends { capableOnly: true }\n ? IsJsonOnlyTransport<Ctx['transport']> extends true\n ? never\n : T extends { isType: (value: unknown) => value is infer S } ? S : never\n : T extends { isType: (value: unknown) => value is infer S } ? S : never\n\nexport type ExtractBox<T> =\n T extends { box: (...args: any[]) => infer B }\n ? B\n : never\n\nexport type ExtractMessages<T> =\n T extends { Messages?: infer B }\n ? B extends { type: string }\n ? string extends B['type'] ? never : B\n : never\n : never\n\nexport type InferMessages<TModules extends readonly unknown[]> =\n ExtractMessages<TModules[number]>\n\nexport type InferRevivables<\n TModules extends readonly unknown[],\n Ctx extends RevivableContext = RevivableContext,\n> =\n ExtractType<TModules[number], Ctx>\n\nexport type InferRevivableBox<TModules extends readonly unknown[]> =\n ExtractBox<TModules[number]>\n\nexport const isRevivableBox = (value: unknown): value is BoxBase =>\n !!value\n && typeof value === 'object'\n && OSRA_BOX in value\n && value[OSRA_BOX] === 'revivable'\n\nexport const serializeError = (error: unknown): string =>\n error instanceof Error ? (error.stack ?? String(error)) : String(error)\n\n/** Wire shape for an ArrayBuffer: base64 on JSON, raw on clone. */\nexport type BoxedBuffer<TCtx extends RevivableContext = RevivableContext> =\n IsJsonOnlyTransport<TCtx['transport']> extends true ? { base64Buffer: string }\n : IsJsonOnlyTransport<TCtx['transport']> extends false ? { arrayBuffer: ArrayBuffer }\n : { base64Buffer: string } | { arrayBuffer: ArrayBuffer }\n\nexport const boxBuffer = <TCtx extends RevivableContext>(\n buffer: ArrayBuffer,\n context: TCtx,\n): BoxedBuffer<TCtx> =>\n (isJsonOnlyTransport(context.transport)\n ? { base64Buffer: new Uint8Array(buffer).toBase64() }\n : { arrayBuffer: buffer }\n ) as BoxedBuffer<TCtx>\n\nexport const reviveBuffer = (boxed: { arrayBuffer: ArrayBuffer } | { base64Buffer: string }): ArrayBuffer =>\n 'arrayBuffer' in boxed\n ? boxed.arrayBuffer\n : Uint8Array.fromBase64(boxed.base64Buffer).buffer\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase, boxBuffer, reviveBuffer } from './utils'\n\nexport const type = 'arrayBuffer' as const\n\nexport const isType = (value: unknown): value is ArrayBuffer =>\n value instanceof ArrayBuffer\n\nexport const box = <T extends ArrayBuffer, T2 extends RevivableContext>(\n value: T,\n context: T2,\n) => ({\n ...BoxBase,\n type,\n ...boxBuffer(value, context),\n})\n\nexport const revive = <T extends ReturnType<typeof box>>(\n value: T,\n _context: RevivableContext,\n) => reviveBuffer(value)\n\nconst typeCheck = () => {\n const boxed = box(new ArrayBuffer(10), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ArrayBuffer = revived\n // @ts-expect-error - not an ArrayBuffer\n const notArrayBuffer: string = revived\n // @ts-expect-error - cannot box non-ArrayBuffer\n box('not an array buffer', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'date' as const\n\nexport const isType = (value: unknown): value is Date =>\n value instanceof Date\n\nexport const box = <T extends Date, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => ({\n ...BoxBase,\n type,\n ISOString: value.toISOString()\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => new Date(value.ISOString)\n\nconst typeCheck = () => {\n const boxed = box(new Date(), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Date = revived\n // @ts-expect-error - not a Date\n const notDate: string = revived\n // @ts-expect-error - cannot box non-Date\n box('not a date', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'headers' as const\n\nexport const isType = (value: unknown): value is Headers =>\n value instanceof Headers\n\nexport const box = <T extends Headers, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => ({\n ...BoxBase,\n type,\n entries: [...value.entries()]\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n _context: T2\n): Headers => {\n return new Headers(value.entries)\n}\n\nconst typeCheck = () => {\n const boxed = box(new Headers(), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Headers = revived\n // @ts-expect-error - not a Headers\n const notHeaders: string = revived\n // @ts-expect-error - cannot box non-Headers\n box('not a header', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'error' as const\n\nexport type BoxedError =\n & BoxBaseType<typeof type>\n & {\n name: string\n message: string\n stack: string\n cause?: Capable\n }\n\nexport const isType = (value: unknown): value is Error =>\n value instanceof Error\n\nexport const box = <T extends Error, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedError => {\n const hasCause = 'cause' in value && value.cause !== undefined\n return {\n ...BoxBase,\n type,\n name: value.name,\n message: value.message,\n stack: value.stack || value.toString(),\n ...(hasCause ? { cause: recursiveBox(value.cause as Capable, context) as Capable } : {}),\n }\n}\n\nexport const revive = <T extends BoxedError, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): Error => {\n const cause = value.cause !== undefined\n ? recursiveRevive(value.cause, context)\n : undefined\n const err = cause !== undefined\n ? new Error(value.message, { cause })\n : new Error(value.message)\n if (value.name) err.name = value.name\n if (value.stack) err.stack = value.stack\n return err\n}\n\nconst typeCheck = () => {\n const boxed = box(new Error('test'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Error = revived\n // @ts-expect-error - not an Error\n const notError: string = revived\n // @ts-expect-error - cannot box non-Error\n box('not an error', {} as RevivableContext)\n}\n","import type { RevivableContext, UnderlyingType, BoxedBuffer } from './utils'\nimport type { TypedArray, TypedArrayType } from '../utils/type-guards'\n\nimport { BoxBase, boxBuffer, reviveBuffer } from './utils'\nimport {\n isTypedArray,\n typedArrayToType,\n typedArrayTypeToTypedArrayConstructor,\n} from '../utils/type-guards'\n\nexport const type = 'typedArray' as const\n\ntype BoxedTypedArray<T extends TypedArray, T2 extends RevivableContext> =\n & typeof BoxBase\n & { type: typeof type }\n & { typedArrayType: TypedArrayType }\n & BoxedBuffer<T2>\n & { [UnderlyingType]: T }\n\nexport const isType = isTypedArray\n\nexport const box = <T extends TypedArray, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedTypedArray<T, T2> =>\n ({\n ...BoxBase,\n type,\n typedArrayType: typedArrayToType(value),\n ...boxBuffer(value.buffer as ArrayBuffer, context),\n }) as unknown as BoxedTypedArray<T, T2>\n\nexport const revive = <T extends BoxedTypedArray<TypedArray, RevivableContext>>(\n value: T,\n _context: RevivableContext,\n): T[UnderlyingType] =>\n new (typedArrayTypeToTypedArrayConstructor(value.typedArrayType))(reviveBuffer(value)) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const uint8Boxed = box(new Uint8Array(10), {} as RevivableContext)\n const uint8Revived = revive(uint8Boxed, {} as RevivableContext)\n const expectedUint8: Uint8Array = uint8Revived\n // @ts-expect-error - wrong typed array type\n const wrongType: Int32Array = uint8Revived\n\n const float32Boxed = box(new Float32Array(10), {} as RevivableContext)\n const float32Revived = revive(float32Boxed, {} as RevivableContext)\n const expectedFloat32: Float32Array = float32Revived\n // @ts-expect-error - wrong typed array type\n const wrongFloat: Uint8Array = float32Revived\n\n // @ts-expect-error - cannot box non-TypedArray\n box('not a typed array', {} as RevivableContext)\n}\n","import type { TypedMessagePort, TypedMessagePortEventMap } from './typed-message-channel'\n\nexport class EventPort<T> extends EventTarget {\n addEventListener<K extends keyof TypedMessagePortEventMap<T> & string>(\n type: K,\n listener: ((event: TypedMessagePortEventMap<T>[K]) => void) | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void {\n super.addEventListener(type, listener, options)\n }\n\n removeEventListener<K extends keyof TypedMessagePortEventMap<T> & string>(\n type: K,\n listener: ((event: TypedMessagePortEventMap<T>[K]) => void) | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void {\n super.removeEventListener(type, listener, options)\n }\n\n _peer: EventPort<any> | undefined\n _queue: MessageEvent<T>[] = []\n _started = false\n _closed = false\n _onClose: (() => void) | undefined\n\n private _onmessage: ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null = null\n\n get onmessage(): ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null {\n return this._onmessage\n }\n set onmessage(value: ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null) {\n this._onmessage = value\n if (value !== null) this.start()\n }\n\n onmessageerror: ((this: MessagePort, ev: MessageEvent) => unknown) | null = null\n\n dispatchEvent(event: Event): boolean {\n if (event.type === 'message') {\n this._onmessage?.call(this, event as MessageEvent<T>)\n } else if (event.type === 'messageerror') {\n this.onmessageerror?.call(this, event as MessageEvent)\n }\n return super.dispatchEvent(event)\n }\n\n postMessage(message: T, _options?: Transferable[] | StructuredSerializeOptions): void {\n const peer = this._peer\n if (!peer || peer._closed) return\n queueMicrotask(() => {\n if (peer._closed) return\n const event = new MessageEvent('message', { data: message })\n if (peer._started) {\n peer.dispatchEvent(event)\n } else {\n peer._queue.push(event)\n }\n })\n }\n\n start(): void {\n if (this._started) return\n this._started = true\n for (const event of this._queue.splice(0)) {\n this.dispatchEvent(event)\n }\n }\n\n close(): void {\n if (this._closed) return\n this._closed = true\n this._queue.length = 0\n this._onClose?.()\n }\n}\n\nexport interface EventPort<T>\n extends Omit<\n TypedMessagePort<T>,\n 'addEventListener' | 'removeEventListener'\n > {}\n\nexport class EventChannel<T1 = unknown, T2 = unknown> {\n readonly port1: EventPort<T1>\n readonly port2: EventPort<T2>\n\n constructor() {\n const port1 = new EventPort<T1>()\n const port2 = new EventPort<T2>()\n port1._peer = port2\n port2._peer = port1\n this.port1 = port1\n this.port2 = port2\n }\n}\n","/**\n * Run `cleanup` after `target` is garbage-collected. Returns a handle to\n * cancel the tracking before that happens.\n *\n * Backed by a single shared FinalizationRegistry — every revivable that\n * needs FR semantics goes through this so the boilerplate (token,\n * unregister, cycle-safety contract) lives in one place.\n *\n * Contract: `cleanup` MUST NOT (transitively) reference `target`. The\n * registry strong-holds the cleanup callback, the cleanup would then\n * strong-hold target, and the engine would never see target as\n * collectable. Use a `WeakRef` if cleanup needs something that points\n * back at target.\n *\n * Errors thrown from cleanup are swallowed: the callback fires from the\n * FR thread, where there's no caller to surface them to.\n */\nexport type GcUnregister = () => void\n\nconst registry = new FinalizationRegistry<() => void>((cleanup) => {\n try { cleanup() } catch { /* no caller to surface to */ }\n})\n\nexport const trackGc = (target: WeakKey, cleanup: () => void): GcUnregister => {\n const token = {}\n registry.register(target, cleanup, token)\n return () => registry.unregister(token)\n}\n","import type { Capable, StructurableTransferable, Uuid } from '../types'\nimport type { TypedMessagePort } from '../utils/typed-message-channel'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from '../utils/capable-check'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\nimport { getTransferableObjects, isJsonOnlyTransport } from '../utils'\nimport { EventChannel, EventPort } from '../utils/event-channel'\nimport { trackGc } from '../utils/gc-tracker'\n\nexport const type = 'messagePort' as const\n\nexport type Messages =\n | { type: 'message', remoteUuid: Uuid, data: Capable, portId: Uuid }\n | { type: 'message-port-close', remoteUuid: Uuid, portId: Uuid }\n\nexport declare const Messages: Messages\n\nexport type AnyPort<T = Capable> =\n | TypedMessagePort<T>\n | EventPort<T>\n\nexport type BoxedMessagePort<T = Capable> =\n & BoxBaseType<typeof type>\n & (\n | { portId: Uuid, synthetic: true }\n | { portId: Uuid, synthetic: false }\n | { port: AnyPort<T>, autoBox?: boolean }\n )\n & { [UnderlyingType]: TypedMessagePort<T> }\n\n// `[T] extends [Capable]` disables distributive conditionals so `A | B` gives\n// back `AnyPort<A | B>`, not `AnyPort<A> | AnyPort<B>`. The error branch\n// intersects with AnyPort<T> so excess-property check targets the failure\n// rather than the user's port-shaped keys.\ntype StructurableTransferablePort<T> = [T] extends [Capable]\n ? AnyPort<T>\n : AnyPort<T> & {\n [ErrorMessage]: 'Message type must extend Capable'\n [BadValue]: BadFieldValue<T, Capable>\n [Path]: BadFieldPath<T, Capable>\n [ParentObject]: BadFieldParent<T, Capable>\n }\n\ntype ConnectionMessagePortState = {\n /** O(1) per-portId dispatch — avoids the O(N) addEventListener scan\n * that was the bottleneck for tight-loop RPC traffic. */\n portHandlers: Map<string, (message: Messages) => void>\n}\n\nconst connectionStateMap = new WeakMap<RevivableContext, ConnectionMessagePortState>()\n\nconst getState = (context: RevivableContext): ConnectionMessagePortState => {\n const state = connectionStateMap.get(context)\n if (!state) throw new Error('osra message-port: connection state missing; did init() run?')\n return state\n}\n\nexport const init = (context: RevivableContext): void => {\n const state: ConnectionMessagePortState = { portHandlers: new Map() }\n connectionStateMap.set(context, state)\n\n context.eventTarget.addEventListener('message', ({ detail }) => {\n if (detail.type !== 'message' && detail.type !== 'message-port-close') return\n state.portHandlers.get(detail.portId)?.(detail)\n })\n}\n\nexport const isType = (value: unknown): value is MessagePort | EventPort<StructurableTransferable> =>\n value instanceof MessagePort || value instanceof EventPort\n\nconst sendClose = (context: RevivableContext, portId: Uuid) => {\n try {\n context.sendMessage({ type: 'message-port-close', remoteUuid: context.remoteUuid, portId })\n } catch { /* connection torn down */ }\n}\n\nconst postRevived = <T>(port: AnyPort<T>, data: T, synthetic: boolean) => {\n if (synthetic) port.postMessage(data)\n else port.postMessage(data, getTransferableObjects(data))\n}\n\nexport const box = <T, T2 extends RevivableContext = RevivableContext>(\n value: StructurableTransferablePort<T>,\n context: T2,\n options?: { autoBox?: boolean },\n): BoxedMessagePort<T> => {\n // Synthetic EventPorts aren't structured-clonable, so even on a clone\n // transport we route them via portId.\n const synthetic = value instanceof EventPort\n if (!synthetic && !isJsonOnlyTransport(context.transport)) {\n return {\n ...BoxBase, type, port: value,\n ...(options?.autoBox ? { autoBox: true } : {}),\n } as BoxedMessagePort<T>\n }\n\n const { portHandlers } = getState(context)\n const liveRef: AnyPort<T> = value\n const portId: Uuid = globalThis.crypto.randomUUID()\n\n let cleanedUp = false\n const performCleanup = () => {\n if (cleanedUp) return\n cleanedUp = true\n portHandlers.delete(portId)\n unregisterGc?.()\n liveRef.removeEventListener('message', outgoingListener as EventListener)\n }\n\n const handler = (message: Messages) => {\n if (message.type === 'message-port-close') {\n performCleanup()\n liveRef.close()\n return\n }\n postRevived(liveRef, recursiveRevive(message.data, context) as T, false)\n }\n\n function outgoingListener({ data }: MessageEvent<Capable>) {\n context.sendMessage({\n type: 'message',\n remoteUuid: context.remoteUuid,\n data: recursiveBox(data, context),\n portId,\n })\n }\n\n // Safety net only — `handler` strong-holds liveRef via portHandlers, so\n // the FR won't fire while the connection is alive. Real cleanup runs via\n // the wire `message-port-close` or EventPort `_onClose`.\n const unregisterGc = trackGc(liveRef, () => {\n sendClose(context, portId)\n performCleanup()\n })\n\n liveRef.addEventListener('message', outgoingListener as EventListener)\n liveRef.start()\n\n if (liveRef instanceof EventPort) {\n liveRef._onClose = () => {\n if (cleanedUp) return\n sendClose(context, portId)\n performCleanup()\n }\n }\n\n portHandlers.set(portId, handler)\n\n return { ...BoxBase, type, portId, synthetic } as BoxedMessagePort<T>\n}\n\nexport const revive = <T extends Capable, T2 extends RevivableContext>(\n value: BoxedMessagePort<T>,\n context: T2,\n): TypedMessagePort<T> => {\n if ('port' in value) {\n if (value.autoBox) return createProtocolPort<T>(value.port as TypedMessagePort<Capable>, context)\n return value.port\n }\n return reviveViaPortId<T>(value.portId, context, value.synthetic)\n}\n\n/** Wraps a real MessagePort so revivables can treat it like a transparent\n * EventTarget that auto-boxes/revives — letting live values (Promises,\n * Functions, …) ride a clone-only transport. */\nconst createProtocolPort = <T>(\n port: TypedMessagePort<Capable>,\n ctx: RevivableContext,\n): TypedMessagePort<T> => {\n const target = new EventTarget() as TypedMessagePort<T>\n const onMessage = ({ data }: MessageEvent<Capable>): void => {\n target.dispatchEvent(new MessageEvent('message', { data: recursiveRevive(data, ctx) }))\n }\n port.addEventListener('message', onMessage)\n target.postMessage = (data: T, opt?: Transferable[] | StructuredSerializeOptions) => {\n const boxed = recursiveBox(data as Capable, ctx)\n const transferables = getTransferableObjects(boxed)\n const extra = Array.isArray(opt) ? opt : []\n port.postMessage(boxed, extra.length ? [...transferables, ...extra] : transferables)\n }\n target.start = () => port.start()\n target.close = () => {\n port.removeEventListener('message', onMessage)\n port.close()\n }\n return target\n}\n\n/** Factory for revivable-internal channels. Returns a local port that\n * auto-boxes live values regardless of transport, plus a pre-boxed remote\n * port the revivable embeds in its Boxed* structure. */\nexport const createRevivableChannel = <T extends Capable>(\n context: RevivableContext,\n): { localPort: AnyPort<T>, boxedRemote: BoxedMessagePort<T> } => {\n if (isJsonOnlyTransport(context.transport)) {\n const { port1, port2 } = new EventChannel<T, T>()\n return {\n localPort: port1,\n boxedRemote: box(port2 as StructurableTransferablePort<T>, context),\n }\n }\n const { port1, port2 } = new MessageChannel() as unknown as {\n port1: TypedMessagePort<Capable>\n port2: TypedMessagePort<Capable>\n }\n return {\n localPort: createProtocolPort<T>(port1, context) as unknown as AnyPort<T>,\n boxedRemote: box(port2 as unknown as StructurableTransferablePort<T>, context, { autoBox: true }),\n }\n}\n\nconst reviveViaPortId = <T extends Capable>(\n portId: Uuid,\n context: RevivableContext,\n synthetic: boolean,\n): TypedMessagePort<T> => {\n const { portHandlers } = getState(context)\n const { port1: userPort, port2: internalPort } =\n synthetic\n ? new EventChannel<T, T>()\n : new MessageChannel() as { port1: TypedMessagePort<T>, port2: TypedMessagePort<T> }\n const userPortRef = new WeakRef(userPort)\n // For synthetic EventChannels, internalPort._peer === userPort — holding\n // internalPort strongly from the trackGc cleanup would re-pin userPort.\n const internalPortRef = new WeakRef(internalPort)\n\n let cleanedUp = false\n const performCleanup = () => {\n if (cleanedUp) return\n cleanedUp = true\n portHandlers.delete(portId)\n const internal = internalPortRef.deref()\n internal?.removeEventListener('message', internalPortListener as EventListener)\n internal?.close()\n unregisterGc?.()\n }\n\n const handler = (message: Messages) => {\n if (message.type === 'message-port-close') {\n performCleanup()\n userPortRef.deref()?.close()\n return\n }\n if (!userPortRef.deref()) {\n performCleanup()\n return\n }\n const internal = internalPortRef.deref()\n if (!internal) return\n postRevived(internal, recursiveRevive(message.data, context) as T, synthetic)\n }\n\n const internalPortListener = ({ data }: MessageEvent<T>) => {\n context.sendMessage({\n type: 'message',\n remoteUuid: context.remoteUuid,\n data: recursiveBox(data, context),\n portId,\n })\n }\n\n const unregisterGc = trackGc(userPort, () => {\n sendClose(context, portId)\n performCleanup()\n })\n\n if (userPort instanceof EventPort) {\n userPort._onClose = () => {\n if (cleanedUp) return\n sendClose(context, portId)\n performCleanup()\n }\n }\n\n internalPort.addEventListener('message', internalPortListener as EventListener)\n internalPort.start()\n\n portHandlers.set(portId, handler)\n\n return userPort\n}\n\nconst typeCheck = () => {\n const port = {} as TypedMessagePort<{ foo: string }>\n const boxed = box(port, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: AnyPort<{ foo: string }> = revived\n // @ts-expect-error - wrong message type\n const wrongType: AnyPort<{ bar: number }> = revived\n box({} as TypedMessagePort<Promise<string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from '../utils/capable-check'\n\nimport { BoxBase, serializeError } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort,\n AnyPort,\n} from './message-port'\n\nexport const type = 'promise' as const\n\nexport type Context =\n | { type: 'resolve', data: Capable }\n | { type: 'reject', error: string }\n\n// Error branches intersect with T so the user's own keys land on the target —\n// otherwise TS's excess-property check flags a user key instead of the failure.\ntype CapablePromise<T> = T extends Promise<infer U>\n ? U extends Capable\n ? T\n : T & {\n [ErrorMessage]: 'Value type must extend a Promise that resolves to a Capable'\n [BadValue]: BadFieldValue<U, Capable>\n [Path]: BadFieldPath<U, Capable>\n [ParentObject]: BadFieldParent<U, Capable>\n }\n : T & {\n [ErrorMessage]: 'Value type must extend a Promise that resolves to a Capable'\n [BadValue]: T\n [Path]: ''\n [ParentObject]: T\n }\n\ntype ExtractCapable<T> = T extends Promise<infer U>\n ? U extends Capable ? U : never\n : never\n\nconst isCapablePromise = <T, U extends Capable = ExtractCapable<T>>(value: T): value is T & Promise<U> =>\n value instanceof Promise\n\nexport type BoxedPromise<T extends Capable = Capable> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Context> }\n & { [UnderlyingType]: T }\n\n// Pins the revived port between executor return and result arrival — the\n// port↔listener cycle has no other anchor (the caller only holds the\n// returned Promise). The once-listener removes its entry on settle.\nconst inFlightPromisePorts = new Set<AnyPort<Context>>()\n\nexport const isType = (value: unknown): value is Promise<any> =>\n value instanceof Promise\n\nexport const box = <T, T2 extends RevivableContext>(\n value: CapablePromise<T>,\n context: T2\n): BoxedPromise<ExtractCapable<T>> => {\n if (!isCapablePromise(value)) throw new TypeError('Expected Promise')\n const { localPort, boxedRemote } = createRevivableChannel<Context>(context)\n\n const sendResult = (result: Context) => {\n localPort.postMessage(result)\n localPort.close()\n }\n\n value\n .then((data: ExtractCapable<T>) => sendResult({ type: 'resolve', data }))\n .catch((error: unknown) => sendResult({ type: 'reject', error: serializeError(error) }))\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedPromise<ExtractCapable<T>>\n}\n\nexport const revive = <T extends BoxedPromise, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => {\n const port = reviveMessagePort(value.port, context)\n inFlightPromisePorts.add(port)\n return new Promise<T[UnderlyingType]>((resolve, reject) => {\n port.addEventListener('message', ({ data: result }) => {\n if (result.type === 'resolve') resolve(result.data as T[UnderlyingType])\n else reject(result.error)\n port.close()\n inFlightPromisePorts.delete(port)\n }, { once: true })\n port.start()\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(Promise.resolve(1 as const), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Promise<1> = revived\n // @ts-expect-error\n const notExpected: Promise<string> = revived\n // @ts-expect-error\n box(1 as const, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { UnderlyingType, RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase, serializeError } from './utils'\nimport { recursiveBox } from '.'\nimport { getTransferableObjects } from '../utils'\nimport { EventChannel, type EventPort } from '../utils/event-channel'\nimport { box as boxMessagePort, revive as reviveMessagePort, BoxedMessagePort } from './message-port'\n\nexport const type = 'function' as const\n\ntype ResultMessage =\n | { __osra_ok__: true, value: Capable }\n | { __osra_err__: true, error: string }\n\ntype CallContext = [EventPort<Capable>, Capable[]]\n\n// Pins return-value ports between call-site return and result arrival —\n// the (port ↔ once-listener ↔ resolve/reject) cycle has no other anchor.\nconst inFlightReturnPorts = new Set<EventPort<Capable>>()\n\nexport type BoxedFunction<T extends (...args: any[]) => any = (...args: any[]) => any> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<CallContext> }\n & { [UnderlyingType]: (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> }\n\ntype CapableFunction<T> = T extends (...args: infer P) => infer R\n ? P extends Capable[]\n ? R extends Capable ? T : never\n : never\n : never\n\nexport const isType = (value: unknown): value is (...args: any[]) => any =>\n typeof value === 'function'\n\nexport const box = <T extends (...args: any[]) => any, T2 extends RevivableContext>(\n value: T & CapableFunction<T>,\n context: T2,\n): BoxedFunction<T> => {\n // EventChannel rather than MessageChannel: revived live values arriving\n // in args (functions, EventTarget façades, …) aren't structured-clonable.\n const { port1: localPort, port2: remotePort } = new EventChannel<CallContext, CallContext>()\n\n localPort.addEventListener('message', ({ data }) => {\n // Don't recursiveRevive — message-port handler already revived in place.\n // Re-walking would Object.fromEntries plain args, breaking identity.\n const [returnPort, args] = data as CallContext\n ;(async () => {\n let message: ResultMessage\n try {\n const resolved = await value(...(args as Parameters<T>))\n message = { __osra_ok__: true, value: resolved as Capable }\n } catch (error) {\n message = { __osra_err__: true, error: serializeError(error) }\n }\n const boxedResult = recursiveBox(message as Capable, context)\n returnPort.postMessage(boxedResult, getTransferableObjects(boxedResult))\n // Defer close so the result reaches the peer before tear-down. The\n // close fires _onClose, dropping per-call routing entries on both\n // sides — without it portHandlers grows one entry per call.\n queueMicrotask(() => {\n try { returnPort.close() } catch { /* may already be closed */ }\n })\n })()\n })\n localPort.start()\n\n return {\n ...BoxBase,\n type,\n port: boxMessagePort(remotePort as unknown as MessagePort, context),\n } as unknown as BoxedFunction<T>\n}\n\nexport const revive = <T extends BoxedFunction, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context) as unknown as MessagePort\n\n return ((...args: Capable[]) =>\n new Promise((resolve, reject) => {\n const { port1: returnLocal, port2: returnRemote } = new EventChannel<Capable, Capable>()\n inFlightReturnPorts.add(returnLocal)\n\n returnLocal.addEventListener('message', ({ data }) => {\n const message = data as ResultMessage\n if ('__osra_ok__' in message) resolve(message.value)\n else reject(message.error)\n returnLocal.close()\n inFlightReturnPorts.delete(returnLocal)\n }, { once: true })\n returnLocal.start()\n\n const callContext = recursiveBox([returnRemote, args] as unknown as Capable, context)\n port.postMessage(callContext, getTransferableObjects(callContext))\n })) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const boxed = box((a: number, b: string) => a + b.length, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: (a: number, b: string) => Promise<number> = revived\n // @ts-expect-error - wrong return type\n const wrongReturn: (a: number, b: string) => Promise<string> = revived\n // @ts-expect-error - wrong parameter types\n const wrongParams: (a: string, b: number) => Promise<number> = revived\n // @ts-expect-error - non-Capable parameter type (WeakMap isn't structured-clonable)\n box((a: WeakMap<object, string>) => a.toString(), {} as RevivableContext)\n // @ts-expect-error - non-Capable return type\n box(() => new WeakMap<object, string>(), {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\n\nimport { BoxBase } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort\n} from './message-port'\n\nexport const type = 'readableStream' as const\n\nexport type PullContext = {\n type: 'pull' | 'cancel'\n}\n\ntype ChunkMessage<T = unknown> = Promise<ReadableStreamReadResult<T>>\n\ntype Msg = PullContext | ChunkMessage\n\nexport type BoxedReadableStream<T extends ReadableStream = ReadableStream> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Msg> }\n & { [UnderlyingType]: T }\n\nexport const isType = (value: unknown): value is ReadableStream =>\n value instanceof ReadableStream\n\nexport const box = <T extends ReadableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): BoxedReadableStream<T> => {\n const { localPort, boxedRemote } = createRevivableChannel<Msg>(context)\n const reader = value.getReader()\n\n localPort.addEventListener('message', ({ data }) => {\n if ('type' in data && data.type === 'pull') {\n // reader.read() is a Promise — localPort boxes it for the transport.\n localPort.postMessage(reader.read())\n } else {\n reader.cancel()\n localPort.close()\n }\n })\n localPort.start()\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedReadableStream<T>\n}\n\nexport const revive = <T extends BoxedReadableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n return new ReadableStream({\n pull: (controller) => new Promise<void>((resolve, reject) => {\n port.addEventListener('message', ({ data }) => {\n if (!(data instanceof Promise)) return\n data\n .then(result => {\n if (result.done) controller.close()\n else controller.enqueue(result.value)\n resolve()\n })\n .catch(reject)\n }, { once: true })\n port.postMessage({ type: 'pull' })\n }),\n cancel: () => {\n port.postMessage({ type: 'cancel' })\n // Defer close so the cancel message dispatches before tear-down.\n queueMicrotask(() => port.close())\n },\n }) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const stream = new ReadableStream<number>()\n const boxed = box(stream, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ReadableStream<number> = revived\n // @ts-expect-error - wrong stream type\n const wrongType: ReadableStream<string> = revived\n // @ts-expect-error - not a ReadableStream\n box('not a stream', {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\nimport type { Capable } from '../types'\n\nimport { BoxBase } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort,\n} from './message-port'\n\nexport const type = 'writableStream' as const\n\n// Outgoing wire shape (revive → box): one of these per call.\nexport type WriteContext =\n | { type: 'write', chunk: Capable }\n | { type: 'close' }\n | { type: 'abort', reason: Capable }\n\n// Reply from box → revive after a write completes (so writer.write() awaits).\nexport type WriteAck =\n | { type: 'ack' }\n | { type: 'err', error: string }\n\nexport type Msg = WriteContext | WriteAck\n\nexport type BoxedWritableStream<T extends WritableStream = WritableStream> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Msg> }\n & { [UnderlyingType]: T }\n\nexport const isType = (value: unknown): value is WritableStream =>\n value instanceof WritableStream\n\nexport const box = <T extends WritableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): BoxedWritableStream<T> => {\n const { localPort, boxedRemote } = createRevivableChannel<Msg>(context)\n const writer = value.getWriter()\n\n localPort.addEventListener('message', ({ data }) => {\n if (!data || typeof data !== 'object' || !('type' in data)) return\n if (data.type === 'write') {\n writer.write((data as { chunk: Capable }).chunk as any)\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n } else if (data.type === 'close') {\n writer.close()\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n } else if (data.type === 'abort') {\n writer.abort((data as { reason: Capable }).reason as any)\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n }\n })\n localPort.start()\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedWritableStream<T>\n}\n\nexport const revive = <T extends BoxedWritableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n // Each `write` call posts a 'write' message and awaits an 'ack'/'err'.\n // The port is shared, so we serialize via a chain — without it, two\n // concurrent writes would race and could mis-pair their ack messages.\n let chain: Promise<void> = Promise.resolve()\n const request = (msg: WriteContext): Promise<void> => {\n const next = chain.then(() => new Promise<void>((resolve, reject) => {\n port.addEventListener('message', ({ data }) => {\n if (!data || typeof data !== 'object' || !('type' in data)) return\n if ((data as { type: string }).type === 'ack') resolve()\n else if ((data as { type: string }).type === 'err') reject(new Error((data as { error: string }).error))\n }, { once: true })\n port.postMessage(msg as Msg)\n }))\n chain = next.catch(() => {})\n return next\n }\n\n return new WritableStream({\n write: (chunk) => request({ type: 'write', chunk: chunk as Capable }),\n close: () => request({ type: 'close' }),\n abort: (reason) => request({ type: 'abort', reason: reason as Capable }),\n }) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const stream = new WritableStream<number>()\n const boxed = box(stream, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: WritableStream<number> = revived\n // @ts-expect-error - wrong stream type\n const wrongType: WritableStream<string> = revived\n // @ts-expect-error - not a WritableStream\n box('not a stream', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { BoxedMessagePort } from './message-port'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n} from './message-port'\n\nexport const type = 'abortSignal' as const\n\ntype AbortMessage = {\n type: 'abort'\n reason?: Capable\n}\n\nexport type BoxedAbortSignal =\n & BoxBaseType<typeof type>\n & {\n aborted: boolean\n reason?: Capable\n port: BoxedMessagePort<AbortMessage>\n }\n\nexport const isType = (value: unknown): value is AbortSignal =>\n value instanceof AbortSignal\n\nexport const box = <T extends AbortSignal, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedAbortSignal => {\n const { localPort, boxedRemote } = createRevivableChannel<AbortMessage>(context)\n\n if (!value.aborted) {\n value.addEventListener('abort', () => {\n localPort.postMessage({ type: 'abort', reason: value.reason as Capable })\n localPort.close()\n }, { once: true })\n } else {\n localPort.close()\n }\n\n // Eagerly-aborted reason rides the wrapper, so we must box it here —\n // recursiveBox short-circuits on OSRA_BOX without descending in.\n return {\n ...BoxBase,\n type,\n aborted: value.aborted,\n reason: value.aborted ? recursiveBox(value.reason as Capable, context) as Capable : undefined,\n port: boxedRemote,\n }\n}\n\nexport const revive = <T extends BoxedAbortSignal, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): AbortSignal => {\n const controller = new AbortController()\n\n if (value.aborted) {\n controller.abort(recursiveRevive(value.reason as Capable, context))\n return controller.signal\n }\n\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n port.addEventListener('message', ({ data: message }) => {\n if (message.type === 'abort') {\n controller.abort(recursiveRevive(message.reason as Capable, context))\n port.close()\n }\n })\n\n return controller.signal\n}\n\nconst typeCheck = () => {\n const boxed = box(new AbortController().signal, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: AbortSignal = revived\n // @ts-expect-error - not an AbortSignal\n const notAbortSignal: string = revived\n // @ts-expect-error - cannot box non-AbortSignal\n box('not an abort signal', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { box as boxHeaders, revive as reviveHeaders } from './headers'\nimport { box as boxReadableStream, revive as reviveReadableStream } from './readable-stream'\n\nexport const type = 'response' as const\n\nexport const isType = (value: unknown): value is Response =>\n value instanceof Response\n\nexport const box = <T extends Response, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => ({\n ...BoxBase,\n type,\n status: value.status,\n statusText: value.statusText,\n headers: boxHeaders(value.headers, context),\n body: value.body ? boxReadableStream(value.body, context) : null,\n url: value.url,\n redirected: value.redirected\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n context: T2\n): Response => {\n const headers = reviveHeaders(value.headers, context)\n const body = value.body ? reviveReadableStream(value.body, context) : null\n\n return new Response(body, {\n status: value.status,\n statusText: value.statusText,\n headers\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(new Response('body', { status: 200 }), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Response = revived\n // @ts-expect-error - not a Response\n const notResponse: string = revived\n // @ts-expect-error - cannot box non-Response\n box('not a response', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { box as boxHeaders, revive as reviveHeaders } from './headers'\nimport { box as boxReadableStream, revive as reviveReadableStream } from './readable-stream'\n\nexport const type = 'request' as const\n\nexport const isType = (value: unknown): value is Request =>\n value instanceof Request\n\nexport const box = <T extends Request, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => ({\n ...BoxBase,\n type,\n method: value.method,\n url: value.url,\n headers: boxHeaders(value.headers, context),\n body: value.body ? boxReadableStream(value.body, context) : null,\n credentials: value.credentials,\n cache: value.cache,\n redirect: value.redirect,\n referrer: value.referrer,\n referrerPolicy: value.referrerPolicy,\n integrity: value.integrity,\n keepalive: value.keepalive\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n context: T2\n): Request => {\n const headers = reviveHeaders(value.headers, context)\n const body = value.body ? reviveReadableStream(value.body, context) : null\n\n return new Request(value.url, {\n method: value.method,\n headers,\n body,\n credentials: value.credentials,\n cache: value.cache,\n redirect: value.redirect,\n referrer: value.referrer,\n referrerPolicy: value.referrerPolicy,\n integrity: value.integrity,\n keepalive: value.keepalive,\n // @ts-expect-error - duplex is needed for streaming bodies\n duplex: 'half',\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(new Request('https://example.com'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Request = revived\n // @ts-expect-error - not a Request\n const notRequest: string = revived\n // @ts-expect-error - cannot box non-Request\n box('not a request', {} as RevivableContext)\n}\n","import type { Capable, Uuid } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'identity' as const\n\nexport type Messages = {\n type: 'identity-dispose'\n remoteUuid: Uuid\n id: string\n}\n\nexport declare const Messages: Messages\n\nconst IDENTITY_MARKER: unique symbol = Symbol.for('osra.identity')\n\ntype IdentityWrapper<T = unknown> = {\n readonly [IDENTITY_MARKER]: true\n readonly value: T\n}\n\nexport type BoxedIdentity<T extends Capable = Capable> = BoxBaseType<typeof type> & {\n id: string\n inner?: Capable\n [UnderlyingType]: T\n}\n\nconst isObjectOrFunction = (value: unknown): value is object =>\n value !== null && (typeof value === 'object' || typeof value === 'function')\n\n/** Anything we can hand to WeakMap/WeakRef/FinalizationRegistry. Excludes\n * registered symbols (Symbol.for) — those throw at runtime. */\nconst isWeakKeyable = (value: unknown): value is WeakKey => {\n if (value === null) return false\n const t = typeof value\n if (t === 'object' || t === 'function') return true\n if (t === 'symbol') return Symbol.keyFor(value as symbol) === undefined\n return false\n}\n\nconst isIdentityWrapper = (value: unknown): value is IdentityWrapper =>\n isObjectOrFunction(value) && IDENTITY_MARKER in value && value[IDENTITY_MARKER] === true\n\nconst wrapperMemo = new WeakMap<object, IdentityWrapper>()\n\nconst wrap = (value: object): IdentityWrapper => {\n if (isIdentityWrapper(value)) return value\n const cached = wrapperMemo.get(value)\n if (cached) return cached\n const wrapper: IdentityWrapper = { [IDENTITY_MARKER]: true, value }\n wrapperMemo.set(value, wrapper)\n return wrapper\n}\n\n/** Wrap a value so osra preserves reference identity across the RPC\n * boundary. Idempotent; primitives pass through unchanged. Lies at the\n * type level — runtime value is an IdentityWrapper<T> typed as T. */\nexport const identity = <T>(value: T): T =>\n (isObjectOrFunction(value) ? wrap(value) : value) as T\n\ntype IdentityState = {\n readonly sendIds: WeakMap<WeakKey, string>\n /** id → ref to the value we sent, so a round-trip resolves to the\n * original reference instead of building a fresh proxy. */\n readonly idToSent: Map<string, WeakRef<WeakKey>>\n readonly sendRegistry: FinalizationRegistry<string>\n readonly receiveCache: Map<string, unknown>\n /** Revived value → id, so user code passing a revived value back to\n * its origin replays the peer's id and short-circuits to the real ref. */\n readonly revivedToId: WeakMap<WeakKey, string>\n listenerInstalled: boolean\n}\n\nconst connectionStates = new WeakMap<RevivableContext, IdentityState>()\n\nconst getOrCreateState = (context: RevivableContext): IdentityState => {\n const existing = connectionStates.get(context)\n if (existing) return existing\n const sendIds = new WeakMap<WeakKey, string>()\n const idToSent = new Map<string, WeakRef<WeakKey>>()\n const receiveCache = new Map<string, unknown>()\n const revivedToId = new WeakMap<WeakKey, string>()\n const sendRegistry = new FinalizationRegistry<string>((id) => {\n idToSent.delete(id)\n try {\n context.sendMessage({ type: 'identity-dispose', remoteUuid: context.remoteUuid, id })\n } catch { /* connection already closed */ }\n })\n const state: IdentityState = {\n sendIds, idToSent, sendRegistry, receiveCache, revivedToId,\n listenerInstalled: false,\n }\n connectionStates.set(context, state)\n installReceiveListener(context, state)\n return state\n}\n\nconst installReceiveListener = (context: RevivableContext, state: IdentityState) => {\n if (state.listenerInstalled) return\n state.listenerInstalled = true\n context.eventTarget.addEventListener('message', ({ detail }) => {\n if (detail?.type !== 'identity-dispose') return\n const revived = state.receiveCache.get(detail.id)\n state.receiveCache.delete(detail.id)\n if (revived !== undefined && isWeakKeyable(revived)) state.revivedToId.delete(revived)\n })\n}\n\nexport const isType = (value: unknown): value is IdentityWrapper =>\n isIdentityWrapper(value)\n\n/** Look up or assign the id for a referenceable value. Returns whether\n * the id is already-known (resend or round-trip) so the caller can skip\n * shipping the inner payload. */\nconst registerForReference = (\n value: WeakKey,\n state: IdentityState,\n): { id: string, isExisting: boolean } => {\n const existingId = state.sendIds.get(value)\n if (existingId !== undefined) return { id: existingId, isExisting: true }\n const receivedId = state.revivedToId.get(value)\n if (receivedId !== undefined) return { id: receivedId, isExisting: true }\n const id = globalThis.crypto.randomUUID()\n state.sendIds.set(value, id)\n state.idToSent.set(id, new WeakRef(value))\n state.sendRegistry.register(value, id)\n return { id, isExisting: false }\n}\n\nexport const box = <T extends Capable, TContext extends RevivableContext>(\n wrapper: IdentityWrapper<T>,\n context: TContext,\n): BoxedIdentity<T> => {\n const state = getOrCreateState(context)\n const inner = wrapper.value\n const innerBox = recursiveBox(inner, context)\n if (!isWeakKeyable(inner)) {\n // Inner can't anchor a WeakMap key — emit fresh id+inner each time, no dedup.\n return { ...BoxBase, type, id: globalThis.crypto.randomUUID(), inner: innerBox } as BoxedIdentity<T>\n }\n const { id, isExisting } = registerForReference(inner, state)\n if (isExisting) return { ...BoxBase, type, id } as BoxedIdentity<T>\n return { ...BoxBase, type, id, inner: innerBox } as BoxedIdentity<T>\n}\n\n/** Identity-box a referenceable value with a caller-supplied inner box,\n * bypassing the recursive-box step. Used by revivables (symbol with\n * description=undefined) where recursing back through their own box\n * would loop into this module again. */\nexport const boxByReference = <T extends WeakKey, TContext extends RevivableContext>(\n value: T,\n innerBox: Capable,\n context: TContext,\n): BoxedIdentity => {\n const state = getOrCreateState(context)\n const { id, isExisting } = registerForReference(value, state)\n if (isExisting) return { ...BoxBase, type, id } as BoxedIdentity\n return { ...BoxBase, type, id, inner: innerBox } as BoxedIdentity\n}\n\nexport const revive = <T extends BoxedIdentity, TContext extends RevivableContext>(\n value: T,\n context: TContext,\n): T[UnderlyingType] => {\n const state = getOrCreateState(context)\n const cached = state.receiveCache.get(value.id)\n if (cached !== undefined) return cached as T[UnderlyingType]\n const originated = state.idToSent.get(value.id)?.deref()\n if (originated !== undefined) return originated as T[UnderlyingType]\n if (!('inner' in value) || value.inner === undefined) {\n throw new Error(`osra identity: received id=${value.id} with no inner payload and no cached value`)\n }\n const revived = recursiveRevive(value.inner, context)\n state.receiveCache.set(value.id, revived)\n if (isWeakKeyable(revived)) state.revivedToId.set(revived, value.id)\n return revived as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const fn = () => 42\n const wrapper = { [IDENTITY_MARKER]: true, value: fn } as IdentityWrapper<typeof fn>\n const boxed = box(wrapper, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: typeof fn = revived\n // @ts-expect-error - revived is the original function type, not string\n const notExpected: string = revived\n // @ts-expect-error - cannot box a non-Capable wrapper (WeakMap not assignable)\n box({ [IDENTITY_MARKER]: true, value: new WeakMap() } as IdentityWrapper<WeakMap<object, string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { BoxBase as BoxBaseType, RevivableContext, UnderlyingType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { instanceOfAny, isJsonOnlyTransport } from '../utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'transfer' as const\n\nconst TRANSFER_MARKER: unique symbol = Symbol.for('osra.transfer')\n\ntype TransferWrapper<T = unknown> = {\n readonly [TRANSFER_MARKER]: true\n readonly value: T\n}\n\nexport type BoxedTransfer<T extends Capable = Capable> = BoxBaseType<typeof type> & {\n inner: Capable\n degraded: boolean\n [UnderlyingType]: T\n}\n\nconst isObject = (value: unknown): value is object =>\n value !== null && typeof value === 'object'\n\nconst isTransferWrapper = (value: unknown): value is TransferWrapper =>\n isObject(value) && TRANSFER_MARKER in value && value[TRANSFER_MARKER] === true\n\nconst isWrappableTransferable = (value: unknown): boolean => {\n if (!isObject(value)) return false\n if (ArrayBuffer.isView(value)) return true\n return instanceOfAny(value, [\n globalThis.ArrayBuffer,\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n ])\n}\n\n/** Opt into transfer (move) semantics for a transferable value. Idempotent;\n * non-transferable inputs pass through unchanged. Silently degrades to a\n * copy when the platform/transport can't transfer the given type. Lies at\n * the type level — runtime value is a TransferWrapper<T> typed as T. */\nexport const transfer = <T>(value: T): T =>\n (isWrappableTransferable(value)\n ? { [TRANSFER_MARKER]: true, value }\n : value\n ) as T\n\nexport const isType = (value: unknown): value is TransferWrapper =>\n isTransferWrapper(value)\n\nexport const box = <T extends Capable, TContext extends RevivableContext>(\n wrapper: TransferWrapper<T>,\n context: TContext,\n): BoxedTransfer<T> =>\n // `degraded` tells the send-time walker in getTransferableObjects to treat\n // this box as a regular value (no transfer-list entry). JSON transports\n // can't move ownership, so transfer semantics don't apply.\n ({\n ...BoxBase,\n type,\n inner: recursiveBox(wrapper.value, context),\n degraded: isJsonOnlyTransport(context.transport),\n }) as unknown as BoxedTransfer<T>\n\nexport const revive = <T extends BoxedTransfer, TContext extends RevivableContext>(\n value: T,\n context: TContext,\n): T[UnderlyingType] =>\n recursiveRevive(value.inner, context) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const ab = new ArrayBuffer(10)\n const wrapper = { [TRANSFER_MARKER]: true, value: ab } as TransferWrapper<ArrayBuffer>\n const boxed = box(wrapper, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ArrayBuffer = revived\n // @ts-expect-error - revived is ArrayBuffer, not string\n const notExpected: string = revived\n // @ts-expect-error - cannot box a non-Capable wrapper (WeakMap not assignable)\n box({ [TRANSFER_MARKER]: true, value: new WeakMap() } as TransferWrapper<WeakMap<object, string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, UnderlyingType, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'map' as const\n\nexport type BoxedMap<T extends Map<Capable, Capable> = Map<Capable, Capable>> =\n & BoxBaseType<typeof type>\n & { entries: Array<[Capable, Capable]> }\n & { [UnderlyingType]: T }\n\n// `Map<unknown, unknown>` (rather than `Map<Capable, Capable>`) breaks the\n// Capable ↔ defaultRevivableModules ↔ this module type cycle. box() still\n// narrows to `Map<Capable, Capable>` so misuse is caught there.\nexport const isType = (value: unknown): value is Map<unknown, unknown> =>\n value instanceof Map\n\nexport const box = <T extends Map<Capable, Capable>, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedMap<T> => ({\n ...BoxBase,\n type,\n entries: Array.from(value, ([k, v]): [Capable, Capable] =>\n [recursiveBox(k, context) as Capable, recursiveBox(v, context) as Capable]),\n}) as BoxedMap<T>\n\nexport const revive = <T extends BoxedMap, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n new Map(value.entries.map(([k, v]) => [\n recursiveRevive(k, context),\n recursiveRevive(v, context),\n ])) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const m = new Map<string, number>([['a', 1]])\n const boxed = box(m, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Map<string, number> = revived\n // @ts-expect-error - wrong value type\n const wrongValue: Map<string, string> = revived\n // @ts-expect-error - cannot box non-Map\n box('not a map', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, UnderlyingType, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'set' as const\n\nexport type BoxedSet<T extends Set<Capable> = Set<Capable>> =\n & BoxBaseType<typeof type>\n & { values: Array<Capable> }\n & { [UnderlyingType]: T }\n\n// `Set<unknown>` breaks the Capable ↔ defaultRevivableModules ↔ this module\n// type cycle; box() narrows to `Set<Capable>` so misuse is caught there.\nexport const isType = (value: unknown): value is Set<unknown> =>\n value instanceof Set\n\nexport const box = <T extends Set<Capable>, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedSet<T> => ({\n ...BoxBase,\n type,\n values: Array.from(value, v => recursiveBox(v, context) as Capable),\n}) as BoxedSet<T>\n\nexport const revive = <T extends BoxedSet, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n new Set(value.values.map(v => recursiveRevive(v, context))) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const s = new Set<number>([1, 2, 3])\n const boxed = box(s, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Set<number> = revived\n // @ts-expect-error - wrong value type\n const wrongValue: Set<string> = revived\n // @ts-expect-error - cannot box non-Set\n box('not a set', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'bigint' as const\n\nexport const isType = (value: unknown): value is bigint =>\n typeof value === 'bigint'\n\nexport const box = <T extends bigint, T2 extends RevivableContext>(\n value: T,\n _context: T2,\n) => ({\n ...BoxBase,\n type,\n value: value.toString(),\n})\n\nexport const revive = <T extends ReturnType<typeof box>>(\n value: T,\n _context: RevivableContext,\n) => BigInt(value.value)\n\nconst typeCheck = () => {\n const boxed = box(123n, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: bigint = revived\n // @ts-expect-error - not a string\n const notString: string = revived\n // @ts-expect-error - cannot box non-bigint\n box('not a bigint', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'event' as const\n\n/** Boxes Event/CustomEvent only. Subclass-specific fields (MessageEvent.data,\n * ErrorEvent.error, ProgressEvent.loaded, etc.) are dropped on the wire. */\nexport type BoxedEvent =\n & BoxBaseType<typeof type>\n & { eventType: string, bubbles: boolean, cancelable: boolean, composed: boolean, detail?: Capable }\n\nexport const isType = (value: unknown): value is Event =>\n value instanceof Event\n\nexport const box = <T extends Event, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedEvent => ({\n ...BoxBase,\n type,\n eventType: value.type,\n bubbles: value.bubbles,\n cancelable: value.cancelable,\n composed: value.composed,\n ...(value instanceof CustomEvent ? { detail: recursiveBox(value.detail as Capable, context) as Capable } : {}),\n})\n\nexport const revive = <T extends BoxedEvent, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): Event => {\n const init = { bubbles: value.bubbles, cancelable: value.cancelable, composed: value.composed }\n return 'detail' in value\n ? new CustomEvent(value.eventType, { ...init, detail: recursiveRevive(value.detail as Capable, context) })\n : new Event(value.eventType, init)\n}\n\nconst typeCheck = () => {\n const boxed = box(new Event('foo'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Event = revived\n // @ts-expect-error - not an Event\n const notEvent: string = revived\n // @ts-expect-error - cannot box non-Event\n box('not an event', {} as RevivableContext)\n}\n","import { BoxBase, type RevivableContext } from './utils'\nimport { identity } from './identity'\nimport { box as boxFunction, revive as reviveFunction } from './function'\nimport { trackGc } from '../utils/gc-tracker'\n\nexport const type = 'eventTarget' as const\n\ntype ListenerOpts = boolean | { capture?: boolean, once?: boolean, passive?: boolean, signal?: AbortSignal }\n\nexport const isType = (value: unknown): value is EventTarget => value instanceof EventTarget\n\nexport const box = <T extends EventTarget, T2 extends RevivableContext>(value: T, context: T2) => ({\n ...BoxBase,\n type,\n addListener: boxFunction(\n (type: string, listener: EventListener, options?: ListenerOpts) =>\n value.addEventListener(type, listener, options),\n context,\n ),\n removeListener: boxFunction(\n (type: string, listener: EventListener, options?: ListenerOpts) =>\n value.removeEventListener(type, listener, options),\n context,\n ),\n})\n\nexport type BoxedEventTarget = ReturnType<typeof box>\n\n// Stable EventListener per EventListenerObject so identity() yields the\n// same id on add and remove.\nconst objectWrappers = new WeakMap<EventListenerObject, EventListener>()\nconst toListener = (listerObject: EventListenerOrEventListenerObject): EventListener => {\n if (typeof listerObject === 'function') return listerObject\n let listener = objectWrappers.get(listerObject)\n if (!listener) objectWrappers.set(listerObject, listener = (e) => listerObject.handleEvent(e))\n return listener\n}\n\ntype Reg = { eventType: string, listener: EventListener, capture: boolean }\n\nconst findReg = (regs: Reg[], eventType: string, listener: EventListener, capture: boolean): Reg | undefined =>\n regs.find(r => r.eventType === eventType && r.listener === listener && r.capture === capture)\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(value: T, context: T2) => {\n const addRpc = reviveFunction(value.addListener, context)\n const removeRpc = reviveFunction(value.removeListener, context)\n // Façade only — events never dispatch through it. Source-side EventTarget\n // owns all semantics; we just track regs for trackGc cleanup.\n const target = new EventTarget()\n const regs: Reg[] = []\n\n Object.defineProperty(target, 'addEventListener', {\n value: (eventType: string, listener: EventListenerOrEventListenerObject | null, options?: ListenerOpts) => {\n if (listener === null) return\n const fn = toListener(listener)\n const capture = typeof options === 'boolean' ? options : !!options?.capture\n if (findReg(regs, eventType, fn, capture)) return\n regs.push({ eventType, listener: fn, capture })\n addRpc(eventType, identity(fn), options).catch(() => {})\n },\n })\n\n Object.defineProperty(target, 'removeEventListener', {\n value: (eventType: string, listener: EventListenerOrEventListenerObject | null, options?: ListenerOpts) => {\n if (listener === null) return\n const fn = toListener(listener)\n const capture = typeof options === 'boolean' ? options : !!options?.capture\n const reg = findReg(regs, eventType, fn, capture)\n if (!reg) return\n regs.splice(regs.indexOf(reg), 1)\n removeRpc(eventType, identity(fn), { capture }).catch(() => {})\n },\n })\n\n // Cleanup must NOT close over `target` — otherwise the FR can never fire.\n trackGc(target, () => {\n for (const { eventType, listener, capture } of regs) {\n removeRpc(eventType, identity(listener), { capture }).catch(() => {})\n }\n regs.length = 0\n })\n\n return target\n}\n\nconst typeCheck = () => {\n const r = revive(box(new EventTarget(), {} as RevivableContext), {} as RevivableContext)\n const expected: EventTarget = r\n // @ts-expect-error - not a string\n const notString: string = r\n // @ts-expect-error - cannot box non-EventTarget\n box('not an event target', {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\nimport type { BoxedPromise } from './promise'\n\nimport { BoxBase } from './utils'\nimport { box as boxPromise, revive as revivePromise } from './promise'\n\nexport const type = 'blob' as const\n\nexport type BoxedBlob<T extends Blob = Blob> =\n & BoxBaseType<typeof type>\n & { mimeType: string }\n & { buffer: BoxedPromise<ArrayBuffer> }\n & { fileName?: string, lastModified?: number }\n & { [UnderlyingType]: Promise<T> }\n\n// File extends Blob and is handled here too — encoding `name` + `lastModified`\n// when present so the receiver reconstructs a File rather than dropping to a\n// plain Blob. Avoids a runtime/type mismatch where File would type-check as\n// Capable but silently coerce.\nexport const isType = (value: unknown): value is Blob =>\n value instanceof Blob\n\nconst isFile = (value: Blob): value is File =>\n typeof File !== 'undefined' && value instanceof File\n\nexport const box = <T extends Blob, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedBlob<T> => ({\n ...BoxBase,\n type,\n mimeType: value.type,\n buffer: boxPromise(value.arrayBuffer(), context),\n ...(isFile(value)\n ? { fileName: value.name, lastModified: value.lastModified }\n : {}),\n}) as unknown as BoxedBlob<T>\n\n// Blob bytes are fetched async (`blob.arrayBuffer()`), so revive can't\n// hand back a Blob synchronously — receivers `await` to get the Blob.\nexport const revive = <T extends BoxedBlob, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n revivePromise(value.buffer, context)\n .then(buffer =>\n value.fileName !== undefined && typeof File !== 'undefined'\n ? new File([buffer], value.fileName, {\n type: value.mimeType,\n lastModified: value.lastModified,\n })\n : new Blob([buffer], { type: value.mimeType })) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const boxed = box(new Blob(['x'], { type: 'text/plain' }), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Promise<Blob> = revived\n // @ts-expect-error - revived is Promise<Blob>, not a sync Blob\n const notBlob: Blob = revived\n // @ts-expect-error - cannot box non-Blob\n box('not a blob', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { boxByReference } from './identity'\n\nexport const type = 'symbol' as const\n\nexport const isType = (value: unknown): value is symbol =>\n typeof value === 'symbol'\n\nexport const box = <T extends symbol, T2 extends RevivableContext>(\n value: T,\n context: T2,\n) => {\n const box = {\n ...BoxBase,\n type,\n description: value.description,\n }\n return (\n value.description === undefined\n ? boxByReference(value, box, context)\n : box\n )\n}\n\nexport const revive = <T extends { description: string | undefined }, T2 extends RevivableContext>(\n value: T,\n _context: T2,\n): symbol => Symbol(value.description)\n\nconst typeCheck = () => {\n const boxed = box(Symbol('foo'), {} as RevivableContext)\n // Description-bearing symbols still round-trip through this revive.\n const revived = revive({ description: 'foo' }, {} as RevivableContext)\n const expected: symbol = revived\n // @ts-expect-error - not a string\n const notString: string = revived\n // @ts-expect-error - cannot box non-symbol\n box('not a symbol', {} as RevivableContext)\n}\n","import type { BoxBase as BoxBaseType, RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { instanceOfAny } from '../utils/type-guards'\n\ntype AnyCtor = abstract new (...args: any[]) => unknown\n\n// -------------------------------------------------------------------------\n// clonable — pass-through fast path for HTML structured-clone types not\n// owned by another revivable. Short-circuits findBoxModule so unclonable's\n// structuredClone probe never fires on a known-safe value.\n// -------------------------------------------------------------------------\n\nconst TYPED_CLONABLE_CTORS = [\n globalThis.File,\n globalThis.FileList,\n globalThis.RegExp,\n globalThis.DataView,\n globalThis.ImageData,\n globalThis.FormData,\n globalThis.DOMException,\n globalThis.DOMMatrix,\n globalThis.DOMMatrixReadOnly,\n globalThis.DOMPoint,\n globalThis.DOMPointReadOnly,\n globalThis.DOMQuad,\n globalThis.DOMRect,\n globalThis.DOMRectReadOnly,\n globalThis.CryptoKey,\n globalThis.FileSystemHandle,\n globalThis.FileSystemFileHandle,\n globalThis.FileSystemDirectoryHandle,\n globalThis.RTCCertificate,\n] as const\n\nconst EXPERIMENTAL_CLONABLE_CTORS = [\n (globalThis as { CropTarget?: AnyCtor }).CropTarget,\n (globalThis as { EncodedAudioChunk?: AnyCtor }).EncodedAudioChunk,\n (globalThis as { EncodedVideoChunk?: AnyCtor }).EncodedVideoChunk,\n (globalThis as { FencedFrameConfig?: AnyCtor }).FencedFrameConfig,\n (globalThis as { GPUCompilationInfo?: AnyCtor }).GPUCompilationInfo,\n (globalThis as { GPUCompilationMessage?: AnyCtor }).GPUCompilationMessage,\n (globalThis as { GPUPipelineError?: AnyCtor }).GPUPipelineError,\n (globalThis as { RTCEncodedAudioFrame?: AnyCtor }).RTCEncodedAudioFrame,\n (globalThis as { RTCEncodedVideoFrame?: AnyCtor }).RTCEncodedVideoFrame,\n (globalThis as { WebTransportError?: AnyCtor }).WebTransportError,\n] as const\n\nexport type Clonable = InstanceType<typeof TYPED_CLONABLE_CTORS[number]>\nexport type BoxedClonable = BoxBaseType<'clonable'>\n\n// `capableOnly: true` tells ExtractType to elide this module from the\n// Capable union on JSON transports — TS can't narrow `isType<Ctx>` via\n// generic inference, so we use a marker flag.\nconst isClonable = (value: unknown): value is Clonable =>\n instanceOfAny(value, TYPED_CLONABLE_CTORS) || instanceOfAny(value, EXPERIMENTAL_CLONABLE_CTORS)\n\nexport const clonable = {\n type: 'clonable',\n capableOnly: true,\n isType: isClonable,\n // Pass-through; structured-clone handles these on the wire. `revive` is\n // never reached — `box` returns the raw value so isRevivableBox is false.\n box: (value: Clonable, _context: RevivableContext<any>): Clonable => value,\n revive: (value: BoxedClonable, _context: RevivableContext<any>): Clonable => value as unknown as Clonable,\n} as const\n\n// -------------------------------------------------------------------------\n// transferable — pass-through fast path for transfer-only host objects.\n// getTransferableObjects pulls them out of the envelope at send time.\n// -------------------------------------------------------------------------\n\nconst TYPED_TRANSFERABLE_CTORS = [\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.MediaStreamTrack,\n globalThis.RTCDataChannel,\n] as const\n\nconst EXPERIMENTAL_TRANSFERABLE_CTORS = [\n (globalThis as { AudioData?: AnyCtor }).AudioData,\n (globalThis as { VideoFrame?: AnyCtor }).VideoFrame,\n (globalThis as { MediaSourceHandle?: AnyCtor }).MediaSourceHandle,\n (globalThis as { MIDIAccess?: AnyCtor }).MIDIAccess,\n (globalThis as { WebTransportReceiveStream?: AnyCtor }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: AnyCtor }).WebTransportSendStream,\n] as const\n\nexport type Transferable = InstanceType<typeof TYPED_TRANSFERABLE_CTORS[number]>\nexport type BoxedTransferable = BoxBaseType<'transferable'>\n\nconst isTransferable = (value: unknown): value is Transferable =>\n instanceOfAny(value, TYPED_TRANSFERABLE_CTORS) || instanceOfAny(value, EXPERIMENTAL_TRANSFERABLE_CTORS)\n\nexport const transferable = {\n type: 'transferable',\n capableOnly: true,\n isType: isTransferable,\n box: (value: Transferable, _context: RevivableContext<any>): Transferable => value,\n revive: (value: BoxedTransferable, _context: RevivableContext<any>): Transferable => value as unknown as Transferable,\n} as const\n\n// -------------------------------------------------------------------------\n// unclonable — catch-all that probes via structuredClone and coerces\n// unclonables to `{}` so the wire never blows up on exotic host objects.\n// -------------------------------------------------------------------------\n\nconst isPlainObject = (value: unknown): boolean => {\n if (value === null || typeof value !== 'object') return false\n const proto = Object.getPrototypeOf(value)\n return proto === Object.prototype || proto === null\n}\n\nconst isUnclonable = (value: unknown): boolean => {\n if (value === null) return false\n const t = typeof value\n if (t !== 'object') return false\n if (Array.isArray(value)) return false\n if (isPlainObject(value)) return false\n try {\n structuredClone(value)\n return false\n } catch {\n return true\n }\n}\n\nexport type BoxedUnclonable = BoxBaseType<'unclonable'>\n\n// Type-level lie: `value is never` so this module doesn't widen Capable.\n// Coercion to `{}` is a runtime rescue for values we shouldn't see.\nconst isUnclonableTyped = isUnclonable as (value: unknown) => value is never\n\nexport const unclonable = {\n type: 'unclonable',\n isType: isUnclonableTyped,\n box: (_value: never, _context: RevivableContext<any>): BoxedUnclonable => ({ ...BoxBase, type: 'unclonable' }),\n revive: (_value: BoxedUnclonable, _context: RevivableContext<any>): Record<string, never> => ({}),\n} as const\n","import type { BoxBase, RevivableContext } from './utils'\nimport type { DeepReplaceWithBox, DeepReplaceWithRevive, ReplaceWithBox, ReplaceWithRevive } from '../utils/replace'\nimport type { MessageFields, Capable } from '../types'\n\nimport { isRevivableBox } from './utils'\nimport * as arrayBuffer from './array-buffer'\nimport * as date from './date'\nimport * as headers from './headers'\nimport * as error from './error'\nimport * as typedArray from './typed-array'\nimport * as promise from './promise'\nimport * as func from './function'\nimport * as messagePort from './message-port'\nimport * as readableStream from './readable-stream'\nimport * as writableStream from './writable-stream'\nimport * as abortSignal from './abort-signal'\nimport * as response from './response'\nimport * as request from './request'\nimport * as identity from './identity'\nimport * as transfer from './transfer'\nimport * as map from './map'\nimport * as set from './set'\nimport * as bigInt from './bigint'\nimport * as event from './event'\nimport * as eventTarget from './event-target'\nimport * as blob from './blob'\nimport * as symbol from './symbol'\nimport { clonable, transferable, unclonable } from './fallbacks'\n\nexport { identity } from './identity'\nexport { transfer } from './transfer'\n\nexport * from './utils'\n\n// `any` on box/revive/init: each module's concrete box has a narrower input\n// than the shared interface can express, and TS treats readonly function\n// types contravariantly. The bivariance escape hatch lets modules assign.\nexport type RevivableModule<\n T extends string = string,\n T2 = any,\n T3 extends BoxBase<T> = any,\n T4 extends MessageFields = MessageFields,\n> = {\n readonly type: T\n readonly isType: (value: unknown) => value is T2\n readonly box: ((value: T2, context: RevivableContext<any>) => T3) | ((...args: any[]) => any)\n readonly revive: (value: T3, context: RevivableContext<any>) => T2\n readonly init?: (context: RevivableContext<any>) => void\n readonly Messages?: T4\n}\n\nexport const defaultRevivableModules = [\n transfer,\n identity,\n arrayBuffer,\n date,\n headers,\n error,\n typedArray,\n // blob MUST come before clonable — clonable would otherwise pass-through\n // a Blob unboxed, which works on clone transports but loses the data on\n // JSON. Blob's isType excludes File so File still rides clonable.\n blob,\n promise,\n func,\n messagePort,\n readableStream,\n writableStream,\n abortSignal,\n response,\n request,\n map,\n set,\n bigInt,\n symbol,\n event,\n // eventTarget MUST be last among instanceof-EventTarget revivables —\n // MessagePort/AbortSignal/Window/Worker all extend EventTarget; the\n // specific ones need first dibs via findBoxModule iteration order.\n eventTarget,\n // Pass-through fast paths for wire-safe types — short-circuit findBoxModule\n // before unclonable's structuredClone probe runs on a known-safe value.\n clonable,\n transferable,\n // Catch-all: structuredClone-probes and coerces unclonables to `{}`,\n // matching JSON.stringify(new WeakMap()) === \"{}\".\n unclonable,\n] as const\n\nexport type DefaultRevivableModules = typeof defaultRevivableModules\nexport type DefaultRevivableModule = DefaultRevivableModules[number]\n\nconst findBoxModule = (\n value: unknown,\n modules: readonly RevivableModule[]\n): RevivableModule | undefined =>\n modules.find(module => module.isType(value))\n\nconst findReviveModule = (\n value: BoxBase,\n modules: readonly RevivableModule[],\n): RevivableModule | undefined =>\n modules.find(module => module.type === value.type)\n\nconst isPlainObject = (value: unknown): value is Record<string, Capable> =>\n !!value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype\n\nconst descend = <TOut>(value: unknown, transform: (v: Capable) => unknown): TOut => {\n if (Array.isArray(value)) {\n return value.map(v => transform(v)) as TOut\n }\n if (isPlainObject(value)) {\n return Object.fromEntries(\n Object.entries<Capable>(value).map(([k, v]) => [k, transform(v)]),\n ) as TOut\n }\n return value as TOut\n}\n\nexport const box = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): ReplaceWithBox<T, TModules[number]> => {\n const handledByModule = findBoxModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.box(value, context) as ReplaceWithBox<T, TModules[number]>\n }\n return value as ReplaceWithBox<T, TModules[number]>\n}\n\nexport const recursiveBox = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): DeepReplaceWithBox<T, TModules[number]> => {\n type ReturnCastType = DeepReplaceWithBox<T, TModules[number]>\n // Already-boxed values pass through — revivables may embed a pre-built\n // BoxedX in their outgoing payload; descending would re-box raw ports.\n if (isRevivableBox(value)) return value as ReturnCastType\n const handledByModule = findBoxModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.box(value, context) as ReturnCastType\n }\n return descend<ReturnCastType>(value, v => recursiveBox(v, context))\n}\n\nexport const revive = <\n T extends ReturnType<typeof box>,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): ReplaceWithRevive<T, TModules[number]> => {\n if (!isRevivableBox(value)) return value as ReplaceWithRevive<T, TModules[number]>\n const handledByModule = findReviveModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.revive(value, context) as ReplaceWithRevive<T, TModules[number]>\n }\n return value as ReplaceWithRevive<T, TModules[number]>\n}\n\nexport const recursiveRevive = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): DeepReplaceWithRevive<T, TModules[number]> => {\n type ReturnCastType = DeepReplaceWithRevive<T, TModules[number]>\n if (isRevivableBox(value)) {\n const handledByModule = findReviveModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.revive(value, context) as ReturnCastType\n }\n }\n return descend<ReturnCastType>(value, v => recursiveRevive(v, context))\n}","import type { Transport } from '../utils/transport'\nimport type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { DeepReplaceWithBox } from '../utils/replace'\nimport type { ProtocolContext } from './utils'\nimport type {\n Capable, MessageEventTarget, MessageFields,\n MessageVariant, Uuid,\n} from '../types'\n\nimport { recursiveBox, recursiveRevive } from '../revivables'\nimport { isEmitTransport, isReceiveTransport } from '../utils/type-guards'\n\nexport const type = 'bidirectional' as const\n\nexport type InitMessage<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> = {\n type: 'init'\n remoteUuid: Uuid\n data: DeepReplaceWithBox<T, TModules[number]>\n}\n\nexport declare const Messages: <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n>(modules: TModules, value: T) =>\n | InitMessage<TModules, T>\n\nexport type Messages<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> = ReturnType<typeof Messages<TModules, T>>\n\nexport type ConnectionContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n type: 'bidirectional'\n eventTarget: MessageEventTarget<TModules>\n connection: BidirectionalConnection<TModules>\n}\n\nexport type ConnectionRevivableContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n remoteUuid: Uuid\n sendMessage: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n eventTarget: MessageEventTarget<TModules>\n}\n\nexport const startBidirectionalConnection = <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n>(\n { transport, value, remoteUuid, eventTarget, send, revivableModules }:\n {\n transport: Transport\n value: Capable<TModules>\n remoteUuid: Uuid\n eventTarget: MessageEventTarget<TModules>\n send: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n },\n) => {\n const revivableContext = {\n transport,\n remoteUuid,\n sendMessage: send,\n eventTarget,\n revivableModules\n } satisfies ConnectionRevivableContext<TModules>\n\n for (const module of revivableModules) {\n module.init?.(revivableContext)\n }\n\n const { promise, resolve } = Promise.withResolvers<InitMessage<TModules>['data']>()\n\n eventTarget.addEventListener('message', function listener ({ detail }) {\n if (detail.type === 'init') {\n resolve(detail.data)\n eventTarget.removeEventListener('message', listener)\n }\n })\n\n send({\n type: 'init',\n remoteUuid,\n data: recursiveBox(value, revivableContext)\n })\n\n return {\n revivableContext,\n remoteValue:\n promise\n .then(initData => recursiveRevive(initData, revivableContext) as Capable),\n }\n}\n\nexport type BidirectionalConnection<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n revivableContext: ConnectionRevivableContext<TModules>\n remoteValue: Promise<Capable>\n}\n\n/** Mounts bidirectional mode on the shared protocol context. Only active\n * when the transport can both emit and receive. */\nexport const init = <TModules extends readonly RevivableModule[]>(\n ctx: ProtocolContext<TModules>\n): void => {\n if (!(isEmitTransport(ctx.transport) && isReceiveTransport(ctx.transport))) return\n\n ctx.protocolEventTarget.addEventListener('message', ({ detail: message }) => {\n if (message.type === 'announce') {\n if (!message.remoteUuid) {\n ctx.sendMessage({ type: 'announce', remoteUuid: message.uuid })\n return\n }\n if (message.remoteUuid !== ctx.getUuid()) return\n // Already-tracked uuid is the normal handshake-echo (peer re-announcing\n // back after our reply), not a collision — drop it.\n if (ctx.connectionContexts.has(message.uuid)) return\n // Echo announce back in case the peer missed our initial one.\n ctx.sendMessage({ type: 'announce', remoteUuid: message.uuid })\n const eventTarget = ctx.createConnectionEventTarget()\n const connectionContext = {\n type: 'bidirectional',\n eventTarget,\n connection:\n startBidirectionalConnection<TModules>({\n transport: ctx.transport,\n value: ctx.value,\n remoteUuid: message.uuid,\n eventTarget,\n send: (m) => ctx.sendMessage(m as MessageVariant),\n revivableModules: ctx.revivableModules\n })\n } satisfies ConnectionContext<TModules>\n ctx.connectionContexts.set(message.uuid, connectionContext)\n connectionContext.connection.remoteValue.then((remoteValue) =>\n ctx.resolveRemoteValue(remoteValue)\n )\n return\n }\n if (message.type === 'close') {\n if (message.remoteUuid !== ctx.getUuid()) return\n ctx.connectionContexts.delete(message.uuid)\n return\n }\n // \"init\" | \"message\" | \"message-port-close\"\n if (message.remoteUuid !== ctx.getUuid()) return\n const connection = ctx.connectionContexts.get(message.uuid)\n // drop messages from peers we haven't tracked (pre-announce or post-close)\n if (!connection) return\n connection.eventTarget.dispatchEvent(\n new CustomEvent('message', { detail: message })\n )\n })\n\n if (ctx.presetRemoteUuid !== undefined) {\n const eventTarget = ctx.createConnectionEventTarget()\n const connectionContext = {\n type: 'bidirectional',\n eventTarget,\n connection:\n startBidirectionalConnection<TModules>({\n transport: ctx.transport,\n value: ctx.value,\n remoteUuid: ctx.presetRemoteUuid,\n eventTarget,\n send: (m) => ctx.sendMessage(m as MessageVariant),\n revivableModules: ctx.revivableModules\n })\n } satisfies ConnectionContext<TModules>\n ctx.connectionContexts.set(ctx.presetRemoteUuid, connectionContext)\n connectionContext.connection.remoteValue.then((remoteValue) =>\n ctx.resolveRemoteValue(remoteValue)\n )\n return\n }\n\n ctx.sendMessage({ type: 'announce' })\n}\n","import type { UnderlyingType } from './type'\n\nexport type EventMap = Record<string, Event>\n\nexport interface TypedEventTarget<T extends EventMap> extends EventTarget {\n [UnderlyingType]?: T\n\n addEventListener<K extends keyof T & string>(\n type: K,\n listener: ((event: T[K]) => void) | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void\n\n removeEventListener<K extends keyof T & string>(\n type: K,\n listener: ((event: T[K]) => void) | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void\n}\n\n/**\n * Create a new `TypedEventTarget<T>` for a given event map. Centralises the\n * `EventTarget` → `TypedEventTarget<T>` cast so individual call sites don't\n * each need their own (`EventTarget` lacks the generic event map at the type\n * level, but the runtime behaviour is identical).\n */\nexport const createTypedEventTarget = <T extends EventMap>(): TypedEventTarget<T> =>\n new EventTarget() as TypedEventTarget<T>\n","import { transfer } from '../revivables/transfer'\nimport { isRevivableBox } from '../revivables/utils'\nimport { instanceOfAny, isClonable, isTransferable } from './type-guards'\n\nexport { transfer }\n\n// Must-transfer types: structured clone can't copy these, so any occurrence\n// in the outgoing message must go on the transfer list — opt-in or not.\n// (MessagePort is the canonical case: cloning would leave the peer mute.)\nconst isMustTransfer = (value: unknown): value is Transferable =>\n instanceOfAny(value, [\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.OffscreenCanvas,\n (globalThis as { MediaSourceHandle?: abstract new (...args: any[]) => unknown }).MediaSourceHandle,\n (globalThis as { MediaStreamTrack?: abstract new (...args: any[]) => unknown }).MediaStreamTrack,\n (globalThis as { MIDIAccess?: abstract new (...args: any[]) => unknown }).MIDIAccess,\n (globalThis as { RTCDataChannel?: abstract new (...args: any[]) => unknown }).RTCDataChannel,\n (globalThis as { WebTransportReceiveStream?: abstract new (...args: any[]) => unknown }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: abstract new (...args: any[]) => unknown }).WebTransportSendStream,\n ])\n\n// Structural check — keeps the walker decoupled from the module graph.\n// `degraded` (set by transfer.box) means the wrapper is a no-op here.\nconst isTransferBox = (value: unknown): value is { inner: unknown, degraded: boolean } =>\n isRevivableBox(value) && value.type === 'transfer'\n\n/** Walk a boxed message and collect Transferables to move (rather than copy)\n * on postMessage:\n * 1. Must-transfer types are always included.\n * 2. Clonable types (SharedArrayBuffer) are skipped.\n * 3. Other Transferables are included only inside a non-degraded transfer\n * box (user opted in AND the platform supports transferring). */\nexport const getTransferableObjects = (value: unknown): Transferable[] => {\n const transferables: Transferable[] = []\n const seen = new WeakSet<object>()\n\n const recurse = (value: unknown, inTransferBox: boolean): void => {\n if (!value || typeof value !== 'object') return\n if (seen.has(value)) return\n seen.add(value)\n\n if (isClonable(value)) return\n\n if (isTransferBox(value)) {\n // Non-degraded box flips into transfer mode for everything below.\n recurse(value.inner, inTransferBox || !value.degraded)\n return\n }\n\n if (isMustTransfer(value)) {\n transferables.push(value)\n return\n }\n\n if (isTransferable(value)) {\n if (inTransferBox) {\n transferables.push(value)\n }\n return\n }\n\n // TypedArray / DataView expose every numeric index — iterating a 100 KB\n // buffer would walk 100 K entries for nothing. The underlying buffer is\n // the only candidate; the typed-array revivable handles that path.\n if (ArrayBuffer.isView(value)) return\n\n if (Array.isArray(value)) {\n for (const item of value) recurse(item, inTransferBox)\n return\n }\n\n for (const item of Object.values(value)) recurse(item, inTransferBox)\n }\n\n recurse(value, false)\n return transferables\n}\n","import type {\n Message, MessageVariant, Uuid,\n Capable, MessageEventMap\n} from '../types'\nimport type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { Transport } from '../utils/transport'\nimport type { ConnectionContext } from '.'\nimport type { TypedEventTarget } from '../utils/typed-event-target'\n\nimport { defaultRevivableModules } from '../revivables'\nimport { isJsonOnlyTransport, isCustomTransport } from '../utils/type-guards'\n\nexport const normalizeTransport = (transport: Transport): Transport => {\n const isJson =\n 'isJson' in transport && transport.isJson !== undefined\n ? transport.isJson\n : isJsonOnlyTransport(transport)\n const ports =\n isCustomTransport(transport)\n ? transport\n : { emit: transport, receive: transport }\n return { isJson, ...ports } satisfies Transport\n}\n\n/** Resolves the final revivable module list. The user supplies a function\n * that takes the defaults and returns whatever ordering/composition they\n * want — add modules, drop defaults, reorder, override per-type. When\n * omitted, the defaults are used as-is. */\nexport const mergeRevivableModules = <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n>(\n configure: ((defaults: DefaultRevivableModules) => TModules) | undefined,\n): TModules =>\n configure\n ? configure(defaultRevivableModules)\n : defaultRevivableModules as unknown as TModules\n\nexport type ProtocolEventMap<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n message: CustomEvent<Message<TModules>>\n}\n\nexport type ProtocolEventTarget<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = TypedEventTarget<ProtocolEventMap<TModules>>\n\nexport type ProtocolContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n value: Capable<TModules>\n revivableModules: TModules\n connectionContexts: Map<string, ConnectionContext<TModules>>\n getUuid: () => Uuid\n presetRemoteUuid?: Uuid\n sendMessage: (message: MessageVariant) => void\n protocolEventTarget: ProtocolEventTarget<TModules>\n resolveRemoteValue: (value: Capable<TModules>) => void\n createConnectionEventTarget: () => TypedEventTarget<MessageEventMap<TModules>>\n}\n\nexport type StartConnectionsOptions<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n name?: string\n remoteName?: string\n key?: string\n origin?: string\n unregisterSignal?: AbortSignal\n /** Configure the revivable module list. Receives the defaults and\n * returns the final ordered list — add modules, drop defaults, reorder,\n * or override per-type as needed. */\n revivableModules?: (defaults: DefaultRevivableModules) => TModules\n uuid?: Uuid\n remoteUuid?: Uuid\n}\n","import type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { ConnectionContext as BidirectionalConnectionContext } from './bidirectional'\nimport type {\n ProtocolContext,\n StartConnectionsOptions,\n} from '../utils'\nimport type {\n Message, MessageVariant, Uuid,\n Capable,\n} from '../types'\nimport type { MessageContext } from '../utils/transport'\n\nimport { OSRA_DEFAULT_KEY, OSRA_KEY } from '../types'\nimport * as bidirectional from './bidirectional'\nimport {\n isEmitTransport,\n isReceiveTransport,\n} from '../utils/type-guards'\nimport { createTypedEventTarget } from '../utils/typed-event-target'\nimport { getTransferableObjects } from '../utils/transferable'\nimport { registerOsraMessageListener, sendOsraMessage } from '../utils/transport'\nimport { mergeRevivableModules, normalizeTransport } from './utils'\n\nexport * from './bidirectional'\nexport * from './utils'\n\nexport type ConnectionModule<T> = {\n readonly type: string\n // ProtocolContext<any> rather than ProtocolContext<readonly RevivableModule[]>\n // for the same bivariance reason as RevivableModule.box — concrete modules\n // declare narrower context generics than the shared interface can express.\n readonly init: (ctx: ProtocolContext<any>) => void\n readonly Messages?: T\n}\n\nexport const connections = [\n bidirectional\n] as const\n\nexport type DefaultConnectionModules = typeof connections\nexport type DefaultConnectionModule = DefaultConnectionModules[number]\n\nexport type ConnectionMessage<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> =\n DefaultConnectionModule extends {\n Messages: (modules: TModules, value: T) => infer R\n }\n ? R\n : never\n\nexport type ConnectionContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n | BidirectionalConnectionContext<TModules>\n\nexport const startConnections = <\n T = unknown,\n const TModules extends readonly RevivableModule[] = DefaultRevivableModules\n>(\n value: Capable<TModules>,\n {\n transport: _transport,\n name,\n remoteName,\n key = OSRA_DEFAULT_KEY,\n origin = '*',\n unregisterSignal,\n revivableModules: configureRevivableModules,\n uuid: _uuid,\n remoteUuid: presetRemoteUuid,\n }: StartConnectionsOptions<TModules>\n): Promise<T> => {\n const transport = normalizeTransport(_transport)\n const mergedRevivableModules = mergeRevivableModules<TModules>(configureRevivableModules)\n type MergedModules = typeof mergedRevivableModules\n const connectionContexts = new Map<string, ConnectionContext<MergedModules>>()\n\n const { promise: remoteValuePromise, resolve: resolveRemoteValue } =\n Promise.withResolvers<Capable<MergedModules>>()\n\n const uuid: Uuid = _uuid ?? globalThis.crypto.randomUUID()\n\n const sendMessage = (message: MessageVariant) => {\n if (unregisterSignal?.aborted) return\n if (!isEmitTransport(transport)) return\n const envelope = { [OSRA_KEY]: key, name, uuid, ...message }\n sendOsraMessage(transport, envelope, origin, getTransferableObjects(envelope))\n }\n\n const protocolEventTarget = createTypedEventTarget<{ message: CustomEvent<Message<MergedModules>> }>()\n\n const ctx: ProtocolContext<MergedModules> = {\n transport,\n value: value as Capable<MergedModules>,\n revivableModules: mergedRevivableModules,\n connectionContexts,\n getUuid: () => uuid,\n presetRemoteUuid,\n sendMessage,\n protocolEventTarget,\n resolveRemoteValue,\n createConnectionEventTarget: createTypedEventTarget,\n }\n\n const listener = (message: Message, _: MessageContext) => {\n // own message looped back on the channel\n if (message.uuid === uuid) return\n protocolEventTarget.dispatchEvent(\n new CustomEvent('message', { detail: message as Message<MergedModules> }),\n )\n }\n\n if (isReceiveTransport(transport)) {\n registerOsraMessageListener({\n listener,\n transport,\n remoteName,\n key,\n unregisterSignal\n })\n }\n\n for (const connectionModule of connections) {\n connectionModule.init(ctx)\n }\n\n return remoteValuePromise as Promise<T>\n}\n","import type { Capable } from './types'\nimport type { DefaultRevivableModules, RevivableContext } from './revivables'\nimport type { RevivableModule } from './revivables'\nimport type { StartConnectionsOptions } from './connections/utils'\nimport type { Transport } from './utils/transport'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from './utils/capable-check'\n\nimport { startConnections } from './utils'\n\nexport * from './types'\nexport * from './revivables'\nexport * from './connections'\nexport * from './utils'\n\n/** Synthetic context so `Capable` can narrow on the inferred transport\n * without an actual context object at the call site. Only `transport`\n * matters; the rest is stubbed with the broadest types. */\ntype ContextOf<TTransport extends Transport> = RevivableContext & { transport: TTransport }\n\ntype CapableCheck<\n T,\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n Ctx extends RevivableContext = RevivableContext,\n> =\n T extends Capable<TModules, Ctx>\n ? T\n : T & {\n [ErrorMessage]: 'Value type must resolve to a Capable'\n [BadValue]: BadFieldValue<T, Capable<TModules, Ctx>>\n [Path]: BadFieldPath<T, Capable<TModules, Ctx>>\n [ParentObject]: BadFieldParent<T, Capable<TModules, Ctx>>\n }\n\nexport const expose = async <\n T = unknown,\n const TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n const TTransport extends Transport = Transport,\n const TValue = Capable<TModules, ContextOf<TTransport>>\n>(\n value: CapableCheck<TValue, TModules, ContextOf<TTransport>>,\n options: StartConnectionsOptions<TModules> & { transport: TTransport }\n): Promise<T> =>\n startConnections<T, TModules>(\n value as Capable<TModules>,\n options\n )\n"],"mappings":";;;;;;;;GAQa,IAAW,gBACX,IAAmB,wBACnB,IAAW,gBCMlB,IAAoB,WAAsD,cAE1E,IAA+B;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA,cAAc;CACd;CACA;CACA;CACA;CACD,EAMK,IAAyB,OAAO,OAAO,EAA6B,EAE7D,KAAoB,MAAsC;CACrE,IAAM,IAAO,EAAM,YAAY;AAC/B,KAAI,KAAQ,EAA8B,QAAO;AAGjD,MAAK,IAAM,CAAC,GAAc,MAAS,OAAO,QAAQ,EAA6B,CAC7E,KAAI,KAAQ,aAAiB,EAAM,QAAO;AAE5C,OAAU,MAAM,2BAA2B;GAGhC,KAAyC,MAAiD;CACrG,IAAM,IAAO,EAA6B;AAC1C,KAAI,CAAC,EAAM,OAAU,MAAM,2BAA2B;AACtD,QAAO;GAGI,KAAgB,MAC3B,EAAuB,MAAK,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ACA,SAAM,GACN,YAAS,KACT,qBACA,kBAAkB,GAClB,MAAM,GACN,YAAY,QAEC;CACf,IAAM,IAAY,GAAmB,EAAW,EAC1C,IAAyB,GAAgC,EAA0B,EAEnF,oBAAqB,IAAI,KAA+C,EAExE,EAAE,SAAS,GAAoB,SAAS,MAC5C,QAAQ,eAAuC,EAE3C,IAAa,KAAS,WAAW,OAAO,YAAY,EAEpD,KAAe,MAA4B;AAE/C,MADI,GAAkB,WAClB,CAAC,EAAgB,EAAU,CAAE;EACjC,IAAM,IAAW;IAAG,IAAW;GAAK;GAAM;GAAM,GAAG;GAAS;AAC5D,KAAgB,GAAW,GAAU,GAAQ,EAAuB,EAAS,CAAC;IAG1E,IAAsB,IAA0E,EAEhG,IAAsC;EAC1C;EACO;EACP,kBAAkB;EAClB;EACA,eAAe;EACf;EACA;EACA;EACA;EACA,6BAA6B;EAC9B;AAUD,CAAI,EAAmB,EAAU,IAC/B,GAA4B;EAC1B,WAVc,GAAkB,MAAsB;AAEpD,KAAQ,SAAS,KACrB,EAAoB,cAClB,IAAI,YAAY,WAAW,EAAE,QAAQ,GAAmC,CAAC,CAC1E;;EAMC;EACA;EACA;EACA;EACD,CAAC;AAGJ,MAAK,IAAM,KAAoB,GAC7B,GAAiB,KAAK,EAAI;AAG5B,QAAO;GC5FI,KAAS,OAMpB,GACA,MAEA,GACE,GACA,EACD"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/types.ts","../src/utils/type-guards.ts","../src/utils/transport.ts","../src/revivables/utils.ts","../src/revivables/array-buffer.ts","../src/revivables/date.ts","../src/revivables/headers.ts","../src/revivables/error.ts","../src/revivables/typed-array.ts","../src/utils/event-channel.ts","../src/utils/gc-tracker.ts","../src/revivables/message-port.ts","../src/revivables/promise.ts","../src/revivables/function.ts","../src/revivables/readable-stream.ts","../src/revivables/writable-stream.ts","../src/revivables/abort-signal.ts","../src/revivables/response.ts","../src/revivables/request.ts","../src/revivables/identity.ts","../src/revivables/transfer.ts","../src/revivables/map.ts","../src/revivables/set.ts","../src/revivables/bigint.ts","../src/revivables/event.ts","../src/revivables/event-target.ts","../src/revivables/blob.ts","../src/revivables/symbol.ts","../src/revivables/fallbacks.ts","../src/revivables/index.ts","../src/connections/bidirectional.ts","../src/utils/typed-event-target.ts","../src/utils/transferable.ts","../src/connections/utils.ts","../src/connections/relay.ts","../src/connections/index.ts","../src/index.ts"],"sourcesContent":["import type { ConnectionMessage } from './connections'\nimport type { TypedEventTarget } from './utils'\nimport type { IsJsonOnlyTransport } from './utils/type-guards'\nimport type {\n DefaultRevivableModules, RevivableModule,\n InferMessages, InferRevivables, RevivableContext\n} from './revivables'\n\nexport const OSRA_KEY = '__OSRA_KEY__' as const\nexport const OSRA_DEFAULT_KEY = '__OSRA_DEFAULT_KEY__' as const\nexport const OSRA_BOX = '__OSRA_BOX__' as const\n\nexport type Uuid = `${string}-${string}-${string}-${string}-${string}`\n\nexport type Jsonable =\n | boolean\n | null\n | number\n | string\n | { [key: string]: Jsonable }\n | Array<Jsonable>\n\nexport type Structurable =\n | Jsonable\n /** not really structureable but here for convenience */\n | void\n | undefined\n | bigint\n | Date\n | RegExp\n | Blob\n | File\n | FileList\n | ArrayBuffer\n | ArrayBufferView\n | ImageBitmap\n | ImageData\n | { [key: string]: Structurable }\n | Array<Structurable>\n | Map<Structurable, Structurable>\n | Set<Structurable>\n\nexport type StructurableTransferable =\n | Structurable\n | Transferable\n | { [key: string]: StructurableTransferable }\n | Array<StructurableTransferable>\n | Map<StructurableTransferable, StructurableTransferable>\n | Set<StructurableTransferable>\n\n/** \"Free\" types in `Capable` — narrows to `Jsonable` on JSON transports so\n * user code can't type a `Date`/`Blob`/etc. that JSON would silently coerce.\n * Modules that DO support JSON (date, map, set, bigint, …) put their type\n * back via `InferRevivables`. */\ntype CapableBase<Ctx extends RevivableContext> =\n IsJsonOnlyTransport<Ctx['transport']> extends true\n ? Jsonable | undefined | void\n : StructurableTransferable\n\nexport type Capable<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n Ctx extends RevivableContext = RevivableContext,\n> =\n | CapableBase<Ctx>\n | InferRevivables<TModules, Ctx>\n | { [key: string]: Capable<TModules, Ctx> }\n | Array<Capable<TModules, Ctx>>\n | Map<Capable<TModules, Ctx>, Capable<TModules, Ctx>>\n | Set<Capable<TModules, Ctx>>\n\nexport type MessageFields = {\n type: string\n remoteUuid: Uuid\n}\n\nexport type MessageBase = {\n [OSRA_KEY]: string\n /** UUID of the client that sent the message */\n uuid: Uuid\n name?: string\n}\n\nexport type ProtocolMessage =\n | { type: 'announce', remoteUuid?: Uuid }\n | { type: 'close', remoteUuid: Uuid }\n\nexport type MessageVariant<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n | ProtocolMessage\n | ConnectionMessage<TModules>\n | InferMessages<TModules>\n\nexport type Message<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n & MessageBase\n & MessageVariant<TModules>\n\nexport type MessageEventMap<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n message: CustomEvent<Message<TModules>>\n}\n\nexport type MessageEventTarget<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n > = TypedEventTarget<MessageEventMap<TModules>>\n","import type { Runtime } from 'webextension-polyfill'\nimport type { Message } from '../types'\nimport type {\n CustomEmitTransport, CustomReceiveTransport,\n CustomTransport, EmitJsonPlatformTransport,\n EmitTransport, JsonPlatformTransport,\n ReceiveJsonPlatformTransport,\n ReceiveTransport, Transport\n} from './transport'\n\nimport { OSRA_KEY } from '../types'\nimport { getWebExtensionRuntime } from './transport'\n\n// Pulled from globalThis so module evaluation doesn't crash on platforms\n// that haven't shipped Float16Array yet (Node ≤ 23, Chrome ≤ 134, Firefox\n// ≤ 132). Platforms without it just don't round-trip Float16 values.\nconst Float16ArrayCtor = (globalThis as { Float16Array?: typeof Float16Array }).Float16Array\n\nconst typedArrayConstructorsByName = {\n Int8Array,\n Uint8Array,\n Uint8ClampedArray,\n Int16Array,\n Uint16Array,\n Int32Array,\n Uint32Array,\n Float16Array: Float16ArrayCtor,\n Float32Array,\n Float64Array,\n BigInt64Array,\n BigUint64Array,\n} as const\n\nexport type TypedArrayType = keyof typeof typedArrayConstructorsByName\nexport type TypedArrayConstructor = NonNullable<(typeof typedArrayConstructorsByName)[TypedArrayType]>\nexport type TypedArray = InstanceType<TypedArrayConstructor>\n\nconst typedArrayConstructors = Object.values(typedArrayConstructorsByName)\n\nexport const typedArrayToType = (value: TypedArray): TypedArrayType => {\n const name = value.constructor.name as TypedArrayType\n if (name in typedArrayConstructorsByName) return name\n // Subclasses (e.g. Node's Buffer extends Uint8Array). Find the nearest\n // TypedArray ancestor by walking the prototype chain.\n for (const [ancestorName, ctor] of Object.entries(typedArrayConstructorsByName)) {\n if (ctor && value instanceof ctor) return ancestorName as TypedArrayType\n }\n throw new Error('Unknown typed array type')\n}\n\nexport const typedArrayTypeToTypedArrayConstructor = (value: TypedArrayType): TypedArrayConstructor => {\n const ctor = typedArrayConstructorsByName[value]\n if (!ctor) throw new Error('Unknown typed array type')\n return ctor\n}\n\nexport const isTypedArray = (value: unknown): value is TypedArray =>\n typedArrayConstructors.some(ctor => !!ctor && value instanceof ctor)\nexport const isWebSocket = (value: unknown): value is WebSocket => value instanceof WebSocket\nexport const isServiceWorkerContainer = (value: unknown): value is ServiceWorkerContainer => !!globalThis.ServiceWorkerContainer && value instanceof ServiceWorkerContainer\nexport const isWorker = (value: unknown): value is Worker => !!globalThis.Worker && value instanceof Worker\n// @ts-expect-error DedicatedWorkerGlobalScope is only present in worker scopes\nexport const isDedicatedWorker = (value: unknown): value is DedicatedWorkerGlobalScope => !!globalThis.DedicatedWorkerGlobalScope && value instanceof DedicatedWorkerGlobalScope\nexport const isSharedWorker = (value: unknown): value is SharedWorker => !!globalThis.SharedWorker && value instanceof SharedWorker\nconst isMessagePort = (value: unknown): value is MessagePort => value instanceof MessagePort\n\nexport const isOsraMessage = (value: unknown): value is Message =>\n !!value\n && typeof value === 'object'\n && OSRA_KEY in value\n && !!value[OSRA_KEY]\n\ntype AnyConstructor = abstract new (...args: any[]) => unknown\n\n/** True if `value` is an instance of any of the given constructors.\n * Tolerates undefined entries (constructors missing on this platform). */\nexport const instanceOfAny = (value: unknown, ctors: readonly (AnyConstructor | undefined)[]): boolean => {\n for (const ctor of ctors) if (ctor && value instanceof ctor) return true\n return false\n}\n\nexport const isClonable = (value: unknown): boolean =>\n instanceOfAny(value, [globalThis.SharedArrayBuffer])\n\n// Types eligible for transfer when the user opts in via `transfer()`. Some\n// entries are also clonable (ArrayBuffer, ImageBitmap, …) — outside a\n// `transfer` box they fall back to clone.\nexport const isTransferable = (value: unknown): value is Transferable =>\n instanceOfAny(value, [\n globalThis.ArrayBuffer,\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n (globalThis as { AudioData?: abstract new (...args: any[]) => unknown }).AudioData,\n (globalThis as { VideoFrame?: abstract new (...args: any[]) => unknown }).VideoFrame,\n (globalThis as { MediaSourceHandle?: abstract new (...args: any[]) => unknown }).MediaSourceHandle,\n (globalThis as { MediaStreamTrack?: abstract new (...args: any[]) => unknown }).MediaStreamTrack,\n (globalThis as { MIDIAccess?: abstract new (...args: any[]) => unknown }).MIDIAccess,\n (globalThis as { RTCDataChannel?: abstract new (...args: any[]) => unknown }).RTCDataChannel,\n (globalThis as { WebTransportReceiveStream?: abstract new (...args: any[]) => unknown }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: abstract new (...args: any[]) => unknown }).WebTransportSendStream,\n ])\n\nexport type WebExtRuntime = typeof browser.runtime\nexport const isWebExtensionRuntime = (value: unknown): value is WebExtRuntime => {\n const runtime = getWebExtensionRuntime()\n if (!runtime) return false\n return value === runtime\n}\n\nexport type WebExtPort = ReturnType<WebExtRuntime['connect']> | Runtime.Port\nexport const isWebExtensionPort = (value: unknown, connectPort: boolean = false): value is WebExtPort => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('name' in value) || !('disconnect' in value) || !('postMessage' in value)) return false\n // these properties are only present on WebExtPorts created through runtime.connect()\n if (!connectPort) return true\n return 'sender' in value && 'onMessage' in value && 'onDisconnect' in value\n}\n\nexport type WebExtSender = NonNullable<WebExtPort['sender']>\n\n// Structural guard. Can't distinguish onConnect from onMessage on its own —\n// they share this exact shape.\nconst hasListenerApi = (value: unknown): boolean =>\n !!value\n && typeof value === 'object'\n && !isWindow(value)\n && 'addListener' in value\n && 'hasListener' in value\n && 'removeListener' in value\n\n// Identity-compare against runtime.onConnect — structural checks can't\n// distinguish onConnect from onMessage, and misclassifying causes us to\n// treat each incoming message as a port and crash.\nexport type WebExtOnConnect = WebExtRuntime['onConnect']\nexport const isWebExtensionOnConnect = (value: unknown): value is WebExtOnConnect => {\n const runtime = getWebExtensionRuntime()\n if (!runtime) return false\n return value === runtime.onConnect || value === runtime.onConnectExternal\n}\n\nexport type WebExtOnMessage = WebExtRuntime['onMessage']\nexport const isWebExtensionOnMessage = (value: unknown): value is WebExtOnMessage =>\n hasListenerApi(value)\n\nexport const isWindow = (value: unknown): value is Window => {\n if (!value || typeof value !== 'object') return false\n try {\n return 'window' in value && value.window === value\n } catch {\n // Cross-origin Window access can throw SecurityError — fall back to a\n // shape probe over properties that don't trigger the security check.\n try {\n return 'closed' in value\n && typeof value.closed === 'boolean'\n && 'close' in value\n && typeof value.close === 'function'\n } catch {\n return false\n }\n }\n}\n\nexport const isEmitJsonOnlyTransport = (value: unknown): value is EmitJsonPlatformTransport =>\n isWebSocket(value)\n || isWebExtensionPort(value)\n || isWebExtensionRuntime(value)\n\nexport const isReceiveJsonOnlyTransport = (value: unknown): value is ReceiveJsonPlatformTransport =>\n isWebSocket(value)\n || isWebExtensionPort(value)\n || isWebExtensionOnConnect(value)\n || isWebExtensionOnMessage(value)\n || isWebExtensionRuntime(value)\n\nexport type IsJsonOnlyTransport<T extends Transport> = T extends JsonPlatformTransport ? true : false\nexport const isJsonOnlyTransport = (value: unknown): value is Extract<Transport, JsonPlatformTransport> =>\n (!!value && typeof value === 'object' && 'isJson' in value && value.isJson === true)\n || isEmitJsonOnlyTransport(value)\n || isReceiveJsonOnlyTransport(value)\n\nexport const isEmitTransport = (value: unknown): value is EmitTransport =>\n isWindow(value)\n || isEmitJsonOnlyTransport(value)\n || isServiceWorkerContainer(value)\n || isWorker(value)\n || isDedicatedWorker(value)\n || isSharedWorker(value)\n || isMessagePort(value)\n || isCustomEmitTransport(value)\n\nexport function assertEmitTransport(transport: Transport): asserts transport is EmitTransport {\n if (!isEmitTransport(transport)) throw new Error('Transport is not emitable')\n}\n\nexport const isReceiveTransport = (value: unknown): value is ReceiveTransport =>\n isWindow(value)\n || isReceiveJsonOnlyTransport(value)\n || isServiceWorkerContainer(value)\n || isWorker(value)\n || isDedicatedWorker(value)\n || isSharedWorker(value)\n || isMessagePort(value)\n || isCustomReceiveTransport(value)\n\nexport function assertReceiveTransport(transport: Transport): asserts transport is ReceiveTransport {\n if (!isReceiveTransport(transport)) throw new Error('Transport is not receiveable')\n}\n\nexport const isCustomEmitTransport = (value: unknown): value is CustomEmitTransport => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('emit' in value)) return false\n return isEmitTransport(value.emit) || typeof value.emit === 'function'\n}\n\nexport const isCustomReceiveTransport = (value: unknown): value is CustomReceiveTransport => {\n if (!value || typeof value !== 'object') return false\n // Prevent SecurityError when `value` is a cross-origin window.\n if (isWindow(value)) return false\n if (!('receive' in value)) return false\n return isReceiveTransport(value.receive) || typeof value.receive === 'function'\n}\n\nexport const isCustomTransport = (value: unknown): value is CustomTransport =>\n isCustomEmitTransport(value)\n || isCustomReceiveTransport(value)\n\nexport const isTransport = (value: unknown): value is Transport =>\n isEmitTransport(value)\n || isReceiveTransport(value)\n || isCustomTransport(value)\n || isJsonOnlyTransport(value)\n","import type { Message} from '../types'\nimport type {\n WebExtOnConnect, WebExtOnMessage,\n WebExtPort, WebExtRuntime, WebExtSender\n} from './type-guards'\n\nimport { OSRA_KEY } from '../types'\nimport {\n isOsraMessage, isCustomTransport,\n isWebExtensionOnConnect, isWebExtensionOnMessage,\n isWebExtensionPort, isWebExtensionRuntime, isWebSocket, isWindow, isSharedWorker\n} from './type-guards'\n\nexport type MessageContext = {\n port?: MessagePort | WebExtPort // WebExtension\n sender?: WebExtSender // WebExtension\n receiveTransport?: ReceivePlatformTransport\n source?: MessageEventSource | null // Window, Worker, WebSocket, ect...\n}\n\nexport type ReceiveHandler = (listener: (event: Message, messageContext: MessageContext) => void) => void\nexport type EmitHandler = (message: Message, transferables?: Transferable[]) => void\n\ntype CustomReceive = ReceivePlatformTransport | ReceiveHandler\ntype CustomEmit = EmitPlatformTransport | EmitHandler\n\nexport type CustomTransport =\n { isJson?: boolean }\n & (\n | { receive: CustomReceive, emit: CustomEmit }\n | { receive: CustomReceive }\n | { emit: CustomEmit }\n )\n\nexport type CustomEmitTransport = Extract<CustomTransport, { emit: any }>\nexport type CustomReceiveTransport = Extract<CustomTransport, { receive: any }>\n\nexport type EmitJsonPlatformTransport =\n | WebSocket\n | WebExtPort\n | WebExtRuntime\n\nexport type ReceiveJsonPlatformTransport =\n | WebSocket\n | WebExtPort\n | WebExtOnConnect\n | WebExtOnMessage\n | WebExtRuntime\n\nexport type JsonPlatformTransport =\n | { isJson: true }\n | EmitJsonPlatformTransport\n | ReceiveJsonPlatformTransport\n\ntype StructuredClonePlatformTransport =\n | Window\n | ServiceWorker\n | Worker\n | SharedWorker\n | MessagePort\n\nexport type EmitPlatformTransport =\n | EmitJsonPlatformTransport\n | StructuredClonePlatformTransport\n\nexport type ReceivePlatformTransport =\n | ReceiveJsonPlatformTransport\n | StructuredClonePlatformTransport\n\nexport type PlatformTransport =\n | EmitPlatformTransport\n | ReceivePlatformTransport\n\nexport type EmitTransport = EmitPlatformTransport & Extract<CustomTransport, { emit: any }>\nexport type ReceiveTransport = ReceivePlatformTransport & Extract<CustomTransport, { receive: any }>\n\nexport type Transport =\n | PlatformTransport\n | CustomTransport\n\nexport const getWebExtensionGlobal = () => globalThis.browser ?? globalThis.chrome\nexport const getWebExtensionRuntime = () => getWebExtensionGlobal()?.runtime\n\nexport const checkOsraMessageKey = (message: any, key: string): message is Message =>\n isOsraMessage(message)\n && message[OSRA_KEY] === key\n\nconst onAbort = (signal: AbortSignal | undefined, fn: () => void) =>\n signal?.addEventListener('abort', fn, { once: true })\n\nexport const registerOsraMessageListener = (\n { listener, transport, remoteName, key = OSRA_KEY, unregisterSignal }:\n {\n listener: (message: Message, messageContext: MessageContext) => void\n transport: ReceiveTransport\n remoteName?: string\n key?: string\n unregisterSignal?: AbortSignal\n }\n) => {\n const receiveTransport: Extract<CustomTransport, { receive: any }>['receive'] =\n isCustomTransport(transport) ? transport.receive : transport\n\n // Custom function handler\n if (typeof receiveTransport === 'function') {\n receiveTransport((message, ctx) => {\n if (!checkOsraMessageKey(message, key)) return\n if (remoteName && message.name !== remoteName) return\n listener(message, ctx)\n })\n return\n }\n\n // WebExtension family — subscribe to an `onMessage`-style listener.\n if (\n isWebExtensionRuntime(receiveTransport)\n || isWebExtensionPort(receiveTransport)\n || isWebExtensionOnConnect(receiveTransport)\n || isWebExtensionOnMessage(receiveTransport)\n ) {\n const listenOnWebExtOnMessage = (onMessage: WebExtOnMessage, port?: WebExtPort) => {\n const _listener = (message: object, sender?: WebExtSender) => {\n if (!checkOsraMessageKey(message, key)) return\n if (remoteName && message.name !== remoteName) return\n listener(message, { port, sender })\n }\n onMessage.addListener(_listener)\n onAbort(unregisterSignal, () => onMessage.removeListener(_listener))\n }\n\n if (isWebExtensionRuntime(receiveTransport)) {\n listenOnWebExtOnMessage(receiveTransport.onMessage)\n } else if (isWebExtensionOnConnect(receiveTransport)) {\n const _listener = (port: WebExtPort) =>\n listenOnWebExtOnMessage(port.onMessage as WebExtOnMessage, port)\n receiveTransport.addListener(_listener)\n onAbort(unregisterSignal, () => receiveTransport.removeListener(_listener))\n } else if (isWebExtensionOnMessage(receiveTransport)) {\n listenOnWebExtOnMessage(receiveTransport)\n } else { // WebExtPort\n listenOnWebExtOnMessage(receiveTransport.onMessage as WebExtOnMessage)\n }\n return\n }\n\n // Window, Worker, WebSocket, ServiceWorker, MessagePort, …\n const messageListener = (event: MessageEvent<Message>) => {\n if (!checkOsraMessageKey(event.data, key)) return\n if (remoteName && event.data.name !== remoteName) return\n listener(event.data, { receiveTransport, source: event.source })\n }\n receiveTransport.addEventListener('message', messageListener as EventListener)\n onAbort(unregisterSignal, () =>\n receiveTransport.removeEventListener('message', messageListener as EventListener),\n )\n}\n\nexport const sendOsraMessage = (\n transport: EmitTransport,\n message: Message,\n origin = '*',\n transferables: Transferable[] = []\n) => {\n const emitTransport: Extract<EmitTransport, { emit: any }>['emit'] =\n isCustomTransport(transport) ? transport.emit : transport\n\n if (typeof emitTransport === 'function') {\n emitTransport(message, transferables)\n } else if (isWindow(emitTransport)) {\n // Must check first — cross-origin windows throw on other property access.\n emitTransport.postMessage(message, origin, transferables)\n } else if (isWebExtensionPort(emitTransport)) {\n emitTransport.postMessage(message)\n } else if (isWebExtensionRuntime(emitTransport)) {\n emitTransport.sendMessage(message)\n } else if (isWebSocket(emitTransport)) {\n emitTransport.send(JSON.stringify(message))\n } else if (isSharedWorker(emitTransport)) {\n emitTransport.port.postMessage(message, transferables)\n } else { // MessagePort | ServiceWorker | Worker\n emitTransport.postMessage(message, transferables)\n }\n}\n","import type { DefaultRevivableModules, RevivableModule } from '.'\nimport type {\n MessageEventTarget,\n MessageFields,\n Uuid,\n} from '../types'\nimport type { Transport } from '../utils/transport'\nimport type { IsJsonOnlyTransport } from '../utils/type-guards'\n\nimport { OSRA_BOX } from '../types'\nimport { isJsonOnlyTransport } from '../utils/type-guards'\n\nexport type { UnderlyingType } from '../utils/type'\n\nexport const BoxBase = {\n [OSRA_BOX]: 'revivable',\n} as const\n\nexport type BoxBase<T extends string = string> =\n & typeof BoxBase\n & { type: T }\n\nexport type RevivableContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n remoteUuid: Uuid\n unregisterSignal?: AbortSignal\n /** Typed as a broad dispatcher so revivables can post their own message\n * variants without triggering contravariant function-parameter mismatches\n * across modules. The shape is enforced structurally via `MessageFields`. */\n sendMessage: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n eventTarget: MessageEventTarget<TModules>\n}\n\n/** Extract the type a module's `isType` narrows to. Modules marked\n * `capableOnly: true` (clonable, transferable) contribute `never` on JSON\n * transports so users can't type values JSON would silently drop. */\nexport type ExtractType<T, Ctx extends RevivableContext = RevivableContext> =\n T extends { capableOnly: true }\n ? IsJsonOnlyTransport<Ctx['transport']> extends true\n ? never\n : T extends { isType: (value: unknown) => value is infer S } ? S : never\n : T extends { isType: (value: unknown) => value is infer S } ? S : never\n\nexport type ExtractBox<T> =\n T extends { box: (...args: any[]) => infer B }\n ? B\n : never\n\nexport type ExtractMessages<T> =\n T extends { Messages?: infer B }\n ? B extends { type: string }\n ? string extends B['type'] ? never : B\n : never\n : never\n\nexport type InferMessages<TModules extends readonly unknown[]> =\n ExtractMessages<TModules[number]>\n\nexport type InferRevivables<\n TModules extends readonly unknown[],\n Ctx extends RevivableContext = RevivableContext,\n> =\n ExtractType<TModules[number], Ctx>\n\nexport type InferRevivableBox<TModules extends readonly unknown[]> =\n ExtractBox<TModules[number]>\n\nexport const isRevivableBox = (value: unknown): value is BoxBase =>\n !!value\n && typeof value === 'object'\n && OSRA_BOX in value\n && value[OSRA_BOX] === 'revivable'\n\nexport const serializeError = (error: unknown): string =>\n error instanceof Error ? (error.stack ?? String(error)) : String(error)\n\n/** Wire shape for an ArrayBuffer: base64 on JSON, raw on clone. */\nexport type BoxedBuffer<TCtx extends RevivableContext = RevivableContext> =\n IsJsonOnlyTransport<TCtx['transport']> extends true ? { base64Buffer: string }\n : IsJsonOnlyTransport<TCtx['transport']> extends false ? { arrayBuffer: ArrayBuffer }\n : { base64Buffer: string } | { arrayBuffer: ArrayBuffer }\n\nexport const boxBuffer = <TCtx extends RevivableContext>(\n buffer: ArrayBuffer,\n context: TCtx,\n): BoxedBuffer<TCtx> =>\n (isJsonOnlyTransport(context.transport)\n ? { base64Buffer: new Uint8Array(buffer).toBase64() }\n : { arrayBuffer: buffer }\n ) as BoxedBuffer<TCtx>\n\nexport const reviveBuffer = (boxed: { arrayBuffer: ArrayBuffer } | { base64Buffer: string }): ArrayBuffer =>\n 'arrayBuffer' in boxed\n ? boxed.arrayBuffer\n : Uint8Array.fromBase64(boxed.base64Buffer).buffer\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase, boxBuffer, reviveBuffer } from './utils'\n\nexport const type = 'arrayBuffer' as const\n\nexport const isType = (value: unknown): value is ArrayBuffer =>\n value instanceof ArrayBuffer\n\nexport const box = <T extends ArrayBuffer, T2 extends RevivableContext>(\n value: T,\n context: T2,\n) => ({\n ...BoxBase,\n type,\n ...boxBuffer(value, context),\n})\n\nexport const revive = <T extends ReturnType<typeof box>>(\n value: T,\n _context: RevivableContext,\n) => reviveBuffer(value)\n\nconst typeCheck = () => {\n const boxed = box(new ArrayBuffer(10), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ArrayBuffer = revived\n // @ts-expect-error - not an ArrayBuffer\n const notArrayBuffer: string = revived\n // @ts-expect-error - cannot box non-ArrayBuffer\n box('not an array buffer', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'date' as const\n\nexport const isType = (value: unknown): value is Date =>\n value instanceof Date\n\nexport const box = <T extends Date, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => ({\n ...BoxBase,\n type,\n ISOString: value.toISOString()\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => new Date(value.ISOString)\n\nconst typeCheck = () => {\n const boxed = box(new Date(), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Date = revived\n // @ts-expect-error - not a Date\n const notDate: string = revived\n // @ts-expect-error - cannot box non-Date\n box('not a date', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'headers' as const\n\nexport const isType = (value: unknown): value is Headers =>\n value instanceof Headers\n\nexport const box = <T extends Headers, T2 extends RevivableContext>(\n value: T,\n _context: T2\n) => ({\n ...BoxBase,\n type,\n entries: [...value.entries()]\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n _context: T2\n): Headers => {\n return new Headers(value.entries)\n}\n\nconst typeCheck = () => {\n const boxed = box(new Headers(), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Headers = revived\n // @ts-expect-error - not a Headers\n const notHeaders: string = revived\n // @ts-expect-error - cannot box non-Headers\n box('not a header', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'error' as const\n\nexport type BoxedError =\n & BoxBaseType<typeof type>\n & {\n name: string\n message: string\n stack: string\n cause?: Capable\n }\n\nexport const isType = (value: unknown): value is Error =>\n value instanceof Error\n\nexport const box = <T extends Error, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedError => {\n const hasCause = 'cause' in value && value.cause !== undefined\n return {\n ...BoxBase,\n type,\n name: value.name,\n message: value.message,\n stack: value.stack || value.toString(),\n ...(hasCause ? { cause: recursiveBox(value.cause as Capable, context) as Capable } : {}),\n }\n}\n\nexport const revive = <T extends BoxedError, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): Error => {\n const cause = value.cause !== undefined\n ? recursiveRevive(value.cause, context)\n : undefined\n const err = cause !== undefined\n ? new Error(value.message, { cause })\n : new Error(value.message)\n if (value.name) err.name = value.name\n if (value.stack) err.stack = value.stack\n return err\n}\n\nconst typeCheck = () => {\n const boxed = box(new Error('test'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Error = revived\n // @ts-expect-error - not an Error\n const notError: string = revived\n // @ts-expect-error - cannot box non-Error\n box('not an error', {} as RevivableContext)\n}\n","import type { RevivableContext, UnderlyingType, BoxedBuffer } from './utils'\nimport type { TypedArray, TypedArrayType } from '../utils/type-guards'\n\nimport { BoxBase, boxBuffer, reviveBuffer } from './utils'\nimport {\n isTypedArray,\n typedArrayToType,\n typedArrayTypeToTypedArrayConstructor,\n} from '../utils/type-guards'\n\nexport const type = 'typedArray' as const\n\ntype BoxedTypedArray<T extends TypedArray, T2 extends RevivableContext> =\n & typeof BoxBase\n & { type: typeof type }\n & { typedArrayType: TypedArrayType }\n & BoxedBuffer<T2>\n & { [UnderlyingType]: T }\n\nexport const isType = isTypedArray\n\nexport const box = <T extends TypedArray, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedTypedArray<T, T2> =>\n ({\n ...BoxBase,\n type,\n typedArrayType: typedArrayToType(value),\n ...boxBuffer(value.buffer as ArrayBuffer, context),\n }) as unknown as BoxedTypedArray<T, T2>\n\nexport const revive = <T extends BoxedTypedArray<TypedArray, RevivableContext>>(\n value: T,\n _context: RevivableContext,\n): T[UnderlyingType] =>\n new (typedArrayTypeToTypedArrayConstructor(value.typedArrayType))(reviveBuffer(value)) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const uint8Boxed = box(new Uint8Array(10), {} as RevivableContext)\n const uint8Revived = revive(uint8Boxed, {} as RevivableContext)\n const expectedUint8: Uint8Array = uint8Revived\n // @ts-expect-error - wrong typed array type\n const wrongType: Int32Array = uint8Revived\n\n const float32Boxed = box(new Float32Array(10), {} as RevivableContext)\n const float32Revived = revive(float32Boxed, {} as RevivableContext)\n const expectedFloat32: Float32Array = float32Revived\n // @ts-expect-error - wrong typed array type\n const wrongFloat: Uint8Array = float32Revived\n\n // @ts-expect-error - cannot box non-TypedArray\n box('not a typed array', {} as RevivableContext)\n}\n","import type { TypedMessagePort, TypedMessagePortEventMap } from './typed-message-channel'\n\nexport class EventPort<T> extends EventTarget {\n addEventListener<K extends keyof TypedMessagePortEventMap<T> & string>(\n type: K,\n listener: ((event: TypedMessagePortEventMap<T>[K]) => void) | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void {\n super.addEventListener(type, listener, options)\n }\n\n removeEventListener<K extends keyof TypedMessagePortEventMap<T> & string>(\n type: K,\n listener: ((event: TypedMessagePortEventMap<T>[K]) => void) | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void {\n super.removeEventListener(type, listener, options)\n }\n\n _peer: EventPort<any> | undefined\n _queue: MessageEvent<T>[] = []\n _started = false\n _closed = false\n _onClose: (() => void) | undefined\n\n private _onmessage: ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null = null\n\n get onmessage(): ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null {\n return this._onmessage\n }\n set onmessage(value: ((this: MessagePort, ev: MessageEvent<T>) => unknown) | null) {\n this._onmessage = value\n if (value !== null) this.start()\n }\n\n onmessageerror: ((this: MessagePort, ev: MessageEvent) => unknown) | null = null\n\n dispatchEvent(event: Event): boolean {\n if (event.type === 'message') {\n this._onmessage?.call(this, event as MessageEvent<T>)\n } else if (event.type === 'messageerror') {\n this.onmessageerror?.call(this, event as MessageEvent)\n }\n return super.dispatchEvent(event)\n }\n\n postMessage(message: T, _options?: Transferable[] | StructuredSerializeOptions): void {\n const peer = this._peer\n if (!peer || peer._closed) return\n queueMicrotask(() => {\n if (peer._closed) return\n const event = new MessageEvent('message', { data: message })\n if (peer._started) {\n peer.dispatchEvent(event)\n } else {\n peer._queue.push(event)\n }\n })\n }\n\n start(): void {\n if (this._started) return\n this._started = true\n for (const event of this._queue.splice(0)) {\n this.dispatchEvent(event)\n }\n }\n\n close(): void {\n if (this._closed) return\n this._closed = true\n this._queue.length = 0\n this._onClose?.()\n }\n}\n\nexport interface EventPort<T>\n extends Omit<\n TypedMessagePort<T>,\n 'addEventListener' | 'removeEventListener'\n > {}\n\nexport class EventChannel<T1 = unknown, T2 = unknown> {\n readonly port1: EventPort<T1>\n readonly port2: EventPort<T2>\n\n constructor() {\n const port1 = new EventPort<T1>()\n const port2 = new EventPort<T2>()\n port1._peer = port2\n port2._peer = port1\n this.port1 = port1\n this.port2 = port2\n }\n}\n","/**\n * Run `cleanup` after `target` is garbage-collected. Returns a handle to\n * cancel the tracking before that happens.\n *\n * Backed by a single shared FinalizationRegistry — every revivable that\n * needs FR semantics goes through this so the boilerplate (token,\n * unregister, cycle-safety contract) lives in one place.\n *\n * Contract: `cleanup` MUST NOT (transitively) reference `target`. The\n * registry strong-holds the cleanup callback, the cleanup would then\n * strong-hold target, and the engine would never see target as\n * collectable. Use a `WeakRef` if cleanup needs something that points\n * back at target.\n *\n * Errors thrown from cleanup are swallowed: the callback fires from the\n * FR thread, where there's no caller to surface them to.\n */\nexport type GcUnregister = () => void\n\nconst registry = new FinalizationRegistry<() => void>((cleanup) => {\n try { cleanup() } catch { /* no caller to surface to */ }\n})\n\nexport const trackGc = (target: WeakKey, cleanup: () => void): GcUnregister => {\n const token = {}\n registry.register(target, cleanup, token)\n return () => registry.unregister(token)\n}\n","import type { Capable, StructurableTransferable, Uuid } from '../types'\nimport type { TypedMessagePort } from '../utils/typed-message-channel'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from '../utils/capable-check'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\nimport { getTransferableObjects, isJsonOnlyTransport } from '../utils'\nimport { EventChannel, EventPort } from '../utils/event-channel'\nimport { trackGc } from '../utils/gc-tracker'\n\nexport const type = 'messagePort' as const\n\nexport type Messages =\n | { type: 'message', remoteUuid: Uuid, data: Capable, portId: Uuid }\n | { type: 'message-port-close', remoteUuid: Uuid, portId: Uuid }\n\nexport declare const Messages: Messages\n\nexport type AnyPort<T = Capable> =\n | TypedMessagePort<T>\n | EventPort<T>\n\nexport type BoxedMessagePort<T = Capable> =\n & BoxBaseType<typeof type>\n & (\n | { portId: Uuid, synthetic: true }\n | { portId: Uuid, synthetic: false }\n | { port: AnyPort<T>, autoBox?: boolean }\n )\n & { [UnderlyingType]: TypedMessagePort<T> }\n\n// `[T] extends [Capable]` disables distributive conditionals so `A | B` gives\n// back `AnyPort<A | B>`, not `AnyPort<A> | AnyPort<B>`. The error branch\n// intersects with AnyPort<T> so excess-property check targets the failure\n// rather than the user's port-shaped keys.\ntype StructurableTransferablePort<T> = [T] extends [Capable]\n ? AnyPort<T>\n : AnyPort<T> & {\n [ErrorMessage]: 'Message type must extend Capable'\n [BadValue]: BadFieldValue<T, Capable>\n [Path]: BadFieldPath<T, Capable>\n [ParentObject]: BadFieldParent<T, Capable>\n }\n\ntype ConnectionMessagePortState = {\n /** O(1) per-portId dispatch — avoids the O(N) addEventListener scan\n * that was the bottleneck for tight-loop RPC traffic. */\n portHandlers: Map<string, (message: Messages) => void>\n}\n\nconst connectionStateMap = new WeakMap<RevivableContext, ConnectionMessagePortState>()\n\nconst getState = (context: RevivableContext): ConnectionMessagePortState => {\n const state = connectionStateMap.get(context)\n if (!state) throw new Error('osra message-port: connection state missing; did init() run?')\n return state\n}\n\nexport const init = (context: RevivableContext): void => {\n const state: ConnectionMessagePortState = { portHandlers: new Map() }\n connectionStateMap.set(context, state)\n\n context.eventTarget.addEventListener('message', ({ detail }) => {\n if (detail.type !== 'message' && detail.type !== 'message-port-close') return\n state.portHandlers.get(detail.portId)?.(detail)\n })\n}\n\nexport const isType = (value: unknown): value is MessagePort | EventPort<StructurableTransferable> =>\n value instanceof MessagePort || value instanceof EventPort\n\nconst sendClose = (context: RevivableContext, portId: Uuid) => {\n try {\n context.sendMessage({ type: 'message-port-close', remoteUuid: context.remoteUuid, portId })\n } catch { /* connection torn down */ }\n}\n\nconst postRevived = <T>(port: AnyPort<T>, data: T, synthetic: boolean) => {\n if (synthetic) port.postMessage(data)\n else port.postMessage(data, getTransferableObjects(data))\n}\n\nexport const box = <T, T2 extends RevivableContext = RevivableContext>(\n value: StructurableTransferablePort<T>,\n context: T2,\n options?: { autoBox?: boolean },\n): BoxedMessagePort<T> => {\n // Synthetic EventPorts aren't structured-clonable, so even on a clone\n // transport we route them via portId.\n const synthetic = value instanceof EventPort\n if (!synthetic && !isJsonOnlyTransport(context.transport)) {\n return {\n ...BoxBase, type, port: value,\n ...(options?.autoBox ? { autoBox: true } : {}),\n } as BoxedMessagePort<T>\n }\n\n const { portHandlers } = getState(context)\n const liveRef: AnyPort<T> = value\n const portId: Uuid = globalThis.crypto.randomUUID()\n\n let cleanedUp = false\n const performCleanup = () => {\n if (cleanedUp) return\n cleanedUp = true\n portHandlers.delete(portId)\n unregisterGc?.()\n liveRef.removeEventListener('message', outgoingListener as EventListener)\n }\n\n const handler = (message: Messages) => {\n if (message.type === 'message-port-close') {\n performCleanup()\n liveRef.close()\n return\n }\n postRevived(liveRef, recursiveRevive(message.data, context) as T, false)\n }\n\n function outgoingListener({ data }: MessageEvent<Capable>) {\n context.sendMessage({\n type: 'message',\n remoteUuid: context.remoteUuid,\n data: recursiveBox(data, context),\n portId,\n })\n }\n\n // Safety net only — `handler` strong-holds liveRef via portHandlers, so\n // the FR won't fire while the connection is alive. Real cleanup runs via\n // the wire `message-port-close` or EventPort `_onClose`.\n const unregisterGc = trackGc(liveRef, () => {\n sendClose(context, portId)\n performCleanup()\n })\n\n liveRef.addEventListener('message', outgoingListener as EventListener)\n liveRef.start()\n\n if (liveRef instanceof EventPort) {\n liveRef._onClose = () => {\n if (cleanedUp) return\n sendClose(context, portId)\n performCleanup()\n }\n }\n\n portHandlers.set(portId, handler)\n\n return { ...BoxBase, type, portId, synthetic } as BoxedMessagePort<T>\n}\n\nexport const revive = <T extends Capable, T2 extends RevivableContext>(\n value: BoxedMessagePort<T>,\n context: T2,\n): TypedMessagePort<T> => {\n if ('port' in value) {\n if (value.autoBox) return createProtocolPort<T>(value.port as TypedMessagePort<Capable>, context)\n return value.port\n }\n return reviveViaPortId<T>(value.portId, context, value.synthetic)\n}\n\n/** Wraps a real MessagePort so revivables can treat it like a transparent\n * EventTarget that auto-boxes/revives — letting live values (Promises,\n * Functions, …) ride a clone-only transport. */\nconst createProtocolPort = <T>(\n port: TypedMessagePort<Capable>,\n ctx: RevivableContext,\n): TypedMessagePort<T> => {\n const target = new EventTarget() as TypedMessagePort<T>\n const onMessage = ({ data }: MessageEvent<Capable>): void => {\n target.dispatchEvent(new MessageEvent('message', { data: recursiveRevive(data, ctx) }))\n }\n port.addEventListener('message', onMessage)\n target.postMessage = (data: T, opt?: Transferable[] | StructuredSerializeOptions) => {\n const boxed = recursiveBox(data as Capable, ctx)\n const transferables = getTransferableObjects(boxed)\n const extra = Array.isArray(opt) ? opt : []\n port.postMessage(boxed, extra.length ? [...transferables, ...extra] : transferables)\n }\n target.start = () => port.start()\n target.close = () => {\n port.removeEventListener('message', onMessage)\n port.close()\n }\n return target\n}\n\n/** Factory for revivable-internal channels. Returns a local port that\n * auto-boxes live values regardless of transport, plus a pre-boxed remote\n * port the revivable embeds in its Boxed* structure. */\nexport const createRevivableChannel = <T extends Capable>(\n context: RevivableContext,\n): { localPort: AnyPort<T>, boxedRemote: BoxedMessagePort<T> } => {\n if (isJsonOnlyTransport(context.transport)) {\n const { port1, port2 } = new EventChannel<T, T>()\n return {\n localPort: port1,\n boxedRemote: box(port2 as StructurableTransferablePort<T>, context),\n }\n }\n const { port1, port2 } = new MessageChannel() as unknown as {\n port1: TypedMessagePort<Capable>\n port2: TypedMessagePort<Capable>\n }\n return {\n localPort: createProtocolPort<T>(port1, context) as unknown as AnyPort<T>,\n boxedRemote: box(port2 as unknown as StructurableTransferablePort<T>, context, { autoBox: true }),\n }\n}\n\nconst reviveViaPortId = <T extends Capable>(\n portId: Uuid,\n context: RevivableContext,\n synthetic: boolean,\n): TypedMessagePort<T> => {\n const { portHandlers } = getState(context)\n const { port1: userPort, port2: internalPort } =\n synthetic\n ? new EventChannel<T, T>()\n : new MessageChannel() as { port1: TypedMessagePort<T>, port2: TypedMessagePort<T> }\n const userPortRef = new WeakRef(userPort)\n // For synthetic EventChannels, internalPort._peer === userPort — holding\n // internalPort strongly from the trackGc cleanup would re-pin userPort.\n const internalPortRef = new WeakRef(internalPort)\n\n let cleanedUp = false\n const performCleanup = () => {\n if (cleanedUp) return\n cleanedUp = true\n portHandlers.delete(portId)\n const internal = internalPortRef.deref()\n internal?.removeEventListener('message', internalPortListener as EventListener)\n internal?.close()\n unregisterGc?.()\n }\n\n const handler = (message: Messages) => {\n if (message.type === 'message-port-close') {\n performCleanup()\n userPortRef.deref()?.close()\n return\n }\n if (!userPortRef.deref()) {\n performCleanup()\n return\n }\n const internal = internalPortRef.deref()\n if (!internal) return\n postRevived(internal, recursiveRevive(message.data, context) as T, synthetic)\n }\n\n const internalPortListener = ({ data }: MessageEvent<T>) => {\n context.sendMessage({\n type: 'message',\n remoteUuid: context.remoteUuid,\n data: recursiveBox(data, context),\n portId,\n })\n }\n\n const unregisterGc = trackGc(userPort, () => {\n sendClose(context, portId)\n performCleanup()\n })\n\n if (userPort instanceof EventPort) {\n userPort._onClose = () => {\n if (cleanedUp) return\n sendClose(context, portId)\n performCleanup()\n }\n }\n\n internalPort.addEventListener('message', internalPortListener as EventListener)\n internalPort.start()\n\n portHandlers.set(portId, handler)\n\n return userPort\n}\n\nconst typeCheck = () => {\n const port = {} as TypedMessagePort<{ foo: string }>\n const boxed = box(port, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: AnyPort<{ foo: string }> = revived\n // @ts-expect-error - wrong message type\n const wrongType: AnyPort<{ bar: number }> = revived\n box({} as TypedMessagePort<Promise<string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from '../utils/capable-check'\n\nimport { BoxBase, serializeError } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort,\n AnyPort,\n} from './message-port'\n\nexport const type = 'promise' as const\n\nexport type Context =\n | { type: 'resolve', data: Capable }\n | { type: 'reject', error: string }\n\n// Error branches intersect with T so the user's own keys land on the target —\n// otherwise TS's excess-property check flags a user key instead of the failure.\ntype CapablePromise<T> = T extends Promise<infer U>\n ? U extends Capable\n ? T\n : T & {\n [ErrorMessage]: 'Value type must extend a Promise that resolves to a Capable'\n [BadValue]: BadFieldValue<U, Capable>\n [Path]: BadFieldPath<U, Capable>\n [ParentObject]: BadFieldParent<U, Capable>\n }\n : T & {\n [ErrorMessage]: 'Value type must extend a Promise that resolves to a Capable'\n [BadValue]: T\n [Path]: ''\n [ParentObject]: T\n }\n\ntype ExtractCapable<T> = T extends Promise<infer U>\n ? U extends Capable ? U : never\n : never\n\nconst isCapablePromise = <T, U extends Capable = ExtractCapable<T>>(value: T): value is T & Promise<U> =>\n value instanceof Promise\n\nexport type BoxedPromise<T extends Capable = Capable> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Context> }\n & { [UnderlyingType]: T }\n\n// Pins the revived port between executor return and result arrival — the\n// port↔listener cycle has no other anchor (the caller only holds the\n// returned Promise). The once-listener removes its entry on settle.\nconst inFlightPromisePorts = new Set<AnyPort<Context>>()\n\nexport const isType = (value: unknown): value is Promise<any> =>\n value instanceof Promise\n\nexport const box = <T, T2 extends RevivableContext>(\n value: CapablePromise<T>,\n context: T2\n): BoxedPromise<ExtractCapable<T>> => {\n if (!isCapablePromise(value)) throw new TypeError('Expected Promise')\n const { localPort, boxedRemote } = createRevivableChannel<Context>(context)\n\n const sendResult = (result: Context) => {\n localPort.postMessage(result)\n localPort.close()\n }\n\n value\n .then((data: ExtractCapable<T>) => sendResult({ type: 'resolve', data }))\n .catch((error: unknown) => sendResult({ type: 'reject', error: serializeError(error) }))\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedPromise<ExtractCapable<T>>\n}\n\nexport const revive = <T extends BoxedPromise, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => {\n const port = reviveMessagePort(value.port, context)\n inFlightPromisePorts.add(port)\n return new Promise<T[UnderlyingType]>((resolve, reject) => {\n port.addEventListener('message', ({ data: result }) => {\n if (result.type === 'resolve') resolve(result.data as T[UnderlyingType])\n else reject(result.error)\n port.close()\n inFlightPromisePorts.delete(port)\n }, { once: true })\n port.start()\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(Promise.resolve(1 as const), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Promise<1> = revived\n // @ts-expect-error\n const notExpected: Promise<string> = revived\n // @ts-expect-error\n box(1 as const, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { UnderlyingType, RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase, serializeError } from './utils'\nimport { recursiveBox } from '.'\nimport { getTransferableObjects } from '../utils'\nimport { EventChannel, type EventPort } from '../utils/event-channel'\nimport { box as boxMessagePort, revive as reviveMessagePort, BoxedMessagePort } from './message-port'\n\nexport const type = 'function' as const\n\ntype ResultMessage =\n | { __osra_ok__: true, value: Capable }\n | { __osra_err__: true, error: string }\n\ntype CallContext = [EventPort<Capable>, Capable[]]\n\n// Pins return-value ports between call-site return and result arrival —\n// the (port ↔ once-listener ↔ resolve/reject) cycle has no other anchor.\nconst inFlightReturnPorts = new Set<EventPort<Capable>>()\n\nexport type BoxedFunction<T extends (...args: any[]) => any = (...args: any[]) => any> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<CallContext> }\n & { [UnderlyingType]: (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> }\n\ntype CapableFunction<T> = T extends (...args: infer P) => infer R\n ? P extends Capable[]\n ? R extends Capable ? T : never\n : never\n : never\n\nexport const isType = (value: unknown): value is (...args: any[]) => any =>\n typeof value === 'function'\n\nexport const box = <T extends (...args: any[]) => any, T2 extends RevivableContext>(\n value: T & CapableFunction<T>,\n context: T2,\n): BoxedFunction<T> => {\n // EventChannel rather than MessageChannel: revived live values arriving\n // in args (functions, EventTarget façades, …) aren't structured-clonable.\n const { port1: localPort, port2: remotePort } = new EventChannel<CallContext, CallContext>()\n\n localPort.addEventListener('message', ({ data }) => {\n // Don't recursiveRevive — message-port handler already revived in place.\n // Re-walking would Object.fromEntries plain args, breaking identity.\n const [returnPort, args] = data as CallContext\n ;(async () => {\n let message: ResultMessage\n try {\n const resolved = await value(...(args as Parameters<T>))\n message = { __osra_ok__: true, value: resolved as Capable }\n } catch (error) {\n message = { __osra_err__: true, error: serializeError(error) }\n }\n const boxedResult = recursiveBox(message as Capable, context)\n returnPort.postMessage(boxedResult, getTransferableObjects(boxedResult))\n // Defer close so the result reaches the peer before tear-down. The\n // close fires _onClose, dropping per-call routing entries on both\n // sides — without it portHandlers grows one entry per call.\n queueMicrotask(() => {\n try { returnPort.close() } catch { /* may already be closed */ }\n })\n })()\n })\n localPort.start()\n\n return {\n ...BoxBase,\n type,\n port: boxMessagePort(remotePort as unknown as MessagePort, context),\n } as unknown as BoxedFunction<T>\n}\n\nexport const revive = <T extends BoxedFunction, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context) as unknown as MessagePort\n\n return ((...args: Capable[]) =>\n new Promise((resolve, reject) => {\n const { port1: returnLocal, port2: returnRemote } = new EventChannel<Capable, Capable>()\n inFlightReturnPorts.add(returnLocal)\n\n returnLocal.addEventListener('message', ({ data }) => {\n const message = data as ResultMessage\n if ('__osra_ok__' in message) resolve(message.value)\n else reject(message.error)\n returnLocal.close()\n inFlightReturnPorts.delete(returnLocal)\n }, { once: true })\n returnLocal.start()\n\n const callContext = recursiveBox([returnRemote, args] as unknown as Capable, context)\n port.postMessage(callContext, getTransferableObjects(callContext))\n })) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const boxed = box((a: number, b: string) => a + b.length, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: (a: number, b: string) => Promise<number> = revived\n // @ts-expect-error - wrong return type\n const wrongReturn: (a: number, b: string) => Promise<string> = revived\n // @ts-expect-error - wrong parameter types\n const wrongParams: (a: string, b: number) => Promise<number> = revived\n // @ts-expect-error - non-Capable parameter type (WeakMap isn't structured-clonable)\n box((a: WeakMap<object, string>) => a.toString(), {} as RevivableContext)\n // @ts-expect-error - non-Capable return type\n box(() => new WeakMap<object, string>(), {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\n\nimport { BoxBase } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort\n} from './message-port'\n\nexport const type = 'readableStream' as const\n\nexport type PullContext = {\n type: 'pull' | 'cancel'\n}\n\ntype ChunkMessage<T = unknown> = Promise<ReadableStreamReadResult<T>>\n\ntype Msg = PullContext | ChunkMessage\n\nexport type BoxedReadableStream<T extends ReadableStream = ReadableStream> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Msg> }\n & { [UnderlyingType]: T }\n\nexport const isType = (value: unknown): value is ReadableStream =>\n value instanceof ReadableStream\n\nexport const box = <T extends ReadableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): BoxedReadableStream<T> => {\n const { localPort, boxedRemote } = createRevivableChannel<Msg>(context)\n const reader = value.getReader()\n\n localPort.addEventListener('message', ({ data }) => {\n if ('type' in data && data.type === 'pull') {\n // reader.read() is a Promise — localPort boxes it for the transport.\n localPort.postMessage(reader.read())\n } else {\n reader.cancel()\n localPort.close()\n }\n })\n localPort.start()\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedReadableStream<T>\n}\n\nexport const revive = <T extends BoxedReadableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n return new ReadableStream({\n pull: (controller) => new Promise<void>((resolve, reject) => {\n port.addEventListener('message', ({ data }) => {\n if (!(data instanceof Promise)) return\n data\n .then(result => {\n if (result.done) controller.close()\n else controller.enqueue(result.value)\n resolve()\n })\n .catch(reject)\n }, { once: true })\n port.postMessage({ type: 'pull' })\n }),\n cancel: () => {\n port.postMessage({ type: 'cancel' })\n // Defer close so the cancel message dispatches before tear-down.\n queueMicrotask(() => port.close())\n },\n }) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const stream = new ReadableStream<number>()\n const boxed = box(stream, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ReadableStream<number> = revived\n // @ts-expect-error - wrong stream type\n const wrongType: ReadableStream<string> = revived\n // @ts-expect-error - not a ReadableStream\n box('not a stream', {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '.'\nimport type { Capable } from '../types'\n\nimport { BoxBase } from './utils'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n BoxedMessagePort,\n} from './message-port'\n\nexport const type = 'writableStream' as const\n\n// Outgoing wire shape (revive → box): one of these per call.\nexport type WriteContext =\n | { type: 'write', chunk: Capable }\n | { type: 'close' }\n | { type: 'abort', reason: Capable }\n\n// Reply from box → revive after a write completes (so writer.write() awaits).\nexport type WriteAck =\n | { type: 'ack' }\n | { type: 'err', error: string }\n\nexport type Msg = WriteContext | WriteAck\n\nexport type BoxedWritableStream<T extends WritableStream = WritableStream> =\n & BoxBaseType<typeof type>\n & { port: BoxedMessagePort<Msg> }\n & { [UnderlyingType]: T }\n\nexport const isType = (value: unknown): value is WritableStream =>\n value instanceof WritableStream\n\nexport const box = <T extends WritableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): BoxedWritableStream<T> => {\n const { localPort, boxedRemote } = createRevivableChannel<Msg>(context)\n const writer = value.getWriter()\n\n localPort.addEventListener('message', ({ data }) => {\n if (!data || typeof data !== 'object' || !('type' in data)) return\n if (data.type === 'write') {\n writer.write((data as { chunk: Capable }).chunk as any)\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n } else if (data.type === 'close') {\n writer.close()\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n } else if (data.type === 'abort') {\n writer.abort((data as { reason: Capable }).reason as any)\n .then(() => localPort.postMessage({ type: 'ack' }))\n .catch((err) => localPort.postMessage({ type: 'err', error: (err as Error)?.message ?? String(err) }))\n }\n })\n localPort.start()\n\n return { ...BoxBase, type, port: boxedRemote } as BoxedWritableStream<T>\n}\n\nexport const revive = <T extends BoxedWritableStream, T2 extends RevivableContext>(\n value: T,\n context: T2\n): T[UnderlyingType] => {\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n // Each `write` call posts a 'write' message and awaits an 'ack'/'err'.\n // The port is shared, so we serialize via a chain — without it, two\n // concurrent writes would race and could mis-pair their ack messages.\n let chain: Promise<void> = Promise.resolve()\n const request = (msg: WriteContext): Promise<void> => {\n const next = chain.then(() => new Promise<void>((resolve, reject) => {\n port.addEventListener('message', ({ data }) => {\n if (!data || typeof data !== 'object' || !('type' in data)) return\n if ((data as { type: string }).type === 'ack') resolve()\n else if ((data as { type: string }).type === 'err') reject(new Error((data as { error: string }).error))\n }, { once: true })\n port.postMessage(msg as Msg)\n }))\n chain = next.catch(() => {})\n return next\n }\n\n return new WritableStream({\n write: (chunk) => request({ type: 'write', chunk: chunk as Capable }),\n close: () => request({ type: 'close' }),\n abort: (reason) => request({ type: 'abort', reason: reason as Capable }),\n }) as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const stream = new WritableStream<number>()\n const boxed = box(stream, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: WritableStream<number> = revived\n // @ts-expect-error - wrong stream type\n const wrongType: WritableStream<string> = revived\n // @ts-expect-error - not a WritableStream\n box('not a stream', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { BoxedMessagePort } from './message-port'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\nimport {\n createRevivableChannel,\n revive as reviveMessagePort,\n} from './message-port'\n\nexport const type = 'abortSignal' as const\n\ntype AbortMessage = {\n type: 'abort'\n reason?: Capable\n}\n\nexport type BoxedAbortSignal =\n & BoxBaseType<typeof type>\n & {\n aborted: boolean\n reason?: Capable\n port: BoxedMessagePort<AbortMessage>\n }\n\nexport const isType = (value: unknown): value is AbortSignal =>\n value instanceof AbortSignal\n\nexport const box = <T extends AbortSignal, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedAbortSignal => {\n const { localPort, boxedRemote } = createRevivableChannel<AbortMessage>(context)\n\n if (!value.aborted) {\n value.addEventListener('abort', () => {\n localPort.postMessage({ type: 'abort', reason: value.reason as Capable })\n localPort.close()\n }, { once: true })\n } else {\n localPort.close()\n }\n\n // Eagerly-aborted reason rides the wrapper, so we must box it here —\n // recursiveBox short-circuits on OSRA_BOX without descending in.\n return {\n ...BoxBase,\n type,\n aborted: value.aborted,\n reason: value.aborted ? recursiveBox(value.reason as Capable, context) as Capable : undefined,\n port: boxedRemote,\n }\n}\n\nexport const revive = <T extends BoxedAbortSignal, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): AbortSignal => {\n const controller = new AbortController()\n\n if (value.aborted) {\n controller.abort(recursiveRevive(value.reason as Capable, context))\n return controller.signal\n }\n\n const port = reviveMessagePort(value.port, context)\n port.start()\n\n port.addEventListener('message', ({ data: message }) => {\n if (message.type === 'abort') {\n controller.abort(recursiveRevive(message.reason as Capable, context))\n port.close()\n }\n })\n\n return controller.signal\n}\n\nconst typeCheck = () => {\n const boxed = box(new AbortController().signal, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: AbortSignal = revived\n // @ts-expect-error - not an AbortSignal\n const notAbortSignal: string = revived\n // @ts-expect-error - cannot box non-AbortSignal\n box('not an abort signal', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { box as boxHeaders, revive as reviveHeaders } from './headers'\nimport { box as boxReadableStream, revive as reviveReadableStream } from './readable-stream'\n\nexport const type = 'response' as const\n\nexport const isType = (value: unknown): value is Response =>\n value instanceof Response\n\nexport const box = <T extends Response, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => ({\n ...BoxBase,\n type,\n status: value.status,\n statusText: value.statusText,\n headers: boxHeaders(value.headers, context),\n body: value.body ? boxReadableStream(value.body, context) : null,\n url: value.url,\n redirected: value.redirected\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n context: T2\n): Response => {\n const headers = reviveHeaders(value.headers, context)\n const body = value.body ? reviveReadableStream(value.body, context) : null\n\n return new Response(body, {\n status: value.status,\n statusText: value.statusText,\n headers\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(new Response('body', { status: 200 }), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Response = revived\n // @ts-expect-error - not a Response\n const notResponse: string = revived\n // @ts-expect-error - cannot box non-Response\n box('not a response', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { box as boxHeaders, revive as reviveHeaders } from './headers'\nimport { box as boxReadableStream, revive as reviveReadableStream } from './readable-stream'\n\nexport const type = 'request' as const\n\nexport const isType = (value: unknown): value is Request =>\n value instanceof Request\n\nexport const box = <T extends Request, T2 extends RevivableContext>(\n value: T,\n context: T2\n) => ({\n ...BoxBase,\n type,\n method: value.method,\n url: value.url,\n headers: boxHeaders(value.headers, context),\n body: value.body ? boxReadableStream(value.body, context) : null,\n credentials: value.credentials,\n cache: value.cache,\n redirect: value.redirect,\n referrer: value.referrer,\n referrerPolicy: value.referrerPolicy,\n integrity: value.integrity,\n keepalive: value.keepalive\n})\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(\n value: T,\n context: T2\n): Request => {\n const headers = reviveHeaders(value.headers, context)\n const body = value.body ? reviveReadableStream(value.body, context) : null\n\n return new Request(value.url, {\n method: value.method,\n headers,\n body,\n credentials: value.credentials,\n cache: value.cache,\n redirect: value.redirect,\n referrer: value.referrer,\n referrerPolicy: value.referrerPolicy,\n integrity: value.integrity,\n keepalive: value.keepalive,\n // @ts-expect-error - duplex is needed for streaming bodies\n duplex: 'half',\n })\n}\n\nconst typeCheck = () => {\n const boxed = box(new Request('https://example.com'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Request = revived\n // @ts-expect-error - not a Request\n const notRequest: string = revived\n // @ts-expect-error - cannot box non-Request\n box('not a request', {} as RevivableContext)\n}\n","import type { Capable, Uuid } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'identity' as const\n\nexport type Messages = {\n type: 'identity-dispose'\n remoteUuid: Uuid\n id: string\n}\n\nexport declare const Messages: Messages\n\nconst IDENTITY_MARKER: unique symbol = Symbol.for('osra.identity')\n\ntype IdentityWrapper<T = unknown> = {\n readonly [IDENTITY_MARKER]: true\n readonly value: T\n}\n\nexport type BoxedIdentity<T extends Capable = Capable> = BoxBaseType<typeof type> & {\n id: string\n inner?: Capable\n [UnderlyingType]: T\n}\n\nconst isObjectOrFunction = (value: unknown): value is object =>\n value !== null && (typeof value === 'object' || typeof value === 'function')\n\n/** Anything we can hand to WeakMap/WeakRef/FinalizationRegistry. Excludes\n * registered symbols (Symbol.for) — those throw at runtime. */\nconst isWeakKeyable = (value: unknown): value is WeakKey => {\n if (value === null) return false\n const t = typeof value\n if (t === 'object' || t === 'function') return true\n if (t === 'symbol') return Symbol.keyFor(value as symbol) === undefined\n return false\n}\n\nconst isIdentityWrapper = (value: unknown): value is IdentityWrapper =>\n isObjectOrFunction(value) && IDENTITY_MARKER in value && value[IDENTITY_MARKER] === true\n\nconst wrapperMemo = new WeakMap<object, IdentityWrapper>()\n\nconst wrap = (value: object): IdentityWrapper => {\n if (isIdentityWrapper(value)) return value\n const cached = wrapperMemo.get(value)\n if (cached) return cached\n const wrapper: IdentityWrapper = { [IDENTITY_MARKER]: true, value }\n wrapperMemo.set(value, wrapper)\n return wrapper\n}\n\n/** Wrap a value so osra preserves reference identity across the RPC\n * boundary. Idempotent; primitives pass through unchanged. Lies at the\n * type level — runtime value is an IdentityWrapper<T> typed as T. */\nexport const identity = <T>(value: T): T =>\n (isObjectOrFunction(value) ? wrap(value) : value) as T\n\ntype IdentityState = {\n readonly sendIds: WeakMap<WeakKey, string>\n /** id → ref to the value we sent, so a round-trip resolves to the\n * original reference instead of building a fresh proxy. */\n readonly idToSent: Map<string, WeakRef<WeakKey>>\n readonly sendRegistry: FinalizationRegistry<string>\n readonly receiveCache: Map<string, unknown>\n /** Revived value → id, so user code passing a revived value back to\n * its origin replays the peer's id and short-circuits to the real ref. */\n readonly revivedToId: WeakMap<WeakKey, string>\n listenerInstalled: boolean\n}\n\nconst connectionStates = new WeakMap<RevivableContext, IdentityState>()\n\nconst getOrCreateState = (context: RevivableContext): IdentityState => {\n const existing = connectionStates.get(context)\n if (existing) return existing\n const sendIds = new WeakMap<WeakKey, string>()\n const idToSent = new Map<string, WeakRef<WeakKey>>()\n const receiveCache = new Map<string, unknown>()\n const revivedToId = new WeakMap<WeakKey, string>()\n const sendRegistry = new FinalizationRegistry<string>((id) => {\n idToSent.delete(id)\n try {\n context.sendMessage({ type: 'identity-dispose', remoteUuid: context.remoteUuid, id })\n } catch { /* connection already closed */ }\n })\n const state: IdentityState = {\n sendIds, idToSent, sendRegistry, receiveCache, revivedToId,\n listenerInstalled: false,\n }\n connectionStates.set(context, state)\n installReceiveListener(context, state)\n return state\n}\n\nconst installReceiveListener = (context: RevivableContext, state: IdentityState) => {\n if (state.listenerInstalled) return\n state.listenerInstalled = true\n context.eventTarget.addEventListener('message', ({ detail }) => {\n if (detail?.type !== 'identity-dispose') return\n const revived = state.receiveCache.get(detail.id)\n state.receiveCache.delete(detail.id)\n if (revived !== undefined && isWeakKeyable(revived)) state.revivedToId.delete(revived)\n })\n}\n\nexport const isType = (value: unknown): value is IdentityWrapper =>\n isIdentityWrapper(value)\n\n/** Look up or assign the id for a referenceable value. Returns whether\n * the id is already-known (resend or round-trip) so the caller can skip\n * shipping the inner payload. */\nconst registerForReference = (\n value: WeakKey,\n state: IdentityState,\n): { id: string, isExisting: boolean } => {\n const existingId = state.sendIds.get(value)\n if (existingId !== undefined) return { id: existingId, isExisting: true }\n const receivedId = state.revivedToId.get(value)\n if (receivedId !== undefined) return { id: receivedId, isExisting: true }\n const id = globalThis.crypto.randomUUID()\n state.sendIds.set(value, id)\n state.idToSent.set(id, new WeakRef(value))\n state.sendRegistry.register(value, id)\n return { id, isExisting: false }\n}\n\nexport const box = <T extends Capable, TContext extends RevivableContext>(\n wrapper: IdentityWrapper<T>,\n context: TContext,\n): BoxedIdentity<T> => {\n const state = getOrCreateState(context)\n const inner = wrapper.value\n const innerBox = recursiveBox(inner, context)\n if (!isWeakKeyable(inner)) {\n // Inner can't anchor a WeakMap key — emit fresh id+inner each time, no dedup.\n return { ...BoxBase, type, id: globalThis.crypto.randomUUID(), inner: innerBox } as BoxedIdentity<T>\n }\n const { id, isExisting } = registerForReference(inner, state)\n if (isExisting) return { ...BoxBase, type, id } as BoxedIdentity<T>\n return { ...BoxBase, type, id, inner: innerBox } as BoxedIdentity<T>\n}\n\n/** Identity-box a referenceable value with a caller-supplied inner box,\n * bypassing the recursive-box step. Used by revivables (symbol with\n * description=undefined) where recursing back through their own box\n * would loop into this module again. */\nexport const boxByReference = <T extends WeakKey, TContext extends RevivableContext>(\n value: T,\n innerBox: Capable,\n context: TContext,\n): BoxedIdentity => {\n const state = getOrCreateState(context)\n const { id, isExisting } = registerForReference(value, state)\n if (isExisting) return { ...BoxBase, type, id } as BoxedIdentity\n return { ...BoxBase, type, id, inner: innerBox } as BoxedIdentity\n}\n\nexport const revive = <T extends BoxedIdentity, TContext extends RevivableContext>(\n value: T,\n context: TContext,\n): T[UnderlyingType] => {\n const state = getOrCreateState(context)\n const cached = state.receiveCache.get(value.id)\n if (cached !== undefined) return cached as T[UnderlyingType]\n const originated = state.idToSent.get(value.id)?.deref()\n if (originated !== undefined) return originated as T[UnderlyingType]\n if (!('inner' in value) || value.inner === undefined) {\n throw new Error(`osra identity: received id=${value.id} with no inner payload and no cached value`)\n }\n const revived = recursiveRevive(value.inner, context)\n state.receiveCache.set(value.id, revived)\n if (isWeakKeyable(revived)) state.revivedToId.set(revived, value.id)\n return revived as T[UnderlyingType]\n}\n\nconst typeCheck = () => {\n const fn = () => 42\n const wrapper = { [IDENTITY_MARKER]: true, value: fn } as IdentityWrapper<typeof fn>\n const boxed = box(wrapper, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: typeof fn = revived\n // @ts-expect-error - revived is the original function type, not string\n const notExpected: string = revived\n // @ts-expect-error - cannot box a non-Capable wrapper (WeakMap not assignable)\n box({ [IDENTITY_MARKER]: true, value: new WeakMap() } as IdentityWrapper<WeakMap<object, string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { BoxBase as BoxBaseType, RevivableContext, UnderlyingType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { instanceOfAny, isJsonOnlyTransport } from '../utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'transfer' as const\n\nconst TRANSFER_MARKER: unique symbol = Symbol.for('osra.transfer')\n\ntype TransferWrapper<T = unknown> = {\n readonly [TRANSFER_MARKER]: true\n readonly value: T\n}\n\nexport type BoxedTransfer<T extends Capable = Capable> = BoxBaseType<typeof type> & {\n inner: Capable\n degraded: boolean\n [UnderlyingType]: T\n}\n\nconst isObject = (value: unknown): value is object =>\n value !== null && typeof value === 'object'\n\nconst isTransferWrapper = (value: unknown): value is TransferWrapper =>\n isObject(value) && TRANSFER_MARKER in value && value[TRANSFER_MARKER] === true\n\nconst isWrappableTransferable = (value: unknown): boolean => {\n if (!isObject(value)) return false\n if (ArrayBuffer.isView(value)) return true\n return instanceOfAny(value, [\n globalThis.ArrayBuffer,\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n ])\n}\n\n/** Opt into transfer (move) semantics for a transferable value. Idempotent;\n * non-transferable inputs pass through unchanged. Silently degrades to a\n * copy when the platform/transport can't transfer the given type. Lies at\n * the type level — runtime value is a TransferWrapper<T> typed as T. */\nexport const transfer = <T>(value: T): T =>\n (isWrappableTransferable(value)\n ? { [TRANSFER_MARKER]: true, value }\n : value\n ) as T\n\nexport const isType = (value: unknown): value is TransferWrapper =>\n isTransferWrapper(value)\n\nexport const box = <T extends Capable, TContext extends RevivableContext>(\n wrapper: TransferWrapper<T>,\n context: TContext,\n): BoxedTransfer<T> =>\n // `degraded` tells the send-time walker in getTransferableObjects to treat\n // this box as a regular value (no transfer-list entry). JSON transports\n // can't move ownership, so transfer semantics don't apply.\n ({\n ...BoxBase,\n type,\n inner: recursiveBox(wrapper.value, context),\n degraded: isJsonOnlyTransport(context.transport),\n }) as unknown as BoxedTransfer<T>\n\nexport const revive = <T extends BoxedTransfer, TContext extends RevivableContext>(\n value: T,\n context: TContext,\n): T[UnderlyingType] =>\n recursiveRevive(value.inner, context) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const ab = new ArrayBuffer(10)\n const wrapper = { [TRANSFER_MARKER]: true, value: ab } as TransferWrapper<ArrayBuffer>\n const boxed = box(wrapper, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: ArrayBuffer = revived\n // @ts-expect-error - revived is ArrayBuffer, not string\n const notExpected: string = revived\n // @ts-expect-error - cannot box a non-Capable wrapper (WeakMap not assignable)\n box({ [TRANSFER_MARKER]: true, value: new WeakMap() } as TransferWrapper<WeakMap<object, string>>, {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, UnderlyingType, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'map' as const\n\nexport type BoxedMap<T extends Map<Capable, Capable> = Map<Capable, Capable>> =\n & BoxBaseType<typeof type>\n & { entries: Array<[Capable, Capable]> }\n & { [UnderlyingType]: T }\n\n// `Map<unknown, unknown>` (rather than `Map<Capable, Capable>`) breaks the\n// Capable ↔ defaultRevivableModules ↔ this module type cycle. box() still\n// narrows to `Map<Capable, Capable>` so misuse is caught there.\nexport const isType = (value: unknown): value is Map<unknown, unknown> =>\n value instanceof Map\n\nexport const box = <T extends Map<Capable, Capable>, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedMap<T> => ({\n ...BoxBase,\n type,\n entries: Array.from(value, ([k, v]): [Capable, Capable] =>\n [recursiveBox(k, context) as Capable, recursiveBox(v, context) as Capable]),\n}) as BoxedMap<T>\n\nexport const revive = <T extends BoxedMap, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n new Map(value.entries.map(([k, v]) => [\n recursiveRevive(k, context),\n recursiveRevive(v, context),\n ])) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const m = new Map<string, number>([['a', 1]])\n const boxed = box(m, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Map<string, number> = revived\n // @ts-expect-error - wrong value type\n const wrongValue: Map<string, string> = revived\n // @ts-expect-error - cannot box non-Map\n box('not a map', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, UnderlyingType, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'set' as const\n\nexport type BoxedSet<T extends Set<Capable> = Set<Capable>> =\n & BoxBaseType<typeof type>\n & { values: Array<Capable> }\n & { [UnderlyingType]: T }\n\n// `Set<unknown>` breaks the Capable ↔ defaultRevivableModules ↔ this module\n// type cycle; box() narrows to `Set<Capable>` so misuse is caught there.\nexport const isType = (value: unknown): value is Set<unknown> =>\n value instanceof Set\n\nexport const box = <T extends Set<Capable>, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedSet<T> => ({\n ...BoxBase,\n type,\n values: Array.from(value, v => recursiveBox(v, context) as Capable),\n}) as BoxedSet<T>\n\nexport const revive = <T extends BoxedSet, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n new Set(value.values.map(v => recursiveRevive(v, context))) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const s = new Set<number>([1, 2, 3])\n const boxed = box(s, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Set<number> = revived\n // @ts-expect-error - wrong value type\n const wrongValue: Set<string> = revived\n // @ts-expect-error - cannot box non-Set\n box('not a set', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\n\nexport const type = 'bigint' as const\n\nexport const isType = (value: unknown): value is bigint =>\n typeof value === 'bigint'\n\nexport const box = <T extends bigint, T2 extends RevivableContext>(\n value: T,\n _context: T2,\n) => ({\n ...BoxBase,\n type,\n value: value.toString(),\n})\n\nexport const revive = <T extends ReturnType<typeof box>>(\n value: T,\n _context: RevivableContext,\n) => BigInt(value.value)\n\nconst typeCheck = () => {\n const boxed = box(123n, {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: bigint = revived\n // @ts-expect-error - not a string\n const notString: string = revived\n // @ts-expect-error - cannot box non-bigint\n box('not a bigint', {} as RevivableContext)\n}\n","import type { Capable } from '../types'\nimport type { RevivableContext, BoxBase as BoxBaseType } from './utils'\n\nimport { BoxBase } from './utils'\nimport { recursiveBox, recursiveRevive } from '.'\n\nexport const type = 'event' as const\n\n/** Boxes Event/CustomEvent only. Subclass-specific fields (MessageEvent.data,\n * ErrorEvent.error, ProgressEvent.loaded, etc.) are dropped on the wire. */\nexport type BoxedEvent =\n & BoxBaseType<typeof type>\n & { eventType: string, bubbles: boolean, cancelable: boolean, composed: boolean, detail?: Capable }\n\nexport const isType = (value: unknown): value is Event =>\n value instanceof Event\n\nexport const box = <T extends Event, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedEvent => ({\n ...BoxBase,\n type,\n eventType: value.type,\n bubbles: value.bubbles,\n cancelable: value.cancelable,\n composed: value.composed,\n ...(value instanceof CustomEvent ? { detail: recursiveBox(value.detail as Capable, context) as Capable } : {}),\n})\n\nexport const revive = <T extends BoxedEvent, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): Event => {\n const init = { bubbles: value.bubbles, cancelable: value.cancelable, composed: value.composed }\n return 'detail' in value\n ? new CustomEvent(value.eventType, { ...init, detail: recursiveRevive(value.detail as Capable, context) })\n : new Event(value.eventType, init)\n}\n\nconst typeCheck = () => {\n const boxed = box(new Event('foo'), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Event = revived\n // @ts-expect-error - not an Event\n const notEvent: string = revived\n // @ts-expect-error - cannot box non-Event\n box('not an event', {} as RevivableContext)\n}\n","import { BoxBase, type RevivableContext } from './utils'\nimport { identity } from './identity'\nimport { box as boxFunction, revive as reviveFunction } from './function'\nimport { trackGc } from '../utils/gc-tracker'\n\nexport const type = 'eventTarget' as const\n\ntype ListenerOpts = boolean | { capture?: boolean, once?: boolean, passive?: boolean, signal?: AbortSignal }\n\nexport const isType = (value: unknown): value is EventTarget => value instanceof EventTarget\n\nexport const box = <T extends EventTarget, T2 extends RevivableContext>(value: T, context: T2) => ({\n ...BoxBase,\n type,\n addListener: boxFunction(\n (type: string, listener: EventListener, options?: ListenerOpts) =>\n value.addEventListener(type, listener, options),\n context,\n ),\n removeListener: boxFunction(\n (type: string, listener: EventListener, options?: ListenerOpts) =>\n value.removeEventListener(type, listener, options),\n context,\n ),\n})\n\nexport type BoxedEventTarget = ReturnType<typeof box>\n\n// Stable EventListener per EventListenerObject so identity() yields the\n// same id on add and remove.\nconst objectWrappers = new WeakMap<EventListenerObject, EventListener>()\nconst toListener = (listerObject: EventListenerOrEventListenerObject): EventListener => {\n if (typeof listerObject === 'function') return listerObject\n let listener = objectWrappers.get(listerObject)\n if (!listener) objectWrappers.set(listerObject, listener = (e) => listerObject.handleEvent(e))\n return listener\n}\n\ntype Reg = { eventType: string, listener: EventListener, capture: boolean }\n\nconst findReg = (regs: Reg[], eventType: string, listener: EventListener, capture: boolean): Reg | undefined =>\n regs.find(r => r.eventType === eventType && r.listener === listener && r.capture === capture)\n\nexport const revive = <T extends ReturnType<typeof box>, T2 extends RevivableContext>(value: T, context: T2) => {\n const addRpc = reviveFunction(value.addListener, context)\n const removeRpc = reviveFunction(value.removeListener, context)\n // Façade only — events never dispatch through it. Source-side EventTarget\n // owns all semantics; we just track regs for trackGc cleanup.\n const target = new EventTarget()\n const regs: Reg[] = []\n\n Object.defineProperty(target, 'addEventListener', {\n value: (eventType: string, listener: EventListenerOrEventListenerObject | null, options?: ListenerOpts) => {\n if (listener === null) return\n const fn = toListener(listener)\n const capture = typeof options === 'boolean' ? options : !!options?.capture\n if (findReg(regs, eventType, fn, capture)) return\n regs.push({ eventType, listener: fn, capture })\n addRpc(eventType, identity(fn), options).catch(() => {})\n },\n })\n\n Object.defineProperty(target, 'removeEventListener', {\n value: (eventType: string, listener: EventListenerOrEventListenerObject | null, options?: ListenerOpts) => {\n if (listener === null) return\n const fn = toListener(listener)\n const capture = typeof options === 'boolean' ? options : !!options?.capture\n const reg = findReg(regs, eventType, fn, capture)\n if (!reg) return\n regs.splice(regs.indexOf(reg), 1)\n removeRpc(eventType, identity(fn), { capture }).catch(() => {})\n },\n })\n\n // Cleanup must NOT close over `target` — otherwise the FR can never fire.\n trackGc(target, () => {\n for (const { eventType, listener, capture } of regs) {\n removeRpc(eventType, identity(listener), { capture }).catch(() => {})\n }\n regs.length = 0\n })\n\n return target\n}\n\nconst typeCheck = () => {\n const r = revive(box(new EventTarget(), {} as RevivableContext), {} as RevivableContext)\n const expected: EventTarget = r\n // @ts-expect-error - not a string\n const notString: string = r\n // @ts-expect-error - cannot box non-EventTarget\n box('not an event target', {} as RevivableContext)\n}\n","import type { RevivableContext, BoxBase as BoxBaseType } from './utils'\nimport type { UnderlyingType } from '../utils/type'\nimport type { BoxedPromise } from './promise'\n\nimport { BoxBase } from './utils'\nimport { box as boxPromise, revive as revivePromise } from './promise'\n\nexport const type = 'blob' as const\n\nexport type BoxedBlob<T extends Blob = Blob> =\n & BoxBaseType<typeof type>\n & { mimeType: string }\n & { buffer: BoxedPromise<ArrayBuffer> }\n & { fileName?: string, lastModified?: number }\n & { [UnderlyingType]: Promise<T> }\n\n// File extends Blob and is handled here too — encoding `name` + `lastModified`\n// when present so the receiver reconstructs a File rather than dropping to a\n// plain Blob. Avoids a runtime/type mismatch where File would type-check as\n// Capable but silently coerce.\nexport const isType = (value: unknown): value is Blob =>\n value instanceof Blob\n\nconst isFile = (value: Blob): value is File =>\n typeof File !== 'undefined' && value instanceof File\n\nexport const box = <T extends Blob, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): BoxedBlob<T> => ({\n ...BoxBase,\n type,\n mimeType: value.type,\n buffer: boxPromise(value.arrayBuffer(), context),\n ...(isFile(value)\n ? { fileName: value.name, lastModified: value.lastModified }\n : {}),\n}) as unknown as BoxedBlob<T>\n\n// Blob bytes are fetched async (`blob.arrayBuffer()`), so revive can't\n// hand back a Blob synchronously — receivers `await` to get the Blob.\nexport const revive = <T extends BoxedBlob, T2 extends RevivableContext>(\n value: T,\n context: T2,\n): T[UnderlyingType] =>\n revivePromise(value.buffer, context)\n .then(buffer =>\n value.fileName !== undefined && typeof File !== 'undefined'\n ? new File([buffer], value.fileName, {\n type: value.mimeType,\n lastModified: value.lastModified,\n })\n : new Blob([buffer], { type: value.mimeType })) as T[UnderlyingType]\n\nconst typeCheck = () => {\n const boxed = box(new Blob(['x'], { type: 'text/plain' }), {} as RevivableContext)\n const revived = revive(boxed, {} as RevivableContext)\n const expected: Promise<Blob> = revived\n // @ts-expect-error - revived is Promise<Blob>, not a sync Blob\n const notBlob: Blob = revived\n // @ts-expect-error - cannot box non-Blob\n box('not a blob', {} as RevivableContext)\n}\n","import type { RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { boxByReference } from './identity'\n\nexport const type = 'symbol' as const\n\nexport const isType = (value: unknown): value is symbol =>\n typeof value === 'symbol'\n\nexport const box = <T extends symbol, T2 extends RevivableContext>(\n value: T,\n context: T2,\n) => {\n const box = {\n ...BoxBase,\n type,\n description: value.description,\n }\n return (\n value.description === undefined\n ? boxByReference(value, box, context)\n : box\n )\n}\n\nexport const revive = <T extends { description: string | undefined }, T2 extends RevivableContext>(\n value: T,\n _context: T2,\n): symbol => Symbol(value.description)\n\nconst typeCheck = () => {\n const boxed = box(Symbol('foo'), {} as RevivableContext)\n // Description-bearing symbols still round-trip through this revive.\n const revived = revive({ description: 'foo' }, {} as RevivableContext)\n const expected: symbol = revived\n // @ts-expect-error - not a string\n const notString: string = revived\n // @ts-expect-error - cannot box non-symbol\n box('not a symbol', {} as RevivableContext)\n}\n","import type { BoxBase as BoxBaseType, RevivableContext } from './utils'\n\nimport { BoxBase } from './utils'\nimport { instanceOfAny } from '../utils/type-guards'\n\ntype AnyCtor = abstract new (...args: any[]) => unknown\n\n// -------------------------------------------------------------------------\n// clonable — pass-through fast path for HTML structured-clone types not\n// owned by another revivable. Short-circuits findBoxModule so unclonable's\n// structuredClone probe never fires on a known-safe value.\n// -------------------------------------------------------------------------\n\nconst TYPED_CLONABLE_CTORS = [\n globalThis.File,\n globalThis.FileList,\n globalThis.RegExp,\n globalThis.DataView,\n globalThis.ImageData,\n globalThis.FormData,\n globalThis.DOMException,\n globalThis.DOMMatrix,\n globalThis.DOMMatrixReadOnly,\n globalThis.DOMPoint,\n globalThis.DOMPointReadOnly,\n globalThis.DOMQuad,\n globalThis.DOMRect,\n globalThis.DOMRectReadOnly,\n globalThis.CryptoKey,\n globalThis.FileSystemHandle,\n globalThis.FileSystemFileHandle,\n globalThis.FileSystemDirectoryHandle,\n globalThis.RTCCertificate,\n] as const\n\nconst EXPERIMENTAL_CLONABLE_CTORS = [\n (globalThis as { CropTarget?: AnyCtor }).CropTarget,\n (globalThis as { EncodedAudioChunk?: AnyCtor }).EncodedAudioChunk,\n (globalThis as { EncodedVideoChunk?: AnyCtor }).EncodedVideoChunk,\n (globalThis as { FencedFrameConfig?: AnyCtor }).FencedFrameConfig,\n (globalThis as { GPUCompilationInfo?: AnyCtor }).GPUCompilationInfo,\n (globalThis as { GPUCompilationMessage?: AnyCtor }).GPUCompilationMessage,\n (globalThis as { GPUPipelineError?: AnyCtor }).GPUPipelineError,\n (globalThis as { RTCEncodedAudioFrame?: AnyCtor }).RTCEncodedAudioFrame,\n (globalThis as { RTCEncodedVideoFrame?: AnyCtor }).RTCEncodedVideoFrame,\n (globalThis as { WebTransportError?: AnyCtor }).WebTransportError,\n] as const\n\nexport type Clonable = InstanceType<typeof TYPED_CLONABLE_CTORS[number]>\nexport type BoxedClonable = BoxBaseType<'clonable'>\n\n// `capableOnly: true` tells ExtractType to elide this module from the\n// Capable union on JSON transports — TS can't narrow `isType<Ctx>` via\n// generic inference, so we use a marker flag.\nconst isClonable = (value: unknown): value is Clonable =>\n instanceOfAny(value, TYPED_CLONABLE_CTORS) || instanceOfAny(value, EXPERIMENTAL_CLONABLE_CTORS)\n\nexport const clonable = {\n type: 'clonable',\n capableOnly: true,\n isType: isClonable,\n // Pass-through; structured-clone handles these on the wire. `revive` is\n // never reached — `box` returns the raw value so isRevivableBox is false.\n box: (value: Clonable, _context: RevivableContext<any>): Clonable => value,\n revive: (value: BoxedClonable, _context: RevivableContext<any>): Clonable => value as unknown as Clonable,\n} as const\n\n// -------------------------------------------------------------------------\n// transferable — pass-through fast path for transfer-only host objects.\n// getTransferableObjects pulls them out of the envelope at send time.\n// -------------------------------------------------------------------------\n\nconst TYPED_TRANSFERABLE_CTORS = [\n globalThis.ImageBitmap,\n globalThis.OffscreenCanvas,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.MediaStreamTrack,\n globalThis.RTCDataChannel,\n] as const\n\nconst EXPERIMENTAL_TRANSFERABLE_CTORS = [\n (globalThis as { AudioData?: AnyCtor }).AudioData,\n (globalThis as { VideoFrame?: AnyCtor }).VideoFrame,\n (globalThis as { MediaSourceHandle?: AnyCtor }).MediaSourceHandle,\n (globalThis as { MIDIAccess?: AnyCtor }).MIDIAccess,\n (globalThis as { WebTransportReceiveStream?: AnyCtor }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: AnyCtor }).WebTransportSendStream,\n] as const\n\nexport type Transferable = InstanceType<typeof TYPED_TRANSFERABLE_CTORS[number]>\nexport type BoxedTransferable = BoxBaseType<'transferable'>\n\nconst isTransferable = (value: unknown): value is Transferable =>\n instanceOfAny(value, TYPED_TRANSFERABLE_CTORS) || instanceOfAny(value, EXPERIMENTAL_TRANSFERABLE_CTORS)\n\nexport const transferable = {\n type: 'transferable',\n capableOnly: true,\n isType: isTransferable,\n box: (value: Transferable, _context: RevivableContext<any>): Transferable => value,\n revive: (value: BoxedTransferable, _context: RevivableContext<any>): Transferable => value as unknown as Transferable,\n} as const\n\n// -------------------------------------------------------------------------\n// unclonable — catch-all that probes via structuredClone and coerces\n// unclonables to `{}` so the wire never blows up on exotic host objects.\n// -------------------------------------------------------------------------\n\nconst isPlainObject = (value: unknown): boolean => {\n if (value === null || typeof value !== 'object') return false\n const proto = Object.getPrototypeOf(value)\n return proto === Object.prototype || proto === null\n}\n\nconst isUnclonable = (value: unknown): boolean => {\n if (value === null) return false\n const t = typeof value\n if (t !== 'object') return false\n if (Array.isArray(value)) return false\n if (isPlainObject(value)) return false\n try {\n structuredClone(value)\n return false\n } catch {\n return true\n }\n}\n\nexport type BoxedUnclonable = BoxBaseType<'unclonable'>\n\n// Type-level lie: `value is never` so this module doesn't widen Capable.\n// Coercion to `{}` is a runtime rescue for values we shouldn't see.\nconst isUnclonableTyped = isUnclonable as (value: unknown) => value is never\n\nexport const unclonable = {\n type: 'unclonable',\n isType: isUnclonableTyped,\n box: (_value: never, _context: RevivableContext<any>): BoxedUnclonable => ({ ...BoxBase, type: 'unclonable' }),\n revive: (_value: BoxedUnclonable, _context: RevivableContext<any>): Record<string, never> => ({}),\n} as const\n","import type { BoxBase, RevivableContext } from './utils'\nimport type { DeepReplaceWithBox, DeepReplaceWithRevive, ReplaceWithBox, ReplaceWithRevive } from '../utils/replace'\nimport type { MessageFields, Capable } from '../types'\n\nimport { isRevivableBox } from './utils'\nimport * as arrayBuffer from './array-buffer'\nimport * as date from './date'\nimport * as headers from './headers'\nimport * as error from './error'\nimport * as typedArray from './typed-array'\nimport * as promise from './promise'\nimport * as func from './function'\nimport * as messagePort from './message-port'\nimport * as readableStream from './readable-stream'\nimport * as writableStream from './writable-stream'\nimport * as abortSignal from './abort-signal'\nimport * as response from './response'\nimport * as request from './request'\nimport * as identity from './identity'\nimport * as transfer from './transfer'\nimport * as map from './map'\nimport * as set from './set'\nimport * as bigInt from './bigint'\nimport * as event from './event'\nimport * as eventTarget from './event-target'\nimport * as blob from './blob'\nimport * as symbol from './symbol'\nimport { clonable, transferable, unclonable } from './fallbacks'\n\nexport { identity } from './identity'\nexport { transfer } from './transfer'\n\nexport * from './utils'\n\n// `any` on box/revive/init: each module's concrete box has a narrower input\n// than the shared interface can express, and TS treats readonly function\n// types contravariantly. The bivariance escape hatch lets modules assign.\nexport type RevivableModule<\n T extends string = string,\n T2 = any,\n T3 extends BoxBase<T> = any,\n T4 extends MessageFields = MessageFields,\n> = {\n readonly type: T\n readonly isType: (value: unknown) => value is T2\n readonly box: ((value: T2, context: RevivableContext<any>) => T3) | ((...args: any[]) => any)\n readonly revive: (value: T3, context: RevivableContext<any>) => T2\n readonly init?: (context: RevivableContext<any>) => void\n readonly Messages?: T4\n}\n\nexport const defaultRevivableModules = [\n transfer,\n identity,\n arrayBuffer,\n date,\n headers,\n error,\n typedArray,\n // blob MUST come before clonable — clonable would otherwise pass-through\n // a Blob unboxed, which works on clone transports but loses the data on\n // JSON. Blob's isType excludes File so File still rides clonable.\n blob,\n promise,\n func,\n messagePort,\n readableStream,\n writableStream,\n abortSignal,\n response,\n request,\n map,\n set,\n bigInt,\n symbol,\n event,\n // eventTarget MUST be last among instanceof-EventTarget revivables —\n // MessagePort/AbortSignal/Window/Worker all extend EventTarget; the\n // specific ones need first dibs via findBoxModule iteration order.\n eventTarget,\n // Pass-through fast paths for wire-safe types — short-circuit findBoxModule\n // before unclonable's structuredClone probe runs on a known-safe value.\n clonable,\n transferable,\n // Catch-all: structuredClone-probes and coerces unclonables to `{}`,\n // matching JSON.stringify(new WeakMap()) === \"{}\".\n unclonable,\n] as const\n\nexport type DefaultRevivableModules = typeof defaultRevivableModules\nexport type DefaultRevivableModule = DefaultRevivableModules[number]\n\nconst findBoxModule = (\n value: unknown,\n modules: readonly RevivableModule[]\n): RevivableModule | undefined =>\n modules.find(module => module.isType(value))\n\nconst findReviveModule = (\n value: BoxBase,\n modules: readonly RevivableModule[],\n): RevivableModule | undefined =>\n modules.find(module => module.type === value.type)\n\nconst isPlainObject = (value: unknown): value is Record<string, Capable> =>\n !!value && typeof value === 'object' && Object.getPrototypeOf(value) === Object.prototype\n\nconst descend = <TOut>(value: unknown, transform: (v: Capable) => unknown): TOut => {\n if (Array.isArray(value)) {\n return value.map(v => transform(v)) as TOut\n }\n if (isPlainObject(value)) {\n return Object.fromEntries(\n Object.entries<Capable>(value).map(([k, v]) => [k, transform(v)]),\n ) as TOut\n }\n return value as TOut\n}\n\nexport const box = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): ReplaceWithBox<T, TModules[number]> => {\n const handledByModule = findBoxModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.box(value, context) as ReplaceWithBox<T, TModules[number]>\n }\n return value as ReplaceWithBox<T, TModules[number]>\n}\n\nexport const recursiveBox = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): DeepReplaceWithBox<T, TModules[number]> => {\n type ReturnCastType = DeepReplaceWithBox<T, TModules[number]>\n // Already-boxed values pass through — revivables may embed a pre-built\n // BoxedX in their outgoing payload; descending would re-box raw ports.\n if (isRevivableBox(value)) return value as ReturnCastType\n const handledByModule = findBoxModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.box(value, context) as ReturnCastType\n }\n return descend<ReturnCastType>(value, v => recursiveBox(v, context))\n}\n\nexport const revive = <\n T extends ReturnType<typeof box>,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): ReplaceWithRevive<T, TModules[number]> => {\n if (!isRevivableBox(value)) return value as ReplaceWithRevive<T, TModules[number]>\n const handledByModule = findReviveModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.revive(value, context) as ReplaceWithRevive<T, TModules[number]>\n }\n return value as ReplaceWithRevive<T, TModules[number]>\n}\n\nexport const recursiveRevive = <\n T extends Capable,\n TModules extends readonly RevivableModule[]\n>(\n value: T,\n context: RevivableContext<TModules>\n): DeepReplaceWithRevive<T, TModules[number]> => {\n type ReturnCastType = DeepReplaceWithRevive<T, TModules[number]>\n if (isRevivableBox(value)) {\n const handledByModule = findReviveModule(value, context.revivableModules)\n if (handledByModule) {\n return handledByModule.revive(value, context) as ReturnCastType\n }\n }\n return descend<ReturnCastType>(value, v => recursiveRevive(v, context))\n}","import type { Transport } from '../utils/transport'\nimport type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { DeepReplaceWithBox } from '../utils/replace'\nimport type { ProtocolContext } from './utils'\nimport type {\n Capable, MessageEventTarget, MessageFields,\n MessageVariant, Uuid,\n} from '../types'\n\nimport { recursiveBox, recursiveRevive } from '../revivables'\nimport { isEmitTransport, isReceiveTransport } from '../utils/type-guards'\n\nexport const type = 'bidirectional' as const\n\nexport type InitMessage<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> = {\n type: 'init'\n remoteUuid: Uuid\n data: DeepReplaceWithBox<T, TModules[number]>\n}\n\nexport declare const Messages: <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n>(modules: TModules, value: T) =>\n | InitMessage<TModules, T>\n\nexport type Messages<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> = ReturnType<typeof Messages<TModules, T>>\n\nexport type ConnectionContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n type: 'bidirectional'\n eventTarget: MessageEventTarget<TModules>\n connection: BidirectionalConnection<TModules>\n}\n\nexport type ConnectionRevivableContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n remoteUuid: Uuid\n sendMessage: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n eventTarget: MessageEventTarget<TModules>\n}\n\nexport const startBidirectionalConnection = <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n>(\n { transport, value, remoteUuid, eventTarget, send, revivableModules }:\n {\n transport: Transport\n value: Capable<TModules>\n remoteUuid: Uuid\n eventTarget: MessageEventTarget<TModules>\n send: (message: MessageFields & Record<string, unknown>) => void\n revivableModules: TModules\n },\n) => {\n const revivableContext = {\n transport,\n remoteUuid,\n sendMessage: send,\n eventTarget,\n revivableModules\n } satisfies ConnectionRevivableContext<TModules>\n\n for (const module of revivableModules) {\n module.init?.(revivableContext)\n }\n\n const { promise, resolve } = Promise.withResolvers<InitMessage<TModules>['data']>()\n\n eventTarget.addEventListener('message', function listener ({ detail }) {\n if (detail.type === 'init') {\n resolve(detail.data)\n eventTarget.removeEventListener('message', listener)\n }\n })\n\n send({\n type: 'init',\n remoteUuid,\n data: recursiveBox(value, revivableContext)\n })\n\n return {\n revivableContext,\n remoteValue:\n promise\n .then(initData => recursiveRevive(initData, revivableContext) as Capable),\n }\n}\n\nexport type BidirectionalConnection<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n revivableContext: ConnectionRevivableContext<TModules>\n remoteValue: Promise<Capable>\n}\n\n/** Mounts bidirectional mode on the shared protocol context. Only active\n * when the transport can both emit and receive. */\nexport const init = <TModules extends readonly RevivableModule[]>(\n ctx: ProtocolContext<TModules>\n): void => {\n if (!(isEmitTransport(ctx.transport) && isReceiveTransport(ctx.transport))) return\n\n ctx.protocolEventTarget.addEventListener('message', ({ detail: message }) => {\n if (message.type === 'announce') {\n if (!message.remoteUuid) {\n ctx.sendMessage({ type: 'announce', remoteUuid: message.uuid })\n return\n }\n if (message.remoteUuid !== ctx.getUuid()) return\n // Already-tracked uuid is the normal handshake-echo (peer re-announcing\n // back after our reply), not a collision — drop it.\n if (ctx.connectionContexts.has(message.uuid)) return\n // Echo announce back in case the peer missed our initial one.\n ctx.sendMessage({ type: 'announce', remoteUuid: message.uuid })\n const eventTarget = ctx.createConnectionEventTarget()\n const connectionContext = {\n type: 'bidirectional',\n eventTarget,\n connection:\n startBidirectionalConnection<TModules>({\n transport: ctx.transport,\n value: ctx.value,\n remoteUuid: message.uuid,\n eventTarget,\n send: (m) => ctx.sendMessage(m as MessageVariant),\n revivableModules: ctx.revivableModules\n })\n } satisfies ConnectionContext<TModules>\n ctx.connectionContexts.set(message.uuid, connectionContext)\n connectionContext.connection.remoteValue.then((remoteValue) =>\n ctx.resolveRemoteValue(remoteValue)\n )\n return\n }\n if (message.type === 'close') {\n if (message.remoteUuid !== ctx.getUuid()) return\n ctx.connectionContexts.delete(message.uuid)\n return\n }\n // \"init\" | \"message\" | \"message-port-close\"\n if (message.remoteUuid !== ctx.getUuid()) return\n const connection = ctx.connectionContexts.get(message.uuid)\n // drop messages from peers we haven't tracked (pre-announce or post-close)\n if (!connection) return\n connection.eventTarget.dispatchEvent(\n new CustomEvent('message', { detail: message })\n )\n })\n\n if (ctx.presetRemoteUuid !== undefined) {\n const eventTarget = ctx.createConnectionEventTarget()\n const connectionContext = {\n type: 'bidirectional',\n eventTarget,\n connection:\n startBidirectionalConnection<TModules>({\n transport: ctx.transport,\n value: ctx.value,\n remoteUuid: ctx.presetRemoteUuid,\n eventTarget,\n send: (m) => ctx.sendMessage(m as MessageVariant),\n revivableModules: ctx.revivableModules\n })\n } satisfies ConnectionContext<TModules>\n ctx.connectionContexts.set(ctx.presetRemoteUuid, connectionContext)\n connectionContext.connection.remoteValue.then((remoteValue) =>\n ctx.resolveRemoteValue(remoteValue)\n )\n return\n }\n\n ctx.sendMessage({ type: 'announce' })\n}\n","import type { UnderlyingType } from './type'\n\nexport type EventMap = Record<string, Event>\n\nexport interface TypedEventTarget<T extends EventMap> extends EventTarget {\n [UnderlyingType]?: T\n\n addEventListener<K extends keyof T & string>(\n type: K,\n listener: ((event: T[K]) => void) | null,\n options?: boolean | AddEventListenerOptions\n ): void\n addEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | AddEventListenerOptions\n ): void\n\n removeEventListener<K extends keyof T & string>(\n type: K,\n listener: ((event: T[K]) => void) | null,\n options?: boolean | EventListenerOptions\n ): void\n removeEventListener(\n type: string,\n listener: EventListenerOrEventListenerObject | null,\n options?: boolean | EventListenerOptions\n ): void\n}\n\n/**\n * Create a new `TypedEventTarget<T>` for a given event map. Centralises the\n * `EventTarget` → `TypedEventTarget<T>` cast so individual call sites don't\n * each need their own (`EventTarget` lacks the generic event map at the type\n * level, but the runtime behaviour is identical).\n */\nexport const createTypedEventTarget = <T extends EventMap>(): TypedEventTarget<T> =>\n new EventTarget() as TypedEventTarget<T>\n","import { transfer } from '../revivables/transfer'\nimport { isRevivableBox } from '../revivables/utils'\nimport { instanceOfAny, isClonable, isTransferable } from './type-guards'\n\nexport { transfer }\n\n// Must-transfer types: structured clone can't copy these, so any occurrence\n// in the outgoing message must go on the transfer list — opt-in or not.\n// (MessagePort is the canonical case: cloning would leave the peer mute.)\nconst isMustTransfer = (value: unknown): value is Transferable =>\n instanceOfAny(value, [\n globalThis.MessagePort,\n globalThis.ReadableStream,\n globalThis.WritableStream,\n globalThis.TransformStream,\n globalThis.OffscreenCanvas,\n (globalThis as { MediaSourceHandle?: abstract new (...args: any[]) => unknown }).MediaSourceHandle,\n (globalThis as { MediaStreamTrack?: abstract new (...args: any[]) => unknown }).MediaStreamTrack,\n (globalThis as { MIDIAccess?: abstract new (...args: any[]) => unknown }).MIDIAccess,\n (globalThis as { RTCDataChannel?: abstract new (...args: any[]) => unknown }).RTCDataChannel,\n (globalThis as { WebTransportReceiveStream?: abstract new (...args: any[]) => unknown }).WebTransportReceiveStream,\n (globalThis as { WebTransportSendStream?: abstract new (...args: any[]) => unknown }).WebTransportSendStream,\n ])\n\n// Structural check — keeps the walker decoupled from the module graph.\n// `degraded` (set by transfer.box) means the wrapper is a no-op here.\nconst isTransferBox = (value: unknown): value is { inner: unknown, degraded: boolean } =>\n isRevivableBox(value) && value.type === 'transfer'\n\n/** Walk a boxed message and collect Transferables to move (rather than copy)\n * on postMessage:\n * 1. Must-transfer types are always included.\n * 2. Clonable types (SharedArrayBuffer) are skipped.\n * 3. Other Transferables are included only inside a non-degraded transfer\n * box (user opted in AND the platform supports transferring). */\nexport const getTransferableObjects = (value: unknown): Transferable[] => {\n const transferables: Transferable[] = []\n const seen = new WeakSet<object>()\n\n const recurse = (value: unknown, inTransferBox: boolean): void => {\n if (!value || typeof value !== 'object') return\n if (seen.has(value)) return\n seen.add(value)\n\n if (isClonable(value)) return\n\n if (isTransferBox(value)) {\n // Non-degraded box flips into transfer mode for everything below.\n recurse(value.inner, inTransferBox || !value.degraded)\n return\n }\n\n if (isMustTransfer(value)) {\n transferables.push(value)\n return\n }\n\n if (isTransferable(value)) {\n if (inTransferBox) {\n transferables.push(value)\n }\n return\n }\n\n // TypedArray / DataView expose every numeric index — iterating a 100 KB\n // buffer would walk 100 K entries for nothing. The underlying buffer is\n // the only candidate; the typed-array revivable handles that path.\n if (ArrayBuffer.isView(value)) return\n\n if (Array.isArray(value)) {\n for (const item of value) recurse(item, inTransferBox)\n return\n }\n\n for (const item of Object.values(value)) recurse(item, inTransferBox)\n }\n\n recurse(value, false)\n return transferables\n}\n","import type {\n Message, MessageVariant, Uuid,\n Capable, MessageEventMap\n} from '../types'\nimport type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { Transport } from '../utils/transport'\nimport type { ConnectionContext } from '.'\nimport type { TypedEventTarget } from '../utils/typed-event-target'\n\nimport { defaultRevivableModules } from '../revivables'\nimport { isJsonOnlyTransport, isCustomTransport } from '../utils/type-guards'\n\nexport const normalizeTransport = (transport: Transport): Transport => {\n const isJson =\n 'isJson' in transport && transport.isJson !== undefined\n ? transport.isJson\n : isJsonOnlyTransport(transport)\n const ports =\n isCustomTransport(transport)\n ? transport\n : { emit: transport, receive: transport }\n return { isJson, ...ports } satisfies Transport\n}\n\n/** Resolves the final revivable module list. The user supplies a function\n * that takes the defaults and returns whatever ordering/composition they\n * want — add modules, drop defaults, reorder, override per-type. When\n * omitted, the defaults are used as-is. */\nexport const mergeRevivableModules = <\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n>(\n configure: ((defaults: DefaultRevivableModules) => TModules) | undefined,\n): TModules =>\n configure\n ? configure(defaultRevivableModules)\n : defaultRevivableModules as unknown as TModules\n\nexport type ProtocolEventMap<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n message: CustomEvent<Message<TModules>>\n}\n\nexport type ProtocolEventTarget<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = TypedEventTarget<ProtocolEventMap<TModules>>\n\nexport type ProtocolContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n value: Capable<TModules>\n revivableModules: TModules\n connectionContexts: Map<string, ConnectionContext<TModules>>\n getUuid: () => Uuid\n presetRemoteUuid?: Uuid\n sendMessage: (message: MessageVariant) => void\n protocolEventTarget: ProtocolEventTarget<TModules>\n resolveRemoteValue: (value: Capable<TModules>) => void\n createConnectionEventTarget: () => TypedEventTarget<MessageEventMap<TModules>>\n}\n\nexport type StartConnectionsOptions<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> = {\n transport: Transport\n name?: string\n remoteName?: string\n key?: string\n origin?: string\n unregisterSignal?: AbortSignal\n /** Configure the revivable module list. Receives the defaults and\n * returns the final ordered list — add modules, drop defaults, reorder,\n * or override per-type as needed. */\n revivableModules?: (defaults: DefaultRevivableModules) => TModules\n uuid?: Uuid\n remoteUuid?: Uuid\n}\n","import type { Transport } from '../utils/transport'\n\nimport { OSRA_DEFAULT_KEY } from '../types'\nimport { isEmitTransport, isReceiveTransport } from '../utils/type-guards'\nimport { getTransferableObjects } from '../utils/transferable'\nimport {\n registerOsraMessageListener,\n sendOsraMessage,\n} from '../utils/transport'\nimport { normalizeTransport } from './utils'\n\nexport type RelayOptions = {\n key?: string\n origin?: string\n originA?: string\n originB?: string\n nameA?: string\n nameB?: string\n unregisterSignal?: AbortSignal\n}\n\nexport const relay = (\n transportA: Transport,\n transportB: Transport,\n {\n key = OSRA_DEFAULT_KEY,\n origin = '*',\n originA = origin,\n originB = origin,\n nameA,\n nameB,\n unregisterSignal,\n }: RelayOptions = {},\n): void => {\n const a = normalizeTransport(transportA)\n const b = normalizeTransport(transportB)\n\n const forward = (\n from: Transport,\n to: Transport,\n toOrigin: string,\n remoteName: string | undefined,\n ): void => {\n if (!isReceiveTransport(from) || !isEmitTransport(to)) return\n registerOsraMessageListener({\n transport: from,\n key,\n remoteName,\n unregisterSignal,\n listener: (message) => {\n sendOsraMessage(to, message, toOrigin, getTransferableObjects(message))\n },\n })\n }\n\n forward(a, b, originB, nameA)\n forward(b, a, originA, nameB)\n}\n","import type { DefaultRevivableModules, RevivableModule } from '../revivables'\nimport type { ConnectionContext as BidirectionalConnectionContext } from './bidirectional'\nimport type {\n ProtocolContext,\n StartConnectionsOptions,\n} from '../utils'\nimport type {\n Message, MessageVariant, Uuid,\n Capable,\n} from '../types'\nimport type { MessageContext } from '../utils/transport'\n\nimport { OSRA_DEFAULT_KEY, OSRA_KEY } from '../types'\nimport * as bidirectional from './bidirectional'\nimport {\n isEmitTransport,\n isReceiveTransport,\n} from '../utils/type-guards'\nimport { createTypedEventTarget } from '../utils/typed-event-target'\nimport { getTransferableObjects } from '../utils/transferable'\nimport { registerOsraMessageListener, sendOsraMessage } from '../utils/transport'\nimport { mergeRevivableModules, normalizeTransport } from './utils'\n\nexport * from './bidirectional'\nexport * from './relay'\nexport * from './utils'\n\nexport type ConnectionModule<T> = {\n readonly type: string\n // ProtocolContext<any> rather than ProtocolContext<readonly RevivableModule[]>\n // for the same bivariance reason as RevivableModule.box — concrete modules\n // declare narrower context generics than the shared interface can express.\n readonly init: (ctx: ProtocolContext<any>) => void\n readonly Messages?: T\n}\n\nexport const connections = [\n bidirectional\n] as const\n\nexport type DefaultConnectionModules = typeof connections\nexport type DefaultConnectionModule = DefaultConnectionModules[number]\n\nexport type ConnectionMessage<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n T extends Capable<TModules> = Capable<TModules>\n> =\n DefaultConnectionModule extends {\n Messages: (modules: TModules, value: T) => infer R\n }\n ? R\n : never\n\nexport type ConnectionContext<\n TModules extends readonly RevivableModule[] = DefaultRevivableModules\n> =\n | BidirectionalConnectionContext<TModules>\n\nexport const startConnections = <\n T = unknown,\n const TModules extends readonly RevivableModule[] = DefaultRevivableModules\n>(\n value: Capable<TModules>,\n {\n transport: _transport,\n name,\n remoteName,\n key = OSRA_DEFAULT_KEY,\n origin = '*',\n unregisterSignal,\n revivableModules: configureRevivableModules,\n uuid: _uuid,\n remoteUuid: presetRemoteUuid,\n }: StartConnectionsOptions<TModules>\n): Promise<T> => {\n const transport = normalizeTransport(_transport)\n const mergedRevivableModules = mergeRevivableModules<TModules>(configureRevivableModules)\n type MergedModules = typeof mergedRevivableModules\n const connectionContexts = new Map<string, ConnectionContext<MergedModules>>()\n\n const { promise: remoteValuePromise, resolve: resolveRemoteValue } =\n Promise.withResolvers<Capable<MergedModules>>()\n\n const uuid: Uuid = _uuid ?? globalThis.crypto.randomUUID()\n\n const sendMessage = (message: MessageVariant) => {\n if (unregisterSignal?.aborted) return\n if (!isEmitTransport(transport)) return\n const envelope = { [OSRA_KEY]: key, name, uuid, ...message }\n sendOsraMessage(transport, envelope, origin, getTransferableObjects(envelope))\n }\n\n const protocolEventTarget = createTypedEventTarget<{ message: CustomEvent<Message<MergedModules>> }>()\n\n const ctx: ProtocolContext<MergedModules> = {\n transport,\n value: value as Capable<MergedModules>,\n revivableModules: mergedRevivableModules,\n connectionContexts,\n getUuid: () => uuid,\n presetRemoteUuid,\n sendMessage,\n protocolEventTarget,\n resolveRemoteValue,\n createConnectionEventTarget: createTypedEventTarget,\n }\n\n const listener = (message: Message, _: MessageContext) => {\n // own message looped back on the channel\n if (message.uuid === uuid) return\n protocolEventTarget.dispatchEvent(\n new CustomEvent('message', { detail: message as Message<MergedModules> }),\n )\n }\n\n if (isReceiveTransport(transport)) {\n registerOsraMessageListener({\n listener,\n transport,\n remoteName,\n key,\n unregisterSignal\n })\n }\n\n for (const connectionModule of connections) {\n connectionModule.init(ctx)\n }\n\n return remoteValuePromise as Promise<T>\n}\n","import type { Capable } from './types'\nimport type { DefaultRevivableModules, RevivableContext } from './revivables'\nimport type { RevivableModule } from './revivables'\nimport type { StartConnectionsOptions } from './connections/utils'\nimport type { Transport } from './utils/transport'\nimport type {\n BadFieldValue, BadFieldPath, BadFieldParent,\n ErrorMessage, BadValue, Path, ParentObject\n} from './utils/capable-check'\n\nimport { startConnections } from './utils'\n\nexport * from './types'\nexport * from './revivables'\nexport * from './connections'\nexport * from './utils'\n\n/** Synthetic context so `Capable` can narrow on the inferred transport\n * without an actual context object at the call site. Only `transport`\n * matters; the rest is stubbed with the broadest types. */\ntype ContextOf<TTransport extends Transport> = RevivableContext & { transport: TTransport }\n\ntype CapableCheck<\n T,\n TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n Ctx extends RevivableContext = RevivableContext,\n> =\n T extends Capable<TModules, Ctx>\n ? T\n : T & {\n [ErrorMessage]: 'Value type must resolve to a Capable'\n [BadValue]: BadFieldValue<T, Capable<TModules, Ctx>>\n [Path]: BadFieldPath<T, Capable<TModules, Ctx>>\n [ParentObject]: BadFieldParent<T, Capable<TModules, Ctx>>\n }\n\nexport const expose = async <\n T = unknown,\n const TModules extends readonly RevivableModule[] = DefaultRevivableModules,\n const TTransport extends Transport = Transport,\n const TValue = Capable<TModules, ContextOf<TTransport>>\n>(\n value: CapableCheck<TValue, TModules, ContextOf<TTransport>>,\n options: StartConnectionsOptions<TModules> & { transport: TTransport }\n): Promise<T> =>\n startConnections<T, TModules>(\n value as Capable<TModules>,\n options\n )\n"],"mappings":";;;;;;;;GAQa,IAAW,gBACX,IAAmB,wBACnB,IAAW,gBCMlB,IAAoB,WAAsD,cAE1E,IAA+B;CACnC;CACA;CACA;CACA;CACA;CACA;CACA;CACA,cAAc;CACd;CACA;CACA;CACA;CACD,EAMK,IAAyB,OAAO,OAAO,EAA6B,EAE7D,KAAoB,MAAsC;CACrE,IAAM,IAAO,EAAM,YAAY;AAC/B,KAAI,KAAQ,EAA8B,QAAO;AAGjD,MAAK,IAAM,CAAC,GAAc,MAAS,OAAO,QAAQ,EAA6B,CAC7E,KAAI,KAAQ,aAAiB,EAAM,QAAO;AAE5C,OAAU,MAAM,2BAA2B;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