@peerbit/log 1.0.2

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.
Files changed (92) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +11 -0
  3. package/lib/esm/change.d.ts +5 -0
  4. package/lib/esm/change.js +2 -0
  5. package/lib/esm/change.js.map +1 -0
  6. package/lib/esm/clock.d.ts +87 -0
  7. package/lib/esm/clock.js +260 -0
  8. package/lib/esm/clock.js.map +1 -0
  9. package/lib/esm/difference.d.ts +1 -0
  10. package/lib/esm/difference.js +20 -0
  11. package/lib/esm/difference.js.map +1 -0
  12. package/lib/esm/encoding.d.ts +7 -0
  13. package/lib/esm/encoding.js +20 -0
  14. package/lib/esm/encoding.js.map +1 -0
  15. package/lib/esm/entry-index.d.ts +21 -0
  16. package/lib/esm/entry-index.js +63 -0
  17. package/lib/esm/entry-index.js.map +1 -0
  18. package/lib/esm/entry-with-refs.d.ts +5 -0
  19. package/lib/esm/entry-with-refs.js +2 -0
  20. package/lib/esm/entry-with-refs.js.map +1 -0
  21. package/lib/esm/entry.d.ts +179 -0
  22. package/lib/esm/entry.js +591 -0
  23. package/lib/esm/entry.js.map +1 -0
  24. package/lib/esm/find-uniques.d.ts +1 -0
  25. package/lib/esm/find-uniques.js +12 -0
  26. package/lib/esm/find-uniques.js.map +1 -0
  27. package/lib/esm/heads-cache.d.ts +64 -0
  28. package/lib/esm/heads-cache.js +317 -0
  29. package/lib/esm/heads-cache.js.map +1 -0
  30. package/lib/esm/heads.d.ts +63 -0
  31. package/lib/esm/heads.js +143 -0
  32. package/lib/esm/heads.js.map +1 -0
  33. package/lib/esm/hrtime.d.ts +5 -0
  34. package/lib/esm/hrtime.js +71 -0
  35. package/lib/esm/hrtime.js.map +1 -0
  36. package/lib/esm/index.d.ts +11 -0
  37. package/lib/esm/index.js +11 -0
  38. package/lib/esm/index.js.map +1 -0
  39. package/lib/esm/is-defined.d.ts +1 -0
  40. package/lib/esm/is-defined.js +2 -0
  41. package/lib/esm/is-defined.js.map +1 -0
  42. package/lib/esm/log-errors.d.ts +5 -0
  43. package/lib/esm/log-errors.js +6 -0
  44. package/lib/esm/log-errors.js.map +1 -0
  45. package/lib/esm/log-sorting.d.ts +44 -0
  46. package/lib/esm/log-sorting.js +86 -0
  47. package/lib/esm/log-sorting.js.map +1 -0
  48. package/lib/esm/log.d.ts +205 -0
  49. package/lib/esm/log.js +1004 -0
  50. package/lib/esm/log.js.map +1 -0
  51. package/lib/esm/logger.d.ts +2 -0
  52. package/lib/esm/logger.js +4 -0
  53. package/lib/esm/logger.js.map +1 -0
  54. package/lib/esm/package.json +3 -0
  55. package/lib/esm/snapshot.d.ts +22 -0
  56. package/lib/esm/snapshot.js +83 -0
  57. package/lib/esm/snapshot.js.map +1 -0
  58. package/lib/esm/trim.d.ts +49 -0
  59. package/lib/esm/trim.js +203 -0
  60. package/lib/esm/trim.js.map +1 -0
  61. package/lib/esm/types.d.ts +6 -0
  62. package/lib/esm/types.js +23 -0
  63. package/lib/esm/types.js.map +1 -0
  64. package/lib/esm/utils.d.ts +2 -0
  65. package/lib/esm/utils.js +3 -0
  66. package/lib/esm/utils.js.map +1 -0
  67. package/lib/esm/values.d.ts +33 -0
  68. package/lib/esm/values.js +141 -0
  69. package/lib/esm/values.js.map +1 -0
  70. package/package.json +79 -0
  71. package/src/change.ts +2 -0
  72. package/src/clock.ts +280 -0
  73. package/src/difference.ts +22 -0
  74. package/src/encoding.ts +27 -0
  75. package/src/entry-index.ts +78 -0
  76. package/src/entry-with-refs.ts +6 -0
  77. package/src/entry.ts +749 -0
  78. package/src/find-uniques.ts +14 -0
  79. package/src/heads-cache.ts +400 -0
  80. package/src/heads.ts +208 -0
  81. package/src/hrtime.ts +78 -0
  82. package/src/index.ts +11 -0
  83. package/src/is-defined.ts +1 -0
  84. package/src/log-errors.ts +9 -0
  85. package/src/log-sorting.ts +108 -0
  86. package/src/log.ts +1262 -0
  87. package/src/logger.ts +3 -0
  88. package/src/snapshot.ts +103 -0
  89. package/src/trim.ts +269 -0
  90. package/src/types.ts +12 -0
  91. package/src/utils.ts +2 -0
  92. package/src/values.ts +193 -0
