@peerbit/please-lib 0.0.5 → 0.0.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.
@@ -1,71 +1,120 @@
1
1
  import { Program } from "@peerbit/program";
2
2
  import { Documents, Role } from "@peerbit/document";
3
+ import { SyncFilter } from "@peerbit/shared-log";
4
+ import { PublicSignKey } from "@peerbit/crypto";
3
5
  import { ProgramClient } from "@peerbit/program";
4
- declare abstract class AbstractFile {
6
+ import { TrustedNetwork } from "@peerbit/trusted-network";
7
+ export declare abstract class AbstractFile {
5
8
  abstract id: string;
6
9
  abstract name: string;
7
- abstract getFile(files: Files): Promise<Uint8Array | undefined>;
10
+ abstract size: number;
11
+ abstract parentId?: string;
12
+ abstract getFile<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(files: Files, properties?: {
13
+ as: OutputType;
14
+ timeout?: number;
15
+ progress?: (progress: number) => any;
16
+ }): Promise<Output>;
17
+ abstract delete(files: Files): Promise<void>;
8
18
  }
9
19
  export declare class TinyFile extends AbstractFile {
10
20
  id: string;
11
21
  name: string;
12
22
  file: Uint8Array;
23
+ parentId?: string;
24
+ get size(): number;
13
25
  constructor(properties: {
14
26
  name: string;
15
27
  file: Uint8Array;
28
+ parentId?: string;
16
29
  });
17
- getFile(_files: Files): Promise<Uint8Array>;
30
+ getFile<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(_files: Files, properties?: {
31
+ as: OutputType;
32
+ progress?: (progress: number) => any;
33
+ }): Promise<Output>;
34
+ delete(): Promise<void>;
18
35
  }
19
36
  export declare class SmallFile extends AbstractFile {
20
37
  id: string;
21
38
  name: string;
22
39
  cid: string;
40
+ size: number;
41
+ parentId?: string;
23
42
  constructor(properties: {
24
43
  name: string;
25
44
  cid: string;
45
+ size: number;
46
+ parentId?: string;
26
47
  });
27
- static create(node: ProgramClient, name: string, file: Uint8Array): Promise<SmallFile>;
28
- getFile(files: Files): Promise<Uint8Array | undefined>;
48
+ static create(node: ProgramClient, name: string, file: Uint8Array, parentId?: string): Promise<SmallFile>;
49
+ getFile<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(files: Files, properties?: {
50
+ as: OutputType;
51
+ timeout?: number;
52
+ progress?: (progress: number) => any;
53
+ }): Promise<Output>;
54
+ delete(files: Files): Promise<void>;
29
55
  }
30
56
  export declare class LargeFile extends AbstractFile {
31
57
  id: string;
32
58
  name: string;
33
59
  fileIds: string[];
60
+ size: number;
34
61
  constructor(properties: {
35
62
  id: string;
36
63
  name: string;
37
64
  fileIds: string[];
65
+ size: number;
38
66
  });
39
67
  static create(name: string, file: Uint8Array, files: Files): Promise<LargeFile>;
40
- getFile(files: Files): Promise<Uint8Array>;
68
+ get parentId(): undefined;
69
+ fetchChunks(files: Files): Promise<AbstractFile[]>;
70
+ delete(files: Files): Promise<void>;
71
+ getFile<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(files: Files, properties?: {
72
+ as: OutputType;
73
+ timeout?: number;
74
+ progress?: (progress: number) => any;
75
+ }): Promise<Output>;
41
76
  }
42
77
  type Args = {
43
78
  role: Role;
79
+ sync?: SyncFilter | undefined;
44
80
  };
45
81
  export declare class Files extends Program<Args> {
46
82
  id: Uint8Array;
83
+ name: string;
84
+ trustGraph?: TrustedNetwork;
47
85
  files: Documents<AbstractFile>;
48
- constructor(id?: Uint8Array);
49
- create(name: string, file: Uint8Array): Promise<string>;
86
+ constructor(properties?: {
87
+ id?: Uint8Array;
88
+ name?: string;
89
+ rootKey?: PublicSignKey;
90
+ });
91
+ add(name: string, file: Uint8Array, parentId?: string): Promise<string>;
92
+ removeById(id: string): Promise<void>;
93
+ removeByName(name: string): Promise<void>;
94
+ list(): Promise<AbstractFile[]>;
50
95
  /**
51
96
  * Get by name
52
97
  * @param id
53
98
  * @returns
54
99
  */
55
- getById(id: string): Promise<{
100
+ getById<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(id: string, properties?: {
101
+ as: OutputType;
102
+ }): Promise<{
56
103
  id: string;
57
104
  name: string;
58
- bytes: Uint8Array;
105
+ bytes: Output;
59
106
  } | undefined>;
60
107
  /**
61
108
  * Get by name
62
109
  * @param name
63
110
  * @returns
64
111
  */
65
- getByName(name: string): Promise<{
112
+ getByName<OutputType extends "chunks" | "joined" = "joined", Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array>(name: string, properties?: {
113
+ as: OutputType;
114
+ }): Promise<{
66
115
  id: string;
67
116
  name: string;
68
- bytes: Uint8Array;
117
+ bytes: Output;
69
118
  } | undefined>;
70
119
  open(args?: Args): Promise<void>;
71
120
  }
package/lib/esm/index.js CHANGED
@@ -8,29 +8,41 @@ var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
10
  var SmallFile_1, LargeFile_1;
11
- import { field, variant, vec } from "@dao-xyz/borsh";
11
+ import { field, variant, vec, option } from "@dao-xyz/borsh";
12
12
  import { Program } from "@peerbit/program";
13
- import { Documents, SearchRequest, StringMatch } from "@peerbit/document";
13
+ import { Documents, SearchRequest, StringMatch, StringMatchMethod, Or, } from "@peerbit/document";
14
14
  import { sha256Base64Sync, randomBytes } from "@peerbit/crypto";
15
15
  import { concat } from "uint8arrays";
16
- class AbstractFile {
16
+ import { sha256Sync } from "@peerbit/crypto";
17
+ import { TrustedNetwork } from "@peerbit/trusted-network";
18
+ import PQueue from "p-queue";
19
+ export class AbstractFile {
17
20
  }
18
21
  const TINY_FILE_SIZE_LIMIT = 1e3;
19
- export let TinyFile = class TinyFile extends AbstractFile {
22
+ let TinyFile = class TinyFile extends AbstractFile {
20
23
  id;
21
24
  name;
22
25
  file; // 10 mb imit
26
+ parentId;
27
+ get size() {
28
+ return this.file.byteLength;
29
+ }
23
30
  constructor(properties) {
24
31
  super();
25
32
  this.id = sha256Base64Sync(properties.file);
26
33
  this.name = properties.name;
27
34
  this.file = properties.file;
35
+ this.parentId = properties.parentId;
28
36
  }
29
- async getFile(_files) {
37
+ async getFile(_files, properties) {
30
38
  if (sha256Base64Sync(this.file) !== this.id) {
31
39
  throw new Error("Hash does not match the file content");
32
40
  }
33
- return Promise.resolve(this.file);
41
+ properties?.progress?.(1);
42
+ return Promise.resolve(properties?.as == "chunks" ? [this.file] : this.file);
43
+ }
44
+ async delete() {
45
+ // Do nothing, since no releated files where created
34
46
  }
35
47
  };
36
48
  __decorate([
@@ -45,32 +57,50 @@ __decorate([
45
57
  field({ type: Uint8Array }),
46
58
  __metadata("design:type", Uint8Array)
47
59
  ], TinyFile.prototype, "file", void 0);
60
+ __decorate([
61
+ field({ type: option("string") }),
62
+ __metadata("design:type", String)
63
+ ], TinyFile.prototype, "parentId", void 0);
48
64
  TinyFile = __decorate([
49
65
  variant(0) // for versioning purposes
50
66
  ,
51
67
  __metadata("design:paramtypes", [Object])
52
68
  ], TinyFile);
69
+ export { TinyFile };
53
70
  const SMALL_FILE_SIZE_LIMIT = 1e6 * 9;
54
- export let SmallFile = SmallFile_1 = class SmallFile extends AbstractFile {
71
+ // TODO Do we really need this if we have TinyFile and LargeFile?
72
+ let SmallFile = SmallFile_1 = class SmallFile extends AbstractFile {
55
73
  id; // cid
56
74
  name;
57
75
  cid; // store file separately in a block store, like IPFS
76
+ size;
77
+ parentId;
58
78
  constructor(properties) {
59
79
  super();
60
80
  this.id = properties.cid;
61
81
  this.name = properties.name;
62
82
  this.cid = properties.cid;
83
+ this.size = properties.size;
84
+ this.parentId = properties.parentId;
63
85
  }
64
- static async create(node, name, file) {
86
+ static async create(node, name, file, parentId) {
65
87
  if (file.length > SMALL_FILE_SIZE_LIMIT) {
66
88
  throw new Error("To large file for SmallFile");
67
89
  }
68
90
  const cid = await node.services.blocks.put(file);
69
- return new SmallFile_1({ name, cid });
91
+ return new SmallFile_1({ name, cid, size: file.byteLength, parentId });
70
92
  }
71
- async getFile(files) {
93
+ async getFile(files, properties) {
94
+ properties?.progress?.(0);
72
95
  // Load the file from the block store
73
- return await files.node.services.blocks.get(this.id);
96
+ const block = await files.node.services.blocks.get(this.id, {
97
+ timeout: properties?.timeout,
98
+ });
99
+ properties?.progress?.(1);
100
+ return (properties?.as == "chunks" ? [block] : block);
101
+ }
102
+ async delete(files) {
103
+ return await files.node.services.blocks.rm(this.id);
74
104
  }
75
105
  };
76
106
  __decorate([
@@ -85,58 +115,113 @@ __decorate([
85
115
  field({ type: "string" }),
86
116
  __metadata("design:type", String)
87
117
  ], SmallFile.prototype, "cid", void 0);
118
+ __decorate([
119
+ field({ type: "u32" }),
120
+ __metadata("design:type", Number)
121
+ ], SmallFile.prototype, "size", void 0);
122
+ __decorate([
123
+ field({ type: option("string") }),
124
+ __metadata("design:type", String)
125
+ ], SmallFile.prototype, "parentId", void 0);
88
126
  SmallFile = SmallFile_1 = __decorate([
89
127
  variant(1) // for versioning purposes
90
128
  ,
91
129
  __metadata("design:paramtypes", [Object])
92
130
  ], SmallFile);
93
- export let LargeFile = LargeFile_1 = class LargeFile extends AbstractFile {
131
+ export { SmallFile };
132
+ let LargeFile = LargeFile_1 = class LargeFile extends AbstractFile {
94
133
  id; // hash
95
134
  name;
96
135
  fileIds;
136
+ size;
97
137
  constructor(properties) {
98
138
  super();
99
139
  this.id = properties.id;
100
140
  this.name = properties.name;
101
141
  this.fileIds = properties.fileIds;
142
+ this.size = properties.size;
102
143
  }
103
144
  static async create(name, file, files) {
104
145
  const segmetSize = SMALL_FILE_SIZE_LIMIT / 10; // 10% of the small size limit
105
146
  const fileIds = [];
106
147
  const id = sha256Base64Sync(file);
148
+ const fileSize = file.byteLength;
107
149
  for (let i = 0; i < Math.ceil(file.byteLength / segmetSize); i++) {
108
- fileIds.push(await files.create(id + "/" + name, file.subarray(i * segmetSize, Math.min((i + 1) * segmetSize, file.length))));
150
+ fileIds.push(await files.add(name + "/" + i, file.subarray(i * segmetSize, Math.min((i + 1) * segmetSize, file.byteLength)), id));
109
151
  }
110
- return new LargeFile_1({ id, name, fileIds });
152
+ return new LargeFile_1({ id, name, fileIds, size: fileSize });
111
153
  }
112
- async getFile(files) {
113
- // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
154
+ get parentId() {
155
+ // Large file can never have a parent
156
+ return undefined;
157
+ }
158
+ async fetchChunks(files) {
114
159
  const expectedIds = new Set(this.fileIds);
115
- const chunks = new Map();
116
- const results = await files.files.index.search(new SearchRequest({
160
+ const allFiles = await files.files.index.search(new SearchRequest({
117
161
  query: [
118
- new StringMatch({
119
- key: "name",
120
- value: this.id + "/" + this.name,
121
- }),
162
+ new Or([...expectedIds].map((x) => new StringMatch({ key: "id", value: x }))),
122
163
  ],
123
- }), {
124
- local: true,
125
- remote: true,
164
+ }));
165
+ return allFiles;
166
+ }
167
+ async delete(files) {
168
+ await Promise.all((await this.fetchChunks(files)).map((x) => x.delete(files)));
169
+ }
170
+ async getFile(files, properties) {
171
+ // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
172
+ properties?.progress?.(0);
173
+ console.log("LISTS CHUNKS!");
174
+ const allChunks = await this.fetchChunks(files);
175
+ console.log("RECEIVED CHUNKS: " + allChunks.length);
176
+ console.log("FETCH CHUNKS");
177
+ const fetchQueue = new PQueue({ concurrency: 10 });
178
+ let fetchError = undefined;
179
+ fetchQueue.on("error", (err) => {
180
+ fetchError = err;
126
181
  });
127
- if (results.length > 0) {
128
- for (const r of results) {
182
+ const chunks = new Map();
183
+ const expectedIds = new Set(this.fileIds);
184
+ if (allChunks.length > 0) {
185
+ let c = 0;
186
+ for (const r of allChunks) {
129
187
  if (chunks.has(r.id)) {
130
188
  // chunk already added;
131
189
  }
132
190
  if (!expectedIds.has(r.id)) {
133
191
  // chunk is not part of this file
134
192
  }
135
- chunks.set(r.id, r.getFile(files));
193
+ fetchQueue
194
+ .add(async () => {
195
+ let lastError = undefined;
196
+ for (let i = 0; i < 3; i++) {
197
+ try {
198
+ const chunk = await r.getFile(files, {
199
+ as: "joined",
200
+ timeout: properties?.timeout,
201
+ });
202
+ if (!chunk) {
203
+ throw new Error("Failed to fetch chunk");
204
+ }
205
+ chunks.set(r.id, chunk);
206
+ c++;
207
+ properties?.progress?.(c / allChunks.length);
208
+ return;
209
+ }
210
+ catch (error) {
211
+ // try 3 times
212
+ lastError = error;
213
+ }
214
+ }
215
+ throw lastError;
216
+ })
217
+ .catch(() => {
218
+ fetchQueue.clear(); // Dont do anything more since we failed to fetch one block
219
+ });
136
220
  }
137
221
  }
138
- if (chunks.size !== expectedIds.size) {
139
- throw new Error("Failed to resolve file");
222
+ await fetchQueue.onIdle();
223
+ if (fetchError || chunks.size !== expectedIds.size) {
224
+ throw new Error(`Failed to resolve file. Recieved ${chunks.size}/${expectedIds.size} chunks`);
140
225
  }
141
226
  const chunkContentResolved = await Promise.all(this.fileIds.map(async (x) => {
142
227
  const chunkValue = await chunks.get(x);
@@ -145,7 +230,10 @@ export let LargeFile = LargeFile_1 = class LargeFile extends AbstractFile {
145
230
  }
146
231
  return chunkValue;
147
232
  }));
148
- return concat(chunkContentResolved);
233
+ console.log("FETCH DONE");
234
+ return (properties?.as == "chunks"
235
+ ? chunkContentResolved
236
+ : concat(chunkContentResolved));
149
237
  }
150
238
  };
151
239
  __decorate([
@@ -160,40 +248,88 @@ __decorate([
160
248
  field({ type: vec("string") }),
161
249
  __metadata("design:type", Array)
162
250
  ], LargeFile.prototype, "fileIds", void 0);
251
+ __decorate([
252
+ field({ type: "u32" }),
253
+ __metadata("design:type", Number)
254
+ ], LargeFile.prototype, "size", void 0);
163
255
  LargeFile = LargeFile_1 = __decorate([
164
256
  variant(2) // for versioning purposes
165
257
  ,
166
258
  __metadata("design:paramtypes", [Object])
167
259
  ], LargeFile);
168
- export let Files = class Files extends Program {
260
+ export { LargeFile };
261
+ let Files = class Files extends Program {
169
262
  id;
263
+ name;
264
+ trustGraph;
170
265
  files;
171
- constructor(id = randomBytes(32)) {
266
+ constructor(properties = {}) {
172
267
  super();
173
- this.id = id;
174
- this.files = new Documents({ id: this.id });
268
+ this.id = properties.id || randomBytes(32);
269
+ this.name = properties.name || "";
270
+ this.trustGraph = properties.rootKey
271
+ ? new TrustedNetwork({ id: this.id, rootTrust: properties.rootKey })
272
+ : undefined;
273
+ this.files = new Documents({
274
+ id: sha256Sync(concat([
275
+ this.id,
276
+ new TextEncoder().encode(this.name),
277
+ properties.rootKey?.bytes || new Uint8Array(0),
278
+ ])),
279
+ });
175
280
  }
176
- async create(name, file) {
281
+ async add(name, file, parentId) {
177
282
  let toPut;
178
283
  if (file.byteLength <= TINY_FILE_SIZE_LIMIT) {
179
- toPut = new TinyFile({ name, file });
284
+ toPut = new TinyFile({ name, file, parentId });
180
285
  }
181
286
  else if (file.byteLength > TINY_FILE_SIZE_LIMIT &&
182
287
  file.byteLength <= SMALL_FILE_SIZE_LIMIT) {
183
- toPut = await SmallFile.create(this.node, name, file);
288
+ toPut = await SmallFile.create(this.node, name, file, parentId);
184
289
  }
185
290
  else {
291
+ if (parentId) {
292
+ throw new Error("Unexpected that a LargeFile to have a parent");
293
+ }
186
294
  toPut = await LargeFile.create(name, file, this);
187
295
  }
188
296
  await this.files.put(toPut);
189
297
  return toPut.id;
190
298
  }
299
+ async removeById(id) {
300
+ const file = await this.files.index.get(id);
301
+ if (file) {
302
+ await file.delete(this);
303
+ await this.files.del(file.id);
304
+ }
305
+ }
306
+ async removeByName(name) {
307
+ const files = await this.files.index.search(new SearchRequest({
308
+ query: new StringMatch({
309
+ key: ["name"],
310
+ value: name,
311
+ caseInsensitive: false,
312
+ method: StringMatchMethod.exact,
313
+ }),
314
+ }));
315
+ for (const file of files) {
316
+ await file.delete(this);
317
+ await this.files.del(file.id);
318
+ }
319
+ }
320
+ async list() {
321
+ const files = await this.files.index.search(new SearchRequest(), {
322
+ local: true,
323
+ remote: true,
324
+ });
325
+ return files;
326
+ }
191
327
  /**
192
328
  * Get by name
193
329
  * @param id
194
330
  * @returns
195
331
  */
196
- async getById(id) {
332
+ async getById(id, properties) {
197
333
  const results = await this.files.index.search(new SearchRequest({
198
334
  query: [new StringMatch({ key: "id", value: id })],
199
335
  }), {
@@ -203,9 +339,13 @@ export let Files = class Files extends Program {
203
339
  },
204
340
  });
205
341
  for (const result of results) {
206
- const file = await result.getFile(this);
342
+ const file = await result.getFile(this, properties);
207
343
  if (file) {
208
- return { id: result.id, name: result.name, bytes: file };
344
+ return {
345
+ id: result.id,
346
+ name: result.name,
347
+ bytes: file,
348
+ };
209
349
  }
210
350
  }
211
351
  }
@@ -214,7 +354,7 @@ export let Files = class Files extends Program {
214
354
  * @param name
215
355
  * @returns
216
356
  */
217
- async getByName(name) {
357
+ async getByName(name, properties) {
218
358
  const results = await this.files.index.search(new SearchRequest({
219
359
  query: [new StringMatch({ key: "name", value: name })],
220
360
  }), {
@@ -224,24 +364,48 @@ export let Files = class Files extends Program {
224
364
  },
225
365
  });
226
366
  for (const result of results) {
227
- const file = await result.getFile(this);
367
+ const file = await result.getFile(this, properties);
228
368
  if (file) {
229
- return { id: result.id, name: result.name, bytes: file };
369
+ return {
370
+ id: result.id,
371
+ name: result.name,
372
+ bytes: file,
373
+ };
230
374
  }
231
375
  }
232
376
  }
233
377
  // Setup lifecycle, will be invoked on 'open'
234
378
  async open(args) {
379
+ await this.trustGraph?.open({
380
+ role: args?.role,
381
+ });
235
382
  await this.files.open({
236
383
  type: AbstractFile,
237
- canAppend: async (entry) => {
238
- await entry.verifySignatures();
239
- return true; // no verification as of now
384
+ // TODO add ACL
385
+ role: args?.role,
386
+ sync: args?.sync,
387
+ replicas: { min: 3 },
388
+ canPerform: async (operation, context) => {
389
+ if (!this.trustGraph) {
390
+ return true;
391
+ }
392
+ for (const key of await context.entry.getPublicKeys()) {
393
+ if (await this.trustGraph.isTrusted(key)) {
394
+ return true;
395
+ }
396
+ }
397
+ return false;
240
398
  },
241
- canRead: async (identity) => {
242
- return true; // Anyone can query
399
+ index: {
400
+ fields: (doc) => {
401
+ return {
402
+ id: doc.id,
403
+ name: doc.name,
404
+ size: doc.size,
405
+ parentId: doc.parentId,
406
+ };
407
+ },
243
408
  },
244
- role: args?.role,
245
409
  });
246
410
  }
247
411
  };
@@ -249,12 +413,21 @@ __decorate([
249
413
  field({ type: Uint8Array }),
250
414
  __metadata("design:type", Uint8Array)
251
415
  ], Files.prototype, "id", void 0);
416
+ __decorate([
417
+ field({ type: "string" }),
418
+ __metadata("design:type", String)
419
+ ], Files.prototype, "name", void 0);
420
+ __decorate([
421
+ field({ type: option(TrustedNetwork) }),
422
+ __metadata("design:type", TrustedNetwork)
423
+ ], Files.prototype, "trustGraph", void 0);
252
424
  __decorate([
253
425
  field({ type: Documents }),
254
426
  __metadata("design:type", Documents)
255
427
  ], Files.prototype, "files", void 0);
256
428
  Files = __decorate([
257
429
  variant("files"),
258
- __metadata("design:paramtypes", [Uint8Array])
430
+ __metadata("design:paramtypes", [Object])
259
431
  ], Files);
432
+ export { Files };
260
433
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAQ,MAAM,mBAAmB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEhE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAe,YAAY;CAI1B;AAED,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAG1B,WAAM,QAAQ,GAAd,MAAM,QAAS,SAAQ,YAAY;IAEtC,EAAE,CAAS;IAGX,IAAI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AE,IAAI,EAAE,SAAS,EAAE,CAAC;8BACpB,SAAS;oCAAe;AAXtB,KAAK;IADjB,OAAO,CAAC,OAAO,CAAC;;GACJ,KAAK,CAmMjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peerbit/please-lib",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "author": "dao.xyz",
5
5
  "repository": "https://github.com/@dao-xyz/peerbit-examples",
6
6
  "license": "Apache-2.0",
@@ -23,7 +23,7 @@
23
23
  "LICENSE"
24
24
  ],
25
25
  "engines": {
26
- "node": ">=16.0"
26
+ "node": ">=16.15.1"
27
27
  },
28
28
  "scripts": {
29
29
  "clean": "shx rm -rf lib/*",
@@ -34,13 +34,14 @@
34
34
  "test:integration": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit"
35
35
  },
36
36
  "devDependencies": {
37
- "@peerbit/test-utils": "^1.0.12",
37
+ "@peerbit/test-utils": "^1",
38
38
  "typescript": "^5.1.3"
39
39
  },
40
40
  "dependencies": {
41
- "@peerbit/document": "^1.1.8",
42
- "peerbit": "^1.1.5",
41
+ "@peerbit/document": "^3",
42
+ "@peerbit/trusted-network": "^2",
43
+ "peerbit": "^2",
43
44
  "uuid": "^9.0.0"
44
45
  },
45
- "gitHead": "753e14c5194cf7b62fb10c44fd56f6e355d22121"
46
+ "gitHead": "00c275554aa08e6a70bceaee3e3377936ca0e8d9"
46
47
  }
package/src/index.ts CHANGED
@@ -1,14 +1,38 @@
1
- import { field, variant, vec } from "@dao-xyz/borsh";
1
+ import { field, variant, vec, option } from "@dao-xyz/borsh";
2
2
  import { Program } from "@peerbit/program";
3
- import { Documents, SearchRequest, StringMatch, Role } from "@peerbit/document";
4
- import { sha256Base64Sync, randomBytes } from "@peerbit/crypto";
3
+ import {
4
+ Documents,
5
+ SearchRequest,
6
+ StringMatch,
7
+ Role,
8
+ StringMatchMethod,
9
+ Or,
10
+ } from "@peerbit/document";
11
+ import { SyncFilter } from "@peerbit/shared-log";
12
+ import { PublicSignKey, sha256Base64Sync, randomBytes } from "@peerbit/crypto";
5
13
  import { ProgramClient } from "@peerbit/program";
6
14
  import { concat } from "uint8arrays";
15
+ import { sha256Sync } from "@peerbit/crypto";
16
+ import { TrustedNetwork } from "@peerbit/trusted-network";
17
+ import PQueue from "p-queue";
7
18
 
8
- abstract class AbstractFile {
19
+ export abstract class AbstractFile {
9
20
  abstract id: string;
10
21
  abstract name: string;
11
- abstract getFile(files: Files): Promise<Uint8Array | undefined>;
22
+ abstract size: number;
23
+ abstract parentId?: string;
24
+ abstract getFile<
25
+ OutputType extends "chunks" | "joined" = "joined",
26
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
27
+ >(
28
+ files: Files,
29
+ properties?: {
30
+ as: OutputType;
31
+ timeout?: number;
32
+ progress?: (progress: number) => any;
33
+ }
34
+ ): Promise<Output>;
35
+ abstract delete(files: Files): Promise<void>;
12
36
  }
13
37
 
14
38
  const TINY_FILE_SIZE_LIMIT = 1e3;
@@ -24,23 +48,49 @@ export class TinyFile extends AbstractFile {
24
48
  @field({ type: Uint8Array })
25
49
  file: Uint8Array; // 10 mb imit
26
50
 
27
- constructor(properties: { name: string; file: Uint8Array }) {
51
+ @field({ type: option("string") })
52
+ parentId?: string;
53
+
54
+ get size() {
55
+ return this.file.byteLength;
56
+ }
57
+
58
+ constructor(properties: {
59
+ name: string;
60
+ file: Uint8Array;
61
+ parentId?: string;
62
+ }) {
28
63
  super();
29
64
  this.id = sha256Base64Sync(properties.file);
30
65
  this.name = properties.name;
31
66
  this.file = properties.file;
67
+ this.parentId = properties.parentId;
32
68
  }
33
69
 
34
- async getFile(_files: Files) {
70
+ async getFile<
71
+ OutputType extends "chunks" | "joined" = "joined",
72
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
73
+ >(
74
+ _files: Files,
75
+ properties?: { as: OutputType; progress?: (progress: number) => any }
76
+ ): Promise<Output> {
35
77
  if (sha256Base64Sync(this.file) !== this.id) {
36
78
  throw new Error("Hash does not match the file content");
37
79
  }
38
- return Promise.resolve(this.file);
80
+ properties?.progress?.(1);
81
+ return Promise.resolve(
82
+ properties?.as == "chunks" ? [this.file] : this.file
83
+ ) as Output;
84
+ }
85
+
86
+ async delete(): Promise<void> {
87
+ // Do nothing, since no releated files where created
39
88
  }
40
89
  }
41
90
 
42
91
  const SMALL_FILE_SIZE_LIMIT = 1e6 * 9;
43
92
 
93
+ // TODO Do we really need this if we have TinyFile and LargeFile?
44
94
  @variant(1) // for versioning purposes
45
95
  export class SmallFile extends AbstractFile {
46
96
  @field({ type: "string" })
@@ -52,24 +102,62 @@ export class SmallFile extends AbstractFile {
52
102
  @field({ type: "string" })
53
103
  cid: string; // store file separately in a block store, like IPFS
54
104
 
55
- constructor(properties: { name: string; cid: string }) {
105
+ @field({ type: "u32" })
106
+ size: number;
107
+
108
+ @field({ type: option("string") })
109
+ parentId?: string;
110
+
111
+ constructor(properties: {
112
+ name: string;
113
+ cid: string;
114
+ size: number;
115
+ parentId?: string;
116
+ }) {
56
117
  super();
57
118
  this.id = properties.cid;
58
119
  this.name = properties.name;
59
120
  this.cid = properties.cid;
121
+ this.size = properties.size;
122
+ this.parentId = properties.parentId;
60
123
  }
61
124
 
62
- static async create(node: ProgramClient, name: string, file: Uint8Array) {
125
+ static async create(
126
+ node: ProgramClient,
127
+ name: string,
128
+ file: Uint8Array,
129
+ parentId?: string
130
+ ) {
63
131
  if (file.length > SMALL_FILE_SIZE_LIMIT) {
64
132
  throw new Error("To large file for SmallFile");
65
133
  }
66
134
  const cid = await node.services.blocks.put(file);
67
- return new SmallFile({ name, cid });
135
+ return new SmallFile({ name, cid, size: file.byteLength, parentId });
68
136
  }
69
137
 
70
- async getFile(files: Files) {
138
+ async getFile<
139
+ OutputType extends "chunks" | "joined" = "joined",
140
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
141
+ >(
142
+ files: Files,
143
+ properties?: {
144
+ as: OutputType;
145
+ timeout?: number;
146
+ progress?: (progress: number) => any;
147
+ }
148
+ ): Promise<Output> {
149
+ properties?.progress?.(0);
150
+
71
151
  // Load the file from the block store
72
- return await files.node.services.blocks.get(this.id);
152
+ const block = await files.node.services.blocks.get(this.id, {
153
+ timeout: properties?.timeout,
154
+ });
155
+ properties?.progress?.(1);
156
+ return (properties?.as == "chunks" ? [block] : block) as Output;
157
+ }
158
+
159
+ async delete(files: Files): Promise<void> {
160
+ return await files.node.services.blocks.rm(this.id);
73
161
  }
74
162
  }
75
163
 
@@ -84,68 +172,143 @@ export class LargeFile extends AbstractFile {
84
172
  @field({ type: vec("string") })
85
173
  fileIds: string[];
86
174
 
87
- constructor(properties: { id: string; name: string; fileIds: string[] }) {
175
+ @field({ type: "u32" })
176
+ size: number;
177
+
178
+ constructor(properties: {
179
+ id: string;
180
+ name: string;
181
+ fileIds: string[];
182
+ size: number;
183
+ }) {
88
184
  super();
89
185
  this.id = properties.id;
90
186
  this.name = properties.name;
91
187
  this.fileIds = properties.fileIds;
188
+ this.size = properties.size;
92
189
  }
93
190
 
94
191
  static async create(name: string, file: Uint8Array, files: Files) {
95
192
  const segmetSize = SMALL_FILE_SIZE_LIMIT / 10; // 10% of the small size limit
96
193
  const fileIds: string[] = [];
97
194
  const id = sha256Base64Sync(file);
195
+ const fileSize = file.byteLength;
98
196
  for (let i = 0; i < Math.ceil(file.byteLength / segmetSize); i++) {
99
197
  fileIds.push(
100
- await files.create(
101
- id + "/" + name,
198
+ await files.add(
199
+ name + "/" + i,
102
200
  file.subarray(
103
201
  i * segmetSize,
104
- Math.min((i + 1) * segmetSize, file.length)
105
- )
202
+ Math.min((i + 1) * segmetSize, file.byteLength)
203
+ ),
204
+ id
106
205
  )
107
206
  );
108
207
  }
109
208
 
110
- return new LargeFile({ id, name, fileIds });
209
+ return new LargeFile({ id, name, fileIds, size: fileSize });
111
210
  }
112
211
 
113
- async getFile(files: Files) {
114
- // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
115
- const expectedIds = new Set(this.fileIds);
116
- const chunks: Map<string, Promise<Uint8Array | undefined>> = new Map();
212
+ get parentId() {
213
+ // Large file can never have a parent
214
+ return undefined;
215
+ }
117
216
 
118
- const results = await files.files.index.search(
217
+ async fetchChunks(files: Files) {
218
+ const expectedIds = new Set(this.fileIds);
219
+ const allFiles = await files.files.index.search(
119
220
  new SearchRequest({
120
221
  query: [
121
- new StringMatch({
122
- key: "name",
123
- value: this.id + "/" + this.name,
124
- }),
222
+ new Or(
223
+ [...expectedIds].map(
224
+ (x) => new StringMatch({ key: "id", value: x })
225
+ )
226
+ ),
125
227
  ],
126
- }),
127
- {
128
- local: true,
129
- remote: true,
130
- }
228
+ })
131
229
  );
230
+ return allFiles;
231
+ }
232
+ async delete(files: Files) {
233
+ await Promise.all(
234
+ (await this.fetchChunks(files)).map((x) => x.delete(files))
235
+ );
236
+ }
237
+
238
+ async getFile<
239
+ OutputType extends "chunks" | "joined" = "joined",
240
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
241
+ >(
242
+ files: Files,
243
+ properties?: {
244
+ as: OutputType;
245
+ timeout?: number;
246
+ progress?: (progress: number) => any;
247
+ }
248
+ ): Promise<Output> {
249
+ // Get all sub files (SmallFiles) and concatinate them in the right order (the order of this.fileIds)
250
+
251
+ properties?.progress?.(0);
252
+
253
+ console.log("LISTS CHUNKS!");
254
+ const allChunks = await this.fetchChunks(files);
255
+ console.log("RECEIVED CHUNKS: " + allChunks.length);
256
+ console.log("FETCH CHUNKS");
132
257
 
133
- if (results.length > 0) {
134
- for (const r of results) {
258
+ const fetchQueue = new PQueue({ concurrency: 10 });
259
+ let fetchError: Error | undefined = undefined;
260
+ fetchQueue.on("error", (err) => {
261
+ fetchError = err;
262
+ });
263
+
264
+ const chunks: Map<string, Uint8Array | undefined> = new Map();
265
+ const expectedIds = new Set(this.fileIds);
266
+ if (allChunks.length > 0) {
267
+ let c = 0;
268
+ for (const r of allChunks) {
135
269
  if (chunks.has(r.id)) {
136
270
  // chunk already added;
137
271
  }
138
272
  if (!expectedIds.has(r.id)) {
139
273
  // chunk is not part of this file
140
274
  }
141
-
142
- chunks.set(r.id, r.getFile(files));
275
+ fetchQueue
276
+ .add(async () => {
277
+ let lastError: Error | undefined = undefined;
278
+ for (let i = 0; i < 3; i++) {
279
+ try {
280
+ const chunk = await r.getFile(files, {
281
+ as: "joined",
282
+ timeout: properties?.timeout,
283
+ });
284
+ if (!chunk) {
285
+ throw new Error("Failed to fetch chunk");
286
+ }
287
+ chunks.set(r.id, chunk);
288
+ c++;
289
+ properties?.progress?.(c / allChunks.length);
290
+ return;
291
+ } catch (error: any) {
292
+ // try 3 times
293
+
294
+ lastError = error;
295
+ }
296
+ }
297
+ throw lastError;
298
+ })
299
+ .catch(() => {
300
+ fetchQueue.clear(); // Dont do anything more since we failed to fetch one block
301
+ });
143
302
  }
144
303
  }
304
+ await fetchQueue.onIdle();
145
305
 
146
- if (chunks.size !== expectedIds.size) {
147
- throw new Error("Failed to resolve file");
306
+ if (fetchError || chunks.size !== expectedIds.size) {
307
+ throw new Error(
308
+ `Failed to resolve file. Recieved ${chunks.size}/${expectedIds.size} chunks`
309
+ );
148
310
  }
311
+
149
312
  const chunkContentResolved: Uint8Array[] = await Promise.all(
150
313
  this.fileIds.map(async (x) => {
151
314
  const chunkValue = await chunks.get(x);
@@ -155,50 +318,118 @@ export class LargeFile extends AbstractFile {
155
318
  return chunkValue;
156
319
  })
157
320
  );
158
- return concat(chunkContentResolved);
321
+ console.log("FETCH DONE");
322
+ return (
323
+ properties?.as == "chunks"
324
+ ? chunkContentResolved
325
+ : concat(chunkContentResolved)
326
+ ) as Output;
159
327
  }
160
328
  }
161
329
 
162
- type Args = { role: Role };
330
+ type Args = { role: Role; sync?: SyncFilter | undefined };
163
331
 
164
332
  @variant("files")
165
333
  export class Files extends Program<Args> {
166
334
  @field({ type: Uint8Array })
167
335
  id: Uint8Array;
168
336
 
337
+ @field({ type: "string" })
338
+ name: string;
339
+
340
+ @field({ type: option(TrustedNetwork) })
341
+ trustGraph?: TrustedNetwork;
342
+
169
343
  @field({ type: Documents })
170
344
  files: Documents<AbstractFile>;
171
345
 
172
- constructor(id: Uint8Array = randomBytes(32)) {
346
+ constructor(
347
+ properties: {
348
+ id?: Uint8Array;
349
+ name?: string;
350
+ rootKey?: PublicSignKey;
351
+ } = {}
352
+ ) {
173
353
  super();
174
- this.id = id;
175
- this.files = new Documents({ id: this.id });
354
+ this.id = properties.id || randomBytes(32);
355
+ this.name = properties.name || "";
356
+ this.trustGraph = properties.rootKey
357
+ ? new TrustedNetwork({ id: this.id, rootTrust: properties.rootKey })
358
+ : undefined;
359
+ this.files = new Documents({
360
+ id: sha256Sync(
361
+ concat([
362
+ this.id,
363
+ new TextEncoder().encode(this.name),
364
+ properties.rootKey?.bytes || new Uint8Array(0),
365
+ ])
366
+ ),
367
+ });
176
368
  }
177
369
 
178
- async create(name: string, file: Uint8Array) {
370
+ async add(name: string, file: Uint8Array, parentId?: string) {
179
371
  let toPut: AbstractFile;
180
372
  if (file.byteLength <= TINY_FILE_SIZE_LIMIT) {
181
- toPut = new TinyFile({ name, file });
373
+ toPut = new TinyFile({ name, file, parentId });
182
374
  } else if (
183
375
  file.byteLength > TINY_FILE_SIZE_LIMIT &&
184
376
  file.byteLength <= SMALL_FILE_SIZE_LIMIT
185
377
  ) {
186
- toPut = await SmallFile.create(this.node, name, file);
378
+ toPut = await SmallFile.create(this.node, name, file, parentId);
187
379
  } else {
380
+ if (parentId) {
381
+ throw new Error("Unexpected that a LargeFile to have a parent");
382
+ }
188
383
  toPut = await LargeFile.create(name, file, this);
189
384
  }
190
385
  await this.files.put(toPut);
191
386
  return toPut.id;
192
387
  }
193
388
 
389
+ async removeById(id: string) {
390
+ const file = await this.files.index.get(id);
391
+ if (file) {
392
+ await file.delete(this);
393
+ await this.files.del(file.id);
394
+ }
395
+ }
396
+
397
+ async removeByName(name: string) {
398
+ const files = await this.files.index.search(
399
+ new SearchRequest({
400
+ query: new StringMatch({
401
+ key: ["name"],
402
+ value: name,
403
+ caseInsensitive: false,
404
+ method: StringMatchMethod.exact,
405
+ }),
406
+ })
407
+ );
408
+ for (const file of files) {
409
+ await file.delete(this);
410
+ await this.files.del(file.id);
411
+ }
412
+ }
413
+
414
+ async list() {
415
+ const files = await this.files.index.search(new SearchRequest(), {
416
+ local: true,
417
+ remote: true,
418
+ });
419
+ return files;
420
+ }
194
421
  /**
195
422
  * Get by name
196
423
  * @param id
197
424
  * @returns
198
425
  */
199
- async getById(
200
- id: string
201
- ): Promise<{ id: string; name: string; bytes: Uint8Array } | undefined> {
426
+ async getById<
427
+ OutputType extends "chunks" | "joined" = "joined",
428
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
429
+ >(
430
+ id: string,
431
+ properties?: { as: OutputType }
432
+ ): Promise<{ id: string; name: string; bytes: Output } | undefined> {
202
433
  const results = await this.files.index.search(
203
434
  new SearchRequest({
204
435
  query: [new StringMatch({ key: "id", value: id })],
@@ -212,9 +443,13 @@ export class Files extends Program<Args> {
212
443
  );
213
444
 
214
445
  for (const result of results) {
215
- const file = await result.getFile(this);
446
+ const file = await result.getFile(this, properties);
216
447
  if (file) {
217
- return { id: result.id, name: result.name, bytes: file };
448
+ return {
449
+ id: result.id,
450
+ name: result.name,
451
+ bytes: file as Output,
452
+ };
218
453
  }
219
454
  }
220
455
  }
@@ -224,9 +459,13 @@ export class Files extends Program<Args> {
224
459
  * @param name
225
460
  * @returns
226
461
  */
227
- async getByName(
228
- name: string
229
- ): Promise<{ id: string; name: string; bytes: Uint8Array } | undefined> {
462
+ async getByName<
463
+ OutputType extends "chunks" | "joined" = "joined",
464
+ Output = OutputType extends "chunks" ? Uint8Array[] : Uint8Array
465
+ >(
466
+ name: string,
467
+ properties?: { as: OutputType }
468
+ ): Promise<{ id: string; name: string; bytes: Output } | undefined> {
230
469
  const results = await this.files.index.search(
231
470
  new SearchRequest({
232
471
  query: [new StringMatch({ key: "name", value: name })],
@@ -240,25 +479,50 @@ export class Files extends Program<Args> {
240
479
  );
241
480
 
242
481
  for (const result of results) {
243
- const file = await result.getFile(this);
482
+ const file = await result.getFile(this, properties);
244
483
  if (file) {
245
- return { id: result.id, name: result.name, bytes: file };
484
+ return {
485
+ id: result.id,
486
+ name: result.name,
487
+ bytes: file as Output,
488
+ };
246
489
  }
247
490
  }
248
491
  }
249
492
 
250
493
  // Setup lifecycle, will be invoked on 'open'
251
494
  async open(args?: Args): Promise<void> {
495
+ await this.trustGraph?.open({
496
+ role: args?.role,
497
+ });
498
+
252
499
  await this.files.open({
253
500
  type: AbstractFile,
254
- canAppend: async (entry) => {
255
- await entry.verifySignatures();
256
- return true; // no verification as of now
501
+ // TODO add ACL
502
+ role: args?.role,
503
+ sync: args?.sync,
504
+ replicas: { min: 3 },
505
+ canPerform: async (operation, context) => {
506
+ if (!this.trustGraph) {
507
+ return true;
508
+ }
509
+ for (const key of await context.entry.getPublicKeys()) {
510
+ if (await this.trustGraph.isTrusted(key)) {
511
+ return true;
512
+ }
513
+ }
514
+ return false;
257
515
  },
258
- canRead: async (identity) => {
259
- return true; // Anyone can query
516
+ index: {
517
+ fields: (doc) => {
518
+ return {
519
+ id: doc.id,
520
+ name: doc.name,
521
+ size: doc.size,
522
+ parentId: doc.parentId,
523
+ };
524
+ },
260
525
  },
261
- role: args?.role,
262
526
  });
263
527
  }
264
528
  }