From ab6d3cf503f89cfea52b4c95d5d101ef2dd37261 Mon Sep 17 00:00:00 2001 From: Danish Arora Date: Wed, 10 Dec 2025 15:38:16 -0500 Subject: [PATCH] minor fixes --- .../sds/src/message_channel/local_history.ts | 36 +++++++++---------- .../src/message_channel/message_channel.ts | 2 +- .../persistent_storage.spec.ts | 16 ++++----- .../src/message_channel/storage/browser.ts | 4 +-- 4 files changed, 28 insertions(+), 30 deletions(-) diff --git a/packages/sds/src/message_channel/local_history.ts b/packages/sds/src/message_channel/local_history.ts index a8d332a7ff..971b52fe74 100644 --- a/packages/sds/src/message_channel/local_history.ts +++ b/packages/sds/src/message_channel/local_history.ts @@ -6,6 +6,23 @@ import { Storage } from "./storage/index.js"; export const DEFAULT_MAX_LENGTH = 10_000; +/** + * Options for the LocalHistory constructor. + * @param storage - The storage to use for the local history. + * - prefix - The prefix for the storage. + * - customInstance - The custom storage instance to use. + * @param maxSize - The maximum number of messages to store. + */ +export type LocalHistoryOptions = { + storage?: { + prefix?: string; + customInstance?: Storage; + }; + maxSize?: number; +}; + +const log = new Logger("sds:local-history"); + /** * In-Memory implementation of a local history of messages. * @@ -19,30 +36,11 @@ export const DEFAULT_MAX_LENGTH = 10_000; * If an array of items longer than `maxLength` is pushed, dropping will happen * at next push. */ - -export type LocalHistoryOptions = { - storage?: { - prefix?: string; - customInstance?: Storage; - }; - maxSize?: number; -}; - -const log = new Logger("sds:local-history"); - export class LocalHistory { private items: ContentMessage[] = []; private readonly storage?: Storage; private readonly maxSize: number; - /** - * Construct a new in-memory local history. - * - * @param opts Configuration object. - * - storagePrefix: Optional prefix for persistent storage (creates Storage if provided). - * - storage: Optional explicit Storage instance. - * - maxSize: The maximum number of messages to store. Optional, defaults to DEFAULT_MAX_LENGTH. - */ public constructor(opts: LocalHistoryOptions = {}) { const { storage, maxSize } = opts; const { prefix, customInstance } = storage ?? {}; diff --git a/packages/sds/src/message_channel/message_channel.ts b/packages/sds/src/message_channel/message_channel.ts index fe460123e7..6091b010dc 100644 --- a/packages/sds/src/message_channel/message_channel.ts +++ b/packages/sds/src/message_channel/message_channel.ts @@ -113,7 +113,7 @@ export class MessageChannel extends TypedEventEmitter { this.possibleAcks = new Map(); this.incomingBuffer = []; this.localHistory = - localHistory ?? new LocalHistory({ storagePrefix: channelId }); + localHistory ?? new LocalHistory({ storage: { prefix: channelId } }); this.causalHistorySize = options.causalHistorySize ?? DEFAULT_CAUSAL_HISTORY_SIZE; // TODO: this should be determined based on the bloom filter parameters and number of hashes diff --git a/packages/sds/src/message_channel/persistent_storage.spec.ts b/packages/sds/src/message_channel/persistent_storage.spec.ts index b575ea7b5f..6109e188f7 100644 --- a/packages/sds/src/message_channel/persistent_storage.spec.ts +++ b/packages/sds/src/message_channel/persistent_storage.spec.ts @@ -18,11 +18,11 @@ describe("Storage", () => { }); it("persists and restores messages", () => { - const history1 = new LocalHistory({ storagePrefix: channelId }); + const history1 = new LocalHistory({ storage: { prefix: channelId } }); history1.push(createMessage("msg-1", 1)); history1.push(createMessage("msg-2", 2)); - const history2 = new LocalHistory({ storagePrefix: channelId }); + const history2 = new LocalHistory({ storage: { prefix: channelId } }); expect(history2.length).to.equal(2); expect(history2.slice(0).map((msg) => msg.messageId)).to.deep.equal([ @@ -34,15 +34,15 @@ describe("Storage", () => { it("handles corrupt data gracefully", () => { localStorage.setItem(`waku:sds:storage:${channelId}`, "{ invalid json }"); - const history = new LocalHistory({ storagePrefix: channelId }); + const history = new LocalHistory({ storage: { prefix: channelId } }); expect(history.length).to.equal(0); // Corrupt data is removed expect(localStorage.getItem(`waku:sds:storage:${channelId}`)).to.be.null; }); it("isolates history by channel ID", () => { - const history1 = new LocalHistory({ storagePrefix: "channel-1" }); - const history2 = new LocalHistory({ storagePrefix: "channel-2" }); + const history1 = new LocalHistory({ storage: { prefix: "channel-1" } }); + const history2 = new LocalHistory({ storage: { prefix: "channel-2" } }); history1.push(createMessage("msg-1", 1)); history2.push(createMessage("msg-2", 2)); @@ -57,7 +57,7 @@ describe("Storage", () => { }); it("saves messages after each push", () => { - const history = new LocalHistory({ storagePrefix: channelId }); + const history = new LocalHistory({ storage: { prefix: channelId } }); expect(localStorage.getItem(`waku:sds:storage:${channelId}`)).to.be.null; @@ -74,13 +74,13 @@ describe("Storage", () => { }); it("loads messages on initialization", () => { - const history1 = new LocalHistory({ storagePrefix: channelId }); + const history1 = new LocalHistory({ storage: { prefix: channelId } }); history1.push(createMessage("msg-1", 1)); history1.push(createMessage("msg-2", 2)); history1.push(createMessage("msg-3", 3)); - const history2 = new LocalHistory({ storagePrefix: channelId }); + const history2 = new LocalHistory({ storage: { prefix: channelId } }); expect(history2.length).to.equal(3); expect(history2.slice(0).map((m) => m.messageId)).to.deep.equal([ diff --git a/packages/sds/src/message_channel/storage/browser.ts b/packages/sds/src/message_channel/storage/browser.ts index aa570bf915..ae0d2a5531 100644 --- a/packages/sds/src/message_channel/storage/browser.ts +++ b/packages/sds/src/message_channel/storage/browser.ts @@ -9,7 +9,7 @@ import { const log = new Logger("sds:storage"); -const STORAGE_PREFIX = "waku:sds:storage:"; +const STORAGE_NAMESPACE = "waku:sds:storage:"; /** * Browser localStorage wrapper for message persistence. @@ -18,7 +18,7 @@ export class Storage { private readonly storageKey: string; public constructor(storagePrefix: string) { - this.storageKey = `${STORAGE_PREFIX}${storagePrefix}`; + this.storageKey = `${STORAGE_NAMESPACE}${storagePrefix}`; } public save(messages: ContentMessage[]): void {