@chaosity/location-client 0.7.0 → 0.9.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/README.md +73 -12
- package/dist/cjs/client/GeoPlacesClient.d.ts +11 -3
- package/dist/cjs/client/GeoPlacesClient.js +38 -12
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/maps/mapStyle.d.ts +3 -1
- package/dist/cjs/maps/mapStyle.js +24 -19
- package/dist/cjs/maps/staticMap.d.ts +3 -1
- package/dist/cjs/maps/staticMap.js +18 -12
- package/dist/cjs/server/LocationServiceConnector.d.ts +1 -1
- package/dist/cjs/server/LocationServiceConnector.js +17 -20
- package/dist/cjs/server/getClientConfig.d.ts +19 -0
- package/dist/cjs/server/getClientConfig.js +44 -10
- package/dist/cjs/transport/errors.d.ts +18 -1
- package/dist/cjs/transport/errors.js +25 -1
- package/dist/cjs/transport/http.d.ts +36 -0
- package/dist/cjs/transport/http.js +118 -15
- package/dist/cjs/types/index.d.ts +19 -2
- package/dist/cjs/utils/tokenClaims.d.ts +11 -13
- package/dist/cjs/utils/tokenClaims.js +11 -14
- package/dist/client/GeoPlacesClient.d.ts +11 -3
- package/dist/client/GeoPlacesClient.js +39 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/maps/mapStyle.d.ts +3 -1
- package/dist/maps/mapStyle.js +25 -20
- package/dist/maps/staticMap.d.ts +3 -1
- package/dist/maps/staticMap.js +19 -13
- package/dist/server/LocationServiceConnector.d.ts +1 -1
- package/dist/server/LocationServiceConnector.js +18 -21
- package/dist/server/getClientConfig.d.ts +19 -0
- package/dist/server/getClientConfig.js +43 -10
- package/dist/transport/errors.d.ts +18 -1
- package/dist/transport/errors.js +24 -1
- package/dist/transport/http.d.ts +36 -0
- package/dist/transport/http.js +116 -14
- package/dist/types/index.d.ts +19 -2
- package/dist/utils/tokenClaims.d.ts +11 -13
- package/dist/utils/tokenClaims.js +11 -14
- package/package.json +1 -1
- package/dist/cjs/utils/roundPosition.d.ts +0 -66
- package/dist/cjs/utils/roundPosition.js +0 -109
- package/dist/utils/roundPosition.d.ts +0 -66
- package/dist/utils/roundPosition.js +0 -104
package/dist/types/index.d.ts
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* At least one of `token`, `getToken` or `refreshToken` must supply a token, or
|
|
3
|
+
* `send` refuses locally with `InvalidCredentialsException` rather than putting
|
|
4
|
+
* `Bearer undefined` on the wire (#37).
|
|
5
|
+
*
|
|
6
|
+
* `token` is optional because a client driven purely by a provider — the shape
|
|
7
|
+
* `@chaosity/location-client-react` uses — has nothing to put there at
|
|
8
|
+
* construction time, and was previously forced to invent a placeholder. It is
|
|
9
|
+
* still required on `ServerClientConfig`, which is a RESULT rather than a
|
|
10
|
+
* configuration: `getClientConfig` always resolves one.
|
|
11
|
+
*/
|
|
1
12
|
export interface ClientConfig {
|
|
2
13
|
apiUrl: string;
|
|
3
|
-
token
|
|
14
|
+
token?: string;
|
|
4
15
|
/** Optional callback to get the current token dynamically. When provided,
|
|
5
16
|
* called on every request so token updates are reflected without recreating the client. */
|
|
6
17
|
getToken?: () => string | undefined;
|
|
7
18
|
/**
|
|
8
19
|
* Asked for a replacement AFTER the API has rejected the current token with a
|
|
9
|
-
* 401, so the request can be retried once instead of failing
|
|
20
|
+
* 401, so the request can be retried once instead of failing — and, since
|
|
21
|
+
* #37, asked once BEFORE the first send when neither `getToken` nor `token`
|
|
22
|
+
* yields anything, so a client whose token has not arrived yet does not spend
|
|
23
|
+
* a round trip on `Bearer undefined` to learn that. Both calls mean the same
|
|
24
|
+
* thing to an implementor — "give me a usable token" — so a `refreshToken`
|
|
25
|
+
* that mints or awaits one needs no change; only one that assumes every
|
|
26
|
+
* invocation follows a 401 does.
|
|
10
27
|
*
|
|
11
28
|
* Separate from `getToken` because that one is synchronous by contract — it
|
|
12
29
|
* is read while a request is being built and cannot await anything, so it can
|
|
@@ -1,29 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Read advisory application config out of the access token (api#65).
|
|
3
3
|
*
|
|
4
|
-
* The API puts an application's own settings —
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* The API puts an application's own settings — today just `countries` — into
|
|
5
|
+
* the JWT alongside `allowedDomain` and `allowedResources`, so this library can
|
|
6
|
+
* stop hard-coding values it has no other way of knowing.
|
|
7
|
+
*
|
|
8
|
+
* `biasDecimals` used to be here too. It sized the grid this library rounded
|
|
9
|
+
* `BiasPosition` onto, so that nearby callers shared a server cache entry; the
|
|
10
|
+
* cache is gone, the API issues no such claim, and rounding a coordinate with
|
|
11
|
+
* nothing to share it with only lowered the precision the upstream geocoder
|
|
12
|
+
* received (#51).
|
|
7
13
|
*
|
|
8
14
|
* DELIBERATELY UNVERIFIED, and that is safe. This library has no signing key
|
|
9
15
|
* and does not need one: every claim here is re-read from the application row
|
|
10
16
|
* by the API on each request, and the API's answer is the one that counts. A
|
|
11
17
|
* forged token would fail at the authorizer long before any of this mattered.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
18
|
+
* Nothing read here reaches a request at all now — it is displayed, never
|
|
19
|
+
* acted on, so a forged value misinforms only the caller who forged it.
|
|
14
20
|
*
|
|
15
21
|
* A JWT is signed, not encrypted, so the payload is plain base64url. Nothing
|
|
16
22
|
* secret is in it; these are the caller's own settings.
|
|
17
23
|
*/
|
|
18
24
|
export interface AppConfigClaims {
|
|
19
|
-
/**
|
|
20
|
-
* Bias precision this application is entitled to.
|
|
21
|
-
*
|
|
22
|
-
* Safe to act on: it only changes how a coordinate is rounded before
|
|
23
|
-
* sending, and the server re-rounds to its own configured value anyway. A
|
|
24
|
-
* stale value here costs precision, never correctness.
|
|
25
|
-
*/
|
|
26
|
-
biasDecimals?: number;
|
|
27
25
|
/**
|
|
28
26
|
* Countries this application may search, ISO 3166-1 alpha-2.
|
|
29
27
|
*
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Read advisory application config out of the access token (api#65).
|
|
3
3
|
*
|
|
4
|
-
* The API puts an application's own settings —
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* The API puts an application's own settings — today just `countries` — into
|
|
5
|
+
* the JWT alongside `allowedDomain` and `allowedResources`, so this library can
|
|
6
|
+
* stop hard-coding values it has no other way of knowing.
|
|
7
|
+
*
|
|
8
|
+
* `biasDecimals` used to be here too. It sized the grid this library rounded
|
|
9
|
+
* `BiasPosition` onto, so that nearby callers shared a server cache entry; the
|
|
10
|
+
* cache is gone, the API issues no such claim, and rounding a coordinate with
|
|
11
|
+
* nothing to share it with only lowered the precision the upstream geocoder
|
|
12
|
+
* received (#51).
|
|
7
13
|
*
|
|
8
14
|
* DELIBERATELY UNVERIFIED, and that is safe. This library has no signing key
|
|
9
15
|
* and does not need one: every claim here is re-read from the application row
|
|
10
16
|
* by the API on each request, and the API's answer is the one that counts. A
|
|
11
17
|
* forged token would fail at the authorizer long before any of this mattered.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
18
|
+
* Nothing read here reaches a request at all now — it is displayed, never
|
|
19
|
+
* acted on, so a forged value misinforms only the caller who forged it.
|
|
14
20
|
*
|
|
15
21
|
* A JWT is signed, not encrypted, so the payload is plain base64url. Nothing
|
|
16
22
|
* secret is in it; these are the caller's own settings.
|
|
@@ -36,15 +42,6 @@ export function readAppConfigClaims(token) {
|
|
|
36
42
|
const json = decodeURIComponent(Array.from(atob(padded), (c) => `%${c.charCodeAt(0).toString(16).padStart(2, '0')}`).join(''));
|
|
37
43
|
const payload = JSON.parse(json);
|
|
38
44
|
const claims = {};
|
|
39
|
-
if (typeof payload.biasDecimals === 'number') {
|
|
40
|
-
claims.biasDecimals = payload.biasDecimals;
|
|
41
|
-
}
|
|
42
|
-
else if (typeof payload.biasDecimals === 'string' &&
|
|
43
|
-
payload.biasDecimals.trim() !== '') {
|
|
44
|
-
const n = Number(payload.biasDecimals);
|
|
45
|
-
if (Number.isFinite(n))
|
|
46
|
-
claims.biasDecimals = n;
|
|
47
|
-
}
|
|
48
45
|
if (Array.isArray(payload.countries)) {
|
|
49
46
|
const list = payload.countries.filter((c) => typeof c === 'string');
|
|
50
47
|
if (list.length)
|
package/package.json
CHANGED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round coordinate arrays in command inputs so nearby callers share a server
|
|
3
|
-
* cache entry.
|
|
4
|
-
*
|
|
5
|
-
* `BiasPosition` is a hint about where to look, so collapsing it onto a grid
|
|
6
|
-
* lets two users in the same area reuse one upstream answer. `QueryPosition`
|
|
7
|
-
* is not a hint — reverse-geocode resolves an exact point a user clicked or a
|
|
8
|
-
* device reported, and rounding it returns a neighbour's address. It is left
|
|
9
|
-
* alone (RFC-0001 §2).
|
|
10
|
-
*
|
|
11
|
-
* WHY THE DEFAULT MOVED FROM 2 dp TO 3 dp
|
|
12
|
-
*
|
|
13
|
-
* 2 dp is a ~1.1 km grid, and that is not a coarser answer — it is a wrong
|
|
14
|
-
* one. Measured against Amazon Location directly, `QueryText: "cafe"` from
|
|
15
|
-
* Sydney CBD returns a completely different set of places when the bias moves
|
|
16
|
-
* 111 m:
|
|
17
|
-
*
|
|
18
|
-
* base -> Crepe de Paris | Deli Ziosa | CBD Patisserie
|
|
19
|
-
* +111 m -> Cafe Chocolat | Paradiso Cafe | Incanto Coffee
|
|
20
|
-
*
|
|
21
|
-
* So two users a kilometre apart both received whichever places were nearest
|
|
22
|
-
* the FIRST of them, with nothing in the response saying so. The server moved
|
|
23
|
-
* to 3 dp in api#21 — but this library rounds BEFORE sending, on both the
|
|
24
|
-
* browser and server paths, so the precision was already gone by the time the
|
|
25
|
-
* request arrived and that fix never reached anyone using the SDK. This is
|
|
26
|
-
* what makes it reach them.
|
|
27
|
-
*
|
|
28
|
-
* The cost is hit rate: cells shrink 100x in area. RFC-0001 §5 expected only
|
|
29
|
-
* "single-digit to low-double-digit percent" in mixed traffic, so there was
|
|
30
|
-
* little to protect, and a miss costs one upstream call while a wrong answer
|
|
31
|
-
* costs trust.
|
|
32
|
-
*/
|
|
33
|
-
/** The server's floor (api#65). A request for less is clamped up to it. */
|
|
34
|
-
export declare const DEFAULT_BIAS_DECIMALS = 3;
|
|
35
|
-
/**
|
|
36
|
-
* The band the server enforces (api#65). Mirrored here so the value this
|
|
37
|
-
* library rounds to is the value the server will actually key its cache by —
|
|
38
|
-
* rounding to something outside the band just means being wrong about what
|
|
39
|
-
* was sent.
|
|
40
|
-
*
|
|
41
|
-
* The floor is correctness: below 3 dp the nearest places are not the ones
|
|
42
|
-
* returned. The ceiling is cost: cells shrink 100x in area per decimal, so
|
|
43
|
-
* finer precision collapses the cache hit rate and every miss is a billable
|
|
44
|
-
* upstream call.
|
|
45
|
-
*/
|
|
46
|
-
export declare const MIN_BIAS_DECIMALS = 3;
|
|
47
|
-
export declare const MAX_BIAS_DECIMALS = 5;
|
|
48
|
-
/**
|
|
49
|
-
* Clamp a requested precision into the allowed band.
|
|
50
|
-
*
|
|
51
|
-
* Anything absent or malformed becomes the default rather than throwing: this
|
|
52
|
-
* runs on every request, and a surprising token claim must not break geocoding
|
|
53
|
-
* for an application that is otherwise working.
|
|
54
|
-
*/
|
|
55
|
-
export declare function clampBiasDecimals(requested?: unknown): number;
|
|
56
|
-
/**
|
|
57
|
-
* Shallow-clone the input and round position arrays that benefit from caching.
|
|
58
|
-
* Returns the original object if no position fields are present.
|
|
59
|
-
*
|
|
60
|
-
* @param input the command input
|
|
61
|
-
* @param decimals precision for this application; defaults to the floor.
|
|
62
|
-
* Comes from the `biasDecimals` JWT claim where the token
|
|
63
|
-
* carries one (api#65) — an application entitled to finer
|
|
64
|
-
* bias gets it without the caller configuring anything.
|
|
65
|
-
*/
|
|
66
|
-
export declare function roundPositionFields<T extends object>(input: T, decimals?: number): T;
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Round coordinate arrays in command inputs so nearby callers share a server
|
|
4
|
-
* cache entry.
|
|
5
|
-
*
|
|
6
|
-
* `BiasPosition` is a hint about where to look, so collapsing it onto a grid
|
|
7
|
-
* lets two users in the same area reuse one upstream answer. `QueryPosition`
|
|
8
|
-
* is not a hint — reverse-geocode resolves an exact point a user clicked or a
|
|
9
|
-
* device reported, and rounding it returns a neighbour's address. It is left
|
|
10
|
-
* alone (RFC-0001 §2).
|
|
11
|
-
*
|
|
12
|
-
* WHY THE DEFAULT MOVED FROM 2 dp TO 3 dp
|
|
13
|
-
*
|
|
14
|
-
* 2 dp is a ~1.1 km grid, and that is not a coarser answer — it is a wrong
|
|
15
|
-
* one. Measured against Amazon Location directly, `QueryText: "cafe"` from
|
|
16
|
-
* Sydney CBD returns a completely different set of places when the bias moves
|
|
17
|
-
* 111 m:
|
|
18
|
-
*
|
|
19
|
-
* base -> Crepe de Paris | Deli Ziosa | CBD Patisserie
|
|
20
|
-
* +111 m -> Cafe Chocolat | Paradiso Cafe | Incanto Coffee
|
|
21
|
-
*
|
|
22
|
-
* So two users a kilometre apart both received whichever places were nearest
|
|
23
|
-
* the FIRST of them, with nothing in the response saying so. The server moved
|
|
24
|
-
* to 3 dp in api#21 — but this library rounds BEFORE sending, on both the
|
|
25
|
-
* browser and server paths, so the precision was already gone by the time the
|
|
26
|
-
* request arrived and that fix never reached anyone using the SDK. This is
|
|
27
|
-
* what makes it reach them.
|
|
28
|
-
*
|
|
29
|
-
* The cost is hit rate: cells shrink 100x in area. RFC-0001 §5 expected only
|
|
30
|
-
* "single-digit to low-double-digit percent" in mixed traffic, so there was
|
|
31
|
-
* little to protect, and a miss costs one upstream call while a wrong answer
|
|
32
|
-
* costs trust.
|
|
33
|
-
*/
|
|
34
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
-
exports.MAX_BIAS_DECIMALS = exports.MIN_BIAS_DECIMALS = exports.DEFAULT_BIAS_DECIMALS = void 0;
|
|
36
|
-
exports.clampBiasDecimals = clampBiasDecimals;
|
|
37
|
-
exports.roundPositionFields = roundPositionFields;
|
|
38
|
-
/** The server's floor (api#65). A request for less is clamped up to it. */
|
|
39
|
-
exports.DEFAULT_BIAS_DECIMALS = 3;
|
|
40
|
-
/**
|
|
41
|
-
* The band the server enforces (api#65). Mirrored here so the value this
|
|
42
|
-
* library rounds to is the value the server will actually key its cache by —
|
|
43
|
-
* rounding to something outside the band just means being wrong about what
|
|
44
|
-
* was sent.
|
|
45
|
-
*
|
|
46
|
-
* The floor is correctness: below 3 dp the nearest places are not the ones
|
|
47
|
-
* returned. The ceiling is cost: cells shrink 100x in area per decimal, so
|
|
48
|
-
* finer precision collapses the cache hit rate and every miss is a billable
|
|
49
|
-
* upstream call.
|
|
50
|
-
*/
|
|
51
|
-
exports.MIN_BIAS_DECIMALS = 3;
|
|
52
|
-
exports.MAX_BIAS_DECIMALS = 5;
|
|
53
|
-
/**
|
|
54
|
-
* Clamp a requested precision into the allowed band.
|
|
55
|
-
*
|
|
56
|
-
* Anything absent or malformed becomes the default rather than throwing: this
|
|
57
|
-
* runs on every request, and a surprising token claim must not break geocoding
|
|
58
|
-
* for an application that is otherwise working.
|
|
59
|
-
*/
|
|
60
|
-
function clampBiasDecimals(requested) {
|
|
61
|
-
const n = Number(requested);
|
|
62
|
-
if ((typeof requested !== 'number' &&
|
|
63
|
-
!(typeof requested === 'string' && requested.trim() !== '')) ||
|
|
64
|
-
!Number.isFinite(n)) {
|
|
65
|
-
return exports.DEFAULT_BIAS_DECIMALS;
|
|
66
|
-
}
|
|
67
|
-
const floored = Math.floor(n);
|
|
68
|
-
if (floored < exports.MIN_BIAS_DECIMALS)
|
|
69
|
-
return exports.MIN_BIAS_DECIMALS;
|
|
70
|
-
if (floored > exports.MAX_BIAS_DECIMALS)
|
|
71
|
-
return exports.MAX_BIAS_DECIMALS;
|
|
72
|
-
return floored;
|
|
73
|
-
}
|
|
74
|
-
/** Known position field names and whether they should be rounded */
|
|
75
|
-
const POSITION_FIELDS = {
|
|
76
|
-
BiasPosition: true, // geocode, autocomplete, search — round for cache
|
|
77
|
-
QueryPosition: false, // reverse geocode — keep full precision
|
|
78
|
-
};
|
|
79
|
-
function roundCoord(value, decimals) {
|
|
80
|
-
const factor = 10 ** decimals;
|
|
81
|
-
return Math.round(value * factor) / factor;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Shallow-clone the input and round position arrays that benefit from caching.
|
|
85
|
-
* Returns the original object if no position fields are present.
|
|
86
|
-
*
|
|
87
|
-
* @param input the command input
|
|
88
|
-
* @param decimals precision for this application; defaults to the floor.
|
|
89
|
-
* Comes from the `biasDecimals` JWT claim where the token
|
|
90
|
-
* carries one (api#65) — an application entitled to finer
|
|
91
|
-
* bias gets it without the caller configuring anything.
|
|
92
|
-
*/
|
|
93
|
-
function roundPositionFields(input, decimals = exports.DEFAULT_BIAS_DECIMALS) {
|
|
94
|
-
if (!input || typeof input !== 'object')
|
|
95
|
-
return input;
|
|
96
|
-
const dp = clampBiasDecimals(decimals);
|
|
97
|
-
let cloned = null;
|
|
98
|
-
for (const [field, shouldRound] of Object.entries(POSITION_FIELDS)) {
|
|
99
|
-
if (!shouldRound)
|
|
100
|
-
continue;
|
|
101
|
-
const value = input[field];
|
|
102
|
-
if (!Array.isArray(value))
|
|
103
|
-
continue;
|
|
104
|
-
if (!cloned)
|
|
105
|
-
cloned = { ...input };
|
|
106
|
-
cloned[field] = value.map((v) => typeof v === 'number' && Number.isFinite(v) ? roundCoord(v, dp) : v);
|
|
107
|
-
}
|
|
108
|
-
return cloned ?? input;
|
|
109
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round coordinate arrays in command inputs so nearby callers share a server
|
|
3
|
-
* cache entry.
|
|
4
|
-
*
|
|
5
|
-
* `BiasPosition` is a hint about where to look, so collapsing it onto a grid
|
|
6
|
-
* lets two users in the same area reuse one upstream answer. `QueryPosition`
|
|
7
|
-
* is not a hint — reverse-geocode resolves an exact point a user clicked or a
|
|
8
|
-
* device reported, and rounding it returns a neighbour's address. It is left
|
|
9
|
-
* alone (RFC-0001 §2).
|
|
10
|
-
*
|
|
11
|
-
* WHY THE DEFAULT MOVED FROM 2 dp TO 3 dp
|
|
12
|
-
*
|
|
13
|
-
* 2 dp is a ~1.1 km grid, and that is not a coarser answer — it is a wrong
|
|
14
|
-
* one. Measured against Amazon Location directly, `QueryText: "cafe"` from
|
|
15
|
-
* Sydney CBD returns a completely different set of places when the bias moves
|
|
16
|
-
* 111 m:
|
|
17
|
-
*
|
|
18
|
-
* base -> Crepe de Paris | Deli Ziosa | CBD Patisserie
|
|
19
|
-
* +111 m -> Cafe Chocolat | Paradiso Cafe | Incanto Coffee
|
|
20
|
-
*
|
|
21
|
-
* So two users a kilometre apart both received whichever places were nearest
|
|
22
|
-
* the FIRST of them, with nothing in the response saying so. The server moved
|
|
23
|
-
* to 3 dp in api#21 — but this library rounds BEFORE sending, on both the
|
|
24
|
-
* browser and server paths, so the precision was already gone by the time the
|
|
25
|
-
* request arrived and that fix never reached anyone using the SDK. This is
|
|
26
|
-
* what makes it reach them.
|
|
27
|
-
*
|
|
28
|
-
* The cost is hit rate: cells shrink 100x in area. RFC-0001 §5 expected only
|
|
29
|
-
* "single-digit to low-double-digit percent" in mixed traffic, so there was
|
|
30
|
-
* little to protect, and a miss costs one upstream call while a wrong answer
|
|
31
|
-
* costs trust.
|
|
32
|
-
*/
|
|
33
|
-
/** The server's floor (api#65). A request for less is clamped up to it. */
|
|
34
|
-
export declare const DEFAULT_BIAS_DECIMALS = 3;
|
|
35
|
-
/**
|
|
36
|
-
* The band the server enforces (api#65). Mirrored here so the value this
|
|
37
|
-
* library rounds to is the value the server will actually key its cache by —
|
|
38
|
-
* rounding to something outside the band just means being wrong about what
|
|
39
|
-
* was sent.
|
|
40
|
-
*
|
|
41
|
-
* The floor is correctness: below 3 dp the nearest places are not the ones
|
|
42
|
-
* returned. The ceiling is cost: cells shrink 100x in area per decimal, so
|
|
43
|
-
* finer precision collapses the cache hit rate and every miss is a billable
|
|
44
|
-
* upstream call.
|
|
45
|
-
*/
|
|
46
|
-
export declare const MIN_BIAS_DECIMALS = 3;
|
|
47
|
-
export declare const MAX_BIAS_DECIMALS = 5;
|
|
48
|
-
/**
|
|
49
|
-
* Clamp a requested precision into the allowed band.
|
|
50
|
-
*
|
|
51
|
-
* Anything absent or malformed becomes the default rather than throwing: this
|
|
52
|
-
* runs on every request, and a surprising token claim must not break geocoding
|
|
53
|
-
* for an application that is otherwise working.
|
|
54
|
-
*/
|
|
55
|
-
export declare function clampBiasDecimals(requested?: unknown): number;
|
|
56
|
-
/**
|
|
57
|
-
* Shallow-clone the input and round position arrays that benefit from caching.
|
|
58
|
-
* Returns the original object if no position fields are present.
|
|
59
|
-
*
|
|
60
|
-
* @param input the command input
|
|
61
|
-
* @param decimals precision for this application; defaults to the floor.
|
|
62
|
-
* Comes from the `biasDecimals` JWT claim where the token
|
|
63
|
-
* carries one (api#65) — an application entitled to finer
|
|
64
|
-
* bias gets it without the caller configuring anything.
|
|
65
|
-
*/
|
|
66
|
-
export declare function roundPositionFields<T extends object>(input: T, decimals?: number): T;
|
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Round coordinate arrays in command inputs so nearby callers share a server
|
|
3
|
-
* cache entry.
|
|
4
|
-
*
|
|
5
|
-
* `BiasPosition` is a hint about where to look, so collapsing it onto a grid
|
|
6
|
-
* lets two users in the same area reuse one upstream answer. `QueryPosition`
|
|
7
|
-
* is not a hint — reverse-geocode resolves an exact point a user clicked or a
|
|
8
|
-
* device reported, and rounding it returns a neighbour's address. It is left
|
|
9
|
-
* alone (RFC-0001 §2).
|
|
10
|
-
*
|
|
11
|
-
* WHY THE DEFAULT MOVED FROM 2 dp TO 3 dp
|
|
12
|
-
*
|
|
13
|
-
* 2 dp is a ~1.1 km grid, and that is not a coarser answer — it is a wrong
|
|
14
|
-
* one. Measured against Amazon Location directly, `QueryText: "cafe"` from
|
|
15
|
-
* Sydney CBD returns a completely different set of places when the bias moves
|
|
16
|
-
* 111 m:
|
|
17
|
-
*
|
|
18
|
-
* base -> Crepe de Paris | Deli Ziosa | CBD Patisserie
|
|
19
|
-
* +111 m -> Cafe Chocolat | Paradiso Cafe | Incanto Coffee
|
|
20
|
-
*
|
|
21
|
-
* So two users a kilometre apart both received whichever places were nearest
|
|
22
|
-
* the FIRST of them, with nothing in the response saying so. The server moved
|
|
23
|
-
* to 3 dp in api#21 — but this library rounds BEFORE sending, on both the
|
|
24
|
-
* browser and server paths, so the precision was already gone by the time the
|
|
25
|
-
* request arrived and that fix never reached anyone using the SDK. This is
|
|
26
|
-
* what makes it reach them.
|
|
27
|
-
*
|
|
28
|
-
* The cost is hit rate: cells shrink 100x in area. RFC-0001 §5 expected only
|
|
29
|
-
* "single-digit to low-double-digit percent" in mixed traffic, so there was
|
|
30
|
-
* little to protect, and a miss costs one upstream call while a wrong answer
|
|
31
|
-
* costs trust.
|
|
32
|
-
*/
|
|
33
|
-
/** The server's floor (api#65). A request for less is clamped up to it. */
|
|
34
|
-
export const DEFAULT_BIAS_DECIMALS = 3;
|
|
35
|
-
/**
|
|
36
|
-
* The band the server enforces (api#65). Mirrored here so the value this
|
|
37
|
-
* library rounds to is the value the server will actually key its cache by —
|
|
38
|
-
* rounding to something outside the band just means being wrong about what
|
|
39
|
-
* was sent.
|
|
40
|
-
*
|
|
41
|
-
* The floor is correctness: below 3 dp the nearest places are not the ones
|
|
42
|
-
* returned. The ceiling is cost: cells shrink 100x in area per decimal, so
|
|
43
|
-
* finer precision collapses the cache hit rate and every miss is a billable
|
|
44
|
-
* upstream call.
|
|
45
|
-
*/
|
|
46
|
-
export const MIN_BIAS_DECIMALS = 3;
|
|
47
|
-
export const MAX_BIAS_DECIMALS = 5;
|
|
48
|
-
/**
|
|
49
|
-
* Clamp a requested precision into the allowed band.
|
|
50
|
-
*
|
|
51
|
-
* Anything absent or malformed becomes the default rather than throwing: this
|
|
52
|
-
* runs on every request, and a surprising token claim must not break geocoding
|
|
53
|
-
* for an application that is otherwise working.
|
|
54
|
-
*/
|
|
55
|
-
export function clampBiasDecimals(requested) {
|
|
56
|
-
const n = Number(requested);
|
|
57
|
-
if ((typeof requested !== 'number' &&
|
|
58
|
-
!(typeof requested === 'string' && requested.trim() !== '')) ||
|
|
59
|
-
!Number.isFinite(n)) {
|
|
60
|
-
return DEFAULT_BIAS_DECIMALS;
|
|
61
|
-
}
|
|
62
|
-
const floored = Math.floor(n);
|
|
63
|
-
if (floored < MIN_BIAS_DECIMALS)
|
|
64
|
-
return MIN_BIAS_DECIMALS;
|
|
65
|
-
if (floored > MAX_BIAS_DECIMALS)
|
|
66
|
-
return MAX_BIAS_DECIMALS;
|
|
67
|
-
return floored;
|
|
68
|
-
}
|
|
69
|
-
/** Known position field names and whether they should be rounded */
|
|
70
|
-
const POSITION_FIELDS = {
|
|
71
|
-
BiasPosition: true, // geocode, autocomplete, search — round for cache
|
|
72
|
-
QueryPosition: false, // reverse geocode — keep full precision
|
|
73
|
-
};
|
|
74
|
-
function roundCoord(value, decimals) {
|
|
75
|
-
const factor = 10 ** decimals;
|
|
76
|
-
return Math.round(value * factor) / factor;
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Shallow-clone the input and round position arrays that benefit from caching.
|
|
80
|
-
* Returns the original object if no position fields are present.
|
|
81
|
-
*
|
|
82
|
-
* @param input the command input
|
|
83
|
-
* @param decimals precision for this application; defaults to the floor.
|
|
84
|
-
* Comes from the `biasDecimals` JWT claim where the token
|
|
85
|
-
* carries one (api#65) — an application entitled to finer
|
|
86
|
-
* bias gets it without the caller configuring anything.
|
|
87
|
-
*/
|
|
88
|
-
export function roundPositionFields(input, decimals = DEFAULT_BIAS_DECIMALS) {
|
|
89
|
-
if (!input || typeof input !== 'object')
|
|
90
|
-
return input;
|
|
91
|
-
const dp = clampBiasDecimals(decimals);
|
|
92
|
-
let cloned = null;
|
|
93
|
-
for (const [field, shouldRound] of Object.entries(POSITION_FIELDS)) {
|
|
94
|
-
if (!shouldRound)
|
|
95
|
-
continue;
|
|
96
|
-
const value = input[field];
|
|
97
|
-
if (!Array.isArray(value))
|
|
98
|
-
continue;
|
|
99
|
-
if (!cloned)
|
|
100
|
-
cloned = { ...input };
|
|
101
|
-
cloned[field] = value.map((v) => typeof v === 'number' && Number.isFinite(v) ? roundCoord(v, dp) : v);
|
|
102
|
-
}
|
|
103
|
-
return cloned ?? input;
|
|
104
|
-
}
|