From 5efff0340ca400153dde3dd84a189e4312f44103 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Wed, 22 Jan 2025 21:39:33 +0100 Subject: [PATCH 1/4] avoid double request, add clearVerifiedLocationToken() --- README.md | 14 +++++----- package-lock.json | 2 +- package.json | 2 +- src/api.ts | 8 ++++-- src/api/track.ts | 34 ------------------------ src/api/verify.ts | 64 +++++++++++++++++++++++++++++----------------- src/http.ts | 8 ++++-- src/ui/RadarMap.ts | 2 +- 8 files changed, 62 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 4d3856fe..e0698587 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 e8ccc4fa..cb0bc684 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9", + "version": "4.4.10", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index f0674883..df56c923 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.9", + "version": "4.4.10", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/api.ts b/src/api.ts index 93e3dc6c..6a414c0c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -135,17 +135,21 @@ class Radar { } public static startTrackingVerified(params: RadarStartTrackingVerifiedParams) { - return VerifyAPI.startTrackingVerified(params); + VerifyAPI.startTrackingVerified(params); } public static stopTrackingVerified() { - return VerifyAPI.stopTrackingVerified(); + VerifyAPI.stopTrackingVerified(); } public static getVerifiedLocationToken(params: RadarTrackVerifiedParams = {}) { return VerifyAPI.getVerifiedLocationToken(params); } + public static clearVerifiedLocationToken() { + VerifyAPI.clearVerifiedLocationToken(); + } + public static setExpectedJurisdiction(countryCode?: string, stateCode?: string) { VerifyAPI.setExpectedJurisdiction(countryCode, stateCode); } 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..1960a3b1 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -114,31 +114,23 @@ class VerifyAPI { return trackRes; } - static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) { - const doTrackVerified = async () => { - let trackRes; - try { - trackRes = await this.trackVerified(params); - } catch (err: any) { - Logger.error(`trackVerified error: ${err.message}`); - } - + static startTrackingVerified(params: RadarStartTrackingVerifiedParams) { + const scheduleNextIntervalWithLastToken = async () => { const { interval } = params; - let expiresIn = 0; let minInterval = interval; - if (trackRes) { - expiresIn = (trackRes.expiresIn || expiresIn); + if (lastToken) { + const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; + + const expiresIn = (lastToken.expiresIn || 0); // if expiresIn is shorter than interval, override interval - minInterval = Math.min(expiresIn, interval); + // re-request early to maximize the likelihood that a cached token is available + minInterval = Math.min(expiresIn - lastTokenElapsed, interval); } - // re-request early to maximize the likelihood that a cached token is available - if (minInterval > 20) { - minInterval = minInterval - 10; - } + minInterval = minInterval - 10; // min interval is 10 seconds if (minInterval < 10) { @@ -150,9 +142,23 @@ class VerifyAPI { } tokenTimeoutId = setTimeout(doTrackVerified, minInterval * 1000); + } + + const doTrackVerified = async () => { + try { + await this.trackVerified(params); + } catch (err: any) { + Logger.error(`trackVerified error: ${err.message}`); + } + + scheduleNextIntervalWithLastToken(); }; - doTrackVerified(); + if (this.isLastTokenValid()) { + scheduleNextIntervalWithLastToken(); + } else { + doTrackVerified(); + } } static stopTrackingVerified() { @@ -162,17 +168,27 @@ class VerifyAPI { } static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) { - const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; - - if (lastToken && lastToken.passed) { - if (lastTokenElapsed < (lastToken.expiresIn || 0)) { - return lastToken; - } + if (this.isLastTokenValid()) { + return lastToken; } return this.trackVerified(params); } + static clearVerifiedLocationToken() { + lastToken = null; + } + + static isLastTokenValid() { + if (!lastToken) { + return false; + } + + const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; + + return lastToken.passed && lastTokenElapsed < (lastToken.expiresIn || 0); + } + static setExpectedJurisdiction(countryCode?: string, stateCode?: string) { expectedCountryCode = countryCode || null; expectedStateCode = stateCode || null; 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/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 f11886344aa95d6f4951439c484c0b088fbee7c5 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Wed, 22 Jan 2025 22:00:20 +0100 Subject: [PATCH 2/4] package-lock --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index cb0bc684..6f2b7bf3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "radar-sdk-js", - "version": "4.4.6-beta.1", + "version": "4.4.10", "license": "ISC", "dependencies": { "@types/geojson": "^7946.0.10" From 1b2930752900e90712d95e8726165496e3ec6dc7 Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Wed, 22 Jan 2025 22:02:36 +0100 Subject: [PATCH 3/4] bump version --- src/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.ts b/src/version.ts index a567a62f..6fa3ea42 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.9'; \ No newline at end of file +export default '4.4.10'; \ No newline at end of file From 2f99d423c25bc45c00eb768be8c68fce2d16e47c Mon Sep 17 00:00:00 2001 From: Nick Patrick Date: Wed, 22 Jan 2025 22:10:03 +0100 Subject: [PATCH 4/4] fix test --- test/http.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/http.test.ts b/test/http.test.ts index 70895c29..8f923e4e 100644 --- a/test/http.test.ts +++ b/test/http.test.ts @@ -142,7 +142,7 @@ describe('Http', () => { try { await Http.request(httpRequestParams); } catch (e: any) { - expect(e.status).toEqual('ERROR_SERVER'); + expect(e.status).toEqual('ERROR_NETWORK'); } });