react-native-nitro-storage 0.4.5 → 0.5.1
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/README.md +254 -945
- package/SECURITY.md +26 -0
- package/docs/api-reference.md +281 -0
- package/docs/batch-transactions-migrations.md +200 -0
- package/docs/benchmarks.md +37 -0
- package/docs/mmkv-migration.md +80 -0
- package/docs/react-hooks.md +113 -0
- package/docs/recipes.md +302 -0
- package/docs/secure-storage.md +190 -0
- package/docs/web-backends.md +141 -0
- package/lib/commonjs/index.js +265 -14
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +220 -11
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/storage-events.js +117 -0
- package/lib/commonjs/storage-events.js.map +1 -0
- package/lib/commonjs/storage-runtime.js.map +1 -1
- package/lib/module/index.js +265 -14
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +220 -11
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/storage-events.js +112 -0
- package/lib/module/storage-events.js.map +1 -0
- package/lib/module/storage-runtime.js.map +1 -1
- package/lib/typescript/index.d.ts +19 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +19 -2
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/lib/typescript/storage-events.d.ts +37 -0
- package/lib/typescript/storage-events.d.ts.map +1 -0
- package/lib/typescript/storage-runtime.d.ts +32 -0
- package/lib/typescript/storage-runtime.d.ts.map +1 -1
- package/package.json +25 -11
- package/src/index.ts +601 -14
- package/src/index.web.ts +535 -22
- package/src/storage-events.ts +184 -0
- package/src/storage-runtime.ts +35 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { StorageScope } from "./Storage.types";
|
|
2
|
+
|
|
3
|
+
export type StorageChangeOperation =
|
|
4
|
+
| "set"
|
|
5
|
+
| "remove"
|
|
6
|
+
| "clear"
|
|
7
|
+
| "clearNamespace"
|
|
8
|
+
| "setBatch"
|
|
9
|
+
| "removeBatch"
|
|
10
|
+
| "import"
|
|
11
|
+
| "external";
|
|
12
|
+
|
|
13
|
+
export type StorageChangeSource = "memory" | "native" | "web" | "external";
|
|
14
|
+
|
|
15
|
+
export type StorageKeyChangeEvent = {
|
|
16
|
+
type: "key";
|
|
17
|
+
scope: StorageScope;
|
|
18
|
+
key: string;
|
|
19
|
+
oldValue: string | undefined;
|
|
20
|
+
newValue: string | undefined;
|
|
21
|
+
operation: StorageChangeOperation;
|
|
22
|
+
source: StorageChangeSource;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type StorageBatchChangeEvent = {
|
|
26
|
+
type: "batch";
|
|
27
|
+
scope: StorageScope;
|
|
28
|
+
operation: StorageChangeOperation;
|
|
29
|
+
source: StorageChangeSource;
|
|
30
|
+
changes: StorageKeyChangeEvent[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type StorageChangeEvent =
|
|
34
|
+
| StorageKeyChangeEvent
|
|
35
|
+
| StorageBatchChangeEvent;
|
|
36
|
+
|
|
37
|
+
export type StorageEventListener = (event: StorageChangeEvent) => void;
|
|
38
|
+
|
|
39
|
+
type PrefixListener = {
|
|
40
|
+
prefix: string;
|
|
41
|
+
listener: StorageEventListener;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function addListener<T>(
|
|
45
|
+
registry: Map<StorageScope, Set<T>>,
|
|
46
|
+
scope: StorageScope,
|
|
47
|
+
listener: T,
|
|
48
|
+
): () => void {
|
|
49
|
+
let listeners = registry.get(scope);
|
|
50
|
+
if (!listeners) {
|
|
51
|
+
listeners = new Set();
|
|
52
|
+
registry.set(scope, listeners);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
listeners.add(listener);
|
|
56
|
+
return () => {
|
|
57
|
+
const scopedListeners = registry.get(scope);
|
|
58
|
+
if (!scopedListeners) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
scopedListeners.delete(listener);
|
|
62
|
+
if (scopedListeners.size === 0) {
|
|
63
|
+
registry.delete(scope);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class StorageEventRegistry {
|
|
69
|
+
private readonly scopeListeners = new Map<
|
|
70
|
+
StorageScope,
|
|
71
|
+
Set<StorageEventListener>
|
|
72
|
+
>();
|
|
73
|
+
private readonly keyListeners = new Map<
|
|
74
|
+
StorageScope,
|
|
75
|
+
Map<string, Set<StorageEventListener>>
|
|
76
|
+
>();
|
|
77
|
+
private readonly prefixListeners = new Map<
|
|
78
|
+
StorageScope,
|
|
79
|
+
Set<PrefixListener>
|
|
80
|
+
>();
|
|
81
|
+
|
|
82
|
+
subscribe(scope: StorageScope, listener: StorageEventListener): () => void {
|
|
83
|
+
return addListener(this.scopeListeners, scope, listener);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
hasListeners(scope: StorageScope): boolean {
|
|
87
|
+
return (
|
|
88
|
+
(this.scopeListeners.get(scope)?.size ?? 0) > 0 ||
|
|
89
|
+
(this.keyListeners.get(scope)?.size ?? 0) > 0 ||
|
|
90
|
+
(this.prefixListeners.get(scope)?.size ?? 0) > 0
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
subscribeKey(
|
|
95
|
+
scope: StorageScope,
|
|
96
|
+
key: string,
|
|
97
|
+
listener: StorageEventListener,
|
|
98
|
+
): () => void {
|
|
99
|
+
let scopedListeners = this.keyListeners.get(scope);
|
|
100
|
+
if (!scopedListeners) {
|
|
101
|
+
scopedListeners = new Map();
|
|
102
|
+
this.keyListeners.set(scope, scopedListeners);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let keyListeners = scopedListeners.get(key);
|
|
106
|
+
if (!keyListeners) {
|
|
107
|
+
keyListeners = new Set();
|
|
108
|
+
scopedListeners.set(key, keyListeners);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
keyListeners.add(listener);
|
|
112
|
+
return () => {
|
|
113
|
+
const currentScopedListeners = this.keyListeners.get(scope);
|
|
114
|
+
const currentKeyListeners = currentScopedListeners?.get(key);
|
|
115
|
+
if (!currentKeyListeners) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
currentKeyListeners.delete(listener);
|
|
119
|
+
if (currentKeyListeners.size === 0) {
|
|
120
|
+
currentScopedListeners?.delete(key);
|
|
121
|
+
}
|
|
122
|
+
if (currentScopedListeners?.size === 0) {
|
|
123
|
+
this.keyListeners.delete(scope);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
subscribePrefix(
|
|
129
|
+
scope: StorageScope,
|
|
130
|
+
prefix: string,
|
|
131
|
+
listener: StorageEventListener,
|
|
132
|
+
): () => void {
|
|
133
|
+
const entry: PrefixListener = { prefix, listener };
|
|
134
|
+
return addListener(this.prefixListeners, scope, entry);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
emitKey(event: StorageKeyChangeEvent): void {
|
|
138
|
+
this.emitToScope(event);
|
|
139
|
+
this.emitToKey(event);
|
|
140
|
+
this.emitToPrefixes(event);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
emitBatch(event: StorageBatchChangeEvent): void {
|
|
144
|
+
this.emitToScope(event);
|
|
145
|
+
event.changes.forEach((change) => {
|
|
146
|
+
this.emitToKey(change);
|
|
147
|
+
});
|
|
148
|
+
this.emitBatchToPrefixes(event);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private emitToScope(event: StorageChangeEvent): void {
|
|
152
|
+
this.scopeListeners.get(event.scope)?.forEach((listener) => {
|
|
153
|
+
listener(event);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private emitToKey(event: StorageKeyChangeEvent): void {
|
|
158
|
+
this.keyListeners
|
|
159
|
+
.get(event.scope)
|
|
160
|
+
?.get(event.key)
|
|
161
|
+
?.forEach((listener) => {
|
|
162
|
+
listener(event);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private emitToPrefixes(event: StorageKeyChangeEvent): void {
|
|
167
|
+
this.prefixListeners.get(event.scope)?.forEach(({ prefix, listener }) => {
|
|
168
|
+
if (event.key.startsWith(prefix)) {
|
|
169
|
+
listener(event);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private emitBatchToPrefixes(event: StorageBatchChangeEvent): void {
|
|
175
|
+
this.prefixListeners.get(event.scope)?.forEach(({ prefix, listener }) => {
|
|
176
|
+
const changes = event.changes.filter((change) =>
|
|
177
|
+
change.key.startsWith(prefix),
|
|
178
|
+
);
|
|
179
|
+
if (changes.length > 0) {
|
|
180
|
+
listener({ ...event, changes });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|
package/src/storage-runtime.ts
CHANGED
|
@@ -19,6 +19,41 @@ export type StorageCapabilities = {
|
|
|
19
19
|
errorClassification: boolean;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
export type SecurityCapabilityStatus = "available" | "unavailable" | "unknown";
|
|
23
|
+
|
|
24
|
+
export type SecurityCapabilities = {
|
|
25
|
+
platform: "native" | "web";
|
|
26
|
+
secureStorage: {
|
|
27
|
+
backend: string;
|
|
28
|
+
encrypted: SecurityCapabilityStatus;
|
|
29
|
+
accessControl: SecurityCapabilityStatus;
|
|
30
|
+
keychainAccessGroup: SecurityCapabilityStatus;
|
|
31
|
+
hardwareBacked: SecurityCapabilityStatus;
|
|
32
|
+
};
|
|
33
|
+
biometric: {
|
|
34
|
+
storage: SecurityCapabilityStatus;
|
|
35
|
+
prompt: SecurityCapabilityStatus;
|
|
36
|
+
biometryOnly: SecurityCapabilityStatus;
|
|
37
|
+
biometryOrPasscode: SecurityCapabilityStatus;
|
|
38
|
+
};
|
|
39
|
+
metadata: {
|
|
40
|
+
perKey: boolean;
|
|
41
|
+
listsWithoutValues: boolean;
|
|
42
|
+
persistsTimestamps: boolean;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type SecureStorageMetadata = {
|
|
47
|
+
key: string;
|
|
48
|
+
exists: boolean;
|
|
49
|
+
kind: "secure" | "biometric" | "missing";
|
|
50
|
+
backend: string;
|
|
51
|
+
encrypted: SecurityCapabilityStatus;
|
|
52
|
+
hardwareBacked: SecurityCapabilityStatus;
|
|
53
|
+
biometricProtected: boolean;
|
|
54
|
+
valueExposed: false;
|
|
55
|
+
};
|
|
56
|
+
|
|
22
57
|
const STORAGE_ERROR_TAG_PATTERN = /\[nitro-error:([a-z_]+)\]/;
|
|
23
58
|
|
|
24
59
|
export function getStorageErrorCode(
|