@peerbit/log 3.0.34-aa577a5 → 3.0.34-cccc078
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/{index.d.ts.map → memory/index.d.ts.map} +1 -1
- 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/dist/benchmark/{index.js → payload.js} +14 -14
- package/dist/benchmark/payload.js.map +1 -0
- package/dist/src/change.d.ts +2 -2
- package/dist/src/change.d.ts.map +1 -1
- package/dist/src/change.js +1 -1
- package/dist/src/change.js.map +1 -1
- package/dist/src/clock.d.ts +0 -24
- package/dist/src/clock.d.ts.map +1 -1
- package/dist/src/clock.js +28 -35
- package/dist/src/clock.js.map +1 -1
- package/dist/src/encoding.d.ts.map +1 -1
- package/dist/src/encoding.js +2 -2
- package/dist/src/encoding.js.map +1 -1
- package/dist/src/entry-index.d.ts +70 -17
- package/dist/src/entry-index.d.ts.map +1 -1
- package/dist/src/entry-index.js +281 -41
- package/dist/src/entry-index.js.map +1 -1
- package/dist/src/entry-with-refs.d.ts +1 -1
- package/dist/src/entry-with-refs.d.ts.map +1 -1
- package/dist/src/entry-with-refs.js +1 -1
- package/dist/src/entry-with-refs.js.map +1 -1
- package/dist/src/entry.d.ts +18 -15
- package/dist/src/entry.d.ts.map +1 -1
- package/dist/src/entry.js +62 -36
- package/dist/src/entry.js.map +1 -1
- package/dist/src/find-uniques.d.ts.map +1 -1
- package/dist/src/heads-cache.d.ts +1 -1
- package/dist/src/heads-cache.d.ts.map +1 -1
- package/dist/src/heads-cache.js +6 -7
- package/dist/src/heads-cache.js.map +1 -1
- package/dist/src/log-sorting.d.ts +27 -37
- package/dist/src/log-sorting.d.ts.map +1 -1
- package/dist/src/log-sorting.js +92 -74
- package/dist/src/log-sorting.js.map +1 -1
- package/dist/src/log.d.ts +71 -54
- package/dist/src/log.d.ts.map +1 -1
- package/dist/src/log.js +349 -468
- package/dist/src/log.js.map +1 -1
- package/dist/src/logger.d.ts.map +1 -1
- package/dist/src/logger.js.map +1 -1
- package/dist/src/snapshot.d.ts +2 -2
- package/dist/src/snapshot.d.ts.map +1 -1
- package/dist/src/snapshot.js +5 -5
- package/dist/src/snapshot.js.map +1 -1
- package/dist/src/trim.d.ts +9 -8
- package/dist/src/trim.d.ts.map +1 -1
- package/dist/src/trim.js +43 -40
- package/dist/src/trim.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/package.json +15 -13
- package/src/change.ts +3 -2
- package/src/clock.ts +25 -19
- package/src/encoding.ts +3 -3
- package/src/entry-index.ts +451 -52
- package/src/entry-with-refs.ts +1 -1
- package/src/entry.ts +89 -72
- package/src/heads-cache.ts +27 -21
- package/src/log-sorting.ts +116 -94
- package/src/log.ts +465 -564
- package/src/logger.ts +1 -0
- package/src/snapshot.ts +10 -10
- package/src/trim.ts +75 -50
- package/src/utils.ts +6 -8
- package/dist/benchmark/index.js.map +0 -1
- package/dist/src/heads.d.ts +0 -70
- package/dist/src/heads.d.ts.map +0 -1
- package/dist/src/heads.js +0 -164
- package/dist/src/heads.js.map +0 -1
- package/dist/src/types.d.ts +0 -7
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/types.js +0 -21
- package/dist/src/types.js.map +0 -1
- package/dist/src/values.d.ts +0 -27
- package/dist/src/values.d.ts.map +0 -1
- package/dist/src/values.js +0 -134
- package/dist/src/values.js.map +0 -1
- package/src/heads.ts +0 -233
- package/src/types.ts +0 -10
- package/src/values.ts +0 -174
- /package/dist/benchmark/{index.d.ts → memory/index.d.ts} +0 -0
package/src/entry.ts
CHANGED
|
@@ -1,33 +1,34 @@
|
|
|
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
9
|
} from "@dao-xyz/borsh";
|
|
11
|
-
|
|
10
|
+
import { type Blocks } from "@peerbit/blocks-interface";
|
|
12
11
|
import {
|
|
12
|
+
AccessError,
|
|
13
13
|
DecryptedThing,
|
|
14
|
+
Ed25519PublicKey,
|
|
15
|
+
type Identity,
|
|
14
16
|
MaybeEncrypted,
|
|
15
17
|
PublicSignKey,
|
|
16
|
-
X25519PublicKey,
|
|
17
18
|
SignatureWithKey,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
sha256Base64,
|
|
19
|
+
X25519Keypair,
|
|
20
|
+
X25519PublicKey,
|
|
21
21
|
randomBytes,
|
|
22
|
-
|
|
23
|
-
X25519Keypair
|
|
22
|
+
sha256Base64,
|
|
24
23
|
} from "@peerbit/crypto";
|
|
25
24
|
import { verify } from "@peerbit/crypto";
|
|
25
|
+
import { id } from "@peerbit/indexer-interface";
|
|
26
|
+
import { type Keychain } from "@peerbit/keychain";
|
|
26
27
|
import { compare } from "uint8arrays";
|
|
28
|
+
import { LamportClock as Clock, HLC, Timestamp } from "./clock.js";
|
|
27
29
|
import { type Encoding, NO_ENCODING } from "./encoding.js";
|
|
30
|
+
import type { SortableEntry } from "./log-sorting.js";
|
|
28
31
|
import { logger } from "./logger.js";
|
|
29
|
-
import { type Blocks } from "@peerbit/blocks-interface";
|
|
30
|
-
import { type Keychain } from "@peerbit/keychain";
|
|
31
32
|
import { equals } from "./utils.js";
|
|
32
33
|
|
|
33
34
|
export type MaybeEncryptionPublicKey =
|
|
@@ -119,7 +120,7 @@ export class Payload<T> {
|
|
|
119
120
|
return this._value;
|
|
120
121
|
}
|
|
121
122
|
getValue(encoding: Encoding<T> = this.encoding || NO_ENCODING): T {
|
|
122
|
-
if (this._value
|
|
123
|
+
if (this._value !== undefined) {
|
|
123
124
|
return this._value;
|
|
124
125
|
}
|
|
125
126
|
return encoding.decoder(this.data);
|
|
@@ -134,10 +135,10 @@ export interface EntryEncryptionTemplate<A, B, C> {
|
|
|
134
135
|
|
|
135
136
|
export enum EntryType {
|
|
136
137
|
APPEND = 0, // Add more data
|
|
137
|
-
CUT = 1 // Delete or Create tombstone ... delete all nexts, i
|
|
138
|
+
CUT = 1, // Delete or Create tombstone ... delete all nexts, i
|
|
138
139
|
}
|
|
139
140
|
|
|
140
|
-
@variant(0)
|
|
141
|
+
/* @variant(0) */
|
|
141
142
|
export class Meta {
|
|
142
143
|
@field({ type: Clock })
|
|
143
144
|
clock: Clock;
|
|
@@ -196,7 +197,7 @@ export class Signatures {
|
|
|
196
197
|
const maybeEncrypt = <Q>(
|
|
197
198
|
thing: Q,
|
|
198
199
|
keypair?: X25519Keypair,
|
|
199
|
-
receiver?: MaybeEncryptionPublicKey
|
|
200
|
+
receiver?: MaybeEncryptionPublicKey,
|
|
200
201
|
): Promise<MaybeEncrypted<Q>> | MaybeEncrypted<Q> => {
|
|
201
202
|
const receivers = receiver
|
|
202
203
|
? Array.isArray(receiver)
|
|
@@ -209,26 +210,41 @@ const maybeEncrypt = <Q>(
|
|
|
209
210
|
}
|
|
210
211
|
return new DecryptedThing<Q>({
|
|
211
212
|
data: serialize(thing),
|
|
212
|
-
value: thing
|
|
213
|
+
value: thing,
|
|
213
214
|
}).encrypt(keypair, receivers);
|
|
214
215
|
}
|
|
215
216
|
return new DecryptedThing<Q>({
|
|
216
217
|
data: serialize(thing),
|
|
217
|
-
value: thing
|
|
218
|
+
value: thing,
|
|
218
219
|
});
|
|
219
220
|
};
|
|
220
221
|
|
|
221
|
-
export
|
|
222
|
+
export class ShallowEntry {
|
|
223
|
+
@id({ type: "string" })
|
|
222
224
|
hash: string;
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
+
}
|
|
231
246
|
}
|
|
247
|
+
export type ShallowOrFullEntry<T> = ShallowEntry | Entry<T>;
|
|
232
248
|
|
|
233
249
|
@variant(0)
|
|
234
250
|
export class Entry<T>
|
|
@@ -272,11 +288,11 @@ export class Entry<T>
|
|
|
272
288
|
init(
|
|
273
289
|
props:
|
|
274
290
|
| {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
| Entry<T
|
|
279
|
-
):
|
|
291
|
+
keychain?: Keychain;
|
|
292
|
+
encoding: Encoding<T>;
|
|
293
|
+
}
|
|
294
|
+
| Entry<T>,
|
|
295
|
+
): this {
|
|
280
296
|
if (props instanceof Entry) {
|
|
281
297
|
this._keychain = props._keychain;
|
|
282
298
|
this._encoding = props._encoding;
|
|
@@ -361,7 +377,7 @@ export class Entry<T>
|
|
|
361
377
|
get size(): number {
|
|
362
378
|
if (this._size == null) {
|
|
363
379
|
throw new Error(
|
|
364
|
-
"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",
|
|
365
381
|
);
|
|
366
382
|
}
|
|
367
383
|
return this._size;
|
|
@@ -373,6 +389,7 @@ export class Entry<T>
|
|
|
373
389
|
get signatures(): SignatureWithKey[] {
|
|
374
390
|
const signatures = this._signatures!.signatures.filter((x) => {
|
|
375
391
|
try {
|
|
392
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
376
393
|
x.decrypted;
|
|
377
394
|
return true;
|
|
378
395
|
} catch (error) {
|
|
@@ -391,7 +408,7 @@ export class Entry<T>
|
|
|
391
408
|
*/
|
|
392
409
|
async getSignatures(): Promise<SignatureWithKey[]> {
|
|
393
410
|
const results = await Promise.allSettled(
|
|
394
|
-
this._signatures!.signatures.map((x) => x.decrypt(this._keychain))
|
|
411
|
+
this._signatures!.signatures.map((x) => x.decrypt(this._keychain)),
|
|
395
412
|
);
|
|
396
413
|
|
|
397
414
|
if (logger.level === "debug" || logger.level === "trace") {
|
|
@@ -432,7 +449,7 @@ export class Entry<T>
|
|
|
432
449
|
payload: entry._payload,
|
|
433
450
|
reserved: entry._reserved,
|
|
434
451
|
signatures: undefined,
|
|
435
|
-
hash: undefined
|
|
452
|
+
hash: undefined,
|
|
436
453
|
});
|
|
437
454
|
return trimmed;
|
|
438
455
|
}
|
|
@@ -477,14 +494,14 @@ export class Entry<T>
|
|
|
477
494
|
type?: EntryType;
|
|
478
495
|
gidSeed?: Uint8Array;
|
|
479
496
|
data?: Uint8Array;
|
|
480
|
-
next?:
|
|
497
|
+
next?: SortableEntry[];
|
|
481
498
|
};
|
|
482
499
|
encoding?: Encoding<T>;
|
|
483
500
|
canAppend?: CanAppend<T>;
|
|
484
501
|
encryption?: EntryEncryption;
|
|
485
502
|
identity: Identity;
|
|
486
503
|
signers?: ((
|
|
487
|
-
data: Uint8Array
|
|
504
|
+
data: Uint8Array,
|
|
488
505
|
) => Promise<SignatureWithKey> | SignatureWithKey)[];
|
|
489
506
|
}): Promise<Entry<T>> {
|
|
490
507
|
if (!properties.encoding || !properties?.meta?.next) {
|
|
@@ -492,9 +509,9 @@ export class Entry<T>
|
|
|
492
509
|
...properties,
|
|
493
510
|
meta: {
|
|
494
511
|
...properties?.meta,
|
|
495
|
-
next: properties.meta?.next ? properties.meta?.next : []
|
|
512
|
+
next: properties.meta?.next ? properties.meta?.next : [],
|
|
496
513
|
},
|
|
497
|
-
encoding: properties.encoding ? properties.encoding : NO_ENCODING
|
|
514
|
+
encoding: properties.encoding ? properties.encoding : NO_ENCODING,
|
|
498
515
|
};
|
|
499
516
|
}
|
|
500
517
|
|
|
@@ -512,7 +529,7 @@ export class Entry<T>
|
|
|
512
529
|
const payloadToSave = new Payload<T>({
|
|
513
530
|
data: properties.encoding.encoder(properties.data),
|
|
514
531
|
value: properties.data,
|
|
515
|
-
encoding: properties.encoding
|
|
532
|
+
encoding: properties.encoding,
|
|
516
533
|
});
|
|
517
534
|
|
|
518
535
|
let clock: Clock | undefined = properties.meta?.clock;
|
|
@@ -527,12 +544,12 @@ export class Entry<T>
|
|
|
527
544
|
properties.encryption?.receiver.meta
|
|
528
545
|
) {
|
|
529
546
|
throw new Error(
|
|
530
|
-
"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",
|
|
531
548
|
);
|
|
532
549
|
}
|
|
533
550
|
clock = new Clock({
|
|
534
551
|
id: properties.identity.publicKey.bytes,
|
|
535
|
-
timestamp: hlc.now()
|
|
552
|
+
timestamp: hlc.now(),
|
|
536
553
|
});
|
|
537
554
|
} else {
|
|
538
555
|
const cv = clock;
|
|
@@ -541,9 +558,9 @@ export class Entry<T>
|
|
|
541
558
|
if (Timestamp.compare(n.meta.clock.timestamp, cv.timestamp) >= 0) {
|
|
542
559
|
throw new Error(
|
|
543
560
|
"Expecting next(s) to happen before entry, got: " +
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
561
|
+
n.meta.clock.timestamp +
|
|
562
|
+
" > " +
|
|
563
|
+
cv.timestamp,
|
|
547
564
|
);
|
|
548
565
|
}
|
|
549
566
|
}
|
|
@@ -556,7 +573,7 @@ export class Entry<T>
|
|
|
556
573
|
// take min gid as our gid
|
|
557
574
|
if (properties.meta?.gid) {
|
|
558
575
|
throw new Error(
|
|
559
|
-
"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",
|
|
560
577
|
);
|
|
561
578
|
}
|
|
562
579
|
for (const n of nexts) {
|
|
@@ -585,16 +602,16 @@ export class Entry<T>
|
|
|
585
602
|
gid: gid!,
|
|
586
603
|
type: properties.meta?.type ?? EntryType.APPEND,
|
|
587
604
|
data: properties.meta?.data,
|
|
588
|
-
next: nextHashes
|
|
605
|
+
next: nextHashes,
|
|
589
606
|
}),
|
|
590
607
|
properties.encryption?.keypair,
|
|
591
|
-
properties.encryption?.receiver.meta
|
|
608
|
+
properties.encryption?.receiver.meta,
|
|
592
609
|
);
|
|
593
610
|
|
|
594
611
|
const payload = await maybeEncrypt(
|
|
595
612
|
payloadToSave,
|
|
596
613
|
properties.encryption?.keypair,
|
|
597
|
-
properties.encryption?.receiver.payload
|
|
614
|
+
properties.encryption?.receiver.payload,
|
|
598
615
|
);
|
|
599
616
|
|
|
600
617
|
// Sign id, encrypted payload, clock, nexts, refs
|
|
@@ -602,40 +619,40 @@ export class Entry<T>
|
|
|
602
619
|
meta: metadataEncrypted,
|
|
603
620
|
payload,
|
|
604
621
|
signatures: undefined,
|
|
605
|
-
createdLocally: true
|
|
622
|
+
createdLocally: true,
|
|
606
623
|
});
|
|
607
624
|
|
|
608
625
|
const signers = properties.signers || [
|
|
609
|
-
properties.identity.sign.bind(properties.identity)
|
|
626
|
+
properties.identity.sign.bind(properties.identity),
|
|
610
627
|
];
|
|
611
628
|
const signable = entry.toSignable();
|
|
612
629
|
const signableBytes = serialize(signable);
|
|
613
630
|
let signatures = await Promise.all(
|
|
614
|
-
signers.map((signer) => signer(signableBytes))
|
|
631
|
+
signers.map((signer) => signer(signableBytes)),
|
|
615
632
|
);
|
|
616
633
|
signatures = signatures.sort((a, b) => compare(a.signature, b.signature));
|
|
617
634
|
|
|
618
635
|
const encryptedSignatures: MaybeEncrypted<SignatureWithKey>[] = [];
|
|
619
636
|
const encryptAllSignaturesWithSameKey = isMaybeEryptionPublicKey(
|
|
620
|
-
properties.encryption?.receiver?.signatures
|
|
637
|
+
properties.encryption?.receiver?.signatures,
|
|
621
638
|
);
|
|
622
639
|
|
|
623
640
|
for (const signature of signatures) {
|
|
624
641
|
const encryptionRecievers = encryptAllSignaturesWithSameKey
|
|
625
642
|
? properties.encryption?.receiver?.signatures
|
|
626
|
-
: (properties.encryption?.receiver?.signatures as any)?.[
|
|
627
|
-
|
|
628
|
-
|
|
643
|
+
: (properties.encryption?.receiver?.signatures as any)?.[
|
|
644
|
+
signature.publicKey.hashcode()
|
|
645
|
+
]; // TODO types
|
|
629
646
|
const signatureEncrypted = await maybeEncrypt(
|
|
630
647
|
signature,
|
|
631
648
|
properties.encryption?.keypair,
|
|
632
|
-
encryptionRecievers
|
|
649
|
+
encryptionRecievers,
|
|
633
650
|
);
|
|
634
651
|
encryptedSignatures.push(signatureEncrypted);
|
|
635
652
|
}
|
|
636
653
|
|
|
637
654
|
entry._signatures = new Signatures({
|
|
638
|
-
signatures: encryptedSignatures
|
|
655
|
+
signatures: encryptedSignatures,
|
|
639
656
|
});
|
|
640
657
|
|
|
641
658
|
if (properties.canAppend && !(await properties.canAppend(entry))) {
|
|
@@ -654,18 +671,19 @@ export class Entry<T>
|
|
|
654
671
|
return this._payload.byteLength;
|
|
655
672
|
}
|
|
656
673
|
|
|
657
|
-
toShallow(): ShallowEntry {
|
|
658
|
-
return {
|
|
674
|
+
toShallow(isHead: boolean): ShallowEntry {
|
|
675
|
+
return new ShallowEntry({
|
|
659
676
|
hash: this.hash,
|
|
660
|
-
|
|
661
|
-
|
|
677
|
+
payloadSize: this._payload.byteLength,
|
|
678
|
+
head: isHead,
|
|
679
|
+
meta: new Meta({
|
|
662
680
|
gid: this.meta.gid,
|
|
663
681
|
data: this.meta.data,
|
|
664
682
|
clock: this.meta.clock,
|
|
665
683
|
next: this.meta.next,
|
|
666
|
-
type: this.meta.type
|
|
667
|
-
}
|
|
668
|
-
};
|
|
684
|
+
type: this.meta.type,
|
|
685
|
+
}),
|
|
686
|
+
});
|
|
669
687
|
}
|
|
670
688
|
|
|
671
689
|
/**
|
|
@@ -682,8 +700,7 @@ export class Entry<T>
|
|
|
682
700
|
|
|
683
701
|
const bytes = serialize(entry);
|
|
684
702
|
entry.size = bytes.length;
|
|
685
|
-
|
|
686
|
-
return result;
|
|
703
|
+
return store.put(bytes);
|
|
687
704
|
}
|
|
688
705
|
|
|
689
706
|
/**
|
|
@@ -696,7 +713,7 @@ export class Entry<T>
|
|
|
696
713
|
static async fromMultihash<T>(
|
|
697
714
|
store: Blocks,
|
|
698
715
|
hash: string,
|
|
699
|
-
options?: { timeout?: number; replicate?: boolean }
|
|
716
|
+
options?: { timeout?: number; replicate?: boolean },
|
|
700
717
|
) {
|
|
701
718
|
if (!hash) throw new Error(`Invalid hash: ${hash}`);
|
|
702
719
|
const bytes = await store.get(hash, options);
|
|
@@ -740,7 +757,7 @@ export class Entry<T>
|
|
|
740
757
|
* @returns {boolean}
|
|
741
758
|
*/
|
|
742
759
|
static isDirectParent<T>(entry1: Entry<T>, entry2: Entry<T>) {
|
|
743
|
-
return entry2.next.
|
|
760
|
+
return entry2.next.includes(entry1.hash as any); // TODO fix types
|
|
744
761
|
}
|
|
745
762
|
|
|
746
763
|
/**
|
|
@@ -752,7 +769,7 @@ export class Entry<T>
|
|
|
752
769
|
*/
|
|
753
770
|
static findDirectChildren<T>(
|
|
754
771
|
entry: Entry<T>,
|
|
755
|
-
values: Entry<T>[]
|
|
772
|
+
values: Entry<T>[],
|
|
756
773
|
): Entry<T>[] {
|
|
757
774
|
let stack: Entry<T>[] = [];
|
|
758
775
|
let parent = values.find((e) => Entry.isDirectParent(entry, e));
|
package/src/heads-cache.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
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
14
|
import { Entry } from "./entry.js";
|
|
4
|
-
import { type 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";
|
|
8
15
|
|
|
9
|
-
import path from "path-browserify";
|
|
10
16
|
export const logger = loggerFn({ module: "heads-cache" });
|
|
11
|
-
export class CachedValue {
|
|
17
|
+
export class CachedValue {}
|
|
12
18
|
/* export type AppendOptions<T> = {
|
|
13
19
|
signers?: ((data: Uint8Array) => Promise<SignatureWithKey>)[];
|
|
14
20
|
nexts?: Entry<T>[];
|
|
@@ -60,23 +66,23 @@ const updateHashes = async (
|
|
|
60
66
|
headsPath: string,
|
|
61
67
|
lastCid: string | undefined,
|
|
62
68
|
lastCounter: bigint,
|
|
63
|
-
hashes: string[]
|
|
69
|
+
hashes: string[],
|
|
64
70
|
): Promise<{ counter: bigint; newPath: string }> => {
|
|
65
71
|
const newHeadsPath = path.join(
|
|
66
72
|
headsPath,
|
|
67
73
|
String(headCache.headsPathCounter),
|
|
68
|
-
uuid()
|
|
74
|
+
uuid(),
|
|
69
75
|
);
|
|
70
76
|
const counter = lastCounter + BigInt(hashes.length);
|
|
71
77
|
await Promise.all([
|
|
72
78
|
headCache.cache?.put(
|
|
73
79
|
headsPath,
|
|
74
|
-
serialize(new CachePath(newHeadsPath.toString()))
|
|
80
|
+
serialize(new CachePath(newHeadsPath.toString())),
|
|
75
81
|
),
|
|
76
82
|
headCache.cache?.put(
|
|
77
83
|
newHeadsPath,
|
|
78
|
-
serialize(new HeadsCacheToSerialize(hashes, counter, lastCid))
|
|
79
|
-
)
|
|
84
|
+
serialize(new HeadsCacheToSerialize(hashes, counter, lastCid)),
|
|
85
|
+
),
|
|
80
86
|
]);
|
|
81
87
|
return { counter, newPath: newHeadsPath };
|
|
82
88
|
};
|
|
@@ -153,7 +159,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
153
159
|
added?: (Entry<T> | string)[];
|
|
154
160
|
removed?: (Entry<T> | string)[];
|
|
155
161
|
},
|
|
156
|
-
reset?: boolean
|
|
162
|
+
reset?: boolean,
|
|
157
163
|
) {
|
|
158
164
|
if (typeof reset !== "boolean" && change.added) {
|
|
159
165
|
// Only reset all heads if loaded once, since we don't want too loose track of unloaded heads
|
|
@@ -190,7 +196,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
190
196
|
this.headsPath,
|
|
191
197
|
this._lastHeadsPath,
|
|
192
198
|
this._lastHeadsCount,
|
|
193
|
-
change.added.map((x) => (typeof x === "string" ? x : x.hash))
|
|
199
|
+
change.added.map((x) => (typeof x === "string" ? x : x.hash)),
|
|
194
200
|
);
|
|
195
201
|
this._lastHeadsPath = update.newPath;
|
|
196
202
|
this._lastHeadsCount = update.counter;
|
|
@@ -204,7 +210,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
204
210
|
this.removedHeadsPath,
|
|
205
211
|
this._lastRemovedHeadsPath,
|
|
206
212
|
this._lastRemovedHeadsCount,
|
|
207
|
-
change.removed.map((x) => (typeof x === "string" ? x : x.hash))
|
|
213
|
+
change.removed.map((x) => (typeof x === "string" ? x : x.hash)),
|
|
208
214
|
);
|
|
209
215
|
this._lastRemovedHeadsPath = update.newPath;
|
|
210
216
|
this._lastRemovedHeadsCount = update.counter;
|
|
@@ -216,11 +222,11 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
216
222
|
) {
|
|
217
223
|
const resetToHeads = await this.getCachedHeads(
|
|
218
224
|
this._lastHeadsPath,
|
|
219
|
-
this._lastRemovedHeadsPath
|
|
225
|
+
this._lastRemovedHeadsPath,
|
|
220
226
|
);
|
|
221
227
|
await this._updateCachedHeads(
|
|
222
228
|
{ added: resetToHeads, removed: [] },
|
|
223
|
-
true
|
|
229
|
+
true,
|
|
224
230
|
);
|
|
225
231
|
}
|
|
226
232
|
}
|
|
@@ -236,14 +242,14 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
236
242
|
|
|
237
243
|
async getCachedHeads(
|
|
238
244
|
lastHeadsPath: string | undefined = this._lastHeadsPath,
|
|
239
|
-
lastRemovedHeadsPath: string | undefined = this._lastRemovedHeadsPath
|
|
245
|
+
lastRemovedHeadsPath: string | undefined = this._lastRemovedHeadsPath,
|
|
240
246
|
): Promise<string[]> {
|
|
241
247
|
if (!this._cache) {
|
|
242
248
|
return [];
|
|
243
249
|
}
|
|
244
250
|
const getHashes = async (
|
|
245
251
|
start: string | undefined,
|
|
246
|
-
filter?: Set<string
|
|
252
|
+
filter?: Set<string>,
|
|
247
253
|
) => {
|
|
248
254
|
const result: string[] = [];
|
|
249
255
|
let next = start;
|
|
@@ -253,7 +259,7 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
253
259
|
const cache = bytes && deserialize(bytes, HeadsCacheToSerialize);
|
|
254
260
|
next = cache?.last;
|
|
255
261
|
cache?.heads.forEach((head) => {
|
|
256
|
-
if (filter
|
|
262
|
+
if (filter?.has(head)) {
|
|
257
263
|
return;
|
|
258
264
|
}
|
|
259
265
|
|
|
@@ -385,10 +391,10 @@ export class HeadsCache<T> /* implements Initiable<T> */ {
|
|
|
385
391
|
added?: (Entry<T> | string)[];
|
|
386
392
|
removed?: (Entry<T> | string)[];
|
|
387
393
|
},
|
|
388
|
-
reset?: boolean
|
|
394
|
+
reset?: boolean,
|
|
389
395
|
) {
|
|
390
396
|
return this._cacheWriteQueue?.add(() =>
|
|
391
|
-
this._updateCachedHeads(changes, reset)
|
|
397
|
+
this._updateCachedHeads(changes, reset),
|
|
392
398
|
);
|
|
393
399
|
}
|
|
394
400
|
}
|