1
>
/*---------------------------------------------------------------------------------------------
proxyIdentifier.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import type { VSBuffer } from '../../../../base/common/buffer.js';
7
>
import type { CancellationToken } from '../../../../base/common/cancellation.js';
8
>
9
>
export interface IRPCProtocol {
10
>
/**
11
>
* Returns a proxy to an object addressable/named in the extension host process or in the renderer process.
12
>
*/
13
>
getProxy<T>(identifier: ProxyIdentifier<T>): Proxied<T>;
14
>
15
>
/**
16
>
* Register manually created instance.
17
>
*/
18
>
set<T, R extends T>(identifier: ProxyIdentifier<T>, instance: R): R;
19
>
20
>
/**
21
>
* Assert these identifiers are already registered via `.set`.
22
>
*/
23
>
assertRegistered(identifiers: ProxyIdentifier<unknown>[]): void;
24
>
25
>
/**
26
>
* Wait for the write buffer (if applicable) to become empty.
27
>
*/
28
>
drain(): Promise<void>;
29
>
30
>
dispose(): void;
31
>
}
32
>
33
>
export class ProxyIdentifier<T> {
34
>
public static count = 0;
35
>
_proxyIdentifierBrand: void = undefined;
36
>
37
>
public readonly sid: string;
38
>
public readonly nid: number;
39
>
40
>
constructor(sid: string) {
41
this.sid = sid;
42
this.nid = (++ProxyIdentifier.count);
43
}
45
>
46
>
const identifiers: ProxyIdentifier<unknown>[] = [];
47
>
48
>
export function createProxyIdentifier<T>(identifier: string): ProxyIdentifier<T> {
49
const result = new ProxyIdentifier<T>(identifier);
50
identifiers[result.nid] = result;
51
return result;
52
}
54
>
/**
55
>
* Mapped-type that replaces all JSONable-types with their toJSON-result type
56
>
*/
57
>
export type Dto<T> = T extends { toJSON(): infer U }
58
>
? U
59
>
: T extends VSBuffer // VSBuffer is understood by rpc-logic
60
>
? T
61
>
: T extends CancellationToken // CancellationToken is understood by rpc-logic
62
>
? T
63
>
: T extends Function // functions are dropped during JSON-stringify
64
>
? never
65
>
: T extends object // recurse
66
>
? { [k in keyof T]: Dto<T[k]>; }
67
>
: T;
68
>
69
>
export type Proxied<T> = { [K in keyof T]: T[K] extends (...args: infer A) => infer R
70
>
? (...args: { [K in keyof A]: Dto<A[K]> }) => Promise<Dto<Awaited<R>>>
71
>
: never
72
>
};
73
>
74
>
export function getStringIdentifierForProxy(nid: number): string {
75
return identifiers[nid].sid;
76
}
78
>
/**
79
>
* Marks the object as containing buffers that should be serialized more efficiently.
80
>
*/
81
>
export class SerializableObjectWithBuffers<T> {
82
>
constructor(
83
public readonly value: T
84
) { }