mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-02 13:53:12 +00:00
minor fixes
This commit is contained in:
parent
e396c3b755
commit
ab6d3cf503
@ -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 ?? {};
|
||||
|
||||
@ -113,7 +113,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
|
||||
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
|
||||
|
||||
@ -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([
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user