@metamask-previews/storage-service 1.0.2-preview-8bfa290fb → 2.0.0-preview-0a30e47

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +10 -2
  2. package/dist/{InMemoryStorageAdapter.d.mts → InMemoryStorageAdapter.d.ts} +3 -3
  3. package/dist/InMemoryStorageAdapter.d.ts.map +1 -0
  4. package/dist/{InMemoryStorageAdapter.mjs → InMemoryStorageAdapter.js} +15 -28
  5. package/dist/InMemoryStorageAdapter.js.map +1 -0
  6. package/dist/{StorageService-method-action-types.d.cts → StorageService-method-action-types.d.ts} +2 -2
  7. package/dist/StorageService-method-action-types.d.ts.map +1 -0
  8. package/dist/{StorageService-method-action-types.mjs → StorageService-method-action-types.js} +1 -1
  9. package/dist/StorageService-method-action-types.js.map +1 -0
  10. package/dist/{StorageService.d.mts → StorageService.d.ts} +4 -4
  11. package/dist/StorageService.d.ts.map +1 -0
  12. package/dist/{StorageService.d.cts → StorageService.js} +55 -13
  13. package/dist/StorageService.js.map +1 -0
  14. package/dist/index.d.ts +6 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +7 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/{types.d.cts → types.d.ts} +4 -4
  19. package/dist/types.d.ts.map +1 -0
  20. package/dist/{types.mjs → types.js} +1 -1
  21. package/dist/types.js.map +1 -0
  22. package/package.json +13 -17
  23. package/dist/InMemoryStorageAdapter.cjs +0 -148
  24. package/dist/InMemoryStorageAdapter.cjs.map +0 -1
  25. package/dist/InMemoryStorageAdapter.d.cts +0 -88
  26. package/dist/InMemoryStorageAdapter.d.cts.map +0 -1
  27. package/dist/InMemoryStorageAdapter.d.mts.map +0 -1
  28. package/dist/InMemoryStorageAdapter.mjs.map +0 -1
  29. package/dist/StorageService-method-action-types.cjs +0 -7
  30. package/dist/StorageService-method-action-types.cjs.map +0 -1
  31. package/dist/StorageService-method-action-types.d.cts.map +0 -1
  32. package/dist/StorageService-method-action-types.d.mts +0 -78
  33. package/dist/StorageService-method-action-types.d.mts.map +0 -1
  34. package/dist/StorageService-method-action-types.mjs.map +0 -1
  35. package/dist/StorageService.cjs +0 -203
  36. package/dist/StorageService.cjs.map +0 -1
  37. package/dist/StorageService.d.cts.map +0 -1
  38. package/dist/StorageService.d.mts.map +0 -1
  39. package/dist/StorageService.mjs +0 -199
  40. package/dist/StorageService.mjs.map +0 -1
  41. package/dist/index.cjs +0 -14
  42. package/dist/index.cjs.map +0 -1
  43. package/dist/index.d.cts +0 -6
  44. package/dist/index.d.cts.map +0 -1
  45. package/dist/index.d.mts +0 -6
  46. package/dist/index.d.mts.map +0 -1
  47. package/dist/index.mjs +0 -7
  48. package/dist/index.mjs.map +0 -1
  49. package/dist/types.cjs +0 -12
  50. package/dist/types.cjs.map +0 -1
  51. package/dist/types.d.cts.map +0 -1
  52. package/dist/types.d.mts +0 -186
  53. package/dist/types.d.mts.map +0 -1
  54. package/dist/types.mjs.map +0 -1
package/CHANGELOG.md CHANGED
@@ -7,10 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.0.0]
11
+
10
12
  ### Changed
11
13
 
