@metamask-previews/geolocation-controller 0.0.0-preview-22ff1f0f2
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/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/dist/GeolocationController-method-action-types.cjs +7 -0
- package/dist/GeolocationController-method-action-types.cjs.map +1 -0
- package/dist/GeolocationController-method-action-types.d.cts +30 -0
- package/dist/GeolocationController-method-action-types.d.cts.map +1 -0
- package/dist/GeolocationController-method-action-types.d.mts +30 -0
- package/dist/GeolocationController-method-action-types.d.mts.map +1 -0
- package/dist/GeolocationController-method-action-types.mjs +6 -0
- package/dist/GeolocationController-method-action-types.mjs.map +1 -0
- package/dist/GeolocationController.cjs +154 -0
- package/dist/GeolocationController.cjs.map +1 -0
- package/dist/GeolocationController.d.cts +112 -0
- package/dist/GeolocationController.d.cts.map +1 -0
- package/dist/GeolocationController.d.mts +112 -0
- package/dist/GeolocationController.d.mts.map +1 -0
- package/dist/GeolocationController.mjs +149 -0
- package/dist/GeolocationController.mjs.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs +7 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.cjs.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts +25 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.cts.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts +25 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.d.mts.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs +6 -0
- package/dist/geolocation-api-service/geolocation-api-service-method-action-types.mjs.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service.cjs +186 -0
- package/dist/geolocation-api-service/geolocation-api-service.cjs.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service.d.cts +127 -0
- package/dist/geolocation-api-service/geolocation-api-service.d.cts.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service.d.mts +127 -0
- package/dist/geolocation-api-service/geolocation-api-service.d.mts.map +1 -0
- package/dist/geolocation-api-service/geolocation-api-service.mjs +182 -0
- package/dist/geolocation-api-service/geolocation-api-service.mjs.map +1 -0
- package/dist/index.cjs +12 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.cjs +13 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +13 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +13 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +10 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +74 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release ([#8037](https://github.com/MetaMask/core/pull/8037))
|
|
13
|
+
|
|
14
|
+
[Unreleased]: https://github.com/MetaMask/core/
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# `@metamask/geolocation-controller`
|
|
2
|
+
|
|
3
|
+
Centralised geolocation controller with TTL caching and request deduplication.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/geolocation-controller`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/geolocation-controller`
|
|
12
|
+
|
|
13
|
+
## Contributing
|
|
14
|
+
|
|
15
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController-method-action-types.cjs","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":";AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { GeolocationController } from './GeolocationController';\n\n/**\n * Returns the geolocation country code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * @returns The ISO country code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * @returns The ISO country code string.\n */\nexport type GeolocationControllerRefreshGeolocationAction = {\n type: `GeolocationController:refreshGeolocation`;\n handler: GeolocationController['refreshGeolocation'];\n};\n\n/**\n * Union of all GeolocationController action types.\n */\nexport type GeolocationControllerMethodActions =\n | GeolocationControllerGetGeolocationAction\n | GeolocationControllerRefreshGeolocationAction;\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { GeolocationController } from "./GeolocationController.cjs";
|
|
6
|
+
/**
|
|
7
|
+
* Returns the geolocation country code. Delegates to the
|
|
8
|
+
* {@link GeolocationApiService} for network fetching and caching, then
|
|
9
|
+
* updates controller state with the result.
|
|
10
|
+
*
|
|
11
|
+
* @returns The ISO country code string.
|
|
12
|
+
*/
|
|
13
|
+
export type GeolocationControllerGetGeolocationAction = {
|
|
14
|
+
type: `GeolocationController:getGeolocation`;
|
|
15
|
+
handler: GeolocationController['getGeolocation'];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
|
+
*
|
|
20
|
+
* @returns The ISO country code string.
|
|
21
|
+
*/
|
|
22
|
+
export type GeolocationControllerRefreshGeolocationAction = {
|
|
23
|
+
type: `GeolocationController:refreshGeolocation`;
|
|
24
|
+
handler: GeolocationController['refreshGeolocation'];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Union of all GeolocationController action types.
|
|
28
|
+
*/
|
|
29
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
|
+
//# sourceMappingURL=GeolocationController-method-action-types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController-method-action-types.d.cts","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,oCAAgC;AAErE;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAC1C,yCAAyC,GACzC,6CAA6C,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is auto generated by `scripts/generate-method-action-types.ts`.
|
|
3
|
+
* Do not edit manually.
|
|
4
|
+
*/
|
|
5
|
+
import type { GeolocationController } from "./GeolocationController.mjs";
|
|
6
|
+
/**
|
|
7
|
+
* Returns the geolocation country code. Delegates to the
|
|
8
|
+
* {@link GeolocationApiService} for network fetching and caching, then
|
|
9
|
+
* updates controller state with the result.
|
|
10
|
+
*
|
|
11
|
+
* @returns The ISO country code string.
|
|
12
|
+
*/
|
|
13
|
+
export type GeolocationControllerGetGeolocationAction = {
|
|
14
|
+
type: `GeolocationController:getGeolocation`;
|
|
15
|
+
handler: GeolocationController['getGeolocation'];
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
19
|
+
*
|
|
20
|
+
* @returns The ISO country code string.
|
|
21
|
+
*/
|
|
22
|
+
export type GeolocationControllerRefreshGeolocationAction = {
|
|
23
|
+
type: `GeolocationController:refreshGeolocation`;
|
|
24
|
+
handler: GeolocationController['refreshGeolocation'];
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Union of all GeolocationController action types.
|
|
28
|
+
*/
|
|
29
|
+
export type GeolocationControllerMethodActions = GeolocationControllerGetGeolocationAction | GeolocationControllerRefreshGeolocationAction;
|
|
30
|
+
//# sourceMappingURL=GeolocationController-method-action-types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController-method-action-types.d.mts","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,qBAAqB,EAAE,oCAAgC;AAErE;;;;;;GAMG;AACH,MAAM,MAAM,yCAAyC,GAAG;IACtD,IAAI,EAAE,sCAAsC,CAAC;IAC7C,OAAO,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;CAClD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,6CAA6C,GAAG;IAC1D,IAAI,EAAE,0CAA0C,CAAC;IACjD,OAAO,EAAE,qBAAqB,CAAC,oBAAoB,CAAC,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,kCAAkC,GAC1C,yCAAyC,GACzC,6CAA6C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController-method-action-types.mjs","sourceRoot":"","sources":["../src/GeolocationController-method-action-types.ts"],"names":[],"mappings":"AAAA;;;GAGG","sourcesContent":["/**\n * This file is auto generated by `scripts/generate-method-action-types.ts`.\n * Do not edit manually.\n */\n\nimport type { GeolocationController } from './GeolocationController';\n\n/**\n * Returns the geolocation country code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * @returns The ISO country code string.\n */\nexport type GeolocationControllerGetGeolocationAction = {\n type: `GeolocationController:getGeolocation`;\n handler: GeolocationController['getGeolocation'];\n};\n\n/**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * @returns The ISO country code string.\n */\nexport type GeolocationControllerRefreshGeolocationAction = {\n type: `GeolocationController:refreshGeolocation`;\n handler: GeolocationController['refreshGeolocation'];\n};\n\n/**\n * Union of all GeolocationController action types.\n */\nexport type GeolocationControllerMethodActions =\n | GeolocationControllerGetGeolocationAction\n | GeolocationControllerRefreshGeolocationAction;\n"]}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _GeolocationController_instances, _GeolocationController_fetchAndUpdate;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.GeolocationController = exports.getDefaultGeolocationControllerState = exports.controllerName = void 0;
|
|
10
|
+
const base_controller_1 = require("@metamask/base-controller");
|
|
11
|
+
const geolocation_api_service_1 = require("./geolocation-api-service/geolocation-api-service.cjs");
|
|
12
|
+
/**
|
|
13
|
+
* The name of the {@link GeolocationController}, used to namespace the
|
|
14
|
+
* controller's actions and events and to namespace the controller's state data
|
|
15
|
+
* when composed with other controllers.
|
|
16
|
+
*/
|
|
17
|
+
exports.controllerName = 'GeolocationController';
|
|
18
|
+
/**
|
|
19
|
+
* The metadata for each property in {@link GeolocationControllerState}.
|
|
20
|
+
*/
|
|
21
|
+
const geolocationControllerMetadata = {
|
|
22
|
+
location: {
|
|
23
|
+
persist: false,
|
|
24
|
+
includeInDebugSnapshot: true,
|
|
25
|
+
includeInStateLogs: true,
|
|
26
|
+
usedInUi: true,
|
|
27
|
+
},
|
|
28
|
+
status: {
|
|
29
|
+
persist: false,
|
|
30
|
+
includeInDebugSnapshot: true,
|
|
31
|
+
includeInStateLogs: true,
|
|
32
|
+
usedInUi: true,
|
|
33
|
+
},
|
|
34
|
+
lastFetchedAt: {
|
|
35
|
+
persist: false,
|
|
36
|
+
includeInDebugSnapshot: true,
|
|
37
|
+
includeInStateLogs: true,
|
|
38
|
+
usedInUi: false,
|
|
39
|
+
},
|
|
40
|
+
error: {
|
|
41
|
+
persist: false,
|
|
42
|
+
includeInDebugSnapshot: true,
|
|
43
|
+
includeInStateLogs: true,
|
|
44
|
+
usedInUi: false,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Constructs the default {@link GeolocationController} state. This allows
|
|
49
|
+
* consumers to provide a partial state object when initializing the controller
|
|
50
|
+
* and also helps in constructing complete state objects for this controller in
|
|
51
|
+
* tests.
|
|
52
|
+
*
|
|
53
|
+
* @returns The default {@link GeolocationController} state.
|
|
54
|
+
*/
|
|
55
|
+
function getDefaultGeolocationControllerState() {
|
|
56
|
+
return {
|
|
57
|
+
location: geolocation_api_service_1.UNKNOWN_LOCATION,
|
|
58
|
+
status: 'idle',
|
|
59
|
+
lastFetchedAt: null,
|
|
60
|
+
error: null,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.getDefaultGeolocationControllerState = getDefaultGeolocationControllerState;
|
|
64
|
+
const MESSENGER_EXPOSED_METHODS = [
|
|
65
|
+
'getGeolocation',
|
|
66
|
+
'refreshGeolocation',
|
|
67
|
+
];
|
|
68
|
+
/**
|
|
69
|
+
* GeolocationController manages UI-facing geolocation state by delegating
|
|
70
|
+
* the actual API interaction to {@link GeolocationApiService} via the
|
|
71
|
+
* messenger.
|
|
72
|
+
*
|
|
73
|
+
* The service (registered externally as
|
|
74
|
+
* `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response
|
|
75
|
+
* validation, TTL caching, and promise deduplication. This controller focuses
|
|
76
|
+
* on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and
|
|
77
|
+
* exposes `getGeolocation` / `refreshGeolocation` as messenger actions.
|
|
78
|
+
*/
|
|
79
|
+
class GeolocationController extends base_controller_1.BaseController {
|
|
80
|
+
/**
|
|
81
|
+
* Constructs a new {@link GeolocationController}.
|
|
82
|
+
*
|
|
83
|
+
* @param args - The arguments to this controller.
|
|
84
|
+
* @param args.messenger - The messenger suited for this controller. Must
|
|
85
|
+
* have a `GeolocationApiService:fetchGeolocation` action handler registered.
|
|
86
|
+
* @param args.state - Optional partial initial state.
|
|
87
|
+
*/
|
|
88
|
+
constructor({ messenger, state }) {
|
|
89
|
+
super({
|
|
90
|
+
messenger,
|
|
91
|
+
metadata: geolocationControllerMetadata,
|
|
92
|
+
name: exports.controllerName,
|
|
93
|
+
state: { ...getDefaultGeolocationControllerState(), ...state },
|
|
94
|
+
});
|
|
95
|
+
_GeolocationController_instances.add(this);
|
|
96
|
+
this.messenger.registerMethodActionHandlers(this, MESSENGER_EXPOSED_METHODS);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns the geolocation country code. Delegates to the
|
|
100
|
+
* {@link GeolocationApiService} for network fetching and caching, then
|
|
101
|
+
* updates controller state with the result.
|
|
102
|
+
*
|
|
103
|
+
* @returns The ISO country code string.
|
|
104
|
+
*/
|
|
105
|
+
async getGeolocation() {
|
|
106
|
+
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
110
|
+
*
|
|
111
|
+
* @returns The ISO country code string.
|
|
112
|
+
*/
|
|
113
|
+
async refreshGeolocation() {
|
|
114
|
+
this.update((draft) => {
|
|
115
|
+
draft.lastFetchedAt = null;
|
|
116
|
+
});
|
|
117
|
+
return __classPrivateFieldGet(this, _GeolocationController_instances, "m", _GeolocationController_fetchAndUpdate).call(this, { bypassCache: true });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.GeolocationController = GeolocationController;
|
|
121
|
+
_GeolocationController_instances = new WeakSet(), _GeolocationController_fetchAndUpdate =
|
|
122
|
+
/**
|
|
123
|
+
* Calls the geolocation service and updates controller state with the
|
|
124
|
+
* result.
|
|
125
|
+
*
|
|
126
|
+
* @param options - Options forwarded to the service.
|
|
127
|
+
* @param options.bypassCache - When true, the service skips its TTL cache.
|
|
128
|
+
* @returns The ISO country code string.
|
|
129
|
+
*/
|
|
130
|
+
async function _GeolocationController_fetchAndUpdate(options) {
|
|
131
|
+
this.update((draft) => {
|
|
132
|
+
draft.status = 'loading';
|
|
133
|
+
draft.error = null;
|
|
134
|
+
});
|
|
135
|
+
try {
|
|
136
|
+
const location = await this.messenger.call('GeolocationApiService:fetchGeolocation', options);
|
|
137
|
+
this.update((draft) => {
|
|
138
|
+
draft.location = location;
|
|
139
|
+
draft.status = 'complete';
|
|
140
|
+
draft.lastFetchedAt = Date.now();
|
|
141
|
+
draft.error = null;
|
|
142
|
+
});
|
|
143
|
+
return location;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
147
|
+
this.update((draft) => {
|
|
148
|
+
draft.status = 'error';
|
|
149
|
+
draft.error = message;
|
|
150
|
+
});
|
|
151
|
+
return this.state.location;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
//# sourceMappingURL=GeolocationController.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController.cjs","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":";;;;;;;;;AAKA,+DAA2D;AAG3D,mGAAqF;AAKrF;;;;GAIG;AACU,QAAA,cAAc,GAAG,uBAAuB,CAAC;AAgBtD;;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vB,KAAK,CAAC,KAAK,GAAG,OAAO,CAAC;QACxB,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { Messenger } from '@metamask/messenger';\n\nimport { UNKNOWN_LOCATION } from './geolocation-api-service/geolocation-api-service';\nimport type { GeolocationApiServiceFetchGeolocationAction } from './geolocation-api-service/geolocation-api-service-method-action-types';\nimport type { GeolocationControllerMethodActions } from './GeolocationController-method-action-types';\nimport type { GeolocationRequestStatus } from './types';\n\n/**\n * The name of the {@link GeolocationController}, used to namespace the\n * controller's actions and events and to namespace the controller's state data\n * when composed with other controllers.\n */\nexport const controllerName = 'GeolocationController';\n\n/**\n * State for the {@link GeolocationController}.\n */\nexport type GeolocationControllerState = {\n /** ISO 3166-1 alpha-2 country code, or \"UNKNOWN\" if not yet determined. */\n location: string;\n /** Current status of the geolocation fetch lifecycle. */\n status: GeolocationRequestStatus;\n /** Epoch milliseconds of the last successful fetch, or null if never fetched. */\n lastFetchedAt: number | null;\n /** Last error message, or null if no error has occurred. */\n error: string | null;\n};\n\n/**\n * The metadata for each property in {@link GeolocationControllerState}.\n */\nconst geolocationControllerMetadata = {\n location: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n status: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: true,\n },\n lastFetchedAt: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n error: {\n persist: false,\n includeInDebugSnapshot: true,\n includeInStateLogs: true,\n usedInUi: false,\n },\n} satisfies StateMetadata<GeolocationControllerState>;\n\n/**\n * Constructs the default {@link GeolocationController} state. This allows\n * consumers to provide a partial state object when initializing the controller\n * and also helps in constructing complete state objects for this controller in\n * tests.\n *\n * @returns The default {@link GeolocationController} state.\n */\nexport function getDefaultGeolocationControllerState(): GeolocationControllerState {\n return {\n location: UNKNOWN_LOCATION,\n status: 'idle',\n lastFetchedAt: null,\n error: null,\n };\n}\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getGeolocation',\n 'refreshGeolocation',\n] as const;\n\n/**\n * Retrieves the state of the {@link GeolocationController}.\n */\nexport type GeolocationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Actions that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerActions =\n | GeolocationControllerGetStateAction\n | GeolocationControllerMethodActions;\n\n/**\n * Actions from other messengers that {@link GeolocationControllerMessenger} calls.\n */\ntype AllowedActions = GeolocationApiServiceFetchGeolocationAction;\n\n/**\n * Published when the state of {@link GeolocationController} changes.\n */\nexport type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n GeolocationControllerState\n>;\n\n/**\n * Events that {@link GeolocationControllerMessenger} exposes to other consumers.\n */\nexport type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;\n\n/**\n * Events from other messengers that {@link GeolocationControllerMessenger}\n * subscribes to.\n */\ntype AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events accessed by\n * {@link GeolocationController}.\n */\nexport type GeolocationControllerMessenger = Messenger<\n typeof controllerName,\n GeolocationControllerActions | AllowedActions,\n GeolocationControllerEvents | AllowedEvents\n>;\n\n/**\n * Options for constructing the {@link GeolocationController}.\n */\nexport type GeolocationControllerOptions = {\n /** The messenger for inter-controller communication. */\n messenger: GeolocationControllerMessenger;\n /** Optional partial initial state. */\n state?: Partial<GeolocationControllerState>;\n};\n\n/**\n * GeolocationController manages UI-facing geolocation state by delegating\n * the actual API interaction to {@link GeolocationApiService} via the\n * messenger.\n *\n * The service (registered externally as\n * `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response\n * validation, TTL caching, and promise deduplication. This controller focuses\n * on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and\n * exposes `getGeolocation` / `refreshGeolocation` as messenger actions.\n */\nexport class GeolocationController extends BaseController<\n typeof controllerName,\n GeolocationControllerState,\n GeolocationControllerMessenger\n> {\n /**\n * Constructs a new {@link GeolocationController}.\n *\n * @param args - The arguments to this controller.\n * @param args.messenger - The messenger suited for this controller. Must\n * have a `GeolocationApiService:fetchGeolocation` action handler registered.\n * @param args.state - Optional partial initial state.\n */\n constructor({ messenger, state }: GeolocationControllerOptions) {\n super({\n messenger,\n metadata: geolocationControllerMetadata,\n name: controllerName,\n state: { ...getDefaultGeolocationControllerState(), ...state },\n });\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n }\n\n /**\n * Returns the geolocation country code. Delegates to the\n * {@link GeolocationApiService} for network fetching and caching, then\n * updates controller state with the result.\n *\n * @returns The ISO country code string.\n */\n async getGeolocation(): Promise<string> {\n return this.#fetchAndUpdate();\n }\n\n /**\n * Forces a fresh geolocation fetch, bypassing the service's cache.\n *\n * @returns The ISO country code string.\n */\n async refreshGeolocation(): Promise<string> {\n this.update((draft) => {\n draft.lastFetchedAt = null;\n });\n return this.#fetchAndUpdate({ bypassCache: true });\n }\n\n /**\n * Calls the geolocation service and updates controller state with the\n * result.\n *\n * @param options - Options forwarded to the service.\n * @param options.bypassCache - When true, the service skips its TTL cache.\n * @returns The ISO country code string.\n */\n async #fetchAndUpdate(options?: { bypassCache?: boolean }): Promise<string> {\n this.update((draft) => {\n draft.status = 'loading';\n draft.error = null;\n });\n\n try {\n const location = await this.messenger.call(\n 'GeolocationApiService:fetchGeolocation',\n options,\n );\n\n this.update((draft) => {\n draft.location = location;\n draft.status = 'complete';\n draft.lastFetchedAt = Date.now();\n draft.error = null;\n });\n\n return location;\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n\n this.update((draft) => {\n draft.status = 'error';\n draft.error = message;\n });\n\n return this.state.location;\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import type { GeolocationApiServiceFetchGeolocationAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.cjs";
|
|
5
|
+
import type { GeolocationControllerMethodActions } from "./GeolocationController-method-action-types.cjs";
|
|
6
|
+
import type { GeolocationRequestStatus } from "./types.cjs";
|
|
7
|
+
/**
|
|
8
|
+
* The name of the {@link GeolocationController}, used to namespace the
|
|
9
|
+
* controller's actions and events and to namespace the controller's state data
|
|
10
|
+
* when composed with other controllers.
|
|
11
|
+
*/
|
|
12
|
+
export declare const controllerName = "GeolocationController";
|
|
13
|
+
/**
|
|
14
|
+
* State for the {@link GeolocationController}.
|
|
15
|
+
*/
|
|
16
|
+
export type GeolocationControllerState = {
|
|
17
|
+
/** ISO 3166-1 alpha-2 country code, or "UNKNOWN" if not yet determined. */
|
|
18
|
+
location: string;
|
|
19
|
+
/** Current status of the geolocation fetch lifecycle. */
|
|
20
|
+
status: GeolocationRequestStatus;
|
|
21
|
+
/** Epoch milliseconds of the last successful fetch, or null if never fetched. */
|
|
22
|
+
lastFetchedAt: number | null;
|
|
23
|
+
/** Last error message, or null if no error has occurred. */
|
|
24
|
+
error: string | null;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Constructs the default {@link GeolocationController} state. This allows
|
|
28
|
+
* consumers to provide a partial state object when initializing the controller
|
|
29
|
+
* and also helps in constructing complete state objects for this controller in
|
|
30
|
+
* tests.
|
|
31
|
+
*
|
|
32
|
+
* @returns The default {@link GeolocationController} state.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getDefaultGeolocationControllerState(): GeolocationControllerState;
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the state of the {@link GeolocationController}.
|
|
37
|
+
*/
|
|
38
|
+
export type GeolocationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, GeolocationControllerState>;
|
|
39
|
+
/**
|
|
40
|
+
* Actions that {@link GeolocationControllerMessenger} exposes to other consumers.
|
|
41
|
+
*/
|
|
42
|
+
export type GeolocationControllerActions = GeolocationControllerGetStateAction | GeolocationControllerMethodActions;
|
|
43
|
+
/**
|
|
44
|
+
* Actions from other messengers that {@link GeolocationControllerMessenger} calls.
|
|
45
|
+
*/
|
|
46
|
+
type AllowedActions = GeolocationApiServiceFetchGeolocationAction;
|
|
47
|
+
/**
|
|
48
|
+
* Published when the state of {@link GeolocationController} changes.
|
|
49
|
+
*/
|
|
50
|
+
export type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, GeolocationControllerState>;
|
|
51
|
+
/**
|
|
52
|
+
* Events that {@link GeolocationControllerMessenger} exposes to other consumers.
|
|
53
|
+
*/
|
|
54
|
+
export type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;
|
|
55
|
+
/**
|
|
56
|
+
* Events from other messengers that {@link GeolocationControllerMessenger}
|
|
57
|
+
* subscribes to.
|
|
58
|
+
*/
|
|
59
|
+
type AllowedEvents = never;
|
|
60
|
+
/**
|
|
61
|
+
* The messenger restricted to actions and events accessed by
|
|
62
|
+
* {@link GeolocationController}.
|
|
63
|
+
*/
|
|
64
|
+
export type GeolocationControllerMessenger = Messenger<typeof controllerName, GeolocationControllerActions | AllowedActions, GeolocationControllerEvents | AllowedEvents>;
|
|
65
|
+
/**
|
|
66
|
+
* Options for constructing the {@link GeolocationController}.
|
|
67
|
+
*/
|
|
68
|
+
export type GeolocationControllerOptions = {
|
|
69
|
+
/** The messenger for inter-controller communication. */
|
|
70
|
+
messenger: GeolocationControllerMessenger;
|
|
71
|
+
/** Optional partial initial state. */
|
|
72
|
+
state?: Partial<GeolocationControllerState>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* GeolocationController manages UI-facing geolocation state by delegating
|
|
76
|
+
* the actual API interaction to {@link GeolocationApiService} via the
|
|
77
|
+
* messenger.
|
|
78
|
+
*
|
|
79
|
+
* The service (registered externally as
|
|
80
|
+
* `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response
|
|
81
|
+
* validation, TTL caching, and promise deduplication. This controller focuses
|
|
82
|
+
* on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and
|
|
83
|
+
* exposes `getGeolocation` / `refreshGeolocation` as messenger actions.
|
|
84
|
+
*/
|
|
85
|
+
export declare class GeolocationController extends BaseController<typeof controllerName, GeolocationControllerState, GeolocationControllerMessenger> {
|
|
86
|
+
#private;
|
|
87
|
+
/**
|
|
88
|
+
* Constructs a new {@link GeolocationController}.
|
|
89
|
+
*
|
|
90
|
+
* @param args - The arguments to this controller.
|
|
91
|
+
* @param args.messenger - The messenger suited for this controller. Must
|
|
92
|
+
* have a `GeolocationApiService:fetchGeolocation` action handler registered.
|
|
93
|
+
* @param args.state - Optional partial initial state.
|
|
94
|
+
*/
|
|
95
|
+
constructor({ messenger, state }: GeolocationControllerOptions);
|
|
96
|
+
/**
|
|
97
|
+
* Returns the geolocation country code. Delegates to the
|
|
98
|
+
* {@link GeolocationApiService} for network fetching and caching, then
|
|
99
|
+
* updates controller state with the result.
|
|
100
|
+
*
|
|
101
|
+
* @returns The ISO country code string.
|
|
102
|
+
*/
|
|
103
|
+
getGeolocation(): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
|
+
*
|
|
107
|
+
* @returns The ISO country code string.
|
|
108
|
+
*/
|
|
109
|
+
refreshGeolocation(): Promise<string>;
|
|
110
|
+
}
|
|
111
|
+
export {};
|
|
112
|
+
//# sourceMappingURL=GeolocationController.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController.d.cts","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,E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M,MAAM,4BAA4B,GAAG;IACzC,wDAAwD;IACxD,SAAS,EAAE,8BAA8B,CAAC;IAC1C,sCAAsC;IACtC,KAAK,CAAC,EAAE,OAAO,CAAC,0BAA0B,CAAC,CAAC;CAC7C,CAAC;AAEF;;;;;;;;;;GAUG;AACH,qBAAa,qBAAsB,SAAQ,cAAc,CACvD,OAAO,cAAc,EACrB,0BAA0B,EAC1B,8BAA8B,CAC/B;;IACC;;;;;;;OAOG;gBACS,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,4BAA4B;IAc9D;;;;;;OAMG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAIvC;;;;OAIG;IACG,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;CA8C5C"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { ControllerGetStateAction, ControllerStateChangeEvent } from "@metamask/base-controller";
|
|
2
|
+
import { BaseController } from "@metamask/base-controller";
|
|
3
|
+
import type { Messenger } from "@metamask/messenger";
|
|
4
|
+
import type { GeolocationApiServiceFetchGeolocationAction } from "./geolocation-api-service/geolocation-api-service-method-action-types.mjs";
|
|
5
|
+
import type { GeolocationControllerMethodActions } from "./GeolocationController-method-action-types.mjs";
|
|
6
|
+
import type { GeolocationRequestStatus } from "./types.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* The name of the {@link GeolocationController}, used to namespace the
|
|
9
|
+
* controller's actions and events and to namespace the controller's state data
|
|
10
|
+
* when composed with other controllers.
|
|
11
|
+
*/
|
|
12
|
+
export declare const controllerName = "GeolocationController";
|
|
13
|
+
/**
|
|
14
|
+
* State for the {@link GeolocationController}.
|
|
15
|
+
*/
|
|
16
|
+
export type GeolocationControllerState = {
|
|
17
|
+
/** ISO 3166-1 alpha-2 country code, or "UNKNOWN" if not yet determined. */
|
|
18
|
+
location: string;
|
|
19
|
+
/** Current status of the geolocation fetch lifecycle. */
|
|
20
|
+
status: GeolocationRequestStatus;
|
|
21
|
+
/** Epoch milliseconds of the last successful fetch, or null if never fetched. */
|
|
22
|
+
lastFetchedAt: number | null;
|
|
23
|
+
/** Last error message, or null if no error has occurred. */
|
|
24
|
+
error: string | null;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Constructs the default {@link GeolocationController} state. This allows
|
|
28
|
+
* consumers to provide a partial state object when initializing the controller
|
|
29
|
+
* and also helps in constructing complete state objects for this controller in
|
|
30
|
+
* tests.
|
|
31
|
+
*
|
|
32
|
+
* @returns The default {@link GeolocationController} state.
|
|
33
|
+
*/
|
|
34
|
+
export declare function getDefaultGeolocationControllerState(): GeolocationControllerState;
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the state of the {@link GeolocationController}.
|
|
37
|
+
*/
|
|
38
|
+
export type GeolocationControllerGetStateAction = ControllerGetStateAction<typeof controllerName, GeolocationControllerState>;
|
|
39
|
+
/**
|
|
40
|
+
* Actions that {@link GeolocationControllerMessenger} exposes to other consumers.
|
|
41
|
+
*/
|
|
42
|
+
export type GeolocationControllerActions = GeolocationControllerGetStateAction | GeolocationControllerMethodActions;
|
|
43
|
+
/**
|
|
44
|
+
* Actions from other messengers that {@link GeolocationControllerMessenger} calls.
|
|
45
|
+
*/
|
|
46
|
+
type AllowedActions = GeolocationApiServiceFetchGeolocationAction;
|
|
47
|
+
/**
|
|
48
|
+
* Published when the state of {@link GeolocationController} changes.
|
|
49
|
+
*/
|
|
50
|
+
export type GeolocationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, GeolocationControllerState>;
|
|
51
|
+
/**
|
|
52
|
+
* Events that {@link GeolocationControllerMessenger} exposes to other consumers.
|
|
53
|
+
*/
|
|
54
|
+
export type GeolocationControllerEvents = GeolocationControllerStateChangeEvent;
|
|
55
|
+
/**
|
|
56
|
+
* Events from other messengers that {@link GeolocationControllerMessenger}
|
|
57
|
+
* subscribes to.
|
|
58
|
+
*/
|
|
59
|
+
type AllowedEvents = never;
|
|
60
|
+
/**
|
|
61
|
+
* The messenger restricted to actions and events accessed by
|
|
62
|
+
* {@link GeolocationController}.
|
|
63
|
+
*/
|
|
64
|
+
export type GeolocationControllerMessenger = Messenger<typeof controllerName, GeolocationControllerActions | AllowedActions, GeolocationControllerEvents | AllowedEvents>;
|
|
65
|
+
/**
|
|
66
|
+
* Options for constructing the {@link GeolocationController}.
|
|
67
|
+
*/
|
|
68
|
+
export type GeolocationControllerOptions = {
|
|
69
|
+
/** The messenger for inter-controller communication. */
|
|
70
|
+
messenger: GeolocationControllerMessenger;
|
|
71
|
+
/** Optional partial initial state. */
|
|
72
|
+
state?: Partial<GeolocationControllerState>;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* GeolocationController manages UI-facing geolocation state by delegating
|
|
76
|
+
* the actual API interaction to {@link GeolocationApiService} via the
|
|
77
|
+
* messenger.
|
|
78
|
+
*
|
|
79
|
+
* The service (registered externally as
|
|
80
|
+
* `GeolocationApiService:fetchGeolocation`) handles HTTP requests, response
|
|
81
|
+
* validation, TTL caching, and promise deduplication. This controller focuses
|
|
82
|
+
* on state lifecycle (`idle` -> `loading` -> `complete` | `error`) and
|
|
83
|
+
* exposes `getGeolocation` / `refreshGeolocation` as messenger actions.
|
|
84
|
+
*/
|
|
85
|
+
export declare class GeolocationController extends BaseController<typeof controllerName, GeolocationControllerState, GeolocationControllerMessenger> {
|
|
86
|
+
#private;
|
|
87
|
+
/**
|
|
88
|
+
* Constructs a new {@link GeolocationController}.
|
|
89
|
+
*
|
|
90
|
+
* @param args - The arguments to this controller.
|
|
91
|
+
* @param args.messenger - The messenger suited for this controller. Must
|
|
92
|
+
* have a `GeolocationApiService:fetchGeolocation` action handler registered.
|
|
93
|
+
* @param args.state - Optional partial initial state.
|
|
94
|
+
*/
|
|
95
|
+
constructor({ messenger, state }: GeolocationControllerOptions);
|
|
96
|
+
/**
|
|
97
|
+
* Returns the geolocation country code. Delegates to the
|
|
98
|
+
* {@link GeolocationApiService} for network fetching and caching, then
|
|
99
|
+
* updates controller state with the result.
|
|
100
|
+
*
|
|
101
|
+
* @returns The ISO country code string.
|
|
102
|
+
*/
|
|
103
|
+
getGeolocation(): Promise<string>;
|
|
104
|
+
/**
|
|
105
|
+
* Forces a fresh geolocation fetch, bypassing the service's cache.
|
|
106
|
+
*
|
|
107
|
+
* @returns The ISO country code string.
|
|
108
|
+
*/
|
|
109
|
+
refreshGeolocation(): Promise<string>;
|
|
110
|
+
}
|
|
111
|
+
export {};
|
|
112
|
+
//# sourceMappingURL=GeolocationController.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GeolocationController.d.mts","sourceRoot":"","sources":["../src/GeolocationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAE3B,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAGrD,OAAO,KAAK,EAAE,2CAA2C,EAAE,kFAA8E;AACzI,OAAO,KAAK,EAAE,kCAAkC,EAAE,wDAAoD;AACtG,OAAO,KAAK,EAAE,wBAAwB,EAAE,oBAAgB;AAExD;;;;GAIG;AACH,eAAO,MAAM,cAAc,0BAA0B,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACvC,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAC;IACjB,yDAAyD;IACzD,MAAM,EAAE,wBAAwB,CAAC;IACjC,iFAAiF;IACjF,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,4DAA4D;IAC5D,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtB,CAAC;AAgCF;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,IAAI,0BAA0B,CAOjF;AAOD;;GAEG;AACH,MAAM,MAAM,mCAAmC,GAAG,wBAAwB,CACxE,OAAO,cAAc,EACrB,0BAA0B,CAC3B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,4BAA4B,GACpC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