@peerbit/shared-log 10.0.6 → 10.1.0

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.
@@ -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;