@bridgebrew/storage 0.0.0-stage → 0.0.2
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/LICENSE +21 -0
- package/browser/browser.tsx +51 -0
- package/browser/storage-model.ts +70 -0
- package/browser/storage-page.tsx +340 -0
- package/browser/tsconfig.json +13 -0
- package/facets.ts +263 -0
- package/inventory.ts +47 -0
- package/objects.ts +1185 -0
- package/package.json +53 -4
- package/references.ts +97 -0
- package/server.ts +1606 -0
- package/tsconfig.json +1 -0
- package/README.md +0 -3
package/server.ts
ADDED
|
@@ -0,0 +1,1606 @@
|
|
|
1
|
+
// Storage service (AD-14): typed domains over this workspace's `storageBackend`. A plugin declares
|
|
2
|
+
// `ctx.storage.domain(ctx, { name, schema, version, migrate? })`; the domain's id is
|
|
3
|
+
// `<plugin key>/<name>` and it is released when the declaring plugin disposes. An object domain
|
|
4
|
+
// (`ctx.storage.objects`, AD-24) holds `{ id, node, value }` objects on nodes of the worker's tree; a facet domain
|
|
5
|
+
// (`ctx.storage.facets`) attaches values to another domain's objects or to nodes (`bridge/node`). Object and facet reads
|
|
6
|
+
// are async, served by the backend entry by entry; plain domains are held in memory and read synchronously. Values may hold
|
|
7
|
+
// declared references `{ domain, id }`, indexed in memory ("used by"), and resolved with a state; queries filter and
|
|
8
|
+
// load through them (`where` relation keys and `usedBy`, `with`). Any plugin reads any declared object or facet domain
|
|
9
|
+
// through `ctx.storage.read(id)` (D6), but a domain declared `private` (research-011 D6), which only its owner reads
|
|
10
|
+
// (see `ObjectsSpec.private`). Only the owner writes, but an object domain its owner opens (`ObjectsSpec.writers:
|
|
11
|
+
// 'workspace'`, research-052 D3), which any plugin of the workspace writes through `ctx.storage.write(id)`, never
|
|
12
|
+
// purges. An object domain's rules (`ObjectsSpec.rules`, research-052 D1) are declared with it and run by storage on
|
|
13
|
+
// every write, whoever writes, in the domain's write queue: a value checked beyond its schema, refused with the rule's
|
|
14
|
+
// own code (`refuse`); they hold a domain open to other writers. A log
|
|
15
|
+
// (`ctx.storage.log`, research-011 D2) is an append-only list of numbered events per stream, in the same id space,
|
|
16
|
+
// with the same ownership; read-only to others through `read(id)` unless private; it keeps bytes whole as blobs
|
|
17
|
+
// (`attach`, an image the user attached), deleted with the last stream referencing them. Deleting an object
|
|
18
|
+
// archives it (soft delete); removing a plugin warns about its referenced objects (the `removal` built-in). Writes check the caller's plugin key, which
|
|
19
|
+
// guards against mistakes, not against a plugin acting on purpose (the key symbol is global; plugins are trusted,
|
|
20
|
+
// AD-20); all share the id space and the declaration rules. Shared domains
|
|
21
|
+
// (`read`/`write`/`remove`/`list`) are plain async functions over serializable data, so `_host`
|
|
22
|
+
// can expose them to the other workspaces as `hostStorage` (E11, AD-5).
|
|
23
|
+
// The audit (research-011 D3) is optional (research-048): while a plugin records it (`storage.audit.record(owner)`,
|
|
24
|
+
// the standard plugin `storage-tools-audit`), every write to an object, facet or plain domain (shared ones too), and
|
|
25
|
+
// every log purge, carries `{ op, fields, plugin, actor }` on its backend change; a database engine (storage-sqlite,
|
|
26
|
+
// storage-postgres) stores that row in the write's own transaction, `storage-json` keeps none. Unrecorded, no row. `fields` are names, never values; `plugin` is the writing plugin's key
|
|
27
|
+
// (`storage` for its own writes: shared domains, a purge's facets, migrations, the JSON import); `actor` is
|
|
28
|
+
// `ctx.actor.current()` read when the write is accepted (`plugin` outside any request). Log appends are not audited.
|
|
29
|
+
// Any plugin reads the audit through `ctx.storage.audit`, private domains included (ids, ops and field names only), and
|
|
30
|
+
// prunes it; its retention, its route and its agents' tool are `storage-tools-audit`'s.
|
|
31
|
+
import { readFile } from 'node:fs/promises';
|
|
32
|
+
import { FiberState, type Context } from 'cordis';
|
|
33
|
+
import { z } from 'zod';
|
|
34
|
+
import {
|
|
35
|
+
checkPath,
|
|
36
|
+
hub,
|
|
37
|
+
keysAt,
|
|
38
|
+
pick,
|
|
39
|
+
type AuditActor,
|
|
40
|
+
type AuditMeta,
|
|
41
|
+
type AuditObject,
|
|
42
|
+
type AuditQuery,
|
|
43
|
+
type AuditRow,
|
|
44
|
+
type Backend,
|
|
45
|
+
type BlobRef,
|
|
46
|
+
type Domain,
|
|
47
|
+
type DomainSpec,
|
|
48
|
+
type Filter,
|
|
49
|
+
type LogEvent,
|
|
50
|
+
type LogStream,
|
|
51
|
+
type LogWindow,
|
|
52
|
+
type Store,
|
|
53
|
+
} from '@bridge/storage';
|
|
54
|
+
import type {} from '@bridge/worker-runtime/router';
|
|
55
|
+
import { facetDomain, type FacetDomain, type FacetsSpec, type StoredFacet } from './facets.ts';
|
|
56
|
+
import type { DomainInventory, Inventory } from './inventory.ts';
|
|
57
|
+
import {
|
|
58
|
+
changedFields,
|
|
59
|
+
checkActive,
|
|
60
|
+
coded,
|
|
61
|
+
refuse,
|
|
62
|
+
ruleNames,
|
|
63
|
+
hidden,
|
|
64
|
+
inArchivedNode,
|
|
65
|
+
isRecord,
|
|
66
|
+
objectDomain,
|
|
67
|
+
pluginDataVersions,
|
|
68
|
+
pluginKey,
|
|
69
|
+
serialWrites,
|
|
70
|
+
view,
|
|
71
|
+
type CountOptions,
|
|
72
|
+
type Envelope,
|
|
73
|
+
type Group,
|
|
74
|
+
type GroupByOptions,
|
|
75
|
+
type Nodes,
|
|
76
|
+
type ObjectDomain,
|
|
77
|
+
type ObjectsSpec,
|
|
78
|
+
type OrderBy,
|
|
79
|
+
type Page,
|
|
80
|
+
type PageOptions,
|
|
81
|
+
type QueryOptions,
|
|
82
|
+
type Related,
|
|
83
|
+
type RuleWrite,
|
|
84
|
+
type Rules,
|
|
85
|
+
type StoredObject,
|
|
86
|
+
type Where,
|
|
87
|
+
type With,
|
|
88
|
+
type WithResult,
|
|
89
|
+
type WriteOptions,
|
|
90
|
+
} from './objects.ts';
|
|
91
|
+
import {
|
|
92
|
+
nodeDomain,
|
|
93
|
+
reference,
|
|
94
|
+
referenceIndex,
|
|
95
|
+
referencesIn,
|
|
96
|
+
referencesSchema,
|
|
97
|
+
type Reference,
|
|
98
|
+
type Referrer,
|
|
99
|
+
type References,
|
|
100
|
+
} from './references.ts';
|
|
101
|
+
|
|
102
|
+
export type { DomainInventory, DomainKind, Inventory } from './inventory.ts';
|
|
103
|
+
export type {
|
|
104
|
+
AuditActor,
|
|
105
|
+
AuditObject,
|
|
106
|
+
AuditQuery,
|
|
107
|
+
AuditRow,
|
|
108
|
+
BlobRef,
|
|
109
|
+
CountOptions,
|
|
110
|
+
Envelope,
|
|
111
|
+
FacetDomain,
|
|
112
|
+
Group,
|
|
113
|
+
GroupByOptions,
|
|
114
|
+
FacetsSpec,
|
|
115
|
+
LogEvent,
|
|
116
|
+
LogStream,
|
|
117
|
+
LogWindow,
|
|
118
|
+
ObjectDomain,
|
|
119
|
+
ObjectsSpec,
|
|
120
|
+
OrderBy,
|
|
121
|
+
Page,
|
|
122
|
+
PageOptions,
|
|
123
|
+
QueryOptions,
|
|
124
|
+
Reference,
|
|
125
|
+
Referrer,
|
|
126
|
+
Related,
|
|
127
|
+
RuleWrite,
|
|
128
|
+
Rules,
|
|
129
|
+
StoredObject,
|
|
130
|
+
Where,
|
|
131
|
+
With,
|
|
132
|
+
WithResult,
|
|
133
|
+
WriteOptions,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export interface LogSpec<T> {
|
|
137
|
+
name: string;
|
|
138
|
+
/** Validates each event on append, never on read. */
|
|
139
|
+
schema: z.ZodType<T>;
|
|
140
|
+
/** Kept with each event (its `v`); events are never rewritten, so a later version reads older ones from their own
|
|
141
|
+
* `v`, and a version change needs no migration nor restart. */
|
|
142
|
+
version: number;
|
|
143
|
+
/** As `ObjectsSpec.private`: only its owner reads it. */
|
|
144
|
+
private?: boolean;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A log `<plugin key>/<name>` (research-011 D2): per stream (a name, `segment` rules), events numbered `n` = 1, 2,
|
|
148
|
+
* 3… with no gap. Writes take the caller's context and refuse one of another plugin (`not_owner`); an event failing the
|
|
149
|
+
* schema is refused `invalid_value` (400), an append whose `after` is not the stream's last `n` `log_conflict` (409),
|
|
150
|
+
* a bad window `invalid_query` (400). An append is never part of a transaction with domain writes. */
|
|
151
|
+
export interface LogDomain<T> {
|
|
152
|
+
/** Appends `events` in one transaction; resolves to the new last `n` once durable and `subscribe` listeners ran. */
|
|
153
|
+
append(
|
|
154
|
+
caller: Context,
|
|
155
|
+
stream: string,
|
|
156
|
+
events: T[],
|
|
157
|
+
options?: { after?: number },
|
|
158
|
+
): Promise<number>;
|
|
159
|
+
/** `[{ n, at, v, data }]`: after `after` ascending (default 0), or before `before` descending (`Infinity`: the
|
|
160
|
+
* newest); at most `take`, 1000 at most and by default. As stored: not validated again. */
|
|
161
|
+
read(stream: string, window?: LogWindow): Promise<LogEvent[]>;
|
|
162
|
+
/** The last `n`; 0 for a stream with none. */
|
|
163
|
+
last(stream: string): Promise<number>;
|
|
164
|
+
/** The streams holding events, by name, after `after`, at most `take` (1000 at most and by default). */
|
|
165
|
+
streams(page?: { after?: string; take?: number }): Promise<LogStream[]>;
|
|
166
|
+
/** Removes `stream` whole, with its references to blobs: a blob no stream references is deleted. */
|
|
167
|
+
purge(caller: Context, stream: string): Promise<void>;
|
|
168
|
+
/** Keeps `bytes` whole as a blob of `stream` (an image the user attached; research-011 D2), in order with its appends:
|
|
169
|
+
* attach, then append the event naming the `{ blob, size, type }` it resolves to. The same bytes are kept once. At
|
|
170
|
+
* most 50 MiB, a lowercase MIME `type`, else `invalid_value` (400); on `storage-json`, `blobs_unavailable` (409). */
|
|
171
|
+
attach(
|
|
172
|
+
caller: Context,
|
|
173
|
+
stream: string,
|
|
174
|
+
bytes: Uint8Array,
|
|
175
|
+
options: { type: string },
|
|
176
|
+
): Promise<BlobRef>;
|
|
177
|
+
/** As `attach`, for a regular file in the plugin's own data folder (`<dataDir>/<plugin key>/`, links resolved): it
|
|
178
|
+
* becomes the blob, moved after one read that names it, never held in memory (a video the user keeps); at most 2 GiB;
|
|
179
|
+
* else `invalid_value` (400). */
|
|
180
|
+
attachFile(
|
|
181
|
+
caller: Context,
|
|
182
|
+
stream: string,
|
|
183
|
+
path: string,
|
|
184
|
+
options: { type: string },
|
|
185
|
+
): Promise<BlobRef>;
|
|
186
|
+
/** Blob `blob` when a stream of this log references it: `{ blob, size, type, path }`, `path` its file (absolute, to
|
|
187
|
+
* read or give to an agent; never write it); else `undefined`. */
|
|
188
|
+
blob(blob: string): Promise<(BlobRef & { path: string }) | undefined>;
|
|
189
|
+
/** The bytes of blob `blob` when a stream of this log references it; else `undefined`. */
|
|
190
|
+
bytes(blob: string): Promise<Uint8Array | undefined>;
|
|
191
|
+
/** `fn` runs after each durable append to `stream` with its events, in this worker only. */
|
|
192
|
+
subscribe(stream: string, fn: (events: LogEvent[]) => void): () => void;
|
|
193
|
+
release(): void;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The audit, read-only (research-011 D3). On `storage-json`, every call rejects `audit_unavailable` (409); a bad
|
|
197
|
+
* query `invalid_query` (400). */
|
|
198
|
+
export interface AuditReader {
|
|
199
|
+
/** Rows matching every field given (`after`, `before`: bounds on `seq`), newest first, at most `take` (1000 at most
|
|
200
|
+
* and by default); `next`, when more match, is the `before` of the next page. */
|
|
201
|
+
query(query?: AuditQuery): Promise<{ rows: AuditRow[]; next?: number }>;
|
|
202
|
+
/** Who changed entry `key` of domain `domain`: `[{ actor, count, last }]`, `last` the `at` (ms) of their newest row. */
|
|
203
|
+
actors(entry: { domain: string; key: string }): Promise<AuditActor[]>;
|
|
204
|
+
/** What `actor` changed: `[{ domain, key, count, last }]`, newest first. */
|
|
205
|
+
objects(of: { actor: string }): Promise<AuditObject[]>;
|
|
206
|
+
/** Deletes up to `auditPruneSlice` rows older than `before` (ms) in one transaction; resolves with how many went. */
|
|
207
|
+
prune(before: number): Promise<number>;
|
|
208
|
+
/** Writes carry their audit row while `owner` is alive (research-048: the audit is optional, off until a plugin
|
|
209
|
+
* records it). Rejects `audit_unavailable` on a backend that keeps none (storage-json), recording nothing. */
|
|
210
|
+
record(owner: Context): Promise<void>;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** What `read` gives over an object domain, a facet domain, or a log: reads only. */
|
|
214
|
+
export type ObjectReader<T> = Pick<ObjectDomain<T>, 'get' | 'query' | 'page' | 'count' | 'groupBy'>;
|
|
215
|
+
/** What `write` gives over an object domain (research-052 D3): its writes but `purge`, and its reads. */
|
|
216
|
+
export type ObjectWriter<T> = Pick<
|
|
217
|
+
ObjectDomain<T>,
|
|
218
|
+
'create' | 'update' | 'delete' | 'restore' | 'move'
|
|
219
|
+
> &
|
|
220
|
+
ObjectReader<T>;
|
|
221
|
+
export type FacetReader<T> = Pick<FacetDomain<T>, 'get' | 'query' | 'page' | 'count'>;
|
|
222
|
+
export type LogReader = Pick<LogDomain<unknown>, 'read' | 'last' | 'streams'>;
|
|
223
|
+
|
|
224
|
+
/** `archived`: the object (or the node, `bridge/node`) is archived; `in-archived-node`: live itself, under an archived
|
|
225
|
+
* node; `owner-missing`: its domain is not declared in this worker (its plugin not loaded); `missing`: declared, no
|
|
226
|
+
* such id. */
|
|
227
|
+
export type ReferenceState = 'live' | 'archived' | 'in-archived-node' | 'owner-missing' | 'missing';
|
|
228
|
+
export interface Resolved {
|
|
229
|
+
state: ReferenceState;
|
|
230
|
+
/** A copy of the object (without facets) or the node, when known; of a private domain's object, `{ id, node,
|
|
231
|
+
* archived }` only. */
|
|
232
|
+
object?: StoredObject<unknown> | { id: string; node?: string; archived: boolean };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** The worker's built-in `removal` service (`@bridge/worker-runtime/removal`), as storage uses it. */
|
|
236
|
+
interface Removal {
|
|
237
|
+
check(owner: Context, fn: (plugin: string) => Promise<string[]>): void;
|
|
238
|
+
}
|
|
239
|
+
export type { Include } from './objects.ts';
|
|
240
|
+
export { nodeDomain, reference, refuse };
|
|
241
|
+
|
|
242
|
+
/** Lowercase only, so two names never share a file on a case-insensitive filesystem. */
|
|
243
|
+
const domainName = /^[a-z0-9][a-z0-9-]*$/;
|
|
244
|
+
/** A domain id `<plugin key>/<name>`. */
|
|
245
|
+
const domainId = /^[^/\s]+\/[^/\s]+$/;
|
|
246
|
+
/** Plugin keys no plugin may declare under, and what they are reserved for. */
|
|
247
|
+
const reservedKeys: Record<string, string> = { shared: 'shared domains', bridge: nodeDomain };
|
|
248
|
+
|
|
249
|
+
/** A declared domain as the inventory tells it, but what is read at each request: who references it, and its count. */
|
|
250
|
+
type Catalogued = Omit<DomainInventory, 'referencedBy' | 'count'> & {
|
|
251
|
+
count?: () => Promise<NonNullable<DomainInventory['count']>>;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
export interface StorageService {
|
|
255
|
+
/** Local only: `owner` is a live context, which cannot cross workers. */
|
|
256
|
+
domain<T>(owner: Context, spec: Omit<DomainSpec<T>, 'id'> & { name: string }): Promise<Domain<T>>;
|
|
257
|
+
/** An object domain `<plugin key>/<name>`; local only, like `domain`. One name is one domain, plain, object, or facet. */
|
|
258
|
+
objects<T>(owner: Context, spec: ObjectsSpec<T>): Promise<ObjectDomain<T>>;
|
|
259
|
+
/** A facet domain `<plugin key>/<name>` over `spec.target` (an object domain id or `bridge/node`); local only. */
|
|
260
|
+
facets<T>(owner: Context, spec: FacetsSpec<T>): Promise<FacetDomain<T>>;
|
|
261
|
+
/** A log `<plugin key>/<name>`; local only, like `domain`. One name is one domain or one log, never both. */
|
|
262
|
+
log<T>(owner: Context, spec: LogSpec<T>): Promise<LogDomain<T>>;
|
|
263
|
+
/** A schema for a reference to `domain`, for a field declared in `references`. */
|
|
264
|
+
reference(domain: string): z.ZodType<Reference>;
|
|
265
|
+
/** A refusal for a domain's rule to throw (`ObjectsSpec.rules`, research-052 D1): a route answers its `status` (409
|
|
266
|
+
* by default, a 4xx) with `{ error: { code, message } }`. */
|
|
267
|
+
refuse(code: string, message: string, status?: number): Error;
|
|
268
|
+
/** The object (or node, `bridge/node`) with the values of every declared facet domain over it (but private facet
|
|
269
|
+
* domains); `undefined` if absent. Of a private domain's object, `{ id, node,
|
|
270
|
+
* archived, facets }`, never its value. */
|
|
271
|
+
envelope(
|
|
272
|
+
target: Reference,
|
|
273
|
+
): Promise<
|
|
274
|
+
| Envelope<unknown>
|
|
275
|
+
| { id: string; node: string; archived?: boolean; facets: Record<string, unknown> }
|
|
276
|
+
| undefined
|
|
277
|
+
>;
|
|
278
|
+
/** Who references `target` among declared domains, sorted; `archived`: the referrer is hidden (archived or under an
|
|
279
|
+
* archived node; for a facet, its target). */
|
|
280
|
+
usedBy(target: Reference): Promise<(Referrer & { archived: boolean })[]>;
|
|
281
|
+
/** What `target` points to, with its state. */
|
|
282
|
+
resolve(target: Reference): Promise<Resolved>;
|
|
283
|
+
/** The value under `key` in the shared domain `name`, or `undefined`. */
|
|
284
|
+
read(name: string, key: string): Promise<unknown>;
|
|
285
|
+
/** Read-only access to the object or facet domain, or the log (`read`, `last`, `streams`; never `subscribe`), `id`
|
|
286
|
+
* declared in this worker, whatever plugin owns it (D6): each read gives what the owner's own read gives, or rejects
|
|
287
|
+
* with `owner_missing` (status 404) while `id` is not declared (its plugin not loaded), `private_domain` (status 403)
|
|
288
|
+
* while it is private (research-011 D6: the owner holds its handle, and needs no `read`). No writes: see `write(id)`,
|
|
289
|
+
* else a plugin changes another's objects through the owner's service. Local only. */
|
|
290
|
+
read<R extends ObjectReader<unknown> | FacetReader<unknown> | LogReader = ObjectReader<unknown>>(
|
|
291
|
+
id: string,
|
|
292
|
+
): R;
|
|
293
|
+
/** Resolves once `value` (JSON: no Date, Map, BigInt, undefined…) is durable. */
|
|
294
|
+
write(name: string, key: string, value: unknown): Promise<void>;
|
|
295
|
+
/** Writes to object domain `id` of another plugin (research-052 D3): `create`, `update`, `delete`, `restore`, `move`,
|
|
296
|
+
* as on the owner's handle (the caller's context, its options, its rules, the audit naming the caller), and the reads
|
|
297
|
+
* `read(id)` gives. Resolved at each call, as `read(id)`: rejects `owner_missing` (404) while `id` is not declared,
|
|
298
|
+
* `private_domain` (403) while private, `not_open` (403) while its `writers` is `owner`, or for a facet domain or a
|
|
299
|
+
* log (their owner writes them only). The owner's own writes pass, open or not: its own handle (its reads of a
|
|
300
|
+
* private domain stay refused, as `read(id)`). Local only. */
|
|
301
|
+
write<T = unknown>(id: string): ObjectWriter<T>;
|
|
302
|
+
remove(name: string, key: string): Promise<void>;
|
|
303
|
+
list(name: string): Promise<string[]>;
|
|
304
|
+
/** The audit trail of this workspace's writes; local only. */
|
|
305
|
+
audit: AuditReader;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
declare module 'cordis' {
|
|
309
|
+
interface Context {
|
|
310
|
+
storage: StorageService;
|
|
311
|
+
storageBackend: Backend;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export const name = 'storage';
|
|
316
|
+
export const inject = ['storageBackend', 'nodes', 'removal', 'router'];
|
|
317
|
+
|
|
318
|
+
export function apply(ctx: Context) {
|
|
319
|
+
/** The actor of the running code (research-011 D4): the `actor` service's, `plugin` without it. */
|
|
320
|
+
const actorNow = () =>
|
|
321
|
+
(typeof ctx.get === 'function'
|
|
322
|
+
? (ctx.get('actor', false) as { current(): string } | undefined)?.current()
|
|
323
|
+
: undefined) ?? 'plugin';
|
|
324
|
+
// The audit is written only while a plugin records it (research-048): unrecorded, a change reaches the backend
|
|
325
|
+
// without its `audit`, so a backend keeps no row.
|
|
326
|
+
// A workspace with storage-tools-audit enabled records from the start (Codex review, research-048): the writes and
|
|
327
|
+
// migrations of the plugins that load before it carry their rows too. `nodes.settings` knows only enabled plugins.
|
|
328
|
+
let recorders = ctx.nodes.settings?.('storage-tools-audit', 'root') !== undefined ? 1 : 0;
|
|
329
|
+
const backend = new Proxy(ctx.storageBackend, {
|
|
330
|
+
get(target, prop) {
|
|
331
|
+
if (prop === 'write')
|
|
332
|
+
return (changes: Parameters<Backend['write']>[0]) =>
|
|
333
|
+
target.write(recorders > 0 ? changes : changes.map(({ audit: _, ...change }) => change));
|
|
334
|
+
if (prop === 'purge')
|
|
335
|
+
return (id: string, stream: string, audit?: AuditMeta) =>
|
|
336
|
+
target.purge(id, stream, recorders > 0 ? audit : undefined);
|
|
337
|
+
const value: unknown = Reflect.get(target, prop, target);
|
|
338
|
+
return typeof value === 'function'
|
|
339
|
+
? (value as (...args: unknown[]) => unknown).bind(target)
|
|
340
|
+
: value;
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
const domains = hub(backend, { actor: actorNow });
|
|
344
|
+
const checkName = (fn: string, name: unknown) => {
|
|
345
|
+
if (typeof name !== 'string' || !domainName.test(name))
|
|
346
|
+
throw new Error(
|
|
347
|
+
`storage.${fn}: name must match ${domainName.source}, got ${JSON.stringify(name)}`,
|
|
348
|
+
);
|
|
349
|
+
};
|
|
350
|
+
// Shared domains `shared/<name>`, owned by this plugin: declared on first use, released on dispose.
|
|
351
|
+
// ponytail: any caller reaches every shared domain, no per-workspace or per-plugin isolation, until
|
|
352
|
+
// the host attaches caller identity to calls (AD-20); shared domains are also unbounded in number and size.
|
|
353
|
+
const shared = new Map<string, Promise<Domain<z.core.util.JSONType>>>();
|
|
354
|
+
let disposed = false;
|
|
355
|
+
// Its dependents dispose first, and `storage-json` closes the backend after this: changes they
|
|
356
|
+
// queued without awaiting (a last `set` in a dispose) land before it closes.
|
|
357
|
+
ctx.effect(() => () => {
|
|
358
|
+
disposed = true;
|
|
359
|
+
for (const domain of shared.values())
|
|
360
|
+
domain.then(
|
|
361
|
+
(d) => d.release(),
|
|
362
|
+
() => {},
|
|
363
|
+
);
|
|
364
|
+
shared.clear();
|
|
365
|
+
return domains.drain();
|
|
366
|
+
});
|
|
367
|
+
const checkKey = (fn: string, key: unknown) => {
|
|
368
|
+
if (typeof key !== 'string')
|
|
369
|
+
throw new Error(`storage.${fn}: key must be a string, got ${typeof key}`);
|
|
370
|
+
};
|
|
371
|
+
const sharedDomain = (fn: string, name: unknown) => {
|
|
372
|
+
checkName(fn, name);
|
|
373
|
+
if (disposed) throw new Error(`storage.${fn}: storage was disposed`);
|
|
374
|
+
let domain = shared.get(name as string);
|
|
375
|
+
if (!domain) {
|
|
376
|
+
domain = domains.domain({ id: `shared/${name as string}`, schema: z.json(), version: 1 });
|
|
377
|
+
domain.catch(() => shared.delete(name as string));
|
|
378
|
+
shared.set(name as string, domain);
|
|
379
|
+
}
|
|
380
|
+
return domain;
|
|
381
|
+
};
|
|
382
|
+
/** Declares `<plugin key>/<name>` for `owner` through `make` (a plain domain, a store for object and facet domains,
|
|
383
|
+
* or a log), released when `owner` disposes. `make` migrates an older stored version, at a workspace start or in a
|
|
384
|
+
* hot reload alike (AD-4, amended by #189): after a backup, the workspace kept running. */
|
|
385
|
+
const declare = async <T, D extends { release(): void }>(
|
|
386
|
+
fn: string,
|
|
387
|
+
owner: Context,
|
|
388
|
+
name: string,
|
|
389
|
+
rest: Omit<DomainSpec<T>, 'id'>,
|
|
390
|
+
make: (spec: DomainSpec<T>) => Promise<D>,
|
|
391
|
+
) => {
|
|
392
|
+
// A call through `hostStorage` arrives structured-cloned: no context survives that.
|
|
393
|
+
if (typeof (owner as { effect?: unknown } | null)?.effect !== 'function')
|
|
394
|
+
throw new Error(
|
|
395
|
+
`storage.${fn} is local only: declare domains with ctx.storage in your own workspace; use read/write/remove/list on hostStorage`,
|
|
396
|
+
);
|
|
397
|
+
checkName(fn, name);
|
|
398
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the kernel sets the plugin key symbols on every plugin context
|
|
399
|
+
const { [pluginKey]: key, [pluginDataVersions]: declared } = owner as unknown as Record<
|
|
400
|
+
symbol,
|
|
401
|
+
unknown
|
|
402
|
+
>;
|
|
403
|
+
if (typeof key !== 'string' || !key)
|
|
404
|
+
throw new Error(`storage.${fn}: owner context has no plugin key`);
|
|
405
|
+
if (Object.hasOwn(reservedKeys, key))
|
|
406
|
+
throw new Error(
|
|
407
|
+
`storage.${fn}: the plugin key ${JSON.stringify(key)} is reserved for ${reservedKeys[key]}`,
|
|
408
|
+
);
|
|
409
|
+
const id = `${key}/${name}`;
|
|
410
|
+
if (!Number.isInteger(rest.version))
|
|
411
|
+
throw new Error(
|
|
412
|
+
`storage.${fn}: ${id}: version must be an integer, got ${JSON.stringify(rest.version)}`,
|
|
413
|
+
);
|
|
414
|
+
// What its manifest declares (`bridge.dataVersions`, research-102 §7.9 condition 5) is what an update trusts to tell
|
|
415
|
+
// whether a version migrates: a declaration its code does not match is refused right before this domain would
|
|
416
|
+
// migrate, and the plugin fails. A log never migrates. Undeclared, nothing to check. Limit: a domain declared earlier
|
|
417
|
+
// in the same start may have migrated already; its data is kept (never put back: a restore could lose writes), and
|
|
418
|
+
// the previous version cannot read it until a fixed version comes. `bridge plugin check` refuses a mismatch it can
|
|
419
|
+
// read before publication.
|
|
420
|
+
if (fn !== 'log' && isRecord(declared)) {
|
|
421
|
+
const said = declared[name];
|
|
422
|
+
if (said === undefined)
|
|
423
|
+
throw new Error(
|
|
424
|
+
`storage.${fn}: ${key}'s package.json does not declare domain ${name} (version ${rest.version}) in bridge.dataVersions: add "${name}": ${rest.version}`,
|
|
425
|
+
);
|
|
426
|
+
if (said !== rest.version)
|
|
427
|
+
throw new Error(
|
|
428
|
+
`storage.${fn}: ${key}'s package.json declares domain ${name} at version ${String(said)}, its code at version ${rest.version}: make bridge.dataVersions say "${name}": ${rest.version}`,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
const domain = await make({ ...rest, id });
|
|
432
|
+
const { fiber } = owner;
|
|
433
|
+
if (
|
|
434
|
+
fiber.uid === null ||
|
|
435
|
+
(fiber.state !== FiberState.LOADING && fiber.state !== FiberState.ACTIVE)
|
|
436
|
+
) {
|
|
437
|
+
domain.release();
|
|
438
|
+
throw new Error(`storage.${fn}: ${key} was disposed while declaring ${name}`);
|
|
439
|
+
}
|
|
440
|
+
owner.effect(() => () => domain.release());
|
|
441
|
+
return { domain, key, id };
|
|
442
|
+
};
|
|
443
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the worker Context carries the nodes service, which its type does not name
|
|
444
|
+
const nodes = (ctx as unknown as { nodes: Nodes }).nodes;
|
|
445
|
+
/** Declared object and facet domains of this worker, by id: what references, facets, and envelopes resolve against. */
|
|
446
|
+
const objectDomains = new Map<string, Store<StoredObject<unknown>>>();
|
|
447
|
+
const facetDomains = new Map<
|
|
448
|
+
string,
|
|
449
|
+
{ target: string; key: string; domain: Store<StoredFacet<unknown>> }
|
|
450
|
+
>();
|
|
451
|
+
const index = referenceIndex();
|
|
452
|
+
/** Per declared object or facet domain, its references; and its reads, for `read`. */
|
|
453
|
+
const declaredRefs = new Map<string, References>();
|
|
454
|
+
const readers = new Map<string, ObjectReader<unknown> | FacetReader<unknown>>();
|
|
455
|
+
/** Declared logs, by id. */
|
|
456
|
+
const logDomains = new Map<string, LogDomain<unknown>>();
|
|
457
|
+
/** Per object domain id, its last generation's handle (current while `objectDomains` holds its `store`), and whether
|
|
458
|
+
* it is open to other plugins' writes (research-052 D3). */
|
|
459
|
+
const writable = new Map<
|
|
460
|
+
string,
|
|
461
|
+
{ store: Store<StoredObject<unknown>>; domain: ObjectDomain<unknown>; open: boolean }
|
|
462
|
+
>();
|
|
463
|
+
/** Object, facet and log domains declared `private`, by id. */
|
|
464
|
+
const privateIds = new Set<string>();
|
|
465
|
+
/** Every domain declared in this worker but shared ones, by id: what the inventory tells of it (`GET /inventory`), and
|
|
466
|
+
* how to count what it holds. */
|
|
467
|
+
const catalog = new Map<string, Catalogued>();
|
|
468
|
+
const keyOf = (id: string) => id.slice(0, id.indexOf('/'));
|
|
469
|
+
/** Refuses `private_domain` (403) following a relation into `target` from domain `from` of another plugin: a private
|
|
470
|
+
* domain is followed from its owner's own domains only (the plugin key is in both ids, so the check is static). */
|
|
471
|
+
const notPrivate = (target: string, from: string, what: string) => {
|
|
472
|
+
if (privateIds.has(target) && keyOf(target) !== keyOf(from))
|
|
473
|
+
throw coded(
|
|
474
|
+
`${what}: ${target} is private to plugin ${keyOf(target)}; ask it through its service`,
|
|
475
|
+
'private_domain',
|
|
476
|
+
403,
|
|
477
|
+
);
|
|
478
|
+
};
|
|
479
|
+
const checkPrivate = (fn: string, value: unknown) => {
|
|
480
|
+
if (value !== undefined && typeof value !== 'boolean')
|
|
481
|
+
throw new Error(`storage.${fn}: private must be a boolean, got ${JSON.stringify(value)}`);
|
|
482
|
+
return value === true;
|
|
483
|
+
};
|
|
484
|
+
const exists = async ({ domain, id }: Reference) =>
|
|
485
|
+
domain === nodeDomain
|
|
486
|
+
? nodes.has(id)
|
|
487
|
+
: (await objectDomains.get(domain)?.get(id)) !== undefined;
|
|
488
|
+
/** The facets on `id` of `target`, less those whose plugin is not active on `node`, the target's node (AD-25: kept, not
|
|
489
|
+
* shown), and less every private facet domain (its owner reads them through its handle). */
|
|
490
|
+
const facetsOf = async (target: string, id: string, node: string) =>
|
|
491
|
+
Object.fromEntries(
|
|
492
|
+
(
|
|
493
|
+
await Promise.all(
|
|
494
|
+
[...facetDomains]
|
|
495
|
+
.filter(
|
|
496
|
+
([facetId, facets]) =>
|
|
497
|
+
facets.target === target &&
|
|
498
|
+
nodes.isActive(facets.key, node) &&
|
|
499
|
+
!privateIds.has(facetId),
|
|
500
|
+
)
|
|
501
|
+
.map(async ([facetId, facets]) => [facetId, await facets.domain.get(id)] as const),
|
|
502
|
+
)
|
|
503
|
+
).flatMap(([facetId, facet]) => (facet ? [[facetId, facet.value]] : [])),
|
|
504
|
+
);
|
|
505
|
+
/** Whether what `{ domain, id }` names is hidden: an object archived or under an archived node, a node archived or
|
|
506
|
+
* under one, a facet whose target is. */
|
|
507
|
+
const hiddenRef = async ({ domain, id }: Reference): Promise<boolean> => {
|
|
508
|
+
const facets = facetDomains.get(domain);
|
|
509
|
+
if (facets) return facets.target !== domain && hiddenRef({ domain: facets.target, id });
|
|
510
|
+
if (domain === nodeDomain) return inArchivedNode(nodes, id);
|
|
511
|
+
const object = await objectDomains.get(domain)?.get(id);
|
|
512
|
+
return object !== undefined && hidden(nodes, object);
|
|
513
|
+
};
|
|
514
|
+
/** Refuses a reference `value` holds to a missing object of a declared domain or an unknown node; one to an
|
|
515
|
+
* undeclared domain is accepted (resolved later, entry 6). */
|
|
516
|
+
const checkReferences =
|
|
517
|
+
(id: string, references: References | undefined) => async (value: unknown) => {
|
|
518
|
+
for (const ref of references ? referencesIn(value, references) : []) {
|
|
519
|
+
if ((ref.domain === nodeDomain || objectDomains.has(ref.domain)) && !(await exists(ref)))
|
|
520
|
+
throw coded(`${id}: ${ref.field}: no ${ref.domain} ${ref.id}`, 'reference_unknown', 400);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
/** Declares a domain of `{ …, value }` records (objects or facets) whose values may hold `references`: `migrate`
|
|
524
|
+
* runs per value; the records' references are indexed now and on each change; `release` (also when `owner`
|
|
525
|
+
* disposes) forgets the domain and its references. */
|
|
526
|
+
const declareValues = async <R, T, E>(
|
|
527
|
+
fn: string,
|
|
528
|
+
owner: Context,
|
|
529
|
+
spec: ObjectsSpec<T>,
|
|
530
|
+
record: (value: z.ZodType) => z.ZodType,
|
|
531
|
+
registry: Map<string, E>,
|
|
532
|
+
entry: (domain: Store<R>, key: string) => E,
|
|
533
|
+
about: Pick<Catalogued, 'writers' | 'rules' | 'target'>,
|
|
534
|
+
) => {
|
|
535
|
+
const { name, schema, version, migrate, references } = spec;
|
|
536
|
+
const isPrivate = checkPrivate(fn, spec.private);
|
|
537
|
+
// Facet domains have no queries to order nor search.
|
|
538
|
+
const indexes = fn === 'objects' ? spec.indexes : undefined;
|
|
539
|
+
const declared = fn === 'objects' ? spec.search : undefined;
|
|
540
|
+
// A bare list, or `{ paths, index? }`.
|
|
541
|
+
const shaped = typeof declared === 'object' && declared !== null && !Array.isArray(declared);
|
|
542
|
+
const {
|
|
543
|
+
paths: search,
|
|
544
|
+
index: searchIndex,
|
|
545
|
+
...extra
|
|
546
|
+
} = (shaped ? declared : { paths: declared }) as { paths?: unknown; index?: unknown };
|
|
547
|
+
const isPath = (path: unknown) => {
|
|
548
|
+
try {
|
|
549
|
+
return typeof path === 'string' && checkPath(path, 2).length > 0;
|
|
550
|
+
} catch {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
if (indexes !== undefined && !(Array.isArray(indexes) && indexes.every(isPath))) {
|
|
555
|
+
throw new Error(
|
|
556
|
+
`storage.${fn}: indexes must list paths of value, one or two names [A-Za-z_][A-Za-z0-9_]* joined by a dot, got ${JSON.stringify(indexes)}`,
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
if (
|
|
560
|
+
shaped &&
|
|
561
|
+
((searchIndex !== undefined && searchIndex !== 'trigram') || Object.keys(extra).length > 0)
|
|
562
|
+
) {
|
|
563
|
+
throw coded(
|
|
564
|
+
`storage.${fn}: search takes a list of paths or { paths, index?: 'trigram' }, got ${JSON.stringify(declared)}`,
|
|
565
|
+
'invalid_query',
|
|
566
|
+
400,
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
if (declared !== undefined && !(Array.isArray(search) && search.every(isPath))) {
|
|
570
|
+
throw new Error(
|
|
571
|
+
`storage.${fn}: search must list paths of value, one or two names [A-Za-z_][A-Za-z0-9_]* joined by a dot, got ${JSON.stringify(search)}`,
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
if (
|
|
575
|
+
references !== undefined &&
|
|
576
|
+
(typeof references !== 'object' ||
|
|
577
|
+
references === null ||
|
|
578
|
+
Array.isArray(references) ||
|
|
579
|
+
Object.values(references).some((each) => typeof each !== 'string' || !domainId.test(each)))
|
|
580
|
+
) {
|
|
581
|
+
throw new Error(
|
|
582
|
+
`storage.${fn}: references must map fields to domain ids <plugin key>/<name>, got ${JSON.stringify(references)}`,
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
// `where` reads these names itself.
|
|
586
|
+
const reserved = (references ? Object.keys(references) : []).filter(
|
|
587
|
+
(each) => each === 'usedBy' || (fn === 'facets' && each === 'target'),
|
|
588
|
+
);
|
|
589
|
+
if (reserved.length > 0)
|
|
590
|
+
throw new Error(
|
|
591
|
+
`storage.${fn}: ${reserved.join(', ')} is reserved in where; give the reference field another name`,
|
|
592
|
+
);
|
|
593
|
+
const value = references ? z.intersection(schema, referencesSchema(references)) : schema;
|
|
594
|
+
const { domain, key, id } = await declare<R, Store<R>>(
|
|
595
|
+
fn,
|
|
596
|
+
owner,
|
|
597
|
+
name,
|
|
598
|
+
{
|
|
599
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: record(value) builds the schema of R
|
|
600
|
+
schema: record(value) as unknown as z.ZodType<R>,
|
|
601
|
+
version,
|
|
602
|
+
...(migrate && {
|
|
603
|
+
migrate: async (data: Record<string, unknown>, from: number) =>
|
|
604
|
+
Object.fromEntries(
|
|
605
|
+
await Promise.all(
|
|
606
|
+
Object.entries(data).map(
|
|
607
|
+
async ([k, each]): Promise<[string, { value: unknown }]> => [
|
|
608
|
+
k,
|
|
609
|
+
{
|
|
610
|
+
...(each as object),
|
|
611
|
+
value: await migrate((each as { value?: unknown } | null)?.value, from),
|
|
612
|
+
},
|
|
613
|
+
],
|
|
614
|
+
),
|
|
615
|
+
),
|
|
616
|
+
),
|
|
617
|
+
}),
|
|
618
|
+
},
|
|
619
|
+
(each) => domains.store(each),
|
|
620
|
+
);
|
|
621
|
+
const valueOf = (each: R | undefined) => (each as { value?: unknown } | undefined)?.value;
|
|
622
|
+
// Registered before any await: an owner disposed meanwhile releases it.
|
|
623
|
+
const held = entry(domain, key);
|
|
624
|
+
registry.set(id, held);
|
|
625
|
+
declaredRefs.set(id, references ?? {});
|
|
626
|
+
if (isPrivate) privateIds.add(id);
|
|
627
|
+
else privateIds.delete(id);
|
|
628
|
+
const described: Catalogued = {
|
|
629
|
+
id,
|
|
630
|
+
plugin: key,
|
|
631
|
+
name,
|
|
632
|
+
kind: fn === 'objects' ? 'objects' : 'facets',
|
|
633
|
+
version,
|
|
634
|
+
private: isPrivate,
|
|
635
|
+
...about,
|
|
636
|
+
references: Object.entries(references ?? {}).map(([field, to]) => ({ field, domain: to })),
|
|
637
|
+
...(fn === 'objects' && {
|
|
638
|
+
indexes: indexes ?? [],
|
|
639
|
+
search: { paths: (search ?? []) as string[], trigram: searchIndex === 'trigram' },
|
|
640
|
+
}),
|
|
641
|
+
count:
|
|
642
|
+
fn === 'objects'
|
|
643
|
+
? async () => {
|
|
644
|
+
const [total, archived] = await Promise.all([
|
|
645
|
+
domain.count(),
|
|
646
|
+
domain.count({ archived: true }),
|
|
647
|
+
]);
|
|
648
|
+
return { total, archived };
|
|
649
|
+
}
|
|
650
|
+
: async () => ({ total: await domain.count() }),
|
|
651
|
+
};
|
|
652
|
+
catalog.set(id, described);
|
|
653
|
+
let released = false;
|
|
654
|
+
const release = () => {
|
|
655
|
+
if (released) return;
|
|
656
|
+
released = true;
|
|
657
|
+
// A newer generation's entry and references are its own.
|
|
658
|
+
if (registry.get(id) === held) {
|
|
659
|
+
registry.delete(id);
|
|
660
|
+
declaredRefs.delete(id);
|
|
661
|
+
privateIds.delete(id);
|
|
662
|
+
index.release(id);
|
|
663
|
+
}
|
|
664
|
+
if (catalog.get(id) === described) catalog.delete(id);
|
|
665
|
+
domain.release();
|
|
666
|
+
};
|
|
667
|
+
owner.effect(() => release);
|
|
668
|
+
if (fn === 'objects') {
|
|
669
|
+
try {
|
|
670
|
+
if (indexes && indexes.length > 0)
|
|
671
|
+
await ctx.storageBackend.index(
|
|
672
|
+
id,
|
|
673
|
+
indexes.map((path) => `value.${path}`),
|
|
674
|
+
);
|
|
675
|
+
// Always: a declaration that drops `search` drops the domain's search text.
|
|
676
|
+
await ctx.storageBackend.searchable(
|
|
677
|
+
id,
|
|
678
|
+
((search ?? []) as string[]).map((path) => `value.${path}`),
|
|
679
|
+
{ trigram: searchIndex === 'trigram' },
|
|
680
|
+
);
|
|
681
|
+
} catch (cause) {
|
|
682
|
+
release();
|
|
683
|
+
throw cause;
|
|
684
|
+
}
|
|
685
|
+
if (released) throw new Error(`storage.${fn}: ${key} was disposed while declaring ${name}`);
|
|
686
|
+
}
|
|
687
|
+
if (references) {
|
|
688
|
+
// Built once from the stored entries, then kept by each change; a key changed during the list keeps its change.
|
|
689
|
+
const changed = new Set<string>();
|
|
690
|
+
domain.subscribe((each, stored) => {
|
|
691
|
+
changed.add(each);
|
|
692
|
+
index.set(id, each, stored === undefined ? [] : referencesIn(valueOf(stored), references));
|
|
693
|
+
});
|
|
694
|
+
let stored;
|
|
695
|
+
try {
|
|
696
|
+
stored = await domain.list();
|
|
697
|
+
} catch (cause) {
|
|
698
|
+
release();
|
|
699
|
+
throw cause;
|
|
700
|
+
}
|
|
701
|
+
// Released meanwhile: its references are not indexed (a newer generation's are its own).
|
|
702
|
+
if (released) throw new Error(`storage.${fn}: ${key} was disposed while declaring ${name}`);
|
|
703
|
+
for (const each of stored)
|
|
704
|
+
if (!changed.has(each.key))
|
|
705
|
+
index.set(id, each.key, referencesIn(valueOf(each.value), references));
|
|
706
|
+
}
|
|
707
|
+
return { domain, key, id, release, check: checkReferences(id, references), isPrivate, value };
|
|
708
|
+
};
|
|
709
|
+
const invalid = (message: string) => coded(message, 'invalid_query', 400);
|
|
710
|
+
const isPlain = (value: unknown): value is Record<string, unknown> =>
|
|
711
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
712
|
+
const both = (a: Where | undefined, b: Where | undefined): Where | undefined =>
|
|
713
|
+
a && b ? { AND: [a, b] } : (a ?? b);
|
|
714
|
+
/** The relation fields of domain `id`: each declared reference field, its key at `value.<field>.id` (`holder` the
|
|
715
|
+
* field), and a facet domain's `target`. */
|
|
716
|
+
const relationsOf = (id: string) => {
|
|
717
|
+
const out: Record<string, { domain: string; path: string; holder: string }> = {};
|
|
718
|
+
for (const [field, domain] of Object.entries(declaredRefs.get(id) ?? {}))
|
|
719
|
+
out[field] = { domain, path: `value.${field}.id`, holder: `value.${field}` };
|
|
720
|
+
const facets = facetDomains.get(id);
|
|
721
|
+
if (facets) out.target = { domain: facets.target, path: 'target', holder: 'target' };
|
|
722
|
+
return out;
|
|
723
|
+
};
|
|
724
|
+
/** Nodes neither archived nor under an archived node. Live, everywhere: a node of these; an object not archived
|
|
725
|
+
* on one of these; a facet whose target is live (as `resolve` says `live`). */
|
|
726
|
+
const liveNodes = () =>
|
|
727
|
+
nodes
|
|
728
|
+
.list()
|
|
729
|
+
.map((each) => each.id)
|
|
730
|
+
.filter((each) => !inArchivedNode(nodes, each));
|
|
731
|
+
/** The entries of domain `id` that are live. */
|
|
732
|
+
const live = (id: string): Filter => {
|
|
733
|
+
const facets = facetDomains.get(id);
|
|
734
|
+
if (!facets) return { archived: false, nodes: liveNodes() };
|
|
735
|
+
if (facets.target === nodeDomain) return { where: { target: { in: liveNodes() } } };
|
|
736
|
+
return {
|
|
737
|
+
where: {
|
|
738
|
+
$ref: {
|
|
739
|
+
domain: facets.target,
|
|
740
|
+
path: 'target',
|
|
741
|
+
mode: 'is',
|
|
742
|
+
archived: false,
|
|
743
|
+
nodes: liveNodes(),
|
|
744
|
+
},
|
|
745
|
+
},
|
|
746
|
+
};
|
|
747
|
+
};
|
|
748
|
+
/** Refuses a relation to a domain not declared in this worker, as `read` does. */
|
|
749
|
+
const ownerMissing = (domain: string) => {
|
|
750
|
+
if (domain !== nodeDomain && !objectDomains.has(domain))
|
|
751
|
+
throw coded(
|
|
752
|
+
`${domain} is not declared in this workspace (its plugin is not loaded)`,
|
|
753
|
+
'owner_missing',
|
|
754
|
+
404,
|
|
755
|
+
);
|
|
756
|
+
};
|
|
757
|
+
/** `<domain id>.<field>` naming a declared reference field of a declared domain to `id`: that domain and key path. */
|
|
758
|
+
const referrer = (name: string, id: string, what: string) => {
|
|
759
|
+
const dot = name.lastIndexOf('.');
|
|
760
|
+
const domain = name.slice(0, dot);
|
|
761
|
+
const field = name.slice(dot + 1);
|
|
762
|
+
// A facet's target: an implicit reference, its key the bare id at `target`.
|
|
763
|
+
if (field === 'target' && facetDomains.get(domain)?.target === id)
|
|
764
|
+
return { domain, path: 'target' };
|
|
765
|
+
const refs = declaredRefs.get(domain);
|
|
766
|
+
if (dot < 0 || !refs || !Object.hasOwn(refs, field) || refs[field] !== id)
|
|
767
|
+
throw invalid(
|
|
768
|
+
`${what} ${JSON.stringify(name)}: not <domain id>.<field> of a declared domain whose field references ${id}`,
|
|
769
|
+
);
|
|
770
|
+
return { domain, path: `value.${field}.id` };
|
|
771
|
+
};
|
|
772
|
+
/** `where` on domain `id` as the backend reads it: plain paths under `value.`, relation keys and `usedBy` resolved
|
|
773
|
+
* to `$ref` and `$usedBy` against live entries, recursively through the targets' own references; none into another
|
|
774
|
+
* plugin's private domain than `from`'s, the domain queried first (`private_domain`). */
|
|
775
|
+
const resolveWhere = (where: unknown, id: string, from = id): Where => {
|
|
776
|
+
if (!isPlain(where)) throw invalid(`a where must be an object, got ${JSON.stringify(where)}`);
|
|
777
|
+
const relations = relationsOf(id);
|
|
778
|
+
const out: Record<string, unknown> = {};
|
|
779
|
+
const and: Where[] = [];
|
|
780
|
+
for (const [key, condition] of Object.entries(where)) {
|
|
781
|
+
if (key === 'AND' || key === 'NOT')
|
|
782
|
+
out[key] = Array.isArray(condition)
|
|
783
|
+
? condition.map((each) => resolveWhere(each, id, from))
|
|
784
|
+
: resolveWhere(condition, id, from);
|
|
785
|
+
else if (key === 'OR') {
|
|
786
|
+
if (!Array.isArray(condition)) throw invalid('OR takes a list of wheres');
|
|
787
|
+
out[key] = condition.map((each) => resolveWhere(each, id, from));
|
|
788
|
+
} else if (key === 'usedBy') {
|
|
789
|
+
if (!isPlain(condition))
|
|
790
|
+
throw invalid(
|
|
791
|
+
`usedBy takes { '<domain id>.<field>': { some: where } | { none: where } }`,
|
|
792
|
+
);
|
|
793
|
+
for (const [name, how] of Object.entries(condition)) {
|
|
794
|
+
const { domain, path } = referrer(name, id, 'usedBy');
|
|
795
|
+
notPrivate(domain, from, `usedBy ${JSON.stringify(name)}`);
|
|
796
|
+
const [mode, inner] =
|
|
797
|
+
isPlain(how) && Object.keys(how).length === 1 ? Object.entries(how)[0]! : [];
|
|
798
|
+
if (mode !== 'some' && mode !== 'none')
|
|
799
|
+
throw invalid(
|
|
800
|
+
`usedBy ${JSON.stringify(name)} takes { some: where } or { none: where }`,
|
|
801
|
+
);
|
|
802
|
+
const { where: liveWhere, ...rest } = live(domain);
|
|
803
|
+
const referrers = both(resolveWhere(inner, domain, from), liveWhere)!;
|
|
804
|
+
and.push({ $usedBy: { domain, path, mode, ...rest, where: referrers } });
|
|
805
|
+
}
|
|
806
|
+
} else if (Object.hasOwn(relations, key)) {
|
|
807
|
+
const { domain, path } = relations[key]!;
|
|
808
|
+
const [mode, inner] =
|
|
809
|
+
condition === null
|
|
810
|
+
? ['isNot', {}]
|
|
811
|
+
: isPlain(condition) && Object.keys(condition).length === 1
|
|
812
|
+
? Object.entries(condition)[0]!
|
|
813
|
+
: [];
|
|
814
|
+
const relational = ['is', 'isNot', 'some', 'every', 'none'].includes(mode as string);
|
|
815
|
+
// A facet's `target` is the bare target id: operators (`equals`, `in`, …) apply to it.
|
|
816
|
+
if (path === 'target' && !relational) {
|
|
817
|
+
out.target = condition;
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
if (path === 'target' && domain === nodeDomain)
|
|
821
|
+
throw invalid(`target is a node id: filter it with operators such as equals or in`);
|
|
822
|
+
if (!relational)
|
|
823
|
+
throw invalid(
|
|
824
|
+
`${key} is a reference: give null or { is | isNot | some | every | none: where }`,
|
|
825
|
+
);
|
|
826
|
+
if (domain === nodeDomain)
|
|
827
|
+
throw invalid(`${key} references nodes: filter on '${key}.id' instead`);
|
|
828
|
+
ownerMissing(domain);
|
|
829
|
+
notPrivate(domain, from, key);
|
|
830
|
+
and.push({
|
|
831
|
+
$ref: {
|
|
832
|
+
domain,
|
|
833
|
+
path,
|
|
834
|
+
mode: mode as 'is',
|
|
835
|
+
archived: false,
|
|
836
|
+
nodes: liveNodes(),
|
|
837
|
+
where: resolveWhere(inner, domain, from),
|
|
838
|
+
},
|
|
839
|
+
});
|
|
840
|
+
} else {
|
|
841
|
+
checkPath(key, 2);
|
|
842
|
+
out[`value.${key}`] = condition;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (and.length > 0)
|
|
846
|
+
out.AND = [...(out.AND === undefined ? [] : ([out.AND].flat() as Where[])), ...and];
|
|
847
|
+
return out as Where;
|
|
848
|
+
};
|
|
849
|
+
/** The live objects (or nodes) of domain `domain` among `keys`, by key: one backend call. An undeclared domain has none. */
|
|
850
|
+
const liveTargets = async (domain: string, keys: string[]): Promise<Map<string, unknown>> => {
|
|
851
|
+
if (keys.length === 0) return new Map();
|
|
852
|
+
if (domain === nodeDomain)
|
|
853
|
+
return new Map(
|
|
854
|
+
keys.flatMap((each) => {
|
|
855
|
+
const node = nodes.ancestors(each)?.[0];
|
|
856
|
+
return node && !inArchivedNode(nodes, each) ? [[each, structuredClone(node)]] : [];
|
|
857
|
+
}),
|
|
858
|
+
);
|
|
859
|
+
const objects = objectDomains.get(domain);
|
|
860
|
+
if (!objects) return new Map();
|
|
861
|
+
return new Map(
|
|
862
|
+
(await objects.list({ keys, archived: false, nodes: liveNodes() })).map(({ key, value }) => [
|
|
863
|
+
key,
|
|
864
|
+
view(value),
|
|
865
|
+
]),
|
|
866
|
+
);
|
|
867
|
+
};
|
|
868
|
+
/** `spec` checked for domain `id`: what loads it for a page of entries, one backend call per term. */
|
|
869
|
+
const withOf = (id: string, spec: unknown) => {
|
|
870
|
+
if (!isPlain(spec)) throw invalid(`with must be an object, got ${JSON.stringify(spec)}`);
|
|
871
|
+
const relations = relationsOf(id);
|
|
872
|
+
const steps: ((
|
|
873
|
+
entries: { key: string; value: unknown }[],
|
|
874
|
+
extras: Related[],
|
|
875
|
+
) => Promise<void>)[] = [];
|
|
876
|
+
for (const [name, wanted] of Object.entries(spec)) {
|
|
877
|
+
if (name === 'facets') {
|
|
878
|
+
if (facetDomains.has(id)) throw invalid('with.facets: object domains only');
|
|
879
|
+
if (!(Array.isArray(wanted) && wanted.every((each) => typeof each === 'string')))
|
|
880
|
+
throw invalid('with.facets takes a list of facet domain ids');
|
|
881
|
+
// An undeclared one (its plugin not loaded) is left out; a declared one must attach to this domain.
|
|
882
|
+
for (const facetId of wanted as string[]) {
|
|
883
|
+
const target = facetDomains.get(facetId)?.target;
|
|
884
|
+
if (target !== undefined && target !== id)
|
|
885
|
+
throw invalid(`with.facets: ${facetId} attaches to ${target}, not ${id}`);
|
|
886
|
+
notPrivate(facetId, id, 'with.facets');
|
|
887
|
+
}
|
|
888
|
+
steps.push(async (entries, extras) => {
|
|
889
|
+
for (const extra of extras) extra.facets = {};
|
|
890
|
+
await Promise.all(
|
|
891
|
+
[...new Set(wanted as string[])].map(async (facetId) => {
|
|
892
|
+
const facets = facetDomains.get(facetId);
|
|
893
|
+
if (!facets || facets.target !== id || entries.length === 0) return;
|
|
894
|
+
const found = new Map(
|
|
895
|
+
(await facets.domain.list({ keys: entries.map(({ key }) => key) })).map(
|
|
896
|
+
({ key, value }) => [key, value.value],
|
|
897
|
+
),
|
|
898
|
+
);
|
|
899
|
+
// AD-25: kept, not shown, where its plugin is not active.
|
|
900
|
+
entries.forEach(({ key, value }, i) => {
|
|
901
|
+
if (
|
|
902
|
+
found.has(key) &&
|
|
903
|
+
nodes.isActive(facets.key, (value as StoredObject<unknown>).node)
|
|
904
|
+
)
|
|
905
|
+
extras[i]!.facets![facetId] = found.get(key);
|
|
906
|
+
});
|
|
907
|
+
}),
|
|
908
|
+
);
|
|
909
|
+
});
|
|
910
|
+
} else if (name === '_count') {
|
|
911
|
+
if (!isPlain(wanted)) throw invalid(`with._count takes { '<domain id>.<field>': true }`);
|
|
912
|
+
for (const [key, on] of Object.entries(wanted)) {
|
|
913
|
+
if (typeof on !== 'boolean')
|
|
914
|
+
throw invalid(`with._count ${JSON.stringify(key)} takes true or false`);
|
|
915
|
+
const { domain, path } = referrer(key, id, 'with._count');
|
|
916
|
+
notPrivate(domain, id, `with._count ${JSON.stringify(key)}`);
|
|
917
|
+
if (!on) continue;
|
|
918
|
+
steps.push(async (entries, extras) => {
|
|
919
|
+
const counts =
|
|
920
|
+
entries.length === 0
|
|
921
|
+
? {}
|
|
922
|
+
: await ctx.storageBackend.countBy(
|
|
923
|
+
domain,
|
|
924
|
+
path,
|
|
925
|
+
entries.map(({ key: each }) => each),
|
|
926
|
+
live(domain),
|
|
927
|
+
);
|
|
928
|
+
entries.forEach(({ key: each }, i) => {
|
|
929
|
+
(extras[i]!.counts ??= {})[key] = counts[each] ?? 0;
|
|
930
|
+
});
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
} else {
|
|
934
|
+
if (!Object.hasOwn(relations, name))
|
|
935
|
+
throw invalid(`with.${name}: not a reference field of ${id}`);
|
|
936
|
+
if (typeof wanted !== 'boolean') throw invalid(`with.${name} takes true or false`);
|
|
937
|
+
if (!wanted) continue;
|
|
938
|
+
const { domain, path, holder } = relations[name]!;
|
|
939
|
+
ownerMissing(domain);
|
|
940
|
+
notPrivate(domain, id, `with.${name}`);
|
|
941
|
+
steps.push(async (entries, extras) => {
|
|
942
|
+
const found = await liveTargets(domain, [
|
|
943
|
+
...new Set(entries.flatMap(({ value }) => keysAt(value, path))),
|
|
944
|
+
]);
|
|
945
|
+
entries.forEach(({ value }, i) => {
|
|
946
|
+
const keys = keysAt(value, path);
|
|
947
|
+
(extras[i]!.related ??= {})[name] = Array.isArray(pick(value, holder))
|
|
948
|
+
? keys.flatMap((each) => (found.has(each) ? [found.get(each)] : []))
|
|
949
|
+
: (keys[0] !== undefined && found.get(keys[0])) || null;
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return async (entries: { key: string; value: unknown }[]) => {
|
|
955
|
+
const extras: Related[] = entries.map(() => ({}));
|
|
956
|
+
await Promise.all(steps.map((step) => step(entries, extras)));
|
|
957
|
+
return extras;
|
|
958
|
+
};
|
|
959
|
+
};
|
|
960
|
+
/** Reads of `id`, whatever it turns out to be at each call (a reader may be taken before its domain or log is
|
|
961
|
+
* declared): `owner_missing` (404) while it is not declared, `private_domain` (403) while it is private,
|
|
962
|
+
* `invalid_query` (400) for a method its kind lacks (a facet domain has no `groupBy`, a log only `read`, `last` and
|
|
963
|
+
* `streams`). */
|
|
964
|
+
const reader = (id: string): ObjectReader<unknown> & FacetReader<unknown> & LogReader => {
|
|
965
|
+
const call =
|
|
966
|
+
(fn: 'get' | 'query' | 'page' | 'count' | 'groupBy' | 'read' | 'last' | 'streams') =>
|
|
967
|
+
(...args: unknown[]) => {
|
|
968
|
+
const log = logDomains.get(id);
|
|
969
|
+
const reads =
|
|
970
|
+
log ?? (objectDomains.has(id) || facetDomains.has(id) ? readers.get(id) : undefined);
|
|
971
|
+
if (!reads)
|
|
972
|
+
return Promise.reject(
|
|
973
|
+
coded(
|
|
974
|
+
`storage.read: ${id} is not declared in this workspace (its plugin is not loaded)`,
|
|
975
|
+
'owner_missing',
|
|
976
|
+
404,
|
|
977
|
+
),
|
|
978
|
+
);
|
|
979
|
+
if (privateIds.has(id)) return Promise.reject(privateRead(id));
|
|
980
|
+
const kind = log ? 'a log' : facetDomains.has(id) ? 'a facet domain' : 'an object domain';
|
|
981
|
+
const has = log
|
|
982
|
+
? ['read', 'last', 'streams'].includes(fn)
|
|
983
|
+
: fn !== 'read' &&
|
|
984
|
+
fn !== 'last' &&
|
|
985
|
+
fn !== 'streams' &&
|
|
986
|
+
!(fn === 'groupBy' && facetDomains.has(id));
|
|
987
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the readers indexed by method name, checked present below
|
|
988
|
+
const byName = reads as unknown as Record<
|
|
989
|
+
string,
|
|
990
|
+
((...each: unknown[]) => Promise<unknown>) | undefined
|
|
991
|
+
>;
|
|
992
|
+
const read = has ? byName[fn] : undefined;
|
|
993
|
+
if (!read)
|
|
994
|
+
return Promise.reject(
|
|
995
|
+
coded(`storage.read: ${id} is ${kind}, which has no ${fn}`, 'invalid_query', 400),
|
|
996
|
+
);
|
|
997
|
+
return read(...args);
|
|
998
|
+
};
|
|
999
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the reader assembled by name above serves all three kinds
|
|
1000
|
+
return {
|
|
1001
|
+
get: call('get'),
|
|
1002
|
+
query: call('query'),
|
|
1003
|
+
page: call('page'),
|
|
1004
|
+
count: call('count'),
|
|
1005
|
+
groupBy: call('groupBy'),
|
|
1006
|
+
read: call('read'),
|
|
1007
|
+
last: call('last'),
|
|
1008
|
+
streams: call('streams'),
|
|
1009
|
+
} as unknown as ObjectReader<unknown> & FacetReader<unknown> & LogReader;
|
|
1010
|
+
};
|
|
1011
|
+
/** Writes to object domain `id` by any plugin, resolved at each call (research-052 D3): the domain's current handle
|
|
1012
|
+
* when `caller` owns it, or it is declared, not private, and open; refused otherwise, as `StorageService.write`
|
|
1013
|
+
* says. */
|
|
1014
|
+
const writer = (id: string): ObjectWriter<unknown> => {
|
|
1015
|
+
const handle = (fn: string, caller: Context) => {
|
|
1016
|
+
const kind = facetDomains.has(id) ? 'a facet domain' : logDomains.has(id) ? 'a log' : '';
|
|
1017
|
+
if (kind)
|
|
1018
|
+
throw coded(
|
|
1019
|
+
`storage.write: ${id} is ${kind}: its owner writes it only; write(id) serves an object domain its owner opens`,
|
|
1020
|
+
'not_open',
|
|
1021
|
+
403,
|
|
1022
|
+
);
|
|
1023
|
+
const entry = writable.get(id);
|
|
1024
|
+
if (!entry || objectDomains.get(id) !== entry.store)
|
|
1025
|
+
throw coded(
|
|
1026
|
+
`storage.write: ${id} is not declared in this workspace (its plugin is not loaded)`,
|
|
1027
|
+
'owner_missing',
|
|
1028
|
+
404,
|
|
1029
|
+
);
|
|
1030
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the kernel sets the plugin key symbol on every plugin context
|
|
1031
|
+
if ((caller as unknown as Record<symbol, unknown> | null)?.[pluginKey] === keyOf(id))
|
|
1032
|
+
return entry.domain;
|
|
1033
|
+
if (privateIds.has(id))
|
|
1034
|
+
throw coded(
|
|
1035
|
+
`storage.write: ${id} is private to plugin ${keyOf(id)}; ask it through its service`,
|
|
1036
|
+
'private_domain',
|
|
1037
|
+
403,
|
|
1038
|
+
);
|
|
1039
|
+
if (!entry.open)
|
|
1040
|
+
throw coded(
|
|
1041
|
+
`storage.write: ${id}.${fn}: plugin ${keyOf(id)} does not open this domain to other writers (writers: 'owner')`,
|
|
1042
|
+
'not_open',
|
|
1043
|
+
403,
|
|
1044
|
+
);
|
|
1045
|
+
return entry.domain;
|
|
1046
|
+
};
|
|
1047
|
+
const reads = reader(id);
|
|
1048
|
+
return {
|
|
1049
|
+
create: async (caller, value, options) =>
|
|
1050
|
+
handle('create', caller).create(caller, value, options),
|
|
1051
|
+
update: async (caller, objectId, value, options) =>
|
|
1052
|
+
handle('update', caller).update(caller, objectId, value, options),
|
|
1053
|
+
delete: async (caller, objectId, options) =>
|
|
1054
|
+
handle('delete', caller).delete(caller, objectId, options),
|
|
1055
|
+
restore: async (caller, objectId, options) =>
|
|
1056
|
+
handle('restore', caller).restore(caller, objectId, options),
|
|
1057
|
+
move: async (caller, objectId, node, options) =>
|
|
1058
|
+
handle('move', caller).move(caller, objectId, node, options),
|
|
1059
|
+
get: reads.get,
|
|
1060
|
+
query: reads.query,
|
|
1061
|
+
page: reads.page,
|
|
1062
|
+
count: reads.count,
|
|
1063
|
+
groupBy: reads.groupBy,
|
|
1064
|
+
};
|
|
1065
|
+
};
|
|
1066
|
+
/** The error refusing another plugin's read of private `id`. */
|
|
1067
|
+
const privateRead = (id: string) =>
|
|
1068
|
+
coded(
|
|
1069
|
+
`storage.read: ${id} is private to plugin ${keyOf(id)}; ask it through its service`,
|
|
1070
|
+
'private_domain',
|
|
1071
|
+
403,
|
|
1072
|
+
);
|
|
1073
|
+
/** Backend errors of a log as a route answers them: `invalid_query` 400, `log_conflict` 409, an event failing the
|
|
1074
|
+
* schema or a plain error coded `invalid_value` 400. */
|
|
1075
|
+
const logErrors = async <R>(id: string, run: () => Promise<R>) => {
|
|
1076
|
+
try {
|
|
1077
|
+
return await run();
|
|
1078
|
+
} catch (cause) {
|
|
1079
|
+
const { code, issues, last, message } = (cause ?? {}) as {
|
|
1080
|
+
code?: unknown;
|
|
1081
|
+
issues?: { path: PropertyKey[]; message: string }[];
|
|
1082
|
+
last?: unknown;
|
|
1083
|
+
message?: string;
|
|
1084
|
+
};
|
|
1085
|
+
if (Array.isArray(issues))
|
|
1086
|
+
throw Object.assign(
|
|
1087
|
+
coded(
|
|
1088
|
+
`${id}: invalid event: ${issues.map(({ path, message: text }) => `${path.map(String).join('.')}: ${text}`).join('; ')}`,
|
|
1089
|
+
'invalid_value',
|
|
1090
|
+
400,
|
|
1091
|
+
),
|
|
1092
|
+
{ cause },
|
|
1093
|
+
);
|
|
1094
|
+
if (code === 'invalid_query' || code === 'invalid_value')
|
|
1095
|
+
throw Object.assign(coded(`${id}: ${message}`, code, 400), { cause });
|
|
1096
|
+
if (code === 'log_conflict')
|
|
1097
|
+
throw Object.assign(coded(`${id}: ${message}`, 'log_conflict', 409), { cause, last });
|
|
1098
|
+
if (code === 'blobs_unavailable')
|
|
1099
|
+
throw Object.assign(coded(`${id}: ${message}`, 'blobs_unavailable', 409), { cause });
|
|
1100
|
+
throw cause;
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
1103
|
+
/** The audit meta of a plain domain's change by `plugin` (`set` of `value`, or `delete`), the actor read now: given
|
|
1104
|
+
* the value it replaces when written, so `fields` diff against what the write overwrites. */
|
|
1105
|
+
const plainAudit = (plugin: string, op: 'set' | 'delete', value?: unknown) => {
|
|
1106
|
+
const actor = actorNow();
|
|
1107
|
+
return (before: unknown): AuditMeta => ({
|
|
1108
|
+
op,
|
|
1109
|
+
fields: op === 'set' ? changedFields(before, value) : null,
|
|
1110
|
+
plugin,
|
|
1111
|
+
actor,
|
|
1112
|
+
});
|
|
1113
|
+
};
|
|
1114
|
+
/** A backend audit call, its errors as a route answers them. */
|
|
1115
|
+
const auditCall = async <R>(run: () => Promise<R>) => {
|
|
1116
|
+
try {
|
|
1117
|
+
return await run();
|
|
1118
|
+
} catch (cause) {
|
|
1119
|
+
const { code, message } = (cause ?? {}) as { code?: unknown; message?: string };
|
|
1120
|
+
if (code === 'audit_unavailable')
|
|
1121
|
+
throw Object.assign(coded(`storage.audit: ${message}`, 'audit_unavailable', 409), {
|
|
1122
|
+
cause,
|
|
1123
|
+
});
|
|
1124
|
+
if (code === 'invalid_query')
|
|
1125
|
+
throw Object.assign(coded(`storage.audit: ${message}`, 'invalid_query', 400), { cause });
|
|
1126
|
+
throw cause;
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1129
|
+
const readShared = async (name: string, key: string) => {
|
|
1130
|
+
checkKey('read', key);
|
|
1131
|
+
return structuredClone((await sharedDomain('read', name)).get(key));
|
|
1132
|
+
};
|
|
1133
|
+
const writeShared = async (name: string, key: string, value: unknown) => {
|
|
1134
|
+
checkKey('write', key);
|
|
1135
|
+
if (value === undefined)
|
|
1136
|
+
throw new Error(
|
|
1137
|
+
`storage.write(${JSON.stringify(name)}, ${JSON.stringify(key)}, undefined): use remove()`,
|
|
1138
|
+
);
|
|
1139
|
+
const audit = plainAudit('storage', 'set', value);
|
|
1140
|
+
await (await sharedDomain('write', name)).set(key, value as z.core.util.JSONType, audit);
|
|
1141
|
+
};
|
|
1142
|
+
const storage: StorageService = {
|
|
1143
|
+
async domain(owner, spec) {
|
|
1144
|
+
const { name, ...rest } = spec;
|
|
1145
|
+
const { domain, key, id } = await declare('domain', owner, name, rest, (each) =>
|
|
1146
|
+
domains.domain(each),
|
|
1147
|
+
);
|
|
1148
|
+
const described: Catalogued = {
|
|
1149
|
+
id,
|
|
1150
|
+
plugin: key,
|
|
1151
|
+
name,
|
|
1152
|
+
kind: 'plain',
|
|
1153
|
+
version: rest.version,
|
|
1154
|
+
private: false,
|
|
1155
|
+
references: [],
|
|
1156
|
+
count: async () => ({ total: domain.keys().length }),
|
|
1157
|
+
};
|
|
1158
|
+
catalog.set(id, described);
|
|
1159
|
+
owner.effect(() => () => {
|
|
1160
|
+
if (catalog.get(id) === described) catalog.delete(id);
|
|
1161
|
+
});
|
|
1162
|
+
// Its writes take no caller: the plugin is the declaring one, the actor the running code's.
|
|
1163
|
+
return {
|
|
1164
|
+
...domain,
|
|
1165
|
+
set: (each, value) => domain.set(each, value, plainAudit(key, 'set', value)),
|
|
1166
|
+
delete: (each) => domain.delete(each, plainAudit(key, 'delete')),
|
|
1167
|
+
};
|
|
1168
|
+
},
|
|
1169
|
+
async objects<T>(owner: Context, spec: ObjectsSpec<T>) {
|
|
1170
|
+
const { rules = {}, writers = 'owner' } = spec;
|
|
1171
|
+
if (writers !== 'owner' && writers !== 'workspace')
|
|
1172
|
+
throw new Error(
|
|
1173
|
+
`storage.objects: writers must be 'owner' or 'workspace', got ${JSON.stringify(writers)}`,
|
|
1174
|
+
);
|
|
1175
|
+
// A writer that cannot read the domain it writes could not see what its writes did.
|
|
1176
|
+
if (spec.private === true && writers === 'workspace')
|
|
1177
|
+
throw new Error(
|
|
1178
|
+
`storage.objects: a private domain is written by its owner only: give private or writers: 'workspace', not both`,
|
|
1179
|
+
);
|
|
1180
|
+
if (
|
|
1181
|
+
!isPlain(rules) ||
|
|
1182
|
+
Object.entries(rules).some(
|
|
1183
|
+
([name, rule]) =>
|
|
1184
|
+
!(ruleNames as readonly string[]).includes(name) || typeof rule !== 'function',
|
|
1185
|
+
)
|
|
1186
|
+
)
|
|
1187
|
+
throw new Error(
|
|
1188
|
+
`storage.objects: rules takes { ${ruleNames.join(', ')} }, each a function, got ${isPlain(rules) ? Object.keys(rules).join(', ') : typeof rules}`,
|
|
1189
|
+
);
|
|
1190
|
+
const { domain, key, id, release, check, isPrivate, value } = await declareValues(
|
|
1191
|
+
'objects',
|
|
1192
|
+
owner,
|
|
1193
|
+
spec,
|
|
1194
|
+
(value) =>
|
|
1195
|
+
z.strictObject({
|
|
1196
|
+
id: z.uuid(),
|
|
1197
|
+
node: z.string().min(1),
|
|
1198
|
+
value,
|
|
1199
|
+
archived: z.boolean().optional(),
|
|
1200
|
+
// research-021 D4, B12: absent on objects stored before revisions.
|
|
1201
|
+
rev: z.number().int().nonnegative().optional(),
|
|
1202
|
+
receipts: z.array(z.string()).max(8).optional(),
|
|
1203
|
+
}),
|
|
1204
|
+
objectDomains,
|
|
1205
|
+
(each: Store<StoredObject<T>>) => each as Store<StoredObject<unknown>>,
|
|
1206
|
+
{ writers, rules: ruleNames.filter((each) => Object.hasOwn(rules, each)) },
|
|
1207
|
+
);
|
|
1208
|
+
const objects = objectDomain(
|
|
1209
|
+
nodes,
|
|
1210
|
+
domain,
|
|
1211
|
+
key,
|
|
1212
|
+
id,
|
|
1213
|
+
{
|
|
1214
|
+
searchable:
|
|
1215
|
+
((Array.isArray(spec.search) ? spec.search : spec.search?.paths)?.length ?? 0) > 0,
|
|
1216
|
+
references: Object.keys(spec.references ?? {}),
|
|
1217
|
+
where: (where) => resolveWhere(where, id),
|
|
1218
|
+
with: (each) => withOf(id, each),
|
|
1219
|
+
check,
|
|
1220
|
+
facets: (objectId, node) => facetsOf(id, objectId, node),
|
|
1221
|
+
usedBy: (objectId) =>
|
|
1222
|
+
index
|
|
1223
|
+
.usedBy({ domain: id, id: objectId })
|
|
1224
|
+
.filter(
|
|
1225
|
+
(each) =>
|
|
1226
|
+
!(
|
|
1227
|
+
each.id === objectId &&
|
|
1228
|
+
(each.domain === id || facetDomains.get(each.domain)?.target === id)
|
|
1229
|
+
),
|
|
1230
|
+
)
|
|
1231
|
+
.map((each) => `${each.domain} ${each.id}`),
|
|
1232
|
+
// Storage removes them as the mechanism's owner; facets in undeclared facet domains stay.
|
|
1233
|
+
// ponytail: an undeclared facet domain (its plugin not loaded) keeps its facet on the purged id; swept when declared if it matters.
|
|
1234
|
+
facetDomains: () =>
|
|
1235
|
+
[...facetDomains]
|
|
1236
|
+
.filter(([, facets]) => facets.target === id)
|
|
1237
|
+
.map(([facetId]) => facetId),
|
|
1238
|
+
facetsOn: async (objectId) =>
|
|
1239
|
+
(
|
|
1240
|
+
await Promise.all(
|
|
1241
|
+
[...facetDomains]
|
|
1242
|
+
.filter(([, facets]) => facets.target === id)
|
|
1243
|
+
.map(async ([facetId, facets]) =>
|
|
1244
|
+
(await facets.domain.get(objectId)) ? [facetId] : [],
|
|
1245
|
+
),
|
|
1246
|
+
)
|
|
1247
|
+
).flat(),
|
|
1248
|
+
write: (changes) => domains.write(changes),
|
|
1249
|
+
private: isPrivate,
|
|
1250
|
+
actor: actorNow,
|
|
1251
|
+
parse: (each) => value.parse(each),
|
|
1252
|
+
},
|
|
1253
|
+
release,
|
|
1254
|
+
rules,
|
|
1255
|
+
writers === 'workspace',
|
|
1256
|
+
);
|
|
1257
|
+
readers.set(id, objects as ObjectReader<unknown>);
|
|
1258
|
+
writable.set(id, {
|
|
1259
|
+
store: domain as Store<StoredObject<unknown>>,
|
|
1260
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: write(id) hands values to the domain, whose schema parses them
|
|
1261
|
+
domain: objects as unknown as ObjectDomain<unknown>,
|
|
1262
|
+
open: writers === 'workspace',
|
|
1263
|
+
});
|
|
1264
|
+
return objects;
|
|
1265
|
+
},
|
|
1266
|
+
async facets<T>(owner: Context, spec: FacetsSpec<T>) {
|
|
1267
|
+
const { target } = spec;
|
|
1268
|
+
// research-052 D5: object domains only, this round.
|
|
1269
|
+
if ((spec as { rules?: unknown }).rules !== undefined)
|
|
1270
|
+
throw new Error('storage.facets: rules are for object domains only');
|
|
1271
|
+
if (typeof target !== 'string' || !domainId.test(target))
|
|
1272
|
+
throw new Error(
|
|
1273
|
+
`storage.facets: target must be an object domain id <plugin key>/<name> or ${nodeDomain}, got ${JSON.stringify(target)}`,
|
|
1274
|
+
);
|
|
1275
|
+
const { domain, key, id, release, check, isPrivate } = await declareValues(
|
|
1276
|
+
'facets',
|
|
1277
|
+
owner,
|
|
1278
|
+
spec,
|
|
1279
|
+
(value) =>
|
|
1280
|
+
z.strictObject({
|
|
1281
|
+
target: z.string().min(1),
|
|
1282
|
+
value,
|
|
1283
|
+
rev: z.number().int().nonnegative().optional(),
|
|
1284
|
+
}),
|
|
1285
|
+
facetDomains,
|
|
1286
|
+
(each: Store<StoredFacet<T>>, key) => ({
|
|
1287
|
+
target,
|
|
1288
|
+
key,
|
|
1289
|
+
domain: each as Store<StoredFacet<unknown>>,
|
|
1290
|
+
}),
|
|
1291
|
+
{ target },
|
|
1292
|
+
);
|
|
1293
|
+
/** The target's node: the node itself for `bridge/node`. */
|
|
1294
|
+
const nodeOf = async (targetId: string) =>
|
|
1295
|
+
target === nodeDomain ? targetId : (await objectDomains.get(target)?.get(targetId))?.node;
|
|
1296
|
+
const facets = facetDomain(
|
|
1297
|
+
domain,
|
|
1298
|
+
key,
|
|
1299
|
+
id,
|
|
1300
|
+
{
|
|
1301
|
+
exists: (targetId) => exists({ domain: target, id: targetId }),
|
|
1302
|
+
active: async (targetId) => checkActive(nodes, key, (await nodeOf(targetId))!),
|
|
1303
|
+
check,
|
|
1304
|
+
node: nodeOf,
|
|
1305
|
+
emit: (event, payload) => nodes.emit(event, payload),
|
|
1306
|
+
// A facet is visible as its target: on a visible node, and live unless `archived`.
|
|
1307
|
+
scope: (visible, archived) =>
|
|
1308
|
+
target === nodeDomain
|
|
1309
|
+
? { where: { target: { in: visible } } }
|
|
1310
|
+
: {
|
|
1311
|
+
where: {
|
|
1312
|
+
$ref: {
|
|
1313
|
+
domain: target,
|
|
1314
|
+
path: 'target',
|
|
1315
|
+
mode: 'is',
|
|
1316
|
+
nodes: visible,
|
|
1317
|
+
...(!archived && { archived: false }),
|
|
1318
|
+
},
|
|
1319
|
+
},
|
|
1320
|
+
},
|
|
1321
|
+
where: (where) => resolveWhere(where, id),
|
|
1322
|
+
with: (each) => withOf(id, each),
|
|
1323
|
+
private: isPrivate,
|
|
1324
|
+
actor: actorNow,
|
|
1325
|
+
},
|
|
1326
|
+
nodes,
|
|
1327
|
+
release,
|
|
1328
|
+
);
|
|
1329
|
+
readers.set(id, facets as FacetReader<unknown>);
|
|
1330
|
+
return facets;
|
|
1331
|
+
},
|
|
1332
|
+
async log<T>(owner: Context, spec: LogSpec<T>) {
|
|
1333
|
+
const { name, schema, version } = spec;
|
|
1334
|
+
const isPrivate = checkPrivate('log', spec.private);
|
|
1335
|
+
// One release path: `declare` releases the handle when `owner` disposes. A released log still takes its owner's
|
|
1336
|
+
// late appends (Cordis runs a plugin's disposers in parallel, so a closing append may come after the release)
|
|
1337
|
+
// until the id is declared again (`Hub.log`).
|
|
1338
|
+
const { domain: handle } = await declare<T, LogDomain<T>>(
|
|
1339
|
+
'log',
|
|
1340
|
+
owner,
|
|
1341
|
+
name,
|
|
1342
|
+
{ schema, version },
|
|
1343
|
+
async (each) => {
|
|
1344
|
+
const { id } = each;
|
|
1345
|
+
const log = await domains.log(each);
|
|
1346
|
+
// No count: a log's streams come by pages.
|
|
1347
|
+
const described: Catalogued = {
|
|
1348
|
+
id,
|
|
1349
|
+
plugin: keyOf(id),
|
|
1350
|
+
name,
|
|
1351
|
+
kind: 'log',
|
|
1352
|
+
version,
|
|
1353
|
+
private: isPrivate,
|
|
1354
|
+
references: [],
|
|
1355
|
+
};
|
|
1356
|
+
const write = serialWrites(keyOf(id), id, actorNow);
|
|
1357
|
+
const made: LogDomain<T> = {
|
|
1358
|
+
append: (caller, stream, events, options) =>
|
|
1359
|
+
write('append', caller, () =>
|
|
1360
|
+
logErrors(id, () => log.append(stream, events, options)),
|
|
1361
|
+
),
|
|
1362
|
+
read: (stream, window) => logErrors(id, () => log.read(stream, window)),
|
|
1363
|
+
last: (stream) => logErrors(id, () => log.last(stream)),
|
|
1364
|
+
streams: (page) => logErrors(id, () => log.streams(page)),
|
|
1365
|
+
purge: (caller, stream) =>
|
|
1366
|
+
write('purge', caller, (author) =>
|
|
1367
|
+
logErrors(id, () =>
|
|
1368
|
+
log.purge(stream, { op: 'log.purge', fields: null, ...author }),
|
|
1369
|
+
),
|
|
1370
|
+
),
|
|
1371
|
+
attach: (caller, stream, bytes, options) =>
|
|
1372
|
+
write('attach', caller, () =>
|
|
1373
|
+
logErrors(id, () => log.attach(stream, bytes, options)),
|
|
1374
|
+
),
|
|
1375
|
+
attachFile: (caller, stream, path, options) =>
|
|
1376
|
+
write('attach', caller, () =>
|
|
1377
|
+
logErrors(id, () => log.attachFile(stream, path, options)),
|
|
1378
|
+
),
|
|
1379
|
+
blob: (blob) => logErrors(id, () => log.blob(blob)),
|
|
1380
|
+
bytes: (blob) =>
|
|
1381
|
+
logErrors(id, async () => {
|
|
1382
|
+
const kept = await log.blob(blob);
|
|
1383
|
+
return kept && new Uint8Array(await readFile(kept.path));
|
|
1384
|
+
}),
|
|
1385
|
+
subscribe: (stream, fn) => log.subscribe(stream, fn),
|
|
1386
|
+
release: () => {
|
|
1387
|
+
// A newer generation's registration is its own.
|
|
1388
|
+
if (logDomains.get(id) === made) {
|
|
1389
|
+
logDomains.delete(id);
|
|
1390
|
+
privateIds.delete(id);
|
|
1391
|
+
}
|
|
1392
|
+
if (catalog.get(id) === described) catalog.delete(id);
|
|
1393
|
+
log.release();
|
|
1394
|
+
},
|
|
1395
|
+
};
|
|
1396
|
+
// Registered before `declare` checks the owner: one disposed meanwhile releases it, which unregisters it.
|
|
1397
|
+
logDomains.set(id, made as LogDomain<unknown>);
|
|
1398
|
+
if (isPrivate) privateIds.add(id);
|
|
1399
|
+
else privateIds.delete(id);
|
|
1400
|
+
catalog.set(id, described);
|
|
1401
|
+
return made;
|
|
1402
|
+
},
|
|
1403
|
+
);
|
|
1404
|
+
return handle;
|
|
1405
|
+
},
|
|
1406
|
+
reference,
|
|
1407
|
+
refuse,
|
|
1408
|
+
envelope: async ({ domain, id }) => {
|
|
1409
|
+
// Copies, here and in `resolve` (backend reads are): a caller cannot change another plugin's data or the tree in memory.
|
|
1410
|
+
if (domain === nodeDomain)
|
|
1411
|
+
return nodes.has(id)
|
|
1412
|
+
? { id, node: id, facets: await facetsOf(nodeDomain, id, id) }
|
|
1413
|
+
: undefined;
|
|
1414
|
+
const object = await objectDomains.get(domain)?.get(id);
|
|
1415
|
+
if (!object) return undefined;
|
|
1416
|
+
const facets = await facetsOf(domain, id, object.node);
|
|
1417
|
+
// A private object: its state, never its value.
|
|
1418
|
+
return privateIds.has(domain)
|
|
1419
|
+
? { id: object.id, node: object.node, archived: object.archived === true, facets }
|
|
1420
|
+
: { ...view(object), facets };
|
|
1421
|
+
},
|
|
1422
|
+
usedBy: (target) =>
|
|
1423
|
+
Promise.all(
|
|
1424
|
+
index.usedBy(target).map(async (each) => ({ ...each, archived: await hiddenRef(each) })),
|
|
1425
|
+
),
|
|
1426
|
+
resolve: async ({ domain, id }) => {
|
|
1427
|
+
if (domain === nodeDomain) {
|
|
1428
|
+
const line = nodes.ancestors(id);
|
|
1429
|
+
if (!line) return { state: 'missing' };
|
|
1430
|
+
return {
|
|
1431
|
+
state: line[0]!.archived
|
|
1432
|
+
? 'archived'
|
|
1433
|
+
: line.some((each) => each.archived)
|
|
1434
|
+
? 'in-archived-node'
|
|
1435
|
+
: 'live',
|
|
1436
|
+
object: structuredClone(line[0]!),
|
|
1437
|
+
};
|
|
1438
|
+
}
|
|
1439
|
+
const objects = objectDomains.get(domain);
|
|
1440
|
+
if (!objects) return { state: 'owner-missing' };
|
|
1441
|
+
const object = await objects.get(id);
|
|
1442
|
+
if (!object) return { state: 'missing' };
|
|
1443
|
+
const state =
|
|
1444
|
+
object.archived === true
|
|
1445
|
+
? 'archived'
|
|
1446
|
+
: inArchivedNode(nodes, object.node)
|
|
1447
|
+
? 'in-archived-node'
|
|
1448
|
+
: 'live';
|
|
1449
|
+
return {
|
|
1450
|
+
state,
|
|
1451
|
+
object: privateIds.has(domain)
|
|
1452
|
+
? { id: object.id, node: object.node, archived: object.archived === true }
|
|
1453
|
+
: view(object),
|
|
1454
|
+
};
|
|
1455
|
+
},
|
|
1456
|
+
// A domain id holds a `/`, a shared name cannot: `read('settings')` without its key still fails as a shared read.
|
|
1457
|
+
read: ((name: string, key?: string) =>
|
|
1458
|
+
typeof name === 'string' && name.includes('/') && key === undefined
|
|
1459
|
+
? reader(name)
|
|
1460
|
+
: readShared(name, key as string)) as StorageService['read'],
|
|
1461
|
+
// As `read`: one argument holding a `/` is a domain id.
|
|
1462
|
+
write: ((name: string, key?: string, value?: unknown) =>
|
|
1463
|
+
typeof name === 'string' && name.includes('/') && key === undefined && value === undefined
|
|
1464
|
+
? writer(name)
|
|
1465
|
+
: writeShared(name, key as string, value)) as StorageService['write'],
|
|
1466
|
+
remove: async (name, key) => {
|
|
1467
|
+
checkKey('remove', key);
|
|
1468
|
+
const audit = plainAudit('storage', 'delete');
|
|
1469
|
+
await (await sharedDomain('remove', name)).delete(key, audit);
|
|
1470
|
+
},
|
|
1471
|
+
list: async (name) => (await sharedDomain('list', name)).keys(),
|
|
1472
|
+
audit: {
|
|
1473
|
+
query: (query) => auditCall(() => ctx.storageBackend.queryAudit(query)),
|
|
1474
|
+
actors: (entry) => auditCall(() => ctx.storageBackend.auditActors(entry)),
|
|
1475
|
+
objects: (of) => auditCall(() => ctx.storageBackend.auditObjects(of)),
|
|
1476
|
+
prune: (before) => auditCall(() => ctx.storageBackend.pruneAudit(before)),
|
|
1477
|
+
async record(owner) {
|
|
1478
|
+
// A backend without audit says so before anything is recorded.
|
|
1479
|
+
await auditCall(() => ctx.storageBackend.queryAudit({ take: 1 }));
|
|
1480
|
+
owner.effect(() => {
|
|
1481
|
+
recorders++;
|
|
1482
|
+
return () => {
|
|
1483
|
+
recorders--;
|
|
1484
|
+
};
|
|
1485
|
+
});
|
|
1486
|
+
},
|
|
1487
|
+
},
|
|
1488
|
+
};
|
|
1489
|
+
// What the data takes, for `bridge doctor` (research-011 slice 4): the `data size` check reports each storage file's
|
|
1490
|
+
// size. The service is optional, read without `inject` so a host with older code keeps loading this plugin.
|
|
1491
|
+
const doctor = ctx.get?.('doctor', false) as
|
|
1492
|
+
| { check(owner: Context, check: { name: string; run(): unknown }): void }
|
|
1493
|
+
| undefined;
|
|
1494
|
+
const mib = (bytes: number) => `${(bytes / 1048576).toFixed(1)} MiB`;
|
|
1495
|
+
doctor?.check(ctx, {
|
|
1496
|
+
name: 'data size',
|
|
1497
|
+
run: async () =>
|
|
1498
|
+
(await ctx.storageBackend.sizes())
|
|
1499
|
+
.map(({ file, bytes }) => `${file} ${mib(bytes)}`)
|
|
1500
|
+
.join(', ') || 'no data yet',
|
|
1501
|
+
});
|
|
1502
|
+
/** The plugin providing `storageBackend`: the key of the context that provided it, as Cordis keeps it. */
|
|
1503
|
+
const backendPlugin = () => {
|
|
1504
|
+
const provider = ctx.reflect?._getImpl('storageBackend', false)?.ctx;
|
|
1505
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the kernel sets the plugin key symbol on every plugin context
|
|
1506
|
+
const key = (provider as unknown as Record<symbol, unknown> | undefined)?.[pluginKey];
|
|
1507
|
+
return typeof key === 'string' ? key : undefined;
|
|
1508
|
+
};
|
|
1509
|
+
/** How this workspace's storage is made (`Inventory`): its backend, and every declared domain with its metadata and,
|
|
1510
|
+
* where the backend counts it, what it holds. Never a value, a private domain's included. */
|
|
1511
|
+
const inventory = async (): Promise<Inventory> => {
|
|
1512
|
+
const declaredNow = [...catalog.values()];
|
|
1513
|
+
const sharedNow = (
|
|
1514
|
+
await Promise.all(
|
|
1515
|
+
[...shared].map(async ([name, held]): Promise<Catalogued[]> => {
|
|
1516
|
+
try {
|
|
1517
|
+
const domain = await held;
|
|
1518
|
+
return [
|
|
1519
|
+
{
|
|
1520
|
+
id: `shared/${name}`,
|
|
1521
|
+
plugin: 'shared',
|
|
1522
|
+
name,
|
|
1523
|
+
kind: 'shared',
|
|
1524
|
+
version: 1,
|
|
1525
|
+
private: false,
|
|
1526
|
+
references: [],
|
|
1527
|
+
count: async () => ({ total: domain.keys().length }),
|
|
1528
|
+
},
|
|
1529
|
+
];
|
|
1530
|
+
} catch {
|
|
1531
|
+
return [];
|
|
1532
|
+
}
|
|
1533
|
+
}),
|
|
1534
|
+
)
|
|
1535
|
+
).flat();
|
|
1536
|
+
const byId = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
|
|
1537
|
+
const domainsNow = await Promise.all(
|
|
1538
|
+
[...declaredNow, ...sharedNow].map(async ({ count, ...each }): Promise<DomainInventory> => {
|
|
1539
|
+
const referencedBy = declaredNow
|
|
1540
|
+
.flatMap((other) => [
|
|
1541
|
+
...other.references
|
|
1542
|
+
.filter((ref) => ref.domain === each.id)
|
|
1543
|
+
.map((ref) => ({ domain: other.id, field: ref.field })),
|
|
1544
|
+
...(other.target === each.id ? [{ domain: other.id, field: 'target' }] : []),
|
|
1545
|
+
])
|
|
1546
|
+
.sort((a, b) => byId(a.domain, b.domain) || byId(a.field, b.field));
|
|
1547
|
+
let counted: DomainInventory['count'];
|
|
1548
|
+
try {
|
|
1549
|
+
counted = await count?.();
|
|
1550
|
+
} catch {
|
|
1551
|
+
// Not counted: the page says nothing rather than a wrong number.
|
|
1552
|
+
}
|
|
1553
|
+
return { ...each, referencedBy, ...(counted && { count: counted }) };
|
|
1554
|
+
}),
|
|
1555
|
+
);
|
|
1556
|
+
let sizes: Inventory['backend']['sizes'];
|
|
1557
|
+
try {
|
|
1558
|
+
sizes = await ctx.storageBackend.sizes();
|
|
1559
|
+
} catch {
|
|
1560
|
+
// A backend that cannot tell: no sizes.
|
|
1561
|
+
}
|
|
1562
|
+
const plugin = backendPlugin();
|
|
1563
|
+
return {
|
|
1564
|
+
backend: { ...(plugin !== undefined && { plugin }), ...(sizes && { sizes }) },
|
|
1565
|
+
domains: domainsNow.sort((a, b) => byId(a.id, b.id)),
|
|
1566
|
+
};
|
|
1567
|
+
};
|
|
1568
|
+
// The router is there in a worker; a test may apply this plugin without one.
|
|
1569
|
+
if ('router' in ctx)
|
|
1570
|
+
ctx.router.route(ctx, {
|
|
1571
|
+
method: 'GET',
|
|
1572
|
+
path: '/inventory',
|
|
1573
|
+
description:
|
|
1574
|
+
"How this workspace's storage is made, metadata only (never a value): the backend plugin and its files' sizes, then every declared domain (id, plugin, kind objects | facets | log | plain | shared, version, private, writers, rule names, facet target, references out and in, indexes, search paths) with its count where cheap (objects: total and archived).",
|
|
1575
|
+
handle: () => inventory(),
|
|
1576
|
+
});
|
|
1577
|
+
// Removing plugin `plugin` (`bridge remove`): every object of its domains another plugin references, and every
|
|
1578
|
+
// other plugin's facet on them. Read from the index and facet domains, so it works with `plugin` disabled too.
|
|
1579
|
+
// ponytail: loaded plugins only: a disabled referrer's references and facets are not seen; a persisted index if that matters.
|
|
1580
|
+
// oxlint-disable-next-line bridge/no-new-unchecked-cast -- typing: the worker Context carries the removal service, which its type does not name
|
|
1581
|
+
(ctx as unknown as { removal: Removal }).removal.check(ctx, async (plugin) => {
|
|
1582
|
+
const own = (domain: string) => domain.startsWith(`${plugin}/`);
|
|
1583
|
+
const referenced = index
|
|
1584
|
+
.targets()
|
|
1585
|
+
.filter((target) => own(target.domain))
|
|
1586
|
+
.flatMap((target) =>
|
|
1587
|
+
index
|
|
1588
|
+
.usedBy(target)
|
|
1589
|
+
.filter((each) => !own(each.domain))
|
|
1590
|
+
.map((each) => `${each.domain} ${each.id} → ${target.domain} ${target.id}`),
|
|
1591
|
+
);
|
|
1592
|
+
const facets = (
|
|
1593
|
+
await Promise.all(
|
|
1594
|
+
[...facetDomains]
|
|
1595
|
+
.filter(([facetId, { target }]) => !own(facetId) && own(target))
|
|
1596
|
+
.map(async ([facetId, { target, domain }]) =>
|
|
1597
|
+
(await domain.list()).map(
|
|
1598
|
+
({ key: targetId }) => `facet ${facetId} on ${target} ${targetId}`,
|
|
1599
|
+
),
|
|
1600
|
+
),
|
|
1601
|
+
)
|
|
1602
|
+
).flat();
|
|
1603
|
+
return [...referenced.sort(), ...facets.sort()];
|
|
1604
|
+
});
|
|
1605
|
+
ctx.provide('storage', storage);
|
|
1606
|
+
}
|