@metamask-previews/storage-service 1.0.0-preview-e493d3e8 → 1.0.0-preview-f5c5aecd
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/dist/InMemoryStorageAdapter.cjs +129 -0
- package/dist/InMemoryStorageAdapter.cjs.map +1 -0
- package/dist/InMemoryStorageAdapter.d.cts +71 -0
- package/dist/InMemoryStorageAdapter.d.cts.map +1 -0
- package/dist/InMemoryStorageAdapter.d.mts +71 -0
- package/dist/InMemoryStorageAdapter.d.mts.map +1 -0
- package/dist/InMemoryStorageAdapter.mjs +125 -0
- package/dist/InMemoryStorageAdapter.mjs.map +1 -0
- package/dist/StorageService.cjs +187 -0
- package/dist/StorageService.cjs.map +1 -0
- package/dist/StorageService.d.cts +130 -0
- package/dist/StorageService.d.cts.map +1 -0
- package/dist/StorageService.d.mts +130 -0
- package/dist/StorageService.d.mts.map +1 -0
- package/dist/StorageService.mjs +183 -0
- package/dist/StorageService.mjs.map +1 -0
- package/dist/index.cjs +14 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +7 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.cjs +12 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +171 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +171 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +9 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +1 -1
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { Messenger } from "@metamask/messenger";
|
|
2
|
+
/**
|
|
3
|
+
* Platform-agnostic storage adapter interface.
|
|
4
|
+
* Each client (mobile, extension) implements this interface
|
|
5
|
+
* with their preferred storage mechanism.
|
|
6
|
+
*
|
|
7
|
+
* @example Mobile implementation using FilesystemStorage
|
|
8
|
+
* @example Extension implementation using IndexedDB
|
|
9
|
+
* @example Tests using InMemoryStorageAdapter
|
|
10
|
+
*/
|
|
11
|
+
export type StorageAdapter = {
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve an item from storage.
|
|
14
|
+
* Adapter is responsible for building the full storage key.
|
|
15
|
+
*
|
|
16
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
17
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
18
|
+
* @returns The value as a string, or null if not found.
|
|
19
|
+
*/
|
|
20
|
+
getItem(namespace: string, key: string): Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Store an item in storage.
|
|
23
|
+
* Adapter is responsible for:
|
|
24
|
+
* - Building the full storage key
|
|
25
|
+
* - Wrapping value with metadata (timestamp, etc.)
|
|
26
|
+
* - Serializing to string (JSON.stringify)
|
|
27
|
+
*
|
|
28
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
29
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
30
|
+
* @param value - The value to store (will be wrapped and serialized by adapter).
|
|
31
|
+
*/
|
|
32
|
+
setItem(namespace: string, key: string, value: unknown): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Remove an item from storage.
|
|
35
|
+
* Adapter is responsible for building the full storage key.
|
|
36
|
+
*
|
|
37
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
38
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
39
|
+
*/
|
|
40
|
+
removeItem(namespace: string, key: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Get all keys for a specific namespace.
|
|
43
|
+
* Should return keys without the 'storage:namespace:' prefix.
|
|
44
|
+
*
|
|
45
|
+
* Adapter is responsible for:
|
|
46
|
+
* - Filtering keys by prefix: 'storage:{namespace}:'
|
|
47
|
+
* - Stripping the prefix from returned keys
|
|
48
|
+
* - Returning only the key portion after the prefix
|
|
49
|
+
*
|
|
50
|
+
* @param namespace - The namespace to get keys for (e.g., 'SnapController').
|
|
51
|
+
* @returns Array of keys without prefix (e.g., ['snap1:sourceCode', 'snap2:sourceCode']).
|
|
52
|
+
*/
|
|
53
|
+
getAllKeys(namespace: string): Promise<string[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Clear all items for a specific namespace.
|
|
56
|
+
*
|
|
57
|
+
* Adapter is responsible for:
|
|
58
|
+
* - Finding all keys with prefix: 'storageService:{namespace}:'
|
|
59
|
+
* - Removing all matching keys
|
|
60
|
+
*
|
|
61
|
+
* @param namespace - The namespace to clear (e.g., 'SnapController').
|
|
62
|
+
*/
|
|
63
|
+
clear(namespace: string): Promise<void>;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Options for constructing a {@link StorageService}.
|
|
67
|
+
*/
|
|
68
|
+
export type StorageServiceOptions = {
|
|
69
|
+
/**
|
|
70
|
+
* The messenger suited for this service.
|
|
71
|
+
*/
|
|
72
|
+
messenger: StorageServiceMessenger;
|
|
73
|
+
/**
|
|
74
|
+
* Storage adapter for persisting data.
|
|
75
|
+
* If not provided, uses in-memory storage (data lost on restart).
|
|
76
|
+
* Production clients MUST provide a persistent storage adapter.
|
|
77
|
+
*/
|
|
78
|
+
storage?: StorageAdapter;
|
|
79
|
+
};
|
|
80
|
+
export declare const SERVICE_NAME = "StorageService";
|
|
81
|
+
/**
|
|
82
|
+
* Storage key prefix for all keys managed by StorageService.
|
|
83
|
+
* Keys are formatted as: {STORAGE_KEY_PREFIX}{namespace}:{key}
|
|
84
|
+
* Example: 'storageService:SnapController:snap-id:sourceCode'
|
|
85
|
+
*/
|
|
86
|
+
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
|
+
/**
|
|
123
|
+
* All actions that {@link StorageService} exposes to other consumers.
|
|
124
|
+
*/
|
|
125
|
+
export type StorageServiceActions = StorageServiceSetItemAction | StorageServiceGetItemAction | StorageServiceRemoveItemAction | StorageServiceGetAllKeysAction | StorageServiceClearAction;
|
|
126
|
+
/**
|
|
127
|
+
* Event published when a storage item is set.
|
|
128
|
+
* Event type includes namespace only, key passed in payload.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* Subscribe to all changes in TokenListController:
|
|
132
|
+
* messenger.subscribe('StorageService:itemSet:TokenListController', (value, key) => {
|
|
133
|
+
* // value = the data that was set
|
|
134
|
+
* // key = 'cache:0x1', 'cache:0x38', etc.
|
|
135
|
+
* if (key.startsWith('cache:')) {
|
|
136
|
+
* const chainId = key.replace('cache:', '');
|
|
137
|
+
* // React to cache change for specific chain
|
|
138
|
+
* }
|
|
139
|
+
* });
|
|
140
|
+
*/
|
|
141
|
+
export type StorageServiceItemSetEvent = {
|
|
142
|
+
type: `${typeof SERVICE_NAME}:itemSet:${string}`;
|
|
143
|
+
payload: [value: unknown, key: string];
|
|
144
|
+
};
|
|
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
|
+
/**
|
|
154
|
+
* All events that {@link StorageService} publishes.
|
|
155
|
+
*/
|
|
156
|
+
export type StorageServiceEvents = StorageServiceItemSetEvent | StorageServiceItemRemovedEvent;
|
|
157
|
+
/**
|
|
158
|
+
* Actions from other messengers that {@link StorageService} calls.
|
|
159
|
+
*/
|
|
160
|
+
type AllowedActions = never;
|
|
161
|
+
/**
|
|
162
|
+
* Events from other messengers that {@link StorageService} subscribes to.
|
|
163
|
+
*/
|
|
164
|
+
type AllowedEvents = never;
|
|
165
|
+
/**
|
|
166
|
+
* The messenger restricted to actions and events that
|
|
167
|
+
* {@link StorageService} needs to access.
|
|
168
|
+
*/
|
|
169
|
+
export type StorageServiceMessenger = Messenger<typeof SERVICE_NAME, StorageServiceActions | AllowedActions, StorageServiceEvents | AllowedEvents>;
|
|
170
|
+
export {};
|
|
171
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc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C1B,8BAA8B,CAAC;AAEnC;;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
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { Messenger } from "@metamask/messenger";
|
|
2
|
+
/**
|
|
3
|
+
* Platform-agnostic storage adapter interface.
|
|
4
|
+
* Each client (mobile, extension) implements this interface
|
|
5
|
+
* with their preferred storage mechanism.
|
|
6
|
+
*
|
|
7
|
+
* @example Mobile implementation using FilesystemStorage
|
|
8
|
+
* @example Extension implementation using IndexedDB
|
|
9
|
+
* @example Tests using InMemoryStorageAdapter
|
|
10
|
+
*/
|
|
11
|
+
export type StorageAdapter = {
|
|
12
|
+
/**
|
|
13
|
+
* Retrieve an item from storage.
|
|
14
|
+
* Adapter is responsible for building the full storage key.
|
|
15
|
+
*
|
|
16
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
17
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
18
|
+
* @returns The value as a string, or null if not found.
|
|
19
|
+
*/
|
|
20
|
+
getItem(namespace: string, key: string): Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Store an item in storage.
|
|
23
|
+
* Adapter is responsible for:
|
|
24
|
+
* - Building the full storage key
|
|
25
|
+
* - Wrapping value with metadata (timestamp, etc.)
|
|
26
|
+
* - Serializing to string (JSON.stringify)
|
|
27
|
+
*
|
|
28
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
29
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
30
|
+
* @param value - The value to store (will be wrapped and serialized by adapter).
|
|
31
|
+
*/
|
|
32
|
+
setItem(namespace: string, key: string, value: unknown): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Remove an item from storage.
|
|
35
|
+
* Adapter is responsible for building the full storage key.
|
|
36
|
+
*
|
|
37
|
+
* @param namespace - The controller namespace (e.g., 'SnapController').
|
|
38
|
+
* @param key - The data key (e.g., 'snap-id:sourceCode').
|
|
39
|
+
*/
|
|
40
|
+
removeItem(namespace: string, key: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Get all keys for a specific namespace.
|
|
43
|
+
* Should return keys without the 'storage:namespace:' prefix.
|
|
44
|
+
*
|
|
45
|
+
* Adapter is responsible for:
|
|
46
|
+
* - Filtering keys by prefix: 'storage:{namespace}:'
|
|
47
|
+
* - Stripping the prefix from returned keys
|
|
48
|
+
* - Returning only the key portion after the prefix
|
|
49
|
+
*
|
|
50
|
+
* @param namespace - The namespace to get keys for (e.g., 'SnapController').
|
|
51
|
+
* @returns Array of keys without prefix (e.g., ['snap1:sourceCode', 'snap2:sourceCode']).
|
|
52
|
+
*/
|
|
53
|
+
getAllKeys(namespace: string): Promise<string[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Clear all items for a specific namespace.
|
|
56
|
+
*
|
|
57
|
+
* Adapter is responsible for:
|
|
58
|
+
* - Finding all keys with prefix: 'storageService:{namespace}:'
|
|
59
|
+
* - Removing all matching keys
|
|
60
|
+
*
|
|
61
|
+
* @param namespace - The namespace to clear (e.g., 'SnapController').
|
|
62
|
+
*/
|
|
63
|
+
clear(namespace: string): Promise<void>;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Options for constructing a {@link StorageService}.
|
|
67
|
+
*/
|
|
68
|
+
export type StorageServiceOptions = {
|
|
69
|
+
/**
|
|
70
|
+
* The messenger suited for this service.
|
|
71
|
+
*/
|
|
72
|
+
messenger: StorageServiceMessenger;
|
|
73
|
+
/**
|
|
74
|
+
* Storage adapter for persisting data.
|
|
75
|
+
* If not provided, uses in-memory storage (data lost on restart).
|
|
76
|
+
* Production clients MUST provide a persistent storage adapter.
|
|
77
|
+
*/
|
|
78
|
+
storage?: StorageAdapter;
|
|
79
|
+
};
|
|
80
|
+
export declare const SERVICE_NAME = "StorageService";
|
|
81
|
+
/**
|
|
82
|
+
* Storage key prefix for all keys managed by StorageService.
|
|
83
|
+
* Keys are formatted as: {STORAGE_KEY_PREFIX}{namespace}:{key}
|
|
84
|
+
* Example: 'storageService:SnapController:snap-id:sourceCode'
|
|
85
|
+
*/
|
|
86
|
+
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
|
+
/**
|
|
123
|
+
* All actions that {@link StorageService} exposes to other consumers.
|
|
124
|
+
*/
|
|
125
|
+
export type StorageServiceActions = StorageServiceSetItemAction | StorageServiceGetItemAction | StorageServiceRemoveItemAction | StorageServiceGetAllKeysAction | StorageServiceClearAction;
|
|
126
|
+
/**
|
|
127
|
+
* Event published when a storage item is set.
|
|
128
|
+
* Event type includes namespace only, key passed in payload.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* Subscribe to all changes in TokenListController:
|
|
132
|
+
* messenger.subscribe('StorageService:itemSet:TokenListController', (value, key) => {
|
|
133
|
+
* // value = the data that was set
|
|
134
|
+
* // key = 'cache:0x1', 'cache:0x38', etc.
|
|
135
|
+
* if (key.startsWith('cache:')) {
|
|
136
|
+
* const chainId = key.replace('cache:', '');
|
|
137
|
+
* // React to cache change for specific chain
|
|
138
|
+
* }
|
|
139
|
+
* });
|
|
140
|
+
*/
|
|
141
|
+
export type StorageServiceItemSetEvent = {
|
|
142
|
+
type: `${typeof SERVICE_NAME}:itemSet:${string}`;
|
|
143
|
+
payload: [value: unknown, key: string];
|
|
144
|
+
};
|
|
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
|
+
/**
|
|
154
|
+
* All events that {@link StorageService} publishes.
|
|
155
|
+
*/
|
|
156
|
+
export type StorageServiceEvents = StorageServiceItemSetEvent | StorageServiceItemRemovedEvent;
|
|
157
|
+
/**
|
|
158
|
+
* Actions from other messengers that {@link StorageService} calls.
|
|
159
|
+
*/
|
|
160
|
+
type AllowedActions = never;
|
|
161
|
+
/**
|
|
162
|
+
* Events from other messengers that {@link StorageService} subscribes to.
|
|
163
|
+
*/
|
|
164
|
+
type AllowedEvents = never;
|
|
165
|
+
/**
|
|
166
|
+
* The messenger restricted to actions and events that
|
|
167
|
+
* {@link StorageService} needs to access.
|
|
168
|
+
*/
|
|
169
|
+
export type StorageServiceMessenger = Messenger<typeof SERVICE_NAME, StorageServiceActions | AllowedActions, StorageServiceEvents | AllowedEvents>;
|
|
170
|
+
export {};
|
|
171
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD;;;;;;;;GAQG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B;;;;;;;OAOG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAE1D;;;;;;;;;;OAUG;IACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvE;;;;;;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;A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,8BAA8B,GAC9B,8BAA8B,GAC9B,yBAAyB,CAAC;AAE9B;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,IAAI,EAAE,GAAG,OAAO,YAAY,YAAY,MAAM,EAAE,CAAC;IACjD,OAAO,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;CACxC,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,IAAI,EAAE,GAAG,OAAO,YAAY,gBAAgB,MAAM,EAAE,CAAC;IACrD,OAAO,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;CACxB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAC5B,0BAA0B,GAC1B,8BAA8B,CAAC;AAEnC;;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
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Service name constant
|
|
2
|
+
export const SERVICE_NAME = 'StorageService';
|
|
3
|
+
/**
|
|
4
|
+
* Storage key prefix for all keys managed by StorageService.
|
|
5
|
+
* Keys are formatted as: {STORAGE_KEY_PREFIX}{namespace}:{key}
|
|
6
|
+
* Example: 'storageService:SnapController:snap-id:sourceCode'
|
|
7
|
+
*/
|
|
8
|
+
export const STORAGE_KEY_PREFIX = 'storageService:';
|
|
9
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAuFA,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\n/**\n * Platform-agnostic storage adapter interface.\n * Each client (mobile, extension) implements this interface\n * with their preferred storage mechanism.\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 an item in storage.\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 * Action for storing data in the storage service.\n */\nexport type StorageServiceSetItemAction = {\n type: `${typeof SERVICE_NAME}:setItem`;\n handler: <T>(namespace: string, key: string, value: T) => Promise<void>;\n};\n\n/**\n * Action for retrieving data from the storage service.\n */\nexport type StorageServiceGetItemAction = {\n type: `${typeof SERVICE_NAME}:getItem`;\n handler: <T>(namespace: string, key: string) => Promise<T | null>;\n};\n\n/**\n * Action for removing data from the storage service.\n */\nexport type StorageServiceRemoveItemAction = {\n type: `${typeof SERVICE_NAME}:removeItem`;\n handler: (namespace: string, key: string) => Promise<void>;\n};\n\n/**\n * Action for getting all keys for a namespace.\n */\nexport type StorageServiceGetAllKeysAction = {\n type: `${typeof SERVICE_NAME}:getAllKeys`;\n handler: (namespace: string) => Promise<string[]>;\n};\n\n/**\n * Action for clearing all data for a namespace.\n */\nexport type StorageServiceClearAction = {\n type: `${typeof SERVICE_NAME}:clear`;\n handler: (namespace: string) => Promise<void>;\n};\n\n/**\n * All actions that {@link StorageService} exposes to other consumers.\n */\nexport type StorageServiceActions =\n | StorageServiceSetItemAction\n | StorageServiceGetItemAction\n | StorageServiceRemoveItemAction\n | StorageServiceGetAllKeysAction\n | StorageServiceClearAction;\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 * Event published when a storage item is removed.\n * Event type includes namespace only, key passed in payload.\n */\nexport type StorageServiceItemRemovedEvent = {\n type: `${typeof SERVICE_NAME}:itemRemoved:${string}`;\n payload: [key: string];\n};\n\n/**\n * All events that {@link StorageService} publishes.\n */\nexport type StorageServiceEvents =\n | StorageServiceItemSetEvent\n | StorageServiceItemRemovedEvent;\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