@metamask-previews/storage-service 1.0.0-preview-f5c5aecd → 1.0.0-preview-81c9cf83
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.
- package/CHANGELOG.md +0 -8
- package/LICENSE +4 -18
- package/LICENSE.APACHE2 +201 -0
- package/LICENSE.MIT +21 -0
- package/README.md +41 -303
- package/dist/StorageService-method-action-types.cjs +7 -0
- package/dist/StorageService-method-action-types.cjs.map +1 -0
- package/dist/StorageService-method-action-types.d.cts +81 -0
- package/dist/StorageService-method-action-types.d.cts.map +1 -0
- package/dist/StorageService-method-action-types.d.mts +81 -0
- package/dist/StorageService-method-action-types.d.mts.map +1 -0
- package/dist/StorageService-method-action-types.mjs +6 -0
- package/dist/StorageService-method-action-types.mjs.map +1 -0
- package/dist/StorageService.cjs +34 -18
- package/dist/StorageService.cjs.map +1 -1
- package/dist/StorageService.d.cts +26 -9
- package/dist/StorageService.d.cts.map +1 -1
- package/dist/StorageService.d.mts +26 -9
- package/dist/StorageService.d.mts.map +1 -1
- package/dist/StorageService.mjs +34 -18
- package/dist/StorageService.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +23 -46
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +23 -46
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StorageService.mjs","sourceRoot":"","sources":["../src/StorageService.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAMlE,OAAO,EAAE,YAAY,EAAE,oBAAgB;AAEvC
|
|
1
|
+
{"version":3,"file":"StorageService.mjs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C,KAAK,EACL,GAAG,CACJ,CAAC;IACJ,CAAC;IAED;;;;;;;;;OASG;IACH,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,GAAW;QAC1C,iDAAiD;QACjD,OAAO,MAAM,uBAAA,IAAI,+BAAS,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACrD,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,UAAU,CAAC,SAAiB,EAAE,GAAW;QAC7C,iFAAiF;QACjF,MAAM,uBAAA,IAAI,+BAAS,CAAC,UAAU,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACjD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,UAAU,CAAC,SAAiB;QAChC,OAAO,MAAM,uBAAA,IAAI,+BAAS,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,KAAK,CAAC,SAAiB;QAC3B,MAAM,uBAAA,IAAI,+BAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACvC,CAAC;CACF","sourcesContent":["import { InMemoryStorageAdapter } from './InMemoryStorageAdapter';\nimport type {\n StorageAdapter,\n StorageServiceMessenger,\n StorageServiceOptions,\n} from './types';\nimport { SERVICE_NAME } from './types';\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 | null> {\n * const result = await this.messenger.call(\n * 'StorageService:getItem',\n * 'SnapController',\n * `${snapId}:sourceCode`,\n * );\n * return result as string | null; // Caller must validate/cast\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 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 - Data to store (should be 100KB+ for optimal use).\n */\n async setItem(namespace: string, key: string, value: unknown): 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: [value, key]\n this.#messenger.publish(\n `${SERVICE_NAME}:itemSet:${namespace}` as const,\n value,\n key,\n );\n }\n\n /**\n * Retrieve data from storage.\n *\n * Returns `unknown` since there's no schema validation.\n * Callers should validate or cast the result to the expected type.\n *\n * @param namespace - Controller namespace (e.g., 'SnapController').\n * @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').\n * @returns Parsed data or null if not found. Type is `unknown` - caller must validate.\n */\n async getItem(namespace: string, key: string): Promise<unknown> {\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"]}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AACvB,uDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAEvB,kBAAkB;AAClB,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uBAAuB;AACvB,uDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAEvB,kBAAkB;AAClB,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAqB/B,uDAAuD;AACvD,qCAA2D;AAAlD,qGAAA,YAAY,OAAA;AAAE,2GAAA,kBAAkB,OAAA","sourcesContent":["// Export service class\nexport { StorageService } from './StorageService';\n\n// Export adapters\nexport { InMemoryStorageAdapter } from './InMemoryStorageAdapter';\n\n// Export types from types.ts\nexport type {\n StorageAdapter,\n StorageServiceOptions,\n StorageServiceActions,\n StorageServiceEvents,\n StorageServiceMessenger,\n StorageServiceItemSetEvent,\n} from './types';\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';\n\n// Export service name and storage key prefix constants\nexport { SERVICE_NAME, STORAGE_KEY_PREFIX } from './types';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { StorageService } from "./StorageService.cjs";
|
|
2
2
|
export { InMemoryStorageAdapter } from "./InMemoryStorageAdapter.cjs";
|
|
3
|
-
export type { StorageAdapter, StorageServiceOptions, StorageServiceActions, StorageServiceEvents, StorageServiceMessenger,
|
|
3
|
+
export type { StorageAdapter, StorageServiceOptions, StorageServiceActions, StorageServiceEvents, StorageServiceMessenger, StorageServiceItemSetEvent, } from "./types.cjs";
|
|
4
|
+
export type { StorageServiceSetItemAction, StorageServiceGetItemAction, StorageServiceRemoveItemAction, StorageServiceGetAllKeysAction, StorageServiceClearAction, } from "./StorageService-method-action-types.cjs";
|
|
4
5
|
export { SERVICE_NAME, STORAGE_KEY_PREFIX } from "./types.cjs";
|
|
5
6
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAGlD,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAGlE,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,EAC3B,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAGlD,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAGlE,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,0BAA0B,GAC3B,oBAAgB;AAGjB,YAAY,EACV,2BAA2B,EAC3B,2BAA2B,EAC3B,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,GAC1B,iDAA6C;AAG9C,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,oBAAgB"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { StorageService } from "./StorageService.mjs";
|
|
2
2
|
export { InMemoryStorageAdapter } from "./InMemoryStorageAdapter.mjs";
|
|
3
|
-
export type { StorageAdapter, StorageServiceOptions, StorageServiceActions, StorageServiceEvents, StorageServiceMessenger,
|
|
3
|
+
export type { StorageAdapter, StorageServiceOptions, StorageServiceActions, StorageServiceEvents, StorageServiceMessenger, StorageServiceItemSetEvent, } from "./types.mjs";
|
|
4
|
+
export type { StorageServiceSetItemAction, StorageServiceGetItemAction, StorageServiceRemoveItemAction, StorageServiceGetAllKeysAction, StorageServiceClearAction, } from "./StorageService-method-action-types.mjs";
|
|
4
5
|
export { SERVICE_NAME, STORAGE_KEY_PREFIX } from "./types.mjs";
|
|
5
6
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAGlD,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAGlE,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,EAC3B,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAGlD,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAGlE,YAAY,EACV,cAAc,EACd,qBAAqB,EACrB,qBAAqB,EACrB,oBAAoB,EACpB,uBAAuB,EACvB,0BAA0B,GAC3B,oBAAgB;AAGjB,YAAY,EACV,2BAA2B,EAC3B,2BAA2B,EAC3B,8BAA8B,EAC9B,8BAA8B,EAC9B,yBAAyB,GAC1B,iDAA6C;AAG9C,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,oBAAgB"}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAElD,kBAAkB;AAClB,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uBAAuB;AACvB,OAAO,EAAE,cAAc,EAAE,6BAAyB;AAElD,kBAAkB;AAClB,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAqBlE,uDAAuD;AACvD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,oBAAgB","sourcesContent":["// Export service class\nexport { StorageService } from './StorageService';\n\n// Export adapters\nexport { InMemoryStorageAdapter } from './InMemoryStorageAdapter';\n\n// Export types from types.ts\nexport type {\n StorageAdapter,\n StorageServiceOptions,\n StorageServiceActions,\n StorageServiceEvents,\n StorageServiceMessenger,\n StorageServiceItemSetEvent,\n} from './types';\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';\n\n// Export service name and storage key prefix constants\nexport { SERVICE_NAME, STORAGE_KEY_PREFIX } from './types';\n"]}
|
package/dist/types.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AA2GA,wBAAwB;AACX,QAAA,YAAY,GAAG,gBAAgB,CAAC;AAE7C;;;;GAIG;AACU,QAAA,kBAAkB,GAAG,iBAAiB,CAAC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\n\nimport type { StorageServiceMethodActions } from './StorageService-method-action-types';\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 The value as a string, or null if not found.\n */\n getItem(namespace: string, key: string): Promise<unknown>;\n\n /**\n * Store a large 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 * - Wrapping value with metadata (timestamp, etc.)\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 value to store (will be wrapped and serialized by adapter).\n */\n setItem(namespace: string, key: string, value: unknown): 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', (value, key) => {\n * // value = the data that was set\n * // key = 'cache:0x1', 'cache:0x38', etc.\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: [value: unknown, key: string];\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/dist/types.d.cts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { Messenger } from "@metamask/messenger";
|
|
2
|
+
import type { StorageServiceMethodActions } from "./StorageService-method-action-types.cjs";
|
|
2
3
|
/**
|
|
3
4
|
* Platform-agnostic storage adapter interface.
|
|
4
5
|
* Each client (mobile, extension) implements this interface
|
|
5
6
|
* with their preferred storage mechanism.
|
|
6
7
|
*
|
|
8
|
+
* ⚠️ **Designed for large, infrequently accessed data (100KB+)**
|
|
9
|
+
*
|
|
10
|
+
* ✅ **Use for:**
|
|
11
|
+
* - Snap source code (~6 MB per snap)
|
|
12
|
+
* - Token metadata caches (~4 MB)
|
|
13
|
+
* - Large API response caches
|
|
14
|
+
*
|
|
15
|
+
* ❌ **Avoid for:**
|
|
16
|
+
* - Small values (< 10 KB) - use controller state instead
|
|
17
|
+
* - Frequently accessed data - use controller state instead
|
|
18
|
+
* - Many small key-value pairs - use a single large object instead
|
|
19
|
+
*
|
|
7
20
|
* @example Mobile implementation using FilesystemStorage
|
|
8
21
|
* @example Extension implementation using IndexedDB
|
|
9
22
|
* @example Tests using InMemoryStorageAdapter
|
|
@@ -19,7 +32,13 @@ export type StorageAdapter = {
|
|
|
19
32
|
*/
|
|
20
33
|
getItem(namespace: string, key: string): Promise<unknown>;
|
|
21
34
|
/**
|
|
22
|
-
* Store
|
|
35
|
+
* Store a large value in storage.
|
|
36
|
+
*
|
|
37
|
+
* ⚠️ **Store large values, not many small ones.**
|
|
38
|
+
* Each storage operation has I/O overhead. For best performance:
|
|
39
|
+
* - Store one large object rather than many small key-value pairs
|
|
40
|
+
* - Minimum recommended size: 100 KB per value
|
|
41
|
+
*
|
|
23
42
|
* Adapter is responsible for:
|
|
24
43
|
* - Building the full storage key
|
|
25
44
|
* - Wrapping value with metadata (timestamp, etc.)
|
|
@@ -84,45 +103,11 @@ export declare const SERVICE_NAME = "StorageService";
|
|
|
84
103
|
* Example: 'storageService:SnapController:snap-id:sourceCode'
|
|
85
104
|
*/
|
|
86
105
|
export declare const STORAGE_KEY_PREFIX = "storageService:";
|
|
87
|
-
/**
|
|
88
|
-
* Action for storing data in the storage service.
|
|
89
|
-
*/
|
|
90
|
-
export type StorageServiceSetItemAction = {
|
|
91
|
-
type: `${typeof SERVICE_NAME}:setItem`;
|
|
92
|
-
handler: <T>(namespace: string, key: string, value: T) => Promise<void>;
|
|
93
|
-
};
|
|
94
|
-
/**
|
|
95
|
-
* Action for retrieving data from the storage service.
|
|
96
|
-
*/
|
|
97
|
-
export type StorageServiceGetItemAction = {
|
|
98
|
-
type: `${typeof SERVICE_NAME}:getItem`;
|
|
99
|
-
handler: <T>(namespace: string, key: string) => Promise<T | null>;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Action for removing data from the storage service.
|
|
103
|
-
*/
|
|
104
|
-
export type StorageServiceRemoveItemAction = {
|
|
105
|
-
type: `${typeof SERVICE_NAME}:removeItem`;
|
|
106
|
-
handler: (namespace: string, key: string) => Promise<void>;
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Action for getting all keys for a namespace.
|
|
110
|
-
*/
|
|
111
|
-
export type StorageServiceGetAllKeysAction = {
|
|
112
|
-
type: `${typeof SERVICE_NAME}:getAllKeys`;
|
|
113
|
-
handler: (namespace: string) => Promise<string[]>;
|
|
114
|
-
};
|
|
115
|
-
/**
|
|
116
|
-
* Action for clearing all data for a namespace.
|
|
117
|
-
*/
|
|
118
|
-
export type StorageServiceClearAction = {
|
|
119
|
-
type: `${typeof SERVICE_NAME}:clear`;
|
|
120
|
-
handler: (namespace: string) => Promise<void>;
|
|
121
|
-
};
|
|
122
106
|
/**
|
|
123
107
|
* All actions that {@link StorageService} exposes to other consumers.
|
|
108
|
+
* Action types are auto-generated from the service methods.
|
|
124
109
|
*/
|
|
125
|
-
export type StorageServiceActions =
|
|
110
|
+
export type StorageServiceActions = StorageServiceMethodActions;
|
|
126
111
|
/**
|
|
127
112
|
* Event published when a storage item is set.
|
|
128
113
|
* Event type includes namespace only, key passed in payload.
|
|
@@ -142,18 +127,10 @@ export type StorageServiceItemSetEvent = {
|
|
|
142
127
|
type: `${typeof SERVICE_NAME}:itemSet:${string}`;
|
|
143
128
|
payload: [value: unknown, key: string];
|
|
144
129
|
};
|
|
145
|
-
/**
|
|
146
|
-
* Event published when a storage item is removed.
|
|
147
|
-
* Event type includes namespace only, key passed in payload.
|
|
148
|
-
*/
|
|
149
|
-
export type StorageServiceItemRemovedEvent = {
|
|
150
|
-
type: `${typeof SERVICE_NAME}:itemRemoved:${string}`;
|
|
151
|
-
payload: [key: string];
|
|
152
|
-
};
|
|
153
130
|
/**
|
|
154
131
|
* All events that {@link StorageService} publishes.
|
|
155
132
|
*/
|
|
156
|
-
export type StorageServiceEvents = StorageServiceItemSetEvent
|
|
133
|
+
export type StorageServiceEvents = StorageServiceItemSetEvent;
|
|
157
134
|
/**
|
|
158
135
|
* Actions from other messengers that {@link StorageService} calls.
|
|
159
136
|
*/
|
package/dist/types.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;A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;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"}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
import type { Messenger } from "@metamask/messenger";
|
|
2
|
+
import type { StorageServiceMethodActions } from "./StorageService-method-action-types.mjs";
|
|
2
3
|
/**
|
|
3
4
|
* Platform-agnostic storage adapter interface.
|
|
4
5
|
* Each client (mobile, extension) implements this interface
|
|
5
6
|
* with their preferred storage mechanism.
|
|
6
7
|
*
|
|
8
|
+
* ⚠️ **Designed for large, infrequently accessed data (100KB+)**
|
|
9
|
+
*
|
|
10
|
+
* ✅ **Use for:**
|
|
11
|
+
* - Snap source code (~6 MB per snap)
|
|
12
|
+
* - Token metadata caches (~4 MB)
|
|
13
|
+
* - Large API response caches
|
|
14
|
+
*
|
|
15
|
+
* ❌ **Avoid for:**
|
|
16
|
+
* - Small values (< 10 KB) - use controller state instead
|
|
17
|
+
* - Frequently accessed data - use controller state instead
|
|
18
|
+
* - Many small key-value pairs - use a single large object instead
|
|
19
|
+
*
|
|
7
20
|
* @example Mobile implementation using FilesystemStorage
|
|
8
21
|
* @example Extension implementation using IndexedDB
|
|
9
22
|
* @example Tests using InMemoryStorageAdapter
|
|
@@ -19,7 +32,13 @@ export type StorageAdapter = {
|
|
|
19
32
|
*/
|
|
20
33
|
getItem(namespace: string, key: string): Promise<unknown>;
|
|
21
34
|
/**
|
|
22
|
-
* Store
|
|
35
|
+
* Store a large value in storage.
|
|
36
|
+
*
|
|
37
|
+
* ⚠️ **Store large values, not many small ones.**
|
|
38
|
+
* Each storage operation has I/O overhead. For best performance:
|
|
39
|
+
* - Store one large object rather than many small key-value pairs
|
|
40
|
+
* - Minimum recommended size: 100 KB per value
|
|
41
|
+
*
|
|
23
42
|
* Adapter is responsible for:
|
|
24
43
|
* - Building the full storage key
|
|
25
44
|
* - Wrapping value with metadata (timestamp, etc.)
|
|
@@ -84,45 +103,11 @@ export declare const SERVICE_NAME = "StorageService";
|
|
|
84
103
|
* Example: 'storageService:SnapController:snap-id:sourceCode'
|
|
85
104
|
*/
|
|
86
105
|
export declare const STORAGE_KEY_PREFIX = "storageService:";
|
|
87
|
-
/**
|
|
88
|
-
* Action for storing data in the storage service.
|
|
89
|
-
*/
|
|
90
|
-
export type StorageServiceSetItemAction = {
|
|
91
|
-
type: `${typeof SERVICE_NAME}:setItem`;
|
|
92
|
-
handler: <T>(namespace: string, key: string, value: T) => Promise<void>;
|
|
93
|
-
};
|
|
94
|
-
/**
|
|
95
|
-
* Action for retrieving data from the storage service.
|
|
96
|
-
*/
|
|
97
|
-
export type StorageServiceGetItemAction = {
|
|
98
|
-
type: `${typeof SERVICE_NAME}:getItem`;
|
|
99
|
-
handler: <T>(namespace: string, key: string) => Promise<T | null>;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Action for removing data from the storage service.
|
|
103
|
-
*/
|
|
104
|
-
export type StorageServiceRemoveItemAction = {
|
|
105
|
-
type: `${typeof SERVICE_NAME}:removeItem`;
|
|
106
|
-
handler: (namespace: string, key: string) => Promise<void>;
|
|
107
|
-
};
|
|
108
|
-
/**
|
|
109
|
-
* Action for getting all keys for a namespace.
|
|
110
|
-
*/
|
|
111
|
-
export type StorageServiceGetAllKeysAction = {
|
|
112
|
-
type: `${typeof SERVICE_NAME}:getAllKeys`;
|
|
113
|
-
handler: (namespace: string) => Promise<string[]>;
|
|
114
|
-
};
|
|
115
|
-
/**
|
|
116
|
-
* Action for clearing all data for a namespace.
|
|
117
|
-
*/
|
|
118
|
-
export type StorageServiceClearAction = {
|
|
119
|
-
type: `${typeof SERVICE_NAME}:clear`;
|
|
120
|
-
handler: (namespace: string) => Promise<void>;
|
|
121
|
-
};
|
|
122
106
|
/**
|
|
123
107
|
* All actions that {@link StorageService} exposes to other consumers.
|
|
108
|
+
* Action types are auto-generated from the service methods.
|
|
124
109
|
*/
|
|
125
|
-
export type StorageServiceActions =
|
|
110
|
+
export type StorageServiceActions = StorageServiceMethodActions;
|
|
126
111
|
/**
|
|
127
112
|
* Event published when a storage item is set.
|
|
128
113
|
* Event type includes namespace only, key passed in payload.
|
|
@@ -142,18 +127,10 @@ export type StorageServiceItemSetEvent = {
|
|
|
142
127
|
type: `${typeof SERVICE_NAME}:itemSet:${string}`;
|
|
143
128
|
payload: [value: unknown, key: string];
|
|
144
129
|
};
|
|
145
|
-
/**
|
|
146
|
-
* Event published when a storage item is removed.
|
|
147
|
-
* Event type includes namespace only, key passed in payload.
|
|
148
|
-
*/
|
|
149
|
-
export type StorageServiceItemRemovedEvent = {
|
|
150
|
-
type: `${typeof SERVICE_NAME}:itemRemoved:${string}`;
|
|
151
|
-
payload: [key: string];
|
|
152
|
-
};
|
|
153
130
|
/**
|
|
154
131
|
* All events that {@link StorageService} publishes.
|
|
155
132
|
*/
|
|
156
|
-
export type StorageServiceEvents = StorageServiceItemSetEvent
|
|
133
|
+
export type StorageServiceEvents = StorageServiceItemSetEvent;
|
|
157
134
|
/**
|
|
158
135
|
* Actions from other messengers that {@link StorageService} calls.
|
|
159
136
|
*/
|
package/dist/types.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,2BAA2B,EAAE,iDAA6C;AAExF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9D;;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"}
|
package/dist/types.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA2GA,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';\n\nimport type { StorageServiceMethodActions } from './StorageService-method-action-types';\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 The value as a string, or null if not found.\n */\n getItem(namespace: string, key: string): Promise<unknown>;\n\n /**\n * Store a large 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 * - Wrapping value with metadata (timestamp, etc.)\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 value to store (will be wrapped and serialized by adapter).\n */\n setItem(namespace: string, key: string, value: unknown): 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', (value, key) => {\n * // value = the data that was set\n * // key = 'cache:0x1', 'cache:0x38', etc.\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: [value: unknown, key: string];\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.0-preview-
|
|
3
|
+
"version": "1.0.0-preview-81c9cf83",
|
|
4
4
|
"description": "Platform-agnostic service for storing large, infrequently accessed controller data",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"type": "git",
|
|
17
17
|
"url": "https://github.com/MetaMask/core.git"
|
|
18
18
|
},
|
|
19
|
-
"license": "MIT",
|
|
19
|
+
"license": "(MIT OR Apache-2.0)",
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|