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