@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
@@ -0,0 +1,159 @@
1
+ import { contractOf, targetCallable } from '@astrale-os/kernel-core/callable';
2
+ import { acceptLevel, Inexpressible, Unread, } from '@astrale-os/kernel-core/wire';
3
+ import * as errors from '@astrale-os/kernel-protocol/errors';
4
+ import { InexpressibleError, ProtocolError, ResponseError, TransportError, } from '../errors/index.js';
5
+ import { servedOf } from '../transport/level.js';
6
+ /**
7
+ * The wire contract a call's target names: that of the callable it spells, by the function a Host
8
+ * names the contract of the callable it resolved with. A target that spells none, and a callable
9
+ * no change names, are read the same at every level.
10
+ */
11
+ function contractOfTarget(target) {
12
+ const callable = targetCallable(target);
13
+ return callable === undefined ? undefined : contractOf(callable);
14
+ }
15
+ /**
16
+ * The request as it is sent to a Host last heard at `heard`: written for that level, and stating
17
+ * it, when the level reads the call differently; the same request otherwise, which states this
18
+ * build's level and so asks the Host again for its own. A call that level cannot express is sent as
19
+ * this build writes it: a level heard earlier is no proof of what the Host reads now, and a Host
20
+ * below refuses the call before any effect.
21
+ */
22
+ export function written(levels, heard, request) {
23
+ if (heard === undefined || heard === levels.current)
24
+ return request;
25
+ const input = lowered(levels, heard, request);
26
+ if (input instanceof Inexpressible || input === request.call.input)
27
+ return request;
28
+ return rewritten(request, input, heard);
29
+ }
30
+ /**
31
+ * The request as it is sent on a connection fixed at `level`: written for that level and stating
32
+ * it. Throws `InexpressibleError`, before anything is sent, when that level cannot express the call.
33
+ */
34
+ export function writtenAt(levels, level, request) {
35
+ if (level === levels.current)
36
+ return request;
37
+ const input = lowered(levels, level, request);
38
+ if (input instanceof Inexpressible) {
39
+ throw new InexpressibleError(input.change, input.level, level);
40
+ }
41
+ return rewritten(request, input, level);
42
+ }
43
+ /** The input of `request` as `level` reads it: the same input when its target names no contract. */
44
+ function lowered(levels, level, request) {
45
+ const contract = contractOfTarget(request.call.target);
46
+ return contract === undefined
47
+ ? request.call.input
48
+ : levels.request.lower(contract, level, request.call.input);
49
+ }
50
+ function rewritten(request, input, level) {
51
+ return Object.freeze({
52
+ ...request,
53
+ call: Object.freeze({ target: request.call.target, input }),
54
+ level,
55
+ });
56
+ }
57
+ /**
58
+ * The wire level a Transport tells for its connection, as one this build writes at. A Transport is
59
+ * an injected capability: a value that is no wire level, or a level above this build's, is its
60
+ * defect, and nothing is sent. A level below the floor is a Host this build does not speak with,
61
+ * which its Client refuses (`belowFloor`).
62
+ */
63
+ export function connectionLevel(levels, told) {
64
+ let level;
65
+ try {
66
+ level = acceptLevel(told);
67
+ }
68
+ catch (cause) {
69
+ throw TransportError.invocation('The transport told a connection level that is no wire level.', {
70
+ phase: 'connect',
71
+ delivery: 'not-sent',
72
+ cause,
73
+ });
74
+ }
75
+ if (level > levels.current) {
76
+ throw TransportError.invocation(`The transport told a connection at wire level ${level}; this client writes levels ` +
77
+ `${levels.floor} to ${levels.current}.`, { phase: 'connect', delivery: 'not-sent' });
78
+ }
79
+ return level;
80
+ }
81
+ /**
82
+ * The refusal of a Host below the floor of this build: it reads, and answers, a wire level this
83
+ * build converts neither to nor from.
84
+ */
85
+ export function belowFloor(levels, host, sent) {
86
+ return new ProtocolError(`server speaks wire level ${host}; this client reads levels ${levels.floor} to ${levels.current}` +
87
+ (sent ? '.' : ', and sent nothing.'));
88
+ }
89
+ /**
90
+ * The wire level one answer is written at, as one this build reads. An answer written above this
91
+ * build's level is the protocol breach of a peer that names a level no request stated.
92
+ */
93
+ export function levelWithin(levels, answer) {
94
+ if (answer.written > levels.current) {
95
+ throw new ProtocolError(`server answers at wire level ${answer.written}; this client reads levels ${levels.floor} to ${levels.current}.`);
96
+ }
97
+ return answer.written;
98
+ }
99
+ /**
100
+ * The wire level of the Host that answered, as far as this build writes: what it told, and at most
101
+ * this build's own. A level below the floor is kept as it is: this build sends that Host nothing
102
+ * more.
103
+ */
104
+ export function heardFrom(levels, answer) {
105
+ return answer.host > levels.current ? levels.current : answer.host;
106
+ }
107
+ /**
108
+ * The value of a call's result as this build reads it, raised from the level it is written at. A
109
+ * value that level does not admit is the protocol breach of the Host that wrote it.
110
+ */
111
+ export function raised(levels, level, call, value) {
112
+ const contract = contractOfTarget(call.target);
113
+ if (contract === undefined)
114
+ return value;
115
+ const read = levels.response.raise(contract, level, value);
116
+ if (read instanceof Unread) {
117
+ throw new ProtocolError(`server answered a value its wire level ${read.level} does not admit (${read.change}).`);
118
+ }
119
+ return read;
120
+ }
121
+ /**
122
+ * The level a Host below read `sent` at, when it refused it at admission: the Host answered that the
123
+ * input is invalid, at a level below the one `sent` is written at, whose shape does not admit it. A
124
+ * Host admits exactly the shape of its level before it runs anything, so nothing ran.
125
+ */
126
+ function unreadAt(levels, sent, cause) {
127
+ if (!(cause instanceof ResponseError) || cause.code !== errors.CODES.VALIDATION_ERROR) {
128
+ return undefined;
129
+ }
130
+ const level = servedOf(cause).written;
131
+ if (level >= (sent.level ?? levels.current) || level < levels.floor)
132
+ return undefined;
133
+ const contract = contractOfTarget(sent.call.target);
134
+ if (contract === undefined || levels.request.reads(contract, level, sent.call.input)) {
135
+ return undefined;
136
+ }
137
+ return level;
138
+ }
139
+ /**
140
+ * What follows once `cause` answered `sent`: when a Host below refused `sent` at admission,
141
+ * `request` written for its level, or the `InexpressibleError`, with that refusal as its cause, when
142
+ * that level cannot express it; `undefined` for every other answer, which a call fails with as it is.
143
+ */
144
+ export function afterRefusal(levels, request, sent, cause) {
145
+ const level = unreadAt(levels, sent, cause);
146
+ if (level === undefined)
147
+ return undefined;
148
+ const input = lowered(levels, level, request);
149
+ return input instanceof Inexpressible
150
+ ? new InexpressibleError(input.change, input.level, level, { cause })
151
+ : rewritten(request, input, level);
152
+ }
153
+ /**
154
+ * The wire level of the Host a failed call heard: the one the Host's own answer told. A failure
155
+ * that is no answer of a Host, a response that is no Invocation envelope included, tells none.
156
+ */
157
+ export function heardFromFailure(levels, cause) {
158
+ return cause instanceof ResponseError ? heardFrom(levels, servedOf(cause)) : undefined;
159
+ }
@@ -1,6 +1,7 @@
1
1
  import type { Target } from '@astrale-os/kernel-core/callable';
