@atol-sh/js 0.1.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.
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Device intelligence types.
3
+ *
4
+ * Wire shapes mirror the Atol control plane's identify endpoint
5
+ * (`POST /api/v1/devices/identify`) -- the same shapes
6
+ * `@atol-sh/fingerprint` returns from `identify()`, since that SDK
7
+ * passes the server response through verbatim. The fingerprint package
8
+ * is an optional peer dependency that is not statically resolvable
9
+ * here, so the contract is mirrored locally instead of imported.
10
+ */
11
+ /** Configuration for device intelligence collection. */
12
+ interface DeviceConfig {
13
+ /** Whether device intelligence is enabled. */
14
+ enabled: boolean;
15
+ /**
16
+ * Base URL of the Atol API host that serves
17
+ * `POST /api/v1/devices/identify`. This is the API host
18
+ * (https://api.atol.sh), NOT the OIDC authority (https://auth.atol.sh).
19
+ * Default: "https://api.atol.sh".
20
+ */
21
+ apiUrl?: string;
22
+ /** Collect device signals on mount. Default: true. */
23
+ collectOnMount?: boolean;
24
+ /** Interval in ms to re-collect signals. 0 = disabled. Default: 0. */
25
+ recheckInterval?: number;
26
+ }
27
+ /**
28
+ * Smart signals computed server-side. Keys match the server's signals
29
+ * map exactly (snake_case); no client-side fabrication.
30
+ */
31
+ interface SmartSignals {
32
+ bot: boolean;
33
+ vpn: boolean;
34
+ proxy: boolean;
35
+ tor: boolean;
36
+ incognito: boolean;
37
+ tampered: boolean;
38
+ emulator: boolean;
39
+ rooted: boolean;
40
+ geo_mismatch: boolean;
41
+ anomaly_score: number;
42
+ }
43
+ /**
44
+ * Raw identify response from the control plane (snake_case, verbatim).
45
+ * `signals` is null when the server computed no smart signals.
46
+ */
47
+ interface IdentifyResult {
48
+ device_id: string;
49
+ known: boolean;
50
+ confidence: number;
51
+ new_device: boolean;
52
+ platform: string;
53
+ browser: string;
54
+ os_version: string;
55
+ signals: SmartSignals | null;
56
+ }
57
+ /** Device intelligence state (binding-facing, camelCase). */
58
+ interface DeviceState {
59
+ /** Unique device identifier assigned by the Atol control plane. */
60
+ deviceId: string | null;
61
+ /** Whether this device has been seen before. */
62
+ known: boolean;
63
+ /** Confidence score (0-1) for the device fingerprint match. */
64
+ confidence: number;
65
+ /** Whether the server created a new device record for this identify. */
66
+ newDevice: boolean;
67
+ /** Platform reported by the server (e.g. "macOS"). Null before identify. */
68
+ platform: string | null;
69
+ /** Browser family reported by the server. Null before identify. */
70
+ browser: string | null;
71
+ /** OS version reported by the server. Null before identify. */
72
+ osVersion: string | null;
73
+ /** Smart signals from server-side analysis. Null before identify or
74
+ * when the server returned none. */
75
+ signals: SmartSignals | null;
76
+ /** Whether device intelligence is currently loading. */
77
+ loading: boolean;
78
+ /** Error from device intelligence collection, if any. */
79
+ error: Error | null;
80
+ }
81
+ /** Initial / not-collected device state. Shared by bindings' context
82
+ * default and the no-provider fallback. */
83
+ declare const initialDeviceState: DeviceState;
84
+
85
+ /**
86
+ * Framework-agnostic device intelligence collector.
87
+ *
88
+ * Owns the `@atol-sh/fingerprint` agent lifecycle and the identify call,
89
+ * plus the mapping from the server's snake_case response to the
90
+ * binding-facing DeviceState. React (and any other) bindings wrap this
91
+ * with their own reactive state; the collector itself has no framework
92
+ * dependency.
93
+ *
94
+ * This module lives under the optional `@atol-sh/js/device` subpath so a
95
+ * consumer that never uses device intelligence never pulls
96
+ * `@atol-sh/fingerprint` (an optional peer dependency, dynamic-imported
97
+ * here) into their bundle.
98
+ */
99
+
100
+ /** Default Atol API host serving POST /api/v1/devices/identify. */
101
+ declare const DEFAULT_DEVICE_API_URL = "https://api.atol.sh";
102
+ /**
103
+ * Map the server's signals object onto SmartSignals. Only the keys the
104
+ * server actually emits are read -- nothing is fabricated. Returns null
105
+ * when the server sent no signals.
106
+ */
107
+ declare function parseSignals(raw: unknown): SmartSignals | null;
108
+ /** Map a raw identify result to the fully-populated DeviceState. */
109
+ declare function deviceStateFromResult(result: IdentifyResult): DeviceState;
110
+ /**
111
+ * DeviceCollector loads and caches the fingerprint agent, and performs
112
+ * identify calls. It is stateless with respect to the reactive UI: callers
113
+ * pass an `onState` sink that receives DeviceState transitions, and drive
114
+ * `collect()` in response to token changes / intervals.
115
+ *
116
+ * The agent is (re)loaded when missing or bound to a stale endpoint, so a
117
+ * changed API host does not pin to the first host seen. A single-flight
118
+ * guard keyed by token prevents duplicate concurrent identify POSTs (e.g.
119
+ * React Strict Mode's double invoke).
120
+ */
121
+ declare class DeviceCollector {
122
+ private readonly apiUrl;
123
+ private readonly onState;
124
+ private agent;
125
+ private agentApiUrl;
126
+ private inFlight;
127
+ private inFlightToken;
128
+ constructor(apiUrl: string, onState: (state: DeviceState) => void);
129
+ /**
130
+ * Collect device signals. With a token, submits an identify call and
131
+ * emits the mapped DeviceState; without one, just pre-loads the agent.
132
+ * Concurrent calls with the same token share one in-flight promise.
133
+ */
134
+ collect(token?: string): Promise<void>;
135
+ }
136
+
137
+ export { DEFAULT_DEVICE_API_URL, DeviceCollector, type DeviceConfig, type DeviceState, type IdentifyResult, type SmartSignals, deviceStateFromResult, initialDeviceState, parseSignals };
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Device intelligence types.
3
+ *
4
+ * Wire shapes mirror the Atol control plane's identify endpoint
5
+ * (`POST /api/v1/devices/identify`) -- the same shapes
6
+ * `@atol-sh/fingerprint` returns from `identify()`, since that SDK
7
+ * passes the server response through verbatim. The fingerprint package
8
+ * is an optional peer dependency that is not statically resolvable
9
+ * here, so the contract is mirrored locally instead of imported.
10
+ */
11
+ /** Configuration for device intelligence collection. */
12
+ interface DeviceConfig {
13
+ /** Whether device intelligence is enabled. */
14
+ enabled: boolean;
15
+ /**
16
+ * Base URL of the Atol API host that serves
17
+ * `POST /api/v1/devices/identify`. This is the API host
18
+ * (https://api.atol.sh), NOT the OIDC authority (https://auth.atol.sh).
19
+ * Default: "https://api.atol.sh".
20
+ */
21
+ apiUrl?: string;
22
+ /** Collect device signals on mount. Default: true. */
23
+ collectOnMount?: boolean;
24
+ /** Interval in ms to re-collect signals. 0 = disabled. Default: 0. */
25
+ recheckInterval?: number;
26
+ }
27
+ /**
28
+ * Smart signals computed server-side. Keys match the server's signals
29
+ * map exactly (snake_case); no client-side fabrication.
30
+ */
31
+ interface SmartSignals {
32
+ bot: boolean;
33
+ vpn: boolean;
34
+ proxy: boolean;
35
+ tor: boolean;
36
+ incognito: boolean;
37
+ tampered: boolean;
38
+ emulator: boolean;
39
+ rooted: boolean;
40
+ geo_mismatch: boolean;
41
+ anomaly_score: number;
42
+ }
43
+ /**
44
+ * Raw identify response from the control plane (snake_case, verbatim).
45
+ * `signals` is null when the server computed no smart signals.
46
+ */
47
+ interface IdentifyResult {
48
+ device_id: string;
49
+ known: boolean;
50
+ confidence: number;
51
+ new_device: boolean;
52
+ platform: string;
53
+ browser: string;
54
+ os_version: string;
55
+ signals: SmartSignals | null;
56
+ }
57
+ /** Device intelligence state (binding-facing, camelCase). */
58
+ interface DeviceState {
59
+ /** Unique device identifier assigned by the Atol control plane. */
60
+ deviceId: string | null;
61
+ /** Whether this device has been seen before. */
62
+ known: boolean;
63
+ /** Confidence score (0-1) for the device fingerprint match. */
64
+ confidence: number;
65
+ /** Whether the server created a new device record for this identify. */
66
+ newDevice: boolean;
67
+ /** Platform reported by the server (e.g. "macOS"). Null before identify. */
68
+ platform: string | null;
69
+ /** Browser family reported by the server. Null before identify. */
70
+ browser: string | null;
71
+ /** OS version reported by the server. Null before identify. */
72
+ osVersion: string | null;
73
+ /** Smart signals from server-side analysis. Null before identify or
74
+ * when the server returned none. */
75
+ signals: SmartSignals | null;
76
+ /** Whether device intelligence is currently loading. */
77
+ loading: boolean;
78
+ /** Error from device intelligence collection, if any. */
79
+ error: Error | null;
80
+ }
81
+ /** Initial / not-collected device state. Shared by bindings' context
82
+ * default and the no-provider fallback. */
83
+ declare const initialDeviceState: DeviceState;
84
+
85
+ /**
86
+ * Framework-agnostic device intelligence collector.
87
+ *
88
+ * Owns the `@atol-sh/fingerprint` agent lifecycle and the identify call,
89
+ * plus the mapping from the server's snake_case response to the
90
+ * binding-facing DeviceState. React (and any other) bindings wrap this
91
+ * with their own reactive state; the collector itself has no framework
92
+ * dependency.
93
+ *
94
+ * This module lives under the optional `@atol-sh/js/device` subpath so a
95
+ * consumer that never uses device intelligence never pulls
96
+ * `@atol-sh/fingerprint` (an optional peer dependency, dynamic-imported
97
+ * here) into their bundle.
98
+ */
99
+
100
+ /** Default Atol API host serving POST /api/v1/devices/identify. */
101
+ declare const DEFAULT_DEVICE_API_URL = "https://api.atol.sh";
102
+ /**
103
+ * Map the server's signals object onto SmartSignals. Only the keys the
104
+ * server actually emits are read -- nothing is fabricated. Returns null
105
+ * when the server sent no signals.
106
+ */
107
+ declare function parseSignals(raw: unknown): SmartSignals | null;
108
+ /** Map a raw identify result to the fully-populated DeviceState. */
109
+ declare function deviceStateFromResult(result: IdentifyResult): DeviceState;
110
+ /**
111
+ * DeviceCollector loads and caches the fingerprint agent, and performs
112
+ * identify calls. It is stateless with respect to the reactive UI: callers
113
+ * pass an `onState` sink that receives DeviceState transitions, and drive
114
+ * `collect()` in response to token changes / intervals.
115
+ *
116
+ * The agent is (re)loaded when missing or bound to a stale endpoint, so a
117
+ * changed API host does not pin to the first host seen. A single-flight
118
+ * guard keyed by token prevents duplicate concurrent identify POSTs (e.g.
119
+ * React Strict Mode's double invoke).
120
+ */
121
+ declare class DeviceCollector {
122
+ private readonly apiUrl;
123
+ private readonly onState;
124
+ private agent;
125
+ private agentApiUrl;
126
+ private inFlight;
127
+ private inFlightToken;
128
+ constructor(apiUrl: string, onState: (state: DeviceState) => void);
129
+ /**
130
+ * Collect device signals. With a token, submits an identify call and
131
+ * emits the mapped DeviceState; without one, just pre-loads the agent.
132
+ * Concurrent calls with the same token share one in-flight promise.
133
+ */
134
+ collect(token?: string): Promise<void>;
135
+ }
136
+
137
+ export { DEFAULT_DEVICE_API_URL, DeviceCollector, type DeviceConfig, type DeviceState, type IdentifyResult, type SmartSignals, deviceStateFromResult, initialDeviceState, parseSignals };
package/dist/device.js ADDED
@@ -0,0 +1,109 @@
1
+ // src/device/types.ts
2
+ var initialDeviceState = {
3
+ deviceId: null,
4
+ known: false,
5
+ confidence: 0,
6
+ newDevice: false,
7
+ platform: null,
8
+ browser: null,
9
+ osVersion: null,
10
+ signals: null,
11
+ loading: false,
12
+ error: null
13
+ };
14
+
15
+ // src/device/collector.ts
16
+ var DEFAULT_DEVICE_API_URL = "https://api.atol.sh";
17
+ function parseSignals(raw) {
18
+ if (!raw || typeof raw !== "object") return null;
19
+ const r = raw;
20
+ return {
21
+ bot: r.bot === true,
22
+ vpn: r.vpn === true,
23
+ proxy: r.proxy === true,
24
+ tor: r.tor === true,
25
+ incognito: r.incognito === true,
26
+ tampered: r.tampered === true,
27
+ emulator: r.emulator === true,
28
+ rooted: r.rooted === true,
29
+ geo_mismatch: r.geo_mismatch === true,
30
+ anomaly_score: typeof r.anomaly_score === "number" ? r.anomaly_score : 0
31
+ };
32
+ }
33
+ function deviceStateFromResult(result) {
34
+ return {
35
+ deviceId: result.device_id ?? null,
36
+ known: result.known ?? false,
37
+ confidence: result.confidence ?? 0,
38
+ newDevice: result.new_device ?? false,
39
+ platform: result.platform || null,
40
+ browser: result.browser || null,
41
+ osVersion: result.os_version || null,
42
+ signals: parseSignals(result.signals),
43
+ loading: false,
44
+ error: null
45
+ };
46
+ }
47
+ var DeviceCollector = class {
48
+ constructor(apiUrl, onState) {
49
+ this.apiUrl = apiUrl;
50
+ this.onState = onState;
51
+ this.agent = null;
52
+ this.agentApiUrl = null;
53
+ this.inFlight = null;
54
+ this.inFlightToken = null;
55
+ }
56
+ /**
57
+ * Collect device signals. With a token, submits an identify call and
58
+ * emits the mapped DeviceState; without one, just pre-loads the agent.
59
+ * Concurrent calls with the same token share one in-flight promise.
60
+ */
61
+ collect(token) {
62
+ const key = token ?? null;
63
+ if (this.inFlight && this.inFlightToken === key) {
64
+ return this.inFlight;
65
+ }
66
+ const run = (async () => {
67
+ this.onState({ ...initialDeviceState, loading: true });
68
+ try {
69
+ let mod;
70
+ try {
71
+ mod = await import('@atol-sh/fingerprint');
72
+ } catch (importErr) {
73
+ throw new Error(
74
+ `device intelligence is enabled but @atol-sh/fingerprint is not installed -- run \`npm i @atol-sh/fingerprint\` or disable device intelligence (${String(importErr)})`
75
+ );
76
+ }
77
+ const AtolFingerprint = mod.AtolFingerprint ?? mod.default;
78
+ if (!AtolFingerprint) {
79
+ throw new Error(
80
+ "@atol-sh/fingerprint loaded but exports no AtolFingerprint -- check the installed package version"
81
+ );
82
+ }
83
+ if (!this.agent || this.agentApiUrl !== this.apiUrl) {
84
+ this.agent = await AtolFingerprint.load({ endpoint: this.apiUrl });
85
+ this.agentApiUrl = this.apiUrl;
86
+ }
87
+ if (token) {
88
+ const result = await this.agent.identify({ token });
89
+ this.onState(deviceStateFromResult(result));
90
+ } else {
91
+ this.onState({ ...initialDeviceState, loading: false });
92
+ }
93
+ } catch (err) {
94
+ const error = err instanceof Error ? err : new Error(`Device intelligence collection failed: ${String(err)}`);
95
+ this.onState({ ...initialDeviceState, loading: false, error });
96
+ } finally {
97
+ this.inFlight = null;
98
+ this.inFlightToken = null;
99
+ }
100
+ })();
101
+ this.inFlight = run;
102
+ this.inFlightToken = key;
103
+ return run;
104
+ }
105
+ };
106
+
107
+ export { DEFAULT_DEVICE_API_URL, DeviceCollector, deviceStateFromResult, initialDeviceState, parseSignals };
108
+ //# sourceMappingURL=device.js.map
109
+ //# sourceMappingURL=device.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/device/types.ts","../src/device/collector.ts"],"names":[],"mappings":";AAmGO,IAAM,kBAAA,GAAkC;AAAA,EAC7C,QAAA,EAAU,IAAA;AAAA,EACV,KAAA,EAAO,KAAA;AAAA,EACP,UAAA,EAAY,CAAA;AAAA,EACZ,SAAA,EAAW,KAAA;AAAA,EACX,QAAA,EAAU,IAAA;AAAA,EACV,OAAA,EAAS,IAAA;AAAA,EACT,SAAA,EAAW,IAAA;AAAA,EACX,OA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,IAAA,CAAK,KAAA,IAAS,IAAA,CAAK,WAAA,KAAgB,KAAK,MAAA,EAAQ;AACnD,UAAA,IAAA,CAAK,KAAA,GAAQ,MAAM,eAAA,CAAgB,IAAA,CAAK,EAAE,QAAA,EAAU,IAAA,CAAK,QAAQ,CAAA;AACjE,UAAA,IAAA,CAAK,cAAc,IAAA,CAAK,MAAA;AAAA,QAC1B;AAEA,QAAA,IAAI,KAAA,EAAO;AACT,UAAA,MAAM,SAAS,MAAM,IAAA,CAAK,MAAM,QAAA,CAAS,EAAE,OAAO,CAAA;AAClD,UAAA,IAAA,CAAK,OAAA,CAAQ,qBAAA,CAAsB,MAAM,CAAC,CAAA;AAAA,QAC5C,CAAA,MAAO;AAEL,UAAA,IAAA,CAAK,QAAQ,EAAE,GAAG,kBAAA,EAAoB,OAAA,EAAS,OAAO,CAAA;AAAA,QACxD;AAAA,MACF,SAAS,GAAA,EAAK;AACZ,QAAA,MAAM,KAAA,GACJ,GAAA,YAAe,KAAA,GACX,GAAA,GACA,IAAI,MAAM,CAAA,uCAAA,EAA0C,MAAA,CAAO,GAAG,CAAC,CAAA,CAAE,CAAA;AACvE,QAAA,IAAA,CAAK,QAAQ,EAAE,GAAG,oBAAoB,OAAA,EAAS,KAAA,EAAO,OAAO,CAAA;AAAA,MAC/D,CAAA,SAAE;AACA,QAAA,IAAA,CAAK,QAAA,GAAW,IAAA;AAChB,QAAA,IAAA,CAAK,aAAA,GAAgB,IAAA;AAAA,MACvB;AAAA,IACF,CAAA,GAAG;AAEH,IAAA,IAAA,CAAK,QAAA,GAAW,GAAA;AAChB,IAAA,IAAA,CAAK,aAAA,GAAgB,GAAA;AACrB,IAAA,OAAO,GAAA;AAAA,EACT;AACF","file":"device.js","sourcesContent":["/**\n * Device intelligence types.\n *\n * Wire shapes mirror the Atol control plane's identify endpoint\n * (`POST /api/v1/devices/identify`) -- the same shapes\n * `@atol-sh/fingerprint` returns from `identify()`, since that SDK\n * passes the server response through verbatim. The fingerprint package\n * is an optional peer dependency that is not statically resolvable\n * here, so the contract is mirrored locally instead of imported.\n */\n\n/** Configuration for device intelligence collection. */\nexport interface DeviceConfig {\n /** Whether device intelligence is enabled. */\n enabled: boolean\n\n /**\n * Base URL of the Atol API host that serves\n * `POST /api/v1/devices/identify`. This is the API host\n * (https://api.atol.sh), NOT the OIDC authority (https://auth.atol.sh).\n * Default: \"https://api.atol.sh\".\n */\n apiUrl?: string\n\n /** Collect device signals on mount. Default: true. */\n collectOnMount?: boolean\n\n /** Interval in ms to re-collect signals. 0 = disabled. Default: 0. */\n recheckInterval?: number\n}\n\n/**\n * Smart signals computed server-side. Keys match the server's signals\n * map exactly (snake_case); no client-side fabrication.\n */\nexport interface SmartSignals {\n bot: boolean\n vpn: boolean\n proxy: boolean\n tor: boolean\n incognito: boolean\n tampered: boolean\n emulator: boolean\n rooted: boolean\n geo_mismatch: boolean\n anomaly_score: number\n}\n\n/**\n * Raw identify response from the control plane (snake_case, verbatim).\n * `signals` is null when the server computed no smart signals.\n */\nexport interface IdentifyResult {\n device_id: string\n known: boolean\n confidence: number\n new_device: boolean\n platform: string\n browser: string\n os_version: string\n signals: SmartSignals | null\n}\n\n/** Device intelligence state (binding-facing, camelCase). */\nexport interface DeviceState {\n /** Unique device identifier assigned by the Atol control plane. */\n deviceId: string | null\n\n /** Whether this device has been seen before. */\n known: boolean\n\n /** Confidence score (0-1) for the device fingerprint match. */\n confidence: number\n\n /** Whether the server created a new device record for this identify. */\n newDevice: boolean\n\n /** Platform reported by the server (e.g. \"macOS\"). Null before identify. */\n platform: string | null\n\n /** Browser family reported by the server. Null before identify. */\n browser: string | null\n\n /** OS version reported by the server. Null before identify. */\n osVersion: string | null\n\n /** Smart signals from server-side analysis. Null before identify or\n * when the server returned none. */\n signals: SmartSignals | null\n\n /** Whether device intelligence is currently loading. */\n loading: boolean\n\n /** Error from device intelligence collection, if any. */\n error: Error | null\n}\n\n/** Initial / not-collected device state. Shared by bindings' context\n * default and the no-provider fallback. */\nexport const initialDeviceState: DeviceState = {\n deviceId: null,\n known: false,\n confidence: 0,\n newDevice: false,\n platform: null,\n browser: null,\n osVersion: null,\n signals: null,\n loading: false,\n error: null,\n}\n","/**\n * Framework-agnostic device intelligence collector.\n *\n * Owns the `@atol-sh/fingerprint` agent lifecycle and the identify call,\n * plus the mapping from the server's snake_case response to the\n * binding-facing DeviceState. React (and any other) bindings wrap this\n * with their own reactive state; the collector itself has no framework\n * dependency.\n *\n * This module lives under the optional `@atol-sh/js/device` subpath so a\n * consumer that never uses device intelligence never pulls\n * `@atol-sh/fingerprint` (an optional peer dependency, dynamic-imported\n * here) into their bundle.\n */\n\nimport {\n initialDeviceState,\n type DeviceState,\n type IdentifyResult,\n type SmartSignals,\n} from './types'\n\n/** Default Atol API host serving POST /api/v1/devices/identify. */\nexport const DEFAULT_DEVICE_API_URL = 'https://api.atol.sh'\n\n/**\n * Structural surface of `@atol-sh/fingerprint` consumed here. The package\n * is an OPTIONAL peer dependency, so it cannot be statically imported\n * without breaking installs that skip it. These interfaces mirror the\n * published package API (`AtolFingerprint.load` / `identify`) and the wire\n * shapes are pinned to the server contract -- the fingerprint SDK returns\n * the server JSON verbatim.\n */\ninterface FingerprintAgent {\n identify(options?: { token?: string }): Promise<IdentifyResult>\n}\n\ninterface FingerprintStatic {\n load(config?: { endpoint?: string; apiKey?: string }): Promise<FingerprintAgent>\n}\n\ninterface FingerprintModule {\n AtolFingerprint?: FingerprintStatic\n default?: FingerprintStatic\n}\n\n/**\n * Map the server's signals object onto SmartSignals. Only the keys the\n * server actually emits are read -- nothing is fabricated. Returns null\n * when the server sent no signals.\n */\nexport function parseSignals(raw: unknown): SmartSignals | null {\n if (!raw || typeof raw !== 'object') return null\n const r = raw as Record<string, unknown>\n return {\n bot: r.bot === true,\n vpn: r.vpn === true,\n proxy: r.proxy === true,\n tor: r.tor === true,\n incognito: r.incognito === true,\n tampered: r.tampered === true,\n emulator: r.emulator === true,\n rooted: r.rooted === true,\n geo_mismatch: r.geo_mismatch === true,\n anomaly_score: typeof r.anomaly_score === 'number' ? r.anomaly_score : 0,\n }\n}\n\n/** Map a raw identify result to the fully-populated DeviceState. */\nexport function deviceStateFromResult(result: IdentifyResult): DeviceState {\n return {\n deviceId: result.device_id ?? null,\n known: result.known ?? false,\n confidence: result.confidence ?? 0,\n newDevice: result.new_device ?? false,\n platform: result.platform || null,\n browser: result.browser || null,\n osVersion: result.os_version || null,\n signals: parseSignals(result.signals),\n loading: false,\n error: null,\n }\n}\n\n/**\n * DeviceCollector loads and caches the fingerprint agent, and performs\n * identify calls. It is stateless with respect to the reactive UI: callers\n * pass an `onState` sink that receives DeviceState transitions, and drive\n * `collect()` in response to token changes / intervals.\n *\n * The agent is (re)loaded when missing or bound to a stale endpoint, so a\n * changed API host does not pin to the first host seen. A single-flight\n * guard keyed by token prevents duplicate concurrent identify POSTs (e.g.\n * React Strict Mode's double invoke).\n */\nexport class DeviceCollector {\n private agent: FingerprintAgent | null = null\n private agentApiUrl: string | null = null\n private inFlight: Promise<void> | null = null\n private inFlightToken: string | null = null\n\n constructor(\n private readonly apiUrl: string,\n private readonly onState: (state: DeviceState) => void,\n ) {}\n\n /**\n * Collect device signals. With a token, submits an identify call and\n * emits the mapped DeviceState; without one, just pre-loads the agent.\n * Concurrent calls with the same token share one in-flight promise.\n */\n collect(token?: string): Promise<void> {\n const key = token ?? null\n if (this.inFlight && this.inFlightToken === key) {\n return this.inFlight\n }\n\n const run = (async () => {\n this.onState({ ...initialDeviceState, loading: true })\n try {\n let mod: FingerprintModule\n try {\n mod = (await import('@atol-sh/fingerprint')) as FingerprintModule\n } catch (importErr) {\n throw new Error(\n 'device intelligence is enabled but @atol-sh/fingerprint is not ' +\n 'installed -- run `npm i @atol-sh/fingerprint` or disable device ' +\n `intelligence (${String(importErr)})`,\n )\n }\n const AtolFingerprint = mod.AtolFingerprint ?? mod.default\n if (!AtolFingerprint) {\n throw new Error(\n '@atol-sh/fingerprint loaded but exports no AtolFingerprint -- ' +\n 'check the installed package version',\n )\n }\n\n // (Re)load the agent when missing or bound to a stale endpoint.\n if (!this.agent || this.agentApiUrl !== this.apiUrl) {\n this.agent = await AtolFingerprint.load({ endpoint: this.apiUrl })\n this.agentApiUrl = this.apiUrl\n }\n\n if (token) {\n const result = await this.agent.identify({ token })\n this.onState(deviceStateFromResult(result))\n } else {\n // No token yet -- agent pre-loaded, nothing to identify.\n this.onState({ ...initialDeviceState, loading: false })\n }\n } catch (err) {\n const error =\n err instanceof Error\n ? err\n : new Error(`Device intelligence collection failed: ${String(err)}`)\n this.onState({ ...initialDeviceState, loading: false, error })\n } finally {\n this.inFlight = null\n this.inFlightToken = null\n }\n })()\n\n this.inFlight = run\n this.inFlightToken = key\n return run\n }\n}\n"]}