document-drive 6.0.0-dev.6 → 6.0.0-dev.60
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/src/cache/lru.js +1 -1
- package/dist/src/cache/lru.js.map +1 -1
- package/dist/src/drive-document-model/gen/document-model.d.ts.map +1 -1
- package/dist/src/drive-document-model/gen/document-model.js +2 -2
- package/dist/src/drive-document-model/gen/document-model.js.map +1 -1
- package/dist/src/drive-document-model/gen/schema/types.d.ts +31 -2
- package/dist/src/drive-document-model/gen/schema/types.d.ts.map +1 -1
- package/dist/src/drive-document-model/gen/schema/zod.d.ts +6 -1
- package/dist/src/drive-document-model/gen/schema/zod.d.ts.map +1 -1
- package/dist/src/drive-document-model/gen/schema/zod.js +41 -2
- package/dist/src/drive-document-model/gen/schema/zod.js.map +1 -1
- package/dist/src/drive-document-model/src/reducers/drive.d.ts.map +1 -1
- package/dist/src/drive-document-model/src/reducers/drive.js +34 -6
- package/dist/src/drive-document-model/src/reducers/drive.js.map +1 -1
- package/dist/src/drive-document-model/src/reducers/node.d.ts.map +1 -1
- package/dist/src/drive-document-model/src/reducers/node.js +12 -0
- package/dist/src/drive-document-model/src/reducers/node.js.map +1 -1
- package/dist/src/processors/processor-manager.d.ts +4 -4
- package/dist/src/processors/processor-manager.d.ts.map +1 -1
- package/dist/src/processors/processor-manager.js +6 -6
- package/dist/src/processors/processor-manager.js.map +1 -1
- package/dist/src/processors/relational.d.ts +10 -10
- package/dist/src/processors/relational.d.ts.map +1 -1
- package/dist/src/processors/relational.js +4 -4
- package/dist/src/processors/relational.js.map +1 -1
- package/dist/src/processors/types.d.ts +25 -23
- package/dist/src/processors/types.d.ts.map +1 -1
- package/dist/src/processors/utils.d.ts +6 -6
- package/dist/src/processors/utils.d.ts.map +1 -1
- package/dist/src/processors/utils.js +11 -11
- package/dist/src/processors/utils.js.map +1 -1
- package/dist/src/queue/event.js +9 -9
- package/dist/src/queue/event.js.map +1 -1
- package/dist/src/read-mode/server.js +2 -2
- package/dist/src/read-mode/server.js.map +1 -1
- package/dist/src/server/base-server.d.ts.map +1 -1
- package/dist/src/server/base-server.js +54 -40
- package/dist/src/server/base-server.js.map +1 -1
- package/dist/src/server/listener/listener-manager.d.ts.map +1 -1
- package/dist/src/server/listener/listener-manager.js +32 -32
- package/dist/src/server/listener/listener-manager.js.map +1 -1
- package/dist/src/server/sync-manager.d.ts.map +1 -1
- package/dist/src/server/sync-manager.js +10 -13
- package/dist/src/server/sync-manager.js.map +1 -1
- package/dist/src/server/transmitter/internal.d.ts +3 -3
- package/dist/src/server/transmitter/internal.d.ts.map +1 -1
- package/dist/src/server/transmitter/internal.js +1 -1
- package/dist/src/server/transmitter/internal.js.map +1 -1
- package/dist/src/server/transmitter/pull-responder.d.ts.map +1 -1
- package/dist/src/server/transmitter/pull-responder.js +42 -42
- package/dist/src/server/transmitter/pull-responder.js.map +1 -1
- package/dist/src/server/transmitter/switchboard-push.d.ts.map +1 -1
- package/dist/src/server/transmitter/switchboard-push.js +7 -7
- package/dist/src/server/transmitter/switchboard-push.js.map +1 -1
- package/dist/src/storage/browser.js +1 -1
- package/dist/src/storage/browser.js.map +1 -1
- package/dist/src/storage/filesystem.js +1 -1
- package/dist/src/storage/filesystem.js.map +1 -1
- package/dist/src/storage/ipfs.d.ts +1 -43
- package/dist/src/storage/ipfs.d.ts.map +1 -1
- package/dist/src/storage/ipfs.js +490 -415
- package/dist/src/storage/ipfs.js.map +1 -1
- package/dist/src/storage/memory.js +1 -1
- package/dist/src/storage/memory.js.map +1 -1
- package/dist/src/storage/prisma/prisma.d.ts.map +1 -1
- package/dist/src/storage/prisma/prisma.js +6 -5
- package/dist/src/storage/prisma/prisma.js.map +1 -1
- package/dist/src/utils/default-drives-manager.js +2 -2
- package/dist/src/utils/default-drives-manager.js.map +1 -1
- package/dist/src/utils/gql-transformations.d.ts +2 -1
- package/dist/src/utils/gql-transformations.d.ts.map +1 -1
- package/dist/src/utils/gql-transformations.js +2 -1
- package/dist/src/utils/gql-transformations.js.map +1 -1
- package/dist/src/utils/graphql.d.ts.map +1 -1
- package/dist/src/utils/graphql.js +6 -34
- package/dist/src/utils/graphql.js.map +1 -1
- package/dist/src/utils/logger.d.ts +2 -20
- package/dist/src/utils/logger.d.ts.map +1 -1
- package/dist/src/utils/logger.js +1 -106
- package/dist/src/utils/logger.js.map +1 -1
- package/dist/src/utils/types.d.ts +1 -7
- package/dist/src/utils/types.d.ts.map +1 -1
- package/dist/test/internal-listener.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +19 -28
package/dist/src/storage/ipfs.js
CHANGED
|
@@ -1,416 +1,491 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {} from "
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
// TODO fix interface errors
|
|
3
|
+
// import type { MFS } from "@helia/mfs";
|
|
4
|
+
// import { mfs } from "@helia/mfs";
|
|
5
|
+
// import type {
|
|
6
|
+
// IDocumentOperationStorage,
|
|
7
|
+
// IDocumentStorage,
|
|
8
|
+
// } from "document-drive";
|
|
9
|
+
// import {
|
|
10
|
+
// DocumentAlreadyExistsError,
|
|
11
|
+
// DocumentAlreadyExistsReason,
|
|
12
|
+
// DocumentNotFoundError,
|
|
13
|
+
// mergeOperations,
|
|
14
|
+
// type DocumentDriveAction,
|
|
15
|
+
// type DocumentDriveDocument,
|
|
16
|
+
// type SynchronizationUnitQuery,
|
|
17
|
+
// } from "document-drive";
|
|
18
|
+
// import {
|
|
19
|
+
// type Operation,
|
|
20
|
+
// type PHDocument,
|
|
21
|
+
// type PHDocumentHeader,
|
|
22
|
+
// } from "document-model";
|
|
23
|
+
// import type { Helia } from "helia";
|
|
24
|
+
// import stringify from "json-stringify-deterministic";
|
|
25
|
+
export {};
|
|
26
|
+
// // Interface for drive manifest that tracks document IDs in a drive
|
|
27
|
+
// interface DriveManifest {
|
|
28
|
+
// documentIds: string[];
|
|
29
|
+
// }
|
|
30
|
+
// // Interface for slug manifest that maps slugs to document IDs
|
|
31
|
+
// interface SlugManifest {
|
|
32
|
+
// slugToId: Record<string, string>;
|
|
33
|
+
// }
|
|
34
|
+
// export class IPFSStorage
|
|
35
|
+
// implements IDriveStorage, IDocumentOperationStorage, IDocumentStorage
|
|
36
|
+
// {
|
|
37
|
+
// private fs: MFS;
|
|
38
|
+
// constructor(helia: Helia) {
|
|
39
|
+
// this.fs = mfs(helia);
|
|
40
|
+
// }
|
|
41
|
+
// ////////////////////////////////
|
|
42
|
+
// // IDocumentView
|
|
43
|
+
// ////////////////////////////////
|
|
44
|
+
// async resolveIds(slugs: string[], signal?: AbortSignal): Promise<string[]> {
|
|
45
|
+
// const ids = [];
|
|
46
|
+
// for (const slug of slugs) {
|
|
47
|
+
// const documentId = this.slugToDocumentId[slug];
|
|
48
|
+
// if (!documentId) {
|
|
49
|
+
// throw new DocumentNotFoundError(slug);
|
|
50
|
+
// }
|
|
51
|
+
// ids.push(documentId);
|
|
52
|
+
// }
|
|
53
|
+
// if (signal?.aborted) {
|
|
54
|
+
// throw new AbortError("Aborted");
|
|
55
|
+
// }
|
|
56
|
+
// return Promise.resolve(ids);
|
|
57
|
+
// }
|
|
58
|
+
// async resolveSlugs(ids: string[], signal?: AbortSignal): Promise<string[]> {
|
|
59
|
+
// const slugs = [];
|
|
60
|
+
// for (const id of ids) {
|
|
61
|
+
// const document = await this.get<PHDocument>(id);
|
|
62
|
+
// if (!document) {
|
|
63
|
+
// throw new DocumentNotFoundError(id);
|
|
64
|
+
// }
|
|
65
|
+
// if (signal?.aborted) {
|
|
66
|
+
// throw new AbortError("Aborted");
|
|
67
|
+
// }
|
|
68
|
+
// slugs.push(document.slug);
|
|
69
|
+
// }
|
|
70
|
+
// return Promise.resolve(slugs);
|
|
71
|
+
// }
|
|
72
|
+
// ////////////////////////////////
|
|
73
|
+
// // IDocumentStorage
|
|
74
|
+
// ////////////////////////////////
|
|
75
|
+
// async exists(documentId: string): Promise<boolean> {
|
|
76
|
+
// try {
|
|
77
|
+
// await this.fs.stat(this._buildDocumentPath(documentId));
|
|
78
|
+
// return true;
|
|
79
|
+
// } catch (error) {
|
|
80
|
+
// //
|
|
81
|
+
// }
|
|
82
|
+
// try {
|
|
83
|
+
// await this.fs.stat(this._buildDrivePath(documentId));
|
|
84
|
+
// return true;
|
|
85
|
+
// } catch (error) {
|
|
86
|
+
// //
|
|
87
|
+
// }
|
|
88
|
+
// return false;
|
|
89
|
+
// }
|
|
90
|
+
// async create(document: PHDocument): Promise<void> {
|
|
91
|
+
// const documentId = document.id;
|
|
92
|
+
// if (await this.exists(documentId)) {
|
|
93
|
+
// throw new DocumentAlreadyExistsError(documentId);
|
|
94
|
+
// }
|
|
95
|
+
// const slug =
|
|
96
|
+
// document.header.slug?.length > 0 ? document.header.slug : documentId;
|
|
97
|
+
// const slugManifest = await this.getSlugManifest();
|
|
98
|
+
// if (slugManifest.slugToId[slug]) {
|
|
99
|
+
// throw new DocumentAlreadyExistsError(
|
|
100
|
+
// documentId,
|
|
101
|
+
// DocumentAlreadyExistsReason.SLUG,
|
|
102
|
+
// );
|
|
103
|
+
// }
|
|
104
|
+
// document.header.slug = slug;
|
|
105
|
+
// await this.fs.writeBytes(
|
|
106
|
+
// new TextEncoder().encode(stringify(document)),
|
|
107
|
+
// this._buildDocumentPath(documentId),
|
|
108
|
+
// );
|
|
109
|
+
// // Update the slug manifest if the document has a slug
|
|
110
|
+
// slugManifest.slugToId[slug] = documentId;
|
|
111
|
+
// await this.updateSlugManifest(slugManifest);
|
|
112
|
+
// // temporary: initialize an empty manifest for new drives
|
|
113
|
+
// if (document.header.documentType === "powerhouse/document-drive") {
|
|
114
|
+
// this.updateDriveManifest(documentId, { documentIds: [] });
|
|
115
|
+
// }
|
|
116
|
+
// }
|
|
117
|
+
// async get<TDocument extends PHDocument>(
|
|
118
|
+
// documentId: string,
|
|
119
|
+
// ): Promise<TDocument> {
|
|
120
|
+
// try {
|
|
121
|
+
// const documentPath = this._buildDocumentPath(documentId);
|
|
122
|
+
// const chunks = [];
|
|
123
|
+
// for await (const chunk of this.fs.cat(documentPath)) {
|
|
124
|
+
// chunks.push(chunk);
|
|
125
|
+
// }
|
|
126
|
+
// const buffer = Buffer.concat(chunks);
|
|
127
|
+
// const content = new TextDecoder().decode(buffer);
|
|
128
|
+
// return JSON.parse(content) as TDocument;
|
|
129
|
+
// } catch (error) {
|
|
130
|
+
// return Promise.reject(new DocumentNotFoundError(documentId));
|
|
131
|
+
// }
|
|
132
|
+
// }
|
|
133
|
+
// async getBySlug<TDocument extends PHDocument>(
|
|
134
|
+
// slug: string,
|
|
135
|
+
// ): Promise<TDocument> {
|
|
136
|
+
// const slugManifest = await this.getSlugManifest();
|
|
137
|
+
// const documentId = slugManifest.slugToId[slug];
|
|
138
|
+
// if (!documentId) {
|
|
139
|
+
// return Promise.reject(new DocumentNotFoundError(slug));
|
|
140
|
+
// }
|
|
141
|
+
// return this.get<TDocument>(documentId);
|
|
142
|
+
// }
|
|
143
|
+
// async findByType(
|
|
144
|
+
// documentModelType: string,
|
|
145
|
+
// limit = 100,
|
|
146
|
+
// cursor?: string,
|
|
147
|
+
// ): Promise<{
|
|
148
|
+
// documents: string[];
|
|
149
|
+
// nextCursor: string | undefined;
|
|
150
|
+
// }> {
|
|
151
|
+
// // Get all document files from IPFS
|
|
152
|
+
// const documentFiles = [];
|
|
153
|
+
// try {
|
|
154
|
+
// for await (const entry of this.fs.ls("/")) {
|
|
155
|
+
// if (
|
|
156
|
+
// entry.name.startsWith("document-") &&
|
|
157
|
+
// entry.name.endsWith(".json")
|
|
158
|
+
// ) {
|
|
159
|
+
// documentFiles.push(entry.name);
|
|
160
|
+
// }
|
|
161
|
+
// }
|
|
162
|
+
// } catch (error) {
|
|
163
|
+
// // If directory listing fails, return empty results
|
|
164
|
+
// return { documents: [], nextCursor: undefined };
|
|
165
|
+
// }
|
|
166
|
+
// // Load documents with matching type and collect their metadata
|
|
167
|
+
// const documentsAndIds: Array<{ id: string; document: PHDocument }> = [];
|
|
168
|
+
// for (const fileName of documentFiles) {
|
|
169
|
+
// // Extract the document ID from the filename
|
|
170
|
+
// const documentId = fileName.replace("document-", "").replace(".json", "");
|
|
171
|
+
// try {
|
|
172
|
+
// // Read and parse the document from IPFS
|
|
173
|
+
// const chunks = [];
|
|
174
|
+
// for await (const chunk of this.fs.cat(`/${fileName}`)) {
|
|
175
|
+
// chunks.push(chunk);
|
|
176
|
+
// }
|
|
177
|
+
// const buffer = Buffer.concat(chunks);
|
|
178
|
+
// const content = new TextDecoder().decode(buffer);
|
|
179
|
+
// const document = JSON.parse(content) as PHDocument;
|
|
180
|
+
// // Only include documents of the requested type
|
|
181
|
+
// if (document.header.documentType === documentModelType) {
|
|
182
|
+
// documentsAndIds.push({ id: documentId, document });
|
|
183
|
+
// }
|
|
184
|
+
// } catch (error) {
|
|
185
|
+
// // Skip files that can't be read or parsed
|
|
186
|
+
// continue;
|
|
187
|
+
// }
|
|
188
|
+
// }
|
|
189
|
+
// // Sort by creation date first, then by ID
|
|
190
|
+
// documentsAndIds.sort((a, b) => {
|
|
191
|
+
// const aDate = new Date(a.document.header.createdAtUtcIso);
|
|
192
|
+
// const bDate = new Date(b.document.header.createdAtUtcIso);
|
|
193
|
+
// if (aDate.getTime() === bDate.getTime()) {
|
|
194
|
+
// return a.id.localeCompare(b.id);
|
|
195
|
+
// }
|
|
196
|
+
// return aDate.getTime() - bDate.getTime();
|
|
197
|
+
// });
|
|
198
|
+
// // cursor
|
|
199
|
+
// let startIndex = 0;
|
|
200
|
+
// if (cursor) {
|
|
201
|
+
// const index = documentsAndIds.findIndex(({ id }) => id === cursor);
|
|
202
|
+
// if (index !== -1) {
|
|
203
|
+
// startIndex = index;
|
|
204
|
+
// }
|
|
205
|
+
// }
|
|
206
|
+
// // count to limit
|
|
207
|
+
// const endIndex = Math.min(startIndex + limit, documentsAndIds.length);
|
|
208
|
+
// let nextCursor: string | undefined;
|
|
209
|
+
// if (endIndex < documentsAndIds.length) {
|
|
210
|
+
// nextCursor = documentsAndIds[endIndex].id;
|
|
211
|
+
// }
|
|
212
|
+
// return {
|
|
213
|
+
// documents: documentsAndIds
|
|
214
|
+
// .slice(startIndex, endIndex)
|
|
215
|
+
// .map(({ id }) => id),
|
|
216
|
+
// nextCursor,
|
|
217
|
+
// };
|
|
218
|
+
// }
|
|
219
|
+
// async delete(documentId: string): Promise<boolean> {
|
|
220
|
+
// // Remove from slug manifest if it has a slug
|
|
221
|
+
// try {
|
|
222
|
+
// const document = await this.get<PHDocument>(documentId);
|
|
223
|
+
// const slug =
|
|
224
|
+
// document.header.slug?.length > 0 ? document.header.slug : documentId;
|
|
225
|
+
// if (slug) {
|
|
226
|
+
// const slugManifest = await this.getSlugManifest();
|
|
227
|
+
// if (slugManifest.slugToId[slug] === documentId) {
|
|
228
|
+
// delete slugManifest.slugToId[slug];
|
|
229
|
+
// await this.updateSlugManifest(slugManifest);
|
|
230
|
+
// }
|
|
231
|
+
// }
|
|
232
|
+
// } catch (error) {
|
|
233
|
+
// // If we can't get the document, we can't remove its slug
|
|
234
|
+
// }
|
|
235
|
+
// // delete the document from parent manifests
|
|
236
|
+
// const parents = await this.getParents(documentId);
|
|
237
|
+
// for (const parent of parents) {
|
|
238
|
+
// await this.removeChild(parent, documentId);
|
|
239
|
+
// }
|
|
240
|
+
// // check children: any children that are only children of this document should be deleted
|
|
241
|
+
// const children = await this.getChildren(documentId);
|
|
242
|
+
// for (const child of children) {
|
|
243
|
+
// const childParents = await this.getParents(child);
|
|
244
|
+
// if (childParents.length === 1 && childParents[0] === documentId) {
|
|
245
|
+
// await this.delete(child);
|
|
246
|
+
// }
|
|
247
|
+
// }
|
|
248
|
+
// // delete any manifest for this document
|
|
249
|
+
// try {
|
|
250
|
+
// await this.fs.rm(this._buildDriveManifestPath(documentId));
|
|
251
|
+
// } catch (error) {
|
|
252
|
+
// // there may be no manifest for this document
|
|
253
|
+
// }
|
|
254
|
+
// // finally, delete the specified document
|
|
255
|
+
// try {
|
|
256
|
+
// await this.fs.rm(this._buildDocumentPath(documentId));
|
|
257
|
+
// return true;
|
|
258
|
+
// } catch (error) {
|
|
259
|
+
// return false;
|
|
260
|
+
// }
|
|
261
|
+
// }
|
|
262
|
+
// async addChild(parentId: string, childId: string): Promise<void> {
|
|
263
|
+
// if (parentId === childId) {
|
|
264
|
+
// throw new Error("Cannot associate a document with itself");
|
|
265
|
+
// }
|
|
266
|
+
// // check if the child is a parent of the parent
|
|
267
|
+
// const children = await this.getChildren(childId);
|
|
268
|
+
// if (children.includes(parentId)) {
|
|
269
|
+
// throw new Error("Cannot associate a document with its child");
|
|
270
|
+
// }
|
|
271
|
+
// const manifest = await this.getDriveManifest(parentId);
|
|
272
|
+
// if (!manifest.documentIds.includes(childId)) {
|
|
273
|
+
// manifest.documentIds.push(childId);
|
|
274
|
+
// await this.updateDriveManifest(parentId, manifest);
|
|
275
|
+
// }
|
|
276
|
+
// }
|
|
277
|
+
// async removeChild(parentId: string, childId: string): Promise<boolean> {
|
|
278
|
+
// const manifest = await this.getDriveManifest(parentId);
|
|
279
|
+
// const docIndex = manifest.documentIds.indexOf(childId);
|
|
280
|
+
// if (docIndex !== -1) {
|
|
281
|
+
// manifest.documentIds.splice(docIndex, 1);
|
|
282
|
+
// await this.updateDriveManifest(parentId, manifest);
|
|
283
|
+
// return true;
|
|
284
|
+
// }
|
|
285
|
+
// return false;
|
|
286
|
+
// }
|
|
287
|
+
// async getChildren(parentId: string): Promise<string[]> {
|
|
288
|
+
// const manifest = await this.getDriveManifest(parentId);
|
|
289
|
+
// return manifest.documentIds;
|
|
290
|
+
// }
|
|
291
|
+
// async getParents(childId: string): Promise<string[]> {
|
|
292
|
+
// const parents: string[] = [];
|
|
293
|
+
// // Get all manifest files by listing the directory and finding manifest files
|
|
294
|
+
// try {
|
|
295
|
+
// for await (const entry of this.fs.ls("/")) {
|
|
296
|
+
// if (
|
|
297
|
+
// entry.name.startsWith("manifest-") &&
|
|
298
|
+
// entry.name.endsWith(".json")
|
|
299
|
+
// ) {
|
|
300
|
+
// // Extract the driveId from the manifest filename
|
|
301
|
+
// const driveId = entry.name
|
|
302
|
+
// .replace("manifest-", "")
|
|
303
|
+
// .replace(".json", "");
|
|
304
|
+
// // Check if the manifest contains the childId
|
|
305
|
+
// const manifest = await this.getDriveManifest(driveId);
|
|
306
|
+
// if (manifest.documentIds.includes(childId)) {
|
|
307
|
+
// parents.push(driveId);
|
|
308
|
+
// }
|
|
309
|
+
// }
|
|
310
|
+
// }
|
|
311
|
+
// } catch (error) {
|
|
312
|
+
// // If listing fails, return empty array
|
|
313
|
+
// }
|
|
314
|
+
// return parents;
|
|
315
|
+
// }
|
|
316
|
+
// // IDriveStorage
|
|
317
|
+
// ////////////////////////////////
|
|
318
|
+
// async addDocumentOperations<TDocument extends PHDocument>(
|
|
319
|
+
// drive: string,
|
|
320
|
+
// id: string,
|
|
321
|
+
// operations: Operation[],
|
|
322
|
+
// header: PHDocumentHeader,
|
|
323
|
+
// ): Promise<void> {
|
|
324
|
+
// const document = await this.get<TDocument>(id);
|
|
325
|
+
// if (!document) {
|
|
326
|
+
// throw new Error(`Document with id ${id} not found`);
|
|
327
|
+
// }
|
|
328
|
+
// const mergedOperations = mergeOperations(document.operations, operations);
|
|
329
|
+
// await this.create(id, {
|
|
330
|
+
// ...document,
|
|
331
|
+
// header,
|
|
332
|
+
// operations: mergedOperations,
|
|
333
|
+
// });
|
|
334
|
+
// await this.addChild(drive, id);
|
|
335
|
+
// }
|
|
336
|
+
// async getDrives(): Promise<string[]> {
|
|
337
|
+
// const result = await this.findByType("powerhouse/document-drive");
|
|
338
|
+
// return result.documents;
|
|
339
|
+
// }
|
|
340
|
+
// async getDrive(id: string): Promise<DocumentDriveDocument> {
|
|
341
|
+
// try {
|
|
342
|
+
// const drivePath = this._buildDrivePath(id);
|
|
343
|
+
// const chunks = [];
|
|
344
|
+
// for await (const chunk of this.fs.cat(drivePath)) {
|
|
345
|
+
// chunks.push(chunk);
|
|
346
|
+
// }
|
|
347
|
+
// const buffer = Buffer.concat(chunks);
|
|
348
|
+
// const content = new TextDecoder().decode(buffer);
|
|
349
|
+
// return JSON.parse(content) as DocumentDriveDocument;
|
|
350
|
+
// } catch {
|
|
351
|
+
// throw new Error(`Drive with id ${id} not found`);
|
|
352
|
+
// }
|
|
353
|
+
// }
|
|
354
|
+
// async addDriveOperations(
|
|
355
|
+
// id: string,
|
|
356
|
+
// operations: Operation<DocumentDriveAction>[],
|
|
357
|
+
// header: PHDocumentHeader,
|
|
358
|
+
// ): Promise<void> {
|
|
359
|
+
// const drive = await this.getDrive(id);
|
|
360
|
+
// const mergedOperations = mergeOperations<DocumentDriveDocument>(
|
|
361
|
+
// drive.operations,
|
|
362
|
+
// operations,
|
|
363
|
+
// );
|
|
364
|
+
// await this.create(id, {
|
|
365
|
+
// ...drive,
|
|
366
|
+
// header,
|
|
367
|
+
// operations: mergedOperations,
|
|
368
|
+
// });
|
|
369
|
+
// }
|
|
370
|
+
// async clearStorage(): Promise<void> {
|
|
371
|
+
// // Delete all files
|
|
372
|
+
// try {
|
|
373
|
+
// for await (const entry of this.fs.ls("/")) {
|
|
374
|
+
// if (entry.type === "file") {
|
|
375
|
+
// await this.fs.rm(`/${entry.name}`);
|
|
376
|
+
// }
|
|
377
|
+
// }
|
|
378
|
+
// } catch (error) {
|
|
379
|
+
// // Ignore any errors when trying to list/delete files
|
|
380
|
+
// }
|
|
381
|
+
// }
|
|
382
|
+
// async getSynchronizationUnitsRevision(
|
|
383
|
+
// units: SynchronizationUnitQuery[],
|
|
384
|
+
// ): Promise<
|
|
385
|
+
// {
|
|
386
|
+
// documentId: string;
|
|
387
|
+
// scope: string;
|
|
388
|
+
// branch: string;
|
|
389
|
+
// lastUpdated: string;
|
|
390
|
+
// revision: number;
|
|
391
|
+
// }[]
|
|
392
|
+
// > {
|
|
393
|
+
// const results = await Promise.allSettled(
|
|
394
|
+
// units.map(async (unit) => {
|
|
395
|
+
// try {
|
|
396
|
+
// const document = await this.get<PHDocument>(unit.documentId);
|
|
397
|
+
// if (!document) {
|
|
398
|
+
// return undefined;
|
|
399
|
+
// }
|
|
400
|
+
// const operation = document.operations[unit.scope].at(-1);
|
|
401
|
+
// if (operation) {
|
|
402
|
+
// return {
|
|
403
|
+
// documentId: unit.documentId,
|
|
404
|
+
// scope: unit.scope,
|
|
405
|
+
// branch: unit.branch,
|
|
406
|
+
// lastUpdated: operation.timestampUtcMs,
|
|
407
|
+
// revision: operation.index,
|
|
408
|
+
// };
|
|
409
|
+
// }
|
|
410
|
+
// } catch {
|
|
411
|
+
// return undefined;
|
|
412
|
+
// }
|
|
413
|
+
// }),
|
|
414
|
+
// );
|
|
415
|
+
// return results.reduce<
|
|
416
|
+
// {
|
|
417
|
+
// documentId: string;
|
|
418
|
+
// scope: string;
|
|
419
|
+
// branch: string;
|
|
420
|
+
// lastUpdated: string;
|
|
421
|
+
// revision: number;
|
|
422
|
+
// }[]
|
|
423
|
+
// >((acc, curr) => {
|
|
424
|
+
// if (curr.status === "fulfilled" && curr.value !== undefined) {
|
|
425
|
+
// acc.push(curr.value);
|
|
426
|
+
// }
|
|
427
|
+
// return acc;
|
|
428
|
+
// }, []);
|
|
429
|
+
// }
|
|
430
|
+
// ////////////////////////////////
|
|
431
|
+
// // Private methods
|
|
432
|
+
// ////////////////////////////////
|
|
433
|
+
// private _buildDocumentPath(documentId: string): string {
|
|
434
|
+
// return `/document-${documentId}.json`;
|
|
435
|
+
// }
|
|
436
|
+
// private _buildDrivePath(driveId: string): string {
|
|
437
|
+
// return `/drive-${driveId}.json`;
|
|
438
|
+
// }
|
|
439
|
+
// private _buildDriveManifestPath(driveId: string): string {
|
|
440
|
+
// return `/manifest-${driveId}.json`;
|
|
441
|
+
// }
|
|
442
|
+
// private _buildSlugManifestPath(): string {
|
|
443
|
+
// return `/slugs.json`;
|
|
444
|
+
// }
|
|
445
|
+
// private async getDriveManifest(driveId: string): Promise<DriveManifest> {
|
|
446
|
+
// try {
|
|
447
|
+
// const manifestPath = this._buildDriveManifestPath(driveId);
|
|
448
|
+
// const chunks = [];
|
|
449
|
+
// for await (const chunk of this.fs.cat(manifestPath)) {
|
|
450
|
+
// chunks.push(chunk);
|
|
451
|
+
// }
|
|
452
|
+
// const buffer = Buffer.concat(chunks);
|
|
453
|
+
// const content = new TextDecoder().decode(buffer);
|
|
454
|
+
// return JSON.parse(content) as DriveManifest;
|
|
455
|
+
// } catch (error) {
|
|
456
|
+
// // If manifest doesn't exist, return an empty one
|
|
457
|
+
// return { documentIds: [] };
|
|
458
|
+
// }
|
|
459
|
+
// }
|
|
460
|
+
// private async updateDriveManifest(
|
|
461
|
+
// driveId: string,
|
|
462
|
+
// manifest: DriveManifest,
|
|
463
|
+
// ): Promise<void> {
|
|
464
|
+
// const manifestPath = this._buildDriveManifestPath(driveId);
|
|
465
|
+
// const manifestContent = stringify(manifest);
|
|
466
|
+
// const manifestBuffer = new TextEncoder().encode(manifestContent);
|
|
467
|
+
// await this.fs.writeBytes(manifestBuffer, manifestPath, { force: true });
|
|
468
|
+
// }
|
|
469
|
+
// private async getSlugManifest(): Promise<SlugManifest> {
|
|
470
|
+
// try {
|
|
471
|
+
// const manifestPath = this._buildSlugManifestPath();
|
|
472
|
+
// const chunks = [];
|
|
473
|
+
// for await (const chunk of this.fs.cat(manifestPath)) {
|
|
474
|
+
// chunks.push(chunk);
|
|
475
|
+
// }
|
|
476
|
+
// const buffer = Buffer.concat(chunks);
|
|
477
|
+
// const content = new TextDecoder().decode(buffer);
|
|
478
|
+
// return JSON.parse(content) as SlugManifest;
|
|
479
|
+
// } catch (error) {
|
|
480
|
+
// // If manifest doesn't exist, return an empty one
|
|
481
|
+
// return { slugToId: {} };
|
|
482
|
+
// }
|
|
483
|
+
// }
|
|
484
|
+
// private async updateSlugManifest(manifest: SlugManifest): Promise<void> {
|
|
485
|
+
// const manifestPath = this._buildSlugManifestPath();
|
|
486
|
+
// const manifestContent = stringify(manifest);
|
|
487
|
+
// const manifestBuffer = new TextEncoder().encode(manifestContent);
|
|
488
|
+
// await this.fs.writeBytes(manifestBuffer, manifestPath, { force: true });
|
|
489
|
+
// }
|
|
490
|
+
// }
|
|
416
491
|
//# sourceMappingURL=ipfs.js.map
|