@astrale-os/kernel-client 1.0.0-beta.12 → 1.0.0-beta.14
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/errors/refusal.js +5 -1
- 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/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/provenance.d.ts +4 -5
- package/dist/graph/provenance.js +5 -6
- package/dist/graph/query.js +4 -3
- package/dist/session/content/retry.js +2 -17
- package/dist/session/context/index.d.ts +1 -1
- package/dist/session/context/index.js +1 -1
- package/dist/session/context/operation.d.ts +2 -0
- package/dist/session/context/operation.js +19 -0
- package/dist/session/credential/credentials.js +9 -4
- package/dist/session/durable.d.ts +13 -0
- package/dist/session/durable.js +45 -0
- package/dist/session/routing/dispatch.d.ts +20 -2
- package/dist/session/routing/dispatch.js +12 -4
- package/dist/session/session.d.ts +4 -0
- package/dist/session/session.js +12 -4
- package/dist/transport/websocket/exchange.js +5 -1
- package/package.json +5 -5
package/dist/errors/refusal.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import * as errors from '@astrale-os/kernel-protocol/errors';
|
|
2
2
|
import { CALLABLE_FINGERPRINT_MISMATCH } from '@astrale-os/kernel-protocol/invocation';
|
|
3
3
|
import { ResponseError } from './response-error.js';
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Sealed conflicts whose plan met a checkpoint advanced past its pinned snapshot, an Authority
|
|
6
|
+
* evaluation's included (POLICY_SNAPSHOT_MISMATCH).
|
|
7
|
+
*/
|
|
5
8
|
const SNAPSHOT_STALE_REASONS = new Set([
|
|
6
9
|
'SNAPSHOT_STALE',
|
|
7
10
|
'QUERY_SNAPSHOT_STALE',
|
|
8
11
|
'MUTATION_SNAPSHOT_STALE',
|
|
12
|
+
'POLICY_SNAPSHOT_MISMATCH',
|
|
9
13
|
]);
|
|
10
14
|
/** Conflicts whose request expects a Domain meaning this Instance does not serve. */
|
|
11
15
|
const EXPECTATION_MISMATCH_REASONS = new Set([
|
|
@@ -1,31 +1,29 @@
|
|
|
1
|
-
import { ClassKey } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
2
1
|
import { isDomain } from '@astrale-os/kernel-dsl/v1/domain';
|
|
3
2
|
import { provenanceOfGraphValue, provenanceOfResponse } from '../provenance.js';
|
|
3
|
+
import { definedClass, validateDefinedProperties } from './conformance.js';
|
|
4
4
|
import { selectedEdges, selectedNodes } from './page.js';
|
|
5
5
|
import { projectEdge, projectNode } from './project.js';
|
|
6
|
-
import { provenanceOfProjectedValue
|
|
6
|
+
import { provenanceOfProjectedValue } from './provenance.js';
|
|
7
7
|
export function createDomainBinding(domain, graph, call) {
|
|
8
8
|
if (!isDomain(domain))
|
|
9
9
|
throw new TypeError('Domain binding requires an admitted DSL Domain.');
|
|
10
10
|
const wrapNode = (selected, node) => {
|
|
11
11
|
requireOwnedClass(domain, selected);
|
|
12
|
-
const provenance = requireProvenance(
|
|
12
|
+
const provenance = requireProvenance(node);
|
|
13
13
|
const exact = requireExactClass(domain, node.class, 'node');
|
|
14
14
|
if (!exact.satisfies(selected))
|
|
15
15
|
throw new TypeError(`${exact.key} does not satisfy ${selected.key}.`);
|
|
16
|
-
|
|
17
|
-
if (diagnostics.length > 0)
|
|
16
|
+
if (validateDefinedProperties(exact, node.props).length > 0)
|
|
18
17
|
throw new TypeError(`Node ${node.id} has invalid exact Properties.`);
|
|
19
18
|
return projectNode(selected, node, provenance, call);
|
|
20
19
|
};
|
|
21
20
|
const wrapEdge = (selected, edge) => {
|
|
22
21
|
requireOwnedClass(domain, selected);
|
|
23
|
-
const provenance = requireProvenance(
|
|
22
|
+
const provenance = requireProvenance(edge);
|
|
24
23
|
const exact = requireExactClass(domain, edge.class, 'edge');
|
|
25
24
|
if (!exact.satisfies(selected))
|
|
26
25
|
throw new TypeError(`${exact.key} does not satisfy ${selected.key}.`);
|
|
27
|
-
|
|
28
|
-
if (diagnostics.length > 0)
|
|
26
|
+
if (validateDefinedProperties(exact, edge.props).length > 0)
|
|
29
27
|
throw new TypeError('Edge has invalid exact Properties.');
|
|
30
28
|
return projectEdge(selected, edge, provenance);
|
|
31
29
|
};
|
|
@@ -34,13 +32,14 @@ export function createDomainBinding(domain, graph, call) {
|
|
|
34
32
|
graph,
|
|
35
33
|
async query(query, options) {
|
|
36
34
|
const selected = selectedSourceClass(domain, query);
|
|
37
|
-
|
|
35
|
+
// The Kernel answers in its active Schema; projection checks each value against `domain`.
|
|
36
|
+
const response = await graph.query(query, options);
|
|
38
37
|
// A repair-session read may hold facts the bound Domain refutes: it is never typed by it.
|
|
39
38
|
if (response.certification === 'uncertified') {
|
|
40
39
|
throw new TypeError('An uncertified Query response cannot be bound to its Domain.');
|
|
41
40
|
}
|
|
42
41
|
const provenance = provenanceOfResponse(response);
|
|
43
|
-
if (provenance === undefined
|
|
42
|
+
if (provenance === undefined) {
|
|
44
43
|
throw new TypeError('Authenticated query provenance was not retained.');
|
|
45
44
|
}
|
|
46
45
|
const next = response.page.next === undefined
|
|
@@ -50,7 +49,9 @@ export function createDomainBinding(domain, graph, call) {
|
|
|
50
49
|
page: { ...options.page, after: response.page.next },
|
|
51
50
|
});
|
|
52
51
|
if (selected.kind === 'node') {
|
|
53
|
-
const nodes = selectedNodes(response)
|
|
52
|
+
const nodes = selectedNodes(response)
|
|
53
|
+
.filter((node) => definedClass(domain, node.class) !== undefined)
|
|
54
|
+
.map((node) => wrapNode(selected, node));
|
|
54
55
|
return Object.freeze({
|
|
55
56
|
nodes: Object.freeze(nodes),
|
|
56
57
|
raw: response,
|
|
@@ -58,7 +59,9 @@ export function createDomainBinding(domain, graph, call) {
|
|
|
58
59
|
refine: (value, target) => refineNode(domain, call, value, target, provenance),
|
|
59
60
|
});
|
|
60
61
|
}
|
|
61
|
-
const edges = selectedEdges(response)
|
|
62
|
+
const edges = selectedEdges(response)
|
|
63
|
+
.filter((edge) => definedClass(domain, edge.class) !== undefined)
|
|
64
|
+
.map((edge) => wrapEdge(selected, edge));
|
|
62
65
|
return Object.freeze({
|
|
63
66
|
edges: Object.freeze(edges),
|
|
64
67
|
raw: response,
|
|
@@ -111,16 +114,16 @@ function requireOwnedClass(domain, selected) {
|
|
|
111
114
|
throw new TypeError('Selected Class does not belong to the bound Domain closure.');
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
|
-
function requireProvenance(
|
|
117
|
+
function requireProvenance(value) {
|
|
115
118
|
const provenance = provenanceOfGraphValue(value);
|
|
116
|
-
if (provenance === undefined
|
|
117
|
-
throw new TypeError('Graph value lacks
|
|
119
|
+
if (provenance === undefined) {
|
|
120
|
+
throw new TypeError('Graph value lacks authenticated query provenance.');
|
|
118
121
|
}
|
|
119
122
|
return provenance;
|
|
120
123
|
}
|
|
121
124
|
function requireExactClass(domain, key, kind) {
|
|
122
|
-
const exact = domain
|
|
123
|
-
if (exact?.
|
|
125
|
+
const exact = definedClass(domain, key);
|
|
126
|
+
if (exact?.kind !== kind || exact.abstract) {
|
|
124
127
|
throw new TypeError(`Exact ${kind} Class ${key} is not active and concrete.`);
|
|
125
128
|
}
|
|
126
129
|
return exact;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Domain } from '@astrale-os/kernel-dsl/v1/domain';
|
|
2
|
+
import type { AnyResolvedClass, Diagnostic } from '@astrale-os/kernel-dsl/v1/language';
|
|
3
|
+
/**
|
|
4
|
+
* The Class `domain` defines under `key`, or `undefined` when that Class was added after `domain`
|
|
5
|
+
* was built. The Kernel answers in its active Schema, so a result may hold values of such a Class.
|
|
6
|
+
*/
|
|
7
|
+
export declare function definedClass(domain: Domain, key: string): AnyResolvedClass | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* What in `props` breaks `exact`: a Property it defines holding an invalid value, or a required one
|
|
10
|
+
* absent. A Property `exact` does not define passes: the active Schema may add it.
|
|
11
|
+
*/
|
|
12
|
+
export declare function validateDefinedProperties(exact: AnyResolvedClass, props: unknown): readonly Diagnostic[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ClassKey } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
2
|
+
/**
|
|
3
|
+
* The Class `domain` defines under `key`, or `undefined` when that Class was added after `domain`
|
|
4
|
+
* was built. The Kernel answers in its active Schema, so a result may hold values of such a Class.
|
|
5
|
+
*/
|
|
6
|
+
export function definedClass(domain, key) {
|
|
7
|
+
const definition = domain.definition(ClassKey.ref(ClassKey(key)));
|
|
8
|
+
return definition?.ref.kind === 'class' ? definition : undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* What in `props` breaks `exact`: a Property it defines holding an invalid value, or a required one
|
|
12
|
+
* absent. A Property `exact` does not define passes: the active Schema may add it.
|
|
13
|
+
*/
|
|
14
|
+
export function validateDefinedProperties(exact, props) {
|
|
15
|
+
return exact
|
|
16
|
+
.validateProperties(props)
|
|
17
|
+
.filter((diagnostic) => diagnostic.code !== 'DOMAIN_PROPERTY_UNKNOWN');
|
|
18
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export type { DomainBinding, DomainQueryOptions } from './binding.js';
|
|
2
|
+
export { definedClass, validateDefinedProperties } from './conformance.js';
|
|
2
3
|
export type { BoundEdge, BoundGraphValue, BoundNode, BoundNodeFor } from './model.js';
|
|
3
4
|
export type { BoundEdgePage, BoundNodePage, BoundQueryPage } from './page.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export { definedClass, validateDefinedProperties } from './conformance.js';
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { Domain } from '@astrale-os/kernel-dsl/v1/domain';
|
|
2
1
|
import type { QueryProvenance } from '../provenance.js';
|
|
3
2
|
export declare function retainProjectedProvenance(value: object, provenance: QueryProvenance): void;
|
|
4
3
|
export declare function provenanceOfProjectedValue(value: object): QueryProvenance | undefined;
|
|
5
|
-
export declare function sameClosure(left: Domain['closure'], right: Domain['closure']): boolean;
|
|
6
4
|
export type { QueryProvenance };
|
|
@@ -5,7 +5,3 @@ export function retainProjectedProvenance(value, provenance) {
|
|
|
5
5
|
export function provenanceOfProjectedValue(value) {
|
|
6
6
|
return projectedValues.get(value);
|
|
7
7
|
}
|
|
8
|
-
export function sameClosure(left, right) {
|
|
9
|
-
return (left.length === right.length &&
|
|
10
|
-
left.every((identity, index) => identity.origin === right[index]?.origin && identity.revision === right[index]?.revision));
|
|
11
|
-
}
|
package/dist/graph/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { createGraph, type GraphApi, type GraphCall, type GraphMutationOptions, type GraphQueryOptions, type MutationAuthor, } from './graph.js';
|
|
2
2
|
export type { QueryPageRequest, QueryResponse } from './page-contract.js';
|
|
3
|
-
export { type BoundEdge, type BoundEdgePage, type BoundGraphValue, type BoundNode, type BoundNodeFor, type BoundNodePage, type BoundQueryPage, type DomainBinding, type DomainQueryOptions, } from './domain/index.js';
|
|
3
|
+
export { type BoundEdge, type BoundEdgePage, type BoundGraphValue, type BoundNode, type BoundNodeFor, type BoundNodePage, type BoundQueryPage, type DomainBinding, type DomainQueryOptions, definedClass, validateDefinedProperties, } from './domain/index.js';
|
|
4
4
|
export { NodeUnavailableError } from './errors.js';
|
|
5
5
|
export type { NeighborsOptions, NodePage } from './page/index.js';
|
|
6
6
|
export { LocalAlias, type GraphSpec } from '@astrale-os/kernel-core/graph/graph';
|
package/dist/graph/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createGraph, } from './graph.js';
|
|
2
|
-
export {} from './domain/index.js';
|
|
2
|
+
export { definedClass, validateDefinedProperties, } from './domain/index.js';
|
|
3
3
|
export { NodeUnavailableError } from './errors.js';
|
|
4
4
|
export { LocalAlias } from '@astrale-os/kernel-core/graph/graph';
|
|
5
5
|
export { MutationAST, MutationResult, } from '@astrale-os/kernel-core/graph/mutate';
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type { Edge } from '@astrale-os/kernel-core/graph/edge';
|
|
2
2
|
import type { Node } from '@astrale-os/kernel-core/graph/node';
|
|
3
|
-
import type { DomainDependency } from '@astrale-os/kernel-dsl/v1/schema';
|
|
4
3
|
import type { QueryResponse } from './page-contract.js';
|
|
4
|
+
/** A certified Query response this client received from the Kernel; `page` identifies it. */
|
|
5
5
|
export interface QueryProvenance {
|
|
6
|
-
readonly closure: readonly DomainDependency[];
|
|
7
6
|
readonly page: object;
|
|
8
7
|
}
|
|
9
|
-
/** Retain
|
|
10
|
-
export declare function retainQueryProvenance(response: QueryResponse
|
|
11
|
-
/** Reattach
|
|
8
|
+
/** Retain the provenance of a certified Query response without adding serialized metadata. */
|
|
9
|
+
export declare function retainQueryProvenance(response: QueryResponse): QueryResponse;
|
|
10
|
+
/** Reattach provenance when Store materialization replaces response objects. */
|
|
12
11
|
export declare function retainDerivedQueryProvenance(source: QueryResponse, derived: QueryResponse): QueryResponse;
|
|
13
12
|
export declare function provenanceOfGraphValue(value: Node | Edge): QueryProvenance | undefined;
|
|
14
13
|
export declare function provenanceOfResponse(value: QueryResponse): QueryProvenance | undefined;
|
package/dist/graph/provenance.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const graphValues = new WeakMap();
|
|
2
2
|
const responses = new WeakMap();
|
|
3
|
-
/** Retain
|
|
4
|
-
export function retainQueryProvenance(response
|
|
5
|
-
const provenance = Object.freeze({
|
|
3
|
+
/** Retain the provenance of a certified Query response without adding serialized metadata. */
|
|
4
|
+
export function retainQueryProvenance(response) {
|
|
5
|
+
const provenance = Object.freeze({ page: Object.freeze({}) });
|
|
6
6
|
responses.set(response, provenance);
|
|
7
7
|
const { result } = response;
|
|
8
8
|
if (result.kind === 'graph') {
|
|
@@ -29,10 +29,9 @@ export function retainQueryProvenance(response, closure) {
|
|
|
29
29
|
}
|
|
30
30
|
return response;
|
|
31
31
|
}
|
|
32
|
-
/** Reattach
|
|
32
|
+
/** Reattach provenance when Store materialization replaces response objects. */
|
|
33
33
|
export function retainDerivedQueryProvenance(source, derived) {
|
|
34
|
-
|
|
35
|
-
return provenance === undefined ? derived : retainQueryProvenance(derived, provenance.closure);
|
|
34
|
+
return responses.has(source) ? retainQueryProvenance(derived) : derived;
|
|
36
35
|
}
|
|
37
36
|
export function provenanceOfGraphValue(value) {
|
|
38
37
|
return graphValues.get(value);
|
package/dist/graph/query.js
CHANGED
|
@@ -28,9 +28,10 @@ export async function queryGraph(call, input, options) {
|
|
|
28
28
|
page: admittedPage,
|
|
29
29
|
})), request);
|
|
30
30
|
const response = decodeResponse(output);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
// A repair-session read may hold facts the Domain refutes: it carries no provenance.
|
|
32
|
+
return response.certification === undefined
|
|
33
|
+
? retainQueryProvenance(response)
|
|
34
|
+
: response;
|
|
34
35
|
}
|
|
35
36
|
function admitPage(input) {
|
|
36
37
|
if (input === null || typeof input !== 'object' || Array.isArray(input)) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CODES } from '@astrale-os/kernel-protocol/errors';
|
|
2
2
|
import { ResponseError, TransportError } from '../../errors/index.js';
|
|
3
|
-
import {
|
|
3
|
+
import { pauseSessionOperation } from '../context/index.js';
|
|
4
4
|
const MAXIMUM_ATTEMPTS = 3;
|
|
5
5
|
/** Repeat one content request that failed before any of its result was delivered. */
|
|
6
6
|
export async function retry(operation, request) {
|
|
@@ -12,7 +12,7 @@ export async function retry(operation, request) {
|
|
|
12
12
|
if (!retryable(cause) || attempt + 1 === MAXIMUM_ATTEMPTS || operation.signal.aborted) {
|
|
13
13
|
throw cause;
|
|
14
14
|
}
|
|
15
|
-
await
|
|
15
|
+
await pauseSessionOperation(operation, 200 * 2 ** attempt);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
}
|
|
@@ -24,18 +24,3 @@ function retryable(cause) {
|
|
|
24
24
|
cause.code === CODES.DEADLINE_EXCEEDED ||
|
|
25
25
|
cause.code === CODES.OUTCOME_UNKNOWN)));
|
|
26
26
|
}
|
|
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';
|
|
3
|
+
export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, type SessionOperation, } from './operation.js';
|
|
4
4
|
export { admitKernel, admitPolicy, allowEndpoint, 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';
|
|
3
|
+
export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, } from './operation.js';
|
|
4
4
|
export { admitKernel, admitPolicy, allowEndpoint, 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>;
|
|
@@ -54,6 +54,25 @@ export async function runSessionOperation(operation, task) {
|
|
|
54
54
|
operation.signal.removeEventListener('abort', abort);
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
|
+
/** Wait `ms` before another attempt, ending early with the operation's interruption. */
|
|
58
|
+
export function pauseSessionOperation(operation, ms) {
|
|
59
|
+
return runSessionOperation(operation, (signal) => delay(ms, signal));
|
|
60
|
+
}
|
|
61
|
+
function delay(ms, signal) {
|
|
62
|
+
return new Promise((resolve, reject) => {
|
|
63
|
+
if (signal.aborted)
|
|
64
|
+
return reject(signal.reason);
|
|
65
|
+
const timer = setTimeout(() => {
|
|
66
|
+
signal.removeEventListener('abort', abort);
|
|
67
|
+
resolve();
|
|
68
|
+
}, ms);
|
|
69
|
+
const abort = () => {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
reject(signal.reason);
|
|
72
|
+
};
|
|
73
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
74
|
+
});
|
|
75
|
+
}
|
|
57
76
|
function requireSessionOperation(operation) {
|
|
58
77
|
if (!operation.signal.aborted)
|
|
59
78
|
return;
|
|
@@ -38,6 +38,8 @@ export function credentials(source) {
|
|
|
38
38
|
let inflight;
|
|
39
39
|
let controller;
|
|
40
40
|
let waiting = 0;
|
|
41
|
+
/** A caller without a signal keeps the mint in flight alive; anticipation does not. */
|
|
42
|
+
let unsignalled = false;
|
|
41
43
|
let anticipating = false;
|
|
42
44
|
let refreshTimer;
|
|
43
45
|
let expiryTimer;
|
|
@@ -76,8 +78,10 @@ export function credentials(source) {
|
|
|
76
78
|
if (signal?.aborted === true)
|
|
77
79
|
return Promise.reject(signal.reason);
|
|
78
80
|
const pending = inflight ?? begin();
|
|
79
|
-
if (signal === undefined)
|
|
81
|
+
if (signal === undefined) {
|
|
82
|
+
unsignalled = true;
|
|
80
83
|
return pending;
|
|
84
|
+
}
|
|
81
85
|
waiting += 1;
|
|
82
86
|
let settled = false;
|
|
83
87
|
return new Promise((complete, fail) => {
|
|
@@ -88,7 +92,7 @@ export function credentials(source) {
|
|
|
88
92
|
waiting -= 1;
|
|
89
93
|
signal.removeEventListener('abort', cancel);
|
|
90
94
|
// Nobody is waiting and nobody is anticipating: the mint has no beneficiary left.
|
|
91
|
-
if (waiting === 0 && !anticipating)
|
|
95
|
+
if (waiting === 0 && !anticipating && !unsignalled)
|
|
92
96
|
controller?.abort(signal.reason);
|
|
93
97
|
};
|
|
94
98
|
const cancel = () => {
|
|
@@ -124,6 +128,7 @@ export function credentials(source) {
|
|
|
124
128
|
publish({ status: 'refreshing', ...(current === undefined ? {} : { credential: current }) });
|
|
125
129
|
const owned = new AbortController();
|
|
126
130
|
controller = owned;
|
|
131
|
+
unsignalled = false;
|
|
127
132
|
const signal = AbortSignal.any([owned.signal, AbortSignal.timeout(MINT_TIMEOUT_MS)]);
|
|
128
133
|
const pending = (async () => {
|
|
129
134
|
try {
|
|
@@ -166,7 +171,7 @@ export function credentials(source) {
|
|
|
166
171
|
if (!anticipating)
|
|
167
172
|
return;
|
|
168
173
|
if (current === undefined) {
|
|
169
|
-
void
|
|
174
|
+
void (inflight ?? begin()).catch(() => undefined);
|
|
170
175
|
return;
|
|
171
176
|
}
|
|
172
177
|
// The threshold itself, with no extra lead: chained providers sharing this rule must not
|
|
@@ -189,7 +194,7 @@ export function credentials(source) {
|
|
|
189
194
|
function armRefresh(delay) {
|
|
190
195
|
refreshTimer = setTimeout(() => {
|
|
191
196
|
refreshTimer = undefined;
|
|
192
|
-
void
|
|
197
|
+
void (inflight ?? begin()).catch(() => undefined);
|
|
193
198
|
}, delay);
|
|
194
199
|
}
|
|
195
200
|
function armExpiry(credential) {
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SessionOperation } from './context/index.js';
|
|
2
|
+
import type { SessionRequestOptions } from './routing/index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options of one typed invocation of a durable callable: the caller's key, or one created for this
|
|
5
|
+
* call. The key names the Execution, so every attempt of the call reaches the same one.
|
|
6
|
+
*/
|
|
7
|
+
export declare function durableOptions(options: Readonly<SessionRequestOptions>): Readonly<SessionRequestOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Send a keyed durable invocation again while its outcome is unknown: a transport failure, sent or
|
|
10
|
+
* not, or the Kernel's `OUTCOME_UNKNOWN`. Its key makes the Kernel answer with the Execution a lost
|
|
11
|
+
* attempt recorded, marked `replayed`, instead of starting another.
|
|
12
|
+
*/
|
|
13
|
+
export declare function attemptDurable<Result>(operation: SessionOperation, attempt: () => Promise<Result>): Promise<Result>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { CODES } from '@astrale-os/kernel-protocol/errors';
|
|
2
|
+
import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
|
+
import { ResponseError, TransportError } from '../errors/index.js';
|
|
4
|
+
import { pauseSessionOperation } from './context/index.js';
|
|
5
|
+
/** The first attempt of a durable invocation and at most two more, within its one deadline. */
|
|
6
|
+
const MAXIMUM_ATTEMPTS = 3;
|
|
7
|
+
const FIRST_PAUSE_MS = 200;
|
|
8
|
+
/**
|
|
9
|
+
* Options of one typed invocation of a durable callable: the caller's key, or one created for this
|
|
10
|
+
* call. The key names the Execution, so every attempt of the call reaches the same one.
|
|
11
|
+
*/
|
|
12
|
+
export function durableOptions(options) {
|
|
13
|
+
if (options.idempotencyKey !== undefined)
|
|
14
|
+
return options;
|
|
15
|
+
return Object.freeze({
|
|
16
|
+
...options,
|
|
17
|
+
idempotencyKey: invocationProtocol.acceptIdempotencyKey(crypto.randomUUID()),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Send a keyed durable invocation again while its outcome is unknown: a transport failure, sent or
|
|
22
|
+
* not, or the Kernel's `OUTCOME_UNKNOWN`. Its key makes the Kernel answer with the Execution a lost
|
|
23
|
+
* attempt recorded, marked `replayed`, instead of starting another.
|
|
24
|
+
*/
|
|
25
|
+
export async function attemptDurable(operation, attempt) {
|
|
26
|
+
for (let attempts = 1;; attempts++) {
|
|
27
|
+
try {
|
|
28
|
+
return await attempt();
|
|
29
|
+
}
|
|
30
|
+
catch (cause) {
|
|
31
|
+
const pause = FIRST_PAUSE_MS * 2 ** (attempts - 1);
|
|
32
|
+
if (attempts === MAXIMUM_ATTEMPTS ||
|
|
33
|
+
!unknownOutcome(cause) ||
|
|
34
|
+
operation.signal.aborted ||
|
|
35
|
+
operation.deadline - Date.now() <= pause) {
|
|
36
|
+
throw cause;
|
|
37
|
+
}
|
|
38
|
+
await pauseSessionOperation(operation, pause);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function unknownOutcome(cause) {
|
|
43
|
+
return ((cause instanceof TransportError && cause.context.kind === 'invocation') ||
|
|
44
|
+
(cause instanceof ResponseError && cause.code === CODES.OUTCOME_UNKNOWN));
|
|
45
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { IssuerId } from '@astrale-os/kernel-core/auth/issuer';
|
|
2
|
+
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
3
|
import type { Result } from '../../client/index.js';
|
|
3
4
|
import type { Fetch } from '../../transport/http/index.js';
|
|
4
5
|
import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
5
|
-
import type { AuthExchange, SessionAuth } from './auth.js';
|
|
6
|
+
import type { AuthExchange, SessionAuth, EffectiveAuth } from './auth.js';
|
|
6
7
|
import type { RouteCache } from './cache.js';
|
|
7
8
|
import type { ClientPoolCapability } from './envelope.js';
|
|
8
9
|
import type { SessionEnvelopeTransport, SessionRequestOptions } from './request.js';
|
|
@@ -24,13 +25,30 @@ export interface SessionDispatcherOptions {
|
|
|
24
25
|
readonly timeoutMs: number;
|
|
25
26
|
};
|
|
26
27
|
}
|
|
28
|
+
/** A streaming call is forced over HTTP and may carry a binary body. */
|
|
29
|
+
export interface Streaming {
|
|
30
|
+
readonly body?: Blob;
|
|
31
|
+
}
|
|
32
|
+
/** One Call with the authority it presents on every dispatch, and the route partition it selects. */
|
|
33
|
+
export interface ResolvedCall {
|
|
34
|
+
readonly call: invocation.Call;
|
|
35
|
+
readonly auth: EffectiveAuth;
|
|
36
|
+
readonly source: IssuerId;
|
|
37
|
+
readonly cacheKey: string;
|
|
38
|
+
}
|
|
27
39
|
export declare class SessionDispatcher {
|
|
28
40
|
private readonly state;
|
|
29
41
|
constructor(state: SessionDispatcherOptions);
|
|
30
42
|
dispatch(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation): Promise<Result>;
|
|
31
43
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
32
44
|
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?: Blob): Promise<Result>;
|
|
33
|
-
|
|
45
|
+
/** Resolve the Session authority of one Call: each dispatch of the result presents it. */
|
|
46
|
+
resolve(input: Parameters<typeof acceptCall>[0], operation: SessionOperation): Promise<ResolvedCall>;
|
|
47
|
+
/**
|
|
48
|
+
* Dispatch a resolved Call. Sending it again presents the same principal and caller, which an
|
|
49
|
+
* idempotency key needs to name the same Execution.
|
|
50
|
+
*/
|
|
51
|
+
dispatchResolved(dispatch: ResolvedCall, options: SessionRequestOptions, operation: SessionOperation, streaming?: Streaming): Promise<Result>;
|
|
34
52
|
/** The cached route for this partition, evicted when it no longer passes delivery checks. */
|
|
35
53
|
private warmRoute;
|
|
36
54
|
private dispatchSource;
|
|
@@ -11,13 +11,14 @@ export class SessionDispatcher {
|
|
|
11
11
|
this.state = state;
|
|
12
12
|
}
|
|
13
13
|
async dispatch(input, options, operation) {
|
|
14
|
-
return this.
|
|
14
|
+
return this.dispatchResolved(await this.resolve(input, operation), options, operation);
|
|
15
15
|
}
|
|
16
16
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
17
17
|
async dispatchHttp(input, options, operation, body) {
|
|
18
|
-
return this.
|
|
18
|
+
return this.dispatchResolved(await this.resolve(input, operation), options, operation, { body });
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
/** Resolve the Session authority of one Call: each dispatch of the result presents it. */
|
|
21
|
+
async resolve(input, operation) {
|
|
21
22
|
this.state.requireOpen();
|
|
22
23
|
const call = acceptCall(input);
|
|
23
24
|
// Authority is resolved before warm lookup so changed credentials or delegation cannot reuse a
|
|
@@ -28,7 +29,14 @@ export class SessionDispatcher {
|
|
|
28
29
|
: await this.state.exchange.resolve(call, resolved, operation);
|
|
29
30
|
const source = this.state.kernel;
|
|
30
31
|
const cacheKey = await runSessionOperation(operation, () => this.state.cache.key(source, call, auth));
|
|
31
|
-
|
|
32
|
+
return Object.freeze({ call, auth, source, cacheKey });
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Dispatch a resolved Call. Sending it again presents the same principal and caller, which an
|
|
36
|
+
* idempotency key needs to name the same Execution.
|
|
37
|
+
*/
|
|
38
|
+
async dispatchResolved(dispatch, options, operation, streaming) {
|
|
39
|
+
this.state.requireOpen();
|
|
32
40
|
const learned = await this.warmRoute(dispatch, operation, streaming);
|
|
33
41
|
if (learned !== undefined) {
|
|
34
42
|
return this.dispatchDestinationWithRecovery(learned, dispatch, options, operation, undefined, streaming);
|
|
@@ -61,6 +61,10 @@ export declare class ClientSession {
|
|
|
61
61
|
dispatch(call: Call, options: SessionRequestOptionsWithBody): Promise<Result>;
|
|
62
62
|
call(call: Call, options?: SessionRequestOptions): Promise<Input>;
|
|
63
63
|
call(call: Call, options: SessionRequestOptionsWithBody): Promise<Input>;
|
|
64
|
+
/**
|
|
65
|
+
* Invoke one typed reference. A durable callable's call is keyed, by the caller's key or by one
|
|
66
|
+
* created for this call, and sent again with that key while its outcome is unknown.
|
|
67
|
+
*/
|
|
64
68
|
invoke<const Reference extends CallableReference<unknown>>(reference: InputModeOfReference<Reference> extends 'binary' ? never : Reference, input: InputOfReference<Reference>, options?: SessionRequestOptions): Promise<OutputOfReference<Reference>>;
|
|
65
69
|
invoke<const Reference extends CallableReference<unknown, unknown, 'binary'>>(reference: Reference, input: InputOfReference<Reference>, options: SessionRequestOptionsWithBody): Promise<OutputOfReference<Reference>>;
|
|
66
70
|
private dispatchWith;
|
package/dist/session/session.js
CHANGED
|
@@ -11,6 +11,7 @@ import { admitFunction } from './admission.js';
|
|
|
11
11
|
import { createContentApi } from './content/index.js';
|
|
12
12
|
import { admitControlOptions, allowEndpoint, createSessionOperation } from './context/index.js';
|
|
13
13
|
import { createSessionDiscovery } from './discovery/index.js';
|
|
14
|
+
import { attemptDurable, durableOptions } from './durable.js';
|
|
14
15
|
import { acquireDomainCredential, admitExchangeRequest, createSessionExchange, } from './exchange/index.js';
|
|
15
16
|
import { acceptSessionOptions } from './options.js';
|
|
16
17
|
import { admitReference, outputModeOf } from './reference.js';
|
|
@@ -160,10 +161,17 @@ export class ClientSession {
|
|
|
160
161
|
}),
|
|
161
162
|
input,
|
|
162
163
|
};
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
164
|
+
// A durable call is keyed, so an attempt whose outcome is unknown can be sent again.
|
|
165
|
+
const durable = admittedReference.outputMode === 'execution';
|
|
166
|
+
const requested = durable ? durableOptions(admitted.options) : admitted.options;
|
|
167
|
+
const operation = this.operation(requested);
|
|
168
|
+
const streaming = admitted.body === undefined && admittedReference.outputMode !== 'binary'
|
|
169
|
+
? undefined
|
|
170
|
+
: { body: admitted.body };
|
|
171
|
+
// Resolved once: every attempt presents the principal and caller its key names an Execution with.
|
|
172
|
+
const resolved = await this.dispatcher.resolve(call, operation);
|
|
173
|
+
const attempt = () => this.dispatcher.dispatchResolved(resolved, requested, operation, streaming);
|
|
174
|
+
const result = await (durable ? attemptDurable(operation, attempt) : attempt());
|
|
167
175
|
const narrowed = requireResultKind(admittedReference.outputMode, result);
|
|
168
176
|
switch (narrowed.kind) {
|
|
169
177
|
case 'stream':
|
|
@@ -57,7 +57,7 @@ export class WebSocketExchange {
|
|
|
57
57
|
this.receiveResponse(input);
|
|
58
58
|
return;
|
|
59
59
|
}
|
|
60
|
-
if (this.state !== 'stream' ||
|
|
60
|
+
if (this.state !== 'stream' || !isStreamFrame(input)) {
|
|
61
61
|
throw new ProtocolError('server sent an unexpected WebSocket response.');
|
|
62
62
|
}
|
|
63
63
|
this.receiveStreamFrame(input);
|
|
@@ -301,6 +301,10 @@ function requireId(expected, received) {
|
|
|
301
301
|
throw new ProtocolError(`response requestId ${JSON.stringify(received)} does not match request requestId ${JSON.stringify(expected)}.`);
|
|
302
302
|
}
|
|
303
303
|
}
|
|
304
|
+
/** Only stream frames may follow `stream.start`; a unary response would end the stream unchecked. */
|
|
305
|
+
function isStreamFrame(input) {
|
|
306
|
+
return 'kind' in input && typeof input.kind === 'string' && input.kind.startsWith('stream.');
|
|
307
|
+
}
|
|
304
308
|
function isKind(input, kind) {
|
|
305
309
|
return (typeof input === 'object' &&
|
|
306
310
|
input !== null &&
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrale-os/kernel-client",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.14",
|
|
4
4
|
"description": "Universal Client — protocol-first, transport-agnostic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"astrale",
|
|
@@ -65,13 +65,13 @@
|
|
|
65
65
|
"access": "public"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@astrale-os/kernel-core": "1.0.0-beta.
|
|
69
|
-
"@astrale-os/kernel-dsl": "1.0.0-beta.
|
|
70
|
-
"@astrale-os/kernel-protocol": "1.0.0-beta.
|
|
68
|
+
"@astrale-os/kernel-core": "1.0.0-beta.12",
|
|
69
|
+
"@astrale-os/kernel-dsl": "1.0.0-beta.6",
|
|
70
|
+
"@astrale-os/kernel-protocol": "1.0.0-beta.12",
|
|
71
71
|
"@tanstack/pacer": "0.20.1"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@astrale-os/kernel-server": "1.0.0-beta.
|
|
74
|
+
"@astrale-os/kernel-server": "1.0.0-beta.12",
|
|
75
75
|
"@astrale/typescript-config": "npm:@jsr/astrale__typescript-config@~1.1.0",
|
|
76
76
|
"@types/node": "^22.0.0",
|
|
77
77
|
"typescript": "7.0.2",
|