@peerbit/shared-log 10.0.5 → 10.0.6-6d2cda9

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 (45) hide show
  1. package/dist/benchmark/get-samples.js +1 -1
  2. package/dist/benchmark/get-samples.js.map +1 -1
  3. package/dist/benchmark/utils.js +1 -1
  4. package/dist/benchmark/utils.js.map +1 -1
  5. package/dist/src/index.d.ts +15 -10
  6. package/dist/src/index.d.ts.map +1 -1
  7. package/dist/src/index.js +144 -68
  8. package/dist/src/index.js.map +1 -1
  9. package/dist/src/ranges.d.ts +95 -11
  10. package/dist/src/ranges.d.ts.map +1 -1
  11. package/dist/src/ranges.js +437 -83
  12. package/dist/src/ranges.js.map +1 -1
  13. package/dist/src/replication-domain-hash.d.ts +2 -2
  14. package/dist/src/replication-domain-hash.d.ts.map +1 -1
  15. package/dist/src/replication-domain-hash.js +2 -17
  16. package/dist/src/replication-domain-hash.js.map +1 -1
  17. package/dist/src/replication-domain-time.d.ts +7 -2
  18. package/dist/src/replication-domain-time.d.ts.map +1 -1
  19. package/dist/src/replication-domain-time.js +7 -12
  20. package/dist/src/replication-domain-time.js.map +1 -1
  21. package/dist/src/replication-domain.d.ts +3 -20
  22. package/dist/src/replication-domain.d.ts.map +1 -1
  23. package/dist/src/replication-domain.js +0 -33
  24. package/dist/src/replication-domain.js.map +1 -1
  25. package/dist/src/sync/rateless-iblt.d.ts +0 -1
  26. package/dist/src/sync/rateless-iblt.d.ts.map +1 -1
  27. package/dist/src/sync/rateless-iblt.js +1 -3
  28. package/dist/src/sync/rateless-iblt.js.map +1 -1
  29. package/package.json +70 -73
  30. package/src/index.ts +211 -110
  31. package/src/ranges.ts +669 -127
  32. package/src/replication-domain-hash.ts +16 -29
  33. package/src/replication-domain-time.ts +46 -40
  34. package/src/replication-domain.ts +7 -59
  35. package/src/sync/rateless-iblt.ts +1 -4
  36. package/dist/src/sync/wasm-init.browser.d.ts +0 -1
  37. package/dist/src/sync/wasm-init.browser.d.ts.map +0 -1
  38. package/dist/src/sync/wasm-init.browser.js +0 -3
  39. package/dist/src/sync/wasm-init.browser.js.map +0 -1
  40. package/dist/src/sync/wasm-init.d.ts +0 -2
  41. package/dist/src/sync/wasm-init.d.ts.map +0 -1
  42. package/dist/src/sync/wasm-init.js +0 -13
  43. package/dist/src/sync/wasm-init.js.map +0 -1
  44. package/src/sync/wasm-init.browser.ts +0 -1
  45. package/src/sync/wasm-init.ts +0 -14
@@ -6,25 +6,10 @@ import { type EntryReplicated } from "./ranges.js";
6
6
  import {
7
7
  type Log,
8
8
  type ReplicationDomain,
9
+ type ReplicationDomainConstructor,
9
10
  type ReplicationDomainMapper,
10
11
  } from "./replication-domain.js";
11
12
 
