@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,129 @@
|
|
|
1
|
+
import { createTransferLink } from "./link.js";
|
|
2
|
+
import { withAbort } from "./abort.js";
|
|
3
|
+
|
|
4
|
+
//#region src/sdk/control.ts
|
|
5
|
+
/** Device faults affect SDK calls while control inspection remains available to the test. */
|
|
6
|
+
/** Each pending call keeps its own record even when bounded history evicts it. */
|
|
7
|
+
function createControls(requestLimit) {
|
|
8
|
+
if (!Number.isSafeInteger(requestLimit) || requestLimit < 1) throw new Error("requestLimit must be a positive safe integer");
|
|
9
|
+
const offline = /* @__PURE__ */ new Set();
|
|
10
|
+
const faults = [];
|
|
11
|
+
const holds = /* @__PURE__ */ new Set();
|
|
12
|
+
const history = [];
|
|
13
|
+
const conditions = {
|
|
14
|
+
downloadBytesPerSecond: 0,
|
|
15
|
+
latencyMs: 0,
|
|
16
|
+
uploadBytesPerSecond: 0
|
|
17
|
+
};
|
|
18
|
+
const download = createTransferLink(() => conditions.downloadBytesPerSecond);
|
|
19
|
+
const upload = createTransferLink(() => conditions.uploadBytesPerSecond);
|
|
20
|
+
let sequence = 0;
|
|
21
|
+
function matches(selector, request) {
|
|
22
|
+
return selector.operation === request.operation && (selector.device === void 0 || selector.device === request.device);
|
|
23
|
+
}
|
|
24
|
+
function checkOnline(device) {
|
|
25
|
+
if (offline.has(device)) throw new Error(`device ${device} is offline`);
|
|
26
|
+
}
|
|
27
|
+
function waitForHolds(request, signal) {
|
|
28
|
+
const matching = [...holds].filter((hold) => matches(hold, request));
|
|
29
|
+
if (matching.length === 0) return Promise.resolve();
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
const detach = () => {
|
|
32
|
+
for (const hold of matching) hold.waiters.delete(released);
|
|
33
|
+
signal?.removeEventListener("abort", aborted);
|
|
34
|
+
};
|
|
35
|
+
const released = () => {
|
|
36
|
+
if (matching.some((hold) => holds.has(hold))) return;
|
|
37
|
+
detach();
|
|
38
|
+
resolve();
|
|
39
|
+
};
|
|
40
|
+
const aborted = () => {
|
|
41
|
+
detach();
|
|
42
|
+
reject(new DOMException("mock operation cancelled", "AbortError"));
|
|
43
|
+
};
|
|
44
|
+
for (const hold of matching) hold.waiters.add(released);
|
|
45
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
46
|
+
if (signal?.aborted) aborted();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
async before(request, signal) {
|
|
51
|
+
signal?.throwIfAborted();
|
|
52
|
+
checkOnline(request.device);
|
|
53
|
+
if (conditions.latencyMs > 0) {
|
|
54
|
+
let timer;
|
|
55
|
+
try {
|
|
56
|
+
await withAbort(new Promise((resolve) => {
|
|
57
|
+
timer = setTimeout(resolve, conditions.latencyMs);
|
|
58
|
+
}), signal);
|
|
59
|
+
} finally {
|
|
60
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
await waitForHolds(request, signal);
|
|
64
|
+
signal?.throwIfAborted();
|
|
65
|
+
checkOnline(request.device);
|
|
66
|
+
const fault = faults.find((item) => matches(item, request));
|
|
67
|
+
if (fault) {
|
|
68
|
+
if (--fault.times === 0) faults.splice(faults.indexOf(fault), 1);
|
|
69
|
+
throw new Error(fault.message);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
begin(input) {
|
|
73
|
+
const record = {
|
|
74
|
+
...input,
|
|
75
|
+
bytes: 0,
|
|
76
|
+
sequence: ++sequence,
|
|
77
|
+
status: "pending"
|
|
78
|
+
};
|
|
79
|
+
history.push(record);
|
|
80
|
+
if (history.length > requestLimit) history.shift();
|
|
81
|
+
return record;
|
|
82
|
+
},
|
|
83
|
+
checkOnline,
|
|
84
|
+
control: {
|
|
85
|
+
addFault(input) {
|
|
86
|
+
const times = input.times ?? 1;
|
|
87
|
+
if (!Number.isSafeInteger(times) || times < 1) throw new Error("fault times must be a positive safe integer");
|
|
88
|
+
faults.push({
|
|
89
|
+
...input,
|
|
90
|
+
message: input.message ?? "injected network failure",
|
|
91
|
+
times
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
hold(input) {
|
|
95
|
+
const record = {
|
|
96
|
+
...input,
|
|
97
|
+
waiters: /* @__PURE__ */ new Set()
|
|
98
|
+
};
|
|
99
|
+
holds.add(record);
|
|
100
|
+
return () => {
|
|
101
|
+
holds.delete(record);
|
|
102
|
+
for (const waiter of record.waiters) waiter();
|
|
103
|
+
record.waiters.clear();
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
requests: () => history.map((record) => ({ ...record })),
|
|
107
|
+
setConditions(input) {
|
|
108
|
+
for (const key of [
|
|
109
|
+
"latencyMs",
|
|
110
|
+
"downloadBytesPerSecond",
|
|
111
|
+
"uploadBytesPerSecond"
|
|
112
|
+
]) {
|
|
113
|
+
const value = input[key];
|
|
114
|
+
if (value !== void 0 && (!Number.isFinite(value) || value < 0)) throw new Error(`${key} must be nonnegative and finite`);
|
|
115
|
+
}
|
|
116
|
+
Object.assign(conditions, input);
|
|
117
|
+
},
|
|
118
|
+
setOffline(device, value) {
|
|
119
|
+
if (value) offline.add(device);
|
|
120
|
+
else offline.delete(device);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
download,
|
|
124
|
+
upload
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
export { createControls };
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { createIndexdModel } from "../model/index.js";
|
|
2
|
+
import { createControls } from "./control.js";
|
|
3
|
+
import { createMockObject, nonnegativeInteger } from "./object.js";
|
|
4
|
+
import { payloadIsCurrent, payloadSlabId, readNetworkState, serializeNetworkState } from "./state.js";
|
|
5
|
+
import { createBufferedPayloadOwner } from "./payload-store.js";
|
|
6
|
+
|
|
7
|
+
//#region src/sdk/index.ts
|
|
8
|
+
/** In-process payload mocks for app tests; no Sia encryption or host protocols are performed. */
|
|
9
|
+
/** Shares payloads between SDK clients while the supplied model owns account and pin rules. */
|
|
10
|
+
function createMockNetwork(options = {}) {
|
|
11
|
+
const network = createMockNetworkWithPayloadOwner(options);
|
|
12
|
+
return {
|
|
13
|
+
control: network.control,
|
|
14
|
+
createSdk(options) {
|
|
15
|
+
const sdk = network.createSdk(options);
|
|
16
|
+
return {
|
|
17
|
+
deleteObject: (...args) => sdk.deleteObject(...args),
|
|
18
|
+
download: (...args) => sdk.download(...args),
|
|
19
|
+
object: (...args) => sdk.object(...args),
|
|
20
|
+
pinObject: (...args) => sdk.pinObject(...args),
|
|
21
|
+
pruneSlabs: (...args) => sdk.pruneSlabs(...args),
|
|
22
|
+
upload: (...args) => sdk.upload(...args)
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
discardUpload(accountId, id) {
|
|
26
|
+
network.discardUpload(accountId, id);
|
|
27
|
+
},
|
|
28
|
+
model: network.model,
|
|
29
|
+
seedObject: network.seedObject,
|
|
30
|
+
serialize: network.serialize
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Construction-time selection is private; portable callers always receive buffered storage. */
|
|
34
|
+
function createMockNetworkWithPayloadOwner(options = {}, suppliedOwner) {
|
|
35
|
+
const buffered = createBufferedPayloadOwner();
|
|
36
|
+
const owner = suppliedOwner ?? buffered;
|
|
37
|
+
if (suppliedOwner && options.state !== void 0) throw new Error("Cannot restore disk payloads");
|
|
38
|
+
if (options.model && (options.state !== void 0 || options.now !== void 0)) throw new Error("snapshot and clock options cannot replace a supplied model");
|
|
39
|
+
const saved = options.state === void 0 ? void 0 : readNetworkState(options.state, options.now);
|
|
40
|
+
const model = saved?.model ?? options.model ?? createIndexdModel(options.now ? { now: options.now } : {});
|
|
41
|
+
const controls = createControls(options.requestLimit ?? 256);
|
|
42
|
+
const chunkBytes = options.chunkBytes ?? 64 * 1024;
|
|
43
|
+
if (!Number.isSafeInteger(chunkBytes) || chunkBytes < 1) throw new Error("chunkBytes must be a positive safe integer");
|
|
44
|
+
const payloads = /* @__PURE__ */ new Map();
|
|
45
|
+
for (const [accountId, records] of saved?.payloads ?? []) payloads.set(accountId, new Map(Array.from(records, ([id, record]) => [id, {
|
|
46
|
+
identity: record.identity,
|
|
47
|
+
slabId: record.slabId,
|
|
48
|
+
token: buffered.finish(record.bytes)
|
|
49
|
+
}])));
|
|
50
|
+
const downloadOwners = /* @__PURE__ */ new WeakMap();
|
|
51
|
+
const payloadsByPin = /* @__PURE__ */ new Map();
|
|
52
|
+
const retirementTrackers = /* @__PURE__ */ new Map();
|
|
53
|
+
for (const [accountId, owned] of payloads) for (const [id, record] of owned) indexPayload(accountId, id, record.identity);
|
|
54
|
+
/** An object's slab can outlive its object record while another packed object uses it. */
|
|
55
|
+
function indexPayload(accountId, id, identity) {
|
|
56
|
+
let pins = payloadsByPin.get(accountId);
|
|
57
|
+
if (!pins) {
|
|
58
|
+
pins = /* @__PURE__ */ new Map();
|
|
59
|
+
payloadsByPin.set(accountId, pins);
|
|
60
|
+
}
|
|
61
|
+
let ids = pins.get(identity);
|
|
62
|
+
if (!ids) {
|
|
63
|
+
ids = /* @__PURE__ */ new Set();
|
|
64
|
+
pins.set(identity, ids);
|
|
65
|
+
let tracker = retirementTrackers.get(accountId);
|
|
66
|
+
if (!tracker) {
|
|
67
|
+
tracker = model.trackSlabPins(accountId);
|
|
68
|
+
retirementTrackers.set(accountId, tracker);
|
|
69
|
+
}
|
|
70
|
+
tracker.watch(identity);
|
|
71
|
+
}
|
|
72
|
+
ids.add(id);
|
|
73
|
+
}
|
|
74
|
+
/** Empty index buckets must release retired identities as well as the cached bytes. */
|
|
75
|
+
function forgetPayload(accountId, id, retired = []) {
|
|
76
|
+
const owned = payloads.get(accountId);
|
|
77
|
+
const record = owned?.get(id);
|
|
78
|
+
if (!record) return;
|
|
79
|
+
const retirement = owner.retire(record.token);
|
|
80
|
+
retired.push(retirement);
|
|
81
|
+
retirement.catch(() => void 0);
|
|
82
|
+
owned.delete(id);
|
|
83
|
+
const pins = payloadsByPin.get(accountId);
|
|
84
|
+
const ids = pins.get(record.identity);
|
|
85
|
+
ids.delete(id);
|
|
86
|
+
if (ids.size === 0) {
|
|
87
|
+
pins.delete(record.identity);
|
|
88
|
+
retirementTrackers.get(accountId).forget(record.identity);
|
|
89
|
+
}
|
|
90
|
+
if (pins.size === 0) payloadsByPin.delete(accountId);
|
|
91
|
+
}
|
|
92
|
+
function payload(accountId, id, retired = []) {
|
|
93
|
+
model.accountStats(accountId);
|
|
94
|
+
const record = payloads.get(accountId)?.get(id);
|
|
95
|
+
if (!record) throw new Error(`payload ${id} not found`);
|
|
96
|
+
if (!payloadIsCurrent(model, accountId, record)) {
|
|
97
|
+
forgetPayload(accountId, id, retired);
|
|
98
|
+
throw new Error(`payload ${id} not found`);
|
|
99
|
+
}
|
|
100
|
+
return record;
|
|
101
|
+
}
|
|
102
|
+
/** A sibling deletion can retire the pin of a payload whose object was deleted earlier. */
|
|
103
|
+
function discardStalePayloads(accountId, identities, retired) {
|
|
104
|
+
const pins = payloadsByPin.get(accountId);
|
|
105
|
+
const owned = payloads.get(accountId);
|
|
106
|
+
for (const identity of identities) {
|
|
107
|
+
const ids = pins?.get(identity);
|
|
108
|
+
if (!ids) continue;
|
|
109
|
+
const record = owned.get(ids.values().next().value);
|
|
110
|
+
if (payloadIsCurrent(model, accountId, record)) continue;
|
|
111
|
+
for (const id of ids) forgetPayload(accountId, id, retired);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function upload(accountId, id, token, retired = []) {
|
|
115
|
+
model.accountStats(accountId);
|
|
116
|
+
const object = createMockObject(id, token.length);
|
|
117
|
+
let owned = payloads.get(accountId);
|
|
118
|
+
if (owned?.has(id)) {
|
|
119
|
+
const prior = owned.get(id);
|
|
120
|
+
if (payloadIsCurrent(model, accountId, prior)) throw new Error(`payload ${id} already exists`);
|
|
121
|
+
}
|
|
122
|
+
const slabId = payloadSlabId(accountId, id);
|
|
123
|
+
const adopt = owner.prepareAdoption(token);
|
|
124
|
+
model.pinSlab(accountId, {
|
|
125
|
+
id: slabId,
|
|
126
|
+
size: token.length
|
|
127
|
+
});
|
|
128
|
+
if (!owned) {
|
|
129
|
+
owned = /* @__PURE__ */ new Map();
|
|
130
|
+
payloads.set(accountId, owned);
|
|
131
|
+
}
|
|
132
|
+
const record = {
|
|
133
|
+
identity: model.slabIdentity(accountId, slabId),
|
|
134
|
+
slabId,
|
|
135
|
+
token
|
|
136
|
+
};
|
|
137
|
+
forgetPayload(accountId, id, retired);
|
|
138
|
+
adopt();
|
|
139
|
+
owned.set(id, record);
|
|
140
|
+
indexPayload(accountId, id, record.identity);
|
|
141
|
+
return object;
|
|
142
|
+
}
|
|
143
|
+
function diskStream(lease, position, end, request, device, signal) {
|
|
144
|
+
const abort = new AbortController();
|
|
145
|
+
let resolveDone;
|
|
146
|
+
let rejectDone;
|
|
147
|
+
const done = new Promise((resolve, reject) => {
|
|
148
|
+
resolveDone = resolve;
|
|
149
|
+
rejectDone = reject;
|
|
150
|
+
});
|
|
151
|
+
done.catch(() => void 0);
|
|
152
|
+
let closure;
|
|
153
|
+
const closeLease = () => {
|
|
154
|
+
closure ??= lease.close().then(resolveDone, (error) => {
|
|
155
|
+
rejectDone(error);
|
|
156
|
+
throw error;
|
|
157
|
+
});
|
|
158
|
+
closure.catch(() => void 0);
|
|
159
|
+
return closure;
|
|
160
|
+
};
|
|
161
|
+
let target;
|
|
162
|
+
let settled = false;
|
|
163
|
+
const detach = () => {
|
|
164
|
+
signal?.removeEventListener("abort", callerCancel);
|
|
165
|
+
lease.signal?.removeEventListener("abort", retired);
|
|
166
|
+
};
|
|
167
|
+
const callerCancel = () => cancel(signal?.reason);
|
|
168
|
+
const retired = () => cancel(lease.signal?.reason);
|
|
169
|
+
const cancel = (reason = signal?.reason) => {
|
|
170
|
+
if (settled) return;
|
|
171
|
+
settled = true;
|
|
172
|
+
request.status = "cancelled";
|
|
173
|
+
abort.abort(reason);
|
|
174
|
+
target.error(reason);
|
|
175
|
+
detach();
|
|
176
|
+
closeLease().catch(() => void 0);
|
|
177
|
+
};
|
|
178
|
+
const stream = new ReadableStream({
|
|
179
|
+
async cancel() {
|
|
180
|
+
settled = true;
|
|
181
|
+
request.status = "cancelled";
|
|
182
|
+
abort.abort();
|
|
183
|
+
detach();
|
|
184
|
+
await closeLease();
|
|
185
|
+
},
|
|
186
|
+
async pull(controller) {
|
|
187
|
+
try {
|
|
188
|
+
controls.checkOnline(device);
|
|
189
|
+
if (position < end) {
|
|
190
|
+
const count = Math.min(65536, chunkBytes, end - position);
|
|
191
|
+
await controls.download(count, abort.signal);
|
|
192
|
+
if (settled) return;
|
|
193
|
+
const block = await lease.read(count);
|
|
194
|
+
if (settled) return;
|
|
195
|
+
if (!block) throw new Error("Unexpected payload EOF");
|
|
196
|
+
controls.checkOnline(device);
|
|
197
|
+
controller.enqueue(block);
|
|
198
|
+
position += block.length;
|
|
199
|
+
request.bytes += block.length;
|
|
200
|
+
}
|
|
201
|
+
if (position === end) {
|
|
202
|
+
await closeLease();
|
|
203
|
+
if (settled) return;
|
|
204
|
+
settled = true;
|
|
205
|
+
request.status = "completed";
|
|
206
|
+
detach();
|
|
207
|
+
controller.close();
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (settled) return;
|
|
211
|
+
settled = true;
|
|
212
|
+
request.status = "failed";
|
|
213
|
+
detach();
|
|
214
|
+
try {
|
|
215
|
+
await closeLease();
|
|
216
|
+
} catch (cleanup) {
|
|
217
|
+
controller.error(new AggregateError([error, cleanup], "Payload read and close failed"));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
controller.error(error);
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
start(controller) {
|
|
224
|
+
target = controller;
|
|
225
|
+
signal?.addEventListener("abort", callerCancel, { once: true });
|
|
226
|
+
lease.signal?.addEventListener("abort", retired, { once: true });
|
|
227
|
+
if (signal?.aborted) callerCancel();
|
|
228
|
+
else if (lease.signal?.aborted) retired();
|
|
229
|
+
}
|
|
230
|
+
}, { highWaterMark: 0 });
|
|
231
|
+
downloadOwners.set(stream, {
|
|
232
|
+
close() {
|
|
233
|
+
cancel(new DOMException("Reader closed", "AbortError"));
|
|
234
|
+
return closeLease();
|
|
235
|
+
},
|
|
236
|
+
done
|
|
237
|
+
});
|
|
238
|
+
return stream;
|
|
239
|
+
}
|
|
240
|
+
function pin(accountId, id, retired = []) {
|
|
241
|
+
const record = payload(accountId, id, retired);
|
|
242
|
+
model.pinObject(accountId, {
|
|
243
|
+
id,
|
|
244
|
+
slabs: [record.slabId]
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
async function drainRetirements(retired) {
|
|
248
|
+
const errors = (await Promise.allSettled(retired)).filter((result) => result.status === "rejected").map((result) => result.reason);
|
|
249
|
+
if (errors.length) throw new AggregateError(errors, "Payload retirement failed");
|
|
250
|
+
}
|
|
251
|
+
async function withRetirements(action) {
|
|
252
|
+
const retired = [];
|
|
253
|
+
let value;
|
|
254
|
+
try {
|
|
255
|
+
value = await action(retired);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
try {
|
|
258
|
+
await drainRetirements(retired);
|
|
259
|
+
} catch (cleanup) {
|
|
260
|
+
throw new AggregateError([error, cleanup], "Mock operation and payload retirement failed", { cause: cleanup });
|
|
261
|
+
}
|
|
262
|
+
throw error;
|
|
263
|
+
}
|
|
264
|
+
await drainRetirements(retired);
|
|
265
|
+
return value;
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
closeDownload: (stream) => downloadOwners.get(stream)?.close() ?? Promise.resolve(),
|
|
269
|
+
control: controls.control,
|
|
270
|
+
createSdk({ accountId, device = "default" }) {
|
|
271
|
+
model.accountStats(accountId);
|
|
272
|
+
async function run(operation, objectId, action, stream = false, signal) {
|
|
273
|
+
const request = controls.begin({
|
|
274
|
+
accountId,
|
|
275
|
+
device,
|
|
276
|
+
operation,
|
|
277
|
+
...objectId === void 0 ? {} : { objectId }
|
|
278
|
+
});
|
|
279
|
+
try {
|
|
280
|
+
await controls.before(request, signal);
|
|
281
|
+
signal?.throwIfAborted();
|
|
282
|
+
const value = owner.kind === "temporary-files" ? await withRetirements((retired) => action(request, retired)) : await action(request, []);
|
|
283
|
+
if (!stream) request.status = "completed";
|
|
284
|
+
return value;
|
|
285
|
+
} catch (error) {
|
|
286
|
+
request.status = signal?.aborted ? "cancelled" : "failed";
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
async deleteObject(id, call = {}) {
|
|
292
|
+
return run("deleteObject", id, (_request, retired) => {
|
|
293
|
+
const committed = call.prepareCommit?.();
|
|
294
|
+
model.deleteObject(accountId, id);
|
|
295
|
+
committed?.();
|
|
296
|
+
discardStalePayloads(accountId, retirementTrackers.get(accountId)?.takeRetired() ?? [], retired);
|
|
297
|
+
}, false, call.signal);
|
|
298
|
+
},
|
|
299
|
+
async download(object, range = {}) {
|
|
300
|
+
const id = object.id();
|
|
301
|
+
const requested = { ...range };
|
|
302
|
+
return run("download", id, async (request, retired) => {
|
|
303
|
+
const record = payload(accountId, id, retired);
|
|
304
|
+
const offset = requested.offset ?? 0;
|
|
305
|
+
const length = requested.length ?? record.token.length;
|
|
306
|
+
nonnegativeInteger(offset, "offset");
|
|
307
|
+
nonnegativeInteger(length, "length");
|
|
308
|
+
let position = Math.min(offset, record.token.length);
|
|
309
|
+
const end = position + Math.min(length, record.token.length - position);
|
|
310
|
+
if (owner.kind === "temporary-files") return diskStream(await owner.openReader(record.token, offset, length), position, end, request, device, requested.signal);
|
|
311
|
+
const bufferedBytes = buffered.bytes(record.token);
|
|
312
|
+
const abort = new AbortController();
|
|
313
|
+
let target;
|
|
314
|
+
const cancelFromCaller = () => {
|
|
315
|
+
request.status = "cancelled";
|
|
316
|
+
abort.abort();
|
|
317
|
+
target.error(new DOMException("mock download cancelled", "AbortError"));
|
|
318
|
+
detach();
|
|
319
|
+
};
|
|
320
|
+
const detach = () => requested.signal?.removeEventListener("abort", cancelFromCaller);
|
|
321
|
+
return new ReadableStream({
|
|
322
|
+
cancel() {
|
|
323
|
+
request.status = "cancelled";
|
|
324
|
+
abort.abort();
|
|
325
|
+
detach();
|
|
326
|
+
},
|
|
327
|
+
async pull(controller) {
|
|
328
|
+
try {
|
|
329
|
+
controls.checkOnline(device);
|
|
330
|
+
if (position < end) {
|
|
331
|
+
const next = Math.min(position + chunkBytes, end);
|
|
332
|
+
await controls.download(next - position, abort.signal);
|
|
333
|
+
if (abort.signal.aborted) return;
|
|
334
|
+
controls.checkOnline(device);
|
|
335
|
+
controller.enqueue(bufferedBytes.slice(position, next));
|
|
336
|
+
request.bytes += next - position;
|
|
337
|
+
position = next;
|
|
338
|
+
}
|
|
339
|
+
if (position === end) {
|
|
340
|
+
request.status = "completed";
|
|
341
|
+
detach();
|
|
342
|
+
controller.close();
|
|
343
|
+
}
|
|
344
|
+
} catch (error) {
|
|
345
|
+
if (abort.signal.aborted) return;
|
|
346
|
+
request.status = "failed";
|
|
347
|
+
detach();
|
|
348
|
+
controller.error(error);
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
start(controller) {
|
|
352
|
+
target = controller;
|
|
353
|
+
requested.signal?.addEventListener("abort", cancelFromCaller, { once: true });
|
|
354
|
+
if (requested.signal?.aborted) cancelFromCaller();
|
|
355
|
+
}
|
|
356
|
+
}, { highWaterMark: 0 });
|
|
357
|
+
}, true, requested.signal);
|
|
358
|
+
},
|
|
359
|
+
async object(id, call = {}) {
|
|
360
|
+
return run("object", id, (_request, retired) => {
|
|
361
|
+
if (!model.object(accountId, id)) {
|
|
362
|
+
const cached = payloads.get(accountId)?.get(id);
|
|
363
|
+
if (cached && !payloadIsCurrent(model, accountId, cached)) forgetPayload(accountId, id, retired);
|
|
364
|
+
throw new Error(`object ${id} not found`);
|
|
365
|
+
}
|
|
366
|
+
return createMockObject(id, payload(accountId, id, retired).token.length);
|
|
367
|
+
}, false, call.signal);
|
|
368
|
+
},
|
|
369
|
+
async pinObject(object, call = {}) {
|
|
370
|
+
const id = object.id();
|
|
371
|
+
return run("pinObject", id, (_request, retired) => {
|
|
372
|
+
const committed = call.prepareCommit?.();
|
|
373
|
+
pin(accountId, id, retired);
|
|
374
|
+
committed?.();
|
|
375
|
+
}, false, call.signal);
|
|
376
|
+
},
|
|
377
|
+
async pruneSlabs(call = {}) {
|
|
378
|
+
return run("pruneSlabs", void 0, (_request, retired) => {
|
|
379
|
+
model.pruneSlabs(accountId);
|
|
380
|
+
discardStalePayloads(accountId, payloadsByPin.get(accountId)?.keys() ?? [], retired);
|
|
381
|
+
}, false, call.signal);
|
|
382
|
+
},
|
|
383
|
+
async upload(bytes, { id, prepareCommit, signal }) {
|
|
384
|
+
if (owner.kind !== "buffered") throw new Error("Disk upload requires an admitted payload token");
|
|
385
|
+
const owned = buffered.finish(bytes);
|
|
386
|
+
return run("upload", id, async (request, retired) => {
|
|
387
|
+
await controls.upload(owned.length, signal);
|
|
388
|
+
signal?.throwIfAborted();
|
|
389
|
+
controls.checkOnline(device);
|
|
390
|
+
const committed = prepareCommit?.();
|
|
391
|
+
const object = upload(accountId, id, owned, retired);
|
|
392
|
+
committed?.();
|
|
393
|
+
request.bytes = owned.length;
|
|
394
|
+
return object;
|
|
395
|
+
}, false, signal);
|
|
396
|
+
},
|
|
397
|
+
async uploadPayload(token, { id, prepareCommit, signal }) {
|
|
398
|
+
owner.prepareAdoption(token);
|
|
399
|
+
return run("upload", id, async (request, retired) => {
|
|
400
|
+
await controls.upload(token.length, signal);
|
|
401
|
+
signal?.throwIfAborted();
|
|
402
|
+
controls.checkOnline(device);
|
|
403
|
+
const committed = prepareCommit?.();
|
|
404
|
+
const object = upload(accountId, id, token, retired);
|
|
405
|
+
committed?.();
|
|
406
|
+
request.bytes = token.length;
|
|
407
|
+
return object;
|
|
408
|
+
}, false, signal);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
},
|
|
412
|
+
discardUpload(accountId, id) {
|
|
413
|
+
const discard = (retired) => {
|
|
414
|
+
const record = payload(accountId, id, retired);
|
|
415
|
+
if (model.object(accountId, id)) throw new Error("Cannot discard a pinned object");
|
|
416
|
+
if (model.slabIdentity(accountId, record.slabId) !== record.identity) throw new Error("Upload slab identity changed");
|
|
417
|
+
model.unpinSlab(accountId, record.slabId);
|
|
418
|
+
forgetPayload(accountId, id, retired);
|
|
419
|
+
};
|
|
420
|
+
if (owner.kind === "temporary-files") return withRetirements(discard);
|
|
421
|
+
discard([]);
|
|
422
|
+
return Promise.resolve();
|
|
423
|
+
},
|
|
424
|
+
downloadDone: (stream) => downloadOwners.get(stream)?.done ?? Promise.resolve(),
|
|
425
|
+
drain: () => owner.drain(),
|
|
426
|
+
model,
|
|
427
|
+
seedObject(accountId, id, bytes) {
|
|
428
|
+
if (owner.kind !== "buffered") throw new Error("Disk seed requires an admitted payload token");
|
|
429
|
+
const object = upload(accountId, id, buffered.finish(bytes));
|
|
430
|
+
try {
|
|
431
|
+
pin(accountId, id);
|
|
432
|
+
} catch (error) {
|
|
433
|
+
const record = payloads.get(accountId).get(id);
|
|
434
|
+
model.unpinSlab(accountId, record.slabId);
|
|
435
|
+
forgetPayload(accountId, id);
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
return object;
|
|
439
|
+
},
|
|
440
|
+
serialize() {
|
|
441
|
+
if (owner.kind !== "buffered") throw new Error("Cannot serialize temporary-file payload storage");
|
|
442
|
+
const records = /* @__PURE__ */ new Map();
|
|
443
|
+
for (const [accountId, owned] of payloads) records.set(accountId, new Map(Array.from(owned, ([id, record]) => [id, {
|
|
444
|
+
bytes: buffered.bytes(record.token),
|
|
445
|
+
identity: record.identity,
|
|
446
|
+
slabId: record.slabId
|
|
447
|
+
}])));
|
|
448
|
+
return serializeNetworkState(model, records);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
//#endregion
|
|
454
|
+
export { createMockNetwork, createMockNetworkWithPayloadOwner };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/sdk/link.ts
|
|
2
|
+
function createTransferLink(rate) {
|
|
3
|
+
const queue = [];
|
|
4
|
+
let active;
|
|
5
|
+
let timer;
|
|
6
|
+
function finish(transfer, error) {
|
|
7
|
+
transfer.signal?.removeEventListener("abort", transfer.abort);
|
|
8
|
+
if (active === transfer) {
|
|
9
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
10
|
+
timer = void 0;
|
|
11
|
+
active = void 0;
|
|
12
|
+
} else {
|
|
13
|
+
const index = queue.indexOf(transfer);
|
|
14
|
+
if (index >= 0) queue.splice(index, 1);
|
|
15
|
+
}
|
|
16
|
+
if (error !== void 0) transfer.reject(error);
|
|
17
|
+
else transfer.resolve();
|
|
18
|
+
start();
|
|
19
|
+
}
|
|
20
|
+
function start() {
|
|
21
|
+
if (active) return;
|
|
22
|
+
const transfer = queue.shift();
|
|
23
|
+
if (!transfer) return;
|
|
24
|
+
active = transfer;
|
|
25
|
+
const bytesPerSecond = rate();
|
|
26
|
+
function wait(remaining) {
|
|
27
|
+
const delay = Math.min(remaining, 2147483647);
|
|
28
|
+
timer = setTimeout(() => {
|
|
29
|
+
if (remaining > delay) wait(remaining - delay);
|
|
30
|
+
else finish(transfer);
|
|
31
|
+
}, delay);
|
|
32
|
+
}
|
|
33
|
+
if (bytesPerSecond === 0 || transfer.bytes === 0) finish(transfer);
|
|
34
|
+
else wait(Math.ceil(transfer.bytes / bytesPerSecond * 1e3));
|
|
35
|
+
}
|
|
36
|
+
return (bytes, signal) => new Promise((resolve, reject) => {
|
|
37
|
+
if (signal?.aborted) {
|
|
38
|
+
reject(new DOMException("transfer cancelled", "AbortError"));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const transfer = {
|
|
42
|
+
abort: () => finish(transfer, signal?.reason ?? /* @__PURE__ */ new Error("transfer cancelled")),
|
|
43
|
+
bytes,
|
|
44
|
+
reject,
|
|
45
|
+
resolve,
|
|
46
|
+
...signal ? { signal } : {}
|
|
47
|
+
};
|
|
48
|
+
queue.push(transfer);
|
|
49
|
+
signal?.addEventListener("abort", transfer.abort, { once: true });
|
|
50
|
+
start();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
export { createTransferLink };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/sdk/object.ts
|
|
2
|
+
/** Readers derive size from slab lengths, so an empty slab map would make nonempty payloads look empty. */
|
|
3
|
+
function createMockObject(id, size) {
|
|
4
|
+
if (!id) throw new Error("object id must not be empty");
|
|
5
|
+
nonnegativeInteger(size, "size");
|
|
6
|
+
return {
|
|
7
|
+
id: () => id,
|
|
8
|
+
size: () => size,
|
|
9
|
+
slabs: () => size === 0 ? [] : [{
|
|
10
|
+
encryptionKey: "",
|
|
11
|
+
length: size,
|
|
12
|
+
minShards: 1,
|
|
13
|
+
offset: 0,
|
|
14
|
+
sectors: [],
|
|
15
|
+
version: 1
|
|
16
|
+
}]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function nonnegativeInteger(value, name) {
|
|
20
|
+
if (!Number.isSafeInteger(value) || value < 0) throw new Error(`${name} must be a nonnegative safe integer`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { createMockObject, nonnegativeInteger };
|