swarpc 0.12.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/README.md +7 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -1
- package/dist/log.d.ts +5 -0
- package/dist/log.d.ts.map +1 -1
- package/dist/log.js +25 -17
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5 -3
- package/dist/types.d.ts +26 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -0
- package/package.json +4 -2
- package/src/client.ts +3 -1
- package/src/log.ts +41 -22
- package/src/server.ts +5 -3
- package/src/types.ts +6 -0
package/README.md
CHANGED
|
@@ -24,7 +24,11 @@ RPC for Service Workers -- move that heavy computation off of your UI thread!
|
|
|
24
24
|
npm add swarpc arktype
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
<details>
|
|
28
|
+
<summary>
|
|
29
|
+
Bleeding edge
|
|
30
|
+
|
|
31
|
+
</summary>
|
|
28
32
|
|
|
29
33
|
If you want to use the latest commit instead of a published version, you can, either by using the Git URL:
|
|
30
34
|
|
|
@@ -42,6 +46,8 @@ npm add file:vendored/swarpc
|
|
|
42
46
|
|
|
43
47
|
This works thanks to the fact that `dist/` is published on the repository (and kept up to date with a CI workflow).
|
|
44
48
|
|
|
49
|
+
</details>
|
|
50
|
+
|
|
45
51
|
## Usage
|
|
46
52
|
|
|
47
53
|
### 1. Declare your procedures in a shared file
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAEL,kBAAkB,EAClB,KAAK,MAAM,EACX,KAAK,QAAQ,EACd,MAAM,UAAU,CAAC;AAElB,OAAO,EACL,YAAY,EACZ,KAAK,EACL,OAAO,EAEP,iBAAiB,EACjB,WAAW,EACX,KAAK,aAA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;;GAOG;AACH,wBAAgB,eAAe,CAAC,UAAU,SAAS,aAAa,EAC9D,CAAC,EAAE,kBAAkB,EACrB,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,EACzC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,EAC5B,OAAO,CAAC,EAAE,0BAA0B,GACnC,IAAI,CAiBN;AAED;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,UAAU,SAAS,aAAa,EACxE,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,iBAsFzB;AAED;;;;GAIG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAEtC"}
|
package/dist/client.js
CHANGED
|
@@ -211,7 +211,7 @@ export async function startClientListener(ctx) {
|
|
|
211
211
|
// We don't use a arktype schema here, we trust the server to send valid data
|
|
212
212
|
const payload = eventData;
|
|
213
213
|
// Ignore #initialize request, it's client->server only
|
|
214
|
-
if ("
|
|
214
|
+
if ("isInitializeRequest" in payload) {
|
|
215
215
|
l.warn(null, "Ignoring unexpected #initialize from server", payload);
|
|
216
216
|
return;
|
|
217
217
|
}
|
|
@@ -254,7 +254,9 @@ export async function startClientListener(ctx) {
|
|
|
254
254
|
await postMessage(ctx, {
|
|
255
255
|
by: "sw&rpc",
|
|
256
256
|
functionName: "#initialize",
|
|
257
|
+
isInitializeRequest: true,
|
|
257
258
|
localStorageData: ctx.localStorage,
|
|
259
|
+
nodeId: ctx.nodeId ?? "(SW)",
|
|
258
260
|
});
|
|
259
261
|
}
|
|
260
262
|
/**
|
package/dist/log.d.ts
CHANGED
|
@@ -25,5 +25,10 @@ export type RequestBoundLogger = {
|
|
|
25
25
|
/** @source */
|
|
26
26
|
declare const LOG_LEVELS: readonly ["debug", "info", "warn", "error"];
|
|
27
27
|
export type LogLevel = (typeof LOG_LEVELS)[number];
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* @param scope
|
|
31
|
+
*/
|
|
32
|
+
export declare function injectIntoConsoleGlobal(scope: WorkerGlobalScope | SharedWorkerGlobalScope, nodeId: string): void;
|
|
28
33
|
export {};
|
|
29
34
|
//# sourceMappingURL=log.d.ts.map
|
package/dist/log.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,QAAQ,GAAG,QAAQ,EACzB,KAAK,EAAE,QAAQ,EACf,GAAG,CAAC,EAAE,MAAM,GACX,MAAM,CAAC;AACV,wBAAgB,YAAY,CAC1B,IAAI,EAAE,QAAQ,GAAG,QAAQ,EACzB,KAAK,EAAE,QAAQ,EACf,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GACX,kBAAkB,CAAC;AA2BtB;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACtE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACrE,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACrE,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CACvE,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACjD,I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K,IAAI,CAAC;IACrE,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CACvE,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IACjD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChD,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;IAChD,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;CAClD,CAAC;AAEF,cAAc;AACd,QAAA,MAAM,UAAU,6CAA8C,CAAC;AAE/D,MAAM,MAAM,QAAQ,GAAG,CAAC,OAAO,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC;AA8EnD;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,iBAAiB,GAAG,uBAAuB,EAClD,MAAM,EAAE,MAAM,QAKf"}
|
package/dist/log.js
CHANGED
|
@@ -22,22 +22,32 @@ export function createLogger(side, level = "debug", nid, rqid) {
|
|
|
22
22
|
}
|
|
23
23
|
/** @source */
|
|
24
24
|
const LOG_LEVELS = ["debug", "info", "warn", "error"];
|
|
25
|
-
|
|
25
|
+
const PATCHABLE_LOG_METHODS = [
|
|
26
|
+
"debug",
|
|
27
|
+
"info",
|
|
28
|
+
"warn",
|
|
29
|
+
"error",
|
|
30
|
+
"log",
|
|
31
|
+
];
|
|
32
|
+
function logger(method, side, ids) {
|
|
26
33
|
if (ids === undefined || typeof ids === "string") {
|
|
27
34
|
const nid = ids ?? null;
|
|
28
|
-
return (rqid,
|
|
35
|
+
return (rqid, ...args) => log(method, side, { nid, rqid }, ...args);
|
|
29
36
|
}
|
|
30
|
-
return (
|
|
37
|
+
return (...args) => log(method, side, ids, ...args);
|
|
31
38
|
}
|
|
39
|
+
const originalConsole = PATCHABLE_LOG_METHODS.reduce((result, method) => {
|
|
40
|
+
result[method] = console[method];
|
|
41
|
+
return result;
|
|
42
|
+
}, {});
|
|
32
43
|
/**
|
|
33
44
|
* Send log messages to the console, with a helpful prefix.
|
|
34
|
-
* @param
|
|
45
|
+
* @param method
|
|
35
46
|
* @param side
|
|
36
47
|
* @param ids request ID and node ID
|
|
37
|
-
* @param message
|
|
38
48
|
* @param args passed to console methods directly
|
|
39
49
|
*/
|
|
40
|
-
function log(
|
|
50
|
+
function log(method, side, { rqid, nid }, ...args) {
|
|
41
51
|
const prefix = [
|
|
42
52
|
`[SWARPC ${side}]`,
|
|
43
53
|
rqid ? `%c${rqid}%c` : "",
|
|
@@ -50,16 +60,14 @@ function log(severity, side, { rqid, nid }, message, ...args) {
|
|
|
50
60
|
prefixStyles.push("color: cyan", "color: inherit");
|
|
51
61
|
if (nid)
|
|
52
62
|
prefixStyles.push("color: hotpink", "color: inherit");
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
else if (severity === "error") {
|
|
63
|
-
console.error(prefix, ...prefixStyles, message, ...args);
|
|
63
|
+
return originalConsole[method](prefix, ...prefixStyles, ...args);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
* @param scope
|
|
68
|
+
*/
|
|
69
|
+
export function injectIntoConsoleGlobal(scope, nodeId) {
|
|
70
|
+
for (const method of PATCHABLE_LOG_METHODS) {
|
|
71
|
+
scope.self.console[method] = logger(method, "server", nodeId);
|
|
64
72
|
}
|
|
65
73
|
}
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAyC,KAAK,QAAQ,EAAE,MAAM,UAAU,CAAC;AAChF,OAAO,EACL,kBAAkB,EAMlB,uBAAuB,EACvB,gBAAgB,EAChB,WAAW,EACX,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AAMpB;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,UAAU,SAAS,aAAa,IAAI;IAC3D,CAAC,WAAW,CAAC,EAAE,UAAU,CAAC;IAC1B,CAAC,gBAAgB,CAAC,EAAE,kBAAkB,CAAC,UAAU,CAAC,CAAC;IACnD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB,GAAG;KACD,CAAC,IAAI,MAAM,UAAU,GAAG,CACvB,IAAI,EAAE,uBAAuB,CAC3B,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,EACtB,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EACzB,UAAU,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CACzB,KACE,IAAI;CACV,CAAC;AAKF;;;;;;;;;;;GAWG;AACH,wBAAgB,MAAM,CAAC,UAAU,SAAS,aAAa,EACrD,UAAU,EAAE,UAAU,EACtB,EACE,QAAkB,EAClB,KAAK,EACL,UAAU,GACX,GAAE;IACD,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,UAAU,CAAC,EAAE,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAC;CAC5C,GACL,YAAY,CAAC,UAAU,CAAC,CA6M1B"}
|
package/dist/server.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
/// <reference lib="webworker" />
|
|
6
6
|
import { type } from "arktype";
|
|
7
|
-
import { createLogger } from "./log.js";
|
|
7
|
+
import { createLogger, injectIntoConsoleGlobal } from "./log.js";
|
|
8
8
|
import { PayloadHeaderSchema, PayloadInitializeSchema, PayloadSchema, zImplementations, zProcedures, } from "./types.js";
|
|
9
9
|
import { findTransferables } from "./utils.js";
|
|
10
10
|
import { FauxLocalStorage } from "./localstorage.js";
|
|
@@ -85,9 +85,10 @@ export function Server(procedures, { loglevel = "debug", scope, _scopeType, } =
|
|
|
85
85
|
};
|
|
86
86
|
const listener = async (event) => {
|
|
87
87
|
if (PayloadInitializeSchema.allows(event.data)) {
|
|
88
|
-
const { localStorageData } = event.data;
|
|
88
|
+
const { localStorageData, nodeId } = event.data;
|
|
89
89
|
l.debug(null, "Setting up faux localStorage", localStorageData);
|
|
90
90
|
new FauxLocalStorage(localStorageData).register(scope);
|
|
91
|
+
injectIntoConsoleGlobal(scope, nodeId);
|
|
91
92
|
return;
|
|
92
93
|
}
|
|
93
94
|
// Decode the payload
|
|
@@ -120,7 +121,7 @@ export function Server(procedures, { loglevel = "debug", scope, _scopeType, } =
|
|
|
120
121
|
}
|
|
121
122
|
// Define payload schema for incoming messages
|
|
122
123
|
const payload = PayloadSchema(type(`"${functionName}"`), schemas.input, schemas.progress, schemas.success).assert(event.data);
|
|
123
|
-
if ("
|
|
124
|
+
if ("isInitializeRequest" in payload)
|
|
124
125
|
throw "Unreachable: #initialize request payload should've been handled already";
|
|
125
126
|
// Handle abortion requests (pro-choice ftw!!)
|
|
126
127
|
if (payload.abort) {
|
|
@@ -142,6 +143,7 @@ export function Server(procedures, { loglevel = "debug", scope, _scopeType, } =
|
|
|
142
143
|
// l.debug(requestId, `Progress for ${functionName}`, progress);
|
|
143
144
|
await postMsg({ progress });
|
|
144
145
|
}, {
|
|
146
|
+
nodeId,
|
|
145
147
|
abortSignal: abortControllers.get(requestId)?.signal,
|
|
146
148
|
logger: createLogger("server", loglevel, nodeId, requestId),
|
|
147
149
|
});
|
package/dist/types.d.ts
CHANGED
|
@@ -74,6 +74,10 @@ tools: {
|
|
|
74
74
|
* Logger instance to use for logging messages related to this procedure call, using the same format as SWARPC's built-in logging.
|
|
75
75
|
*/
|
|
76
76
|
logger: RequestBoundLogger;
|
|
77
|
+
/**
|
|
78
|
+
* ID of the Node the request is being processed on.
|
|
79
|
+
*/
|
|
80
|
+
nodeId: string;
|
|
77
81
|
}) => Promise<S["inferIn"]>;
|
|
78
82
|
/**
|
|
79
83
|
* Declarations of procedures by name.
|
|
@@ -108,7 +112,9 @@ export type Hooks<Procedures extends ProceduresMap> = {
|
|
|
108
112
|
export declare const PayloadInitializeSchema: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
109
113
|
by: "sw&rpc";
|
|
110
114
|
functionName: "#initialize";
|
|
115
|
+
isInitializeRequest: true;
|
|
111
116
|
localStorageData: Record<string, unknown>;
|
|
117
|
+
nodeId: string;
|
|
112
118
|
}, {}>;
|
|
113
119
|
export type PayloadInitialize = typeof PayloadInitializeSchema.infer;
|
|
114
120
|
/**
|
|
@@ -187,12 +193,16 @@ export declare const PayloadSchema: import("arktype").Generic<[["Name", string],
|
|
|
187
193
|
PayloadInitializeSchema: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
188
194
|
by: "sw&rpc";
|
|
189
195
|
functionName: "#initialize";
|
|
196
|
+
isInitializeRequest: true;
|
|
190
197
|
localStorageData: Record<string, unknown>;
|
|
198
|
+
nodeId: string;
|
|
191
199
|
}, {}> & {
|
|
192
200
|
readonly " brand": [import("arktype/internal/methods/object.ts").ObjectType<{
|
|
193
201
|
by: "sw&rpc";
|
|
194
202
|
functionName: "#initialize";
|
|
203
|
+
isInitializeRequest: true;
|
|
195
204
|
localStorageData: Record<string, unknown>;
|
|
205
|
+
nodeId: string;
|
|
196
206
|
}, {}>, "unparsed"];
|
|
197
207
|
};
|
|
198
208
|
} & {}>;
|
|
@@ -220,19 +230,25 @@ export declare const PayloadSchema: import("arktype").Generic<[["Name", string],
|
|
|
220
230
|
PayloadInitializeSchema: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
221
231
|
by: "sw&rpc";
|
|
222
232
|
functionName: "#initialize";
|
|
233
|
+
isInitializeRequest: true;
|
|
223
234
|
localStorageData: Record<string, unknown>;
|
|
235
|
+
nodeId: string;
|
|
224
236
|
}, {}> & {
|
|
225
237
|
readonly " brand": [import("arktype/internal/methods/object.ts").ObjectType<{
|
|
226
238
|
by: "sw&rpc";
|
|
227
239
|
functionName: "#initialize";
|
|
240
|
+
isInitializeRequest: true;
|
|
228
241
|
localStorageData: Record<string, unknown>;
|
|
242
|
+
nodeId: string;
|
|
229
243
|
}, {}>, "unparsed"];
|
|
230
244
|
};
|
|
231
245
|
} & {}>;
|
|
232
246
|
PayloadInitializeSchema: {
|
|
233
247
|
by: "sw&rpc";
|
|
234
248
|
functionName: "#initialize";
|
|
249
|
+
isInitializeRequest: true;
|
|
235
250
|
localStorageData: Record<string, unknown>;
|
|
251
|
+
nodeId: string;
|
|
236
252
|
};
|
|
237
253
|
}, {
|
|
238
254
|
PayloadCoreSchema: import("arktype/internal/scope.ts").bindGenericToScope<import("@ark/schema").GenericAst<[["I", unknown], ["P", unknown], ["S", unknown]], {
|
|
@@ -265,12 +281,16 @@ export declare const PayloadSchema: import("arktype").Generic<[["Name", string],
|
|
|
265
281
|
PayloadInitializeSchema: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
266
282
|
by: "sw&rpc";
|
|
267
283
|
functionName: "#initialize";
|
|
284
|
+
isInitializeRequest: true;
|
|
268
285
|
localStorageData: Record<string, unknown>;
|
|
286
|
+
nodeId: string;
|
|
269
287
|
}, {}> & {
|
|
270
288
|
readonly " brand": [import("arktype/internal/methods/object.ts").ObjectType<{
|
|
271
289
|
by: "sw&rpc";
|
|
272
290
|
functionName: "#initialize";
|
|
291
|
+
isInitializeRequest: true;
|
|
273
292
|
localStorageData: Record<string, unknown>;
|
|
293
|
+
nodeId: string;
|
|
274
294
|
}, {}>, "unparsed"];
|
|
275
295
|
};
|
|
276
296
|
} & {}>;
|
|
@@ -298,19 +318,25 @@ export declare const PayloadSchema: import("arktype").Generic<[["Name", string],
|
|
|
298
318
|
PayloadInitializeSchema: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
299
319
|
by: "sw&rpc";
|
|
300
320
|
functionName: "#initialize";
|
|
321
|
+
isInitializeRequest: true;
|
|
301
322
|
localStorageData: Record<string, unknown>;
|
|
323
|
+
nodeId: string;
|
|
302
324
|
}, {}> & {
|
|
303
325
|
readonly " brand": [import("arktype/internal/methods/object.ts").ObjectType<{
|
|
304
326
|
by: "sw&rpc";
|
|
305
327
|
functionName: "#initialize";
|
|
328
|
+
isInitializeRequest: true;
|
|
306
329
|
localStorageData: Record<string, unknown>;
|
|
330
|
+
nodeId: string;
|
|
307
331
|
}, {}>, "unparsed"];
|
|
308
332
|
};
|
|
309
333
|
} & {}>;
|
|
310
334
|
PayloadInitializeSchema: {
|
|
311
335
|
by: "sw&rpc";
|
|
312
336
|
functionName: "#initialize";
|
|
337
|
+
isInitializeRequest: true;
|
|
313
338
|
localStorageData: Record<string, unknown>;
|
|
339
|
+
nodeId: string;
|
|
314
340
|
};
|
|
315
341
|
}>;
|
|
316
342
|
/**
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAQ,KAAK,IAAI,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,IAAI,EAAE,CAAC,SAAS,IAAI,EAAE,CAAC,SAAS,IAAI,IAAI;IACtE;;OAEG;IACH,KAAK,EAAE,CAAC,CAAC;IACT;;;OAGG;IACH,QAAQ,EAAE,CAAC,CAAC;I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AAI,EAC1D,SAAS,CAAC,EAAE,MAAM,KACf,iBAAiB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC;IACjD;;;;OAIG;IACH,SAAS,EAAE,CACT,KAAK,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,SAAS,CAAC,EAC5B,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,UAAU,CAAC,KAAK,IAAI;IAC1D,mFAAmF;IACnF,KAAK,CAAC,EAAE,MAAM,KACX,OAAO,CACV,KAAK,CAAC,oBAAoB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,CAAC,GAAG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CACzE,CAAC;CACH,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,eAAmC,CAAC;AAEjE;;;;GAIG;AACH,eAAO,MAAM,WAAW,eAA8B,CAAC;AAEvD,MAAM,MAAM,iBAAiB,CAC3B,CAAC,SAAS,MAAM,GAAG,YAAY,GAAG,MAAM,GAAG,YAAY,IACrD;IACF,KAAK,IAAI,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,CAAC,CAAC;CACnC,CAAC"}
|
package/dist/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swarpc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "Full type-safe RPC library for service worker -- move things off of the UI thread with ease!",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"service-workers",
|
|
@@ -40,12 +40,14 @@
|
|
|
40
40
|
"typedoc:withplugins": "npm run typedoc:plugins && npm run typedoc",
|
|
41
41
|
"prepare": "husky"
|
|
42
42
|
},
|
|
43
|
-
"
|
|
43
|
+
"peerDependencies": {
|
|
44
44
|
"arktype": "^2.1.22"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@8hobbies/typedoc-plugin-plausible": "^2.2.0",
|
|
48
|
+
"@playwright/test": "^1.55.0",
|
|
48
49
|
"@vitest/web-worker": "^3.2.4",
|
|
50
|
+
"date-fns": "^4.1.0",
|
|
49
51
|
"husky": "^9.1.7",
|
|
50
52
|
"kacl": "^1.1.1",
|
|
51
53
|
"knip": "^5.63.1",
|
package/src/client.ts
CHANGED
|
@@ -367,7 +367,7 @@ export async function startClientListener<Procedures extends ProceduresMap>(
|
|
|
367
367
|
const payload = eventData as Payload<Procedures>;
|
|
368
368
|
|
|
369
369
|
// Ignore #initialize request, it's client->server only
|
|
370
|
-
if ("
|
|
370
|
+
if ("isInitializeRequest" in payload) {
|
|
371
371
|
l.warn(null, "Ignoring unexpected #initialize from server", payload);
|
|
372
372
|
return;
|
|
373
373
|
}
|
|
@@ -416,7 +416,9 @@ export async function startClientListener<Procedures extends ProceduresMap>(
|
|
|
416
416
|
await postMessage(ctx, {
|
|
417
417
|
by: "sw&rpc",
|
|
418
418
|
functionName: "#initialize",
|
|
419
|
+
isInitializeRequest: true,
|
|
419
420
|
localStorageData: ctx.localStorage,
|
|
421
|
+
nodeId: ctx.nodeId ?? "(SW)",
|
|
420
422
|
});
|
|
421
423
|
}
|
|
422
424
|
|
package/src/log.ts
CHANGED
|
@@ -65,51 +65,65 @@ const LOG_LEVELS = ["debug", "info", "warn", "error"] as const;
|
|
|
65
65
|
|
|
66
66
|
export type LogLevel = (typeof LOG_LEVELS)[number];
|
|
67
67
|
|
|
68
|
+
const PATCHABLE_LOG_METHODS = [
|
|
69
|
+
"debug",
|
|
70
|
+
"info",
|
|
71
|
+
"warn",
|
|
72
|
+
"error",
|
|
73
|
+
"log",
|
|
74
|
+
] as const;
|
|
75
|
+
type LogMethod = (typeof PATCHABLE_LOG_METHODS)[number];
|
|
76
|
+
|
|
68
77
|
/**
|
|
69
78
|
* Creates partially-applied logging functions given the first 2 or 3 args
|
|
70
|
-
* @param
|
|
79
|
+
* @param method
|
|
71
80
|
* @param side
|
|
72
81
|
* @param ids request ID, {reqid, nodeId}, or null to not bind it
|
|
73
82
|
* @returns
|
|
74
83
|
*/
|
|
75
84
|
function logger(
|
|
76
|
-
|
|
85
|
+
method: LogMethod,
|
|
77
86
|
side: "server" | "client",
|
|
78
87
|
ids: { rqid: string; nid: string },
|
|
79
|
-
): (
|
|
88
|
+
): (...args: any[]) => void;
|
|
80
89
|
function logger(
|
|
81
|
-
|
|
90
|
+
method: LogMethod,
|
|
82
91
|
side: "server" | "client",
|
|
83
92
|
nid?: string,
|
|
84
|
-
): (rqid: string | null,
|
|
93
|
+
): (rqid: string | null, ...args: any[]) => void;
|
|
85
94
|
function logger(
|
|
86
|
-
|
|
95
|
+
method: LogMethod,
|
|
87
96
|
side: "server" | "client",
|
|
88
97
|
ids?: string | { rqid: string; nid: string },
|
|
89
98
|
) {
|
|
90
99
|
if (ids === undefined || typeof ids === "string") {
|
|
91
100
|
const nid = ids ?? null;
|
|
92
|
-
return (rqid: string | null,
|
|
93
|
-
log(
|
|
101
|
+
return (rqid: string | null, ...args: any[]) =>
|
|
102
|
+
log(method, side, { nid, rqid }, ...args);
|
|
94
103
|
}
|
|
95
104
|
|
|
96
|
-
return (
|
|
97
|
-
log(severity, side, ids, message, ...args);
|
|
105
|
+
return (...args: any[]) => log(method, side, ids, ...args);
|
|
98
106
|
}
|
|
99
107
|
|
|
108
|
+
const originalConsole = PATCHABLE_LOG_METHODS.reduce(
|
|
109
|
+
(result, method) => {
|
|
110
|
+
result[method] = console[method];
|
|
111
|
+
return result;
|
|
112
|
+
},
|
|
113
|
+
{} as Pick<typeof console, LogMethod>,
|
|
114
|
+
);
|
|
115
|
+
|
|
100
116
|
/**
|
|
101
117
|
* Send log messages to the console, with a helpful prefix.
|
|
102
|
-
* @param
|
|
118
|
+
* @param method
|
|
103
119
|
* @param side
|
|
104
120
|
* @param ids request ID and node ID
|
|
105
|
-
* @param message
|
|
106
121
|
* @param args passed to console methods directly
|
|
107
122
|
*/
|
|
108
123
|
function log(
|
|
109
|
-
|
|
124
|
+
method: LogMethod,
|
|
110
125
|
side: "server" | "client",
|
|
111
126
|
{ rqid, nid }: { rqid: string | null; nid: string | null },
|
|
112
|
-
message: string,
|
|
113
127
|
...args: any[]
|
|
114
128
|
) {
|
|
115
129
|
const prefix = [
|
|
@@ -124,13 +138,18 @@ function log(
|
|
|
124
138
|
if (rqid) prefixStyles.push("color: cyan", "color: inherit");
|
|
125
139
|
if (nid) prefixStyles.push("color: hotpink", "color: inherit");
|
|
126
140
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
141
|
+
return originalConsole[method](prefix, ...prefixStyles, ...args);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
*
|
|
146
|
+
* @param scope
|
|
147
|
+
*/
|
|
148
|
+
export function injectIntoConsoleGlobal(
|
|
149
|
+
scope: WorkerGlobalScope | SharedWorkerGlobalScope,
|
|
150
|
+
nodeId: string,
|
|
151
|
+
) {
|
|
152
|
+
for (const method of PATCHABLE_LOG_METHODS) {
|
|
153
|
+
scope.self.console[method] = logger(method, "server", nodeId);
|
|
135
154
|
}
|
|
136
155
|
}
|
package/src/server.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
/// <reference lib="webworker" />
|
|
7
7
|
import { type } from "arktype";
|
|
8
|
-
import { createLogger, type LogLevel } from "./log.js";
|
|
8
|
+
import { createLogger, injectIntoConsoleGlobal, type LogLevel } from "./log.js";
|
|
9
9
|
import {
|
|
10
10
|
ImplementationsMap,
|
|
11
11
|
Payload,
|
|
@@ -141,9 +141,10 @@ export function Server<Procedures extends ProceduresMap>(
|
|
|
141
141
|
event: MessageEvent<any> | ExtendableMessageEvent,
|
|
142
142
|
): Promise<void> => {
|
|
143
143
|
if (PayloadInitializeSchema.allows(event.data)) {
|
|
144
|
-
const { localStorageData } = event.data;
|
|
144
|
+
const { localStorageData, nodeId } = event.data;
|
|
145
145
|
l.debug(null, "Setting up faux localStorage", localStorageData);
|
|
146
146
|
new FauxLocalStorage(localStorageData).register(scope);
|
|
147
|
+
injectIntoConsoleGlobal(scope, nodeId);
|
|
147
148
|
return;
|
|
148
149
|
}
|
|
149
150
|
|
|
@@ -194,7 +195,7 @@ export function Server<Procedures extends ProceduresMap>(
|
|
|
194
195
|
schemas.success,
|
|
195
196
|
).assert(event.data);
|
|
196
197
|
|
|
197
|
-
if ("
|
|
198
|
+
if ("isInitializeRequest" in payload)
|
|
198
199
|
throw "Unreachable: #initialize request payload should've been handled already";
|
|
199
200
|
|
|
200
201
|
// Handle abortion requests (pro-choice ftw!!)
|
|
@@ -225,6 +226,7 @@ export function Server<Procedures extends ProceduresMap>(
|
|
|
225
226
|
await postMsg({ progress });
|
|
226
227
|
},
|
|
227
228
|
{
|
|
229
|
+
nodeId,
|
|
228
230
|
abortSignal: abortControllers.get(requestId)?.signal,
|
|
229
231
|
logger: createLogger("server", loglevel, nodeId, requestId),
|
|
230
232
|
},
|
package/src/types.ts
CHANGED
|
@@ -82,6 +82,10 @@ export type ProcedureImplementation<
|
|
|
82
82
|
* Logger instance to use for logging messages related to this procedure call, using the same format as SWARPC's built-in logging.
|
|
83
83
|
*/
|
|
84
84
|
logger: RequestBoundLogger;
|
|
85
|
+
/**
|
|
86
|
+
* ID of the Node the request is being processed on.
|
|
87
|
+
*/
|
|
88
|
+
nodeId: string;
|
|
85
89
|
},
|
|
86
90
|
) => Promise<S["inferIn"]>;
|
|
87
91
|
|
|
@@ -134,7 +138,9 @@ export type Hooks<Procedures extends ProceduresMap> = {
|
|
|
134
138
|
export const PayloadInitializeSchema = type({
|
|
135
139
|
by: '"sw&rpc"',
|
|
136
140
|
functionName: '"#initialize"',
|
|
141
|
+
isInitializeRequest: "true",
|
|
137
142
|
localStorageData: "Record<string, unknown>",
|
|
143
|
+
nodeId: "string",
|
|
138
144
|
});
|
|
139
145
|
|
|
140
146
|
export type PayloadInitialize = typeof PayloadInitializeSchema.infer;
|