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/nodes.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { PendingRequest } from "./client.js";
|
|
2
|
-
import { scopeIsDedicated, scopeIsShared } from "./scopes.js";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Returns to which node to send the next request, given the state of the currently pending requests
|
|
6
|
-
*/
|
|
7
|
-
export function whoToSendTo(
|
|
8
|
-
nodes: undefined | Record<string, unknown>,
|
|
9
|
-
requests: Map<string, PendingRequest>,
|
|
10
|
-
): undefined | string {
|
|
11
|
-
if (!nodes) return undefined;
|
|
12
|
-
|
|
13
|
-
let chosen = Object.keys(nodes)[0];
|
|
14
|
-
|
|
15
|
-
const requestsPerNode = Map.groupBy(requests.values(), (req) => req.nodeId);
|
|
16
|
-
|
|
17
|
-
for (const node of Object.keys(nodes)) {
|
|
18
|
-
if (!requestsPerNode.has(node)) requestsPerNode.set(node, []);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
for (const [node, reqs] of requestsPerNode.entries()) {
|
|
22
|
-
if (!node) continue;
|
|
23
|
-
|
|
24
|
-
// Send to the least busy node
|
|
25
|
-
if (reqs.length < requestsPerNode.get(chosen)!.length) chosen = node;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
console.debug(
|
|
29
|
-
"[SWARPC Load balancer] Choosing",
|
|
30
|
-
chosen,
|
|
31
|
-
"load map is",
|
|
32
|
-
requestsPerNode,
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
return chosen;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function nodeIdFromScope(
|
|
39
|
-
scope: WorkerGlobalScope,
|
|
40
|
-
_scopeType?: "dedicated" | "shared" | "service",
|
|
41
|
-
): string {
|
|
42
|
-
if (scopeIsDedicated(scope, _scopeType) || scopeIsShared(scope, _scopeType)) {
|
|
43
|
-
return scope.name;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return "(SW)";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Generate a random request ID, used to identify nodes in the client
|
|
51
|
-
* @source
|
|
52
|
-
*/
|
|
53
|
-
export function makeNodeId(): string {
|
|
54
|
-
return "N" + Math.random().toString(16).substring(2, 5).toUpperCase();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const serviceWorkerNodeId = "(SW)" as const; // Fixed ID for the service worker, as there's only one
|
|
58
|
-
|
|
59
|
-
export function nodeIdOrSW(
|
|
60
|
-
id: string | undefined,
|
|
61
|
-
): string | typeof serviceWorkerNodeId {
|
|
62
|
-
return id ?? serviceWorkerNodeId;
|
|
63
|
-
}
|
package/src/polyfills.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Groups elements from an iterable into a Map based on a callback function.
|
|
3
|
-
*
|
|
4
|
-
* @template K, T
|
|
5
|
-
* @param {Iterable<T>} iterable - The iterable to group.
|
|
6
|
-
* @param {function(T, number): K} callbackfn - The callback function to
|
|
7
|
-
* determine the grouping key.
|
|
8
|
-
* @returns {Map<K, T[]>} A Map where keys are the grouping keys and values are
|
|
9
|
-
* arrays of grouped elements.
|
|
10
|
-
*/
|
|
11
|
-
Map.groupBy ??= function groupBy(iterable, callbackfn) {
|
|
12
|
-
const map = new Map();
|
|
13
|
-
let i = 0;
|
|
14
|
-
for (const value of iterable) {
|
|
15
|
-
const key = callbackfn(value, i++),
|
|
16
|
-
list = map.get(key);
|
|
17
|
-
list ? list.push(value) : map.set(key, [value]);
|
|
18
|
-
}
|
|
19
|
-
return map;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export {};
|
package/src/scopes.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
class MockedWorkerGlobalScope {
|
|
2
|
-
constructor() {}
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
const SharedWorkerGlobalScope =
|
|
6
|
-
globalThis.SharedWorkerGlobalScope ?? MockedWorkerGlobalScope;
|
|
7
|
-
|
|
8
|
-
const DedicatedWorkerGlobalScope =
|
|
9
|
-
globalThis.DedicatedWorkerGlobalScope ?? MockedWorkerGlobalScope;
|
|
10
|
-
|
|
11
|
-
const ServiceWorkerGlobalScope =
|
|
12
|
-
globalThis.ServiceWorkerGlobalScope ?? MockedWorkerGlobalScope;
|
|
13
|
-
|
|
14
|
-
export function scopeIsShared(
|
|
15
|
-
scope: WorkerGlobalScope,
|
|
16
|
-
_scopeType?: "dedicated" | "shared" | "service",
|
|
17
|
-
): scope is SharedWorkerGlobalScope {
|
|
18
|
-
return scope instanceof SharedWorkerGlobalScope || _scopeType === "shared";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function scopeIsDedicated(
|
|
22
|
-
scope: WorkerGlobalScope,
|
|
23
|
-
_scopeType?: "dedicated" | "shared" | "service",
|
|
24
|
-
): scope is DedicatedWorkerGlobalScope {
|
|
25
|
-
return (
|
|
26
|
-
scope instanceof DedicatedWorkerGlobalScope || _scopeType === "dedicated"
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function scopeIsService(
|
|
31
|
-
scope: WorkerGlobalScope,
|
|
32
|
-
_scopeType?: "dedicated" | "shared" | "service",
|
|
33
|
-
): scope is ServiceWorkerGlobalScope {
|
|
34
|
-
return scope instanceof ServiceWorkerGlobalScope || _scopeType === "service";
|
|
35
|
-
}
|
package/src/server.ts
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
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
|
-
ProcedureImplementation,
|
|
16
|
-
validatePayloadCore as validatePayloadCore,
|
|
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 = validatePayloadCore(schemas, event.data);
|
|
192
|
-
|
|
193
|
-
if ("isInitializeRequest" in payload)
|
|
194
|
-
throw "Unreachable: #initialize request payload should've been handled already";
|
|
195
|
-
|
|
196
|
-
// Handle abortion requests (pro-choice ftw!!)
|
|
197
|
-
if ("abort" in payload) {
|
|
198
|
-
const controller = abortControllers.get(requestId);
|
|
199
|
-
|
|
200
|
-
if (!controller)
|
|
201
|
-
await postError("No abort controller found for request");
|
|
202
|
-
|
|
203
|
-
controller?.abort(payload.abort.reason);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// Set up the abort controller for this request
|
|
208
|
-
abortControllers.set(requestId, new AbortController());
|
|
209
|
-
|
|
210
|
-
if (!("input" in payload)) {
|
|
211
|
-
await postError("No input provided");
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
try {
|
|
216
|
-
// Call the implementation with the input and a progress callback
|
|
217
|
-
const result = await implementation(
|
|
218
|
-
payload.input,
|
|
219
|
-
async (progress: any) => {
|
|
220
|
-
// l.debug(requestId, `Progress for ${functionName}`, progress);
|
|
221
|
-
await postMsg({ progress });
|
|
222
|
-
},
|
|
223
|
-
{
|
|
224
|
-
nodeId,
|
|
225
|
-
abortSignal: abortControllers.get(requestId)?.signal,
|
|
226
|
-
logger: createLogger("server", loglevel, nodeId, requestId),
|
|
227
|
-
},
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
// Send results
|
|
231
|
-
l.debug(requestId, `Result for ${functionName}`, result);
|
|
232
|
-
await postMsg({ result });
|
|
233
|
-
} catch (error: any) {
|
|
234
|
-
// Send errors
|
|
235
|
-
// Handle errors caused by abortions
|
|
236
|
-
if ("aborted" in error) {
|
|
237
|
-
l.debug(
|
|
238
|
-
requestId,
|
|
239
|
-
`Received abort error for ${functionName}`,
|
|
240
|
-
error.aborted,
|
|
241
|
-
);
|
|
242
|
-
abortedRequests.add(requestId);
|
|
243
|
-
abortControllers.delete(requestId);
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
l.info(requestId, `Error in ${functionName}`, error);
|
|
248
|
-
await postError(error);
|
|
249
|
-
} finally {
|
|
250
|
-
abortedRequests.delete(requestId);
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
// Listen for messages from the client
|
|
255
|
-
if (scopeIsShared(scope, _scopeType)) {
|
|
256
|
-
if (!port) throw new Error("SharedWorker port not initialized");
|
|
257
|
-
l.info(null, "Listening for shared worker messages on port", port);
|
|
258
|
-
port.addEventListener("message", listener);
|
|
259
|
-
port.start();
|
|
260
|
-
} else if (scopeIsDedicated(scope, _scopeType)) {
|
|
261
|
-
scope.addEventListener("message", listener);
|
|
262
|
-
} else if (scopeIsService(scope, _scopeType)) {
|
|
263
|
-
scope.addEventListener("message", listener);
|
|
264
|
-
} else {
|
|
265
|
-
throw new Error(`Unsupported worker scope ${scope}`);
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
return instance;
|
|
270
|
-
}
|
package/src/standardschema.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/** The Standard Schema interface. */
|
|
2
|
-
export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
-
/** The Standard Schema properties. */
|
|
4
|
-
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export declare namespace StandardSchemaV1 {
|
|
8
|
-
/** The Standard Schema properties interface. */
|
|
9
|
-
export interface Props<Input = unknown, Output = Input> {
|
|
10
|
-
/** The version number of the standard. */
|
|
11
|
-
readonly version: 1;
|
|
12
|
-
/** The vendor name of the schema library. */
|
|
13
|
-
readonly vendor: string;
|
|
14
|
-
/** Validates unknown input values. */
|
|
15
|
-
readonly validate: (
|
|
16
|
-
value: unknown,
|
|
17
|
-
) => Result<Output> | Promise<Result<Output>>;
|
|
18
|
-
/** Inferred types associated with the schema. */
|
|
19
|
-
readonly types?: Types<Input, Output> | undefined;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** The result interface of the validate function. */
|
|
23
|
-
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
24
|
-
|
|
25
|
-
/** The result interface if validation succeeds. */
|
|
26
|
-
export interface SuccessResult<Output> {
|
|
27
|
-
/** The typed output value. */
|
|
28
|
-
readonly value: Output;
|
|
29
|
-
/** The non-existent issues. */
|
|
30
|
-
readonly issues?: undefined;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** The result interface if validation fails. */
|
|
34
|
-
export interface FailureResult {
|
|
35
|
-
/** The issues of failed validation. */
|
|
36
|
-
readonly issues: ReadonlyArray<Issue>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** The issue interface of the failure output. */
|
|
40
|
-
export interface Issue {
|
|
41
|
-
/** The error message of the issue. */
|
|
42
|
-
readonly message: string;
|
|
43
|
-
/** The path of the issue, if any. */
|
|
44
|
-
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** The path segment interface of the issue. */
|
|
48
|
-
export interface PathSegment {
|
|
49
|
-
/** The key representing a path segment. */
|
|
50
|
-
readonly key: PropertyKey;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** The Standard Schema types interface. */
|
|
54
|
-
export interface Types<Input = unknown, Output = Input> {
|
|
55
|
-
/** The input type of the schema. */
|
|
56
|
-
readonly input: Input;
|
|
57
|
-
/** The output type of the schema. */
|
|
58
|
-
readonly output: Output;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Infers the input type of a Standard Schema. */
|
|
62
|
-
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<
|
|
63
|
-
Schema["~standard"]["types"]
|
|
64
|
-
>["input"];
|
|
65
|
-
|
|
66
|
-
/** Infers the output type of a Standard Schema. */
|
|
67
|
-
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<
|
|
68
|
-
Schema["~standard"]["types"]
|
|
69
|
-
>["output"];
|
|
70
|
-
}
|