@procedures/client 0.7.0 → 0.8.1
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/CHANGELOG.md +107 -0
- package/README.md +115 -11
- package/dist/.tsbuildinfo +1 -1
- package/dist/adapters/http/client/transport.d.ts +4 -0
- package/dist/adapters/http/client/transport.d.ts.map +1 -1
- package/dist/adapters/http/client/transport.js +11 -1
- package/dist/adapters/http/client/transport.js.map +1 -1
- package/dist/adapters/http/client/types.d.ts +19 -0
- package/dist/adapters/http/client/types.d.ts.map +1 -1
- package/dist/adapters/http/server/cors.d.ts +64 -0
- package/dist/adapters/http/server/cors.d.ts.map +1 -0
- package/dist/adapters/http/server/cors.js +113 -0
- package/dist/adapters/http/server/cors.js.map +1 -0
- package/dist/adapters/http/server/handler.d.ts +136 -0
- package/dist/adapters/http/server/handler.d.ts.map +1 -0
- package/dist/adapters/http/server/handler.js +495 -0
- package/dist/adapters/http/server/handler.js.map +1 -0
- package/dist/adapters/http/server/strategies.d.ts +14 -5
- package/dist/adapters/http/server/strategies.d.ts.map +1 -1
- package/dist/adapters/http/server/strategies.js.map +1 -1
- package/dist/adapters/http/server/transport.d.ts +36 -46
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +58 -433
- package/dist/adapters/http/server/transport.js.map +1 -1
- package/dist/adapters/http/server/types.d.ts +13 -0
- package/dist/adapters/http/server/types.d.ts.map +1 -1
- package/dist/adapters/websocket/client/transport.d.ts +7 -3
- package/dist/adapters/websocket/client/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/client/transport.js +9 -3
- package/dist/adapters/websocket/client/transport.js.map +1 -1
- package/dist/adapters/websocket/server/transport.d.ts +56 -1
- package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/server/transport.js +174 -17
- package/dist/adapters/websocket/server/transport.js.map +1 -1
- package/dist/adapters/websocket/server/types.d.ts +10 -3
- package/dist/adapters/websocket/server/types.d.ts.map +1 -1
- package/dist/adapters/websocket/server/upgrade-router.d.ts +28 -0
- package/dist/adapters/websocket/server/upgrade-router.d.ts.map +1 -0
- package/dist/adapters/websocket/server/upgrade-router.js +62 -0
- package/dist/adapters/websocket/server/upgrade-router.js.map +1 -0
- package/dist/adapters/websocket/server/upgrade.d.ts +68 -0
- package/dist/adapters/websocket/server/upgrade.d.ts.map +1 -0
- package/dist/adapters/websocket/server/upgrade.js +49 -0
- package/dist/adapters/websocket/server/upgrade.js.map +1 -0
- package/dist/adapters/websocket/shared/rpc.d.ts +2 -0
- package/dist/adapters/websocket/shared/rpc.d.ts.map +1 -1
- package/dist/adapters/websocket/shared/rpc.js +8 -2
- package/dist/adapters/websocket/shared/rpc.js.map +1 -1
- package/dist/browser.d.ts +14 -6
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +12 -3
- package/dist/browser.js.map +1 -1
- package/dist/client/client.d.ts +43 -0
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/client.js +69 -1
- package/dist/client/client.js.map +1 -1
- package/dist/client/errors/registry.d.ts.map +1 -1
- package/dist/client/errors/registry.js +1 -0
- package/dist/client/errors/registry.js.map +1 -1
- package/dist/client/middleware/retry.d.ts +4 -0
- package/dist/client/middleware/retry.d.ts.map +1 -1
- package/dist/client/middleware/retry.js +86 -78
- package/dist/client/middleware/retry.js.map +1 -1
- package/dist/events/bus.d.ts +32 -14
- package/dist/events/bus.d.ts.map +1 -1
- package/dist/events/bus.js +119 -65
- package/dist/events/bus.js.map +1 -1
- package/dist/events/index.d.ts +3 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js.map +1 -1
- package/dist/events/publisher.d.ts +14 -0
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +62 -10
- package/dist/events/publisher.js.map +1 -1
- package/dist/events/remote.d.ts +80 -18
- package/dist/events/remote.d.ts.map +1 -1
- package/dist/events/remote.js +448 -58
- package/dist/events/remote.js.map +1 -1
- package/dist/events/rows.d.ts +31 -0
- package/dist/events/rows.d.ts.map +1 -0
- package/dist/events/rows.js +39 -0
- package/dist/events/rows.js.map +1 -0
- package/dist/events/types.d.ts +13 -2
- package/dist/events/types.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +14 -0
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +9 -0
- package/dist/node.js.map +1 -1
- package/dist/procedures/caller.d.ts +33 -0
- package/dist/procedures/caller.d.ts.map +1 -0
- package/dist/procedures/caller.js +27 -0
- package/dist/procedures/caller.js.map +1 -0
- package/dist/procedures/capability.d.ts +6 -0
- package/dist/procedures/capability.d.ts.map +1 -1
- package/dist/procedures/capability.js +25 -0
- package/dist/procedures/capability.js.map +1 -1
- package/dist/procedures/collection/changes.d.ts +94 -0
- package/dist/procedures/collection/changes.d.ts.map +1 -0
- package/dist/procedures/collection/changes.js +182 -0
- package/dist/procedures/collection/changes.js.map +1 -0
- package/dist/procedures/collection/index.d.ts +8 -3
- package/dist/procedures/collection/index.d.ts.map +1 -1
- package/dist/procedures/collection/index.js +6 -1
- package/dist/procedures/collection/index.js.map +1 -1
- package/dist/procedures/collection/procedures.d.ts +39 -2
- package/dist/procedures/collection/procedures.d.ts.map +1 -1
- package/dist/procedures/collection/procedures.js +131 -8
- package/dist/procedures/collection/procedures.js.map +1 -1
- package/dist/procedures/collection/query.d.ts +246 -0
- package/dist/procedures/collection/query.d.ts.map +1 -0
- package/dist/procedures/collection/query.js +854 -0
- package/dist/procedures/collection/query.js.map +1 -0
- package/dist/procedures/collection/schemas.d.ts +59 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +172 -6
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +64 -6
- package/dist/procedures/collection/storage.d.ts.map +1 -1
- package/dist/procedures/collection/storage.js +23 -0
- package/dist/procedures/collection/storage.js.map +1 -1
- package/dist/procedures/elicit.d.ts +190 -0
- package/dist/procedures/elicit.d.ts.map +1 -0
- package/dist/procedures/elicit.js +127 -0
- package/dist/procedures/elicit.js.map +1 -0
- package/dist/procedures/hints.d.ts +43 -0
- package/dist/procedures/hints.d.ts.map +1 -0
- package/dist/procedures/hints.js +74 -0
- package/dist/procedures/hints.js.map +1 -0
- package/dist/procedures/index.d.ts +12 -3
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +7 -1
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/invoke.d.ts +20 -0
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +16 -0
- package/dist/procedures/invoke.js.map +1 -1
- package/dist/procedures/progress.d.ts +24 -0
- package/dist/procedures/progress.d.ts.map +1 -0
- package/dist/procedures/progress.js +11 -0
- package/dist/procedures/progress.js.map +1 -0
- package/dist/procedures/registry.d.ts.map +1 -1
- package/dist/procedures/registry.js +5 -0
- package/dist/procedures/registry.js.map +1 -1
- package/dist/procedures/types.d.ts +92 -0
- package/dist/procedures/types.d.ts.map +1 -1
- package/dist/procedures/types.js.map +1 -1
- package/dist/procedures/untrusted.d.ts +28 -0
- package/dist/procedures/untrusted.d.ts.map +1 -0
- package/dist/procedures/untrusted.js +32 -0
- package/dist/procedures/untrusted.js.map +1 -0
- package/dist/protocol.d.ts +1 -1
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +1 -0
- package/dist/protocol.js.map +1 -1
- package/dist/server/browser-guard.d.ts +12 -2
- package/dist/server/browser-guard.d.ts.map +1 -1
- package/dist/server/browser-guard.js +43 -6
- package/dist/server/browser-guard.js.map +1 -1
- package/dist/server/host-policy.d.ts +10 -1
- package/dist/server/host-policy.d.ts.map +1 -1
- package/dist/server/host-policy.js +5 -0
- package/dist/server/host-policy.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js.map +1 -1
- package/dist/server/procedure-server.d.ts +75 -9
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +151 -17
- package/dist/server/procedure-server.js.map +1 -1
- package/dist/server/request-auth.d.ts +51 -4
- package/dist/server/request-auth.d.ts.map +1 -1
- package/dist/server/request-auth.js +116 -5
- package/dist/server/request-auth.js.map +1 -1
- package/dist/server/types.d.ts +73 -5
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js.map +1 -1
- package/dist/timers.d.ts +5 -0
- package/dist/timers.d.ts.map +1 -1
- package/dist/timers.js +3 -1
- package/dist/timers.js.map +1 -1
- package/package.json +5 -1
- package/src/adapters/http/client/transport.ts +11 -1
- package/src/adapters/http/client/types.ts +22 -0
- package/src/adapters/http/server/cors.ts +144 -0
- package/src/adapters/http/server/handler.ts +624 -0
- package/src/adapters/http/server/strategies.ts +16 -6
- package/src/adapters/http/server/transport.ts +66 -479
- package/src/adapters/http/server/types.ts +15 -0
- package/src/adapters/websocket/client/transport.ts +9 -3
- package/src/adapters/websocket/server/transport.ts +189 -18
- package/src/adapters/websocket/server/types.ts +11 -3
- package/src/adapters/websocket/server/upgrade-router.ts +77 -0
- package/src/adapters/websocket/server/upgrade.ts +88 -0
- package/src/adapters/websocket/shared/rpc.ts +9 -2
- package/src/browser.ts +85 -2
- package/src/client/client.ts +83 -1
- package/src/client/errors/registry.ts +1 -0
- package/src/client/middleware/retry.ts +89 -78
- package/src/events/bus.ts +141 -73
- package/src/events/index.ts +3 -0
- package/src/events/publisher.ts +71 -10
- package/src/events/remote.ts +575 -73
- package/src/events/rows.ts +48 -0
- package/src/events/types.ts +15 -2
- package/src/index.ts +1 -1
- package/src/node.ts +21 -0
- package/src/procedures/caller.ts +45 -0
- package/src/procedures/capability.ts +21 -0
- package/src/procedures/collection/changes.ts +245 -0
- package/src/procedures/collection/index.ts +56 -1
- package/src/procedures/collection/procedures.ts +185 -7
- package/src/procedures/collection/query.ts +1009 -0
- package/src/procedures/collection/schemas.ts +233 -7
- package/src/procedures/collection/storage.ts +84 -6
- package/src/procedures/elicit.ts +320 -0
- package/src/procedures/hints.ts +91 -0
- package/src/procedures/index.ts +82 -2
- package/src/procedures/invoke.ts +31 -0
- package/src/procedures/progress.ts +25 -0
- package/src/procedures/registry.ts +4 -0
- package/src/procedures/types.ts +102 -0
- package/src/procedures/untrusted.ts +50 -0
- package/src/protocol.ts +3 -1
- package/src/server/browser-guard.ts +48 -6
- package/src/server/host-policy.ts +13 -1
- package/src/server/index.ts +3 -0
- package/src/server/procedure-server.ts +185 -20
- package/src/server/request-auth.ts +128 -6
- package/src/server/types.ts +82 -5
- package/src/timers.ts +8 -1
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rows of a published value (roadmap CLI-15).
|
|
3
|
+
*
|
|
4
|
+
* A value on the bus can hold rows of data: the values of a collection change event, for example.
|
|
5
|
+
* The publisher of the remote events gives each row to the output filter of the host (`admitItem`)
|
|
6
|
+
* for each subscriber, and each subscriber gets only the rows that its filter admits. Thus a row
|
|
7
|
+
* reaches a subscriber only through the same filter as a read.
|
|
8
|
+
*
|
|
9
|
+
* The rows are on a symbol key, so they stay in this process: JSON does not take them.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** The rows of a published value, and the function that makes the value of the admitted rows. */
|
|
13
|
+
export interface PublishedRows {
|
|
14
|
+
/** The rows that the output filter examines, one at a time. */
|
|
15
|
+
readonly rows: readonly unknown[];
|
|
16
|
+
/** This function gives the value for a subscriber: `admitted[i]` tells if row `i` stays. */
|
|
17
|
+
keep(admitted: readonly boolean[]): unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The key of the rows of a published value. */
|
|
21
|
+
const ROWS: unique symbol = Symbol.for("procedures.publishedRows");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* This function gives the value with its rows, for the filter of each subscriber. The rows are not
|
|
25
|
+
* enumerable: a copy or a comparison of the value does not see them.
|
|
26
|
+
*/
|
|
27
|
+
export function withRows<T extends object>(value: T, rows: PublishedRows): T {
|
|
28
|
+
return Object.defineProperty(value, ROWS, { value: rows, enumerable: false });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The rows of a published value, or undefined for a value without rows. */
|
|
32
|
+
export function rowsOf(value: unknown): PublishedRows | undefined {
|
|
33
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
34
|
+
const rows = (value as { [ROWS]?: unknown })[ROWS];
|
|
35
|
+
return typeof rows === "object" && rows !== null ? (rows as PublishedRows) : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The value of a published value for one subscriber: each row passes the filter, and the value
|
|
40
|
+
* keeps the admitted rows. An error of the filter refuses the row. Without rows or a filter, the
|
|
41
|
+
* value stays as it is.
|
|
42
|
+
*/
|
|
43
|
+
export async function admittedValue(value: unknown, admit: ((item: unknown) => Promise<boolean>) | undefined): Promise<unknown> {
|
|
44
|
+
const rows = rowsOf(value);
|
|
45
|
+
if (!rows || !admit) return value;
|
|
46
|
+
const admitted = await Promise.all(rows.rows.map((row) => admit(row).catch(() => false)));
|
|
47
|
+
return rows.keep(admitted);
|
|
48
|
+
}
|
package/src/events/types.ts
CHANGED
|
@@ -82,9 +82,10 @@ export interface EventBus {
|
|
|
82
82
|
*
|
|
83
83
|
* @param channel - Channel to subscribe to
|
|
84
84
|
* @param handler - Handler function called for each event
|
|
85
|
+
* @param options - `onClear`: the function that runs when `clear()` or `clearAll()` removes the handler
|
|
85
86
|
* @returns Unsubscribe function
|
|
86
87
|
*/
|
|
87
|
-
on<T>(channel: string, handler: EventHandler<T
|
|
88
|
+
on<T>(channel: string, handler: EventHandler<T>, options?: EventListenOptions): Unsubscribe;
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
91
|
* Subscribe to a single event on a channel.
|
|
@@ -177,7 +178,8 @@ export interface TypedEventBus<TChannels extends ChannelMap> {
|
|
|
177
178
|
emit<K extends keyof TChannels & string>(channel: K, data: TChannels[K]): void;
|
|
178
179
|
on<K extends keyof TChannels & string>(
|
|
179
180
|
channel: K,
|
|
180
|
-
handler: EventHandler<TChannels[K]
|
|
181
|
+
handler: EventHandler<TChannels[K]>,
|
|
182
|
+
options?: EventListenOptions
|
|
181
183
|
): Unsubscribe;
|
|
182
184
|
once<K extends keyof TChannels & string>(channel: K, options?: EventWaitOptions): Promise<TChannels[K]>;
|
|
183
185
|
stream<K extends keyof TChannels & string>(channel: K, options?: EventWaitOptions): AsyncIterable<TChannels[K]>;
|
|
@@ -238,6 +240,17 @@ export interface EventWaitOptions {
|
|
|
238
240
|
signal?: AbortSignal | undefined;
|
|
239
241
|
}
|
|
240
242
|
|
|
243
|
+
/**
|
|
244
|
+
* Options for a handler of `on()`.
|
|
245
|
+
*/
|
|
246
|
+
export interface EventListenOptions {
|
|
247
|
+
/**
|
|
248
|
+
* This function runs one time when `clear()` or `clearAll()` removes the handler. An unsubscribe
|
|
249
|
+
* does not run it. The publisher of the remote events uses it to end a subscription with notice.
|
|
250
|
+
*/
|
|
251
|
+
onClear?: (() => void) | undefined;
|
|
252
|
+
}
|
|
253
|
+
|
|
241
254
|
/**
|
|
242
255
|
* Options for creating an event bus.
|
|
243
256
|
*/
|
package/src/index.ts
CHANGED
|
@@ -27,7 +27,7 @@ export type {
|
|
|
27
27
|
HttpUrlStrategy,
|
|
28
28
|
} from "./server/index.js";
|
|
29
29
|
export { WebSocketServerTransport } from "./server/index.js";
|
|
30
|
-
export { checkBrowserRequest, isLoopbackHost, isLoopbackAddress, hostNameOf } from "./server/browser-guard.js";
|
|
30
|
+
export { checkBrowserRequest, hasAmbientCredentials, isLoopbackHost, isLoopbackAddress, hostNameOf } from "./server/browser-guard.js";
|
|
31
31
|
export type { BrowserGuardOptions, GuardedRequest, GuardResult } from "./server/browser-guard.js";
|
|
32
32
|
export type {
|
|
33
33
|
WebSocketServerTransportOptions,
|
package/src/node.ts
CHANGED
|
@@ -9,9 +9,30 @@
|
|
|
9
9
|
* This entry also exports `writeNdjsonStream`: the writer of an NDJSON stream response of wire
|
|
10
10
|
* protocol v1, for a `ServerResponse` of `node:http`. `HttpServerTransport` uses it, so a server
|
|
11
11
|
* without Express writes the same bytes.
|
|
12
|
+
*
|
|
13
|
+
* The procedure faces of Node, without a framework (CLI-3): `procedureHandler(server, options)`
|
|
14
|
+
* gives a request handler `(req, res) => boolean`, and `upgradeHandler(server, options)` gives an
|
|
15
|
+
* upgrade handler `(req, socket, head) => boolean`. Each one takes the requests of its path and
|
|
16
|
+
* gives `false` for the others, so the routes of the application share the listener.
|
|
12
17
|
*/
|
|
13
18
|
|
|
14
19
|
export { WebSocketTransport } from "./adapters/websocket/client/node.js";
|
|
15
20
|
export type { WebSocketTransportOptions, WebSocketClass } from "./adapters/websocket/client/types.js";
|
|
16
21
|
export { writeNdjsonStream } from "./adapters/http/server/ndjson.js";
|
|
17
22
|
export type { WriteNdjsonStreamOptions, NdjsonError } from "./adapters/http/server/ndjson.js";
|
|
23
|
+
export { procedureHandler } from "./adapters/http/server/handler.js";
|
|
24
|
+
export type { ProcedureHandlerOptions, NodeProcedureHandler, NodeUrlStrategy, UrlRequest } from "./adapters/http/server/handler.js";
|
|
25
|
+
export type { CorsOptions } from "./adapters/http/server/cors.js";
|
|
26
|
+
export type { RoutedRequest } from "./adapters/http/server/strategies.js";
|
|
27
|
+
export { defaultServerUrlStrategy, rpcServerUrlStrategy } from "./adapters/http/server/strategies.js";
|
|
28
|
+
export { upgradeHandler } from "./adapters/websocket/server/upgrade.js";
|
|
29
|
+
export type { UpgradeHandlerOptions, NodeUpgradeHandler } from "./adapters/websocket/server/upgrade.js";
|
|
30
|
+
export { WebSocketServerTransport } from "./adapters/websocket/server/transport.js";
|
|
31
|
+
export type {
|
|
32
|
+
WebSocketServerTransportOptions,
|
|
33
|
+
WebSocketAuthHandler,
|
|
34
|
+
WebSocketConnectionHandler,
|
|
35
|
+
WebSocketMessage,
|
|
36
|
+
TrackedConnection,
|
|
37
|
+
ConnectionEventHandler,
|
|
38
|
+
} from "./adapters/websocket/server/types.js";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A caller of the procedures of some modules (`callerOf`). An app that calls the procedures of a
|
|
3
|
+
* package in its own process repeats three steps: a registry of the module, `invokePath` and
|
|
4
|
+
* `outputValue`. A caller does the three steps in one call. jarvis had four copies of them.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ProcedurePath } from "./types.js";
|
|
8
|
+
import type { ProcedureRegistry } from "./registry.js";
|
|
9
|
+
import { invokePath, outputValue, type InvokeOptions } from "./invoke.js";
|
|
10
|
+
import { createRegistry, type ModuleInput } from "./module.js";
|
|
11
|
+
|
|
12
|
+
/** The options of one call of a caller: the options of `invokePath` without the registry. */
|
|
13
|
+
export type CallerOptions = Omit<InvokeOptions, "registry">;
|
|
14
|
+
|
|
15
|
+
/** A function that runs the procedures of its modules (`callerOf`). */
|
|
16
|
+
export interface Caller {
|
|
17
|
+
/**
|
|
18
|
+
* This function runs the procedure at `path` with `input`, and gives one value: the value of
|
|
19
|
+
* the procedure, or the last item of its stream. An empty stream rejects with `NO_OUTPUT`, and
|
|
20
|
+
* a path that the modules do not have rejects with `NOT_FOUND`.
|
|
21
|
+
*/
|
|
22
|
+
<T = unknown>(path: ProcedurePath, input: unknown, options?: CallerOptions): Promise<T>;
|
|
23
|
+
/** The registry of the caller: `clientModule` and the modules, with their required modules. The first use makes it. */
|
|
24
|
+
readonly registry: ProcedureRegistry;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* This function makes a caller of the procedures of the modules. The registry of the modules is
|
|
29
|
+
* made at the first call, so a caller costs nothing until it calls. The caller does not use the
|
|
30
|
+
* global registry.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const gh = callerOf(ghModule);
|
|
34
|
+
* const runs = await gh<RunList>(["gh", "run", "list"], { repo: "owner/name" });
|
|
35
|
+
*/
|
|
36
|
+
export function callerOf(...modules: ModuleInput[]): Caller {
|
|
37
|
+
let registry: ProcedureRegistry | undefined;
|
|
38
|
+
const registryOf = (): ProcedureRegistry => (registry ??= createRegistry(...modules));
|
|
39
|
+
const call = async <T = unknown>(path: ProcedurePath, input: unknown, options: CallerOptions = {}): Promise<T> => {
|
|
40
|
+
const output = await invokePath<T>(path, input, { ...options, registry: registryOf() });
|
|
41
|
+
return outputValue(output, path);
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(call, "registry", { get: registryOf, enumerable: true });
|
|
44
|
+
return call as Caller;
|
|
45
|
+
}
|
|
@@ -178,6 +178,27 @@ export function narrowCapability<C>(
|
|
|
178
178
|
return Object.freeze(result) as Capability<C>;
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
+
/**
|
|
182
|
+
* This function joins two requests for a narrower capability into one request that asks for
|
|
183
|
+
* both: the meet of the clearances, the intersection of the effects and the shorter time. Two
|
|
184
|
+
* different clearances need an order.
|
|
185
|
+
*/
|
|
186
|
+
export function joinNarrowing<C>(first: CapabilityRequest<C>, second: CapabilityRequest<C>, order?: ClearanceOrder<C>): CapabilityRequest<C> {
|
|
187
|
+
const result: CapabilityRequest<C> = {};
|
|
188
|
+
if (first.clearance === undefined || second.clearance === undefined || Object.is(first.clearance, second.clearance)) {
|
|
189
|
+
const clearance = first.clearance ?? second.clearance;
|
|
190
|
+
if (clearance !== undefined) result.clearance = clearance;
|
|
191
|
+
} else {
|
|
192
|
+
if (!order) throw new Error("Two different clearances need a clearance order: give clearanceOrder to the host (hostPolicy)");
|
|
193
|
+
result.clearance = order.meet(first.clearance, second.clearance);
|
|
194
|
+
}
|
|
195
|
+
const effects = first.effects === undefined ? second.effects : narrowEffects(first.effects, second.effects);
|
|
196
|
+
if (effects !== undefined) result.effects = effects;
|
|
197
|
+
const times = [first.timeoutMs, second.timeoutMs].filter((time): time is number => typeof time === "number" && Number.isFinite(time) && time >= 0);
|
|
198
|
+
if (times.length > 0) result.timeoutMs = Math.min(...times);
|
|
199
|
+
return result;
|
|
200
|
+
}
|
|
201
|
+
|
|
181
202
|
// =============================================================================
|
|
182
203
|
// Admission
|
|
183
204
|
// =============================================================================
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The change events of the collection procedures (roadmap CLI-15).
|
|
3
|
+
*
|
|
4
|
+
* Each write of a collection procedure publishes one `CollectionChangeEvent` on the bus of its
|
|
5
|
+
* context (`ctx.bus`), under the topic `collections.<name>`. A peer gets the events with
|
|
6
|
+
* `events.subscribe` (`documentation/EVENTS.md`). The delivery is at most once, so an event is a
|
|
7
|
+
* hint of a change, and a subscriber reads the changes when it misses an event.
|
|
8
|
+
*
|
|
9
|
+
* - `since` and `version` are versions of `getChangesSince()` of the storage. The events of one
|
|
10
|
+
* storage and one topic make a chain: the `since` of an event is the `version` of the event
|
|
11
|
+
* before it. A subscriber whose cursor is `since` applies the event without a read.
|
|
12
|
+
* - The changes of an event come from the change log of the storage, not from the input of the
|
|
13
|
+
* write. Thus an event also has the writes that did not go through the procedures.
|
|
14
|
+
* - By default, an event has only the ids and the versions: a value reaches a caller only through
|
|
15
|
+
* a read, with its checks. With the option `values`, an event has the values too, and each row
|
|
16
|
+
* `{ id, value }` passes the output filter of the host for each subscriber (`withRows()`).
|
|
17
|
+
* - A subscriber of the topic must have a capability that reads `collection:<name>`, as for a
|
|
18
|
+
* read (`collectionTopicReads()`, the default `reads` of `EventPublisher`).
|
|
19
|
+
* - An event without `since` only tells that the collection changed: the storage has no change
|
|
20
|
+
* log, the chain starts, or the log dropped the version. The subscriber then reads the changes.
|
|
21
|
+
* - The chain starts at the version of a call of `changes` (a pull of a subscriber). While no one
|
|
22
|
+
* listens to the topic, the procedures publish nothing, and the chain stops.
|
|
23
|
+
* - The publishes of one storage and one topic run one at a time, in the order of the writes. A
|
|
24
|
+
* write resolves after its event is on the bus. A failed publish never fails the write.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { EventBus } from "../../events/types.js";
|
|
28
|
+
import { withRows } from "../../events/rows.js";
|
|
29
|
+
import type { StorageChanges } from "./storage.js";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The change event of the writes of a collection, on the topic `collections.<name>` (roadmap
|
|
33
|
+
* CLI-15). The data crosses the wire as JSON.
|
|
34
|
+
*/
|
|
35
|
+
export interface CollectionChangeEvent {
|
|
36
|
+
/**
|
|
37
|
+
* The version of the storage before the changes. A subscriber whose cursor is this version
|
|
38
|
+
* applies the changes without a read. Without it, the event is only a hint: read the changes.
|
|
39
|
+
*/
|
|
40
|
+
readonly since?: string | undefined;
|
|
41
|
+
/** The version of the storage after the changes, when the storage has a change log. */
|
|
42
|
+
readonly version?: string | undefined;
|
|
43
|
+
/** The ids of the items that changed, in the order of the last change of each id. */
|
|
44
|
+
readonly changed: readonly string[];
|
|
45
|
+
/** The ids that were deleted. */
|
|
46
|
+
readonly deleted: readonly string[];
|
|
47
|
+
/**
|
|
48
|
+
* With the option `values`: the changed items that the output filter of the subscriber admits.
|
|
49
|
+
* Without the option, the event has no values: read them with `changes`.
|
|
50
|
+
*/
|
|
51
|
+
readonly values?: ReadonlyArray<{ readonly id: string; readonly value: unknown }> | undefined;
|
|
52
|
+
/** With the option `values`: the changed ids whose values the filter refused. Read them with `changes`. */
|
|
53
|
+
readonly withheld?: readonly string[] | undefined;
|
|
54
|
+
/** The origin of the writes, when one origin made all the changes of the event. */
|
|
55
|
+
readonly origin?: string | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function gives the topic of the change events of a collection: `collections.<name>`. The
|
|
60
|
+
* partition of roadmap CLI-17 comes after the name, with the option `topic` of the procedures.
|
|
61
|
+
*/
|
|
62
|
+
export function collectionChangeTopic(collection: string): string {
|
|
63
|
+
return `collections.${collection}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* This function gives the resources that a subscriber of a topic reads: `collection:<name>` for
|
|
68
|
+
* the topic `collections.<name>` and for a topic of a partition, `collections.<name>.<partition>`.
|
|
69
|
+
* Another topic gives no resource. `EventPublisher` uses it as its default `reads`.
|
|
70
|
+
*/
|
|
71
|
+
export function collectionTopicReads(topic: string): readonly string[] {
|
|
72
|
+
const match = /^collections\.([^.]+)(?:\.|$)/.exec(topic);
|
|
73
|
+
return match ? [`collection:${match[1]}`] : [];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The part of a storage that the change events read. */
|
|
77
|
+
interface ChangeLog {
|
|
78
|
+
getChangesSince?(since?: string): Promise<StorageChanges<unknown>>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** A write that ended, and that no event told yet. */
|
|
82
|
+
interface EndedWrite {
|
|
83
|
+
/** The ids of the write, or "*" for a clear. */
|
|
84
|
+
readonly ids: readonly string[] | "*";
|
|
85
|
+
readonly origin: string | undefined;
|
|
86
|
+
/** True when the chain had a version when the write started. */
|
|
87
|
+
readonly anchored: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The chain of the events of one storage and one topic. */
|
|
91
|
+
interface ChangeChain {
|
|
92
|
+
/** The `version` of the last event, or of a call of `changes`: the `since` of the next event. */
|
|
93
|
+
anchor: string | undefined;
|
|
94
|
+
/** The publishes, one at a time. */
|
|
95
|
+
tail: Promise<void>;
|
|
96
|
+
/** The writes that ended since the last publish. */
|
|
97
|
+
readonly ended: EndedWrite[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The chains of each storage, by topic. A storage that goes away takes its chains with it. */
|
|
101
|
+
const CHAINS = new WeakMap<object, Map<string, ChangeChain>>();
|
|
102
|
+
|
|
103
|
+
function chainOf(storage: object, topic: string): ChangeChain {
|
|
104
|
+
let topics = CHAINS.get(storage);
|
|
105
|
+
if (!topics) {
|
|
106
|
+
topics = new Map();
|
|
107
|
+
CHAINS.set(storage, topics);
|
|
108
|
+
}
|
|
109
|
+
let chain = topics.get(topic);
|
|
110
|
+
if (!chain) {
|
|
111
|
+
chain = { anchor: undefined, tail: Promise.resolve(), ended: [] };
|
|
112
|
+
topics.set(topic, chain);
|
|
113
|
+
}
|
|
114
|
+
return chain;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The origin of an event: the one origin of its writes, when the changes are only of these writes. */
|
|
118
|
+
function originOf(writes: readonly EndedWrite[], changes: StorageChanges<unknown> | undefined): string | undefined {
|
|
119
|
+
const origin = writes[0]?.origin;
|
|
120
|
+
if (origin === undefined || writes.some((write) => write.origin !== origin)) return undefined;
|
|
121
|
+
if (!changes || writes.some((write) => write.ids === "*")) return origin;
|
|
122
|
+
const ids = new Set<string>();
|
|
123
|
+
for (const write of writes) for (const id of write.ids as readonly string[]) ids.add(id);
|
|
124
|
+
const own = changes.changed.every(([id]) => ids.has(id)) && changes.deleted.every((id) => ids.has(id));
|
|
125
|
+
return own ? origin : undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A hint: the collection changed, and the subscriber reads the changes. */
|
|
129
|
+
function hint(version: string | undefined, origin: string | undefined): CollectionChangeEvent {
|
|
130
|
+
return {
|
|
131
|
+
...(version !== undefined && { version }),
|
|
132
|
+
changed: [],
|
|
133
|
+
deleted: [],
|
|
134
|
+
...(origin !== undefined && { origin }),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The event of a delta with the values of the changed items. Each row `{ id, value }` passes the
|
|
140
|
+
* output filter of each subscriber, and a refused row goes from `values` to `withheld`.
|
|
141
|
+
*/
|
|
142
|
+
function withValues(event: CollectionChangeEvent, entries: ReadonlyArray<{ readonly id: string; readonly value: unknown }>): CollectionChangeEvent {
|
|
143
|
+
return withRows(
|
|
144
|
+
{ ...event, values: entries },
|
|
145
|
+
{
|
|
146
|
+
rows: entries,
|
|
147
|
+
keep: (admitted) => {
|
|
148
|
+
const withheld = entries.filter((_, index) => !admitted[index]).map(({ id }) => id);
|
|
149
|
+
return {
|
|
150
|
+
...event,
|
|
151
|
+
values: entries.filter((_, index) => admitted[index]),
|
|
152
|
+
...(withheld.length > 0 && { withheld }),
|
|
153
|
+
};
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** One publish: the event of the writes that ended since the last publish. */
|
|
160
|
+
async function publish(chain: ChangeChain, storage: ChangeLog, bus: EventBus, topic: string, values: boolean): Promise<void> {
|
|
161
|
+
const writes = chain.ended.splice(0);
|
|
162
|
+
// An earlier publish told these writes
|
|
163
|
+
if (writes.length === 0) return;
|
|
164
|
+
if (!bus.hasSubscribers(topic)) {
|
|
165
|
+
// No one listens: the procedures do not read the log. The next call of `changes` starts the chain again.
|
|
166
|
+
chain.anchor = undefined;
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const since = chain.anchor;
|
|
170
|
+
if (!storage.getChangesSince || since === undefined) {
|
|
171
|
+
bus.emit(topic, hint(undefined, originOf(writes, undefined)));
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
let changes: StorageChanges<unknown>;
|
|
175
|
+
try {
|
|
176
|
+
changes = await storage.getChangesSince(since);
|
|
177
|
+
} catch {
|
|
178
|
+
// The subscribers read the changes themselves. The chain keeps its version.
|
|
179
|
+
bus.emit(topic, hint(undefined, originOf(writes, undefined)));
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
chain.anchor = changes.version;
|
|
183
|
+
// A full result: the log does not know the version of the chain any more
|
|
184
|
+
if (changes.full || writes.some((write) => !write.anchored)) {
|
|
185
|
+
bus.emit(topic, hint(changes.version, originOf(writes, undefined)));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (changes.changed.length === 0 && changes.deleted.length === 0) return;
|
|
189
|
+
const origin = originOf(writes, changes);
|
|
190
|
+
const event: CollectionChangeEvent = {
|
|
191
|
+
since,
|
|
192
|
+
version: changes.version,
|
|
193
|
+
changed: changes.changed.map(([id]) => id),
|
|
194
|
+
deleted: changes.deleted,
|
|
195
|
+
...(origin !== undefined && { origin }),
|
|
196
|
+
};
|
|
197
|
+
bus.emit(topic, values ? withValues(event, changes.changed.map(([id, value]) => ({ id, value }))) : event);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* This function runs a write of a collection procedure, and then publishes the change event of
|
|
202
|
+
* the write on the bus. Without a bus or a topic, it only runs the write. A failed write
|
|
203
|
+
* publishes too, because a batch can change a part of the storage. The error of the publish
|
|
204
|
+
* never reaches the caller.
|
|
205
|
+
*
|
|
206
|
+
* @param bus - The bus of the context, or undefined
|
|
207
|
+
* @param storage - The storage of the collection: the chain of the events belongs to it
|
|
208
|
+
* @param topic - The topic of the events, or undefined for no event
|
|
209
|
+
* @param ids - The ids of the write, or "*" for a clear
|
|
210
|
+
* @param origin - The origin of the write
|
|
211
|
+
* @param write - The write
|
|
212
|
+
* @param values - True: the events carry the values, for the output filter of each subscriber
|
|
213
|
+
*/
|
|
214
|
+
export async function writeWithChangeEvent<R>(
|
|
215
|
+
bus: EventBus | undefined,
|
|
216
|
+
storage: object & ChangeLog,
|
|
217
|
+
topic: string | undefined,
|
|
218
|
+
ids: readonly string[] | "*",
|
|
219
|
+
origin: string | undefined,
|
|
220
|
+
write: () => Promise<R>,
|
|
221
|
+
values = false,
|
|
222
|
+
): Promise<R> {
|
|
223
|
+
if (bus === undefined || topic === undefined) return write();
|
|
224
|
+
const chain = chainOf(storage, topic);
|
|
225
|
+
const started: EndedWrite = { ids, origin, anchored: chain.anchor !== undefined };
|
|
226
|
+
try {
|
|
227
|
+
return await write();
|
|
228
|
+
} finally {
|
|
229
|
+
chain.ended.push(started);
|
|
230
|
+
const run = chain.tail.then(() => publish(chain, storage, bus, topic, values));
|
|
231
|
+
chain.tail = run.catch(() => undefined);
|
|
232
|
+
await chain.tail;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* This function starts the chain of the events of a storage at the version of a call of
|
|
238
|
+
* `changes`, when the chain has no version. The subscriber that made the call then applies the
|
|
239
|
+
* next event without a read.
|
|
240
|
+
*/
|
|
241
|
+
export function startChangeChain(bus: EventBus | undefined, storage: object, topic: string | undefined, version: string): void {
|
|
242
|
+
if (bus === undefined || topic === undefined) return;
|
|
243
|
+
const chain = chainOf(storage, topic);
|
|
244
|
+
if (chain.anchor === undefined) chain.anchor = version;
|
|
245
|
+
}
|
|
@@ -21,6 +21,12 @@ export {
|
|
|
21
21
|
getAllOutputSchema,
|
|
22
22
|
entriesInputSchema,
|
|
23
23
|
entriesOutputSchema,
|
|
24
|
+
findInputSchema,
|
|
25
|
+
findOutputSchema,
|
|
26
|
+
patchInputSchema,
|
|
27
|
+
patchOutputSchema,
|
|
28
|
+
idsInputSchema,
|
|
29
|
+
idsOutputSchema,
|
|
24
30
|
changesInputSchema,
|
|
25
31
|
changesOutputSchema,
|
|
26
32
|
sizeInputSchema,
|
|
@@ -44,6 +50,9 @@ export type {
|
|
|
44
50
|
GetAllInput,
|
|
45
51
|
EntriesInput,
|
|
46
52
|
CollectionEntry,
|
|
53
|
+
FindInput,
|
|
54
|
+
PatchInput,
|
|
55
|
+
IdsInput,
|
|
47
56
|
ChangesInput,
|
|
48
57
|
ChangesOutput,
|
|
49
58
|
SizeInput,
|
|
@@ -55,6 +64,52 @@ export type {
|
|
|
55
64
|
|
|
56
65
|
// Procedures
|
|
57
66
|
export { createCollectionProcedures } from "./procedures.js";
|
|
67
|
+
export type { CollectionProceduresOptions, CollectionChangeEventsOptions } from "./procedures.js";
|
|
68
|
+
|
|
69
|
+
// The change events of the writes (roadmap CLI-15)
|
|
70
|
+
export { collectionChangeTopic, collectionTopicReads } from "./changes.js";
|
|
71
|
+
export type { CollectionChangeEvent } from "./changes.js";
|
|
58
72
|
|
|
59
73
|
// The storage interface of the core (the shape of client-collections' CollectionStorage)
|
|
60
|
-
export type {
|
|
74
|
+
export type {
|
|
75
|
+
CollectionStorage,
|
|
76
|
+
StorageChanges,
|
|
77
|
+
StorageMetadata,
|
|
78
|
+
StorageWriteOptions,
|
|
79
|
+
StorageSetChange,
|
|
80
|
+
StorageDeleteChange,
|
|
81
|
+
StorageClearChange,
|
|
82
|
+
StorageChangeEvent,
|
|
83
|
+
StorageChangeBatch,
|
|
84
|
+
StorageObserver,
|
|
85
|
+
} from "./storage.js";
|
|
86
|
+
|
|
87
|
+
// The query language of the storages: the same JSON on the wire and in query() (roadmap CLI-34:
|
|
88
|
+
// the one copy of the language)
|
|
89
|
+
export {
|
|
90
|
+
DEFAULT_FIND_LIMIT,
|
|
91
|
+
DEFAULT_QUERY_OPERATORS,
|
|
92
|
+
DEFAULT_QUERY_POLICY,
|
|
93
|
+
QUERY_OPERATORS,
|
|
94
|
+
applyPatch,
|
|
95
|
+
compareStorageValues,
|
|
96
|
+
compileSelector,
|
|
97
|
+
isPatchOperations,
|
|
98
|
+
matchesSelector,
|
|
99
|
+
mergePatch,
|
|
100
|
+
patchProblems,
|
|
101
|
+
queryEntries,
|
|
102
|
+
queryOptionsProblems,
|
|
103
|
+
selectorProblems,
|
|
104
|
+
} from "./query.js";
|
|
105
|
+
export type {
|
|
106
|
+
StorageFieldOperators,
|
|
107
|
+
StoragePatch,
|
|
108
|
+
StoragePatchOperations,
|
|
109
|
+
StorageQueryOperator,
|
|
110
|
+
StorageQueryOptions,
|
|
111
|
+
StorageQueryPolicy,
|
|
112
|
+
StorageSelector,
|
|
113
|
+
StorageSort,
|
|
114
|
+
StorageSortDirection,
|
|
115
|
+
} from "./query.js";
|