Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
278eaa9
feat: Add StorageService for offloading large controller data
andrepimenta Nov 19, 2025
e844fdd
refactor(storage-service): Move getAllKeys/clear logic to adapters
andrepimenta Nov 19, 2025
19c7d6f
refactor: Adapters now build storage keys (not core)
andrepimenta Nov 25, 2025
5fedbc0
refactor(storage-service): delegate key building and serialization to…
andrepimenta Nov 25, 2025
5af0861
docs(storage-service): update CHANGELOG for initial release
andrepimenta Nov 25, 2025
de5388c
docs(storage-service): fix CHANGELOG format
andrepimenta Nov 25, 2025
e493d3e
docs(storage-service): update README with current API and precise met…
andrepimenta Nov 25, 2025
81100c2
build: add storage-service to tsconfig.build.json
andrepimenta Nov 26, 2025
f5c5aec
Merge branch 'main' into storage-service
andrepimenta Nov 26, 2025
04939f6
docs(storage-service): add JSDoc guidance for large value storage
andrepimenta Nov 26, 2025
3fe4314
Update packages/storage-service/src/StorageService.ts
andrepimenta Nov 26, 2025
84adfd5
Update packages/storage-service/src/StorageService.ts
andrepimenta Nov 26, 2025
7ac8bfc
refactor(storage-service): remove itemRemoved events, keep only itemSet
andrepimenta Nov 27, 2025
24b150e
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 27, 2025
65efd41
chore(storage-service): add dual MIT+Apache2 license
andrepimenta Nov 27, 2025
60efad1
refactor(storage-service): use unknown instead of generic types
andrepimenta Nov 27, 2025
2222e57
refactor(storage-service): use generate-method-action-types pattern
andrepimenta Nov 27, 2025
ce68a05
Update packages/storage-service/package.json
andrepimenta Nov 27, 2025
afde837
docs(storage-service): simplify README to focus on usage
andrepimenta Nov 27, 2025
ae501a3
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 27, 2025
81c9cf8
docs(storage-service): simplify CHANGELOG for initial release
andrepimenta Nov 27, 2025
625eee6
fix(storage-service): change event payload order to [key, value]
andrepimenta Nov 27, 2025
bda7243
Merge branch 'main' into storage-service
andrepimenta Nov 27, 2025
d3a7e4e
Fix prettier
andrepimenta Nov 27, 2025
389e50a
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 27, 2025
3aeafac
Fix keywords
andrepimenta Nov 27, 2025
c46e2e2
Update packages/storage-service/package.json
andrepimenta Nov 27, 2025
1b48670
test(storage-service): add tests for itemSet event
andrepimenta Nov 27, 2025
8050f20
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 27, 2025
9a65054
refactor(storage-service): use Json type instead of unknown
andrepimenta Nov 27, 2025
d6fe459
chore(storage-service): add CODEOWNERS and teams.json entries
andrepimenta Nov 27, 2025
5b4faeb
Update packages/storage-service/src/StorageService.ts
andrepimenta Nov 28, 2025
363533a
Update packages/storage-service/src/InMemoryStorageAdapter.ts
andrepimenta Nov 28, 2025
f5ccb18
Update packages/storage-service/src/InMemoryStorageAdapter.ts
andrepimenta Nov 28, 2025
b5c5d79
feat(storage-service): add StorageGetResult type for getItem responses
andrepimenta Nov 28, 2025
8557995
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 28, 2025
7ccd865
Update packages/storage-service/CHANGELOG.md
andrepimenta Nov 28, 2025
a139816
fix(storage-service): fix prettier formatting in test
andrepimenta Nov 28, 2025
5598df9
Merge branch 'storage-service' of https://github.com/MetaMask/core in…
andrepimenta Nov 28, 2025
bbd8e39
Merge branch 'main' into storage-service
andrepimenta Nov 28, 2025
cf634fb
Prettier fix
andrepimenta Nov 28, 2025
6bf384a
Fix return types on StorageServiceGetItemAction
andrepimenta Nov 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(storage-service): use unknown instead of generic types
Since there's no schema validation, using generics gives false type safety.
- setItem now accepts value: unknown
- getItem now returns Promise<unknown>
- Callers must validate/cast the returned data
- Updated JSDoc examples to show casting pattern
  • Loading branch information
