@proxze/kyc-react-native 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.
- package/LICENSE +51 -0
- package/README.md +216 -0
- package/dist/client.d.ts +55 -0
- package/dist/client.js +120 -0
- package/dist/hooks.d.ts +27 -0
- package/dist/hooks.js +59 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +40 -0
- package/dist/location/adapter.d.ts +57 -0
- package/dist/location/adapter.js +2 -0
- package/dist/location/backgroundGeolocationAdapter.d.ts +41 -0
- package/dist/location/backgroundGeolocationAdapter.js +199 -0
- package/dist/nightWindow.d.ts +34 -0
- package/dist/nightWindow.js +76 -0
- package/dist/queue.d.ts +51 -0
- package/dist/queue.js +125 -0
- package/dist/session.d.ts +100 -0
- package/dist/session.js +289 -0
- package/dist/storage.d.ts +17 -0
- package/dist/storage.js +35 -0
- package/dist/types.d.ts +116 -0
- package/dist/types.js +8 -0
- package/package.json +69 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { GeofenceConfig } from '../types';
|
|
2
|
+
import { AdapterLocationEvent, LocationAdapter, LocationEventListener, PermissionState } from './adapter';
|
|
3
|
+
/**
|
|
4
|
+
* Default adapter over `react-native-background-geolocation` (Transistorsoft).
|
|
5
|
+
*
|
|
6
|
+
* Chosen because it is the one battle-tested way to get what this product
|
|
7
|
+
* needs on both platforms: geofence enter/exit that survives app termination,
|
|
8
|
+
* a heartbeat that still fires under Doze, and mock-location flags. On Android
|
|
9
|
+
* it rides Google Play Services (fused location + Geofencing API) — the same
|
|
10
|
+
* Google location stack the rest of the Proxze platform uses; on iOS it rides
|
|
11
|
+
* CoreLocation region monitoring, which is why the "arrived and stayed" night
|
|
12
|
+
* strategy exists at all: iOS will not deliver 30-minute background pings, and
|
|
13
|
+
* the service's HYBRID evaluator already accounts for that.
|
|
14
|
+
*
|
|
15
|
+
* The library is an optional peer dependency — constructing this adapter
|
|
16
|
+
* without it installed throws with install instructions.
|
|
17
|
+
*/
|
|
18
|
+
export declare class BackgroundGeolocationAdapter implements LocationAdapter {
|
|
19
|
+
private readonly bg;
|
|
20
|
+
private listeners;
|
|
21
|
+
private subscriptions;
|
|
22
|
+
private started;
|
|
23
|
+
constructor(options?: {
|
|
24
|
+
license?: string;
|
|
25
|
+
debug?: boolean;
|
|
26
|
+
});
|
|
27
|
+
private readonly debug;
|
|
28
|
+
requestPermissions(): Promise<PermissionState>;
|
|
29
|
+
checkPermissions(): Promise<PermissionState>;
|
|
30
|
+
private toPermissionState;
|
|
31
|
+
registerGeofence(fence: GeofenceConfig): Promise<void>;
|
|
32
|
+
clearGeofence(): Promise<void>;
|
|
33
|
+
start(): Promise<void>;
|
|
34
|
+
stop(): Promise<void>;
|
|
35
|
+
getCurrentPosition(): Promise<AdapterLocationEvent>;
|
|
36
|
+
onEvent(listener: LocationEventListener): () => void;
|
|
37
|
+
private readyPromise;
|
|
38
|
+
private ready;
|
|
39
|
+
private fromLocation;
|
|
40
|
+
private emit;
|
|
41
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BackgroundGeolocationAdapter = void 0;
|
|
4
|
+
const GEOFENCE_ID = 'proxze-kyc-residence';
|
|
5
|
+
/**
|
|
6
|
+
* Default adapter over `react-native-background-geolocation` (Transistorsoft).
|
|
7
|
+
*
|
|
8
|
+
* Chosen because it is the one battle-tested way to get what this product
|
|
9
|
+
* needs on both platforms: geofence enter/exit that survives app termination,
|
|
10
|
+
* a heartbeat that still fires under Doze, and mock-location flags. On Android
|
|
11
|
+
* it rides Google Play Services (fused location + Geofencing API) — the same
|
|
12
|
+
* Google location stack the rest of the Proxze platform uses; on iOS it rides
|
|
13
|
+
* CoreLocation region monitoring, which is why the "arrived and stayed" night
|
|
14
|
+
* strategy exists at all: iOS will not deliver 30-minute background pings, and
|
|
15
|
+
* the service's HYBRID evaluator already accounts for that.
|
|
16
|
+
*
|
|
17
|
+
* The library is an optional peer dependency — constructing this adapter
|
|
18
|
+
* without it installed throws with install instructions.
|
|
19
|
+
*/
|
|
20
|
+
class BackgroundGeolocationAdapter {
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
var _a;
|
|
23
|
+
this.listeners = [];
|
|
24
|
+
this.subscriptions = [];
|
|
25
|
+
this.started = false;
|
|
26
|
+
// ---------------------------------------------------------------------------
|
|
27
|
+
this.readyPromise = null;
|
|
28
|
+
let mod;
|
|
29
|
+
try {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
31
|
+
mod = require('react-native-background-geolocation');
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
throw new Error('@proxze/kyc-react-native: the default location adapter needs the ' +
|
|
35
|
+
'optional peer dependency "react-native-background-geolocation". ' +
|
|
36
|
+
'Install and license it, or pass your own LocationAdapter.');
|
|
37
|
+
}
|
|
38
|
+
this.bg = (_a = mod === null || mod === void 0 ? void 0 : mod.default) !== null && _a !== void 0 ? _a : mod;
|
|
39
|
+
this.debug = Boolean(options.debug);
|
|
40
|
+
}
|
|
41
|
+
async requestPermissions() {
|
|
42
|
+
let status;
|
|
43
|
+
try {
|
|
44
|
+
status = await this.bg.requestPermission();
|
|
45
|
+
}
|
|
46
|
+
catch (denied) {
|
|
47
|
+
// The lib rejects with the status code on refusal.
|
|
48
|
+
status = typeof denied === 'number' ? denied : 0;
|
|
49
|
+
}
|
|
50
|
+
return this.toPermissionState(status);
|
|
51
|
+
}
|
|
52
|
+
async checkPermissions() {
|
|
53
|
+
const state = await this.bg.getProviderState();
|
|
54
|
+
return this.toPermissionState(state === null || state === void 0 ? void 0 : state.status, state);
|
|
55
|
+
}
|
|
56
|
+
async toPermissionState(status, provider) {
|
|
57
|
+
var _a, _b, _c;
|
|
58
|
+
const state = provider !== null && provider !== void 0 ? provider : (await this.bg.getProviderState().catch(() => null));
|
|
59
|
+
// iOS 14+ "Precise Location" off / Android 12+ "Approximate" grant.
|
|
60
|
+
// 2 = ACCURACY_AUTHORIZATION_REDUCED in the lib's constants.
|
|
61
|
+
const precise = !((state === null || state === void 0 ? void 0 : state.accuracyAuthorization) ===
|
|
62
|
+
((_a = this.bg.ACCURACY_AUTHORIZATION_REDUCED) !== null && _a !== void 0 ? _a : 2) &&
|
|
63
|
+
(state === null || state === void 0 ? void 0 : state.accuracyAuthorization) !== undefined);
|
|
64
|
+
// 3 = AUTHORIZATION_STATUS_ALWAYS, 4 = WHEN_IN_USE (per the lib's docs).
|
|
65
|
+
if (status === ((_b = this.bg.AUTHORIZATION_STATUS_ALWAYS) !== null && _b !== void 0 ? _b : 3)) {
|
|
66
|
+
return { grade: 'granted_always', precise };
|
|
67
|
+
}
|
|
68
|
+
if (status === ((_c = this.bg.AUTHORIZATION_STATUS_WHEN_IN_USE) !== null && _c !== void 0 ? _c : 4)) {
|
|
69
|
+
return { grade: 'granted_when_in_use', precise };
|
|
70
|
+
}
|
|
71
|
+
return { grade: 'denied', precise };
|
|
72
|
+
}
|
|
73
|
+
async registerGeofence(fence) {
|
|
74
|
+
await this.ready();
|
|
75
|
+
await this.bg.addGeofence({
|
|
76
|
+
identifier: GEOFENCE_ID,
|
|
77
|
+
radius: fence.radiusM,
|
|
78
|
+
latitude: fence.lat,
|
|
79
|
+
longitude: fence.lng,
|
|
80
|
+
notifyOnEntry: true,
|
|
81
|
+
notifyOnExit: true,
|
|
82
|
+
notifyOnDwell: true,
|
|
83
|
+
loiteringDelay: 5 * 60000,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
async clearGeofence() {
|
|
87
|
+
try {
|
|
88
|
+
await this.bg.removeGeofence(GEOFENCE_ID);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
/* already gone */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async start() {
|
|
95
|
+
await this.ready();
|
|
96
|
+
this.subscriptions.push(this.bg.onGeofence((event) => {
|
|
97
|
+
var _a;
|
|
98
|
+
const action = String((_a = event === null || event === void 0 ? void 0 : event.action) !== null && _a !== void 0 ? _a : '').toUpperCase();
|
|
99
|
+
const type = action === 'ENTER' ? 'enter' : action === 'EXIT' ? 'exit' : 'dwell';
|
|
100
|
+
this.emit(this.fromLocation(type, event === null || event === void 0 ? void 0 : event.location));
|
|
101
|
+
}));
|
|
102
|
+
// Heartbeat is the OS-tolerated stand-in for the BRD's 30-minute ping: it
|
|
103
|
+
// fires periodically even under Doze, and each firing samples a position.
|
|
104
|
+
this.subscriptions.push(this.bg.onHeartbeat(async () => {
|
|
105
|
+
try {
|
|
106
|
+
const location = await this.bg.getCurrentPosition({
|
|
107
|
+
samples: 1,
|
|
108
|
+
persist: false,
|
|
109
|
+
timeout: 30,
|
|
110
|
+
desiredAccuracy: 40,
|
|
111
|
+
});
|
|
112
|
+
this.emit(this.fromLocation('ping', location));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
this.emit({ type: 'heartbeat', recordedAt: new Date() });
|
|
116
|
+
}
|
|
117
|
+
}));
|
|
118
|
+
await this.bg.start();
|
|
119
|
+
// Geofence transitions keep arriving even in geofence-only mode, and
|
|
120
|
+
// starting monitoring without full tracking keeps battery drain polite.
|
|
121
|
+
this.started = true;
|
|
122
|
+
}
|
|
123
|
+
async stop() {
|
|
124
|
+
for (const sub of this.subscriptions) {
|
|
125
|
+
try {
|
|
126
|
+
sub.remove();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
/* noop */
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
this.subscriptions = [];
|
|
133
|
+
if (this.started) {
|
|
134
|
+
await this.bg.stop();
|
|
135
|
+
this.started = false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async getCurrentPosition() {
|
|
139
|
+
const location = await this.bg.getCurrentPosition({
|
|
140
|
+
samples: 1,
|
|
141
|
+
persist: false,
|
|
142
|
+
timeout: 30,
|
|
143
|
+
desiredAccuracy: 40,
|
|
144
|
+
});
|
|
145
|
+
return this.fromLocation('ping', location);
|
|
146
|
+
}
|
|
147
|
+
onEvent(listener) {
|
|
148
|
+
this.listeners.push(listener);
|
|
149
|
+
return () => {
|
|
150
|
+
this.listeners = this.listeners.filter((l) => l !== listener);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
ready() {
|
|
154
|
+
var _a;
|
|
155
|
+
if (this.readyPromise !== null)
|
|
156
|
+
return this.readyPromise;
|
|
157
|
+
{
|
|
158
|
+
this.readyPromise = this.bg
|
|
159
|
+
.ready({
|
|
160
|
+
desiredAccuracy: (_a = this.bg.DESIRED_ACCURACY_HIGH) !== null && _a !== void 0 ? _a : -1,
|
|
161
|
+
distanceFilter: 50,
|
|
162
|
+
stopOnTerminate: false, // tracking must survive an app kill
|
|
163
|
+
startOnBoot: true, // and a phone restart mid-verification
|
|
164
|
+
heartbeatInterval: 30 * 60, // seconds — the 30-minute cadence
|
|
165
|
+
preventSuspend: false,
|
|
166
|
+
debug: this.debug,
|
|
167
|
+
logLevel: this.debug ? this.bg.LOG_LEVEL_VERBOSE : this.bg.LOG_LEVEL_OFF,
|
|
168
|
+
})
|
|
169
|
+
.then(() => undefined);
|
|
170
|
+
}
|
|
171
|
+
return this.readyPromise;
|
|
172
|
+
}
|
|
173
|
+
fromLocation(type, location) {
|
|
174
|
+
var _a, _b, _c, _d, _e;
|
|
175
|
+
return {
|
|
176
|
+
type,
|
|
177
|
+
lat: (_a = location === null || location === void 0 ? void 0 : location.coords) === null || _a === void 0 ? void 0 : _a.latitude,
|
|
178
|
+
lng: (_b = location === null || location === void 0 ? void 0 : location.coords) === null || _b === void 0 ? void 0 : _b.longitude,
|
|
179
|
+
accuracyM: (_c = location === null || location === void 0 ? void 0 : location.coords) === null || _c === void 0 ? void 0 : _c.accuracy,
|
|
180
|
+
recordedAt: (location === null || location === void 0 ? void 0 : location.timestamp) ? new Date(location.timestamp) : new Date(),
|
|
181
|
+
isMocked: Boolean(location === null || location === void 0 ? void 0 : location.mock),
|
|
182
|
+
batteryLevel: typeof ((_d = location === null || location === void 0 ? void 0 : location.battery) === null || _d === void 0 ? void 0 : _d.level) === 'number'
|
|
183
|
+
? Math.round(Math.abs(location.battery.level) * 100)
|
|
184
|
+
: undefined,
|
|
185
|
+
isCharging: (_e = location === null || location === void 0 ? void 0 : location.battery) === null || _e === void 0 ? void 0 : _e.isCharging,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
emit(event) {
|
|
189
|
+
for (const listener of this.listeners) {
|
|
190
|
+
try {
|
|
191
|
+
listener(event);
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
/* a listener throwing must not kill the pipeline */
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
exports.BackgroundGeolocationAdapter = BackgroundGeolocationAdapter;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Night-window arithmetic, mirroring the service's `time.util.ts`.
|
|
3
|
+
*
|
|
4
|
+
* Nigeria is a single fixed offset (WAT, UTC+1, no DST), so fixed-offset maths
|
|
5
|
+
* is correct and avoids shipping a timezone database to the handset.
|
|
6
|
+
*/
|
|
7
|
+
export declare const WAT_OFFSET_MINUTES = 60;
|
|
8
|
+
export declare const NIGHT_START_HOUR = 0;
|
|
9
|
+
export declare const NIGHT_END_HOUR = 5;
|
|
10
|
+
/** How long after 05:00 the morning check remains useful (matches server). */
|
|
11
|
+
export declare const MORNING_CHECK_GRACE_MINUTES = 180;
|
|
12
|
+
/** BRD cadence — one sample every 30 minutes inside the window. */
|
|
13
|
+
export declare const PING_INTERVAL_MINUTES = 30;
|
|
14
|
+
export interface NightWindow {
|
|
15
|
+
nightDate: string;
|
|
16
|
+
startUtc: Date;
|
|
17
|
+
endUtc: Date;
|
|
18
|
+
}
|
|
19
|
+
export declare const buildWindow: (nightDate: string, offsetMinutes?: number) => NightWindow;
|
|
20
|
+
/** The window `instant` falls into (nights are labelled by their start date). */
|
|
21
|
+
export declare const nightWindowFor: (instant: Date, offsetMinutes?: number) => NightWindow;
|
|
22
|
+
export declare const isWithinNightWindow: (instant: Date, offsetMinutes?: number) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* True when `instant` sits inside the morning-check slot — the three hours
|
|
25
|
+
* after the most recent window closed. The arrived-and-stayed strategy needs
|
|
26
|
+
* one in-fence sample here to close the night.
|
|
27
|
+
*/
|
|
28
|
+
export declare const isInMorningCheckSlot: (instant: Date, offsetMinutes?: number) => boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Milliseconds until the next moment the SDK wants a location sample:
|
|
31
|
+
* the next 30-minute ping mark inside tonight's window, the morning check
|
|
32
|
+
* right after it closes, or the opening of the next window.
|
|
33
|
+
*/
|
|
34
|
+
export declare const msUntilNextSample: (now?: Date, offsetMinutes?: number) => number;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Night-window arithmetic, mirroring the service's `time.util.ts`.
|
|
4
|
+
*
|
|
5
|
+
* Nigeria is a single fixed offset (WAT, UTC+1, no DST), so fixed-offset maths
|
|
6
|
+
* is correct and avoids shipping a timezone database to the handset.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.msUntilNextSample = exports.isInMorningCheckSlot = exports.isWithinNightWindow = exports.nightWindowFor = exports.buildWindow = exports.PING_INTERVAL_MINUTES = exports.MORNING_CHECK_GRACE_MINUTES = exports.NIGHT_END_HOUR = exports.NIGHT_START_HOUR = exports.WAT_OFFSET_MINUTES = void 0;
|
|
10
|
+
exports.WAT_OFFSET_MINUTES = 60;
|
|
11
|
+
exports.NIGHT_START_HOUR = 0; // 12:00 AM local
|
|
12
|
+
exports.NIGHT_END_HOUR = 5; // 05:00 AM local
|
|
13
|
+
/** How long after 05:00 the morning check remains useful (matches server). */
|
|
14
|
+
exports.MORNING_CHECK_GRACE_MINUTES = 180;
|
|
15
|
+
/** BRD cadence — one sample every 30 minutes inside the window. */
|
|
16
|
+
exports.PING_INTERVAL_MINUTES = 30;
|
|
17
|
+
const MINUTE = 60000;
|
|
18
|
+
const HOUR = 60 * MINUTE;
|
|
19
|
+
const toLocal = (utc, offset) => new Date(utc.getTime() + offset * MINUTE);
|
|
20
|
+
const toUtc = (local, offset) => new Date(local.getTime() - offset * MINUTE);
|
|
21
|
+
const isoDate = (d) => d.toISOString().slice(0, 10);
|
|
22
|
+
const buildWindow = (nightDate, offsetMinutes = exports.WAT_OFFSET_MINUTES) => {
|
|
23
|
+
const localMidnight = new Date(`${nightDate}T00:00:00.000Z`);
|
|
24
|
+
return {
|
|
25
|
+
nightDate,
|
|
26
|
+
startUtc: toUtc(new Date(localMidnight.getTime() + exports.NIGHT_START_HOUR * HOUR), offsetMinutes),
|
|
27
|
+
endUtc: toUtc(new Date(localMidnight.getTime() + exports.NIGHT_END_HOUR * HOUR), offsetMinutes),
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.buildWindow = buildWindow;
|
|
31
|
+
/** The window `instant` falls into (nights are labelled by their start date). */
|
|
32
|
+
const nightWindowFor = (instant, offsetMinutes = exports.WAT_OFFSET_MINUTES) => (0, exports.buildWindow)(isoDate(toLocal(instant, offsetMinutes)), offsetMinutes);
|
|
33
|
+
exports.nightWindowFor = nightWindowFor;
|
|
34
|
+
const isWithinNightWindow = (instant, offsetMinutes = exports.WAT_OFFSET_MINUTES) => {
|
|
35
|
+
const window = (0, exports.nightWindowFor)(instant, offsetMinutes);
|
|
36
|
+
return instant >= window.startUtc && instant < window.endUtc;
|
|
37
|
+
};
|
|
38
|
+
exports.isWithinNightWindow = isWithinNightWindow;
|
|
39
|
+
/**
|
|
40
|
+
* True when `instant` sits inside the morning-check slot — the three hours
|
|
41
|
+
* after the most recent window closed. The arrived-and-stayed strategy needs
|
|
42
|
+
* one in-fence sample here to close the night.
|
|
43
|
+
*/
|
|
44
|
+
const isInMorningCheckSlot = (instant, offsetMinutes = exports.WAT_OFFSET_MINUTES) => {
|
|
45
|
+
const local = toLocal(instant, offsetMinutes);
|
|
46
|
+
const hour = local.getUTCHours();
|
|
47
|
+
const graceEndHour = exports.NIGHT_END_HOUR + exports.MORNING_CHECK_GRACE_MINUTES / 60;
|
|
48
|
+
return hour >= exports.NIGHT_END_HOUR && hour < graceEndHour;
|
|
49
|
+
};
|
|
50
|
+
exports.isInMorningCheckSlot = isInMorningCheckSlot;
|
|
51
|
+
/**
|
|
52
|
+
* Milliseconds until the next moment the SDK wants a location sample:
|
|
53
|
+
* the next 30-minute ping mark inside tonight's window, the morning check
|
|
54
|
+
* right after it closes, or the opening of the next window.
|
|
55
|
+
*/
|
|
56
|
+
const msUntilNextSample = (now = new Date(), offsetMinutes = exports.WAT_OFFSET_MINUTES) => {
|
|
57
|
+
const window = (0, exports.nightWindowFor)(now, offsetMinutes);
|
|
58
|
+
if (now >= window.startUtc && now < window.endUtc) {
|
|
59
|
+
const sinceStart = now.getTime() - window.startUtc.getTime();
|
|
60
|
+
const nextMark = Math.floor(sinceStart / (exports.PING_INTERVAL_MINUTES * MINUTE)) + 1;
|
|
61
|
+
const next = window.startUtc.getTime() + nextMark * exports.PING_INTERVAL_MINUTES * MINUTE;
|
|
62
|
+
return Math.min(next, window.endUtc.getTime() + MINUTE) - now.getTime();
|
|
63
|
+
}
|
|
64
|
+
if ((0, exports.isInMorningCheckSlot)(now, offsetMinutes)) {
|
|
65
|
+
// In the grace slot: sample soon, then the cadence relaxes.
|
|
66
|
+
return 5 * MINUTE;
|
|
67
|
+
}
|
|
68
|
+
// Outside any active slot — wake at the next window start.
|
|
69
|
+
const local = toLocal(now, offsetMinutes);
|
|
70
|
+
const todayWindow = (0, exports.buildWindow)(isoDate(local), offsetMinutes);
|
|
71
|
+
const nextStart = now < todayWindow.startUtc
|
|
72
|
+
? todayWindow.startUtc
|
|
73
|
+
: new Date(todayWindow.startUtc.getTime() + 24 * HOUR);
|
|
74
|
+
return nextStart.getTime() - now.getTime();
|
|
75
|
+
};
|
|
76
|
+
exports.msUntilNextSample = msUntilNextSample;
|
package/dist/queue.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ProxzeKycClient } from './client';
|
|
2
|
+
import { StorageAdapter } from './storage';
|
|
3
|
+
import { LocationEventType } from './types';
|
|
4
|
+
export interface QueueEventInput {
|
|
5
|
+
type: LocationEventType;
|
|
6
|
+
lat?: number;
|
|
7
|
+
lng?: number;
|
|
8
|
+
accuracyM?: number;
|
|
9
|
+
recordedAt?: Date | string;
|
|
10
|
+
isMocked?: boolean;
|
|
11
|
+
batteryLevel?: number;
|
|
12
|
+
isCharging?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Store-and-forward queue between the location adapter and the API.
|
|
16
|
+
*
|
|
17
|
+
* Every event gets a client id at enqueue time and the server dedupes on it,
|
|
18
|
+
* so the flush strategy can afford to be simple: send oldest-first, drop from
|
|
19
|
+
* the queue whatever the server acknowledged (accepted OR duplicate), keep the
|
|
20
|
+
* rest for the next flush. A flush that dies mid-flight just re-sends.
|
|
21
|
+
*/
|
|
22
|
+
export declare class OfflineEventQueue {
|
|
23
|
+
private readonly storage;
|
|
24
|
+
private readonly kycId;
|
|
25
|
+
private events;
|
|
26
|
+
private loaded;
|
|
27
|
+
private flushing;
|
|
28
|
+
private counter;
|
|
29
|
+
constructor(storage: StorageAdapter, kycId: string);
|
|
30
|
+
private get key();
|
|
31
|
+
enqueue(input: QueueEventInput): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Push queued events to the server. Returns how many the server now has.
|
|
34
|
+
* Throws ProxzeKycError only on session closure (401) so the caller can stop
|
|
35
|
+
* tracking; transient failures resolve quietly and retry on the next flush.
|
|
36
|
+
*/
|
|
37
|
+
flush(client: ProxzeKycClient, device?: {
|
|
38
|
+
platform?: 'ios' | 'android';
|
|
39
|
+
sdkVersion?: string;
|
|
40
|
+
deviceId?: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
sent: number;
|
|
43
|
+
remaining: number;
|
|
44
|
+
}>;
|
|
45
|
+
size(): Promise<number>;
|
|
46
|
+
clear(): Promise<void>;
|
|
47
|
+
private load;
|
|
48
|
+
private persist;
|
|
49
|
+
/** Unique enough for dedupe without pulling in a uuid dependency. */
|
|
50
|
+
private newEventId;
|
|
51
|
+
}
|
package/dist/queue.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OfflineEventQueue = void 0;
|
|
4
|
+
const client_1 = require("./client");
|
|
5
|
+
/** Server accepts at most 200 events per batch. */
|
|
6
|
+
const MAX_BATCH = 200;
|
|
7
|
+
/** Cap the queue so a device offline for weeks doesn't grow unbounded. */
|
|
8
|
+
const MAX_QUEUE = 2000;
|
|
9
|
+
/**
|
|
10
|
+
* Store-and-forward queue between the location adapter and the API.
|
|
11
|
+
*
|
|
12
|
+
* Every event gets a client id at enqueue time and the server dedupes on it,
|
|
13
|
+
* so the flush strategy can afford to be simple: send oldest-first, drop from
|
|
14
|
+
* the queue whatever the server acknowledged (accepted OR duplicate), keep the
|
|
15
|
+
* rest for the next flush. A flush that dies mid-flight just re-sends.
|
|
16
|
+
*/
|
|
17
|
+
class OfflineEventQueue {
|
|
18
|
+
constructor(storage, kycId) {
|
|
19
|
+
this.storage = storage;
|
|
20
|
+
this.kycId = kycId;
|
|
21
|
+
this.events = [];
|
|
22
|
+
this.loaded = false;
|
|
23
|
+
this.flushing = false;
|
|
24
|
+
this.counter = 0;
|
|
25
|
+
}
|
|
26
|
+
get key() {
|
|
27
|
+
return `@proxze-kyc/queue/${this.kycId}`;
|
|
28
|
+
}
|
|
29
|
+
async enqueue(input) {
|
|
30
|
+
var _a;
|
|
31
|
+
await this.load();
|
|
32
|
+
const recordedAt = input.recordedAt instanceof Date
|
|
33
|
+
? input.recordedAt.toISOString()
|
|
34
|
+
: (_a = input.recordedAt) !== null && _a !== void 0 ? _a : new Date().toISOString();
|
|
35
|
+
this.events.push({
|
|
36
|
+
clientEventId: this.newEventId(),
|
|
37
|
+
type: input.type,
|
|
38
|
+
lat: input.lat,
|
|
39
|
+
lng: input.lng,
|
|
40
|
+
accuracyM: input.accuracyM,
|
|
41
|
+
recordedAt: recordedAt,
|
|
42
|
+
isMocked: input.isMocked,
|
|
43
|
+
batteryLevel: input.batteryLevel,
|
|
44
|
+
isCharging: input.isCharging,
|
|
45
|
+
});
|
|
46
|
+
// Oldest events go first: they are the ones a night verdict depends on.
|
|
47
|
+
if (this.events.length > MAX_QUEUE) {
|
|
48
|
+
this.events = this.events.slice(this.events.length - MAX_QUEUE);
|
|
49
|
+
}
|
|
50
|
+
await this.persist();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Push queued events to the server. Returns how many the server now has.
|
|
54
|
+
* Throws ProxzeKycError only on session closure (401) so the caller can stop
|
|
55
|
+
* tracking; transient failures resolve quietly and retry on the next flush.
|
|
56
|
+
*/
|
|
57
|
+
async flush(client, device = {}) {
|
|
58
|
+
await this.load();
|
|
59
|
+
if (this.flushing || !this.events.length) {
|
|
60
|
+
return { sent: 0, remaining: this.events.length };
|
|
61
|
+
}
|
|
62
|
+
this.flushing = true;
|
|
63
|
+
let sent = 0;
|
|
64
|
+
try {
|
|
65
|
+
while (this.events.length) {
|
|
66
|
+
const batch = this.events.slice(0, MAX_BATCH);
|
|
67
|
+
let response;
|
|
68
|
+
try {
|
|
69
|
+
response = await client.submitEvents(batch, device);
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (err instanceof client_1.ProxzeKycError && err.isSessionClosed)
|
|
73
|
+
throw err;
|
|
74
|
+
break; // transient — keep the queue, try again later
|
|
75
|
+
}
|
|
76
|
+
// Acknowledged either way; rejected events are refused permanently
|
|
77
|
+
// (malformed/expired) and re-sending them would loop forever.
|
|
78
|
+
this.events = this.events.slice(batch.length);
|
|
79
|
+
sent += response.accepted + response.duplicates;
|
|
80
|
+
await this.persist();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
this.flushing = false;
|
|
85
|
+
await this.persist();
|
|
86
|
+
}
|
|
87
|
+
return { sent, remaining: this.events.length };
|
|
88
|
+
}
|
|
89
|
+
async size() {
|
|
90
|
+
await this.load();
|
|
91
|
+
return this.events.length;
|
|
92
|
+
}
|
|
93
|
+
async clear() {
|
|
94
|
+
this.events = [];
|
|
95
|
+
await this.storage.removeItem(this.key);
|
|
96
|
+
}
|
|
97
|
+
async load() {
|
|
98
|
+
if (this.loaded)
|
|
99
|
+
return;
|
|
100
|
+
try {
|
|
101
|
+
const raw = await this.storage.getItem(this.key);
|
|
102
|
+
if (raw)
|
|
103
|
+
this.events = JSON.parse(raw);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
this.events = [];
|
|
107
|
+
}
|
|
108
|
+
this.loaded = true;
|
|
109
|
+
}
|
|
110
|
+
async persist() {
|
|
111
|
+
try {
|
|
112
|
+
await this.storage.setItem(this.key, JSON.stringify(this.events));
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
/* storage full/unavailable — queue still lives in memory */
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** Unique enough for dedupe without pulling in a uuid dependency. */
|
|
119
|
+
newEventId() {
|
|
120
|
+
this.counter = (this.counter + 1) % 0xffff;
|
|
121
|
+
const random = Math.random().toString(36).slice(2, 10);
|
|
122
|
+
return `${Date.now().toString(36)}-${random}-${this.counter.toString(16)}`;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.OfflineEventQueue = OfflineEventQueue;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ProxzeKycClient } from './client';
|
|
2
|
+
import { StorageAdapter } from './storage';
|
|
3
|
+
import { LocationAdapter, PermissionState } from './location/adapter';
|
|
4
|
+
import { BillUploadResponse, RestartResponse, RNFile, SdkSession, UtilityDocumentType, VideoMeta, VideoUploadResponse } from './types';
|
|
5
|
+
export type KycTrackingState = 'idle' | 'awaiting_consent' | 'awaiting_permissions' | 'awaiting_address_pin' | 'tracking' | 'completed' | 'stopped';
|
|
6
|
+
export interface KycSessionEvents {
|
|
7
|
+
onStateChange?: (state: KycTrackingState) => void;
|
|
8
|
+
onSessionUpdate?: (session: SdkSession) => void;
|
|
9
|
+
onError?: (error: Error) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface ProxzeKycSessionOptions {
|
|
12
|
+
/** e.g. `https://api.proxze.com`. */
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
/** From the bank backend's `POST /v2/kyc/initiate` response. */
|
|
15
|
+
sessionToken: string;
|
|
16
|
+
/** Location stack. Defaults to the background-geolocation adapter. */
|
|
17
|
+
adapter?: LocationAdapter;
|
|
18
|
+
/** Queue persistence. Defaults to AsyncStorage when installed. */
|
|
19
|
+
storage?: StorageAdapter;
|
|
20
|
+
deviceId?: string;
|
|
21
|
+
events?: KycSessionEvents;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Orchestrates one verification on the device:
|
|
25
|
+
*
|
|
26
|
+
* bootstrap → consent → permissions → (optional address pin) → fence
|
|
27
|
+
* registration → night tracking → evidence uploads → done.
|
|
28
|
+
*
|
|
29
|
+
* Night tracking is dual-mode, matching the service's HYBRID evaluator:
|
|
30
|
+
* geofence enter/exit transitions arrive from the OS whenever they happen,
|
|
31
|
+
* and a scheduler requests position samples on the 30-minute marks inside
|
|
32
|
+
* the 12AM–5AM window plus one morning check after it closes. Whichever
|
|
33
|
+
* signals survive the device's battery policies, the server can score.
|
|
34
|
+
*/
|
|
35
|
+
export declare class ProxzeKycSession {
|
|
36
|
+
readonly client: ProxzeKycClient;
|
|
37
|
+
private readonly storage;
|
|
38
|
+
private readonly events;
|
|
39
|
+
private readonly deviceId?;
|
|
40
|
+
private adapter?;
|
|
41
|
+
private adapterFactory?;
|
|
42
|
+
private queue?;
|
|
43
|
+
private session?;
|
|
44
|
+
private state;
|
|
45
|
+
private sampleTimer?;
|
|
46
|
+
private flushTimer?;
|
|
47
|
+
private unsubscribe?;
|
|
48
|
+
constructor(options: ProxzeKycSessionOptions);
|
|
49
|
+
getState(): KycTrackingState;
|
|
50
|
+
getSession(): SdkSession | undefined;
|
|
51
|
+
/** Fetch server-side state and derive the local step. */
|
|
52
|
+
bootstrap(): Promise<SdkSession>;
|
|
53
|
+
/** Record NDPR consent. Must precede any location work. */
|
|
54
|
+
grantConsent(consentVersion: string): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Re-centre the fence on the customer's pin — required when the declared
|
|
57
|
+
* address geocoded too coarsely for a 100m fence.
|
|
58
|
+
*/
|
|
59
|
+
confirmAddress(pin: {
|
|
60
|
+
lat: number;
|
|
61
|
+
lng: number;
|
|
62
|
+
accuracyM?: number;
|
|
63
|
+
}): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Request permissions, register the fence and begin the night loop.
|
|
66
|
+
*
|
|
67
|
+
* Returns the granted permission state so the app can route the customer:
|
|
68
|
+
* `denied` → explain and re-prompt; `precise: false` → walk them to
|
|
69
|
+
* settings (approximate location cannot confirm a 100m fence);
|
|
70
|
+
* `granted_when_in_use` → warn that overnight confirmation is unlikely.
|
|
71
|
+
* Tracking starts on anything better than an outright denial — degraded
|
|
72
|
+
* signals still feed the server's hybrid evaluator.
|
|
73
|
+
*/
|
|
74
|
+
startTracking(): Promise<PermissionState>;
|
|
75
|
+
/** Stop monitoring and clear the fence (verification finished or abandoned). */
|
|
76
|
+
stopTracking(): Promise<void>;
|
|
77
|
+
/** NDPR — stops collection immediately, server-side and locally. */
|
|
78
|
+
withdrawConsent(): Promise<void>;
|
|
79
|
+
uploadUtilityBill(file: RNFile, documentType?: UtilityDocumentType): Promise<BillUploadResponse>;
|
|
80
|
+
uploadVideo(file: RNFile, meta: VideoMeta): Promise<VideoUploadResponse>;
|
|
81
|
+
/** Restart the window after a missed night (FLOW-03). */
|
|
82
|
+
restart(): Promise<RestartResponse>;
|
|
83
|
+
refresh(): Promise<SdkSession>;
|
|
84
|
+
/**
|
|
85
|
+
* Tracking health for a status screen or support tooling: what the OS is
|
|
86
|
+
* actually letting us do, and whether events are reaching the server.
|
|
87
|
+
*/
|
|
88
|
+
diagnostics(): Promise<{
|
|
89
|
+
state: KycTrackingState;
|
|
90
|
+
permission: PermissionState | null;
|
|
91
|
+
queuedEvents: number;
|
|
92
|
+
sessionStatus: string | null;
|
|
93
|
+
}>;
|
|
94
|
+
private resolveAdapter;
|
|
95
|
+
private handleEvent;
|
|
96
|
+
private scheduleNextSample;
|
|
97
|
+
private flushQueue;
|
|
98
|
+
private setState;
|
|
99
|
+
private emitError;
|
|
100
|
+
}
|