@astrale-os/kernel-client 1.0.0-beta.5 → 1.0.0-beta.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/dist/auth/authorization.d.ts +10 -5
  2. package/dist/auth/authorization.js +14 -1
  3. package/dist/auth/index.d.ts +1 -0
  4. package/dist/client/admission.js +3 -1
  5. package/dist/client/client.d.ts +70 -4
  6. package/dist/client/client.js +209 -32
  7. package/dist/client/index.d.ts +3 -3
  8. package/dist/client/index.js +2 -2
  9. package/dist/client/level.d.ts +58 -0
  10. package/dist/client/level.js +159 -0
  11. package/dist/client/request.d.ts +7 -2
  12. package/dist/client/request.js +11 -7
  13. package/dist/client/result.d.ts +14 -1
  14. package/dist/client/result.js +19 -8
  15. package/dist/client/routing-control.d.ts +11 -15
  16. package/dist/client/routing-control.js +21 -29
  17. package/dist/errors/index.d.ts +3 -0
  18. package/dist/errors/index.js +3 -0
  19. package/dist/errors/inexpressible-error.d.ts +23 -0
  20. package/dist/errors/inexpressible-error.js +26 -0
  21. package/dist/errors/query-input.d.ts +47 -0
  22. package/dist/errors/query-input.js +85 -0
  23. package/dist/errors/refusal.d.ts +12 -0
  24. package/dist/errors/refusal.js +44 -6
  25. package/dist/errors/response-too-large-error.d.ts +11 -0
  26. package/dist/errors/response-too-large-error.js +15 -0
  27. package/dist/errors/stale-route-error.d.ts +1 -1
  28. package/dist/errors/stale-route-error.js +1 -1
  29. package/dist/{schema → execution}/admission.d.ts +2 -2
  30. package/dist/{schema → execution}/admission.js +2 -2
  31. package/dist/{session/execution.d.ts → execution/contracts.d.ts} +22 -34
  32. package/dist/execution/contracts.js +1 -0
  33. package/dist/execution/deadline.d.ts +18 -0
  34. package/dist/execution/deadline.js +25 -0
  35. package/dist/execution/execution.d.ts +61 -0
  36. package/dist/execution/execution.js +132 -0
  37. package/dist/execution/index.d.ts +4 -0
  38. package/dist/execution/index.js +3 -0
  39. package/dist/graph/domain/binding.d.ts +2 -2
  40. package/dist/graph/domain/binding.js +20 -17
  41. package/dist/graph/domain/conformance.d.ts +12 -0
  42. package/dist/graph/domain/conformance.js +18 -0
  43. package/dist/graph/domain/index.d.ts +1 -0
  44. package/dist/graph/domain/index.js +1 -1
  45. package/dist/graph/domain/provenance.d.ts +0 -2
  46. package/dist/graph/domain/provenance.js +0 -4
  47. package/dist/graph/graph.d.ts +17 -3
  48. package/dist/graph/graph.js +4 -1
  49. package/dist/graph/index.d.ts +3 -3
  50. package/dist/graph/index.js +1 -1
  51. package/dist/graph/page-contract.d.ts +66 -1
  52. package/dist/graph/provenance.d.ts +4 -5
  53. package/dist/graph/provenance.js +5 -6
  54. package/dist/graph/query.d.ts +10 -4
  55. package/dist/graph/query.js +144 -5
  56. package/dist/graph/query.typecheck.d.ts +81 -0
  57. package/dist/graph/query.typecheck.js +34 -0
  58. package/dist/index.d.ts +1 -1
  59. package/dist/index.js +1 -1
  60. package/dist/schema/contracts.d.ts +54 -79
  61. package/dist/schema/index.d.ts +2 -2
  62. package/dist/schema/index.js +1 -1
  63. package/dist/schema/installation.d.ts +36 -64
  64. package/dist/schema/installation.js +67 -131
  65. package/dist/schema/schema.d.ts +10 -16
  66. package/dist/schema/schema.js +57 -34
  67. package/dist/session/admission.js +10 -3
  68. package/dist/session/bound.d.ts +12 -3
  69. package/dist/session/bound.js +36 -32
  70. package/dist/session/content/api.d.ts +20 -2
  71. package/dist/session/content/api.js +17 -7
  72. package/dist/session/content/files.d.ts +12 -0
  73. package/dist/session/content/files.js +47 -0
  74. package/dist/session/content/index.d.ts +1 -1
  75. package/dist/session/content/input.d.ts +13 -2
  76. package/dist/session/content/input.js +25 -4
  77. package/dist/session/content/retry.d.ts +5 -1
  78. package/dist/session/content/retry.js +21 -31
  79. package/dist/session/context/index.d.ts +2 -2
  80. package/dist/session/context/index.js +2 -2
  81. package/dist/session/context/operation.d.ts +2 -0
  82. package/dist/session/context/operation.js +5 -0
  83. package/dist/session/context/policy.d.ts +2 -0
  84. package/dist/session/context/policy.js +6 -3
  85. package/dist/session/credential/credentials.d.ts +2 -1
  86. package/dist/session/credential/credentials.js +62 -29
  87. package/dist/session/discovery/bundle.d.ts +13 -4
  88. package/dist/session/discovery/bundle.js +85 -36
  89. package/dist/session/discovery/discovery.d.ts +1 -1
  90. package/dist/session/discovery/discovery.js +8 -11
  91. package/dist/session/discovery/index.d.ts +1 -0
  92. package/dist/session/discovery/index.js +5 -1
  93. package/dist/session/discovery/release.d.ts +2 -0
  94. package/dist/session/discovery/release.js +34 -31
  95. package/dist/session/discovery/response.d.ts +4 -0
  96. package/dist/session/discovery/response.js +10 -6
  97. package/dist/session/discovery/snapshot.d.ts +5 -1
  98. package/dist/session/discovery/transport.d.ts +37 -3
  99. package/dist/session/discovery/transport.js +62 -1
  100. package/dist/session/durable.d.ts +13 -0
  101. package/dist/session/durable.js +39 -0
  102. package/dist/session/exchange/exchange.d.ts +3 -2
  103. package/dist/session/exchange/exchange.js +10 -7
  104. package/dist/session/exchange/index.d.ts +1 -1
  105. package/dist/session/exchange/index.js +1 -1
  106. package/dist/session/exchange/presentation.js +28 -10
  107. package/dist/session/exchange/source.d.ts +7 -7
  108. package/dist/session/exchange/source.js +26 -27
  109. package/dist/session/index.d.ts +5 -4
  110. package/dist/session/index.js +0 -1
  111. package/dist/session/options.d.ts +4 -2
  112. package/dist/session/options.js +3 -3
  113. package/dist/session/reference.d.ts +27 -6
  114. package/dist/session/reference.js +18 -3
  115. package/dist/session/retry.d.ts +12 -0
  116. package/dist/session/retry.js +23 -0
  117. package/dist/session/routing/auth.d.ts +9 -3
  118. package/dist/session/routing/auth.js +4 -3
  119. package/dist/session/routing/cache.d.ts +12 -5
  120. package/dist/session/routing/cache.js +56 -48
  121. package/dist/session/routing/dispatch.d.ts +26 -3
  122. package/dist/session/routing/dispatch.js +112 -69
  123. package/dist/session/routing/envelope.d.ts +3 -3
  124. package/dist/session/routing/index.d.ts +1 -1
  125. package/dist/session/routing/legacy/release-route.d.ts +12 -0
  126. package/dist/session/routing/legacy/release-route.js +6 -0
  127. package/dist/session/routing/recovery.d.ts +3 -6
  128. package/dist/session/routing/recovery.js +3 -14
  129. package/dist/session/routing/request.d.ts +2 -1
  130. package/dist/session/routing/request.js +2 -1
  131. package/dist/session/routing/result.d.ts +1 -1
  132. package/dist/session/routing/result.js +5 -1
  133. package/dist/session/routing/store.js +4 -5
  134. package/dist/session/session.d.ts +31 -5
  135. package/dist/session/session.js +70 -22
  136. package/dist/session/session.typecheck.js +20 -6
  137. package/dist/session/views/index.d.ts +2 -2
  138. package/dist/session/views/index.js +2 -2
  139. package/dist/session/views/kernel.d.ts +31 -0
  140. package/dist/session/views/kernel.js +30 -0
  141. package/dist/session/views/legacy/catalog.d.ts +21 -0
  142. package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
  143. package/dist/session/views/legacy/index.d.ts +14 -0
  144. package/dist/session/views/legacy/index.js +14 -0
  145. package/dist/session/views/legacy/resolve.d.ts +46 -0
  146. package/dist/session/views/legacy/resolve.js +83 -0
  147. package/dist/session/views/legacy/unplaced.d.ts +22 -0
  148. package/dist/session/views/legacy/unplaced.js +26 -0
  149. package/dist/session/views/read.d.ts +49 -0
  150. package/dist/session/views/read.js +148 -0
  151. package/dist/session/views/view.d.ts +48 -0
  152. package/dist/session/views/view.js +74 -0
  153. package/dist/store/memory.d.ts +1 -3
  154. package/dist/store/memory.js +61 -112
  155. package/dist/store/shape.d.ts +33 -12
  156. package/dist/store/shape.js +125 -16
  157. package/dist/store/store.d.ts +6 -6
  158. package/dist/store/store.js +1 -1
  159. package/dist/transport/auto/decision.d.ts +28 -14
  160. package/dist/transport/auto/decision.js +39 -32
  161. package/dist/transport/auto/fallback.d.ts +1 -1
  162. package/dist/transport/auto/fallback.js +5 -10
  163. package/dist/transport/auto/transport.d.ts +10 -3
  164. package/dist/transport/auto/transport.js +41 -32
  165. package/dist/transport/deadline.d.ts +2 -0
  166. package/dist/transport/deadline.js +18 -0
  167. package/dist/transport/failure.d.ts +28 -1
  168. package/dist/transport/failure.js +63 -1
  169. package/dist/transport/http/admission.js +8 -35
  170. package/dist/transport/http/binary-request.d.ts +44 -0
  171. package/dist/transport/http/binary-request.js +110 -0
  172. package/dist/transport/http/binary.js +3 -3
  173. package/dist/transport/http/byte-stream.d.ts +2 -1
  174. package/dist/transport/http/byte-stream.js +12 -8
  175. package/dist/transport/http/configuration.d.ts +7 -0
  176. package/dist/transport/http/configuration.js +2 -0
  177. package/dist/transport/http/index.d.ts +1 -1
  178. package/dist/transport/http/index.js +1 -1
  179. package/dist/transport/http/response.d.ts +13 -1
  180. package/dist/transport/http/response.js +22 -9
  181. package/dist/transport/http/stream.d.ts +8 -13
  182. package/dist/transport/http/stream.js +34 -81
  183. package/dist/transport/http/transport.d.ts +14 -1
  184. package/dist/transport/http/transport.js +110 -35
  185. package/dist/transport/http/unprocessed.d.ts +5 -0
  186. package/dist/transport/http/unprocessed.js +28 -0
  187. package/dist/transport/index.d.ts +2 -1
  188. package/dist/transport/index.js +2 -1
  189. package/dist/transport/level.d.ts +42 -0
  190. package/dist/transport/level.js +58 -0
  191. package/dist/transport/stream.d.ts +52 -0
  192. package/dist/transport/stream.js +101 -0
  193. package/dist/transport/transport.d.ts +33 -0
  194. package/dist/transport/unary.d.ts +9 -0
  195. package/dist/transport/unary.js +36 -0
  196. package/dist/transport/websocket/connection.d.ts +13 -4
  197. package/dist/transport/websocket/connection.js +123 -96
  198. package/dist/transport/websocket/exchange.d.ts +15 -19
  199. package/dist/transport/websocket/exchange.js +87 -183
  200. package/dist/transport/websocket/transport.d.ts +19 -1
  201. package/dist/transport/websocket/transport.js +50 -21
  202. package/package.json +9 -7
  203. package/dist/session/execution.js +0 -34
  204. package/dist/session/views/catalog.d.ts +0 -19
  205. package/dist/session/views/resolve.d.ts +0 -13
  206. package/dist/session/views/resolve.js +0 -21
