cspell-lib 9.6.0 → 9.6.2

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.
Files changed (98) hide show
  1. package/dist/cspell-rpc/api.d.ts +28 -0
  2. package/dist/cspell-rpc/api.js +6 -0
  3. package/dist/cspell-rpc/client.d.ts +17 -0
  4. package/dist/cspell-rpc/client.js +20 -0
  5. package/dist/cspell-rpc/index.d.ts +7 -0
  6. package/dist/cspell-rpc/index.js +4 -0
  7. package/dist/cspell-rpc/server.d.ts +16 -0
  8. package/dist/cspell-rpc/server.js +37 -0
  9. package/dist/cspell-rpc/spellCheckFile.d.ts +2 -0
  10. package/dist/cspell-rpc/spellCheckFile.js +2 -0
  11. package/dist/index.d.ts +2 -0
  12. package/dist/index.js +2 -0
  13. package/dist/lib/Settings/CSpellSettingsServer.d.ts +4 -2
  14. package/dist/lib/Settings/CSpellSettingsServer.js +10 -2
  15. package/dist/lib/Settings/Controller/configLoader/configLoader.js +1 -1
  16. package/dist/lib/Settings/Controller/configLoader/defaultSettings.js +1 -1
  17. package/dist/lib/Settings/Controller/configLoader/normalizeRawSettings.js +1 -1
  18. package/dist/lib/Settings/Controller/configLoader/types.d.ts +1 -1
  19. package/dist/lib/Settings/DefaultSettings.d.ts +1 -1
  20. package/dist/lib/Settings/DefaultSettings.js +1 -1
  21. package/dist/lib/Settings/InDocSettings.d.ts +0 -3
  22. package/dist/lib/Settings/InDocSettings.js +2 -3
  23. package/dist/lib/Settings/LanguageSettings.js +3 -3
  24. package/dist/lib/Settings/TextDocumentSettings.d.ts +1 -1
  25. package/dist/lib/Settings/index.d.ts +3 -1
  26. package/dist/lib/Settings/index.js +2 -1
  27. package/dist/lib/Settings/internal/CSpellSettingsInternalDef.d.ts +18 -0
  28. package/dist/lib/{Models → Settings/internal}/CSpellSettingsInternalDef.js +1 -10
  29. package/dist/lib/Settings/{DictionarySettings.d.ts → internal/DictionarySettings.d.ts} +3 -2
  30. package/dist/lib/Settings/{DictionarySettings.js → internal/DictionarySettings.js} +14 -9
  31. package/dist/lib/{Models/CSpellSettingsInternalDef.d.ts → Settings/internal/InternalDictionaryDef.d.ts} +2 -17
  32. package/dist/lib/Settings/internal/InternalDictionaryDef.js +10 -0
  33. package/dist/lib/Settings/internal/index.d.ts +6 -0
  34. package/dist/lib/Settings/internal/index.js +4 -0
  35. package/dist/lib/Settings/sanitizeSettings.d.ts +11 -0
  36. package/dist/lib/Settings/sanitizeSettings.js +254 -0
  37. package/dist/lib/Settings/util/settingsToJson.d.ts +28 -0
  38. package/dist/lib/Settings/util/settingsToJson.js +125 -0
  39. package/dist/lib/SpellingDictionary/Dictionaries.d.ts +1 -1
  40. package/dist/lib/SpellingDictionary/Dictionaries.js +1 -1
  41. package/dist/lib/SpellingDictionary/DictionaryController/DictionaryLoader.d.ts +1 -1
  42. package/dist/lib/SpellingDictionary/DictionaryController/DictionaryLoader.js +99 -20
  43. package/dist/lib/SpellingDictionary/DictionaryLoader.d.ts +1 -1
  44. package/dist/lib/index.d.ts +2 -2
  45. package/dist/lib/index.js +2 -2
  46. package/dist/lib/perf/index.d.ts +1 -1
  47. package/dist/lib/perf/index.js +1 -1
  48. package/dist/lib/spellCheckFile.d.ts +10 -0
  49. package/dist/lib/spellCheckFile.js +24 -4
  50. package/dist/lib/suggestions.js +3 -3
  51. package/dist/lib/textValidation/cleanValidationIssue.d.ts +3 -0
  52. package/dist/lib/textValidation/cleanValidationIssue.js +21 -0
  53. package/dist/lib/textValidation/determineTextDocumentSettings.d.ts +1 -1
  54. package/dist/lib/textValidation/docValidator.d.ts +4 -1
  55. package/dist/lib/textValidation/docValidator.js +18 -1
  56. package/dist/lib/textValidation/settingsToValidateOptions.d.ts +1 -1
  57. package/dist/lib/util/clone.d.ts +35 -0
  58. package/dist/lib/util/clone.js +76 -0
  59. package/dist/lib/util/memoizeLastCall.d.ts +13 -0
  60. package/dist/lib/util/{memorizeLastCall.js → memoizeLastCall.js} +2 -2
  61. package/dist/lib/util/memoizeWeak.d.ts +6 -0
  62. package/dist/lib/util/{memorizerWeak.js → memoizeWeak.js} +2 -2
  63. package/dist/lib/util/text.js +6 -4
  64. package/dist/lib/util/textRegex.d.ts +2 -2
  65. package/dist/lib/util/textRegex.js +2 -2
  66. package/dist/rpc/Future.d.ts +18 -0
  67. package/dist/rpc/Future.js +49 -0
  68. package/dist/rpc/MessagePortEvents.d.ts +56 -0
  69. package/dist/rpc/MessagePortEvents.js +94 -0
  70. package/dist/rpc/assert.d.ts +14 -0
  71. package/dist/rpc/assert.js +21 -0
  72. package/dist/rpc/client.d.ts +186 -0
  73. package/dist/rpc/client.js +364 -0
  74. package/dist/rpc/errors.d.ts +24 -0
  75. package/dist/rpc/errors.js +47 -0
  76. package/dist/rpc/index.d.ts +11 -0
  77. package/dist/rpc/index.js +6 -0
  78. package/dist/rpc/messagePort.d.ts +34 -0
  79. package/dist/rpc/messagePort.js +2 -0
  80. package/dist/rpc/models.d.ts +134 -0
  81. package/dist/rpc/models.js +2 -0
  82. package/dist/rpc/modelsHelpers.d.ts +56 -0
  83. package/dist/rpc/modelsHelpers.js +117 -0
  84. package/dist/rpc/notify.d.ts +79 -0
  85. package/dist/rpc/notify.js +135 -0
  86. package/dist/rpc/protocol.d.ts +21 -0
  87. package/dist/rpc/protocol.js +17 -0
  88. package/dist/rpc/server.d.ts +39 -0
  89. package/dist/rpc/server.js +146 -0
  90. package/dist/rpc/types.d.ts +9 -0
  91. package/dist/rpc/types.js +2 -0
  92. package/dist/rpc.d.ts +2 -0
  93. package/dist/rpc.js +2 -0
  94. package/package.json +34 -21
  95. package/dist/lib/perf/performance.d.ts +0 -16
  96. package/dist/lib/perf/performance.js +0 -34
  97. package/dist/lib/util/memorizeLastCall.d.ts +0 -13
  98. package/dist/lib/util/memorizerWeak.d.ts +0 -6
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Note: This code is here to avoid a dependency on Node's 'assert' module.
3
+ */
4
+ /**
5
+ * Asserts that a condition is true.
6
+ * @param condition - The condition to assert.
7
+ * @param msg - optional message for the assertion error.
8
+ * @throws {AssertionError} If the condition is false.
9
+ */
10
+ export function assert(condition, msg) {
11
+ if (!condition) {
12
+ throw new AssertionError(msg);
13
+ }
14
+ }
15
+ export class AssertionError extends Error {
16
+ constructor(message) {
17
+ super(message || 'Assertion failed');
18
+ this.name = 'AssertionError';
19
+ }
20
+ }
21
+ //# sourceMappingURL=assert.js.map
@@ -0,0 +1,186 @@
1
+ import type { MessagePortLike } from './messagePort.js';
2
+ import type { RCPBaseRequest, RequestID, RPCPendingClientRequest, RPCResponse } from './models.js';
3
+ import type { RPCProtocol, RPCProtocolMethodNames } from './protocol.js';
4
+ interface PendingRequest {
5
+ readonly id: RequestID;
6
+ readonly request: RCPBaseRequest;
7
+ readonly response: Promise<RPCResponse>;
8
+ readonly isResolved: boolean;
9
+ readonly isCanceled: boolean;
10
+ /** calling abort will cancel the request if it has not already been resolved. */
11
+ abort: AbortController['abort'];
12
+ handleResponse: (res: RPCResponse) => void;
13
+ /**
14
+ * Cancels the request by telling the server to cancel the request and waiting on the response.
15
+ */
16
+ cancel: () => Promise<boolean>;
17
+ }
18
+ export interface RPCClientOptions {
19
+ /**
20
+ * A function to generate random UUIDs.
21
+ * @default undefined
22
+ */
23
+ randomUUID?: () => string;
24
+ /**
25
+ * If true, the client will close the port when disposed.
26
+ * @default false
27
+ */
28
+ closePortOnDispose?: boolean;
29
+ /**
30
+ * Set the default timeout in milliseconds for requests.
31
+ */
32
+ timeoutMs?: number;
33
+ }
34
+ export interface RPCClientConfiguration extends RPCClientOptions {
35
+ /**
36
+ * The message port to use for communication.
37
+ */
38
+ port: MessagePortLike;
39
+ }
40
+ export interface RequestOptions {
41
+ /**
42
+ * An AbortSignal to abort the request.
43
+ */
44
+ signal?: AbortSignal | undefined;
45
+ /**
46
+ * Timeout in milliseconds to wait before aborting the request.
47
+ */
48
+ timeoutMs?: number | undefined;
49
+ }
50
+ /**
51
+ * The RPC Client.
52
+ */
53
+ declare class RPCClientImpl<T, P extends RPCProtocol<T> = RPCProtocol<T>, MethodNames extends RPCProtocolMethodNames<P> = RPCProtocolMethodNames<P>> {
54
+ #private;
55
+ /**
56
+ * Create an RPC Client.
57
+ * @param config - The client configuration.
58
+ */
59
+ constructor(config: RPCClientConfiguration);
60
+ /**
61
+ * Make a request to the RPC server.
62
+ *
63
+ * It is unlikely you need to use this method directly. Consider using `call` or `getApi` instead.
64
+ *
65
+ * @param method - The method name.
66
+ * @param params - The method parameters.
67
+ * @param options - Request options including abort signal and timeout.
68
+ * @returns The pending client request.
69
+ */
70
+ request<M extends MethodNames>(method: M, params: Parameters<P[M]>, options?: RequestOptions): RPCPendingClientRequest<M, ReturnType<P[M]>>;
71
+ /**
72
+ * Check the health of the RPC server.
73
+ * @param options - used to set timeout and abort signal.
74
+ * @returns resolves to true if the server is OK, false on timeout.
75
+ */
76
+ isOK(options?: RequestOptions): Promise<boolean>;
77
+ /**
78
+ * The current known ready state of the RPC server.
79
+ * - `true` - The server is ready.
80
+ * - `false` - The server is not ready.
81
+ */
82
+ get isReady(): boolean;
83
+ /**
84
+ * Check if the RPC server is ready. If already ready, returns true immediately.
85
+ * If not ready, sends a 'ready' request to the server.
86
+ * @param options - used to set timeout and abort signal.
87
+ * @returns resolves to true when the server is ready, rejects if the request times out or fails.
88
+ */
89
+ ready(options?: RequestOptions): Promise<boolean>;
90
+ /**
91
+ * Call a method on the RPC server.
92
+ * @param method - The method name.
93
+ * @param params - The method parameters.
94
+ * @param options - Call options including abort signal.
95
+ * @returns A Promise with the method result.
96
+ */
97
+ call<M extends MethodNames>(method: M, params: Parameters<P[M]>, options?: RequestOptions): ReturnType<P[M]>;
98
+ /**
99
+ * Get the API for the given method names.
100
+ *
101
+ * This is useful passing the API to other parts of the code that do not need to know about the RPCClient.
102
+ *
103
+ * @param methods - The method names to include in the API.
104
+ * @returns A partial API with the requested methods.
105
+ */
106
+ getApi<M extends MethodNames>(methods: M[]): Pick<P, M>;
107
+ /**
108
+ * Get info about a pending request by its RequestID.
109
+ * @param id - The RequestID of the pending request.
110
+ * @returns The found pending request or undefined if not found.
111
+ */
112
+ getPendingRequestById(id: RequestID): PendingRequest | undefined;
113
+ /**
114
+ * Get info about a pending request by the promise returned using `call` or an api method.
115
+ * @param id - The RequestID of the pending request.
116
+ * @returns The found pending request or undefined if not found.
117
+ */
118
+ getPendingRequestByPromise(promise: Promise<unknown>): PendingRequest | undefined;
119
+ /**
120
+ * Get the number of pending requests.
121
+ */
122
+ get length(): number;
123
+ /**
124
+ * Abort a pending request by its promise.
125
+ *
126
+ * Note: the request promise will be rejected with an AbortRequestError.
127
+ * @param promise - The promise returned by the request.
128
+ * @param reason - The reason for aborting the request.
129
+ * @returns True if the request was found and aborted, false otherwise.
130
+ */
131
+ abortPromise(promise: Promise<unknown>, reason: unknown): boolean;
132
+ /**
133
+ * Abort a pending request by its RequestId.
134
+ *
135
+ * Note: the request promise will be rejected with an AbortRequestError.
136
+ * @param requestId - The RequestID of the request to abort.
137
+ * @param reason - The reason for aborting the request.
138
+ * @returns True if the request was found and aborted, false otherwise.
139
+ */
140
+ abortRequest(requestId: RequestID, reason?: unknown): boolean;
141
+ /**
142
+ * Abort all pending requests.
143
+ *
144
+ * Note: each request promise will be rejected with an AbortRequestError.
145
+ *
146
+ * @param reason - The reason for aborting the request.
147
+ */
148
+ abortAllRequests(reason?: unknown): void;
149
+ /**
150
+ * Cancel a pending request by its RequestID.
151
+ *
152
+ * Tries to cancel the request by sending a cancel request to the server and waiting for the response.
153
+ * @param id - The RequestID of the request to cancel.
154
+ * @returns resolves to true if the request was found and canceled, false otherwise.
155
+ */
156
+ cancelRequest(id: RequestID): Promise<boolean>;
157
+ /**
158
+ * Cancel a pending request by its Promise.
159
+ *
160
+ * Tries to cancel the request by sending a cancel request to the server and waiting for the response.
161
+ * @param id - The RequestID of the request to cancel.
162
+ * @returns resolves to true if the request was found and canceled, false otherwise.
163
+ */
164
+ cancelPromise(promise: Promise<unknown>): Promise<boolean>;
165
+ /**
166
+ * Set the default timeout for requests. Requests can override this value.
167
+ * @param timeoutMs - the timeout in milliseconds
168
+ */
169
+ setTimeout(timeoutMs: number | undefined): void;
170
+ /**
171
+ * Dispose of the RPC client, aborting all pending requests and closing the port if specified in options.
172
+ */
173
+ [Symbol.dispose](): void;
174
+ }
175
+ /**
176
+ * The RPC Client.
177
+ */
178
+ export declare class RPCClient<T> extends RPCClientImpl<T> {
179
+ /**
180
+ * Create an RPC Client.
181
+ * @param config - The client configuration.
182
+ */
183
+ constructor(config: RPCClientConfiguration);
184
+ }
185
+ export {};
186
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1,364 @@
1
+ import { AbortRPCRequestError, CanceledRPCRequestError, TimeoutRPCRequestError } from './errors.js';
2
+ import { Future } from './Future.js';
3
+ import { MessagePortNotifyEvents } from './MessagePortEvents.js';
4
+ import { createRPCCancelRequest, createRPCMethodRequest, createRPCOkRequest, createRPCReadyRequest, isBaseResponse, isRPCCanceledResponse, isRPCErrorResponse, isRPCReadyResponse, isRPCResponse, } from './modelsHelpers.js';
5
+ const DefaultOkOptions = {
6
+ timeoutMs: 200,
7
+ };
8
+ /**
9
+ * The RPC Client.
10
+ */
11
+ class RPCClientImpl {
12
+ #port;
13
+ #count = 0;
14
+ #options;
15
+ #pendingRequests = new Map();
16
+ #pendingRequestsByPromise = new WeakMap();
17
+ #defaultTimeoutMs;
18
+ #isReady;
19
+ #ready;
20
+ /**
21
+ * Create an RPC Client.
22
+ * @param config - The client configuration.
23
+ */
24
+ constructor(config) {
25
+ this.#port = new MessagePortNotifyEvents(config.port);
26
+ this.#options = config;
27
+ this.#defaultTimeoutMs = config.timeoutMs;
28
+ this.#isReady = false;
29
+ this.#ready = new Future();
30
+ this.#port.onMessage((msg) => this.#processMessageFromServer(msg));
31
+ this.#port.start();
32
+ }
33
+ /**
34
+ * Make a request to the RPC server.
35
+ *
36
+ * It is unlikely you need to use this method directly. Consider using `call` or `getApi` instead.
37
+ *
38
+ * @param method - The method name.
39
+ * @param params - The method parameters.
40
+ * @param options - Request options including abort signal and timeout.
41
+ * @returns The pending client request.
42
+ */
43
+ request(method, params, options) {
44
+ // Register the request.
45
+ const id = this.#calcId(method);
46
+ const request = createRPCMethodRequest(id, method, params);
47
+ const pendingRequest = this.#sendRequest(request, options);
48
+ const response = pendingRequest.response.then(handleResponse);
49
+ // Record the promise to request ID mapping.
50
+ this.#pendingRequestsByPromise.set(response, id);
51
+ const clientRequest = {
52
+ id,
53
+ method,
54
+ response,
55
+ abort: pendingRequest.abort,
56
+ cancel: pendingRequest.cancel,
57
+ get isResolved() {
58
+ return pendingRequest.isResolved;
59
+ },
60
+ get isCanceled() {
61
+ return pendingRequest.isCanceled;
62
+ },
63
+ };
64
+ return clientRequest;
65
+ function handleResponse(res) {
66
+ if (isRPCErrorResponse(res)) {
67
+ throw res.error;
68
+ }
69
+ if (isRPCCanceledResponse(res)) {
70
+ throw new CanceledRPCRequestError(`Request ${id} was canceled`);
71
+ }
72
+ if (isRPCResponse(res)) {
73
+ return res.result;
74
+ }
75
+ throw new Error(`Malformed response for request ${id}`); // Should not happen.
76
+ }
77
+ }
78
+ #sendRequest(request, options = {}) {
79
+ // Register the request.
80
+ const id = request.id;
81
+ const requestType = request.type;
82
+ let isResolved = false;
83
+ let isCanceled = false;
84
+ const timeoutMs = options.timeoutMs ?? this.#defaultTimeoutMs;
85
+ const timeoutSignal = timeoutMs ? AbortSignal.timeout(timeoutMs) : undefined;
86
+ const future = new Future();
87
+ options.signal?.addEventListener('abort', abort);
88
+ timeoutSignal?.addEventListener('abort', timeoutHandler);
89
+ const response = future.promise;
90
+ const cancelRequest = () => this.#port.postMessage(createRPCCancelRequest(id));
91
+ const cancel = async () => {
92
+ if (isResolved || isCanceled)
93
+ return isCanceled;
94
+ cancelRequest();
95
+ await response.catch(() => { });
96
+ return isCanceled;
97
+ };
98
+ const pendingRequest = {
99
+ id,
100
+ request,
101
+ response,
102
+ handleResponse,
103
+ abort,
104
+ cancel,
105
+ get isResolved() {
106
+ return isResolved;
107
+ },
108
+ get isCanceled() {
109
+ return isCanceled;
110
+ },
111
+ };
112
+ this.#pendingRequests.set(id, pendingRequest);
113
+ this.#pendingRequestsByPromise.set(response, id);
114
+ const cleanup = () => {
115
+ options.signal?.removeEventListener('abort', abort);
116
+ timeoutSignal?.removeEventListener('abort', timeoutHandler);
117
+ this.#cleanupPendingRequest(pendingRequest);
118
+ };
119
+ this.#port.postMessage(request);
120
+ return pendingRequest;
121
+ function timeoutHandler() {
122
+ abort(new TimeoutRPCRequestError(`Request ${requestType} ${id} timed out after ${timeoutMs} ms`));
123
+ }
124
+ function abort(reason) {
125
+ if (isResolved || isCanceled)
126
+ return;
127
+ isCanceled = true;
128
+ cancelRequest();
129
+ reason = reason instanceof Event ? undefined : reason;
130
+ reason ??= `Request ${id} aborted`;
131
+ reason = typeof reason === 'string' ? new AbortRPCRequestError(reason) : reason;
132
+ cleanup();
133
+ future.reject(reason);
134
+ }
135
+ function handleResponse(res) {
136
+ // Do not process anything if already resolved or canceled.
137
+ if (isResolved || isCanceled)
138
+ return;
139
+ isResolved = true;
140
+ if (isRPCCanceledResponse(res)) {
141
+ isCanceled = true;
142
+ }
143
+ cleanup();
144
+ future.resolve(res);
145
+ }
146
+ }
147
+ /**
148
+ * Check the health of the RPC server.
149
+ * @param options - used to set timeout and abort signal.
150
+ * @returns resolves to true if the server is OK, false on timeout.
151
+ */
152
+ async isOK(options = DefaultOkOptions) {
153
+ try {
154
+ const req = this.#sendRequest(createRPCOkRequest(this.#calcId('isOK')), options);
155
+ const res = await req.response;
156
+ return isBaseResponse(res) && res.type === 'ok' && res.code === 200;
157
+ }
158
+ catch {
159
+ return false;
160
+ }
161
+ }
162
+ /**
163
+ * The current known ready state of the RPC server.
164
+ * - `true` - The server is ready.
165
+ * - `false` - The server is not ready.
166
+ */
167
+ get isReady() {
168
+ return this.#isReady;
169
+ }
170
+ /**
171
+ * Check if the RPC server is ready. If already ready, returns true immediately.
172
+ * If not ready, sends a 'ready' request to the server.
173
+ * @param options - used to set timeout and abort signal.
174
+ * @returns resolves to true when the server is ready, rejects if the request times out or fails.
175
+ */
176
+ async ready(options) {
177
+ if (this.#isReady)
178
+ return true;
179
+ // We send the request, but we do not care about the result other than it succeeded.
180
+ await this.#sendRequest(createRPCReadyRequest(this.#calcId('ready')), options).response;
181
+ return this.#isReady; // We are returning the current state.
182
+ }
183
+ /**
184
+ * Call a method on the RPC server.
185
+ * @param method - The method name.
186
+ * @param params - The method parameters.
187
+ * @param options - Call options including abort signal.
188
+ * @returns A Promise with the method result.
189
+ */
190
+ call(method, params, options) {
191
+ const req = this.request(method, params, options);
192
+ return req.response;
193
+ }
194
+ /**
195
+ * Get the API for the given method names.
196
+ *
197
+ * This is useful passing the API to other parts of the code that do not need to know about the RPCClient.
198
+ *
199
+ * @param methods - The method names to include in the API.
200
+ * @returns A partial API with the requested methods.
201
+ */
202
+ getApi(methods) {
203
+ const apiEntries = methods.map((method) => [method, ((...params) => this.call(method, params))]);
204
+ return Object.fromEntries(apiEntries);
205
+ }
206
+ /**
207
+ * Get info about a pending request by its RequestID.
208
+ * @param id - The RequestID of the pending request.
209
+ * @returns The found pending request or undefined if not found.
210
+ */
211
+ getPendingRequestById(id) {
212
+ return this.#pendingRequests.get(id);
213
+ }
214
+ /**
215
+ * Get info about a pending request by the promise returned using `call` or an api method.
216
+ * @param id - The RequestID of the pending request.
217
+ * @returns The found pending request or undefined if not found.
218
+ */
219
+ getPendingRequestByPromise(promise) {
220
+ const requestId = this.#pendingRequestsByPromise.get(promise);
221
+ if (!requestId)
222
+ return undefined;
223
+ return this.getPendingRequestById(requestId);
224
+ }
225
+ /**
226
+ * Get the number of pending requests.
227
+ */
228
+ get length() {
229
+ return this.#pendingRequests.size;
230
+ }
231
+ #calcId(method) {
232
+ const suffix = this.#options.randomUUID ? this.#options.randomUUID() : `${performance.now()}`;
233
+ return `${method}-${++this.#count}-${suffix}`;
234
+ }
235
+ #cleanupPendingRequest(request) {
236
+ this.#pendingRequests.delete(request.id);
237
+ this.#pendingRequestsByPromise.delete(request.response);
238
+ }
239
+ #processMessageFromServer(msg) {
240
+ // Ignore messages that are not RPC messages
241
+ if (!isBaseResponse(msg))
242
+ return;
243
+ this.#handleReadyResponse(msg);
244
+ const pendingRequest = this.#pendingRequests.get(msg.id);
245
+ if (!pendingRequest)
246
+ return;
247
+ pendingRequest.handleResponse(msg);
248
+ }
249
+ /**
250
+ * Handle possible ready response messages.
251
+ * @param msg - The message to handle.
252
+ */
253
+ #handleReadyResponse(msg) {
254
+ if (!isRPCReadyResponse(msg))
255
+ return;
256
+ if (this.#ready.isResolved)
257
+ return;
258
+ this.#isReady = msg.code === 200;
259
+ this.#ready.resolve(this.#isReady);
260
+ }
261
+ /**
262
+ * Abort a pending request by its promise.
263
+ *
264
+ * Note: the request promise will be rejected with an AbortRequestError.
265
+ * @param promise - The promise returned by the request.
266
+ * @param reason - The reason for aborting the request.
267
+ * @returns True if the request was found and aborted, false otherwise.
268
+ */
269
+ abortPromise(promise, reason) {
270
+ const pendingRequest = this.getPendingRequestByPromise(promise);
271
+ if (!pendingRequest)
272
+ return false;
273
+ return this.abortRequest(pendingRequest.id, reason);
274
+ }
275
+ /**
276
+ * Abort a pending request by its RequestId.
277
+ *
278
+ * Note: the request promise will be rejected with an AbortRequestError.
279
+ * @param requestId - The RequestID of the request to abort.
280
+ * @param reason - The reason for aborting the request.
281
+ * @returns True if the request was found and aborted, false otherwise.
282
+ */
283
+ abortRequest(requestId, reason) {
284
+ const pendingRequest = this.getPendingRequestById(requestId);
285
+ if (!pendingRequest)
286
+ return false;
287
+ pendingRequest.abort(reason);
288
+ return true;
289
+ }
290
+ /**
291
+ * Abort all pending requests.
292
+ *
293
+ * Note: each request promise will be rejected with an AbortRequestError.
294
+ *
295
+ * @param reason - The reason for aborting the request.
296
+ */
297
+ abortAllRequests(reason) {
298
+ for (const pendingRequest of this.#pendingRequests.values()) {
299
+ try {
300
+ pendingRequest.abort(reason);
301
+ }
302
+ catch {
303
+ // ignore
304
+ }
305
+ }
306
+ }
307
+ /**
308
+ * Cancel a pending request by its RequestID.
309
+ *
310
+ * Tries to cancel the request by sending a cancel request to the server and waiting for the response.
311
+ * @param id - The RequestID of the request to cancel.
312
+ * @returns resolves to true if the request was found and canceled, false otherwise.
313
+ */
314
+ async cancelRequest(id) {
315
+ const pendingRequest = this.getPendingRequestById(id);
316
+ if (!pendingRequest)
317
+ return false;
318
+ return await pendingRequest.cancel();
319
+ }
320
+ /**
321
+ * Cancel a pending request by its Promise.
322
+ *
323
+ * Tries to cancel the request by sending a cancel request to the server and waiting for the response.
324
+ * @param id - The RequestID of the request to cancel.
325
+ * @returns resolves to true if the request was found and canceled, false otherwise.
326
+ */
327
+ async cancelPromise(promise) {
328
+ const request = this.getPendingRequestByPromise(promise);
329
+ if (!request)
330
+ return false;
331
+ return this.cancelRequest(request.id);
332
+ }
333
+ /**
334
+ * Set the default timeout for requests. Requests can override this value.
335
+ * @param timeoutMs - the timeout in milliseconds
336
+ */
337
+ setTimeout(timeoutMs) {
338
+ this.#defaultTimeoutMs = timeoutMs;
339
+ }
340
+ /**
341
+ * Dispose of the RPC client, aborting all pending requests and closing the port if specified in options.
342
+ */
343
+ [Symbol.dispose]() {
344
+ this.abortAllRequests(new Error('RPC Client disposed'));
345
+ this.#pendingRequests.clear();
346
+ if (this.#options.closePortOnDispose) {
347
+ this.#port.close();
348
+ }
349
+ this.#port[Symbol.dispose]();
350
+ }
351
+ }
352
+ /**
353
+ * The RPC Client.
354
+ */
355
+ export class RPCClient extends RPCClientImpl {
356
+ /**
357
+ * Create an RPC Client.
358
+ * @param config - The client configuration.
359
+ */
360
+ constructor(config) {
361
+ super(config);
362
+ }
363
+ }
364
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1,24 @@
1
+ export declare class RPCRequestError extends Error {
2
+ constructor(message: string);
3
+ }
4
+ export declare class AbortRPCRequestError extends RPCRequestError {
5
+ constructor(message: string);
6
+ }
7
+ export declare class TimeoutRPCRequestError extends RPCRequestError {
8
+ constructor(message: string);
9
+ }
10
+ export declare class UnknownMethodRPCRequestError extends RPCRequestError {
11
+ method: string;
12
+ constructor(method: string, message?: string);
13
+ }
14
+ export declare class MalformedRPCRequestError extends RPCRequestError {
15
+ request?: unknown;
16
+ constructor(message: string, request?: unknown);
17
+ }
18
+ export declare class CanceledRPCRequestError extends RPCRequestError {
19
+ constructor(message?: string);
20
+ }
21
+ export declare class AlreadyDisposedError extends Error {
22
+ constructor();
23
+ }
24
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1,47 @@
1
+ export class RPCRequestError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'RPCRequestError';
5
+ }
6
+ }
7
+ export class AbortRPCRequestError extends RPCRequestError {
8
+ constructor(message) {
9
+ super(message);
10
+ this.name = 'AbortRPCRequestError';
11
+ }
12
+ }
13
+ export class TimeoutRPCRequestError extends RPCRequestError {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = 'TimeoutRPCRequestError';
17
+ }
18
+ }
19
+ export class UnknownMethodRPCRequestError extends RPCRequestError {
20
+ method;
21
+ constructor(method, message) {
22
+ super(message || `Unknown method: ${method}`);
23
+ this.name = 'UnknownMethodRPCRequestError';
24
+ this.method = method;
25
+ }
26
+ }
27
+ export class MalformedRPCRequestError extends RPCRequestError {
28
+ request;
29
+ constructor(message, request) {
30
+ super(message);
31
+ this.name = 'MalformedRPCRequestError';
32
+ this.request = request;
33
+ }
34
+ }
35
+ export class CanceledRPCRequestError extends RPCRequestError {
36
+ constructor(message) {
37
+ super(message ?? 'The RPC request was canceled');
38
+ this.name = 'CanceledRPCRequestError';
39
+ }
40
+ }
41
+ export class AlreadyDisposedError extends Error {
42
+ constructor() {
43
+ super('NotifyEmitter has been disposed.');
44
+ this.name = 'AlreadyDisposedError';
45
+ }
46
+ }
47
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1,11 @@
1
+ export type { RPCClientConfiguration, RPCClientOptions } from './client.js';
2
+ export { RPCClient } from './client.js';
3
+ export { AbortRPCRequestError, CanceledRPCRequestError, RPCRequestError, TimeoutRPCRequestError, UnknownMethodRPCRequestError, } from './errors.js';
4
+ export type { MessagePortLike } from './messagePort.js';
5
+ export type { NotifyEvent, NotifyHandler, NotifyOnceEvent } from './notify.js';
6
+ export { NotifyEmitter, notifyEventOnce, notifyEventToPromise } from './notify.js';
7
+ export type { RPCProtocol, RPCProtocolMethods } from './protocol.js';
8
+ export { protocolDefinition, protocolMethods } from './protocol.js';
9
+ export type { RPCServerConfiguration, RPCServerOptions } from './server.js';
10
+ export { RPCServer } from './server.js';
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,6 @@
1
+ export { RPCClient } from './client.js';
2
+ export { AbortRPCRequestError, CanceledRPCRequestError, RPCRequestError, TimeoutRPCRequestError, UnknownMethodRPCRequestError, } from './errors.js';
3
+ export { NotifyEmitter, notifyEventOnce, notifyEventToPromise } from './notify.js';
4
+ export { protocolDefinition, protocolMethods } from './protocol.js';
5
+ export { RPCServer } from './server.js';
6
+ //# sourceMappingURL=index.js.map