nostr-double-ratchet 0.0.35 → 0.0.37

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.
@@ -1,8 +1,8 @@
1
1
  /*
2
- * Simple async key value storage interface plus an in memory implementation.
2
+ * Simple async key-value storage interface plus implementations.
3
3
  *
4
- * All methods are Promise based to accommodate back ends like
5
- * IndexedDB, SQLite, remote HTTP APIs, etc. For environments where you only
4
+ * All methods are Promise-based to accommodate back-ends like
5
+ * IndexedDB, SQLite, remote HTTP APIs, etc. For environments where you only
6
6
  * need ephemeral data (tests, Node scripts) the InMemoryStorageAdapter can be
7
7
  * used directly.
8
8
  */
@@ -33,11 +33,70 @@ export class InMemoryStorageAdapter implements StorageAdapter {
33
33
  this.store.delete(key)
34
34
  }
35
35
 
36
- async list(prefix = ''): Promise<string[]> {
36
+ async list(prefix = ""): Promise<string[]> {
37
37
  const keys: string[] = []
38
- for (const k of this.store.keys()) {
38
+ const storeKeys = Array.from(this.store.keys())
39
+ for (const k of storeKeys) {
39
40
  if (k.startsWith(prefix)) keys.push(k)
40
41
  }
41
42
  return keys
42
43
  }
43
- }
44
+ }
45
+
46
+ export class LocalStorageAdapter implements StorageAdapter {
47
+ private keyPrefix: string
48
+
49
+ constructor(keyPrefix = "session_") {
50
+ this.keyPrefix = keyPrefix
51
+ }
52
+
53
+ private getFullKey(key: string): string {
54
+ return `${this.keyPrefix}${key}`
55
+ }
56
+
57
+ async get<T = unknown>(key: string): Promise<T | undefined> {
58
+ try {
59
+ const item = localStorage.getItem(this.getFullKey(key))
60
+ return item ? JSON.parse(item) : undefined
61
+ } catch (e) {
62
+ console.warn(`Failed to get key ${key} from localStorage:`, e)
63
+ return undefined
64
+ }
65
+ }
66
+
67
+ async put<T = unknown>(key: string, value: T): Promise<void> {
68
+ try {
69
+ localStorage.setItem(this.getFullKey(key), JSON.stringify(value))
70
+ } catch (e) {
71
+ console.error(`Failed to put key ${key} to localStorage:`, e)
72
+ throw e
73
+ }
74
+ }
75
+
76
+ async del(key: string): Promise<void> {
77
+ try {
78
+ localStorage.removeItem(this.getFullKey(key))
79
+ } catch (e) {
80
+ console.warn(`Failed to delete key ${key} from localStorage:`, e)
81
+ }
82
+ }
83
+
84
+ async list(prefix = ""): Promise<string[]> {
85
+ const keys: string[] = []
86
+ const searchPrefix = this.getFullKey(prefix)
87
+
88
+ try {
89
+ for (let i = 0; i < localStorage.length; i++) {
90
+ const key = localStorage.key(i)
91
+ if (key && key.startsWith(searchPrefix)) {
92
+ // Remove our prefix to return the original key
93
+ keys.push(key.substring(this.keyPrefix.length))
94
+ }
95
+ }
96
+ } catch (e) {
97
+ console.warn("Failed to list keys from localStorage:", e)
98
+ }
99
+
100
+ return keys
101
+ }
102
+ }
package/src/index.ts CHANGED
@@ -2,4 +2,5 @@ export * from "./Session"
2
2
  export * from "./Invite"
3
3
  export * from "./types"
4
4
  export * from "./utils"
5
- export * from "./SessionManager"
5
+ export * from "./SessionManager"
6
+ export * from "./StorageAdapter"