14
+ - **BREAKING:** Drop CommonJS support ([#9536](https://github.com/MetaMask/core/pull/9536))
15
+ - This package is now ESM-only, but can still be used in CommonJS projects via `require(esm)` in modern Node.js versions (22+), or dynamic imports in older Node.js versions.
16
+ - **BREAKING:** Bump minimum Node.js version to 22 ([#9976](https://github.com/MetaMask/core/pull/9976))
17
+ - **BREAKING:** Bump TypeScript target to ES2022 ([#10019](https://github.com/MetaMask/core/pull/10019))
18
+ - This package now ships ES2022 code, requiring a compatible modern environment or bundler configuration to consume.
12
19
  - Bump `@metamask/utils` from `^11.9.0` to `^11.12.0` ([#9074](https://github.com/MetaMask/core/pull/9074), [#10076](https://github.com/MetaMask/core/pull/10076))
13
- - Bump `@metamask/messenger` from `^1.2.0` to `^2.0.0` ([#9392](https://github.com/MetaMask/core/pull/9392))
20
+ - Bump `@metamask/messenger` from `^1.2.0` to `^3.0.0` ([#9392](https://github.com/MetaMask/core/pull/9392), [#10160](https://github.com/MetaMask/core/pull/10160))
14
21
 
15
22
  ## [1.0.2]
16
23
 
@@ -40,7 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
40
47
 
41
48
  - Initial release of `@metamask/storage-service` ([#7192](https://github.com/MetaMask/core/pull/7192))
42
49
 
43
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/storage-service@1.0.2...HEAD
50
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/storage-service@2.0.0...HEAD
51
+ [2.0.0]: https://github.com/MetaMask/core/compare/@metamask/storage-service@1.0.2...@metamask/storage-service@2.0.0
44
52
  [1.0.2]: https://github.com/MetaMask/core/compare/@metamask/storage-service@1.0.1...@metamask/storage-service@1.0.2
45
53
  [1.0.1]: https://github.com/MetaMask/core/compare/@metamask/storage-service@1.0.0...@metamask/storage-service@1.0.1
46
54
  [1.0.0]: https://github.com/MetaMask/core/compare/@metamask/storage-service@0.0.1...@metamask/storage-service@1.0.0
@@ -1,5 +1,5 @@
1
- import type { Json } from "@metamask/utils";
2
- import type { InitialStorageData, StorageAdapter, StorageGetResult } from "./types.mjs";
1
+ import type { Json } from '@metamask/utils';
2
+ import type { InitialStorageData, StorageAdapter, StorageGetResult } from './types.js';
3
3
  /**
4
4
  * In-memory storage adapter (default fallback).
5
5
  * Implements the {@link StorageAdapter} interface using a Map.
@@ -85,4 +85,4 @@ export declare class InMemoryStorageAdapter implements StorageAdapter {
85
85
  */
86
86
  clear(namespace: string): Promise<void>;
87
87
  }
88
- //# sourceMappingURL=InMemoryStorageAdapter.d.mts.map
88
+ //# sourceMappingURL=InMemoryStorageAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InMemoryStorageAdapter.d.ts","sourceRoot":"","sources":["../src/InMemoryStorageAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EACV,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EACjB,MAAM,YAAY,CAAC;AAGpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,sBAAuB,YAAW,cAAc;;IAO3D;;;;;OAKG;IACH,YAAY,WAAW,CAAC,EAAE,kBAAkB,EAW3C;IAED;;;;;;;OAOG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAgBvE;IAED;;;;;;;OAOG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAGxE;IAED;;;;;OAKG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG9D;IAED;;;;;;OAMG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAKrD;IAED;;;;OAIG;IACG,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAM5C;CACF"}
@@ -1,16 +1,4 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _InMemoryStorageAdapter_storage;
13
- import { STORAGE_KEY_PREFIX } from "./types.mjs";
1
+ import { STORAGE_KEY_PREFIX } from './types.js';
14
2
  /**
15
3
  * In-memory storage adapter (default fallback).
16
4
  * Implements the {@link StorageAdapter} interface using a Map.
@@ -48,6 +36,11 @@ import { STORAGE_KEY_PREFIX } from "./types.mjs";
48
36
  * ```
49
37
  */
50
38
  export class InMemoryStorageAdapter {
39
+ // Explicitly implement StorageAdapter interface
40
+ /**
41
+ * Internal storage map.
42
+ */
43
+ #storage;
51
44
  /**
52
45
  * Constructs a new InMemoryStorageAdapter.
53
46
  *
@@ -55,17 +48,12 @@ export class InMemoryStorageAdapter {
55
48
  * Useful for setting up test fixtures without calling setItem multiple times.
56
49
  */
57
50
  constructor(initialData) {
58
- // Explicitly implement StorageAdapter interface
59
- /**
60
- * Internal storage map.
61
- */
62
- _InMemoryStorageAdapter_storage.set(this, void 0);
63
- __classPrivateFieldSet(this, _InMemoryStorageAdapter_storage, new Map(), "f");
51
+ this.#storage = new Map();
64
52
  if (initialData) {
65
53
  for (const [namespace, items] of Object.entries(initialData)) {
66
54
  for (const [key, value] of Object.entries(items)) {
67
55
  const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
68
- __classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").set(fullKey, JSON.stringify(value));
56
+ this.#storage.set(fullKey, JSON.stringify(value));
69
57
  }
70
58
  }
71
59
  }
@@ -80,7 +68,7 @@ export class InMemoryStorageAdapter {
80
68
  */
81
69
  async getItem(namespace, key) {
82
70
  const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
83
- const serialized = __classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").get(fullKey);
71
+ const serialized = this.#storage.get(fullKey);
84
72
  // Key not found - return empty object
85
73
  if (serialized === undefined) {
86
74
  return {};
@@ -104,7 +92,7 @@ export class InMemoryStorageAdapter {
104
92
  */
105
93
  async setItem(namespace, key, value) {
106
94
  const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
107
- __classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").set(fullKey, JSON.stringify(value));
95
+ this.#storage.set(fullKey, JSON.stringify(value));
108
96
  }
109
97
  /**
110
98
  * Remove an item from in-memory storage.
@@ -114,7 +102,7 @@ export class InMemoryStorageAdapter {
114
102
  */
115
103
  async removeItem(namespace, key) {
116
104
  const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
117
- __classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").delete(fullKey);
105
+ this.#storage.delete(fullKey);
118
106
  }
119
107
  /**
120
108
  * Get all keys for a namespace.
@@ -125,7 +113,7 @@ export class InMemoryStorageAdapter {
125
113
  */
126
114
  async getAllKeys(namespace) {
127
115
  const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;
128
- return Array.from(__classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").keys())
116
+ return Array.from(this.#storage.keys())
129
117
  .filter((key) => key.startsWith(prefix))
130
118
  .map((key) => key.slice(prefix.length));
131
119
  }
@@ -136,9 +124,8 @@ export class InMemoryStorageAdapter {
136
124
  */
137
125
  async clear(namespace) {
138
126
  const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;
139
- const keysToDelete = Array.from(__classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").keys()).filter((key) => key.startsWith(prefix));
140
- keysToDelete.forEach((key) => __classPrivateFieldGet(this, _InMemoryStorageAdapter_storage, "f").delete(key));
127
+ const keysToDelete = Array.from(this.#storage.keys()).filter((key) => key.startsWith(prefix));
128
+ keysToDelete.forEach((key) => this.#storage.delete(key));
141
129
  }
142
130
  }
143
- _InMemoryStorageAdapter_storage = new WeakMap();
144
- //# sourceMappingURL=InMemoryStorageAdapter.mjs.map
131
+ //# sourceMappingURL=InMemoryStorageAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"InMemoryStorageAdapter.js","sourceRoot":"","sources":["../src/InMemoryStorageAdapter.ts"],"names":[],"mappings":"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","sourcesContent":["import type { Json } from '@metamask/utils';\n\nimport type {\n InitialStorageData,\n StorageAdapter,\n StorageGetResult,\n} from './types.js';\nimport { STORAGE_KEY_PREFIX } from './types.js';\n\n/**\n * In-memory storage adapter (default fallback).\n * Implements the {@link StorageAdapter} interface using a Map.\n *\n * ⚠️ **Warning**: Data is NOT persisted - lost on restart.\n *\n * **Suitable for:**\n * - Testing (isolated, no mocking needed)\n * - Development (quick start, zero config)\n * - Temporary/ephemeral data\n *\n * **Not suitable for:**\n * - Production (unless data is truly ephemeral)\n * - Data that needs to persist across restarts\n *\n * @example Basic usage\n * ```typescript\n * const adapter = new InMemoryStorageAdapter();\n * await adapter.setItem('SnapController', 'snap-id:sourceCode', 'const x = 1;');\n * const value = await adapter.getItem('SnapController', 'snap-id:sourceCode'); // 'const x = 1;'\n * // After restart: data is lost\n * ```\n *\n * @example Initialize with data (useful for testing)\n * ```typescript\n * const adapter = new InMemoryStorageAdapter({\n * SnapController: {\n * 'snap-id:sourceCode': 'const x = 1;',\n * 'snap-id:manifest': { name: 'My Snap' },\n * },\n * TokenListController: {\n * cache: { '0x1': ['token1', 'token2'] },\n * },\n * });\n * ```\n */\nexport class InMemoryStorageAdapter implements StorageAdapter {\n // Explicitly implement StorageAdapter interface\n /**\n * Internal storage map.\n */\n readonly #storage: Map<string, string>;\n\n /**\n * Constructs a new InMemoryStorageAdapter.\n *\n * @param initialData - Optional initial data to populate the storage.\n * Useful for setting up test fixtures without calling setItem multiple times.\n */\n constructor(initialData?: InitialStorageData) {\n this.#storage = new Map();\n\n if (initialData) {\n for (const [namespace, items] of Object.entries(initialData)) {\n for (const [key, value] of Object.entries(items)) {\n const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;\n this.#storage.set(fullKey, JSON.stringify(value));\n }\n }\n }\n }\n\n /**\n * Retrieve an item from in-memory storage.\n * Deserializes JSON data from storage.\n *\n * @param namespace - The controller namespace.\n * @param key - The data key.\n * @returns StorageGetResult: { result } if found, {} if not found, { error } on failure.\n */\n async getItem(namespace: string, key: string): Promise<StorageGetResult> {\n const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;\n const serialized = this.#storage.get(fullKey);\n\n // Key not found - return empty object\n if (serialized === undefined) {\n return {};\n }\n\n try {\n const result = JSON.parse(serialized);\n return { result };\n } catch (error) {\n console.error(`Failed to parse stored data for ${fullKey}:`, error);\n return { error: error as Error };\n }\n }\n\n /**\n * Store an item in in-memory storage.\n * Serializes JSON data to string.\n *\n * @param namespace - The controller namespace.\n * @param key - The data key.\n * @param value - The JSON value to store.\n */\n async setItem(namespace: string, key: string, value: Json): Promise<void> {\n const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;\n this.#storage.set(fullKey, JSON.stringify(value));\n }\n\n /**\n * Remove an item from in-memory storage.\n *\n * @param namespace - The controller namespace.\n * @param key - The data key.\n */\n async removeItem(namespace: string, key: string): Promise<void> {\n const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;\n this.#storage.delete(fullKey);\n }\n\n /**\n * Get all keys for a namespace.\n * Returns keys without the 'storage:namespace:' prefix.\n *\n * @param namespace - The namespace to get keys for.\n * @returns Array of keys (without prefix) for this namespace.\n */\n async getAllKeys(namespace: string): Promise<string[]> {\n const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;\n return Array.from(this.#storage.keys())\n .filter((key) => key.startsWith(prefix))\n .map((key) => key.slice(prefix.length));\n }\n\n /**\n * Clear all items for a namespace.\n *\n * @param namespace - The namespace to clear.\n */\n async clear(namespace: string): Promise<void> {\n const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;\n const keysToDelete = Array.from(this.#storage.keys()).filter((key) =>\n key.startsWith(prefix),\n );\n keysToDelete.forEach((key) => this.#storage.delete(key));\n }\n}\n"]}
@@ -2,7 +2,7 @@
2
2
  * This file is auto generated.
3
3
  * Do not edit manually.
4
4
  */
5
- import type { StorageService } from "./StorageService.cjs";
5
+ import type { StorageService } from './StorageService.js';
6
6
  /**
7
7
  * Store large JSON data in storage.
8
8
  *
@@ -75,4 +75,4 @@ export type StorageServiceClearAction = {
75
75
  * Union of all StorageService action types.
76
76
  */
77
77
  export type StorageServiceMethodActions = StorageServiceSetItemAction | StorageServiceGetItemAction | StorageServiceRemoveItemAction | StorageServiceGetAllKeysAction | StorageServiceClearAction;
78
- //# sourceMappingURL=StorageService-method-action-types.d.cts.map
78
+ //# sourceMappingURL=StorageService-method-action-types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageService-method-action-types.d.ts","sourceRoot":"","sources":["../src/StorageService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,wBAAwB,CAAC;IAC/B,OAAO,EAAE,cAAc,CAAC,SAAS,CAAC,CAAC;CACpC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,wBAAwB,CAAC;IAC/B,OAAO,EAAE,cAAc,CAAC,SAAS,CAAC,CAAC;CACpC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,2BAA2B,CAAC;IAClC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;CACvC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,2BAA2B,CAAC;IAClC,OAAO,EAAE,cAAc,CAAC,YAAY,CAAC,CAAC;CACvC,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,yBAAyB,GAAG;IACtC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;CAClC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,2BAA2B,GACnC,2BAA2B,GAC3B,2BAA2B,GAC3B,8BAA8B,GAC9B,8BAA8B,GAC9B,yBAAyB,CAAC"}
@@ -3,4 +3,4 @@
3
3
  * Do not edit manually.
4
4
  */
5
5
  export {};
6
- //# sourceMappingURL=StorageService-method-action-types.mjs.map
6
+ //# sourceMappingURL=StorageService-method-action-types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageService-method-action-types.js","sourceRoot":"","sources":["../src/StorageService-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated.\n * Do not edit manually.\n */\n\nimport type { StorageService } from './StorageService.js';\n\n/**\n * Store large JSON data in storage.\n *\n * ⚠️ **Designed for large values (100KB+), not many small ones.**\n * Each storage operation has I/O overhead. For best performance,\n * store one large object rather than many small key-value pairs.\n *\n * @example Good: Store entire cache as one value\n * ```typescript\n * await service.setItem('TokenList', 'cache', { '0x1': [...], '0x38': [...] });\n * ```\n *\n * @example Avoid: Many small values\n * ```typescript\n * // ❌ Don't do this - too many small writes\n * await service.setItem('TokenList', 'cache:0x1', [...]);\n * await service.setItem('TokenList', 'cache:0x38', [...]);\n * ```\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n * @param value - JSON data to store (should be 100KB+ for optimal use).\n */\nexport type StorageServiceSetItemAction = {\n type: `StorageService:setItem`;\n handler: StorageService['setItem'];\n};\n\n/**\n * Retrieve JSON data from storage.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n * @returns StorageGetResult: { result } if found, {} if not found, { error } on failure.\n */\nexport type StorageServiceGetItemAction = {\n type: `StorageService:getItem`;\n handler: StorageService['getItem'];\n};\n\n/**\n * Remove data from storage.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n */\nexport type StorageServiceRemoveItemAction = {\n type: `StorageService:removeItem`;\n handler: StorageService['removeItem'];\n};\n\n/**\n * Get all keys for a namespace.\n * Delegates to storage adapter which handles filtering.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @returns Array of keys (without prefix) for this namespace.\n */\nexport type StorageServiceGetAllKeysAction = {\n type: `StorageService:getAllKeys`;\n handler: StorageService['getAllKeys'];\n};\n\n/**\n * Clear all data for a namespace.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n */\nexport type StorageServiceClearAction = {\n type: `StorageService:clear`;\n handler: StorageService['clear'];\n};\n\n/**\n * Union of all StorageService action types.\n */\nexport type StorageServiceMethodActions =\n | StorageServiceSetItemAction\n | StorageServiceGetItemAction\n | StorageServiceRemoveItemAction\n | StorageServiceGetAllKeysAction\n | StorageServiceClearAction;\n"]}
@@ -1,6 +1,6 @@
1
- import type { Json } from "@metamask/utils";
2
- import type { StorageGetResult, StorageServiceOptions } from "./types.mjs";
3
- import { SERVICE_NAME } from "./types.mjs";
1
+ import type { Json } from '@metamask/utils';
2
+ import type { StorageGetResult, StorageServiceOptions } from './types.js';
3
+ import { SERVICE_NAME } from './types.js';
4
4
  /**
5
5
  * StorageService provides a platform-agnostic way for controllers to store
6
6
  * large, infrequently accessed data outside of memory/Redux state.
@@ -145,4 +145,4 @@ export declare class StorageService {
145
145
  */
146
146
  clear(namespace: string): Promise<void>;
147
147
  }
148
- //# sourceMappingURL=StorageService.d.mts.map
148
+ //# sourceMappingURL=StorageService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageService.d.ts","sourceRoot":"","sources":["../src/StorageService.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,KAAK,EAEV,gBAAgB,EAEhB,qBAAqB,EACtB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAY1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0EG;AACH,qBAAa,cAAc;;IACzB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,OAAO,YAAY,CAAC;IAYnC;;;;;;;OAOG;IACH,YAAY,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,qBAAqB,EAkBxD;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAYxE;IAED;;;;;;OAMG;IACG,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAGvE;IAED;;;;;OAKG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG9D;IAED;;;;;;OAMG;IACG,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAErD;IAED;;;;OAIG;IACG,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5C;CACF"}
@@ -1,6 +1,13 @@
1
- import type { Json } from "@metamask/utils";
2
- import type { StorageGetResult, StorageServiceOptions } from "./types.cjs";
3
- import { SERVICE_NAME } from "./types.cjs";
1
+ import { InMemoryStorageAdapter } from './InMemoryStorageAdapter.js';
2
+ import { SERVICE_NAME } from './types.js';
3
+ // === MESSENGER ===
4
+ const MESSENGER_EXPOSED_METHODS = [
5
+ 'setItem',
6
+ 'getItem',
7
+ 'removeItem',
8
+ 'getAllKeys',
9
+ 'clear',
10
+ ];
4
11
  /**
5
12
  * StorageService provides a platform-agnostic way for controllers to store
6
13
  * large, infrequently accessed data outside of memory/Redux state.
@@ -76,12 +83,19 @@ import { SERVICE_NAME } from "./types.cjs";
76
83
  * });
77
84
  * ```
78
85
  */
79
- export declare class StorageService {
80
- #private;
86
+ export class StorageService {
81
87
  /**
82
88
  * The name of the service.
83
89
  */
84
- readonly name: typeof SERVICE_NAME;
90
+ name;
91
+ /**
92
+ * The messenger suited for this service.
93
+ */
94
+ #messenger;
95
+ /**
96
+ * The storage adapter for persisting data.
97
+ */
98
+ #storage;
85
99
  /**
86
100
  * Constructs a new StorageService.
87
101
  *
@@ -90,7 +104,18 @@ export declare class StorageService {
90
104
  * @param options.storage - Storage adapter for persisting data.
91
105
  * If not provided, uses InMemoryStorageAdapter (data lost on restart).
92
106
  */
93
- constructor({ messenger, storage }: StorageServiceOptions);
107
+ constructor({ messenger, storage }) {
108
+ this.name = SERVICE_NAME;
109
+ this.#messenger = messenger;
110
+ this.#storage = storage ?? new InMemoryStorageAdapter();
111
+ // Warn if using in-memory storage (data won't persist)
112
+ if (!storage) {
113
+ console.warn(`${SERVICE_NAME}: No storage adapter provided. Using in-memory storage. ` +
114
+ 'Data will be lost on restart. Provide a storage adapter for persistence.');
115
+ }
116
+ // Register messenger actions
117
+ this.#messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
118
+ }
94
119
  /**
95
120
  * Store large JSON data in storage.
96
121
  *
@@ -114,7 +139,14 @@ export declare class StorageService {
114
139
  * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').
115
140
  * @param value - JSON data to store (should be 100KB+ for optimal use).
116
141
  */
117
- setItem(namespace: string, key: string, value: Json): Promise<void>;
142
+ async setItem(namespace, key, value) {
143
+ // Adapter handles serialization and wrapping with metadata
144
+ await this.#storage.setItem(namespace, key, value);
145
+ // Publish event so other controllers can react to changes
146
+ // Event type: StorageService:itemSet:namespace
147
+ // Payload: [key, value]
148
+ this.#messenger.publish(`${SERVICE_NAME}:itemSet:${namespace}`, key, value);
149
+ }
118
150
  /**
119
151
  * Retrieve JSON data from storage.
120
152
  *
@@ -122,14 +154,20 @@ export declare class StorageService {
122
154
  * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').
123
155
  * @returns StorageGetResult: { result } if found, {} if not found, { error } on failure.
124
156
  */
125
- getItem(namespace: string, key: string): Promise<StorageGetResult>;
157
+ async getItem(namespace, key) {
158
+ // Adapter handles deserialization and unwrapping
159
+ return await this.#storage.getItem(namespace, key);
160
+ }
126
161
  /**
127
162
  * Remove data from storage.
128
163
  *
129
164
  * @param namespace - Controller namespace (e.g., 'SnapController').
130
165
  * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').
131
166
  */
132
- removeItem(namespace: string, key: string): Promise<void>;
167
+ async removeItem(namespace, key) {
168
+ // Adapter builds full storage key (e.g., mobile: 'storageService:namespace:key')
169
+ await this.#storage.removeItem(namespace, key);
170
+ }
133
171
  /**
134
172
  * Get all keys for a namespace.
135
173
  * Delegates to storage adapter which handles filtering.
@@ -137,12 +175,16 @@ export declare class StorageService {
137
175
  * @param namespace - Controller namespace (e.g., 'SnapController').
138
176
  * @returns Array of keys (without prefix) for this namespace.
139
177
  */
140
- getAllKeys(namespace: string): Promise<string[]>;
178
+ async getAllKeys(namespace) {
179
+ return await this.#storage.getAllKeys(namespace);
180
+ }
141
181
  /**
142
182
  * Clear all data for a namespace.
143
183
  *
144
184
  * @param namespace - Controller namespace (e.g., 'SnapController').
145
185
  */
146
- clear(namespace: string): Promise<void>;
186
+ async clear(namespace) {
187
+ await this.#storage.clear(namespace);
188
+ }
147
189
  }
148
- //# sourceMappingURL=StorageService.d.cts.map
190
+ //# sourceMappingURL=StorageService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageService.js","sourceRoot":"","sources":["../src/StorageService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAOrE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,oBAAoB;AAEpB,MAAM,yBAAyB,GAAG;IAChC,SAAS;IACT,SAAS;IACT,YAAY;IACZ,YAAY;IACZ,OAAO;CACC,CAAC;AAEX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0EG;AACH,MAAM,OAAO,cAAc;IACzB;;OAEG;IACM,IAAI,CAAsB;IAEnC;;OAEG;IACM,UAAU,CAA0B;IAE7C;;OAEG;IACM,QAAQ,CAAiB;IAElC;;;;;;;OAOG;IACH,YAAY,EAAE,SAAS,EAAE,OAAO,EAAyB;QACvD,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,QAAQ,GAAG,OAAO,IAAI,IAAI,sBAAsB,EAAE,CAAC;QAExD,uDAAuD;QACvD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CACV,GAAG,YAAY,0DAA0D;gBACvE,0EAA0E,CAC7E,CAAC;QACJ,CAAC;QAED,6BAA6B;QAC7B,IAAI,CAAC,UAAU,CAAC,4BAA4B,CAC1C,IAAI,EACJ,yBAAyB,CAC1B,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,GAAW,EAAE,KAAW;QACvD,2DAA2D;QAC3D,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QAEnD,0DAA0D;QAC1D,+CAA+C;QAC/C,wBAAwB;QACxB,IAAI,CAAC,UAAU,CAAC,OAAO,CACrB,GAAG,YAAY,YAAY,SAAS,EAAW,EAC/C,GAAG,EACH,KAAK,CACN,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,GAAW;QAC1C,iDAAiD;QACjD,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACrD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CAAC,SAAiB,EAAE,GAAW;QAC7C,iFAAiF;QACjF,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,UAAU,CAAC,SAAiB;QAChC,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK,CAAC,SAAiB;QAC3B,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACvC,CAAC;CACF","sourcesContent":["import type { Json } from '@metamask/utils';\n\nimport { InMemoryStorageAdapter } from './InMemoryStorageAdapter.js';\nimport type {\n StorageAdapter,\n StorageGetResult,\n StorageServiceMessenger,\n StorageServiceOptions,\n} from './types.js';\nimport { SERVICE_NAME } from './types.js';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'setItem',\n 'getItem',\n 'removeItem',\n 'getAllKeys',\n 'clear',\n] as const;\n\n/**\n * StorageService provides a platform-agnostic way for controllers to store\n * large, infrequently accessed data outside of memory/Redux state.\n *\n * **Use cases:**\n * - Snap source code (6+ MB that's rarely accessed)\n * - Token metadata caches (4+ MB of cached data)\n * - Large cached responses from APIs\n * - Any data > 100 KB that's not frequently accessed\n *\n * **Benefits:**\n * - Reduces memory usage (data stays on disk)\n * - Faster Redux persist (less data to serialize)\n * - Faster app startup (less data to parse)\n * - Lazy loading (data loaded only when needed)\n *\n * **Platform Support:**\n * - Mobile: FilesystemStorage adapter\n * - Extension: IndexedDB adapter\n * - Tests/Dev: InMemoryStorageAdapter (default)\n *\n * @example Using the service via messenger\n *\n * ```typescript\n * // In a controller\n * type AllowedActions =\n * | StorageServiceSetItemAction\n * | StorageServiceGetItemAction;\n *\n * class SnapController extends BaseController {\n * async storeSnapSourceCode(snapId: string, sourceCode: string) {\n * await this.messenger.call(\n * 'StorageService:setItem',\n * 'SnapController',\n * `${snapId}:sourceCode`,\n * sourceCode,\n * );\n * }\n *\n * async getSnapSourceCode(snapId: string): Promise<string | undefined> {\n * const { result, error } = await this.messenger.call(\n * 'StorageService:getItem',\n * 'SnapController',\n * `${snapId}:sourceCode`,\n * );\n * if (error) {\n * throw error; // Handle error\n * }\n * return result as string | undefined; // undefined if not found\n * }\n * }\n * ```\n *\n * @example Initializing in a client\n *\n * ```typescript\n * // Mobile\n * const service = new StorageService({\n * messenger: storageServiceMessenger,\n * storage: filesystemStorageAdapter, // Platform-specific\n * });\n *\n * // Extension\n * const service = new StorageService({\n * messenger: storageServiceMessenger,\n * storage: indexedDBAdapter, // Platform-specific\n * });\n *\n * // Tests (uses in-memory by default)\n * const service = new StorageService({\n * messenger: storageServiceMessenger,\n * // No storage - uses InMemoryStorageAdapter\n * });\n * ```\n */\nexport class StorageService {\n /**\n * The name of the service.\n */\n readonly name: typeof SERVICE_NAME;\n\n /**\n * The messenger suited for this service.\n */\n readonly #messenger: StorageServiceMessenger;\n\n /**\n * The storage adapter for persisting data.\n */\n readonly #storage: StorageAdapter;\n\n /**\n * Constructs a new StorageService.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited for this service.\n * @param options.storage - Storage adapter for persisting data.\n * If not provided, uses InMemoryStorageAdapter (data lost on restart).\n */\n constructor({ messenger, storage }: StorageServiceOptions) {\n this.name = SERVICE_NAME;\n this.#messenger = messenger;\n this.#storage = storage ?? new InMemoryStorageAdapter();\n\n // Warn if using in-memory storage (data won't persist)\n if (!storage) {\n console.warn(\n `${SERVICE_NAME}: No storage adapter provided. Using in-memory storage. ` +\n 'Data will be lost on restart. Provide a storage adapter for persistence.',\n );\n }\n\n // Register messenger actions\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Store large JSON data in storage.\n *\n * ⚠️ **Designed for large values (100KB+), not many small ones.**\n * Each storage operation has I/O overhead. For best performance,\n * store one large object rather than many small key-value pairs.\n *\n * @example Good: Store entire cache as one value\n * ```typescript\n * await service.setItem('TokenList', 'cache', { '0x1': [...], '0x38': [...] });\n * ```\n *\n * @example Avoid: Many small values\n * ```typescript\n * // ❌ Don't do this - too many small writes\n * await service.setItem('TokenList', 'cache:0x1', [...]);\n * await service.setItem('TokenList', 'cache:0x38', [...]);\n * ```\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n * @param value - JSON data to store (should be 100KB+ for optimal use).\n */\n async setItem(namespace: string, key: string, value: Json): Promise<void> {\n // Adapter handles serialization and wrapping with metadata\n await this.#storage.setItem(namespace, key, value);\n\n // Publish event so other controllers can react to changes\n // Event type: StorageService:itemSet:namespace\n // Payload: [key, value]\n this.#messenger.publish(\n `${SERVICE_NAME}:itemSet:${namespace}` as const,\n key,\n value,\n );\n }\n\n /**\n * Retrieve JSON data from storage.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n * @returns StorageGetResult: { result } if found, {} if not found, { error } on failure.\n */\n async getItem(namespace: string, key: string): Promise<StorageGetResult> {\n // Adapter handles deserialization and unwrapping\n return await this.#storage.getItem(namespace, key);\n }\n\n /**\n * Remove data from storage.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n */\n async removeItem(namespace: string, key: string): Promise<void> {\n // Adapter builds full storage key (e.g., mobile: 'storageService:namespace:key')\n await this.#storage.removeItem(namespace, key);\n }\n\n /**\n * Get all keys for a namespace.\n * Delegates to storage adapter which handles filtering.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @returns Array of keys (without prefix) for this namespace.\n */\n async getAllKeys(namespace: string): Promise<string[]> {\n return await this.#storage.getAllKeys(namespace);\n }\n\n /**\n * Clear all data for a namespace.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n */\n async clear(namespace: string): Promise<void> {\n await this.#storage.clear(namespace);\n }\n}\n"]}
@@ -0,0 +1,6 @@
1
+ export { StorageService } from './StorageService.js';
2
+ export { InMemoryStorageAdapter } from './InMemoryStorageAdapter.js';
3
+ export type { InitialStorageData, StorageAdapter, StorageGetResult, StorageServiceOptions, StorageServiceActions, StorageServiceEvents, StorageServiceMessenger, StorageServiceItemSetEvent, } from './types.js';
4
+ export type { StorageServiceSetItemAction, StorageServiceGetItemAction, StorageServiceRemoveItemAction, StorageServiceGetAllKeysAction, StorageServiceClearAction, } from './StorageService-method-action-types.js';
5
+ export { SERVICE_NAME, STORAGE_KEY_PREFIX } from './types.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAGrD,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAGrE,YAAY,EACV,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,0BAA0B,GAC3B,MAAM,YAAY,CAAC;AAGpB,YAAY,EACV,2BAA2B,EAC3B,2BAA2B,EAC3B,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,GAC1B,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Export service class
2
+ export { StorageService } from './StorageService.js';
3
+ // Export adapters
4
+ export { InMemoryStorageAdapter } from './InMemoryStorageAdapter.js';
5
+ // Export service name and storage key prefix constants
6
+ export { SERVICE_NAME, STORAGE_KEY_PREFIX } from './types.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,kBAAkB;AAClB,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAuBrE,uDAAuD;AACvD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC","sourcesContent":["// Export service class\nexport { StorageService } from './StorageService.js';\n\n// Export adapters\nexport { InMemoryStorageAdapter } from './InMemoryStorageAdapter.js';\n\n// Export types from types.ts\nexport type {\n InitialStorageData,\n StorageAdapter,\n StorageGetResult,\n StorageServiceOptions,\n StorageServiceActions,\n StorageServiceEvents,\n StorageServiceMessenger,\n StorageServiceItemSetEvent,\n} from './types.js';\n\n// Export individual action types from generated file\nexport type {\n StorageServiceSetItemAction,\n StorageServiceGetItemAction,\n StorageServiceRemoveItemAction,\n StorageServiceGetAllKeysAction,\n StorageServiceClearAction,\n} from './StorageService-method-action-types.js';\n\n// Export service name and storage key prefix constants\nexport { SERVICE_NAME, STORAGE_KEY_PREFIX } from './types.js';\n"]}
@@ -1,6 +1,6 @@
1
- import type { Messenger } from "@metamask/messenger";
2
- import type { Json } from "@metamask/utils";
3
- import type { StorageServiceMethodActions } from "./StorageService-method-action-types.cjs";
1
+ import type { Messenger } from '@metamask/messenger';
2
+ import type { Json } from '@metamask/utils';
3
+ import type { StorageServiceMethodActions } from './StorageService-method-action-types.js';
4
4
  /**
5
5
  * Initial data structure for populating InMemoryStorageAdapter.
6
6
  * Useful for setting up test fixtures.
@@ -183,4 +183,4 @@ type AllowedEvents = never;
183
183
  */
184
184
  export type StorageServiceMessenger = Messenger<typeof SERVICE_NAME, StorageServiceActions | AllowedActions, StorageServiceEvents | AllowedEvents>;
185
185
  export {};
186
- //# sourceMappingURL=types.d.cts.map
186
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,yCAAyC,CAAC;AAE3F;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,CAAC,SAAS,EAAE,MAAM,GAAG;QACnB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;KACrB,CAAC;CACH,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GACxB;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,GAC/B;IAAE,MAAM,CAAC,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,KAAK,CAAA;CAAE,GAChC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAEnE;;;;;;;;;;;;;;;OAeG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEpE;;;;;;OAMG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1D;;;;;;;;;;;OAWG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAEjD;;;;;;;;OAQG;IACH,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACzC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC;;OAEG;IACH,SAAS,EAAE,uBAAuB,CAAC;IAEnC;;;;OAIG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B,CAAC;AAGF,eAAO,MAAM,YAAY,mBAAmB,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,oBAAoB,CAAC;AAEpD;;;GAGG;AACH,MAAM,MAAM,qBAAqB,GAAG,2BAA2B,CAAC;AAEhE;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,GAAG,OAAO,YAAY,YAAY,MAAM,EAAE,CAAC;IACjD,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;CACrC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE9D;;GAEG;AACH,KAAK,cAAc,GAAG,KAAK,CAAC;AAE5B;;GAEG;AACH,KAAK,aAAa,GAAG,KAAK,CAAC;AAE3B;;;GAGG;AACH,MAAM,MAAM,uBAAuB,GAAG,SAAS,CAC7C,OAAO,YAAY,EACnB,qBAAqB,GAAG,cAAc,EACtC,oBAAoB,GAAG,aAAa,CACrC,CAAC"}
@@ -6,4 +6,4 @@ export const SERVICE_NAME = 'StorageService';
6
6
  * Example: 'storageService:SnapController:snap-id:sourceCode'
7
7
  */
8
8
  export const STORAGE_KEY_PREFIX = 'storageService:';
9
- //# sourceMappingURL=types.mjs.map
9
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAgJA,wBAAwB;AACxB,MAAM,CAAC,MAAM,YAAY,GAAG,gBAAgB,CAAC;AAE7C;;;;GAIG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,iBAAiB,CAAC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { Json } from '@metamask/utils';\n\nimport type { StorageServiceMethodActions } from './StorageService-method-action-types.js';\n\n/**\n * Initial data structure for populating InMemoryStorageAdapter.\n * Useful for setting up test fixtures.\n *\n * @example\n * ```typescript\n * const initialData: InitialStorageData = {\n * SnapController: {\n * 'snap-id:sourceCode': 'const x = 1;',\n * 'snap-id:manifest': { name: 'My Snap' },\n * },\n * TokenListController: {\n * cache: { '0x1': ['token1', 'token2'] },\n * },\n * };\n * const adapter = new InMemoryStorageAdapter(initialData);\n * ```\n */\nexport type InitialStorageData = {\n [namespace: string]: {\n [key: string]: Json;\n };\n};\n\n/**\n * Result type for getItem operations.\n * Distinguishes between: found data, not found, and error conditions.\n *\n * - `{ result: Json }` - Data was found and successfully retrieved\n * - `{}` (empty object) - No data stored with that key\n * - `{ error: Error }` - Error occurred during retrieval\n */\nexport type StorageGetResult =\n | { result: Json; error?: never }\n | { result?: never; error: Error }\n | Record<string, never>;\n\n/**\n * Platform-agnostic storage adapter interface.\n * Each client (mobile, extension) implements this interface\n * with their preferred storage mechanism.\n *\n * ⚠️ **Designed for large, infrequently accessed data (100KB+)**\n *\n * ✅ **Use for:**\n * - Snap source code (~6 MB per snap)\n * - Token metadata caches (~4 MB)\n * - Large API response caches\n *\n * ❌ **Avoid for:**\n * - Small values (< 10 KB) - use controller state instead\n * - Frequently accessed data - use controller state instead\n * - Many small key-value pairs - use a single large object instead\n *\n * @example Mobile implementation using FilesystemStorage\n * @example Extension implementation using IndexedDB\n * @example Tests using InMemoryStorageAdapter\n */\nexport type StorageAdapter = {\n /**\n * Retrieve an item from storage.\n * Adapter is responsible for building the full storage key.\n *\n * @param namespace - The controller namespace (e.g., 'SnapController').\n * @param key - The data key (e.g., 'snap-id:sourceCode').\n * @returns StorageGetResult: { result } if found, {} if not found, { error } on failure.\n */\n getItem(namespace: string, key: string): Promise<StorageGetResult>;\n\n /**\n * Store a large JSON value in storage.\n *\n * ⚠️ **Store large values, not many small ones.**\n * Each storage operation has I/O overhead. For best performance:\n * - Store one large object rather than many small key-value pairs\n * - Minimum recommended size: 100 KB per value\n *\n * Adapter is responsible for:\n * - Building the full storage key\n * - Serializing to string (JSON.stringify)\n *\n * @param namespace - The controller namespace (e.g., 'SnapController').\n * @param key - The data key (e.g., 'snap-id:sourceCode').\n * @param value - The JSON value to store.\n */\n setItem(namespace: string, key: string, value: Json): Promise<void>;\n\n /**\n * Remove an item from storage.\n * Adapter is responsible for building the full storage key.\n *\n * @param namespace - The controller namespace (e.g., 'SnapController').\n * @param key - The data key (e.g., 'snap-id:sourceCode').\n */\n removeItem(namespace: string, key: string): Promise<void>;\n\n /**\n * Get all keys for a specific namespace.\n * Should return keys without the 'storage:namespace:' prefix.\n *\n * Adapter is responsible for:\n * - Filtering keys by prefix: 'storage:{namespace}:'\n * - Stripping the prefix from returned keys\n * - Returning only the key portion after the prefix\n *\n * @param namespace - The namespace to get keys for (e.g., 'SnapController').\n * @returns Array of keys without prefix (e.g., ['snap1:sourceCode', 'snap2:sourceCode']).\n */\n getAllKeys(namespace: string): Promise<string[]>;\n\n /**\n * Clear all items for a specific namespace.\n *\n * Adapter is responsible for:\n * - Finding all keys with prefix: 'storageService:{namespace}:'\n * - Removing all matching keys\n *\n * @param namespace - The namespace to clear (e.g., 'SnapController').\n */\n clear(namespace: string): Promise<void>;\n};\n\n/**\n * Options for constructing a {@link StorageService}.\n */\nexport type StorageServiceOptions = {\n /**\n * The messenger suited for this service.\n */\n messenger: StorageServiceMessenger;\n\n /**\n * Storage adapter for persisting data.\n * If not provided, uses in-memory storage (data lost on restart).\n * Production clients MUST provide a persistent storage adapter.\n */\n storage?: StorageAdapter;\n};\n\n// Service name constant\nexport const SERVICE_NAME = 'StorageService';\n\n/**\n * Storage key prefix for all keys managed by StorageService.\n * Keys are formatted as: {STORAGE_KEY_PREFIX}{namespace}:{key}\n * Example: 'storageService:SnapController:snap-id:sourceCode'\n */\nexport const STORAGE_KEY_PREFIX = 'storageService:';\n\n/**\n * All actions that {@link StorageService} exposes to other consumers.\n * Action types are auto-generated from the service methods.\n */\nexport type StorageServiceActions = StorageServiceMethodActions;\n\n/**\n * Event published when a storage item is set.\n * Event type includes namespace only, key passed in payload.\n *\n * @example\n * Subscribe to all changes in TokenListController:\n * messenger.subscribe('StorageService:itemSet:TokenListController', (key, value) => {\n * // key = 'cache:0x1', 'cache:0x38', etc.\n * // value = the data that was set\n * if (key.startsWith('cache:')) {\n * const chainId = key.replace('cache:', '');\n * // React to cache change for specific chain\n * }\n * });\n */\nexport type StorageServiceItemSetEvent = {\n type: `${typeof SERVICE_NAME}:itemSet:${string}`;\n payload: [key: string, value: Json];\n};\n\n/**\n * All events that {@link StorageService} publishes.\n */\nexport type StorageServiceEvents = StorageServiceItemSetEvent;\n\n/**\n * Actions from other messengers that {@link StorageService} calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events from other messengers that {@link StorageService} subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events that\n * {@link StorageService} needs to access.\n */\nexport type StorageServiceMessenger = Messenger<\n typeof SERVICE_NAME,\n StorageServiceActions | AllowedActions,\n StorageServiceEvents | AllowedEvents\n>;\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/storage-service",
3
- "version": "1.0.2-preview-8bfa290fb",
3
+ "version": "2.0.0-preview-0a30e47",
4
4
  "description": "Platform-agnostic service for storing large, infrequently accessed controller data",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -18,19 +18,12 @@
18
18
  "files": [
19
19
  "dist/"
20
20
  ],
21
+ "type": "module",
21
22
  "sideEffects": false,
22
- "main": "./dist/index.cjs",
23
- "types": "./dist/index.d.cts",
24
23
  "exports": {
25
24
  ".": {
26
- "import": {
27
- "types": "./dist/index.d.mts",
28
- "default": "./dist/index.mjs"
29
- },
30
- "require": {
31
- "types": "./dist/index.d.cts",
32
- "default": "./dist/index.cjs"
33
- }
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
34
27
  },
35
28
  "./package.json": "./package.json"
36
29
  },
@@ -39,9 +32,11 @@
39
32
  "registry": "https://registry.npmjs.org/"
40
33
  },
41
34
  "scripts": {
42
- "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
43
- "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
35
+ "build": "tsc --project tsconfig.build.json",
36
+ "build:all": "tsc --build tsconfig.build.json --verbose",
37
+ "build:clean": "yarn build:only-clean && yarn build",
44
38
  "build:docs": "typedoc",
39
+ "build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
45
40
  "changelog:update": "../../scripts/update-changelog.sh @metamask/storage-service",
46
41
  "changelog:validate": "../../scripts/validate-changelog.sh @metamask/storage-service",
47
42
  "lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
@@ -55,22 +50,23 @@
55
50
  "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
56
51
  },
57
52
  "dependencies": {
58
- "@metamask/messenger": "^2.0.0",
53
+ "@metamask/messenger": "^3.0.0",
59
54
  "@metamask/utils": "^11.12.0"
60
55
  },
61
56
  "devDependencies": {
62
57
  "@metamask/auto-changelog": "^6.1.0",
63
- "@ts-bridge/cli": "^0.6.4",
64
58
  "@types/jest": "^30.0.0",
59
+ "@typescript/native": "npm:typescript@^7.0.2",
65
60
  "deepmerge": "^4.2.2",
66
61
  "jest": "^30.4.2",
62
+ "rimraf": "^5.0.5",
67
63
  "ts-jest": "^29.4.11",
68
64
  "tsx": "^4.20.5",
69
65
  "typedoc": "^0.25.13",
70
66
  "typedoc-plugin-missing-exports": "^2.0.0",
71
- "typescript": "~5.3.3"
67
+ "typescript": "npm:@typescript/typescript6@^6.0.2"
72
68
  },
73
69
  "engines": {
74
- "node": "^18.18 || >=20"
70
+ "node": "^22.14.0 || ^24"
75
71
  }
76
72
  }