@astrale-os/kernel-client 1.0.0-beta.3 → 1.0.0-beta.30
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/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +19 -2
- package/dist/client/client.js +62 -30
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/request.d.ts +8 -2
- package/dist/client/request.js +28 -7
- package/dist/client/result.d.ts +10 -1
- package/dist/client/result.js +10 -4
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.js +3 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +19 -0
- package/dist/errors/refusal.js +63 -0
- package/dist/errors/response-too-large-error.d.ts +11 -0
- package/dist/errors/response-too-large-error.js +15 -0
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/{schema → execution}/admission.d.ts +2 -2
- package/dist/{schema → execution}/admission.js +3 -3
- package/dist/execution/contracts.d.ts +76 -0
- package/dist/execution/contracts.js +1 -0
- package/dist/execution/deadline.d.ts +18 -0
- package/dist/execution/deadline.js +25 -0
- package/dist/execution/execution.d.ts +61 -0
- package/dist/execution/execution.js +132 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +3 -0
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/domain/binding.js +24 -17
- package/dist/graph/domain/conformance.d.ts +12 -0
- package/dist/graph/domain/conformance.js +18 -0
- package/dist/graph/domain/index.d.ts +1 -0
- package/dist/graph/domain/index.js +1 -1
- package/dist/graph/domain/provenance.d.ts +0 -2
- package/dist/graph/domain/provenance.js +0 -4
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +3 -3
- package/dist/graph/index.js +1 -1
- package/dist/graph/page-contract.d.ts +64 -1
- package/dist/graph/provenance.d.ts +4 -5
- package/dist/graph/provenance.js +5 -6
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +145 -7
- package/dist/graph/query.typecheck.d.ts +77 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/pacer/index.d.ts +1 -1
- package/dist/pacer/index.js +1 -1
- package/dist/pacer/retry/index.d.ts +1 -0
- package/dist/pacer/retry/index.js +1 -0
- package/dist/pacer/retry/refusals.d.ts +7 -0
- package/dist/pacer/retry/refusals.js +10 -0
- package/dist/pacer/retry/retry.d.ts +2 -0
- package/dist/pacer/retry/retry.js +33 -1
- package/dist/schema/contracts.d.ts +77 -9
- package/dist/schema/index.d.ts +3 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +76 -0
- package/dist/schema/installation.js +82 -0
- package/dist/schema/schema.d.ts +22 -13
- package/dist/schema/schema.js +67 -48
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +66 -20
- package/dist/session/content/api.d.ts +10 -2
- package/dist/session/content/api.js +13 -4
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +6 -1
- package/dist/session/content/input.js +12 -4
- package/dist/session/content/retry.d.ts +5 -1
- package/dist/session/content/retry.js +21 -31
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/credential/credentials.js +43 -28
- package/dist/session/discovery/bundle.d.ts +9 -4
- package/dist/session/discovery/bundle.js +28 -35
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/release.js +18 -28
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +35 -3
- package/dist/session/discovery/transport.js +40 -1
- package/dist/session/durable.d.ts +13 -0
- package/dist/session/durable.js +39 -0
- package/dist/session/exchange/exchange.d.ts +3 -2
- package/dist/session/exchange/exchange.js +10 -7
- package/dist/session/exchange/index.d.ts +1 -1
- package/dist/session/exchange/index.js +1 -1
- package/dist/session/exchange/presentation.js +28 -10
- package/dist/session/exchange/source.d.ts +7 -7
- package/dist/session/exchange/source.js +26 -27
- package/dist/session/index.d.ts +4 -3
- package/dist/session/index.js +0 -1
- package/dist/session/options.d.ts +0 -2
- package/dist/session/options.js +0 -3
- package/dist/session/reference.d.ts +9 -5
- package/dist/session/reference.js +7 -2
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/auth.d.ts +9 -3
- package/dist/session/routing/auth.js +4 -3
- package/dist/session/routing/cache.d.ts +12 -5
- package/dist/session/routing/cache.js +56 -48
- package/dist/session/routing/dispatch.d.ts +26 -3
- package/dist/session/routing/dispatch.js +112 -67
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/legacy/release-route.d.ts +12 -0
- package/dist/session/routing/legacy/release-route.js +6 -0
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/request.d.ts +6 -1
- package/dist/session/routing/request.js +13 -2
- package/dist/session/routing/result.d.ts +1 -1
- package/dist/session/routing/result.js +5 -1
- package/dist/session/routing/store.js +4 -5
- package/dist/session/session.d.ts +31 -5
- package/dist/session/session.js +60 -19
- package/dist/session/session.typecheck.js +20 -6
- package/dist/session/views/index.d.ts +2 -2
- package/dist/session/views/index.js +2 -2
- package/dist/session/views/kernel.d.ts +31 -0
- package/dist/session/views/kernel.js +30 -0
- package/dist/session/views/legacy/catalog.d.ts +21 -0
- package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
- package/dist/session/views/legacy/index.d.ts +14 -0
- package/dist/session/views/legacy/index.js +14 -0
- package/dist/session/views/legacy/resolve.d.ts +46 -0
- package/dist/session/views/legacy/resolve.js +83 -0
- package/dist/session/views/legacy/unplaced.d.ts +22 -0
- package/dist/session/views/legacy/unplaced.js +26 -0
- package/dist/session/views/read.d.ts +49 -0
- package/dist/session/views/read.js +148 -0
- package/dist/session/views/view.d.ts +48 -0
- package/dist/session/views/view.js +74 -0
- package/dist/store/memory.d.ts +1 -3
- package/dist/store/memory.js +73 -105
- package/dist/store/shape.d.ts +33 -12
- package/dist/store/shape.js +125 -16
- package/dist/store/store.d.ts +6 -6
- package/dist/store/store.js +1 -1
- package/dist/transport/auto/decision.d.ts +26 -14
- package/dist/transport/auto/decision.js +35 -32
- package/dist/transport/auto/fallback.d.ts +1 -1
- package/dist/transport/auto/fallback.js +5 -10
- package/dist/transport/auto/transport.d.ts +0 -2
- package/dist/transport/auto/transport.js +24 -32
- package/dist/transport/deadline.d.ts +2 -0
- package/dist/transport/deadline.js +18 -0
- package/dist/transport/failure.d.ts +28 -1
- package/dist/transport/failure.js +63 -1
- package/dist/transport/http/admission.js +7 -34
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -0
- package/dist/transport/http/binary.js +3 -3
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +12 -8
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +3 -1
- package/dist/transport/http/response.js +7 -9
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +34 -81
- package/dist/transport/http/transport.d.ts +10 -0
- package/dist/transport/http/transport.js +87 -32
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +1 -1
- package/dist/transport/index.js +1 -1
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +10 -0
- package/dist/transport/unary.d.ts +8 -0
- package/dist/transport/unary.js +34 -0
- package/dist/transport/websocket/connection.d.ts +7 -3
- package/dist/transport/websocket/connection.js +103 -92
- package/dist/transport/websocket/exchange.d.ts +12 -19
- package/dist/transport/websocket/exchange.js +86 -184
- package/dist/transport/websocket/transport.js +11 -19
- package/package.json +7 -5
- package/dist/session/execution.d.ts +0 -65
- package/dist/session/execution.js +0 -34
- package/dist/session/views/catalog.d.ts +0 -19
- package/dist/session/views/resolve.d.ts +0 -13
- package/dist/session/views/resolve.js +0 -21
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { equal } from '@astrale-os/kernel-core/graph/properties';
|
|
2
|
+
import { Query } from '@astrale-os/kernel-core/graph/query';
|
|
3
|
+
import { Path } from '@astrale-os/kernel-core/path';
|
|
4
|
+
import { patterns } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
5
|
+
import { ClientError } from '../../errors/index.js';
|
|
6
|
+
import { DOMAIN, MEMBER, OF_DOMAIN, OF_DOMAIN_CLASS, ORIGIN, PLACEMENT, VIEW_CLASS, } from './kernel.js';
|
|
7
|
+
import { declaredView } from './view.js';
|
|
8
|
+
/** Reads of one answer before giving up, the bound View.resolve put on its Registry reads. */
|
|
9
|
+
const ATTEMPTS = 3;
|
|
10
|
+
/** Selected Edges per page: one of_domain Edge per View the Domain declares. */
|
|
11
|
+
const PAGE_SIZE = 100;
|
|
12
|
+
/**
|
|
13
|
+
* Read one View by any Path: an exact `@<id>`, its declaration `/:<origin>:view.<name>`, or a
|
|
14
|
+
* traversal such as `Path.domain(origin).in(K.classes.entrypoint.key)`. One Query selects the
|
|
15
|
+
* of_domain Edge of the View it reaches, which only projection writes: with it, its Domain declares
|
|
16
|
+
* the View; without it, a second read returns the instance. A declared View without a placement
|
|
17
|
+
* is answered `unplaced`, with its Path and Domain.
|
|
18
|
+
*/
|
|
19
|
+
export async function readView(reader, target, operation) {
|
|
20
|
+
const { nodes, edges } = await readCoherent(reader, declarationQuery(target), operation);
|
|
21
|
+
const edge = edges[0];
|
|
22
|
+
if (edge === undefined) {
|
|
23
|
+
return { placed: reader.admit(await reader.read(target, operation), undefined) };
|
|
24
|
+
}
|
|
25
|
+
const view = nodes.find(({ id }) => id === edge.source);
|
|
26
|
+
const domain = nodes.find(({ id }) => id === edge.target);
|
|
27
|
+
if (edges.length !== 1 || edge.class !== OF_DOMAIN || !view || domain?.class !== DOMAIN) {
|
|
28
|
+
throw new ClientError('View answer is invalid.');
|
|
29
|
+
}
|
|
30
|
+
const declaration = Object.freeze({ domain, member: edge.props[MEMBER] });
|
|
31
|
+
if (view.props[PLACEMENT] === undefined)
|
|
32
|
+
return { unplaced: declaredView(declaration) };
|
|
33
|
+
return { placed: reader.admit(view, declaration) };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Read the Views one Domain declares, in Path order, from the graph alone: one Query selects the
|
|
37
|
+
* of_domain Edges of its Views, which the answer closes with their View Nodes and the Domain Node.
|
|
38
|
+
* Only an exact Domain Path `/:<origin>` or an origin is accepted; any other Path is refused before
|
|
39
|
+
* the Query. A caller that may not read them meets the Query's own refusal or an empty list. An
|
|
40
|
+
* answer that locates declared Views and places none of them is answered `unplaced`, with the
|
|
41
|
+
* Domain.
|
|
42
|
+
*/
|
|
43
|
+
export async function readDomainViews(reader, domain, operation) {
|
|
44
|
+
const origin = domainOrigin(domain);
|
|
45
|
+
const located = locateDomainViews(await readCoherent(reader, domainViewsQuery(origin), operation), origin);
|
|
46
|
+
if (located === undefined)
|
|
47
|
+
return { placed: Object.freeze([]) };
|
|
48
|
+
const { owner, members } = located;
|
|
49
|
+
const declarations = [...members].map(([node, member]) => [node, Object.freeze({ domain: owner, member })]);
|
|
50
|
+
const paths = declarations.map(([, declared]) => declaredView(declared).path);
|
|
51
|
+
if (new Set(paths).size !== paths.length) {
|
|
52
|
+
throw new ClientError('Declared View keys are not unique.');
|
|
53
|
+
}
|
|
54
|
+
// Projection places each View the active Release binds. One no binding places has nothing a
|
|
55
|
+
// Shell could mount, and refusing it would let one unplaced View hide every other: it is skipped.
|
|
56
|
+
const placed = declarations.filter(([node]) => node.props[PLACEMENT] !== undefined);
|
|
57
|
+
if (declarations.length > 0 && placed.length === 0)
|
|
58
|
+
return { unplaced: origin };
|
|
59
|
+
const views = placed.map(([node, declared]) => reader.admit(node, declared));
|
|
60
|
+
return { placed: Object.freeze(views.sort((left, right) => compareText(left.path, right.path))) };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Locate the Views of one Domain in one coherent answer, refusing an answer that is not of it. An
|
|
64
|
+
* empty answer is a Domain the caller may not read or that does not exist, as View.resolve
|
|
65
|
+
* answered for a Domain it did not know.
|
|
66
|
+
*/
|
|
67
|
+
function locateDomainViews(graph, domain) {
|
|
68
|
+
const nodes = new Map(graph.nodes.map((node) => [node.id, node]));
|
|
69
|
+
const owner = graph.nodes.find((node) => node.class === DOMAIN);
|
|
70
|
+
if (owner !== undefined && owner.props[ORIGIN] !== domain) {
|
|
71
|
+
throw new ClientError('Domain View answer is not of the requested Domain.');
|
|
72
|
+
}
|
|
73
|
+
const members = new Map();
|
|
74
|
+
for (const edge of graph.edges) {
|
|
75
|
+
const view = nodes.get(edge.source);
|
|
76
|
+
if (edge.target !== owner?.id || view === undefined) {
|
|
77
|
+
throw new ClientError('Domain View answer is not of the requested Domain.');
|
|
78
|
+
}
|
|
79
|
+
if (edge.class !== OF_DOMAIN)
|
|
80
|
+
throw new ClientError('Domain View answer is invalid.');
|
|
81
|
+
members.set(view, edge.props[MEMBER]);
|
|
82
|
+
}
|
|
83
|
+
if (owner === undefined)
|
|
84
|
+
return undefined;
|
|
85
|
+
return Object.freeze({ owner, members });
|
|
86
|
+
}
|
|
87
|
+
/** Read one answer whose pages agree, at most ATTEMPTS times. */
|
|
88
|
+
async function readCoherent(reader, ast, operation) {
|
|
89
|
+
for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
|
|
90
|
+
const graph = await readAnswer(reader, ast, operation);
|
|
91
|
+
if (graph !== undefined)
|
|
92
|
+
return graph;
|
|
93
|
+
}
|
|
94
|
+
throw new ClientError('Views could not be read from one coherent graph.');
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Read every page of one answer, or nothing when its pages disagree. Each page is its own Query
|
|
98
|
+
* and every page carries the target of each selected Edge, so two pages that differ on a Node they
|
|
99
|
+
* share were read from different snapshots: the answer is read again.
|
|
100
|
+
*/
|
|
101
|
+
async function readAnswer(reader, ast, operation) {
|
|
102
|
+
const nodes = new Map();
|
|
103
|
+
const edges = [];
|
|
104
|
+
let after;
|
|
105
|
+
do {
|
|
106
|
+
const { result, page } = await reader.query(ast, after === undefined ? { size: PAGE_SIZE } : { size: PAGE_SIZE, after }, operation);
|
|
107
|
+
if (result.kind !== 'graph' || result.selection.kind !== 'edge') {
|
|
108
|
+
throw new ClientError('View query returned a non-Edge graph result.');
|
|
109
|
+
}
|
|
110
|
+
for (const node of result.graph.nodes) {
|
|
111
|
+
const seen = nodes.get(node.id);
|
|
112
|
+
if (seen === undefined)
|
|
113
|
+
nodes.set(node.id, node);
|
|
114
|
+
else if (seen.class !== node.class || !equal(seen.props, node.props))
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
edges.push(...result.graph.edges);
|
|
118
|
+
after = page.next;
|
|
119
|
+
} while (after !== undefined);
|
|
120
|
+
return Object.freeze({ nodes: Object.freeze([...nodes.values()]), edges: Object.freeze(edges) });
|
|
121
|
+
}
|
|
122
|
+
/** The of_domain Edge from the View a Path reaches to the Domain that declares it, if any. */
|
|
123
|
+
function declarationQuery(target) {
|
|
124
|
+
const declared = Query.from({ nodes: [target] })
|
|
125
|
+
.filter({ class: VIEW_CLASS })
|
|
126
|
+
.expand({ via: [OF_DOMAIN_CLASS], direction: 'outgoing' });
|
|
127
|
+
return declared.select({ kind: 'graph', binding: declared.edge });
|
|
128
|
+
}
|
|
129
|
+
/** The of_domain Edges that reach a Domain from its Views, with both endpoints. */
|
|
130
|
+
function domainViewsQuery(origin) {
|
|
131
|
+
const views = Query.from({ nodes: [Path.domain(origin)] })
|
|
132
|
+
.expand({ via: [OF_DOMAIN_CLASS], direction: 'incoming' })
|
|
133
|
+
.filter({ class: VIEW_CLASS });
|
|
134
|
+
return views.select({ kind: 'graph', binding: views.edge });
|
|
135
|
+
}
|
|
136
|
+
/** The origin of a Domain, named by itself or by its exact Domain Path. */
|
|
137
|
+
function domainOrigin(input) {
|
|
138
|
+
if (typeof input === 'string' && patterns.origin.test(input))
|
|
139
|
+
return input;
|
|
140
|
+
const { anchor, steps } = Path.from(input).ast;
|
|
141
|
+
if (anchor.kind !== 'domain' || steps.length !== 0) {
|
|
142
|
+
throw new TypeError('Domain Views are read for a Domain Path /:<origin> or its origin.');
|
|
143
|
+
}
|
|
144
|
+
return anchor.origin;
|
|
145
|
+
}
|
|
146
|
+
function compareText(left, right) {
|
|
147
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
148
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { IssuerId } from '@astrale-os/kernel-core/auth';
|
|
2
|
+
import type { Node } from '@astrale-os/kernel-core/graph/node';
|
|
3
|
+
import type { NodePath } from '@astrale-os/kernel-core/path';
|
|
4
|
+
import type { Key } from '@astrale-os/kernel-dsl/v1';
|
|
5
|
+
import type { ViewPlacement } from '@astrale-os/kernel-protocol/placement';
|
|
6
|
+
import type { SessionPolicy } from '../context/index.js';
|
|
7
|
+
/** One View, read from its Node, whoever wrote it. */
|
|
8
|
+
export interface View {
|
|
9
|
+
/**
|
|
10
|
+
* The Path that names it, which `view` reads it by: `/:<origin>:view.<name>` for a View its
|
|
11
|
+
* Domain declares, `@<id>` for an instance of a Domain Class that extends View.
|
|
12
|
+
*/
|
|
13
|
+
readonly path: NodePath;
|
|
14
|
+
/** The Domain that declares it, or the Domain of the Class it is an instance of. */
|
|
15
|
+
readonly domain: Key.Origin;
|
|
16
|
+
readonly description?: string;
|
|
17
|
+
/** Where and how a Shell mounts it, as its Release binds it or a Mutation wrote it. */
|
|
18
|
+
readonly placement: ViewPlacement;
|
|
19
|
+
/**
|
|
20
|
+
* The Identity that answers for its code: the `iss` of the Domain that declares it, or for an
|
|
21
|
+
* instance its own `iss`, self-issued by this Kernel. Absent when it has none.
|
|
22
|
+
*/
|
|
23
|
+
readonly issuer?: IssuerId;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* What declares a View its Domain declares: the Domain Node its of_domain Edge reaches, and the
|
|
27
|
+
* member that Edge names it by. Only projection writes an of_domain Edge.
|
|
28
|
+
*/
|
|
29
|
+
export interface Declaration {
|
|
30
|
+
readonly domain: Node;
|
|
31
|
+
readonly member: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Admit one View from its Node, with the Declaration that names it, or none for an instance. The
|
|
35
|
+
* placement passes the Protocol placement codec, which a Release View binding composes, then Session
|
|
36
|
+
* endpoint policy; the client interprets neither its handshake nor its host. The issuer is whoever
|
|
37
|
+
* answers for the View's code: the Domain that declares it, or for an instance its own Identity,
|
|
38
|
+
* self-issued by this Kernel (`register` mode `self`), never its Class Domain nor an external
|
|
39
|
+
* issuer other Identities share.
|
|
40
|
+
*/
|
|
41
|
+
export declare function admitView(node: Node, declaration: Declaration | undefined, kernel: IssuerId, policy: Readonly<SessionPolicy>): View;
|
|
42
|
+
/** The Path and Domain of a View its Domain declares, from its Declaration. */
|
|
43
|
+
export declare function declaredView(declaration: Declaration): Pick<View, 'path' | 'domain'>;
|
|
44
|
+
/**
|
|
45
|
+
* Admit the placement a View Node carries with the Protocol placement codec, which a Release View
|
|
46
|
+
* binding composes, then Session endpoint policy.
|
|
47
|
+
*/
|
|
48
|
+
export declare function admitViewPlacement(value: unknown, policy: Readonly<SessionPolicy>): ViewPlacement;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { issuer, register } from '@astrale-os/kernel-core/auth';
|
|
2
|
+
import { Path } from '@astrale-os/kernel-core/path';
|
|
3
|
+
import { Key as DefinitionKey } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
4
|
+
import { acceptViewPlacement, PlacementError } from '@astrale-os/kernel-protocol/placement';
|
|
5
|
+
import { ClientError } from '../../errors/index.js';
|
|
6
|
+
import { allowEndpoint } from '../context/index.js';
|
|
7
|
+
import { DESCRIPTION, ISSUER, ORIGIN, PLACEMENT, VIEW } from './kernel.js';
|
|
8
|
+
const MEMBER = /^view\.([A-Za-z][A-Za-z0-9_]*)$/u;
|
|
9
|
+
/**
|
|
10
|
+
* Admit one View from its Node, with the Declaration that names it, or none for an instance. The
|
|
11
|
+
* placement passes the Protocol placement codec, which a Release View binding composes, then Session
|
|
12
|
+
* endpoint policy; the client interprets neither its handshake nor its host. The issuer is whoever
|
|
13
|
+
* answers for the View's code: the Domain that declares it, or for an instance its own Identity,
|
|
14
|
+
* self-issued by this Kernel (`register` mode `self`), never its Class Domain nor an external
|
|
15
|
+
* issuer other Identities share.
|
|
16
|
+
*/
|
|
17
|
+
export function admitView(node, declaration, kernel, policy) {
|
|
18
|
+
// A Kernel View Node is declared by projection, which attaches it to its Domain; an instance is
|
|
19
|
+
// a Node of a Domain Class. One read without its of_domain Edge cannot name it.
|
|
20
|
+
if (declaration === undefined && node.class === VIEW) {
|
|
21
|
+
throw new ClientError('The Domain that declares this View is not readable.');
|
|
22
|
+
}
|
|
23
|
+
const value = node.props[PLACEMENT];
|
|
24
|
+
if (value === undefined)
|
|
25
|
+
throw new ClientError('Node is not a placed View.');
|
|
26
|
+
const placement = admitViewPlacement(value, policy);
|
|
27
|
+
const named = declaration === undefined
|
|
28
|
+
? { path: Path.id(node.id).raw, domain: DefinitionKey.origin(node.class) }
|
|
29
|
+
: declaredView(declaration);
|
|
30
|
+
const answerer = declaration === undefined ? selfIssuer(node, kernel) : domainIssuer(declaration);
|
|
31
|
+
const description = node.props[DESCRIPTION];
|
|
32
|
+
return Object.freeze({
|
|
33
|
+
...named,
|
|
34
|
+
...(typeof description === 'string' ? { description } : {}),
|
|
35
|
+
placement,
|
|
36
|
+
...(answerer === undefined ? {} : { issuer: answerer }),
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/** The Path and Domain of a View its Domain declares, from its Declaration. */
|
|
40
|
+
export function declaredView(declaration) {
|
|
41
|
+
const origin = declaration.domain.props[ORIGIN];
|
|
42
|
+
const name = typeof declaration.member === 'string' ? MEMBER.exec(declaration.member)?.[1] : undefined;
|
|
43
|
+
const key = `${String(origin)}:${String(declaration.member)}`;
|
|
44
|
+
if (typeof origin !== 'string' || name === undefined || !DefinitionKey.is(key)) {
|
|
45
|
+
throw new ClientError('Declared View member is invalid.');
|
|
46
|
+
}
|
|
47
|
+
const domain = DefinitionKey.origin(key);
|
|
48
|
+
return Object.freeze({ path: Path.project({ origin: domain, kind: 'view', name }).raw, domain });
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Admit the placement a View Node carries with the Protocol placement codec, which a Release View
|
|
52
|
+
* binding composes, then Session endpoint policy.
|
|
53
|
+
*/
|
|
54
|
+
export function admitViewPlacement(value, policy) {
|
|
55
|
+
let placement;
|
|
56
|
+
try {
|
|
57
|
+
placement = acceptViewPlacement(value);
|
|
58
|
+
}
|
|
59
|
+
catch (cause) {
|
|
60
|
+
if (!(cause instanceof PlacementError))
|
|
61
|
+
throw cause;
|
|
62
|
+
throw new ClientError('View placement is invalid.', { cause });
|
|
63
|
+
}
|
|
64
|
+
allowEndpoint(placement.href, policy);
|
|
65
|
+
return placement;
|
|
66
|
+
}
|
|
67
|
+
function domainIssuer({ domain }) {
|
|
68
|
+
const served = domain.props[ISSUER];
|
|
69
|
+
return issuer.is(served) ? served : undefined;
|
|
70
|
+
}
|
|
71
|
+
function selfIssuer(node, kernel) {
|
|
72
|
+
const registered = node.props[ISSUER];
|
|
73
|
+
return issuer.is(registered) && register.isSelfIssuer(kernel, registered) ? registered : undefined;
|
|
74
|
+
}
|
package/dist/store/memory.d.ts
CHANGED
|
@@ -34,15 +34,13 @@ export declare class GraphMemory {
|
|
|
34
34
|
optimistic(): void;
|
|
35
35
|
settleOptimistic(): void;
|
|
36
36
|
sweep(): void;
|
|
37
|
+
private rematerialize;
|
|
37
38
|
private releaseExpired;
|
|
38
39
|
private evictLeastRecentlyUsed;
|
|
39
|
-
private releasable;
|
|
40
|
-
private touch;
|
|
41
40
|
private sweepEntities;
|
|
42
41
|
close(error: ClientError): void;
|
|
43
42
|
private materialize;
|
|
44
43
|
private materializeResult;
|
|
45
|
-
private publish;
|
|
46
44
|
private schedule;
|
|
47
45
|
private drain;
|
|
48
46
|
private report;
|
package/dist/store/memory.js
CHANGED
|
@@ -4,6 +4,10 @@ import { retainDerivedQueryProvenance } from '../graph/provenance.js';
|
|
|
4
4
|
import { EntityIndex } from './entity-index.js';
|
|
5
5
|
import { OptimisticOverlay } from './optimistic.js';
|
|
6
6
|
import { COLD_SNAPSHOT, QueryShape } from './shape.js';
|
|
7
|
+
const NO_REFERENCES = Object.freeze({
|
|
8
|
+
nodes: Object.freeze([]),
|
|
9
|
+
edges: Object.freeze([]),
|
|
10
|
+
});
|
|
7
11
|
export class GraphMemory {
|
|
8
12
|
options;
|
|
9
13
|
entities = new EntityIndex();
|
|
@@ -18,7 +22,7 @@ export class GraphMemory {
|
|
|
18
22
|
const existing = this.shapes.get(key);
|
|
19
23
|
const now = this.options.now();
|
|
20
24
|
if (existing !== undefined) {
|
|
21
|
-
|
|
25
|
+
existing.touch(now);
|
|
22
26
|
return existing;
|
|
23
27
|
}
|
|
24
28
|
this.releaseExpired(now);
|
|
@@ -27,76 +31,83 @@ export class GraphMemory {
|
|
|
27
31
|
if (this.shapes.size >= this.options.maximumShapes) {
|
|
28
32
|
throw new ClientError('GraphStore shape capacity is exhausted by observed or in-flight queries.');
|
|
29
33
|
}
|
|
30
|
-
const shape = new QueryShape(key, ast, page, expected, now);
|
|
31
|
-
shape.releasedAt = now;
|
|
34
|
+
const shape = new QueryShape(key, ast, page, expected, now, (error) => this.report(error));
|
|
32
35
|
this.shapes.set(key, shape);
|
|
33
36
|
return shape;
|
|
34
37
|
}
|
|
35
38
|
get(key) {
|
|
36
39
|
const shape = this.shapes.get(key);
|
|
37
40
|
if (shape !== undefined)
|
|
38
|
-
|
|
41
|
+
shape.touch(this.options.now());
|
|
39
42
|
return shape;
|
|
40
43
|
}
|
|
41
44
|
async read(shape, options) {
|
|
42
45
|
this.options.requireOpen();
|
|
43
46
|
requireReadLive(options.signal, this.options.lifecycle);
|
|
44
|
-
const
|
|
47
|
+
const prior = shape.answer;
|
|
48
|
+
const reusable = prior !== undefined &&
|
|
45
49
|
shape.state === 'ready' &&
|
|
46
|
-
|
|
47
|
-
this.options.now() - shape.refreshedAt < this.options.ttlMs;
|
|
50
|
+
this.options.now() - prior.refreshedAt < this.options.ttlMs;
|
|
48
51
|
if (options.freshness !== 'refresh' && reusable)
|
|
49
|
-
return
|
|
52
|
+
return prior.response;
|
|
50
53
|
if (shape.inflight !== undefined) {
|
|
51
54
|
return observeRead(shape.inflight, options, this.options.lifecycle);
|
|
52
55
|
}
|
|
53
|
-
const prior = shape.response;
|
|
54
56
|
const invalidation = shape.invalidation;
|
|
55
|
-
|
|
57
|
+
shape.publish(prior === undefined ? 'loading' : this.overlay.hasAny() ? 'optimistic' : 'refreshing');
|
|
56
58
|
const inflight = this.schedule(async () => {
|
|
57
59
|
const result = await this.options.graph.query(shape.ast, queryOptions(shape.page, this.options.lifecycle, shape.expected));
|
|
58
60
|
this.options.requireOpen();
|
|
59
61
|
if (shape.invalidation !== invalidation) {
|
|
60
|
-
|
|
62
|
+
shape.publish(prior === undefined ? 'cold' : this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
61
63
|
return result;
|
|
62
64
|
}
|
|
65
|
+
// An uncertified read may hold facts the active schema refutes: it stays raw on its own
|
|
66
|
+
// shape and never enters, or is derived from, the shared entity cache.
|
|
67
|
+
if (result.certification === 'uncertified') {
|
|
68
|
+
const answer = {
|
|
69
|
+
confirmed: result,
|
|
70
|
+
references: NO_REFERENCES,
|
|
71
|
+
response: result,
|
|
72
|
+
refreshedAt: this.options.now(),
|
|
73
|
+
};
|
|
74
|
+
shape.settle(answer, 'ready');
|
|
75
|
+
return answer.response;
|
|
76
|
+
}
|
|
63
77
|
const integration = this.entities.integrate(result.result);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
const answer = {
|
|
79
|
+
confirmed: result,
|
|
80
|
+
references: integration.references,
|
|
81
|
+
response: this.materialize(result, integration.references),
|
|
82
|
+
refreshedAt: this.options.now(),
|
|
83
|
+
};
|
|
84
|
+
shape.settle(answer, this.overlay.hasAny() ? 'optimistic' : 'ready');
|
|
70
85
|
for (const other of this.shapes.values()) {
|
|
71
|
-
if (other === shape
|
|
86
|
+
if (other === shape)
|
|
72
87
|
continue;
|
|
73
88
|
if (other.referencesAny(integration.changedNodes, integration.changedEdges)) {
|
|
74
|
-
|
|
75
|
-
this.publish(other, other.state);
|
|
89
|
+
this.rematerialize(other, other.state);
|
|
76
90
|
}
|
|
77
91
|
}
|
|
78
|
-
return
|
|
92
|
+
return answer.response;
|
|
79
93
|
})
|
|
80
94
|
.catch((error) => {
|
|
81
95
|
if (prior === undefined)
|
|
82
|
-
|
|
83
|
-
else
|
|
84
|
-
shape.
|
|
85
|
-
this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
86
|
-
}
|
|
96
|
+
shape.publish('failed', error);
|
|
97
|
+
else
|
|
98
|
+
shape.show(prior, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
87
99
|
throw error;
|
|
88
100
|
})
|
|
89
101
|
.finally(() => {
|
|
90
|
-
if (shape.inflight
|
|
102
|
+
if (!shape.finish(inflight))
|
|
91
103
|
return;
|
|
92
|
-
shape.inflight = undefined;
|
|
93
104
|
if (shape.invalidation !== invalidation &&
|
|
94
105
|
shape.observed &&
|
|
95
106
|
!this.options.lifecycle.aborted) {
|
|
96
107
|
void this.read(shape, { page: shape.page }).catch((error) => this.report(error));
|
|
97
108
|
}
|
|
98
109
|
});
|
|
99
|
-
shape.inflight
|
|
110
|
+
shape.begin(inflight);
|
|
100
111
|
return observeRead(inflight, options, this.options.lifecycle);
|
|
101
112
|
}
|
|
102
113
|
snapshot(shape) {
|
|
@@ -104,22 +115,20 @@ export class GraphMemory {
|
|
|
104
115
|
}
|
|
105
116
|
subscribe(shape, listener) {
|
|
106
117
|
this.options.requireOpen();
|
|
107
|
-
shape.
|
|
108
|
-
shape.accessedAt = this.options.now();
|
|
109
|
-
shape.releasedAt = undefined;
|
|
118
|
+
shape.listen(listener, this.options.now());
|
|
110
119
|
return () => {
|
|
111
|
-
shape.
|
|
120
|
+
shape.unlisten(listener);
|
|
112
121
|
if (!shape.observed)
|
|
113
|
-
shape.
|
|
122
|
+
shape.release(this.options.now());
|
|
114
123
|
};
|
|
115
124
|
}
|
|
116
125
|
invalidate(shape) {
|
|
117
126
|
this.options.requireOpen();
|
|
118
127
|
for (const candidate of shape === undefined ? this.shapes.values() : [shape]) {
|
|
119
|
-
candidate.
|
|
120
|
-
if (candidate.
|
|
128
|
+
candidate.invalidate();
|
|
129
|
+
if (candidate.answer === undefined)
|
|
121
130
|
continue;
|
|
122
|
-
|
|
131
|
+
candidate.publish(this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
123
132
|
if (candidate.observed && candidate.inflight === undefined) {
|
|
124
133
|
void this.read(candidate, { page: candidate.page }).catch((error) => this.report(error));
|
|
125
134
|
}
|
|
@@ -127,27 +136,31 @@ export class GraphMemory {
|
|
|
127
136
|
}
|
|
128
137
|
optimistic() {
|
|
129
138
|
for (const shape of this.shapes.values()) {
|
|
130
|
-
if (shape.
|
|
139
|
+
if (shape.answer?.confirmed.certification === 'uncertified')
|
|
131
140
|
continue;
|
|
132
|
-
|
|
133
|
-
this.publish(shape, 'optimistic');
|
|
141
|
+
this.rematerialize(shape, 'optimistic');
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
144
|
settleOptimistic() {
|
|
137
145
|
for (const shape of this.shapes.values()) {
|
|
138
|
-
|
|
139
|
-
continue;
|
|
140
|
-
shape.response = this.materialize(shape.confirmed, shape.references);
|
|
141
|
-
this.publish(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
146
|
+
this.rematerialize(shape, this.overlay.hasAny() ? 'optimistic' : 'stale');
|
|
142
147
|
}
|
|
143
148
|
}
|
|
144
149
|
sweep() {
|
|
145
150
|
this.options.requireOpen();
|
|
146
151
|
this.releaseExpired(this.options.now());
|
|
147
152
|
}
|
|
153
|
+
rematerialize(shape, state) {
|
|
154
|
+
const answer = shape.answer;
|
|
155
|
+
if (answer === undefined)
|
|
156
|
+
return;
|
|
157
|
+
const response = this.materialize(answer.confirmed, answer.references);
|
|
158
|
+
shape.show({ ...answer, response }, state);
|
|
159
|
+
}
|
|
148
160
|
releaseExpired(now) {
|
|
149
161
|
for (const [key, shape] of this.shapes) {
|
|
150
|
-
|
|
162
|
+
const idleSince = shape.idleSince();
|
|
163
|
+
if (idleSince !== undefined && now - idleSince >= this.options.ttlMs) {
|
|
151
164
|
this.shapes.delete(key);
|
|
152
165
|
}
|
|
153
166
|
}
|
|
@@ -156,7 +169,7 @@ export class GraphMemory {
|
|
|
156
169
|
evictLeastRecentlyUsed() {
|
|
157
170
|
let oldest;
|
|
158
171
|
for (const shape of this.shapes.values()) {
|
|
159
|
-
if (
|
|
172
|
+
if (shape.idleSince() === undefined)
|
|
160
173
|
continue;
|
|
161
174
|
if (oldest === undefined || shape.accessedAt < oldest.accessedAt)
|
|
162
175
|
oldest = shape;
|
|
@@ -166,28 +179,18 @@ export class GraphMemory {
|
|
|
166
179
|
this.sweepEntities();
|
|
167
180
|
}
|
|
168
181
|
}
|
|
169
|
-
releasable(shape) {
|
|
170
|
-
return !shape.observed && shape.inflight === undefined && shape.releasedAt !== undefined;
|
|
171
|
-
}
|
|
172
|
-
touch(shape, now) {
|
|
173
|
-
shape.accessedAt = now;
|
|
174
|
-
if (!shape.observed)
|
|
175
|
-
shape.releasedAt = now;
|
|
176
|
-
}
|
|
177
182
|
sweepEntities() {
|
|
178
183
|
const nodes = new Set();
|
|
179
184
|
const edges = new Set();
|
|
180
185
|
for (const shape of this.shapes.values()) {
|
|
181
|
-
shape.references
|
|
182
|
-
shape.references
|
|
186
|
+
shape.answer?.references.nodes.forEach((id) => nodes.add(id));
|
|
187
|
+
shape.answer?.references.edges.forEach((key) => edges.add(key));
|
|
183
188
|
}
|
|
184
189
|
this.entities.sweep(nodes, edges);
|
|
185
190
|
}
|
|
186
191
|
close(error) {
|
|
187
|
-
for (const shape of this.shapes.values())
|
|
188
|
-
shape.
|
|
189
|
-
shape.inflight?.catch(() => undefined);
|
|
190
|
-
}
|
|
192
|
+
for (const shape of this.shapes.values())
|
|
193
|
+
shape.close();
|
|
191
194
|
this.shapes.clear();
|
|
192
195
|
this.entities.clear();
|
|
193
196
|
this.overlay.clear();
|
|
@@ -195,9 +198,13 @@ export class GraphMemory {
|
|
|
195
198
|
this.pending.shift()?.reject(error);
|
|
196
199
|
}
|
|
197
200
|
materialize(template, references) {
|
|
201
|
+
// Optimistic overlays and cache refreshes never rewrite an uncertified read.
|
|
202
|
+
if (template.certification === 'uncertified')
|
|
203
|
+
return template;
|
|
198
204
|
const response = Object.freeze({
|
|
199
205
|
result: this.materializeResult(template.result, references),
|
|
200
206
|
page: template.page,
|
|
207
|
+
...(template.certification === undefined ? {} : { certification: template.certification }),
|
|
201
208
|
});
|
|
202
209
|
return retainDerivedQueryProvenance(template, response);
|
|
203
210
|
}
|
|
@@ -270,21 +277,6 @@ export class GraphMemory {
|
|
|
270
277
|
? Object.freeze({ kind: 'graph', graph, selection })
|
|
271
278
|
: Object.freeze({ kind: 'graph', graph, selection });
|
|
272
279
|
}
|
|
273
|
-
publish(shape, state, error) {
|
|
274
|
-
const next = snapshotFor(state, shape.response, error);
|
|
275
|
-
if (sameSnapshot(shape.snapshot, next))
|
|
276
|
-
return;
|
|
277
|
-
shape.state = state;
|
|
278
|
-
shape.snapshot = next;
|
|
279
|
-
for (const listener of Array.from(shape.listeners)) {
|
|
280
|
-
try {
|
|
281
|
-
listener();
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
this.report(error);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
280
|
schedule(task) {
|
|
289
281
|
return new Promise((resolve, reject) => {
|
|
290
282
|
const run = () => {
|
|
@@ -301,8 +293,11 @@ export class GraphMemory {
|
|
|
301
293
|
});
|
|
302
294
|
}
|
|
303
295
|
drain() {
|
|
304
|
-
while (this.active < this.options.concurrency
|
|
305
|
-
this.pending.shift()
|
|
296
|
+
while (this.active < this.options.concurrency) {
|
|
297
|
+
const task = this.pending.shift();
|
|
298
|
+
if (task === undefined)
|
|
299
|
+
return;
|
|
300
|
+
task.run();
|
|
306
301
|
}
|
|
307
302
|
}
|
|
308
303
|
report(error) {
|
|
@@ -349,7 +344,7 @@ function observeRead(pending, options, lifecycle) {
|
|
|
349
344
|
}
|
|
350
345
|
return new Promise((resolve, reject) => {
|
|
351
346
|
let timeout;
|
|
352
|
-
const callerCancelled = () => settleReject(cancellation(options.signal
|
|
347
|
+
const callerCancelled = () => settleReject(cancellation(options.signal?.reason));
|
|
353
348
|
const storeClosed = () => settleReject(cancellation(lifecycle.reason));
|
|
354
349
|
let settled = false;
|
|
355
350
|
const cleanup = () => {
|
|
@@ -391,30 +386,3 @@ function cancellation(reason) {
|
|
|
391
386
|
? reason
|
|
392
387
|
: new ClientError('Store read was cancelled.', { cause: reason });
|
|
393
388
|
}
|
|
394
|
-
function snapshotFor(state, response, error) {
|
|
395
|
-
switch (state) {
|
|
396
|
-
case 'cold':
|
|
397
|
-
return Object.freeze({ state });
|
|
398
|
-
case 'loading':
|
|
399
|
-
return Object.freeze({ state });
|
|
400
|
-
case 'failed':
|
|
401
|
-
return Object.freeze({ state, error, ...(response === undefined ? {} : { response }) });
|
|
402
|
-
case 'ready':
|
|
403
|
-
case 'stale':
|
|
404
|
-
case 'refreshing':
|
|
405
|
-
case 'optimistic':
|
|
406
|
-
if (response === undefined)
|
|
407
|
-
throw new TypeError(`Store ${state} snapshot requires a response.`);
|
|
408
|
-
return Object.freeze({ state, response });
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
function sameSnapshot(left, right) {
|
|
412
|
-
if (left.state !== right.state)
|
|
413
|
-
return false;
|
|
414
|
-
if ('response' in left || 'response' in right) {
|
|
415
|
-
if (!('response' in left) || !('response' in right) || left.response !== right.response) {
|
|
416
|
-
return false;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
return left.state !== 'failed' || right.state !== 'failed' || left.error === right.error;
|
|
420
|
-
}
|