swarpc 0.15.0 → 0.16.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 +30 -4
- package/dist/client.d.ts +2 -49
- package/dist/client.js +3 -75
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -6
- package/dist/localstorage.d.ts +1 -14
- package/dist/localstorage.js +0 -1
- package/dist/log.d.ts +0 -29
- package/dist/log.js +2 -18
- package/dist/nodes.d.ts +0 -14
- package/dist/nodes.js +1 -9
- package/dist/polyfills.d.ts +0 -1
- package/dist/polyfills.js +0 -10
- package/dist/scopes.d.ts +1 -4
- package/dist/server.d.ts +1 -2
- package/dist/server.js +9 -45
- package/dist/standardschema.d.ts +0 -1
- package/dist/types.d.ts +0 -67
- package/dist/types.js +63 -47
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +0 -1
- package/package.json +13 -7
- package/dist/client.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/localstorage.d.ts.map +0 -1
- package/dist/log.d.ts.map +0 -1
- package/dist/nodes.d.ts.map +0 -1
- package/dist/polyfills.d.ts.map +0 -1
- package/dist/scopes.d.ts.map +0 -1
- package/dist/server.d.ts.map +0 -1
- package/dist/standardschema.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/src/client.ts +0 -448
- package/src/index.ts +0 -9
- package/src/localstorage.ts +0 -46
- package/src/log.ts +0 -155
- package/src/nodes.ts +0 -63
- package/src/polyfills.ts +0 -22
- package/src/scopes.ts +0 -35
- package/src/server.ts +0 -270
- package/src/standardschema.ts +0 -70
- package/src/types.ts +0 -300
- package/src/utils.ts +0 -34
package/src/types.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module
|
|
3
|
-
* @mergeModuleWith <project>
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
StandardSchemaV1 as Schema,
|
|
8
|
-
StandardSchemaV1,
|
|
9
|
-
} from "./standardschema.js";
|
|
10
|
-
import { ArkErrors, type } from "arktype";
|
|
11
|
-
import { RequestBoundLogger } from "./log.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* A procedure declaration
|
|
15
|
-
*/
|
|
16
|
-
export type Procedure<I extends Schema, P extends Schema, S extends Schema> = {
|
|
17
|
-
/**
|
|
18
|
-
* ArkType type for the input (first argument) of the procedure, when calling it from the client.
|
|
19
|
-
*/
|
|
20
|
-
input: I;
|
|
21
|
-
/**
|
|
22
|
-
* ArkType type for the data as the first argument given to the `onProgress` callback
|
|
23
|
-
* when calling the procedure from the client.
|
|
24
|
-
*/
|
|
25
|
-
progress: P;
|
|
26
|
-
/**
|
|
27
|
-
* ArkType type for the output (return value) of the procedure, when calling it from the client.
|
|
28
|
-
*/
|
|
29
|
-
success: S;
|
|
30
|
-
/**
|
|
31
|
-
* When should the procedure automatically add ArrayBuffers and other transferable objects
|
|
32
|
-
* to the [transfer list](https://developer.mozilla.org/en-US/docs/Web/API/DedicatedWorkerGlobalScope/postMessage#transfer)
|
|
33
|
-
* when sending messages, both from the client to the server and vice versa.
|
|
34
|
-
*
|
|
35
|
-
* Transferring objects can improve performance by avoiding copies of large objects,
|
|
36
|
-
* but _moves_ them to the other context, meaning that they cannot be used in the original context after being sent.
|
|
37
|
-
*
|
|
38
|
-
* 'output-only' by default: only transferables sent from the server to the client will be transferred.
|
|
39
|
-
*/
|
|
40
|
-
autotransfer?: "always" | "never" | "output-only";
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* A promise that you can cancel by calling `.cancel(reason)` on it:
|
|
45
|
-
*
|
|
46
|
-
* ```js
|
|
47
|
-
* const { request, cancel } = client.runProcedure.cancelable(input, onProgress)
|
|
48
|
-
* setTimeout(() => cancel("Cancelled by user"), 1000)
|
|
49
|
-
* const result = await request
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export type CancelablePromise<T = unknown> = {
|
|
53
|
-
request: Promise<T>;
|
|
54
|
-
/**
|
|
55
|
-
* Abort the request.
|
|
56
|
-
* @param reason The reason for cancelling the request.
|
|
57
|
-
*/
|
|
58
|
-
cancel: (reason: string) => void;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* An implementation of a procedure
|
|
63
|
-
*/
|
|
64
|
-
export type ProcedureImplementation<
|
|
65
|
-
I extends Schema,
|
|
66
|
-
P extends Schema,
|
|
67
|
-
S extends Schema,
|
|
68
|
-
> = (
|
|
69
|
-
/**
|
|
70
|
-
* Input data for the procedure
|
|
71
|
-
*/
|
|
72
|
-
input: Schema.InferInput<I>,
|
|
73
|
-
/**
|
|
74
|
-
* Callback to call with progress updates.
|
|
75
|
-
*/
|
|
76
|
-
onProgress: (progress: Schema.InferInput<P>) => void,
|
|
77
|
-
/**
|
|
78
|
-
* Additional tools useful when implementing the procedure.
|
|
79
|
-
*/
|
|
80
|
-
tools: {
|
|
81
|
-
/**
|
|
82
|
-
* AbortSignal that can be used to handle request cancellation -- see [Make cancellable requests](https://gwennlbh.github.io/swarpc/docs/#make-cancelable-requests)
|
|
83
|
-
*/
|
|
84
|
-
abortSignal?: AbortSignal;
|
|
85
|
-
/**
|
|
86
|
-
* Logger instance to use for logging messages related to this procedure call, using the same format as SWARPC's built-in logging.
|
|
87
|
-
*/
|
|
88
|
-
logger: RequestBoundLogger;
|
|
89
|
-
/**
|
|
90
|
-
* ID of the Node the request is being processed on.
|
|
91
|
-
*/
|
|
92
|
-
nodeId: string;
|
|
93
|
-
},
|
|
94
|
-
) => Promise<Schema.InferInput<S>>;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Declarations of procedures by name.
|
|
98
|
-
*
|
|
99
|
-
* An example of declaring procedures:
|
|
100
|
-
* {@includeCode ../example/src/lib/procedures.ts}
|
|
101
|
-
*/
|
|
102
|
-
export type ProceduresMap = Record<string, Procedure<Schema, Schema, Schema>>;
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Implementations of procedures by name
|
|
106
|
-
*/
|
|
107
|
-
export type ImplementationsMap<Procedures extends ProceduresMap> = {
|
|
108
|
-
[F in keyof Procedures]: ProcedureImplementation<
|
|
109
|
-
Procedures[F]["input"],
|
|
110
|
-
Procedures[F]["progress"],
|
|
111
|
-
Procedures[F]["success"]
|
|
112
|
-
>;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Declaration of hooks to run on messages received from the server
|
|
117
|
-
*/
|
|
118
|
-
export type Hooks<Procedures extends ProceduresMap> = {
|
|
119
|
-
/**
|
|
120
|
-
* Called when a procedure call has been successful.
|
|
121
|
-
*/
|
|
122
|
-
success?: <Procedure extends keyof ProceduresMap>(
|
|
123
|
-
procedure: Procedure,
|
|
124
|
-
data: Schema.InferOutput<Procedures[Procedure]["success"]>,
|
|
125
|
-
) => void;
|
|
126
|
-
/**
|
|
127
|
-
* Called when a procedure call has failed.
|
|
128
|
-
*/
|
|
129
|
-
error?: <Procedure extends keyof ProceduresMap>(
|
|
130
|
-
procedure: Procedure,
|
|
131
|
-
error: Error,
|
|
132
|
-
) => void;
|
|
133
|
-
/**
|
|
134
|
-
* Called when a procedure call sends progress updates.
|
|
135
|
-
*/
|
|
136
|
-
progress?: <Procedure extends keyof ProceduresMap>(
|
|
137
|
-
procedure: Procedure,
|
|
138
|
-
data: Schema.InferOutput<Procedures[Procedure]["progress"]>,
|
|
139
|
-
) => void;
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
export const PayloadInitializeSchema = type({
|
|
143
|
-
by: '"sw&rpc"',
|
|
144
|
-
functionName: '"#initialize"',
|
|
145
|
-
isInitializeRequest: "true",
|
|
146
|
-
localStorageData: "Record<string, unknown>",
|
|
147
|
-
nodeId: "string",
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
export type PayloadInitialize = typeof PayloadInitializeSchema.infer;
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* @source
|
|
154
|
-
*/
|
|
155
|
-
export const PayloadHeaderSchema = type("<Name extends string>", {
|
|
156
|
-
by: '"sw&rpc"',
|
|
157
|
-
functionName: "Name",
|
|
158
|
-
requestId: "string >= 1",
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
export type PayloadHeader<
|
|
162
|
-
PM extends ProceduresMap,
|
|
163
|
-
Name extends keyof PM = keyof PM,
|
|
164
|
-
> = {
|
|
165
|
-
by: "sw&rpc";
|
|
166
|
-
functionName: Name & string;
|
|
167
|
-
requestId: string;
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* @source
|
|
172
|
-
*/
|
|
173
|
-
export const PayloadCoreSchema = type("<I, P, S>", {
|
|
174
|
-
"input?": "I",
|
|
175
|
-
"progress?": "P",
|
|
176
|
-
"result?": "S",
|
|
177
|
-
"abort?": { reason: "string" },
|
|
178
|
-
"error?": { message: "string" },
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
export type PayloadCore<
|
|
182
|
-
PM extends ProceduresMap,
|
|
183
|
-
Name extends keyof PM = keyof PM,
|
|
184
|
-
> =
|
|
185
|
-
| {
|
|
186
|
-
input: Schema.InferOutput<PM[Name]["input"]>;
|
|
187
|
-
}
|
|
188
|
-
| {
|
|
189
|
-
progress: Schema.InferOutput<PM[Name]["progress"]>;
|
|
190
|
-
}
|
|
191
|
-
| {
|
|
192
|
-
result: Schema.InferOutput<PM[Name]["success"]>;
|
|
193
|
-
}
|
|
194
|
-
| {
|
|
195
|
-
abort: { reason: string };
|
|
196
|
-
}
|
|
197
|
-
| {
|
|
198
|
-
error: { message: string };
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const AbortOrError = type.or(
|
|
202
|
-
{ abort: { reason: "string" } },
|
|
203
|
-
{ error: { message: "string" } },
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @source
|
|
208
|
-
*/
|
|
209
|
-
export function validatePayloadCore<
|
|
210
|
-
PM extends ProceduresMap,
|
|
211
|
-
Name extends keyof PM,
|
|
212
|
-
>(procedure: PM[Name], payload: unknown): PayloadCore<PM, keyof PM> {
|
|
213
|
-
if (typeof payload !== "object") throw new Error("payload is not an object");
|
|
214
|
-
if (payload === null) throw new Error("payload is null");
|
|
215
|
-
|
|
216
|
-
if ("input" in payload) {
|
|
217
|
-
const input = procedure.input["~standard"].validate(payload.input);
|
|
218
|
-
if ("value" in input) return { input: input.value };
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
if ("progress" in payload) {
|
|
222
|
-
const progress = procedure.progress["~standard"].validate(payload.progress);
|
|
223
|
-
if ("value" in progress) return { progress: progress.value };
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
if ("result" in payload) {
|
|
227
|
-
const result = procedure.success["~standard"].validate(payload.result);
|
|
228
|
-
if ("value" in result) return { result: result.value };
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const abortOrError = AbortOrError(payload);
|
|
232
|
-
if (!(abortOrError instanceof ArkErrors)) {
|
|
233
|
-
return abortOrError;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
throw new Error("invalid payload");
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* The effective payload as sent by the server to the client
|
|
241
|
-
*/
|
|
242
|
-
export type Payload<
|
|
243
|
-
PM extends ProceduresMap,
|
|
244
|
-
Name extends keyof PM = keyof PM,
|
|
245
|
-
> = (PayloadHeader<PM, Name> & PayloadCore<PM, Name>) | PayloadInitialize;
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* A procedure's corresponding method on the client instance -- used to call the procedure. If you want to be able to cancel the request, you can use the `cancelable` method instead of running the procedure directly.
|
|
249
|
-
*/
|
|
250
|
-
export type ClientMethod<P extends Procedure<Schema, Schema, Schema>> = ((
|
|
251
|
-
input: Schema.InferInput<P["input"]>,
|
|
252
|
-
onProgress?: (progress: Schema.InferOutput<P["progress"]>) => void,
|
|
253
|
-
) => Promise<Schema.InferOutput<P["success"]>>) & {
|
|
254
|
-
/**
|
|
255
|
-
* A method that returns a `CancelablePromise`. Cancel it by calling `.cancel(reason)` on it, and wait for the request to resolve by awaiting the `request` property on the returned object.
|
|
256
|
-
*/
|
|
257
|
-
cancelable: (
|
|
258
|
-
input: Schema.InferInput<P["input"]>,
|
|
259
|
-
onProgress?: (progress: Schema.InferOutput<P["progress"]>) => void,
|
|
260
|
-
requestId?: string,
|
|
261
|
-
) => CancelablePromise<Schema.InferOutput<P["success"]>>;
|
|
262
|
-
/**
|
|
263
|
-
* Send the request to specific nodes, or all nodes.
|
|
264
|
-
* Returns an array of results, one for each node the request was sent to.
|
|
265
|
-
* Each result is a {@link PromiseSettledResult}, with also an additional property, the node ID of the request
|
|
266
|
-
*/
|
|
267
|
-
broadcast: (
|
|
268
|
-
input: Schema.InferInput<P["input"]>,
|
|
269
|
-
onProgress?: (
|
|
270
|
-
/** Map of node IDs to their progress updates */
|
|
271
|
-
progresses: Map<string, Schema.InferOutput<P["progress"]>>,
|
|
272
|
-
) => void,
|
|
273
|
-
/** Number of nodes to send the request to. Leave undefined to send to all nodes */
|
|
274
|
-
nodes?: number,
|
|
275
|
-
) => Promise<
|
|
276
|
-
Array<
|
|
277
|
-
PromiseSettledResult<Schema.InferOutput<P["success"]>> & { node: string }
|
|
278
|
-
>
|
|
279
|
-
>;
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Symbol used as the key for the procedures map on the server instance
|
|
284
|
-
* @internal
|
|
285
|
-
* @source
|
|
286
|
-
*/
|
|
287
|
-
export const zImplementations = Symbol("SWARPC implementations");
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Symbol used as the key for the procedures map on instances
|
|
291
|
-
* @internal
|
|
292
|
-
* @source
|
|
293
|
-
*/
|
|
294
|
-
export const zProcedures = Symbol("SWARPC procedures");
|
|
295
|
-
|
|
296
|
-
export type WorkerConstructor<
|
|
297
|
-
T extends Worker | SharedWorker = Worker | SharedWorker,
|
|
298
|
-
> = {
|
|
299
|
-
new (opts?: { name?: string }): T;
|
|
300
|
-
};
|
package/src/utils.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
type Constructor<T> = new (...args: any[]) => T;
|
|
2
|
-
|
|
3
|
-
// TODO: keep it in sync with web standards, how?
|
|
4
|
-
const transferableClasses: Constructor<Transferable>[] = [
|
|
5
|
-
MessagePort,
|
|
6
|
-
ReadableStream,
|
|
7
|
-
WritableStream,
|
|
8
|
-
TransformStream,
|
|
9
|
-
ArrayBuffer,
|
|
10
|
-
];
|
|
11
|
-
|
|
12
|
-
export function findTransferables(value: any): Transferable[] {
|
|
13
|
-
if (value === null || value === undefined) {
|
|
14
|
-
return [];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (typeof value === "object") {
|
|
18
|
-
if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
|
|
19
|
-
return [value];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (transferableClasses.some((cls) => value instanceof cls)) {
|
|
23
|
-
return [value as Transferable];
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (Array.isArray(value)) {
|
|
27
|
-
return value.flatMap(findTransferables);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return Object.values(value).flatMap(findTransferables);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return [];
|
|
34
|
-
}
|