@oatscenter/trellis 0.100.0-rc.1

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 (264) hide show
  1. package/README.md +59 -0
  2. package/auth/authorization/client_context.d.ts +61 -0
  3. package/auth/authorization/client_context.js +266 -0
  4. package/auth/authorization/nats_registry.d.ts +41 -0
  5. package/auth/authorization/nats_registry.js +283 -0
  6. package/auth/authorization/provider_cache.d.ts +90 -0
  7. package/auth/authorization/provider_cache.js +783 -0
  8. package/auth/authorization/refresh.d.ts +46 -0
  9. package/auth/authorization/refresh.js +246 -0
  10. package/auth/authorization/types.d.ts +207 -0
  11. package/auth/authorization/types.js +60 -0
  12. package/auth/authorization_context.d.ts +11 -0
  13. package/auth/authorization_context.js +10 -0
  14. package/auth/browser/flow_types.d.ts +84 -0
  15. package/auth/browser/flow_types.js +70 -0
  16. package/auth/browser/portal.d.ts +21 -0
  17. package/auth/browser/portal.js +307 -0
  18. package/auth/browser/storage.d.ts +26 -0
  19. package/auth/browser/storage.js +224 -0
  20. package/auth/browser.d.ts +11 -0
  21. package/auth/browser.js +10 -0
  22. package/auth/browser_recovery.d.ts +13 -0
  23. package/auth/browser_recovery.js +38 -0
  24. package/auth/device_activation.d.ts +134 -0
  25. package/auth/device_activation.js +376 -0
  26. package/auth/http_error.d.ts +12 -0
  27. package/auth/http_error.js +32 -0
  28. package/auth/keys.d.ts +5 -0
  29. package/auth/keys.js +63 -0
  30. package/auth/mod.d.ts +16 -0
  31. package/auth/mod.js +16 -0
  32. package/auth/proof.d.ts +31 -0
  33. package/auth/proof.js +95 -0
  34. package/auth/protocol_wasm/trellis_protocol_wasm.d.ts +138 -0
  35. package/auth/protocol_wasm/trellis_protocol_wasm.js +605 -0
  36. package/auth/protocol_wasm/trellis_protocol_wasm_bg.wasm +0 -0
  37. package/auth/protocol_wasm/trellis_protocol_wasm_bytes.d.ts +1 -0
  38. package/auth/protocol_wasm/trellis_protocol_wasm_bytes.js +2 -0
  39. package/auth/protocol_wasm.d.ts +179 -0
  40. package/auth/protocol_wasm.js +133 -0
  41. package/auth/session_auth.d.ts +30 -0
  42. package/auth/session_auth.js +77 -0
  43. package/auth/session_proof.d.ts +53 -0
  44. package/auth/session_proof.js +43 -0
  45. package/auth/time.d.ts +6 -0
  46. package/auth/time.js +7 -0
  47. package/auth/trellis_id.d.ts +1 -0
  48. package/auth/trellis_id.js +5 -0
  49. package/auth/utils.d.ts +10 -0
  50. package/auth/utils.js +69 -0
  51. package/auth.d.ts +1 -0
  52. package/auth.js +1 -0
  53. package/caller.d.ts +111 -0
  54. package/caller.js +85 -0
  55. package/client.d.ts +14 -0
  56. package/client.js +1 -0
  57. package/client_connect.d.ts +143 -0
  58. package/client_connect.js +1097 -0
  59. package/codec.d.ts +13 -0
  60. package/codec.js +192 -0
  61. package/connection.d.ts +107 -0
  62. package/connection.js +396 -0
  63. package/device/activation.d.ts +42 -0
  64. package/device/activation.js +319 -0
  65. package/device.d.ts +80 -0
  66. package/device.js +687 -0
  67. package/env.d.ts +2 -0
  68. package/env.js +10 -0
  69. package/errors/AuthError.d.ts +30 -0
  70. package/errors/AuthError.js +34 -0
  71. package/errors/KVError.d.ts +30 -0
  72. package/errors/KVError.js +36 -0
  73. package/errors/OperationLifecycleError.d.ts +110 -0
  74. package/errors/OperationLifecycleError.js +142 -0
  75. package/errors/RemoteError.d.ts +61 -0
  76. package/errors/RemoteError.js +72 -0
  77. package/errors/SchemaValidationError.d.ts +48 -0
  78. package/errors/SchemaValidationError.js +49 -0
  79. package/errors/StoreError.d.ts +21 -0
  80. package/errors/StoreError.js +31 -0
  81. package/errors/TransferError.d.ts +21 -0
  82. package/errors/TransferError.js +31 -0
  83. package/errors/TransportError.d.ts +29 -0
  84. package/errors/TransportError.js +33 -0
  85. package/errors/TrellisError.d.ts +15 -0
  86. package/errors/TrellisError.js +15 -0
  87. package/errors/ValidationError.d.ts +50 -0
  88. package/errors/ValidationError.js +59 -0
  89. package/errors/index.d.ts +254 -0
  90. package/errors/index.js +238 -0
  91. package/generated.d.ts +4 -0
  92. package/generated.js +3 -0
  93. package/generated_support.d.ts +120 -0
  94. package/generated_support.js +359 -0
  95. package/globals.d.ts +9 -0
  96. package/globals.js +16 -0
  97. package/health_transport.d.ts +18 -0
  98. package/health_transport.js +32 -0
  99. package/helpers.d.ts +11 -0
  100. package/helpers.js +53 -0
  101. package/index.d.ts +2 -0
  102. package/index.js +2 -0
  103. package/internal_sdk/generated/apis/auth/mod.d.ts +805 -0
  104. package/internal_sdk/generated/apis/auth/mod.js +958 -0
  105. package/internal_sdk/generated/apis/core/mod.d.ts +79 -0
  106. package/internal_sdk/generated/apis/core/mod.js +93 -0
  107. package/internal_sdk/generated/apis/events/mod.d.ts +224 -0
  108. package/internal_sdk/generated/apis/events/mod.js +313 -0
  109. package/internal_sdk/generated/apis/health/mod.d.ts +106 -0
  110. package/internal_sdk/generated/apis/health/mod.js +132 -0
  111. package/internal_sdk/generated/apis/index.d.ts +7 -0
  112. package/internal_sdk/generated/apis/index.js +8 -0
  113. package/internal_sdk/generated/apis/jobs/mod.d.ts +176 -0
  114. package/internal_sdk/generated/apis/jobs/mod.js +209 -0
  115. package/internal_sdk/generated/apis/state/mod.d.ts +104 -0
  116. package/internal_sdk/generated/apis/state/mod.js +139 -0
  117. package/internal_sdk/generated/index.d.ts +4 -0
  118. package/internal_sdk/generated/index.js +5 -0
  119. package/internal_sdk/generated/package.json +18 -0
  120. package/internal_sdk/generated/participants/cli/mod.d.ts +402 -0
  121. package/internal_sdk/generated/participants/cli/mod.js +460 -0
  122. package/internal_sdk/generated/participants/cli/types.d.ts +2 -0
  123. package/internal_sdk/generated/participants/cli/types.js +3 -0
  124. package/internal_sdk/generated/participants/console/mod.d.ts +489 -0
  125. package/internal_sdk/generated/participants/console/mod.js +579 -0
  126. package/internal_sdk/generated/participants/console/types.d.ts +2 -0
  127. package/internal_sdk/generated/participants/console/types.js +3 -0
  128. package/internal_sdk/generated/participants/eventsRuntime/mod.d.ts +64 -0
  129. package/internal_sdk/generated/participants/eventsRuntime/mod.js +42 -0
  130. package/internal_sdk/generated/participants/eventsRuntime/types.d.ts +2 -0
  131. package/internal_sdk/generated/participants/eventsRuntime/types.js +3 -0
  132. package/internal_sdk/generated/participants/healthRuntime/mod.d.ts +58 -0
  133. package/internal_sdk/generated/participants/healthRuntime/mod.js +36 -0
  134. package/internal_sdk/generated/participants/healthRuntime/types.d.ts +2 -0
  135. package/internal_sdk/generated/participants/healthRuntime/types.js +3 -0
  136. package/internal_sdk/generated/participants/index.d.ts +8 -0
  137. package/internal_sdk/generated/participants/index.js +9 -0
  138. package/internal_sdk/generated/participants/jobsRuntime/mod.d.ts +64 -0
  139. package/internal_sdk/generated/participants/jobsRuntime/mod.js +42 -0
  140. package/internal_sdk/generated/participants/jobsRuntime/types.d.ts +2 -0
  141. package/internal_sdk/generated/participants/jobsRuntime/types.js +3 -0
  142. package/internal_sdk/generated/participants/platform/mod.d.ts +164 -0
  143. package/internal_sdk/generated/participants/platform/mod.js +149 -0
  144. package/internal_sdk/generated/participants/platform/types.d.ts +7 -0
  145. package/internal_sdk/generated/participants/platform/types.js +5 -0
  146. package/internal_sdk/generated/participants/portal/mod.d.ts +131 -0
  147. package/internal_sdk/generated/participants/portal/mod.js +109 -0
  148. package/internal_sdk/generated/participants/portal/types.d.ts +2 -0
  149. package/internal_sdk/generated/participants/portal/types.js +3 -0
  150. package/internal_sdk/generated/types/_internal/p0.d.ts +13511 -0
  151. package/internal_sdk/generated/types/_internal/p0.js +6105 -0
  152. package/internal_sdk/generated/types/index.d.ts +2951 -0
  153. package/internal_sdk/generated/types/index.js +1477 -0
  154. package/jobs.d.ts +309 -0
  155. package/jobs.js +389 -0
  156. package/kv.d.ts +85 -0
  157. package/kv.js +330 -0
  158. package/operations.d.ts +189 -0
  159. package/operations.js +847 -0
  160. package/package.json +73 -0
  161. package/pagination.d.ts +25 -0
  162. package/pagination.js +117 -0
  163. package/participant.d.ts +6 -0
  164. package/participant.js +6 -0
  165. package/participant_runtime/api.d.ts +131 -0
  166. package/participant_runtime/api.js +42 -0
  167. package/participant_runtime/json.d.ts +16 -0
  168. package/participant_runtime/json.js +193 -0
  169. package/participant_runtime/metadata.d.ts +34 -0
  170. package/participant_runtime/metadata.js +4 -0
  171. package/participant_runtime/participant.d.ts +200 -0
  172. package/participant_runtime/participant.js +330 -0
  173. package/participant_runtime/schemas.d.ts +347 -0
  174. package/participant_runtime/schemas.js +211 -0
  175. package/participant_runtime/surface_names.d.ts +16 -0
  176. package/participant_runtime/surface_names.js +34 -0
  177. package/portable.d.ts +30 -0
  178. package/portable.js +14 -0
  179. package/provider.d.ts +69 -0
  180. package/provider.js +68 -0
  181. package/runtime_transport.d.ts +25 -0
  182. package/runtime_transport.js +48 -0
  183. package/service/mod.d.ts +14 -0
  184. package/service/mod.js +13 -0
  185. package/service/outbox_inbox.d.ts +259 -0
  186. package/service/outbox_inbox.js +908 -0
  187. package/service/runtime/bootstrap.d.ts +56 -0
  188. package/service/runtime/bootstrap.js +226 -0
  189. package/service/runtime/core.d.ts +46 -0
  190. package/service/runtime/core.js +1747 -0
  191. package/service/runtime/health.d.ts +97 -0
  192. package/service/runtime/health.js +153 -0
  193. package/service/runtime/health_schemas.d.ts +23 -0
  194. package/service/runtime/health_schemas.js +20 -0
  195. package/service/runtime/internal_jobs/active-job.d.ts +40 -0
  196. package/service/runtime/internal_jobs/active-job.js +91 -0
  197. package/service/runtime/internal_jobs/bindings.d.ts +40 -0
  198. package/service/runtime/internal_jobs/bindings.js +1 -0
  199. package/service/runtime/internal_jobs/cancellation-registry.d.ts +13 -0
  200. package/service/runtime/internal_jobs/cancellation-registry.js +55 -0
  201. package/service/runtime/internal_jobs/heartbeat.d.ts +33 -0
  202. package/service/runtime/internal_jobs/heartbeat.js +65 -0
  203. package/service/runtime/internal_jobs/job-manager.d.ts +126 -0
  204. package/service/runtime/internal_jobs/job-manager.js +821 -0
  205. package/service/runtime/internal_jobs/key-coordinator.d.ts +280 -0
  206. package/service/runtime/internal_jobs/key-coordinator.js +747 -0
  207. package/service/runtime/internal_jobs/projection.d.ts +3 -0
  208. package/service/runtime/internal_jobs/projection.js +42 -0
  209. package/service/runtime/internal_jobs/runtime-worker.d.ts +126 -0
  210. package/service/runtime/internal_jobs/runtime-worker.js +454 -0
  211. package/service/runtime/internal_jobs/types.d.ts +246 -0
  212. package/service/runtime/internal_jobs/types.js +163 -0
  213. package/service/runtime/logger.d.ts +2 -0
  214. package/service/runtime/logger.js +7 -0
  215. package/service/runtime/runtime.d.ts +16 -0
  216. package/service/runtime/runtime.js +1 -0
  217. package/service/runtime/service.d.ts +372 -0
  218. package/service/runtime/service.js +1896 -0
  219. package/service/runtime/transfer/download.d.ts +27 -0
  220. package/service/runtime/transfer/download.js +1 -0
  221. package/service/runtime/transfer/protocol.d.ts +15 -0
  222. package/service/runtime/transfer/protocol.js +46 -0
  223. package/service/runtime/transfer/queue.d.ts +18 -0
  224. package/service/runtime/transfer/queue.js +136 -0
  225. package/service/runtime/transfer/upload.d.ts +40 -0
  226. package/service/runtime/transfer/upload.js +27 -0
  227. package/service/runtime/transfer.d.ts +62 -0
  228. package/service/runtime/transfer.js +763 -0
  229. package/session.d.ts +862 -0
  230. package/session.js +3407 -0
  231. package/store.d.ts +71 -0
  232. package/store.js +412 -0
  233. package/store_entry.d.ts +20 -0
  234. package/store_entry.js +75 -0
  235. package/tasks.d.ts +10 -0
  236. package/tasks.js +28 -0
  237. package/telemetry/carrier.d.ts +8 -0
  238. package/telemetry/carrier.js +20 -0
  239. package/telemetry/core.d.ts +7 -0
  240. package/telemetry/core.js +14 -0
  241. package/telemetry/env.d.ts +2 -0
  242. package/telemetry/env.js +10 -0
  243. package/telemetry/init.d.ts +2 -0
  244. package/telemetry/init.js +7 -0
  245. package/telemetry/metrics.d.ts +81 -0
  246. package/telemetry/metrics.js +267 -0
  247. package/telemetry/mod.d.ts +13 -0
  248. package/telemetry/mod.js +8 -0
  249. package/telemetry/nats.d.ts +6 -0
  250. package/telemetry/nats.js +6 -0
  251. package/telemetry/result.d.ts +1 -0
  252. package/telemetry/result.js +18 -0
  253. package/telemetry/runtime.d.ts +1 -0
  254. package/telemetry/runtime.js +141 -0
  255. package/telemetry/trace.d.ts +4 -0
  256. package/telemetry/trace.js +26 -0
  257. package/telemetry/trellis.d.ts +1 -0
  258. package/telemetry/trellis.js +1 -0
  259. package/telemetry.d.ts +1 -0
  260. package/telemetry.js +1 -0
  261. package/transfer.d.ts +112 -0
  262. package/transfer.js +525 -0
  263. package/transfer_protocol.d.ts +1 -0
  264. package/transfer_protocol.js +13 -0