andrepimenta committed Nov 27, 2025
commit 60efad1bbfa3fc717139f90d9a90833eefb5e2a7
13 changes: 5 additions & 8 deletions packages/storage-service/src/StorageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ describe('StorageService', () => {
const { service } = getService();

await service.setItem('TestController', 'testKey', { data: 'test' });
const result = await service.getItem<{ data: string }>(
'TestController',
'testKey',
);
const result = await service.getItem('TestController', 'testKey');

expect(result).toStrictEqual({ data: 'test' });
});
Expand Down Expand Up @@ -174,7 +171,7 @@ describe('StorageService', () => {
const { service } = getService();

await service.setItem('TestController', 'string', 'simple string');
const result = await service.getItem<string>('TestController', 'string');
const result = await service.getItem('TestController', 'string');

expect(result).toBe('simple string');
});
Expand All @@ -183,7 +180,7 @@ describe('StorageService', () => {
const { service } = getService();

await service.setItem('TestController', 'number', 42);
const result = await service.getItem<number>('TestController', 'number');
const result = await service.getItem('TestController', 'number');

expect(result).toBe(42);
});
Expand All @@ -193,7 +190,7 @@ describe('StorageService', () => {
const array = [1, 2, 3];

await service.setItem('TestController', 'array', array);
const result = await service.getItem<number[]>('TestController', 'array');
const result = await service.getItem('TestController', 'array');

expect(result).toStrictEqual(array);
});
Expand Down Expand Up @@ -466,7 +463,7 @@ describe('StorageService', () => {
expect(keys).toHaveLength(5);

// Retrieve specific snap source code
const bitcoinSource = await service.getItem<string>(
const bitcoinSource = await service.getItem(
'SnapController',
'npm:@metamask/bitcoin-wallet-snap:sourceCode',
);
Expand Down
19 changes: 10 additions & 9 deletions packages/storage-service/src/StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ import { SERVICE_NAME } from './types';
* }
*
* async getSnapSourceCode(snapId: string): Promise<string | null> {
* return await this.messenger.call(
* const result = await this.messenger.call(
* 'StorageService:getItem',
* 'SnapController',
* `${snapId}:sourceCode`,
* );
* return result as string | null; // Caller must validate/cast
* }
* }
* ```
Expand Down Expand Up @@ -159,11 +160,10 @@ export class StorageService {
* @param namespace - Controller namespace (e.g., 'SnapController').
* @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').
* @param value - Data to store (should be 100KB+ for optimal use).
* @template T - The type of the value being stored.
*/
async setItem<T>(namespace: string, key: string, value: T): Promise<void> {
async setItem(namespace: string, key: string, value: unknown): Promise<void> {
// Adapter handles serialization and wrapping with metadata
await this.#storage.setItem(namespace, key, value as never);
await this.#storage.setItem(namespace, key, value);

// Publish event so other controllers can react to changes
// Event type: StorageService:itemSet:namespace
Expand All @@ -178,15 +178,16 @@ export class StorageService {
/**
* Retrieve data from storage.
*
* Returns `unknown` since there's no schema validation.
* Callers should validate or cast the result to the expected type.
*
* @param namespace - Controller namespace (e.g., 'SnapController').
* @param key - Storage key (e.g., 'npm:@metamask/example-snap:sourceCode').
* @returns Parsed data or null if not found.
* @template T - The type of the value being retrieved.
* @returns Parsed data or null if not found. Type is `unknown` - caller must validate.
*/
async getItem<T>(namespace: string, key: string): Promise<T | null> {
async getItem(namespace: string, key: string): Promise<unknown> {
// Adapter handles deserialization and unwrapping
const result = await this.#storage.getItem(namespace, key);
return result as T | null;
return await this.#storage.getItem(namespace, key);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/storage-service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,17 @@ export const STORAGE_KEY_PREFIX = 'storageService:';
*/
export type StorageServiceSetItemAction = {
type: `${typeof SERVICE_NAME}:setItem`;
handler: <T>(namespace: string, key: string, value: T) => Promise<void>;
handler: (namespace: string, key: string, value: unknown) => Promise<void>;
};

/**
* Action for retrieving data from the storage service.
* Returns `unknown` since there's no schema validation.
* Callers should validate/cast the result.
*/
export type StorageServiceGetItemAction = {
type: `${typeof SERVICE_NAME}:getItem`;
handler: <T>(namespace: string, key: string) => Promise<T | null>;
handler: (namespace: string, key: string) => Promise<unknown>;
};

/**
Expand Down
Loading