@@ -1,12 +1,17 @@
1
- import type { NodeId } from '@astrale-os/kernel-core/graph/node';
2
- import type { NodePath, PathLike } from '@astrale-os/kernel-core/path';
1
+ import type { NodePath, NodeTarget, PathLike } from '@astrale-os/kernel-core/path';
3
2
  import type { PolicyRef } from '@astrale-os/kernel-dsl/v1';
4
3
  import type { RequestOptions } from '../transport/index.js';
5
4
  import type { AuthCall } from './auth.js';
6
- export interface PolicyDecisionInput {
5
+ /** Each supplied Node is an ID, Path or DefinitionRef resolved by the Kernel. */
6
+ export type PolicyDecisionInput = {
7
7
  readonly policy: PathLike | PolicyRef;
8
- readonly object: NodeId;
9
- }
8
+ } & ({
9
+ readonly object: NodeTarget;
10
+ readonly input?: never;
11
+ } | {
12
+ readonly input: Readonly<Record<string, NodeTarget>>;
13
+ readonly object?: never;
14
+ });
10
15
  export declare const IDENTITY_CAN_PATH: NodePath;
11
16
  export declare function authorizationApi(call: AuthCall): Readonly<{
12
17
  can: {
@@ -28,7 +28,20 @@ function isBatch(input) {
28
28
  return Array.isArray(input);
29
29
  }
30
30
  function check(input) {
31
- return Object.freeze({ policy: policyPath(input.policy), object: input.object });
31
+ if ('input' in input && 'object' in input) {
32
+ throw new TypeError('Identity.can accepts either object or named input.');
33
+ }
34
+ const policy = policyPath(input.policy);
35
+ return input.input !== undefined
36
+ ? {
37
+ policy,
38
+ input: Object.fromEntries(Object.entries(input.input).map(([name, target]) => [name, objectTarget(target)])),
39
+ }
40
+ : { policy, object: objectTarget(input.object) };
41
+ }
42
+ /** Text is already its wire form; a rich target serializes to its canonical Path. */
43
+ function objectTarget(input) {
44
+ return typeof input === 'string' ? input : Path.target(input).raw;
32
45
  }
33
46
  function policyPath(input) {
34
47
  return input instanceof Path || typeof input === 'string'
@@ -1,4 +1,5 @@
1
1
  export type { AuthApi, AdmissionOptions, FunctionAdmission } from './auth.js';
2
+ export type { RequestOptions } from '../transport/index.js';
2
3
  export { IDENTITY_CAN_PATH, type PolicyDecisionInput } from './authorization.js';
3
4
  export { IDENTITY_MINT_PATH, IDENTITY_IMPERSONATE_PATH, type DelegateOptions, type MintOptions, type ImpersonateOptions, } from './credential.js';
4
5
  export { IDENTITY_WHOAMI_PATH, IDENTITY_WHOIS_PATH, type WhoisInput } from './identity.js';
@@ -1,6 +1,7 @@
1
1
  import { Target } from '@astrale-os/kernel-core/callable';
2
2
  import { Path } from '@astrale-os/kernel-core/path';
3
3
  import * as portable from '@astrale-os/kernel-dsl/value';
4
+ import { ENVELOPE_LIMITS } from '@astrale-os/kernel-protocol/codecs';
4
5
  /** Parent-private Call acceptance shared by Client and Client Session. */
5
6
  export function acceptCall(input) {
6
7
  if (input === null || typeof input !== 'object' || Array.isArray(input)) {
@@ -25,7 +26,8 @@ function admitPath(input) {
25
26
  }
26
27
  function admitInput(input) {
27
28
  try {
28
- return portable.acceptImmutable(input);
29
+ // As deep as an envelope carries it: a Query document reaches its own nesting bound first.
30
+ return portable.acceptImmutable(input, { limits: ENVELOPE_LIMITS });
29
31
  }
30
32
  catch (cause) {
31
33
  throw new TypeError('Client input must be a portable Value.', { cause });
@@ -1,18 +1,49 @@
1
- import type { invocation as protocol } from '@astrale-os/kernel-protocol';
1
+ import type { History, Level } from '@astrale-os/kernel-core/wire';
2
+ import { invocation as protocol } from '@astrale-os/kernel-protocol';
3
+ import type { BinaryBody } from '../transport/http/binary-request.js';
2
4
  import type { Transport } from '../transport/index.js';
3
5
  import type { AuthenticatedClient, RemoteCredential } from './credential.js';
4
6
  import type { Call, ClientDispatchOptions, ClientDispatchOptionsWithBody } from './request.js';
5
7
  import type { BinaryResponse, Result, StreamResult, ValueResult } from './result.js';
8
+ import type { RoutingControl } from './routing-control.js';
6
9
  import { type HttpTransportOptions } from '../transport/http/index.js';
7
- export type ClientOptions = HttpTransportOptions | {
10
+ export type ClientOptions = (HttpTransportOptions | {
8
11
  readonly transport: Transport;
12
+ }) & {
13
+ /**
14
+ * The wire History this Client converts under: it lowers a request for a Host behind it and
15
+ * raises what such a Host answers. Absent, the Invocation History of this build.
16
+ */
17
+ readonly levels?: History;
9
18
  };
10
- /** Package-private redirect continuation path; intentionally absent from the public Client facade. */
11
- export declare function dispatchTrace(client: Client, credential: RemoteCredential | undefined, call: Call, options: ClientDispatchOptions | ClientDispatchOptionsWithBody | undefined, trace: protocol.Trace): Promise<Result>;
19
+ /** One package-private send: the Trace of a redirect continuation and an admitted body, if any. */
20
+ export interface InternalDispatch {
21
+ readonly credential?: RemoteCredential;
22
+ readonly call: Call;
23
+ readonly options?: ClientDispatchOptions;
24
+ readonly trace?: protocol.Trace;
25
+ readonly body?: BinaryBody;
26
+ }
27
+ /**
28
+ * Package-private Session routing path: it presents a redirect continuation's Trace, sends an
29
+ * admitted body, which may be a one-shot stream, and answers routing control as a value.
30
+ * Intentionally absent from the public Client facade. A Client this package did not construct is
31
+ * reached through its public methods, so it never answers routing control.
32
+ */
33
+ export declare function dispatchInternal(client: Client, dispatch: InternalDispatch): Promise<Result | RoutingControl>;
12
34
  /** Pure client for the universal remote Function invocation boundary. */
13
35
  export declare class Client implements AuthenticatedClient {
14
36
  private readonly transport;
15
37
  private readonly lifecycle;
38
+ private readonly levels;
39
+ /** The wire level of the Host, as its last response told it. */
40
+ private heard;
41
+ /**
42
+ * The level of a Host heard below the floor of this build. It is set once and nothing clears it:
43
+ * a call in flight may still be answered by another Host of the fleet, and this Client sends
44
+ * nothing more whatever that answer tells.
45
+ */
46
+ private below;
16
47
  private closed;
17
48
  constructor(options: ClientOptions);
18
49
  dispatch(call: Call, options?: ClientDispatchOptions): Promise<Result>;
@@ -22,6 +53,41 @@ export declare class Client implements AuthenticatedClient {
22
53
  stream(call: Call, options?: ClientDispatchOptions): Promise<StreamResult>;
23
54
  binary(call: Call, options?: ClientDispatchOptions): Promise<BinaryResponse>;
24
55
  as(credential: RemoteCredential): AuthenticatedClient;
56
+ /**
57
+ * The wire level of the Host as its last response told it, an error it answered included, and
58
+ * at most this build's; undefined before any. Below this build's level, the Host does not state
59
+ * what later changes added. A Client that heard a Host below its floor sends nothing more,
60
+ * whatever a later answer makes of this level.
61
+ */
62
+ level(): Level | undefined;
25
63
  close(): void;
64
+ /** The public path: routing control is reported as the error every Client raises for it. */
26
65
  private dispatchWith;
66
+ private dispatchRouted;
67
+ /**
68
+ * Send one envelope written for the level its Host reads. A Transport whose connection fixes a
69
+ * level tells it first, and the request is written for it before anything is sent. Otherwise the
70
+ * level last heard is a hint, the Host judges, and the request is written once more, at most,
71
+ * when a Host below refused it at admission, before any effect, and its level expresses it. A
72
+ * body that cannot be read twice is never sent again. Every entry of the Transport for one call
73
+ * shares one deadline.
74
+ */
75
+ private send;
76
+ /**
77
+ * Take the level a Host told, or the one a connection is fixed at. One below the floor of this
78
+ * build is kept for good: no later answer makes this Client send again.
79
+ */
80
+ private hear;
81
+ /**
82
+ * `envelope` as it is written for its Host: for the level of a connection that fixes one, else for
83
+ * the level last heard. A connection fixed below the floor of this build is given nothing.
84
+ */
85
+ private write;
86
+ /**
87
+ * The options of a later entry of the Transport for one call: what is left of the deadline its
88
+ * first entry started, which is the caller's or, when the caller names none, the Transport's.
89
+ * When nothing is left the request is not given to the Transport: the call timed out, with
90
+ * `cause` as what answered its earlier entry and `delivery` as whether that entry was sent.
91
+ */
92
+ private remaining;
27
93
  }
@@ -1,30 +1,67 @@
1
- import { ClientError } from '../errors/index.js';
2
- import { HttpTransport } from '../transport/http/index.js';
1
+ import { invocation as protocol } from '@astrale-os/kernel-protocol';
2
+ import { ClientError, InexpressibleError, TransportError } from '../errors/index.js';
3
+ import { cancelUnsent } from '../transport/http/binary-request.js';
4
+ import { dispatchBinaryBody, HttpTransport, } from '../transport/http/index.js';
5
+ import { ConnectionMoved, servedOf } from '../transport/level.js';
3
6
  import { authenticated, resolveCredential } from './credential.js';
7
+ import { afterRefusal, belowFloor, connectionLevel, heardFrom, heardFromFailure, levelWithin, raised, written, writtenAt, } from './level.js';
4
8
  import { admitRequest, invocation } from './request.js';
5
- import { requireBinary, requireStream, requireValue } from './result.js';
6
- import { admittedDeploymentRetiredError, admittedMissError, admittedStaleRouteError, } from './routing-control.js';
9
+ import { release, requireBinary, requireStream, requireValue } from './result.js';
10
+ import { isRoutingControl, raisedRoutingControl, routingControlError } from './routing-control.js';
7
11
  const controlledDispatch = new WeakMap();
8
- /** Package-private redirect continuation path; intentionally absent from the public Client facade. */
9
- export function dispatchTrace(client, credential, call, options, trace) {
10
- const dispatch = controlledDispatch.get(client);
11
- if (dispatch === undefined)
12
- throw new TypeError('Trace dispatch requires an owned Client.');
13
- return dispatch(credential, call, options, trace);
12
+ /**
13
+ * Package-private Session routing path: it presents a redirect continuation's Trace, sends an
14
+ * admitted body, which may be a one-shot stream, and answers routing control as a value.
15
+ * Intentionally absent from the public Client facade. A Client this package did not construct is
16
+ * reached through its public methods, so it never answers routing control.
17
+ */
18
+ export function dispatchInternal(client, dispatch) {
19
+ const controlled = controlledDispatch.get(client);
20
+ if (controlled !== undefined)
21
+ return controlled(dispatch);
22
+ if (dispatch.trace !== undefined || dispatch.body !== undefined) {
23
+ throw new TypeError('Internal dispatch requires an owned Client.');
24
+ }
25
+ return dispatchPublic(client, dispatch);
26
+ }
27
+ /**
28
+ * A caller-supplied pool may wrap a real Client, whose public dispatch raises routing control as
29
+ * an error: only the exact error a Client raised answers its control; any other error propagates.
30
+ */
31
+ async function dispatchPublic(client, { credential, call, options }) {
32
+ try {
33
+ return await (credential === undefined ? client : client.as(credential)).dispatch(call, options);
34
+ }
35
+ catch (cause) {
36
+ const control = raisedRoutingControl(cause);
37
+ if (control === undefined)
38
+ throw cause;
39
+ return control;
40
+ }
14
41
  }
15
42
  /** Pure client for the universal remote Function invocation boundary. */
16
43
  export class Client {
17
44
  transport;
18
45
  lifecycle = new AbortController();
46
+ levels;
47
+ /** The wire level of the Host, as its last response told it. */
48
+ heard;
49
+ /**
50
+ * The level of a Host heard below the floor of this build. It is set once and nothing clears it:
51
+ * a call in flight may still be answered by another Host of the fleet, and this Client sends
52
+ * nothing more whatever that answer tells.
53
+ */
54
+ below;
19
55
  closed = false;
20
56
  constructor(options) {
21
57
  if (options === null || typeof options !== 'object') {
22
58
  throw new TypeError('Client options must be an object.');
23
59
  }
60
+ this.levels = options.levels ?? protocol.history;
24
61
  this.transport = Object.hasOwn(options, 'transport')
25
62
  ? admitTransport(options.transport)
26
63
  : new HttpTransport(options);
27
- controlledDispatch.set(this, (credential, call, requestOptions, trace) => this.dispatchWith(credential, call, requestOptions, trace));
64
+ controlledDispatch.set(this, (dispatch) => this.dispatchRouted(dispatch.credential, dispatch.call, dispatch.options, dispatch.trace, dispatch.body));
28
65
  }
29
66
  dispatch(call, options) {
30
67
  return this.dispatchWith(undefined, call, options);
@@ -41,6 +78,15 @@ export class Client {
41
78
  as(credential) {
42
79
  return authenticated((bound, request, options) => this.dispatchWith(bound, request, options), credential);
43
80
  }
81
+ /**
82
+ * The wire level of the Host as its last response told it, an error it answered included, and
83
+ * at most this build's; undefined before any. Below this build's level, the Host does not state
84
+ * what later changes added. A Client that heard a Host below its floor sends nothing more,
85
+ * whatever a later answer makes of this level.
86
+ */
87
+ level() {
88
+ return this.heard;
89
+ }
44
90
  close() {
45
91
  if (this.closed)
46
92
  return;
@@ -49,33 +95,155 @@ export class Client {
49
95
  this.lifecycle.abort(failure);
50
96
  this.transport.close?.();
51
97
  }
52
- async dispatchWith(credential, input, options, trace) {
98
+ /** The public path: routing control is reported as the error every Client raises for it. */
99
+ async dispatchWith(credential, input, options) {
100
+ const result = await this.dispatchRouted(credential, input, options);
101
+ if (isRoutingControl(result))
102
+ throw routingControlError(result);
103
+ return result;
104
+ }
105
+ async dispatchRouted(credential, input, options, trace, body) {
53
106
  if (this.closed)
54
107
  throw new ClientError('Client is closed.');
55
- const request = admitRequest(input, options, this.lifecycle.signal);
56
- const resolved = credential === undefined
57
- ? undefined
58
- : await resolveCredential(credential, request.options.signal);
59
- requireRequestLive(request.options.signal);
60
- const envelope = invocation(request, resolved, trace);
108
+ const request = admitRequest(input, options, this.lifecycle.signal, body);
109
+ const levels = this.levels;
61
110
  let result;
62
- if (request.body === undefined) {
63
- result = await this.transport.dispatch(envelope, request.options);
111
+ try {
112
+ const resolved = credential === undefined
113
+ ? undefined
114
+ : await resolveCredential(credential, request.options.signal);
115
+ requireRequestLive(request.options.signal);
116
+ result = await this.send(request, invocation(request, resolved, trace));
117
+ }
118
+ catch (cause) {
119
+ // A stream body this request never started to send is cancelled with it.
120
+ cancelUnsent(request.body);
121
+ throw cause;
122
+ }
123
+ // Each response is read at the level it is written at, never at a remembered one.
124
+ const answer = servedOf(result);
125
+ this.hear(heardFrom(levels, answer));
126
+ if (answer.written < levels.floor) {
127
+ // What the call did stays done. Its answer is not read, and what it handed off is released.
128
+ this.below ??= answer.written;
129
+ await release(result, 'The server answers below the wire floor of this client.');
130
+ throw belowFloor(levels, answer.host, true);
64
131
  }
65
- else {
66
- const transport = this.transport;
67
- if (!(transport instanceof HttpTransport)) {
68
- throw new TypeError('Binary dispatch requires HTTP transport.');
132
+ const level = levelWithin(levels, answer);
133
+ if (level === levels.current || result.kind !== 'value')
134
+ return result;
135
+ const value = raised(levels, level, request.call, result.value);
136
+ return value === result.value ? result : Object.freeze({ ...result, value });
137
+ }
138
+ /**
139
+ * Send one envelope written for the level its Host reads. A Transport whose connection fixes a
140
+ * level tells it first, and the request is written for it before anything is sent. Otherwise the
141
+ * level last heard is a hint, the Host judges, and the request is written once more, at most,
142
+ * when a Host below refused it at admission, before any effect, and its level expresses it. A
143
+ * body that cannot be read twice is never sent again. Every entry of the Transport for one call
144
+ * shares one deadline.
145
+ */
146
+ async send(request, envelope) {
147
+ const levels = this.levels;
148
+ // A Host heard below the floor is sent nothing, and its Transport is not asked to connect.
149
+ if (this.below !== undefined)
150
+ throw belowFloor(levels, this.below, false);
151
+ const started = Date.now();
152
+ const told = await this.transport.fixed?.(request.options);
153
+ const fixed = told === undefined ? undefined : connectionLevel(levels, told);
154
+ let sent = this.write(envelope, fixed);
155
+ // Connecting is part of the call, whether or not the connection fixes a level: its first entry
156
+ // runs within what is left of its deadline. A Transport that is asked nothing spent none of it.
157
+ let options = this.transport.fixed === undefined
158
+ ? request.options
159
+ : this.remaining(request.options, started, 'not-sent');
160
+ let moved = false;
161
+ let again = false;
162
+ for (;;) {
163
+ try {
164
+ if (request.body === undefined) {
165
+ return await this.transport.dispatch(sent, options);
166
+ }
167
+ const transport = this.transport;
168
+ if (!(transport instanceof HttpTransport)) {
169
+ throw new TypeError('Binary dispatch requires HTTP transport.');
170
+ }
171
+ return await dispatchBinaryBody(transport, sent, request.body, options);
172
+ }
173
+ catch (cause) {
174
+ if (cause instanceof ConnectionMoved) {
175
+ // The connection was made again at another level after it told its own: nothing was
176
+ // sent. The request is written for the level it is at now, once.
177
+ if (moved) {
178
+ throw TransportError.invocation('The connection changed wire level again before the request was sent.', { phase: 'send', delivery: 'not-sent', cause });
179
+ }
180
+ moved = true;
181
+ sent = this.write(envelope, connectionLevel(levels, cause.level));
182
+ options = this.remaining(request.options, started, 'not-sent', cause);
183
+ }
184
+ else {
185
+ const host = heardFromFailure(levels, cause);
186
+ if (host !== undefined)
187
+ this.hear(host);
188
+ const next = afterRefusal(levels, envelope, sent, cause);
189
+ if (next === undefined)
190
+ throw cause;
191
+ if (next instanceof InexpressibleError)
192
+ throw next;
193
+ if (again || fixed !== undefined || moved || request.body?.kind === 'once')
194
+ throw cause;
195
+ again = true;
196
+ sent = next;
197
+ // The request crossed the wire once and its Host refused it: that refusal is the cause
198
+ // of a call whose deadline leaves no time to send it again.
199
+ options = this.remaining(request.options, started, 'unknown', cause);
200
+ }
69
201
  }
70
- result = await transport.dispatchBinary(envelope, request.body, request.options);
71
202
  }
72
- if (result.kind === 'stale')
73
- throw admittedStaleRouteError(result.current, result.invocation);
74
- if (result.kind === 'miss')
75
- throw admittedMissError(result.invocation);
76
- if (result.kind === 'deployment-retired')
77
- throw admittedDeploymentRetiredError(result);
78
- return result;
203
+ }
204
+ /**
205
+ * Take the level a Host told, or the one a connection is fixed at. One below the floor of this
206
+ * build is kept for good: no later answer makes this Client send again.
207
+ */
208
+ hear(level) {
209
+ this.heard = level;
210
+ if (level < this.levels.floor)
211
+ this.below ??= level;
212
+ }
213
+ /**
214
+ * `envelope` as it is written for its Host: for the level of a connection that fixes one, else for
215
+ * the level last heard. A connection fixed below the floor of this build is given nothing.
216
+ */
217
+ write(envelope, fixed) {
218
+ const levels = this.levels;
219
+ if (fixed === undefined)
220
+ return written(levels, this.heard, envelope);
221
+ this.hear(fixed);
222
+ if (fixed < levels.floor)
223
+ throw belowFloor(levels, fixed, false);
224
+ return writtenAt(levels, fixed, envelope);
225
+ }
226
+ /**
227
+ * The options of a later entry of the Transport for one call: what is left of the deadline its
228
+ * first entry started, which is the caller's or, when the caller names none, the Transport's.
229
+ * When nothing is left the request is not given to the Transport: the call timed out, with
230
+ * `cause` as what answered its earlier entry and `delivery` as whether that entry was sent.
231
+ */
232
+ remaining(options, started, delivery, cause) {
233
+ const deadline = options.timeoutMs ?? this.transport.timeoutMs;
234
+ if (deadline === undefined)
235
+ return options;
236
+ const elapsed = Date.now() - started;
237
+ if (elapsed <= 0)
238
+ return options;
239
+ if (elapsed >= deadline) {
240
+ throw TransportError.invocation(`request timed out after ${deadline}ms.`, {
241
+ phase: 'timeout',
242
+ delivery,
243
+ ...(cause === undefined ? {} : { cause }),
244
+ });
245
+ }
246
+ return { ...options, timeoutMs: deadline - elapsed };
79
247
  }
80
248
  }
81
249
  function requireRequestLive(signal) {
@@ -98,8 +266,17 @@ function admitTransport(input) {
98
266
  if (close !== undefined && typeof close !== 'function') {
99
267
  throw new TypeError('Client transport close capability is invalid.');
100
268
  }
269
+ const fixed = input.fixed;
270
+ if (fixed !== undefined && typeof fixed !== 'function') {
271
+ throw new TypeError('Client transport fixed capability is invalid.');
272
+ }
273
+ const timeoutMs = input.timeoutMs;
101
274
  return Object.freeze({
102
275
  dispatch: dispatch.bind(input),
276
+ ...(fixed === undefined ? {} : { fixed: fixed.bind(input) }),
277
+ ...(typeof timeoutMs === 'number' && Number.isSafeInteger(timeoutMs) && timeoutMs > 0
278
+ ? { timeoutMs }
279
+ : {}),
103
280
  ...(close === undefined ? {} : { close: close.bind(input) }),
104
281
  });
105
282
  }
@@ -1,6 +1,6 @@
1
- export { Client, dispatchTrace, type ClientOptions } from './client.js';
1
+ export { Client, dispatchInternal, type ClientOptions, type InternalDispatch } from './client.js';
2
2
  export { acceptCall } from './admission.js';
3
3
  export type { AuthenticatedClient, RemoteCredential } from './credential.js';
4
4
  export { call, type Call, type ClientDispatchOptions, type ClientDispatchOptionsWithBody, } from './request.js';
5
- export type { Binary, BinaryResponse, Input, RedirectResult, Result, Stream, StreamResult, ValueResult, } from './result.js';
6
- export { isAdmittedDeploymentRetiredError, isAdmittedMissError, isAdmittedStaleRouteError, } from './routing-control.js';
5
+ export type { Binary, BinaryResponse, ExecutionResult, Input, RedirectResult, Result, Stream, StreamResult, ValueResult, } from './result.js';
6
+ export { isRoutingControl, routingControlError, type RoutingControl } from './routing-control.js';
@@ -1,4 +1,4 @@
1
- export { Client, dispatchTrace } from './client.js';
1
+ export { Client, dispatchInternal } from './client.js';
2
2
  export { acceptCall } from './admission.js';
3
3
  export { call, } from './request.js';
4
- export { isAdmittedDeploymentRetiredError, isAdmittedMissError, isAdmittedStaleRouteError, } from './routing-control.js';
4
+ export { isRoutingControl, routingControlError } from './routing-control.js';
@@ -0,0 +1,58 @@
1
+ import type { Value } from '@astrale-os/kernel-dsl/value';
2
+ import type * as protocol from '@astrale-os/kernel-protocol/invocation';
3
+ import { type History, type Level } from '@astrale-os/kernel-core/wire';
4
+ import type { TransportRequest } from '../transport/index.js';
5
+ import { InexpressibleError, ProtocolError } from '../errors/index.js';
6
+ import { type Served } from '../transport/level.js';
7
+ /**
8
+ * The request as it is sent to a Host last heard at `heard`: written for that level, and stating
9
+ * it, when the level reads the call differently; the same request otherwise, which states this
10
+ * build's level and so asks the Host again for its own. A call that level cannot express is sent as
11
+ * this build writes it: a level heard earlier is no proof of what the Host reads now, and a Host
12
+ * below refuses the call before any effect.
13
+ */
14
+ export declare function written(levels: History, heard: Level | undefined, request: TransportRequest): TransportRequest;
15
+ /**
16
+ * The request as it is sent on a connection fixed at `level`: written for that level and stating
17
+ * it. Throws `InexpressibleError`, before anything is sent, when that level cannot express the call.
18
+ */
19
+ export declare function writtenAt(levels: History, level: Level, request: TransportRequest): TransportRequest;
20
+ /**
21
+ * The wire level a Transport tells for its connection, as one this build writes at. A Transport is
22
+ * an injected capability: a value that is no wire level, or a level above this build's, is its
23
+ * defect, and nothing is sent. A level below the floor is a Host this build does not speak with,
24
+ * which its Client refuses (`belowFloor`).
25
+ */
26
+ export declare function connectionLevel(levels: History, told: unknown): Level;
27
+ /**
28
+ * The refusal of a Host below the floor of this build: it reads, and answers, a wire level this
29
+ * build converts neither to nor from.
30
+ */
31
+ export declare function belowFloor(levels: History, host: Level, sent: boolean): ProtocolError;
32
+ /**
33
+ * The wire level one answer is written at, as one this build reads. An answer written above this
34
+ * build's level is the protocol breach of a peer that names a level no request stated.
35
+ */
36
+ export declare function levelWithin(levels: History, answer: Served): Level;
37
+ /**
38
+ * The wire level of the Host that answered, as far as this build writes: what it told, and at most
39
+ * this build's own. A level below the floor is kept as it is: this build sends that Host nothing
40
+ * more.
41
+ */
42
+ export declare function heardFrom(levels: History, answer: Served): Level;
43
+ /**
44
+ * The value of a call's result as this build reads it, raised from the level it is written at. A
45
+ * value that level does not admit is the protocol breach of the Host that wrote it.
46
+ */
47
+ export declare function raised(levels: History, level: Level, call: protocol.Call, value: Value): Value;
48
+ /**
49
+ * What follows once `cause` answered `sent`: when a Host below refused `sent` at admission,
50
+ * `request` written for its level, or the `InexpressibleError`, with that refusal as its cause, when
51
+ * that level cannot express it; `undefined` for every other answer, which a call fails with as it is.
52
+ */
53
+ export declare function afterRefusal(levels: History, request: TransportRequest, sent: TransportRequest, cause: unknown): TransportRequest | InexpressibleError | undefined;
54
+ /**
55
+ * The wire level of the Host a failed call heard: the one the Host's own answer told. A failure
56
+ * that is no answer of a Host, a response that is no Invocation envelope included, tells none.
57
+ */
58
+ export declare function heardFromFailure(levels: History, cause: unknown): Level | undefined;