@astrale-os/kernel-client 1.0.0-beta.25 → 1.0.0-beta.27
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/client/admission.js +3 -1
- package/dist/client/client.d.ts +16 -3
- package/dist/client/client.js +36 -15
- package/dist/client/index.d.ts +2 -2
- package/dist/client/index.js +1 -1
- package/dist/client/routing-control.d.ts +11 -15
- package/dist/client/routing-control.js +21 -29
- package/dist/errors/query-input.js +2 -1
- package/dist/errors/stale-route-error.d.ts +1 -1
- package/dist/errors/stale-route-error.js +1 -1
- package/dist/graph/page-contract.d.ts +8 -3
- package/dist/graph/query.js +10 -4
- package/dist/graph/query.typecheck.js +1 -1
- package/dist/session/admission.js +4 -2
- package/dist/session/credential/credentials.js +32 -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 +7 -10
- package/dist/session/discovery/release.js +18 -28
- package/dist/session/discovery/transport.d.ts +35 -3
- package/dist/session/discovery/transport.js +40 -1
- 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/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 +2 -1
- package/dist/session/routing/dispatch.js +83 -61
- package/dist/session/routing/index.d.ts +1 -1
- package/dist/session/routing/recovery.d.ts +3 -6
- package/dist/session/routing/recovery.js +3 -14
- package/dist/session/routing/store.js +4 -5
- package/dist/session/views/kernel.d.ts +31 -0
- package/dist/session/views/kernel.js +30 -0
- package/dist/session/views/read.js +5 -10
- package/dist/session/views/view.js +1 -6
- 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.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/failure.d.ts +28 -1
- package/dist/transport/failure.js +57 -1
- package/dist/transport/http/admission.js +2 -2
- package/dist/transport/http/binary.js +1 -1
- package/dist/transport/http/byte-stream.d.ts +2 -1
- package/dist/transport/http/byte-stream.js +10 -7
- package/dist/transport/http/stream.d.ts +8 -13
- package/dist/transport/http/stream.js +32 -79
- package/dist/transport/http/transport.js +11 -22
- package/dist/transport/stream.d.ts +52 -0
- package/dist/transport/stream.js +101 -0
- package/dist/transport/unary.d.ts +3 -2
- package/dist/transport/unary.js +3 -3
- package/dist/transport/websocket/connection.d.ts +7 -3
- package/dist/transport/websocket/connection.js +102 -85
- package/dist/transport/websocket/exchange.d.ts +10 -19
- package/dist/transport/websocket/exchange.js +72 -153
- package/dist/transport/websocket/transport.js +11 -19
- package/package.json +6 -5
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
import { bundleUrl, decodeBundle } from '@astrale-os/kernel-protocol/release';
|
|
2
2
|
import { ClientError } from '../../errors/index.js';
|
|
3
3
|
import { allowEndpoint } from '../context/index.js';
|
|
4
|
-
import {
|
|
5
|
-
import { runAcquisition } from './transport.js';
|
|
6
|
-
/** Internal evidence that a selected immutable artifact is no longer served. */
|
|
7
|
-
export class BundleUnavailableError extends ClientError {
|
|
8
|
-
}
|
|
4
|
+
import { fetchDocument } from './transport.js';
|
|
9
5
|
export class BundleDiscovery {
|
|
10
6
|
options;
|
|
11
7
|
retained;
|
|
@@ -16,47 +12,44 @@ export class BundleDiscovery {
|
|
|
16
12
|
const key = bundleKey(release);
|
|
17
13
|
allowEndpoint(release.schema.bundle.href, this.options.policy);
|
|
18
14
|
if (this.retained?.key === key)
|
|
19
|
-
return this.retained.value;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
return { kind: 'bundle', bundle: this.retained.value };
|
|
16
|
+
const read = await this.fetch(release, operation);
|
|
17
|
+
if (read.kind === 'bundle')
|
|
18
|
+
this.retained = { key, value: read.bundle };
|
|
19
|
+
return read;
|
|
23
20
|
}
|
|
24
21
|
clear() {
|
|
25
22
|
this.retained = undefined;
|
|
26
23
|
}
|
|
27
24
|
async fetch(release, operation) {
|
|
28
|
-
|
|
25
|
+
const { href, ref } = release.schema.bundle;
|
|
26
|
+
if (ref.size > this.options.maximumBytes) {
|
|
29
27
|
throw new ClientError('Bundle artifact exceeds its byte limit.');
|
|
30
28
|
}
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (response.headers.get('content-type') !== release.schema.bundle.ref.mediaType) {
|
|
49
|
-
throw new ClientError('Bundle artifact media type differs from its descriptor.');
|
|
50
|
-
}
|
|
51
|
-
const etag = response.headers.get('etag');
|
|
52
|
-
if (etag !== null && etag !== `"${release.schema.bundle.ref.digest}"`) {
|
|
29
|
+
const immutable = href === bundleUrl(release.identity.issuer, ref);
|
|
30
|
+
const fetched = await fetchDocument(operation, this.options.fetch, {
|
|
31
|
+
resource: 'bundle',
|
|
32
|
+
label: 'Bundle retrieval',
|
|
33
|
+
url: href,
|
|
34
|
+
mediaType: ref.mediaType,
|
|
35
|
+
mediaTypeMismatch: 'Bundle artifact media type differs from its descriptor.',
|
|
36
|
+
maximumBytes: ref.size,
|
|
37
|
+
...(immutable ? {} : { cache: 'no-cache' }),
|
|
38
|
+
notModified: false,
|
|
39
|
+
// Only a digest-addressed artifact can be retired by a Release change.
|
|
40
|
+
unavailable: immutable ? [404, 410] : [],
|
|
41
|
+
});
|
|
42
|
+
if (fetched.kind !== 'document')
|
|
43
|
+
return fetched;
|
|
44
|
+
const etag = fetched.headers.get('etag');
|
|
45
|
+
if (etag !== null && etag !== `"${ref.digest}"`) {
|
|
53
46
|
throw new ClientError('Bundle response ETag differs from its descriptor.');
|
|
54
47
|
}
|
|
55
|
-
const bytes = await
|
|
56
|
-
if (bytes.byteLength !==
|
|
48
|
+
const bytes = await fetched.read();
|
|
49
|
+
if (bytes.byteLength !== ref.size) {
|
|
57
50
|
throw new ClientError('Bundle artifact size differs from its descriptor.');
|
|
58
51
|
}
|
|
59
|
-
return decodeBundle(release, bytes);
|
|
52
|
+
return { kind: 'bundle', bundle: decodeBundle(release, bytes) };
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
55
|
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
2
|
import { rootViewCatalog } from '../views/legacy/index.js';
|
|
3
|
-
import { BundleUnavailableError } from './bundle.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;
|
|
@@ -2,8 +2,7 @@ import * as codecs from '@astrale-os/kernel-protocol/codecs';
|
|
|
2
2
|
import * as releaseCodec from '@astrale-os/kernel-protocol/release';
|
|
3
3
|
import { ClientError } from '../../errors/index.js';
|
|
4
4
|
import { allowEndpoint } from '../context/index.js';
|
|
5
|
-
import {
|
|
6
|
-
import { runAcquisition } from './transport.js';
|
|
5
|
+
import { fetchDocument } from './transport.js';
|
|
7
6
|
/**
|
|
8
7
|
* Conditionally revalidate by release digest. Only an exact 404 means absence;
|
|
9
8
|
* all other unsuccessful responses and Fetch failures remain errors.
|
|
@@ -37,42 +36,33 @@ export class ReleaseDiscovery {
|
|
|
37
36
|
async fetch(operation, previous) {
|
|
38
37
|
const url = releaseCodec.url(this.options.kernel);
|
|
39
38
|
allowEndpoint(url, this.options.policy);
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw
|
|
53
|
-
|
|
54
|
-
if (response.status === 304) {
|
|
39
|
+
const fetched = await fetchDocument(operation, this.options.fetch, {
|
|
40
|
+
resource: 'release',
|
|
41
|
+
label: 'Release discovery',
|
|
42
|
+
url,
|
|
43
|
+
mediaType: releaseCodec.MEDIA_TYPE,
|
|
44
|
+
mediaTypeMismatch: 'Release response media type differs from the DomainRelease v4 media type.',
|
|
45
|
+
maximumBytes: this.options.maximumBytes,
|
|
46
|
+
...(previous === undefined ? {} : { ifNoneMatch: `"${previous.digest}"` }),
|
|
47
|
+
notModified: true,
|
|
48
|
+
unavailable: [404],
|
|
49
|
+
});
|
|
50
|
+
if (fetched.kind === 'unavailable')
|
|
51
|
+
throw fetched.error;
|
|
52
|
+
if (fetched.kind === 'not-modified') {
|
|
55
53
|
if (previous === undefined) {
|
|
56
54
|
throw new ClientError('Release discovery returned an unexpected not-modified response.');
|
|
57
55
|
}
|
|
58
|
-
|
|
59
|
-
if (etag !== null && etag !== `"${previous.digest}"`) {
|
|
56
|
+
if (fetched.etag !== null && fetched.etag !== `"${previous.digest}"`) {
|
|
60
57
|
throw new ClientError('Release not-modified ETag differs from the retained release digest.');
|
|
61
58
|
}
|
|
62
59
|
return previous;
|
|
63
60
|
}
|
|
64
|
-
|
|
65
|
-
throw new ClientError(`Release discovery returned HTTP ${response.status}.`);
|
|
66
|
-
}
|
|
67
|
-
if (response.headers.get('content-type') !== releaseCodec.MEDIA_TYPE) {
|
|
68
|
-
throw new ClientError('Release response media type differs from the DomainRelease v4 media type.');
|
|
69
|
-
}
|
|
70
|
-
const body = await runAcquisition(operation, 'release', 'receive', 'Kernel release response could not be read.', (signal) => boundedResponse(response, this.options.maximumBytes, signal));
|
|
71
|
-
const admitted = releaseCodec.accept(codecs.json.decode(body));
|
|
61
|
+
const admitted = releaseCodec.accept(codecs.json.decode(await fetched.read()));
|
|
72
62
|
if (admitted.identity.issuer !== this.options.kernel) {
|
|
73
63
|
throw new ClientError('Release issuer differs from the requested Kernel issuer.');
|
|
74
64
|
}
|
|
75
|
-
const etag =
|
|
65
|
+
const etag = fetched.headers.get('etag');
|
|
76
66
|
if (etag !== null && etag !== `"${admitted.digest}"`) {
|
|
77
67
|
throw new ClientError('Release response ETag differs from its release digest.');
|
|
78
68
|
}
|
|
@@ -1,4 +1,36 @@
|
|
|
1
|
-
import type { AcquisitionResource
|
|
1
|
+
import type { AcquisitionResource } from '../../errors/index.js';
|
|
2
|
+
import type { Fetch } from '../../transport/http/index.js';
|
|
2
3
|
import type { SessionOperation } from '../context/index.js';
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
import { ClientError } from '../../errors/index.js';
|
|
5
|
+
/** One public Session document requested from an endpoint the Session policy already allows. */
|
|
6
|
+
export interface DocumentRequest<NotModified extends boolean = boolean> {
|
|
7
|
+
readonly resource: AcquisitionResource;
|
|
8
|
+
/** Names the request in status and URL errors, as in `Release discovery returned HTTP 500.` */
|
|
9
|
+
readonly label: string;
|
|
10
|
+
readonly url: string;
|
|
11
|
+
/** Both the `accept` header and the only admitted `content-type`. */
|
|
12
|
+
readonly mediaType: string;
|
|
13
|
+
readonly mediaTypeMismatch: string;
|
|
14
|
+
readonly maximumBytes: number;
|
|
15
|
+
readonly ifNoneMatch?: string;
|
|
16
|
+
readonly cache?: 'no-cache';
|
|
17
|
+
/** Whether a 304 answers the request instead of failing it. */
|
|
18
|
+
readonly notModified: NotModified;
|
|
19
|
+
/** Statuses that mean the document is no longer served, as opposed to a failed retrieval. */
|
|
20
|
+
readonly unavailable: readonly number[];
|
|
21
|
+
}
|
|
22
|
+
/** The answer to a public Session document request; 304 answers only a request that admits it. */
|
|
23
|
+
export type DocumentResponse<NotModified extends boolean = boolean> = {
|
|
24
|
+
readonly kind: 'document';
|
|
25
|
+
readonly headers: Headers;
|
|
26
|
+
/** Read the body within the request's byte bound. */
|
|
27
|
+
read(): Promise<Uint8Array>;
|
|
28
|
+
} | {
|
|
29
|
+
readonly kind: 'unavailable';
|
|
30
|
+
readonly error: ClientError;
|
|
31
|
+
} | (NotModified extends true ? {
|
|
32
|
+
readonly kind: 'not-modified';
|
|
33
|
+
readonly etag: string | null;
|
|
34
|
+
} : never);
|
|
35
|
+
/** Fetch one public Session document and admit its exact URL, status and media type. */
|
|
36
|
+
export declare function fetchDocument<NotModified extends boolean>(operation: SessionOperation, fetch: Fetch, request: DocumentRequest<NotModified>): Promise<DocumentResponse<NotModified>>;
|
|
@@ -1,7 +1,46 @@
|
|
|
1
1
|
import { ClientError, TransportError } from '../../errors/index.js';
|
|
2
2
|
import { runSessionOperation } from '../context/index.js';
|
|
3
|
+
import { boundedResponse, requireExactResponse } from './response.js';
|
|
4
|
+
/** Fetch one public Session document and admit its exact URL, status and media type. */
|
|
5
|
+
export async function fetchDocument(operation, fetch, request) {
|
|
6
|
+
const { resource, label, url } = request;
|
|
7
|
+
const response = await runAcquisition(operation, resource, 'unknown', `Kernel ${resource} could not be retrieved.`, (signal) => fetch(url, {
|
|
8
|
+
headers: {
|
|
9
|
+
accept: request.mediaType,
|
|
10
|
+
...(request.ifNoneMatch === undefined ? {} : { 'if-none-match': request.ifNoneMatch }),
|
|
11
|
+
},
|
|
12
|
+
// Omitting `cache` is the browser default; Workers reject the DOM-only 'default' mode.
|
|
13
|
+
...(request.cache === undefined ? {} : { cache: request.cache }),
|
|
14
|
+
redirect: 'manual',
|
|
15
|
+
signal,
|
|
16
|
+
}));
|
|
17
|
+
requireExactResponse(response, url, label);
|
|
18
|
+
const status = `${label} returned HTTP ${response.status}.`;
|
|
19
|
+
if (request.unavailable.includes(response.status)) {
|
|
20
|
+
// Cleanup must not hold discovery open on an injected body cancellation.
|
|
21
|
+
void response.body?.cancel().catch(() => undefined);
|
|
22
|
+
return { kind: 'unavailable', error: new ClientError(status) };
|
|
23
|
+
}
|
|
24
|
+
if (request.notModified && response.status === 304) {
|
|
25
|
+
// `notModified` is true here, so the conditional member is present.
|
|
26
|
+
return {
|
|
27
|
+
kind: 'not-modified',
|
|
28
|
+
etag: response.headers.get('etag'),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (!response.ok)
|
|
32
|
+
throw new ClientError(status);
|
|
33
|
+
if (response.headers.get('content-type') !== request.mediaType) {
|
|
34
|
+
throw new ClientError(request.mediaTypeMismatch);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
kind: 'document',
|
|
38
|
+
headers: response.headers,
|
|
39
|
+
read: () => runAcquisition(operation, resource, 'receive', `Kernel ${resource} response could not be read.`, (signal) => boundedResponse(response, request.maximumBytes, signal)),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
3
42
|
/** Normalize only unknown failures from one injected acquisition capability. */
|
|
4
|
-
|
|
43
|
+
async function runAcquisition(operation, resource, phase, message, task) {
|
|
5
44
|
try {
|
|
6
45
|
return await runSessionOperation(operation, task);
|
|
7
46
|
}
|
|
@@ -7,6 +7,7 @@ import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
|
7
7
|
import type { SessionCredential } from '../credential/index.js';
|
|
8
8
|
import type { EffectiveAuth, SessionAuth } from '../routing/index.js';
|
|
9
9
|
import type { ExchangeRequest } from './options.js';
|
|
10
|
+
import type { Exhausted } from './source.js';
|
|
10
11
|
/** Everything an exchange reaches: the Session's source Kernel and authority, and the Domain legs. */
|
|
11
12
|
export interface ExchangeDependencies {
|
|
12
13
|
readonly kernel: IssuerId;
|
|
@@ -37,9 +38,9 @@ export interface DomainExchangeRequest {
|
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Delegate the source caller to one Domain issuer and redeem that delegation at the issuer's
|
|
40
|
-
* advertised endpoint for the Domain's credential to the source Kernel.
|
|
41
|
+
* advertised endpoint for the Domain's credential to the source Kernel, or find the source exhausted.
|
|
41
42
|
*/
|
|
42
|
-
export declare function exchangeDomainCredential(dependencies: ExchangeDependencies, source: EffectiveAuth, request: DomainExchangeRequest, operation: SessionOperation): Promise<DomainCredential>;
|
|
43
|
+
export declare function exchangeDomainCredential(dependencies: ExchangeDependencies, source: EffectiveAuth, request: DomainExchangeRequest, operation: SessionOperation): Promise<DomainCredential | Exhausted>;
|
|
43
44
|
/**
|
|
44
45
|
* One Domain credential for a named Domain issuer, as `session.exchange` returns it. When the
|
|
45
46
|
* request asks for more than the Session delegation, the credential covers that delegation, and the
|
|
@@ -3,10 +3,10 @@ import { identityApi } from '../../auth/identity.js';
|
|
|
3
3
|
import { delegationCoverageMs } from '../credential/index.js';
|
|
4
4
|
import { resolveAuth } from '../routing/index.js';
|
|
5
5
|
import { redeem, tokenEndpoint } from './issuer.js';
|
|
6
|
-
import { delegationTtl,
|
|
6
|
+
import { delegationTtl, EXHAUSTED, renewing, sourceCaller, sourceExhausted } from './source.js';
|
|
7
7
|
/**
|
|
8
8
|
* Delegate the source caller to one Domain issuer and redeem that delegation at the issuer's
|
|
9
|
-
* advertised endpoint for the Domain's credential to the source Kernel.
|
|
9
|
+
* advertised endpoint for the Domain's credential to the source Kernel, or find the source exhausted.
|
|
10
10
|
*/
|
|
11
11
|
export async function exchangeDomainCredential(dependencies, source, request, operation) {
|
|
12
12
|
const caller = sourceCaller(dependencies, source, operation);
|
|
@@ -16,7 +16,9 @@ export async function exchangeDomainCredential(dependencies, source, request, op
|
|
|
16
16
|
.whoami()
|
|
17
17
|
.then(({ id }) => id),
|
|
18
18
|
]);
|
|
19
|
-
const ttlSeconds = delegationTtl(source
|
|
19
|
+
const ttlSeconds = delegationTtl(source, request.ttlSeconds, request.coverageMs);
|
|
20
|
+
if (ttlSeconds === EXHAUSTED)
|
|
21
|
+
return EXHAUSTED;
|
|
20
22
|
const delegation = await credentialApi(caller.call).delegate(principal, {
|
|
21
23
|
audience: request.issuer,
|
|
22
24
|
ttlSeconds,
|
|
@@ -24,9 +26,8 @@ export async function exchangeDomainCredential(dependencies, source, request, op
|
|
|
24
26
|
const credential = await redeem(dependencies, request.issuer, endpoint, delegation, operation);
|
|
25
27
|
// A Domain credential never outlives its source, so only a fresher source can lengthen it.
|
|
26
28
|
const bySource = ttlSeconds < request.ttlSeconds;
|
|
27
|
-
if (bySource && credential.expiresAt - Date.now() <= (request.coverageMs ?? 0))
|
|
28
|
-
|
|
29
|
-
}
|
|
29
|
+
if (bySource && credential.expiresAt - Date.now() <= (request.coverageMs ?? 0))
|
|
30
|
+
return EXHAUSTED;
|
|
30
31
|
return Object.freeze({ ...credential, delegation });
|
|
31
32
|
}
|
|
32
33
|
/**
|
|
@@ -47,8 +48,10 @@ export async function acquireDomainCredential(dependencies, request, operation)
|
|
|
47
48
|
...(request.ttlSeconds * 1_000 > coverageMs ? { coverageMs } : {}),
|
|
48
49
|
}, operation);
|
|
49
50
|
const source = await resolveAuth(auth, call, operation);
|
|
50
|
-
const exchanged = auth === undefined ||
|
|
51
|
+
const exchanged = auth === undefined || source.kind === 'anonymous'
|
|
51
52
|
? await exchange(source)
|
|
52
53
|
: await renewing(auth, call, source, operation, exchange);
|
|
54
|
+
if (exchanged === EXHAUSTED)
|
|
55
|
+
throw sourceExhausted();
|
|
53
56
|
return Object.freeze({ credential: exchanged.credential, expiresAt: exchanged.expiresAt });
|
|
54
57
|
}
|
|
@@ -2,4 +2,4 @@ export { callableOrigin, installedIssuer } from './domain.js';
|
|
|
2
2
|
export { acquireDomainCredential, exchangeDomainCredential, type DomainCredential, type DomainExchangeRequest, type ExchangeDependencies, } from './exchange.js';
|
|
3
3
|
export { admitExchangeOptions, admitExchangeRequest, type ExchangeRequest, type SessionExchangeOptions, type SessionExchangeRequest, } from './options.js';
|
|
4
4
|
export { createSessionExchange, type SessionExchange } from './presentation.js';
|
|
5
|
-
export {
|
|
5
|
+
export { EXHAUSTED, sourceExhausted, type Exhausted } from './source.js';
|
|
@@ -2,4 +2,4 @@ export { callableOrigin, installedIssuer } from './domain.js';
|
|
|
2
2
|
export { acquireDomainCredential, exchangeDomainCredential, } from './exchange.js';
|
|
3
3
|
export { admitExchangeOptions, admitExchangeRequest, } from './options.js';
|
|
4
4
|
export { createSessionExchange } from './presentation.js';
|
|
5
|
-
export {
|
|
5
|
+
export { EXHAUSTED, sourceExhausted } from './source.js';
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { ClientError } from '../../errors/index.js';
|
|
1
|
+
import { ClientError, ExchangeError } from '../../errors/index.js';
|
|
2
2
|
import { runSessionOperation } from '../context/index.js';
|
|
3
3
|
import { credentials, delegationCoverageMs } from '../credential/index.js';
|
|
4
4
|
import { createInstallations, declaringOrigin, isKernelOrigin } from './domain.js';
|
|
5
5
|
import { exchangeDomainCredential } from './exchange.js';
|
|
6
6
|
import { MAXIMUM_EXCHANGE_SOURCES } from './limits.js';
|
|
7
|
-
import {
|
|
7
|
+
import { EXHAUSTED, renewing, sourceExhausted } from './source.js';
|
|
8
8
|
/**
|
|
9
9
|
* Present each Call whose callable a remote installed Domain declares with the credential that
|
|
10
10
|
* Domain exchanged for the Call's source caller.
|
|
@@ -20,7 +20,7 @@ export function createSessionExchange(dependencies, options) {
|
|
|
20
20
|
const sources = new Map();
|
|
21
21
|
return Object.freeze({ resolve, clear });
|
|
22
22
|
async function resolve(call, effective, operation) {
|
|
23
|
-
if (
|
|
23
|
+
if (effective.kind === 'anonymous')
|
|
24
24
|
return effective;
|
|
25
25
|
const origin = declaringOrigin(call.target);
|
|
26
26
|
if (origin === undefined || isKernelOrigin(origin))
|
|
@@ -33,8 +33,21 @@ export function createSessionExchange(dependencies, options) {
|
|
|
33
33
|
throw new ClientError('Session delegation outlives the Domain credential it would carry.');
|
|
34
34
|
}
|
|
35
35
|
const domainCredentials = credentialsFor(source, domain);
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
let exchanged;
|
|
37
|
+
try {
|
|
38
|
+
exchanged = await runSessionOperation(operation, (signal) => domainCredentials.acquire(signal));
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// Credentials settles a mint by value or rejection, so exhaustion crosses it as the error.
|
|
42
|
+
if (error instanceof ExchangeError && error.failure === 'source-exhausted')
|
|
43
|
+
return EXHAUSTED;
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
return Object.freeze({
|
|
47
|
+
kind: 'authenticated',
|
|
48
|
+
credential: exchanged.credential,
|
|
49
|
+
delegate: source.delegate,
|
|
50
|
+
});
|
|
38
51
|
});
|
|
39
52
|
}
|
|
40
53
|
function clear() {
|
|
@@ -48,11 +61,16 @@ export function createSessionExchange(dependencies, options) {
|
|
|
48
61
|
return known;
|
|
49
62
|
const domainCredentials = credentials({
|
|
50
63
|
ttlSeconds: auth.ttlSeconds,
|
|
51
|
-
mint: (signal) =>
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
mint: async (signal) => {
|
|
65
|
+
const exchanged = await exchangeDomainCredential(dependencies, source, {
|
|
66
|
+
issuer: domain,
|
|
67
|
+
ttlSeconds: options.ttlSeconds,
|
|
68
|
+
coverageMs,
|
|
69
|
+
}, dependencies.operation(signal));
|
|
70
|
+
if (exchanged === EXHAUSTED)
|
|
71
|
+
throw sourceExhausted();
|
|
72
|
+
return exchanged;
|
|
73
|
+
},
|
|
56
74
|
});
|
|
57
75
|
held.set(domain, domainCredentials);
|
|
58
76
|
return domainCredentials;
|
|
@@ -2,19 +2,19 @@ import type { invocation } from '@astrale-os/kernel-protocol';
|
|
|
2
2
|
import type { AuthCall } from '../../auth/auth.js';
|
|
3
3
|
import type { Call, Result } from '../../client/index.js';
|
|
4
4
|
import type { SessionOperation } from '../context/index.js';
|
|
5
|
-
import type { EffectiveAuth, SessionAuth } from '../routing/index.js';
|
|
5
|
+
import type { Authenticated, EffectiveAuth, SessionAuth } from '../routing/index.js';
|
|
6
6
|
import type { ExchangeDependencies } from './exchange.js';
|
|
7
7
|
import { ExchangeError } from '../../errors/index.js';
|
|
8
|
-
/** A resolved source that carries both a credential and its delegation. */
|
|
9
|
-
export type SourceAuthority = Required<EffectiveAuth>;
|
|
10
8
|
/** Source Kernel syscalls issued as exactly one admitted source caller within one operation. */
|
|
11
9
|
interface SourceCaller {
|
|
12
10
|
dispatch(call: Call): Promise<Result>;
|
|
13
11
|
readonly call: AuthCall;
|
|
14
12
|
}
|
|
15
13
|
/** The source credential can no longer grant a delegation that covers what its exchange carries. */
|
|
14
|
+
export declare const EXHAUSTED: unique symbol;
|
|
15
|
+
export type Exhausted = typeof EXHAUSTED;
|
|
16
|
+
/** The public failure of an exhausted source, once no renewal can replace it. */
|
|
16
17
|
export declare function sourceExhausted(): ExchangeError;
|
|
17
|
-
export declare function isAuthority(auth: EffectiveAuth): auth is SourceAuthority;
|
|
18
18
|
/** The source Kernel conversation of one exchange, acting as exactly the admitted source caller. */
|
|
19
19
|
export declare function sourceCaller(dependencies: Pick<ExchangeDependencies, 'source'>, auth: EffectiveAuth, operation: SessionOperation): SourceCaller;
|
|
20
20
|
/**
|
|
@@ -23,11 +23,11 @@ export declare function sourceCaller(dependencies: Pick<ExchangeDependencies, 's
|
|
|
23
23
|
* transport-held source leaves the bound to the Kernel. A source that cannot grant one second, or
|
|
24
24
|
* bounds the delegation within `coverageMs`, is exhausted.
|
|
25
25
|
*/
|
|
26
|
-
export declare function delegationTtl(
|
|
26
|
+
export declare function delegationTtl(source: EffectiveAuth, ttlSeconds: number, coverageMs?: number): number | Exhausted;
|
|
27
27
|
/**
|
|
28
28
|
* Run `attempt` with the resolved source, and once more with a renewed source if that one proved
|
|
29
29
|
* exhausted. The Session auth is refreshed only while it still resolves the exhausted credential, so
|
|
30
|
-
* concurrent Calls share one renewal; an auth that cannot refresh
|
|
30
|
+
* concurrent Calls share one renewal; an auth that cannot refresh fails as `sourceExhausted`.
|
|
31
31
|
*/
|
|
32
|
-
export declare function renewing<T>(auth: SessionAuth, call: invocation.Call, source:
|
|
32
|
+
export declare function renewing<T>(auth: SessionAuth, call: invocation.Call, source: Authenticated, operation: SessionOperation, attempt: (source: Authenticated) => Promise<T | Exhausted>): Promise<T>;
|
|
33
33
|
export {};
|
|
@@ -4,21 +4,20 @@ import { runSessionOperation, sessionTransportOptions } from '../context/index.j
|
|
|
4
4
|
import { requireResultKind, resolveAuth } from '../routing/index.js';
|
|
5
5
|
import { ISSUANCE_MARGIN_SECONDS } from './limits.js';
|
|
6
6
|
/** The source credential can no longer grant a delegation that covers what its exchange carries. */
|
|
7
|
+
export const EXHAUSTED = Symbol('exhausted');
|
|
8
|
+
/** The public failure of an exhausted source, once no renewal can replace it. */
|
|
7
9
|
export function sourceExhausted() {
|
|
8
10
|
return new ExchangeError('The source credential expires too soon for a Domain token exchange.', {
|
|
9
11
|
failure: 'source-exhausted',
|
|
10
12
|
});
|
|
11
13
|
}
|
|
12
|
-
export function isAuthority(auth) {
|
|
13
|
-
return auth.credential !== undefined && auth.delegate !== undefined;
|
|
14
|
-
}
|
|
15
14
|
/** The source Kernel conversation of one exchange, acting as exactly the admitted source caller. */
|
|
16
15
|
export function sourceCaller(dependencies, auth, operation) {
|
|
17
16
|
const client = dependencies.source();
|
|
18
|
-
const caller = auth.
|
|
17
|
+
const caller = auth.kind === 'anonymous' ? client : client.as(auth.credential);
|
|
19
18
|
const dispatch = (input) => runSessionOperation(operation, () => caller.dispatch(input, {
|
|
20
19
|
...sessionTransportOptions(operation),
|
|
21
|
-
...(auth.
|
|
20
|
+
...(auth.kind === 'anonymous' ? {} : { delegate: auth.delegate }),
|
|
22
21
|
}));
|
|
23
22
|
const call = async (target, input) => requireResultKind('value', await dispatch({ target, input })).value;
|
|
24
23
|
return Object.freeze({ dispatch, call });
|
|
@@ -29,42 +28,42 @@ export function sourceCaller(dependencies, auth, operation) {
|
|
|
29
28
|
* transport-held source leaves the bound to the Kernel. A source that cannot grant one second, or
|
|
30
29
|
* bounds the delegation within `coverageMs`, is exhausted.
|
|
31
30
|
*/
|
|
32
|
-
export function delegationTtl(
|
|
33
|
-
const remaining = remainingSeconds(credential);
|
|
31
|
+
export function delegationTtl(source, ttlSeconds, coverageMs = 0) {
|
|
32
|
+
const remaining = source.kind === 'anonymous' ? undefined : remainingSeconds(source.credential);
|
|
34
33
|
if (remaining === undefined || remaining >= ttlSeconds)
|
|
35
34
|
return ttlSeconds;
|
|
36
35
|
if (remaining < 1 || remaining * 1_000 <= coverageMs)
|
|
37
|
-
|
|
36
|
+
return EXHAUSTED;
|
|
38
37
|
return remaining;
|
|
39
38
|
}
|
|
40
39
|
/**
|
|
41
40
|
* Run `attempt` with the resolved source, and once more with a renewed source if that one proved
|
|
42
41
|
* exhausted. The Session auth is refreshed only while it still resolves the exhausted credential, so
|
|
43
|
-
* concurrent Calls share one renewal; an auth that cannot refresh
|
|
42
|
+
* concurrent Calls share one renewal; an auth that cannot refresh fails as `sourceExhausted`.
|
|
44
43
|
*/
|
|
45
44
|
export async function renewing(auth, call, source, operation, attempt) {
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
const first = await attempt(source);
|
|
46
|
+
if (first !== EXHAUSTED)
|
|
47
|
+
return first;
|
|
48
|
+
let renewed = await resolveAuth(auth, call, operation);
|
|
49
|
+
const refresh = auth.refresh;
|
|
50
|
+
if (renewed.kind === 'authenticated' &&
|
|
51
|
+
renewed.credential === source.credential &&
|
|
52
|
+
refresh !== undefined) {
|
|
53
|
+
await runSessionOperation(operation, async (signal) => {
|
|
54
|
+
await refresh(signal);
|
|
55
|
+
});
|
|
56
|
+
renewed = await resolveAuth(auth, call, operation);
|
|
48
57
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
throw cause;
|
|
52
|
-
let renewed = await resolveAuth(auth, call, operation);
|
|
53
|
-
if (renewed.credential === source.credential && auth.refresh !== undefined) {
|
|
54
|
-
const refresh = auth.refresh;
|
|
55
|
-
await runSessionOperation(operation, async (signal) => {
|
|
56
|
-
await refresh(signal);
|
|
57
|
-
});
|
|
58
|
-
renewed = await resolveAuth(auth, call, operation);
|
|
59
|
-
}
|
|
60
|
-
if (!isAuthority(renewed) || renewed.credential === source.credential)
|
|
61
|
-
throw cause;
|
|
62
|
-
return attempt(renewed);
|
|
58
|
+
if (renewed.kind === 'anonymous' || renewed.credential === source.credential) {
|
|
59
|
+
throw sourceExhausted();
|
|
63
60
|
}
|
|
61
|
+
const second = await attempt(renewed);
|
|
62
|
+
if (second === EXHAUSTED)
|
|
63
|
+
throw sourceExhausted();
|
|
64
|
+
return second;
|
|
64
65
|
}
|
|
65
66
|
function remainingSeconds(credential) {
|
|
66
|
-
if (credential === undefined)
|
|
67
|
-
return undefined;
|
|
68
67
|
let expiresAt;
|
|
69
68
|
try {
|
|
70
69
|
expiresAt = inspect(credential).claims.exp;
|
|
@@ -16,10 +16,16 @@ export interface SessionAuth {
|
|
|
16
16
|
*/
|
|
17
17
|
refresh?(signal: AbortSignal): PromiseLike<unknown> | void;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
readonly
|
|
19
|
+
/** A credential with the delegation it carries. */
|
|
20
|
+
export interface Authenticated {
|
|
21
|
+
readonly kind: 'authenticated';
|
|
22
|
+
readonly credential: string;
|
|
23
|
+
readonly delegate: invocation.Delegate;
|
|
22
24
|
}
|
|
25
|
+
/** The authority one Call presents: none, or a credential with its delegation. */
|
|
26
|
+
export type EffectiveAuth = {
|
|
27
|
+
readonly kind: 'anonymous';
|
|
28
|
+
} | Authenticated;
|
|
23
29
|
/** Replace the resolved source authority of one Call before routing partitions and presents it. */
|
|
24
30
|
export interface AuthExchange {
|
|
25
31
|
resolve(call: invocation.Call, auth: EffectiveAuth, operation: SessionOperation): Promise<EffectiveAuth>;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import { ClientError } from '../../errors/index.js';
|
|
3
3
|
import { runSessionOperation } from '../context/index.js';
|
|
4
|
+
const ANONYMOUS = Object.freeze({ kind: 'anonymous' });
|
|
4
5
|
export function admitSessionAuth(input) {
|
|
5
6
|
if (input === undefined)
|
|
6
7
|
return undefined;
|
|
@@ -21,7 +22,7 @@ export function admitSessionAuth(input) {
|
|
|
21
22
|
}
|
|
22
23
|
export async function resolveAuth(resolver, call, operation) {
|
|
23
24
|
if (resolver === undefined)
|
|
24
|
-
return
|
|
25
|
+
return ANONYMOUS;
|
|
25
26
|
const input = await runSessionOperation(operation, (signal) => resolver.resolve(call, signal));
|
|
26
27
|
if (input === null || typeof input !== 'object' || Array.isArray(input)) {
|
|
27
28
|
throw new ClientError('Session auth resolver returned an invalid value.');
|
|
@@ -38,9 +39,9 @@ export async function resolveAuth(resolver, call, operation) {
|
|
|
38
39
|
throw new ClientError('Session delegation overrides require a source credential.');
|
|
39
40
|
}
|
|
40
41
|
if (credential === undefined)
|
|
41
|
-
return
|
|
42
|
+
return ANONYMOUS;
|
|
42
43
|
const delegate = input.delegate === undefined
|
|
43
44
|
? protocol.acceptDelegate({ ttlSeconds: resolver.ttlSeconds })
|
|
44
45
|
: protocol.acceptDelegate(input.delegate);
|
|
45
|
-
return Object.freeze({ credential, delegate });
|
|
46
|
+
return Object.freeze({ kind: 'authenticated', credential, delegate });
|
|
46
47
|
}
|