@astrale-os/kernel-client 1.0.0-beta.4 → 1.0.0-beta.41

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 (194) hide show
  1. package/dist/auth/index.d.ts +1 -0
  2. package/dist/client/admission.js +3 -1
  3. package/dist/client/client.d.ts +19 -2
  4. package/dist/client/client.js +62 -30
  5. package/dist/client/index.d.ts +3 -3
  6. package/dist/client/index.js +2 -2
  7. package/dist/client/request.d.ts +8 -2
  8. package/dist/client/request.js +28 -7
  9. package/dist/client/result.d.ts +10 -1
  10. package/dist/client/result.js +10 -4
  11. package/dist/client/routing-control.d.ts +11 -15
  12. package/dist/client/routing-control.js +21 -29
  13. package/dist/errors/index.d.ts +3 -0
  14. package/dist/errors/index.js +3 -0
  15. package/dist/errors/query-input.d.ts +47 -0
  16. package/dist/errors/query-input.js +85 -0
  17. package/dist/errors/refusal.d.ts +24 -0
  18. package/dist/errors/refusal.js +78 -0
  19. package/dist/errors/response-too-large-error.d.ts +11 -0
  20. package/dist/errors/response-too-large-error.js +15 -0
  21. package/dist/errors/stale-route-error.d.ts +1 -1
  22. package/dist/errors/stale-route-error.js +1 -1
  23. package/dist/{schema → execution}/admission.d.ts +2 -2
  24. package/dist/{schema → execution}/admission.js +3 -3
  25. package/dist/execution/contracts.d.ts +76 -0
  26. package/dist/execution/contracts.js +1 -0
  27. package/dist/execution/deadline.d.ts +18 -0
  28. package/dist/execution/deadline.js +25 -0
  29. package/dist/execution/execution.d.ts +61 -0
  30. package/dist/execution/execution.js +132 -0
  31. package/dist/execution/index.d.ts +4 -0
  32. package/dist/execution/index.js +3 -0
  33. package/dist/graph/domain/binding.d.ts +2 -2
  34. package/dist/graph/domain/binding.js +24 -17
  35. package/dist/graph/domain/conformance.d.ts +12 -0
  36. package/dist/graph/domain/conformance.js +18 -0
  37. package/dist/graph/domain/index.d.ts +1 -0
  38. package/dist/graph/domain/index.js +1 -1
  39. package/dist/graph/domain/provenance.d.ts +0 -2
  40. package/dist/graph/domain/provenance.js +0 -4
  41. package/dist/graph/graph.d.ts +17 -3
  42. package/dist/graph/graph.js +4 -1
  43. package/dist/graph/index.d.ts +3 -3
  44. package/dist/graph/index.js +1 -1
  45. package/dist/graph/page-contract.d.ts +66 -1
  46. package/dist/graph/provenance.d.ts +4 -5
  47. package/dist/graph/provenance.js +5 -6
  48. package/dist/graph/query.d.ts +10 -4
  49. package/dist/graph/query.js +149 -7
  50. package/dist/graph/query.typecheck.d.ts +79 -0
  51. package/dist/graph/query.typecheck.js +34 -0
  52. package/dist/pacer/index.d.ts +1 -1
  53. package/dist/pacer/index.js +1 -1
  54. package/dist/pacer/retry/index.d.ts +1 -0
  55. package/dist/pacer/retry/index.js +1 -0
  56. package/dist/pacer/retry/refusals.d.ts +7 -0
  57. package/dist/pacer/retry/refusals.js +10 -0
  58. package/dist/pacer/retry/retry.d.ts +2 -0
  59. package/dist/pacer/retry/retry.js +33 -1
  60. package/dist/schema/contracts.d.ts +77 -9
  61. package/dist/schema/index.d.ts +3 -2
  62. package/dist/schema/index.js +1 -1
  63. package/dist/schema/installation.d.ts +76 -0
  64. package/dist/schema/installation.js +82 -0
  65. package/dist/schema/schema.d.ts +22 -13
  66. package/dist/schema/schema.js +67 -48
  67. package/dist/session/admission.js +10 -3
  68. package/dist/session/bound.d.ts +12 -3
  69. package/dist/session/bound.js +66 -20
  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 +1 -1
  80. package/dist/session/context/index.js +1 -1
  81. package/dist/session/context/operation.d.ts +2 -0
  82. package/dist/session/context/operation.js +5 -0
  83. package/dist/session/credential/credentials.js +43 -28
  84. package/dist/session/discovery/bundle.d.ts +9 -4
  85. package/dist/session/discovery/bundle.js +28 -35
  86. package/dist/session/discovery/discovery.d.ts +1 -1
  87. package/dist/session/discovery/discovery.js +8 -11
  88. package/dist/session/discovery/release.js +18 -28
  89. package/dist/session/discovery/snapshot.d.ts +5 -1
  90. package/dist/session/discovery/transport.d.ts +35 -3
  91. package/dist/session/discovery/transport.js +40 -1
  92. package/dist/session/durable.d.ts +13 -0
  93. package/dist/session/durable.js +39 -0
  94. package/dist/session/exchange/exchange.d.ts +3 -2
  95. package/dist/session/exchange/exchange.js +10 -7
  96. package/dist/session/exchange/index.d.ts +1 -1
  97. package/dist/session/exchange/index.js +1 -1
  98. package/dist/session/exchange/presentation.js +28 -10
  99. package/dist/session/exchange/source.d.ts +7 -7
  100. package/dist/session/exchange/source.js +26 -27
  101. package/dist/session/index.d.ts +5 -4
  102. package/dist/session/index.js +0 -1
  103. package/dist/session/options.d.ts +0 -2
  104. package/dist/session/options.js +0 -3
  105. package/dist/session/reference.d.ts +27 -6
  106. package/dist/session/reference.js +18 -3
  107. package/dist/session/retry.d.ts +12 -0
  108. package/dist/session/retry.js +23 -0
  109. package/dist/session/routing/auth.d.ts +9 -3
  110. package/dist/session/routing/auth.js +4 -3
  111. package/dist/session/routing/cache.d.ts +12 -5
  112. package/dist/session/routing/cache.js +56 -48
  113. package/dist/session/routing/dispatch.d.ts +26 -3
  114. package/dist/session/routing/dispatch.js +112 -67
  115. package/dist/session/routing/envelope.d.ts +3 -3
  116. package/dist/session/routing/index.d.ts +1 -1
  117. package/dist/session/routing/legacy/release-route.d.ts +12 -0
  118. package/dist/session/routing/legacy/release-route.js +6 -0
  119. package/dist/session/routing/recovery.d.ts +3 -6
  120. package/dist/session/routing/recovery.js +3 -14
  121. package/dist/session/routing/request.d.ts +6 -1
  122. package/dist/session/routing/request.js +13 -2
  123. package/dist/session/routing/result.d.ts +1 -1
  124. package/dist/session/routing/result.js +5 -1
  125. package/dist/session/routing/store.js +4 -5
  126. package/dist/session/session.d.ts +31 -5
  127. package/dist/session/session.js +67 -20
  128. package/dist/session/session.typecheck.js +20 -6
  129. package/dist/session/views/index.d.ts +2 -2
  130. package/dist/session/views/index.js +2 -2
  131. package/dist/session/views/kernel.d.ts +31 -0
  132. package/dist/session/views/kernel.js +30 -0
  133. package/dist/session/views/legacy/catalog.d.ts +21 -0
  134. package/dist/session/views/{catalog.js → legacy/catalog.js} +7 -31
  135. package/dist/session/views/legacy/index.d.ts +14 -0
  136. package/dist/session/views/legacy/index.js +14 -0
  137. package/dist/session/views/legacy/resolve.d.ts +46 -0
  138. package/dist/session/views/legacy/resolve.js +83 -0
  139. package/dist/session/views/legacy/unplaced.d.ts +22 -0
  140. package/dist/session/views/legacy/unplaced.js +26 -0
  141. package/dist/session/views/read.d.ts +49 -0
  142. package/dist/session/views/read.js +148 -0
  143. package/dist/session/views/view.d.ts +48 -0
  144. package/dist/session/views/view.js +74 -0
  145. package/dist/store/memory.d.ts +1 -3
  146. package/dist/store/memory.js +73 -105
  147. package/dist/store/shape.d.ts +33 -12
  148. package/dist/store/shape.js +125 -16
  149. package/dist/store/store.d.ts +6 -6
  150. package/dist/store/store.js +1 -1
  151. package/dist/transport/auto/decision.d.ts +26 -14
  152. package/dist/transport/auto/decision.js +35 -32
  153. package/dist/transport/auto/fallback.d.ts +1 -1
  154. package/dist/transport/auto/fallback.js +5 -10
  155. package/dist/transport/auto/transport.d.ts +0 -2
  156. package/dist/transport/auto/transport.js +24 -32
  157. package/dist/transport/deadline.d.ts +2 -0
  158. package/dist/transport/deadline.js +18 -0
  159. package/dist/transport/failure.d.ts +28 -1
  160. package/dist/transport/failure.js +63 -1
  161. package/dist/transport/http/admission.js +7 -34
  162. package/dist/transport/http/binary-request.d.ts +44 -0
  163. package/dist/transport/http/binary-request.js +110 -0
  164. package/dist/transport/http/binary.js +3 -3
  165. package/dist/transport/http/byte-stream.d.ts +2 -1
  166. package/dist/transport/http/byte-stream.js +12 -8
  167. package/dist/transport/http/index.d.ts +1 -1
  168. package/dist/transport/http/index.js +1 -1
  169. package/dist/transport/http/response.d.ts +3 -1
  170. package/dist/transport/http/response.js +7 -9
  171. package/dist/transport/http/stream.d.ts +8 -13
  172. package/dist/transport/http/stream.js +34 -81
  173. package/dist/transport/http/transport.d.ts +10 -0
  174. package/dist/transport/http/transport.js +87 -32
  175. package/dist/transport/http/unprocessed.d.ts +5 -0
  176. package/dist/transport/http/unprocessed.js +28 -0
  177. package/dist/transport/index.d.ts +1 -1
  178. package/dist/transport/index.js +1 -1
  179. package/dist/transport/stream.d.ts +52 -0
  180. package/dist/transport/stream.js +101 -0
  181. package/dist/transport/transport.d.ts +10 -0
  182. package/dist/transport/unary.d.ts +8 -0
  183. package/dist/transport/unary.js +34 -0
  184. package/dist/transport/websocket/connection.d.ts +7 -3
  185. package/dist/transport/websocket/connection.js +103 -92
  186. package/dist/transport/websocket/exchange.d.ts +12 -19
  187. package/dist/transport/websocket/exchange.js +86 -184
  188. package/dist/transport/websocket/transport.js +11 -19
  189. package/package.json +9 -7
  190. package/dist/session/execution.d.ts +0 -65
  191. package/dist/session/execution.js +0 -34
  192. package/dist/session/views/catalog.d.ts +0 -19
  193. package/dist/session/views/resolve.d.ts +0 -13
  194. package/dist/session/views/resolve.js +0 -21
