@lumeweb/sia-testing 0.0.0-stage → 0.0.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.
- package/LICENSE +9 -0
- package/README.md +210 -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 +90 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ActorRef, BrowserAccess, MockShareGrant, MockSia } from "./types.js";
|
|
2
|
+
import { MockCredential } from "./runtime.js";
|
|
3
|
+
import { MockConnection } from "./transport.js";
|
|
4
|
+
|
|
5
|
+
//#region src/testing/msw.d.ts
|
|
6
|
+
declare function createMockHandlers(mock: MockSia, options: {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
}): {
|
|
9
|
+
close: () => Promise<void>;
|
|
10
|
+
handlers: import("msw").HttpHandler[];
|
|
11
|
+
connection(input: {
|
|
12
|
+
readonly access: BrowserAccess;
|
|
13
|
+
readonly as: ActorRef;
|
|
14
|
+
readonly shareGrants?: readonly MockShareGrant[];
|
|
15
|
+
}): MockConnection & {
|
|
16
|
+
readonly credential: MockCredential;
|
|
17
|
+
};
|
|
18
|
+
url: string;
|
|
19
|
+
};
|
|
20
|
+
//#endregion
|
|
21
|
+
export { createMockHandlers };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { isLostReply } from "./reply-loss.js";
|
|
2
|
+
import { createMockEndpoint } from "./transport.js";
|
|
3
|
+
import { HttpResponse, http } from "msw";
|
|
4
|
+
|
|
5
|
+
//#region src/testing/msw.ts
|
|
6
|
+
/** Scoped MSW routes use the same credential-aware dispatcher as a loopback server. */
|
|
7
|
+
function createMockHandlers(mock, options) {
|
|
8
|
+
const configured = createMockEndpoint(mock, options.baseUrl, applicationBody);
|
|
9
|
+
return {
|
|
10
|
+
...configured.endpoint,
|
|
11
|
+
close: () => configured.close(),
|
|
12
|
+
handlers: [http.all(`${configured.endpoint.url}*`, async ({ request }) => {
|
|
13
|
+
try {
|
|
14
|
+
return await configured.dispatch(request);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
if (isLostReply(error)) return HttpResponse.error();
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
})]
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** MSW owns a logging clone, so upstream cancellation cannot be joined by the resolver. */
|
|
23
|
+
function applicationBody(request, onCleanupError) {
|
|
24
|
+
const body = request.body;
|
|
25
|
+
if (!body) return null;
|
|
26
|
+
let reader;
|
|
27
|
+
let stopped = false;
|
|
28
|
+
const release = () => {
|
|
29
|
+
reader?.releaseLock();
|
|
30
|
+
reader = void 0;
|
|
31
|
+
};
|
|
32
|
+
return new ReadableStream({
|
|
33
|
+
cancel(reason) {
|
|
34
|
+
stopped = true;
|
|
35
|
+
reader ??= body.getReader();
|
|
36
|
+
reader.cancel(reason).catch(onCleanupError);
|
|
37
|
+
release();
|
|
38
|
+
},
|
|
39
|
+
async pull(controller) {
|
|
40
|
+
reader ??= body.getReader();
|
|
41
|
+
try {
|
|
42
|
+
const next = await reader.read();
|
|
43
|
+
if (stopped) return;
|
|
44
|
+
if (next.done) {
|
|
45
|
+
stopped = true;
|
|
46
|
+
controller.close();
|
|
47
|
+
release();
|
|
48
|
+
} else controller.enqueue(next.value);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (!stopped) {
|
|
51
|
+
stopped = true;
|
|
52
|
+
controller.error(error);
|
|
53
|
+
release();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}, { highWaterMark: 0 });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
export { createMockHandlers };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { RequestHold } from "./controls.js";
|
|
2
|
+
import { ActorRef, ApplicationObject, MockShareGrant } from "./types.js";
|
|
3
|
+
|
|
4
|
+
//#region src/testing/mutations.d.ts
|
|
5
|
+
interface MockMutationFaults {
|
|
6
|
+
fail(selector: MutationSelector, options: {
|
|
7
|
+
code: "denied" | "unavailable";
|
|
8
|
+
times: number;
|
|
9
|
+
}): void;
|
|
10
|
+
hold(selector: MutationSelector): RequestHold;
|
|
11
|
+
loseReply(selector: MutationReplySelector, options: {
|
|
12
|
+
times: number;
|
|
13
|
+
}): void;
|
|
14
|
+
}
|
|
15
|
+
type MutationReplySelector = {
|
|
16
|
+
readonly actor: ActorRef;
|
|
17
|
+
} & ({
|
|
18
|
+
readonly object?: ApplicationObject;
|
|
19
|
+
readonly operation: "delete" | "pin";
|
|
20
|
+
readonly share?: never;
|
|
21
|
+
} | {
|
|
22
|
+
readonly object?: never;
|
|
23
|
+
readonly operation: "share-add" | "share-end" | "share-remove";
|
|
24
|
+
readonly share?: MockShareGrant;
|
|
25
|
+
} | {
|
|
26
|
+
readonly object?: never;
|
|
27
|
+
readonly operation: "upload";
|
|
28
|
+
readonly share?: never;
|
|
29
|
+
});
|
|
30
|
+
type MutationSelector = {
|
|
31
|
+
readonly actor: ActorRef;
|
|
32
|
+
readonly afterBytes: number;
|
|
33
|
+
readonly object?: never;
|
|
34
|
+
readonly operation: "packed-add" | "upload";
|
|
35
|
+
readonly share?: never;
|
|
36
|
+
readonly stage: "collect";
|
|
37
|
+
} | ({
|
|
38
|
+
readonly actor?: ActorRef;
|
|
39
|
+
readonly stage: "beforeCommit";
|
|
40
|
+
} & ({
|
|
41
|
+
readonly object?: ApplicationObject;
|
|
42
|
+
readonly operation: "delete" | "pin";
|
|
43
|
+
readonly share?: never;
|
|
44
|
+
} | {
|
|
45
|
+
readonly object?: never;
|
|
46
|
+
readonly operation: "metadata" | "packed-add" | "packed-finalize" | "upload";
|
|
47
|
+
readonly share?: never;
|
|
48
|
+
} | {
|
|
49
|
+
readonly object?: never;
|
|
50
|
+
readonly operation: "share-add" | "share-end" | "share-remove";
|
|
51
|
+
readonly share?: MockShareGrant;
|
|
52
|
+
}));
|
|
53
|
+
interface PinReplySelector {
|
|
54
|
+
readonly actor: ActorRef;
|
|
55
|
+
readonly object?: ApplicationObject;
|
|
56
|
+
readonly operation: "pin";
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { MockMutationFaults, MutationReplySelector, MutationSelector, PinReplySelector };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createRequestControls } from "./controls.js";
|
|
2
|
+
import { lostReply } from "./reply-loss.js";
|
|
3
|
+
|
|
4
|
+
//#region src/testing/mutations.ts
|
|
5
|
+
/** Mutation failures precede effects; reply losses consume only committed effects. */
|
|
6
|
+
function createMutationControls(validate, records = [], validateReply, actorLabel) {
|
|
7
|
+
const receiptGrants = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
const controls = createRequestControls(records, actorLabel, receiptGrants);
|
|
9
|
+
const replies = createRequestControls([], actorLabel, receiptGrants);
|
|
10
|
+
const control = {
|
|
11
|
+
fail(selector, options) {
|
|
12
|
+
selector = { ...selector };
|
|
13
|
+
options = { ...options };
|
|
14
|
+
validate(selector);
|
|
15
|
+
if (options.code !== "denied" && options.code !== "unavailable") throw Object.assign(/* @__PURE__ */ new Error("Invalid mutation failure"), { code: "invalid-input" });
|
|
16
|
+
controls.control.fail(selector, options);
|
|
17
|
+
},
|
|
18
|
+
hold(selector) {
|
|
19
|
+
selector = { ...selector };
|
|
20
|
+
validate(selector);
|
|
21
|
+
return controls.control.hold(selector);
|
|
22
|
+
},
|
|
23
|
+
loseReply(selector, options) {
|
|
24
|
+
selector = { ...selector };
|
|
25
|
+
options = { ...options };
|
|
26
|
+
validateReply(selector);
|
|
27
|
+
replies.control.fail(selector, {
|
|
28
|
+
code: "unavailable",
|
|
29
|
+
times: options.times
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
...controls,
|
|
35
|
+
afterCommit(receipt) {
|
|
36
|
+
if (receipt.committed && replies.takeFailure(receipt)) throw lostReply();
|
|
37
|
+
},
|
|
38
|
+
async collect(actor, operation, signal, work) {
|
|
39
|
+
const receipt = controls.createReceipt(actor, operation, void 0, void 0, false);
|
|
40
|
+
receipt.stage = "collect";
|
|
41
|
+
try {
|
|
42
|
+
const result = await work((bytes) => {
|
|
43
|
+
receipt.bytes = bytes;
|
|
44
|
+
return controls.checkpoint(receipt, signal);
|
|
45
|
+
});
|
|
46
|
+
receipt.status = "completed";
|
|
47
|
+
return result;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
receipt.status = signal.aborted ? "cancelled" : "failed";
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
control
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//#endregion
|
|
58
|
+
export { createMutationControls };
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { createPayloadSourceValidator } from "./byte-view.js";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
7
|
+
|
|
8
|
+
//#region src/testing/node-payload-store.ts
|
|
9
|
+
/** A run owns every recorded file, descriptor and settled cleanup failure until explicit close. */
|
|
10
|
+
/** Dependencies remain private and exist to exercise actual filesystem failure boundaries. */
|
|
11
|
+
async function createNodePayloadOwner(limits, io = fs) {
|
|
12
|
+
limits = validatePayloadLimits(limits);
|
|
13
|
+
const root = await fs.mkdtemp(join(tmpdir(), "sia-testing-payload-"));
|
|
14
|
+
try {
|
|
15
|
+
await io.chmod(root, 448);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
try {
|
|
18
|
+
await io.rmdir(root);
|
|
19
|
+
} catch (cleanup) {
|
|
20
|
+
throw new AggregateError([error, cleanup], `Payload root setup and removal failed: ${root}`, { cause: cleanup });
|
|
21
|
+
}
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
const entries = /* @__PURE__ */ new Set();
|
|
25
|
+
const tokens = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
const jobs = /* @__PURE__ */ new Set();
|
|
27
|
+
const reservations = /* @__PURE__ */ new Set();
|
|
28
|
+
const failures = [];
|
|
29
|
+
let retainedBytes = 0;
|
|
30
|
+
let attempts = 0;
|
|
31
|
+
let writers = 0;
|
|
32
|
+
let readers = 0;
|
|
33
|
+
let stopped = false;
|
|
34
|
+
let closed = false;
|
|
35
|
+
let closing;
|
|
36
|
+
function healthy() {
|
|
37
|
+
if (closed || stopped) throw new Error("Payload owner closed or descriptor closure uncertain");
|
|
38
|
+
}
|
|
39
|
+
function owned(token) {
|
|
40
|
+
const entry = tokens.get(token);
|
|
41
|
+
if (!entry || entry.removed || entry.retiring) throw new Error("Unknown or retired payload token");
|
|
42
|
+
return entry;
|
|
43
|
+
}
|
|
44
|
+
function remember(job) {
|
|
45
|
+
jobs.add(job);
|
|
46
|
+
job.catch(() => void 0);
|
|
47
|
+
return job;
|
|
48
|
+
}
|
|
49
|
+
function closeWriter(entry) {
|
|
50
|
+
if (entry.writerClose) return entry.writerClose;
|
|
51
|
+
entry.writerClose = (async () => {
|
|
52
|
+
if (!entry.handle) return;
|
|
53
|
+
try {
|
|
54
|
+
await entry.handle.close();
|
|
55
|
+
delete entry.handle;
|
|
56
|
+
writers--;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
stopped = true;
|
|
59
|
+
failures.push(error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
63
|
+
entry.writerClose.catch(() => void 0);
|
|
64
|
+
return entry.writerClose;
|
|
65
|
+
}
|
|
66
|
+
function retireEntry(entry) {
|
|
67
|
+
if (entry.retirement) return entry.retirement;
|
|
68
|
+
entry.retiring = true;
|
|
69
|
+
entry.abort.abort(/* @__PURE__ */ new Error("Payload retired"));
|
|
70
|
+
entry.retirement = remember((async () => {
|
|
71
|
+
const results = await Promise.allSettled([entry.pending, ...Array.from(entry.readers, (reader) => reader.close())]);
|
|
72
|
+
if (entry.removed) return;
|
|
73
|
+
const errors = results.filter((result) => result.status === "rejected").map((result) => result.reason);
|
|
74
|
+
try {
|
|
75
|
+
await closeWriter(entry);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
errors.push(error);
|
|
78
|
+
}
|
|
79
|
+
if (errors.length) throw new AggregateError(errors, `Payload closure uncertain: ${entry.path}`);
|
|
80
|
+
try {
|
|
81
|
+
await io.unlink(entry.path);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
failures.push(error);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
entry.removed = true;
|
|
87
|
+
entries.delete(entry);
|
|
88
|
+
entry.onRemoved();
|
|
89
|
+
})());
|
|
90
|
+
return entry.retirement;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
close() {
|
|
94
|
+
if (closing) return closing;
|
|
95
|
+
closed = true;
|
|
96
|
+
closing = (async () => {
|
|
97
|
+
await Promise.allSettled(Array.from(entries, retireEntry));
|
|
98
|
+
for (const reservation of reservations) try {
|
|
99
|
+
reservation.release();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
failures.push(error);
|
|
102
|
+
}
|
|
103
|
+
const errors = (await Promise.allSettled(jobs)).filter((result) => result.status === "rejected").map((result) => result.reason);
|
|
104
|
+
if (entries.size || readers || writers || errors.length || failures.length) throw new AggregateError([...failures, ...errors], `Payload residue or uncertain descriptors at ${root}; entries=${entries.size}, writers=${writers}, readers=${readers}, retainedBytes=${retainedBytes}`);
|
|
105
|
+
await io.rmdir(root);
|
|
106
|
+
})();
|
|
107
|
+
closing.catch(() => void 0);
|
|
108
|
+
return closing;
|
|
109
|
+
},
|
|
110
|
+
discardUncommitted(token) {
|
|
111
|
+
const entry = tokens.get(token);
|
|
112
|
+
if (!entry) return Promise.reject(/* @__PURE__ */ new Error("Unknown payload token"));
|
|
113
|
+
return entry.adopted ? Promise.resolve() : retireEntry(entry);
|
|
114
|
+
},
|
|
115
|
+
async drain() {
|
|
116
|
+
const errors = (await Promise.allSettled(jobs)).filter((result) => result.status === "rejected").map((result) => result.reason);
|
|
117
|
+
if (errors.length) throw new AggregateError(errors, "Payload retirement failed");
|
|
118
|
+
},
|
|
119
|
+
kind: "temporary-files",
|
|
120
|
+
async openReader(token, offset, length) {
|
|
121
|
+
healthy();
|
|
122
|
+
integer(offset, Number.MAX_SAFE_INTEGER, "offset");
|
|
123
|
+
integer(length, Number.MAX_SAFE_INTEGER, "length");
|
|
124
|
+
const entry = owned(token);
|
|
125
|
+
if (readers >= 2) throw new Error("Payload reader capacity exhausted");
|
|
126
|
+
readers++;
|
|
127
|
+
let handle;
|
|
128
|
+
const opening = io.open(entry.path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)).then((value) => {
|
|
129
|
+
handle = value;
|
|
130
|
+
}, (error) => {
|
|
131
|
+
readers--;
|
|
132
|
+
entry.readers.delete(reader);
|
|
133
|
+
throw error;
|
|
134
|
+
});
|
|
135
|
+
opening.catch(() => void 0);
|
|
136
|
+
let position = Math.min(offset, token.length);
|
|
137
|
+
const end = position + Math.min(length, token.length - position);
|
|
138
|
+
let closingReader;
|
|
139
|
+
let pending = Promise.resolve();
|
|
140
|
+
let reading = false;
|
|
141
|
+
const reader = {
|
|
142
|
+
close() {
|
|
143
|
+
if (!closingReader) {
|
|
144
|
+
closingReader = (async () => {
|
|
145
|
+
await opening.catch(() => void 0);
|
|
146
|
+
await pending;
|
|
147
|
+
if (!handle) return;
|
|
148
|
+
try {
|
|
149
|
+
await handle.close();
|
|
150
|
+
} catch (error) {
|
|
151
|
+
stopped = true;
|
|
152
|
+
failures.push(error);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
readers--;
|
|
156
|
+
entry.readers.delete(reader);
|
|
157
|
+
})();
|
|
158
|
+
closingReader.catch(() => void 0);
|
|
159
|
+
}
|
|
160
|
+
return closingReader;
|
|
161
|
+
},
|
|
162
|
+
read(maxBytes = 65536) {
|
|
163
|
+
integer(maxBytes, 65536, "read block");
|
|
164
|
+
if (!maxBytes) return Promise.reject(/* @__PURE__ */ new Error("Empty read block"));
|
|
165
|
+
if (closingReader || entry.retiring || reading) return Promise.reject(/* @__PURE__ */ new Error("Payload reader unavailable"));
|
|
166
|
+
reading = true;
|
|
167
|
+
const operation = (async () => {
|
|
168
|
+
if (position === end) return null;
|
|
169
|
+
const block = new Uint8Array(Math.min(maxBytes, end - position));
|
|
170
|
+
const { bytesRead } = await handle.read(block, 0, block.length, position);
|
|
171
|
+
if (!bytesRead) throw new Error("Unexpected payload EOF");
|
|
172
|
+
position += bytesRead;
|
|
173
|
+
return block.subarray(0, bytesRead);
|
|
174
|
+
})();
|
|
175
|
+
pending = operation.then(() => void 0, () => void 0);
|
|
176
|
+
operation.catch(() => void 0);
|
|
177
|
+
return operation.finally(() => {
|
|
178
|
+
reading = false;
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
signal: entry.abort.signal
|
|
182
|
+
};
|
|
183
|
+
entry.readers.add(reader);
|
|
184
|
+
await opening;
|
|
185
|
+
if (entry.retiring || closed) {
|
|
186
|
+
await reader.close();
|
|
187
|
+
throw new Error("Payload retired during open");
|
|
188
|
+
}
|
|
189
|
+
return reader;
|
|
190
|
+
},
|
|
191
|
+
prepareAdoption(token) {
|
|
192
|
+
healthy();
|
|
193
|
+
const entry = owned(token);
|
|
194
|
+
if (entry.adopted) throw new Error("Payload already adopted");
|
|
195
|
+
return () => {
|
|
196
|
+
entry.adopted = true;
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
reserveUpload(capacity) {
|
|
200
|
+
healthy();
|
|
201
|
+
integer(capacity, limits.maxRetainedBytes, "upload reservation");
|
|
202
|
+
if (reservations.size || retainedBytes + capacity > limits.maxRetainedBytes) throw new Error("Payload upload capacity exhausted");
|
|
203
|
+
retainedBytes += capacity;
|
|
204
|
+
let unused = capacity;
|
|
205
|
+
let released = false;
|
|
206
|
+
let active = false;
|
|
207
|
+
const sources = /* @__PURE__ */ new Set();
|
|
208
|
+
const reservation = {
|
|
209
|
+
async admitSource(maxBytes) {
|
|
210
|
+
healthy();
|
|
211
|
+
integer(maxBytes, limits.maxObjectBytes, "source capacity");
|
|
212
|
+
if (released || active || writers || maxBytes > unused || attempts >= limits.maxUploadAttempts || entries.size >= limits.maxUploadAttempts) throw new Error("Payload source admission exhausted");
|
|
213
|
+
attempts++;
|
|
214
|
+
unused -= maxBytes;
|
|
215
|
+
active = true;
|
|
216
|
+
writers++;
|
|
217
|
+
const entry = {
|
|
218
|
+
abort: new AbortController(),
|
|
219
|
+
adopted: false,
|
|
220
|
+
capacity: maxBytes,
|
|
221
|
+
length: 0,
|
|
222
|
+
onRemoved() {
|
|
223
|
+
if (!released && !entry.adopted) unused += entry.capacity;
|
|
224
|
+
else retainedBytes -= entry.capacity;
|
|
225
|
+
},
|
|
226
|
+
path: join(root, randomUUID()),
|
|
227
|
+
pending: Promise.resolve(),
|
|
228
|
+
readers: /* @__PURE__ */ new Set(),
|
|
229
|
+
removed: false,
|
|
230
|
+
retiring: false
|
|
231
|
+
};
|
|
232
|
+
entries.add(entry);
|
|
233
|
+
sources.add(entry);
|
|
234
|
+
try {
|
|
235
|
+
const opening = io.open(entry.path, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | (constants.O_NOFOLLOW ?? 0), 384).then((handle) => {
|
|
236
|
+
entry.handle = handle;
|
|
237
|
+
});
|
|
238
|
+
entry.pending = opening.then(() => void 0, () => void 0);
|
|
239
|
+
await opening;
|
|
240
|
+
} catch (error) {
|
|
241
|
+
entry.removed = true;
|
|
242
|
+
entries.delete(entry);
|
|
243
|
+
entry.onRemoved();
|
|
244
|
+
writers--;
|
|
245
|
+
active = false;
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
const hash = createHash("sha256");
|
|
249
|
+
let finishing = false;
|
|
250
|
+
const validateSource = createPayloadSourceValidator();
|
|
251
|
+
let busy = false;
|
|
252
|
+
let writeFailed = false;
|
|
253
|
+
let writeError;
|
|
254
|
+
return {
|
|
255
|
+
append(input) {
|
|
256
|
+
try {
|
|
257
|
+
healthy();
|
|
258
|
+
if (writeFailed) throw writeError;
|
|
259
|
+
if (finishing || busy || entry.retiring) throw new Error("Payload writer unavailable");
|
|
260
|
+
const view = validateSource(input, entry.capacity - entry.length);
|
|
261
|
+
const block = new Uint8Array(view);
|
|
262
|
+
busy = true;
|
|
263
|
+
const operation = (async () => {
|
|
264
|
+
let position = 0;
|
|
265
|
+
while (position < block.length) {
|
|
266
|
+
const result = await entry.handle.write(block, position, block.length - position, entry.length + position);
|
|
267
|
+
if (result.bytesWritten <= 0) throw new Error("Payload write made no progress");
|
|
268
|
+
position += result.bytesWritten;
|
|
269
|
+
}
|
|
270
|
+
hash.update(block);
|
|
271
|
+
entry.length += block.length;
|
|
272
|
+
})();
|
|
273
|
+
entry.pending = operation.then(() => void 0, (error) => {
|
|
274
|
+
writeFailed = true;
|
|
275
|
+
writeError = error;
|
|
276
|
+
});
|
|
277
|
+
operation.catch(() => void 0);
|
|
278
|
+
return operation.finally(() => {
|
|
279
|
+
busy = false;
|
|
280
|
+
});
|
|
281
|
+
} catch (error) {
|
|
282
|
+
return Promise.reject(error);
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
async discard() {
|
|
286
|
+
try {
|
|
287
|
+
await retireEntry(entry);
|
|
288
|
+
} finally {
|
|
289
|
+
if (!entry.handle) active = false;
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
async finish() {
|
|
293
|
+
if (finishing || entry.retiring) throw new Error("Payload writer unavailable");
|
|
294
|
+
finishing = true;
|
|
295
|
+
await entry.pending;
|
|
296
|
+
if (writeFailed) throw writeError;
|
|
297
|
+
await closeWriter(entry);
|
|
298
|
+
active = false;
|
|
299
|
+
unused += entry.capacity - entry.length;
|
|
300
|
+
entry.capacity = entry.length;
|
|
301
|
+
const token = Object.freeze({
|
|
302
|
+
length: entry.length,
|
|
303
|
+
sha256: hash.digest("hex")
|
|
304
|
+
});
|
|
305
|
+
entry.token = token;
|
|
306
|
+
tokens.set(token, entry);
|
|
307
|
+
return token;
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
},
|
|
311
|
+
release() {
|
|
312
|
+
if (released) return;
|
|
313
|
+
if (active && Array.from(sources).some((entry) => !entry.removed) || writers) throw new Error("Payload writer still owned");
|
|
314
|
+
released = true;
|
|
315
|
+
retainedBytes -= unused;
|
|
316
|
+
unused = 0;
|
|
317
|
+
reservations.delete(reservation);
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
reservations.add(reservation);
|
|
321
|
+
return reservation;
|
|
322
|
+
},
|
|
323
|
+
resources: () => ({
|
|
324
|
+
attempts,
|
|
325
|
+
entries: entries.size,
|
|
326
|
+
readers,
|
|
327
|
+
retainedBytes,
|
|
328
|
+
stopped,
|
|
329
|
+
writers
|
|
330
|
+
}),
|
|
331
|
+
retire(token) {
|
|
332
|
+
const entry = tokens.get(token);
|
|
333
|
+
if (!entry) return Promise.reject(/* @__PURE__ */ new Error("Unknown payload token"));
|
|
334
|
+
return retireEntry(entry);
|
|
335
|
+
},
|
|
336
|
+
root
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function validatePayloadLimits(limits) {
|
|
340
|
+
limits = Object.freeze({ ...limits });
|
|
341
|
+
integer(limits.maxObjectBytes, 64 * 1024 * 1024, "object budget");
|
|
342
|
+
integer(limits.maxLogicalUploadBytes, 128 * 1024 * 1024, "logical budget");
|
|
343
|
+
integer(limits.maxRetainedBytes, 128 * 1024 * 1024, "retained budget");
|
|
344
|
+
integer(limits.maxUploadAttempts, 16, "source attempts");
|
|
345
|
+
if (limits.maxUploadAttempts < 1 || limits.maxObjectBytes > limits.maxLogicalUploadBytes || limits.maxObjectBytes > limits.maxRetainedBytes) throw new Error("Inconsistent payload budgets");
|
|
346
|
+
return limits;
|
|
347
|
+
}
|
|
348
|
+
function integer(value, ceiling, name) {
|
|
349
|
+
if (!Number.isSafeInteger(value) || value < 0 || value > ceiling) throw new Error(`Invalid ${name}`);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
//#endregion
|
|
353
|
+
export { createNodePayloadOwner, validatePayloadLimits };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { PayloadLimits } from "../sdk/payload-store.js";
|
|
2
|
+
import { AppDefinition, LiveRun, LiveRunSummary, MockOptions, MockRun, MockRunSummary, RunLimits, RunResult, SiaRun, TestFile } from "./types.js";
|
|
3
|
+
import { LiveConsent } from "./live/types.js";
|
|
4
|
+
|
|
5
|
+
//#region src/testing/node.d.ts
|
|
6
|
+
interface LiveNodeRunOptions {
|
|
7
|
+
readonly backend: {
|
|
8
|
+
readonly consent: LiveConsent;
|
|
9
|
+
readonly kind: "live";
|
|
10
|
+
readonly profileFile: string;
|
|
11
|
+
};
|
|
12
|
+
readonly journalFile?: string;
|
|
13
|
+
readonly limits?: Partial<RunLimits>;
|
|
14
|
+
readonly signal?: AbortSignal;
|
|
15
|
+
}
|
|
16
|
+
interface MockNodeRunOptions {
|
|
17
|
+
readonly backend: {
|
|
18
|
+
readonly app: AppDefinition;
|
|
19
|
+
readonly kind: "mock";
|
|
20
|
+
readonly options?: MockOptions;
|
|
21
|
+
readonly payloadStorage?: {
|
|
22
|
+
readonly kind: "temporary-files";
|
|
23
|
+
readonly limits: PayloadLimits;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
readonly limits?: Partial<RunLimits>;
|
|
27
|
+
readonly signal?: AbortSignal;
|
|
28
|
+
}
|
|
29
|
+
interface RecoveryOptions {
|
|
30
|
+
readonly backend: LiveNodeRunOptions["backend"];
|
|
31
|
+
readonly journalFile: string;
|
|
32
|
+
}
|
|
33
|
+
type RunOptions = LiveNodeRunOptions | MockNodeRunOptions;
|
|
34
|
+
declare function fileFixture(path: string | URL): Promise<TestFile>;
|
|
35
|
+
declare function recoverSiaRun(options: RecoveryOptions): Promise<LiveRunSummary>;
|
|
36
|
+
declare function withSiaRun<T>(options: MockNodeRunOptions, use: (run: MockRun) => Promise<T>): Promise<RunResult<T, MockRunSummary>>;
|
|
37
|
+
declare function withSiaRun<T>(options: LiveNodeRunOptions, use: (run: LiveRun) => Promise<T>): Promise<RunResult<T, LiveRunSummary>>;
|
|
38
|
+
declare function withSiaRun<T>(options: RunOptions, use: (run: SiaRun) => Promise<T>): Promise<RunResult<T>>;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { LiveNodeRunOptions, MockNodeRunOptions, RecoveryOptions, RunOptions, fileFixture, recoverSiaRun, withSiaRun };
|