@astrale-os/kernel-client 0.6.0-beta.86 → 0.6.0-beta.87
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/identity.d.ts +2 -1
- package/dist/client/credential.d.ts +2 -1
- package/dist/errors/stale-route-error.d.ts +4 -14
- package/dist/errors/stale-route-error.js +2 -13
- package/dist/errors/transport-error.d.ts +1 -1
- package/dist/graph/domain/domain.typecheck.js +1 -0
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/schema/introspection.js +1 -5
- package/dist/schema/schema.js +4 -8
- package/dist/session/bound.d.ts +1 -1
- package/dist/session/bound.js +2 -1
- package/dist/session/content/api.d.ts +45 -0
- package/dist/session/content/api.js +28 -0
- package/dist/session/content/batch.d.ts +5 -0
- package/dist/session/content/batch.js +93 -0
- package/dist/session/content/index.d.ts +1 -0
- package/dist/session/content/index.js +1 -0
- package/dist/session/content/input.d.ts +9 -0
- package/dist/session/content/input.js +33 -0
- package/dist/session/content/parts.d.ts +36 -0
- package/dist/session/content/parts.js +154 -0
- package/dist/session/content/retry.d.ts +3 -0
- package/dist/session/content/retry.js +41 -0
- package/dist/session/content/wire.d.ts +12 -0
- package/dist/session/content/wire.js +60 -0
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/limits.d.ts +0 -1
- package/dist/session/context/limits.js +0 -1
- package/dist/session/context/operation.d.ts +0 -1
- package/dist/session/context/operation.js +1 -1
- package/dist/session/context/policy.d.ts +0 -1
- package/dist/session/context/policy.js +0 -16
- package/dist/session/discovery/bundle.d.ts +4 -3
- package/dist/session/discovery/bundle.js +20 -21
- package/dist/session/discovery/discovery.d.ts +0 -3
- package/dist/session/discovery/discovery.js +6 -14
- package/dist/session/discovery/index.d.ts +0 -5
- package/dist/session/discovery/index.js +1 -2
- package/dist/session/discovery/release.d.ts +6 -5
- package/dist/session/discovery/release.js +3 -3
- package/dist/session/discovery/snapshot.d.ts +1 -8
- package/dist/session/discovery/snapshot.js +2 -2
- package/dist/session/exchange/domain.js +1 -1
- package/dist/session/exchange/source.d.ts +2 -1
- package/dist/session/index.d.ts +3 -2
- package/dist/session/index.js +1 -1
- package/dist/session/options.d.ts +65 -0
- package/dist/session/options.js +132 -0
- package/dist/session/routing/cache.d.ts +9 -14
- package/dist/session/routing/cache.js +17 -178
- package/dist/session/routing/dispatch.d.ts +4 -3
- package/dist/session/routing/dispatch.js +63 -113
- package/dist/session/routing/envelope.d.ts +3 -3
- package/dist/session/routing/expiry.d.ts +4 -0
- package/dist/session/routing/expiry.js +32 -0
- package/dist/session/routing/index.d.ts +4 -2
- package/dist/session/routing/index.js +4 -2
- package/dist/session/routing/integrity.d.ts +7 -0
- package/dist/session/routing/integrity.js +44 -0
- package/dist/session/routing/recovery.d.ts +16 -0
- package/dist/session/routing/recovery.js +41 -0
- package/dist/session/routing/result.d.ts +5 -0
- package/dist/session/routing/result.js +12 -0
- package/dist/session/routing/store.d.ts +23 -0
- package/dist/session/routing/store.js +99 -0
- package/dist/session/session.d.ts +6 -50
- package/dist/session/session.js +28 -134
- package/dist/session/session.typecheck.js +8 -3
- package/dist/session/views/catalog.d.ts +5 -9
- package/dist/session/views/catalog.js +12 -24
- package/dist/session/views/resolve.d.ts +1 -1
- package/dist/session/views/resolve.js +1 -1
- package/dist/store/entity-index.d.ts +1 -2
- package/dist/store/entity-index.js +0 -3
- package/dist/store/memory.js +2 -2
- package/dist/store/shape.d.ts +2 -1
- package/dist/transport/auto/decision.d.ts +3 -2
- package/dist/transport/http/admission.d.ts +5 -0
- package/dist/transport/http/admission.js +101 -0
- package/dist/transport/http/binary.d.ts +5 -0
- package/dist/transport/http/binary.js +70 -0
- package/dist/transport/http/configuration.d.ts +24 -0
- package/dist/transport/http/configuration.js +116 -0
- package/dist/transport/http/index.d.ts +2 -1
- package/dist/transport/http/response.d.ts +7 -0
- package/dist/transport/http/response.js +9 -0
- package/dist/transport/http/retired.d.ts +8 -0
- package/dist/transport/http/retired.js +28 -0
- package/dist/transport/http/transport.d.ts +1 -19
- package/dist/transport/http/transport.js +17 -299
- package/dist/transport/websocket/connection.d.ts +2 -1
- package/package.json +5 -5
- package/dist/session/content.d.ts +0 -27
- package/dist/session/content.js +0 -135
- package/dist/session/discovery/legacy/index.d.ts +0 -38
- package/dist/session/discovery/legacy/index.js +0 -38
- package/dist/session/discovery/legacy/intrinsic.d.ts +0 -33
- package/dist/session/discovery/legacy/intrinsic.js +0 -67
- package/dist/session/discovery/legacy/projection.d.ts +0 -8
- package/dist/session/discovery/legacy/projection.js +0 -17
- package/dist/session/discovery/legacy/publication.d.ts +0 -26
- package/dist/session/discovery/legacy/publication.js +0 -76
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Key } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import { ClientError, ResponseError } from '../../errors/index.js';
|
|
2
|
+
import { acceptCall, Client, dispatchTrace } from '../../client/index.js';
|
|
3
|
+
import { ClientError } from '../../errors/index.js';
|
|
5
4
|
import { runSessionOperation, remainingSessionTime, sessionTransportOptions, allowEndpoint, } from '../context/index.js';
|
|
6
5
|
import { resolveAuth } from './auth.js';
|
|
7
6
|
import { selectEnvelope, selectEnvelopeTarget } from './envelope.js';
|
|
7
|
+
import { isRouteCompatibilityConflict, recoveryRefresh, routingControl } from './recovery.js';
|
|
8
8
|
export class SessionDispatcher {
|
|
9
9
|
state;
|
|
10
10
|
constructor(state) {
|
|
@@ -28,28 +28,29 @@ export class SessionDispatcher {
|
|
|
28
28
|
: await this.state.exchange.resolve(call, resolved, operation);
|
|
29
29
|
const source = this.state.kernel;
|
|
30
30
|
const cacheKey = await runSessionOperation(operation, () => this.state.cache.key(source, call, auth));
|
|
31
|
-
const dispatch = Object.freeze({
|
|
32
|
-
|
|
33
|
-
auth,
|
|
34
|
-
source,
|
|
35
|
-
cacheKey,
|
|
36
|
-
});
|
|
37
|
-
let learned = await runSessionOperation(operation, () => this.state.cache.get(cacheKey, auth.credential, undefined, operation.deadline));
|
|
38
|
-
if (learned !== undefined) {
|
|
39
|
-
try {
|
|
40
|
-
requireCachedRoute(learned, source, call.target, streaming === undefined ? this.state.envelopeTransport : 'http', this.state.websocketAvailable, this.state.policy);
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
this.state.cache.delete(cacheKey);
|
|
44
|
-
learned = undefined;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
31
|
+
const dispatch = Object.freeze({ call, auth, source, cacheKey });
|
|
32
|
+
const learned = await this.warmRoute(dispatch, operation, streaming);
|
|
47
33
|
if (learned !== undefined) {
|
|
48
34
|
return this.dispatchDestinationWithRecovery(learned, dispatch, options, operation, undefined, streaming);
|
|
49
35
|
}
|
|
50
36
|
const result = await this.dispatchSource(dispatch, undefined, options, operation, streaming);
|
|
51
37
|
return this.followSource(result, dispatch, options, operation, streaming);
|
|
52
38
|
}
|
|
39
|
+
/** The cached route for this partition, evicted when it no longer passes delivery checks. */
|
|
40
|
+
async warmRoute(state, operation, streaming) {
|
|
41
|
+
const learned = await runSessionOperation(operation, () => this.state.cache.get(state.cacheKey, state.auth.credential, undefined, operation.deadline));
|
|
42
|
+
if (learned === undefined)
|
|
43
|
+
return undefined;
|
|
44
|
+
try {
|
|
45
|
+
requireLearnedRoute(learned, state.source, state.call.target);
|
|
46
|
+
selectEnvelopeTarget(learned.route.endpoint, streaming === undefined ? this.state.envelopeTransport : 'http', this.state.websocketAvailable, this.state.policy);
|
|
47
|
+
return learned;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
this.state.cache.delete(state.cacheKey);
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
53
54
|
async dispatchSource(state, routing, options, operation, streaming) {
|
|
54
55
|
this.state.requireOpen();
|
|
55
56
|
remainingSessionTime(operation);
|
|
@@ -57,18 +58,11 @@ export class SessionDispatcher {
|
|
|
57
58
|
const client = streaming === undefined
|
|
58
59
|
? this.state.pool.clientFor(Object.freeze({ transport: 'http', url: this.state.sourceEndpoint }))
|
|
59
60
|
: this.streamingClientFor(this.state.sourceEndpoint);
|
|
60
|
-
return runSessionOperation(operation, () =>
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
})
|
|
66
|
-
: authenticated(client, state.auth.credential).dispatch(state.call, {
|
|
67
|
-
...sessionTransportOptions(operation),
|
|
68
|
-
...(state.auth.delegate === undefined ? {} : { delegate: state.auth.delegate }),
|
|
69
|
-
...(routing === undefined ? {} : { routing }),
|
|
70
|
-
body: streaming.body,
|
|
71
|
-
}));
|
|
61
|
+
return runSessionOperation(operation, () => send(client, state.auth.credential, state.call, {
|
|
62
|
+
...transportOptions(operation, options, streaming),
|
|
63
|
+
...(state.auth.delegate === undefined ? {} : { delegate: state.auth.delegate }),
|
|
64
|
+
...(routing === undefined ? {} : { routing }),
|
|
65
|
+
}));
|
|
72
66
|
}
|
|
73
67
|
async followSource(result, state, options, operation, streaming) {
|
|
74
68
|
if (result.kind !== 'redirect')
|
|
@@ -77,7 +71,7 @@ export class SessionDispatcher {
|
|
|
77
71
|
if (redirect.route.via.issuer !== state.source) {
|
|
78
72
|
throw new ClientError('Redirect issuer differs from the session-pinned source Kernel identity.');
|
|
79
73
|
}
|
|
80
|
-
|
|
74
|
+
requireTargetRelease(redirect.route, state.call.target);
|
|
81
75
|
const learned = await runSessionOperation(operation, () => this.state.cache.learn(state.cacheKey, redirect.route, redirect.credential, state.auth.credential));
|
|
82
76
|
return this.dispatchDestinationWithRecovery(learned, state, options, operation, redirect.trace, streaming);
|
|
83
77
|
}
|
|
@@ -95,55 +89,38 @@ export class SessionDispatcher {
|
|
|
95
89
|
this.state.cache.delete(state.cacheKey);
|
|
96
90
|
if (!operation.claimRecovery())
|
|
97
91
|
throw cause;
|
|
98
|
-
|
|
99
|
-
// names a Path, so the source receives no digest for it.
|
|
100
|
-
const routing = control === 'miss'
|
|
101
|
-
? undefined
|
|
102
|
-
: Object.freeze({
|
|
103
|
-
kind: 'refresh',
|
|
104
|
-
stale: invocationProtocol.routedDigest(learned.route.via),
|
|
105
|
-
});
|
|
92
|
+
const routing = recoveryRefresh(control, learned);
|
|
106
93
|
const refreshed = await this.dispatchSource(state, routing, options, operation, streaming);
|
|
107
94
|
return this.followSource(refreshed, state, options, operation, streaming);
|
|
108
95
|
}
|
|
109
96
|
}
|
|
110
97
|
async dispatchDestination(learned, call, options, operation, source, trace, streaming) {
|
|
111
98
|
remainingSessionTime(operation);
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const destination = streaming === undefined
|
|
117
|
-
? undefined
|
|
118
|
-
: selectEnvelopeTarget(learned.route.endpoint, 'http', this.state.websocketAvailable, this.state.policy);
|
|
119
|
-
const client = streaming === undefined
|
|
120
|
-
? selectEnvelope(learned.route.endpoint, this.state.envelopeTransport, this.state.websocketAvailable, this.state.pool, this.state.policy).client
|
|
121
|
-
: destination?.transport === 'http'
|
|
122
|
-
? this.streamingClientFor(destination.url)
|
|
123
|
-
: invalidStreamingDestination();
|
|
124
|
-
const requestOptions = {
|
|
125
|
-
...sessionTransportOptions(operation, options.idempotencyKey),
|
|
126
|
-
routing: learned.route.via,
|
|
127
|
-
};
|
|
128
|
-
const binaryRequestOptions = {
|
|
129
|
-
...sessionTransportOptions(operation),
|
|
99
|
+
requireLearnedRoute(learned, source, call.target);
|
|
100
|
+
const client = this.destinationClient(learned, streaming);
|
|
101
|
+
const request = {
|
|
102
|
+
...transportOptions(operation, options, streaming),
|
|
130
103
|
routing: learned.route.via,
|
|
131
104
|
};
|
|
132
|
-
const result = await runSessionOperation(operation, () =>
|
|
133
|
-
? trace === undefined
|
|
134
|
-
? authenticated(client, learned.credential).dispatch(call, {
|
|
135
|
-
...binaryRequestOptions,
|
|
136
|
-
body: streaming.body,
|
|
137
|
-
})
|
|
138
|
-
: dispatchTrace(client, learned.credential, call, { ...binaryRequestOptions, body: streaming.body }, trace)
|
|
139
|
-
: trace === undefined
|
|
140
|
-
? authenticated(client, learned.credential).dispatch(call, requestOptions)
|
|
141
|
-
: dispatchTrace(client, learned.credential, call, requestOptions, trace));
|
|
105
|
+
const result = await runSessionOperation(operation, () => send(client, learned.credential, call, request, trace));
|
|
142
106
|
if (result.kind === 'redirect') {
|
|
143
107
|
throw new ClientError('A destination redirect is invalid; only the source Kernel may redirect.');
|
|
144
108
|
}
|
|
145
109
|
return result;
|
|
146
110
|
}
|
|
111
|
+
/** The Client for a destination: the pooled envelope, or a fresh HTTP Client when streaming. */
|
|
112
|
+
destinationClient(learned, streaming) {
|
|
113
|
+
const { endpoint } = learned.route;
|
|
114
|
+
const { pool, policy, websocketAvailable } = this.state;
|
|
115
|
+
if (streaming === undefined) {
|
|
116
|
+
return selectEnvelope(endpoint, this.state.envelopeTransport, websocketAvailable, pool, policy).client;
|
|
117
|
+
}
|
|
118
|
+
const destination = selectEnvelopeTarget(endpoint, 'http', websocketAvailable, policy);
|
|
119
|
+
if (destination.transport !== 'http') {
|
|
120
|
+
throw new ClientError('Streaming route has no HTTP endpoint.');
|
|
121
|
+
}
|
|
122
|
+
return this.streamingClientFor(destination.url);
|
|
123
|
+
}
|
|
147
124
|
streamingClientFor(url) {
|
|
148
125
|
return new Client({
|
|
149
126
|
url,
|
|
@@ -153,60 +130,33 @@ export class SessionDispatcher {
|
|
|
153
130
|
});
|
|
154
131
|
}
|
|
155
132
|
}
|
|
156
|
-
|
|
157
|
-
|
|
133
|
+
/** A body-carrying streaming call omits the idempotency key; every other call keeps it. */
|
|
134
|
+
function transportOptions(operation, options, streaming) {
|
|
135
|
+
return streaming?.body === undefined
|
|
136
|
+
? sessionTransportOptions(operation, options.idempotencyKey)
|
|
137
|
+
: { ...sessionTransportOptions(operation), body: streaming.body };
|
|
158
138
|
}
|
|
159
|
-
function
|
|
139
|
+
function requireLearnedRoute(learned, source, target) {
|
|
160
140
|
if (learned.route.via.issuer !== source) {
|
|
161
141
|
throw new ClientError('Learned route issuer differs from the session-pinned source Kernel.');
|
|
162
142
|
}
|
|
163
|
-
|
|
164
|
-
selectEnvelopeTarget(learned.route.endpoint, envelopeTransport, websocketAvailable, policy);
|
|
143
|
+
requireTargetRelease(learned.route, target);
|
|
165
144
|
}
|
|
166
|
-
|
|
167
|
-
function routingControl(input) {
|
|
168
|
-
if (isAdmittedStaleRouteError(input))
|
|
169
|
-
return 'stale';
|
|
170
|
-
if (isAdmittedMissError(input))
|
|
171
|
-
return 'miss';
|
|
172
|
-
if (isAdmittedDeploymentRetiredError(input))
|
|
173
|
-
return 'retired';
|
|
174
|
-
return undefined;
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* A failure that proves the warm route can no longer serve this call without witnessing a
|
|
178
|
-
* pre-handler control: the route is evicted for later calls, but the invocation is never replayed.
|
|
179
|
-
*/
|
|
180
|
-
function isRouteCompatibilityConflict(input) {
|
|
181
|
-
if (!(input instanceof ResponseError))
|
|
182
|
-
return false;
|
|
183
|
-
const failure = invocationProtocol.serviceErrorOf({
|
|
184
|
-
code: input.code,
|
|
185
|
-
message: input.message,
|
|
186
|
-
...(input.data === undefined ? {} : { data: input.data }),
|
|
187
|
-
});
|
|
188
|
-
return (failure?.data.code === invocationProtocol.CALLABLE_FINGERPRINT_MISMATCH ||
|
|
189
|
-
failure?.data.code === invocationProtocol.SERVICE_TRUST_REQUIRED ||
|
|
190
|
-
failure?.data.code === invocationProtocol.DEPLOYMENT_RETIRED);
|
|
191
|
-
}
|
|
192
|
-
function requireTargetPublication(route, target) {
|
|
145
|
+
function requireTargetRelease(route, target) {
|
|
193
146
|
if (typeof target === 'string')
|
|
194
147
|
return;
|
|
195
|
-
const actual =
|
|
148
|
+
const actual = route.via.release;
|
|
196
149
|
if (actual.origin !== Key.origin(target.callable)) {
|
|
197
150
|
throw new ClientError('Resolved route does not match the Target origin.');
|
|
198
151
|
}
|
|
199
152
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
}
|
|
210
|
-
function authenticated(client, credential) {
|
|
211
|
-
return credential === undefined ? client : client.as(credential);
|
|
153
|
+
/** Send one call, presenting the Trace only on the attempt that carries it. */
|
|
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);
|
|
212
162
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type * as release from '@astrale-os/kernel-protocol/release';
|
|
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: release.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: release.InvocationEndpoints, preference: SessionEnvelopeTransport, websocketAvailable: boolean, policy: Readonly<SessionPolicy>): ClientTarget;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The finite `exp` of a compact JWT in milliseconds, or undefined when none is supported. */
|
|
2
|
+
export declare function credentialExpiry(credential: string): number | undefined;
|
|
3
|
+
/** The instant a carrier must be replaced before its expiry; unbounded without one. */
|
|
4
|
+
export declare function refreshBoundary(expiry: number | undefined): number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const DESTINATION_CREDENTIAL_REFRESH_SKEW_MS = 5_000;
|
|
2
|
+
/** The finite `exp` of a compact JWT in milliseconds, or undefined when none is supported. */
|
|
3
|
+
export function credentialExpiry(credential) {
|
|
4
|
+
const parts = credential.split('.');
|
|
5
|
+
if (parts.length !== 3)
|
|
6
|
+
return undefined;
|
|
7
|
+
try {
|
|
8
|
+
const encoded = parts[1].replace(/-/gu, '+').replace(/_/gu, '/');
|
|
9
|
+
const padding = '='.repeat((4 - (encoded.length % 4)) % 4);
|
|
10
|
+
const json = decodeBase64(encoded + padding);
|
|
11
|
+
const payload = JSON.parse(json);
|
|
12
|
+
return typeof payload.exp === 'number' && Number.isSafeInteger(payload.exp) && payload.exp > 0
|
|
13
|
+
? payload.exp * 1_000
|
|
14
|
+
: undefined;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** The instant a carrier must be replaced before its expiry; unbounded without one. */
|
|
21
|
+
export function refreshBoundary(expiry) {
|
|
22
|
+
return expiry === undefined
|
|
23
|
+
? Number.POSITIVE_INFINITY
|
|
24
|
+
: expiry - DESTINATION_CREDENTIAL_REFRESH_SKEW_MS;
|
|
25
|
+
}
|
|
26
|
+
function decodeBase64(input) {
|
|
27
|
+
if (typeof globalThis.atob === 'function') {
|
|
28
|
+
const binary = globalThis.atob(input);
|
|
29
|
+
return new TextDecoder().decode(Uint8Array.from(binary, (character) => character.charCodeAt(0)));
|
|
30
|
+
}
|
|
31
|
+
throw new TypeError('Session route caching requires base64 decoding support.');
|
|
32
|
+
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { admitSessionAuth, resolveAuth, type Auth, type AuthExchange, type EffectiveAuth, type SessionAuth, } from './auth.js';
|
|
2
|
-
export { RouteCache, type LearnedRoute
|
|
3
|
-
export {
|
|
2
|
+
export { RouteCache, type LearnedRoute } from './cache.js';
|
|
3
|
+
export { SessionDispatcher, type SessionDispatcherOptions } from './dispatch.js';
|
|
4
4
|
export { type ClientPoolCapability, type EnvelopeSelection } from './envelope.js';
|
|
5
5
|
export { admitEnvelopeTransport, admitInvocationOptions, admitRequestOptions, type SessionEnvelopeTransport, type SessionRequestOptions, type SessionRequestOptionsWithBody, } from './request.js';
|
|
6
|
+
export { requireResultKind } from './result.js';
|
|
7
|
+
export { type PersistedRoute, type SessionRouteArtifact, type SessionRouteStore } from './store.js';
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { admitSessionAuth, resolveAuth, } from './auth.js';
|
|
2
|
-
export { RouteCache
|
|
3
|
-
export {
|
|
2
|
+
export { RouteCache } from './cache.js';
|
|
3
|
+
export { SessionDispatcher } from './dispatch.js';
|
|
4
4
|
export {} from './envelope.js';
|
|
5
5
|
export { admitEnvelopeTransport, admitInvocationOptions, admitRequestOptions, } from './request.js';
|
|
6
|
+
export { requireResultKind } from './result.js';
|
|
7
|
+
export {} from './store.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LearnedRoute } from './cache.js';
|
|
2
|
+
/** The HMAC binding one persisted route entry to its source credential and cache key. */
|
|
3
|
+
export declare function routeIntegrity(sourceCredential: string, key: string, learned: LearnedRoute): Promise<string>;
|
|
4
|
+
/** Whether `expected` is the integrity of this entry; unsupported crypto never matches. */
|
|
5
|
+
export declare function matchesIntegrity(sourceCredential: string, key: string, learned: LearnedRoute, expected: string): Promise<boolean>;
|
|
6
|
+
/** The hex SHA-256 of a text, partitioning the cache without retaining the text. */
|
|
7
|
+
export declare function digestText(input: string): Promise<string>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** The HMAC binding one persisted route entry to its source credential and cache key. */
|
|
2
|
+
export async function routeIntegrity(sourceCredential, key, learned) {
|
|
3
|
+
const webCrypto = typeof crypto === 'undefined' ? undefined : crypto;
|
|
4
|
+
if (webCrypto?.subtle === undefined) {
|
|
5
|
+
throw new TypeError('Persistent Session route integrity requires Web Crypto HMAC support.');
|
|
6
|
+
}
|
|
7
|
+
const algorithm = Object.freeze({ name: 'HMAC', hash: 'SHA-256' });
|
|
8
|
+
const cryptographicKey = await webCrypto.subtle.importKey('raw', new TextEncoder().encode(sourceCredential), algorithm, false, ['sign']);
|
|
9
|
+
const signature = await webCrypto.subtle.sign(algorithm, cryptographicKey, new TextEncoder().encode(integrityMaterial(key, learned)));
|
|
10
|
+
return encodeBase64Url(new Uint8Array(signature));
|
|
11
|
+
}
|
|
12
|
+
/** Whether `expected` is the integrity of this entry; unsupported crypto never matches. */
|
|
13
|
+
export async function matchesIntegrity(sourceCredential, key, learned, expected) {
|
|
14
|
+
try {
|
|
15
|
+
return (await routeIntegrity(sourceCredential, key, learned)) === expected;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** The hex SHA-256 of a text, partitioning the cache without retaining the text. */
|
|
22
|
+
export async function digestText(input) {
|
|
23
|
+
const webCrypto = typeof crypto === 'undefined' ? undefined : crypto;
|
|
24
|
+
if (webCrypto?.subtle === undefined) {
|
|
25
|
+
throw new TypeError('Session route caching requires Web Crypto SHA-256 support.');
|
|
26
|
+
}
|
|
27
|
+
const bytes = await webCrypto.subtle.digest('SHA-256', new TextEncoder().encode(input));
|
|
28
|
+
return [...new Uint8Array(bytes)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
29
|
+
}
|
|
30
|
+
function integrityMaterial(key, learned) {
|
|
31
|
+
return JSON.stringify({
|
|
32
|
+
version: 1,
|
|
33
|
+
key,
|
|
34
|
+
route: learned.route,
|
|
35
|
+
...(learned.credential === undefined ? {} : { credential: learned.credential }),
|
|
36
|
+
expiresAt: learned.expiresAt,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function encodeBase64Url(input) {
|
|
40
|
+
let binary = '';
|
|
41
|
+
for (const byte of input)
|
|
42
|
+
binary += String.fromCharCode(byte);
|
|
43
|
+
return globalThis.btoa(binary).replace(/\+/gu, '-').replace(/\//gu, '_').replace(/=+$/gu, '');
|
|
44
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
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
|
+
/**
|
|
8
|
+
* The Refresh a recovery source hop carries. A stale or retired route names the release the
|
|
9
|
+
* destination no longer serves; a miss names a Path, so the source receives no digest for it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function recoveryRefresh(control: RoutingControl, learned: LearnedRoute): invocation.Refresh | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* A failure that proves the warm route can no longer serve this call without witnessing a
|
|
14
|
+
* pre-handler control: the route is evicted for later calls, but the invocation is never replayed.
|
|
15
|
+
*/
|
|
16
|
+
export declare function isRouteCompatibilityConflict(input: unknown): boolean;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import { isAdmittedDeploymentRetiredError, isAdmittedMissError, isAdmittedStaleRouteError, } from '../../client/index.js';
|
|
3
|
+
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
|
+
/**
|
|
15
|
+
* The Refresh a recovery source hop carries. A stale or retired route names the release the
|
|
16
|
+
* destination no longer serves; a miss names a Path, so the source receives no digest for it.
|
|
17
|
+
*/
|
|
18
|
+
export function recoveryRefresh(control, learned) {
|
|
19
|
+
if (control === 'miss')
|
|
20
|
+
return undefined;
|
|
21
|
+
return Object.freeze({
|
|
22
|
+
kind: 'refresh',
|
|
23
|
+
stale: invocationProtocol.routedDigest(learned.route.via),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A failure that proves the warm route can no longer serve this call without witnessing a
|
|
28
|
+
* pre-handler control: the route is evicted for later calls, but the invocation is never replayed.
|
|
29
|
+
*/
|
|
30
|
+
export function isRouteCompatibilityConflict(input) {
|
|
31
|
+
if (!(input instanceof ResponseError))
|
|
32
|
+
return false;
|
|
33
|
+
const failure = invocationProtocol.serviceErrorOf({
|
|
34
|
+
code: input.code,
|
|
35
|
+
message: input.message,
|
|
36
|
+
...(input.data === undefined ? {} : { data: input.data }),
|
|
37
|
+
});
|
|
38
|
+
return (failure?.data.code === invocationProtocol.CALLABLE_FINGERPRINT_MISMATCH ||
|
|
39
|
+
failure?.data.code === invocationProtocol.SERVICE_TRUST_REQUIRED ||
|
|
40
|
+
failure?.data.code === invocationProtocol.DEPLOYMENT_RETIRED);
|
|
41
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Result } from '../../client/index.js';
|
|
2
|
+
/** Narrow a Result to the kind a sugar requires, releasing a live body before rejecting it. */
|
|
3
|
+
export declare function requireResultKind<Kind extends 'value' | 'stream' | 'binary'>(expected: Kind, result: Result): Extract<Result, {
|
|
4
|
+
kind: Kind;
|
|
5
|
+
}>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ClientError } from '../../errors/index.js';
|
|
2
|
+
/** Narrow a Result to the kind a sugar requires, releasing a live body before rejecting it. */
|
|
3
|
+
export function requireResultKind(expected, result) {
|
|
4
|
+
if (result.kind === expected)
|
|
5
|
+
return result;
|
|
6
|
+
if (result.kind === 'stream')
|
|
7
|
+
void result.stream.cancel(`Expected ${expected} result.`);
|
|
8
|
+
if (result.kind === 'binary' && !(result.value.body instanceof Uint8Array)) {
|
|
9
|
+
void result.value.body[Symbol.asyncIterator]().return?.();
|
|
10
|
+
}
|
|
11
|
+
throw new ClientError(`Function returned ${result.kind}; ${expected} was required.`);
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CachedRoute, LearnedRoute } from './cache.js';
|
|
2
|
+
export interface PersistedRoute extends LearnedRoute {
|
|
3
|
+
/** HMAC over this entry and its exact authority partition. */
|
|
4
|
+
readonly integrity: string;
|
|
5
|
+
}
|
|
6
|
+
export interface SessionRouteArtifact {
|
|
7
|
+
readonly version: 1;
|
|
8
|
+
readonly entries: Readonly<Record<string, PersistedRoute>>;
|
|
9
|
+
}
|
|
10
|
+
/** Caller-owned persistence for confidential route artifacts. */
|
|
11
|
+
export interface SessionRouteStore {
|
|
12
|
+
read(): unknown;
|
|
13
|
+
write(artifact: SessionRouteArtifact): void;
|
|
14
|
+
}
|
|
15
|
+
/** Snapshot and receiver-bind a caller's route store. */
|
|
16
|
+
export declare function captureRouteStore(input: SessionRouteStore): SessionRouteStore;
|
|
17
|
+
/**
|
|
18
|
+
* Re-admit the entries of a stored artifact in their stored order. An unsupported artifact, or
|
|
19
|
+
* one holding more than `maximumSize` entries, restores nothing; an invalid entry is skipped.
|
|
20
|
+
*/
|
|
21
|
+
export declare function restoreRoutes(input: unknown, now: number, maximumAgeMs: number, maximumSize: number): ReadonlyMap<string, CachedRoute>;
|
|
22
|
+
/** The artifact persisting every integrity-bound entry, as it was sealed. */
|
|
23
|
+
export declare function routeArtifact(entries: Iterable<readonly [string, CachedRoute]>): SessionRouteArtifact;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { invocation } from '@astrale-os/kernel-protocol';
|
|
2
|
+
import { credentialExpiry, refreshBoundary } from './expiry.js';
|
|
3
|
+
/** Snapshot and receiver-bind a caller's route store. */
|
|
4
|
+
export function captureRouteStore(input) {
|
|
5
|
+
if (input === null ||
|
|
6
|
+
typeof input !== 'object' ||
|
|
7
|
+
typeof input.read !== 'function' ||
|
|
8
|
+
typeof input.write !== 'function') {
|
|
9
|
+
throw new TypeError('Session route store capability is invalid.');
|
|
10
|
+
}
|
|
11
|
+
const read = input.read;
|
|
12
|
+
const write = input.write;
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
read: () => read.call(input),
|
|
15
|
+
write: (artifact) => write.call(input, artifact),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Re-admit the entries of a stored artifact in their stored order. An unsupported artifact, or
|
|
20
|
+
* one holding more than `maximumSize` entries, restores nothing; an invalid entry is skipped.
|
|
21
|
+
*/
|
|
22
|
+
export function restoreRoutes(input, now, maximumAgeMs, maximumSize) {
|
|
23
|
+
const restored = new Map();
|
|
24
|
+
if (typeof input !== 'object' || input === null || Array.isArray(input))
|
|
25
|
+
return restored;
|
|
26
|
+
const artifact = input;
|
|
27
|
+
if (artifact.version !== 1 ||
|
|
28
|
+
typeof artifact.entries !== 'object' ||
|
|
29
|
+
artifact.entries === null ||
|
|
30
|
+
Array.isArray(artifact.entries)) {
|
|
31
|
+
return restored;
|
|
32
|
+
}
|
|
33
|
+
let count = 0;
|
|
34
|
+
for (const key in artifact.entries) {
|
|
35
|
+
if (!Object.hasOwn(artifact.entries, key))
|
|
36
|
+
continue;
|
|
37
|
+
count += 1;
|
|
38
|
+
if (count > maximumSize)
|
|
39
|
+
return new Map();
|
|
40
|
+
const value = Reflect.get(artifact.entries, key);
|
|
41
|
+
const entry = admitStoredEntry(value, now, maximumAgeMs);
|
|
42
|
+
if (entry !== undefined)
|
|
43
|
+
restored.set(key, entry);
|
|
44
|
+
}
|
|
45
|
+
return restored;
|
|
46
|
+
}
|
|
47
|
+
/** The artifact persisting every integrity-bound entry, as it was sealed. */
|
|
48
|
+
export function routeArtifact(entries) {
|
|
49
|
+
const persisted = Object.fromEntries([...entries].flatMap(([key, cached]) => cached.integrity === undefined
|
|
50
|
+
? []
|
|
51
|
+
: [
|
|
52
|
+
[
|
|
53
|
+
key,
|
|
54
|
+
Object.freeze({
|
|
55
|
+
...(cached.sealed ?? cached.learned),
|
|
56
|
+
integrity: cached.integrity,
|
|
57
|
+
}),
|
|
58
|
+
],
|
|
59
|
+
]));
|
|
60
|
+
return Object.freeze({ version: 1, entries: Object.freeze(persisted) });
|
|
61
|
+
}
|
|
62
|
+
function admitStoredEntry(input, now, maximumAgeMs) {
|
|
63
|
+
try {
|
|
64
|
+
if (typeof input !== 'object' || input === null || Array.isArray(input))
|
|
65
|
+
return undefined;
|
|
66
|
+
const value = input;
|
|
67
|
+
if ((value.credential !== undefined && typeof value.credential !== 'string') ||
|
|
68
|
+
typeof value.expiresAt !== 'number' ||
|
|
69
|
+
!Number.isSafeInteger(value.expiresAt) ||
|
|
70
|
+
typeof value.integrity !== 'string' ||
|
|
71
|
+
!/^[A-Za-z0-9_-]{43}$/u.test(value.integrity)) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
const expiry = value.credential === undefined ? undefined : credentialExpiry(value.credential);
|
|
75
|
+
if (value.credential !== undefined && expiry === undefined)
|
|
76
|
+
return undefined;
|
|
77
|
+
const sealed = Object.freeze({
|
|
78
|
+
route: invocation.acceptRoute(value.route),
|
|
79
|
+
...(value.credential === undefined ? {} : { credential: value.credential }),
|
|
80
|
+
expiresAt: value.expiresAt,
|
|
81
|
+
});
|
|
82
|
+
const expiresAt = Math.min(value.expiresAt, now + maximumAgeMs, refreshBoundary(expiry));
|
|
83
|
+
if (expiresAt <= now)
|
|
84
|
+
return undefined;
|
|
85
|
+
return Object.freeze({
|
|
86
|
+
learned: Object.freeze({
|
|
87
|
+
route: sealed.route,
|
|
88
|
+
...(sealed.credential === undefined ? {} : { credential: sealed.credential }),
|
|
89
|
+
expiresAt,
|
|
90
|
+
}),
|
|
91
|
+
sealed,
|
|
92
|
+
integrity: value.integrity,
|
|
93
|
+
...(expiry === undefined ? {} : { credentialRefreshBoundary: refreshBoundary(expiry) }),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|