@metamask-previews/geolocation-controller 0.1.0-preview-0aca970c8

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +21 -0
  3. package/README.md +15 -0
  4. package/dist/GeolocationController-method-action-types.cjs +7 -0
  5. package/dist/GeolocationController-method-action-types.cjs.map +1 -0
  6. package/dist/GeolocationController-method-action-types.d.cts +30 -0
  7. package/dist/GeolocationController-method-action-types.d.cts.map +1 -0
  8. package/dist/GeolocationController-method-action-types.d.mts +30 -0
  9. package/dist/GeolocationController-method-action-types.d.mts.map +1 -0
  10. package/dist/GeolocationController-method-action-types.mjs +6 -0
  11. package/dist/GeolocationController-method-action-types.mjs.map +1 -0
  12. package/dist/GeolocationController.cjs +154 -0
  13. package/dist/GeolocationController.cjs.map +1 -0
  14. package/dist/GeolocationController.d.cts +112 -0
  15. package/dist/GeolocationController.d.cts.map +1 -0
  16. package/dist/GeolocationController.d.mts +112 -0
  17. package/dist/GeolocationController.d.mts.map +1 -0
  18. package/dist/GeolocationController.mjs +149 -0
  19. package/dist/GeolocationController.mjs.map +1 -0
  20. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs +7 -0
  21. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs.map +1 -0
  22. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts +25 -0
  23. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts.map +1 -0
  24. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts +25 -0
  25. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts.map +1 -0
  26. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs +6 -0
  27. package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs.map +1 -0
  28. package/dist/geolocation-api-service/geolocation-api-service.cjs +186 -0
  29. package/dist/geolocation-api-service/geolocation-api-service.cjs.map +1 -0
  30. package/dist/geolocation-api-service/geolocation-api-service.d.cts +127 -0
  31. package/dist/geolocation-api-service/geolocation-api-service.d.cts.map +1 -0
  32. package/dist/geolocation-api-service/geolocation-api-service.d.mts +127 -0
  33. package/dist/geolocation-api-service/geolocation-api-service.d.mts.map +1 -0
  34. package/dist/geolocation-api-service/geolocation-api-service.mjs +182 -0
  35. package/dist/geolocation-api-service/geolocation-api-service.mjs.map +1 -0
  36. package/dist/index.cjs +12 -0
  37. package/dist/index.cjs.map +1 -0
  38. package/dist/index.d.cts +9 -0
  39. package/dist/index.d.cts.map +1 -0
  40. package/dist/index.d.mts +9 -0
  41. package/dist/index.d.mts.map +1 -0
  42. package/dist/index.mjs +4 -0
  43. package/dist/index.mjs.map +1 -0
  44. package/dist/types.cjs +13 -0
  45. package/dist/types.cjs.map +1 -0
  46. package/dist/types.d.cts +13 -0
  47. package/dist/types.d.cts.map +1 -0
  48. package/dist/types.d.mts +13 -0
  49. package/dist/types.d.mts.map +1 -0
  50. package/dist/types.mjs +10 -0
  51. package/dist/types.mjs.map +1 -0
  52. package/package.json +74 -0
