@enbox/agent 0.1.5 → 0.1.7
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/dist/browser.mjs +11 -11
- package/dist/browser.mjs.map +4 -4
- package/dist/esm/anonymous-dwn-api.js +184 -0
- package/dist/esm/anonymous-dwn-api.js.map +1 -0
- package/dist/esm/dwn-api.js +85 -785
- package/dist/esm/dwn-api.js.map +1 -1
- package/dist/esm/dwn-encryption.js +342 -0
- package/dist/esm/dwn-encryption.js.map +1 -0
- package/dist/esm/dwn-key-delivery.js +256 -0
- package/dist/esm/dwn-key-delivery.js.map +1 -0
- package/dist/esm/dwn-record-upgrade.js +119 -0
- package/dist/esm/dwn-record-upgrade.js.map +1 -0
- package/dist/esm/dwn-type-guards.js +23 -0
- package/dist/esm/dwn-type-guards.js.map +1 -0
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/permissions-api.js +43 -2
- package/dist/esm/permissions-api.js.map +1 -1
- package/dist/esm/protocol-utils.js +158 -0
- package/dist/esm/protocol-utils.js.map +1 -0
- package/dist/esm/store-data-protocols.js +1 -1
- package/dist/esm/store-data-protocols.js.map +1 -1
- package/dist/esm/store-data.js +3 -0
- package/dist/esm/store-data.js.map +1 -1
- package/dist/esm/sync-engine-level.js +23 -354
- package/dist/esm/sync-engine-level.js.map +1 -1
- package/dist/esm/sync-messages.js +237 -0
- package/dist/esm/sync-messages.js.map +1 -0
- package/dist/esm/sync-topological-sort.js +143 -0
- package/dist/esm/sync-topological-sort.js.map +1 -0
- package/dist/esm/test-harness.js +20 -0
- package/dist/esm/test-harness.js.map +1 -1
- package/dist/types/anonymous-dwn-api.d.ts +140 -0
- package/dist/types/anonymous-dwn-api.d.ts.map +1 -0
- package/dist/types/dwn-api.d.ts +36 -184
- package/dist/types/dwn-api.d.ts.map +1 -1
- package/dist/types/dwn-encryption.d.ts +144 -0
- package/dist/types/dwn-encryption.d.ts.map +1 -0
- package/dist/types/dwn-key-delivery.d.ts +112 -0
- package/dist/types/dwn-key-delivery.d.ts.map +1 -0
- package/dist/types/dwn-record-upgrade.d.ts +33 -0
- package/dist/types/dwn-record-upgrade.d.ts.map +1 -0
- package/dist/types/dwn-type-guards.d.ts +9 -0
- package/dist/types/dwn-type-guards.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/permissions-api.d.ts +6 -1
- package/dist/types/permissions-api.d.ts.map +1 -1
- package/dist/types/protocol-utils.d.ts +70 -0
- package/dist/types/protocol-utils.d.ts.map +1 -0
- package/dist/types/store-data.d.ts +4 -0
- package/dist/types/store-data.d.ts.map +1 -1
- package/dist/types/sync-engine-level.d.ts +5 -42
- package/dist/types/sync-engine-level.d.ts.map +1 -1
- package/dist/types/sync-messages.d.ts +76 -0
- package/dist/types/sync-messages.d.ts.map +1 -0
- package/dist/types/sync-topological-sort.d.ts +15 -0
- package/dist/types/sync-topological-sort.d.ts.map +1 -0
- package/dist/types/test-harness.d.ts +10 -0
- package/dist/types/test-harness.d.ts.map +1 -1
- package/dist/types/types/permissions.d.ts +2 -0
- package/dist/types/types/permissions.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/anonymous-dwn-api.ts +263 -0
- package/src/dwn-api.ts +158 -1024
- package/src/dwn-encryption.ts +481 -0
- package/src/dwn-key-delivery.ts +370 -0
- package/src/dwn-record-upgrade.ts +166 -0
- package/src/dwn-type-guards.ts +43 -0
- package/src/index.ts +6 -0
- package/src/permissions-api.ts +54 -2
- package/src/protocol-utils.ts +185 -0
- package/src/store-data-protocols.ts +1 -1
- package/src/store-data.ts +5 -2
- package/src/sync-engine-level.ts +25 -414
- package/src/sync-messages.ts +279 -0
- package/src/sync-topological-sort.ts +167 -0
- package/src/test-harness.ts +19 -0
- package/src/types/permissions.ts +2 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { PermissionsApi } from './types/permissions.js';
|
|
2
|
+
import type { Web5PlatformAgent } from './types/agent.js';
|
|
3
|
+
import type { GenericMessage, MessagesReadReply, UnionMessageReply } from '@enbox/dwn-sdk-js';
|
|
4
|
+
|
|
5
|
+
import { DwnInterfaceName, DwnMethodName, Message } from '@enbox/dwn-sdk-js';
|
|
6
|
+
|
|
7
|
+
import { DwnInterface } from './types/dwn.js';
|
|
8
|
+
import { isRecordsWrite } from './utils.js';
|
|
9
|
+
import { topologicalSort } from './sync-topological-sort.js';
|
|
10
|
+
|
|
11
|
+
/** Entry type for fetched messages with optional data stream. */
|
|
12
|
+
export type SyncMessageEntry = { message: GenericMessage; dataStream?: ReadableStream<Uint8Array> };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 202: message was successfully written to the remote DWN
|
|
16
|
+
* 204: an initial write message was written without any data
|
|
17
|
+
* 409: message was already present on the remote DWN
|
|
18
|
+
* RecordsDelete + 404: the initial write was not found or already deleted
|
|
19
|
+
*/
|
|
20
|
+
export function syncMessageReplyIsSuccessful(reply: UnionMessageReply): boolean {
|
|
21
|
+
return reply.status.code === 202 ||
|
|
22
|
+
reply.status.code === 204 ||
|
|
23
|
+
reply.status.code === 409 ||
|
|
24
|
+
(
|
|
25
|
+
reply.entry?.message.descriptor.interface === DwnInterfaceName.Records &&
|
|
26
|
+
reply.entry?.message.descriptor.method === DwnMethodName.Delete &&
|
|
27
|
+
reply.status.code === 404
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Helper to get the CID of a message for logging purposes.
|
|
33
|
+
*/
|
|
34
|
+
export async function getMessageCid(message: GenericMessage): Promise<string> {
|
|
35
|
+
try {
|
|
36
|
+
return await Message.getCid(message);
|
|
37
|
+
} catch {
|
|
38
|
+
return 'unknown';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Fetches missing messages from the remote DWN and processes them on the local DWN
|
|
44
|
+
* in dependency order (topological sort).
|
|
45
|
+
*
|
|
46
|
+
* Messages that fail processing are re-fetched from the remote before each retry
|
|
47
|
+
* pass rather than buffered in memory. ReadableStream is single-use, so a failed
|
|
48
|
+
* message's data stream is consumed on the first attempt. Re-fetching provides a
|
|
49
|
+
* fresh stream without holding all record data in memory simultaneously.
|
|
50
|
+
*/
|
|
51
|
+
export async function pullMessages({ did, dwnUrl, delegateDid, protocol, messageCids, agent, permissionsApi }: {
|
|
52
|
+
did: string;
|
|
53
|
+
dwnUrl: string;
|
|
54
|
+
delegateDid?: string;
|
|
55
|
+
protocol?: string;
|
|
56
|
+
messageCids: string[];
|
|
57
|
+
agent: Web5PlatformAgent;
|
|
58
|
+
permissionsApi: PermissionsApi;
|
|
59
|
+
}): Promise<void> {
|
|
60
|
+
// Step 1: Fetch all missing messages from the remote in parallel.
|
|
61
|
+
const fetched = await fetchRemoteMessages({ did, dwnUrl, delegateDid, protocol, messageCids, agent, permissionsApi });
|
|
62
|
+
|
|
63
|
+
// Step 2: Build dependency graph and topological sort.
|
|
64
|
+
const sorted = topologicalSort(fetched);
|
|
65
|
+
|
|
66
|
+
// Step 3: Process messages in dependency order with multi-pass retry.
|
|
67
|
+
// Retry up to MAX_RETRY_PASSES times for messages that fail due to
|
|
68
|
+
// dependency ordering issues (e.g., a RecordsWrite whose ProtocolsConfigure
|
|
69
|
+
// hasn't committed yet). Failed messages are re-fetched from the remote
|
|
70
|
+
// to obtain a fresh data stream, since ReadableStream is single-use.
|
|
71
|
+
const MAX_RETRY_PASSES = 3;
|
|
72
|
+
let pending = sorted;
|
|
73
|
+
|
|
74
|
+
for (let pass = 0; pass <= MAX_RETRY_PASSES && pending.length > 0; pass++) {
|
|
75
|
+
const failedCids: string[] = [];
|
|
76
|
+
|
|
77
|
+
for (const entry of pending) {
|
|
78
|
+
const pullReply = await agent.dwn.node.processMessage(did, entry.message, { dataStream: entry.dataStream });
|
|
79
|
+
if (!syncMessageReplyIsSuccessful(pullReply)) {
|
|
80
|
+
const cid = await getMessageCid(entry.message);
|
|
81
|
+
failedCids.push(cid);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Re-fetch failed messages from the remote to get fresh data streams.
|
|
86
|
+
if (failedCids.length > 0) {
|
|
87
|
+
const reFetched = await fetchRemoteMessages({ did, dwnUrl, delegateDid, protocol, messageCids: failedCids, agent, permissionsApi });
|
|
88
|
+
pending = topologicalSort(reFetched);
|
|
89
|
+
} else {
|
|
90
|
+
pending = [];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Fetches messages from a remote DWN by their CIDs using MessagesRead.
|
|
97
|
+
*/
|
|
98
|
+
export async function fetchRemoteMessages({ did, dwnUrl, delegateDid, protocol, messageCids, agent, permissionsApi }: {
|
|
99
|
+
did: string;
|
|
100
|
+
dwnUrl: string;
|
|
101
|
+
delegateDid?: string;
|
|
102
|
+
protocol?: string;
|
|
103
|
+
messageCids: string[];
|
|
104
|
+
agent: Web5PlatformAgent;
|
|
105
|
+
permissionsApi: PermissionsApi;
|
|
106
|
+
}): Promise<SyncMessageEntry[]> {
|
|
107
|
+
const results: SyncMessageEntry[] = [];
|
|
108
|
+
|
|
109
|
+
let permissionGrantId: string | undefined;
|
|
110
|
+
if (delegateDid) {
|
|
111
|
+
try {
|
|
112
|
+
const messagesReadGrant = await permissionsApi.getPermissionForRequest({
|
|
113
|
+
connectedDid : did,
|
|
114
|
+
messageType : DwnInterface.MessagesRead,
|
|
115
|
+
delegateDid,
|
|
116
|
+
protocol,
|
|
117
|
+
cached : true
|
|
118
|
+
});
|
|
119
|
+
permissionGrantId = messagesReadGrant.grant.id;
|
|
120
|
+
} catch (error: any) {
|
|
121
|
+
console.error('SyncEngineLevel: pull - Error fetching MessagesRead permission grant for delegate DID', error);
|
|
122
|
+
return results;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Fetch messages in parallel with bounded concurrency.
|
|
127
|
+
const CONCURRENCY = 10;
|
|
128
|
+
let cursor = 0;
|
|
129
|
+
|
|
130
|
+
while (cursor < messageCids.length) {
|
|
131
|
+
const batch = messageCids.slice(cursor, cursor + CONCURRENCY);
|
|
132
|
+
cursor += CONCURRENCY;
|
|
133
|
+
|
|
134
|
+
type FetchResult = SyncMessageEntry | undefined;
|
|
135
|
+
const batchResults = await Promise.all(batch.map(async (messageCid): Promise<FetchResult> => {
|
|
136
|
+
const messagesRead = await agent.processDwnRequest({
|
|
137
|
+
store : false,
|
|
138
|
+
author : did,
|
|
139
|
+
target : did,
|
|
140
|
+
messageType : DwnInterface.MessagesRead,
|
|
141
|
+
granteeDid : delegateDid,
|
|
142
|
+
messageParams : { messageCid, permissionGrantId }
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
let reply: MessagesReadReply;
|
|
146
|
+
try {
|
|
147
|
+
reply = await agent.rpc.sendDwnRequest({
|
|
148
|
+
dwnUrl,
|
|
149
|
+
targetDid : did,
|
|
150
|
+
message : messagesRead.message,
|
|
151
|
+
}) as MessagesReadReply;
|
|
152
|
+
} catch (error: any) {
|
|
153
|
+
console.error(`SyncEngineLevel: pull - failed to read ${messageCid} from ${dwnUrl}:`, error.message ?? error);
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (reply.status.code !== 200 || !reply.entry?.message) {
|
|
158
|
+
return undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const replyEntry = reply.entry;
|
|
162
|
+
let dataStream: ReadableStream<Uint8Array> | undefined;
|
|
163
|
+
if (isRecordsWrite(replyEntry) && replyEntry.data) {
|
|
164
|
+
dataStream = replyEntry.data;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { message: replyEntry.message, dataStream };
|
|
168
|
+
}));
|
|
169
|
+
|
|
170
|
+
for (const result of batchResults) {
|
|
171
|
+
if (result) {
|
|
172
|
+
results.push(result);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return results;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Reads missing messages from the local DWN and pushes them to the remote DWN.
|
|
182
|
+
* Messages are fetched first, then sorted in dependency order (topological sort)
|
|
183
|
+
* so that initial writes come before updates, and ProtocolsConfigures come before
|
|
184
|
+
* records that reference those protocols.
|
|
185
|
+
*/
|
|
186
|
+
export async function pushMessages({ did, dwnUrl, delegateDid, protocol, messageCids, agent, permissionsApi }: {
|
|
187
|
+
did: string;
|
|
188
|
+
dwnUrl: string;
|
|
189
|
+
delegateDid?: string;
|
|
190
|
+
protocol?: string;
|
|
191
|
+
messageCids: string[];
|
|
192
|
+
agent: Web5PlatformAgent;
|
|
193
|
+
permissionsApi: PermissionsApi;
|
|
194
|
+
}): Promise<void> {
|
|
195
|
+
// Step 1: Fetch all local messages (streams are pull-based, not yet consumed).
|
|
196
|
+
const fetched: SyncMessageEntry[] = [];
|
|
197
|
+
for (const messageCid of messageCids) {
|
|
198
|
+
const dwnMessage = await getLocalMessage({ author: did, messageCid, delegateDid, protocol, agent, permissionsApi });
|
|
199
|
+
if (dwnMessage) {
|
|
200
|
+
fetched.push(dwnMessage);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Step 2: Sort in dependency order using topological sort.
|
|
205
|
+
const sorted = topologicalSort(fetched);
|
|
206
|
+
|
|
207
|
+
// Step 3: Push messages in dependency order, consuming each stream as we go.
|
|
208
|
+
for (const entry of sorted) {
|
|
209
|
+
try {
|
|
210
|
+
const reply = await agent.rpc.sendDwnRequest({
|
|
211
|
+
dwnUrl,
|
|
212
|
+
targetDid : did,
|
|
213
|
+
data : entry.dataStream,
|
|
214
|
+
message : entry.message
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
if (!syncMessageReplyIsSuccessful(reply)) {
|
|
218
|
+
const cid = await getMessageCid(entry.message);
|
|
219
|
+
console.error(`SyncEngineLevel: push failed for ${cid}: ${reply.status.code} ${reply.status.detail}`);
|
|
220
|
+
}
|
|
221
|
+
} catch (error: any) {
|
|
222
|
+
// Preserve the original error so callers can diagnose the root cause.
|
|
223
|
+
const detail = error.message ?? error;
|
|
224
|
+
throw new Error(`SyncEngineLevel: push to ${dwnUrl} failed: ${detail}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Reads a message from the local DWN by its CID using MessagesRead.
|
|
231
|
+
*/
|
|
232
|
+
export async function getLocalMessage({ author, delegateDid, protocol, messageCid, agent, permissionsApi }: {
|
|
233
|
+
author: string;
|
|
234
|
+
delegateDid?: string;
|
|
235
|
+
protocol?: string;
|
|
236
|
+
messageCid: string;
|
|
237
|
+
agent: Web5PlatformAgent;
|
|
238
|
+
permissionsApi: PermissionsApi;
|
|
239
|
+
}): Promise<SyncMessageEntry | undefined> {
|
|
240
|
+
let permissionGrantId: string | undefined;
|
|
241
|
+
if (delegateDid) {
|
|
242
|
+
try {
|
|
243
|
+
const messagesReadGrant = await permissionsApi.getPermissionForRequest({
|
|
244
|
+
connectedDid : author,
|
|
245
|
+
messageType : DwnInterface.MessagesRead,
|
|
246
|
+
delegateDid,
|
|
247
|
+
protocol,
|
|
248
|
+
cached : true
|
|
249
|
+
});
|
|
250
|
+
permissionGrantId = messagesReadGrant.grant.id;
|
|
251
|
+
} catch (error: any) {
|
|
252
|
+
console.error('SyncEngineLevel: push - Error fetching MessagesRead permission grant for delegate DID', error);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const { reply } = await agent.dwn.processRequest({
|
|
258
|
+
author,
|
|
259
|
+
target : author,
|
|
260
|
+
messageType : DwnInterface.MessagesRead,
|
|
261
|
+
granteeDid : delegateDid,
|
|
262
|
+
messageParams : { messageCid, permissionGrantId }
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
if (reply.status.code !== 200 || !reply.entry) {
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
const messageEntry = reply.entry!;
|
|
269
|
+
|
|
270
|
+
const result: SyncMessageEntry = {
|
|
271
|
+
message: messageEntry.message
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
if (isRecordsWrite(messageEntry) && messageEntry.data) {
|
|
275
|
+
result.dataStream = messageEntry.data;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { GenericMessage } from '@enbox/dwn-sdk-js';
|
|
2
|
+
|
|
3
|
+
import { DwnInterfaceName, DwnMethodName, PermissionsProtocol } from '@enbox/dwn-sdk-js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Checks whether a message is an initial RecordsWrite (not an update).
|
|
7
|
+
* An initial write has dateCreated === messageTimestamp (first write for this recordId).
|
|
8
|
+
*/
|
|
9
|
+
function isInitialWrite(message: GenericMessage): boolean {
|
|
10
|
+
const desc = message.descriptor as any;
|
|
11
|
+
if (desc.interface !== DwnInterfaceName.Records || desc.method !== DwnMethodName.Write) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
// A RecordsWrite is initial if dateCreated === messageTimestamp (first write for this recordId).
|
|
15
|
+
return desc.dateCreated === desc.messageTimestamp;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Builds a dependency graph from the fetched messages and returns them in
|
|
20
|
+
* topological order so that dependencies are processed before dependents.
|
|
21
|
+
*
|
|
22
|
+
* Dependencies:
|
|
23
|
+
* - ProtocolsConfigure must come before any RecordsWrite using that protocol
|
|
24
|
+
* - Parent record must come before child record (via parentId)
|
|
25
|
+
* - Initial write must come before update writes (same recordId, not initial)
|
|
26
|
+
* - Permission grant must come before records using that permissionGrantId
|
|
27
|
+
*/
|
|
28
|
+
export function topologicalSort<T extends { message: GenericMessage }>(
|
|
29
|
+
messages: T[]
|
|
30
|
+
): T[] {
|
|
31
|
+
if (messages.length <= 1) {
|
|
32
|
+
return messages;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Index messages by various keys for dependency resolution.
|
|
36
|
+
const byIndex = new Map<number, T>();
|
|
37
|
+
const protocolConfigureIndex = new Map<string, number>(); // protocol URL -> index
|
|
38
|
+
const initialWriteIndex = new Map<string, number>(); // recordId -> index of initial write
|
|
39
|
+
const grantIndex = new Map<string, number>(); // grant recordId -> index
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < messages.length; i++) {
|
|
42
|
+
const entry = messages[i];
|
|
43
|
+
byIndex.set(i, entry);
|
|
44
|
+
const desc = entry.message.descriptor;
|
|
45
|
+
|
|
46
|
+
if (desc.interface === DwnInterfaceName.Protocols && desc.method === DwnMethodName.Configure) {
|
|
47
|
+
const protocolUrl = (desc as any).definition?.protocol;
|
|
48
|
+
if (protocolUrl) {
|
|
49
|
+
protocolConfigureIndex.set(protocolUrl, i);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (desc.interface === DwnInterfaceName.Records && desc.method === DwnMethodName.Write) {
|
|
54
|
+
const recordId = (entry.message as any).recordId;
|
|
55
|
+
const initial = isInitialWrite(entry.message);
|
|
56
|
+
if (initial && recordId) {
|
|
57
|
+
initialWriteIndex.set(recordId, i);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Index permission grants by recordId so dependents can reference them.
|
|
61
|
+
if (
|
|
62
|
+
(desc as any).protocol === PermissionsProtocol.uri &&
|
|
63
|
+
(desc as any).protocolPath === PermissionsProtocol.grantPath &&
|
|
64
|
+
recordId
|
|
65
|
+
) {
|
|
66
|
+
grantIndex.set(recordId, i);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Build adjacency list (edges: dependency -> dependent).
|
|
72
|
+
const edges = new Map<number, Set<number>>();
|
|
73
|
+
const inDegree = new Array(messages.length).fill(0) as number[];
|
|
74
|
+
|
|
75
|
+
const addEdge = (from: number, to: number): void => {
|
|
76
|
+
if (from === to) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!edges.has(from)) {
|
|
80
|
+
edges.set(from, new Set());
|
|
81
|
+
}
|
|
82
|
+
const edgeSet = edges.get(from)!;
|
|
83
|
+
if (!edgeSet.has(to)) {
|
|
84
|
+
edgeSet.add(to);
|
|
85
|
+
inDegree[to]++;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
for (let i = 0; i < messages.length; i++) {
|
|
90
|
+
const desc = messages[i].message.descriptor;
|
|
91
|
+
|
|
92
|
+
// Protocol dependency: RecordsWrite depends on ProtocolsConfigure for its protocol.
|
|
93
|
+
if (desc.interface === DwnInterfaceName.Records) {
|
|
94
|
+
const protocol = (desc as any).protocol;
|
|
95
|
+
if (protocol && protocolConfigureIndex.has(protocol)) {
|
|
96
|
+
addEdge(protocolConfigureIndex.get(protocol)!, i);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Parent dependency: child record depends on parent record.
|
|
101
|
+
if (desc.interface === DwnInterfaceName.Records && (desc as any).parentId) {
|
|
102
|
+
const parentId = (desc as any).parentId;
|
|
103
|
+
if (initialWriteIndex.has(parentId)) {
|
|
104
|
+
addEdge(initialWriteIndex.get(parentId)!, i);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Initial write dependency: update depends on initial write.
|
|
109
|
+
if (desc.interface === DwnInterfaceName.Records && desc.method === DwnMethodName.Write) {
|
|
110
|
+
const recordId = (messages[i].message as any).recordId;
|
|
111
|
+
if (recordId && !isInitialWrite(messages[i].message) && initialWriteIndex.has(recordId)) {
|
|
112
|
+
addEdge(initialWriteIndex.get(recordId)!, i);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Delete depends on initial write.
|
|
117
|
+
if (desc.interface === DwnInterfaceName.Records && desc.method === DwnMethodName.Delete) {
|
|
118
|
+
const recordId = (desc as any).recordId;
|
|
119
|
+
if (recordId && initialWriteIndex.has(recordId)) {
|
|
120
|
+
addEdge(initialWriteIndex.get(recordId)!, i);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Permission grant dependency: message depends on the grant it references.
|
|
125
|
+
const permissionGrantId = (desc as any).permissionGrantId;
|
|
126
|
+
if (permissionGrantId && grantIndex.has(permissionGrantId)) {
|
|
127
|
+
addEdge(grantIndex.get(permissionGrantId)!, i);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Kahn's algorithm for topological sort.
|
|
132
|
+
const queue: number[] = [];
|
|
133
|
+
for (let i = 0; i < messages.length; i++) {
|
|
134
|
+
if (inDegree[i] === 0) {
|
|
135
|
+
queue.push(i);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const sorted: T[] = [];
|
|
140
|
+
while (queue.length > 0) {
|
|
141
|
+
const node = queue.shift()!;
|
|
142
|
+
sorted.push(byIndex.get(node)!);
|
|
143
|
+
|
|
144
|
+
const neighbors = edges.get(node);
|
|
145
|
+
if (neighbors) {
|
|
146
|
+
for (const neighbor of neighbors) {
|
|
147
|
+
inDegree[neighbor]--;
|
|
148
|
+
if (inDegree[neighbor] === 0) {
|
|
149
|
+
queue.push(neighbor);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// If there are nodes not in sorted (cycle), append them at the end.
|
|
156
|
+
if (sorted.length < messages.length) {
|
|
157
|
+
const sortedSet = new Set(sorted);
|
|
158
|
+
for (let i = 0; i < messages.length; i++) {
|
|
159
|
+
const entry = byIndex.get(i)!;
|
|
160
|
+
if (!sortedSet.has(entry)) {
|
|
161
|
+
sorted.push(entry);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return sorted;
|
|
167
|
+
}
|
package/src/test-harness.ts
CHANGED
|
@@ -128,6 +128,25 @@ export class PlatformAgentTestHarness {
|
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
+
/**
|
|
132
|
+
* Clear only DWN-level stores (data, messages, state index, resumable tasks,
|
|
133
|
+
* sync, permissions) and the DWN-backed store caches — but preserve the
|
|
134
|
+
* agent DID, vault, and all key/DID/identity material.
|
|
135
|
+
*
|
|
136
|
+
* Use this in `beforeEach` when `createAgentDid()` (and optionally
|
|
137
|
+
* `createIdentity()`) was called once in `beforeAll` to avoid expensive
|
|
138
|
+
* DID re-creation on every test.
|
|
139
|
+
*/
|
|
140
|
+
public async clearDwnStores(): Promise<void> {
|
|
141
|
+
await this.syncStore.clear();
|
|
142
|
+
await this.dwnDataStore.clear();
|
|
143
|
+
await this.dwnStateIndex.clear();
|
|
144
|
+
await this.dwnMessageStore.clear();
|
|
145
|
+
await this.dwnResumableTaskStore.clear();
|
|
146
|
+
await this.agent.permissions.clear();
|
|
147
|
+
this.dwnStores.clear();
|
|
148
|
+
}
|
|
149
|
+
|
|
131
150
|
public async closeStorage(): Promise<void> {
|
|
132
151
|
await this.didResolverCache.close();
|
|
133
152
|
await this.dwnDataStore.close();
|
package/src/types/permissions.ts
CHANGED