Skip to content

Commit ab6d3cf

Browse files
minor fixes
1 parent e396c3b commit ab6d3cf

File tree

4 files changed

+28
-30
lines changed

4 files changed

+28
-30
lines changed

packages/sds/src/message_channel/local_history.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ import { Storage } from "./storage/index.js";
66

77
export const DEFAULT_MAX_LENGTH = 10_000;
88

9+
/**
10+
* Options for the LocalHistory constructor.
11+
* @param storage - The storage to use for the local history.
12+
* - prefix - The prefix for the storage.
13+
* - customInstance - The custom storage instance to use.
14+
* @param maxSize - The maximum number of messages to store.
15+
*/
16+
export type LocalHistoryOptions = {
17+
storage?: {
18+
prefix?: string;
19+
customInstance?: Storage;
20+
};
21+
maxSize?: number;
22+
};
23+
24+
const log = new Logger("sds:local-history");
25+
926
/**
1027
* In-Memory implementation of a local history of messages.
1128
*
@@ -19,30 +36,11 @@ export const DEFAULT_MAX_LENGTH = 10_000;
1936
* If an array of items longer than `maxLength` is pushed, dropping will happen
2037
* at next push.
2138
*/
22-
23-
export type LocalHistoryOptions = {
24-
storage?: {
25-
prefix?: string;
26-
customInstance?: Storage;
27-
};
28-
maxSize?: number;
29-
};
30-
31-
const log = new Logger("sds:local-history");
32-
3339
export class LocalHistory {
3440
private items: ContentMessage[] = [];
3541
private readonly storage?: Storage;
3642
private readonly maxSize: number;
3743

38-
/**
39-
* Construct a new in-memory local history.
40-
*
41-
* @param opts Configuration object.
42-
* - storagePrefix: Optional prefix for persistent storage (creates Storage if provided).
43-
* - storage: Optional explicit Storage instance.
44-
* - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH.
45-
*/
4644
public constructor(opts: LocalHistoryOptions = {}) {
4745
const { storage, maxSize } = opts;
4846
const { prefix, customInstance } = storage ?? {};

packages/sds/src/message_channel/message_channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
113113
this.possibleAcks = new Map();
114114
this.incomingBuffer = [];
115115
this.localHistory =
116-
localHistory ?? new LocalHistory({ storagePrefix: channelId });
116+
localHistory ?? new LocalHistory({ storage: { prefix: channelId } });
117117
this.causalHistorySize =
118118
options.causalHistorySize ?? DEFAULT_CAUSAL_HISTORY_SIZE;
119119
// 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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ describe("Storage", () => {
1818
});
1919

2020
it("persists and restores messages", () => {
21-
const history1 = new LocalHistory({ storagePrefix: channelId });
21+
const history1 = new LocalHistory({ storage: { prefix: channelId } });
2222
history1.push(createMessage("msg-1", 1));
2323
history1.push(createMessage("msg-2", 2));
2424

25-
const history2 = new LocalHistory({ storagePrefix: channelId });
25+
const history2 = new LocalHistory({ storage: { prefix: channelId } });
2626

2727
expect(history2.length).to.equal(2);
2828
expect(history2.slice(0).map((msg) => msg.messageId)).to.deep.equal([
@@ -34,15 +34,15 @@ describe("Storage", () => {
3434
it("handles corrupt data gracefully", () => {
3535
localStorage.setItem(`waku:sds:storage:${channelId}`, "{ invalid json }");
3636

37-
const history = new LocalHistory({ storagePrefix: channelId });
37+
const history = new LocalHistory({ storage: { prefix: channelId } });
3838
expect(history.length).to.equal(0);
3939
// Corrupt data is removed
4040
expect(localStorage.getItem(`waku:sds:storage:${channelId}`)).to.be.null;
4141
});
4242

4343
it("isolates history by channel ID", () => {
44-
const history1 = new LocalHistory({ storagePrefix: "channel-1" });
45-
const history2 = new LocalHistory({ storagePrefix: "channel-2" });
44+
const history1 = new LocalHistory({ storage: { prefix: "channel-1" } });
45+
const history2 = new LocalHistory({ storage: { prefix: "channel-2" } });
4646

4747
history1.push(createMessage("msg-1", 1));
4848
history2.push(createMessage("msg-2", 2));
@@ -57,7 +57,7 @@ describe("Storage", () => {
5757
});
5858

5959
it("saves messages after each push", () => {
60-
const history = new LocalHistory({ storagePrefix: channelId });
60+
const history = new LocalHistory({ storage: { prefix: channelId } });
6161

6262
expect(localStorage.getItem(`waku:sds:storage:${channelId}`)).to.be.null;
6363

@@ -74,13 +74,13 @@ describe("Storage", () => {
7474
});
7575

7676
it("loads messages on initialization", () => {
77-
const history1 = new LocalHistory({ storagePrefix: channelId });
77+
const history1 = new LocalHistory({ storage: { prefix: channelId } });
7878

7979
history1.push(createMessage("msg-1", 1));
8080
history1.push(createMessage("msg-2", 2));
8181
history1.push(createMessage("msg-3", 3));
8282

83-
const history2 = new LocalHistory({ storagePrefix: channelId });
83+
const history2 = new LocalHistory({ storage: { prefix: channelId } });
8484

8585
expect(history2.length).to.equal(3);
8686
expect(history2.slice(0).map((m) => m.messageId)).to.deep.equal([

packages/sds/src/message_channel/storage/browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
const log = new Logger("sds:storage");
1111

12-
const STORAGE_PREFIX = "waku:sds:storage:";
12+
const STORAGE_NAMESPACE = "waku:sds:storage:";
1313

1414
/**
1515
* Browser localStorage wrapper for message persistence.
@@ -18,7 +18,7 @@ export class Storage {
1818
private readonly storageKey: string;
1919

2020
public constructor(storagePrefix: string) {
21-
this.storageKey = `${STORAGE_PREFIX}${storagePrefix}`;
21+
this.storageKey = `${STORAGE_NAMESPACE}${storagePrefix}`;
2222
}
2323

2424
public save(messages: ContentMessage[]): void {

0 commit comments

Comments
 (0)