cspell-lib 9.6.2 → 9.6.3
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/dist/cspell-rpc/client.d.ts +3 -3
- package/dist/cspell-rpc/client.js +1 -1
- package/dist/cspell-rpc/server.d.ts +3 -3
- package/dist/cspell-rpc/server.js +1 -1
- package/dist/lib/Settings/sanitizeSettings.js +2 -0
- package/dist/rpc.d.ts +1 -1
- package/dist/rpc.js +1 -1
- package/package.json +22 -21
- package/dist/rpc/Future.d.ts +0 -18
- package/dist/rpc/Future.js +0 -49
- package/dist/rpc/MessagePortEvents.d.ts +0 -56
- package/dist/rpc/MessagePortEvents.js +0 -94
- package/dist/rpc/assert.d.ts +0 -14
- package/dist/rpc/assert.js +0 -21
- package/dist/rpc/client.d.ts +0 -186
- package/dist/rpc/client.js +0 -364
- package/dist/rpc/errors.d.ts +0 -24
- package/dist/rpc/errors.js +0 -47
- package/dist/rpc/index.d.ts +0 -11
- package/dist/rpc/index.js +0 -6
- package/dist/rpc/messagePort.d.ts +0 -34
- package/dist/rpc/messagePort.js +0 -2
- package/dist/rpc/models.d.ts +0 -134
- package/dist/rpc/models.js +0 -2
- package/dist/rpc/modelsHelpers.d.ts +0 -56
- package/dist/rpc/modelsHelpers.js +0 -117
- package/dist/rpc/notify.d.ts +0 -79
- package/dist/rpc/notify.js +0 -135
- package/dist/rpc/protocol.d.ts +0 -21
- package/dist/rpc/protocol.js +0 -17
- package/dist/rpc/server.d.ts +0 -39
- package/dist/rpc/server.js +0 -146
- package/dist/rpc/types.d.ts +0 -9
- package/dist/rpc/types.js +0 -2
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
const RequestTypeNames = {
|
|
2
|
-
ready: 'ready',
|
|
3
|
-
request: 'request',
|
|
4
|
-
cancel: 'cancel',
|
|
5
|
-
ok: 'ok',
|
|
6
|
-
};
|
|
7
|
-
const ResponseTypeNames = {
|
|
8
|
-
ready: 'ready',
|
|
9
|
-
response: 'response',
|
|
10
|
-
canceled: 'canceled',
|
|
11
|
-
ok: 'ok',
|
|
12
|
-
};
|
|
13
|
-
const knownRPCMessageTypes = new Set([...Object.keys(RequestTypeNames), ...Object.keys(ResponseTypeNames)]);
|
|
14
|
-
const sig = 'RPC0';
|
|
15
|
-
export function isRPCBaseMessage(message) {
|
|
16
|
-
if (!message || typeof message !== 'object')
|
|
17
|
-
return false;
|
|
18
|
-
const m = message;
|
|
19
|
-
return m.sig === sig && (typeof m.id === 'string' || typeof m.id === 'number') && knownRPCMessageTypes.has(m.type);
|
|
20
|
-
}
|
|
21
|
-
export function isBaseRequest(message) {
|
|
22
|
-
return message.type in RequestTypeNames;
|
|
23
|
-
}
|
|
24
|
-
export function isBaseResponse(message) {
|
|
25
|
-
return isRPCBaseMessage(message) && message.type in ResponseTypeNames;
|
|
26
|
-
}
|
|
27
|
-
export function isRPCErrorResponse(response) {
|
|
28
|
-
return (isBaseResponse(response) &&
|
|
29
|
-
response.type === ResponseTypeNames.response &&
|
|
30
|
-
response.error !== undefined);
|
|
31
|
-
}
|
|
32
|
-
export function isRPCCancelRequest(request) {
|
|
33
|
-
return request.type === RequestTypeNames.cancel;
|
|
34
|
-
}
|
|
35
|
-
export function isRPCCanceledResponse(response) {
|
|
36
|
-
return response.type === ResponseTypeNames.canceled;
|
|
37
|
-
}
|
|
38
|
-
export function isRPCOkRequest(request) {
|
|
39
|
-
return request.type === RequestTypeNames.ok;
|
|
40
|
-
}
|
|
41
|
-
export function isRPCOkResponse(response) {
|
|
42
|
-
return response.type === ResponseTypeNames.ok && typeof response.code === 'number';
|
|
43
|
-
}
|
|
44
|
-
export function isRPCReadyRequest(request) {
|
|
45
|
-
return request.type === RequestTypeNames.ready;
|
|
46
|
-
}
|
|
47
|
-
export function isRPCReadyResponse(response) {
|
|
48
|
-
return response.type === ResponseTypeNames.ready && typeof response.code === 'number';
|
|
49
|
-
}
|
|
50
|
-
export function isRPCResponse(response) {
|
|
51
|
-
return response.type === ResponseTypeNames.response && 'result' in response;
|
|
52
|
-
}
|
|
53
|
-
export function isRPCRequest(message) {
|
|
54
|
-
return message.type === RequestTypeNames.request && message.method !== undefined;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Creates a RPC Request Message.
|
|
58
|
-
* @param id - The unique request identifier.
|
|
59
|
-
* @param method - The method name.
|
|
60
|
-
* @param params - The parameters for the request.
|
|
61
|
-
* @returns A RPC Request Message.
|
|
62
|
-
*/
|
|
63
|
-
export function createRPCMethodRequest(id, method, params) {
|
|
64
|
-
return { sig, id, type: RequestTypeNames.request, method, params };
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Creates a cancel request message.
|
|
68
|
-
* @param id - The request ID to be canceled.
|
|
69
|
-
* @returns A cancel request message.
|
|
70
|
-
*/
|
|
71
|
-
export function createRPCCancelRequest(id) {
|
|
72
|
-
return { sig, id, type: RequestTypeNames.cancel };
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Creates a cancel request message.
|
|
76
|
-
* @param id - The request ID to be canceled.
|
|
77
|
-
* @param code - The response code
|
|
78
|
-
* - 200 (ok) - if canceled successfully upon request.
|
|
79
|
-
* - 408 (timeout) - if the cancellation was initiated by the server.
|
|
80
|
-
* - 503 (unavailable) - if the server is shutting down.
|
|
81
|
-
* @returns A cancel request message.
|
|
82
|
-
*/
|
|
83
|
-
export function createRPCCanceledResponse(id, code) {
|
|
84
|
-
return { sig, id, type: ResponseTypeNames.canceled, code };
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Creates a RPC Response Message.
|
|
88
|
-
* @param id - The matching request ID.
|
|
89
|
-
* @param result - the result of the request.
|
|
90
|
-
* @returns A RPC Response Message.
|
|
91
|
-
*/
|
|
92
|
-
export function createRPCResponse(id, result, code) {
|
|
93
|
-
return { sig, id, type: ResponseTypeNames.response, code, result };
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Creates a RPC Error Message.
|
|
97
|
-
* @param id - The matching request ID for which the error occurred.
|
|
98
|
-
* @param error - The error information.
|
|
99
|
-
* @returns A RPC Error Message.
|
|
100
|
-
*/
|
|
101
|
-
export function createRPCError(id, error, code) {
|
|
102
|
-
const msg = { sig, id, type: ResponseTypeNames.response, code, error };
|
|
103
|
-
return msg;
|
|
104
|
-
}
|
|
105
|
-
export function createRPCOkRequest(id) {
|
|
106
|
-
return { sig, id, type: RequestTypeNames.ok };
|
|
107
|
-
}
|
|
108
|
-
export function createRPCOkResponse(id, code) {
|
|
109
|
-
return { sig, id, type: ResponseTypeNames.ok, code };
|
|
110
|
-
}
|
|
111
|
-
export function createRPCReadyRequest(id) {
|
|
112
|
-
return { sig, id, type: RequestTypeNames.ready };
|
|
113
|
-
}
|
|
114
|
-
export function createRPCReadyResponse(id, code) {
|
|
115
|
-
return { sig, id, type: ResponseTypeNames.ready, code };
|
|
116
|
-
}
|
|
117
|
-
//# sourceMappingURL=modelsHelpers.js.map
|
package/dist/rpc/notify.d.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
export type NotifyHandler<T> = (event: T) => void;
|
|
2
|
-
export type NotifyEvent<T> = (handler: NotifyHandler<T>) => Disposable;
|
|
3
|
-
/**
|
|
4
|
-
* Used to have a type distinction between NotifyOnceEvents and NotifyEvents.
|
|
5
|
-
* It is not used at runtime.
|
|
6
|
-
*/
|
|
7
|
-
declare const SymbolNotifyOnceEvent: unique symbol;
|
|
8
|
-
export type NotifyOnceEvent<T> = NotifyEvent<T> & {
|
|
9
|
-
[SymbolNotifyOnceEvent]?: true;
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* A Class used to emit notifications to registered handlers.
|
|
13
|
-
*/
|
|
14
|
-
export declare class NotifyEmitter<T> {
|
|
15
|
-
#private;
|
|
16
|
-
/**
|
|
17
|
-
* Registers a handler for the event. Multiple handlers can be added. The same handler will
|
|
18
|
-
* not be added more than once. To add the same handler multiple times, use a wrapper function.
|
|
19
|
-
*
|
|
20
|
-
* The handler will NOT be called during the registration. They will be called when {@link notify} is called.
|
|
21
|
-
*
|
|
22
|
-
* Note: This function can be used without needing to bind 'this'.
|
|
23
|
-
* @param handler - the handler to add.
|
|
24
|
-
* @returns a Disposable to remove the handler.
|
|
25
|
-
*/
|
|
26
|
-
readonly onEvent: NotifyEvent<T>;
|
|
27
|
-
/**
|
|
28
|
-
* Notify all handlers of the event.
|
|
29
|
-
*
|
|
30
|
-
* If a handler throws an error, the error is not caught and will propagate up the call stack.
|
|
31
|
-
*
|
|
32
|
-
* Note: This function can be used without needing to bind 'this'.
|
|
33
|
-
* @param value - The event value.
|
|
34
|
-
*/
|
|
35
|
-
readonly notify: (value: T) => void;
|
|
36
|
-
/**
|
|
37
|
-
* A NotifyEvent that only fires once for each handler added.
|
|
38
|
-
*
|
|
39
|
-
* Multiple handlers can be added. The same handler can be added multiple times
|
|
40
|
-
* and will be called once for each time it is added.
|
|
41
|
-
*
|
|
42
|
-
* Note: This property can be used without needing to bind 'this'.
|
|
43
|
-
*/
|
|
44
|
-
readonly once: NotifyOnceEvent<T>;
|
|
45
|
-
/**
|
|
46
|
-
* Get a Promise that resolves with the next event.
|
|
47
|
-
* @param signal - A signal to abort the wait.
|
|
48
|
-
* @returns a Promise that will resolve with the next value emitted.
|
|
49
|
-
*/
|
|
50
|
-
readonly awaitNext: (signal?: AbortSignal) => Promise<T>;
|
|
51
|
-
/**
|
|
52
|
-
* The number of registered handlers.
|
|
53
|
-
*/
|
|
54
|
-
get size(): number;
|
|
55
|
-
/**
|
|
56
|
-
* Removes all registered handlers.
|
|
57
|
-
*/
|
|
58
|
-
clear(): void;
|
|
59
|
-
[Symbol.dispose](): void;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Convert a NotifyEvent to a Promise.
|
|
63
|
-
* @param event - The event to convert.
|
|
64
|
-
* @param signal - Optional AbortSignal to cancel the subscription if the promise is abandoned.
|
|
65
|
-
* @returns A Promise that resolves with the first value emitted by the event.
|
|
66
|
-
*/
|
|
67
|
-
export declare function notifyEventToPromise<T>(event: NotifyEvent<T>, signal?: AbortSignal): Promise<T>;
|
|
68
|
-
/**
|
|
69
|
-
* Create a NotifyEvent that only fires once.
|
|
70
|
-
*
|
|
71
|
-
* The same handler can be added multiple times and will be called once for each time it is added.
|
|
72
|
-
* This is different from a normal NotifyEvent where the same handler is only added once.
|
|
73
|
-
*
|
|
74
|
-
* @param event - The event to wrap.
|
|
75
|
-
* @returns A NotifyOnceEvent that only fires once for the handlers added.
|
|
76
|
-
*/
|
|
77
|
-
export declare function notifyEventOnce<T>(event: NotifyEvent<T>): NotifyOnceEvent<T>;
|
|
78
|
-
export {};
|
|
79
|
-
//# sourceMappingURL=notify.d.ts.map
|
package/dist/rpc/notify.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { AlreadyDisposedError } from './errors.js';
|
|
2
|
-
/**
|
|
3
|
-
* A Class used to emit notifications to registered handlers.
|
|
4
|
-
*/
|
|
5
|
-
export class NotifyEmitter {
|
|
6
|
-
#handlers = new Map();
|
|
7
|
-
#disposed = false;
|
|
8
|
-
/**
|
|
9
|
-
* Registers a handler for the event. Multiple handlers can be added. The same handler will
|
|
10
|
-
* not be added more than once. To add the same handler multiple times, use a wrapper function.
|
|
11
|
-
*
|
|
12
|
-
* The handler will NOT be called during the registration. They will be called when {@link notify} is called.
|
|
13
|
-
*
|
|
14
|
-
* Note: This function can be used without needing to bind 'this'.
|
|
15
|
-
* @param handler - the handler to add.
|
|
16
|
-
* @returns a Disposable to remove the handler.
|
|
17
|
-
*/
|
|
18
|
-
onEvent = (handler) => this.#onEvent(handler);
|
|
19
|
-
/**
|
|
20
|
-
* Notify all handlers of the event.
|
|
21
|
-
*
|
|
22
|
-
* If a handler throws an error, the error is not caught and will propagate up the call stack.
|
|
23
|
-
*
|
|
24
|
-
* Note: This function can be used without needing to bind 'this'.
|
|
25
|
-
* @param value - The event value.
|
|
26
|
-
*/
|
|
27
|
-
notify = (value) => this.#notify(value);
|
|
28
|
-
/**
|
|
29
|
-
* A NotifyEvent that only fires once for each handler added.
|
|
30
|
-
*
|
|
31
|
-
* Multiple handlers can be added. The same handler can be added multiple times
|
|
32
|
-
* and will be called once for each time it is added.
|
|
33
|
-
*
|
|
34
|
-
* Note: This property can be used without needing to bind 'this'.
|
|
35
|
-
*/
|
|
36
|
-
once = notifyEventOnce(this.onEvent);
|
|
37
|
-
/**
|
|
38
|
-
* Get a Promise that resolves with the next event.
|
|
39
|
-
* @param signal - A signal to abort the wait.
|
|
40
|
-
* @returns a Promise that will resolve with the next value emitted.
|
|
41
|
-
*/
|
|
42
|
-
awaitNext = (signal) => notifyEventToPromise(this.onEvent, signal);
|
|
43
|
-
/**
|
|
44
|
-
* The number of registered handlers.
|
|
45
|
-
*/
|
|
46
|
-
get size() {
|
|
47
|
-
return this.#handlers.size;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Removes all registered handlers.
|
|
51
|
-
*/
|
|
52
|
-
clear() {
|
|
53
|
-
this.#handlers.clear();
|
|
54
|
-
}
|
|
55
|
-
#onEvent(handler) {
|
|
56
|
-
if (this.#disposed) {
|
|
57
|
-
throw new AlreadyDisposedError();
|
|
58
|
-
}
|
|
59
|
-
const found = this.#handlers.get(handler);
|
|
60
|
-
if (found) {
|
|
61
|
-
return found;
|
|
62
|
-
}
|
|
63
|
-
let disposed = false;
|
|
64
|
-
const disposable = {
|
|
65
|
-
[Symbol.dispose]: () => {
|
|
66
|
-
if (disposed)
|
|
67
|
-
return;
|
|
68
|
-
disposed = true;
|
|
69
|
-
this.#handlers.delete(handler);
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
this.#handlers.set(handler, disposable);
|
|
73
|
-
return disposable;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Notify all handlers of the event.
|
|
77
|
-
* @param value - The event value.
|
|
78
|
-
*/
|
|
79
|
-
#notify(value) {
|
|
80
|
-
for (const handler of [...this.#handlers.keys()]) {
|
|
81
|
-
handler(value);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
[Symbol.dispose]() {
|
|
85
|
-
if (this.#disposed)
|
|
86
|
-
return;
|
|
87
|
-
this.#disposed = true;
|
|
88
|
-
this.#handlers.clear();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Convert a NotifyEvent to a Promise.
|
|
93
|
-
* @param event - The event to convert.
|
|
94
|
-
* @param signal - Optional AbortSignal to cancel the subscription if the promise is abandoned.
|
|
95
|
-
* @returns A Promise that resolves with the first value emitted by the event.
|
|
96
|
-
*/
|
|
97
|
-
export function notifyEventToPromise(event, signal) {
|
|
98
|
-
const once = notifyEventOnce(event);
|
|
99
|
-
return new Promise((resolve, reject) => {
|
|
100
|
-
signal?.throwIfAborted();
|
|
101
|
-
const disposable = once((value) => {
|
|
102
|
-
signal?.removeEventListener('abort', onAbort);
|
|
103
|
-
resolve(value);
|
|
104
|
-
});
|
|
105
|
-
function onAbort() {
|
|
106
|
-
disposable[Symbol.dispose]();
|
|
107
|
-
signal?.removeEventListener('abort', onAbort);
|
|
108
|
-
reject(signal?.reason);
|
|
109
|
-
}
|
|
110
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Create a NotifyEvent that only fires once.
|
|
115
|
-
*
|
|
116
|
-
* The same handler can be added multiple times and will be called once for each time it is added.
|
|
117
|
-
* This is different from a normal NotifyEvent where the same handler is only added once.
|
|
118
|
-
*
|
|
119
|
-
* @param event - The event to wrap.
|
|
120
|
-
* @returns A NotifyOnceEvent that only fires once for the handlers added.
|
|
121
|
-
*/
|
|
122
|
-
export function notifyEventOnce(event) {
|
|
123
|
-
function notifyOnce(handler) {
|
|
124
|
-
const disposable = event((e) => {
|
|
125
|
-
// A NotifyEvent should register a handler, but never call it immediately.
|
|
126
|
-
// Therefore the disposable should always be defined here. A ReferenceError
|
|
127
|
-
// would indicate a bug in NotifyEmitter or NotifyEvent implementation.
|
|
128
|
-
disposable[Symbol.dispose]();
|
|
129
|
-
handler(e);
|
|
130
|
-
});
|
|
131
|
-
return disposable;
|
|
132
|
-
}
|
|
133
|
-
return notifyOnce;
|
|
134
|
-
}
|
|
135
|
-
//# sourceMappingURL=notify.js.map
|
package/dist/rpc/protocol.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { OnlyFunctionsOrNever, StringKeyOf, ToReturnPromise } from './types.js';
|
|
2
|
-
export type RPCProtocolMethods<T> = {
|
|
3
|
-
[K in StringKeyOf<T> as T[K] extends OnlyFunctionsOrNever<T[K]> ? K : never]: OnlyFunctionsOrNever<T[K]>;
|
|
4
|
-
};
|
|
5
|
-
export type RPCProtocol<T> = {
|
|
6
|
-
[K in StringKeyOf<T> as T[K] extends OnlyFunctionsOrNever<T[K]> ? K : never]: ToReturnPromise<OnlyFunctionsOrNever<T[K]>>;
|
|
7
|
-
};
|
|
8
|
-
export type RPCProtocolMethodNames<P> = StringKeyOf<RPCProtocol<P>>;
|
|
9
|
-
/**
|
|
10
|
-
* Cast the API methods to RPCProtocol.
|
|
11
|
-
* @param methods - The API methods.
|
|
12
|
-
* @returns the API methods as RPCProtocol.
|
|
13
|
-
*/
|
|
14
|
-
export declare function protocolDefinition<API extends RPCProtocol<API>>(methods: API): RPCProtocol<API>;
|
|
15
|
-
/**
|
|
16
|
-
* Cast the API methods to RPCProtocolMethods.
|
|
17
|
-
* @param apiMethods - The API methods.
|
|
18
|
-
* @returns the API methods as RPCProtocolMethods.
|
|
19
|
-
*/
|
|
20
|
-
export declare function protocolMethods<API extends RPCProtocolMethods<API>>(apiMethods: API): RPCProtocolMethods<API>;
|
|
21
|
-
//# sourceMappingURL=protocol.d.ts.map
|
package/dist/rpc/protocol.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cast the API methods to RPCProtocol.
|
|
3
|
-
* @param methods - The API methods.
|
|
4
|
-
* @returns the API methods as RPCProtocol.
|
|
5
|
-
*/
|
|
6
|
-
export function protocolDefinition(methods) {
|
|
7
|
-
return methods;
|
|
8
|
-
}
|
|
9
|
-
/**
|
|
10
|
-
* Cast the API methods to RPCProtocolMethods.
|
|
11
|
-
* @param apiMethods - The API methods.
|
|
12
|
-
* @returns the API methods as RPCProtocolMethods.
|
|
13
|
-
*/
|
|
14
|
-
export function protocolMethods(apiMethods) {
|
|
15
|
-
return apiMethods;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=protocol.js.map
|
package/dist/rpc/server.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import type { MessagePortLike } from './messagePort.js';
|
|
2
|
-
import type { RPCProtocol, RPCProtocolMethodNames } from './protocol.js';
|
|
3
|
-
export interface RPCServerOptions {
|
|
4
|
-
/**
|
|
5
|
-
* If true, the server will close the message port when disposed.
|
|
6
|
-
* @default false
|
|
7
|
-
*/
|
|
8
|
-
closePortOnDispose?: boolean;
|
|
9
|
-
/**
|
|
10
|
-
* If true, the server will respond with an error message for unknown or malformed requests.
|
|
11
|
-
* @default false
|
|
12
|
-
*/
|
|
13
|
-
returnMalformedRPCRequestError?: boolean;
|
|
14
|
-
}
|
|
15
|
-
export interface RPCServerConfiguration extends RPCServerOptions {
|
|
16
|
-
/**
|
|
17
|
-
* The message port to use for communication.
|
|
18
|
-
*/
|
|
19
|
-
port: MessagePortLike;
|
|
20
|
-
}
|
|
21
|
-
declare class RPCServerImpl<ServerApi, PApi extends RPCProtocol<ServerApi> = RPCProtocol<ServerApi>, MethodsNames extends RPCProtocolMethodNames<PApi> = RPCProtocolMethodNames<PApi>> {
|
|
22
|
-
#private;
|
|
23
|
-
constructor(config: RPCServerConfiguration, methods: ServerApi);
|
|
24
|
-
[Symbol.dispose](): void;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* RPC Server implementation.
|
|
28
|
-
* @param ServerApi - The API methods of the server.
|
|
29
|
-
*/
|
|
30
|
-
export declare class RPCServer<ServerApi> extends RPCServerImpl<ServerApi> {
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
* @param config - The server configuration, including the message port and options.
|
|
34
|
-
* @param methods - The methods to implement the API.
|
|
35
|
-
*/
|
|
36
|
-
constructor(config: RPCServerConfiguration, methods: ServerApi);
|
|
37
|
-
}
|
|
38
|
-
export {};
|
|
39
|
-
//# sourceMappingURL=server.d.ts.map
|
package/dist/rpc/server.js
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { assert } from './assert.js';
|
|
2
|
-
import { MalformedRPCRequestError, UnknownMethodRPCRequestError } from './errors.js';
|
|
3
|
-
import { MessagePortNotifyEvents } from './MessagePortEvents.js';
|
|
4
|
-
import { createRPCCanceledResponse, createRPCError, createRPCOkResponse, createRPCReadyResponse, createRPCResponse, isRPCBaseMessage, isRPCCancelRequest, isRPCOkRequest, isRPCReadyRequest, isRPCRequest, } from './modelsHelpers.js';
|
|
5
|
-
const RESPONSE_CODES = {
|
|
6
|
-
OK: 200,
|
|
7
|
-
BadRequest: 400,
|
|
8
|
-
RequestTimeout: 408,
|
|
9
|
-
InternalServerError: 500,
|
|
10
|
-
ServiceUnavailable: 503,
|
|
11
|
-
};
|
|
12
|
-
class RPCServerImpl {
|
|
13
|
-
#portWrapper;
|
|
14
|
-
#options;
|
|
15
|
-
#allowedMethods;
|
|
16
|
-
#methods;
|
|
17
|
-
#pendingRequests;
|
|
18
|
-
constructor(config, methods) {
|
|
19
|
-
this.#portWrapper = new MessagePortNotifyEvents(config.port);
|
|
20
|
-
this.#options = config;
|
|
21
|
-
this.#methods = methods;
|
|
22
|
-
this.#allowedMethods = new Set(Object.keys(this.#methods).filter((k) => typeof this.#methods[k] === 'function'));
|
|
23
|
-
this.#pendingRequests = new Map();
|
|
24
|
-
this.#portWrapper.onMessage((msg) => this.#handleMessage(msg));
|
|
25
|
-
this.#portWrapper.start();
|
|
26
|
-
this.#sendReadyResponse();
|
|
27
|
-
}
|
|
28
|
-
get #isClosed() {
|
|
29
|
-
return this.#portWrapper.isClosed;
|
|
30
|
-
}
|
|
31
|
-
#sendResponse(response) {
|
|
32
|
-
if (this.#isClosed)
|
|
33
|
-
return;
|
|
34
|
-
this.#portWrapper.postMessage(response);
|
|
35
|
-
}
|
|
36
|
-
#handleMessage(msg) {
|
|
37
|
-
let id = 0;
|
|
38
|
-
try {
|
|
39
|
-
if (!isRPCBaseMessage(msg)) {
|
|
40
|
-
if (this.#options.returnMalformedRPCRequestError) {
|
|
41
|
-
throw new MalformedRPCRequestError('Malformed RPC request', msg);
|
|
42
|
-
}
|
|
43
|
-
// Not a valid RPC message; ignore.
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
id = msg.id;
|
|
47
|
-
if (isRPCCancelRequest(msg)) {
|
|
48
|
-
// For now just remove it from pending requests.
|
|
49
|
-
// later, implement aborting the request if possible.
|
|
50
|
-
this.#pendingRequests.delete(msg.id);
|
|
51
|
-
this.#sendCancelResponse(msg.id, RESPONSE_CODES.OK);
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (isRPCReadyRequest(msg)) {
|
|
55
|
-
this.#sendReadyResponse(msg.id);
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
if (isRPCOkRequest(msg)) {
|
|
59
|
-
this.#sendResponse(createRPCOkResponse(msg.id, RESPONSE_CODES.OK));
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (!isRPCRequest(msg)) {
|
|
63
|
-
if (this.#options.returnMalformedRPCRequestError) {
|
|
64
|
-
throw new MalformedRPCRequestError('Malformed RPC request', msg);
|
|
65
|
-
}
|
|
66
|
-
// Not a request; ignore.
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
this.#handleRequest(msg);
|
|
70
|
-
}
|
|
71
|
-
catch (err) {
|
|
72
|
-
this.#sendErrorResponse(id, err);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
#sendReadyResponse(id) {
|
|
76
|
-
this.#sendResponse(createRPCReadyResponse(id || 0, RESPONSE_CODES.OK));
|
|
77
|
-
}
|
|
78
|
-
#isMethod(method) {
|
|
79
|
-
return this.#allowedMethods.has(method);
|
|
80
|
-
}
|
|
81
|
-
#handleRequest(msg) {
|
|
82
|
-
const handleAsync = async () => {
|
|
83
|
-
if (!this.#isMethod(msg.method)) {
|
|
84
|
-
this.#sendErrorResponse(msg.id, new UnknownMethodRPCRequestError(msg.method));
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
const method = msg.method;
|
|
88
|
-
const params = msg.params;
|
|
89
|
-
assert(Array.isArray(params), 'RPC method parameters must be an array');
|
|
90
|
-
assert(typeof this.#methods[method] === 'function', `RPC method ${method} is not a function`);
|
|
91
|
-
const result = await this.#methods[method](...params);
|
|
92
|
-
if (this.#pendingRequests.has(msg.id)) {
|
|
93
|
-
const response = createRPCResponse(msg.id, result, RESPONSE_CODES.OK);
|
|
94
|
-
this.#sendResponse(response);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
this.#pendingRequests.set(msg.id, {
|
|
98
|
-
requestMessage: msg,
|
|
99
|
-
promise: handleAsync().catch((err) => this.#sendErrorResponse(msg.id, err, RESPONSE_CODES.InternalServerError)),
|
|
100
|
-
});
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
#sendCancelResponse(id, code = RESPONSE_CODES.ServiceUnavailable) {
|
|
104
|
-
this.#sendResponse(createRPCCanceledResponse(id, code));
|
|
105
|
-
}
|
|
106
|
-
#sendErrorResponse(id, error, code = RESPONSE_CODES.BadRequest) {
|
|
107
|
-
try {
|
|
108
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
109
|
-
this.#sendResponse(createRPCError(id, err, code));
|
|
110
|
-
}
|
|
111
|
-
catch {
|
|
112
|
-
// Nothing to do if the port is closed.
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
#cancelAllRequests(reason) {
|
|
116
|
-
if (!this.#pendingRequests.size)
|
|
117
|
-
return;
|
|
118
|
-
reason ??= new Error('RPC Server is shutting down');
|
|
119
|
-
for (const id of this.#pendingRequests.keys()) {
|
|
120
|
-
this.#sendErrorResponse(id, reason);
|
|
121
|
-
}
|
|
122
|
-
this.#pendingRequests.clear();
|
|
123
|
-
}
|
|
124
|
-
[Symbol.dispose]() {
|
|
125
|
-
this.#cancelAllRequests();
|
|
126
|
-
if (this.#options.closePortOnDispose) {
|
|
127
|
-
this.#portWrapper.close();
|
|
128
|
-
}
|
|
129
|
-
this.#portWrapper[Symbol.dispose]();
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* RPC Server implementation.
|
|
134
|
-
* @param ServerApi - The API methods of the server.
|
|
135
|
-
*/
|
|
136
|
-
export class RPCServer extends RPCServerImpl {
|
|
137
|
-
/**
|
|
138
|
-
*
|
|
139
|
-
* @param config - The server configuration, including the message port and options.
|
|
140
|
-
* @param methods - The methods to implement the API.
|
|
141
|
-
*/
|
|
142
|
-
constructor(config, methods) {
|
|
143
|
-
super(config, methods);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
//# sourceMappingURL=server.js.map
|
package/dist/rpc/types.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export type ANY = any;
|
|
2
|
-
export type OnlyFunctionsOrNever<T> = T extends (...args: ANY[]) => ANY ? T : never;
|
|
3
|
-
export type ToReturnPromise<T extends (...args: ANY) => ANY> = T extends (...args: infer A) => infer R ? R extends Promise<ANY> ? (...args: A) => R : (...args: A) => Promise<R> : ANY;
|
|
4
|
-
export type PromiseOrNever<T> = T extends Promise<ANY> ? T : never;
|
|
5
|
-
export type RemovePromise<T> = T extends Promise<infer R> ? R : T;
|
|
6
|
-
export type StringKeyOf<T> = Exclude<Extract<keyof T, string>, number | symbol>;
|
|
7
|
-
export type MustExtendString<T> = T extends string ? T : never;
|
|
8
|
-
export type AsPromise<T> = T extends Promise<ANY> ? T : Promise<T>;
|
|
9
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/rpc/types.js
DELETED