From 54af23202edde108a7c21ecf4e1442a20de22ec5 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Sun, 15 Dec 2024 22:16:26 +0000 Subject: [PATCH 1/7] add IP change listener to getVerifiedLocationToken(), bump version --- README.md | 14 +++++++------- package-lock.json | 2 +- package.json | 2 +- src/api/track.ts | 34 ---------------------------------- src/api/verify.ts | 24 +++++++++++++++++++++++- src/types.ts | 1 + src/version.ts | 2 +- 7 files changed, 34 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 6eb14a71..cc8cd1a8 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index 92f02483..898a1456 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.8", + "version": "4.4.9-beta.1", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 9f0b3fd3..9c686a8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.8", + "version": "4.4.9-beta.1", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/api/track.ts b/src/api/track.ts index 3862a44a..5add1e7e 100644 --- a/src/api/track.ts +++ b/src/api/track.ts @@ -89,7 +89,6 @@ class TrackAPI { let response: any; if (fraud) { const host = 'https://api-verified.radar.io'; - const pingHost = 'ping.radar-verify.com'; const lang = navigator.language; const langs = navigator.languages; @@ -109,31 +108,9 @@ class TrackAPI { }, }); - let sclVal = -1; - let cslVal = -1; - /* - try { - const [sclRes, csl] = await Promise.all([ - Http.request({ - host: `https://${pingHost}`, - method: 'GET', - path: 'ping', - }), - ping(`wss://${pingHost}`), - ]); - const { scl }: any = sclRes; - sclVal = scl; - cslVal = csl; - } catch (err) { - // do nothing, send scl = -1 and csl = -1 - } - */ - const payload = { payload: JSON.stringify({ ...body, - scl: sclVal, - csl: cslVal, lang, langs, }), @@ -153,17 +130,6 @@ class TrackAPI { }, }); - if (options.debug && response && response.user) { - if (!response.user.metadata) { - response.user.metadata = {}; - } - - response.user.metadata['radar:debug'] = { - sclVal, - cslVal, - }; - } - let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response; const location = { latitude, longitude, accuracy }; if (expiresAt) { diff --git a/src/api/verify.ts b/src/api/verify.ts index 9bcb6107..57eb9f64 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -15,8 +15,25 @@ let lastToken: RadarTrackVerifiedResponse | null = null; let lastTokenNow: number = 0; let expectedCountryCode: string | null = null; let expectedStateCode: string | null = null; +let lastIp: string | null = null; class VerifyAPI { + static async ipChanged() { + const { ip }: any = await Http.request({ + method: 'GET', + path: 'ping', + }); + + const ipChanged = ip !== lastIp; + if (ipChanged) { + Logger.info(`IP changed from ${lastIp} to ${ip}`); + } + + lastIp = ip; + + return ipChanged; + } + static async trackVerified(params: RadarTrackVerifiedParams, encrypted: Boolean = false) { const options = Config.get(); @@ -164,7 +181,12 @@ class VerifyAPI { static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) { const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; - if (lastToken && lastToken.passed) { + let ipChanged = false; + if (params.ipChanges) { + ipChanged = await this.ipChanged(); + } + + if (lastToken && lastToken.passed && !ipChanged) { if (lastTokenElapsed < (lastToken.expiresIn || 0)) { return lastToken; } diff --git a/src/types.ts b/src/types.ts index c10c82a8..1d9b5a4c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,6 +81,7 @@ export interface RadarTrackVerifiedParams { description?: string; metadata?: RadarMetadata; skipVerifyApp?: boolean; + ipChanges?: boolean; } export interface RadarStartTrackingVerifiedParams { diff --git a/src/version.ts b/src/version.ts index aedeb33a..537fab8a 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.8'; \ No newline at end of file +export default '4.4.9-beta.1'; \ No newline at end of file From 59c80404e0fac532095ac54cbc640d320ccee965 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Sun, 15 Dec 2024 22:40:20 +0000 Subject: [PATCH 2/7] pick up IP in startTrackingVerified() --- src/api/verify.ts | 13 +++++++++++-- src/types.ts | 2 ++ src/ui/RadarMap.ts | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/api/verify.ts b/src/api/verify.ts index 57eb9f64..5f4d10f0 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -132,6 +132,12 @@ class VerifyAPI { } static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) { + let { interval } = params; + if (!interval) { + interval = 60; + Logger.info(`interval not provided, using 60 seconds`); + } + const doTrackVerified = async () => { let trackRes; try { @@ -139,8 +145,6 @@ class VerifyAPI { } catch (err: any) { Logger.error(`trackVerified error: ${err.message}`); } - - const { interval } = params; let expiresIn = 0; let minInterval = interval; @@ -148,6 +152,11 @@ class VerifyAPI { if (trackRes) { expiresIn = (trackRes.expiresIn || expiresIn); + if (params.ipChanges && trackRes.user?.ip) { + lastIp = trackRes.user?.ip; + Logger.info(`Setting ip to ${lastIp}`); + } + // if expiresIn is shorter than interval, override interval minInterval = Math.min(expiresIn, interval); } diff --git a/src/types.ts b/src/types.ts index 1d9b5a4c..32f152ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -87,6 +87,7 @@ export interface RadarTrackVerifiedParams { export interface RadarStartTrackingVerifiedParams { interval: number; skipVerifyApp?: boolean; + ipChanges?: boolean; } export enum RadarEventConfidence { @@ -218,6 +219,7 @@ export interface RadarUser { dma?: RadarRegion; postalCode?: RadarRegion; fraud?: RadarFraud; + ip?: string; } export interface RadarTrackResponse extends RadarResponse { diff --git a/src/ui/RadarMap.ts b/src/ui/RadarMap.ts index 7d04429a..09593531 100644 --- a/src/ui/RadarMap.ts +++ b/src/ui/RadarMap.ts @@ -96,7 +96,7 @@ class RadarMap extends maplibregl.Map { radarMapOptions, { style }, ); - Logger.debug('map initailized with options', mapOptions); + Logger.debug('map initialized with options', mapOptions); (mapOptions as maplibregl.MapOptions).transformRequest = (url, resourceType) => { // this handles when a style is switched From 743fc9414cca1b462b03f193fce23b9d49f4682a Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Sun, 15 Dec 2024 22:46:18 +0000 Subject: [PATCH 3/7] move IP set to trackVerified() --- src/api/verify.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/verify.ts b/src/api/verify.ts index 5f4d10f0..40d0eb54 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -103,6 +103,11 @@ class VerifyAPI { if (expiresAt) { expiresAt = new Date(expiresAt); } + + if (params.ipChanges && user?.ip) { + lastIp = user?.ip; + Logger.info(`Setting ip to ${lastIp}`); + } trackRes = { user, @@ -152,11 +157,6 @@ class VerifyAPI { if (trackRes) { expiresIn = (trackRes.expiresIn || expiresIn); - if (params.ipChanges && trackRes.user?.ip) { - lastIp = trackRes.user?.ip; - Logger.info(`Setting ip to ${lastIp}`); - } - // if expiresIn is shorter than interval, override interval minInterval = Math.min(expiresIn, interval); } From 2433b4492e8dffda2e584ee09728c1e1b403be39 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Sun, 15 Dec 2024 22:47:13 +0000 Subject: [PATCH 4/7] move IP set to trackVerified() --- src/api/verify.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/api/verify.ts b/src/api/verify.ts index 40d0eb54..d528bd36 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -103,11 +103,6 @@ class VerifyAPI { if (expiresAt) { expiresAt = new Date(expiresAt); } - - if (params.ipChanges && user?.ip) { - lastIp = user?.ip; - Logger.info(`Setting ip to ${lastIp}`); - } trackRes = { user, @@ -126,6 +121,11 @@ class VerifyAPI { } } + if (params.ipChanges && trackRes?.user?.ip) { + lastIp = trackRes?.user?.ip; + Logger.info(`Setting ip to ${lastIp}`); + } + lastToken = trackRes; lastTokenNow = performance.now(); From bb2d127d3a185d40b7c6fa1a02a6106dd3a8f5c4 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Sun, 15 Dec 2024 22:56:28 +0000 Subject: [PATCH 5/7] bump version --- README.md | 14 +++++++------- package-lock.json | 2 +- package.json | 2 +- src/version.ts | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cc8cd1a8..4cacd7c5 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index 898a1456..a72f0b37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9-beta.1", + "version": "4.4.9-beta.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 9c686a8d..3fe80515 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9-beta.1", + "version": "4.4.9-beta.0", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/version.ts b/src/version.ts index 537fab8a..90861533 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.9-beta.1'; \ No newline at end of file +export default '4.4.9-beta.0'; \ No newline at end of file From e39b30b3b8f8a9f9cf18f7ffed6c05424e9f8dab Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Mon, 23 Dec 2024 07:54:56 -0500 Subject: [PATCH 6/7] null token on IP change --- README.md | 14 +++++++------- package-lock.json | 2 +- package.json | 2 +- src/api/verify.ts | 9 +++++---- src/version.ts | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4cacd7c5..17017aa2 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index a72f0b37..92d45c02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9-beta.0", + "version": "4.4.10-beta.0", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 3fe80515..31e037b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9-beta.0", + "version": "4.4.10-beta.0", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/api/verify.ts b/src/api/verify.ts index d528bd36..43b94748 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -18,7 +18,7 @@ let expectedStateCode: string | null = null; let lastIp: string | null = null; class VerifyAPI { - static async ipChanged() { + static async checkIpChanges() { const { ip }: any = await Http.request({ method: 'GET', path: 'ping', @@ -27,6 +27,8 @@ class VerifyAPI { const ipChanged = ip !== lastIp; if (ipChanged) { Logger.info(`IP changed from ${lastIp} to ${ip}`); + + lastToken = null; } lastIp = ip; @@ -190,12 +192,11 @@ class VerifyAPI { static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) { const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; - let ipChanged = false; if (params.ipChanges) { - ipChanged = await this.ipChanged(); + await this.checkIpChanges(); } - if (lastToken && lastToken.passed && !ipChanged) { + if (lastToken && lastToken.passed) { if (lastTokenElapsed < (lastToken.expiresIn || 0)) { return lastToken; } diff --git a/src/version.ts b/src/version.ts index 90861533..dbb4df5f 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.9-beta.0'; \ No newline at end of file +export default '4.4.10-beta.0'; \ No newline at end of file From 77b940a98250fd8f65e30413e961026b5c9bfc3b Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Mon, 23 Dec 2024 09:26:21 -0500 Subject: [PATCH 7/7] better status codes for network errors and timeouts --- README.md | 14 +++++++------- package-lock.json | 2 +- package.json | 2 +- src/http.ts | 8 ++++++-- src/version.ts | 2 +- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 17017aa2..ef162016 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index 92d45c02..339ebda1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.10-beta.0", + "version": "4.4.10-beta.1", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 31e037b3..d4396a29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.10-beta.0", + "version": "4.4.10-beta.1", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/http.ts b/src/http.ts index 625276e6..60e06a11 100644 --- a/src/http.ts +++ b/src/http.ts @@ -189,12 +189,16 @@ class Http { if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) { reject(new RadarVerifyAppError()); } else { - reject(new RadarServerError()); + reject(new RadarNetworkError()); } } xhr.ontimeout = function () { - reject(new RadarVerifyAppError()); + if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) { + reject(new RadarVerifyAppError()); + } else { + reject(new RadarNetworkError()); + } } xhr.send(JSON.stringify(body)); diff --git a/src/version.ts b/src/version.ts index dbb4df5f..c07025a8 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.10-beta.0'; \ No newline at end of file +export default '4.4.10-beta.1'; \ No newline at end of file