@@ -1,6 +1,8 @@
1
1
  import { createAuth } from '../auth/auth.js';
2
2
  import { call as createCall, } from '../client/index.js';
3
+ import { createExecution } from '../execution/index.js';
3
4
  import { createGraph } from '../graph/index.js';
5
+ import { unavailableViewCatalog } from './views/legacy/index.js';
4
6
  /** Capture one Session into the ordinary graph, auth, and invocation Kernel journey. */
5
7
  export function bindSession(source, controls) {
6
8
  if (source === null || typeof source !== 'object')
@@ -17,46 +19,73 @@ export function bindSession(source, controls) {
17
19
  invalid();
18
20
  const uploadSource = capture(contentSource, 'upload');
19
21
  const downloadSource = capture(contentSource, 'download');
20
- const viewsForSource = capture(source, 'viewsFor');
22
+ const viewSource = capture(source, 'view');
23
+ const viewsForSource = source.viewsFor === undefined ? unavailableViewCatalog : capture(source, 'viewsFor');
21
24
  const schemaSource = captureSchema(source.schema);
22
- const dispatch = ((input, options) => dispatchSource(input, requestOptions(options, admittedControls)));
25
+ const dispatch = ((input, options) => dispatchSource(input, mergeOptions(options, admittedControls)));
23
26
  const admit = (input, options) => admitSource(input, mergeOptions(options, admittedControls));
24
- const call = ((input, options) => callSource(input, requestOptions(options, admittedControls)));
25
- const invoke = ((reference, input, options) => invokeSource(reference, input, requestOptions(options, admittedControls)));
26
- const stream = (input, options) => streamSource(input, requestOptions(options, admittedControls));
27
- const binary = (input, options) => binarySource(input, requestOptions(options, admittedControls));
27
+ const call = ((input, options) => callSource(input, mergeOptions(options, admittedControls)));
28
+ const invoke = ((reference, input, options) => invokeSource(reference, input, mergeOptions(options, admittedControls)));
29
+ const stream = (input, options) => streamSource(input, mergeOptions(options, admittedControls));
30
+ const binary = (input, options) => binarySource(input, mergeOptions(options, admittedControls));
28
31
  const upload = (input, options) => uploadSource(input, mergeOptions(options, admittedControls));
29
32
  // Both forms share the bound controls; the source selects the form from its argument.
30
- const download = ((input, options) => downloadSource(input, controlOptions(options, admittedControls)));
33
+ const download = ((input, options) => downloadSource(input, mergeOptions(options, admittedControls)));
31
34
  const content = Object.freeze({ upload, download });
32
- const viewsFor = (domain, options) => viewsForSource(domain, controlOptions(options, admittedControls));
33
- const install = (request, options) => schemaSource.install(request, controlOptions(options, admittedControls));
34
- const uninstall = (request, options) => schemaSource.uninstall(request, controlOptions(options, admittedControls));
35
- const inspect = (origin, options) => schemaSource.inspect(origin, controlOptions(options, admittedControls));
36
- const bundle = (origin, options) => schemaSource.bundle(origin, controlOptions(options, admittedControls));
37
- const dependencies = (origin, options) => schemaSource.dependencies(origin, controlOptions(options, admittedControls));
38
- const installed = (options) => schemaSource.installed(controlOptions(options, admittedControls));
35
+ // Both forms share the bound controls; the source selects the form from its argument.
36
+ const view = ((input, options) => viewSource(input, mergeOptions(options, admittedControls)));
37
+ const viewsFor = (domain, options) => viewsForSource(domain, mergeOptions(options, admittedControls));
38
+ const install = (request, options) => schemaSource.install(request, mergeOptions(options, admittedControls));
39
+ const uninstall = (request, options) => schemaSource.uninstall(request, mergeOptions(options, admittedControls));
40
+ const inspect = (origin, options) => schemaSource.inspect(origin, mergeOptions(options, admittedControls));
41
+ const bundle = (origin, options) => schemaSource.bundle(origin, mergeOptions(options, admittedControls));
42
+ const dependencies = (origin, options) => schemaSource.dependencies(origin, mergeOptions(options, admittedControls));
43
+ const installed = (options) => schemaSource.installed(mergeOptions(options, admittedControls));
44
+ const installs = schemaSource.installation;
45
+ const installation = Object.freeze({
46
+ status: (execution, options) => installs.status(execution, mergeOptions(options, admittedControls)),
47
+ result: (execution, wait, options) => installs.result(execution, wait, mergeOptions(options, admittedControls)),
48
+ watch: (execution, watch, options) => installs.watch(execution, watch, mergeOptions(options, admittedControls)),
49
+ apply: (execution, command, options) => installs.apply(execution, command, mergeOptions(options, admittedControls)),
50
+ abort: (execution, attempt, options) => installs.abort(execution, attempt, mergeOptions(options, admittedControls)),
51
+ cancel: (execution, input, options) => installs.cancel(execution, input, mergeOptions(options, admittedControls)),
52
+ resume: (execution, options) => installs.resume(execution, mergeOptions(options, admittedControls)),
53
+ delete: (execution, options) => installs.delete(execution, mergeOptions(options, admittedControls)),
54
+ });
55
+ const uninstalls = schemaSource.uninstallation;
56
+ const uninstallation = Object.freeze({
57
+ status: (execution, options) => uninstalls.status(execution, mergeOptions(options, admittedControls)),
58
+ result: (execution, wait, options) => uninstalls.result(execution, wait, mergeOptions(options, admittedControls)),
59
+ cancel: (execution, input, options) => uninstalls.cancel(execution, input, mergeOptions(options, admittedControls)),
60
+ resume: (execution, options) => uninstalls.resume(execution, mergeOptions(options, admittedControls)),
61
+ delete: (execution, options) => uninstalls.delete(execution, mergeOptions(options, admittedControls)),
62
+ });
39
63
  const schema = Object.freeze({
40
64
  install,
65
+ installation,
41
66
  uninstall,
67
+ uninstallation,
42
68
  inspect,
43
69
  bundle,
44
70
  dependencies,
45
71
  installed,
46
72
  });
47
73
  const graph = createGraph(call);
74
+ const execution = createExecution(dispatch);
48
75
  const auth = createAuth((target, input, options) => call(createCall(target, input), options), admit);
49
76
  return Object.freeze({
50
77
  ...graph,
51
78
  kernel: source.kernel,
52
79
  auth,
53
80
  schema,
81
+ execution,
54
82
  dispatch,
55
83
  call,
56
84
  invoke,
57
85
  stream,
58
86
  binary,
59
87
  content,
88
+ view,
60
89
  viewsFor,
61
90
  });
62
91
  }
@@ -79,12 +108,6 @@ function admitControls(input) {
79
108
  }
80
109
  return Object.freeze({ signal: input.signal, deadline: input.deadline });
81
110
  }