2
2
  import type { PathLike } from '@astrale-os/kernel-core/path';
3
3
  import type * as protocol from '@astrale-os/kernel-protocol/invocation';
4
+ import type { BinaryBody } from '../transport/http/binary-request.js';
4
5
  import type { RequestOptions, TransportRequest } from '../transport/index.js';
5
6
  import type { Input } from './result.js';
6
7
  /** One portable call before Path canonicalization, correlation, and credential binding. */
@@ -27,8 +28,12 @@ export interface AdmittedRequest {
27
28
  readonly schema?: protocol.SchemaTarget;
28
29
  readonly maintenance?: protocol.Maintenance;
29
30
  readonly options: RequestOptions;
30
- readonly body?: Blob;
31
+ readonly body?: BinaryBody;
31
32
  }
32
33
  export declare function call(target: PathLike | Target, input: Input): Call;
33
- export declare function admitRequest(input: Call, options: ClientDispatchOptions | ClientDispatchOptionsWithBody | undefined, lifecycle: AbortSignal): AdmittedRequest;
34
+ /**
35
+ * Admit one request. A public caller's body is a Blob in its options; the package-private path
36
+ * passes a body its owner already admitted, which may be a stream that only Session routing sends.
37
+ */
38
+ export declare function admitRequest(input: Call, options: ClientDispatchOptions | ClientDispatchOptionsWithBody | undefined, lifecycle: AbortSignal, admittedBody?: BinaryBody): AdmittedRequest;
34
39
  export declare function invocation(request: AdmittedRequest, credential?: string, trace?: protocol.Trace): TransportRequest;
