swarpc 0.11.0 → 0.13.0

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/src/server.ts CHANGED
@@ -1,299 +1,275 @@
1
- /**
2
- * @module
3
- * @mergeModuleWith <project>
4
- */
5
-
6
- /// <reference lib="webworker" />
7
- import { type } from "arktype"
8
- import { createLogger, type LogLevel } from "./log.js"
9
- import {
10
- ImplementationsMap,
11
- Payload,
12
- PayloadCore,
13
- PayloadHeaderSchema,
14
- PayloadInitializeSchema,
15
- PayloadSchema,
16
- ProcedureImplementation,
17
- zImplementations,
18
- zProcedures,
19
- type ProceduresMap,
20
- } from "./types.js"
21
- import { findTransferables } from "./utils.js"
22
- import { FauxLocalStorage } from "./localstorage.js"
23
-
24
- class MockedWorkerGlobalScope {
25
- constructor() {}
26
- }
27
-
28
- const SharedWorkerGlobalScope =
29
- globalThis.SharedWorkerGlobalScope ?? MockedWorkerGlobalScope
30
-
31
- const DedicatedWorkerGlobalScope =
32
- globalThis.DedicatedWorkerGlobalScope ?? MockedWorkerGlobalScope
33
-
34
- const ServiceWorkerGlobalScope =
35
- globalThis.ServiceWorkerGlobalScope ?? MockedWorkerGlobalScope
36
-
37
- /**
38
- * The sw&rpc server instance, which provides methods to register {@link ProcedureImplementation | procedure implementations},
39
- * and listens for incoming messages that call those procedures
40
- */
41
- export type SwarpcServer<Procedures extends ProceduresMap> = {
42
- [zProcedures]: Procedures
43
- [zImplementations]: ImplementationsMap<Procedures>
44
- start(): Promise<void>
45
- } & {
46
- [F in keyof Procedures]: (
47
- impl: ProcedureImplementation<
48
- Procedures[F]["input"],
49
- Procedures[F]["progress"],
50
- Procedures[F]["success"]
51
- >
52
- ) => void
53
- }
54
-
55
- const abortControllers = new Map<string, AbortController>()
56
- const abortedRequests = new Set<string>()
57
-
58
- /**
59
- * Creates a sw&rpc server instance.
60
- * @param procedures procedures the server will implement, see {@link ProceduresMap}
61
- * @param options various options
62
- * @param options.scope The worker scope to use, defaults to the `self` of the file where Server() is called.
63
- * @param options.loglevel Maximum log level to use, defaults to "debug" (shows everything). "info" will not show debug messages, "warn" will only show warnings and errors, "error" will only show errors.
64
- * @param options._scopeType @internal Don't touch, this is used in testing environments because the mock is subpar. Manually overrides worker scope type detection.
65
- * @returns a SwarpcServer instance. Each property of the procedures map will be a method, that accepts a function implementing the procedure (see {@link ProcedureImplementation}). There is also .start(), to be called after implementing all procedures.
66
- *
67
- * An example of defining a server:
68
- * {@includeCode ../example/src/service-worker.ts}
69
- */
70
- export function Server<Procedures extends ProceduresMap>(
71
- procedures: Procedures,
72
- {
73
- loglevel = "debug",
74
- scope,
75
- _scopeType,
76
- }: {
77
- scope?: WorkerGlobalScope
78
- loglevel?: LogLevel
79
- _scopeType?: "dedicated" | "shared" | "service"
80
- } = {}
81
- ): SwarpcServer<Procedures> {
82
- const l = createLogger("server", loglevel)
83
-
84
- // If scope is not provided, use the global scope
85
- // This function is meant to be used in a worker, so `self` is a WorkerGlobalScope
86
- scope ??= self as WorkerGlobalScope
87
-
88
- function scopeIsShared(
89
- scope: WorkerGlobalScope
90
- ): scope is SharedWorkerGlobalScope {
91
- return scope instanceof SharedWorkerGlobalScope || _scopeType === "shared"
92
- }
93
-
94
- function scopeIsDedicated(
95
- scope: WorkerGlobalScope
96
- ): scope is DedicatedWorkerGlobalScope {
97
- return (
98
- scope instanceof DedicatedWorkerGlobalScope || _scopeType === "dedicated"
99
- )
100
- }
101
-
102
- function scopeIsService(
103
- scope: WorkerGlobalScope
104
- ): scope is ServiceWorkerGlobalScope {
105
- return scope instanceof ServiceWorkerGlobalScope || _scopeType === "service"
106
- }
107
-
108
- // Initialize the instance.
109
- // Procedures and implementations are stored on properties with symbol keys,
110
- // to avoid any conflicts with procedure names, and also discourage direct access to them.
111
- const instance = {
112
- [zProcedures]: procedures,
113
- [zImplementations]: {} as ImplementationsMap<Procedures>,
114
- start: async () => {},
115
- } as SwarpcServer<Procedures>
116
-
117
- // Set all implementation-setter methods
118
- for (const functionName in procedures) {
119
- instance[functionName] = ((implementation) => {
120
- if (!instance[zProcedures][functionName]) {
121
- throw new Error(`No procedure found for function name: ${functionName}`)
122
- }
123
- instance[zImplementations][functionName] = (input, onProgress, tools) => {
124
- tools.abortSignal?.throwIfAborted()
125
- return new Promise((resolve, reject) => {
126
- tools.abortSignal?.addEventListener("abort", () => {
127
- let { requestId, reason } = tools.abortSignal?.reason
128
- l.debug(requestId, `Aborted ${functionName} request: ${reason}`)
129
- reject({ aborted: reason })
130
- })
131
-
132
- implementation(input, onProgress, tools).then(resolve).catch(reject)
133
- })
134
- }
135
- }) as SwarpcServer<Procedures>[typeof functionName]
136
- }
137
-
138
- instance.start = async () => {
139
- const port = await new Promise<MessagePort | undefined>((resolve) => {
140
- if (!scopeIsShared(scope)) return resolve(undefined)
141
- l.debug(null, "Awaiting shared worker connection...")
142
- scope.addEventListener("connect", ({ ports: [port] }) => {
143
- l.debug(null, "Shared worker connected with port", port)
144
- resolve(port)
145
- })
146
- })
147
-
148
- // Used to post messages back to the client
149
- const postMessage = async (
150
- autotransfer: boolean,
151
- data: Payload<Procedures>
152
- ) => {
153
- const transfer = autotransfer ? [] : findTransferables(data)
154
-
155
- if (port) {
156
- port.postMessage(data, { transfer })
157
- } else if (scopeIsDedicated(scope)) {
158
- scope.postMessage(data, { transfer })
159
- } else if (scopeIsService(scope)) {
160
- await scope.clients.matchAll().then((clients) => {
161
- clients.forEach((client) => client.postMessage(data, { transfer }))
162
- })
163
- }
164
- }
165
-
166
- const listener = async (
167
- event: MessageEvent<any> | ExtendableMessageEvent
168
- ): Promise<void> => {
169
- if (PayloadInitializeSchema.allows(event.data)) {
170
- const { localStorageData } = event.data
171
- l.debug(null, "Setting up faux localStorage", localStorageData)
172
- new FauxLocalStorage(localStorageData).register(scope)
173
- return
174
- }
175
-
176
- // Decode the payload
177
- const { requestId, functionName } = PayloadHeaderSchema(
178
- type.enumerated(...Object.keys(procedures))
179
- ).assert(event.data)
180
-
181
- l.debug(requestId, `Received request for ${functionName}`, event.data)
182
-
183
- // Get autotransfer preference from the procedure definition
184
- const { autotransfer = "output-only", ...schemas } =
185
- instance[zProcedures][functionName]
186
-
187
- // Shorthand function with functionName, requestId, etc. set
188
- const postMsg = async (
189
- data: PayloadCore<Procedures, typeof functionName>
190
- ) => {
191
- if (abortedRequests.has(requestId)) return
192
- await postMessage(autotransfer !== "never", {
193
- by: "sw&rpc",
194
- functionName,
195
- requestId,
196
- ...data,
197
- })
198
- }
199
-
200
- // Prepare a function to post errors back to the client
201
- const postError = async (error: any) =>
202
- postMsg({
203
- error: {
204
- message: "message" in error ? error.message : String(error),
205
- },
206
- })
207
-
208
- // Retrieve the implementation for the requested function
209
- const implementation = instance[zImplementations][functionName]
210
- if (!implementation) {
211
- await postError("No implementation found")
212
- return
213
- }
214
-
215
- // Define payload schema for incoming messages
216
- const payload = PayloadSchema(
217
- type(`"${functionName}"`),
218
- schemas.input,
219
- schemas.progress,
220
- schemas.success
221
- ).assert(event.data)
222
-
223
- if ("localStorageData" in payload)
224
- throw "Unreachable: #initialize request payload should've been handled already"
225
-
226
- // Handle abortion requests (pro-choice ftw!!)
227
- if (payload.abort) {
228
- const controller = abortControllers.get(requestId)
229
-
230
- if (!controller)
231
- await postError("No abort controller found for request")
232
-
233
- controller?.abort(payload.abort.reason)
234
- return
235
- }
236
-
237
- // Set up the abort controller for this request
238
- abortControllers.set(requestId, new AbortController())
239
-
240
- if (!payload.input) {
241
- await postError("No input provided")
242
- return
243
- }
244
-
245
- try {
246
- // Call the implementation with the input and a progress callback
247
- const result = await implementation(
248
- payload.input,
249
- async (progress: any) => {
250
- l.debug(requestId, `Progress for ${functionName}`, progress)
251
- await postMsg({ progress })
252
- },
253
- {
254
- abortSignal: abortControllers.get(requestId)?.signal,
255
- logger: createLogger("server", loglevel, requestId),
256
- }
257
- )
258
-
259
- // Send results
260
- l.debug(requestId, `Result for ${functionName}`, result)
261
- await postMsg({ result })
262
- } catch (error: any) {
263
- // Send errors
264
- // Handle errors caused by abortions
265
- if ("aborted" in error) {
266
- l.debug(
267
- requestId,
268
- `Received abort error for ${functionName}`,
269
- error.aborted
270
- )
271
- abortedRequests.add(requestId)
272
- abortControllers.delete(requestId)
273
- return
274
- }
275
-
276
- l.info(requestId, `Error in ${functionName}`, error)
277
- await postError(error)
278
- } finally {
279
- abortedRequests.delete(requestId)
280
- }
281
- }
282
-
283
- // Listen for messages from the client
284
- if (scopeIsShared(scope)) {
285
- if (!port) throw new Error("SharedWorker port not initialized")
286
- console.log("Listening for shared worker messages on port", port)
287
- port.addEventListener("message", listener)
288
- port.start()
289
- } else if (scopeIsDedicated(scope)) {
290
- scope.addEventListener("message", listener)
291
- } else if (scopeIsService(scope)) {
292
- scope.addEventListener("message", listener)
293
- } else {
294
- throw new Error(`Unsupported worker scope ${scope}`)
295
- }
296
- }
297
-
298
- return instance
299
- }
1
+ /**
2
+ * @module
3
+ * @mergeModuleWith <project>
4
+ */
5
+
6
+ /// <reference lib="webworker" />
7
+ import { type } from "arktype";
8
+ import { createLogger, injectIntoConsoleGlobal, type LogLevel } from "./log.js";
9
+ import {
10
+ ImplementationsMap,
11
+ Payload,
12
+ PayloadCore,
13
+ PayloadHeaderSchema,
14
+ PayloadInitializeSchema,
15
+ PayloadSchema,
16
+ ProcedureImplementation,
17
+ zImplementations,
18
+ zProcedures,
19
+ type ProceduresMap,
20
+ } from "./types.js";
21
+ import { findTransferables } from "./utils.js";
22
+ import { FauxLocalStorage } from "./localstorage.js";
23
+ import { scopeIsDedicated, scopeIsShared, scopeIsService } from "./scopes.js";
24
+ import { nodeIdFromScope } from "./nodes.js";
25
+
26
+ /**
27
+ * The sw&rpc server instance, which provides methods to register {@link ProcedureImplementation | procedure implementations},
28
+ * and listens for incoming messages that call those procedures
29
+ */
30
+ export type SwarpcServer<Procedures extends ProceduresMap> = {
31
+ [zProcedures]: Procedures;
32
+ [zImplementations]: ImplementationsMap<Procedures>;
33
+ start(): Promise<void>;
34
+ } & {
35
+ [F in keyof Procedures]: (
36
+ impl: ProcedureImplementation<
37
+ Procedures[F]["input"],
38
+ Procedures[F]["progress"],
39
+ Procedures[F]["success"]
40
+ >,
41
+ ) => void;
42
+ };
43
+
44
+ const abortControllers = new Map<string, AbortController>();
45
+ const abortedRequests = new Set<string>();
46
+
47
+ /**
48
+ * Creates a sw&rpc server instance.
49
+ * @param procedures procedures the server will implement, see {@link ProceduresMap}
50
+ * @param options various options
51
+ * @param options.scope The worker scope to use, defaults to the `self` of the file where Server() is called.
52
+ * @param options.loglevel Maximum log level to use, defaults to "debug" (shows everything). "info" will not show debug messages, "warn" will only show warnings and errors, "error" will only show errors.
53
+ * @param options._scopeType @internal Don't touch, this is used in testing environments because the mock is subpar. Manually overrides worker scope type detection.
54
+ * @returns a SwarpcServer instance. Each property of the procedures map will be a method, that accepts a function implementing the procedure (see {@link ProcedureImplementation}). There is also .start(), to be called after implementing all procedures.
55
+ *
56
+ * An example of defining a server:
57
+ * {@includeCode ../example/src/service-worker.ts}
58
+ */
59
+ export function Server<Procedures extends ProceduresMap>(
60
+ procedures: Procedures,
61
+ {
62
+ loglevel = "debug",
63
+ scope,
64
+ _scopeType,
65
+ }: {
66
+ scope?: WorkerGlobalScope;
67
+ loglevel?: LogLevel;
68
+ _scopeType?: "dedicated" | "shared" | "service";
69
+ } = {},
70
+ ): SwarpcServer<Procedures> {
71
+ // If scope is not provided, use the global scope
72
+ // This function is meant to be used in a worker, so `self` is a WorkerGlobalScope
73
+ scope ??= self as WorkerGlobalScope;
74
+
75
+ // Service workers don't have a name, but it's fine anyways cuz we don't have multiple nodes when running with a SW
76
+ const nodeId = nodeIdFromScope(scope, _scopeType);
77
+
78
+ const l = createLogger("server", loglevel, nodeId);
79
+
80
+ // Initialize the instance.
81
+ // Procedures and implementations are stored on properties with symbol keys,
82
+ // to avoid any conflicts with procedure names, and also discourage direct access to them.
83
+ const instance = {
84
+ [zProcedures]: procedures,
85
+ [zImplementations]: {} as ImplementationsMap<Procedures>,
86
+ start: async () => {},
87
+ } as SwarpcServer<Procedures>;
88
+
89
+ // Set all implementation-setter methods
90
+ for (const functionName in procedures) {
91
+ instance[functionName] = ((implementation) => {
92
+ if (!instance[zProcedures][functionName]) {
93
+ throw new Error(
94
+ `No procedure found for function name: ${functionName}`,
95
+ );
96
+ }
97
+ instance[zImplementations][functionName] = (input, onProgress, tools) => {
98
+ tools.abortSignal?.throwIfAborted();
99
+ return new Promise((resolve, reject) => {
100
+ tools.abortSignal?.addEventListener("abort", () => {
101
+ let { requestId, reason } = tools.abortSignal!.reason;
102
+ l.debug(requestId, `Aborted ${functionName} request: ${reason}`);
103
+ reject({ aborted: reason });
104
+ });
105
+
106
+ implementation(input, onProgress, tools).then(resolve).catch(reject);
107
+ });
108
+ };
109
+ }) as SwarpcServer<Procedures>[typeof functionName];
110
+ }
111
+
112
+ instance.start = async () => {
113
+ const port = await new Promise<MessagePort | undefined>((resolve) => {
114
+ if (!scopeIsShared(scope, _scopeType)) return resolve(undefined);
115
+ l.debug(null, "Awaiting shared worker connection...");
116
+ scope.addEventListener("connect", ({ ports: [port] }) => {
117
+ l.debug(null, "Shared worker connected with port", port);
118
+ resolve(port);
119
+ });
120
+ });
121
+
122
+ // Used to post messages back to the client
123
+ const postMessage = async (
124
+ autotransfer: boolean,
125
+ data: Payload<Procedures>,
126
+ ) => {
127
+ const transfer = autotransfer ? [] : findTransferables(data);
128
+
129
+ if (port) {
130
+ port.postMessage(data, { transfer });
131
+ } else if (scopeIsDedicated(scope, _scopeType)) {
132
+ scope.postMessage(data, { transfer });
133
+ } else if (scopeIsService(scope, _scopeType)) {
134
+ await scope.clients.matchAll().then((clients) => {
135
+ clients.forEach((client) => client.postMessage(data, { transfer }));
136
+ });
137
+ }
138
+ };
139
+
140
+ const listener = async (
141
+ event: MessageEvent<any> | ExtendableMessageEvent,
142
+ ): Promise<void> => {
143
+ if (PayloadInitializeSchema.allows(event.data)) {
144
+ const { localStorageData, nodeId } = event.data;
145
+ l.debug(null, "Setting up faux localStorage", localStorageData);
146
+ new FauxLocalStorage(localStorageData).register(scope);
147
+ injectIntoConsoleGlobal(scope, nodeId);
148
+ return;
149
+ }
150
+
151
+ // Decode the payload
152
+ const { requestId, functionName } = PayloadHeaderSchema(
153
+ type.enumerated(...Object.keys(procedures)),
154
+ ).assert(event.data);
155
+
156
+ l.debug(requestId, `Received request for ${functionName}`, event.data);
157
+
158
+ // Get autotransfer preference from the procedure definition
159
+ const { autotransfer = "output-only", ...schemas } =
160
+ instance[zProcedures][functionName];
161
+
162
+ // Shorthand function with functionName, requestId, etc. set
163
+ const postMsg = async (
164
+ data: PayloadCore<Procedures, typeof functionName>,
165
+ ) => {
166
+ if (abortedRequests.has(requestId)) return;
167
+ await postMessage(autotransfer !== "never", {
168
+ by: "sw&rpc",
169
+ functionName,
170
+ requestId,
171
+ ...data,
172
+ });
173
+ };
174
+
175
+ // Prepare a function to post errors back to the client
176
+ const postError = async (error: any) =>
177
+ postMsg({
178
+ error: {
179
+ message: "message" in error ? error.message : String(error),
180
+ },
181
+ });
182
+
183
+ // Retrieve the implementation for the requested function
184
+ const implementation = instance[zImplementations][functionName];
185
+ if (!implementation) {
186
+ await postError("No implementation found");
187
+ return;
188
+ }
189
+
190
+ // Define payload schema for incoming messages
191
+ const payload = PayloadSchema(
192
+ type(`"${functionName}"`),
193
+ schemas.input,
194
+ schemas.progress,
195
+ schemas.success,
196
+ ).assert(event.data);
197
+
198
+ if ("isInitializeRequest" in payload)
199
+ throw "Unreachable: #initialize request payload should've been handled already";
200
+
201
+ // Handle abortion requests (pro-choice ftw!!)
202
+ if (payload.abort) {
203
+ const controller = abortControllers.get(requestId);
204
+
205
+ if (!controller)
206
+ await postError("No abort controller found for request");
207
+
208
+ controller?.abort(payload.abort.reason);
209
+ return;
210
+ }
211
+
212
+ // Set up the abort controller for this request
213
+ abortControllers.set(requestId, new AbortController());
214
+
215
+ if (!payload.input) {
216
+ await postError("No input provided");
217
+ return;
218
+ }
219
+
220
+ try {
221
+ // Call the implementation with the input and a progress callback
222
+ const result = await implementation(
223
+ payload.input,
224
+ async (progress: any) => {
225
+ // l.debug(requestId, `Progress for ${functionName}`, progress);
226
+ await postMsg({ progress });
227
+ },
228
+ {
229
+ nodeId,
230
+ abortSignal: abortControllers.get(requestId)?.signal,
231
+ logger: createLogger("server", loglevel, nodeId, requestId),
232
+ },
233
+ );
234
+
235
+ // Send results
236
+ l.debug(requestId, `Result for ${functionName}`, result);
237
+ await postMsg({ result });
238
+ } catch (error: any) {
239
+ // Send errors
240
+ // Handle errors caused by abortions
241
+ if ("aborted" in error) {
242
+ l.debug(
243
+ requestId,
244
+ `Received abort error for ${functionName}`,
245
+ error.aborted,
246
+ );
247
+ abortedRequests.add(requestId);
248
+ abortControllers.delete(requestId);
249
+ return;
250
+ }
251
+
252
+ l.info(requestId, `Error in ${functionName}`, error);
253
+ await postError(error);
254
+ } finally {
255
+ abortedRequests.delete(requestId);
256
+ }
257
+ };
258
+
259
+ // Listen for messages from the client
260
+ if (scopeIsShared(scope, _scopeType)) {
261
+ if (!port) throw new Error("SharedWorker port not initialized");
262
+ l.info(null, "Listening for shared worker messages on port", port);
263
+ port.addEventListener("message", listener);
264
+ port.start();
265
+ } else if (scopeIsDedicated(scope, _scopeType)) {
266
+ scope.addEventListener("message", listener);
267
+ } else if (scopeIsService(scope, _scopeType)) {
268
+ scope.addEventListener("message", listener);
269
+ } else {
270
+ throw new Error(`Unsupported worker scope ${scope}`);
271
+ }
272
+ };
273
+
274
+ return instance;
275
+ }