@metarouter/react-native-sdk 1.7.0 → 1.8.0

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.
Files changed (31) hide show
  1. package/README.md +164 -56
  2. package/lib/analytics/MetaRouterAnalyticsClient.d.ts +50 -2
  3. package/lib/analytics/MetaRouterAnalyticsClient.d.ts.map +1 -1
  4. package/lib/analytics/MetaRouterAnalyticsClient.js +239 -44
  5. package/lib/analytics/init.d.ts +7 -0
  6. package/lib/analytics/init.d.ts.map +1 -1
  7. package/lib/analytics/init.js +197 -37
  8. package/lib/analytics/lifecycle/lifecycleEvents.d.ts +52 -0
  9. package/lib/analytics/lifecycle/lifecycleEvents.d.ts.map +1 -0
  10. package/lib/analytics/lifecycle/lifecycleEvents.js +66 -0
  11. package/lib/analytics/proxy/proxyClient.d.ts +29 -0
  12. package/lib/analytics/proxy/proxyClient.d.ts.map +1 -1
  13. package/lib/analytics/proxy/proxyClient.js +125 -5
  14. package/lib/analytics/types.d.ts +50 -1
  15. package/lib/analytics/types.d.ts.map +1 -1
  16. package/lib/analytics/utils/appContext.d.ts +20 -0
  17. package/lib/analytics/utils/appContext.d.ts.map +1 -0
  18. package/lib/analytics/utils/appContext.js +22 -0
  19. package/lib/analytics/utils/contextInfo.d.ts +7 -3
  20. package/lib/analytics/utils/contextInfo.d.ts.map +1 -1
  21. package/lib/analytics/utils/contextInfo.js +12 -9
  22. package/lib/analytics/utils/initOptions.d.ts +22 -0
  23. package/lib/analytics/utils/initOptions.d.ts.map +1 -0
  24. package/lib/analytics/utils/initOptions.js +100 -0
  25. package/lib/analytics/utils/lifecycleStorage.d.ts +13 -0
  26. package/lib/analytics/utils/lifecycleStorage.d.ts.map +1 -0
  27. package/lib/analytics/utils/lifecycleStorage.js +35 -0
  28. package/lib/index.d.ts +4 -4
  29. package/lib/index.d.ts.map +1 -1
  30. package/lib/index.js +3 -3
  31. package/package.json +1 -1
