@procedures/client 0.8.0 → 0.9.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/CHANGELOG.md +78 -0
- package/README.md +95 -8
- 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 +30 -46
- package/dist/adapters/http/server/transport.d.ts.map +1 -1
- package/dist/adapters/http/server/transport.js +48 -432
- 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.map +1 -1
- package/dist/adapters/websocket/client/transport.js +4 -12
- package/dist/adapters/websocket/client/transport.js.map +1 -1
- package/dist/adapters/websocket/server/transport.d.ts +46 -1
- package/dist/adapters/websocket/server/transport.d.ts.map +1 -1
- package/dist/adapters/websocket/server/transport.js +152 -15
- 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 +12 -4
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +13 -2
- 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/events/publisher.d.ts +25 -1
- package/dist/events/publisher.d.ts.map +1 -1
- package/dist/events/publisher.js +66 -9
- package/dist/events/publisher.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/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/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 +10 -3
- package/dist/procedures/collection/index.d.ts.map +1 -1
- package/dist/procedures/collection/index.js +8 -1
- package/dist/procedures/collection/index.js.map +1 -1
- package/dist/procedures/collection/partition.d.ts +67 -0
- package/dist/procedures/collection/partition.d.ts.map +1 -0
- package/dist/procedures/collection/partition.js +82 -0
- package/dist/procedures/collection/partition.js.map +1 -0
- 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 +147 -44
- 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 +50 -0
- package/dist/procedures/collection/schemas.d.ts.map +1 -1
- package/dist/procedures/collection/schemas.js +144 -6
- package/dist/procedures/collection/schemas.js.map +1 -1
- package/dist/procedures/collection/storage.d.ts +58 -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 +9 -2
- package/dist/procedures/index.d.ts.map +1 -1
- package/dist/procedures/index.js +5 -1
- package/dist/procedures/index.js.map +1 -1
- package/dist/procedures/invoke.d.ts +24 -2
- package/dist/procedures/invoke.d.ts.map +1 -1
- package/dist/procedures/invoke.js +14 -1
- 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 +55 -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/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 +68 -7
- package/dist/server/procedure-server.d.ts.map +1 -1
- package/dist/server/procedure-server.js +124 -24
- 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/package.json +7 -3
- 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 +55 -478
- package/src/adapters/http/server/types.ts +15 -0
- package/src/adapters/websocket/client/transport.ts +4 -12
- package/src/adapters/websocket/server/transport.ts +166 -16
- 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/events/publisher.ts +75 -11
- package/src/events/rows.ts +48 -0
- package/src/index.ts +1 -1
- package/src/node.ts +21 -0
- package/src/procedures/capability.ts +21 -0
- package/src/procedures/collection/changes.ts +245 -0
- package/src/procedures/collection/index.ts +57 -1
- package/src/procedures/collection/partition.ts +123 -0
- package/src/procedures/collection/procedures.ts +206 -51
- package/src/procedures/collection/query.ts +1009 -0
- package/src/procedures/collection/schemas.ts +196 -7
- package/src/procedures/collection/storage.ts +78 -6
- package/src/procedures/elicit.ts +320 -0
- package/src/procedures/hints.ts +91 -0
- package/src/procedures/index.ts +83 -2
- package/src/procedures/invoke.ts +33 -2
- package/src/procedures/progress.ts +25 -0
- package/src/procedures/registry.ts +4 -0
- package/src/procedures/types.ts +65 -0
- package/src/procedures/untrusted.ts +50 -0
- 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 +147 -26
- package/src/server/request-auth.ts +128 -6
- package/src/server/types.ts +82 -5
|
@@ -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>`, or
|
|
60
|
+
* `collections.<name>.<partition>` for a partition (roadmap CLI-17).
|
|
61
|
+
*/
|
|
62
|
+
export function collectionChangeTopic(collection: string, partition?: string): string {
|
|
63
|
+
return partition === undefined ? `collections.${collection}` : `collections.${collection}.${partition}`;
|
|
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,10 @@ export {
|
|
|
21
21
|
getAllOutputSchema,
|
|
22
22
|
entriesInputSchema,
|
|
23
23
|
entriesOutputSchema,
|
|
24
|
+
findInputSchema,
|
|
25
|
+
findOutputSchema,
|
|
26
|
+
patchInputSchema,
|
|
27
|
+
patchOutputSchema,
|
|
24
28
|
idsInputSchema,
|
|
25
29
|
idsOutputSchema,
|
|
26
30
|
changesInputSchema,
|
|
@@ -46,6 +50,8 @@ export type {
|
|
|
46
50
|
GetAllInput,
|
|
47
51
|
EntriesInput,
|
|
48
52
|
CollectionEntry,
|
|
53
|
+
FindInput,
|
|
54
|
+
PatchInput,
|
|
49
55
|
IdsInput,
|
|
50
56
|
ChangesInput,
|
|
51
57
|
ChangesOutput,
|
|
@@ -58,6 +64,56 @@ export type {
|
|
|
58
64
|
|
|
59
65
|
// Procedures
|
|
60
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";
|
|
72
|
+
|
|
73
|
+
// The partitions of a collection (roadmap CLI-17)
|
|
74
|
+
export { PARTITION_PATTERN, collectionTopicPartition, partitionOfCall, storageOfCall, withPartition } from "./partition.js";
|
|
75
|
+
export type { CollectionPartitions, PartitionInput, PartitionResolver } from "./partition.js";
|
|
61
76
|
|
|
62
77
|
// The storage interface of the core (the shape of client-collections' CollectionStorage)
|
|
63
|
-
export type {
|
|
78
|
+
export type {
|
|
79
|
+
CollectionStorage,
|
|
80
|
+
StorageChanges,
|
|
81
|
+
StorageMetadata,
|
|
82
|
+
StorageWriteOptions,
|
|
83
|
+
StorageSetChange,
|
|
84
|
+
StorageDeleteChange,
|
|
85
|
+
StorageClearChange,
|
|
86
|
+
StorageChangeEvent,
|
|
87
|
+
StorageChangeBatch,
|
|
88
|
+
StorageObserver,
|
|
89
|
+
} from "./storage.js";
|
|
90
|
+
|
|
91
|
+
// The query language of the storages: the same JSON on the wire and in query() (roadmap CLI-34:
|
|
92
|
+
// the one copy of the language)
|
|
93
|
+
export {
|
|
94
|
+
DEFAULT_FIND_LIMIT,
|
|
95
|
+
DEFAULT_QUERY_OPERATORS,
|
|
96
|
+
DEFAULT_QUERY_POLICY,
|
|
97
|
+
QUERY_OPERATORS,
|
|
98
|
+
applyPatch,
|
|
99
|
+
compareStorageValues,
|
|
100
|
+
compileSelector,
|
|
101
|
+
isPatchOperations,
|
|
102
|
+
matchesSelector,
|
|
103
|
+
mergePatch,
|
|
104
|
+
patchProblems,
|
|
105
|
+
queryEntries,
|
|
106
|
+
queryOptionsProblems,
|
|
107
|
+
selectorProblems,
|
|
108
|
+
} from "./query.js";
|
|
109
|
+
export type {
|
|
110
|
+
StorageFieldOperators,
|
|
111
|
+
StoragePatch,
|
|
112
|
+
StoragePatchOperations,
|
|
113
|
+
StorageQueryOperator,
|
|
114
|
+
StorageQueryOptions,
|
|
115
|
+
StorageQueryPolicy,
|
|
116
|
+
StorageSelector,
|
|
117
|
+
StorageSort,
|
|
118
|
+
StorageSortDirection,
|
|
119
|
+
} from "./query.js";
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The partitions of a collection (roadmap CLI-17). One `collections.<name>.*` surface serves the
|
|
3
|
+
* items of several partitions, for example one per tenant, or one per tenant and scope. Each call
|
|
4
|
+
* reads and writes one partition.
|
|
5
|
+
*
|
|
6
|
+
* - The host registers the partitions of a collection with its repository: the storage of each
|
|
7
|
+
* partition and the resolver (`ProcedureServer.usePartitions(name, { storageOf, resolve })`).
|
|
8
|
+
* - The input of a call can ask for a partition (`partition`, for example a scope). The resolver
|
|
9
|
+
* gives the partition of the call from its context: the tenant comes from the principal, never
|
|
10
|
+
* from the input alone. It can refuse the call.
|
|
11
|
+
* - A subscriber names the partition that it asks for in the topic, as an input does:
|
|
12
|
+
* `collections.<name>.<requested>`. The publisher resolves it in the same way and listens to the
|
|
13
|
+
* topic of the resolved partition, so a subscriber cannot name the topic of another tenant.
|
|
14
|
+
* - A collection without partitions refuses a call or a topic with a partition.
|
|
15
|
+
*/
|
|
16
|
+
import type { ZodLike } from "../../client/validation/types.js";
|
|
17
|
+
import { InvocationError } from "../invoke.js";
|
|
18
|
+
import type { ProcedureContext } from "../types.js";
|
|
19
|
+
import type { CollectionStorage } from "./storage.js";
|
|
20
|
+
|
|
21
|
+
/** The characters of a partition: letters, digits and `_ . : @ ~ + | / -`, 256 at most. */
|
|
22
|
+
export const PARTITION_PATTERN: RegExp = /^[A-Za-z0-9_.:@~+|/-]{1,256}$/;
|
|
23
|
+
|
|
24
|
+
/** The field of an input that asks for a partition. */
|
|
25
|
+
export interface PartitionInput {
|
|
26
|
+
partition?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The resolver of the partition of a call. It gets the context (with `ctx.principal`) and the
|
|
31
|
+
* partition that the input asks for. It gives the partition, `undefined` for the storage of the
|
|
32
|
+
* collection without a partition, or `null` to refuse the call (`FORBIDDEN`, reason
|
|
33
|
+
* `partition-not-allowed`). An error that it throws passes through.
|
|
34
|
+
*/
|
|
35
|
+
export type PartitionResolver = (ctx: ProcedureContext, requested: string | undefined) => string | undefined | null | Promise<string | undefined | null>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* This function adds the field `partition` to the input schema of a collection procedure. The
|
|
39
|
+
* repository of the call decides at the call if the collection has partitions.
|
|
40
|
+
*/
|
|
41
|
+
export function withPartition<T>(schema: ZodLike<T>): ZodLike<T & PartitionInput> {
|
|
42
|
+
const validate = (data: unknown): { success: true; data: T & PartitionInput } | { success: false; error: { message: string; errors: { path: (string | number)[]; message: string }[] } } => {
|
|
43
|
+
const fail = (message: string) => ({ success: false as const, error: { message, errors: [{ path: ["partition"], message }] } });
|
|
44
|
+
const requested = typeof data === "object" && data !== null && !Array.isArray(data) ? (data as Record<string, unknown>)["partition"] : undefined;
|
|
45
|
+
if (requested !== undefined && (typeof requested !== "string" || !PARTITION_PATTERN.test(requested))) {
|
|
46
|
+
return fail("partition must have 1 to 256 letters, digits or the characters _ . : @ ~ + | / -");
|
|
47
|
+
}
|
|
48
|
+
const inner = schema.safeParse(data);
|
|
49
|
+
if (!inner.success) return inner as { success: false; error: { message: string; errors: { path: (string | number)[]; message: string }[] } };
|
|
50
|
+
const parsed = inner.data as T & PartitionInput;
|
|
51
|
+
return { success: true, data: requested === undefined ? parsed : { ...parsed, partition: requested as string } };
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
parse(data: unknown) {
|
|
55
|
+
const result = validate(data);
|
|
56
|
+
if (result.success) return result.data;
|
|
57
|
+
throw new Error(result.error.message);
|
|
58
|
+
},
|
|
59
|
+
safeParse: validate,
|
|
60
|
+
} as ZodLike<T & PartitionInput>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The partitions of a collection: the storage of each partition, and the resolver of a call. */
|
|
64
|
+
export interface CollectionPartitions<T = unknown> {
|
|
65
|
+
/**
|
|
66
|
+
* The storage of a partition. Give the same storage for the same partition each time: the change
|
|
67
|
+
* events of a storage keep their order with it.
|
|
68
|
+
*/
|
|
69
|
+
storageOf(partition: string): CollectionStorage<T>;
|
|
70
|
+
/** The resolver of the partition of a call. */
|
|
71
|
+
resolve: PartitionResolver;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The refusal of a partition. */
|
|
75
|
+
function partitionRefused(ctx: ProcedureContext, message: string): InvocationError {
|
|
76
|
+
return new InvocationError("FORBIDDEN", message, ctx.path, "partition-not-allowed");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* This function gives the partition of a call to a collection: the resolver of the repository
|
|
81
|
+
* gives it from the context and the requested partition. It throws `FORBIDDEN` (reason
|
|
82
|
+
* `partition-not-allowed`) when the resolver refuses, or when the collection has no partitions and
|
|
83
|
+
* the call asks for one.
|
|
84
|
+
*/
|
|
85
|
+
export async function partitionOfCall(ctx: ProcedureContext, collection: string, requested: string | undefined): Promise<string | undefined> {
|
|
86
|
+
const repository = ctx.repository;
|
|
87
|
+
if (!repository?.hasPartitions?.(collection) || !repository.resolvePartition) {
|
|
88
|
+
if (requested !== undefined) throw partitionRefused(ctx, `The collection ${collection} has no partitions`);
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
const partition = await repository.resolvePartition(collection, ctx, requested);
|
|
92
|
+
if (partition === null) {
|
|
93
|
+
throw partitionRefused(ctx, `The call cannot use the partition${requested === undefined ? "" : ` "${requested}"`} of ${collection}`);
|
|
94
|
+
}
|
|
95
|
+
if (partition !== undefined && !PARTITION_PATTERN.test(partition)) {
|
|
96
|
+
throw new Error(`The partition resolver gave a partition that is not valid: ${JSON.stringify(partition).slice(0, 80)}`);
|
|
97
|
+
}
|
|
98
|
+
return partition;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* This function gives the storage of a call: of the collection, or of its partition. A repository
|
|
103
|
+
* without `getPartitionStorage` refuses a partition.
|
|
104
|
+
*/
|
|
105
|
+
export function storageOfCall<T>(ctx: ProcedureContext, collection: string, partition: string | undefined): CollectionStorage<T> {
|
|
106
|
+
const repository = ctx.repository;
|
|
107
|
+
if (!repository) throw new Error("Repository not available in procedure context");
|
|
108
|
+
if (partition === undefined) return repository.getStorage<T>(collection);
|
|
109
|
+
if (!repository.getPartitionStorage) {
|
|
110
|
+
throw new Error(`The repository has no partitions, so it cannot give the partition "${partition}" of ${collection}`);
|
|
111
|
+
}
|
|
112
|
+
return repository.getPartitionStorage<T>(collection, partition);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* This function gives the collection and the partition part of a topic `collections.<name>` or
|
|
117
|
+
* `collections.<name>.<partition>`, or undefined for another topic.
|
|
118
|
+
*/
|
|
119
|
+
export function collectionTopicPartition(topic: string): { collection: string; partition: string | undefined } | undefined {
|
|
120
|
+
const match = /^collections\.([^.]+)(?:\.(.+))?$/.exec(topic);
|
|
121
|
+
if (!match) return undefined;
|
|
122
|
+
return { collection: match[1]!, partition: match[2] };
|
|
123
|
+
}
|