@@ -1,11 +1,17 @@
1
1
  import * as invocationProtocol from '@astrale-os/kernel-protocol/invocation';
2
+ import { replayableBody } from '../transport/http/binary-request.js';
2
3
  import { acceptCall } from './admission.js';
3
4
  export function call(target, input) {
4
5
  return Object.freeze({ target, input });
5
6
  }
6
- export function admitRequest(input, options, lifecycle) {
7
+ /**
8
+ * Admit one request. A public caller's body is a Blob in its options; the package-private path
9
+ * passes a body its owner already admitted, which may be a stream that only Session routing sends.
10
+ */
11
+ export function admitRequest(input, options, lifecycle, admittedBody) {
7
12
  const call = acceptCall(input);
8
13
  const admitted = admitRequestOptions(options);
14
+ const body = admittedBody ?? admitted.body;
9
15
  const admittedOptions = lifecycleOptions(admitted.options, lifecycle);
10
16
  const delegate = admitDelegate(options?.delegate);
11
17
  const routing = admitRouting(options?.routing);
@@ -17,7 +23,7 @@ export function admitRequest(input, options, lifecycle) {
17
23
  ...(routing === undefined ? {} : { routing }),
18
24
  ...(schema === undefined ? {} : { schema }),
19
25
  ...(maintenance === undefined ? {} : { maintenance }),
20
- ...(admitted.body === undefined ? {} : { body: admitted.body }),
26
+ ...(body === undefined ? {} : { body }),
21
27
  options: admittedOptions,
22
28
  });
23
29
  }
@@ -71,14 +77,12 @@ function admitRequestOptions(input) {
71
77
  input.timeoutMs > 2_147_483_647)) {
72
78
  throw new TypeError('Client request timeoutMs must be a positive safe platform integer.');
73
79
  }
74
- const hasBody = Object.hasOwn(input, 'body');
75
80
  let body;