12
- /* const hashToU32 = (hash: Uint8Array) => {
13
- const seedNumber = new BinaryReader(
14
- hash.subarray(hash.length - 4, hash.length),
15
- ).u32();
16
- return seedNumber;
17
- };
18
-
19
-
20
- const hashToU64 = (hash: Uint8Array): bigint => {
21
- const seedNumber = new BinaryReader(
22
- hash.subarray(hash.length - 4, hash.length), //
23
- ).u64();
24
- return seedNumber;
25
- };
26
- */
27
-
28
13
  const hashTransformer = <R extends "u32" | "u64">(
29
14
  resolution: R,
30
15
  ): ReplicationDomainMapper<any, R> => {
@@ -54,17 +39,19 @@ export type ReplicationDomainHash<R extends "u32" | "u64"> = ReplicationDomain<
54
39
  R
55
40
  >;
56
41
 
57
- export const createReplicationDomainHash = <R extends "u32" | "u64">(
58
- resolution: R,
59
- ): ReplicationDomainHash<R> => {
60
- return {
61
- resolution,
62
- type: "hash",
63
- fromEntry: hashTransformer<R>(resolution),
64
- fromArgs: async (args: undefined, log: Log) => {
65
- return {
66
- offset: log.node.identity.publicKey,
67
- };
68
- },
42
+ export const createReplicationDomainHash =
43
+ <R extends "u32" | "u64">(
44
+ resolution: R,
45
+ ): ReplicationDomainConstructor<ReplicationDomainHash<R>> =>
46
+ (log: Log) => {
47
+ return {
48
+ resolution,
49
+ type: "hash",
50
+ fromEntry: hashTransformer<R>(resolution),
51
+ fromArgs: async (args: undefined) => {
52
+ return {
53
+ offset: log.node.identity.publicKey,
54
+ };
55
+ },
56
+ };
69
57
  };
70
- };
@@ -1,7 +1,12 @@
1
1
  import type { ShallowOrFullEntry } from "@peerbit/log";
2
- import { type EntryReplicated } from "./ranges.js";
3
2
  import {
3
+ type EntryReplicated,
4
+ type ReplicationRangeIndexable,
5
+ } from "./ranges.js";
6
+ import {
7
+ type Log,
4
8
  type ReplicationDomain,
9
+ type ReplicationDomainConstructor,
5
10
  type ReplicationDomainMapper,
6
11
  } from "./replication-domain.js";
7
12
 
@@ -41,46 +46,47 @@ export type ReplicationDomainTime = ReplicationDomain<TimeRange, any, "u32"> & {
41
46
  fromDuration: (duration: number) => number;
42
47
  };
43
48
 
44
- export const createReplicationDomainTime = (
45
- origin: Date,
46
- unit: TimeUnit = "milliseconds",
47
- ): ReplicationDomainTime => {
48
- const originScaled = +origin * scalarMilliToUnit[unit];
49
- const fromMilliToUnit = scalarMilliToUnit[unit];
50
- const fromTime = (time: number | Date): number => {
51
- return (
52
- (typeof time === "number" ? time : +time * fromMilliToUnit) - originScaled
53
- );
54
- };
49
+ export const createReplicationDomainTime =
50
+ (properties: {
51
+ origin?: Date;
52
+ unit?: TimeUnit;
53
+ canMerge?: (
54
+ from: ReplicationRangeIndexable<"u32">,
55
+ into: ReplicationRangeIndexable<"u32">,
56
+ ) => boolean;
57
+ }): ReplicationDomainConstructor<ReplicationDomainTime> =>
58
+ (log: Log) => {
59
+ const origin = properties.origin || new Date();
60
+ const unit = properties.unit || "milliseconds";
61
+ const originScaled = +origin * scalarMilliToUnit[unit];
62
+ const fromMilliToUnit = scalarMilliToUnit[unit];
63
+ const fromTime = (time: number | Date): number => {
64
+ return (
65
+ (typeof time === "number" ? time : +time * fromMilliToUnit) -
66
+ originScaled
67
+ );
68
+ };
55
69
 
56
- const fromDuration = (duration: number): number => {
57
- return duration;
58
- };
59
- return {
60
- resolution: "u32",
61
- type: "time",
62
- fromTime,
63
- fromDuration,
64
- fromEntry: fromEntry(origin, unit),
65
- fromArgs: async (args: TimeRange | undefined, log) => {
66
- if (!args) {
70
+ const fromDuration = (duration: number): number => {
71
+ return duration;
72
+ };
73
+ return {
74
+ resolution: "u32",
75
+ type: "time",
76
+ fromTime,
77
+ fromDuration,
78
+ fromEntry: fromEntry(origin, unit),
79
+ fromArgs: async (args: TimeRange | undefined) => {
80
+ if (!args) {
81
+ return {
82
+ offset: log.node.identity.publicKey,
83
+ };
84
+ }
67
85
  return {
68
- offset: log.node.identity.publicKey,
86
+ offset: fromTime(args.from),
87
+ length: fromDuration(args.to - args.from),
69
88
  };
70
- }
71
- return {
72
- offset: fromTime(args.from),
73
- length: fromDuration(args.to - args.from),
74
- };
75
- /* roleAge = roleAge ?? (await log.getDefaultMinRoleAge());
76
- const ranges = await getCoverSet(
77
- log.replicationIndex,
78
- roleAge,
79
- fromTime(args.from),
80
- fromDuration(args.to - args.from),
81
- undefined,
82
- );
83
- return [...ranges]; */
84
- },
89
+ },
90
+ canMerge: properties.canMerge,
91
+ };
85
92
  };
86
- };
@@ -1,7 +1,6 @@
1
1
  import type { PublicSignKey } from "@peerbit/crypto";
2
2
  import { type Index } from "@peerbit/indexer-interface";
3
3
  import type { Entry, ShallowEntry } from "@peerbit/log";
4
- import { debounceAccumulator } from "./debounce.js";
5
4
  import type { ReplicationRangeIndexable } from "./index.js";
6
5
  import type { NumberFromType } from "./integers.js";
7
6
  import type { EntryReplicated } from "./ranges.js";
@@ -31,63 +30,6 @@ type CoverRange<T extends number | bigint> = {
31
30
  offset: T | PublicSignKey;
32
31
  length?: T;
33
32
  };
34
- export type ReplicationChanges = ReplicationChange[];
35
- export type ReplicationChange =
36
- | {
37
- type: "added";
38
- range: ReplicationRangeIndexable<any>;
39
- }
40
- | {
41
- type: "removed";
42
- range: ReplicationRangeIndexable<any>;
43
- }
44
- | {
45
- type: "updated";
46
- range: ReplicationRangeIndexable<any>;
47
- prev: ReplicationRangeIndexable<any>;
48
- };
49
-
50
- export const mergeReplicationChanges = (
51
- changes: ReplicationChanges | ReplicationChanges[],
52
- ): ReplicationChanges => {
53
- let first = changes[0];
54
- if (!Array.isArray(first)) {
55
- return changes as ReplicationChanges;
56
- }
57
- return (changes as ReplicationChanges[]).flat();
58
- };
59
-
60
- export const debounceAggregationChanges = (
61
- fn: (changeOrChanges: ReplicationChange[]) => void,
62
- delay: number,
63
- ) => {
64
- return debounceAccumulator(
65
- (result) => {
66
- return fn([...result.values()]);
67
- },
68
- () => {
69
- let aggregated: Map<string, ReplicationChange> = new Map();
70
- return {
71
- add: (change: ReplicationChange) => {
72
- const prev = aggregated.get(change.range.idString);
73
- if (prev) {
74
- if (prev.range.timestamp < change.range.timestamp) {
75
- aggregated.set(change.range.idString, change);
76
- }
77
- } else {
78
- aggregated.set(change.range.idString, change);
79
- }
80
- },
81
- delete: (key: string) => {
82
- aggregated.delete(key);
83
- },
84
- size: () => aggregated.size,
85
- value: aggregated,
86
- };
87
- },
88
- delay,
89
- );
90
- };
91
33
 
92
34
  export type ReplicationDomain<Args, T, R extends "u32" | "u64"> = {
93
35
  resolution: R;
@@ -95,9 +37,15 @@ export type ReplicationDomain<Args, T, R extends "u32" | "u64"> = {
95
37
  fromEntry: ReplicationDomainMapper<T, R>;
96
38
  fromArgs: (
97
39
  args: Args | undefined,
98
- log: Log,
99
40
  ) => Promise<CoverRange<NumberFromType<R>>> | CoverRange<NumberFromType<R>>;
41
+ canMerge?: (
42
+ from: ReplicationRangeIndexable<R>,
43
+ into: ReplicationRangeIndexable<R>,
44
+ ) => boolean;
100
45
  };
101
46
 
47
+ export type ReplicationDomainConstructor<
48
+ D extends ReplicationDomain<any, any, any>,
49
+ > = (log: Log) => D;
102
50
  export type ExtractDomainArgs<T> =
103
51
  T extends ReplicationDomain<infer Args, any, any> ? Args : never;
@@ -3,7 +3,7 @@ import { Cache } from "@peerbit/cache";
3
3
  import { type PublicSignKey, randomBytes, toBase64 } from "@peerbit/crypto";
4
4
  import { type Index } from "@peerbit/indexer-interface";
5
5
  import type { Entry, Log } from "@peerbit/log";
6
- import init, { DecoderWrapper, EncoderWrapper } from "@peerbit/riblt";
6
+ import { DecoderWrapper, EncoderWrapper } from "@peerbit/riblt";
7
7
  import type { RPC, RequestContext } from "@peerbit/rpc";
8
8
  import { SilentDelivery } from "@peerbit/stream-interface";
9
9
  import type { SyncableKey, Syncronizer } from ".";
@@ -16,9 +16,6 @@ import {
16
16
  matchEntriesInRangeQuery,
17
17
  } from "../ranges.js";
18
18
  import { SimpleSyncronizer } from "./simple.js";
19
- import "./wasm-init.js";
20
-
21
- await init();
22
19
 
23
20
  type NumberOrBigint = number | bigint;
24
21
 
@@ -1 +0,0 @@
1
- //# sourceMappingURL=wasm-init.browser.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wasm-init.browser.d.ts","sourceRoot":"","sources":["../../../src/sync/wasm-init.browser.ts"],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
- "use strict";
2
- // nothing to do since 'fetch' works as expected in the browsere
3
- //# sourceMappingURL=wasm-init.browser.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wasm-init.browser.js","sourceRoot":"","sources":["../../../src/sync/wasm-init.browser.ts"],"names":[],"mappings":";AAAA,gEAAgE"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=wasm-init.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wasm-init.d.ts","sourceRoot":"","sources":["../../../src/sync/wasm-init.ts"],"names":[],"mappings":""}
@@ -1,13 +0,0 @@
1
- // Override globalThis.fetch to intercept .wasm requests
2
- import { readFile } from "fs/promises";
3
- const defaultFetch = globalThis.fetch.bind(globalThis);
4
- globalThis.fetch = async (url, options) => {
5
- // If you have multiple wasm files, you might use some logic to handle them.
6
- // Here, we assume any request ending in `.wasm` is local on disk at the same path.
7
- if (url.toString().endsWith(".wasm")) {
8
- // Return a NodeResponse that looks enough like a fetch Response
9
- return readFile(url);
10
- }
11
- return defaultFetch(url, options);
12
- };
13
- //# sourceMappingURL=wasm-init.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"wasm-init.js","sourceRoot":"","sources":["../../../src/sync/wasm-init.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,MAAM,YAAY,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;AACtD,UAAU,CAAC,KAAa,GAAG,KAAK,EAAE,GAAQ,EAAE,OAAY,EAAE,EAAE;IAC5D,4EAA4E;IAC5E,mFAAmF;IACnF,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACtC,gEAAgE;QAChE,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IAED,OAAO,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACnC,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- // nothing to do since 'fetch' works as expected in the browsere
@@ -1,14 +0,0 @@
1
- // Override globalThis.fetch to intercept .wasm requests
2
- import { readFile } from "fs/promises";
3
-
4
- const defaultFetch = globalThis.fetch.bind(globalThis);
5
- (globalThis.fetch as any) = async (url: any, options: any) => {
6
- // If you have multiple wasm files, you might use some logic to handle them.
7
- // Here, we assume any request ending in `.wasm` is local on disk at the same path.
8
- if (url.toString().endsWith(".wasm")) {
9
- // Return a NodeResponse that looks enough like a fetch Response
10
- return readFile(url);
11
- }
12
-
13
- return defaultFetch(url, options);
14
- };