Skip to content

Commit 96ec51f

Browse files
chore: address comments, reduce diff
1 parent 3e3c511 commit 96ec51f

File tree

5 files changed

+36
-67
lines changed

5 files changed

+36
-67
lines changed

packages/sds/src/message_channel/mem_local_history.spec.ts renamed to packages/sds/src/message_channel/local_history.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { expect } from "chai";
22

3-
import { MemLocalHistory } from "./mem_local_history.js";
3+
import { LocalHistory } from "./local_history.js";
44
import { ContentMessage } from "./message.js";
55

6-
describe("MemLocalHistory", () => {
6+
describe("LocalHistory", () => {
77
it("Cap max size when messages are pushed one at a time", () => {
88
const maxSize = 2;
99

10-
const hist = new MemLocalHistory({ maxSize: maxSize });
10+
const hist = new LocalHistory({ maxSize });
1111

1212
hist.push(
1313
new ContentMessage("1", "c", "a", [], 1n, undefined, new Uint8Array([1]))
@@ -31,7 +31,7 @@ describe("MemLocalHistory", () => {
3131
it("Cap max size when a pushed array is exceeding the cap", () => {
3232
const maxSize = 2;
3333

34-
const hist = new MemLocalHistory({ maxSize: maxSize });
34+
const hist = new LocalHistory({ maxSize });
3535

3636
hist.push(
3737
new ContentMessage("1", "c", "a", [], 1n, undefined, new Uint8Array([1]))

packages/sds/src/message_channel/mem_local_history.ts renamed to packages/sds/src/message_channel/local_history.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Logger } from "@waku/utils";
22
import _ from "lodash";
33

44
import { type ChannelId, ContentMessage, isContentMessage } from "./message.js";
5+
import { ILocalHistory } from "./message_channel.js";
56
import { PersistentStorage } from "./persistent_storage.js";
67

78
export const DEFAULT_MAX_LENGTH = 10_000;
@@ -19,44 +20,15 @@ export const DEFAULT_MAX_LENGTH = 10_000;
1920
* If an array of items longer than `maxLength` is pushed, dropping will happen
2021
* at next push.
2122
*/
22-
export interface ILocalHistory {
23-
length: number;
24-
push(...items: ContentMessage[]): number;
25-
some(
26-
predicate: (
27-
value: ContentMessage,
28-
index: number,
29-
array: ContentMessage[]
30-
) => unknown,
31-
thisArg?: any
32-
): boolean;
33-
slice(start?: number, end?: number): ContentMessage[];
34-
find(
35-
predicate: (
36-
value: ContentMessage,
37-
index: number,
38-
obj: ContentMessage[]
39-
) => unknown,
40-
thisArg?: any
41-
): ContentMessage | undefined;
42-
findIndex(
43-
predicate: (
44-
value: ContentMessage,
45-
index: number,
46-
obj: ContentMessage[]
47-
) => unknown,
48-
thisArg?: any
49-
): number;
50-
}
5123

52-
export type MemLocalHistoryOptions = {
24+
export type LocalHistoryOptions = {
5325
storage?: ChannelId | PersistentStorage;
5426
maxSize?: number;
5527
};
5628

5729
const log = new Logger("sds:local-history");
5830

59-
export class MemLocalHistory implements ILocalHistory {
31+
export class LocalHistory implements ILocalHistory {
6032
private items: ContentMessage[] = [];
6133
private readonly storage?: PersistentStorage;
6234
private readonly maxSize: number;
@@ -68,7 +40,7 @@ export class MemLocalHistory implements ILocalHistory {
6840
* - storage: Optional persistent storage backend for message persistence or channelId to use with PersistentStorage.
6941
* - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH.
7042
*/
71-
public constructor(opts: MemLocalHistoryOptions = {}) {
43+
public constructor(opts: LocalHistoryOptions = {}) {
7244
const { storage, maxSize } = opts;
7345
this.maxSize = maxSize ?? DEFAULT_MAX_LENGTH;
7446
if (storage instanceof PersistentStorage) {

packages/sds/src/message_channel/message_channel.spec.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { expect } from "chai";
44
import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
55

66
import { MessageChannelEvent } from "./events.js";
7-
import { MemLocalHistory } from "./mem_local_history.js";
8-
import { ILocalHistory } from "./mem_local_history.js";
7+
import { LocalHistory } from "./local_history.js";
98
import {
109
ContentMessage,
1110
HistoryEntry,
@@ -25,20 +24,15 @@ const callback = (_message: Message): Promise<{ success: boolean }> => {
2524
};
2625

2726
/**
28-
* Test helper to create a MessageChannel with MemLocalHistory.
27+
* Test helper to create a MessageChannel with LocalHistory.
2928
* This avoids localStorage pollution in tests and tests core functionality.
3029
*/
3130
const createTestChannel = (
3231
channelId: string,
3332
senderId: string,
3433
options: MessageChannelOptions = {}
3534
): MessageChannel => {
36-
return new MessageChannel(
37-
channelId,
38-
senderId,
39-
options,
40-
new MemLocalHistory()
41-
);
35+
return new MessageChannel(channelId, senderId, options, new LocalHistory());
4236
};
4337

4438
const getBloomFilter = (channel: MessageChannel): DefaultBloomFilter => {
@@ -117,11 +111,11 @@ describe("MessageChannel", function () {
117111
const expectedTimestamp = channelA["lamportTimestamp"] + 1n;
118112
const messageId = MessageChannel.getMessageId(payload);
119113
await sendMessage(channelA, payload, callback);
120-
const messageIdLog = channelA["localHistory"] as ILocalHistory;
114+
const messageIdLog = channelA["localHistory"] as LocalHistory;
121115
expect(messageIdLog.length).to.equal(1);
122116
expect(
123117
messageIdLog.some(
124-
(log) =>
118+
(log: ContentMessage) =>
125119
log.lamportTimestamp === expectedTimestamp &&
126120
log.messageId === messageId
127121
)
@@ -138,7 +132,7 @@ describe("MessageChannel", function () {
138132
return { success: true, retrievalHint: testRetrievalHint };
139133
});
140134

141-
const localHistory = channelA["localHistory"] as ILocalHistory;
135+
const localHistory = channelA["localHistory"] as LocalHistory;
142136
expect(localHistory.length).to.equal(1);
143137

144138
// Find the message in local history

packages/sds/src/message_channel/message_channel.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
77

88
import { Command, Handlers, ParamsByAction, Task } from "./command_queue.js";
99
import { MessageChannelEvent, MessageChannelEvents } from "./events.js";
10-
import { ILocalHistory, MemLocalHistory } from "./mem_local_history.js";
10+
import { LocalHistory } from "./local_history.js";
1111
import {
1212
ChannelId,
1313
ContentMessage,
@@ -23,8 +23,6 @@ import {
2323
} from "./message.js";
2424
import { RepairConfig, RepairManager } from "./repair/repair.js";
2525

26-
export type { ILocalHistory };
27-
2826
export const DEFAULT_BLOOM_FILTER_OPTIONS = {
2927
capacity: 10000,
3028
errorRate: 0.001
@@ -40,6 +38,11 @@ const DEFAULT_POSSIBLE_ACKS_THRESHOLD = 2;
4038

4139
const log = new Logger("sds:message-channel");
4240

41+
export type ILocalHistory = Pick<
42+
Array<ContentMessage>,
43+
"some" | "push" | "slice" | "find" | "length" | "findIndex"
44+
>;
45+
4346
export interface MessageChannelOptions {
4447
causalHistorySize?: number;
4548
/**
@@ -115,7 +118,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
115118
this.possibleAcks = new Map();
116119
this.incomingBuffer = [];
117120
this.localHistory =
118-
localHistory ?? new MemLocalHistory({ storage: channelId });
121+
localHistory ?? new LocalHistory({ storage: channelId });
119122
this.causalHistorySize =
120123
options.causalHistorySize ?? DEFAULT_CAUSAL_HISTORY_SIZE;
121124
// TODO: this should be determined based on the bloom filter parameters and number of hashes

packages/sds/src/message_channel/persistent_storage.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from "chai";
22

3-
import { MemLocalHistory } from "./mem_local_history.js";
3+
import { LocalHistory } from "./local_history.js";
44
import { ContentMessage } from "./message.js";
55
import { HistoryStorage, PersistentStorage } from "./persistent_storage.js";
66

@@ -14,11 +14,11 @@ describe("PersistentStorage", () => {
1414

1515
expect(persistentStorage).to.not.be.undefined;
1616

17-
const history1 = new MemLocalHistory({ storage: persistentStorage });
17+
const history1 = new LocalHistory({ storage: persistentStorage });
1818
history1.push(createMessage("msg-1", 1));
1919
history1.push(createMessage("msg-2", 2));
2020

21-
const history2 = new MemLocalHistory({ storage: persistentStorage });
21+
const history2 = new LocalHistory({ storage: persistentStorage });
2222

2323
expect(history2.length).to.equal(2);
2424
expect(history2.slice(0).map((msg) => msg.messageId)).to.deep.equal([
@@ -28,13 +28,13 @@ describe("PersistentStorage", () => {
2828
});
2929

3030
it("uses in-memory only when no storage is provided", () => {
31-
const history = new MemLocalHistory({ maxSize: 100 });
31+
const history = new LocalHistory({ maxSize: 100 });
3232
history.push(createMessage("msg-3", 3));
3333

3434
expect(history.length).to.equal(1);
3535
expect(history.slice(0)[0].messageId).to.equal("msg-3");
3636

37-
const history2 = new MemLocalHistory({ maxSize: 100 });
37+
const history2 = new LocalHistory({ maxSize: 100 });
3838
expect(history2.length).to.equal(0);
3939
});
4040

@@ -44,7 +44,7 @@ describe("PersistentStorage", () => {
4444
storage.setItem("waku:sds:history:channel-1", "{ invalid json }");
4545

4646
const persistentStorage = PersistentStorage.create(channelId, storage);
47-
const history = new MemLocalHistory({ storage: persistentStorage });
47+
const history = new LocalHistory({ storage: persistentStorage });
4848

4949
expect(history.length).to.equal(0);
5050

@@ -58,8 +58,8 @@ describe("PersistentStorage", () => {
5858
const storage1 = PersistentStorage.create("channel-1", storage);
5959
const storage2 = PersistentStorage.create("channel-2", storage);
6060

61-
const history1 = new MemLocalHistory({ storage: storage1 });
62-
const history2 = new MemLocalHistory({ storage: storage2 });
61+
const history1 = new LocalHistory({ storage: storage1 });
62+
const history2 = new LocalHistory({ storage: storage2 });
6363

6464
history1.push(createMessage("msg-1", 1));
6565
history2.push(createMessage("msg-2", 2));
@@ -77,7 +77,7 @@ describe("PersistentStorage", () => {
7777
it("saves messages after each push", () => {
7878
const storage = new MemoryStorage();
7979
const persistentStorage = PersistentStorage.create(channelId, storage);
80-
const history = new MemLocalHistory({ storage: persistentStorage });
80+
const history = new LocalHistory({ storage: persistentStorage });
8181

8282
expect(storage.getItem("waku:sds:history:channel-1")).to.be.null;
8383

@@ -93,14 +93,14 @@ describe("PersistentStorage", () => {
9393
it("loads messages on initialization", () => {
9494
const storage = new MemoryStorage();
9595
const persistentStorage1 = PersistentStorage.create(channelId, storage);
96-
const history1 = new MemLocalHistory({ storage: persistentStorage1 });
96+
const history1 = new LocalHistory({ storage: persistentStorage1 });
9797

9898
history1.push(createMessage("msg-1", 1));
9999
history1.push(createMessage("msg-2", 2));
100100
history1.push(createMessage("msg-3", 3));
101101

102102
const persistentStorage2 = PersistentStorage.create(channelId, storage);
103-
const history2 = new MemLocalHistory({ storage: persistentStorage2 });
103+
const history2 = new LocalHistory({ storage: persistentStorage2 });
104104

105105
expect(history2.length).to.equal(3);
106106
expect(history2.slice(0).map((m) => m.messageId)).to.deep.equal([
@@ -134,11 +134,11 @@ describe("PersistentStorage", () => {
134134

135135
it("persists and restores messages with channelId", () => {
136136
const testChannelId = `test-${Date.now()}`;
137-
const history1 = new MemLocalHistory({ storage: testChannelId });
137+
const history1 = new LocalHistory({ storage: testChannelId });
138138
history1.push(createMessage("msg-1", 1));
139139
history1.push(createMessage("msg-2", 2));
140140

141-
const history2 = new MemLocalHistory({ storage: testChannelId });
141+
const history2 = new LocalHistory({ storage: testChannelId });
142142

143143
expect(history2.length).to.equal(2);
144144
expect(history2.slice(0).map((msg) => msg.messageId)).to.deep.equal([
@@ -152,11 +152,11 @@ describe("PersistentStorage", () => {
152152
it("auto-uses localStorage when channelId is provided", () => {
153153
const testChannelId = `auto-storage-${Date.now()}`;
154154

155-
const history = new MemLocalHistory({ storage: testChannelId });
155+
const history = new LocalHistory({ storage: testChannelId });
156156
history.push(createMessage("msg-auto-1", 1));
157157
history.push(createMessage("msg-auto-2", 2));
158158

159-
const history2 = new MemLocalHistory({ storage: testChannelId });
159+
const history2 = new LocalHistory({ storage: testChannelId });
160160
expect(history2.length).to.equal(2);
161161
expect(history2.slice(0).map((m) => m.messageId)).to.deep.equal([
162162
"msg-auto-1",

0 commit comments

Comments
 (0)