@astrale-os/kernel-client 1.0.0-beta.5 → 1.0.0-beta.50
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/authorization.d.ts +10 -5
- package/dist/auth/authorization.js +14 -1
- package/dist/auth/index.d.ts +1 -0
- package/dist/client/admission.js +3 -1
- package/dist/client/client.d.ts +70 -4
- package/dist/client/client.js +209 -32
- package/dist/client/index.d.ts +3 -3
- package/dist/client/index.js +2 -2
- package/dist/client/level.d.ts +58 -0
- package/dist/client/level.js +159 -0
- package/dist/client/request.d.ts +7 -2
- package/dist/client/request.js +11 -7
- package/dist/client/result.d.ts +14 -1
- package/dist/client/result.js +19 -8
- 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/inexpressible-error.d.ts +23 -0
- package/dist/errors/inexpressible-error.js +26 -0
- package/dist/errors/query-input.d.ts +47 -0
- package/dist/errors/query-input.js +85 -0
- package/dist/errors/refusal.d.ts +12 -0
- package/dist/errors/refusal.js +44 -6
- 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 +2 -2
- package/dist/{session/execution.d.ts → execution/contracts.d.ts} +22 -34
- 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 +20 -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 +144 -5
- package/dist/graph/query.typecheck.d.ts +81 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/schema/contracts.d.ts +54 -79
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.js +1 -1
- package/dist/schema/installation.d.ts +36 -64
- package/dist/schema/installation.js +67 -131
- package/dist/schema/schema.d.ts +10 -16
- package/dist/schema/schema.js +57 -34
- package/dist/session/admission.js +10 -3
- package/dist/session/bound.d.ts +12 -3
- package/dist/session/bound.js +36 -32
- 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 +2 -2
- package/dist/session/context/index.js +2 -2
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +5 -0
- package/dist/session/context/policy.d.ts +2 -0
- package/dist/session/context/policy.js +6 -3
- package/dist/session/credential/credentials.d.ts +2 -1
- package/dist/session/credential/credentials.js +62 -29
- package/dist/session/discovery/bundle.d.ts +13 -4
- package/dist/session/discovery/bundle.js +85 -36
- package/dist/session/discovery/discovery.d.ts +1 -1
- package/dist/session/discovery/discovery.js +8 -11
- package/dist/session/discovery/index.d.ts +1 -0
- package/dist/session/discovery/index.js +5 -1
- package/dist/session/discovery/release.d.ts +2 -0
- package/dist/session/discovery/release.js +34 -31
- package/dist/session/discovery/response.d.ts +4 -0
- package/dist/session/discovery/response.js +10 -6
- package/dist/session/discovery/snapshot.d.ts +5 -1
- package/dist/session/discovery/transport.d.ts +37 -3
- package/dist/session/discovery/transport.js +62 -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 +4 -2
- package/dist/session/options.js +3 -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 -69
- 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 +2 -1
- package/dist/session/routing/request.js +2 -1
- 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 +70 -22
- 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 +61 -112
- 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 +28 -14
- package/dist/transport/auto/decision.js +39 -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 +10 -3
- package/dist/transport/auto/transport.js +41 -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 +8 -35
- 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/configuration.d.ts +7 -0
- package/dist/transport/http/configuration.js +2 -0
- package/dist/transport/http/index.d.ts +1 -1
- package/dist/transport/http/index.js +1 -1
- package/dist/transport/http/response.d.ts +13 -1
- package/dist/transport/http/response.js +22 -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 +14 -1
- package/dist/transport/http/transport.js +110 -35
- package/dist/transport/http/unprocessed.d.ts +5 -0
- package/dist/transport/http/unprocessed.js +28 -0
- package/dist/transport/index.d.ts +2 -1
- package/dist/transport/index.js +2 -1
- package/dist/transport/level.d.ts +42 -0
- package/dist/transport/level.js +58 -0
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/transport.d.ts +33 -0
- package/dist/transport/unary.d.ts +9 -0
- package/dist/transport/unary.js +36 -0
- package/dist/transport/websocket/connection.d.ts +13 -4
- package/dist/transport/websocket/connection.js +123 -96
- package/dist/transport/websocket/exchange.d.ts +15 -19
- package/dist/transport/websocket/exchange.js +87 -183
- package/dist/transport/websocket/transport.d.ts +19 -1
- package/dist/transport/websocket/transport.js +50 -21
- package/package.json +9 -7
- 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
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { CODES } from '@astrale-os/kernel-protocol/errors';
|
|
2
|
-
import { ResponseError, TransportError } from '../../errors/index.js';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
import { ResponseError, retryAfterOf, TransportError } from '../../errors/index.js';
|
|
3
|
+
import { retrySessionRequest } from '../retry.js';
|
|
4
|
+
/**
|
|
5
|
+
* Repeat one content request that failed before any of its result was delivered: after a pause of
|
|
6
|
+
* at least 100 ms, then 200 ms, or of the delay a refusal prescribes when it is longer, each
|
|
7
|
+
* lengthened by a random share of up to as much again.
|
|
8
|
+
*/
|
|
9
|
+
export function retry(operation, request) {
|
|
10
|
+
return retrySessionRequest(operation, pause, request);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The pause after failed attempt `failed`: at least 100 ms doubled at each attempt, or the delay a
|
|
14
|
+
* refusal prescribes when it is longer, and up to as much again drawn at random, so that clients
|
|
15
|
+
* refused together do not all come back at once.
|
|
16
|
+
*/
|
|
17
|
+
function pause(cause, failed) {
|
|
18
|
+
const after = retryAfterOf(cause);
|
|
19
|
+
if (after === undefined && !retryable(cause))
|
|
20
|
+
return undefined;
|
|
21
|
+
const least = Math.max(after ?? 0, 100 * 2 ** (failed - 1));
|
|
22
|
+
return least + Math.random() * least;
|
|
18
23
|
}
|
|
19
24
|
function retryable(cause) {
|
|
20
25
|
return ((cause instanceof TransportError && cause.context.kind === 'invocation') ||
|
|
@@ -24,18 +29,3 @@ function retryable(cause) {
|
|
|
24
29
|
cause.code === CODES.DEADLINE_EXCEEDED ||
|
|
25
30
|
cause.code === CODES.OUTCOME_UNKNOWN)));
|
|
26
31
|
}
|
|
27
|
-
function delay(ms, signal) {
|
|
28
|
-
return new Promise((resolve, reject) => {
|
|
29
|
-
if (signal.aborted)
|
|
30
|
-
return reject(signal.reason);
|
|
31
|
-
const timer = setTimeout(() => {
|
|
32
|
-
signal.removeEventListener('abort', abort);
|
|
33
|
-
resolve();
|
|
34
|
-
}, ms);
|
|
35
|
-
const abort = () => {
|
|
36
|
-
clearTimeout(timer);
|
|
37
|
-
reject(signal.reason);
|
|
38
|
-
};
|
|
39
|
-
signal.addEventListener('abort', abort, { once: true });
|
|
40
|
-
});
|
|
41
|
-
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { admitControlOptions, type SessionControlOptions } from './control.js';
|
|
2
2
|
export { DEFAULT_MAXIMUM_BUNDLE_BYTES, DEFAULT_MAXIMUM_RELEASE_BYTES, DEFAULT_MAXIMUM_RESPONSE_BYTES, DEFAULT_MAXIMUM_ROUTES, DEFAULT_TIMEOUT_MS, platformDuration, positive, } from './limits.js';
|
|
3
|
-
export { createSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, type SessionOperation, } from './operation.js';
|
|
4
|
-
export { admitKernel, admitPolicy, allowEndpoint, kernelInvocationEndpoint, type SessionPolicy, } from './policy.js';
|
|
3
|
+
export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, type SessionOperation, } from './operation.js';
|
|
4
|
+
export { admitKernel, admitPolicy, allowEndpoint, endpointAllowed, kernelInvocationEndpoint, type SessionPolicy, } from './policy.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { admitControlOptions } from './control.js';
|
|
2
2
|
export { DEFAULT_MAXIMUM_BUNDLE_BYTES, DEFAULT_MAXIMUM_RELEASE_BYTES, DEFAULT_MAXIMUM_RESPONSE_BYTES, DEFAULT_MAXIMUM_ROUTES, DEFAULT_TIMEOUT_MS, platformDuration, positive, } from './limits.js';
|
|
3
|
-
export { createSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, } from './operation.js';
|
|
4
|
-
export { admitKernel, admitPolicy, allowEndpoint, kernelInvocationEndpoint, } from './policy.js';
|
|
3
|
+
export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, } from './operation.js';
|
|
4
|
+
export { admitKernel, admitPolicy, allowEndpoint, endpointAllowed, kernelInvocationEndpoint, } from './policy.js';
|
|
@@ -20,3 +20,5 @@ export declare function sessionTransportOptions(operation: SessionOperation): {
|
|
|
20
20
|
export declare function sessionTransportOptions(operation: SessionOperation, idempotencyKey: RequestOptions['idempotencyKey']): RequestOptions;
|
|
21
21
|
/** Settle independently of a caller capability that ignores its supplied AbortSignal. */
|
|
22
22
|
export declare function runSessionOperation<Result>(operation: SessionOperation, task: (signal: AbortSignal) => Result | PromiseLike<Result>): Promise<Result>;
|
|
23
|
+
/** Wait `ms` before another attempt, ending early with the operation's interruption. */
|
|
24
|
+
export declare function pauseSessionOperation(operation: SessionOperation, ms: number): Promise<void>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SessionError } from '../../errors/index.js';
|
|
2
|
+
import { pause } from '../../transport/index.js';
|
|
2
3
|
/** Create one total wall-clock budget for a complete public Client Session operation. */
|
|
3
4
|
export function createSessionOperation(lifecycle, input, defaultTimeoutMs) {
|
|
4
5
|
const timeoutMs = input?.timeoutMs ?? defaultTimeoutMs;
|
|
@@ -54,6 +55,10 @@ export async function runSessionOperation(operation, task) {
|
|
|
54
55
|
operation.signal.removeEventListener('abort', abort);
|
|
55
56
|
});
|
|
56
57
|
}
|
|
58
|
+
/** Wait `ms` before another attempt, ending early with the operation's interruption. */
|
|
59
|
+
export function pauseSessionOperation(operation, ms) {
|
|
60
|
+
return runSessionOperation(operation, (signal) => pause(ms, signal));
|
|
61
|
+
}
|
|
57
62
|
function requireSessionOperation(operation) {
|
|
58
63
|
if (!operation.signal.aborted)
|
|
59
64
|
return;
|
|
@@ -8,3 +8,5 @@ export declare function admitPolicy(input: SessionPolicy): Readonly<SessionPolic
|
|
|
8
8
|
export declare function admitKernel(input: string, insecure: boolean): IssuerId;
|
|
9
9
|
export declare function kernelInvocationEndpoint(input: IssuerId): string;
|
|
10
10
|
export declare function allowEndpoint(url: string, policy: Readonly<SessionPolicy>): void;
|
|
11
|
+
/** Apply transport policy and ask whether the caller allows this exact URL. */
|
|
12
|
+
export declare function endpointAllowed(url: string, policy: Readonly<SessionPolicy>): boolean;
|
|
@@ -40,6 +40,11 @@ export function kernelInvocationEndpoint(input) {
|
|
|
40
40
|
return `${input}/invoke`;
|
|
41
41
|
}
|
|
42
42
|
export function allowEndpoint(url, policy) {
|
|
43
|
+
if (!endpointAllowed(url, policy))
|
|
44
|
+
throw new ClientError('Endpoint is denied by Session policy.');
|
|
45
|
+
}
|
|
46
|
+
/** Apply transport policy and ask whether the caller allows this exact URL. */
|
|
47
|
+
export function endpointAllowed(url, policy) {
|
|
43
48
|
const parsed = new URL(url);
|
|
44
49
|
if (parsed.protocol !== 'https:' && parsed.protocol !== 'wss:') {
|
|
45
50
|
if ((parsed.protocol !== 'http:' && parsed.protocol !== 'ws:') ||
|
|
@@ -47,7 +52,5 @@ export function allowEndpoint(url, policy) {
|
|
|
47
52
|
throw new ClientError('Insecure or unsupported endpoint is not allowed.');
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
|
-
|
|
51
|
-
throw new ClientError('Endpoint is denied by Session policy.');
|
|
52
|
-
}
|
|
55
|
+
return policy.allowEndpoint?.(parsed.toString()) !== false;
|
|
53
56
|
}
|
|
@@ -24,7 +24,8 @@ export type CredentialsState = {
|
|
|
24
24
|
};
|
|
25
25
|
/**
|
|
26
26
|
* Acquire one fresh credential: a compact JWT, whose `exp` claim is its expiry, or an explicit
|
|
27
|
-
* credential and expiry. The supplied signal is owned by Credentials
|
|
27
|
+
* credential and expiry. The supplied signal is owned by Credentials: the attempt fails once it
|
|
28
|
+
* aborts, at the mint budget at the latest, whether or not the mint observes it.
|
|
28
29
|
*/
|
|
29
30
|
export type CredentialsMint = (signal: AbortSignal) => string | SessionCredential | PromiseLike<string | SessionCredential>;
|
|
30
31
|
export interface CredentialsOptions {
|
|
@@ -31,13 +31,14 @@ export function credentials(source) {
|
|
|
31
31
|
const notify = options.onChange?.bind(options);
|
|
32
32
|
// The threshold is the coverage the caller already declared, never a separate guessed constant.
|
|
33
33
|
const marginMs = delegationCoverageMs(ttlSeconds);
|
|
34
|
-
|
|
35
|
-
let state = Object.freeze(
|
|
34
|
+
const initial = options.initial === undefined ? undefined : admitInitial(options.initial);
|
|
35
|
+
let state = Object.freeze(initial === undefined
|
|
36
36
|
? { status: 'empty' }
|
|
37
|
-
: { status: 'valid', credential:
|
|
38
|
-
let
|
|
39
|
-
let controller;
|
|
37
|
+
: { status: 'valid', credential: initial });
|
|
38
|
+
let minting;
|
|
40
39
|
let waiting = 0;
|
|
40
|
+
/** A caller without a signal keeps the mint in flight alive; anticipation does not. */
|
|
41
|
+
let unsignalled = false;
|
|
41
42
|
let anticipating = false;
|
|
42
43
|
let refreshTimer;
|
|
43
44
|
let expiryTimer;
|
|
@@ -54,7 +55,12 @@ export function credentials(source) {
|
|
|
54
55
|
async function resolve(_call, signal) {
|
|
55
56
|
return { credential: (await acquire(signal)).credential };
|
|
56
57
|
}
|
|
58
|
+
/** The credential the published state holds, if any. */
|
|
59
|
+
function held() {
|
|
60
|
+
return 'credential' in state ? state.credential : undefined;
|
|
61
|
+
}
|
|
57
62
|
function acquire(signal) {
|
|
63
|
+
const current = held();
|
|
58
64
|
if (current !== undefined && covers(current))
|
|
59
65
|
return Promise.resolve(current);
|
|
60
66
|
return share(signal);
|
|
@@ -75,9 +81,11 @@ export function credentials(source) {
|
|
|
75
81
|
function share(signal) {
|
|
76
82
|
if (signal?.aborted === true)
|
|
77
83
|
return Promise.reject(signal.reason);
|
|
78
|
-
const
|
|
79
|
-
if (signal === undefined)
|
|
80
|
-
|
|
84
|
+
const flight = minting ?? begin();
|
|
85
|
+
if (signal === undefined) {
|
|
86
|
+
unsignalled = true;
|
|
87
|
+
return flight.promise;
|
|
88
|
+
}
|
|
81
89
|
waiting += 1;
|
|
82
90
|
let settled = false;
|
|
83
91
|
return new Promise((complete, fail) => {
|
|
@@ -88,8 +96,8 @@ export function credentials(source) {
|
|
|
88
96
|
waiting -= 1;
|
|
89
97
|
signal.removeEventListener('abort', cancel);
|
|
90
98
|
// Nobody is waiting and nobody is anticipating: the mint has no beneficiary left.
|
|
91
|
-
if (waiting === 0 && !anticipating)
|
|
92
|
-
controller
|
|
99
|
+
if (waiting === 0 && !anticipating && !unsignalled)
|
|
100
|
+
minting?.controller.abort(signal.reason);
|
|
93
101
|
};
|
|
94
102
|
const cancel = () => {
|
|
95
103
|
const reason = signal.reason;
|
|
@@ -97,7 +105,7 @@ export function credentials(source) {
|
|
|
97
105
|
fail(reason);
|
|
98
106
|
};
|
|
99
107
|
signal.addEventListener('abort', cancel, { once: true });
|
|
100
|
-
|
|
108
|
+
flight.promise.then((credential) => {
|
|
101
109
|
if (settled)
|
|
102
110
|
return;
|
|
103
111
|
settled = true;
|
|
@@ -114,21 +122,38 @@ export function credentials(source) {
|
|
|
114
122
|
});
|
|
115
123
|
});
|
|
116
124
|
}
|
|
125
|
+
/** Begin a mint; without a mint capability, the returned one has already failed and is not held. */
|
|
117
126
|
function begin() {
|
|
127
|
+
const controller = new AbortController();
|
|
118
128
|
if (mint === undefined) {
|
|
119
129
|
const error = new ClientError('Session credential needs renewal, but no mint was supplied.');
|
|
120
130
|
settleFailure(error);
|
|
121
|
-
return Promise.reject(error);
|
|
131
|
+
return { promise: Promise.reject(error), controller };
|
|
122
132
|
}
|
|
123
133
|
const renew = mint;
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
134
|
+
unsignalled = false;
|
|
135
|
+
const signal = controller.signal;
|
|
136
|
+
// The budget is a timer of this owner's clock, cleared once the attempt settles.
|
|
137
|
+
const deadline = setTimeout(() => {
|
|
138
|
+
controller.abort(new DOMException('Session credential mint timed out.', 'TimeoutError'));
|
|
139
|
+
}, MINT_TIMEOUT_MS);
|
|
140
|
+
let abandon;
|
|
141
|
+
const promise = (async () => {
|
|
129
142
|
try {
|
|
130
|
-
|
|
131
|
-
|
|
143
|
+
// A mint that throws instead of rejecting fails this attempt alike: the executor turns the
|
|
144
|
+
// throw into a rejection, so the attempt settles only after it yields, once `minting`
|
|
145
|
+
// holds it, and the cleanup below releases it for the next acquisition. The budget is
|
|
146
|
+
// this owner's, not the mint's: the attempt fails once its signal aborts, whether or not
|
|
147
|
+
// the mint observes it, so an answer that never comes cannot hold `minting` forever.
|
|
148
|
+
const answer = new Promise((complete) => {
|
|
149
|
+
complete(renew(signal));
|
|
150
|
+
});
|
|
151
|
+
const returned = new Promise((complete, fail) => {
|
|
152
|
+
abandon = () => fail(signal.reason);
|
|
153
|
+
signal.addEventListener('abort', abandon, { once: true });
|
|
154
|
+
answer.then(complete, fail);
|
|
155
|
+
});
|
|
156
|
+
const minted = admitMinted(await returned);
|
|
132
157
|
attempt = 0;
|
|
133
158
|
publish({ status: 'valid', credential: minted });
|
|
134
159
|
if (anticipating)
|
|
@@ -140,21 +165,26 @@ export function credentials(source) {
|
|
|
140
165
|
throw error;
|
|
141
166
|
}
|
|
142
167
|
finally {
|
|
143
|
-
|
|
144
|
-
|
|
168
|
+
clearTimeout(deadline);
|
|
169
|
+
if (abandon !== undefined)
|
|
170
|
+
signal.removeEventListener('abort', abandon);
|
|
171
|
+
minting = undefined;
|
|
145
172
|
}
|
|
146
173
|
})();
|
|
147
|
-
|
|
174
|
+
minting = { promise, controller };
|
|
175
|
+
// Published once the mint is held: an observer that acquires now joins it. It settles later.
|
|
176
|
+
const current = held();
|
|
177
|
+
publish({ status: 'refreshing', ...(current === undefined ? {} : { credential: current }) });
|
|
148
178
|
// A rejection reaches every waiter; keep an unattached one from surfacing as unhandled.
|
|
149
|
-
|
|
150
|
-
return
|
|
179
|
+
promise.catch(() => undefined);
|
|
180
|
+
return minting;
|
|
151
181
|
}
|
|
152
182
|
function settleFailure(error) {
|
|
183
|
+
const current = held();
|
|
153
184
|
if (current !== undefined && current.expiresAt > Date.now()) {
|
|
154
185
|
publish({ status: 'degraded', credential: current, error });
|
|
155
186
|
}
|
|
156
187
|
else {
|
|
157
|
-
current = undefined;
|
|
158
188
|
publish({ status: 'expired', error });
|
|
159
189
|
}
|
|
160
190
|
if (anticipating)
|
|
@@ -165,8 +195,9 @@ export function credentials(source) {
|
|
|
165
195
|
clearTimers();
|
|
166
196
|
if (!anticipating)
|
|
167
197
|
return;
|
|
198
|
+
const current = held();
|
|
168
199
|
if (current === undefined) {
|
|
169
|
-
void
|
|
200
|
+
void (minting ?? begin()).promise.catch(() => undefined);
|
|
170
201
|
return;
|
|
171
202
|
}
|
|
172
203
|
// The threshold itself, with no extra lead: chained providers sharing this rule must not
|
|
@@ -177,6 +208,7 @@ export function credentials(source) {
|
|
|
177
208
|
function retry() {
|
|
178
209
|
if (refreshTimer !== undefined)
|
|
179
210
|
return;
|
|
211
|
+
const current = held();
|
|
180
212
|
if (current !== undefined)
|
|
181
213
|
armExpiry(current);
|
|
182
214
|
// Without a mint nothing can replace the held credential; only its expiry remains to report.
|
|
@@ -189,7 +221,7 @@ export function credentials(source) {
|
|
|
189
221
|
function armRefresh(delay) {
|
|
190
222
|
refreshTimer = setTimeout(() => {
|
|
191
223
|
refreshTimer = undefined;
|
|
192
|
-
void
|
|
224
|
+
void (minting ?? begin()).promise.catch(() => undefined);
|
|
193
225
|
}, delay);
|
|
194
226
|
}
|
|
195
227
|
function armExpiry(credential) {
|
|
@@ -197,9 +229,10 @@ export function credentials(source) {
|
|
|
197
229
|
clearTimeout(expiryTimer);
|
|
198
230
|
expiryTimer = setTimeout(() => {
|
|
199
231
|
expiryTimer = undefined;
|
|
200
|
-
|
|
232
|
+
// A mint in flight settles within its budget and reports the outcome itself: expiry is
|
|
233
|
+
// published only while no attempt is pending, so `expired` always means a retry can start.
|
|
234
|
+
if (!anticipating || held() !== credential || minting !== undefined)
|
|
201
235
|
return;
|
|
202
|
-
current = undefined;
|
|
203
236
|
publish({ status: 'expired', error: new ClientError('Session credential expired.') });
|
|
204
237
|
}, clamp(credential.expiresAt - Date.now()));
|
|
205
238
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { bundle } from '@astrale-os/kernel-dsl/v1';
|
|
2
2
|
import type { DomainRelease } from '@astrale-os/kernel-protocol/release';
|
|
3
|
+
import { type CompactBundleHint } from '@astrale-os/kernel-protocol/release';
|
|
3
4
|
import type { Fetch } from '../../transport/http/index.js';
|
|
4
5
|
import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
5
6
|
import { ClientError } from '../../errors/index.js';
|
|
@@ -7,16 +8,24 @@ interface BundleDiscoveryOptions {
|
|
|
7
8
|
readonly fetch: Fetch;
|
|
8
9
|
readonly policy: Readonly<SessionPolicy>;
|
|
9
10
|
readonly maximumBytes: number;
|
|
11
|
+
readonly maximumLogicalBytes: number;
|
|
12
|
+
readonly hintFor: (release: DomainRelease) => CompactBundleHint | undefined;
|
|
10
13
|
}
|
|
11
|
-
/**
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
+
/** A Release's Bundle, or evidence that its digest-addressed artifact is no longer served. */
|
|
15
|
+
export type BundleRead = {
|
|
16
|
+
readonly kind: 'bundle';
|
|
17
|
+
readonly bundle: bundle.Bundle;
|
|
18
|
+
} | {
|
|
19
|
+
readonly kind: 'unavailable';
|
|
20
|
+
readonly error: ClientError;
|
|
21
|
+
};
|
|
14
22
|
export declare class BundleDiscovery {
|
|
15
23
|
private readonly options;
|
|
16
24
|
private retained?;
|
|
17
25
|
constructor(options: BundleDiscoveryOptions);
|
|
18
|
-
get(release: DomainRelease, operation: SessionOperation): Promise<
|
|
26
|
+
get(release: DomainRelease, operation: SessionOperation): Promise<BundleRead>;
|
|
19
27
|
clear(): void;
|
|
20
28
|
private fetch;
|
|
29
|
+
private compact;
|
|
21
30
|
}
|
|
22
31
|
export {};
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
import { bundleUrl, decodeBundle } from '@astrale-os/kernel-protocol/release';
|
|
1
|
+
import { bundleUrl, COMPACT_BUNDLE_MEDIA_TYPE, decodeBundle, decodeCompactBundle, isCompactBundleRepresentationError, verifyBundleInventory, } from '@astrale-os/kernel-protocol/release';
|
|
2
2
|
import { ClientError } from '../../errors/index.js';
|
|
3
|
-
import { allowEndpoint } from '../context/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
/** Internal evidence that a selected immutable artifact is no longer served. */
|
|
7
|
-
export class BundleUnavailableError extends ClientError {
|
|
8
|
-
}
|
|
3
|
+
import { allowEndpoint, endpointAllowed } from '../context/index.js';
|
|
4
|
+
import { DocumentRepresentationError } from './response.js';
|
|
5
|
+
import { fetchDocument } from './transport.js';
|
|
9
6
|
export class BundleDiscovery {
|
|
10
7
|
options;
|
|
11
8
|
retained;
|
|
@@ -15,48 +12,100 @@ export class BundleDiscovery {
|
|
|
15
12
|
async get(release, operation) {
|
|
16
13
|
const key = bundleKey(release);
|
|
17
14
|
allowEndpoint(release.schema.bundle.href, this.options.policy);
|
|
18
|
-
if (this.retained?.key === key)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
if (this.retained?.key === key) {
|
|
16
|
+
const { value } = this.retained;
|
|
17
|
+
if (this.retained.release !== release.digest) {
|
|
18
|
+
verifyBundleInventory(release, value);
|
|
19
|
+
this.retained = { key, release: release.digest, value };
|
|
20
|
+
}
|
|
21
|
+
return { kind: 'bundle', bundle: value };
|
|
22
|
+
}
|
|
23
|
+
const read = await this.fetch(release, operation);
|
|
24
|
+
if (read.kind === 'bundle')
|
|
25
|
+
this.retained = { key, release: release.digest, value: read.bundle };
|
|
26
|
+
return read;
|
|
23
27
|
}
|
|
24
28
|
clear() {
|
|
25
29
|
this.retained = undefined;
|
|
26
30
|
}
|
|
27
31
|
async fetch(release, operation) {
|
|
28
|
-
|
|
32
|
+
const { href, ref } = release.schema.bundle;
|
|
33
|
+
if (ref.size > this.options.maximumLogicalBytes) {
|
|
29
34
|
throw new ClientError('Bundle artifact exceeds its byte limit.');
|
|
30
35
|
}
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
void response.body?.cancel().catch(() => undefined);
|
|
44
|
-
throw new BundleUnavailableError(`Bundle retrieval returned HTTP ${response.status}.`);
|
|
36
|
+
const hint = this.options.hintFor(release);
|
|
37
|
+
if (hint !== undefined && endpointAllowed(hint.href, this.options.policy)) {
|
|
38
|
+
try {
|
|
39
|
+
const result = await this.compact(release, hint, operation);
|
|
40
|
+
if (result.kind === 'bundle' || ref.size > this.options.maximumBytes)
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
const unusable = error instanceof DocumentRepresentationError || isCompactBundleRepresentationError(error);
|
|
45
|
+
if (!unusable || ref.size > this.options.maximumBytes)
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
45
48
|
}
|
|
46
|
-
if (
|
|
47
|
-
throw new ClientError(
|
|
48
|
-
if (response.headers.get('content-type') !== release.schema.bundle.ref.mediaType) {
|
|
49
|
-
throw new ClientError('Bundle artifact media type differs from its descriptor.');
|
|
49
|
+
if (ref.size > this.options.maximumBytes) {
|
|
50
|
+
throw new ClientError('Bundle artifact exceeds its byte limit.');
|
|
50
51
|
}
|
|
51
|
-
const
|
|
52
|
-
|
|
52
|
+
const immutable = href === bundleUrl(release.identity.issuer, ref);
|
|
53
|
+
const fetched = await fetchDocument(operation, this.options.fetch, {
|
|
54
|
+
resource: 'bundle',
|
|
55
|
+
label: 'Bundle retrieval',
|
|
56
|
+
url: href,
|
|
57
|
+
mediaType: ref.mediaType,
|
|
58
|
+
mediaTypeMismatch: 'Bundle artifact media type differs from its descriptor.',
|
|
59
|
+
maximumBytes: ref.size,
|
|
60
|
+
...(immutable ? {} : { cache: 'no-cache' }),
|
|
61
|
+
notModified: false,
|
|
62
|
+
// Only a digest-addressed artifact can be retired by a Release change.
|
|
63
|
+
unavailable: immutable ? [404, 410] : [],
|
|
64
|
+
});
|
|
65
|
+
if (fetched.kind !== 'document')
|
|
66
|
+
return fetched;
|
|
67
|
+
const etag = fetched.headers.get('etag');
|
|
68
|
+
if (etag !== null && etag !== `"${ref.digest}"`) {
|
|
53
69
|
throw new ClientError('Bundle response ETag differs from its descriptor.');
|
|
54
70
|
}
|
|
55
|
-
const bytes = await
|
|
56
|
-
if (bytes.byteLength !==
|
|
71
|
+
const bytes = await fetched.read();
|
|
72
|
+
if (bytes.byteLength !== ref.size) {
|
|
57
73
|
throw new ClientError('Bundle artifact size differs from its descriptor.');
|
|
58
74
|
}
|
|
59
|
-
return decodeBundle(release, bytes);
|
|
75
|
+
return { kind: 'bundle', bundle: decodeBundle(release, bytes) };
|
|
76
|
+
}
|
|
77
|
+
async compact(release, hint, operation) {
|
|
78
|
+
const fetched = await fetchDocument(operation, this.options.fetch, {
|
|
79
|
+
resource: 'bundle',
|
|
80
|
+
label: 'Compact Bundle retrieval',
|
|
81
|
+
status: 200,
|
|
82
|
+
url: hint.href,
|
|
83
|
+
mediaType: COMPACT_BUNDLE_MEDIA_TYPE,
|
|
84
|
+
mediaTypeMismatch: 'Compact Bundle response media type differs from its advertisement.',
|
|
85
|
+
maximumBytes: this.options.maximumBytes,
|
|
86
|
+
notModified: false,
|
|
87
|
+
unavailable: [404, 410],
|
|
88
|
+
});
|
|
89
|
+
if (fetched.kind !== 'document')
|
|
90
|
+
return fetched;
|
|
91
|
+
const etag = fetched.headers.get('etag');
|
|
92
|
+
const encoding = fetched.headers.get('content-encoding');
|
|
93
|
+
if ((etag !== null && etag !== `"${hint.digest}"`) ||
|
|
94
|
+
(encoding !== null && encoding.toLowerCase() !== 'identity')) {
|
|
95
|
+
fetched.discard();
|
|
96
|
+
throw new DocumentRepresentationError('Compact Bundle response validators or encoding differ from its advertisement.');
|
|
97
|
+
}
|
|
98
|
+
const bytes = await fetched.read();
|
|
99
|
+
const length = fetched.headers.get('content-length');
|
|
100
|
+
if (length !== null && (!/^\d+$/u.test(length) || Number(length) !== bytes.byteLength)) {
|
|
101
|
+
throw new DocumentRepresentationError('Compact Bundle size differs from its Content-Length.');
|
|
102
|
+
}
|
|
103
|
+
const physical = {
|
|
104
|
+
digest: hint.digest,
|
|
105
|
+
mediaType: COMPACT_BUNDLE_MEDIA_TYPE,
|
|
106
|
+
size: bytes.byteLength,
|
|
107
|
+
};
|
|
108
|
+
return { kind: 'bundle', bundle: await decodeCompactBundle(release, physical, bytes) };
|
|
60
109
|
}
|
|
61
110
|
}
|
|
62
111
|
function bundleKey(release) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { IssuerId } from '@astrale-os/kernel-core/auth/issuer';
|
|
2
2
|
import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
3
|
+
import type { BundleDiscovery } from './bundle.js';
|
|
3
4
|
import type { IntrinsicRelease } from './release.js';
|
|
4
5
|
import type { SessionSnapshot } from './snapshot.js';
|
|
5
|
-
import { type BundleDiscovery } from './bundle.js';
|
|
6
6
|
/** Revalidating reader of the source Kernel's intrinsic release. */
|
|
7
7
|
interface IntrinsicReleases {
|
|
8
8
|
revalidate(operation: SessionOperation): Promise<IntrinsicRelease>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { runSessionOperation } from '../context/index.js';
|
|
2
|
-
import { rootViewCatalog } from '../views/index.js';
|
|
3
|
-
import { BundleUnavailableError } from './bundle.js';
|
|
2
|
+
import { rootViewCatalog } from '../views/legacy/index.js';
|
|
4
3
|
import { createSessionSnapshot } from './snapshot.js';
|
|
5
4
|
export class SessionDiscovery {
|
|
6
5
|
kernel;
|
|
@@ -60,19 +59,17 @@ export class SessionDiscovery {
|
|
|
60
59
|
}
|
|
61
60
|
async acquireSnapshot(operation) {
|
|
62
61
|
let release = await this.release(operation);
|
|
63
|
-
let
|
|
64
|
-
|
|
65
|
-
artifact = await this.bundles.get(release, operation);
|
|
66
|
-
}
|
|
67
|
-
catch (cause) {
|
|
68
|
-
if (!(cause instanceof BundleUnavailableError))
|
|
69
|
-
throw cause;
|
|
62
|
+
let read = await this.bundles.get(release, operation);
|
|
63
|
+
if (read.kind === 'unavailable') {
|
|
70
64
|
const current = await this.release(operation);
|
|
71
65
|
if (current.digest === release.digest)
|
|
72
|
-
throw
|
|
66
|
+
throw read.error;
|
|
73
67
|
release = current;
|
|
74
|
-
|
|
68
|
+
read = await this.bundles.get(release, operation);
|
|
69
|
+
if (read.kind === 'unavailable')
|
|
70
|
+
throw read.error;
|
|
75
71
|
}
|
|
72
|
+
const artifact = read.bundle;
|
|
76
73
|
this.requireOpen();
|
|
77
74
|
if (this.retained !== undefined && this.retained.release.digest === release.digest) {
|
|
78
75
|
return this.retained;
|
|
@@ -19,6 +19,7 @@ export interface SessionDiscoveryOptions {
|
|
|
19
19
|
/** Bounds the source Kernel's own `release.json`. */
|
|
20
20
|
readonly maximumReleaseBytes: number;
|
|
21
21
|
readonly maximumBundleBytes: number;
|
|
22
|
+
readonly maximumLogicalBundleBytes?: number;
|
|
22
23
|
readonly requireOpen: () => void;
|
|
23
24
|
readonly acquisitionOperation: () => SessionOperation;
|
|
24
25
|
}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import { defaultLimits } from '@astrale-os/kernel-dsl/value';
|
|
1
2
|
import { BundleDiscovery } from './bundle.js';
|
|
2
3
|
import { SessionDiscovery as SessionDiscoveryImplementation } from './discovery.js';
|
|
3
4
|
import { ReleaseDiscovery } from './release.js';
|
|
4
5
|
export { boundedResponse, requireExactResponse } from './response.js';
|
|
5
6
|
export function createSessionDiscovery(options) {
|
|
6
7
|
const source = { kernel: options.kernel, fetch: options.fetch, policy: options.policy };
|
|
7
|
-
|
|
8
|
+
const releases = new ReleaseDiscovery({ ...source, maximumBytes: options.maximumReleaseBytes });
|
|
9
|
+
return new SessionDiscoveryImplementation(options.kernel, options.policy, releases, new BundleDiscovery({
|
|
8
10
|
fetch: options.fetch,
|
|
9
11
|
policy: options.policy,
|
|
10
12
|
maximumBytes: options.maximumBundleBytes,
|
|
13
|
+
maximumLogicalBytes: Math.min(options.maximumLogicalBundleBytes ?? options.maximumBundleBytes, defaultLimits.maxBytes),
|
|
14
|
+
hintFor: (release) => releases.hintFor(release),
|
|
11
15
|
}), options.requireOpen, options.acquisitionOperation);
|
|
12
16
|
}
|
|
@@ -17,6 +17,8 @@ interface ReleaseDiscoveryOptions {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare class ReleaseDiscovery {
|
|
19
19
|
private readonly options;
|
|
20
|
+
private readonly hints;
|
|
21
|
+
hintFor(release: IntrinsicRelease): release.CompactBundleHint | undefined;
|
|
20
22
|
private release;
|
|
21
23
|
private revision;
|
|
22
24
|
constructor(options: ReleaseDiscoveryOptions);
|