@metamask-previews/storage-service 1.0.0-preview-e493d3e8 → 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/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-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 +203 -0
- package/dist/StorageService.cjs.map +1 -0
- package/dist/StorageService.d.cts +147 -0
- package/dist/StorageService.d.cts.map +1 -0
- package/dist/StorageService.d.mts +147 -0
- package/dist/StorageService.d.mts.map +1 -0
- package/dist/StorageService.mjs +199 -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 +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -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 +148 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +148 -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 +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -10,13 +10,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
12
|
- Initial release of `@metamask/storage-service`
|
|
13
|
-
- Add `StorageService` class for platform-agnostic storage
|
|
14
|
-
- Add `StorageAdapter` interface for platform-specific implementations
|
|
15
|
-
- Add `InMemoryStorageAdapter` as default storage (for tests/dev)
|
|
16
|
-
- Add namespace-based key isolation
|
|
17
|
-
- Add support for `setItem`, `getItem`, `removeItem`, `getAllKeys`, and `clear` operations
|
|
18
|
-
- Add messenger integration for cross-controller communication
|
|
19
|
-
- Add `STORAGE_KEY_PREFIX` constant for consistent key prefixing across adapters
|
|
20
|
-
- Add comprehensive test coverage
|
|
21
13
|
|
|
22
14
|
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
CHANGED
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
This project is licensed under either of
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
* MIT license ([LICENSE.MIT](LICENSE.MIT))
|
|
4
|
+
* Apache License, Version 2.0 ([LICENSE.APACHE2](LICENSE.APACHE2))
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
6
|
+
at your option.
|
package/LICENSE.APACHE2
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 MetaMask
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE.MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,22 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
A platform-agnostic service for storing large, infrequently accessed controller data outside of memory.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
Controllers store large, infrequently-accessed data in Redux state, causing:
|
|
8
|
-
- **State bloat**: 10.79 MB total, with 9.94 MB (92%) in just 2 controllers
|
|
9
|
-
- **Slow app startup**: Parsing 10.79 MB on every launch
|
|
10
|
-
- **High memory usage**: All data loaded, even if rarely accessed
|
|
11
|
-
- **Slow persist operations**: Up to 5.95 MB written per controller change
|
|
12
|
-
|
|
13
|
-
**Production measurements** (MetaMask Mobile):
|
|
14
|
-
- SnapController sourceCode: 5.95 MB (55% of state)
|
|
15
|
-
- TokenListController cache: 3.99 MB (37% of state)
|
|
16
|
-
- **Combined**: 9.94 MB in just 2 controllers
|
|
5
|
+
## When to Use
|
|
17
6
|
|
|
18
|
-
|
|
7
|
+
✅ **Use StorageService for:**
|
|
8
|
+
- Large data (> 100 KB)
|
|
9
|
+
- Infrequently accessed data
|
|
10
|
+
- Data that doesn't need to be in Redux state
|
|
11
|
+
- Examples: Snap source code, cached API responses
|
|
19
12
|
|
|
20
|
-
|
|
13
|
+
❌ **Don't use for:**
|
|
14
|
+
- Frequently accessed data (use controller state)
|
|
15
|
+
- Small data (< 10 KB - overhead not worth it)
|
|
16
|
+
- Data needed for UI rendering
|
|
21
17
|
|
|
22
18
|
## Installation
|
|
23
19
|
|
|
@@ -27,298 +23,77 @@ or
|
|
|
27
23
|
|
|
28
24
|
`npm install @metamask/storage-service`
|
|
29
25
|
|
|
30
|
-
## Architecture
|
|
31
|
-
|
|
32
|
-
The service is **platform-agnostic** and accepts an optional `StorageAdapter`:
|
|
33
|
-
|
|
34
|
-
- **With Adapter** (Production): Client provides platform-specific storage
|
|
35
|
-
- Mobile: FilesystemStorage adapter → Data persists
|
|
36
|
-
- Extension: IndexedDB adapter → Data persists
|
|
37
|
-
|
|
38
|
-
- **Without Adapter** (Default): Uses in-memory storage
|
|
39
|
-
- Testing: No setup needed, isolated tests
|
|
40
|
-
- Development: Quick start, no config
|
|
41
|
-
- ⚠️ Data lost on restart
|
|
42
|
-
|
|
43
|
-
## Events
|
|
44
|
-
|
|
45
|
-
StorageService publishes events when data changes, enabling reactive patterns:
|
|
46
|
-
|
|
47
|
-
**Events published**:
|
|
48
|
-
- `StorageService:itemSet:{namespace}` - When data is stored
|
|
49
|
-
- Payload: `[value, key]`
|
|
50
|
-
- `StorageService:itemRemoved:{namespace}` - When data is removed
|
|
51
|
-
- Payload: `[key]`
|
|
52
|
-
|
|
53
|
-
**Example - Subscribe to changes**:
|
|
54
|
-
```typescript
|
|
55
|
-
// In another controller
|
|
56
|
-
this.messenger.subscribe(
|
|
57
|
-
'StorageService:itemSet:ControllerA',
|
|
58
|
-
(value, key) => {
|
|
59
|
-
console.log(`ControllerA stored data: ${key}`);
|
|
60
|
-
// React to changes without coupling
|
|
61
|
-
},
|
|
62
|
-
);
|
|
63
|
-
```
|
|
64
|
-
|
|
65
26
|
## Usage
|
|
66
27
|
|
|
67
|
-
###
|
|
68
|
-
|
|
69
|
-
The service is designed to be used via a messenger, allowing controllers to access storage without direct dependencies.
|
|
70
|
-
|
|
71
|
-
#### 1. Controller Setup
|
|
28
|
+
### Controller Setup
|
|
72
29
|
|
|
73
30
|
```typescript
|
|
74
31
|
import type {
|
|
75
32
|
StorageServiceSetItemAction,
|
|
76
33
|
StorageServiceGetItemAction,
|
|
77
|
-
StorageServiceRemoveItemAction,
|
|
78
34
|
} from '@metamask/storage-service';
|
|
79
35
|
|
|
80
36
|
// Grant access to storage actions
|
|
81
37
|
type AllowedActions =
|
|
82
38
|
| StorageServiceSetItemAction
|
|
83
|
-
| StorageServiceGetItemAction
|
|
84
|
-
| StorageServiceRemoveItemAction;
|
|
39
|
+
| StorageServiceGetItemAction;
|
|
85
40
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
SnapControllerActions | AllowedActions,
|
|
89
|
-
SnapControllerEvents
|
|
90
|
-
>;
|
|
91
|
-
|
|
92
|
-
class SnapController extends BaseController<
|
|
93
|
-
'SnapController',
|
|
94
|
-
SnapControllerState,
|
|
95
|
-
SnapControllerMessenger
|
|
96
|
-
> {
|
|
97
|
-
async storeSnapSourceCode(snapId: string, sourceCode: string) {
|
|
98
|
-
// Store 3.86 MB of source code on disk, not in state
|
|
41
|
+
class MyController extends BaseController<...> {
|
|
42
|
+
async storeData(id: string, data: string) {
|
|
99
43
|
await this.messenger.call(
|
|
100
44
|
'StorageService:setItem',
|
|
101
|
-
'
|
|
102
|
-
`${
|
|
103
|
-
|
|
45
|
+
'MyController',
|
|
46
|
+
`${id}:data`,
|
|
47
|
+
data,
|
|
104
48
|
);
|
|
105
49
|
}
|
|
106
50
|
|
|
107
|
-
async
|
|
108
|
-
// Load source code only when snap needs to execute
|
|
51
|
+
async getData(id: string): Promise<unknown> {
|
|
109
52
|
return await this.messenger.call(
|
|
110
53
|
'StorageService:getItem',
|
|
111
|
-
'
|
|
112
|
-
`${
|
|
54
|
+
'MyController',
|
|
55
|
+
`${id}:data`,
|
|
113
56
|
);
|
|
114
57
|
}
|
|
115
58
|
}
|
|
116
59
|
```
|
|
117
60
|
|
|
118
|
-
|
|
61
|
+
### Service Initialization
|
|
119
62
|
|
|
120
|
-
|
|
63
|
+
The service accepts an optional `StorageAdapter` for platform-specific storage:
|
|
121
64
|
|
|
122
65
|
```typescript
|
|
123
|
-
import {
|
|
124
|
-
StorageService,
|
|
125
|
-
type StorageAdapter,
|
|
126
|
-
STORAGE_KEY_PREFIX,
|
|
127
|
-
} from '@metamask/storage-service';
|
|
128
|
-
import FilesystemStorage from 'redux-persist-filesystem-storage';
|
|
66
|
+
import { StorageService, type StorageAdapter } from '@metamask/storage-service';
|
|
129
67
|
|
|
130
|
-
//
|
|
131
|
-
const mobileStorageAdapter: StorageAdapter = {
|
|
132
|
-
async getItem(namespace: string, key: string) {
|
|
133
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
134
|
-
const serialized = await FilesystemStorage.getItem(fullKey);
|
|
135
|
-
if (!serialized) return null;
|
|
136
|
-
const wrapper = JSON.parse(serialized);
|
|
137
|
-
return wrapper.data;
|
|
138
|
-
},
|
|
139
|
-
async setItem(namespace: string, key: string, value: unknown) {
|
|
140
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
141
|
-
const wrapper = { timestamp: Date.now(), data: value };
|
|
142
|
-
await FilesystemStorage.setItem(fullKey, JSON.stringify(wrapper), Device.isIos());
|
|
143
|
-
},
|
|
144
|
-
async removeItem(namespace: string, key: string) {
|
|
145
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
146
|
-
await FilesystemStorage.removeItem(fullKey);
|
|
147
|
-
},
|
|
148
|
-
async getAllKeys(namespace: string) {
|
|
149
|
-
const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;
|
|
150
|
-
const allKeys = await FilesystemStorage.getAllKeys();
|
|
151
|
-
return allKeys
|
|
152
|
-
.filter((k: string) => k.startsWith(prefix))
|
|
153
|
-
.map((k: string) => k.slice(prefix.length));
|
|
154
|
-
},
|
|
155
|
-
async clear(namespace: string) {
|
|
156
|
-
const keys = await this.getAllKeys(namespace);
|
|
157
|
-
await Promise.all(keys.map((k) => this.removeItem(namespace, k)));
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
// Initialize service
|
|
162
|
-
const service = new StorageService({
|
|
163
|
-
messenger: storageServiceMessenger,
|
|
164
|
-
storage: mobileStorageAdapter,
|
|
165
|
-
});
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
**Extension:**
|
|
169
|
-
|
|
170
|
-
```typescript
|
|
171
|
-
import {
|
|
172
|
-
StorageService,
|
|
173
|
-
type StorageAdapter,
|
|
174
|
-
STORAGE_KEY_PREFIX,
|
|
175
|
-
} from '@metamask/storage-service';
|
|
176
|
-
|
|
177
|
-
// Adapters handle key building and serialization
|
|
178
|
-
const extensionStorageAdapter: StorageAdapter = {
|
|
179
|
-
async getItem(namespace: string, key: string) {
|
|
180
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
181
|
-
const db = await openDB();
|
|
182
|
-
const serialized = await db.get('storage-service', fullKey);
|
|
183
|
-
if (!serialized) return null;
|
|
184
|
-
const wrapper = JSON.parse(serialized);
|
|
185
|
-
return wrapper.data;
|
|
186
|
-
},
|
|
187
|
-
async setItem(namespace: string, key: string, value: unknown) {
|
|
188
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
189
|
-
const wrapper = { timestamp: Date.now(), data: value };
|
|
190
|
-
const db = await openDB();
|
|
191
|
-
await db.put('storage-service', JSON.stringify(wrapper), fullKey);
|
|
192
|
-
},
|
|
193
|
-
async removeItem(namespace: string, key: string) {
|
|
194
|
-
const fullKey = `${STORAGE_KEY_PREFIX}${namespace}:${key}`;
|
|
195
|
-
const db = await openDB();
|
|
196
|
-
await db.delete('storage-service', fullKey);
|
|
197
|
-
},
|
|
198
|
-
async getAllKeys(namespace: string) {
|
|
199
|
-
const prefix = `${STORAGE_KEY_PREFIX}${namespace}:`;
|
|
200
|
-
const db = await openDB();
|
|
201
|
-
const allKeys = await db.getAllKeys('storage-service');
|
|
202
|
-
return allKeys
|
|
203
|
-
.filter((k: string) => k.startsWith(prefix))
|
|
204
|
-
.map((k: string) => k.slice(prefix.length));
|
|
205
|
-
},
|
|
206
|
-
async clear(namespace: string) {
|
|
207
|
-
const keys = await this.getAllKeys(namespace);
|
|
208
|
-
await Promise.all(keys.map((k) => this.removeItem(namespace, k)));
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
// Initialize service
|
|
68
|
+
// Production: Provide a platform-specific adapter
|
|
213
69
|
const service = new StorageService({
|
|
214
70
|
messenger: storageServiceMessenger,
|
|
215
|
-
storage:
|
|
71
|
+
storage: myPlatformAdapter, // FilesystemStorage, IndexedDB, etc.
|
|
216
72
|
});
|
|
217
|
-
```
|
|
218
73
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
```typescript
|
|
222
|
-
import { StorageService } from '@metamask/storage-service';
|
|
223
|
-
|
|
224
|
-
// No storage adapter needed - uses in-memory by default
|
|
225
|
-
const service = new StorageService({
|
|
74
|
+
// Testing: Uses in-memory storage by default
|
|
75
|
+
const testService = new StorageService({
|
|
226
76
|
messenger: testMessenger,
|
|
227
|
-
//
|
|
77
|
+
// No adapter needed - data isolated per test
|
|
228
78
|
});
|
|
229
|
-
|
|
230
|
-
// Works immediately, data isolated per test
|
|
231
|
-
await service.setItem('TestController', 'key', 'value');
|
|
232
79
|
```
|
|
233
80
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
```typescript
|
|
237
|
-
rootMessenger.delegate({
|
|
238
|
-
actions: [
|
|
239
|
-
'StorageService:setItem',
|
|
240
|
-
'StorageService:getItem',
|
|
241
|
-
'StorageService:removeItem',
|
|
242
|
-
],
|
|
243
|
-
messenger: snapControllerMessenger,
|
|
244
|
-
});
|
|
245
|
-
```
|
|
81
|
+
### Events
|
|
246
82
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
You can also use the service directly without a messenger:
|
|
250
|
-
|
|
251
|
-
```typescript
|
|
252
|
-
import { StorageService, InMemoryStorageAdapter } from '@metamask/storage-service';
|
|
253
|
-
|
|
254
|
-
const service = new StorageService({
|
|
255
|
-
messenger: myMessenger,
|
|
256
|
-
storage: new InMemoryStorageAdapter(),
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
await service.setItem('MyController', 'myKey', { data: 'value' });
|
|
260
|
-
const data = await service.getItem('MyController', 'myKey');
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
## API
|
|
264
|
-
|
|
265
|
-
### `StorageService`
|
|
266
|
-
|
|
267
|
-
#### `setItem<T>(namespace: string, key: string, value: T): Promise<void>`
|
|
268
|
-
|
|
269
|
-
Store data in storage.
|
|
270
|
-
|
|
271
|
-
- `namespace` - Controller namespace (e.g., 'SnapController')
|
|
272
|
-
- `key` - Storage key (e.g., 'npm:@metamask/bitcoin-wallet-snap:sourceCode')
|
|
273
|
-
- `value` - Data to store (will be JSON stringified)
|
|
274
|
-
|
|
275
|
-
```typescript
|
|
276
|
-
await service.setItem('SnapController', 'snap-id:sourceCode', sourceCode);
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
#### `getItem<T>(namespace: string, key: string): Promise<T | null>`
|
|
280
|
-
|
|
281
|
-
Retrieve data from storage.
|
|
282
|
-
|
|
283
|
-
- `namespace` - Controller namespace
|
|
284
|
-
- `key` - Storage key
|
|
285
|
-
- **Returns**: Parsed data or null if not found
|
|
286
|
-
|
|
287
|
-
```typescript
|
|
288
|
-
const sourceCode = await service.getItem('SnapController', 'snap-id:sourceCode');
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
#### `removeItem(namespace: string, key: string): Promise<void>`
|
|
292
|
-
|
|
293
|
-
Remove data from storage.
|
|
294
|
-
|
|
295
|
-
```typescript
|
|
296
|
-
await service.removeItem('SnapController', 'snap-id:sourceCode');
|
|
297
|
-
```
|
|
298
|
-
|
|
299
|
-
#### `getAllKeys(namespace: string): Promise<string[]>`
|
|
300
|
-
|
|
301
|
-
Get all keys for a namespace (without prefix).
|
|
302
|
-
|
|
303
|
-
```typescript
|
|
304
|
-
const keys = await service.getAllKeys('SnapController');
|
|
305
|
-
// Returns: ['snap-id-1:sourceCode', 'snap-id-2:sourceCode', ...]
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
#### `clear(namespace: string): Promise<void>`
|
|
309
|
-
|
|
310
|
-
Clear all data for a namespace.
|
|
83
|
+
Subscribe to storage changes:
|
|
311
84
|
|
|
312
85
|
```typescript
|
|
313
|
-
|
|
86
|
+
this.messenger.subscribe(
|
|
87
|
+
'StorageService:itemSet:MyController',
|
|
88
|
+
(value, key) => {
|
|
89
|
+
console.log(`Data stored: ${key}`);
|
|
90
|
+
},
|
|
91
|
+
);
|
|
314
92
|
```
|
|
315
93
|
|
|
316
94
|
## StorageAdapter Interface
|
|
317
95
|
|
|
318
|
-
Implement this interface to provide platform-specific storage
|
|
319
|
-
- Building the full storage key (e.g., `storageService:namespace:key`)
|
|
320
|
-
- Wrapping data with metadata (timestamp) before serialization
|
|
321
|
-
- Serializing/deserializing data (JSON.stringify/parse)
|
|
96
|
+
Implement this interface to provide platform-specific storage:
|
|
322
97
|
|
|
323
98
|
```typescript
|
|
324
99
|
export type StorageAdapter = {
|
|
@@ -330,48 +105,11 @@ export type StorageAdapter = {
|
|
|
330
105
|
};
|
|
331
106
|
```
|
|
332
107
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
-
|
|
337
|
-
- Infrequently accessed data
|
|
338
|
-
- Data that doesn't need to be in Redux state
|
|
339
|
-
- Examples: Snap source code (6 MB), cached API responses (4 MB)
|
|
340
|
-
|
|
341
|
-
❌ **Don't use for:**
|
|
342
|
-
- Frequently accessed data (use controller state)
|
|
343
|
-
- Small data (< 10 KB - overhead not worth it)
|
|
344
|
-
- Data needed for UI rendering
|
|
345
|
-
- Critical data that must be in Redux
|
|
346
|
-
|
|
347
|
-
## Storage Key Format
|
|
348
|
-
|
|
349
|
-
Adapters build keys with prefix: `storageService:{namespace}:{key}`
|
|
350
|
-
|
|
351
|
-
Examples:
|
|
352
|
-
- `storageService:SnapController:npm:@metamask/bitcoin-wallet-snap:sourceCode`
|
|
353
|
-
- `storageService:TokenListController:cache:0x1`
|
|
354
|
-
|
|
355
|
-
This provides:
|
|
356
|
-
- Namespace isolation (prevents collisions)
|
|
357
|
-
- Easy debugging (clear key format)
|
|
358
|
-
- Scoped clearing (clear removes all keys for controller)
|
|
359
|
-
|
|
360
|
-
## Real-World Impact
|
|
361
|
-
|
|
362
|
-
**Production measurements** (MetaMask Mobile):
|
|
363
|
-
|
|
364
|
-
**Per-controller**:
|
|
365
|
-
- SnapController: 5.95 MB sourceCode → 166 KB metadata (97% reduction)
|
|
366
|
-
- TokenListController: 3.99 MB cache → 61 bytes metadata (99.9% reduction)
|
|
367
|
-
|
|
368
|
-
**Combined**:
|
|
369
|
-
- Total state: 10.79 MB → 0.85 MB (**92% reduction**)
|
|
370
|
-
- App startup: 92% less data to parse
|
|
371
|
-
- Memory freed: 9.94 MB
|
|
372
|
-
- Disk I/O: Up to 9.94 MB less per persist operation
|
|
108
|
+
Adapters are responsible for:
|
|
109
|
+
- Building the full storage key (e.g., `storageService:namespace:key`)
|
|
110
|
+
- Wrapping data with metadata before serialization
|
|
111
|
+
- Serializing/deserializing data
|
|
373
112
|
|
|
374
113
|
## Contributing
|
|
375
114
|
|
|
376
115
|
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
377
|
-
|