@amplitude/analytics-react-native 1.5.54 → 1.5.55
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/lib/commonjs/storage/local-storage.js +78 -6
- package/lib/commonjs/storage/local-storage.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/module/storage/local-storage.js +78 -5
- package/lib/module/storage/local-storage.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/typescript/storage/local-storage.d.ts.map +1 -1
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/storage/local-storage.ts +88 -5
- package/src/version.ts +1 -1
|
@@ -5,14 +5,62 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.LocalStorage = void 0;
|
|
7
7
|
var _analyticsCore = require("@amplitude/analytics-core");
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
// Three sentinel states:
|
|
9
|
+
// undefined - not tried yet
|
|
10
|
+
// null - tried, package isn't available (don't retry)
|
|
11
|
+
// object - tried, succeeded
|
|
12
|
+
//
|
|
13
|
+
// Resolve AsyncStorage lazily so the module can be opted out of via custom
|
|
14
|
+
// `storageProvider` + `react-native.config.js` autolinking exclusion without
|
|
15
|
+
// the SDK throwing at module-load time.
|
|
16
|
+
//
|
|
17
|
+
// The result is cached for the app lifetime: `require()` is synchronous in
|
|
18
|
+
// React Native and the module registry is stable, so retrying after a failed
|
|
19
|
+
// resolution would never produce a different result. Caching the failure as
|
|
20
|
+
// `null` lets us skip re-running `require()` on every storage call — Metro
|
|
21
|
+
// doesn't cache failed module resolutions, so without this we'd re-throw on
|
|
22
|
+
// every storage call. See https://nodejs.org/api/modules.html#requirecache
|
|
23
|
+
// for the success-case caching that `require()` gives us for free.
|
|
24
|
+
let asyncStorage = undefined;
|
|
25
|
+
const getAsyncStorage = () => {
|
|
26
|
+
if (asyncStorage !== undefined) {
|
|
27
|
+
return asyncStorage;
|
|
28
|
+
}
|
|
29
|
+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
30
|
+
try {
|
|
31
|
+
const mod = require('@react-native-async-storage/async-storage');
|
|
32
|
+
// Handles both ES-module (`{ default: AsyncStorage }`) and direct-export
|
|
33
|
+
// shapes — e.g. `jest.mock(..., () => mockAsyncStorage)` returns the mock
|
|
34
|
+
// directly without a `default` wrapper. The outer `?? null` ensures we
|
|
35
|
+
// never cache a falsy success value, which would be misread as "not tried".
|
|
36
|
+
asyncStorage = (mod === null || mod === void 0 ? void 0 : mod.default) ?? mod ?? null;
|
|
37
|
+
} catch (e) {
|
|
38
|
+
asyncStorage = null;
|
|
39
|
+
// Only swallow "this exact package is not installed" silently — that's
|
|
40
|
+
// the supported opt-out path. Anything else — including a `MODULE_NOT_FOUND`
|
|
41
|
+
// that's actually about a transitive dependency, or syntax/eval errors in
|
|
42
|
+
// the package itself — should be surfaced so it can be diagnosed instead of
|
|
43
|
+
// silently degrading to in-memory storage.
|
|
44
|
+
const code = e === null || e === void 0 ? void 0 : e.code;
|
|
45
|
+
const message = e instanceof Error ? e.message : '';
|
|
46
|
+
const ourPackageMissing = code === 'MODULE_NOT_FOUND' && message.includes('@react-native-async-storage/async-storage');
|
|
47
|
+
if (!ourPackageMissing) {
|
|
48
|
+
// eslint-disable-next-line no-console
|
|
49
|
+
console.warn('[Amplitude] Failed to load @react-native-async-storage/async-storage; persistence is disabled.', e);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
53
|
+
return asyncStorage;
|
|
54
|
+
};
|
|
10
55
|
class LocalStorage {
|
|
11
56
|
async isEnabled() {
|
|
12
57
|
/* istanbul ignore if */
|
|
13
58
|
if (!(0, _analyticsCore.getGlobalScope)()) {
|
|
14
59
|
return false;
|
|
15
60
|
}
|
|
61
|
+
if (!getAsyncStorage()) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
16
64
|
const random = String(Date.now());
|
|
17
65
|
const testStorage = new LocalStorage();
|
|
18
66
|
const testKey = 'AMP_TEST';
|
|
@@ -41,25 +89,49 @@ class LocalStorage {
|
|
|
41
89
|
}
|
|
42
90
|
}
|
|
43
91
|
async getRaw(key) {
|
|
44
|
-
|
|
92
|
+
const storage = getAsyncStorage();
|
|
93
|
+
if (!storage) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
return (await storage.getItem(key)) || undefined;
|
|
98
|
+
} catch {
|
|
99
|
+
// AsyncStorage's JS package is resolvable but the native bridge is null
|
|
100
|
+
// (e.g. customer excluded it via autolinking but the JS package is still
|
|
101
|
+
// in node_modules). Callers like `parseOldCookies` consume `getRaw`
|
|
102
|
+
// directly without their own try/catch, so we must not propagate.
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
45
105
|
}
|
|
46
106
|
async set(key, value) {
|
|
107
|
+
const storage = getAsyncStorage();
|
|
108
|
+
if (!storage) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
47
111
|
try {
|
|
48
|
-
await
|
|
112
|
+
await storage.setItem(key, JSON.stringify(value));
|
|
49
113
|
} catch {
|
|
50
114
|
//
|
|
51
115
|
}
|
|
52
116
|
}
|
|
53
117
|
async remove(key) {
|
|
118
|
+
const storage = getAsyncStorage();
|
|
119
|
+
if (!storage) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
54
122
|
try {
|
|
55
|
-
await
|
|
123
|
+
await storage.removeItem(key);
|
|
56
124
|
} catch {
|
|
57
125
|
//
|
|
58
126
|
}
|
|
59
127
|
}
|
|
60
128
|
async reset() {
|
|
129
|
+
const storage = getAsyncStorage();
|
|
130
|
+
if (!storage) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
61
133
|
try {
|
|
62
|
-
await
|
|
134
|
+
await storage.clear();
|
|
63
135
|
} catch {
|
|
64
136
|
//
|
|
65
137
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_analyticsCore","require","
|
|
1
|
+
{"version":3,"names":["_analyticsCore","require","asyncStorage","undefined","getAsyncStorage","mod","default","e","code","message","Error","ourPackageMissing","includes","console","warn","LocalStorage","isEnabled","getGlobalScope","random","String","Date","now","testStorage","testKey","set","value","get","remove","key","getRaw","JSON","parse","storage","getItem","setItem","stringify","removeItem","reset","clear","exports"],"sourceRoot":"../../../src","sources":["storage/local-storage.ts"],"mappings":";;;;;;AAAA,IAAAA,cAAA,GAAAC,OAAA;AASA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIC,YAAiD,GAAGC,SAAS;AAEjE,MAAMC,eAAe,GAAGA,CAAA,KAA2C;EACjE,IAAIF,YAAY,KAAKC,SAAS,EAAE;IAC9B,OAAOD,YAAY;EACrB;EACA;EACA,IAAI;IACF,MAAMG,GAAG,GAAGJ,OAAO,CAAC,2CAA2C,CAAC;IAChE;IACA;IACA;IACA;IACAC,YAAY,GAAG,CAAAG,GAAG,aAAHA,GAAG,uBAAHA,GAAG,CAAEC,OAAO,KAAID,GAAG,IAAI,IAAI;EAC5C,CAAC,CAAC,OAAOE,CAAC,EAAE;IACVL,YAAY,GAAG,IAAI;IACnB;IACA;IACA;IACA;IACA;IACA,MAAMM,IAAI,GAAID,CAAC,aAADA,CAAC,uBAADA,CAAC,CAAwCC,IAAI;IAC3D,MAAMC,OAAO,GAAGF,CAAC,YAAYG,KAAK,GAAGH,CAAC,CAACE,OAAO,GAAG,EAAE;IACnD,MAAME,iBAAiB,GACrBH,IAAI,KAAK,kBAAkB,IAAIC,OAAO,CAACG,QAAQ,CAAC,2CAA2C,CAAC;IAC9F,IAAI,CAACD,iBAAiB,EAAE;MACtB;MACAE,OAAO,CAACC,IAAI,CAAC,gGAAgG,EAAEP,CAAC,CAAC;IACnH;EACF;EACA;EAC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MACN;MACA;MACA;MACA;MACA,OAAOA,SAAS;IAClB;EACF;EAEA,MAAMqB,GAAGA,CAACI,GAAW,EAAEH,KAAQ,EAAiB;IAC9C,MAAMO,OAAO,GAAG5B,eAAe,CAAC,CAAC;IACjC,IAAI,CAAC4B,OAAO,EAAE;MACZ;IACF;IACA,IAAI;MACF,MAAMA,OAAO,CAACE,OAAO,CAACN,GAAG,EAAEE,IAAI,CAACK,SAAS,CAACV,KAAK,CAAC,CAAC;IACnD,CAAC,CAAC,MAAM;MACN;IAAA;EAEJ;EAEA,MAAME,MAAMA,CAACC,GAAW,EAAiB;IACvC,MAAMI,OAAO,GAAG5B,eAAe,CAAC,CAAC;IACjC,IAAI,CAAC4B,OAAO,EAAE;MACZ;IACF;IACA,IAAI;MACF,MAAMA,OAAO,CAACI,UAAU,CAACR,GAAG,CAAC;IAC/B,CAAC,CAAC,MAAM;MACN;IAAA;EAEJ;EAEA,MAAMS,KAAKA,CAAA,EAAkB;IAC3B,MAAML,OAAO,GAAG5B,eAAe,CAAC,CAAC;IACjC,IAAI,CAAC4B,OAAO,EAAE;MACZ;IACF;IACA,IAAI;MACF,MAAMA,OAAO,CAACM,KAAK,CAAC,CAAC;IACvB,CAAC,CAAC,MAAM;MACN;IAAA;EAEJ;AACF;AAACC,OAAA,CAAAxB,YAAA,GAAAA,YAAA","ignoreList":[]}
|
package/lib/commonjs/version.js
CHANGED
|
@@ -1,11 +1,60 @@
|
|
|
1
1
|
import { getGlobalScope } from '@amplitude/analytics-core';
|
|
2
|
-
|
|
2
|
+
// Three sentinel states:
|
|
3
|
+
// undefined - not tried yet
|
|
4
|
+
// null - tried, package isn't available (don't retry)
|
|
5
|
+
// object - tried, succeeded
|
|
6
|
+
//
|
|
7
|
+
// Resolve AsyncStorage lazily so the module can be opted out of via custom
|
|
8
|
+
// `storageProvider` + `react-native.config.js` autolinking exclusion without
|
|
9
|
+
// the SDK throwing at module-load time.
|
|
10
|
+
//
|
|
11
|
+
// The result is cached for the app lifetime: `require()` is synchronous in
|
|
12
|
+
// React Native and the module registry is stable, so retrying after a failed
|
|
13
|
+
// resolution would never produce a different result. Caching the failure as
|
|
14
|
+
// `null` lets us skip re-running `require()` on every storage call — Metro
|
|
15
|
+
// doesn't cache failed module resolutions, so without this we'd re-throw on
|
|
16
|
+
// every storage call. See https://nodejs.org/api/modules.html#requirecache
|
|
17
|
+
// for the success-case caching that `require()` gives us for free.
|
|
18
|
+
let asyncStorage = undefined;
|
|
19
|
+
const getAsyncStorage = () => {
|
|
20
|
+
if (asyncStorage !== undefined) {
|
|
21
|
+
return asyncStorage;
|
|
22
|
+
}
|
|
23
|
+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
24
|
+
try {
|
|
25
|
+
const mod = require('@react-native-async-storage/async-storage');
|
|
26
|
+
// Handles both ES-module (`{ default: AsyncStorage }`) and direct-export
|
|
27
|
+
// shapes — e.g. `jest.mock(..., () => mockAsyncStorage)` returns the mock
|
|
28
|
+
// directly without a `default` wrapper. The outer `?? null` ensures we
|
|
29
|
+
// never cache a falsy success value, which would be misread as "not tried".
|
|
30
|
+
asyncStorage = (mod === null || mod === void 0 ? void 0 : mod.default) ?? mod ?? null;
|
|
31
|
+
} catch (e) {
|
|
32
|
+
asyncStorage = null;
|
|
33
|
+
// Only swallow "this exact package is not installed" silently — that's
|
|
34
|
+
// the supported opt-out path. Anything else — including a `MODULE_NOT_FOUND`
|
|
35
|
+
// that's actually about a transitive dependency, or syntax/eval errors in
|
|
36
|
+
// the package itself — should be surfaced so it can be diagnosed instead of
|
|
37
|
+
// silently degrading to in-memory storage.
|
|
38
|
+
const code = e === null || e === void 0 ? void 0 : e.code;
|
|
39
|
+
const message = e instanceof Error ? e.message : '';
|
|
40
|
+
const ourPackageMissing = code === 'MODULE_NOT_FOUND' && message.includes('@react-native-async-storage/async-storage');
|
|
41
|
+
if (!ourPackageMissing) {
|
|
42
|
+
// eslint-disable-next-line no-console
|
|
43
|
+
console.warn('[Amplitude] Failed to load @react-native-async-storage/async-storage; persistence is disabled.', e);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
47
|
+
return asyncStorage;
|
|
48
|
+
};
|
|
3
49
|
export class LocalStorage {
|
|
4
50
|
async isEnabled() {
|
|
5
51
|
/* istanbul ignore if */
|
|
6
52
|
if (!getGlobalScope()) {
|
|
7
53
|
return false;
|
|
8
54
|
}
|
|
55
|
+
if (!getAsyncStorage()) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
9
58
|
const random = String(Date.now());
|
|
10
59
|
const testStorage = new LocalStorage();
|
|
11
60
|
const testKey = 'AMP_TEST';
|
|
@@ -34,25 +83,49 @@ export class LocalStorage {
|
|
|
34
83
|
}
|
|
35
84
|
}
|
|
36
85
|
async getRaw(key) {
|
|
37
|
-
|
|
86
|
+
const storage = getAsyncStorage();
|
|
87
|
+
if (!storage) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
return (await storage.getItem(key)) || undefined;
|
|
92
|
+
} catch {
|
|
93
|
+
// AsyncStorage's JS package is resolvable but the native bridge is null
|
|
94
|
+
// (e.g. customer excluded it via autolinking but the JS package is still
|
|
95
|
+
// in node_modules). Callers like `parseOldCookies` consume `getRaw`
|
|
96
|
+
// directly without their own try/catch, so we must not propagate.
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
38
99
|
}
|
|
39
100
|
async set(key, value) {
|
|
101
|
+
const storage = getAsyncStorage();
|
|
102
|
+
if (!storage) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
40
105
|
try {
|
|
41
|
-
await
|
|
106
|
+
await storage.setItem(key, JSON.stringify(value));
|
|
42
107
|
} catch {
|
|
43
108
|
//
|
|
44
109
|
}
|
|
45
110
|
}
|
|
46
111
|
async remove(key) {
|
|
112
|
+
const storage = getAsyncStorage();
|
|
113
|
+
if (!storage) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
47
116
|
try {
|
|
48
|
-
await
|
|
117
|
+
await storage.removeItem(key);
|
|
49
118
|
} catch {
|
|
50
119
|
//
|
|
51
120
|
}
|
|
52
121
|
}
|
|
53
122
|
async reset() {
|
|
123
|
+
const storage = getAsyncStorage();
|
|
124
|
+
if (!storage) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
54
127
|
try {
|
|
55
|
-
await
|
|
128
|
+
await storage.clear();
|
|
56
129
|
} catch {
|
|
57
130
|
//
|
|
58
131
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["getGlobalScope","
|
|
1
|
+
{"version":3,"names":["getGlobalScope","asyncStorage","undefined","getAsyncStorage","mod","require","default","e","code","message","Error","ourPackageMissing","includes","console","warn","LocalStorage","isEnabled","random","String","Date","now","testStorage","testKey","set","value","get","remove","key","getRaw","JSON","parse","storage","getItem","setItem","stringify","removeItem","reset","clear"],"sourceRoot":"../../../src","sources":["storage/local-storage.ts"],"mappings":"AAAA,SAAkBA,cAAc,QAAQ,2BAA2B;AASnE;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAIC,YAAiD,GAAGC,SAAS;AAEjE,MAAMC,eAAe,GAAGA,CAAA,KAA2C;EACjE,IAAIF,YAAY,KAAKC,SAAS,EAAE;IAC9B,OAAOD,YAAY;EACrB;EACA;EACA,IAAI;IACF,MAAMG,GAAG,GAAGC,OAAO,CAAC,2CAA2C,CAAC;IAChE;IACA;IACA;IACA;IACAJ,YAAY,GAAG,CAAAG,GAAG,aAAHA,GAAG,uBAAHA,GAAG,CAAEE,OAAO,KAAIF,GAAG,IAAI,IAAI;EAC5C,CAAC,CAAC,OAAOG,CAAC,EAAE;IACVN,YAAY,GAAG,IAAI;IACnB;IACA;IACA;IACA;IACA;IACA,MA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|
1
|
+
export const VERSION = '1.5.55';
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/local-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"local-storage.d.ts","sourceRoot":"","sources":["../../../src/storage/local-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,2BAA2B,CAAC;AA2DpE,qBAAa,YAAY,CAAC,CAAC,CAAE,YAAW,OAAO,CAAC,CAAC,CAAC;IAC1C,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAwB7B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAcxC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC;IAgBhD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAYzC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAYlC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAW7B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "1.5.
|
|
1
|
+
export declare const VERSION = "1.5.55";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
import { Storage, getGlobalScope } from '@amplitude/analytics-core';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
interface AsyncStorageLike {
|
|
4
|
+
getItem(key: string): Promise<string | null>;
|
|
5
|
+
setItem(key: string, value: string): Promise<void>;
|
|
6
|
+
removeItem(key: string): Promise<void>;
|
|
7
|
+
clear(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Three sentinel states:
|
|
11
|
+
// undefined - not tried yet
|
|
12
|
+
// null - tried, package isn't available (don't retry)
|
|
13
|
+
// object - tried, succeeded
|
|
14
|
+
//
|
|
15
|
+
// Resolve AsyncStorage lazily so the module can be opted out of via custom
|
|
16
|
+
// `storageProvider` + `react-native.config.js` autolinking exclusion without
|
|
17
|
+
// the SDK throwing at module-load time.
|
|
18
|
+
//
|
|
19
|
+
// The result is cached for the app lifetime: `require()` is synchronous in
|
|
20
|
+
// React Native and the module registry is stable, so retrying after a failed
|
|
21
|
+
// resolution would never produce a different result. Caching the failure as
|
|
22
|
+
// `null` lets us skip re-running `require()` on every storage call — Metro
|
|
23
|
+
// doesn't cache failed module resolutions, so without this we'd re-throw on
|
|
24
|
+
// every storage call. See https://nodejs.org/api/modules.html#requirecache
|
|
25
|
+
// for the success-case caching that `require()` gives us for free.
|
|
26
|
+
let asyncStorage: AsyncStorageLike | null | undefined = undefined;
|
|
27
|
+
|
|
28
|
+
const getAsyncStorage = (): AsyncStorageLike | null | undefined => {
|
|
29
|
+
if (asyncStorage !== undefined) {
|
|
30
|
+
return asyncStorage;
|
|
31
|
+
}
|
|
32
|
+
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
33
|
+
try {
|
|
34
|
+
const mod = require('@react-native-async-storage/async-storage');
|
|
35
|
+
// Handles both ES-module (`{ default: AsyncStorage }`) and direct-export
|
|
36
|
+
// shapes — e.g. `jest.mock(..., () => mockAsyncStorage)` returns the mock
|
|
37
|
+
// directly without a `default` wrapper. The outer `?? null` ensures we
|
|
38
|
+
// never cache a falsy success value, which would be misread as "not tried".
|
|
39
|
+
asyncStorage = mod?.default ?? mod ?? null;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
asyncStorage = null;
|
|
42
|
+
// Only swallow "this exact package is not installed" silently — that's
|
|
43
|
+
// the supported opt-out path. Anything else — including a `MODULE_NOT_FOUND`
|
|
44
|
+
// that's actually about a transitive dependency, or syntax/eval errors in
|
|
45
|
+
// the package itself — should be surfaced so it can be diagnosed instead of
|
|
46
|
+
// silently degrading to in-memory storage.
|
|
47
|
+
const code = (e as NodeJS.ErrnoException | undefined)?.code;
|
|
48
|
+
const message = e instanceof Error ? e.message : '';
|
|
49
|
+
const ourPackageMissing =
|
|
50
|
+
code === 'MODULE_NOT_FOUND' && message.includes('@react-native-async-storage/async-storage');
|
|
51
|
+
if (!ourPackageMissing) {
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
console.warn('[Amplitude] Failed to load @react-native-async-storage/async-storage; persistence is disabled.', e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/* eslint-enable @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
57
|
+
return asyncStorage;
|
|
58
|
+
};
|
|
3
59
|
|
|
4
60
|
export class LocalStorage<T> implements Storage<T> {
|
|
5
61
|
async isEnabled(): Promise<boolean> {
|
|
@@ -7,6 +63,9 @@ export class LocalStorage<T> implements Storage<T> {
|
|
|
7
63
|
if (!getGlobalScope()) {
|
|
8
64
|
return false;
|
|
9
65
|
}
|
|
66
|
+
if (!getAsyncStorage()) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
10
69
|
|
|
11
70
|
const random = String(Date.now());
|
|
12
71
|
const testStorage = new LocalStorage<string>();
|
|
@@ -38,28 +97,52 @@ export class LocalStorage<T> implements Storage<T> {
|
|
|
38
97
|
}
|
|
39
98
|
|
|
40
99
|
async getRaw(key: string): Promise<string | undefined> {
|
|
41
|
-
|
|
100
|
+
const storage = getAsyncStorage();
|
|
101
|
+
if (!storage) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
try {
|
|
105
|
+
return (await storage.getItem(key)) || undefined;
|
|
106
|
+
} catch {
|
|
107
|
+
// AsyncStorage's JS package is resolvable but the native bridge is null
|
|
108
|
+
// (e.g. customer excluded it via autolinking but the JS package is still
|
|
109
|
+
// in node_modules). Callers like `parseOldCookies` consume `getRaw`
|
|
110
|
+
// directly without their own try/catch, so we must not propagate.
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
42
113
|
}
|
|
43
114
|
|
|
44
115
|
async set(key: string, value: T): Promise<void> {
|
|
116
|
+
const storage = getAsyncStorage();
|
|
117
|
+
if (!storage) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
45
120
|
try {
|
|
46
|
-
await
|
|
121
|
+
await storage.setItem(key, JSON.stringify(value));
|
|
47
122
|
} catch {
|
|
48
123
|
//
|
|
49
124
|
}
|
|
50
125
|
}
|
|
51
126
|
|
|
52
127
|
async remove(key: string): Promise<void> {
|
|
128
|
+
const storage = getAsyncStorage();
|
|
129
|
+
if (!storage) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
53
132
|
try {
|
|
54
|
-
await
|
|
133
|
+
await storage.removeItem(key);
|
|
55
134
|
} catch {
|
|
56
135
|
//
|
|
57
136
|
}
|
|
58
137
|
}
|
|
59
138
|
|
|
60
139
|
async reset(): Promise<void> {
|
|
140
|
+
const storage = getAsyncStorage();
|
|
141
|
+
if (!storage) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
61
144
|
try {
|
|
62
|
-
await
|
|
145
|
+
await storage.clear();
|
|
63
146
|
} catch {
|
|
64
147
|
//
|
|
65
148
|
}
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '1.5.
|
|
1
|
+
export const VERSION = '1.5.55';
|