82
- function requestOptions(options, controls) {
83
- return mergeOptions(options, controls);
84
- }
85
- function controlOptions(options, controls) {
86
- return mergeOptions(options, controls);
87
- }
88
111
  function mergeOptions(options, controls) {
89
112
  if (controls === undefined)
90
113
  return options;
@@ -106,9 +129,32 @@ function capture(source, key) {
106
129
  function captureSchema(input) {
107
130
  if (input === null || typeof input !== 'object')
108
131
  invalid();
132
+ const installation = input.installation;
133
+ if (installation === null || typeof installation !== 'object')
134
+ invalid();
135
+ const uninstallation = input.uninstallation;
136
+ if (uninstallation === null || typeof uninstallation !== 'object')
137
+ invalid();
109
138
  return Object.freeze({
110
139
  install: capture(input, 'install'),
140
+ installation: Object.freeze({
141
+ status: capture(installation, 'status'),
142
+ result: capture(installation, 'result'),
143
+ watch: capture(installation, 'watch'),
144
+ apply: capture(installation, 'apply'),
145
+ abort: capture(installation, 'abort'),
146
+ cancel: capture(installation, 'cancel'),
147
+ resume: capture(installation, 'resume'),
148
+ delete: capture(installation, 'delete'),
149
+ }),
111
150
  uninstall: capture(input, 'uninstall'),
151
+ uninstallation: Object.freeze({
152
+ status: capture(uninstallation, 'status'),
153
+ result: capture(uninstallation, 'result'),
154
+ cancel: capture(uninstallation, 'cancel'),
155
+ resume: capture(uninstallation, 'resume'),
156
+ delete: capture(uninstallation, 'delete'),
157
+ }),
112
158
  inspect: capture(input, 'inspect'),
113
159
  bundle: capture(input, 'bundle'),
114
160
  dependencies: capture(input, 'dependencies'),
@@ -1,12 +1,18 @@
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;
8
10
  readonly property: string;
9
11
  }
12
+ /** The content one upload receipt of this Session's caller names, read back without a holder. */
13
+ export interface ContentReceipt {
14
+ readonly receipt: string;
15
+ }
10
16
  export interface ContentUploadOptions extends SessionControlOptions {
11
17
  readonly mediaType: string;
12
18
  }
@@ -27,9 +33,21 @@ export type ContentDownload = {
27
33
  readonly outcome: 'rejected';
28
34
  readonly code: ContentRejection;
29
35
  };
36
+ /**
37
+ * The bytes one upload stores. A Blob, File, or Uint8Array is read again by every attempt. A stream
38
+ * must yield exactly `length` bytes; the upload consumes it in one attempt, and cancels it if it
39
+ * fails before reading it through its end. A Domain's own binary input has this shape.
40
+ */
41
+ export type ContentSource = Blob | Uint8Array | BinaryRequestBody;
30
42
  export interface ContentApi {
31
- upload(source: Blob | Uint8Array, options: ContentUploadOptions): Promise<ContentUploadResult>;
43
+ upload(source: ContentSource, options: ContentUploadOptions): Promise<ContentUploadResult>;
32
44
  download(location: ContentLocation, options?: SessionControlOptions): Promise<Binary>;
45
+ /**
46
+ * The content an upload receipt names, for the caller it was issued to, before or without any
47
+ * attachment. A receipt another caller received, an expired one, or a Ref is refused
48
+ * (`BLOB_RECEIPT_INVALID`).
49
+ */
50
+ download(receipt: ContentReceipt, options?: SessionControlOptions): Promise<Binary>;
33
51
  /**
34
52
  * One outcome per location, in order, within one time budget for the whole list. Consume each
35
53
  * content body before requesting the next outcome: an unread body is skipped.
@@ -39,7 +57,7 @@ export interface ContentApi {
39
57
  /** What the Session lends the content transfer: its time budgets and its routed dispatch. */
40
58
  export interface ContentDependencies {
41
59
  readonly operation: (options: SessionControlOptions) => SessionOperation;
42
- readonly dispatch: (target: Path, input: Input, operation: SessionOperation, body?: Blob) => Promise<Result>;
60
+ readonly dispatch: (target: Path, input: Input, operation: SessionOperation, body?: BinaryBody) => Promise<Result>;
43
61
  }
44
62
  /** Transfer immutable content over the authenticated Session routing conversation. */
45
63
  export declare function createContentApi(dependencies: ContentDependencies): ContentApi;
@@ -1,16 +1,25 @@
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
- import { acceptBody, acceptControls, acceptLocation, acceptMediaType } from './input.js';
4
+ import { acceptBody, acceptControls, acceptLocation, acceptMediaType, acceptReceipt, isReceipt, } from './input.js';
4
5
  import { retry } from './retry.js';
5
6
  import { DOWNLOAD, UPLOAD, decodeDownload, decodeUploadResult } from './wire.js';
6
7
  /** Transfer immutable content over the authenticated Session routing conversation. */
7
8
  export function createContentApi(dependencies) {
8
9
  async function upload(source, options) {
9
10
  const body = acceptBody(source);
10
- const mediaType = acceptMediaType(options?.mediaType);
11
- const operation = dependencies.operation(acceptControls(options));
12
- const result = await retry(operation, () => dependencies.dispatch(UPLOAD, { mediaType }, operation, body));
13
- return decodeUploadResult(requireResultKind('value', result).value);
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)) {
@@ -19,9 +28,10 @@ export function createContentApi(dependencies) {
19
28
  }
20
29
  return downloadOne(input, options);
21
30
  }
22
- async function downloadOne(location, options) {
31
+ async function downloadOne(source, options) {
32
+ const request = isReceipt(source) ? acceptReceipt(source) : acceptLocation(source);
23
33
  const operation = dependencies.operation(acceptControls(options));
24
- const result = await dependencies.dispatch(DOWNLOAD, acceptLocation(location), operation);
34
+ const result = await dependencies.dispatch(DOWNLOAD, request, operation);
25
35
  return decodeDownload(requireResultKind('binary', result).value);
26
36
  }
27
37
  return Object.freeze({ upload, download });
@@ -0,0 +1,12 @@
1
+ import type { schema } from '@astrale-os/kernel-dsl/v1';
2
+ import type { ContentUploadOptions, ContentUploadResult } from './api.js';
3
+ type FilePosition = schema.values.FilePosition;
4
+ /** What sending a typed input's files needs: one upload per file. */
5
+ export type FileUpload = (source: Blob, options: ContentUploadOptions) => Promise<ContentUploadResult>;
6
+ /**
7
+ * `input` with every Blob or File at one of `positions` uploaded and replaced by its receipt. A
8
+ * file is checked against the constraints its field states before it is uploaded; a value that is
9
+ * not a Blob, a receipt already, is sent as it is. Without a Blob the input is returned unchanged.
10
+ */
11
+ export declare function uploadFiles(positions: readonly FilePosition[], input: unknown, upload: FileUpload, options: Omit<ContentUploadOptions, 'mediaType'>): Promise<unknown>;
12
+ export {};
@@ -0,0 +1,47 @@
1
+ import { locateFiles } from '@astrale-os/kernel-dsl/v1/schema/values';
2
+ import { acceptMediaType } from './input.js';
3
+ /**
4
+ * `input` with every Blob or File at one of `positions` uploaded and replaced by its receipt. A
5
+ * file is checked against the constraints its field states before it is uploaded; a value that is
6
+ * not a Blob, a receipt already, is sent as it is. Without a Blob the input is returned unchanged.
7
+ */
8
+ export async function uploadFiles(positions, input, upload, options) {
9
+ const files = locateFiles(positions, input).filter(({ value }) => value instanceof Blob);
10
+ if (files.length === 0)
11
+ return input;
12
+ let sent = input;
13
+ for (const { pointer, position, value } of files) {
14
+ const blob = value;
15
+ const mediaType = acceptMediaType(blob.type === '' ? 'application/octet-stream' : blob.type);
16
+ if (position.maxBytes !== undefined && blob.size > position.maxBytes) {
17
+ throw new TypeError(`File at ${pointer || '/'} exceeds its ${position.maxBytes} bytes.`);
18
+ }
19
+ if (position.mediaTypes !== undefined && !position.mediaTypes.includes(mediaType)) {
20
+ throw new TypeError(`File at ${pointer || '/'} has a media type its field refuses.`);
21
+ }
22
+ const { receipt } = await upload(blob, { ...options, mediaType });
23
+ sent = replaceAt(sent, tokensOf(pointer), receipt);
24
+ }
25
+ return sent;
26
+ }
27
+ /** A copy of `value` with `replacement` at `tokens`; only the containers on the way are copied. */
28
+ function replaceAt(value, tokens, replacement) {
29
+ if (tokens.length === 0)
30
+ return replacement;
31
+ const [token, ...rest] = tokens;
32
+ if (Array.isArray(value)) {
33
+ const copy = [...value];
34
+ copy[Number(token)] = replaceAt(value[Number(token)], rest, replacement);
35
+ return copy;
36
+ }
37
+ const record = value;
38
+ return { ...record, [token]: replaceAt(record[token], rest, replacement) };
39
+ }
40
+ function tokensOf(pointer) {
41
+ if (pointer === '')
42
+ return [];
43
+ return pointer
44
+ .slice(1)
45
+ .split('/')
46
+ .map((token) => token.replaceAll('~1', '/').replaceAll('~0', '~'));
47
+ }
@@ -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 ContentReceipt, type ContentRejection, type ContentSource, type ContentUploadOptions, type ContentUploadResult, } from './api.js';
@@ -1,9 +1,20 @@
1
+ import type { BinaryBody } from '../../transport/http/binary-request.js';
1
2
  import type { SessionControlOptions } from '../context/index.js';
2
- import type { ContentLocation } from './api.js';
3
+ import type { ContentLocation, ContentReceipt } 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
- export declare function acceptBody(input: unknown): Blob;
7
+ /** Whether a download names a receipt rather than a holder location. */
8
+ export declare function isReceipt(input: ContentLocation | ContentReceipt): input is ContentReceipt;
9
+ /** One receipt download, exactly as the request carries it; the Kernel admits the receipt. */
10
+ export declare function acceptReceipt(input: ContentReceipt): Readonly<{
11
+ receipt: string;
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
+ */
17
+ export declare function acceptBody(input: unknown): BinaryBody;
7
18
  export declare function acceptMediaType(input: unknown): string;
8
19
  /** The caller's controls, with the content transfer's own default time budget. */
9
20
  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,37 @@ export function acceptLocation(input) {
9
10
  }
10
11
  return Object.freeze({ node: NodeId(input.node), property: PropertyKey(input.property) });
11
12
  }
13
+ /** Whether a download names a receipt rather than a holder location. */
14
+ export function isReceipt(input) {
15
+ return input !== null && typeof input === 'object' && Object.hasOwn(input, 'receipt');
16
+ }
17
+ /** One receipt download, exactly as the request carries it; the Kernel admits the receipt. */
18
+ export function acceptReceipt(input) {
19
+ if (Object.keys(input).length !== 1 ||
20
+ typeof input.receipt !== 'string' ||
21
+ input.receipt.length === 0) {
22
+ throw new TypeError('Content receipt is invalid.');
23
+ }
24
+ return Object.freeze({ receipt: input.receipt });
25
+ }
26
+ /**
27
+ * The bytes of one upload: a Blob, an owned snapshot of a Uint8Array, or a byte stream with its
28
+ * exact length, which the upload consumes.
29
+ */
12
30
  export function acceptBody(input) {
13
- if (typeof Blob !== 'undefined' && input instanceof Blob)
14
- return input;
15
31
  if (input instanceof Uint8Array) {
16
32
  // Blob snapshots an ArrayBuffer view; a SharedArrayBuffer view needs an owned copy.
17
33
  const bytes = input.buffer instanceof ArrayBuffer
18
34
  ? input
19
35
  : new Uint8Array(input);
20
- return new Blob([bytes]);
36
+ return replayableBody(new Blob([bytes]));
37
+ }
38
+ try {
39
+ return acceptBinaryBody(input);
40
+ }
41
+ catch (cause) {
42
+ throw new TypeError('Content upload source must be a Blob, File, Uint8Array, or { body: ReadableStream, length }.', { cause });
21
43
  }
22
- throw new TypeError('Content upload source must be a Blob, File, or Uint8Array.');
23
44
  }
24
45
  export function acceptMediaType(input) {
25
46
  if (typeof input !== 'string')
@@ -1,3 +1,7 @@
1
1
  import type { SessionOperation } from '../context/index.js';
2
- /** Repeat one content request that failed before any of its result was delivered. */
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 { runSessionOperation } from '../context/index.js';
4
- const MAXIMUM_ATTEMPTS = 3;
5
- /** Repeat one content request that failed before any of its result was delivered. */
6
- export async function retry(operation, request) {
7
- for (let attempt = 0;; attempt++) {
8
- try {
9
- return await request();
10
- }
11
- catch (cause) {
12
- if (!retryable(cause) || attempt + 1 === MAXIMUM_ATTEMPTS || operation.signal.aborted) {
13
- throw cause;
14
- }
15
- await runSessionOperation(operation, (signal) => delay(200 * 2 ** attempt, signal));
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') ||
@@ -24,18 +29,3 @@ function retryable(cause) {
24
29
  cause.code === CODES.DEADLINE_EXCEEDED ||
25
30
  cause.code === CODES.OUTCOME_UNKNOWN)));
26
31
  }
27
- function delay(ms, signal) {
28
- return new Promise((resolve, reject) => {
29
- if (signal.aborted)
30
- return reject(signal.reason);
31
- const timer = setTimeout(() => {
32
- signal.removeEventListener('abort', abort);
33
- resolve();
34
- }, ms);
35
- const abort = () => {
36
- clearTimeout(timer);
37
- reject(signal.reason);
38
- };
39
- signal.addEventListener('abort', abort, { once: true });
40
- });
41
- }
@@ -1,4 +1,4 @@
1
1
  export { admitControlOptions, type SessionControlOptions } from './control.js';
2
2
  export { DEFAULT_MAXIMUM_BUNDLE_BYTES, DEFAULT_MAXIMUM_RELEASE_BYTES, DEFAULT_MAXIMUM_RESPONSE_BYTES, DEFAULT_MAXIMUM_ROUTES, DEFAULT_TIMEOUT_MS, platformDuration, positive, } from './limits.js';
3
- export { createSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, type SessionOperation, } from './operation.js';
3
+ export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, type SessionOperation, } from './operation.js';
4
4
  export { admitKernel, admitPolicy, allowEndpoint, kernelInvocationEndpoint, type SessionPolicy, } from './policy.js';
@@ -1,4 +1,4 @@
1
1
  export { admitControlOptions } from './control.js';
2
2
  export { DEFAULT_MAXIMUM_BUNDLE_BYTES, DEFAULT_MAXIMUM_RELEASE_BYTES, DEFAULT_MAXIMUM_RESPONSE_BYTES, DEFAULT_MAXIMUM_ROUTES, DEFAULT_TIMEOUT_MS, platformDuration, positive, } from './limits.js';
3
- export { createSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, } from './operation.js';
3
+ export { createSessionOperation, pauseSessionOperation, remainingSessionTime, runSessionOperation, sessionTransportOptions, } from './operation.js';
4
4
  export { admitKernel, admitPolicy, allowEndpoint, kernelInvocationEndpoint, } from './policy.js';
@@ -20,3 +20,5 @@ export declare function sessionTransportOptions(operation: SessionOperation): {
20
20
  export declare function sessionTransportOptions(operation: SessionOperation, idempotencyKey: RequestOptions['idempotencyKey']): RequestOptions;
21
21
  /** Settle independently of a caller capability that ignores its supplied AbortSignal. */
22
22
  export declare function runSessionOperation<Result>(operation: SessionOperation, task: (signal: AbortSignal) => Result | PromiseLike<Result>): Promise<Result>;
23
+ /** Wait `ms` before another attempt, ending early with the operation's interruption. */
24
+ export declare function pauseSessionOperation(operation: SessionOperation, ms: number): Promise<void>;
@@ -1,4 +1,5 @@
1
1
  import { SessionError } from '../../errors/index.js';
2
+ import { pause } from '../../transport/index.js';
2
3
  /** Create one total wall-clock budget for a complete public Client Session operation. */
3
4
  export function createSessionOperation(lifecycle, input, defaultTimeoutMs) {
4
5
  const timeoutMs = input?.timeoutMs ?? defaultTimeoutMs;
@@ -54,6 +55,10 @@ export async function runSessionOperation(operation, task) {
54
55
  operation.signal.removeEventListener('abort', abort);
55
56
  });
56
57
  }
58
+ /** Wait `ms` before another attempt, ending early with the operation's interruption. */
59
+ export function pauseSessionOperation(operation, ms) {
60
+ return runSessionOperation(operation, (signal) => pause(ms, signal));
61
+ }
57
62
  function requireSessionOperation(operation) {
58
63
  if (!operation.signal.aborted)
59
64
  return;