@@ -0,0 +1,71 @@
1
+ /*
2
+
3
+ MIT License
4
+
5
+ Copyright (c) 2020 Vlad Tansky
6
+ Copyright (c) 2022 dao.xyz
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ of this software and associated documentation files (the "Software"), to deal
10
+ in the Software without restriction, including without limitation the rights
11
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ copies of the Software, and to permit persons to whom the Software is
13
+ furnished to do so, subject to the following conditions:
14
+
15
+ The above copyright notice and this permission notice shall be included in all
16
+ copies or substantial portions of the Software.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+
26
+ */
27
+ const _perfomancePolyfill = () => {
28
+ // based on https://gist.github.com/paulirish/5438650 copyright Paul Irish 2015.
29
+ if ("performance" in window === false) {
30
+ window.performance = {};
31
+ }
32
+ Date.now =
33
+ Date.now ||
34
+ (() => {
35
+ // thanks IE8
36
+ return new Date().getTime();
37
+ });
38
+ if ("now" in window.performance === false) {
39
+ let nowOffset = Date.now();
40
+ if (performance.timing && performance.timing.navigationStart) {
41
+ nowOffset = performance.timing.navigationStart;
42
+ }
43
+ window.performance.now = () => Date.now() - nowOffset;
44
+ }
45
+ };
46
+ const _hrtime = (previousTimestamp) => {
47
+ _perfomancePolyfill();
48
+ const baseNow = Math.floor((Date.now() - performance.now()) * 1e-3);
49
+ const clocktime = performance.now() * 1e-3;
50
+ let seconds = Math.floor(clocktime) + baseNow;
51
+ let nanoseconds = Math.floor((clocktime % 1) * 1e9);
52
+ if (previousTimestamp) {
53
+ seconds = seconds - previousTimestamp[0];
54
+ nanoseconds = nanoseconds - previousTimestamp[1];
55
+ if (nanoseconds < 0) {
56
+ seconds--;
57
+ nanoseconds += 1e9;
58
+ }
59
+ }
60
+ return [seconds, nanoseconds];
61
+ };
62
+ const NS_PER_SEC = 1e9;
63
+ _hrtime.bigint = (time) => {
64
+ const diff = _hrtime(time);
65
+ return BigInt(diff[0] * NS_PER_SEC + diff[1]);
66
+ };
67
+ export default typeof process === "undefined" ||
68
+ typeof process.hrtime === "undefined"
69
+ ? _hrtime
70
+ : process.hrtime;
71
+ //# sourceMappingURL=hrtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hrtime.js","sourceRoot":"","sources":["../../src/hrtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;EAyBE;AAEF,MAAM,mBAAmB,GAAG,GAAG,EAAE;IAChC,gFAAgF;IAChF,IAAI,aAAa,IAAI,MAAM,KAAK,KAAK,EAAE;QACrC,MAAM,CAAC,WAAmB,GAAG,EAAE,CAAC;KACjC;IAED,IAAI,CAAC,GAAG;QACP,IAAI,CAAC,GAAG;YACR,CAAC,GAAG,EAAE;gBACL,aAAa;gBACb,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;YAC7B,CAAC,CAAC,CAAC;IAEJ,IAAI,KAAK,IAAI,MAAM,CAAC,WAAW,KAAK,KAAK,EAAE;QAC1C,IAAI,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE3B,IAAI,WAAW,CAAC,MAAM,IAAI,WAAW,CAAC,MAAM,CAAC,eAAe,EAAE;YAC7D,SAAS,G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AAe,OAAO,OAAO,KAAK,WAAW;IAC7C,OAAO,OAAO,CAAC,MAAM,KAAK,WAAW;IACpC,CAAC,CAAC,OAAO;IACT,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC"}
@@ -0,0 +1,11 @@
1
+ export * from "./log.js";
2
+ export * from "./log-sorting.js";
3
+ export * from "./log-errors.js";
4
+ export * from "./snapshot.js";
5
+ export * from "./entry.js";
6
+ export * from "./utils.js";
7
+ export * from "./clock.js";
8
+ export * from "./encoding.js";
9
+ export * from "./trim.js";
10
+ export * from "./change.js";
11
+ export type { TrimToByteLengthOption, TrimToLengthOption } from "./trim.js";
@@ -0,0 +1,11 @@
1
+ export * from "./log.js";
2
+ export * from "./log-sorting.js";
3
+ export * from "./log-errors.js";
4
+ export * from "./snapshot.js";
5
+ export * from "./entry.js";
6
+ export * from "./utils.js";
7
+ export * from "./clock.js";
8
+ export * from "./encoding.js";
9
+ export * from "./trim.js";
10
+ export * from "./change.js";
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
@@ -0,0 +1 @@
1
+ export declare const isDefined: (arg: any) => boolean;
@@ -0,0 +1,2 @@
1
+ export const isDefined = (arg) => arg !== undefined && arg !== null;
2
+ //# sourceMappingURL=is-defined.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-defined.js","sourceRoot":"","sources":["../../src/is-defined.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAE,EAAE,CAAC,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,IAAI,CAAC"}
@@ -0,0 +1,5 @@
1
+ export declare const BlockStoreNotDefinedError: () => Error;
2
+ export declare const LogNotDefinedError: () => Error;
3
+ export declare const NotALogError: () => Error;
4
+ export declare const CannotJoinWithDifferentId: () => Error;
5
+ export declare const LtOrLteMustBeStringOrArray: () => Error;
@@ -0,0 +1,6 @@
1
+ export const BlockStoreNotDefinedError = () => new Error("Block store not defined");
2
+ export const LogNotDefinedError = () => new Error("Log instance not defined");
3
+ export const NotALogError = () => new Error("Given argument is not an instance of Log");
4
+ export const CannotJoinWithDifferentId = () => new Error("Can't join logs with different IDs");
5
+ export const LtOrLteMustBeStringOrArray = () => new Error("lt or lte must be a string or array of Entries");
6
+ //# sourceMappingURL=log-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log-errors.js","sourceRoot":"","sources":["../../src/log-errors.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAG,EAAE,CAC7C,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,EAAE,CAAC,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE,CAChC,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;AACvD,MAAM,CAAC,MAAM,yBAAyB,GAAG,GAAG,EAAE,CAC7C,IAAI,KAAK,CAAC,oCAAoC,CAAC,CAAC;AACjD,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,EAAE,CAC9C,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC"}
@@ -0,0 +1,44 @@
1
+ import { Entry } from "./entry.js";
2
+ export type ISortFunction = <T>(a: Entry<T>, b: Entry<T>, resolveConflict?: (a: Entry<T>, b: Entry<T>) => number) => number;
3
+ /**
4
+ * Sort two entries as Last-Write-Wins (LWW).
5
+ *
6
+ * Last Write Wins is a conflict resolution strategy for sorting elements
7
+ * where the element with a greater clock (latest) is chosen as the winner.
8
+ *
9
+ * @param {Entry} a First entry
10
+ * @param {Entry} b Second entry
11
+ * @returns {number} 1 if a is latest, -1 if b is latest
12
+ */
13
+ export declare const LastWriteWins: ISortFunction;
14
+ /**
15
+ * Sort two entries by their hash.
16
+ *
17
+ * @param {Entry} a First entry
18
+ * @param {Entry} b Second entry
19
+ * @returns {number} 1 if a is latest, -1 if b is latest
20
+ */
21
+ export declare const SortByEntryHash: ISortFunction;
22
+ /**
23
+ * Sort two entries by their clock time.
24
+ * @param {Entry} a First entry to compare
25
+ * @param {Entry} b Second entry to compare
26
+ * @param {function(a, b)} resolveConflict A function to call if entries are concurrent (happened at the same time). The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
27
+ * @returns {number} 1 if a is greater, -1 if b is greater
28
+ */
29
+ export declare const SortByClocks: ISortFunction;
30
+ /**
31
+ * Sort two entries by their clock id.
32
+ * @param {Entry} a First entry to compare
33
+ * @param {Entry} b Second entry to compare
34
+ * @param {function(a, b)} resolveConflict A function to call if the clocks ids are the same. The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
35
+ * @returns {number} 1 if a is greater, -1 if b is greater
36
+ */
37
+ export declare const SortByClockId: ISortFunction;
38
+ /**
39
+ * A wrapper function to throw an error if the results of a passed function return zero
40
+ * @param {function(a, b)} [tiebreaker] The tiebreaker function to validate.
41
+ * @returns {function(a, b)} 1 if a is greater, -1 if b is greater
42
+ * @throws {Error} if func ever returns 0
43
+ */
44
+ export declare const NoZeroes: (func: ISortFunction) => <T>(a: Entry<T>, b: Entry<T>) => number;
@@ -0,0 +1,86 @@
1
+ import { LamportClock as Clock } from "./clock.js";
2
+ import { compare } from "@peerbit/uint8arrays";
3
+ const First = (a, b) => a;
4
+ /**
5
+ * Sort two entries as Last-Write-Wins (LWW).
6
+ *
7
+ * Last Write Wins is a conflict resolution strategy for sorting elements
8
+ * where the element with a greater clock (latest) is chosen as the winner.
9
+ *
10
+ * @param {Entry} a First entry
11
+ * @param {Entry} b Second entry
12
+ * @returns {number} 1 if a is latest, -1 if b is latest
13
+ */
14
+ export const LastWriteWins = (a, b) => {
15
+ // Ultimate conflict resolution (take the first/left arg)
16
+ // Sort two entries by their clock id, if the same always take the first
17
+ const sortById = (a, b) => SortByClockId(a, b, First);
18
+ // Sort two entries by their clock time, if concurrent,
19
+ // determine sorting using provided conflict resolution function
20
+ const sortByEntryClocks = (a, b) => SortByClocks(a, b, sortById);
21
+ // Sort entries by clock time as the primary sort criteria
22
+ return sortByEntryClocks(a, b);
23
+ };
24
+ /**
25
+ * Sort two entries by their hash.
26
+ *
27
+ * @param {Entry} a First entry
28
+ * @param {Entry} b Second entry
29
+ * @returns {number} 1 if a is latest, -1 if b is latest
30
+ */
31
+ export const SortByEntryHash = (a, b) => {
32
+ // Ultimate conflict resolution (compare hashes)
33
+ const compareHash = (a, b) => a.hash < b.hash ? -1 : 1;
34
+ // Sort two entries by their clock id, if the same then compare hashes
35
+ const sortById = (a, b) => SortByClockId(a, b, compareHash);
36
+ // Sort two entries by their clock time, if concurrent,
37
+ // determine sorting using provided conflict resolution function
38
+ const sortByEntryClocks = (a, b) => SortByClocks(a, b, sortById);
39
+ // Sort entries by clock time as the primary sort criteria
40
+ return sortByEntryClocks(a, b);
41
+ };
42
+ /**
43
+ * Sort two entries by their clock time.
44
+ * @param {Entry} a First entry to compare
45
+ * @param {Entry} b Second entry to compare
46
+ * @param {function(a, b)} resolveConflict A function to call if entries are concurrent (happened at the same time). The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
47
+ * @returns {number} 1 if a is greater, -1 if b is greater
48
+ */
49
+ export const SortByClocks = (a, b, resolveConflict) => {
50
+ // Compare the clocks
51
+ const diff = Clock.compare(a.metadata.clock, b.metadata.clock);
52
+ // If the clocks are concurrent, use the provided
53
+ // conflict resolution function to determine which comes first
54
+ return diff === 0 ? (resolveConflict || First)(a, b) : diff;
55
+ };
56
+ /**
57
+ * Sort two entries by their clock id.
58
+ * @param {Entry} a First entry to compare
59
+ * @param {Entry} b Second entry to compare
60
+ * @param {function(a, b)} resolveConflict A function to call if the clocks ids are the same. The function should take in two entries and return 1 if the first entry should be chosen and -1 if the second entry should be chosen.
61
+ * @returns {number} 1 if a is greater, -1 if b is greater
62
+ */
63
+ export const SortByClockId = (a, b, resolveConflict) => {
64
+ // Sort by ID if clocks are concurrent,
65
+ // take the entry with a "greater" clock id
66
+ const clockCompare = compare(a.metadata.clock.id, b.metadata.clock.id);
67
+ return clockCompare === 0 ? (resolveConflict || First)(a, b) : clockCompare;
68
+ };
69
+ /**
70
+ * A wrapper function to throw an error if the results of a passed function return zero
71
+ * @param {function(a, b)} [tiebreaker] The tiebreaker function to validate.
72
+ * @returns {function(a, b)} 1 if a is greater, -1 if b is greater
73
+ * @throws {Error} if func ever returns 0
74
+ */
75
+ export const NoZeroes = (func) => {
76
+ const comparator = (a, b) => {
77
+ // Validate by calling the function
78
+ const result = func(a, b, (a, b) => -1);
79
+ if (result === 0) {
80
+ throw Error(`Your log's tiebreaker function, ${func.name}, has returned zero and therefore cannot be`);
81
+ }
82
+ return result;
83
+ };
84
+ return comparator;
85
+ };
86
+ //# sourceMappingURL=log-sorting.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log-sorting.js","sourceRoot":"","sources":["../../src/log-sorting.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,KAAK,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAE/C,MAAM,KAAK,GAAG,CAAC,CAAM,EAAE,CAAM,EAAE,EAAE,CAAC,CAAC,CAAC;AAOpC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAkB,CAAI,CAAW,EAAE,CAAW,EAAE,EAAE;IAC3E,yDAAyD;IACzD,wEAAwE;IACxE,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3
+ import { EntryIndex } from "./entry-index.js";
4
+ import * as Sorting from "./log-sorting.js";
5
+ import { EncryptionTemplateMaybeEncrypted, Entry, Payload, CanAppend, EntryType } from "./entry.js";
6
+ import { HLC, LamportClock, Timestamp } from "./clock.js";
7
+ import { Encoding } from "./encoding.js";
8
+ import { CacheUpdateOptions, HeadsIndex } from "./heads.js";
9
+ import { Values } from "./values.js";
10
+ import { TrimOptions } from "./trim.js";
11
+ import { Change } from "./change.js";
12
+ import { EntryWithRefs } from "./entry-with-refs.js";
13
+ import { Blocks } from "@peerbit/blocks-interface";
14
+ export type LogEvents<T> = {
15
+ onChange?: (change: Change<T>) => void;
16
+ };
17
+ export type MemoryProperties = {
18
+ cache?: SimpleLevel;
19
+ };
20
+ export type LogProperties<T> = {
21
+ keychain?: Keychain;
22
+ encoding?: Encoding<T>;
23
+ clock?: LamportClock;
24
+ sortFn?: Sorting.ISortFunction;
25
+ trim?: TrimOptions;
26
+ canAppend?: CanAppend<T>;
27
+ };
28
+ export type LogOptions<T> = LogProperties<T> & LogEvents<T> & MemoryProperties;
29
+ export type AppendOptions<T> = {
30
+ type?: EntryType;
31
+ gidSeed?: Uint8Array;
32
+ nexts?: Entry<any>[];
33
+ identity?: Identity;
34
+ signers?: ((data: Uint8Array) => Promise<SignatureWithKey> | SignatureWithKey)[];
35
+ onGidsShadowed?: (gids: string[]) => void;
36
+ trim?: TrimOptions;
37
+ timestamp?: Timestamp;
38
+ encryption?: {
39
+ keypair: X25519Keypair;
40
+ reciever: EncryptionTemplateMaybeEncrypted;
41
+ };
42
+ };
43
+ export declare class Log<T> {
44
+ private _id;
45
+ private _sortFn;
46
+ private _storage;
47
+ private _hlc;
48
+ private _identity;
49
+ private _entryIndex;
50
+ private _headsIndex;
51
+ private _values;
52
+ private _nextsIndex;
53
+ private _keychain?;
54
+ private _encoding;
55
+ private _trim;
56
+ private _entryCache;
57
+ private _canAppend?;
58
+ private _onChange?;
59
+ private _closed;
60
+ private _memory?;
61
+ private _joining;
62
+ constructor(properties?: {
63
+ id?: Uint8Array;
64
+ });
65
+ open(store: Blocks, identity: Identity, options?: LogOptions<T>): Promise<void>;
66
+ private _idString;
67
+ get idString(): string;
68
+ static createIdString(id: Uint8Array): string;
69
+ get id(): Uint8Array;
70
+ set id(id: Uint8Array);
71
+ /**
72
+ * Returns the length of the log.
73
+ */
74
+ get length(): number;
75
+ get values(): Values<T>;
76
+ /**
77
+ * Checks if a entry is part of the log
78
+ * @param {string} hash The hash of the entry
79
+ * @returns {boolean}
80
+ */
81
+ has(cid: string): boolean;
82
+ /**
83
+ * Get all entries sorted. Don't use this method anywhere where performance matters
84
+ */
85
+ toArray(): Promise<Entry<T>[]>;
86
+ /**
87
+ * Returns the head index
88
+ */
89
+ get headsIndex(): HeadsIndex<T>;
90
+ get memory(): SimpleLevel | undefined;
91
+ /**
92
+ * Don't use this anywhere performance matters
93
+ */
94
+ getHeads(): Promise<Entry<T>[]>;
95
+ /**
96
+ * Returns an array of Entry objects that reference entries which
97
+ * are not in the log currently.
98
+ * @returns {Array<Entry<T>>}
99
+ */
100
+ getTails(): Promise<Entry<T>[]>;
101
+ /**
102
+ * Returns an array of hashes that are referenced by entries which
103
+ * are not in the log currently.
104
+ * @returns {Array<string>} Array of hashes
105
+ */
106
+ getTailHashes(): Promise<string[]>;
107
+ /**
108
+ * Get local HLC
109
+ */
110
+ get hlc(): HLC;
111
+ get identity(): Identity;
112
+ get storage(): Blocks;
113
+ get nextsIndex(): Map<string, Set<string>>;
114
+ get entryIndex(): EntryIndex<T>;
115
+ get keychain(): Keychain | undefined;
116
+ get encoding(): Encoding<T>;
117
+ get sortFn(): Sorting.ISortFunction;
118
+ get closed(): boolean;
119
+ /**
120
+ * Set the identity for the log
121
+ * @param {Identity} [identity] The identity to be set
122
+ */
123
+ setIdentity(identity: Identity): void;
124
+ /**
125
+ * Find an entry.
126
+ * @param {string} [hash] The hashes of the entry
127
+ */
128
+ get(hash: string, options?: {
129
+ timeout?: number;
130
+ }): Promise<Entry<T> | undefined>;
131
+ traverse(rootEntries: Entry<T>[], amount?: number, endHash?: string): Promise<{
132
+ [key: string]: Entry<T>;
133
+ }>;
134
+ getReferenceSamples(from: Entry<T>, options?: {
135
+ pointerCount?: number;
136
+ memoryLimit?: number;
137
+ }): Promise<Entry<T>[]>;
138
+ /**
139
+ * Append an entry to the log.
140
+ * @param {Entry} entry Entry to add
141
+ * @return {Log} New Log containing the appended value
142
+ */
143
+ append(data: T, options?: AppendOptions<T>): Promise<{
144
+ entry: Entry<T>;
145
+ removed: Entry<T>[];
146
+ }>;
147
+ reset(entries: Entry<T>[], heads?: (string | Entry<T>)[]): Promise<void>;
148
+ remove(entry: Entry<T> | Entry<T>[], options?: {
149
+ recursively?: boolean;
150
+ }): Promise<Change<T>>;
151
+ iterator(options?: {
152
+ from?: "tail" | "head";
153
+ amount?: number;
154
+ }): IterableIterator<string>;
155
+ trim(option?: TrimOptions | undefined): Promise<Entry<T>[]>;
156
+ /**
157
+ *
158
+ * @param entries
159
+ * @returns change
160
+ */
161
+ join(entriesOrLog: (string | Entry<T> | EntryWithRefs<T>)[] | Log<T>, options?: {
162
+ verifySignatures?: boolean;
163
+ trim?: TrimOptions;
164
+ timeout?: number;
165
+ } & CacheUpdateOptions): Promise<void>;
166
+ private joinEntry;
167
+ private processEntry;
168
+ deleteRecursively(from: Entry<any> | Entry<any>[], skipFirst?: boolean): Promise<Entry<T>[]>;
169
+ delete(entry: Entry<any>): Promise<void>;
170
+ /**
171
+ * Returns the log entries as a formatted string.
172
+ * @returns {string}
173
+ * @example
174
+ * two
175
+ * └─one
176
+ * └─three
177
+ */
178
+ toString(payloadMapper?: (payload: Payload<T>) => string): Promise<string>;
179
+ idle(): Promise<void>;
180
+ close(): Promise<void>;
181
+ drop(): Promise<void>;
182
+ load(opts?: ({
183
+ fetchEntryTimeout?: number;
184
+ } & ({} | {
185
+ heads?: true;
186
+ })) & {
187
+ reload: boolean;
188
+ }): Promise<void>;
189
+ static fromEntry<T>(store: Blocks, identity: Identity, entryOrHash: string | string[] | Entry<T> | Entry<T>[], options?: {
190
+ id?: Uint8Array;
191
+ timeout?: number;
192
+ } & LogOptions<T>): Promise<Log<T>>;
193
+ /**
194
+ * Find heads from a collection of entries.
195
+ *
196
+ * Finds entries that are the heads of this collection,
197
+ * ie. entries that are not referenced by other entries.
198
+ *
199
+ * @param {Array<Entry<T>>} entries Entries to search heads from
200
+ * @returns {Array<Entry<T>>}
201
+ */
202
+ static findHeads<T>(entries: Entry<T>[]): Entry<T>[];
203
+ static findTails<T>(entries: Entry<T>[]): Entry<T>[];
204
+ static findTailHashes(entries: Entry<any>[]): string[];
205
+ }