@astrale-os/kernel-client 1.0.0-beta.4 → 1.0.0-beta.41
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 +24 -0
- package/dist/errors/refusal.js +78 -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 +66 -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 +149 -7
- package/dist/graph/query.typecheck.d.ts +79 -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 +20 -2
- package/dist/session/content/api.js +17 -7
- package/dist/session/content/files.d.ts +12 -0
- package/dist/session/content/files.js +47 -0
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +13 -2
- package/dist/session/content/input.js +25 -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 +5 -4
- 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 +27 -6
- package/dist/session/reference.js +18 -3
- 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 +67 -20
- 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 +9 -7
- 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
|
@@ -7,7 +7,6 @@ export class RouteCache {
|
|
|
7
7
|
maximumAgeMs;
|
|
8
8
|
maximumSize;
|
|
9
9
|
entries = new Map();
|
|
10
|
-
keyTokens = new Map();
|
|
11
10
|
store;
|
|
12
11
|
constructor(maximumAgeMs, maximumSize = DEFAULT_MAXIMUM_ROUTES, store) {
|
|
13
12
|
this.maximumAgeMs = maximumAgeMs;
|
|
@@ -16,27 +15,27 @@ export class RouteCache {
|
|
|
16
15
|
this.restore();
|
|
17
16
|
}
|
|
18
17
|
async key(source, call, auth) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
if (auth.kind === 'anonymous') {
|
|
19
|
+
return JSON.stringify([source, call.target, 'anonymous', 'anonymous']);
|
|
20
|
+
}
|
|
21
|
+
const credential = await digestText(auth.credential);
|
|
22
|
+
const delegate = await digestText(JSON.stringify({ ttlSeconds: auth.delegate.ttlSeconds }));
|
|
23
23
|
return JSON.stringify([source, call.target, credential, delegate]);
|
|
24
24
|
}
|
|
25
25
|
async get(key, sourceCredential, now, operationDeadline) {
|
|
26
|
-
const
|
|
27
|
-
|
|
26
|
+
const entry = this.entries.get(key);
|
|
27
|
+
const cached = entry?.cached;
|
|
28
|
+
if (entry === undefined || cached === undefined)
|
|
28
29
|
return undefined;
|
|
29
|
-
const token = this.keyTokens.get(key);
|
|
30
30
|
if (cached.learned.expiresAt <= (now ?? Date.now())) {
|
|
31
31
|
this.invalidate(key);
|
|
32
32
|
return undefined;
|
|
33
33
|
}
|
|
34
|
-
if (cached.
|
|
34
|
+
if (cached.persisted !== undefined) {
|
|
35
35
|
const matches = sourceCredential !== undefined &&
|
|
36
|
-
(await matchesIntegrity(sourceCredential, key, cached.sealed
|
|
37
|
-
if (this.
|
|
36
|
+
(await matchesIntegrity(sourceCredential, key, cached.persisted.sealed, cached.persisted.integrity));
|
|
37
|
+
if (this.entries.get(key) !== entry)
|
|
38
38
|
return undefined;
|
|
39
|
-
}
|
|
40
39
|
if (!matches) {
|
|
41
40
|
this.invalidate(key);
|
|
42
41
|
return undefined;
|
|
@@ -53,91 +52,100 @@ export class RouteCache {
|
|
|
53
52
|
// This operation needs a fresh carrier; a shorter concurrent operation may still reuse it.
|
|
54
53
|
return undefined;
|
|
55
54
|
}
|
|
56
|
-
this.
|
|
57
|
-
this.entries.set(key, cached);
|
|
55
|
+
this.touch(key, entry);
|
|
58
56
|
return cached.learned;
|
|
59
57
|
}
|
|
60
58
|
async learn(key, route, credential, sourceCredential, now = Date.now()) {
|
|
61
59
|
const expiry = credential === undefined ? undefined : credentialExpiry(credential);
|
|
62
60
|
const credentialRefreshBoundary = expiry === undefined ? undefined : refreshBoundary(expiry);
|
|
63
61
|
const expiresAt = Math.min(now + this.maximumAgeMs, refreshBoundary(expiry));
|
|
64
|
-
const
|
|
62
|
+
const learned = Object.freeze({
|
|
65
63
|
route,
|
|
66
64
|
...(credential === undefined ? {} : { credential }),
|
|
67
65
|
expiresAt,
|
|
68
66
|
});
|
|
69
67
|
// A carrier with no supported finite expiry may be used for this follow, but never warmed.
|
|
70
68
|
if (credential !== undefined && expiry === undefined)
|
|
71
|
-
return
|
|
69
|
+
return learned;
|
|
72
70
|
if (expiresAt <= now)
|
|
73
|
-
return
|
|
74
|
-
const
|
|
71
|
+
return learned;
|
|
72
|
+
const reserved = this.reserve(key);
|
|
75
73
|
let integrity;
|
|
76
74
|
if (this.store !== undefined && sourceCredential !== undefined) {
|
|
77
75
|
try {
|
|
78
|
-
integrity = await routeIntegrity(sourceCredential, key,
|
|
76
|
+
integrity = await routeIntegrity(sourceCredential, key, learned);
|
|
79
77
|
}
|
|
80
78
|
catch {
|
|
81
79
|
// Integrity support is a persistence optimization; the learned route remains usable in memory.
|
|
82
80
|
}
|
|
83
81
|
}
|
|
84
|
-
if (this.
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
while (this.entries.size >= this.maximumSize) {
|
|
89
|
-
const oldest = this.entries.keys().next().value;
|
|
90
|
-
if (oldest === undefined)
|
|
91
|
-
break;
|
|
92
|
-
this.entries.delete(oldest);
|
|
93
|
-
this.keyTokens.delete(oldest);
|
|
94
|
-
}
|
|
95
|
-
this.entries.set(key, Object.freeze({
|
|
96
|
-
learned: entry,
|
|
82
|
+
if (this.entries.get(key) !== reserved)
|
|
83
|
+
return learned;
|
|
84
|
+
this.warm(key, Object.freeze({
|
|
85
|
+
learned,
|
|
97
86
|
...(credentialRefreshBoundary === undefined ? {} : { credentialRefreshBoundary }),
|
|
98
|
-
...(integrity === undefined
|
|
87
|
+
...(integrity === undefined
|
|
88
|
+
? {}
|
|
89
|
+
: { persisted: Object.freeze({ sealed: learned, integrity }) }),
|
|
99
90
|
}));
|
|
100
91
|
this.persist();
|
|
101
|
-
return
|
|
92
|
+
return learned;
|
|
102
93
|
}
|
|
103
94
|
delete(key) {
|
|
104
95
|
this.invalidate(key);
|
|
105
96
|
}
|
|
106
97
|
/** Clear this Session's memory without deleting caller-owned persistent state. */
|
|
107
98
|
clear() {
|
|
108
|
-
this.keyTokens.clear();
|
|
109
99
|
this.entries.clear();
|
|
110
100
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
101
|
+
/** Mark the key's entry most recently used. */
|
|
102
|
+
touch(key, entry) {
|
|
103
|
+
this.entries.delete(key);
|
|
104
|
+
this.entries.set(key, entry);
|
|
115
105
|
}
|
|
106
|
+
/** Fence any check in flight on the key; its warm route, if any, stays usable in place. */
|
|
116
107
|
reserve(key) {
|
|
117
|
-
const
|
|
118
|
-
this.
|
|
119
|
-
return
|
|
108
|
+
const reserved = Object.freeze({ ...this.entries.get(key) });
|
|
109
|
+
this.entries.set(key, reserved);
|
|
110
|
+
return reserved;
|
|
111
|
+
}
|
|
112
|
+
/** Make the key the newest warm entry, evicting the least recently used beyond the bound. */
|
|
113
|
+
warm(key, cached) {
|
|
114
|
+
this.entries.delete(key);
|
|
115
|
+
const warmKeys = [...this.entries].flatMap(([other, entry]) => entry.cached === undefined ? [] : [other]);
|
|
116
|
+
while (warmKeys.length >= this.maximumSize) {
|
|
117
|
+
const oldest = warmKeys.shift();
|
|
118
|
+
if (oldest === undefined)
|
|
119
|
+
break;
|
|
120
|
+
this.entries.delete(oldest);
|
|
121
|
+
}
|
|
122
|
+
this.entries.set(key, Object.freeze({ cached }));
|
|
123
|
+
}
|
|
124
|
+
invalidate(key) {
|
|
125
|
+
const entry = this.entries.get(key);
|
|
126
|
+
this.entries.delete(key);
|
|
127
|
+
if (entry?.cached !== undefined)
|
|
128
|
+
this.persist();
|
|
120
129
|
}
|
|
121
130
|
restore(now = Date.now()) {
|
|
122
131
|
if (this.store === undefined)
|
|
123
132
|
return;
|
|
124
133
|
try {
|
|
125
134
|
const restored = restoreRoutes(this.store.read(), now, this.maximumAgeMs, this.maximumSize);
|
|
126
|
-
for (const [key,
|
|
127
|
-
this.
|
|
128
|
-
this.entries.set(key, entry);
|
|
129
|
-
}
|
|
135
|
+
for (const [key, cached] of restored)
|
|
136
|
+
this.entries.set(key, Object.freeze({ cached }));
|
|
130
137
|
}
|
|
131
138
|
catch {
|
|
132
|
-
|
|
139
|
+
// An unreadable store restores nothing; the Session starts cold.
|
|
133
140
|
this.entries.clear();
|
|
134
141
|
}
|
|
135
142
|
}
|
|
136
143
|
persist() {
|
|
137
144
|
if (this.store === undefined)
|
|
138
145
|
return;
|
|
146
|
+
const warmRoutes = [...this.entries].flatMap(([key, entry]) => entry.cached === undefined ? [] : [[key, entry.cached]]);
|
|
139
147
|
try {
|
|
140
|
-
this.store.write(routeArtifact(
|
|
148
|
+
this.store.write(routeArtifact(warmRoutes));
|
|
141
149
|
}
|
|
142
150
|
catch {
|
|
143
151
|
// A cache-store outage must not turn a valid invocation into a product failure.
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { IssuerId } from '@astrale-os/kernel-core/auth/issuer';
|
|
2
|
+
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
3
|
import type { Result } from '../../client/index.js';
|
|
4
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
3
5
|
import type { Fetch } from '../../transport/http/index.js';
|
|
4
6
|
import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
5
|
-
import type { AuthExchange, SessionAuth } from './auth.js';
|
|
7
|
+
import type { AuthExchange, SessionAuth, EffectiveAuth } from './auth.js';
|
|
6
8
|
import type { RouteCache } from './cache.js';
|
|
7
9
|
import type { ClientPoolCapability } from './envelope.js';
|
|
8
10
|
import type { SessionEnvelopeTransport, SessionRequestOptions } from './request.js';
|
|
@@ -24,16 +26,37 @@ export interface SessionDispatcherOptions {
|
|
|
24
26
|
readonly timeoutMs: number;
|
|
25
27
|
};
|
|
26
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* A streaming call is forced over HTTP and may carry an admitted binary body, whose kind says
|
|
31
|
+
* whether it can be sent again: a Blob is read anew by every send, a stream only by the first.
|
|
32
|
+
*/
|
|
33
|
+
export interface Streaming {
|
|
34
|
+
readonly body?: BinaryBody;
|
|
35
|
+
}
|
|
36
|
+
/** One Call with the authority it presents on every dispatch, and the route partition it selects. */
|
|
37
|
+
export interface ResolvedCall {
|
|
38
|
+
readonly call: invocation.Call;
|
|
39
|
+
readonly auth: EffectiveAuth;
|
|
40
|
+
readonly source: IssuerId;
|
|
41
|
+
readonly cacheKey: string;
|
|
42
|
+
}
|
|
27
43
|
export declare class SessionDispatcher {
|
|
28
44
|
private readonly state;
|
|
29
45
|
constructor(state: SessionDispatcherOptions);
|
|
30
46
|
dispatch(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation): Promise<Result>;
|
|
31
47
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
32
|
-
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?:
|
|
33
|
-
|
|
48
|
+
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?: BinaryBody): Promise<Result>;
|
|
49
|
+
/** Resolve the Session authority of one Call: each dispatch of the result presents it. */
|
|
50
|
+
resolve(input: Parameters<typeof acceptCall>[0], operation: SessionOperation): Promise<ResolvedCall>;
|
|
51
|
+
/**
|
|
52
|
+
* Dispatch a resolved Call. Sending it again presents the same principal and caller, which an
|
|
53
|
+
* idempotency key needs to name the same Execution.
|
|
54
|
+
*/
|
|
55
|
+
dispatchResolved(resolved: ResolvedCall, options: SessionRequestOptions, operation: SessionOperation, streaming?: Streaming): Promise<Result>;
|
|
34
56
|
/** The cached route for this partition, evicted when it no longer passes delivery checks. */
|
|
35
57
|
private warmRoute;
|
|
36
58
|
private dispatchSource;
|
|
59
|
+
/** The source answers routing control only as the public error: it has no route to recover. */
|
|
37
60
|
private followSource;
|
|
38
61
|
private dispatchDestinationWithRecovery;
|
|
39
62
|
private dispatchDestination;
|
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { acceptCall, Client, dispatchTrace } from '../../client/index.js';
|
|
1
|
+
import { acceptCall, Client, dispatchInternal, isRoutingControl, routingControlError, } from '../../client/index.js';
|
|
3
2
|
import { ClientError } from '../../errors/index.js';
|
|
4
3
|
import { runSessionOperation, remainingSessionTime, sessionTransportOptions, allowEndpoint, } from '../context/index.js';
|
|
5
4
|
import { resolveAuth } from './auth.js';
|
|
6
5
|
import { selectEnvelope, selectEnvelopeTarget } from './envelope.js';
|
|
7
|
-
import {
|
|
6
|
+
import { releaseRoutesDeclaration } from './legacy/release-route.js';
|
|
7
|
+
import { isRouteCompatibilityConflict, recoveryRefresh } from './recovery.js';
|
|
8
8
|
export class SessionDispatcher {
|
|
9
9
|
state;
|
|
10
10
|
constructor(state) {
|
|
11
11
|
this.state = state;
|
|
12
12
|
}
|
|
13
13
|
async dispatch(input, options, operation) {
|
|
14
|
-
return this.
|
|
14
|
+
return this.dispatchResolved(await this.resolve(input, operation), options, operation);
|
|
15
15
|
}
|
|
16
16
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
17
17
|
async dispatchHttp(input, options, operation, body) {
|
|
18
|
-
return this.
|
|
18
|
+
return this.dispatchResolved(await this.resolve(input, operation), options, operation, { body });
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
/** Resolve the Session authority of one Call: each dispatch of the result presents it. */
|
|
21
|
+
async resolve(input, operation) {
|
|
21
22
|
this.state.requireOpen();
|
|
22
23
|
const call = acceptCall(input);
|
|
23
24
|
// Authority is resolved before warm lookup so changed credentials or delegation cannot reuse a
|
|
@@ -28,81 +29,113 @@ export class SessionDispatcher {
|
|
|
28
29
|
: await this.state.exchange.resolve(call, resolved, operation);
|
|
29
30
|
const source = this.state.kernel;
|
|
30
31
|
const cacheKey = await runSessionOperation(operation, () => this.state.cache.key(source, call, auth));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
return Object.freeze({ call, auth, source, cacheKey });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Dispatch a resolved Call. Sending it again presents the same principal and caller, which an
|
|
36
|
+
* idempotency key needs to name the same Execution.
|
|
37
|
+
*/
|
|
38
|
+
async dispatchResolved(resolved, options, operation, streaming) {
|
|
39
|
+
this.state.requireOpen();
|
|
40
|
+
const attempt = Object.freeze({
|
|
41
|
+
resolved,
|
|
42
|
+
options,
|
|
43
|
+
operation,
|
|
44
|
+
...(streaming === undefined ? {} : { streaming }),
|
|
45
|
+
});
|
|
46
|
+
const learned = await this.warmRoute(attempt);
|
|
47
|
+
if (learned !== undefined)
|
|
48
|
+
return this.dispatchDestinationWithRecovery(learned, attempt);
|
|
49
|
+
const result = await this.dispatchSource(attempt, undefined);
|
|
50
|
+
return this.followSource(result, attempt);
|
|
38
51
|
}
|
|
39
52
|
/** The cached route for this partition, evicted when it no longer passes delivery checks. */
|
|
40
|
-
async warmRoute(
|
|
41
|
-
const
|
|
53
|
+
async warmRoute(attempt) {
|
|
54
|
+
const { resolved, operation, streaming } = attempt;
|
|
55
|
+
const learned = await runSessionOperation(operation, () => this.state.cache.get(resolved.cacheKey, presentedCredential(resolved.auth), undefined, operation.deadline));
|
|
42
56
|
if (learned === undefined)
|
|
43
57
|
return undefined;
|
|
44
58
|
try {
|
|
45
|
-
requireLearnedRoute(learned,
|
|
59
|
+
requireLearnedRoute(learned, resolved.source, resolved.call.target);
|
|
46
60
|
selectEnvelopeTarget(learned.route.endpoint, streaming === undefined ? this.state.envelopeTransport : 'http', this.state.websocketAvailable, this.state.policy);
|
|
47
61
|
return learned;
|
|
48
62
|
}
|
|
49
63
|
catch {
|
|
50
|
-
this.state.cache.delete(
|
|
64
|
+
this.state.cache.delete(resolved.cacheKey);
|
|
51
65
|
return undefined;
|
|
52
66
|
}
|
|
53
67
|
}
|
|
54
|
-
async dispatchSource(
|
|
68
|
+
async dispatchSource(attempt, routing) {
|
|
69
|
+
const { resolved, options, operation, streaming } = attempt;
|
|
55
70
|
this.state.requireOpen();
|
|
56
71
|
remainingSessionTime(operation);
|
|
57
72
|
allowEndpoint(this.state.sourceEndpoint, this.state.policy);
|
|
58
73
|
const client = streaming === undefined
|
|
59
74
|
? this.state.pool.clientFor(Object.freeze({ transport: 'http', url: this.state.sourceEndpoint }))
|
|
60
75
|
: this.streamingClientFor(this.state.sourceEndpoint);
|
|
61
|
-
return
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
76
|
+
return sendWithin(operation, client, {
|
|
77
|
+
credential: presentedCredential(resolved.auth),
|
|
78
|
+
call: resolved.call,
|
|
79
|
+
options: {
|
|
80
|
+
...transportOptions(attempt),
|
|
81
|
+
...(resolved.auth.kind === 'anonymous' ? {} : { delegate: resolved.auth.delegate }),
|
|
82
|
+
...(routing === undefined ? {} : { routing }),
|
|
83
|
+
// The guard is the source Kernel's: a routed destination request never carries it.
|
|
84
|
+
...(options.maintenance === undefined ? {} : { maintenance: options.maintenance }),
|
|
85
|
+
},
|
|
86
|
+
body: streaming?.body,
|
|
87
|
+
});
|
|
66
88
|
}
|
|
67
|
-
|
|
89
|
+
/** The source answers routing control only as the public error: it has no route to recover. */
|
|
90
|
+
async followSource(result, attempt) {
|
|
91
|
+
if (isRoutingControl(result))
|
|
92
|
+
throw routingControlError(result);
|
|
68
93
|
if (result.kind !== 'redirect')
|
|
69
94
|
return result;
|
|
95
|
+
const { resolved, operation, streaming } = attempt;
|
|
96
|
+
requireResendable(streaming, 'follow a Redirect');
|
|
70
97
|
const redirect = result.redirect;
|
|
71
|
-
if (redirect.route.via.issuer !==
|
|
98
|
+
if (redirect.route.via.issuer !== resolved.source) {
|
|
72
99
|
throw new ClientError('Redirect issuer differs from the session-pinned source Kernel identity.');
|
|
73
100
|
}
|
|
74
|
-
|
|
75
|
-
const learned = await runSessionOperation(operation, () => this.state.cache.learn(
|
|
76
|
-
return this.dispatchDestinationWithRecovery(learned,
|
|
101
|
+
requireRouteForTarget(redirect.route, resolved.call.target);
|
|
102
|
+
const learned = await runSessionOperation(operation, () => this.state.cache.learn(resolved.cacheKey, redirect.route, redirect.credential, presentedCredential(resolved.auth)));
|
|
103
|
+
return this.dispatchDestinationWithRecovery(learned, attempt, redirect.trace);
|
|
77
104
|
}
|
|
78
|
-
async dispatchDestinationWithRecovery(learned,
|
|
105
|
+
async dispatchDestinationWithRecovery(learned, attempt, trace) {
|
|
106
|
+
const { resolved, operation, streaming } = attempt;
|
|
107
|
+
let result;
|
|
79
108
|
try {
|
|
80
|
-
|
|
109
|
+
result = await this.dispatchDestination(learned, attempt, trace);
|
|
81
110
|
}
|
|
82
111
|
catch (cause) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.state.cache.delete(state.cacheKey);
|
|
87
|
-
throw cause;
|
|
88
|
-
}
|
|
89
|
-
this.state.cache.delete(state.cacheKey);
|
|
90
|
-
if (!operation.claimRecovery())
|
|
91
|
-
throw cause;
|
|
92
|
-
const routing = recoveryRefresh(control, learned);
|
|
93
|
-
const refreshed = await this.dispatchSource(state, routing, options, operation, streaming);
|
|
94
|
-
return this.followSource(refreshed, state, options, operation, streaming);
|
|
112
|
+
if (isRouteCompatibilityConflict(cause))
|
|
113
|
+
this.state.cache.delete(resolved.cacheKey);
|
|
114
|
+
throw cause;
|
|
95
115
|
}
|
|
116
|
+
if (!isRoutingControl(result))
|
|
117
|
+
return result;
|
|
118
|
+
this.state.cache.delete(resolved.cacheKey);
|
|
119
|
+
const failure = routingControlError(result);
|
|
120
|
+
if (!operation.claimRecovery())
|
|
121
|
+
throw failure;
|
|
122
|
+
requireResendable(streaming, 'recover its route', failure);
|
|
123
|
+
const refreshed = await this.dispatchSource(attempt, recoveryRefresh(result, learned));
|
|
124
|
+
return this.followSource(refreshed, attempt);
|
|
96
125
|
}
|
|
97
|
-
async dispatchDestination(learned,
|
|
126
|
+
async dispatchDestination(learned, attempt, trace) {
|
|
127
|
+
const { resolved, operation, streaming } = attempt;
|
|
98
128
|
remainingSessionTime(operation);
|
|
99
|
-
requireLearnedRoute(learned, source, call.target);
|
|
129
|
+
requireLearnedRoute(learned, resolved.source, resolved.call.target);
|
|
100
130
|
const client = this.destinationClient(learned, streaming);
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
131
|
+
const options = { ...transportOptions(attempt), routing: learned.route.via };
|
|
132
|
+
const result = await sendWithin(operation, client, {
|
|
133
|
+
credential: learned.credential,
|
|
134
|
+
call: resolved.call,
|
|
135
|
+
options,
|
|
136
|
+
trace,
|
|
137
|
+
body: streaming?.body,
|
|
138
|
+
});
|
|
106
139
|
if (result.kind === 'redirect') {
|
|
107
140
|
throw new ClientError('A destination redirect is invalid; only the source Kernel may redirect.');
|
|
108
141
|
}
|
|
@@ -130,33 +163,45 @@ export class SessionDispatcher {
|
|
|
130
163
|
});
|
|
131
164
|
}
|
|
132
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* Send one request within the operation. Routing control that answers an interrupted operation
|
|
168
|
+
* yields its interruption, exactly as a failure inside the operation does: it is never recovered.
|
|
169
|
+
*/
|
|
170
|
+
async function sendWithin(operation, client, dispatch) {
|
|
171
|
+
const result = await runSessionOperation(operation, () => dispatchInternal(client, dispatch));
|
|
172
|
+
// Re-entering the operation is its own interruption check; it adds no work.
|
|
173
|
+
return isRoutingControl(result) ? runSessionOperation(operation, () => result) : result;
|
|
174
|
+
}
|
|
175
|
+
/** The credential a Call presents to the source Kernel; an anonymous Call presents none. */
|
|
176
|
+
function presentedCredential(auth) {
|
|
177
|
+
return auth.kind === 'anonymous' ? undefined : auth.credential;
|
|
178
|
+
}
|
|
133
179
|
/** A body-carrying streaming call omits the idempotency key; every other call keeps it. */
|
|
134
|
-
function transportOptions(operation, options, streaming) {
|
|
180
|
+
function transportOptions({ operation, options, streaming }) {
|
|
135
181
|
return streaming?.body === undefined
|
|
136
182
|
? sessionTransportOptions(operation, options.idempotencyKey)
|
|
137
|
-
:
|
|
183
|
+
: sessionTransportOptions(operation);
|
|
184
|
+
}
|
|
185
|
+
/** A stream body was consumed by the send that just answered: it is never sent again. */
|
|
186
|
+
function requireResendable(streaming, step, cause) {
|
|
187
|
+
if (streaming?.body?.kind !== 'once')
|
|
188
|
+
return;
|
|
189
|
+
throw new ClientError(`A call with a streamed binary body is sent once and cannot ${step}; send it again with a new stream.`, cause === undefined ? undefined : { cause });
|
|
138
190
|
}
|
|
139
191
|
function requireLearnedRoute(learned, source, target) {
|
|
140
192
|
if (learned.route.via.issuer !== source) {
|
|
141
193
|
throw new ClientError('Learned route issuer differs from the session-pinned source Kernel.');
|
|
142
194
|
}
|
|
143
|
-
|
|
195
|
+
requireRouteForTarget(learned.route, target);
|
|
144
196
|
}
|
|
145
|
-
|
|
197
|
+
/** A Target names a declaration: only a witness of that declaration routes it. */
|
|
198
|
+
function requireRouteForTarget(route, target) {
|
|
146
199
|
if (typeof target === 'string')
|
|
147
200
|
return;
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
function send(client, credential, call, options, trace) {
|
|
155
|
-
if (trace !== undefined)
|
|
156
|
-
return dispatchTrace(client, credential, call, options, trace);
|
|
157
|
-
const sender = credential === undefined ? client : client.as(credential);
|
|
158
|
-
// Both branches are needed: each narrows `options` to one `Client.dispatch` overload.
|
|
159
|
-
return options.body === undefined
|
|
160
|
-
? sender.dispatch(call, options)
|
|
161
|
-
: sender.dispatch(call, options);
|
|
201
|
+
const { via } = route;
|
|
202
|
+
const routes = 'function' in via
|
|
203
|
+
? via.function.declaration === target.callable
|
|
204
|
+
: releaseRoutesDeclaration(via, target.callable);
|
|
205
|
+
if (!routes)
|
|
206
|
+
throw new ClientError('Resolved route does not name the Target declaration.');
|
|
162
207
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type * as
|
|
1
|
+
import type * as placement from '@astrale-os/kernel-protocol/placement';
|
|
2
2
|
import type { Client } from '../../client/index.js';
|
|
3
3
|
import type { ClientTarget } from '../../pool/index.js';
|
|
4
4
|
import type { SessionPolicy } from '../context/index.js';
|
|
@@ -10,6 +10,6 @@ export interface EnvelopeSelection {
|
|
|
10
10
|
readonly client: Client;
|
|
11
11
|
readonly destination: ClientTarget;
|
|
12
12
|
}
|
|
13
|
-
export declare function selectEnvelope(endpoints:
|
|
13
|
+
export declare function selectEnvelope(endpoints: placement.InvocationEndpoints, preference: SessionEnvelopeTransport, websocketAvailable: boolean, pool: ClientPoolCapability, policy: Readonly<SessionPolicy>): EnvelopeSelection;
|
|
14
14
|
/** Resolve and admit one route endpoint without acquiring a transport Client. */
|
|
15
|
-
export declare function selectEnvelopeTarget(endpoints:
|
|
15
|
+
export declare function selectEnvelopeTarget(endpoints: placement.InvocationEndpoints, preference: SessionEnvelopeTransport, websocketAvailable: boolean, policy: Readonly<SessionPolicy>): ClientTarget;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { admitSessionAuth, resolveAuth, type Auth, type AuthExchange, type EffectiveAuth, type SessionAuth, } from './auth.js';
|
|
1
|
+
export { admitSessionAuth, resolveAuth, type Auth, type Authenticated, type AuthExchange, type EffectiveAuth, type SessionAuth, } from './auth.js';
|
|
2
2
|
export { RouteCache, type LearnedRoute } from './cache.js';
|
|
3
3
|
export { SessionDispatcher, type SessionDispatcherOptions } from './dispatch.js';
|
|
4
4
|
export { type ClientPoolCapability, type EnvelopeSelection } from './envelope.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Kernel routes a declaration by its Release witness while published destinations verify only it
|
|
3
|
+
* (`invocation/legacy/release-via.ts` of the Protocol). That witness names a Release, not the
|
|
4
|
+
* declaration: the Session can only check that the Release is of the origin that declares the
|
|
5
|
+
* Target.
|
|
6
|
+
*
|
|
7
|
+
* Retirement: once no supported Kernel routes a declaration by its Release, delete this folder.
|
|
8
|
+
*/
|
|
9
|
+
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
10
|
+
import { Key } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
11
|
+
/** @deprecated Whether `via` is the Release witness of the origin that declares `callable`. */
|
|
12
|
+
export declare function releaseRoutesDeclaration(via: invocation.Via, callable: Key.Callable): boolean;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Key } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
2
|
+
import { isReleaseVia } from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
/** @deprecated Whether `via` is the Release witness of the origin that declares `callable`. */
|
|
4
|
+
export function releaseRoutesDeclaration(via, callable) {
|
|
5
|
+
return isReleaseVia(via) && via.release.origin === Key.origin(callable);
|
|
6
|
+
}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import type { RoutingControl } from '../../client/index.js';
|
|
2
3
|
import type { LearnedRoute } from './cache.js';
|
|
3
|
-
/** The privately witnessed pre-handler control a destination failure carries, if any. */
|
|
4
|
-
export type RoutingControl = 'stale' | 'miss' | 'retired';
|
|
5
|
-
/** The routing control a destination failure witnesses, if any. */
|
|
6
|
-
export declare function routingControl(input: unknown): RoutingControl | undefined;
|
|
7
4
|
/**
|
|
8
5
|
* The Refresh a recovery source hop carries. A stale or retired route names the release the
|
|
9
6
|
* destination no longer serves; a miss names a Path, so the source receives no digest for it.
|
|
10
7
|
*/
|
|
11
8
|
export declare function recoveryRefresh(control: RoutingControl, learned: LearnedRoute): invocation.Refresh | undefined;
|
|
12
9
|
/**
|
|
13
|
-
* A failure that proves the warm route can no longer serve this call without
|
|
14
|
-
*
|
|
10
|
+
* A failure that proves the warm route can no longer serve this call without carrying routing
|
|
11
|
+
* control: the route is evicted for later calls, but the invocation is never replayed.
|
|
15
12
|
*/
|
|
16
13
|
export declare function isRouteCompatibilityConflict(input: unknown): boolean;
|
|
@@ -1,22 +1,11 @@
|
|
|
1
1
|
import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
-
import { isAdmittedDeploymentRetiredError, isAdmittedMissError, isAdmittedStaleRouteError, } from '../../client/index.js';
|
|
3
2
|
import { ResponseError } from '../../errors/index.js';
|
|
4
|
-
/** The routing control a destination failure witnesses, if any. */
|
|
5
|
-
export function routingControl(input) {
|
|
6
|
-
if (isAdmittedStaleRouteError(input))
|
|
7
|
-
return 'stale';
|
|
8
|
-
if (isAdmittedMissError(input))
|
|
9
|
-
return 'miss';
|
|
10
|
-
if (isAdmittedDeploymentRetiredError(input))
|
|
11
|
-
return 'retired';
|
|
12
|
-
return undefined;
|
|
13
|
-
}
|
|
14
3
|
/**
|
|
15
4
|
* The Refresh a recovery source hop carries. A stale or retired route names the release the
|
|
16
5
|
* destination no longer serves; a miss names a Path, so the source receives no digest for it.
|
|
17
6
|
*/
|
|
18
7
|
export function recoveryRefresh(control, learned) {
|
|
19
|
-
if (control === 'miss')
|
|
8
|
+
if (control.kind === 'miss')
|
|
20
9
|
return undefined;
|
|
21
10
|
return Object.freeze({
|
|
22
11
|
kind: 'refresh',
|
|
@@ -24,8 +13,8 @@ export function recoveryRefresh(control, learned) {
|
|
|
24
13
|
});
|
|
25
14
|
}
|
|
26
15
|
/**
|
|
27
|
-
* A failure that proves the warm route can no longer serve this call without
|
|
28
|
-
*
|
|
16
|
+
* A failure that proves the warm route can no longer serve this call without carrying routing
|
|
17
|
+
* control: the route is evicted for later calls, but the invocation is never replayed.
|
|
29
18
|
*/
|
|
30
19
|
export function isRouteCompatibilityConflict(input) {
|
|
31
20
|
if (!(input instanceof ResponseError))
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
2
3
|
import type { SessionControlOptions } from '../context/index.js';
|
|
3
4
|
export type SessionEnvelopeTransport = 'auto' | 'http' | 'websocket';
|
|
4
5
|
export interface SessionRequestOptions extends SessionControlOptions {
|
|
5
6
|
readonly idempotencyKey?: invocation.IdempotencyKey;
|
|
7
|
+
/** Sent to the source Kernel only; a routed destination never receives it. */
|
|
8
|
+
readonly maintenance?: {
|
|
9
|
+
readonly session: string;
|
|
10
|
+
};
|
|
6
11
|
readonly body?: never;
|
|
7
12
|
}
|
|
8
13
|
export type SessionRequestOptionsWithBody = Omit<SessionRequestOptions, 'body' | 'idempotencyKey'> & {
|
|
@@ -11,7 +16,7 @@ export type SessionRequestOptionsWithBody = Omit<SessionRequestOptions, 'body' |
|
|
|
11
16
|
};
|
|
12
17
|
export declare function admitInvocationOptions(input: SessionRequestOptions | SessionRequestOptionsWithBody | undefined): {
|
|
13
18
|
readonly options: Readonly<SessionRequestOptions>;
|
|
14
|
-
readonly body?:
|
|
19
|
+
readonly body?: BinaryBody;
|
|
15
20
|
};
|
|
16
21
|
export declare function admitRequestOptions(input?: SessionRequestOptions): Readonly<SessionRequestOptions>;
|
|
17
22
|
export declare function admitEnvelopeTransport(input: SessionEnvelopeTransport): SessionEnvelopeTransport;
|