@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,509 @@
|
|
|
1
|
+
import { readModelState } from "./state.js";
|
|
2
|
+
|
|
3
|
+
//#region src/model/index.ts
|
|
4
|
+
/** In-memory Indexd control-plane state for deterministic SDK fixtures. */
|
|
5
|
+
/** Models Indexd account ownership without treating opaque wire values as verified crypto. */
|
|
6
|
+
function createIndexdModel(options = {}) {
|
|
7
|
+
if (options.state !== void 0 && (options.hosts !== void 0 || options.quotaBytes !== void 0 || options.eventPublication !== void 0)) throw new Error("snapshot settings cannot be replaced");
|
|
8
|
+
const saved = options.state === void 0 ? void 0 : readModelState(options.state);
|
|
9
|
+
const now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
10
|
+
const accounts = new Map(saved?.accounts.map((row) => [row.account.id, row.account]));
|
|
11
|
+
const requests = new Map(saved?.requests.map((row) => [row.id, {
|
|
12
|
+
approved: row.approved,
|
|
13
|
+
expiresAt: new Date(row.expiresAt),
|
|
14
|
+
publicKey: row.publicKey
|
|
15
|
+
}]));
|
|
16
|
+
let requestNumber = saved?.requestNumber ?? 0;
|
|
17
|
+
const objects = new Map(saved?.accounts.map((row) => [row.account.id, new Map(row.objects.map((object) => [object.id, object]))]));
|
|
18
|
+
const slabs = new Map(saved?.slabs.map((slab) => [slab.id, slab]));
|
|
19
|
+
const accountSlabs = new Map(saved?.accounts.map((row) => [row.account.id, new Map(row.slabs.map((id) => [id, Symbol()]))]));
|
|
20
|
+
const pinTrackers = /* @__PURE__ */ new Map();
|
|
21
|
+
const events = new Map(saved?.accounts.map((row) => [row.account.id, saved.version === 2 ? [] : row.events]));
|
|
22
|
+
const eventPublication = saved?.version === 2 ? "manual" : options.eventPublication ?? "immediate";
|
|
23
|
+
const pendingEvents = new Map(saved?.accounts.map((row) => [row.account.id, /* @__PURE__ */ new Map()]));
|
|
24
|
+
const publishedEvents = new Map(saved?.accounts.map((row) => [row.account.id, new Map(eventPublication === "manual" ? row.events.map((event) => [event.key, event]) : [])]));
|
|
25
|
+
for (const row of saved?.publication?.pending ?? []) pendingEvents.get(row.accountId).set(row.event.key, row.event);
|
|
26
|
+
let lastPublishedAt = saved?.publication?.lastPublishedAt === void 0 ? void 0 : Date.parse(saved.publication.lastPublishedAt);
|
|
27
|
+
const sharing = new Map(saved?.sharing.map((row) => [row.key.publicKey, {
|
|
28
|
+
attachments: new Map(row.attachments.map((attachment) => [attachment.objectID, attachment])),
|
|
29
|
+
key: row.key,
|
|
30
|
+
owner: row.owner,
|
|
31
|
+
revoked: row.revoked
|
|
32
|
+
}]));
|
|
33
|
+
const hostList = (saved?.hosts ?? options.hosts ?? []).map(copyHost);
|
|
34
|
+
const quotaBytes = saved?.quotaBytes ?? options.quotaBytes;
|
|
35
|
+
function requireAccount(id) {
|
|
36
|
+
if (!accounts.has(id)) throw new Error(`unknown account ${id}`);
|
|
37
|
+
}
|
|
38
|
+
function createAccount({ publicKey }) {
|
|
39
|
+
if (accounts.has(publicKey)) throw new Error(`account ${publicKey} already exists`);
|
|
40
|
+
const account = {
|
|
41
|
+
id: publicKey,
|
|
42
|
+
publicKey
|
|
43
|
+
};
|
|
44
|
+
accounts.set(account.id, account);
|
|
45
|
+
objects.set(account.id, /* @__PURE__ */ new Map());
|
|
46
|
+
accountSlabs.set(account.id, /* @__PURE__ */ new Map());
|
|
47
|
+
events.set(account.id, []);
|
|
48
|
+
pendingEvents.set(account.id, /* @__PURE__ */ new Map());
|
|
49
|
+
publishedEvents.set(account.id, /* @__PURE__ */ new Map());
|
|
50
|
+
return { ...account };
|
|
51
|
+
}
|
|
52
|
+
function ownedShare(ownerId, publicKey) {
|
|
53
|
+
const record = sharing.get(publicKey);
|
|
54
|
+
if (!record || record.owner !== ownerId) throw new Error("sharing key not found");
|
|
55
|
+
return record;
|
|
56
|
+
}
|
|
57
|
+
function activeShare(publicKey) {
|
|
58
|
+
const record = sharing.get(publicKey) ?? [...sharing.values()].find((item) => recipientMatches(item.key.publicKey, publicKey));
|
|
59
|
+
if (!record || record.revoked || expired(record.key.expiresAt, now())) throw new Error("sharing key unavailable");
|
|
60
|
+
return record;
|
|
61
|
+
}
|
|
62
|
+
function recipientShare(recipientId, publicKey) {
|
|
63
|
+
const record = activeShare(publicKey);
|
|
64
|
+
if (recipientId !== publicKey && !recipientMatches(record.key.publicKey, recipientId)) throw new Error("sharing key unavailable");
|
|
65
|
+
return record;
|
|
66
|
+
}
|
|
67
|
+
/** Retires one account's pin without discarding a slab another account still owns. */
|
|
68
|
+
function removeSlabPin(accountId, slabId) {
|
|
69
|
+
const identity = accountSlabs.get(accountId).get(slabId);
|
|
70
|
+
accountSlabs.get(accountId).delete(slabId);
|
|
71
|
+
if (identity !== void 0) retirePin(accountId, identity);
|
|
72
|
+
if (![...accountSlabs.values()].some((pins) => pins.has(slabId))) slabs.delete(slabId);
|
|
73
|
+
}
|
|
74
|
+
/** Only watched pins enter queues, so unrelated model uploads cannot fill a cache's retirement history. */
|
|
75
|
+
function retirePin(accountId, identity) {
|
|
76
|
+
const trackers = pinTrackers.get(accountId);
|
|
77
|
+
for (const reference of trackers ?? []) {
|
|
78
|
+
const state = reference.deref();
|
|
79
|
+
if (!state) trackers.delete(reference);
|
|
80
|
+
else if (state.watched.delete(identity)) state.retired.add(identity);
|
|
81
|
+
}
|
|
82
|
+
if (trackers?.size === 0) pinTrackers.delete(accountId);
|
|
83
|
+
}
|
|
84
|
+
/** A changed event loses its old cursor position until the publisher assigns a new one. */
|
|
85
|
+
function recordEvent(accountId, event) {
|
|
86
|
+
if (eventPublication === "manual") {
|
|
87
|
+
publishedEvents.get(accountId).delete(event.key);
|
|
88
|
+
pendingEvents.get(accountId).set(event.key, event);
|
|
89
|
+
} else events.get(accountId).push(event);
|
|
90
|
+
}
|
|
91
|
+
function slabBytes(ids) {
|
|
92
|
+
let size = 0;
|
|
93
|
+
for (const id of ids) size += slabs.get(id)?.size ?? 0;
|
|
94
|
+
return size;
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
accountStats(accountId) {
|
|
98
|
+
requireAccount(accountId);
|
|
99
|
+
const records = objects.get(accountId);
|
|
100
|
+
const ids = /* @__PURE__ */ new Set();
|
|
101
|
+
for (const object of records.values()) for (const slab of object.slabs ?? []) ids.add(slab);
|
|
102
|
+
return {
|
|
103
|
+
objectCount: records.size,
|
|
104
|
+
pinnedSize: slabBytes(ids)
|
|
105
|
+
};
|
|
106
|
+
},
|
|
107
|
+
approveConnection(id, _connectKey) {
|
|
108
|
+
const request = requests.get(id);
|
|
109
|
+
if (!request) throw new Error(`unknown request ${id}`);
|
|
110
|
+
if (now() > request.expiresAt) throw new Error(`request ${id} expired`);
|
|
111
|
+
request.approved = true;
|
|
112
|
+
},
|
|
113
|
+
attachSharedObject(ownerId, publicKey, attachment) {
|
|
114
|
+
const record = activeShare(publicKey);
|
|
115
|
+
if (record.owner !== ownerId) throw new Error("sharing key unavailable");
|
|
116
|
+
if (!objects.get(ownerId).has(attachment.objectID)) throw new Error(`object ${attachment.objectID} not found`);
|
|
117
|
+
record.attachments.set(attachment.objectID, { ...attachment });
|
|
118
|
+
record.key.objectCount = record.attachments.size;
|
|
119
|
+
record.key.updatedAt = now().toISOString();
|
|
120
|
+
},
|
|
121
|
+
caller(publicKey) {
|
|
122
|
+
const account = accounts.get(publicKey);
|
|
123
|
+
return account && { ...account };
|
|
124
|
+
},
|
|
125
|
+
connectionRequest(id) {
|
|
126
|
+
const request = requests.get(id);
|
|
127
|
+
if (!request) return void 0;
|
|
128
|
+
if (now() > request.expiresAt) throw new Error(`request ${id} expired`);
|
|
129
|
+
return {
|
|
130
|
+
...request,
|
|
131
|
+
expiresAt: new Date(request.expiresAt)
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
createAccount,
|
|
135
|
+
createSharingKey(ownerId, input) {
|
|
136
|
+
requireAccount(ownerId);
|
|
137
|
+
if (sharing.has(input.publicKey)) throw new Error(`sharing key ${input.publicKey} already exists`);
|
|
138
|
+
const stamp = now().toISOString();
|
|
139
|
+
const key = {
|
|
140
|
+
account: ownerId,
|
|
141
|
+
createdAt: stamp,
|
|
142
|
+
description: input.description ?? "",
|
|
143
|
+
nonce: input.nonce,
|
|
144
|
+
objectCount: 0,
|
|
145
|
+
objectSize: 0,
|
|
146
|
+
pinnedData: 0,
|
|
147
|
+
pinnedSize: 0,
|
|
148
|
+
publicKey: input.publicKey,
|
|
149
|
+
updatedAt: stamp,
|
|
150
|
+
...input.expiresAt ? { expiresAt: input.expiresAt } : {}
|
|
151
|
+
};
|
|
152
|
+
sharing.set(input.publicKey, {
|
|
153
|
+
attachments: /* @__PURE__ */ new Map(),
|
|
154
|
+
key,
|
|
155
|
+
owner: ownerId,
|
|
156
|
+
revoked: false
|
|
157
|
+
});
|
|
158
|
+
return { ...key };
|
|
159
|
+
},
|
|
160
|
+
deleteObject(accountId, objectId) {
|
|
161
|
+
requireAccount(accountId);
|
|
162
|
+
const records = objects.get(accountId);
|
|
163
|
+
const object = records.get(objectId);
|
|
164
|
+
if (!object) throw new Error(`object ${objectId} not found`);
|
|
165
|
+
records.delete(objectId);
|
|
166
|
+
const updatedAt = now().toISOString();
|
|
167
|
+
for (const record of sharing.values()) if (record.owner === accountId && record.attachments.delete(objectId)) {
|
|
168
|
+
record.key.objectCount = record.attachments.size;
|
|
169
|
+
record.key.updatedAt = updatedAt;
|
|
170
|
+
}
|
|
171
|
+
for (const slabId of object.slabs ?? []) if (![...records.values()].some((item) => item.slabs?.includes(slabId))) removeSlabPin(accountId, slabId);
|
|
172
|
+
recordEvent(accountId, {
|
|
173
|
+
deleted: true,
|
|
174
|
+
key: objectId,
|
|
175
|
+
updatedAt
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
detachSharedObject(ownerId, publicKey, objectID) {
|
|
179
|
+
const record = ownedShare(ownerId, publicKey);
|
|
180
|
+
record.attachments.delete(objectID);
|
|
181
|
+
record.key.objectCount = record.attachments.size;
|
|
182
|
+
record.key.updatedAt = now().toISOString();
|
|
183
|
+
},
|
|
184
|
+
getSharingKey(ownerId, publicKey) {
|
|
185
|
+
return { ...ownedShare(ownerId, publicKey).key };
|
|
186
|
+
},
|
|
187
|
+
getSlab(accountId, slabId) {
|
|
188
|
+
requireAccount(accountId);
|
|
189
|
+
if (!accountSlabs.get(accountId).has(slabId) || !slabs.has(slabId)) throw new Error(`slab ${slabId} not found`);
|
|
190
|
+
return { ...slabs.get(slabId) };
|
|
191
|
+
},
|
|
192
|
+
hasOwnedSharedObject(ownerId, publicKey, objectId) {
|
|
193
|
+
const record = ownedShare(ownerId, publicKey);
|
|
194
|
+
if (record.revoked) throw new Error("sharing key unavailable");
|
|
195
|
+
return record.attachments.has(objectId);
|
|
196
|
+
},
|
|
197
|
+
hosts(accountId, filter = {}) {
|
|
198
|
+
requireAccount(accountId);
|
|
199
|
+
return hostList.filter((host) => (!filter.countryCode || host.countryCode === filter.countryCode) && (filter.goodForUpload === void 0 || host.goodForUpload === filter.goodForUpload) && (!filter.publicKey || host.publicKey === filter.publicKey)).map(copyHost);
|
|
200
|
+
},
|
|
201
|
+
listOwnedSharedObjects(ownerId, publicKey) {
|
|
202
|
+
const record = ownedShare(ownerId, publicKey);
|
|
203
|
+
if (record.revoked) throw new Error("sharing key unavailable");
|
|
204
|
+
return [...record.attachments.values()].map((item) => ({ ...item }));
|
|
205
|
+
},
|
|
206
|
+
listRecipientObjects(recipientId, publicKey) {
|
|
207
|
+
return [...recipientShare(recipientId, publicKey).attachments.values()].map((item) => ({ ...item }));
|
|
208
|
+
},
|
|
209
|
+
listSharedObjects(ownerId, publicKey) {
|
|
210
|
+
const record = activeShare(publicKey);
|
|
211
|
+
if (ownerId !== record.owner) throw new Error("sharing key unavailable");
|
|
212
|
+
return [...record.attachments.values()].map((item) => ({ ...item }));
|
|
213
|
+
},
|
|
214
|
+
listSharingKeys(ownerId) {
|
|
215
|
+
requireAccount(ownerId);
|
|
216
|
+
return [...sharing.values()].filter((record) => record.owner === ownerId && !record.revoked).map((record) => ({ ...record.key }));
|
|
217
|
+
},
|
|
218
|
+
listSlabs(accountId) {
|
|
219
|
+
requireAccount(accountId);
|
|
220
|
+
return [...accountSlabs.get(accountId).keys()].filter((id) => slabs.has(id)).map((id) => ({ ...slabs.get(id) }));
|
|
221
|
+
},
|
|
222
|
+
object(accountId, objectId) {
|
|
223
|
+
const object = objects.get(accountId)?.get(objectId);
|
|
224
|
+
return object && copyObject(object);
|
|
225
|
+
},
|
|
226
|
+
objectEvents(accountId) {
|
|
227
|
+
requireAccount(accountId);
|
|
228
|
+
return [...eventPublication === "manual" ? publishedEvents.get(accountId).values() : events.get(accountId)].map((event) => ({
|
|
229
|
+
...event,
|
|
230
|
+
...event.object ? { object: copyObject(event.object) } : {}
|
|
231
|
+
}));
|
|
232
|
+
},
|
|
233
|
+
pinObject(accountId, object) {
|
|
234
|
+
requireAccount(accountId);
|
|
235
|
+
if (object.encryptedMetadata && decodedBase64Length(object.encryptedMetadata) > 1024) throw new Error("object metadata size limit (1024) exceeded");
|
|
236
|
+
for (const slabId of object.slabs) if (!accountSlabs.get(accountId).has(slabId)) throw new Error(`unpinned slab ${slabId}`);
|
|
237
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
238
|
+
for (const item of objects.get(accountId).values()) if (item.id !== object.id) for (const id of item.slabs ?? []) referenced.add(id);
|
|
239
|
+
for (const id of object.slabs) referenced.add(id);
|
|
240
|
+
const nextSize = slabBytes(referenced);
|
|
241
|
+
if (quotaBytes !== void 0 && nextSize > quotaBytes) throw new Error("account quota exceeded");
|
|
242
|
+
const stored = {
|
|
243
|
+
...objects.get(accountId).get(object.id),
|
|
244
|
+
id: object.id,
|
|
245
|
+
slabs: [...object.slabs]
|
|
246
|
+
};
|
|
247
|
+
for (const field of [
|
|
248
|
+
"encryptedMetadata",
|
|
249
|
+
"encryptedDataKey",
|
|
250
|
+
"dataSignature",
|
|
251
|
+
"encryptedMetadataKey",
|
|
252
|
+
"metadataSignature"
|
|
253
|
+
]) {
|
|
254
|
+
const value = object[field];
|
|
255
|
+
if (value !== void 0) stored[field] = value;
|
|
256
|
+
}
|
|
257
|
+
objects.get(accountId).set(object.id, stored);
|
|
258
|
+
recordEvent(accountId, {
|
|
259
|
+
deleted: false,
|
|
260
|
+
key: object.id,
|
|
261
|
+
object: copyObject(stored),
|
|
262
|
+
updatedAt: now().toISOString()
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
pinSlab(accountId, slab) {
|
|
266
|
+
requireAccount(accountId);
|
|
267
|
+
const prior = slabs.get(slab.id);
|
|
268
|
+
if (prior && prior.size !== slab.size) throw new Error(`conflicting slab ${slab.id}`);
|
|
269
|
+
slabs.set(slab.id, { ...slab });
|
|
270
|
+
const pins = accountSlabs.get(accountId);
|
|
271
|
+
if (!pins.has(slab.id)) pins.set(slab.id, Symbol());
|
|
272
|
+
},
|
|
273
|
+
pruneSlabs(accountId) {
|
|
274
|
+
requireAccount(accountId);
|
|
275
|
+
const removed = [];
|
|
276
|
+
for (const slabId of [...accountSlabs.get(accountId).keys()]) if (![...objects.get(accountId).values()].some((object) => object.slabs?.includes(slabId))) {
|
|
277
|
+
removeSlabPin(accountId, slabId);
|
|
278
|
+
removed.push(slabId);
|
|
279
|
+
}
|
|
280
|
+
return removed;
|
|
281
|
+
},
|
|
282
|
+
publishObjectEvents() {
|
|
283
|
+
if (eventPublication !== "manual" || ![...pendingEvents.values()].some((rows) => rows.size > 0)) return;
|
|
284
|
+
const second = Math.floor(now().getTime() / 1e3) * 1e3;
|
|
285
|
+
if (lastPublishedAt !== void 0 && second <= lastPublishedAt) return;
|
|
286
|
+
const updatedAt = new Date(second).toISOString();
|
|
287
|
+
let remaining = 25e3;
|
|
288
|
+
for (const accountId of accounts.keys()) {
|
|
289
|
+
const pending = pendingEvents.get(accountId);
|
|
290
|
+
for (const key of [...pending.keys()].sort()) {
|
|
291
|
+
if (remaining === 0) break;
|
|
292
|
+
const event = pending.get(key);
|
|
293
|
+
publishedEvents.get(accountId).set(key, {
|
|
294
|
+
...event,
|
|
295
|
+
updatedAt
|
|
296
|
+
});
|
|
297
|
+
pending.delete(key);
|
|
298
|
+
remaining--;
|
|
299
|
+
}
|
|
300
|
+
if (remaining === 0) break;
|
|
301
|
+
}
|
|
302
|
+
lastPublishedAt = second;
|
|
303
|
+
},
|
|
304
|
+
registerConnection(id, appKey) {
|
|
305
|
+
const request = requests.get(id);
|
|
306
|
+
if (!request) throw new Error(`unknown request ${id}`);
|
|
307
|
+
if (now() > request.expiresAt) throw new Error(`request ${id} expired`);
|
|
308
|
+
if (!request.approved) throw new Error(`request ${id} is not approved`);
|
|
309
|
+
const account = createAccount({ publicKey: appKey });
|
|
310
|
+
requests.delete(id);
|
|
311
|
+
return account;
|
|
312
|
+
},
|
|
313
|
+
rejectConnection(id) {
|
|
314
|
+
requests.delete(id);
|
|
315
|
+
},
|
|
316
|
+
requestConnection({ appId: _appId, publicKey }) {
|
|
317
|
+
const id = `request-${++requestNumber}`;
|
|
318
|
+
const expiresAt = new Date(now().getTime() + 600 * 1e3);
|
|
319
|
+
requests.set(id, {
|
|
320
|
+
approved: false,
|
|
321
|
+
expiresAt,
|
|
322
|
+
publicKey
|
|
323
|
+
});
|
|
324
|
+
return {
|
|
325
|
+
expiresAt: new Date(expiresAt),
|
|
326
|
+
id
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
reset() {
|
|
330
|
+
for (const [accountId, pins] of accountSlabs) for (const identity of pins.values()) retirePin(accountId, identity);
|
|
331
|
+
accounts.clear();
|
|
332
|
+
objects.clear();
|
|
333
|
+
slabs.clear();
|
|
334
|
+
accountSlabs.clear();
|
|
335
|
+
events.clear();
|
|
336
|
+
pendingEvents.clear();
|
|
337
|
+
publishedEvents.clear();
|
|
338
|
+
lastPublishedAt = void 0;
|
|
339
|
+
requests.clear();
|
|
340
|
+
sharing.clear();
|
|
341
|
+
requestNumber = 0;
|
|
342
|
+
},
|
|
343
|
+
revokeSharingKey(ownerId, publicKey) {
|
|
344
|
+
const record = ownedShare(ownerId, publicKey);
|
|
345
|
+
record.revoked = true;
|
|
346
|
+
record.key.updatedAt = now().toISOString();
|
|
347
|
+
},
|
|
348
|
+
seedObject(accountId, object) {
|
|
349
|
+
requireAccount(accountId);
|
|
350
|
+
objects.get(accountId).set(object.id, copyObject(object));
|
|
351
|
+
},
|
|
352
|
+
serialize() {
|
|
353
|
+
const state = {
|
|
354
|
+
accounts: [...accounts.values()].map((account) => ({
|
|
355
|
+
account,
|
|
356
|
+
events: eventPublication === "manual" ? [...publishedEvents.get(account.id).values()] : events.get(account.id),
|
|
357
|
+
objects: [...objects.get(account.id).values()],
|
|
358
|
+
slabs: [...accountSlabs.get(account.id).keys()]
|
|
359
|
+
})),
|
|
360
|
+
hosts: hostList,
|
|
361
|
+
...eventPublication === "manual" ? { publication: {
|
|
362
|
+
...lastPublishedAt === void 0 ? {} : { lastPublishedAt: new Date(lastPublishedAt).toISOString() },
|
|
363
|
+
pending: [...pendingEvents].flatMap(([accountId, pending]) => [...pending.values()].map((event) => ({
|
|
364
|
+
accountId,
|
|
365
|
+
event
|
|
366
|
+
})))
|
|
367
|
+
} } : {},
|
|
368
|
+
...quotaBytes === void 0 ? {} : { quotaBytes },
|
|
369
|
+
requestNumber,
|
|
370
|
+
requests: [...requests.entries()].map(([id, request]) => ({
|
|
371
|
+
...request,
|
|
372
|
+
expiresAt: request.expiresAt.toISOString(),
|
|
373
|
+
id
|
|
374
|
+
})),
|
|
375
|
+
sharing: [...sharing.values()].map((record) => ({
|
|
376
|
+
attachments: [...record.attachments.values()],
|
|
377
|
+
key: record.key,
|
|
378
|
+
owner: record.owner,
|
|
379
|
+
revoked: record.revoked
|
|
380
|
+
})),
|
|
381
|
+
slabs: [...slabs.values()],
|
|
382
|
+
version: eventPublication === "manual" ? 2 : 1
|
|
383
|
+
};
|
|
384
|
+
const serialized = JSON.stringify(state);
|
|
385
|
+
readModelState(serialized);
|
|
386
|
+
return serialized;
|
|
387
|
+
},
|
|
388
|
+
sharedObject(recipientId, publicKey, objectID) {
|
|
389
|
+
const attachment = recipientShare(recipientId, publicKey).attachments.get(objectID);
|
|
390
|
+
if (!attachment) throw new Error(`object ${objectID} not shared`);
|
|
391
|
+
return { ...attachment };
|
|
392
|
+
},
|
|
393
|
+
sharedStats(recipientId, publicKey) {
|
|
394
|
+
const record = recipientShare(recipientId, publicKey);
|
|
395
|
+
let objectSize = 0;
|
|
396
|
+
for (const attachment of record.attachments.values()) objectSize += slabBytes(objects.get(record.owner).get(attachment.objectID)?.slabs ?? []);
|
|
397
|
+
return {
|
|
398
|
+
createdAt: record.key.createdAt,
|
|
399
|
+
objectCount: record.attachments.size,
|
|
400
|
+
objectSize,
|
|
401
|
+
pinnedData: 0,
|
|
402
|
+
pinnedSize: 0,
|
|
403
|
+
updatedAt: record.key.updatedAt
|
|
404
|
+
};
|
|
405
|
+
},
|
|
406
|
+
slabIdentity(accountId, slabId) {
|
|
407
|
+
requireAccount(accountId);
|
|
408
|
+
const identity = accountSlabs.get(accountId).get(slabId);
|
|
409
|
+
if (identity === void 0 || !slabs.has(slabId)) throw new Error(`slab ${slabId} not found`);
|
|
410
|
+
return identity;
|
|
411
|
+
},
|
|
412
|
+
snapshot() {
|
|
413
|
+
return { accounts: [...accounts.values()].map((account) => ({ ...account })) };
|
|
414
|
+
},
|
|
415
|
+
trackSlabPins(accountId) {
|
|
416
|
+
requireAccount(accountId);
|
|
417
|
+
const state = {
|
|
418
|
+
retired: /* @__PURE__ */ new Set(),
|
|
419
|
+
watched: /* @__PURE__ */ new Set()
|
|
420
|
+
};
|
|
421
|
+
let trackers = pinTrackers.get(accountId);
|
|
422
|
+
if (!trackers) {
|
|
423
|
+
trackers = /* @__PURE__ */ new Set();
|
|
424
|
+
pinTrackers.set(accountId, trackers);
|
|
425
|
+
}
|
|
426
|
+
for (const reference of trackers) if (!reference.deref()) trackers.delete(reference);
|
|
427
|
+
trackers.add(new WeakRef(state));
|
|
428
|
+
return {
|
|
429
|
+
forget(identity) {
|
|
430
|
+
state.watched.delete(identity);
|
|
431
|
+
state.retired.delete(identity);
|
|
432
|
+
},
|
|
433
|
+
takeRetired() {
|
|
434
|
+
const retired = [...state.retired];
|
|
435
|
+
state.retired.clear();
|
|
436
|
+
return retired;
|
|
437
|
+
},
|
|
438
|
+
watch(identity) {
|
|
439
|
+
state.watched.add(identity);
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
},
|
|
443
|
+
unpinSlab(accountId, slabId) {
|
|
444
|
+
requireAccount(accountId);
|
|
445
|
+
if (!accountSlabs.get(accountId).has(slabId)) throw new Error(`slab ${slabId} not found`);
|
|
446
|
+
for (const object of objects.get(accountId).values()) if (object.slabs?.includes(slabId)) throw new Error(`slab ${slabId} is still referenced`);
|
|
447
|
+
removeSlabPin(accountId, slabId);
|
|
448
|
+
},
|
|
449
|
+
validateSignedShareUrl(rawUrl) {
|
|
450
|
+
const url = new URL(rawUrl);
|
|
451
|
+
if (url.hash) throw new Error("share URL fragments are not accepted");
|
|
452
|
+
const sc = url.searchParams.get("sc");
|
|
453
|
+
const ss = url.searchParams.get("ss");
|
|
454
|
+
const sv = url.searchParams.get("sv");
|
|
455
|
+
if (!sc || !ss || !sv || !/^[A-Za-z0-9_-]+$/.test(sc) || !/^[A-Za-z0-9_-]+$/.test(ss)) throw new Error("invalid signed share URL");
|
|
456
|
+
const expiresAt = Number(sv);
|
|
457
|
+
if (!Number.isSafeInteger(expiresAt) || expiresAt < Math.floor(now().getTime() / 1e3)) throw new Error("share URL expired");
|
|
458
|
+
const match = /\/objects\/([^/]+)\/shared$/.exec(url.pathname);
|
|
459
|
+
if (!match?.[1]) throw new Error("invalid share URL path");
|
|
460
|
+
return {
|
|
461
|
+
expiresAt,
|
|
462
|
+
objectID: match[1],
|
|
463
|
+
publicKey: sc
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
function copyHost(host) {
|
|
469
|
+
return {
|
|
470
|
+
...host,
|
|
471
|
+
addresses: [...host.addresses]
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
/** Detached slab arrays prevent callers from bypassing pin validation through snapshots. */
|
|
475
|
+
function copyObject(object) {
|
|
476
|
+
return {
|
|
477
|
+
...object,
|
|
478
|
+
...object.slabs ? { slabs: [...object.slabs] } : {}
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
function decodedBase64Length(value) {
|
|
482
|
+
try {
|
|
483
|
+
return atob(value).length;
|
|
484
|
+
} catch {
|
|
485
|
+
throw new Error("invalid encrypted metadata encoding");
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
function expired(value, now) {
|
|
489
|
+
return value !== void 0 && Date.parse(value) <= now.getTime();
|
|
490
|
+
}
|
|
491
|
+
function recipientMatches(publicKey, recipient) {
|
|
492
|
+
const normalizedPublic = publicKey.replace(/=+$/, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
493
|
+
const normalizedRecipient = recipient.replace(/=+$/, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
494
|
+
return recipient === publicKey || wirePublicKeyMatches(publicKey, recipient) || normalizedPublic === normalizedRecipient;
|
|
495
|
+
}
|
|
496
|
+
function wirePublicKeyMatches(publicKey, wireKey) {
|
|
497
|
+
const encoded = wireKey.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat((4 - wireKey.length % 4) % 4);
|
|
498
|
+
try {
|
|
499
|
+
const bytes = Uint8Array.from(atob(encoded), (char) => char.charCodeAt(0));
|
|
500
|
+
const hex = [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
501
|
+
const base64 = btoa(String.fromCharCode(...bytes));
|
|
502
|
+
return publicKey.endsWith(`:${hex}`) || publicKey.endsWith(`:${base64}`) || publicKey.endsWith(`:${base64.replace(/=+$/, "")}`);
|
|
503
|
+
} catch {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
//#endregion
|
|
509
|
+
export { createIndexdModel };
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
//#region src/model/state.ts
|
|
2
|
+
/** Validation rejects identities and slab references that would change ownership when maps are rebuilt. */
|
|
3
|
+
function readModelState(serialized) {
|
|
4
|
+
try {
|
|
5
|
+
const state = object(JSON.parse(serialized));
|
|
6
|
+
if (state.version !== 1 && state.version !== 2) throw new Error("unsupported snapshot version");
|
|
7
|
+
if (state.version === 1 && state.publication !== void 0) invalid();
|
|
8
|
+
integer(state.requestNumber);
|
|
9
|
+
if (state.quotaBytes !== void 0) integer(state.quotaBytes);
|
|
10
|
+
const accounts = array(state.accounts);
|
|
11
|
+
const accountIds = unique(accounts.map((entry) => {
|
|
12
|
+
const row = object(entry);
|
|
13
|
+
const account = object(row.account);
|
|
14
|
+
strings(account, ["id", "publicKey"]);
|
|
15
|
+
if (account.id !== account.publicKey) invalid();
|
|
16
|
+
unique(array(row.objects).map((value) => {
|
|
17
|
+
checkObject(value);
|
|
18
|
+
return object(value).id;
|
|
19
|
+
}));
|
|
20
|
+
unique(array(row.slabs));
|
|
21
|
+
for (const id of array(row.slabs)) text(id);
|
|
22
|
+
for (const value of array(row.events)) checkEvent(value);
|
|
23
|
+
return account.id;
|
|
24
|
+
}));
|
|
25
|
+
if (state.version === 2) {
|
|
26
|
+
const publication = object(state.publication);
|
|
27
|
+
const lastPublishedAt = publication.lastPublishedAt;
|
|
28
|
+
if (lastPublishedAt !== void 0) {
|
|
29
|
+
date(lastPublishedAt);
|
|
30
|
+
if (Date.parse(lastPublishedAt) % 1e3 !== 0) invalid();
|
|
31
|
+
}
|
|
32
|
+
const visible = /* @__PURE__ */ new Set();
|
|
33
|
+
for (const entry of accounts) {
|
|
34
|
+
const row = object(entry);
|
|
35
|
+
const accountId = object(row.account).id;
|
|
36
|
+
unique(array(row.events).map((value) => object(value).key));
|
|
37
|
+
for (const value of array(row.events)) {
|
|
38
|
+
const event = object(value);
|
|
39
|
+
const stamp = Date.parse(event.updatedAt);
|
|
40
|
+
if (lastPublishedAt === void 0 || stamp % 1e3 !== 0 || stamp > Date.parse(lastPublishedAt)) invalid();
|
|
41
|
+
if (event.deleted !== (event.object === void 0)) invalid();
|
|
42
|
+
visible.add(JSON.stringify([accountId, event.key]));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
unique(array(publication.pending).map((value) => {
|
|
46
|
+
const row = object(value);
|
|
47
|
+
text(row.accountId);
|
|
48
|
+
if (!accountIds.has(row.accountId)) invalid();
|
|
49
|
+
checkEvent(row.event);
|
|
50
|
+
const event = object(row.event);
|
|
51
|
+
if (event.deleted !== (event.object === void 0)) invalid();
|
|
52
|
+
const key = JSON.stringify([row.accountId, event.key]);
|
|
53
|
+
if (visible.has(key)) invalid();
|
|
54
|
+
return key;
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
const slabIds = unique(array(state.slabs).map((value) => {
|
|
58
|
+
const slab = object(value);
|
|
59
|
+
text(slab.id);
|
|
60
|
+
integer(slab.size);
|
|
61
|
+
return slab.id;
|
|
62
|
+
}));
|
|
63
|
+
for (const entry of accounts) for (const id of array(object(entry).slabs)) if (!slabIds.has(id)) invalid();
|
|
64
|
+
unique(array(state.requests).map((value) => {
|
|
65
|
+
const request = object(value);
|
|
66
|
+
strings(request, [
|
|
67
|
+
"id",
|
|
68
|
+
"publicKey",
|
|
69
|
+
"expiresAt"
|
|
70
|
+
]);
|
|
71
|
+
bool(request.approved);
|
|
72
|
+
date(request.expiresAt);
|
|
73
|
+
const match = /^request-([1-9]\d*)$/.exec(request.id);
|
|
74
|
+
if (!match || Number(match[1]) > state.requestNumber) invalid();
|
|
75
|
+
return request.id;
|
|
76
|
+
}));
|
|
77
|
+
unique(array(state.sharing).map((value) => {
|
|
78
|
+
const share = object(value);
|
|
79
|
+
text(share.owner);
|
|
80
|
+
bool(share.revoked);
|
|
81
|
+
const key = object(share.key);
|
|
82
|
+
strings(key, [
|
|
83
|
+
"account",
|
|
84
|
+
"createdAt",
|
|
85
|
+
"description",
|
|
86
|
+
"nonce",
|
|
87
|
+
"publicKey",
|
|
88
|
+
"updatedAt"
|
|
89
|
+
]);
|
|
90
|
+
date(key.createdAt);
|
|
91
|
+
date(key.updatedAt);
|
|
92
|
+
if (key.expiresAt !== void 0) date(key.expiresAt);
|
|
93
|
+
for (const field of [
|
|
94
|
+
"objectCount",
|
|
95
|
+
"objectSize",
|
|
96
|
+
"pinnedData",
|
|
97
|
+
"pinnedSize"
|
|
98
|
+
]) integer(key[field]);
|
|
99
|
+
if (!accountIds.has(share.owner) || key.account !== share.owner) invalid();
|
|
100
|
+
const attachments = array(share.attachments);
|
|
101
|
+
unique(attachments.map((item) => {
|
|
102
|
+
const attachment = object(item);
|
|
103
|
+
strings(attachment, [
|
|
104
|
+
"dataSignature",
|
|
105
|
+
"encryptedDataKey",
|
|
106
|
+
"objectID"
|
|
107
|
+
]);
|
|
108
|
+
strings(attachment, [
|
|
109
|
+
"encryptedMetadata",
|
|
110
|
+
"encryptedMetadataKey",
|
|
111
|
+
"metadataSignature"
|
|
112
|
+
], true);
|
|
113
|
+
return attachment.objectID;
|
|
114
|
+
}));
|
|
115
|
+
if (key.objectCount !== attachments.length) invalid();
|
|
116
|
+
return key.publicKey;
|
|
117
|
+
}));
|
|
118
|
+
for (const value of array(state.hosts)) {
|
|
119
|
+
const host = object(value);
|
|
120
|
+
text(host.publicKey);
|
|
121
|
+
for (const address of array(host.addresses)) text(address);
|
|
122
|
+
strings(host, ["countryCode"], true);
|
|
123
|
+
if (host.goodForUpload !== void 0) bool(host.goodForUpload);
|
|
124
|
+
for (const field of ["latitude", "longitude"]) if (host[field] !== void 0 && (typeof host[field] !== "number" || !Number.isFinite(host[field]))) invalid();
|
|
125
|
+
}
|
|
126
|
+
return state;
|
|
127
|
+
} catch (cause) {
|
|
128
|
+
throw new Error("invalid model snapshot", { cause });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function array(value) {
|
|
132
|
+
if (!Array.isArray(value)) invalid();
|
|
133
|
+
return value;
|
|
134
|
+
}
|
|
135
|
+
function bool(value) {
|
|
136
|
+
if (typeof value !== "boolean") invalid();
|
|
137
|
+
}
|
|
138
|
+
/** Event snapshots must keep their object identity aligned with the stream key. */
|
|
139
|
+
function checkEvent(value) {
|
|
140
|
+
const event = object(value);
|
|
141
|
+
strings(event, ["key", "updatedAt"]);
|
|
142
|
+
date(event.updatedAt);
|
|
143
|
+
bool(event.deleted);
|
|
144
|
+
if (event.object !== void 0) {
|
|
145
|
+
checkObject(event.object);
|
|
146
|
+
if (object(event.object).id !== event.key) invalid();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function checkObject(value) {
|
|
150
|
+
const row = object(value);
|
|
151
|
+
text(row.id);
|
|
152
|
+
strings(row, [
|
|
153
|
+
"dataSignature",
|
|
154
|
+
"encryptedDataKey",
|
|
155
|
+
"encryptedMetadata",
|
|
156
|
+
"encryptedMetadataKey",
|
|
157
|
+
"metadataSignature"
|
|
158
|
+
], true);
|
|
159
|
+
if (row.slabs !== void 0) for (const id of array(row.slabs)) text(id);
|
|
160
|
+
}
|
|
161
|
+
function date(value) {
|
|
162
|
+
text(value);
|
|
163
|
+
if (!Number.isFinite(Date.parse(value))) invalid();
|
|
164
|
+
}
|
|
165
|
+
function integer(value) {
|
|
166
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) invalid();
|
|
167
|
+
}
|
|
168
|
+
function invalid() {
|
|
169
|
+
throw new Error("invalid snapshot record");
|
|
170
|
+
}
|
|
171
|
+
function object(value) {
|
|
172
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) invalid();
|
|
173
|
+
return value;
|
|
174
|
+
}
|
|
175
|
+
function strings(value, keys, optional = false) {
|
|
176
|
+
for (const key of keys) if (!optional || value[key] !== void 0) text(value[key]);
|
|
177
|
+
}
|
|
178
|
+
function text(value) {
|
|
179
|
+
if (typeof value !== "string") invalid();
|
|
180
|
+
}
|
|
181
|
+
function unique(values) {
|
|
182
|
+
const set = new Set(values);
|
|
183
|
+
if (set.size !== values.length) invalid();
|
|
184
|
+
return set;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
//#endregion
|
|
188
|
+
export { readModelState };
|
package/dist/esm/msw.js
ADDED