@peerbit/log 3.0.34 → 4.0.0-55cebfe
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/benchmark/append.d.ts +2 -0
- package/dist/benchmark/append.d.ts.map +1 -0
- package/dist/benchmark/append.js +40 -0
- package/dist/benchmark/append.js.map +1 -0
- package/dist/benchmark/memory/index.d.ts +2 -0
- package/dist/benchmark/memory/index.d.ts.map +1 -0
- package/dist/benchmark/memory/index.js +122 -0
- package/dist/benchmark/memory/index.js.map +1 -0
- package/dist/benchmark/memory/insert.d.ts +2 -0
- package/dist/benchmark/memory/insert.d.ts.map +1 -0
- package/dist/benchmark/memory/insert.js +59 -0
- package/dist/benchmark/memory/insert.js.map +1 -0
- package/dist/benchmark/memory/utils.d.ts +13 -0
- package/dist/benchmark/memory/utils.d.ts.map +1 -0
- package/dist/benchmark/memory/utils.js +2 -0
- package/dist/benchmark/memory/utils.js.map +1 -0
- package/dist/benchmark/payload.d.ts +2 -0
- package/dist/benchmark/payload.d.ts.map +1 -0
- package/{lib/esm/__benchmark__/index.js → dist/benchmark/payload.js} +20 -22
- package/dist/benchmark/payload.js.map +1 -0
- package/dist/src/change.d.ts +6 -0
- package/dist/src/change.d.ts.map +1 -0
- package/{lib/esm → dist/src}/clock.d.ts +3 -26
- package/dist/src/clock.d.ts.map +1 -0
- package/{lib/esm → dist/src}/clock.js +30 -39
- package/dist/src/clock.js.map +1 -0
- package/{lib/esm → dist/src}/difference.d.ts +1 -0
- package/dist/src/difference.d.ts.map +1 -0
- package/{lib/esm → dist/src}/encoding.d.ts +2 -1
- package/dist/src/encoding.d.ts.map +1 -0
- package/{lib/esm → dist/src}/encoding.js +2 -2
- package/{lib/esm → dist/src}/encoding.js.map +1 -1
- package/dist/src/entry-index.d.ts +78 -0
- package/dist/src/entry-index.d.ts.map +1 -0
- package/dist/src/entry-index.js +316 -0
- package/dist/src/entry-index.js.map +1 -0
- package/{lib/esm → dist/src}/entry-with-refs.d.ts +2 -1
- package/dist/src/entry-with-refs.d.ts.map +1 -0
- package/{lib/esm → dist/src}/entry.d.ts +22 -18
- package/dist/src/entry.d.ts.map +1 -0
- package/{lib/esm → dist/src}/entry.js +69 -42
- package/dist/src/entry.js.map +1 -0
- package/{lib/esm → dist/src}/find-uniques.d.ts +1 -0
- package/dist/src/find-uniques.d.ts.map +1 -0
- package/{lib/esm → dist/src}/heads-cache.d.ts +4 -3
- package/dist/src/heads-cache.d.ts.map +1 -0
- package/{lib/esm → dist/src}/heads-cache.js +9 -10
- package/dist/src/heads-cache.js.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/{lib/esm → dist/src}/log-errors.d.ts +1 -0
- package/dist/src/log-errors.d.ts.map +1 -0
- package/dist/src/log-sorting.d.ts +35 -0
- package/dist/src/log-sorting.d.ts.map +1 -0
- package/dist/src/log-sorting.js +105 -0
- package/dist/src/log-sorting.js.map +1 -0
- package/{lib/esm → dist/src}/log.d.ts +78 -56
- package/dist/src/log.d.ts.map +1 -0
- package/{lib/esm → dist/src}/log.js +355 -465
- package/dist/src/log.js.map +1 -0
- package/{lib/esm → dist/src}/logger.d.ts +1 -0
- package/dist/src/logger.d.ts.map +1 -0
- package/{lib/esm → dist/src}/logger.js.map +1 -1
- package/{lib/esm → dist/src}/snapshot.d.ts +5 -4
- package/dist/src/snapshot.d.ts.map +1 -0
- package/{lib/esm → dist/src}/snapshot.js +10 -10
- package/dist/src/snapshot.js.map +1 -0
- package/{lib/esm → dist/src}/trim.d.ts +11 -9
- package/dist/src/trim.d.ts.map +1 -0
- package/{lib/esm → dist/src}/trim.js +46 -35
- package/dist/src/trim.js.map +1 -0
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +12 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +104 -78
- package/src/change.ts +3 -2
- package/src/clock.ts +22 -22
- package/src/encoding.ts +4 -4
- package/src/entry-index.ts +451 -52
- package/src/entry-with-refs.ts +1 -1
- package/src/entry.ts +95 -81
- package/src/heads-cache.ts +33 -29
- package/src/log-sorting.ts +116 -94
- package/src/log.ts +482 -571
- package/src/logger.ts +1 -0
- package/src/snapshot.ts +15 -17
- package/src/trim.ts +81 -50
- package/src/utils.ts +10 -0
- package/lib/esm/__benchmark__/index.d.ts +0 -1
- package/lib/esm/__benchmark__/index.js.map +0 -1
- package/lib/esm/change.d.ts +0 -5
- package/lib/esm/clock.js.map +0 -1
- package/lib/esm/entry-index.d.ts +0 -24
- package/lib/esm/entry-index.js +0 -74
- package/lib/esm/entry-index.js.map +0 -1
- package/lib/esm/entry.js.map +0 -1
- package/lib/esm/heads-cache.js.map +0 -1
- package/lib/esm/heads.d.ts +0 -69
- package/lib/esm/heads.js +0 -157
- package/lib/esm/heads.js.map +0 -1
- package/lib/esm/log-sorting.d.ts +0 -44
- package/lib/esm/log-sorting.js +0 -86
- package/lib/esm/log-sorting.js.map +0 -1
- package/lib/esm/log.js.map +0 -1
- package/lib/esm/snapshot.js.map +0 -1
- package/lib/esm/trim.js.map +0 -1
- package/lib/esm/types.d.ts +0 -6
- package/lib/esm/types.js +0 -23
- package/lib/esm/types.js.map +0 -1
- package/lib/esm/utils.d.ts +0 -2
- package/lib/esm/utils.js +0 -3
- package/lib/esm/utils.js.map +0 -1
- package/lib/esm/values.d.ts +0 -26
- package/lib/esm/values.js +0 -131
- package/lib/esm/values.js.map +0 -1
- package/src/__benchmark__/index.ts +0 -130
- package/src/heads.ts +0 -233
- package/src/types.ts +0 -12
- package/src/values.ts +0 -174
- /package/{lib/esm → dist/src}/change.js +0 -0
- /package/{lib/esm → dist/src}/change.js.map +0 -0
- /package/{lib/esm → dist/src}/difference.js +0 -0
- /package/{lib/esm → dist/src}/difference.js.map +0 -0
- /package/{lib/esm → dist/src}/entry-with-refs.js +0 -0
- /package/{lib/esm → dist/src}/entry-with-refs.js.map +0 -0
- /package/{lib/esm → dist/src}/find-uniques.js +0 -0
- /package/{lib/esm → dist/src}/find-uniques.js.map +0 -0
- /package/{lib/esm → dist/src}/index.js +0 -0
- /package/{lib/esm → dist/src}/index.js.map +0 -0
- /package/{lib/esm → dist/src}/log-errors.js +0 -0
- /package/{lib/esm → dist/src}/log-errors.js.map +0 -0
- /package/{lib/esm → dist/src}/logger.js +0 -0
package/src/entry-with-refs.ts
CHANGED
package/src/entry.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
import { HLC, LamportClock as Clock, Timestamp } from "./clock.js";
|
|
2
1
|
import {
|
|
3
|
-
variant,
|
|
4
|
-
field,
|
|
5
|
-
serialize,
|
|
6
2
|
deserialize,
|
|
3
|
+
field,
|
|
4
|
+
fixedArray,
|
|
7
5
|
option,
|
|
6
|
+
serialize,
|
|
7
|
+
variant,
|
|
8
8
|
vec,
|
|
9
|
-
fixedArray,
|
|
10
|
-
BinaryWriter,
|
|
11
|
-
serializer
|
|
12
9
|
} from "@dao-xyz/borsh";
|
|
13
|
-
|
|
10
|
+
import { type Blocks } from "@peerbit/blocks-interface";
|
|
14
11
|
import {
|
|
12
|
+
AccessError,
|
|
15
13
|
DecryptedThing,
|
|
14
|
+
Ed25519PublicKey,
|
|
15
|
+
type Identity,
|
|
16
16
|
MaybeEncrypted,
|
|
17
17
|
PublicSignKey,
|
|
18
|
-
X25519PublicKey,
|
|
19
18
|
SignatureWithKey,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
sha256Base64,
|
|
19
|
+
X25519Keypair,
|
|
20
|
+
X25519PublicKey,
|
|
23
21
|
randomBytes,
|
|
24
|
-
|
|
25
|
-
X25519Keypair
|
|
22
|
+
sha256Base64,
|
|
26
23
|
} from "@peerbit/crypto";
|
|
27
24
|
import { verify } from "@peerbit/crypto";
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
25
|
+
import { id } from "@peerbit/indexer-interface";
|
|
26
|
+
import { type Keychain } from "@peerbit/keychain";
|
|
27
|
+
import { compare } from "uint8arrays";
|
|
28
|
+
import { LamportClock as Clock, HLC, Timestamp } from "./clock.js";
|
|
29
|
+
import { type Encoding, NO_ENCODING } from "./encoding.js";
|
|
30
|
+
import type { SortableEntry } from "./log-sorting.js";
|
|
30
31
|
import { logger } from "./logger.js";
|
|
31
|
-
import {
|
|
32
|
-
import { Keychain } from "@peerbit/keychain";
|
|
32
|
+
import { equals } from "./utils.js";
|
|
33
33
|
|
|
34
34
|
export type MaybeEncryptionPublicKey =
|
|
35
35
|
| X25519PublicKey
|
|
@@ -120,7 +120,7 @@ export class Payload<T> {
|
|
|
120
120
|
return this._value;
|
|
121
121
|
}
|
|
122
122
|
getValue(encoding: Encoding<T> = this.encoding || NO_ENCODING): T {
|
|
123
|
-
if (this._value
|
|
123
|
+
if (this._value !== undefined) {
|
|
124
124
|
return this._value;
|
|
125
125
|
}
|
|
126
126
|
return encoding.decoder(this.data);
|
|
@@ -135,10 +135,10 @@ export interface EntryEncryptionTemplate<A, B, C> {
|
|
|
135
135
|
|
|
136
136
|
export enum EntryType {
|
|
137
137
|
APPEND = 0, // Add more data
|
|
138
|
-
CUT = 1 // Delete or Create tombstone ... delete all nexts, i
|
|
138
|
+
CUT = 1, // Delete or Create tombstone ... delete all nexts, i
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
@variant(0)
|
|
141
|
+
/* @variant(0) */
|
|
142
142
|
export class Meta {
|
|
143
143
|
@field({ type: Clock })
|
|
144
144
|
clock: Clock;
|
|
@@ -155,27 +155,25 @@ export class Meta {
|
|
|
155
155
|
@field({ type: option(Uint8Array) })
|
|
156
156
|
data?: Uint8Array; // Optional metadata
|
|
157
157
|
|
|
158
|
-
constructor(properties
|
|
158
|
+
constructor(properties: {
|
|
159
159
|
gid: string;
|
|
160
160
|
clock: Clock;
|
|
161
161
|
type: EntryType;
|
|
162
162
|
data?: Uint8Array;
|
|
163
163
|
next: string[];
|
|
164
164
|
}) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
this.next = properties.next;
|
|
171
|
-
}
|
|
165
|
+
this.gid = properties.gid;
|
|
166
|
+
this.clock = properties.clock;
|
|
167
|
+
this.type = properties.type;
|
|
168
|
+
this.data = properties.data;
|
|
169
|
+
this.next = properties.next;
|
|
172
170
|
}
|
|
173
171
|
}
|
|
174
172
|
|
|
175
173
|
@variant(0)
|
|
176
174
|
export class Signatures {
|
|
177
175
|
@field({ type: vec(MaybeEncrypted) })
|
|
178
|
-
signatures
|
|
176
|
+
signatures!: MaybeEncrypted<SignatureWithKey>[];
|
|
179
177
|
|
|
180
178
|
constructor(properties?: { signatures: MaybeEncrypted<SignatureWithKey>[] }) {
|
|
181
179
|
if (properties) {
|
|
@@ -199,7 +197,7 @@ export class Signatures {
|
|
|
199
197
|
const maybeEncrypt = <Q>(
|
|
200
198
|
thing: Q,
|
|
201
199
|
keypair?: X25519Keypair,
|
|
202
|
-
receiver?: MaybeEncryptionPublicKey
|
|
200
|
+
receiver?: MaybeEncryptionPublicKey,
|
|
203
201
|
): Promise<MaybeEncrypted<Q>> | MaybeEncrypted<Q> => {
|
|
204
202
|
const receivers = receiver
|
|
205
203
|
? Array.isArray(receiver)
|
|
@@ -212,26 +210,41 @@ const maybeEncrypt = <Q>(
|
|
|
212
210
|
}
|
|
213
211
|
return new DecryptedThing<Q>({
|
|
214
212
|
data: serialize(thing),
|
|
215
|
-
value: thing
|
|
213
|
+
value: thing,
|
|
216
214
|
}).encrypt(keypair, receivers);
|
|
217
215
|
}
|
|
218
216
|
return new DecryptedThing<Q>({
|
|
219
217
|
data: serialize(thing),
|
|
220
|
-
value: thing
|
|
218
|
+
value: thing,
|
|
221
219
|
});
|
|
222
220
|
};
|
|
223
221
|
|
|
224
|
-
export
|
|
222
|
+
export class ShallowEntry {
|
|
223
|
+
@id({ type: "string" })
|
|
225
224
|
hash: string;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
225
|
+
|
|
226
|
+
@field({ type: Meta })
|
|
227
|
+
meta: Meta;
|
|
228
|
+
|
|
229
|
+
@field({ type: "u32" })
|
|
230
|
+
payloadSize: number;
|
|
231
|
+
|
|
232
|
+
@field({ type: "bool" })
|
|
233
|
+
head: boolean;
|
|
234
|
+
|
|
235
|
+
constructor(properties: {
|
|
236
|
+
hash: string;
|
|
237
|
+
meta: Meta;
|
|
238
|
+
payloadSize: number;
|
|
239
|
+
head: boolean;
|
|
240
|
+
}) {
|
|
241
|
+
this.hash = properties.hash;
|
|
242
|
+
this.meta = properties.meta;
|
|
243
|
+
this.payloadSize = properties.payloadSize;
|
|
244
|
+
this.head = properties.head;
|
|
245
|
+
}
|
|
234
246
|
}
|
|
247
|
+
export type ShallowOrFullEntry<T> = ShallowEntry | Entry<T>;
|
|
235
248
|
|
|
236
249
|
@variant(0)
|
|
237
250
|
export class Entry<T>
|
|
@@ -250,7 +263,7 @@ export class Entry<T>
|
|
|
250
263
|
_signatures?: Signatures;
|
|
251
264
|
|
|
252
265
|
@field({ type: option("string") }) // we do option because we serialize and store this in a block without the hash, to receive the hash, which we later set
|
|
253
|
-
hash
|
|
266
|
+
hash!: string; // "zd...Foo", we'll set the hash after persisting the entry
|
|
254
267
|
|
|
255
268
|
createdLocally?: boolean;
|
|
256
269
|
|
|
@@ -278,8 +291,8 @@ export class Entry<T>
|
|
|
278
291
|
keychain?: Keychain;
|
|
279
292
|
encoding: Encoding<T>;
|
|
280
293
|
}
|
|
281
|
-
| Entry<T
|
|
282
|
-
):
|
|
294
|
+
| Entry<T>,
|
|
295
|
+
): this {
|
|
283
296
|
if (props instanceof Entry) {
|
|
284
297
|
this._keychain = props._keychain;
|
|
285
298
|
this._encoding = props._encoding;
|
|
@@ -355,7 +368,7 @@ export class Entry<T>
|
|
|
355
368
|
return (await this.getMeta()).next;
|
|
356
369
|
}
|
|
357
370
|
|
|
358
|
-
private _size
|
|
371
|
+
private _size!: number;
|
|
359
372
|
|
|
360
373
|
set size(number: number) {
|
|
361
374
|
this._size = number;
|
|
@@ -364,7 +377,7 @@ export class Entry<T>
|
|
|
364
377
|
get size(): number {
|
|
365
378
|
if (this._size == null) {
|
|
366
379
|
throw new Error(
|
|
367
|
-
"Size not set. Size is set when entry is, created, loaded or joined"
|
|
380
|
+
"Size not set. Size is set when entry is, created, loaded or joined",
|
|
368
381
|
);
|
|
369
382
|
}
|
|
370
383
|
return this._size;
|
|
@@ -376,6 +389,7 @@ export class Entry<T>
|
|
|
376
389
|
get signatures(): SignatureWithKey[] {
|
|
377
390
|
const signatures = this._signatures!.signatures.filter((x) => {
|
|
378
391
|
try {
|
|
392
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
379
393
|
x.decrypted;
|
|
380
394
|
return true;
|
|
381
395
|
} catch (error) {
|
|
@@ -394,7 +408,7 @@ export class Entry<T>
|
|
|
394
408
|
*/
|
|
395
409
|
async getSignatures(): Promise<SignatureWithKey[]> {
|
|
396
410
|
const results = await Promise.allSettled(
|
|
397
|
-
this._signatures!.signatures.map((x) => x.decrypt(this._keychain))
|
|
411
|
+
this._signatures!.signatures.map((x) => x.decrypt(this._keychain)),
|
|
398
412
|
);
|
|
399
413
|
|
|
400
414
|
if (logger.level === "debug" || logger.level === "trace") {
|
|
@@ -435,7 +449,7 @@ export class Entry<T>
|
|
|
435
449
|
payload: entry._payload,
|
|
436
450
|
reserved: entry._reserved,
|
|
437
451
|
signatures: undefined,
|
|
438
|
-
hash: undefined
|
|
452
|
+
hash: undefined,
|
|
439
453
|
});
|
|
440
454
|
return trimmed;
|
|
441
455
|
}
|
|
@@ -480,14 +494,14 @@ export class Entry<T>
|
|
|
480
494
|
type?: EntryType;
|
|
481
495
|
gidSeed?: Uint8Array;
|
|
482
496
|
data?: Uint8Array;
|
|
483
|
-
next?:
|
|
497
|
+
next?: SortableEntry[];
|
|
484
498
|
};
|
|
485
499
|
encoding?: Encoding<T>;
|
|
486
500
|
canAppend?: CanAppend<T>;
|
|
487
501
|
encryption?: EntryEncryption;
|
|
488
502
|
identity: Identity;
|
|
489
503
|
signers?: ((
|
|
490
|
-
data: Uint8Array
|
|
504
|
+
data: Uint8Array,
|
|
491
505
|
) => Promise<SignatureWithKey> | SignatureWithKey)[];
|
|
492
506
|
}): Promise<Entry<T>> {
|
|
493
507
|
if (!properties.encoding || !properties?.meta?.next) {
|
|
@@ -495,9 +509,9 @@ export class Entry<T>
|
|
|
495
509
|
...properties,
|
|
496
510
|
meta: {
|
|
497
511
|
...properties?.meta,
|
|
498
|
-
next: properties.meta?.next ? properties.meta?.next : []
|
|
512
|
+
next: properties.meta?.next ? properties.meta?.next : [],
|
|
499
513
|
},
|
|
500
|
-
encoding: properties.encoding ? properties.encoding : NO_ENCODING
|
|
514
|
+
encoding: properties.encoding ? properties.encoding : NO_ENCODING,
|
|
501
515
|
};
|
|
502
516
|
}
|
|
503
517
|
|
|
@@ -515,7 +529,7 @@ export class Entry<T>
|
|
|
515
529
|
const payloadToSave = new Payload<T>({
|
|
516
530
|
data: properties.encoding.encoder(properties.data),
|
|
517
531
|
value: properties.data,
|
|
518
|
-
encoding: properties.encoding
|
|
532
|
+
encoding: properties.encoding,
|
|
519
533
|
});
|
|
520
534
|
|
|
521
535
|
let clock: Clock | undefined = properties.meta?.clock;
|
|
@@ -530,12 +544,12 @@ export class Entry<T>
|
|
|
530
544
|
properties.encryption?.receiver.meta
|
|
531
545
|
) {
|
|
532
546
|
throw new Error(
|
|
533
|
-
"Signature is to be encrypted yet the clock is not, which contains the publicKey as id. Either provide a custom Clock value that is not sensitive or set the receiver (encryption target) for the clock"
|
|
547
|
+
"Signature is to be encrypted yet the clock is not, which contains the publicKey as id. Either provide a custom Clock value that is not sensitive or set the receiver (encryption target) for the clock",
|
|
534
548
|
);
|
|
535
549
|
}
|
|
536
550
|
clock = new Clock({
|
|
537
551
|
id: properties.identity.publicKey.bytes,
|
|
538
|
-
timestamp: hlc.now()
|
|
552
|
+
timestamp: hlc.now(),
|
|
539
553
|
});
|
|
540
554
|
} else {
|
|
541
555
|
const cv = clock;
|
|
@@ -546,7 +560,7 @@ export class Entry<T>
|
|
|
546
560
|
"Expecting next(s) to happen before entry, got: " +
|
|
547
561
|
n.meta.clock.timestamp +
|
|
548
562
|
" > " +
|
|
549
|
-
cv.timestamp
|
|
563
|
+
cv.timestamp,
|
|
550
564
|
);
|
|
551
565
|
}
|
|
552
566
|
}
|
|
@@ -559,7 +573,7 @@ export class Entry<T>
|
|
|
559
573
|
// take min gid as our gid
|
|
560
574
|
if (properties.meta?.gid) {
|
|
561
575
|
throw new Error(
|
|
562
|
-
"Expecting '.meta.gid' property to be undefined if '.meta.next' is provided"
|
|
576
|
+
"Expecting '.meta.gid' property to be undefined if '.meta.next' is provided",
|
|
563
577
|
);
|
|
564
578
|
}
|
|
565
579
|
for (const n of nexts) {
|
|
@@ -588,16 +602,16 @@ export class Entry<T>
|
|
|
588
602
|
gid: gid!,
|
|
589
603
|
type: properties.meta?.type ?? EntryType.APPEND,
|
|
590
604
|
data: properties.meta?.data,
|
|
591
|
-
next: nextHashes
|
|
605
|
+
next: nextHashes,
|
|
592
606
|
}),
|
|
593
607
|
properties.encryption?.keypair,
|
|
594
|
-
properties.encryption?.receiver.meta
|
|
608
|
+
properties.encryption?.receiver.meta,
|
|
595
609
|
);
|
|
596
610
|
|
|
597
611
|
const payload = await maybeEncrypt(
|
|
598
612
|
payloadToSave,
|
|
599
613
|
properties.encryption?.keypair,
|
|
600
|
-
properties.encryption?.receiver.payload
|
|
614
|
+
properties.encryption?.receiver.payload,
|
|
601
615
|
);
|
|
602
616
|
|
|
603
617
|
// Sign id, encrypted payload, clock, nexts, refs
|
|
@@ -605,40 +619,40 @@ export class Entry<T>
|
|
|
605
619
|
meta: metadataEncrypted,
|
|
606
620
|
payload,
|
|
607
621
|
signatures: undefined,
|
|
608
|
-
createdLocally: true
|
|
622
|
+
createdLocally: true,
|
|
609
623
|
});
|
|
610
624
|
|
|
611
625
|
const signers = properties.signers || [
|
|
612
|
-
properties.identity.sign.bind(properties.identity)
|
|
626
|
+
properties.identity.sign.bind(properties.identity),
|
|
613
627
|
];
|
|
614
628
|
const signable = entry.toSignable();
|
|
615
629
|
const signableBytes = serialize(signable);
|
|
616
630
|
let signatures = await Promise.all(
|
|
617
|
-
signers.map((signer) => signer(signableBytes))
|
|
631
|
+
signers.map((signer) => signer(signableBytes)),
|
|
618
632
|
);
|
|
619
633
|
signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
|
|
620
634
|
|
|
621
635
|
const encryptedSignatures: MaybeEncrypted<SignatureWithKey>[] = [];
|
|
622
636
|
const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(
|
|
623
|
-
properties.encryption?.receiver?.signatures
|
|
637
|
+
properties.encryption?.receiver?.signatures,
|
|
624
638
|
);
|
|
625
639
|
|
|
626
640
|
for (const signature of signatures) {
|
|
627
641
|
const encryptionRecievers = encryptAllSignaturesWithSameKey
|
|
628
642
|
? properties.encryption?.receiver?.signatures
|
|
629
|
-
: properties.encryption?.receiver?.signatures?.[
|
|
643
|
+
: (properties.encryption?.receiver?.signatures as any)?.[
|
|
630
644
|
signature.publicKey.hashcode()
|
|
631
|
-
];
|
|
645
|
+
]; // TODO types
|
|
632
646
|
const signatureEncrypted = await maybeEncrypt(
|
|
633
647
|
signature,
|
|
634
648
|
properties.encryption?.keypair,
|
|
635
|
-
encryptionRecievers
|
|
649
|
+
encryptionRecievers,
|
|
636
650
|
);
|
|
637
651
|
encryptedSignatures.push(signatureEncrypted);
|
|
638
652
|
}
|
|
639
653
|
|
|
640
654
|
entry._signatures = new Signatures({
|
|
641
|
-
signatures: encryptedSignatures
|
|
655
|
+
signatures: encryptedSignatures,
|
|
642
656
|
});
|
|
643
657
|
|
|
644
658
|
if (properties.canAppend && !(await properties.canAppend(entry))) {
|
|
@@ -657,18 +671,19 @@ export class Entry<T>
|
|
|
657
671
|
return this._payload.byteLength;
|
|
658
672
|
}
|
|
659
673
|
|
|
660
|
-
toShallow(): ShallowEntry {
|
|
661
|
-
return {
|
|
674
|
+
toShallow(isHead: boolean): ShallowEntry {
|
|
675
|
+
return new ShallowEntry({
|
|
662
676
|
hash: this.hash,
|
|
663
|
-
|
|
664
|
-
|
|
677
|
+
payloadSize: this._payload.byteLength,
|
|
678
|
+
head: isHead,
|
|
679
|
+
meta: new Meta({
|
|
665
680
|
gid: this.meta.gid,
|
|
666
681
|
data: this.meta.data,
|
|
667
682
|
clock: this.meta.clock,
|
|
668
683
|
next: this.meta.next,
|
|
669
|
-
type: this.meta.type
|
|
670
|
-
}
|
|
671
|
-
};
|
|
684
|
+
type: this.meta.type,
|
|
685
|
+
}),
|
|
686
|
+
});
|
|
672
687
|
}
|
|
673
688
|
|
|
674
689
|
/**
|
|
@@ -685,8 +700,7 @@ export class Entry<T>
|
|
|
685
700
|
|
|
686
701
|
const bytes = serialize(entry);
|
|
687
702
|
entry.size = bytes.length;
|
|
688
|
-
|
|
689
|
-
return result;
|
|
703
|
+
return store.put(bytes);
|
|
690
704
|
}
|
|
691
705
|
|
|
692
706
|
/**
|
|
@@ -699,7 +713,7 @@ export class Entry<T>
|
|
|
699
713
|
static async fromMultihash<T>(
|
|
700
714
|
store: Blocks,
|
|
701
715
|
hash: string,
|
|
702
|
-
options?: { timeout?: number; replicate?: boolean }
|
|
716
|
+
options?: { timeout?: number; replicate?: boolean },
|
|
703
717
|
) {
|
|
704
718
|
if (!hash) throw new Error(`Invalid hash: ${hash}`);
|
|
705
719
|
const bytes = await store.get(hash, options);
|
|
@@ -743,7 +757,7 @@ export class Entry<T>
|
|
|
743
757
|
* @returns {boolean}
|
|
744
758
|
*/
|
|
745
759
|
static isDirectParent<T>(entry1: Entry<T>, entry2: Entry<T>) {
|
|
746
|
-
return entry2.next.
|
|
760
|
+
return entry2.next.includes(entry1.hash as any); // TODO fix types
|
|
747
761
|
}
|
|
748
762
|
|
|
749
763
|
/**
|
|
@@ -755,7 +769,7 @@ export class Entry<T>
|
|
|
755
769
|
*/
|
|
756
770
|
static findDirectChildren<T>(
|
|
757
771
|
entry: Entry<T>,
|
|
758
|
-
values: Entry<T>[]
|
|
772
|
+
values: Entry<T>[],
|
|
759
773
|
): Entry<T>[] {
|
|
760
774
|
let stack: Entry<T>[] = [];
|
|
761
775
|
let parent = values.find((e) => Entry.isDirectParent(entry, e));
|
package/src/heads-cache.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deserialize,
|
|
3
|
+
field,
|
|
4
|
+
option,
|
|
5
|
+
serialize,
|
|
6
|
+
variant,
|
|
7
|
+
vec,
|
|
8
|
+
} from "@dao-xyz/borsh";
|
|
9
|
+
import { type AnyStore } from "@peerbit/any-store";
|
|
10
|
+
import { logger as loggerFn } from "@peerbit/logger";
|
|
1
11
|
import PQueue from "p-queue";
|
|
12
|
+
import path from "path-browserify";
|
|
2
13
|
import { v4 as uuid } from "uuid";
|
|
3
|
-
import { Entry } from "./entry";
|
|
4
|
-
import { AnyStore } from "@peerbit/any-store";
|
|
5
|
-
import { variant, option, field, vec } from "@dao-xyz/borsh";
|
|
6
|
-
import { serialize, deserialize } from "@dao-xyz/borsh";
|
|
7
|
-
import { logger as loggerFn } from "@peerbit/logger";
|
|
14
|
+
import { Entry } from "./entry.js";
|
|
8
15
|
|
|
9
|
-
import path from "path-browserify";
|
|
10
16
|
export const logger = loggerFn({ module: "heads-cache" });
|
|
11
17
|
export class CachedValue {}
|
|
12
18
|
/* export type AppendOptions<T> = {
|
|
@@ -32,10 +38,8 @@ export class UnsfinishedReplication {
|
|
|
32
38
|
@field({ type: vec("string") })
|
|
33
39
|
hashes: string[];
|
|
34
40
|
|
|
35
|
-
constructor(opts
|
|
36
|
-
|
|
37
|
-
this.hashes = opts.hashes;
|
|
38
|
-
}
|
|
41
|
+
constructor(opts: { hashes: string[] }) {
|
|
42
|
+
this.hashes = opts.hashes;
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|
|
@@ -62,23 +66,23 @@ const updateHashes = async (
|
|
|
62
66
|
headsPath: string,
|
|
63
67
|
lastCid: string | undefined,
|
|
64
68
|
lastCounter: bigint,
|
|
65
|
-
hashes: string[]
|
|
69
|
+
hashes: string[],
|
|
66
70
|
): Promise<{ counter: bigint; newPath: string }> => {
|
|
67
71
|
const newHeadsPath = path.join(
|
|
68
72
|
headsPath,
|
|
69
73
|
String(headCache.headsPathCounter),
|
|
70
|
-
uuid()
|
|
74
|
+
uuid(),
|
|
71
75
|
);
|
|
72
76
|
const counter = lastCounter + BigInt(hashes.length);
|
|
73
77
|
await Promise.all([
|
|
74
78
|
headCache.cache?.put(
|
|
75
79
|
headsPath,
|
|
76
|
-
serialize(new CachePath(newHeadsPath.toString()))
|
|
80
|
+
serialize(new CachePath(newHeadsPath.toString())),
|
|
77
81
|
),
|
|
78
82
|
headCache.cache?.put(
|
|
79
83
|
newHeadsPath,
|
|
80
|
-
serialize(new HeadsCacheToSerialize(hashes, counter, lastCid))
|
|
81
|
-
)
|
|
84
|
+
serialize(new HeadsCacheToSerialize(hashes, counter, lastCid)),
|
|
85
|
+
),
|
|
82
86
|
]);
|
|
83
87
|
return { counter, newPath: newHeadsPath };
|
|
84
88
|
};
|
|
@@ -90,9 +94,9 @@ interface HeadsIndex {
|
|
|
90
94
|
}
|
|
91
95
|
export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
92
96
|
// An access controller that is note part of the store manifest, usefull for circular store -> access controller -> store structures
|
|
93
|
-
headsPath
|
|
94
|
-
removedHeadsPath
|
|
95
|
-
initialized: boolean;
|
|
97
|
+
headsPath!: string;
|
|
98
|
+
removedHeadsPath!: string;
|
|
99
|
+
initialized: boolean = false;
|
|
96
100
|
|
|
97
101
|
private _headsPathCounter = 0;
|
|
98
102
|
|
|
@@ -106,7 +110,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
106
110
|
private _cacheWriteQueue?: PQueue<any, any>;
|
|
107
111
|
|
|
108
112
|
private _loaded = false;
|
|
109
|
-
private _index
|
|
113
|
+
private _index!: HeadsIndex;
|
|
110
114
|
|
|
111
115
|
constructor(index: HeadsIndex) {
|
|
112
116
|
this._index = index;
|
|
@@ -155,7 +159,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
155
159
|
added?: (Entry<T> | string)[];
|
|
156
160
|
removed?: (Entry<T> | string)[];
|
|
157
161
|
},
|
|
158
|
-
reset?: boolean
|
|
162
|
+
reset?: boolean,
|
|
159
163
|
) {
|
|
160
164
|
if (typeof reset !== "boolean" && change.added) {
|
|
161
165
|
// Only reset all heads if loaded once, since we don't want too loose track of unloaded heads
|
|
@@ -192,7 +196,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
192
196
|
this.headsPath,
|
|
193
197
|
this._lastHeadsPath,
|
|
194
198
|
this._lastHeadsCount,
|
|
195
|
-
change.added.map((x) => (typeof x === "string" ? x : x.hash))
|
|
199
|
+
change.added.map((x) => (typeof x === "string" ? x : x.hash)),
|
|
196
200
|
);
|
|
197
201
|
this._lastHeadsPath = update.newPath;
|
|
198
202
|
this._lastHeadsCount = update.counter;
|
|
@@ -206,7 +210,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
206
210
|
this.removedHeadsPath,
|
|
207
211
|
this._lastRemovedHeadsPath,
|
|
208
212
|
this._lastRemovedHeadsCount,
|
|
209
|
-
change.removed.map((x) => (typeof x === "string" ? x : x.hash))
|
|
213
|
+
change.removed.map((x) => (typeof x === "string" ? x : x.hash)),
|
|
210
214
|
);
|
|
211
215
|
this._lastRemovedHeadsPath = update.newPath;
|
|
212
216
|
this._lastRemovedHeadsCount = update.counter;
|
|
@@ -218,11 +222,11 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
218
222
|
) {
|
|
219
223
|
const resetToHeads = await this.getCachedHeads(
|
|
220
224
|
this._lastHeadsPath,
|
|
221
|
-
this._lastRemovedHeadsPath
|
|
225
|
+
this._lastRemovedHeadsPath,
|
|
222
226
|
);
|
|
223
227
|
await this._updateCachedHeads(
|
|
224
228
|
{ added: resetToHeads, removed: [] },
|
|
225
|
-
true
|
|
229
|
+
true,
|
|
226
230
|
);
|
|
227
231
|
}
|
|
228
232
|
}
|
|
@@ -238,14 +242,14 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
238
242
|
|
|
239
243
|
async getCachedHeads(
|
|
240
244
|
lastHeadsPath: string | undefined = this._lastHeadsPath,
|
|
241
|
-
lastRemovedHeadsPath: string | undefined = this._lastRemovedHeadsPath
|
|
245
|
+
lastRemovedHeadsPath: string | undefined = this._lastRemovedHeadsPath,
|
|
242
246
|
): Promise<string[]> {
|
|
243
247
|
if (!this._cache) {
|
|
244
248
|
return [];
|
|
245
249
|
}
|
|
246
250
|
const getHashes = async (
|
|
247
251
|
start: string | undefined,
|
|
248
|
-
filter?: Set<string
|
|
252
|
+
filter?: Set<string>,
|
|
249
253
|
) => {
|
|
250
254
|
const result: string[] = [];
|
|
251
255
|
let next = start;
|
|
@@ -255,7 +259,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
255
259
|
const cache = bytes && deserialize(bytes, HeadsCacheToSerialize);
|
|
256
260
|
next = cache?.last;
|
|
257
261
|
cache?.heads.forEach((head) => {
|
|
258
|
-
if (filter
|
|
262
|
+
if (filter?.has(head)) {
|
|
259
263
|
return;
|
|
260
264
|
}
|
|
261
265
|
|
|
@@ -387,10 +391,10 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
387
391
|
added?: (Entry<T> | string)[];
|
|
388
392
|
removed?: (Entry<T> | string)[];
|
|
389
393
|
},
|
|
390
|
-
reset?: boolean
|
|
394
|
+
reset?: boolean,
|
|
391
395
|
) {
|
|
392
396
|
return this._cacheWriteQueue?.add(() =>
|
|
393
|
-
this._updateCachedHeads(changes, reset)
|
|
397
|
+
this._updateCachedHeads(changes, reset),
|
|
394
398
|
);
|
|
395
399
|
}
|
|
396
400
|
}
|