@@ -0,0 +1,27 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
2
+ import type { Subscription } from "@nats-io/nats-core";
3
+ import type { PermissionAtom } from "../../../participant_runtime/api.ts";
4
+ import type { TypedStore } from "../../../store.ts";
5
+ import type { FileInfo } from "../../../transfer.ts";
6
+ export type DownloadSession = {
7
+ kind: "download";
8
+ subject: string;
9
+ transferId: string;
10
+ sessionKey: string;
11
+ permission: PermissionAtom;
12
+ requiredCapabilities: readonly string[];
13
+ inboxPrefix: string;
14
+ expiresAtMs: number;
15
+ store: TypedStore;
16
+ key: string;
17
+ info: FileInfo;
18
+ subscription: Subscription;
19
+ timeoutId: ReturnType<typeof setTimeout>;
20
+ reader?: ReadableStreamDefaultReader<Uint8Array>;
21
+ pending?: Uint8Array;
22
+ pendingOffset: number;
23
+ nextSeq: number;
24
+ sentBytes: number;
25
+ hasher: ReturnType<typeof sha256.create>;
26
+ cancellation: AbortController;
27
+ };
@@ -0,0 +1 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
@@ -0,0 +1,15 @@
1
+ import { type Result as ResultType } from "@oatscenter/result";
2
+ import { type Msg, type NatsConnection } from "@nats-io/nats-core";
3
+ import { TransferError } from "../../../errors/TransferError.ts";
4
+ import type { StoreInfo } from "../../../store.ts";
5
+ import type { FileInfo } from "../../../transfer.ts";
6
+ export declare const UPLOAD_SUBJECT_PREFIX = "transfer.v1.upload";
7
+ export declare const DOWNLOAD_SUBJECT_PREFIX = "transfer.v1.download";
8
+ export declare const TRANSFER_SEQUENCE_HEADER = "trellis-transfer-seq";
9
+ export declare const TRANSFER_EOF_HEADER = "trellis-transfer-eof";
10
+ export declare const TRANSFER_CONTROL_HEADER = "trellis-transfer-control";
11
+ export declare const DEFAULT_TRANSFER_CHUNK_BYTES: number;
12
+ export declare function fileInfoFromStoreInfo(info: StoreInfo): FileInfo;
13
+ export declare function replyError(msg: Msg, error: TransferError): void;
14
+ export declare function publishError(nc: NatsConnection, subject: string, error: TransferError): void;
15
+ export declare function parseSeq(msg: Msg): ResultType<number, TransferError>;
@@ -0,0 +1,46 @@
1
+ import { Result } from "@oatscenter/result";
2
+ import { headers as natsHeaders, } from "@nats-io/nats-core";
3
+ import { TransferError } from "../../../errors/TransferError.js";
4
+ export const UPLOAD_SUBJECT_PREFIX = "transfer.v1.upload";
5
+ export const DOWNLOAD_SUBJECT_PREFIX = "transfer.v1.download";
6
+ export const TRANSFER_SEQUENCE_HEADER = "trellis-transfer-seq";
7
+ export const TRANSFER_EOF_HEADER = "trellis-transfer-eof";
8
+ export const TRANSFER_CONTROL_HEADER = "trellis-transfer-control";
9
+ export const DEFAULT_TRANSFER_CHUNK_BYTES = 256 * 1024;
10
+ export function fileInfoFromStoreInfo(info) {
11
+ return {
12
+ key: info.key,
13
+ size: info.size,
14
+ updatedAt: info.updatedAt,
15
+ ...(info.digest ? { digest: info.digest } : {}),
16
+ ...(info.contentType ? { contentType: info.contentType } : {}),
17
+ metadata: info.metadata,
18
+ };
19
+ }
20
+ export function replyError(msg, error) {
21
+ const headers = natsHeaders();
22
+ headers.set("status", "error");
23
+ msg.respond(JSON.stringify(error.toSerializable()), { headers });
24
+ }
25
+ export function publishError(nc, subject, error) {
26
+ const headers = natsHeaders();
27
+ headers.set("status", "error");
28
+ nc.publish(subject, JSON.stringify(error.toSerializable()), { headers });
29
+ }
30
+ export function parseSeq(msg) {
31
+ const raw = msg.headers?.get(TRANSFER_SEQUENCE_HEADER);
32
+ if (!raw) {
33
+ return Result.err(new TransferError({
34
+ operation: "transfer",
35
+ context: { reason: "missing_sequence" },
36
+ }));
37
+ }
38
+ const value = Number(raw);
39
+ if (!Number.isInteger(value) || value < 0) {
40
+ return Result.err(new TransferError({
41
+ operation: "transfer",
42
+ context: { reason: "invalid_sequence", raw },
43
+ }));
44
+ }
45
+ return Result.ok(value);
46
+ }
@@ -0,0 +1,18 @@
1
+ export declare function deferred<T>(): {
2
+ promise: Promise<T>;
3
+ resolve(value: T): void;
4
+ };
5
+ export declare class AsyncValueBroadcaster<T> {
6
+ #private;
7
+ push(value: T): void;
8
+ close(): void;
9
+ subscribe(): AsyncIterable<T>;
10
+ }
11
+ export declare class AsyncChunkQueue implements AsyncIterable<Uint8Array> {
12
+ #private;
13
+ push(chunk: Uint8Array): Promise<void>;
14
+ close(): void;
15
+ fail(error: unknown): void;
16
+ next(): Promise<IteratorResult<Uint8Array>>;
17
+ [Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
18
+ }
@@ -0,0 +1,136 @@
1
+ export function deferred() {
2
+ let resolve;
3
+ const promise = new Promise((r) => {
4
+ resolve = r;
5
+ });
6
+ return { promise, resolve };
7
+ }
8
+ class AsyncValueQueue {
9
+ #values = [];
10
+ #resolvers = [];
11
+ #closed = false;
12
+ push(value) {
13
+ if (this.#closed)
14
+ return;
15
+ const resolver = this.#resolvers.shift();
16
+ if (resolver) {
17
+ resolver({ value, done: false });
18
+ }
19
+ else {
20
+ this.#values.push(value);
21
+ }
22
+ }
23
+ close() {
24
+ if (this.#closed)
25
+ return;
26
+ this.#closed = true;
27
+ for (const resolver of this.#resolvers.splice(0)) {
28
+ resolver({ value: undefined, done: true });
29
+ }
30
+ }
31
+ [Symbol.asyncIterator]() {
32
+ return {
33
+ next: async () => {
34
+ const value = this.#values.shift();
35
+ if (value !== undefined)
36
+ return { value, done: false };
37
+ if (this.#closed)
38
+ return { value: undefined, done: true };
39
+ return await new Promise((resolve) => {
40
+ this.#resolvers.push(resolve);
41
+ });
42
+ },
43
+ };
44
+ }
45
+ }
46
+ export class AsyncValueBroadcaster {
47
+ #subscribers = new Set();
48
+ #closed = false;
49
+ push(value) {
50
+ if (this.#closed)
51
+ return;
52
+ for (const subscriber of this.#subscribers)
53
+ subscriber.push(value);
54
+ }
55
+ close() {
56
+ if (this.#closed)
57
+ return;
58
+ this.#closed = true;
59
+ for (const subscriber of this.#subscribers)
60
+ subscriber.close();
61
+ this.#subscribers.clear();
62
+ }
63
+ subscribe() {
64
+ const subscriber = new AsyncValueQueue();
65
+ if (this.#closed) {
66
+ subscriber.close();
67
+ }
68
+ else {
69
+ this.#subscribers.add(subscriber);
70
+ }
71
+ const subscribers = this.#subscribers;
72
+ return {
73
+ async *[Symbol.asyncIterator]() {
74
+ try {
75
+ yield* subscriber;
76
+ }
77
+ finally {
78
+ subscribers.delete(subscriber);
79
+ }
80
+ },
81
+ };
82
+ }
83
+ }
84
+ export class AsyncChunkQueue {
85
+ #values = [];
86
+ #pending = [];
87
+ #closed = false;
88
+ #error;
89
+ async push(chunk) {
90
+ if (this.#closed)
91
+ return;
92
+ const pending = this.#pending.shift();
93
+ if (pending) {
94
+ pending.resolve({ value: chunk, done: false });
95
+ return;
96
+ }
97
+ await new Promise((consumed) => {
98
+ this.#values.push({ chunk, consumed });
99
+ });
100
+ }
101
+ close() {
102
+ if (this.#closed)
103
+ return;
104
+ this.#closed = true;
105
+ for (const pending of this.#pending.splice(0)) {
106
+ pending.resolve({ value: undefined, done: true });
107
+ }
108
+ }
109
+ fail(error) {
110
+ if (this.#closed)
111
+ return;
112
+ this.#error = error;
113
+ this.#closed = true;
114
+ for (const value of this.#values.splice(0))
115
+ value.consumed();
116
+ for (const pending of this.#pending.splice(0))
117
+ pending.reject(error);
118
+ }
119
+ async next() {
120
+ const value = this.#values.shift();
121
+ if (value) {
122
+ value.consumed();
123
+ return { value: value.chunk, done: false };
124
+ }
125
+ if (this.#error)
126
+ throw this.#error;
127
+ if (this.#closed)
128
+ return { value: undefined, done: true };
129
+ return await new Promise((resolve, reject) => {
130
+ this.#pending.push({ resolve, reject });
131
+ });
132
+ }
133
+ [Symbol.asyncIterator]() {
134
+ return { next: () => this.next() };
135
+ }
136
+ }
@@ -0,0 +1,40 @@
1
+ import type { Subscription } from "@nats-io/nats-core";
2
+ import type { PermissionAtom } from "../../../participant_runtime/api.ts";
3
+ import type { RuntimeOperationTransferProgress } from "../../../session.ts";
4
+ import type { StoreError } from "../../../errors/StoreError.ts";
5
+ import type { TransferError } from "../../../errors/TransferError.ts";
6
+ import type { TypedStore } from "../../../store.ts";
7
+ import type { FileInfo } from "../../../transfer.ts";
8
+ import type { AsyncResult } from "@oatscenter/result";
9
+ import type { StoredTransfer } from "../transfer.ts";
10
+ import type { AsyncChunkQueue } from "./queue.ts";
11
+ import { sha256 } from "@noble/hashes/sha256";
12
+ export type UploadSession = {
13
+ kind: "upload";
14
+ subject: string;
15
+ transferId: string;
16
+ sessionKey: string;
17
+ permission: PermissionAtom | undefined;
18
+ requiredCapabilities: readonly string[];
19
+ expiresAtMs: number;
20
+ store: TypedStore;
21
+ key: string;
22
+ maxBytes?: number;
23
+ contentType?: string;
24
+ metadata?: Record<string, string>;
25
+ onProgress?: (progress: RuntimeOperationTransferProgress) => Promise<void> | void;
26
+ onComplete?: (info: FileInfo) => Promise<void> | void;
27
+ onError?: (error: TransferError) => Promise<void> | void;
28
+ onStored?: (stored: StoredTransfer) => Promise<void> | void;
29
+ subscription: Subscription;
30
+ timeoutId: ReturnType<typeof setTimeout>;
31
+ queue: AsyncChunkQueue;
32
+ putPromise: AsyncResult<void, StoreError>;
33
+ cancellation: AbortController;
34
+ committing: boolean;
35
+ nextSeq: number;
36
+ receivedBytes: number;
37
+ hasher: ReturnType<typeof sha256.create>;
38
+ };
39
+ export declare function raceCancellation<T>(promise: Promise<T>, signal: AbortSignal): Promise<T>;
40
+ export declare function effectiveUploadMaxBytes(argsMaxBytes?: number, storeMaxObjectBytes?: number): number | undefined;
@@ -0,0 +1,27 @@
1
+ import { sha256 } from "@noble/hashes/sha256";
2
+ export function raceCancellation(promise, signal) {
3
+ if (signal.aborted)
4
+ return Promise.reject(signal.reason);
5
+ return new Promise((resolve, reject) => {
6
+ const cancel = () => {
7
+ finish();
8
+ reject(signal.reason);
9
+ };
10
+ const finish = () => signal.removeEventListener("abort", cancel);
11
+ signal.addEventListener("abort", cancel, { once: true });
12
+ promise.then((value) => {
13
+ finish();
14
+ resolve(value);
15
+ }, (error) => {
16
+ finish();
17
+ reject(error);
18
+ });
19
+ });
20
+ }
21
+ export function effectiveUploadMaxBytes(argsMaxBytes, storeMaxObjectBytes) {
22
+ if (argsMaxBytes === undefined)
23
+ return storeMaxObjectBytes;
24
+ if (storeMaxObjectBytes === undefined)
25
+ return argsMaxBytes;
26
+ return Math.min(argsMaxBytes, storeMaxObjectBytes);
27
+ }
@@ -0,0 +1,62 @@
1
+ import { AsyncResult, type Result as ResultType } from "@oatscenter/result";
2
+ import { type NatsConnection } from "@nats-io/nats-core";
3
+ import type { PermissionAtom } from "../../participant_runtime/api.ts";
4
+ import { type OperationTransferHandle, type RuntimeOperationTransferProgress, type TrellisAuth } from "../../session.ts";
5
+ import type { StoreError } from "../../errors/StoreError.ts";
6
+ import { TransferError } from "../../errors/TransferError.ts";
7
+ import { TypedStore, TypedStoreEntry } from "../../store.ts";
8
+ import type { FileInfo, ReceiveTransferGrant, SendTransferGrant } from "../../transfer.ts";
9
+ export type TransferStoreHandle = {
10
+ open(): AsyncResult<TypedStore, StoreError>;
11
+ };
12
+ export type InitiateUploadArgs = {
13
+ sessionKey: string;
14
+ permission: PermissionAtom | undefined;
15
+ requiredCapabilities: readonly string[];
16
+ store: string;
17
+ key: string;
18
+ expiresInMs: number;
19
+ maxBytes?: number;
20
+ contentType?: string;
21
+ metadata?: Record<string, string>;
22
+ onProgress?: (progress: RuntimeOperationTransferProgress) => Promise<void> | void;
23
+ onComplete?: (info: FileInfo) => Promise<void> | void;
24
+ onError?: (error: TransferError) => Promise<void> | void;
25
+ onStored?: (stored: StoredTransfer) => Promise<void> | void;
26
+ };
27
+ export type OperationUploadTransfer = {
28
+ grant: SendTransferGrant;
29
+ transfer: OperationTransferHandle;
30
+ };
31
+ export type InitiateDownloadArgs = {
32
+ sessionKey: string;
33
+ permission: PermissionAtom;
34
+ requiredCapabilities?: readonly string[];
35
+ inboxPrefix: string;
36
+ store: string;
37
+ key: string;
38
+ expiresInMs: number;
39
+ };
40
+ type ServiceTransferOpts = {
41
+ name: string;
42
+ nc: NatsConnection;
43
+ auth: TrellisAuth;
44
+ stores: Record<string, TransferStoreHandle>;
45
+ chunkBytes?: number;
46
+ };
47
+ export type StoredTransfer = {
48
+ transferId: string;
49
+ sessionKey: string;
50
+ store: TypedStore;
51
+ entry: TypedStoreEntry;
52
+ info: FileInfo;
53
+ };
54
+ export declare class ServiceTransfer {
55
+ #private;
56
+ constructor(opts: ServiceTransferOpts);
57
+ initiateUpload(args: InitiateUploadArgs): Promise<ResultType<SendTransferGrant, TransferError>>;
58
+ createOperationUpload(args: InitiateUploadArgs): AsyncResult<OperationUploadTransfer, TransferError>;
59
+ initiateDownload(args: InitiateDownloadArgs): Promise<ResultType<ReceiveTransferGrant, TransferError>>;
60
+ stop(): Promise<void>;
61
+ }
62
+ export {};