76
- if (hasBody) {
81
+ if (Object.hasOwn(input, 'body')) {
77
82
  const candidate = input.body;
78
- if (!(candidate instanceof Blob)) {
83
+ if (!(candidate instanceof Blob))
79
84
  throw new TypeError('Binary input must be a Blob or File.');
80
- }
81
- body = candidate;
85
+ body = replayableBody(candidate);
82
86
  }
83
87
  if (body !== undefined && input.idempotencyKey !== undefined) {
84
88
  throw new TypeError('Binary requests cannot carry an idempotency key.');
@@ -23,13 +23,26 @@ export type StreamResult = Correlated<Extract<Callable, {
23
23
  export type BinaryResponse = Correlated<Extract<Callable, {
24
24
  readonly kind: 'binary';
25
25
  }>>;
26
+ /**
27
+ * A durable call's answer. `replayed` is absent when the Host does not say whether the call
28
+ * replayed its Execution.
29
+ */
30
+ export type ExecutionResult = Correlated<Omit<Extract<Callable, {
31
+ readonly kind: 'execution';
32
+ }>, 'replayed'> & {
33
+ readonly replayed?: boolean;
34
+ }>;
26
35
  export interface RedirectResult {
27
36
  readonly kind: 'redirect';
28
37
  readonly redirect: protocol.Redirection;
29
38
  readonly invocation: protocol.InvocationId;
30
39
  }
31
- export type Result = ValueResult | StreamResult | BinaryResponse | RedirectResult;
40
+ export type Result = ValueResult | StreamResult | BinaryResponse | ExecutionResult | RedirectResult;
32
41
  export declare function requireValue(result: Promise<Result>): Promise<ValueResult>;
33
42
  export declare function requireStream(result: Promise<Result>): Promise<StreamResult>;
34
43
  export declare function requireBinary(result: Promise<Result>): Promise<BinaryResponse>;
44
+ /** Release what a result its caller will not receive handed off: its stream, or its binary body. */
45
+ export declare function release(result: {
46
+ readonly kind: string;
47
+ }, reason: string): Promise<void>;
35
48
  export {};
@@ -8,7 +8,7 @@ export async function requireValue(result) {
8
8
  export async function requireStream(result) {
9
9
  const settled = await result;
10
10
  if (settled.kind !== 'stream')
11
- mismatch('stream', settled.kind);
11
+ mismatch('stream', settled);
12
12
  return settled;
13
13
  }
14
14
  export async function requireBinary(result) {
@@ -17,15 +17,26 @@ export async function requireBinary(result) {
17
17
  return rejectMismatch('binary', settled);
18
18
  return settled;
19
19
  }
20
- function mismatch(expected, received) {
21
- throw new ClientError(`Function returned ${received}; ${expected} was required.`);
20
+ function mismatch(expected, result) {
21
+ throw new ClientError(`${returned(result)}; ${expected} was required.`);
22
+ }
23
+ /** What the call did; a started Execution is named so its caller never starts it again. */
24
+ function returned(result) {
25
+ return result.kind === 'execution'
26
+ ? `Function started Execution ${result.execution}`
27
+ : `Function returned ${result.kind}`;
22
28
  }
23
29
  async function rejectMismatch(expected, result) {
24
- if (result.kind === 'stream') {
25
- await result.stream.cancel(`Expected ${expected} Function result.`).catch(() => undefined);
30
+ await release(result, `Expected ${expected} Function result.`);
31
+ mismatch(expected, result);
32
+ }
33
+ /** Release what a result its caller will not receive handed off: its stream, or its binary body. */
34
+ export async function release(result, reason) {
35
+ const held = result;
36
+ if (held.kind === 'stream') {
37
+ await held.stream.cancel(reason).catch(() => undefined);
26
38
  }
27
- if (result.kind === 'binary' && !(result.value.body instanceof Uint8Array)) {
28
- await result.value.body[Symbol.asyncIterator]().return?.();
39
+ if (held.kind === 'binary' && !(held.value.body instanceof Uint8Array)) {
40
+ await held.value.body[Symbol.asyncIterator]().return?.();
29
41
  }
30
- mismatch(expected, result.kind);
31
42
  }
@@ -1,18 +1,14 @@
1
- import type { invocation as protocol, release } from '@astrale-os/kernel-protocol';
2
- import type { DeploymentRetired } from '../transport/index.js';
1
+ import type { DeploymentRetired, MissControl, StaleControl } from '../transport/index.js';
2
+ import type { Result } from './result.js';
3
3
  import { MissError, ResponseError, StaleRouteError } from '../errors/index.js';
4
- /** Create a private routing witness only after exact reserved-control decoding. */
5
- export declare function admittedMissError(invocation: protocol.InvocationId): MissError;
6
- /** Create a private routing witness only after exact reserved-control decoding. */
7
- export declare function admittedStaleRouteError(current: release.Sha256Digest, invocation: protocol.InvocationId): StaleRouteError;
4
+ /** A reserved pre-handler control the Transport decoded in place of a Function result. */
5
+ export type RoutingControl = StaleControl | MissControl | DeploymentRetired;
6
+ /** Whether a package-private dispatch answered with routing control rather than a result. */
7
+ export declare function isRoutingControl(input: Result | RoutingControl): input is RoutingControl;
8
8
  /**
9
- * Raise one decoded retirement control as the typed error every Client reports for it,
10
- * `ResponseError(3002, DEPLOYMENT_RETIRED)`, privately witnessed for Session recovery.
9
+ * The public error every Client reports for one routing control; a retirement is
10
+ * `ResponseError(3002, DEPLOYMENT_RETIRED)`.
11
11
  */
12
- export declare function admittedDeploymentRetiredError(control: DeploymentRetired): ResponseError;
13
- /** Distinguish decoded routing control from public or caller-supplied lookalike errors. */
14
- export declare function isAdmittedMissError(input: unknown): input is MissError;
15
- /** Distinguish decoded routing control from public or caller-supplied lookalike errors. */
16
- export declare function isAdmittedStaleRouteError(input: unknown): input is StaleRouteError;
17
- /** Distinguish a decoded retirement control from a lookalike `DEPLOYMENT_RETIRED` response. */
18
- export declare function isAdmittedDeploymentRetiredError(input: unknown): input is ResponseError;
12
+ export declare function routingControlError(control: RoutingControl): StaleRouteError | MissError | ResponseError;
13
+ /** The routing control this exact error was raised for; a lookalike error carries none. */
14
+ export declare function raisedRoutingControl(input: unknown): RoutingControl | undefined;
@@ -1,38 +1,30 @@
1
1
  import { CODES } from '@astrale-os/kernel-protocol/errors';
2
2
  import { MissError, ResponseError, StaleRouteError } from '../errors/index.js';
3
- const admittedMisses = new WeakSet();
4
- const admittedStaleRoutes = new WeakSet();
5
- const admittedRetirements = new WeakSet();
6
- /** Create a private routing witness only after exact reserved-control decoding. */
7
- export function admittedMissError(invocation) {
8
- const error = new MissError(invocation);
9
- admittedMisses.add(error);
10
- return error;
11
- }
12
- /** Create a private routing witness only after exact reserved-control decoding. */
13
- export function admittedStaleRouteError(current, invocation) {
14
- const error = new StaleRouteError(current, invocation);
15
- admittedStaleRoutes.add(error);
16
- return error;
3
+ const raised = new WeakMap();
4
+ /** Whether a package-private dispatch answered with routing control rather than a result. */
5
+ export function isRoutingControl(input) {
6
+ return input.kind === 'stale' || input.kind === 'miss' || input.kind === 'deployment-retired';
17
7
  }
18
8
  /**
19
- * Raise one decoded retirement control as the typed error every Client reports for it,
20
- * `ResponseError(3002, DEPLOYMENT_RETIRED)`, privately witnessed for Session recovery.
9
+ * The public error every Client reports for one routing control; a retirement is
10
+ * `ResponseError(3002, DEPLOYMENT_RETIRED)`.
21
11
  */
22
- export function admittedDeploymentRetiredError(control) {
23
- const error = new ResponseError(CODES.NOT_FOUND, control.message, control.invocation, control.reason);
24
- admittedRetirements.add(error);
12
+ export function routingControlError(control) {
13
+ const error = publicError(control);
14
+ raised.set(error, control);
25
15
  return error;
26
16
  }
27
- /** Distinguish decoded routing control from public or caller-supplied lookalike errors. */
28
- export function isAdmittedMissError(input) {
29
- return input instanceof MissError && admittedMisses.has(input);
30
- }
31
- /** Distinguish decoded routing control from public or caller-supplied lookalike errors. */
32
- export function isAdmittedStaleRouteError(input) {
33
- return input instanceof StaleRouteError && admittedStaleRoutes.has(input);
17
+ /** The routing control this exact error was raised for; a lookalike error carries none. */
18
+ export function raisedRoutingControl(input) {
19
+ return typeof input === 'object' && input !== null ? raised.get(input) : undefined;
34
20
  }
35
- /** Distinguish a decoded retirement control from a lookalike `DEPLOYMENT_RETIRED` response. */
36
- export function isAdmittedDeploymentRetiredError(input) {
37
- return input instanceof ResponseError && admittedRetirements.has(input);
21
+ function publicError(control) {
22
+ switch (control.kind) {
23
+ case 'stale':
24
+ return new StaleRouteError(control.current, control.invocation);
25
+ case 'miss':
26
+ return new MissError(control.invocation);
27
+ case 'deployment-retired':
28
+ return new ResponseError(CODES.NOT_FOUND, control.message, control.invocation, control.reason);
29
+ }
38
30
  }
@@ -1,9 +1,12 @@
1
1
  export * from './client-error.js';
2
2
  export * from './exchange-error.js';
3
+ export * from './inexpressible-error.js';
3
4
  export * from './miss-error.js';
4
5
  export * from './protocol-error.js';
6
+ export * from './query-input.js';
5
7
  export * from './refusal.js';
6
8
  export * from './response-error.js';
9
+ export * from './response-too-large-error.js';
7
10
  export * from './session-error.js';
8
11
  export * from './stale-route-error.js';
9
12
  export * from './transport-error.js';
@@ -1,9 +1,12 @@
1
1
  export * from './client-error.js';
2
2
  export * from './exchange-error.js';
3
+ export * from './inexpressible-error.js';
3
4
  export * from './miss-error.js';
4
5
  export * from './protocol-error.js';
6
+ export * from './query-input.js';
5
7
  export * from './refusal.js';
6
8
  export * from './response-error.js';
9
+ export * from './response-too-large-error.js';
7
10
  export * from './session-error.js';
8
11
  export * from './stale-route-error.js';
9
12
  export * from './transport-error.js';
@@ -0,0 +1,23 @@
1
+ import type { Level } from '@astrale-os/kernel-core/wire';
2
+ import { ClientError } from './client-error.js';
3
+ /**
4
+ * A request the Host cannot express: it uses what a wire change above the Host's level introduced.
5
+ * The Client raises it in place of the refusal of a Host below, which is then its `cause`, and
6
+ * before sending on a connection fixed at a lower level. The Host ran nothing either way.
7
+ */
8
+ export declare class InexpressibleError extends ClientError {
9
+ /** The wire change that introduced what the request uses. */
10
+ readonly change: string;
11
+ /** The lowest wire level that expresses the request. */
12
+ readonly requiredLevel: Level;
13
+ /** The wire level the Host reads requests at. */
14
+ readonly hostLevel: Level;
15
+ readonly code: 4002;
16
+ constructor(
17
+ /** The wire change that introduced what the request uses. */
18
+ change: string,
19
+ /** The lowest wire level that expresses the request. */
20
+ requiredLevel: Level,
21
+ /** The wire level the Host reads requests at. */
22
+ hostLevel: Level, options?: ErrorOptions);
23
+ }
@@ -0,0 +1,26 @@
1
+ import * as errors from '@astrale-os/kernel-protocol/errors';
2
+ import { ClientError } from './client-error.js';
3
+ /**
4
+ * A request the Host cannot express: it uses what a wire change above the Host's level introduced.
5
+ * The Client raises it in place of the refusal of a Host below, which is then its `cause`, and
6
+ * before sending on a connection fixed at a lower level. The Host ran nothing either way.
7
+ */
8
+ export class InexpressibleError extends ClientError {
9
+ change;
10
+ requiredLevel;
11
+ hostLevel;
12
+ code = errors.CODES.UNSUPPORTED;
13
+ constructor(
14
+ /** The wire change that introduced what the request uses. */
15
+ change,
16
+ /** The lowest wire level that expresses the request. */
17
+ requiredLevel,
18
+ /** The wire level the Host reads requests at. */
19
+ hostLevel, options) {
20
+ super(`The server reads wire level ${hostLevel} and cannot express this request, which needs level ${requiredLevel} (${change}).`, options);
21
+ this.change = change;
22
+ this.requiredLevel = requiredLevel;
23
+ this.hostLevel = hostLevel;
24
+ this.name = 'InexpressibleError';
25
+ }
26
+ }
@@ -0,0 +1,47 @@
1
+ import type { Refusal } from '@astrale-os/kernel-core/graph/query';
2
+ import * as errors from '@astrale-os/kernel-protocol/errors';
3
+ import { ResponseError } from './response-error.js';
4
+ /**
5
+ * Why the Kernel refused a Query as input (`QUERY_INPUT_INVALID`), by the phase that refused it.
6
+ * Nothing was read, and the same request is refused again.
7
+ */
8
+ export type QueryInputDetails =
9
+ /** A v7 Query the wire refused whole: each fault, where it is and how to fix it (§ Wire v7). */
10
+ {
11
+ readonly phase: 'decode';
12
+ readonly refusals: readonly Readonly<Refusal>[];
13
+ }
14
+ /** A Query its planning refused: the issue, and where in the request when it says. */
15
+ | {
16
+ readonly phase: 'plan';
17
+ readonly issue: string;
18
+ readonly path?: string;
19
+ }
20
+ /** A Query past a limit the Kernel sets for every caller: which, its maximum, what it needs. */
21
+ | {
22
+ readonly phase: 'plan';
23
+ readonly issue: 'QUERY_PLAN_LIMIT_EXCEEDED';
24
+ readonly limit: string;
25
+ readonly maximum: number;
26
+ readonly actual: number;
27
+ readonly path?: string;
28
+ }
29
+ /** An `expected` closure the Kernel cannot read, or a V6 page size: where. */
30
+ | {
31
+ readonly phase: 'input';
32
+ readonly path: string;
33
+ }
34
+ /** A V6 QueryAST the Kernel could not decode: where. */
35
+ | {
36
+ readonly phase: 'decode';
37
+ readonly path: string;
38
+ };
39
+ /** The reason of a Query the Kernel refused as input. */
40
+ export type QueryInputReason = errors.ErrorReason<'QUERY_INPUT_INVALID', QueryInputDetails>;
41
+ /**
42
+ * Whether the Kernel refused a Query as input: nothing was read, and resending it unchanged is
43
+ * refused again. Its `reason.details` then say where and why, typed by phase.
44
+ */
45
+ export declare function isQueryInputInvalid(error: unknown): error is Omit<ResponseError, 'reason'> & {
46
+ readonly reason: QueryInputReason;
47
+ };
@@ -0,0 +1,85 @@
1
+ import { ENVELOPE_POINTER_LENGTH } from '@astrale-os/kernel-protocol/codecs';
2
+ import * as errors from '@astrale-os/kernel-protocol/errors';
3
+ import { ResponseError } from './response-error.js';
4
+ /**
5
+ * Whether the Kernel refused a Query as input: nothing was read, and resending it unchanged is
6
+ * refused again. Its `reason.details` then say where and why, typed by phase.
7
+ */
8
+ export function isQueryInputInvalid(error) {
9
+ return (error instanceof ResponseError &&
10
+ error.code === errors.CODES.VALIDATION_ERROR &&
11
+ error.reason?.code === 'QUERY_INPUT_INVALID' &&
12
+ isQueryInputDetails(error.reason.details));
13
+ }
14
+ const REFUSAL_FIELDS = new Set([
15
+ 'code',
16
+ 'path',
17
+ 'expected',
18
+ 'fix',
19
+ 'didYouMean',
20
+ 'tier',
21
+ ]);
22
+ const ISSUE = /^[A-Z][A-Z0-9_]{0,127}$/u;
23
+ /** The details the Kernel gives a refused Query, each phase with exactly its own fields. */
24
+ function isQueryInputDetails(details) {
25
+ const path = details.path === undefined ? [] : ['path'];
26
+ switch (details.phase) {
27
+ case 'decode':
28
+ return Object.hasOwn(details, 'refusals')
29
+ ? exact(details, ['phase', 'refusals']) &&
30
+ Array.isArray(details.refusals) &&
31
+ details.refusals.length > 0 &&
32
+ details.refusals.every(isRefusal)
33
+ : exact(details, ['phase', 'path']) && isPointer(details.path);
34
+ case 'input':
35
+ return exact(details, ['phase', 'path']) && isPointer(details.path);
36
+ case 'plan':
37
+ if (typeof details.issue !== 'string' ||
38
+ !ISSUE.test(details.issue) ||
39
+ (details.path !== undefined && !isPointer(details.path))) {
40
+ return false;
41
+ }
42
+ if (!Object.hasOwn(details, 'limit'))
43
+ return exact(details, ['phase', 'issue', ...path]);
44
+ return (details.issue === 'QUERY_PLAN_LIMIT_EXCEEDED' &&
45
+ exact(details, ['phase', 'issue', 'limit', 'maximum', 'actual', ...path]) &&
46
+ typeof details.limit === 'string' &&
47
+ details.limit.length > 0 &&
48
+ isCount(details.maximum) &&
49
+ isCount(details.actual) &&
50
+ details.actual > details.maximum);
51
+ default:
52
+ return false;
53
+ }
54
+ }
55
+ /** One refusal of the v7 wire, as the Kernel carries it: `{code, path, expected, fix, …}`. */
56
+ function isRefusal(input) {
57
+ return (isObject(input) &&
58
+ Object.keys(input).every((field) => REFUSAL_FIELDS.has(field)) &&
59
+ typeof input.code === 'string' &&
60
+ /^QUERY_[A-Z_]{1,64}$/u.test(input.code) &&
61
+ isPointer(input.path) &&
62
+ typeof input.expected === 'string' &&
63
+ typeof input.fix === 'string' &&
64
+ (input.didYouMean === undefined ||
65
+ (Array.isArray(input.didYouMean) &&
66
+ input.didYouMean.every((name) => typeof name === 'string'))) &&
67
+ (input.tier === undefined || typeof input.tier === 'string'));
68
+ }
69
+ /** An RFC 6901 pointer into the request, as the Kernel bounds it. */
70
+ function isPointer(input) {
71
+ return (typeof input === 'string' &&
72
+ input.length <= ENVELOPE_POINTER_LENGTH &&
73
+ input.normalize('NFC') === input &&
74
+ (input === '' || input.startsWith('/')));
75
+ }
76
+ function isCount(input) {
77
+ return typeof input === 'number' && Number.isSafeInteger(input) && input >= 0;
78
+ }
79
+ function isObject(input) {
80
+ return input !== null && typeof input === 'object' && !Array.isArray(input);
81
+ }
82
+ function exact(input, fields) {
83
+ return (Object.keys(input).length === fields.length &&
84
+ fields.every((field) => Object.hasOwn(input, field)));
85
+ }
@@ -1,6 +1,11 @@
1
1
  import { ResponseError } from './response-error.js';
2
2
  /** Return the delay a refused invocation prescribes before it is sent again, and nothing else. */
3
3
  export declare function retryAfterOf(error: unknown): number | undefined;
4
+ /**
5
+ * Return the byte bound the answer to an invocation exceeded, whichever side refused it: the
6
+ * client's own `maximumResponseBytes`, or the server's, and nothing else.
7
+ */
8
+ export declare function maximumResponseBytesOf(error: unknown): number | undefined;
4
9
  /** Whether the Instance refused the invocation because a maintenance session admits only root. */
5
10
  export declare function isMaintenance(error: unknown): error is ResponseError;
6
11
  /**
@@ -10,3 +15,10 @@ export declare function isMaintenance(error: unknown): error is ResponseError;
10
15
  export declare function isMaintenanceSessionStale(error: unknown): error is ResponseError;
11
16
  /** Whether a sealed conflict ran nothing because the graph snapshot advanced; resend it at once. */
12
17
  export declare function isSnapshotStale(error: unknown): error is ResponseError;
18
+ /**
19
+ * Whether the Kernel refused a request because the client was built against a Domain meaning this
20
+ * Instance does not serve: the revision a Query or a Mutation expects of a Domain, or the meaning
21
+ * of a callable. Nothing ran; resending it unchanged cannot succeed, so the client is rebuilt or
22
+ * reloaded against what the Instance serves.
23
+ */
24
+ export declare function isExpectationMismatch(error: unknown): error is ResponseError;