@@ -0,0 +1,127 @@
1
+ import type { CreateServicePolicyOptions, ServicePolicy } from "@metamask/controller-utils";
2
+ import type { Messenger } from "@metamask/messenger";
3
+ import type { IDisposable } from "cockatiel";
4
+ import type { GeolocationApiServiceMethodActions } from "./geolocation-api-service-method-action-types.mjs";
5
+ import { Env } from "../types.mjs";
6
+ /**
7
+ * The name of the {@link GeolocationApiService}, used to namespace the
8
+ * service's actions and events.
9
+ */
10
+ export declare const serviceName = "GeolocationApiService";
11
+ /**
12
+ * Sentinel value used when the geolocation has not been determined yet or when
13
+ * the API returns an empty / invalid response.
14
+ */
15
+ export declare const UNKNOWN_LOCATION = "UNKNOWN";
16
+ /**
17
+ * Actions that {@link GeolocationApiService} exposes to other consumers.
18
+ */
19
+ export type GeolocationApiServiceActions = GeolocationApiServiceMethodActions;
20
+ /**
21
+ * Actions from other messengers that {@link GeolocationApiServiceMessenger}
22
+ * calls.
23
+ */
24
+ type AllowedActions = never;
25
+ /**
26
+ * Events that {@link GeolocationApiService} exposes to other consumers.
27
+ */
28
+ export type GeolocationApiServiceEvents = never;
29
+ /**
30
+ * Events from other messengers that {@link GeolocationApiService} subscribes
31
+ * to.
32
+ */
33
+ type AllowedEvents = never;
34
+ /**
35
+ * The messenger restricted to actions and events accessed by
36
+ * {@link GeolocationApiService}.
37
+ */
38
+ export type GeolocationApiServiceMessenger = Messenger<typeof serviceName, GeolocationApiServiceActions | AllowedActions, GeolocationApiServiceEvents | AllowedEvents>;
39
+ /**
40
+ * Options accepted by {@link GeolocationApiService.fetchGeolocation}.
41
+ */
42
+ export type FetchGeolocationOptions = {
43
+ /** When true, the TTL cache is invalidated so the next request fetches fresh data. */
44
+ bypassCache?: boolean;
45
+ };
46
+ /**
47
+ * Low-level data service that fetches a country code from the geolocation API.
48
+ *
49
+ * Responsibilities:
50
+ * - HTTP request to the geolocation endpoint (wrapped in a service policy)
51
+ * - ISO 3166-1 alpha-2 response validation
52
+ * - TTL-based in-memory cache
53
+ * - Promise deduplication (concurrent callers share a single in-flight request)
54
+ *
55
+ * This class is intentionally not a controller: it does not manage UI state.
56
+ * Its {@link fetchGeolocation} method is automatically registered on the
57
+ * messenger so that controllers and other packages can call it directly.
58
+ */
59
+ export declare class GeolocationApiService {
60
+ #private;
61
+ /**
62
+ * The name of the service.
63
+ */
64
+ readonly name: typeof serviceName;
65
+ /**
66
+ * Constructs a new {@link GeolocationApiService}.
67
+ *
68
+ * @param args - The constructor arguments.
69
+ * @param args.messenger - The messenger suited for this service.
70
+ * @param args.env - The environment to determine the correct API endpoint.
71
+ * Defaults to PRD.
72
+ * @param args.fetch - A function that can be used to make an HTTP request.
73
+ * Defaults to the global fetch.
74
+ * @param args.ttlMs - Cache TTL in milliseconds. Defaults to 5 minutes.
75
+ * @param args.policyOptions - Options to pass to `createServicePolicy`, which
76
+ * is used to wrap each request. See {@link CreateServicePolicyOptions}.
77
+ */
78
+ constructor({ messenger, env, fetch: fetchFunction, ttlMs, policyOptions, }: {
79
+ messenger: GeolocationApiServiceMessenger;
80
+ env?: Env;
81
+ fetch?: typeof fetch;
82
+ ttlMs?: number;
83
+ policyOptions?: CreateServicePolicyOptions;
84
+ });
85
+ /**
86
+ * Registers a handler that will be called after a request returns a 5xx
87
+ * response, causing a retry.
88
+ *
89
+ * @param listener - The handler to be called.
90
+ * @returns An object that can be used to unregister the handler.
91
+ * @see {@link createServicePolicy}
92
+ */
93
+ onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable;
94
+ /**
95
+ * Registers a handler that will be called after a set number of retry rounds
96
+ * prove that requests to the API endpoint consistently return a 5xx response.
97
+ *
98
+ * @param listener - The handler to be called.
99
+ * @returns An object that can be used to unregister the handler.
100
+ * @see {@link createServicePolicy}
101
+ */
102
+ onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]): IDisposable;
103
+ /**
104
+ * Registers a handler that will be called when requests are consistently
105
+ * failing or when a successful request takes longer than the degraded
106
+ * threshold.
107
+ *
108
+ * @param listener - The handler to be called.
109
+ * @returns An object that can be used to unregister the handler.
110
+ */
111
+ onDegraded(listener: Parameters<ServicePolicy['onDegraded']>[0]): IDisposable;
112
+ /**
113
+ * Returns the geolocation country code. Serves from cache when the TTL has
114
+ * not expired, otherwise performs a network fetch. Concurrent callers are
115
+ * deduplicated to a single in-flight request.
116
+ *
117
+ * @param options - Optional fetch options.
118
+ * @param options.bypassCache - When true, invalidates the TTL cache. If a
119
+ * request is already in-flight it will be reused (deduplication always
120
+ * applies).
121
+ * @returns The ISO 3166-1 alpha-2 country code, or {@link UNKNOWN_LOCATION}
122
+ * when the API returns an empty or invalid body.
123
+ */
124
+ fetchGeolocation(options?: FetchGeolocationOptions): Promise<string>;
125
+ }
126
+ export {};
127
+ //# sourceMappingURL=geolocation-api-service.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"geolocation-api-service.d.mts","sourceRoot":"","sources":["../../src/geol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AE,0BAA0B,CAAC;KAC5C;IAcD;;;;;;;OAOG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW;IAIvE;;;;;;;OAOG;IACH,OAAO,CAAC,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,WAAW;IAIvE;;;;;;;OAOG;IACH,UAAU,CACR,QAAQ,EAAE,UAAU,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,GACnD,WAAW;IAId;;;;;;;;;;;OAWG;IACG,gBAAgB,CAAC,OAAO,CAAC,EAAE,uBAAuB,GAAG,OAAO,CAAC,MAAM,CAAC;CA+D3E"}
@@ -0,0 +1,182 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _GeolocationApiService_instances, _GeolocationApiService_messenger, _GeolocationApiService_fetch, _GeolocationApiService_url, _GeolocationApiService_ttlMs, _GeolocationApiService_policy, _GeolocationApiService_cachedLocation, _GeolocationApiService_lastFetchedAt, _GeolocationApiService_fetchPromise, _GeolocationApiService_isCacheValid, _GeolocationApiService_performFetch;
13
+ import { createServicePolicy, HttpError } from "@metamask/controller-utils";
14
+ import { Env } from "../types.mjs";
15
+ const DEFAULT_TTL_MS = 5 * 60 * 1000;
16
+ const ENDPOINT_PATH = '/geolocation';
17
+ // === GENERAL ===
18
+ /**
19
+ * The name of the {@link GeolocationApiService}, used to namespace the
20
+ * service's actions and events.
21
+ */
22
+ export const serviceName = 'GeolocationApiService';
23
+ /**
24
+ * Sentinel value used when the geolocation has not been determined yet or when
25
+ * the API returns an empty / invalid response.
26
+ */
27
+ export const UNKNOWN_LOCATION = 'UNKNOWN';
28
+ // === MESSENGER ===
29
+ const MESSENGER_EXPOSED_METHODS = ['fetchGeolocation'];
30
+ // === SERVICE DEFINITION ===
31
+ /**
32
+ * Returns the base URL for the geolocation API for the given environment.
33
+ *
34
+ * @param env - The environment to get the URL for.
35
+ * @returns The full URL for the geolocation endpoint.
36
+ */
37
+ function getGeolocationUrl(env) {
38
+ const envPrefix = env === Env.PRD ? '' : `${env}-`;
39
+ return `https://on-ramp.${envPrefix}api.cx.metamask.io${ENDPOINT_PATH}`;
40
+ }
41
+ /**
42
+ * Low-level data service that fetches a country code from the geolocation API.
43
+ *
44
+ * Responsibilities:
45
+ * - HTTP request to the geolocation endpoint (wrapped in a service policy)
46
+ * - ISO 3166-1 alpha-2 response validation
47
+ * - TTL-based in-memory cache
48
+ * - Promise deduplication (concurrent callers share a single in-flight request)
49
+ *
50
+ * This class is intentionally not a controller: it does not manage UI state.
51
+ * Its {@link fetchGeolocation} method is automatically registered on the
52
+ * messenger so that controllers and other packages can call it directly.
53
+ */
54
+ export class GeolocationApiService {
55
+ /**
56
+ * Constructs a new {@link GeolocationApiService}.
57
+ *
58
+ * @param args - The constructor arguments.
59
+ * @param args.messenger - The messenger suited for this service.
60
+ * @param args.env - The environment to determine the correct API endpoint.
61
+ * Defaults to PRD.
62
+ * @param args.fetch - A function that can be used to make an HTTP request.
63
+ * Defaults to the global fetch.
64
+ * @param args.ttlMs - Cache TTL in milliseconds. Defaults to 5 minutes.
65
+ * @param args.policyOptions - Options to pass to `createServicePolicy`, which
66
+ * is used to wrap each request. See {@link CreateServicePolicyOptions}.
67
+ */
68
+ constructor({ messenger, env = Env.PRD, fetch: fetchFunction = globalThis.fetch, ttlMs, policyOptions = {}, }) {
69
+ _GeolocationApiService_instances.add(this);
70
+ _GeolocationApiService_messenger.set(this, void 0);
71
+ _GeolocationApiService_fetch.set(this, void 0);
72
+ _GeolocationApiService_url.set(this, void 0);
73
+ _GeolocationApiService_ttlMs.set(this, void 0);
74
+ /**
75
+ * The policy that wraps each HTTP request.
76
+ *
77
+ * @see {@link createServicePolicy}
78
+ */
79
+ _GeolocationApiService_policy.set(this, void 0);
80
+ _GeolocationApiService_cachedLocation.set(this, UNKNOWN_LOCATION);
81
+ _GeolocationApiService_lastFetchedAt.set(this, null);
82
+ _GeolocationApiService_fetchPromise.set(this, null);
83
+ this.name = serviceName;
84
+ __classPrivateFieldSet(this, _GeolocationApiService_messenger, messenger, "f");
85
+ __classPrivateFieldSet(this, _GeolocationApiService_url, getGeolocationUrl(env), "f");
86
+ __classPrivateFieldSet(this, _GeolocationApiService_fetch, fetchFunction, "f");
87
+ __classPrivateFieldSet(this, _GeolocationApiService_ttlMs, ttlMs ?? DEFAULT_TTL_MS, "f");
88
+ __classPrivateFieldSet(this, _GeolocationApiService_policy, createServicePolicy(policyOptions), "f");
89
+ __classPrivateFieldGet(this, _GeolocationApiService_messenger, "f").registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
90
+ }
91
+ /**
92
+ * Registers a handler that will be called after a request returns a 5xx
93
+ * response, causing a retry.
94
+ *
95
+ * @param listener - The handler to be called.
96
+ * @returns An object that can be used to unregister the handler.
97
+ * @see {@link createServicePolicy}
98
+ */
99
+ onRetry(listener) {
100
+ return __classPrivateFieldGet(this, _GeolocationApiService_policy, "f").onRetry(listener);
101
+ }
102
+ /**
103
+ * Registers a handler that will be called after a set number of retry rounds
104
+ * prove that requests to the API endpoint consistently return a 5xx response.
105
+ *
106
+ * @param listener - The handler to be called.
107
+ * @returns An object that can be used to unregister the handler.
108
+ * @see {@link createServicePolicy}
109
+ */
110
+ onBreak(listener) {
111
+ return __classPrivateFieldGet(this, _GeolocationApiService_policy, "f").onBreak(listener);
112
+ }
113
+ /**
114
+ * Registers a handler that will be called when requests are consistently
115
+ * failing or when a successful request takes longer than the degraded
116
+ * threshold.
117
+ *
118
+ * @param listener - The handler to be called.
119
+ * @returns An object that can be used to unregister the handler.
120
+ */
121
+ onDegraded(listener) {
122
+ return __classPrivateFieldGet(this, _GeolocationApiService_policy, "f").onDegraded(listener);
123
+ }
124
+ /**
125
+ * Returns the geolocation country code. Serves from cache when the TTL has
126
+ * not expired, otherwise performs a network fetch. Concurrent callers are
127
+ * deduplicated to a single in-flight request.
128
+ *
129
+ * @param options - Optional fetch options.
130
+ * @param options.bypassCache - When true, invalidates the TTL cache. If a
131
+ * request is already in-flight it will be reused (deduplication always
132
+ * applies).
133
+ * @returns The ISO 3166-1 alpha-2 country code, or {@link UNKNOWN_LOCATION}
134
+ * when the API returns an empty or invalid body.
135
+ */
136
+ async fetchGeolocation(options) {
137
+ if (options?.bypassCache) {
138
+ __classPrivateFieldSet(this, _GeolocationApiService_lastFetchedAt, null, "f");
139
+ }
140
+ if (__classPrivateFieldGet(this, _GeolocationApiService_instances, "m", _GeolocationApiService_isCacheValid).call(this)) {
141
+ return __classPrivateFieldGet(this, _GeolocationApiService_cachedLocation, "f");
142
+ }
143
+ if (__classPrivateFieldGet(this, _GeolocationApiService_fetchPromise, "f")) {
144
+ return __classPrivateFieldGet(this, _GeolocationApiService_fetchPromise, "f");
145
+ }
146
+ const promise = __classPrivateFieldGet(this, _GeolocationApiService_instances, "m", _GeolocationApiService_performFetch).call(this);
147
+ __classPrivateFieldSet(this, _GeolocationApiService_fetchPromise, promise, "f");
148
+ try {
149
+ return await promise;
150
+ }
151
+ finally {
152
+ __classPrivateFieldSet(this, _GeolocationApiService_fetchPromise, null, "f");
153
+ }
154
+ }
155
+ }
156
+ _GeolocationApiService_messenger = new WeakMap(), _GeolocationApiService_fetch = new WeakMap(), _GeolocationApiService_url = new WeakMap(), _GeolocationApiService_ttlMs = new WeakMap(), _GeolocationApiService_policy = new WeakMap(), _GeolocationApiService_cachedLocation = new WeakMap(), _GeolocationApiService_lastFetchedAt = new WeakMap(), _GeolocationApiService_fetchPromise = new WeakMap(), _GeolocationApiService_instances = new WeakSet(), _GeolocationApiService_isCacheValid = function _GeolocationApiService_isCacheValid() {
157
+ return (__classPrivateFieldGet(this, _GeolocationApiService_lastFetchedAt, "f") !== null &&
158
+ Date.now() - __classPrivateFieldGet(this, _GeolocationApiService_lastFetchedAt, "f") < __classPrivateFieldGet(this, _GeolocationApiService_ttlMs, "f"));
159
+ }, _GeolocationApiService_performFetch =
160
+ /**
161
+ * Performs the actual HTTP fetch, wrapped in the service policy for automatic
162
+ * retry and circuit-breaking, and validates the response.
163
+ *
164
+ * @returns The ISO country code string.
165
+ */
166
+ async function _GeolocationApiService_performFetch() {
167
+ const response = await __classPrivateFieldGet(this, _GeolocationApiService_policy, "f").execute(async () => {
168
+ const localResponse = await __classPrivateFieldGet(this, _GeolocationApiService_fetch, "f").call(this, __classPrivateFieldGet(this, _GeolocationApiService_url, "f"));
169
+ if (!localResponse.ok) {
170
+ throw new HttpError(localResponse.status, `Geolocation fetch failed: ${localResponse.status}`);
171
+ }
172
+ return localResponse;
173
+ });
174
+ const raw = (await response.text()).trim();
175
+ const location = /^[A-Z]{2}$/u.test(raw) ? raw : UNKNOWN_LOCATION;
176
+ if (location !== UNKNOWN_LOCATION) {
177
+ __classPrivateFieldSet(this, _GeolocationApiService_cachedLocation, location, "f");
178
+ __classPrivateFieldSet(this, _GeolocationApiService_lastFetchedAt, Date.now(), "f");
179
+ }
180
+ return location;
181
+ };
182
+ //# sourceMappingURL=geolocation-api-service.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"geolocation-api-service.m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ACvB,CAAC;gBAAS,CAAC;YACT,uBAAA,IAAI,uCAAiB,IAAI,MAAA,CAAC;QAC5B,CAAC;IACH,CAAC;CA0CF;;IAlCG,OAAO,CACL,uBAAA,IAAI,4CAAe,KAAK,IAAI;QAC5B,IAAI,CAAC,GAAG,EAAE,GAAG,uBAAA,IAAI,4CAAe,GAAG,uBAAA,IAAI,oCAAO,CAC/C,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,KAAK;IACH,MAAM,QAAQ,GAAG,MAAM,uBAAA,IAAI,qCAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;QACrD,MAAM,aAAa,GAAG,MAAM,uBAAA,IAAI,oCAAO,MAAX,IAAI,EAAQ,uBAAA,IAAI,kCAAK,CAAC,CAAC;QACnD,IAAI,CAAC,aAAa,CAAC,EAAE,EAAE,CAAC;YACtB,MAAM,IAAI,SAAS,CACjB,aAAa,CAAC,MAAM,EACpB,6BAA6B,aAAa,CAAC,MAAM,EAAE,CACpD,CAAC;QACJ,CAAC;QACD,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,MAAM,GAAG,GAAG,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3C,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,gBAAgB,CAAC;IAElE,IAAI,QAAQ,KAAK,gBAAgB,EAAE,CAAC;QAClC,uBAAA,IAAI,yCAAmB,QAAQ,MAAA,CAAC;QAChC,uBAAA,IAAI,wCAAkB,IAAI,CAAC,GAAG,EAAE,MAAA,CAAC;IACnC,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import type {\n CreateServicePolicyOptions,\n ServicePolicy,\n} from '@metamask/controller-utils';\nimport { createServicePolicy, HttpError } from '@metamask/controller-utils';\nimport type { Messenger } from '@metamask/messenger';\nimport type { IDisposable } from 'cockatiel';\n\nimport type { GeolocationApiServiceMethodActions } from './geolocation-api-service-method-action-types';\nimport { Env } from '../types';\n\nconst DEFAULT_TTL_MS = 5 * 60 * 1000;\n\nconst ENDPOINT_PATH = '/geolocation';\n\n// === GENERAL ===\n\n/**\n * The name of the {@link GeolocationApiService}, used to namespace the\n * service's actions and events.\n */\nexport const serviceName = 'GeolocationApiService';\n\n/**\n * Sentinel value used when the geolocation has not been determined yet or when\n * the API returns an empty / invalid response.\n */\nexport const UNKNOWN_LOCATION = 'UNKNOWN';\n\n// === MESSENGER ===\n\nconst MESSENGER_EXPOSED_METHODS = ['fetchGeolocation'] as const;\n\n/**\n * Actions that {@link GeolocationApiService} exposes to other consumers.\n */\nexport type GeolocationApiServiceActions = GeolocationApiServiceMethodActions;\n\n/**\n * Actions from other messengers that {@link GeolocationApiServiceMessenger}\n * calls.\n */\ntype AllowedActions = never;\n\n/**\n * Events that {@link GeolocationApiService} exposes to other consumers.\n */\nexport type GeolocationApiServiceEvents = never;\n\n/**\n * Events from other messengers that {@link GeolocationApiService} subscribes\n * to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link GeolocationApiService}.\n */\nexport type GeolocationApiServiceMessenger = Messenger<\n typeof serviceName,\n GeolocationApiServiceActions | AllowedActions,\n GeolocationApiServiceEvents | AllowedEvents\n>;\n\n// === SERVICE DEFINITION ===\n\n/**\n * Returns the base URL for the geolocation API for the given environment.\n *\n * @param env - The environment to get the URL for.\n * @returns The full URL for the geolocation endpoint.\n */\nfunction getGeolocationUrl(env: Env): string {\n const envPrefix = env === Env.PRD ? '' : `${env}-`;\n return `https://on-ramp.${envPrefix}api.cx.metamask.io${ENDPOINT_PATH}`;\n}\n\n/**\n * Options accepted by {@link GeolocationApiService.fetchGeolocation}.\n */\nexport type FetchGeolocationOptions = {\n /** When true, the TTL cache is invalidated so the next request fetches fresh data. */\n bypassCache?: boolean;\n};\n\n/**\n * Low-level data service that fetches a country code from the geolocation API.\n *\n * Responsibilities:\n * - HTTP request to the geolocation endpoint (wrapped in a service policy)\n * - ISO 3166-1 alpha-2 response validation\n * - TTL-based in-memory cache\n * - Promise deduplication (concurrent callers share a single in-flight request)\n *\n * This class is intentionally not a controller: it does not manage UI state.\n * Its {@link fetchGeolocation} method is automatically registered on the\n * messenger so that controllers and other packages can call it directly.\n */\nexport class GeolocationApiService {\n /**\n * The name of the service.\n */\n readonly name: typeof serviceName;\n\n readonly #messenger: GeolocationApiServiceMessenger;\n\n readonly #fetch: typeof globalThis.fetch;\n\n readonly #url: string;\n\n readonly #ttlMs: number;\n\n /**\n * The policy that wraps each HTTP request.\n *\n * @see {@link createServicePolicy}\n */\n readonly #policy: ServicePolicy;\n\n #cachedLocation: string = UNKNOWN_LOCATION;\n\n #lastFetchedAt: number | null = null;\n\n #fetchPromise: Promise<string> | null = null;\n\n /**\n * Constructs a new {@link GeolocationApiService}.\n *\n * @param args - The constructor arguments.\n * @param args.messenger - The messenger suited for this service.\n * @param args.env - The environment to determine the correct API endpoint.\n * Defaults to PRD.\n * @param args.fetch - A function that can be used to make an HTTP request.\n * Defaults to the global fetch.\n * @param args.ttlMs - Cache TTL in milliseconds. Defaults to 5 minutes.\n * @param args.policyOptions - Options to pass to `createServicePolicy`, which\n * is used to wrap each request. See {@link CreateServicePolicyOptions}.\n */\n constructor({\n messenger,\n env = Env.PRD,\n fetch: fetchFunction = globalThis.fetch,\n ttlMs,\n policyOptions = {},\n }: {\n messenger: GeolocationApiServiceMessenger;\n env?: Env;\n fetch?: typeof fetch;\n ttlMs?: number;\n policyOptions?: CreateServicePolicyOptions;\n }) {\n this.name = serviceName;\n this.#messenger = messenger;\n this.#url = getGeolocationUrl(env);\n this.#fetch = fetchFunction;\n this.#ttlMs = ttlMs ?? DEFAULT_TTL_MS;\n this.#policy = createServicePolicy(policyOptions);\n\n this.#messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Registers a handler that will be called after a request returns a 5xx\n * response, causing a retry.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler.\n * @see {@link createServicePolicy}\n */\n onRetry(listener: Parameters<ServicePolicy['onRetry']>[0]): IDisposable {\n return this.#policy.onRetry(listener);\n }\n\n /**\n * Registers a handler that will be called after a set number of retry rounds\n * prove that requests to the API endpoint consistently return a 5xx response.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler.\n * @see {@link createServicePolicy}\n */\n onBreak(listener: Parameters<ServicePolicy['onBreak']>[0]): IDisposable {\n return this.#policy.onBreak(listener);\n }\n\n /**\n * Registers a handler that will be called when requests are consistently\n * failing or when a successful request takes longer than the degraded\n * threshold.\n *\n * @param listener - The handler to be called.\n * @returns An object that can be used to unregister the handler.\n */\n onDegraded(\n listener: Parameters<ServicePolicy['onDegraded']>[0],\n ): IDisposable {\n return this.#policy.onDegraded(listener);\n }\n\n /**\n * Returns the geolocation country code. Serves from cache when the TTL has\n * not expired, otherwise performs a network fetch. Concurrent callers are\n * deduplicated to a single in-flight request.\n *\n * @param options - Optional fetch options.\n * @param options.bypassCache - When true, invalidates the TTL cache. If a\n * request is already in-flight it will be reused (deduplication always\n * applies).\n * @returns The ISO 3166-1 alpha-2 country code, or {@link UNKNOWN_LOCATION}\n * when the API returns an empty or invalid body.\n */\n async fetchGeolocation(options?: FetchGeolocationOptions): Promise<string> {\n if (options?.bypassCache) {\n this.#lastFetchedAt = null;\n }\n\n if (this.#isCacheValid()) {\n return this.#cachedLocation;\n }\n\n if (this.#fetchPromise) {\n return this.#fetchPromise;\n }\n\n const promise = this.#performFetch();\n this.#fetchPromise = promise;\n\n try {\n return await promise;\n } finally {\n this.#fetchPromise = null;\n }\n }\n\n /**\n * Checks whether the cached geolocation is still within the TTL window.\n *\n * @returns True if the cache is valid.\n */\n #isCacheValid(): boolean {\n return (\n this.#lastFetchedAt !== null &&\n Date.now() - this.#lastFetchedAt < this.#ttlMs\n );\n }\n\n /**\n * Performs the actual HTTP fetch, wrapped in the service policy for automatic\n * retry and circuit-breaking, and validates the response.\n *\n * @returns The ISO country code string.\n */\n async #performFetch(): Promise<string> {\n const response = await this.#policy.execute(async () => {\n const localResponse = await this.#fetch(this.#url);\n if (!localResponse.ok) {\n throw new HttpError(\n localResponse.status,\n `Geolocation fetch failed: ${localResponse.status}`,\n );\n }\n return localResponse;\n });\n\n const raw = (await response.text()).trim();\n const location = /^[A-Z]{2}$/u.test(raw) ? raw : UNKNOWN_LOCATION;\n\n if (location !== UNKNOWN_LOCATION) {\n this.#cachedLocation = location;\n this.#lastFetchedAt = Date.now();\n }\n\n return location;\n }\n}\n"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UNKNOWN_LOCATION = exports.GeolocationApiService = exports.getDefaultGeolocationControllerState = exports.GeolocationController = exports.Env = void 0;
4
+ var types_1 = require("./types.cjs");
5
+ Object.defineProperty(exports, "Env", { enumerable: true, get: function () { return types_1.Env; } });
6
+ var GeolocationController_1 = require("./GeolocationController.cjs");
7
+ Object.defineProperty(exports, "GeolocationController", { enumerable: true, get: function () { return GeolocationController_1.GeolocationController; } });
8
+ Object.defineProperty(exports, "getDefaultGeolocationControllerState", { enumerable: true, get: function () { return GeolocationController_1.getDefaultGeolocationControllerState; } });
9
+ var geolocation_api_service_1 = require("./geolocation-api-service/geolocation-api-service.cjs");
10
+ Object.defineProperty(exports, "GeolocationApiService", { enumerable: true, get: function () { return geolocation_api_service_1.GeolocationApiService; } });
11
+ Object.defineProperty(exports, "UNKNOWN_LOCATION", { enumerable: true, get: function () { return geolocation_api_service_1.UNKNOWN_LOCATION; } });
12
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAcA,qCAA8B;AAArB,4FAAA,GAAG,OAAA;AACZ,qEAGiC;AAF/B,8HAAA,qBAAqB,OAAA;AACrB,6IAAA,oCAAoC,OAAA;AAEtC,iGAG2D;AAFzD,gIAAA,qBAAqB,OAAA;AACrB,2HAAA,gBAAgB,OAAA","sourcesContent":["export type {\n GeolocationControllerState,\n GeolocationControllerGetStateAction,\n GeolocationControllerActions,\n GeolocationControllerStateChangeEvent,\n GeolocationControllerEvents,\n GeolocationControllerMessenger,\n GeolocationControllerOptions,\n} from './GeolocationController';\nexport type {\n GeolocationControllerGetGeolocationAction,\n GeolocationControllerRefreshGeolocationAction,\n} from './GeolocationController-method-action-types';\nexport type { GeolocationRequestStatus } from './types';\nexport { Env } from './types';\nexport {\n GeolocationController,\n getDefaultGeolocationControllerState,\n} from './GeolocationController';\nexport {\n GeolocationApiService,\n UNKNOWN_LOCATION,\n} from './geolocation-api-service/geolocation-api-service';\nexport type {\n GeolocationApiServiceMessenger,\n GeolocationApiServiceActions,\n GeolocationApiServiceEvents,\n FetchGeolocationOptions,\n} from './geolocation-api-service/geolocation-api-service';\nexport type { GeolocationApiServiceFetchGeolocationAction } from './geolocation-api-service/geolocation-api-service-method-action-types';\n"]}
@@ -0,0 +1,9 @@
1
+ export type { GeolocationControllerState, GeolocationControllerGetStateAction, GeolocationControllerActions, GeolocationControllerStateChangeEvent, GeolocationControllerEvents, GeolocationControllerMessenger, GeolocationControllerOptions, } from "./GeolocationController.cjs";
2
+ export type { GeolocationControllerGetGeolocationAction, GeolocationControllerRefreshGeolocationAction, } from "./GeolocationController-method-action-types.cjs";
3
+ export type { GeolocationRequestStatus } from "./types.cjs";
4
+ export { Env } from "./types.cjs";
5
+ export { GeolocationController, getDefaultGeolocationControllerState, } from "./GeolocationController.cjs";
6
+ export { GeolocationApiService, UNKNOWN_LOCATION, } from "./geolocation-api-service/geolocation-api-service.cjs";
7
+ export type { GeolocationApiServiceMessenger, GeolocationApiServiceActions, GeolocationApiServiceEvents, FetchGeolocationOptions, } from "./geolocation-api-service/geolocation-api-service.cjs";
8
+ export type { GeolocationApiServiceFetchGeolocationAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.cjs";
9
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,4BAA4B,GAC7B,oCAAgC;AACjC,YAAY,EACV,yCAAyC,EACzC,6CAA6C,GAC9C,wDAAoD;AACrD,YAAY,EAAE,wBAAwB,EAAE,oBAAgB;AACxD,OAAO,EAAE,GAAG,EAAE,oBAAgB;AAC9B,OAAO,EACL,qBAAqB,EACrB,oCAAoC,GACrC,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,gBAAgB,GACjB,8DAA0D;AAC3D,YAAY,EACV,8BAA8B,EAC9B,4BAA4B,EAC5B,2BAA2B,EAC3B,uBAAuB,GACxB,8DAA0D;AAC3D,YAAY,EAAE,2CAA2C,EAAE,kFAA8E"}
@@ -0,0 +1,9 @@
1
+ export type { GeolocationControllerState, GeolocationControllerGetStateAction, GeolocationControllerActions, GeolocationControllerStateChangeEvent, GeolocationControllerEvents, GeolocationControllerMessenger, GeolocationControllerOptions, } from "./GeolocationController.mjs";
2
+ export type { GeolocationControllerGetGeolocationAction, GeolocationControllerRefreshGeolocationAction, } from "./GeolocationController-method-action-types.mjs";
3
+ export type { GeolocationRequestStatus } from "./types.mjs";
4
+ export { Env } from "./types.mjs";
5
+ export { GeolocationController, getDefaultGeolocationControllerState, } from "./GeolocationController.mjs";
6
+ export { GeolocationApiService, UNKNOWN_LOCATION, } from "./geolocation-api-service/geolocation-api-service.mjs";
7
+ export type { GeolocationApiServiceMessenger, GeolocationApiServiceActions, GeolocationApiServiceEvents, FetchGeolocationOptions, } from "./geolocation-api-service/geolocation-api-service.mjs";
8
+ export type { GeolocationApiServiceFetchGeolocationAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.mjs";
9
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,0BAA0B,EAC1B,mCAAmC,EACnC,4BAA4B,EAC5B,qCAAqC,EACrC,2BAA2B,EAC3B,8BAA8B,EAC9B,4BAA4B,GAC7B,oCAAgC;AACjC,YAAY,EACV,yCAAyC,EACzC,6CAA6C,GAC9C,wDAAoD;AACrD,YAAY,EAAE,wBAAwB,EAAE,oBAAgB;AACxD,OAAO,EAAE,GAAG,EAAE,oBAAgB;AAC9B,OAAO,EACL,qBAAqB,EACrB,oCAAoC,GACrC,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,gBAAgB,GACjB,8DAA0D;AAC3D,YAAY,EACV,8BAA8B,EAC9B,4BAA4B,EAC5B,2BAA2B,EAC3B,uBAAuB,GACxB,8DAA0D;AAC3D,YAAY,EAAE,2CAA2C,EAAE,kFAA8E"}
package/dist/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ export { Env } from "./types.mjs";
2
+ export { GeolocationController, getDefaultGeolocationControllerState } from "./GeolocationController.mjs";
3
+ export { GeolocationApiService, UNKNOWN_LOCATION } from "./geolocation-api-service/geolocation-api-service.mjs";
4
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,GAAG,EAAE,oBAAgB;AAC9B,OAAO,EACL,qBAAqB,EACrB,oCAAoC,EACrC,oCAAgC;AACjC,OAAO,EACL,qBAAqB,EACrB,gBAAgB,EACjB,8DAA0D","sourcesContent":["export type {\n GeolocationControllerState,\n GeolocationControllerGetStateAction,\n GeolocationControllerActions,\n GeolocationControllerStateChangeEvent,\n GeolocationControllerEvents,\n GeolocationControllerMessenger,\n GeolocationControllerOptions,\n} from './GeolocationController';\nexport type {\n GeolocationControllerGetGeolocationAction,\n GeolocationControllerRefreshGeolocationAction,\n} from './GeolocationController-method-action-types';\nexport type { GeolocationRequestStatus } from './types';\nexport { Env } from './types';\nexport {\n GeolocationController,\n getDefaultGeolocationControllerState,\n} from './GeolocationController';\nexport {\n GeolocationApiService,\n UNKNOWN_LOCATION,\n} from './geolocation-api-service/geolocation-api-service';\nexport type {\n GeolocationApiServiceMessenger,\n GeolocationApiServiceActions,\n GeolocationApiServiceEvents,\n FetchGeolocationOptions,\n} from './geolocation-api-service/geolocation-api-service';\nexport type { GeolocationApiServiceFetchGeolocationAction } from './geolocation-api-service/geolocation-api-service-method-action-types';\n"]}
package/dist/types.cjs ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Env = void 0;
4
+ /**
5
+ * Deployment environment for API endpoint selection.
6
+ */
7
+ var Env;
8
+ (function (Env) {
9
+ Env["DEV"] = "dev";
10
+ Env["UAT"] = "uat";
11
+ Env["PRD"] = "prd";
12
+ })(Env || (exports.Env = Env = {}));
13
+ //# sourceMappingURL=types.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AASA;;GAEG;AACH,IAAY,GAIX;AAJD,WAAY,GAAG;IACb,kBAAW,CAAA;IACX,kBAAW,CAAA;IACX,kBAAW,CAAA;AACb,CAAC,EAJW,GAAG,mBAAH,GAAG,QAId","sourcesContent":["/**\n * The status of a geolocation fetch operation.\n */\nexport type GeolocationRequestStatus =\n | 'idle'\n | 'loading'\n | 'complete'\n | 'error';\n\n/**\n * Deployment environment for API endpoint selection.\n */\nexport enum Env {\n DEV = 'dev',\n UAT = 'uat',\n PRD = 'prd',\n}\n"]}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The status of a geolocation fetch operation.
3
+ */
4
+ export type GeolocationRequestStatus = 'idle' | 'loading' | 'complete' | 'error';
5
+ /**
6
+ * Deployment environment for API endpoint selection.
7
+ */
8
+ export declare enum Env {
9
+ DEV = "dev",
10
+ UAT = "uat",
11
+ PRD = "prd"
12
+ }
13
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAChC,MAAM,GACN,SAAS,GACT,UAAU,GACV,OAAO,CAAC;AAEZ;;GAEG;AACH,oBAAY,GAAG;IACb,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The status of a geolocation fetch operation.
3
+ */
4
+ export type GeolocationRequestStatus = 'idle' | 'loading' | 'complete' | 'error';
5
+ /**
6
+ * Deployment environment for API endpoint selection.
7
+ */
8
+ export declare enum Env {
9
+ DEV = "dev",
10
+ UAT = "uat",
11
+ PRD = "prd"
12
+ }
13
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,wBAAwB,GAChC,MAAM,GACN,SAAS,GACT,UAAU,GACV,OAAO,CAAC;AAEZ;;GAEG;AACH,oBAAY,GAAG;IACb,GAAG,QAAQ;IACX,GAAG,QAAQ;IACX,GAAG,QAAQ;CACZ"}
package/dist/types.mjs ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Deployment environment for API endpoint selection.
3
+ */
4
+ export var Env;
5
+ (function (Env) {
6
+ Env["DEV"] = "dev";
7
+ Env["UAT"] = "uat";
8
+ Env["PRD"] = "prd";
9
+ })(Env || (Env = {}));
10
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AASA;;GAEG;AACH,MAAM,CAAN,IAAY,GAIX;AAJD,WAAY,GAAG;IACb,kBAAW,CAAA;IACX,kBAAW,CAAA;IACX,kBAAW,CAAA;AACb,CAAC,EAJW,GAAG,KAAH,GAAG,QAId","sourcesContent":["/**\n * The status of a geolocation fetch operation.\n */\nexport type GeolocationRequestStatus =\n | 'idle'\n | 'loading'\n | 'complete'\n | 'error';\n\n/**\n * Deployment environment for API endpoint selection.\n */\nexport enum Env {\n DEV = 'dev',\n UAT = 'uat',\n PRD = 'prd',\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@metamask-previews/geolocation-controller",
3
+ "version": "0.1.0-preview-0aca970c8",
4
+ "description": "Centralised geolocation controller with TTL caching and request deduplication",
5
+ "keywords": [
6
+ "MetaMask",
7
+ "Ethereum"
8
+ ],
9
+ "homepage": "https://github.com/MetaMask/core/tree/main/packages/geolocation-controller#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/MetaMask/core/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/MetaMask/core.git"
16
+ },
17
+ "license": "MIT",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "main": "./dist/index.cjs",
33
+ "types": "./dist/index.d.cts",
34
+ "files": [
35
+ "dist/"
36
+ ],
37
+ "scripts": {
38
+ "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
+ "build:all": "ts-bridge --project tsconfig.build.json --verbose --clean",
40
+ "build:docs": "typedoc",
41
+ "changelog:update": "../../scripts/update-changelog.sh @metamask/geolocation-controller",
42
+ "changelog:validate": "../../scripts/validate-changelog.sh @metamask/geolocation-controller",
43
+ "generate-method-action-types": "tsx ../../scripts/generate-method-action-types.ts",
44
+ "since-latest-release": "../../scripts/since-latest-release.sh",
45
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
46
+ "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
47
+ "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
48
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
49
+ },
50
+ "dependencies": {
51
+ "@metamask/base-controller": "^9.0.0",
52
+ "@metamask/controller-utils": "^11.19.0",
53
+ "@metamask/messenger": "^0.3.0"
54
+ },
55
+ "devDependencies": {
56
+ "@metamask/auto-changelog": "^3.4.4",
57
+ "@ts-bridge/cli": "^0.6.4",
58
+ "@types/jest": "^29.5.14",
59
+ "deepmerge": "^4.2.2",
60
+ "jest": "^29.7.0",
61
+ "ts-jest": "^29.2.5",
62
+ "tsx": "^4.20.5",
63
+ "typedoc": "^0.25.13",
64
+ "typedoc-plugin-missing-exports": "^2.0.0",
65
+ "typescript": "~5.3.3"
66
+ },
67
+ "engines": {
68
+ "node": "^18.18 || >=20"
69
+ },
70
+ "publishConfig": {
71
+ "access": "public",
72
+ "registry": "https://registry.npmjs.org/"
73
+ }
74
+ }