1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import { Endpoint, EventSource, PostMessageWithOrigin } from "./protocol";
|
7 | export type { Endpoint };
|
8 | export declare const proxyMarker: unique symbol;
|
9 | export declare const createEndpoint: unique symbol;
|
10 | export declare const releaseProxy: unique symbol;
|
11 | export declare const finalizer: unique symbol;
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export interface ProxyMarked {
|
17 | [proxyMarker]: true;
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | type Unpromisify<P> = P extends Promise<infer T> ? T : P;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | type RemoteProperty<T> = T extends Function | ProxyMarked ? Remote<T> : Promisify<T>;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | type LocalProperty<T> = T extends Function | ProxyMarked ? Local<T> : Unpromisify<T>;
|
50 |
|
51 |
|
52 |
|
53 | export type ProxyOrClone<T> = T extends ProxyMarked ? Remote<T> : T;
|
54 |
|
55 |
|
56 |
|
57 | export type UnproxyOrClone<T> = T extends RemoteObject<ProxyMarked> ? Local<T> : T;
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | export type RemoteObject<T> = {
|
67 | [P in keyof T]: RemoteProperty<T[P]>;
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | export type LocalObject<T> = {
|
80 | [P in keyof T]: LocalProperty<T[P]>;
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 | export interface ProxyMethods {
|
86 | [createEndpoint]: () => Promise<MessagePort>;
|
87 | [releaseProxy]: () => void;
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export 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 | */
|
105 | type 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 | */
|
112 | export 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 | */
|
127 | export 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 | */
|
150 | export declare const transferHandlers: Map<string, TransferHandler<unknown, unknown>>;
|
151 | export declare function expose(obj: any, ep?: Endpoint, allowedOrigins?: (string | RegExp)[]): void;
|
152 | export declare function wrap<T>(ep: Endpoint, target?: any): Remote<T>;
|
153 | export declare function transfer<T>(obj: T, transfers: Transferable[]): T;
|
154 | export declare function proxy<T extends {}>(obj: T): T & ProxyMarked;
|
155 | export declare function windowEndpoint(w: PostMessageWithOrigin, context?: EventSource, targetOrigin?: string): Endpoint;
|
156 |
|
\ | No newline at end of file |