@esri/arcgis-rest-basemap-sessions 1.0.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.
Files changed (57) hide show
  1. package/README.md +60 -0
  2. package/dist/bundled/basemap-sessions.esm.js +362 -0
  3. package/dist/bundled/basemap-sessions.esm.js.map +1 -0
  4. package/dist/bundled/basemap-sessions.esm.min.js +12 -0
  5. package/dist/bundled/basemap-sessions.esm.min.js.map +1 -0
  6. package/dist/bundled/basemap-sessions.umd.js +376 -0
  7. package/dist/bundled/basemap-sessions.umd.js.map +1 -0
  8. package/dist/bundled/basemap-sessions.umd.min.js +12 -0
  9. package/dist/bundled/basemap-sessions.umd.min.js.map +1 -0
  10. package/dist/cjs/BaseSession.js +312 -0
  11. package/dist/cjs/BaseSession.js.map +1 -0
  12. package/dist/cjs/BasemapStyleSession.js +30 -0
  13. package/dist/cjs/BasemapStyleSession.js.map +1 -0
  14. package/dist/cjs/StaticBasemapTilesSession.js +30 -0
  15. package/dist/cjs/StaticBasemapTilesSession.js.map +1 -0
  16. package/dist/cjs/index.js +9 -0
  17. package/dist/cjs/index.js.map +1 -0
  18. package/dist/cjs/package.json +3 -0
  19. package/dist/cjs/types/StyleFamily.js +3 -0
  20. package/dist/cjs/types/StyleFamily.js.map +1 -0
  21. package/dist/cjs/utils/defaults.js +8 -0
  22. package/dist/cjs/utils/defaults.js.map +1 -0
  23. package/dist/cjs/utils/detemineSafetyMargin.js +15 -0
  24. package/dist/cjs/utils/detemineSafetyMargin.js.map +1 -0
  25. package/dist/cjs/utils/startNewSession.js +14 -0
  26. package/dist/cjs/utils/startNewSession.js.map +1 -0
  27. package/dist/cjs/utils/writable.js +3 -0
  28. package/dist/cjs/utils/writable.js.map +1 -0
  29. package/dist/esm/BaseSession.d.ts +270 -0
  30. package/dist/esm/BaseSession.js +308 -0
  31. package/dist/esm/BaseSession.js.map +1 -0
  32. package/dist/esm/BasemapStyleSession.d.ts +20 -0
  33. package/dist/esm/BasemapStyleSession.js +26 -0
  34. package/dist/esm/BasemapStyleSession.js.map +1 -0
  35. package/dist/esm/StaticBasemapTilesSession.d.ts +20 -0
  36. package/dist/esm/StaticBasemapTilesSession.js +26 -0
  37. package/dist/esm/StaticBasemapTilesSession.js.map +1 -0
  38. package/dist/esm/index.d.ts +5 -0
  39. package/dist/esm/index.js +6 -0
  40. package/dist/esm/index.js.map +1 -0
  41. package/dist/esm/package.json +3 -0
  42. package/dist/esm/types/StyleFamily.d.ts +4 -0
  43. package/dist/esm/types/StyleFamily.js +2 -0
  44. package/dist/esm/types/StyleFamily.js.map +1 -0
  45. package/dist/esm/utils/defaults.d.ts +4 -0
  46. package/dist/esm/utils/defaults.js +5 -0
  47. package/dist/esm/utils/defaults.js.map +1 -0
  48. package/dist/esm/utils/detemineSafetyMargin.d.ts +1 -0
  49. package/dist/esm/utils/detemineSafetyMargin.js +11 -0
  50. package/dist/esm/utils/detemineSafetyMargin.js.map +1 -0
  51. package/dist/esm/utils/startNewSession.d.ts +15 -0
  52. package/dist/esm/utils/startNewSession.js +10 -0
  53. package/dist/esm/utils/startNewSession.js.map +1 -0
  54. package/dist/esm/utils/writable.d.ts +8 -0
  55. package/dist/esm/utils/writable.js +2 -0
  56. package/dist/esm/utils/writable.js.map +1 -0
  57. package/package.json +75 -0
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ [![npm version][npm-img]][npm-url]
2
+ [![gzip bundle size][gzip-image]][npm-url]
3
+ [![Coverage Status][coverage-img]][coverage-url]
4
+ [![apache licensed](https://img.shields.io/badge/license-Apache-green.svg?style=flat-square)](https://raw.githubusercontent.com/Esri/arcgis-rest-js/master/LICENSE)
5
+
6
+ [npm-img]: https://img.shields.io/npm/v/@esri/arcgis-rest-basemap-sessions.svg?style=flat-square
7
+ [npm-url]: https://www.npmjs.com/package/@esri/arcgis-rest-basemap-sessions
8
+ [gzip-image]: https://img.badgesize.io/https://unpkg.com/@esri/arcgis-rest-basemap-sessions/dist/bundled/basemap-sessions.umd.min.js?compression=gzip
9
+ [coverage-img]: https://codecov.io/gh/Esri/arcgis-rest-js/branch/master/graph/badge.svg
10
+ [coverage-url]: https://codecov.io/gh/Esri/arcgis-rest-js
11
+
12
+ # @esri/arcgis-rest-basemap-sessions
13
+
14
+ > Helpers to initiate and manage a basemap session in for use with te Basemap Style Service in ArcGIS REST JS helpers.
15
+
16
+ ### Example
17
+
18
+ ```bash
19
+ @TODO
20
+ ```
21
+
22
+ ### [API Reference](https://developers.arcgis.com/arcgis-rest-js/api-reference/arcgis-rest-geocoding/)
23
+
24
+ @TODO
25
+
26
+ ### Issues
27
+
28
+ If something isn't working the way you expected, please take a look at [previously logged issues](https://github.com/Esri/arcgis-rest-js/issues) first. Have you found a new bug? Want to request a new feature? We'd [**love**](https://github.com/Esri/arcgis-rest-js/issues/new) to hear from you.
29
+
30
+ If you're looking for help you can also post issues on [GIS Stackexchange](http://gis.stackexchange.com/questions/ask?tags=esri-oss).
31
+
32
+ ### Versioning
33
+
34
+ For transparency into the release cycle and in striving to maintain backward compatibility, @esri/arcgis-rest-js is maintained under Semantic Versioning guidelines and will adhere to these rules whenever possible.
35
+
36
+ For more information on SemVer, please visit <http://semver.org/>.
37
+
38
+ ### Contributing
39
+
40
+ Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](CONTRIBUTING.md).
41
+
42
+ ### [Changelog](https://github.com/Esri/arcgis-rest-js/blob/master/CHANGELOG.md)
43
+
44
+ ### License
45
+
46
+ Copyright &copy; 2025 Esri
47
+
48
+ Licensed under the Apache License, Version 2.0 (the "License");
49
+ you may not use this file except in compliance with the License.
50
+ You may obtain a copy of the License at
51
+
52
+ > http://www.apache.org/licenses/LICENSE-2.0
53
+
54
+ Unless required by applicable law or agreed to in writing, software
55
+ distributed under the License is distributed on an "AS IS" BASIS,
56
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
57
+ See the License for the specific language governing permissions and
58
+ limitations under the License.
59
+
60
+ A copy of the license is available in the repository's [LICENSE](../../LICENSE) file.
@@ -0,0 +1,362 @@
1
+ /* @preserve
2
+ * @esri/arcgis-rest-basemap-sessions - v1.0.0 - Apache-2.0
3
+ * Copyright (c) 2017-2025 Esri, Inc.
4
+ * Wed Jul 30 2025 12:24:38 GMT-0700 (Pacific Daylight Time)
5
+ */
6
+ import { request } from '@esri/arcgis-rest-request';
7
+
8
+ function mitt(n){return {all:n=n||new Map,on:function(t,e){var i=n.get(t);i?i.push(e):n.set(t,[e]);},off:function(t,e){var i=n.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):n.set(t,[]));},emit:function(t,e){var i=n.get(t);i&&i.slice().map(function(n){n(e);}),(i=n.get("*"))&&i.slice().map(function(n){n(t,e);});}}}
9
+
10
+ const DEFAULT_START_BASEMAP_STYLE_SESSION_URL = "https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/sessions/start";
11
+ const DEFAULT_SAFETY_MARGIN = 5 * 60; // Default to 5 minutes in seconds
12
+ const DEFAULT_CHECK_EXPIRATION_INTERVAL = 10; // Default to 10 seconds
13
+ const DEFAULT_DURATION = 12 * 60 * 60; // Default to 12 hours in seconds
14
+
15
+ function startNewSession({ startSessionUrl, authentication, styleFamily = "arcgis", duration = DEFAULT_DURATION }) {
16
+ return request(startSessionUrl, {
17
+ httpMethod: "GET",
18
+ authentication: authentication,
19
+ params: { styleFamily, durationSeconds: duration }
20
+ });
21
+ }
22
+
23
+ function determineSafetyMargin(duration, safetyMargin) {
24
+ if (safetyMargin) {
25
+ return safetyMargin;
26
+ }
27
+ // common cases are
28
+ // duration is 60 seconds, this will return a 1 second safety margin
29
+ // duration is 43200 seconds, this will return a 300 second (5 minutes) safety margin
30
+ return Math.min(Math.max(duration / 100, 1), DEFAULT_SAFETY_MARGIN);
31
+ }
32
+
33
+ /**
34
+ * The base class for all basemap sessions. This class implements the {@linkcode IAuthenticationManager} interface and provides methods to start, refresh, and check the expiration of a session.
35
+ * This is not intendet to be used directly, but instead is extended by other classes such as {@linkcode BasemapStyleSession} and {@linkcode StaticBasemapTilesSession}.
36
+ *
37
+ * @abstract
38
+ * @implements {IAuthenticationManager}
39
+ */
40
+ class BaseSession {
41
+ /**
42
+ * Creates a new instance of the BaseSession class. Generally you should not create an instance of this class directly, but instead use the static methods to start a session or deserialize a session.
43
+ *
44
+ * You may need to create an instance of this class directly if you are not using the built in deserialize method.
45
+ *
46
+ * @param params - The parameters for the session.
47
+ * @param params.startSessionUrl - The URL to start the session.
48
+ * @param params.token - The token for the session.
49
+ * @param params.styleFamily - The style family of the session.
50
+ * @param params.authentication - The authentication manager or token used for the session.
51
+ * @param params.expires - The expiration date of the session.
52
+ * @param params.startTime - The start time of the session.
53
+ * @param params.endTime - The end time of the session.
54
+ * @param params.safetyMargin - The safety margin in milliseconds.
55
+ * @param params.duration - Indicates if this is a test session.
56
+ */
57
+ constructor(params) {
58
+ /**
59
+ * The ID of the timer used to check the expiration time of the session.
60
+ */
61
+ this.expirationTimerId = null;
62
+ /**
63
+ * A handler that is used to automatically refresh the session when it expires.
64
+ */
65
+ this.autoRefreshHandler = null;
66
+ this.startSessionUrl = params.startSessionUrl;
67
+ this.token = params.token;
68
+ this.styleFamily = params.styleFamily || "arcgis";
69
+ this.authentication = params.authentication;
70
+ this.duration = params.duration || DEFAULT_DURATION;
71
+ this.startTime = params.startTime;
72
+ this.endTime = params.endTime;
73
+ this.expires = params.expires;
74
+ this.safetyMargin = params.safetyMargin;
75
+ this.expirationCheckInterval =
76
+ Math.min(this.duration / 100, DEFAULT_CHECK_EXPIRATION_INTERVAL) * 1000;
77
+ this.emitter = mitt();
78
+ }
79
+ /**
80
+ * Checks if the session is expired. If it is expired, it emits an "expired" event. The event will fire **before** the method returns true.
81
+ *
82
+ * @returns {boolean} - Returns true if the session is expired, otherwise false.
83
+ */
84
+ isSessionExpired() {
85
+ if (this.isExpired) {
86
+ this.emitter.emit("expired", {
87
+ token: this.token,
88
+ startTime: this.startTime,
89
+ endTime: this.endTime,
90
+ expires: this.expires
91
+ });
92
+ }
93
+ return this.isExpired;
94
+ }
95
+ /**
96
+ * Starts checking the expiration time of the session. This will check the expiration time immediately and then on an interval.
97
+ * If the session is expired, it will emit an "expired" event.
98
+ */
99
+ startCheckingExpirationTime() {
100
+ const check = () => {
101
+ this.isSessionExpired();
102
+ };
103
+ if (!this.expirationTimerId) {
104
+ this.expirationTimerId = setInterval(check,
105
+ // check every 10 seconds or 1/100th of the duration, whichever is smaller
106
+ this.expirationCheckInterval); // check immediatly then on an interval
107
+ }
108
+ setTimeout(() => {
109
+ check(); // check immediately after starting the interval
110
+ }, 10);
111
+ return this.expirationTimerId; // return the timer ID so it can be stopped later
112
+ }
113
+ /**
114
+ * Stops checking the expiration time of the session. This will clear the interval that was set by {@linkcode BaseSession.startCheckingExpirationTime}.
115
+ */
116
+ stopCheckingExpirationTime() {
117
+ if (this.expirationTimerId) {
118
+ clearInterval(this.expirationTimerId);
119
+ this.expirationTimerId = null;
120
+ }
121
+ }
122
+ /**
123
+ * Indicates if the session is currently checking for expiration time.
124
+ *
125
+ * @returns {boolean} - Returns true if the session is checking for expiration time, otherwise false.
126
+ */
127
+ get checkingExpirationTime() {
128
+ return !!this.expirationTimerId;
129
+ }
130
+ /**
131
+ * Starts a new session using the provided parameters and returns an instance of the session class.
132
+ *
133
+ * @param params - The parameters for starting the session.
134
+ * @param SessionClass - The class to use for the session.
135
+ * @returns A promise that resolves to an instance of the session class.
136
+ */
137
+ static async startSession({ startSessionUrl, styleFamily = "arcgis", authentication, safetyMargin, duration = DEFAULT_DURATION, autoRefresh = true }, SessionClass) {
138
+ const sessionResponse = await startNewSession({
139
+ startSessionUrl,
140
+ styleFamily,
141
+ authentication,
142
+ duration
143
+ });
144
+ const actualSafetyMargin = determineSafetyMargin(duration, safetyMargin);
145
+ const session = new SessionClass({
146
+ startSessionUrl: startSessionUrl,
147
+ token: sessionResponse.sessionToken,
148
+ styleFamily,
149
+ authentication,
150
+ safetyMargin: actualSafetyMargin,
151
+ expires: new Date(sessionResponse.endTime - actualSafetyMargin * 1000),
152
+ startTime: new Date(sessionResponse.startTime),
153
+ endTime: new Date(sessionResponse.endTime),
154
+ duration
155
+ });
156
+ session.startCheckingExpirationTime();
157
+ if (autoRefresh) {
158
+ session.startAutoRefresh();
159
+ }
160
+ return session;
161
+ }
162
+ /**
163
+ * Checks if the session is expired.
164
+ *
165
+ */
166
+ get isExpired() {
167
+ return this.expires < new Date();
168
+ }
169
+ /**
170
+ * Gets the session token. If the session is expired, it will refresh the credentials and return the new token.
171
+ *
172
+ * @returns A promise that resolves to the session token.
173
+ */
174
+ getToken() {
175
+ if (this.isExpired) {
176
+ return this.refreshCredentials().then(() => this.token);
177
+ }
178
+ return Promise.resolve(this.token);
179
+ }
180
+ /**
181
+ * Indicates if the session can be refreshed. This is always true for this class.
182
+ *
183
+ * @returns {boolean} - Always returns true.
184
+ */
185
+ get canRefresh() {
186
+ return true;
187
+ }
188
+ /**
189
+ * Indicates if the session is set to automatically refresh when it expires.
190
+ *
191
+ * @returns {boolean} - Returns true if auto-refresh is enabled, otherwise false.
192
+ */
193
+ get autoRefresh() {
194
+ return !!this.autoRefreshHandler && !!this.expirationTimerId;
195
+ }
196
+ /**
197
+ * Refreshes the session credentials by starting a new session.
198
+ * This will emit a "refreshed" event with the previous and current session details.
199
+ *
200
+ * @returns A promise that resolves to the current instance of the session.
201
+ */
202
+ async refreshCredentials() {
203
+ // @TODO switch this to structured clone when we upgrade to Node 20+ types so we don't have to parse the dates later
204
+ const previous = JSON.parse(JSON.stringify({
205
+ token: this.token,
206
+ startTime: this.startTime,
207
+ endTime: this.endTime,
208
+ expires: this.expires
209
+ }));
210
+ try {
211
+ const newSession = await startNewSession({
212
+ startSessionUrl: this.startSessionUrl,
213
+ styleFamily: this.styleFamily,
214
+ authentication: this.authentication,
215
+ duration: this.duration
216
+ });
217
+ this.setToken(newSession.sessionToken);
218
+ this.setStartTime(new Date(newSession.startTime));
219
+ this.setEndTime(new Date(newSession.endTime));
220
+ this.setExpires(new Date(newSession.endTime - this.safetyMargin * 1000));
221
+ this.emitter.emit("refreshed", {
222
+ previous: {
223
+ token: previous.token,
224
+ startTime: new Date(previous.startTime),
225
+ endTime: new Date(previous.endTime),
226
+ expires: new Date(previous.expires)
227
+ },
228
+ current: {
229
+ token: this.token,
230
+ startTime: this.startTime,
231
+ endTime: this.endTime,
232
+ expires: this.expires
233
+ }
234
+ });
235
+ }
236
+ catch (error) {
237
+ this.emitter.emit("error", error);
238
+ throw error;
239
+ }
240
+ return this;
241
+ }
242
+ /**
243
+ * Enables auto-refresh for the session. This will automatically refresh the session when it expires.
244
+ * It will also start checking the expiration time of the session if it is not already started via {@linkcode BaseSession.startCheckingExpirationTime}.
245
+ */
246
+ startAutoRefresh() {
247
+ if (!this.expirationTimerId) {
248
+ this.startCheckingExpirationTime();
249
+ }
250
+ this.autoRefreshHandler = () => {
251
+ this.refreshCredentials().catch((error) => {
252
+ this.emitter.emit("error", error);
253
+ });
254
+ };
255
+ this.on("expired", this.autoRefreshHandler);
256
+ }
257
+ /**
258
+ * Disables auto-refresh for the session. This will stop automatically refreshing the session when it expires.
259
+ * This will **not** stop checking the expiration time of the session. If you want to stop automated expiration
260
+ * checking, call {@linkcode BaseSession.stopCheckingExpirationTime} after calling this method.
261
+ */
262
+ stopAutoRefresh() {
263
+ if (this.autoRefreshHandler) {
264
+ this.off("expired", this.autoRefreshHandler);
265
+ this.autoRefreshHandler = null;
266
+ }
267
+ }
268
+ on(eventName, handler) {
269
+ this.emitter.on(eventName, handler);
270
+ this.isSessionExpired(); // check if the session is expired immediately after adding the handler
271
+ }
272
+ once(eventName, handler) {
273
+ const fn = (e) => {
274
+ this.emitter.off(eventName, fn);
275
+ handler(e);
276
+ };
277
+ this.emitter.on(eventName, fn);
278
+ }
279
+ off(eventName, handler) {
280
+ this.emitter.off(eventName, handler);
281
+ }
282
+ /**
283
+ * These private methods are used to set the internal state of the session.
284
+ */
285
+ setToken(token) {
286
+ this.token = token;
287
+ }
288
+ setStartTime(startTime) {
289
+ this.startTime = startTime;
290
+ }
291
+ setEndTime(endTime) {
292
+ this.endTime = endTime;
293
+ }
294
+ setExpires(expires) {
295
+ this.expires = expires;
296
+ }
297
+ }
298
+ // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.
299
+ /* istanbul ignore next -- @preserve */
300
+ /**
301
+ * Event handler for when an error occurs during session management.
302
+ */
303
+ BaseSession.error = function error(e) { }; // eslint-disable-line @typescript-eslint/no-empty-function
304
+ // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.
305
+ /* istanbul ignore next -- @preserve */
306
+ /**
307
+ * Event handler for when a session expires and the `token` it no longer valid.
308
+ *
309
+ * @event expired
310
+ * @param e - The parameters for the expired event.
311
+ * @param e.token - The session token that expired.
312
+ * @param e.startTime - The start time of the session.
313
+ * @param e.endTime - The end time of the session.
314
+ * @param e.expires - The expiration time of the session.
315
+ */
316
+ BaseSession.expired = function expired(e) { }; // eslint-disable-line @typescript-eslint/no-empty-function
317
+ // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.
318
+ /* istanbul ignore next -- @preserve */
319
+ /**
320
+ * Event handler for when a session refreshes and a new `token` is available.
321
+ *
322
+ * @event refreshed
323
+ * @param e. - The parameters for the refreshed event.
324
+ * @param e.previous - The previous session details.
325
+ * @param e.previous.token - The previous session token.
326
+ * @param e.previous.startTime - The start time of the previous session.
327
+ * @param e.previous.endTime - The end time of the previous session.
328
+ * @param e.previous.expires - The expiration time of the previous session.
329
+ * @param e.current - The current session details.
330
+ * @param e.current.token - The current session token.
331
+ * @param e.current.startTime - The start time of the current token.
332
+ * @param e.current.endTime - The end time of the current session.
333
+ * @param e.current.expires - The expiration time of the current token.
334
+ */
335
+ BaseSession.refreshed = function refreshed(e) { }; // eslint-disable-line @typescript-eslint/no-empty-function
336
+
337
+ /**
338
+ * `BasemapStyleSession` is a class that extends {@linkcode BaseSession} to manage sessions
339
+ * for basemap styles. It provides methods to {@linkcode BasemapStyleSession.start} a new session
340
+ * which should be used instead of constructing a new instance directly.
341
+ *
342
+ * @class BasemapStyleSession
343
+ * @extends BaseSession
344
+ */
345
+ class BasemapStyleSession extends BaseSession {
346
+ /**
347
+ * Creates an instance of `BasemapStyleSession`. Constructing `BasemapStyleSession` directly is discouraged.
348
+ * Instead, use the static method {@linkcode BasemapStyleSession.start} to start a new session.
349
+ */
350
+ constructor(params) {
351
+ super(params);
352
+ }
353
+ /**
354
+ * Starts a new basemap style session.
355
+ */
356
+ static async start(params) {
357
+ return BaseSession.startSession(Object.assign(Object.assign({}, params), { startSessionUrl: (params === null || params === void 0 ? void 0 : params.startSessionUrl) || DEFAULT_START_BASEMAP_STYLE_SESSION_URL }), BasemapStyleSession);
358
+ }
359
+ }
360
+
361
+ export { BaseSession, BasemapStyleSession, DEFAULT_CHECK_EXPIRATION_INTERVAL, DEFAULT_DURATION, DEFAULT_SAFETY_MARGIN, DEFAULT_START_BASEMAP_STYLE_SESSION_URL, startNewSession };
362
+ //# sourceMappingURL=basemap-sessions.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basemap-sessions.esm.js","sources":["../../node_modules/mitt/dist/mitt.mjs","../../src/utils/defaults.ts","../../src/utils/startNewSession.ts","../../src/utils/detemineSafetyMargin.ts","../../src/BaseSession.ts","../../src/BasemapStyleSession.ts"],"sourcesContent":["export default function(n){return{all:n=n||new Map,on:function(t,e){var i=n.get(t);i?i.push(e):n.set(t,[e])},off:function(t,e){var i=n.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):n.set(t,[]))},emit:function(t,e){var i=n.get(t);i&&i.slice().map(function(n){n(e)}),(i=n.get(\"*\"))&&i.slice().map(function(n){n(t,e)})}}}\n//# sourceMappingURL=mitt.mjs.map\n","export const DEFAULT_START_BASEMAP_STYLE_SESSION_URL =\n \"https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/sessions/start\";\n\nexport const DEFAULT_SAFETY_MARGIN = 5 * 60; // Default to 5 minutes in seconds\n\nexport const DEFAULT_CHECK_EXPIRATION_INTERVAL = 10; // Default to 10 seconds\n\nexport const DEFAULT_DURATION = 12 * 60 * 60; // Default to 12 hours in seconds\n","import { IAuthenticationManager, request } from \"@esri/arcgis-rest-request\";\nimport { StyleFamily } from \"../types/StyleFamily.js\";\nimport { DEFAULT_DURATION } from \"./defaults.js\";\n\nexport interface IRequestNewSessionParams {\n startSessionUrl: string;\n authentication: IAuthenticationManager | string;\n styleFamily?: StyleFamily;\n duration?: number;\n}\n\nexport interface IStartSessionResponse {\n sessionToken: string;\n endTime: number;\n startTime: number;\n styleFamily: StyleFamily;\n}\n\nexport function startNewSession({\n startSessionUrl,\n authentication,\n styleFamily = \"arcgis\",\n duration = DEFAULT_DURATION\n}: IRequestNewSessionParams): Promise<IStartSessionResponse> {\n return request(startSessionUrl, {\n httpMethod: \"GET\",\n authentication: authentication,\n params: { styleFamily, durationSeconds: duration }\n });\n}\n","import { DEFAULT_SAFETY_MARGIN } from \"./defaults.js\";\n\nexport function determineSafetyMargin(\n duration: number | undefined,\n safetyMargin: number | undefined\n): number {\n if (safetyMargin) {\n return safetyMargin;\n }\n // common cases are\n // duration is 60 seconds, this will return a 1 second safety margin\n // duration is 43200 seconds, this will return a 300 second (5 minutes) safety margin\n return Math.min(Math.max(duration / 100, 1), DEFAULT_SAFETY_MARGIN);\n}\n","import mitt from \"mitt\";\n\nimport { IAuthenticationManager } from \"@esri/arcgis-rest-request\";\nimport { StyleFamily } from \"./types/StyleFamily.js\";\nimport { startNewSession } from \"./utils/startNewSession.js\";\nimport { Writable } from \"./utils/writable.js\";\nimport { determineSafetyMargin } from \"./utils/detemineSafetyMargin.js\";\nimport {\n DEFAULT_DURATION,\n DEFAULT_SAFETY_MARGIN,\n DEFAULT_CHECK_EXPIRATION_INTERVAL\n} from \"./utils/defaults.js\";\n\nexport interface IBasemapSessionParams {\n token: string;\n startSessionUrl: string;\n styleFamily: StyleFamily;\n authentication: IAuthenticationManager | string;\n expires: Date;\n startTime: Date;\n endTime: Date;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n}\n\nexport interface IStartSessionParams {\n styleFamily?: StyleFamily;\n authentication: IAuthenticationManager | string;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n\n /**\n * The URL to start the session. If not provided, it will use the default URL.\n * @private\n */\n startSessionUrl?: string;\n}\n\n/**\n * The base class for all basemap sessions. This class implements the {@linkcode IAuthenticationManager} interface and provides methods to start, refresh, and check the expiration of a session.\n * This is not intendet to be used directly, but instead is extended by other classes such as {@linkcode BasemapStyleSession} and {@linkcode StaticBasemapTilesSession}.\n *\n * @abstract\n * @implements {IAuthenticationManager}\n */\nexport abstract class BaseSession implements IAuthenticationManager {\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when an error occurs during session management.\n */\n static readonly error = function error(e: Error): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when a session expires and the `token` it no longer valid.\n *\n * @event expired\n * @param e - The parameters for the expired event.\n * @param e.token - The session token that expired.\n * @param e.startTime - The start time of the session.\n * @param e.endTime - The end time of the session.\n * @param e.expires - The expiration time of the session.\n */\n static readonly expired = function expired(e: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n }): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when a session refreshes and a new `token` is available.\n *\n * @event refreshed\n * @param e. - The parameters for the refreshed event.\n * @param e.previous - The previous session details.\n * @param e.previous.token - The previous session token.\n * @param e.previous.startTime - The start time of the previous session.\n * @param e.previous.endTime - The end time of the previous session.\n * @param e.previous.expires - The expiration time of the previous session.\n * @param e.current - The current session details.\n * @param e.current.token - The current session token.\n * @param e.current.startTime - The start time of the current token.\n * @param e.current.endTime - The end time of the current session.\n * @param e.current.expires - The expiration time of the current token.\n */\n static readonly refreshed = function refreshed(e: {\n previous: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n };\n current: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n };\n }): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n /**\n * The portal URL that the session is associated with. This generally is not used but exists to implement the `IAuthenticationManager` interface.\n */\n readonly portal: string;\n\n /**\n * The style family of the session. This is used to determine the type of basemap styles that are available.\n */\n readonly styleFamily: StyleFamily;\n\n /**\n * The authentication manager or token used for the session.\n * This can be an instance of {@linkcode ApiKeyManager}, {@linkcode ArcGISIdentityManager}, {@linkcode ApplicationCredentialsManager} or a string token.\n */\n readonly authentication: IAuthenticationManager | string;\n\n /**\n * The expiration date of the session. This is the {@linkcode BaseSession.endTime} minus the {@linkcode BaseSession.safetyMargin}. This is used internally to determine if the session is expired.\n */\n readonly expires: Date;\n\n /**\n * The start time of the session. This is the time returned from the API when the session war started.\n */\n readonly startTime: Date;\n\n /**\n * The end time of the session. This is the time returned from the API when the session will end.\n */\n readonly endTime: Date;\n\n /**\n * The token for the session.\n */\n readonly token: string;\n\n /**\n * The URL used to start the session.\n */\n readonly startSessionUrl: string;\n\n /**\n * The safety margin in milliseconds. This subtracted from the {@linkcode BaseSession.endTime} to get the {@linkcode BaseSession.expiration}.\n */\n readonly safetyMargin: number;\n\n /**\n * The duration of the session in seconds. This is used to determine how long the session will last when the session is refreshed.\n */\n readonly duration: number;\n\n /**\n * The interval at which to check the expiration time of the session. This is always 10 seconds or 1/100th of the duration, whichever is smaller.\n */\n readonly expirationCheckInterval: number;\n\n /**\n * The ID of the timer used to check the expiration time of the session.\n */\n private expirationTimerId: any = null;\n\n /**\n * Internal instance of [`mitt`](https://github.com/developit/mitt) used for event handlers. It is recommended to use {@linkcode BasemapSession.on}, {@linkcode BasemapSession.off} or {@linkcode BasemapSession.once} instead of `emitter.`\n */\n private emitter: any;\n\n /**\n * A handler that is used to automatically refresh the session when it expires.\n */\n private autoRefreshHandler: (() => void) | null = null;\n\n /**\n * Creates a new instance of the BaseSession class. Generally you should not create an instance of this class directly, but instead use the static methods to start a session or deserialize a session.\n *\n * You may need to create an instance of this class directly if you are not using the built in deserialize method.\n *\n * @param params - The parameters for the session.\n * @param params.startSessionUrl - The URL to start the session.\n * @param params.token - The token for the session.\n * @param params.styleFamily - The style family of the session.\n * @param params.authentication - The authentication manager or token used for the session.\n * @param params.expires - The expiration date of the session.\n * @param params.startTime - The start time of the session.\n * @param params.endTime - The end time of the session.\n * @param params.safetyMargin - The safety margin in milliseconds.\n * @param params.duration - Indicates if this is a test session.\n */\n constructor(params: IBasemapSessionParams) {\n this.startSessionUrl = params.startSessionUrl;\n this.token = params.token;\n this.styleFamily = params.styleFamily || \"arcgis\";\n this.authentication = params.authentication;\n this.duration = params.duration || DEFAULT_DURATION;\n this.startTime = params.startTime;\n this.endTime = params.endTime;\n this.expires = params.expires;\n this.safetyMargin = params.safetyMargin;\n this.expirationCheckInterval =\n Math.min(this.duration / 100, DEFAULT_CHECK_EXPIRATION_INTERVAL) * 1000;\n this.emitter = mitt();\n }\n\n /**\n * Checks if the session is expired. If it is expired, it emits an \"expired\" event. The event will fire **before** the method returns true.\n *\n * @returns {boolean} - Returns true if the session is expired, otherwise false.\n */\n isSessionExpired() {\n if (this.isExpired) {\n this.emitter.emit(\"expired\", {\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n });\n }\n return this.isExpired;\n }\n\n /**\n * Starts checking the expiration time of the session. This will check the expiration time immediately and then on an interval.\n * If the session is expired, it will emit an \"expired\" event.\n */\n startCheckingExpirationTime() {\n const check = () => {\n this.isSessionExpired();\n };\n\n if (!this.expirationTimerId) {\n this.expirationTimerId = setInterval(\n check,\n // check every 10 seconds or 1/100th of the duration, whichever is smaller\n this.expirationCheckInterval\n ); // check immediatly then on an interval\n }\n\n setTimeout(() => {\n check(); // check immediately after starting the interval\n }, 10);\n\n return this.expirationTimerId; // return the timer ID so it can be stopped later\n }\n\n /**\n * Stops checking the expiration time of the session. This will clear the interval that was set by {@linkcode BaseSession.startCheckingExpirationTime}.\n */\n stopCheckingExpirationTime() {\n if (this.expirationTimerId) {\n clearInterval(this.expirationTimerId);\n this.expirationTimerId = null;\n }\n }\n\n /**\n * Indicates if the session is currently checking for expiration time.\n *\n * @returns {boolean} - Returns true if the session is checking for expiration time, otherwise false.\n */\n get checkingExpirationTime(): boolean {\n return !!this.expirationTimerId;\n }\n\n /**\n * Starts a new session using the provided parameters and returns an instance of the session class.\n *\n * @param params - The parameters for starting the session.\n * @param SessionClass - The class to use for the session.\n * @returns A promise that resolves to an instance of the session class.\n */\n protected static async startSession<T extends BaseSession>(\n {\n startSessionUrl,\n styleFamily = \"arcgis\",\n authentication,\n safetyMargin,\n duration = DEFAULT_DURATION,\n autoRefresh = true\n }: {\n startSessionUrl?: string;\n styleFamily?: StyleFamily;\n authentication: IAuthenticationManager | string;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n },\n SessionClass: new (params: IBasemapSessionParams) => T\n ): Promise<T> {\n const sessionResponse = await startNewSession({\n startSessionUrl,\n styleFamily,\n authentication,\n duration\n });\n const actualSafetyMargin = determineSafetyMargin(duration, safetyMargin);\n\n const session = new SessionClass({\n startSessionUrl: startSessionUrl,\n token: sessionResponse.sessionToken,\n styleFamily,\n authentication,\n safetyMargin: actualSafetyMargin,\n expires: new Date(sessionResponse.endTime - actualSafetyMargin * 1000),\n startTime: new Date(sessionResponse.startTime),\n endTime: new Date(sessionResponse.endTime),\n duration\n });\n\n session.startCheckingExpirationTime();\n\n if (autoRefresh) {\n session.startAutoRefresh();\n }\n\n return session as T;\n }\n\n /**\n * Checks if the session is expired.\n *\n */\n get isExpired(): boolean {\n return this.expires < new Date();\n }\n\n /**\n * Gets the session token. If the session is expired, it will refresh the credentials and return the new token.\n *\n * @returns A promise that resolves to the session token.\n */\n getToken(): Promise<string> {\n if (this.isExpired) {\n return this.refreshCredentials().then(() => this.token);\n }\n\n return Promise.resolve(this.token);\n }\n\n /**\n * Indicates if the session can be refreshed. This is always true for this class.\n *\n * @returns {boolean} - Always returns true.\n */\n get canRefresh(): boolean {\n return true;\n }\n\n /**\n * Indicates if the session is set to automatically refresh when it expires.\n *\n * @returns {boolean} - Returns true if auto-refresh is enabled, otherwise false.\n */\n get autoRefresh(): boolean {\n return !!this.autoRefreshHandler && !!this.expirationTimerId;\n }\n\n /**\n * Refreshes the session credentials by starting a new session.\n * This will emit a \"refreshed\" event with the previous and current session details.\n *\n * @returns A promise that resolves to the current instance of the session.\n */\n async refreshCredentials(): Promise<this> {\n // @TODO switch this to structured clone when we upgrade to Node 20+ types so we don't have to parse the dates later\n const previous = JSON.parse(\n JSON.stringify({\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n })\n );\n\n try {\n const newSession = await startNewSession({\n startSessionUrl: this.startSessionUrl,\n styleFamily: this.styleFamily,\n authentication: this.authentication,\n duration: this.duration\n });\n\n this.setToken(newSession.sessionToken);\n this.setStartTime(new Date(newSession.startTime));\n this.setEndTime(new Date(newSession.endTime));\n this.setExpires(new Date(newSession.endTime - this.safetyMargin * 1000));\n\n this.emitter.emit(\"refreshed\", {\n previous: {\n token: previous.token,\n startTime: new Date(previous.startTime),\n endTime: new Date(previous.endTime),\n expires: new Date(previous.expires)\n },\n current: {\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n }\n });\n } catch (error) {\n this.emitter.emit(\"error\", error);\n throw error;\n }\n\n return this;\n }\n /**\n * Enables auto-refresh for the session. This will automatically refresh the session when it expires.\n * It will also start checking the expiration time of the session if it is not already started via {@linkcode BaseSession.startCheckingExpirationTime}.\n */\n startAutoRefresh() {\n if (!this.expirationTimerId) {\n this.startCheckingExpirationTime();\n }\n\n this.autoRefreshHandler = () => {\n this.refreshCredentials().catch((error: Error) => {\n this.emitter.emit(\"error\", error);\n });\n };\n\n this.on(\"expired\", this.autoRefreshHandler);\n }\n\n /**\n * Disables auto-refresh for the session. This will stop automatically refreshing the session when it expires.\n * This will **not** stop checking the expiration time of the session. If you want to stop automated expiration\n * checking, call {@linkcode BaseSession.stopCheckingExpirationTime} after calling this method.\n */\n stopAutoRefresh() {\n if (this.autoRefreshHandler) {\n this.off(\"expired\", this.autoRefreshHandler);\n this.autoRefreshHandler = null;\n }\n }\n\n /**\n * A handler that listens for an eventName and returns custom handler.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n on(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n on(event: \"expired\", handler: typeof BaseSession.expired): void;\n on(event: \"error\", handler: typeof BaseSession.error): void;\n on(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n this.emitter.on(eventName, handler);\n this.isSessionExpired(); // check if the session is expired immediately after adding the handler\n }\n\n /**\n * A handler that listens for an event once and returns a custom handler. Events listened to with this method cannot be removed with {@linkcode BasemapSession.off}.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n once(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n once(event: \"expired\", handler: typeof BaseSession.expired): void;\n once(event: \"error\", handler: typeof BaseSession.error): void;\n once(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n const fn = (e: any) => {\n this.emitter.off(eventName, fn);\n handler(e);\n };\n\n this.emitter.on(eventName, fn);\n }\n\n /**\n * A handler that will remove a listener from a given event.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n off(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n off(event: \"expired\", handler: typeof BaseSession.expired): void;\n off(event: \"error\", handler: typeof BaseSession.error): void;\n off(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n this.emitter.off(eventName, handler);\n }\n\n /**\n * These private methods are used to set the internal state of the session.\n */\n private setToken(token: string) {\n (this as Writable<typeof this>).token = token;\n }\n private setStartTime(startTime: Date) {\n (this as Writable<typeof this>).startTime = startTime;\n }\n private setEndTime(endTime: Date) {\n (this as Writable<typeof this>).endTime = endTime;\n }\n private setExpires(expires: Date) {\n (this as Writable<typeof this>).expires = expires;\n }\n}\n","import {\n BaseSession,\n IBasemapSessionParams,\n IStartSessionParams\n} from \"./BaseSession.js\";\nimport { DEFAULT_START_BASEMAP_STYLE_SESSION_URL } from \"./utils/defaults.js\";\n\n/**\n * `BasemapStyleSession` is a class that extends {@linkcode BaseSession} to manage sessions\n * for basemap styles. It provides methods to {@linkcode BasemapStyleSession.start} a new session\n * which should be used instead of constructing a new instance directly.\n *\n * @class BasemapStyleSession\n * @extends BaseSession\n */\nexport class BasemapStyleSession extends BaseSession {\n /**\n * Creates an instance of `BasemapStyleSession`. Constructing `BasemapStyleSession` directly is discouraged.\n * Instead, use the static method {@linkcode BasemapStyleSession.start} to start a new session.\n */\n constructor(params: IBasemapSessionParams) {\n super(params);\n }\n\n /**\n * Starts a new basemap style session.\n */\n static async start(params: IStartSessionParams) {\n return BaseSession.startSession<BasemapStyleSession>(\n {\n ...params,\n startSessionUrl:\n params?.startSessionUrl || DEFAULT_START_BASEMAP_STYLE_SESSION_URL\n },\n BasemapStyleSession as new (\n params: IBasemapSessionParams\n ) => BasemapStyleSession\n );\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAe,aAAQ,CAAC,CAAC,CAAC,CAAC,OAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG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AAC;KACnD;IACO,UAAU,CAAC,OAAa;QAC7B,IAA8B,CAAC,OAAO,GAAG,OAAO,CAAC;KACnD;;AAxdD;AACA;AACA;;;AAGgB,iBAAK,GAAG,SAAS,KAAK,CAAC,CAAQ,KAAU,CAAC;AAE1D;AACA;AACA;;;;;;;;;;AAUgB,mBAAO,GAAG,SAAS,OAAO,CAAC,CAK1C,KAAU,CAAC;AAEZ;AACA;AACA;;;;;;;;;;;;;;;;AAgBgB,qBAAS,GAAG,SAAS,SAAS,CAAC,CAa9C,KAAU,CAAC;;AClGd;;;;;;;;MAQa,mBAAoB,SAAQ,WAAW;;;;;IAKlD,YAAY,MAA6B;QACvC,KAAK,CAAC,MAAM,CAAC,CAAC;KACf;;;;IAKD,aAAa,KAAK,CAAC,MAA2B;QAC5C,OAAO,WAAW,CAAC,YAAY,iCAExB,MAAM,KACT,eAAe,EACb,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,eAAe,KAAI,uCAAuC,KAEtE,mBAEwB,CACzB,CAAC;KACH;;;;;"}
@@ -0,0 +1,12 @@
1
+ /* @preserve
2
+ * @esri/arcgis-rest-basemap-sessions - v1.0.0 - Apache-2.0
3
+ * Copyright (c) 2017-2025 Esri, Inc.
4
+ * Wed Jul 30 2025 12:24:38 GMT-0700 (Pacific Daylight Time)
5
+ */
6
+ import{request as t}from"@esri/arcgis-rest-request";const e="https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/sessions/start",i=300,s=10,r=43200;function n({startSessionUrl:e,authentication:i,styleFamily:s="arcgis",duration:r=43200}){return t(e,{httpMethod:"GET",authentication:i,params:{styleFamily:s,durationSeconds:r}})}class a{constructor(t){var e;this.expirationTimerId=null,this.autoRefreshHandler=null,this.startSessionUrl=t.startSessionUrl,this.token=t.token,this.styleFamily=t.styleFamily||"arcgis",this.authentication=t.authentication,this.duration=t.duration||r,this.startTime=t.startTime,this.endTime=t.endTime,this.expires=t.expires,this.safetyMargin=t.safetyMargin,this.expirationCheckInterval=1e3*Math.min(this.duration/100,10),this.emitter={all:e=e||new Map,on:function(t,i){var s=e.get(t);s?s.push(i):e.set(t,[i])},off:function(t,i){var s=e.get(t);s&&(i?s.splice(s.indexOf(i)>>>0,1):e.set(t,[]))},emit:function(t,i){var s=e.get(t);s&&s.slice().map((function(t){t(i)})),(s=e.get("*"))&&s.slice().map((function(e){e(t,i)}))}}}isSessionExpired(){return this.isExpired&&this.emitter.emit("expired",{token:this.token,startTime:this.startTime,endTime:this.endTime,expires:this.expires}),this.isExpired}startCheckingExpirationTime(){const t=()=>{this.isSessionExpired()};return this.expirationTimerId||(this.expirationTimerId=setInterval(t,this.expirationCheckInterval)),setTimeout((()=>{t()}),10),this.expirationTimerId}stopCheckingExpirationTime(){this.expirationTimerId&&(clearInterval(this.expirationTimerId),this.expirationTimerId=null)}get checkingExpirationTime(){return!!this.expirationTimerId}static async startSession({startSessionUrl:t,styleFamily:e="arcgis",authentication:i,safetyMargin:s,duration:r=43200,autoRefresh:a=!0},o){const h=await n({startSessionUrl:t,styleFamily:e,authentication:i,duration:r}),m=function(t,e){return e||Math.min(Math.max(t/100,1),300)}(r,s),c=new o({startSessionUrl:t,token:h.sessionToken,styleFamily:e,authentication:i,safetyMargin:m,expires:new Date(h.endTime-1e3*m),startTime:new Date(h.startTime),endTime:new Date(h.endTime),duration:r});return c.startCheckingExpirationTime(),a&&c.startAutoRefresh(),c}get isExpired(){return this.expires<new Date}getToken(){return this.isExpired?this.refreshCredentials().then((()=>this.token)):Promise.resolve(this.token)}get canRefresh(){return!0}get autoRefresh(){return!!this.autoRefreshHandler&&!!this.expirationTimerId}async refreshCredentials(){const t=JSON.parse(JSON.stringify({token:this.token,startTime:this.startTime,endTime:this.endTime,expires:this.expires}));try{const e=await n({startSessionUrl:this.startSessionUrl,styleFamily:this.styleFamily,authentication:this.authentication,duration:this.duration});this.setToken(e.sessionToken),this.setStartTime(new Date(e.startTime)),this.setEndTime(new Date(e.endTime)),this.setExpires(new Date(e.endTime-1e3*this.safetyMargin)),this.emitter.emit("refreshed",{previous:{token:t.token,startTime:new Date(t.startTime),endTime:new Date(t.endTime),expires:new Date(t.expires)},current:{token:this.token,startTime:this.startTime,endTime:this.endTime,expires:this.expires}})}catch(t){throw this.emitter.emit("error",t),t}return this}startAutoRefresh(){this.expirationTimerId||this.startCheckingExpirationTime(),this.autoRefreshHandler=()=>{this.refreshCredentials().catch((t=>{this.emitter.emit("error",t)}))},this.on("expired",this.autoRefreshHandler)}stopAutoRefresh(){this.autoRefreshHandler&&(this.off("expired",this.autoRefreshHandler),this.autoRefreshHandler=null)}on(t,e){this.emitter.on(t,e),this.isSessionExpired()}once(t,e){const i=s=>{this.emitter.off(t,i),e(s)};this.emitter.on(t,i)}off(t,e){this.emitter.off(t,e)}setToken(t){this.token=t}setStartTime(t){this.startTime=t}setEndTime(t){this.endTime=t}setExpires(t){this.expires=t}}
7
+ /* istanbul ignore next -- @preserve */a.error=function(t){},
8
+ /* istanbul ignore next -- @preserve */
9
+ a.expired=function(t){},
10
+ /* istanbul ignore next -- @preserve */
11
+ a.refreshed=function(t){};class o extends a{constructor(t){super(t)}static async start(t){return a.startSession(Object.assign(Object.assign({},t),{startSessionUrl:(null==t?void 0:t.startSessionUrl)||e}),o)}}export{a as BaseSession,o as BasemapStyleSession,s as DEFAULT_CHECK_EXPIRATION_INTERVAL,r as DEFAULT_DURATION,i as DEFAULT_SAFETY_MARGIN,e as DEFAULT_START_BASEMAP_STYLE_SESSION_URL,n as startNewSession};
12
+ //# sourceMappingURL=basemap-sessions.esm.min.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basemap-sessions.esm.min.js","sources":["../../src/utils/defaults.ts","../../src/utils/startNewSession.ts","../../src/BaseSession.ts","../../node_modules/mitt/dist/mitt.mjs","../../src/utils/detemineSafetyMargin.ts","../../src/BasemapStyleSession.ts"],"sourcesContent":["export const DEFAULT_START_BASEMAP_STYLE_SESSION_URL =\n \"https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/sessions/start\";\n\nexport const DEFAULT_SAFETY_MARGIN = 5 * 60; // Default to 5 minutes in seconds\n\nexport const DEFAULT_CHECK_EXPIRATION_INTERVAL = 10; // Default to 10 seconds\n\nexport const DEFAULT_DURATION = 12 * 60 * 60; // Default to 12 hours in seconds\n","import { IAuthenticationManager, request } from \"@esri/arcgis-rest-request\";\nimport { StyleFamily } from \"../types/StyleFamily.js\";\nimport { DEFAULT_DURATION } from \"./defaults.js\";\n\nexport interface IRequestNewSessionParams {\n startSessionUrl: string;\n authentication: IAuthenticationManager | string;\n styleFamily?: StyleFamily;\n duration?: number;\n}\n\nexport interface IStartSessionResponse {\n sessionToken: string;\n endTime: number;\n startTime: number;\n styleFamily: StyleFamily;\n}\n\nexport function startNewSession({\n startSessionUrl,\n authentication,\n styleFamily = \"arcgis\",\n duration = DEFAULT_DURATION\n}: IRequestNewSessionParams): Promise<IStartSessionResponse> {\n return request(startSessionUrl, {\n httpMethod: \"GET\",\n authentication: authentication,\n params: { styleFamily, durationSeconds: duration }\n });\n}\n","import mitt from \"mitt\";\n\nimport { IAuthenticationManager } from \"@esri/arcgis-rest-request\";\nimport { StyleFamily } from \"./types/StyleFamily.js\";\nimport { startNewSession } from \"./utils/startNewSession.js\";\nimport { Writable } from \"./utils/writable.js\";\nimport { determineSafetyMargin } from \"./utils/detemineSafetyMargin.js\";\nimport {\n DEFAULT_DURATION,\n DEFAULT_SAFETY_MARGIN,\n DEFAULT_CHECK_EXPIRATION_INTERVAL\n} from \"./utils/defaults.js\";\n\nexport interface IBasemapSessionParams {\n token: string;\n startSessionUrl: string;\n styleFamily: StyleFamily;\n authentication: IAuthenticationManager | string;\n expires: Date;\n startTime: Date;\n endTime: Date;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n}\n\nexport interface IStartSessionParams {\n styleFamily?: StyleFamily;\n authentication: IAuthenticationManager | string;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n\n /**\n * The URL to start the session. If not provided, it will use the default URL.\n * @private\n */\n startSessionUrl?: string;\n}\n\n/**\n * The base class for all basemap sessions. This class implements the {@linkcode IAuthenticationManager} interface and provides methods to start, refresh, and check the expiration of a session.\n * This is not intendet to be used directly, but instead is extended by other classes such as {@linkcode BasemapStyleSession} and {@linkcode StaticBasemapTilesSession}.\n *\n * @abstract\n * @implements {IAuthenticationManager}\n */\nexport abstract class BaseSession implements IAuthenticationManager {\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when an error occurs during session management.\n */\n static readonly error = function error(e: Error): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when a session expires and the `token` it no longer valid.\n *\n * @event expired\n * @param e - The parameters for the expired event.\n * @param e.token - The session token that expired.\n * @param e.startTime - The start time of the session.\n * @param e.endTime - The end time of the session.\n * @param e.expires - The expiration time of the session.\n */\n static readonly expired = function expired(e: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n }): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n // the static methods for event handlers are used to provide doc via typedoc and do not need to be tested.\n /* istanbul ignore next -- @preserve */\n /**\n * Event handler for when a session refreshes and a new `token` is available.\n *\n * @event refreshed\n * @param e. - The parameters for the refreshed event.\n * @param e.previous - The previous session details.\n * @param e.previous.token - The previous session token.\n * @param e.previous.startTime - The start time of the previous session.\n * @param e.previous.endTime - The end time of the previous session.\n * @param e.previous.expires - The expiration time of the previous session.\n * @param e.current - The current session details.\n * @param e.current.token - The current session token.\n * @param e.current.startTime - The start time of the current token.\n * @param e.current.endTime - The end time of the current session.\n * @param e.current.expires - The expiration time of the current token.\n */\n static readonly refreshed = function refreshed(e: {\n previous: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n };\n current: {\n token: string;\n startTime: Date;\n endTime: Date;\n expires: Date;\n };\n }): void {}; // eslint-disable-line @typescript-eslint/no-empty-function\n\n /**\n * The portal URL that the session is associated with. This generally is not used but exists to implement the `IAuthenticationManager` interface.\n */\n readonly portal: string;\n\n /**\n * The style family of the session. This is used to determine the type of basemap styles that are available.\n */\n readonly styleFamily: StyleFamily;\n\n /**\n * The authentication manager or token used for the session.\n * This can be an instance of {@linkcode ApiKeyManager}, {@linkcode ArcGISIdentityManager}, {@linkcode ApplicationCredentialsManager} or a string token.\n */\n readonly authentication: IAuthenticationManager | string;\n\n /**\n * The expiration date of the session. This is the {@linkcode BaseSession.endTime} minus the {@linkcode BaseSession.safetyMargin}. This is used internally to determine if the session is expired.\n */\n readonly expires: Date;\n\n /**\n * The start time of the session. This is the time returned from the API when the session war started.\n */\n readonly startTime: Date;\n\n /**\n * The end time of the session. This is the time returned from the API when the session will end.\n */\n readonly endTime: Date;\n\n /**\n * The token for the session.\n */\n readonly token: string;\n\n /**\n * The URL used to start the session.\n */\n readonly startSessionUrl: string;\n\n /**\n * The safety margin in milliseconds. This subtracted from the {@linkcode BaseSession.endTime} to get the {@linkcode BaseSession.expiration}.\n */\n readonly safetyMargin: number;\n\n /**\n * The duration of the session in seconds. This is used to determine how long the session will last when the session is refreshed.\n */\n readonly duration: number;\n\n /**\n * The interval at which to check the expiration time of the session. This is always 10 seconds or 1/100th of the duration, whichever is smaller.\n */\n readonly expirationCheckInterval: number;\n\n /**\n * The ID of the timer used to check the expiration time of the session.\n */\n private expirationTimerId: any = null;\n\n /**\n * Internal instance of [`mitt`](https://github.com/developit/mitt) used for event handlers. It is recommended to use {@linkcode BasemapSession.on}, {@linkcode BasemapSession.off} or {@linkcode BasemapSession.once} instead of `emitter.`\n */\n private emitter: any;\n\n /**\n * A handler that is used to automatically refresh the session when it expires.\n */\n private autoRefreshHandler: (() => void) | null = null;\n\n /**\n * Creates a new instance of the BaseSession class. Generally you should not create an instance of this class directly, but instead use the static methods to start a session or deserialize a session.\n *\n * You may need to create an instance of this class directly if you are not using the built in deserialize method.\n *\n * @param params - The parameters for the session.\n * @param params.startSessionUrl - The URL to start the session.\n * @param params.token - The token for the session.\n * @param params.styleFamily - The style family of the session.\n * @param params.authentication - The authentication manager or token used for the session.\n * @param params.expires - The expiration date of the session.\n * @param params.startTime - The start time of the session.\n * @param params.endTime - The end time of the session.\n * @param params.safetyMargin - The safety margin in milliseconds.\n * @param params.duration - Indicates if this is a test session.\n */\n constructor(params: IBasemapSessionParams) {\n this.startSessionUrl = params.startSessionUrl;\n this.token = params.token;\n this.styleFamily = params.styleFamily || \"arcgis\";\n this.authentication = params.authentication;\n this.duration = params.duration || DEFAULT_DURATION;\n this.startTime = params.startTime;\n this.endTime = params.endTime;\n this.expires = params.expires;\n this.safetyMargin = params.safetyMargin;\n this.expirationCheckInterval =\n Math.min(this.duration / 100, DEFAULT_CHECK_EXPIRATION_INTERVAL) * 1000;\n this.emitter = mitt();\n }\n\n /**\n * Checks if the session is expired. If it is expired, it emits an \"expired\" event. The event will fire **before** the method returns true.\n *\n * @returns {boolean} - Returns true if the session is expired, otherwise false.\n */\n isSessionExpired() {\n if (this.isExpired) {\n this.emitter.emit(\"expired\", {\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n });\n }\n return this.isExpired;\n }\n\n /**\n * Starts checking the expiration time of the session. This will check the expiration time immediately and then on an interval.\n * If the session is expired, it will emit an \"expired\" event.\n */\n startCheckingExpirationTime() {\n const check = () => {\n this.isSessionExpired();\n };\n\n if (!this.expirationTimerId) {\n this.expirationTimerId = setInterval(\n check,\n // check every 10 seconds or 1/100th of the duration, whichever is smaller\n this.expirationCheckInterval\n ); // check immediatly then on an interval\n }\n\n setTimeout(() => {\n check(); // check immediately after starting the interval\n }, 10);\n\n return this.expirationTimerId; // return the timer ID so it can be stopped later\n }\n\n /**\n * Stops checking the expiration time of the session. This will clear the interval that was set by {@linkcode BaseSession.startCheckingExpirationTime}.\n */\n stopCheckingExpirationTime() {\n if (this.expirationTimerId) {\n clearInterval(this.expirationTimerId);\n this.expirationTimerId = null;\n }\n }\n\n /**\n * Indicates if the session is currently checking for expiration time.\n *\n * @returns {boolean} - Returns true if the session is checking for expiration time, otherwise false.\n */\n get checkingExpirationTime(): boolean {\n return !!this.expirationTimerId;\n }\n\n /**\n * Starts a new session using the provided parameters and returns an instance of the session class.\n *\n * @param params - The parameters for starting the session.\n * @param SessionClass - The class to use for the session.\n * @returns A promise that resolves to an instance of the session class.\n */\n protected static async startSession<T extends BaseSession>(\n {\n startSessionUrl,\n styleFamily = \"arcgis\",\n authentication,\n safetyMargin,\n duration = DEFAULT_DURATION,\n autoRefresh = true\n }: {\n startSessionUrl?: string;\n styleFamily?: StyleFamily;\n authentication: IAuthenticationManager | string;\n safetyMargin?: number;\n duration?: number;\n autoRefresh?: boolean;\n },\n SessionClass: new (params: IBasemapSessionParams) => T\n ): Promise<T> {\n const sessionResponse = await startNewSession({\n startSessionUrl,\n styleFamily,\n authentication,\n duration\n });\n const actualSafetyMargin = determineSafetyMargin(duration, safetyMargin);\n\n const session = new SessionClass({\n startSessionUrl: startSessionUrl,\n token: sessionResponse.sessionToken,\n styleFamily,\n authentication,\n safetyMargin: actualSafetyMargin,\n expires: new Date(sessionResponse.endTime - actualSafetyMargin * 1000),\n startTime: new Date(sessionResponse.startTime),\n endTime: new Date(sessionResponse.endTime),\n duration\n });\n\n session.startCheckingExpirationTime();\n\n if (autoRefresh) {\n session.startAutoRefresh();\n }\n\n return session as T;\n }\n\n /**\n * Checks if the session is expired.\n *\n */\n get isExpired(): boolean {\n return this.expires < new Date();\n }\n\n /**\n * Gets the session token. If the session is expired, it will refresh the credentials and return the new token.\n *\n * @returns A promise that resolves to the session token.\n */\n getToken(): Promise<string> {\n if (this.isExpired) {\n return this.refreshCredentials().then(() => this.token);\n }\n\n return Promise.resolve(this.token);\n }\n\n /**\n * Indicates if the session can be refreshed. This is always true for this class.\n *\n * @returns {boolean} - Always returns true.\n */\n get canRefresh(): boolean {\n return true;\n }\n\n /**\n * Indicates if the session is set to automatically refresh when it expires.\n *\n * @returns {boolean} - Returns true if auto-refresh is enabled, otherwise false.\n */\n get autoRefresh(): boolean {\n return !!this.autoRefreshHandler && !!this.expirationTimerId;\n }\n\n /**\n * Refreshes the session credentials by starting a new session.\n * This will emit a \"refreshed\" event with the previous and current session details.\n *\n * @returns A promise that resolves to the current instance of the session.\n */\n async refreshCredentials(): Promise<this> {\n // @TODO switch this to structured clone when we upgrade to Node 20+ types so we don't have to parse the dates later\n const previous = JSON.parse(\n JSON.stringify({\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n })\n );\n\n try {\n const newSession = await startNewSession({\n startSessionUrl: this.startSessionUrl,\n styleFamily: this.styleFamily,\n authentication: this.authentication,\n duration: this.duration\n });\n\n this.setToken(newSession.sessionToken);\n this.setStartTime(new Date(newSession.startTime));\n this.setEndTime(new Date(newSession.endTime));\n this.setExpires(new Date(newSession.endTime - this.safetyMargin * 1000));\n\n this.emitter.emit(\"refreshed\", {\n previous: {\n token: previous.token,\n startTime: new Date(previous.startTime),\n endTime: new Date(previous.endTime),\n expires: new Date(previous.expires)\n },\n current: {\n token: this.token,\n startTime: this.startTime,\n endTime: this.endTime,\n expires: this.expires\n }\n });\n } catch (error) {\n this.emitter.emit(\"error\", error);\n throw error;\n }\n\n return this;\n }\n /**\n * Enables auto-refresh for the session. This will automatically refresh the session when it expires.\n * It will also start checking the expiration time of the session if it is not already started via {@linkcode BaseSession.startCheckingExpirationTime}.\n */\n startAutoRefresh() {\n if (!this.expirationTimerId) {\n this.startCheckingExpirationTime();\n }\n\n this.autoRefreshHandler = () => {\n this.refreshCredentials().catch((error: Error) => {\n this.emitter.emit(\"error\", error);\n });\n };\n\n this.on(\"expired\", this.autoRefreshHandler);\n }\n\n /**\n * Disables auto-refresh for the session. This will stop automatically refreshing the session when it expires.\n * This will **not** stop checking the expiration time of the session. If you want to stop automated expiration\n * checking, call {@linkcode BaseSession.stopCheckingExpirationTime} after calling this method.\n */\n stopAutoRefresh() {\n if (this.autoRefreshHandler) {\n this.off(\"expired\", this.autoRefreshHandler);\n this.autoRefreshHandler = null;\n }\n }\n\n /**\n * A handler that listens for an eventName and returns custom handler.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n on(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n on(event: \"expired\", handler: typeof BaseSession.expired): void;\n on(event: \"error\", handler: typeof BaseSession.error): void;\n on(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n this.emitter.on(eventName, handler);\n this.isSessionExpired(); // check if the session is expired immediately after adding the handler\n }\n\n /**\n * A handler that listens for an event once and returns a custom handler. Events listened to with this method cannot be removed with {@linkcode BasemapSession.off}.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n once(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n once(event: \"expired\", handler: typeof BaseSession.expired): void;\n once(event: \"error\", handler: typeof BaseSession.error): void;\n once(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n const fn = (e: any) => {\n this.emitter.off(eventName, fn);\n handler(e);\n };\n\n this.emitter.on(eventName, fn);\n }\n\n /**\n * A handler that will remove a listener from a given event.\n *\n * @param eventName A string of what event to listen for.\n * @param handler A function of what to do when eventName was called.\n */\n off(event: \"refreshed\", handler: typeof BaseSession.refreshed): void;\n off(event: \"expired\", handler: typeof BaseSession.expired): void;\n off(event: \"error\", handler: typeof BaseSession.error): void;\n off(\n eventName: string,\n handler:\n | typeof BaseSession.refreshed\n | typeof BaseSession.expired\n | typeof BaseSession.error\n ) {\n this.emitter.off(eventName, handler);\n }\n\n /**\n * These private methods are used to set the internal state of the session.\n */\n private setToken(token: string) {\n (this as Writable<typeof this>).token = token;\n }\n private setStartTime(startTime: Date) {\n (this as Writable<typeof this>).startTime = startTime;\n }\n private setEndTime(endTime: Date) {\n (this as Writable<typeof this>).endTime = endTime;\n }\n private setExpires(expires: Date) {\n (this as Writable<typeof this>).expires = expires;\n }\n}\n","export default function(n){return{all:n=n||new Map,on:function(t,e){var i=n.get(t);i?i.push(e):n.set(t,[e])},off:function(t,e){var i=n.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):n.set(t,[]))},emit:function(t,e){var i=n.get(t);i&&i.slice().map(function(n){n(e)}),(i=n.get(\"*\"))&&i.slice().map(function(n){n(t,e)})}}}\n//# sourceMappingURL=mitt.mjs.map\n","import { DEFAULT_SAFETY_MARGIN } from \"./defaults.js\";\n\nexport function determineSafetyMargin(\n duration: number | undefined,\n safetyMargin: number | undefined\n): number {\n if (safetyMargin) {\n return safetyMargin;\n }\n // common cases are\n // duration is 60 seconds, this will return a 1 second safety margin\n // duration is 43200 seconds, this will return a 300 second (5 minutes) safety margin\n return Math.min(Math.max(duration / 100, 1), DEFAULT_SAFETY_MARGIN);\n}\n","import {\n BaseSession,\n IBasemapSessionParams,\n IStartSessionParams\n} from \"./BaseSession.js\";\nimport { DEFAULT_START_BASEMAP_STYLE_SESSION_URL } from \"./utils/defaults.js\";\n\n/**\n * `BasemapStyleSession` is a class that extends {@linkcode BaseSession} to manage sessions\n * for basemap styles. It provides methods to {@linkcode BasemapStyleSession.start} a new session\n * which should be used instead of constructing a new instance directly.\n *\n * @class BasemapStyleSession\n * @extends BaseSession\n */\nexport class BasemapStyleSession extends BaseSession {\n /**\n * Creates an instance of `BasemapStyleSession`. Constructing `BasemapStyleSession` directly is discouraged.\n * Instead, use the static method {@linkcode BasemapStyleSession.start} to start a new session.\n */\n constructor(params: IBasemapSessionParams) {\n super(params);\n }\n\n /**\n * Starts a new basemap style session.\n */\n static async start(params: IStartSessionParams) {\n return BaseSession.startSession<BasemapStyleSession>(\n {\n ...params,\n startSessionUrl:\n params?.startSessionUrl || DEFAULT_START_BASEMAP_STYLE_SESSION_URL\n },\n BasemapStyleSession as new (\n params: IBasemapSessionParams\n ) => BasemapStyleSession\n );\n }\n}\n"],"names":["DEFAULT_START_BASEMAP_STYLE_SESSION_URL","DEFAULT_SAFETY_MARGIN","DEFAULT_CHECK_EXPIRATION_INTERVAL","DEFAULT_DURATION","startNewSession","startSessionUrl","authentication","styleFamily","duration","request","httpMethod","params","durationSeconds","BaseSession","constructor","n","this","token","startTime","endTime","expires","safetyMargin","expirationCheckInterval","Math","min","emitter","all","Map","on","t","e","i","get","push","set","off","splice","indexOf","emit","slice","map","isSessionExpired","isExpired","startCheckingExpirationTime","check","expirationTimerId","setInterval","setTimeout","stopCheckingExpirationTime","clearInterval","checkingExpirationTime","static","autoRefresh","SessionClass","sessionResponse","actualSafetyMargin","max","determineSafetyMargin","session","sessionToken","Date","startAutoRefresh","getToken","refreshCredentials","then","Promise","resolve","canRefresh","autoRefreshHandler","async","previous","JSON","parse","stringify","newSession","setToken","setStartTime","setEndTime","setExpires","current","error","catch","stopAutoRefresh","eventName","handler","once","fn","BasemapStyleSession","super","startSession"],"mappings":";;;;;0DAAaA,EACX,qFAEWC,EAAwB,IAExBC,EAAoC,GAEpCC,EAAmB,eCWhBC,GAAgBC,gBAC9BA,EAAeC,eACfA,EAAcC,YACdA,EAAc,SAAQC,SACtBA,EAAWL,QAEX,OAAOM,EAAQJ,EAAiB,CAC9BK,WAAY,MACZJ,eAAgBA,EAChBK,OAAQ,CAAEJ,cAAaK,gBAAiBJ,IAE5C,OCkBsBK,EAmJpBC,YAAYH,GClMC,IAASI,EDsKdC,uBAAyB,KAUzBA,wBAA0C,KAmBhDA,KAAKX,gBAAkBM,EAAON,gBAC9BW,KAAKC,MAAQN,EAAOM,MACpBD,KAAKT,YAAcI,EAAOJ,aAAe,SACzCS,KAAKV,eAAiBK,EAAOL,eAC7BU,KAAKR,SAAWG,EAAOH,UAAYL,EACnCa,KAAKE,UAAYP,EAAOO,UACxBF,KAAKG,QAAUR,EAAOQ,QACtBH,KAAKI,QAAUT,EAAOS,QACtBJ,KAAKK,aAAeV,EAAOU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