UNPKG

6.6 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright 2019 Google LLC
4 * SPDX-License-Identifier: Apache-2.0
5 */
6import { Endpoint, EventSource, PostMessageWithOrigin } from "./protocol";
7export type { Endpoint };
8export declare const proxyMarker: unique symbol;
9export declare const createEndpoint: unique symbol;
10export declare const releaseProxy: unique symbol;
11export declare const finalizer: unique symbol;
12/**
13 * Interface of values that were marked to be proxied with `comlink.proxy()`.
14 * Can also be implemented by classes.
15 */
16export interface ProxyMarked {
17 [proxyMarker]: true;
18}
19/**
20 * Takes a type and wraps it in a Promise, if it not already is one.
21 * This is to avoid `Promise<Promise<T>>`.
22 *
23 * This is the inverse of `Unpromisify<T>`.
24 */
25type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
26/**
27 * Takes a type that may be Promise and unwraps the Promise type.
28 * If `P` is not a Promise, it returns `P`.
29 *
30 * This is the inverse of `Promisify<T>`.
31 */
32type Unpromisify<P> = P extends Promise<infer T> ? T : P;
33/**
34 * Takes the raw type of a remote property and returns the type that is visible to the local thread on the proxy.
35 *
36 * Note: This needs to be its own type alias, otherwise it will not distribute over unions.
37 * See https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types
38 */
39type RemoteProperty<T> = T extends Function | ProxyMarked ? Remote<T> : Promisify<T>;
40/**
41 * 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
42 * argument) and returns the type that the local thread has to supply.
43 *
44 * This is the inverse of `RemoteProperty<T>`.
45 *
46 * Note: This needs to be its own type alias, otherwise it will not distribute over unions. See
47 * https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types
48 */
49type LocalProperty<T> = T extends Function | ProxyMarked ? Local<T> : Unpromisify<T>;
50/**
51 * Proxies `T` if it is a `ProxyMarked`, clones it otherwise (as handled by structured cloning and transfer handlers).
52 */
53export type ProxyOrClone<T> = T extends ProxyMarked ? Remote<T> : T;
54/**
55 * Inverse of `ProxyOrClone<T>`.
56 */
57export type UnproxyOrClone<T> = T extends RemoteObject<ProxyMarked> ? Local<T> : T;
58/**
59 * Takes the raw type of a remote object in the other thread and returns the type as it is visible to the local thread
60 * when proxied with `Comlink.proxy()`.
61 *
62 * This does not handle call signatures, which is handled by the more general `Remote<T>` type.
63 *
64 * @template T The raw type of a remote object as seen in the other thread.
65 */
66export type RemoteObject<T> = {
67 [P in keyof T]: RemoteProperty<T[P]>;
68};
69/**
70 * Takes the type of an object as a remote thread would see it through a proxy (e.g. when passed in as a function
71 * argument) and returns the type that the local thread has to supply.
72 *
73 * This does not handle call signatures, which is handled by the more general `Local<T>` type.
74 *
75 * This is the inverse of `RemoteObject<T>`.
76 *
77 * @template T The type of a proxied object.
78 */
79export type LocalObject<T> = {
80 [P in keyof T]: LocalProperty<T[P]>;
81};
82/**
83 * Additional special comlink methods available on each proxy returned by `Comlink.wrap()`.
84 */
85export interface ProxyMethods {
86 [createEndpoint]: () => Promise<MessagePort>;
87 [releaseProxy]: () => void;
88}
89/**
90 * Takes the raw type of a remote object, function or class in the other thread and returns the type as it is visible to
91 * the local thread from the proxy return value of `Comlink.wrap()` or `Comlink.proxy()`.
92 */
93export type Remote<T> = RemoteObject<T> & (T extends (...args: infer TArguments) => infer TReturn ? (...args: {
94 [I in keyof TArguments]: UnproxyOrClone<TArguments[I]>;
95}) => Promisify<ProxyOrClone<Unpromisify<TReturn>>> : unknown) & (T extends {
96 new (...args: infer TArguments): infer TInstance;
97} ? {
98 new (...args: {
99 [I in keyof TArguments]: UnproxyOrClone<TArguments[I]>;
100 }): Promisify<Remote<TInstance>>;
101} : unknown) & ProxyMethods;
102/**
103 * Expresses that a type can be either a sync or async.
104 */
105type MaybePromise<T> = Promise<T> | T;
106/**
107 * Takes the raw type of a remote object, function or class as a remote thread would see it through a proxy (e.g. when
108 * passed in as a function argument) and returns the type the local thread has to supply.
109 *
110 * This is the inverse of `Remote<T>`. It takes a `Remote<T>` and returns its original input `T`.
111 */
112export type Local<T> = Omit<LocalObject<T>, keyof ProxyMethods> & (T extends (...args: infer TArguments) => infer TReturn ? (...args: {
113 [I in keyof TArguments]: ProxyOrClone<TArguments[I]>;
114}) => MaybePromise<UnproxyOrClone<Unpromisify<TReturn>>> : unknown) & (T extends {
115 new (...args: infer TArguments): infer TInstance;
116} ? {
117 new (...args: {
118 [I in keyof TArguments]: ProxyOrClone<TArguments[I]>;
119 }): MaybePromise<Local<Unpromisify<TInstance>>>;
120} : unknown);
121/**
122 * Customizes the serialization of certain values as determined by `canHandle()`.
123 *
124 * @template T The input type being handled by this transfer handler.
125 * @template S The serialized type sent over the wire.
126 */
127export interface TransferHandler<T, S> {
128 /**
129 * Gets called for every value to determine whether this transfer handler
130 * should serialize the value, which includes checking that it is of the right
131 * type (but can perform checks beyond that as well).
132 */
133 canHandle(value: unknown): value is T;
134 /**
135 * Gets called with the value if `canHandle()` returned `true` to produce a
136 * value that can be sent in a message, consisting of structured-cloneable
137 * values and/or transferrable objects.
138 */
139 serialize(value: T): [S, Transferable[]];
140 /**
141 * Gets called to deserialize an incoming value that was serialized in the
142 * other thread with this transfer handler (known through the name it was
143 * registered under).
144 */
145 deserialize(value: S): T;
146}
147/**
148 * Allows customizing the serialization of certain values.
149 */
150export declare const transferHandlers: Map<string, TransferHandler<unknown, unknown>>;
151export declare function expose(obj: any, ep?: Endpoint, allowedOrigins?: (string | RegExp)[]): void;
152export declare function wrap<T>(ep: Endpoint, target?: any): Remote<T>;
153export declare function transfer<T>(obj: T, transfers: Transferable[]): T;
154export declare function proxy<T extends {}>(obj: T): T & ProxyMarked;
155export declare function windowEndpoint(w: PostMessageWithOrigin, context?: EventSource, targetOrigin?: string): Endpoint;
156
\No newline at end of file