import { type Endpoint } from './types.ts'; export * from './types.ts'; export declare const proxyMarker: unique symbol; export declare const releaseProxy: unique symbol; export declare const finalizer: unique symbol; export declare const throwMarker: unique symbol; /** * Interface of values that were marked to be proxied with `abslink.proxy()`. * Can also be implemented by classes. */ export interface ProxyMarked { [proxyMarker]: number; } /** * Takes a type and wraps it in a Promise, if it not already is one. * This is to avoid `Promise>`. * * This is the inverse of `Unpromisify`. */ type Promisify = T extends Promise ? T : Promise; /** * Takes a type that may be Promise and unwraps the Promise type. * If `P` is not a Promise, it returns `P`. * * This is the inverse of `Promisify`. */ type Unpromisify

= P extends Promise ? T : P; /** * Takes the raw type of a remote property and returns the type that is visible to the local thread on the proxy. * * Note: This needs to be its own type alias, otherwise it will not distribute over unions. * See https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types */ type RemoteProperty = T extends Function | ProxyMarked ? Remote : T extends object ? Promisify & { [K in keyof T]: RemoteProperty; } : Promisify; /** * Takes the raw type of a property as a remote thread would see it through a proxy (e.g. when passed in as a function * argument) and returns the type that the local thread has to supply. * * This is the inverse of `RemoteProperty`. * * Note: This needs to be its own type alias, otherwise it will not distribute over unions. See * https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types */ type LocalProperty = T extends Function | ProxyMarked ? Local : Unpromisify; /** * Proxies `T` if it is a `ProxyMarked`, clones it otherwise (as handled by structured cloning and transfer handlers). */ export type ProxyOrClone = T extends ProxyMarked ? Remote : T; /** * Inverse of `ProxyOrClone`. */ export type UnproxyOrClone = T extends RemoteObject ? Local : T; /** * Takes the raw type of a remote object in the other thread and returns the type as it is visible to the local thread * when proxied with `Abslink.proxy()`. * * This does not handle call signatures, which is handled by the more general `Remote` type. * * @template T The raw type of a remote object as seen in the other thread. */ export type RemoteObject = { [P in keyof T]: RemoteProperty; }; /** * Takes the type of an object as a remote thread would see it through a proxy (e.g. when passed in as a function * argument) and returns the type that the local thread has to supply. * * This does not handle call signatures, which is handled by the more general `Local` type. * * This is the inverse of `RemoteObject`. * * @template T The type of a proxied object. */ export type LocalObject = { [P in keyof T]: LocalProperty; }; /** * Additional special abslink methods available on each proxy returned by `Abslink.wrap()`. */ export interface ProxyMethods { [releaseProxy]: () => Promise; } /** * Takes the raw type of a remote object, function or class in the other thread and returns the type as it is visible to * the local thread from the proxy return value of `Abslink.wrap()` or `Abslink.proxy()`. */ export type Remote = RemoteObject & (T extends (...args: infer TArguments) => infer TReturn ? (...args: { [I in keyof TArguments]: UnproxyOrClone; }) => Promisify>> : unknown) & (T extends new (...args: infer TArguments) => infer TInstance ? new (...args: { [I in keyof TArguments]: UnproxyOrClone; }) => Remote & Promisify> : unknown) & ProxyMethods; /** * Expresses that a type can be either a sync or async. */ type MaybePromise = Promise | T; /** * Takes the raw type of a remote object, function or class as a remote thread would see it through a proxy (e.g. when * passed in as a function argument) and returns the type the local thread has to supply. * * This is the inverse of `Remote`. It takes a `Remote` and returns its original input `T`. */ export type Local = Omit, keyof ProxyMethods> & (T extends (...args: infer TArguments) => infer TReturn ? (...args: { [I in keyof TArguments]: ProxyOrClone; }) => MaybePromise>> : unknown) & (T extends new (...args: infer TArguments) => infer TInstance ? new (...args: { [I in keyof TArguments]: ProxyOrClone; }) => MaybePromise>> : unknown); export declare const isObject: (val: unknown) => val is object; /** * Customizes the serialization of certain values as determined by `canHandle()`. * * @template T The input type being handled by this transfer handler. * @template S The serialized type sent over the wire. */ export interface TransferHandler { /** * Gets called for every value to determine whether this transfer handler * should serialize the value, which includes checking that it is of the right * type (but can perform checks beyond that as well). */ canHandle: (value: unknown) => value is T; /** * Gets called with the value if `canHandle()` returned `true` to produce a * value that can be sent in a message, consisting of structured-cloneable * values and/or transferrable objects. */ serialize: (value: T, ep: Endpoint) => [S, Transferable[]]; /** * Gets called to deserialize an incoming value that was serialized in the * other thread with this transfer handler (known through the name it was * registered under). */ deserialize: (value: S, ep: Endpoint) => T; } /** * Allows customizing the serialization of certain values. */ export declare const transferHandlers: Map>; export declare function expose(obj: T, ep: Endpoint, rootMarkerID?: number): T; export declare function wrap(endpoint: Endpoint, rootMarkerID?: number): Remote; export declare function transfer(obj: T, transfers: Transferable[]): T; export declare function proxy(obj: T): T & ProxyMarked;