@@ -0,0 +1,100 @@
1
+ import { warn as logWarn } from './logger';
2
+ /*
3
+ * Why the options were invalid. Recorded instead of thrown: a throw from the
4
+ * synchronous `createAnalyticsClient` call would take down a host that sources
5
+ * its config remotely and forgot a try/catch — the crash this contract exists
6
+ * to prevent. Each error carries its description, as `ConfigError.description`
7
+ * does on iOS and Android, and the text is byte-identical with both SDKs so one
8
+ * support answer covers all three.
9
+ */
10
+ const emptyWriteKeyError = () => ({
11
+ type: 'emptyWriteKey',
12
+ description: 'writeKey must not be empty or whitespace-only',
13
+ });
14
+ const invalidIngestionHostError = (raw) => ({
15
+ type: 'invalidIngestionHost',
16
+ raw,
17
+ description: `ingestionHost must be an http(s) URL, got "${raw}"`,
18
+ });
19
+ // `http(s)://[userinfo@]host[:port][/path]`. Parsed by hand rather than with
20
+ // `URL`: React Native's `URL` polyfill does not implement `hostname`, so it
21
+ // cannot tell "https:" (no host) from a real origin. Accepts what Foundation
22
+ // and java.net.URL accept, so the same string passes on all three platforms:
23
+ // userinfo, an empty port ("host:"), and bracketed IPv6 hosts.
24
+ const HTTP_URL = /^(https?):\/\/(?:[^\s/?#@]*@)?(\[[^\]\s]+\]|[^\s/?#:@]+)(:\d*)?([/?#]\S*)?$/i;
25
+ const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1']);
26
+ /** True for localhost / 127.0.0.1 / ::1 (IPv6 brackets stripped), exact match. */
27
+ export function isLoopbackHost(host) {
28
+ const bare = host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
29
+ return LOOPBACK_HOSTS.has(bare);
30
+ }
31
+ /**
32
+ * Single validation funnel, run once per `createAnalyticsClient` call — the
33
+ * equivalent of the native InitOptions constructor. Never throws: records the
34
+ * first error and clamps every numeric bound with a warning (one policy for
35
+ * all fields, matching iOS and Android). Warnings are logged here and nowhere
36
+ * else, so they appear once per call.
37
+ */
38
+ export function validateInitOptions(raw, opts = {}) {
39
+ const warn = opts.logWarnings === false ? () => { } : logWarn;
40
+ let configError = null;
41
+ const writeKey = typeof raw.writeKey === 'string' ? raw.writeKey.trim() : '';
42
+ if (writeKey === '') {
43
+ configError = emptyWriteKeyError();
44
+ }
45
+ const rawHost = typeof raw.ingestionHost === 'string' ? raw.ingestionHost : '';
46
+ const ingestionHost = rawHost.trim().replace(/\/+$/, '');
47
+ const match = HTTP_URL.exec(ingestionHost);
48
+ if (!match) {
49
+ if (configError === null) {
50
+ configError = invalidIngestionHostError(rawHost);
51
+ }
52
+ }
53
+ else if (match[1].toLowerCase() === 'http' && !isLoopbackHost(match[2])) {
54
+ // A cleartext origin is spoofable in transit — legitimate for local
55
+ // development, worth a warning anywhere else.
56
+ warn(`ingestionHost ${ingestionHost} is cleartext http — use https outside local development.`);
57
+ }
58
+ // NaN / Infinity (e.g. Number('') from remote config) would slip past every
59
+ // `<` check and reach the dispatcher as a 0ms flush loop or a silently
60
+ // disabled disk queue. Treated as unset, with a warning. Native integer
61
+ // options cannot hold these values, so only JS needs the check.
62
+ const finiteOrUnset = (name, value) => {
63
+ if (value == null || Number.isFinite(value))
64
+ return value;
65
+ warn(`${name} (${value}) is not a finite number — using the default`);
66
+ return undefined;
67
+ };
68
+ const flushIntervalSeconds = finiteOrUnset('flushIntervalSeconds', raw.flushIntervalSeconds);
69
+ if (flushIntervalSeconds != null && flushIntervalSeconds < 1) {
70
+ warn(`flushIntervalSeconds (${flushIntervalSeconds}) clamped to 1`);
71
+ }
72
+ const maxQueueEvents = finiteOrUnset('maxQueueEvents', raw.maxQueueEvents);
73
+ if (maxQueueEvents != null && maxQueueEvents < 1) {
74
+ warn(`maxQueueEvents (${maxQueueEvents}) clamped to 1`);
75
+ }
76
+ const maxDiskEvents = finiteOrUnset('maxDiskEvents', raw.maxDiskEvents);
77
+ if (maxDiskEvents != null && maxDiskEvents < 0) {
78
+ warn(`maxDiskEvents (${maxDiskEvents}) clamped to 0 — use 0 to disable disk persistence`);
79
+ }
80
+ const clampedQueue = Math.max(1, maxQueueEvents !== null && maxQueueEvents !== void 0 ? maxQueueEvents : DEFAULT_MAX_QUEUE_EVENTS);
81
+ const clampedDisk = Math.max(0, maxDiskEvents !== null && maxDiskEvents !== void 0 ? maxDiskEvents : DEFAULT_MAX_DISK_EVENTS);
82
+ if (clampedDisk > 0 && clampedDisk < clampedQueue) {
83
+ warn(`maxDiskEvents (${clampedDisk}) is less than maxQueueEvents (${clampedQueue}) — memory can hold more events than disk can preserve; events may be dropped during background flush`);
84
+ }
85
+ return {
86
+ options: {
87
+ ...raw,
88
+ writeKey,
89
+ ingestionHost,
90
+ flushIntervalSeconds: flushIntervalSeconds != null
91
+ ? Math.max(1, flushIntervalSeconds)
92
+ : undefined,
93
+ maxQueueEvents: clampedQueue,
94
+ maxDiskEvents: clampedDisk,
95
+ },
96
+ configError,
97
+ };
98
+ }
99
+ export const DEFAULT_MAX_QUEUE_EVENTS = 2000;
100
+ export const DEFAULT_MAX_DISK_EVENTS = 10000;
@@ -0,0 +1,13 @@
1
+ export declare const LIFECYCLE_VERSION_KEY = "metarouter:lifecycle:version";
2
+ export declare const LIFECYCLE_BUILD_KEY = "metarouter:lifecycle:build";
3
+ /**
4
+ * Storage for app lifecycle state (last-seen version + build). Lives in a
5
+ * dedicated module so neither IdentityManager.reset() nor the client's reset()
6
+ * can wipe install/update history. Errors are swallowed so missing or
7
+ * unavailable storage is treated as "no prior lifecycle state" — the caller
8
+ * decides whether that means install or update.
9
+ */
10
+ export declare function getLifecycleVersion(): Promise<string | null>;
11
+ export declare function getLifecycleBuild(): Promise<string | null>;
12
+ export declare function setLifecycleVersionBuild(version: string, build: string): Promise<void>;
13
+ //# sourceMappingURL=lifecycleStorage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycleStorage.d.ts","sourceRoot":"","sources":["../../../src/analytics/utils/lifecycleStorage.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,qBAAqB,iCAAiC,CAAC;AACpE,eAAO,MAAM,mBAAmB,+BAA+B,CAAC;AAEhE;;;;;;GAMG;AAEH,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAMlE;AAED,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAMhE;AAED,wBAAsB,wBAAwB,CAC5C,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,IAAI,CAAC,CAOf"}
@@ -0,0 +1,35 @@
1
+ import AsyncStorage from '@react-native-async-storage/async-storage';
2
+ export const LIFECYCLE_VERSION_KEY = 'metarouter:lifecycle:version';
3
+ export const LIFECYCLE_BUILD_KEY = 'metarouter:lifecycle:build';
4
+ /**
5
+ * Storage for app lifecycle state (last-seen version + build). Lives in a
6
+ * dedicated module so neither IdentityManager.reset() nor the client's reset()
7
+ * can wipe install/update history. Errors are swallowed so missing or
8
+ * unavailable storage is treated as "no prior lifecycle state" — the caller
9
+ * decides whether that means install or update.
10
+ */
11
+ export async function getLifecycleVersion() {
12
+ try {
13
+ return await AsyncStorage.getItem(LIFECYCLE_VERSION_KEY);
14
+ }
15
+ catch {
16
+ return null;
17
+ }
18
+ }
19
+ export async function getLifecycleBuild() {
20
+ try {
21
+ return await AsyncStorage.getItem(LIFECYCLE_BUILD_KEY);
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ }
27
+ export async function setLifecycleVersionBuild(version, build) {
28
+ try {
29
+ await AsyncStorage.setItem(LIFECYCLE_VERSION_KEY, version);
30
+ await AsyncStorage.setItem(LIFECYCLE_BUILD_KEY, build);
31
+ }
32
+ catch {
33
+ // best-effort; cold-launch state will be re-derived on next run
34
+ }
35
+ }
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { createAnalyticsClient } from "./analytics/init";
2
- export { MetaRouterProvider } from "./react/context";
3
- export { useMetaRouter } from "./react/useMetaRouter";
4
- export type { InitOptions, AnalyticsInterface } from "./analytics/types";
1
+ export { createAnalyticsClient } from './analytics/init';
2
+ export { MetaRouterProvider } from './react/context';
3
+ export { useMetaRouter } from './react/useMetaRouter';
4
+ export type { InitOptions, AnalyticsInterface, ConfigError, } from './analytics/types';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,YAAY,EACV,WAAW,EACX,kBAAkB,EAClB,WAAW,GACZ,MAAM,mBAAmB,CAAC"}
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { createAnalyticsClient } from "./analytics/init";
2
- export { MetaRouterProvider } from "./react/context";
3
- export { useMetaRouter } from "./react/useMetaRouter";
1
+ export { createAnalyticsClient } from './analytics/init';
2
+ export { MetaRouterProvider } from './react/context';
3
+ export { useMetaRouter } from './react/useMetaRouter';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metarouter/react-native-sdk",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "main": "./lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "exports": {