@astrale-os/kernel-client 1.0.0-beta.21 → 1.0.0-beta.23
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/client.d.ts +6 -2
- package/dist/client/client.js +32 -21
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.js +1 -1
- package/dist/client/request.d.ts +7 -2
- package/dist/client/request.js +11 -7
- package/dist/graph/domain/binding.d.ts +2 -2
- package/dist/graph/graph.d.ts +17 -3
- package/dist/graph/graph.js +4 -1
- package/dist/graph/index.d.ts +2 -2
- package/dist/graph/page-contract.d.ts +53 -1
- package/dist/graph/query.d.ts +10 -4
- package/dist/graph/query.js +125 -2
- package/dist/graph/query.typecheck.d.ts +77 -0
- package/dist/graph/query.typecheck.js +34 -0
- package/dist/session/bound.js +25 -31
- package/dist/session/content/api.d.ts +10 -2
- package/dist/session/content/api.js +13 -4
- package/dist/session/content/index.d.ts +1 -1
- package/dist/session/content/input.d.ts +6 -1
- package/dist/session/content/input.js +12 -4
- package/dist/session/content/retry.d.ts +5 -1
- package/dist/session/content/retry.js +21 -16
- package/dist/session/durable.js +13 -19
- package/dist/session/index.d.ts +1 -1
- package/dist/session/retry.d.ts +12 -0
- package/dist/session/retry.js +23 -0
- package/dist/session/routing/dispatch.d.ts +7 -3
- package/dist/session/routing/dispatch.js +23 -13
- package/dist/session/routing/request.d.ts +2 -1
- package/dist/session/routing/request.js +2 -1
- package/dist/session/session.typecheck.js +15 -2
- package/dist/store/store.d.ts +6 -6
- package/dist/transport/http/admission.js +7 -42
- package/dist/transport/http/binary-request.d.ts +44 -0
- package/dist/transport/http/binary-request.js +110 -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 +0 -1
- package/dist/transport/http/response.js +0 -6
- package/dist/transport/http/transport.d.ts +10 -0
- package/dist/transport/http/transport.js +71 -17
- package/dist/transport/unary.d.ts +7 -0
- package/dist/transport/unary.js +34 -0
- package/dist/transport/websocket/connection.js +1 -7
- package/dist/transport/websocket/exchange.d.ts +2 -0
- package/dist/transport/websocket/exchange.js +13 -43
- package/package.json +5 -5
package/dist/session/bound.js
CHANGED
|
@@ -20,40 +20,40 @@ export function bindSession(source, controls) {
|
|
|
20
20
|
const downloadSource = capture(contentSource, 'download');
|
|
21
21
|
const viewsForSource = capture(source, 'viewsFor');
|
|
22
22
|
const schemaSource = captureSchema(source.schema);
|
|
23
|
-
const dispatch = ((input, options) => dispatchSource(input,
|
|
23
|
+
const dispatch = ((input, options) => dispatchSource(input, mergeOptions(options, admittedControls)));
|
|
24
24
|
const admit = (input, options) => admitSource(input, mergeOptions(options, admittedControls));
|
|
25
|
-
const call = ((input, options) => callSource(input,
|
|
26
|
-
const invoke = ((reference, input, options) => invokeSource(reference, input,
|
|
27
|
-
const stream = (input, options) => streamSource(input,
|
|
28
|
-
const binary = (input, options) => binarySource(input,
|
|
25
|
+
const call = ((input, options) => callSource(input, mergeOptions(options, admittedControls)));
|
|
26
|
+
const invoke = ((reference, input, options) => invokeSource(reference, input, mergeOptions(options, admittedControls)));
|
|
27
|
+
const stream = (input, options) => streamSource(input, mergeOptions(options, admittedControls));
|
|
28
|
+
const binary = (input, options) => binarySource(input, mergeOptions(options, admittedControls));
|
|
29
29
|
const upload = (input, options) => uploadSource(input, mergeOptions(options, admittedControls));
|
|
30
30
|
// Both forms share the bound controls; the source selects the form from its argument.
|
|
31
|
-
const download = ((input, options) => downloadSource(input,
|
|
31
|
+
const download = ((input, options) => downloadSource(input, mergeOptions(options, admittedControls)));
|
|
32
32
|
const content = Object.freeze({ upload, download });
|
|
33
|
-
const viewsFor = (domain, options) => viewsForSource(domain,
|
|
34
|
-
const install = (request, options) => schemaSource.install(request,
|
|
35
|
-
const uninstall = (request, options) => schemaSource.uninstall(request,
|
|
36
|
-
const inspect = (origin, options) => schemaSource.inspect(origin,
|
|
37
|
-
const bundle = (origin, options) => schemaSource.bundle(origin,
|
|
38
|
-
const dependencies = (origin, options) => schemaSource.dependencies(origin,
|
|
39
|
-
const installed = (options) => schemaSource.installed(
|
|
33
|
+
const viewsFor = (domain, options) => viewsForSource(domain, mergeOptions(options, admittedControls));
|
|
34
|
+
const install = (request, options) => schemaSource.install(request, mergeOptions(options, admittedControls));
|
|
35
|
+
const uninstall = (request, options) => schemaSource.uninstall(request, mergeOptions(options, admittedControls));
|
|
36
|
+
const inspect = (origin, options) => schemaSource.inspect(origin, mergeOptions(options, admittedControls));
|
|
37
|
+
const bundle = (origin, options) => schemaSource.bundle(origin, mergeOptions(options, admittedControls));
|
|
38
|
+
const dependencies = (origin, options) => schemaSource.dependencies(origin, mergeOptions(options, admittedControls));
|
|
39
|
+
const installed = (options) => schemaSource.installed(mergeOptions(options, admittedControls));
|
|
40
40
|
const installs = schemaSource.installation;
|
|
41
41
|
const installation = Object.freeze({
|
|
42
|
-
status: (execution, options) => installs.status(execution,
|
|
43
|
-
result: (execution, wait, options) => installs.result(execution, wait,
|
|
44
|
-
apply: (execution, command, options) => installs.apply(execution, command,
|
|
45
|
-
abort: (execution, attempt, options) => installs.abort(execution, attempt,
|
|
46
|
-
cancel: (execution, input, options) => installs.cancel(execution, input,
|
|
47
|
-
resume: (execution, options) => installs.resume(execution,
|
|
48
|
-
delete: (execution, options) => installs.delete(execution,
|
|
42
|
+
status: (execution, options) => installs.status(execution, mergeOptions(options, admittedControls)),
|
|
43
|
+
result: (execution, wait, options) => installs.result(execution, wait, mergeOptions(options, admittedControls)),
|
|
44
|
+
apply: (execution, command, options) => installs.apply(execution, command, mergeOptions(options, admittedControls)),
|
|
45
|
+
abort: (execution, attempt, options) => installs.abort(execution, attempt, mergeOptions(options, admittedControls)),
|
|
46
|
+
cancel: (execution, input, options) => installs.cancel(execution, input, mergeOptions(options, admittedControls)),
|
|
47
|
+
resume: (execution, options) => installs.resume(execution, mergeOptions(options, admittedControls)),
|
|
48
|
+
delete: (execution, options) => installs.delete(execution, mergeOptions(options, admittedControls)),
|
|
49
49
|
});
|
|
50
50
|
const uninstalls = schemaSource.uninstallation;
|
|
51
51
|
const uninstallation = Object.freeze({
|
|
52
|
-
status: (execution, options) => uninstalls.status(execution,
|
|
53
|
-
result: (execution, wait, options) => uninstalls.result(execution, wait,
|
|
54
|
-
cancel: (execution, input, options) => uninstalls.cancel(execution, input,
|
|
55
|
-
resume: (execution, options) => uninstalls.resume(execution,
|
|
56
|
-
delete: (execution, options) => uninstalls.delete(execution,
|
|
52
|
+
status: (execution, options) => uninstalls.status(execution, mergeOptions(options, admittedControls)),
|
|
53
|
+
result: (execution, wait, options) => uninstalls.result(execution, wait, mergeOptions(options, admittedControls)),
|
|
54
|
+
cancel: (execution, input, options) => uninstalls.cancel(execution, input, mergeOptions(options, admittedControls)),
|
|
55
|
+
resume: (execution, options) => uninstalls.resume(execution, mergeOptions(options, admittedControls)),
|
|
56
|
+
delete: (execution, options) => uninstalls.delete(execution, mergeOptions(options, admittedControls)),
|
|
57
57
|
});
|
|
58
58
|
const schema = Object.freeze({
|
|
59
59
|
install,
|
|
@@ -102,12 +102,6 @@ function admitControls(input) {
|
|
|
102
102
|
}
|
|
103
103
|
return Object.freeze({ signal: input.signal, deadline: input.deadline });
|
|
104
104
|
}
|
|
105
|
-
function requestOptions(options, controls) {
|
|
106
|
-
return mergeOptions(options, controls);
|
|
107
|
-
}
|
|
108
|
-
function controlOptions(options, controls) {
|
|
109
|
-
return mergeOptions(options, controls);
|
|
110
|
-
}
|
|
111
105
|
function mergeOptions(options, controls) {
|
|
112
106
|
if (controls === undefined)
|
|
113
107
|
return options;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import type { BinaryRequestBody } from '@astrale-os/kernel-core/callable';
|
|
1
2
|
import type { Path } from '@astrale-os/kernel-core/path';
|
|
2
3
|
import type { schema } from '@astrale-os/kernel-dsl/v1';
|
|
3
4
|
import type { Ref } from '@astrale-os/kernel-protocol/artifact';
|
|
4
5
|
import type { Binary, Input, Result } from '../../client/index.js';
|
|
6
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
5
7
|
import type { SessionControlOptions, SessionOperation } from '../context/index.js';
|
|
6
8
|
export interface ContentLocation {
|
|
7
9
|
readonly node: string;
|
|
@@ -27,8 +29,14 @@ export type ContentDownload = {
|
|
|
27
29
|
readonly outcome: 'rejected';
|
|
28
30
|
readonly code: ContentRejection;
|
|
29
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* The bytes one upload stores. A Blob, File, or Uint8Array is read again by every attempt. A stream
|
|
34
|
+
* must yield exactly `length` bytes; the upload consumes it in one attempt, and cancels it if it
|
|
35
|
+
* fails before reading it through its end. A Domain's own binary input has this shape.
|
|
36
|
+
*/
|
|
37
|
+
export type ContentSource = Blob | Uint8Array | BinaryRequestBody;
|
|
30
38
|
export interface ContentApi {
|
|
31
|
-
upload(source:
|
|
39
|
+
upload(source: ContentSource, options: ContentUploadOptions): Promise<ContentUploadResult>;
|
|
32
40
|
download(location: ContentLocation, options?: SessionControlOptions): Promise<Binary>;
|
|
33
41
|
/**
|
|
34
42
|
* One outcome per location, in order, within one time budget for the whole list. Consume each
|
|
@@ -39,7 +47,7 @@ export interface ContentApi {
|
|
|
39
47
|
/** What the Session lends the content transfer: its time budgets and its routed dispatch. */
|
|
40
48
|
export interface ContentDependencies {
|
|
41
49
|
readonly operation: (options: SessionControlOptions) => SessionOperation;
|
|
42
|
-
readonly dispatch: (target: Path, input: Input, operation: SessionOperation, body?:
|
|
50
|
+
readonly dispatch: (target: Path, input: Input, operation: SessionOperation, body?: BinaryBody) => Promise<Result>;
|
|
43
51
|
}
|
|
44
52
|
/** Transfer immutable content over the authenticated Session routing conversation. */
|
|
45
53
|
export declare function createContentApi(dependencies: ContentDependencies): ContentApi;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cancelUnsent } from '../../transport/http/binary-request.js';
|
|
1
2
|
import { requireResultKind } from '../routing/index.js';
|
|
2
3
|
import { downloadBatch } from './batch.js';
|
|
3
4
|
import { acceptBody, acceptControls, acceptLocation, acceptMediaType } from './input.js';
|
|
@@ -7,10 +8,18 @@ import { DOWNLOAD, UPLOAD, decodeDownload, decodeUploadResult } from './wire.js'
|
|
|
7
8
|
export function createContentApi(dependencies) {
|
|
8
9
|
async function upload(source, options) {
|
|
9
10
|
const body = acceptBody(source);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
try {
|
|
12
|
+
const mediaType = acceptMediaType(options?.mediaType);
|
|
13
|
+
const operation = dependencies.operation(acceptControls(options));
|
|
14
|
+
const send = () => dependencies.dispatch(UPLOAD, { mediaType }, operation, body);
|
|
15
|
+
// A stream cannot be read twice: it has one attempt.
|
|
16
|
+
const result = await (body.kind === 'replayable' ? retry(operation, send) : send());
|
|
17
|
+
return decodeUploadResult(requireResultKind('value', result).value);
|
|
18
|
+
}
|
|
19
|
+
catch (cause) {
|
|
20
|
+
cancelUnsent(body);
|
|
21
|
+
throw cause;
|
|
22
|
+
}
|
|
14
23
|
}
|
|
15
24
|
function download(input, options) {
|
|
16
25
|
if (Array.isArray(input)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { createContentApi, type ContentApi, type ContentDownload, type ContentLocation, type ContentRejection, type ContentUploadOptions, type ContentUploadResult, } from './api.js';
|
|
1
|
+
export { createContentApi, type ContentApi, type ContentDownload, type ContentLocation, type ContentRejection, type ContentSource, type ContentUploadOptions, type ContentUploadResult, } from './api.js';
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
1
2
|
import type { SessionControlOptions } from '../context/index.js';
|
|
2
3
|
import type { ContentLocation } from './api.js';
|
|
3
4
|
/** One canonical content location, as a download request carries it. */
|
|
4
5
|
export type Slot = Readonly<Record<'node' | 'property', string>>;
|
|
5
6
|
export declare function acceptLocation(input: ContentLocation): Slot;
|
|
6
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The bytes of one upload: a Blob, an owned snapshot of a Uint8Array, or a byte stream with its
|
|
9
|
+
* exact length, which the upload consumes.
|
|
10
|
+
*/
|
|
11
|
+
export declare function acceptBody(input: unknown): BinaryBody;
|
|
7
12
|
export declare function acceptMediaType(input: unknown): string;
|
|
8
13
|
/** The caller's controls, with the content transfer's own default time budget. */
|
|
9
14
|
export declare function acceptControls(input: SessionControlOptions | undefined): SessionControlOptions;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NodeId } from '@astrale-os/kernel-core/graph/node';
|
|
2
2
|
import { PropertyKey } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
3
3
|
import * as artifact from '@astrale-os/kernel-protocol/artifact';
|
|
4
|
+
import { acceptBinaryBody, replayableBody } from '../../transport/http/binary-request.js';
|
|
4
5
|
import { admitControlOptions } from '../context/index.js';
|
|
5
6
|
const CONTENT_TIMEOUT_MS = 30 * 60 * 1_000;
|
|
6
7
|
export function acceptLocation(input) {
|
|
@@ -9,17 +10,24 @@ export function acceptLocation(input) {
|
|
|
9
10
|
}
|
|
10
11
|
return Object.freeze({ node: NodeId(input.node), property: PropertyKey(input.property) });
|
|
11
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* The bytes of one upload: a Blob, an owned snapshot of a Uint8Array, or a byte stream with its
|
|
15
|
+
* exact length, which the upload consumes.
|
|
16
|
+
*/
|
|
12
17
|
export function acceptBody(input) {
|
|
13
|
-
if (typeof Blob !== 'undefined' && input instanceof Blob)
|
|
14
|
-
return input;
|
|
15
18
|
if (input instanceof Uint8Array) {
|
|
16
19
|
// Blob snapshots an ArrayBuffer view; a SharedArrayBuffer view needs an owned copy.
|
|
17
20
|
const bytes = input.buffer instanceof ArrayBuffer
|
|
18
21
|
? input
|
|
19
22
|
: new Uint8Array(input);
|
|
20
|
-
return new Blob([bytes]);
|
|
23
|
+
return replayableBody(new Blob([bytes]));
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return acceptBinaryBody(input);
|
|
27
|
+
}
|
|
28
|
+
catch (cause) {
|
|
29
|
+
throw new TypeError('Content upload source must be a Blob, File, Uint8Array, or { body: ReadableStream, length }.', { cause });
|
|
21
30
|
}
|
|
22
|
-
throw new TypeError('Content upload source must be a Blob, File, or Uint8Array.');
|
|
23
31
|
}
|
|
24
32
|
export function acceptMediaType(input) {
|
|
25
33
|
if (typeof input !== 'string')
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
import type { SessionOperation } from '../context/index.js';
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* Repeat one content request that failed before any of its result was delivered: after a pause of
|
|
4
|
+
* at least 100 ms, then 200 ms, or of the delay a refusal prescribes when it is longer, each
|
|
5
|
+
* lengthened by a random share of up to as much again.
|
|
6
|
+
*/
|
|
3
7
|
export declare function retry<Result>(operation: SessionOperation, request: () => Promise<Result>): Promise<Result>;
|
|
@@ -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') ||
|
package/dist/session/durable.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { CODES } from '@astrale-os/kernel-protocol/errors';
|
|
2
2
|
import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
|
|
3
3
|
import { ResponseError, TransportError } from '../errors/index.js';
|
|
4
|
-
import {
|
|
5
|
-
/** The first attempt of a durable invocation and at most two more, within its one deadline. */
|
|
6
|
-
const MAXIMUM_ATTEMPTS = 3;
|
|
4
|
+
import { retrySessionRequest } from './retry.js';
|
|
7
5
|
const FIRST_PAUSE_MS = 200;
|
|
8
6
|
/**
|
|
9
7
|
* Options of one typed invocation of a durable callable: the caller's key, or one created for this
|
|
@@ -22,22 +20,18 @@ export function durableOptions(options) {
|
|
|
22
20
|
* not, or the Kernel's `OUTCOME_UNKNOWN`. Its key makes the Kernel answer with the Execution a lost
|
|
23
21
|
* attempt recorded, marked `replayed`, instead of starting another.
|
|
24
22
|
*/
|
|
25
|
-
export
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
await pauseSessionOperation(operation, pause);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
23
|
+
export function attemptDurable(operation, attempt) {
|
|
24
|
+
return retrySessionRequest(operation, pause, attempt);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The pause after failed attempt `failed`: 200 ms doubled at each attempt, unless the outcome is
|
|
28
|
+
* known or the call's deadline would pass first.
|
|
29
|
+
*/
|
|
30
|
+
function pause(cause, failed, operation) {
|
|
31
|
+
if (!unknownOutcome(cause))
|
|
32
|
+
return undefined;
|
|
33
|
+
const ms = FIRST_PAUSE_MS * 2 ** (failed - 1);
|
|
34
|
+
return operation.deadline - Date.now() <= ms ? undefined : ms;
|
|
41
35
|
}
|
|
42
36
|
function unknownOutcome(cause) {
|
|
43
37
|
return ((cause instanceof TransportError && cause.context.kind === 'invocation') ||
|
package/dist/session/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type { SessionControlOptions, SessionPolicy } from './context/index.js';
|
|
2
|
-
export type { ContentApi, ContentDownload, ContentLocation, ContentRejection, ContentUploadOptions, ContentUploadResult, } from './content/index.js';
|
|
2
|
+
export type { ContentApi, ContentDownload, ContentLocation, ContentRejection, ContentSource, ContentUploadOptions, ContentUploadResult, } from './content/index.js';
|
|
3
3
|
export { bindSession, type BoundClientSession, type BoundSessionControls, type BoundSessionSource, } from './bound.js';
|
|
4
4
|
export { credentials, type Credentials, type CredentialsMint, type CredentialsOptions, type CredentialsState, type SessionCredential, } from './credential/index.js';
|
|
5
5
|
export type { IntrinsicRelease, SessionSnapshot } from './discovery/index.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SessionOperation } from './context/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* The pause before another attempt of a request whose attempt `failed` (counted from 1) threw
|
|
4
|
+
* `cause`, or `undefined` when that failure must not be retried.
|
|
5
|
+
*/
|
|
6
|
+
export type RetryPause = (cause: unknown, failed: number, operation: SessionOperation) => number | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* Attempt `request` until it settles, its operation is interrupted, it has been attempted
|
|
9
|
+
* {@link MAXIMUM_ATTEMPTS} times, or `pause` declines its failure, pausing through the operation
|
|
10
|
+
* between attempts. A request that is not retried throws its last cause.
|
|
11
|
+
*/
|
|
12
|
+
export declare function retrySessionRequest<Result>(operation: SessionOperation, pause: RetryPause, request: () => Promise<Result>): Promise<Result>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { pauseSessionOperation } from './context/index.js';
|
|
2
|
+
/** The first attempt of a session request and at most two more. */
|
|
3
|
+
const MAXIMUM_ATTEMPTS = 3;
|
|
4
|
+
/**
|
|
5
|
+
* Attempt `request` until it settles, its operation is interrupted, it has been attempted
|
|
6
|
+
* {@link MAXIMUM_ATTEMPTS} times, or `pause` declines its failure, pausing through the operation
|
|
7
|
+
* between attempts. A request that is not retried throws its last cause.
|
|
8
|
+
*/
|
|
9
|
+
export async function retrySessionRequest(operation, pause, request) {
|
|
10
|
+
for (let failed = 1;; failed++) {
|
|
11
|
+
try {
|
|
12
|
+
return await request();
|
|
13
|
+
}
|
|
14
|
+
catch (cause) {
|
|
15
|
+
if (failed === MAXIMUM_ATTEMPTS || operation.signal.aborted)
|
|
16
|
+
throw cause;
|
|
17
|
+
const ms = pause(cause, failed, operation);
|
|
18
|
+
if (ms === undefined)
|
|
19
|
+
throw cause;
|
|
20
|
+
await pauseSessionOperation(operation, ms);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { IssuerId } from '@astrale-os/kernel-core/auth/issuer';
|
|
2
2
|
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
3
3
|
import type { Result } from '../../client/index.js';
|
|
4
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
4
5
|
import type { Fetch } from '../../transport/http/index.js';
|
|
5
6
|
import type { SessionOperation, SessionPolicy } from '../context/index.js';
|
|
6
7
|
import type { AuthExchange, SessionAuth, EffectiveAuth } from './auth.js';
|
|
@@ -25,9 +26,12 @@ export interface SessionDispatcherOptions {
|
|
|
25
26
|
readonly timeoutMs: number;
|
|
26
27
|
};
|
|
27
28
|
}
|
|
28
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* A streaming call is forced over HTTP and may carry an admitted binary body, whose kind says
|
|
31
|
+
* whether it can be sent again: a Blob is read anew by every send, a stream only by the first.
|
|
32
|
+
*/
|
|
29
33
|
export interface Streaming {
|
|
30
|
-
readonly body?:
|
|
34
|
+
readonly body?: BinaryBody;
|
|
31
35
|
}
|
|
32
36
|
/** One Call with the authority it presents on every dispatch, and the route partition it selects. */
|
|
33
37
|
export interface ResolvedCall {
|
|
@@ -41,7 +45,7 @@ export declare class SessionDispatcher {
|
|
|
41
45
|
constructor(state: SessionDispatcherOptions);
|
|
42
46
|
dispatch(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation): Promise<Result>;
|
|
43
47
|
/** Reuse Session authority and route recovery while forcing HTTP for streaming calls. */
|
|
44
|
-
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?:
|
|
48
|
+
dispatchHttp(input: Parameters<typeof acceptCall>[0], options: SessionRequestOptions, operation: SessionOperation, body?: BinaryBody): Promise<Result>;
|
|
45
49
|
/** Resolve the Session authority of one Call: each dispatch of the result presents it. */
|
|
46
50
|
resolve(input: Parameters<typeof acceptCall>[0], operation: SessionOperation): Promise<ResolvedCall>;
|
|
47
51
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Key } from '@astrale-os/kernel-dsl/v1/addressing';
|
|
2
|
-
import { acceptCall, Client,
|
|
2
|
+
import { acceptCall, Client, dispatchInternal } from '../../client/index.js';
|
|
3
3
|
import { ClientError } from '../../errors/index.js';
|
|
4
4
|
import { runSessionOperation, remainingSessionTime, sessionTransportOptions, allowEndpoint, } from '../context/index.js';
|
|
5
5
|
import { resolveAuth } from './auth.js';
|
|
@@ -72,11 +72,12 @@ export class SessionDispatcher {
|
|
|
72
72
|
...(routing === undefined ? {} : { routing }),
|
|
73
73
|
// The guard is the source Kernel's: a routed destination request never carries it.
|
|
74
74
|
...(options.maintenance === undefined ? {} : { maintenance: options.maintenance }),
|
|
75
|
-
}));
|
|
75
|
+
}, streaming?.body));
|
|
76
76
|
}
|
|
77
77
|
async followSource(result, state, options, operation, streaming) {
|
|
78
78
|
if (result.kind !== 'redirect')
|
|
79
79
|
return result;
|
|
80
|
+
requireResendable(streaming, 'follow a Redirect');
|
|
80
81
|
const redirect = result.redirect;
|
|
81
82
|
if (redirect.route.via.issuer !== state.source) {
|
|
82
83
|
throw new ClientError('Redirect issuer differs from the session-pinned source Kernel identity.');
|
|
@@ -99,6 +100,7 @@ export class SessionDispatcher {
|
|
|
99
100
|
this.state.cache.delete(state.cacheKey);
|
|
100
101
|
if (!operation.claimRecovery())
|
|
101
102
|
throw cause;
|
|
103
|
+
requireResendable(streaming, 'recover its route', cause);
|
|
102
104
|
const routing = recoveryRefresh(control, learned);
|
|
103
105
|
const refreshed = await this.dispatchSource(state, routing, options, operation, streaming);
|
|
104
106
|
return this.followSource(refreshed, state, options, operation, streaming);
|
|
@@ -112,7 +114,7 @@ export class SessionDispatcher {
|
|
|
112
114
|
...transportOptions(operation, options, streaming),
|
|
113
115
|
routing: learned.route.via,
|
|
114
116
|
};
|
|
115
|
-
const result = await runSessionOperation(operation, () => send(client, learned.credential, call, request, trace));
|
|
117
|
+
const result = await runSessionOperation(operation, () => send(client, learned.credential, call, request, streaming?.body, trace));
|
|
116
118
|
if (result.kind === 'redirect') {
|
|
117
119
|
throw new ClientError('A destination redirect is invalid; only the source Kernel may redirect.');
|
|
118
120
|
}
|
|
@@ -144,7 +146,13 @@ export class SessionDispatcher {
|
|
|
144
146
|
function transportOptions(operation, options, streaming) {
|
|
145
147
|
return streaming?.body === undefined
|
|
146
148
|
? sessionTransportOptions(operation, options.idempotencyKey)
|
|
147
|
-
:
|
|
149
|
+
: sessionTransportOptions(operation);
|
|
150
|
+
}
|
|
151
|
+
/** A stream body was consumed by the send that just answered: it is never sent again. */
|
|
152
|
+
function requireResendable(streaming, step, cause) {
|
|
153
|
+
if (streaming?.body?.kind !== 'once')
|
|
154
|
+
return;
|
|
155
|
+
throw new ClientError(`A call with a streamed binary body is sent once and cannot ${step}; send it again with a new stream.`, cause === undefined ? undefined : { cause });
|
|
148
156
|
}
|
|
149
157
|
function requireLearnedRoute(learned, source, target) {
|
|
150
158
|
if (learned.route.via.issuer !== source) {
|
|
@@ -160,13 +168,15 @@ function requireTargetRelease(route, target) {
|
|
|
160
168
|
throw new ClientError('Resolved route does not match the Target origin.');
|
|
161
169
|
}
|
|
162
170
|
}
|
|
163
|
-
/**
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
171
|
+
/**
|
|
172
|
+
* Send one call, presenting the Trace only on the attempt that carries it. A call with a body goes
|
|
173
|
+
* through the package-private path, which sends the admitted body: the public Client refuses a
|
|
174
|
+
* stream, because only this routing refuses to resend it. A body-carrying call is always sent by a
|
|
175
|
+
* Client this routing created.
|
|
176
|
+
*/
|
|
177
|
+
function send(client, credential, call, options, body, trace) {
|
|
178
|
+
if (trace !== undefined || body !== undefined) {
|
|
179
|
+
return dispatchInternal(client, credential, call, options, trace, body);
|
|
180
|
+
}
|
|
181
|
+
return (credential === undefined ? client : client.as(credential)).dispatch(call, options);
|
|
172
182
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type * as invocation from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import type { BinaryBody } from '../../transport/http/binary-request.js';
|
|
2
3
|
import type { SessionControlOptions } from '../context/index.js';
|
|
3
4
|
export type SessionEnvelopeTransport = 'auto' | 'http' | 'websocket';
|
|
4
5
|
export interface SessionRequestOptions extends SessionControlOptions {
|
|
@@ -15,7 +16,7 @@ export type SessionRequestOptionsWithBody = Omit<SessionRequestOptions, 'body' |
|
|
|
15
16
|
};
|
|
16
17
|
export declare function admitInvocationOptions(input: SessionRequestOptions | SessionRequestOptionsWithBody | undefined): {
|
|
17
18
|
readonly options: Readonly<SessionRequestOptions>;
|
|
18
|
-
readonly body?:
|
|
19
|
+
readonly body?: BinaryBody;
|
|
19
20
|
};
|
|
20
21
|
export declare function admitRequestOptions(input?: SessionRequestOptions): Readonly<SessionRequestOptions>;
|
|
21
22
|
export declare function admitEnvelopeTransport(input: SessionEnvelopeTransport): SessionEnvelopeTransport;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
|
+
import { replayableBody } from '../../transport/http/binary-request.js';
|
|
2
3
|
import { admitControlOptions } from '../context/index.js';
|
|
3
4
|
export function admitInvocationOptions(input) {
|
|
4
5
|
if (input === undefined)
|
|
@@ -15,7 +16,7 @@ export function admitInvocationOptions(input) {
|
|
|
15
16
|
if (options.idempotencyKey !== undefined) {
|
|
16
17
|
throw new TypeError('Binary requests cannot carry an idempotency key.');
|
|
17
18
|
}
|
|
18
|
-
return Object.freeze({ options: admitRequestOptions(options), body });
|
|
19
|
+
return Object.freeze({ options: admitRequestOptions(options), body: replayableBody(body) });
|
|
19
20
|
}
|
|
20
21
|
export function admitRequestOptions(input = {}) {
|
|
21
22
|
if (input === null || typeof input !== 'object' || Array.isArray(input)) {
|
|
@@ -110,6 +110,10 @@ const boundBlobReceipt = boundSession.content
|
|
|
110
110
|
.then(({ receipt }) => receipt);
|
|
111
111
|
void blobReceipt;
|
|
112
112
|
void boundBlobReceipt;
|
|
113
|
+
void session.content.upload(received, { mediaType: 'image/png' });
|
|
114
|
+
void boundSession.content.upload({ body: new ReadableStream(), length: 0 }, { mediaType: 'image/png' });
|
|
115
|
+
// @ts-expect-error A stream upload declares its exact length.
|
|
116
|
+
void session.content.upload({ body: new ReadableStream() }, { mediaType: 'image/png' });
|
|
113
117
|
void session.invoke(registerReference, registration);
|
|
114
118
|
void boundSession.invoke(staticRegisterReference, registration);
|
|
115
119
|
void session.invoke(renameReference, renameRequest);
|
|
@@ -121,6 +125,12 @@ void client.dispatch(rawBinaryCall, { body: new Blob() });
|
|
|
121
125
|
void client.as('credential').call(rawBinaryCall, { body: new Blob() });
|
|
122
126
|
// @ts-expect-error Binary Functions require a byte source.
|
|
123
127
|
void session.invoke(binaryUploadReference, { mediaType: 'image/png' });
|
|
128
|
+
// @ts-expect-error A Session invocation sends a re-readable Blob: it may follow a Redirect.
|
|
129
|
+
void session.invoke(binaryUploadReference, { mediaType: 'image/png' }, { body: received });
|
|
130
|
+
// @ts-expect-error A raw Client sends a re-readable Blob: it cannot resend a stream a Redirect answers.
|
|
131
|
+
void client.dispatch(rawBinaryCall, { body: received });
|
|
132
|
+
// @ts-expect-error The public HTTP transport sends a re-readable Blob too.
|
|
133
|
+
void httpTransport.dispatchBinary(rawEnvelope, received);
|
|
124
134
|
// @ts-expect-error Value Functions reject binary bodies.
|
|
125
135
|
void session.invoke(registerReference, registration, { body: new Blob() });
|
|
126
136
|
// @ts-expect-error Bound binary Functions require a byte source.
|
|
@@ -207,5 +217,8 @@ void session.invoke(dynamicReference, dynamicInput, { expect: undefined });
|
|
|
207
217
|
const unsupportedClientOptions = { expect: undefined };
|
|
208
218
|
void unsupportedClientOptions;
|
|
209
219
|
// @ts-expect-error Internal metadata dispatch is absent from the public package facade.
|
|
210
|
-
import {
|
|
211
|
-
void
|
|
220
|
+
import { dispatchInternal } from '../index.js';
|
|
221
|
+
void dispatchInternal;
|
|
222
|
+
// @ts-expect-error The stream-capable HTTP binary path is absent from the public package facade.
|
|
223
|
+
import { dispatchBinaryBody } from '../index.js';
|
|
224
|
+
void dispatchBinaryBody;
|
package/dist/store/store.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { MutationAST, MutationInput, MutationResult } from '@astrale-os/kernel-core/graph/mutate';
|
|
2
2
|
import type { QueryAST, QueryResultFor } from '@astrale-os/kernel-core/graph/query';
|
|
3
3
|
import type { QueryResponse } from '../graph/index.js';
|
|
4
|
-
import type { GraphApi, GraphMutationOptions,
|
|
4
|
+
import type { GraphApi, GraphMutationOptions, GraphQueryV6Options, MutationAuthor } from '../graph/index.js';
|
|
5
5
|
export type StoreQuery = QueryAST;
|
|
6
6
|
export type StoreQueryResponse<Query extends StoreQuery = StoreQuery> = QueryResponse<QueryResultFor<Query>>;
|
|
7
|
-
export interface StoreReadOptions extends
|
|
7
|
+
export interface StoreReadOptions extends GraphQueryV6Options {
|
|
8
8
|
readonly freshness?: 'cached' | 'refresh';
|
|
9
9
|
}
|
|
10
10
|
export interface StoreMutationOptions extends GraphMutationOptions {
|
|
@@ -50,15 +50,15 @@ export declare class GraphStore {
|
|
|
50
50
|
private closed;
|
|
51
51
|
constructor(options: StoreOptions);
|
|
52
52
|
read<Query extends StoreQuery>(query: Query, options: StoreReadOptions): Promise<StoreQueryResponse<Query>>;
|
|
53
|
-
peek<Query extends StoreQuery>(query: Query, options:
|
|
53
|
+
peek<Query extends StoreQuery>(query: Query, options: GraphQueryV6Options): StoreQueryResponse<Query> | undefined;
|
|
54
54
|
preload<Query extends StoreQuery>(query: Query, options: StoreReadOptions): Promise<void>;
|
|
55
|
-
subscribe<Query extends StoreQuery>(query: Query, listener: () => void, options:
|
|
56
|
-
getSnapshot<Query extends StoreQuery>(query: Query, options:
|
|
55
|
+
subscribe<Query extends StoreQuery>(query: Query, listener: () => void, options: GraphQueryV6Options): () => void;
|
|
56
|
+
getSnapshot<Query extends StoreQuery>(query: Query, options: GraphQueryV6Options): StoreSnapshot<Query>;
|
|
57
57
|
observe<Query extends StoreQuery>(query: Query, listener: (snapshot: StoreSnapshot<Query>) => void, options: StoreReadOptions): () => void;
|
|
58
58
|
mutate(author: MutationAuthor, options?: StoreMutationOptions): Promise<MutationResult>;
|
|
59
59
|
mutate(input: MutationAST | MutationInput, options?: StoreMutationOptions): Promise<MutationResult>;
|
|
60
60
|
invalidate(): void;
|
|
61
|
-
invalidate(query: StoreQuery, options:
|
|
61
|
+
invalidate(query: StoreQuery, options: GraphQueryV6Options): void;
|
|
62
62
|
sweep(): void;
|
|
63
63
|
close(): void;
|
|
64
64
|
private admit;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as protocol from '@astrale-os/kernel-protocol/invocation';
|
|
2
2
|
import * as retirement from '@astrale-os/kernel-protocol/routes';
|
|
3
|
-
import { ProtocolError
|
|
3
|
+
import { ProtocolError } from '../../errors/index.js';
|
|
4
4
|
import { invocationFailure } from '../failure.js';
|
|
5
5
|
import { requireLive } from '../index.js';
|
|
6
|
+
import { responseError, unaryResult } from '../unary.js';
|
|
6
7
|
import { decodeBinary } from './binary.js';
|
|
7
8
|
import { codecForResponse, decodeProtocol, readBoundedBody, requireId, requireNoStore, } from './response.js';
|
|
8
9
|
import { decodeDeploymentRetired } from './retired.js';
|
|
@@ -63,47 +64,11 @@ export async function admitResponse(response, requestId, context) {
|
|
|
63
64
|
requireLive(control);
|
|
64
65
|
const decoded = decodeProtocol('response body', () => responseCodec.decode(body));
|
|
65
66
|
const envelope = decodeProtocol('response envelope', () => protocol.decodeResponse(decoded));
|
|
66
|
-
if (envelope.requestId === null && 'error' in envelope)
|
|
67
|
-
throw
|
|
68
|
-
}
|
|
67
|
+
if (envelope.requestId === null && 'error' in envelope)
|
|
68
|
+
throw responseError(envelope);
|
|
69
69
|
requireId(requestId, envelope.requestId);
|
|
70
|
-
if ('
|
|
71
|
-
|
|
72
|
-
requireLive(control);
|
|
73
|
-
return Object.freeze({
|
|
74
|
-
kind: 'stale',
|
|
75
|
-
current: stale.current,
|
|
76
|
-
invocation: stale.invocation,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
if ('kind' in envelope && envelope.kind === 'miss') {
|
|
80
|
-
decodeProtocol('miss response', () => protocol.decodeMiss(envelope));
|
|
81
|
-
requireLive(control);
|
|
82
|
-
return Object.freeze({ kind: 'miss', invocation: envelope.invocation });
|
|
83
|
-
}
|
|
84
|
-
if ('kind' in envelope && envelope.kind === 'redirect') {
|
|
85
|
-
const response = decodeProtocol('redirect response', () => protocol.decodeRedirectResponse(envelope));
|
|
86
|
-
requireLive(control);
|
|
87
|
-
return redirectResult(response);
|
|
88
|
-
}
|
|
89
|
-
if ('error' in envelope) {
|
|
90
|
-
throw new ResponseError(envelope.error.code, envelope.error.message, envelope.invocation, envelope.error.data);
|
|
91
|
-
}
|
|
70
|
+
if ('error' in envelope)
|
|
71
|
+
throw responseError(envelope);
|
|
92
72
|
requireLive(control);
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
kind: 'execution',
|
|
96
|
-
execution: envelope.execution,
|
|
97
|
-
...(envelope.replayed === undefined ? {} : { replayed: envelope.replayed }),
|
|
98
|
-
invocation: envelope.invocation,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
return { kind: 'value', value: envelope.result, invocation: envelope.invocation };
|
|
102
|
-
}
|
|
103
|
-
function redirectResult(response) {
|
|
104
|
-
return Object.freeze({
|
|
105
|
-
kind: 'redirect',
|
|
106
|
-
invocation: response.invocation,
|
|
107
|
-
redirect: response.redirect,
|
|
108
|
-
});
|
|
73
|
+
return unaryResult(envelope);
|
|
109
74
|
}
|