@lumeweb/sia-testing 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +9 -0
- package/README.md +202 -2
- package/dist/esm/client.d.ts +8 -0
- package/dist/esm/client.js +6 -0
- package/dist/esm/http.d.ts +3 -0
- package/dist/esm/http.js +3 -0
- package/dist/esm/mock.d.ts +8 -0
- package/dist/esm/mock.js +7 -0
- package/dist/esm/model/index.js +509 -0
- package/dist/esm/model/state.js +188 -0
- package/dist/esm/msw.d.ts +2 -0
- package/dist/esm/msw.js +3 -0
- package/dist/esm/node.d.ts +4 -0
- package/dist/esm/node.js +4 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_md.js +169 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/_u64.js +12 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/sha2.js +199 -0
- package/dist/esm/node_modules/.pnpm/@noble_hashes@2.4.0/node_modules/@noble/hashes/utils.js +220 -0
- package/dist/esm/sdk/abort.js +23 -0
- package/dist/esm/sdk/control.js +129 -0
- package/dist/esm/sdk/index.js +454 -0
- package/dist/esm/sdk/link.js +55 -0
- package/dist/esm/sdk/object.js +24 -0
- package/dist/esm/sdk/payload-store.d.ts +9 -0
- package/dist/esm/sdk/payload-store.js +67 -0
- package/dist/esm/sdk/state.js +74 -0
- package/dist/esm/testing/application-client.d.ts +8 -0
- package/dist/esm/testing/application-client.js +295 -0
- package/dist/esm/testing/application-metadata.js +17 -0
- package/dist/esm/testing/application-transport.js +363 -0
- package/dist/esm/testing/auth-client.d.ts +20 -0
- package/dist/esm/testing/auth-client.js +63 -0
- package/dist/esm/testing/authentication.js +48 -0
- package/dist/esm/testing/byte-view.js +28 -0
- package/dist/esm/testing/chunked-files.d.ts +12 -0
- package/dist/esm/testing/chunked-files.js +50 -0
- package/dist/esm/testing/client.d.ts +8 -0
- package/dist/esm/testing/client.js +105 -0
- package/dist/esm/testing/controls.d.ts +44 -0
- package/dist/esm/testing/controls.js +255 -0
- package/dist/esm/testing/diagnostics.js +55 -0
- package/dist/esm/testing/files.d.ts +9 -0
- package/dist/esm/testing/files.js +86 -0
- package/dist/esm/testing/http-body.js +33 -0
- package/dist/esm/testing/http.d.ts +14 -0
- package/dist/esm/testing/http.js +181 -0
- package/dist/esm/testing/live/agent.d.ts +1 -0
- package/dist/esm/testing/live/agent.js +211 -0
- package/dist/esm/testing/live/approval.js +42 -0
- package/dist/esm/testing/live/cleanup.js +74 -0
- package/dist/esm/testing/live/journal.js +180 -0
- package/dist/esm/testing/live/lease.js +56 -0
- package/dist/esm/testing/live/private-files.js +50 -0
- package/dist/esm/testing/live/process.js +208 -0
- package/dist/esm/testing/live/profile.js +214 -0
- package/dist/esm/testing/live/recovery.js +164 -0
- package/dist/esm/testing/live/resource-limits.js +9 -0
- package/dist/esm/testing/live/run.js +390 -0
- package/dist/esm/testing/live/session.js +93 -0
- package/dist/esm/testing/live/types.d.ts +8 -0
- package/dist/esm/testing/mock.d.ts +8 -0
- package/dist/esm/testing/mock.js +1364 -0
- package/dist/esm/testing/msw.d.ts +21 -0
- package/dist/esm/testing/msw.js +61 -0
- package/dist/esm/testing/mutations.d.ts +59 -0
- package/dist/esm/testing/mutations.js +58 -0
- package/dist/esm/testing/node-payload-store.js +353 -0
- package/dist/esm/testing/node.d.ts +40 -0
- package/dist/esm/testing/node.js +148 -0
- package/dist/esm/testing/owner-client.d.ts +9 -0
- package/dist/esm/testing/owner-client.js +101 -0
- package/dist/esm/testing/owner-shares.js +80 -0
- package/dist/esm/testing/packed.d.ts +46 -0
- package/dist/esm/testing/packed.js +326 -0
- package/dist/esm/testing/reader-lifetime.js +40 -0
- package/dist/esm/testing/reply-loss.js +14 -0
- package/dist/esm/testing/runtime.d.ts +7 -0
- package/dist/esm/testing/runtime.js +13 -0
- package/dist/esm/testing/sdk-upload.d.ts +45 -0
- package/dist/esm/testing/sdk-upload.js +198 -0
- package/dist/esm/testing/streaming-source.js +72 -0
- package/dist/esm/testing/transport.d.ts +22 -0
- package/dist/esm/testing/transport.js +238 -0
- package/dist/esm/testing/types.d.ts +276 -0
- package/dist/esm/vitest.d.ts +23 -0
- package/dist/esm/vitest.js +34 -0
- package/docs/live-auth.md +154 -0
- package/docs/live-runs.md +188 -0
- package/docs/mock-contracts.md +647 -0
- package/package.json +91 -3
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
//#region src/testing/live/resource-limits.ts
|
|
2
|
+
/** Metadata and sealed-object bounds let creation reserve its durable acknowledgement before SDK work. */
|
|
3
|
+
const maximumMetadataBytes = 16 * 1024;
|
|
4
|
+
const maximumSealedObjectBytes = 1024 * 1024;
|
|
5
|
+
const mutationRecordBytes = 4096;
|
|
6
|
+
const uploadRecordBytes = 1052672;
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
export { maximumMetadataBytes, maximumSealedObjectBytes, mutationRecordBytes, uploadRecordBytes };
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { boundedByteView } from "../byte-view.js";
|
|
2
|
+
import { profileDocument } from "./profile.js";
|
|
3
|
+
import { readFixture } from "../files.js";
|
|
4
|
+
import { createJournal } from "./journal.js";
|
|
5
|
+
import { acquireJournalLease } from "./lease.js";
|
|
6
|
+
import { closeLiveSession, openLiveSession, sessionInfo, sessionProcess } from "./session.js";
|
|
7
|
+
import { cleanupKnownResources } from "./cleanup.js";
|
|
8
|
+
import { maximumMetadataBytes, maximumSealedObjectBytes, mutationRecordBytes, uploadRecordBytes } from "./resource-limits.js";
|
|
9
|
+
import { randomUUID } from "node:crypto";
|
|
10
|
+
|
|
11
|
+
//#region src/testing/live/run.ts
|
|
12
|
+
/** A live run owns its fixture mutations and journals their outcomes before returning handles. */
|
|
13
|
+
async function withLiveRun(options, use) {
|
|
14
|
+
const document = profileDocument(options.profile);
|
|
15
|
+
const id = randomUUID();
|
|
16
|
+
const lease = await acquireJournalLease(options.journalFile);
|
|
17
|
+
const execution = new AbortController();
|
|
18
|
+
const signal = options.signal ? AbortSignal.any([options.signal, execution.signal]) : execution.signal;
|
|
19
|
+
const timer = setTimeout(() => execution.abort(failure("deadline")), document.limits.deadlineMs);
|
|
20
|
+
const errors = [];
|
|
21
|
+
const unknownCreations = /* @__PURE__ */ new Set();
|
|
22
|
+
const objects = /* @__PURE__ */ new Map();
|
|
23
|
+
const shares = /* @__PURE__ */ new Map();
|
|
24
|
+
const pending = /* @__PURE__ */ new Set();
|
|
25
|
+
let journal;
|
|
26
|
+
let session;
|
|
27
|
+
let credential;
|
|
28
|
+
let run;
|
|
29
|
+
let value;
|
|
30
|
+
let opened = true;
|
|
31
|
+
let reservedBytes = 0;
|
|
32
|
+
let objectAttempts = 0;
|
|
33
|
+
let shareAttempts = 0;
|
|
34
|
+
let logicalUploadedBytes = 0;
|
|
35
|
+
let observedShardBytes = 0;
|
|
36
|
+
let uncertain = false;
|
|
37
|
+
let queue = Promise.resolve();
|
|
38
|
+
const agentOptions = options.agentModule ? { agentModule: options.agentModule } : {};
|
|
39
|
+
function ensureOpen() {
|
|
40
|
+
if (!opened) throw failure("closed");
|
|
41
|
+
signal.throwIfAborted();
|
|
42
|
+
}
|
|
43
|
+
function ownObject(handle) {
|
|
44
|
+
const record = objects.get(handle);
|
|
45
|
+
if (!record) throw failure("foreign-handle");
|
|
46
|
+
return record;
|
|
47
|
+
}
|
|
48
|
+
function ownShare(handle) {
|
|
49
|
+
const record = shares.get(handle);
|
|
50
|
+
if (!record) throw failure("foreign-handle");
|
|
51
|
+
return record;
|
|
52
|
+
}
|
|
53
|
+
function track(work) {
|
|
54
|
+
let task;
|
|
55
|
+
try {
|
|
56
|
+
ensureOpen();
|
|
57
|
+
task = work();
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return Promise.reject(error instanceof Error ? error : failure("invalid-input"));
|
|
60
|
+
}
|
|
61
|
+
pending.add(task);
|
|
62
|
+
task.finally(() => pending.delete(task)).catch(() => void 0);
|
|
63
|
+
return task;
|
|
64
|
+
}
|
|
65
|
+
function request(operation, input, current = session, currentSignal = signal) {
|
|
66
|
+
const result = queue.then(() => sessionProcess(current).request(operation, input, { signal: currentSignal }));
|
|
67
|
+
queue = result.then(() => void 0, () => void 0);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
async function mutate(operation, input, work, observed, capacity = mutationRecordBytes) {
|
|
71
|
+
const reservation = journal.reserve(capacity);
|
|
72
|
+
try {
|
|
73
|
+
const operationId = randomUUID();
|
|
74
|
+
await reservation.append({
|
|
75
|
+
id: operationId,
|
|
76
|
+
input,
|
|
77
|
+
kind: "intent",
|
|
78
|
+
operation
|
|
79
|
+
});
|
|
80
|
+
if (operation === "upload" || operation === "create-share") unknownCreations.add(operationId);
|
|
81
|
+
const result = await work();
|
|
82
|
+
observed?.(result);
|
|
83
|
+
await reservation.append({
|
|
84
|
+
id: operationId,
|
|
85
|
+
kind: "ack",
|
|
86
|
+
result
|
|
87
|
+
});
|
|
88
|
+
unknownCreations.delete(operationId);
|
|
89
|
+
return result;
|
|
90
|
+
} finally {
|
|
91
|
+
reservation.release();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
journal = await createJournal(options.journalFile, {
|
|
96
|
+
apiBaseUrl: document.apiBaseUrl,
|
|
97
|
+
appId: document.app.id,
|
|
98
|
+
kind: "header",
|
|
99
|
+
runId: id,
|
|
100
|
+
version: 1
|
|
101
|
+
});
|
|
102
|
+
session = await openLiveSession(options.profile, {
|
|
103
|
+
...agentOptions,
|
|
104
|
+
signal
|
|
105
|
+
});
|
|
106
|
+
credential = sessionInfo(session).appKeyHex;
|
|
107
|
+
await journal.append({
|
|
108
|
+
appKeyHex: credential,
|
|
109
|
+
kind: "auth"
|
|
110
|
+
});
|
|
111
|
+
const ownerAccess = opaque("access");
|
|
112
|
+
run = Object.freeze({
|
|
113
|
+
backend: "live",
|
|
114
|
+
id,
|
|
115
|
+
objects: {
|
|
116
|
+
delete(handle) {
|
|
117
|
+
return track(async () => {
|
|
118
|
+
const record = ownObject(handle);
|
|
119
|
+
if (record.deleted) return;
|
|
120
|
+
await mutate("delete", { objectId: record.handle.id }, () => request("delete", { objectId: record.handle.id }), () => {
|
|
121
|
+
record.deleted = true;
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
inspect(handle) {
|
|
126
|
+
return track(async () => {
|
|
127
|
+
const result = await request("inspect", { objectId: ownObject(handle).handle.id });
|
|
128
|
+
if (result !== "present" && result !== "missing") throw failure("agent-protocol");
|
|
129
|
+
return result;
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
upload(input) {
|
|
133
|
+
return track(async () => {
|
|
134
|
+
const source = input.file;
|
|
135
|
+
let name = input.name;
|
|
136
|
+
const suppliedMetadata = input.metadata;
|
|
137
|
+
const file = {
|
|
138
|
+
name: source.name,
|
|
139
|
+
open: source.open.bind(source),
|
|
140
|
+
sha256: source.sha256,
|
|
141
|
+
sizeBytes: source.sizeBytes
|
|
142
|
+
};
|
|
143
|
+
if (name === void 0) name = file.name;
|
|
144
|
+
if (typeof name !== "string" || !name || !Number.isSafeInteger(file.sizeBytes) || file.sizeBytes < 0 || !/^[a-f0-9]{64}$/.test(file.sha256)) throw failure("invalid-input");
|
|
145
|
+
let metadata;
|
|
146
|
+
try {
|
|
147
|
+
metadata = new Uint8Array(boundedByteView(suppliedMetadata, maximumMetadataBytes));
|
|
148
|
+
} catch {
|
|
149
|
+
throw failure("invalid-input");
|
|
150
|
+
}
|
|
151
|
+
if (objectAttempts >= document.limits.maxObjects || file.sizeBytes > document.limits.maxLogicalUploadBytes - reservedBytes) throw failure("limit");
|
|
152
|
+
objectAttempts++;
|
|
153
|
+
reservedBytes += file.sizeBytes;
|
|
154
|
+
const bytes = await readFixture(file, signal);
|
|
155
|
+
ensureOpen();
|
|
156
|
+
let record;
|
|
157
|
+
await mutate("upload", {
|
|
158
|
+
sha256: file.sha256,
|
|
159
|
+
sizeBytes: file.sizeBytes
|
|
160
|
+
}, async () => {
|
|
161
|
+
const result = await request("upload", {
|
|
162
|
+
bytes,
|
|
163
|
+
metadata
|
|
164
|
+
});
|
|
165
|
+
if (!result || typeof result !== "object" || !("objectId" in result) || typeof result.objectId !== "string" || !/^[a-f0-9]{64}$/.test(result.objectId) || !("sealed" in result) || typeof result.sealed !== "string" || Buffer.byteLength(result.sealed) > 1048576 || !("sizeBytes" in result) || result.sizeBytes !== file.sizeBytes || !("observedShardBytes" in result) || !Number.isSafeInteger(result.observedShardBytes) || result.observedShardBytes < observedShardBytes) throw failure("agent-protocol");
|
|
166
|
+
observedShardBytes = result.observedShardBytes;
|
|
167
|
+
return {
|
|
168
|
+
objectId: result.objectId,
|
|
169
|
+
observedShardBytes,
|
|
170
|
+
sealed: result.sealed,
|
|
171
|
+
sizeBytes: result.sizeBytes
|
|
172
|
+
};
|
|
173
|
+
}, (result) => {
|
|
174
|
+
const handle = Object.freeze({
|
|
175
|
+
id: result.objectId,
|
|
176
|
+
sha256: file.sha256,
|
|
177
|
+
sizeBytes: file.sizeBytes
|
|
178
|
+
});
|
|
179
|
+
record = {
|
|
180
|
+
deleted: false,
|
|
181
|
+
handle,
|
|
182
|
+
pinned: false,
|
|
183
|
+
sealed: result.sealed
|
|
184
|
+
};
|
|
185
|
+
objects.set(handle, record);
|
|
186
|
+
}, uploadRecordBytes);
|
|
187
|
+
await mutate("pin", { objectId: record.handle.id }, () => request("pin", {
|
|
188
|
+
objectId: record.handle.id,
|
|
189
|
+
sealed: record.sealed
|
|
190
|
+
}), () => {
|
|
191
|
+
record.pinned = true;
|
|
192
|
+
});
|
|
193
|
+
logicalUploadedBytes += file.sizeBytes;
|
|
194
|
+
return record.handle;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
ownerAccess,
|
|
199
|
+
shares: {
|
|
200
|
+
attach(share, object) {
|
|
201
|
+
return track(() => attach(ownShare(share), ownObject(object)));
|
|
202
|
+
},
|
|
203
|
+
create(input) {
|
|
204
|
+
return track(async () => {
|
|
205
|
+
const attached = [...input.objects].map((object) => ownObject(object));
|
|
206
|
+
if (attached.some((object) => object.deleted)) throw failure("not-found");
|
|
207
|
+
const expiresAt = input.expiresAt?.getTime();
|
|
208
|
+
if (expiresAt !== void 0 && (!Number.isFinite(expiresAt) || expiresAt <= Date.now())) throw failure("invalid-input");
|
|
209
|
+
if (shareAttempts >= document.limits.maxShares) throw failure("limit");
|
|
210
|
+
shareAttempts++;
|
|
211
|
+
let record;
|
|
212
|
+
await mutate("create-share", {}, async () => {
|
|
213
|
+
const result = await request("create-share", {
|
|
214
|
+
description: `sia-testing:${id}:${randomUUID()}`,
|
|
215
|
+
...expiresAt === void 0 ? {} : { expiresAt: new Date(expiresAt).toISOString() }
|
|
216
|
+
});
|
|
217
|
+
if (!result || typeof result !== "object" || !("seedHex" in result) || typeof result.seedHex !== "string" || !/^[a-f0-9]{64}$/.test(result.seedHex) || !("shareId" in result) || typeof result.shareId !== "string" || !/^(?:ed25519:)?[a-f0-9]{64}$/.test(result.shareId)) throw failure("agent-protocol");
|
|
218
|
+
return {
|
|
219
|
+
seedHex: result.seedHex,
|
|
220
|
+
shareId: result.shareId
|
|
221
|
+
};
|
|
222
|
+
}, (result) => {
|
|
223
|
+
const access = opaque("access");
|
|
224
|
+
const handle = Object.freeze({
|
|
225
|
+
access,
|
|
226
|
+
id: result.shareId
|
|
227
|
+
});
|
|
228
|
+
record = {
|
|
229
|
+
handle,
|
|
230
|
+
revoked: false,
|
|
231
|
+
seedHex: result.seedHex
|
|
232
|
+
};
|
|
233
|
+
shares.set(handle, record);
|
|
234
|
+
});
|
|
235
|
+
for (const object of attached) await attach(record, object);
|
|
236
|
+
return record.handle;
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
detach(share, object) {
|
|
240
|
+
return track(async () => {
|
|
241
|
+
const sharing = ownShare(share);
|
|
242
|
+
const stored = ownObject(object);
|
|
243
|
+
if (sharing.revoked) throw failure("denied");
|
|
244
|
+
await mutate("detach", {
|
|
245
|
+
objectId: stored.handle.id,
|
|
246
|
+
shareId: sharing.handle.id
|
|
247
|
+
}, () => request("detach", {
|
|
248
|
+
objectId: stored.handle.id,
|
|
249
|
+
seedHex: sharing.seedHex,
|
|
250
|
+
shareId: sharing.handle.id
|
|
251
|
+
}));
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
revoke(share) {
|
|
255
|
+
return track(async () => {
|
|
256
|
+
const record = ownShare(share);
|
|
257
|
+
if (record.revoked) return;
|
|
258
|
+
await mutate("revoke", { shareId: record.handle.id }, () => request("revoke", {
|
|
259
|
+
seedHex: record.seedHex,
|
|
260
|
+
shareId: record.handle.id
|
|
261
|
+
}), () => {
|
|
262
|
+
record.revoked = true;
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
value = await abortable(use(run), signal);
|
|
269
|
+
await abortable(Promise.all([...pending]), signal);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
errors.push(error);
|
|
272
|
+
}
|
|
273
|
+
opened = false;
|
|
274
|
+
execution.abort();
|
|
275
|
+
clearTimeout(timer);
|
|
276
|
+
try {
|
|
277
|
+
if (options.beforeCleanup) await abortable(Promise.resolve().then(options.beforeCleanup), AbortSignal.timeout(document.limits.cleanupDeadlineMs));
|
|
278
|
+
} catch (error) {
|
|
279
|
+
errors.push(error);
|
|
280
|
+
uncertain = true;
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
if (session) await closeLiveSession(session);
|
|
284
|
+
} catch (error) {
|
|
285
|
+
errors.push(error);
|
|
286
|
+
uncertain = true;
|
|
287
|
+
}
|
|
288
|
+
if (credential && !uncertain && (objects.size || shares.size)) try {
|
|
289
|
+
await cleanupKnownResources({
|
|
290
|
+
...agentOptions,
|
|
291
|
+
appKeyHex: credential,
|
|
292
|
+
journal,
|
|
293
|
+
objects: [...objects.values()].map((record) => ({
|
|
294
|
+
deleted: record.deleted,
|
|
295
|
+
objectId: record.handle.id,
|
|
296
|
+
pinned: record.pinned,
|
|
297
|
+
sealed: record.sealed
|
|
298
|
+
})),
|
|
299
|
+
profile: options.profile,
|
|
300
|
+
shares: [...shares.values()].map((record) => ({
|
|
301
|
+
revoked: record.revoked,
|
|
302
|
+
seedHex: record.seedHex,
|
|
303
|
+
shareId: record.handle.id
|
|
304
|
+
}))
|
|
305
|
+
});
|
|
306
|
+
} catch (error) {
|
|
307
|
+
errors.push(error);
|
|
308
|
+
uncertain = true;
|
|
309
|
+
}
|
|
310
|
+
uncertain ||= unknownCreations.size > 0;
|
|
311
|
+
const summary = Object.freeze({
|
|
312
|
+
backend: "live",
|
|
313
|
+
cleanup: uncertain ? "uncertain" : "clean",
|
|
314
|
+
id,
|
|
315
|
+
journalFile: options.journalFile,
|
|
316
|
+
logicalUploadedBytes,
|
|
317
|
+
observedShardBytes
|
|
318
|
+
});
|
|
319
|
+
try {
|
|
320
|
+
await journal?.append({
|
|
321
|
+
cleanup: summary.cleanup,
|
|
322
|
+
kind: "complete"
|
|
323
|
+
});
|
|
324
|
+
} catch (error) {
|
|
325
|
+
errors.push(error);
|
|
326
|
+
uncertain = true;
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
await journal?.close();
|
|
330
|
+
} catch (error) {
|
|
331
|
+
errors.push(error);
|
|
332
|
+
uncertain = true;
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
await lease.release();
|
|
336
|
+
} catch (error) {
|
|
337
|
+
errors.push(error);
|
|
338
|
+
uncertain = true;
|
|
339
|
+
}
|
|
340
|
+
const finalSummary = uncertain && summary.cleanup === "clean" ? Object.freeze({
|
|
341
|
+
...summary,
|
|
342
|
+
cleanup: "uncertain"
|
|
343
|
+
}) : summary;
|
|
344
|
+
if (uncertain && errors.length === 0) errors.push(failure("cleanup-uncertain"));
|
|
345
|
+
if (errors.length) throw Object.assign(new AggregateError(errors, "Sia managed live run failed"), { summary: finalSummary });
|
|
346
|
+
return {
|
|
347
|
+
summary: finalSummary,
|
|
348
|
+
value
|
|
349
|
+
};
|
|
350
|
+
async function attach(share, object) {
|
|
351
|
+
if (share.revoked) throw failure("denied");
|
|
352
|
+
if (object.deleted) throw failure("not-found");
|
|
353
|
+
await mutate("attach", {
|
|
354
|
+
objectId: object.handle.id,
|
|
355
|
+
shareId: share.handle.id
|
|
356
|
+
}, () => request("attach", {
|
|
357
|
+
objectId: object.handle.id,
|
|
358
|
+
seedHex: share.seedHex,
|
|
359
|
+
shareId: share.handle.id
|
|
360
|
+
}));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
async function abortable(work, signal) {
|
|
364
|
+
signal.throwIfAborted();
|
|
365
|
+
let abort;
|
|
366
|
+
const stopped = new Promise((_resolve, reject) => {
|
|
367
|
+
abort = () => reject(signal.reason instanceof Error ? signal.reason : failure("cancelled"));
|
|
368
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
369
|
+
});
|
|
370
|
+
try {
|
|
371
|
+
return await Promise.race([work, stopped]);
|
|
372
|
+
} finally {
|
|
373
|
+
signal.removeEventListener("abort", abort);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function failure(code) {
|
|
377
|
+
return Object.assign(/* @__PURE__ */ new Error("Sia managed live run operation failed"), {
|
|
378
|
+
code,
|
|
379
|
+
phase: "resources"
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
function opaque(kind) {
|
|
383
|
+
return Object.freeze(Object.defineProperties(Object.create(null), {
|
|
384
|
+
[Symbol.for("nodejs.util.inspect.custom")]: { value: () => `[Sia ${kind}]` },
|
|
385
|
+
toJSON: { value: () => `[Sia ${kind}]` }
|
|
386
|
+
}));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
//#endregion
|
|
390
|
+
export { withLiveRun };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { profileDocument } from "./profile.js";
|
|
2
|
+
import { approveLiveConnection } from "./approval.js";
|
|
3
|
+
import { startOwnedProcess } from "./process.js";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
|
|
7
|
+
//#region src/testing/live/session.ts
|
|
8
|
+
/** Authenticated sessions keep SDK credentials behind identity-bound Node handles. */
|
|
9
|
+
const sessions = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
function closeLiveSession(session) {
|
|
11
|
+
const state = sessions.get(session);
|
|
12
|
+
if (!state) return Promise.reject(failure("foreign-handle"));
|
|
13
|
+
if (state.close) return state.close;
|
|
14
|
+
state.info = void 0;
|
|
15
|
+
state.abort.abort();
|
|
16
|
+
state.close = state.agent.close();
|
|
17
|
+
return state.close;
|
|
18
|
+
}
|
|
19
|
+
async function openLiveSession(profile, options = {}) {
|
|
20
|
+
const document = profileDocument(profile);
|
|
21
|
+
options.signal?.throwIfAborted();
|
|
22
|
+
const abort = new AbortController();
|
|
23
|
+
const signal = options.signal ? AbortSignal.any([options.signal, abort.signal]) : abort.signal;
|
|
24
|
+
const agent = startOwnedProcess({
|
|
25
|
+
cleanupDeadlineMs: document.limits.cleanupDeadlineMs,
|
|
26
|
+
deadlineMs: document.limits.deadlineMs,
|
|
27
|
+
entrypoint: options.agentModule ?? new URL("./dist/esm/testing/live/agent.js", pathToFileURL(createRequire(import.meta.url).resolve("@lumeweb/sia-testing/package.json"))),
|
|
28
|
+
async onEvent(event) {
|
|
29
|
+
if (event.name !== "approval" || typeof event.value !== "string" || document.auth.kind !== "register") throw failure("agent-protocol");
|
|
30
|
+
await approveLiveConnection(profile, event.value, true, signal);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
let primary;
|
|
34
|
+
try {
|
|
35
|
+
const info = parseInfo(await agent.request("authenticate", document, { signal }));
|
|
36
|
+
if (info.appId !== document.app.id || !info.ready) throw failure("not-ready");
|
|
37
|
+
if (document.auth.kind === "app-key" && info.appKeyHex !== document.auth.appKeyHex) throw failure("authentication");
|
|
38
|
+
signal.throwIfAborted();
|
|
39
|
+
const session = Object.freeze(Object.defineProperties(Object.create(null), {
|
|
40
|
+
[Symbol.for("nodejs.util.inspect.custom")]: { value: () => "[Sia live session]" },
|
|
41
|
+
toJSON: { value: () => "[Sia live session]" }
|
|
42
|
+
}));
|
|
43
|
+
sessions.set(session, {
|
|
44
|
+
abort,
|
|
45
|
+
agent,
|
|
46
|
+
info
|
|
47
|
+
});
|
|
48
|
+
return session;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
primary = error;
|
|
51
|
+
}
|
|
52
|
+
abort.abort();
|
|
53
|
+
const errors = [primary];
|
|
54
|
+
try {
|
|
55
|
+
await agent.close();
|
|
56
|
+
} catch (cleanup) {
|
|
57
|
+
errors.push(cleanup);
|
|
58
|
+
}
|
|
59
|
+
if (errors.length > 1) throw new AggregateError(errors, "SDK authentication and shutdown failed", { cause: primary });
|
|
60
|
+
throw primary;
|
|
61
|
+
}
|
|
62
|
+
function sessionInfo(session) {
|
|
63
|
+
const state = sessions.get(session);
|
|
64
|
+
if (!state?.info) throw failure("closed");
|
|
65
|
+
return state.info;
|
|
66
|
+
}
|
|
67
|
+
function sessionProcess(session) {
|
|
68
|
+
const state = sessions.get(session);
|
|
69
|
+
if (!state?.info) throw failure("closed");
|
|
70
|
+
return state.agent;
|
|
71
|
+
}
|
|
72
|
+
function failure(code) {
|
|
73
|
+
return Object.assign(/* @__PURE__ */ new Error("Sia SDK authentication failed"), {
|
|
74
|
+
code,
|
|
75
|
+
phase: "authentication"
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function parseInfo(value) {
|
|
79
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw failure("agent-protocol");
|
|
80
|
+
const info = value;
|
|
81
|
+
if (typeof info.appKeyHex !== "string" || !/^[a-f0-9]{64}$/.test(info.appKeyHex) || typeof info.accountKey !== "string" || !/^(?:ed25519:)?[a-f0-9]{64}$/.test(info.accountKey) || typeof info.appId !== "string" || !/^[a-f0-9]{64}$/.test(info.appId) || typeof info.ready !== "boolean" || typeof info.remainingStorage !== "bigint" || info.remainingStorage < 0n || typeof info.maxPinnedData !== "bigint" || info.maxPinnedData < 0n) throw failure("agent-protocol");
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
accountKey: info.accountKey,
|
|
84
|
+
appId: info.appId,
|
|
85
|
+
appKeyHex: info.appKeyHex,
|
|
86
|
+
maxPinnedData: info.maxPinnedData,
|
|
87
|
+
ready: info.ready,
|
|
88
|
+
remainingStorage: info.remainingStorage
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { closeLiveSession, openLiveSession, sessionInfo, sessionProcess };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MockOptions, MockSia } from "./types.js";
|
|
2
|
+
import { bytesFixture } from "./files.js";
|
|
3
|
+
|
|
4
|
+
//#region src/testing/mock.d.ts
|
|
5
|
+
/** Store ownership is per run so closing one account cannot reset another active run. */
|
|
6
|
+
declare function createMockSia(options?: MockOptions): MockSia;
|
|
7
|
+
//#endregion
|
|
8
|
+
export { bytesFixture, createMockSia };
|