@esri/hub-common 9.25.7 → 9.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es2017/resources/_internal/_validate-url-helpers.js +231 -0
- package/dist/es2017/resources/_internal/_validate-url-helpers.js.map +1 -0
- package/dist/es2017/resources/index.js +1 -0
- package/dist/es2017/resources/index.js.map +1 -1
- package/dist/es2017/resources/validate-url.js +86 -0
- package/dist/es2017/resources/validate-url.js.map +1 -0
- package/dist/es2017/types.js +862 -0
- package/dist/es2017/types.js.map +1 -1
- package/dist/esm/resources/_internal/_validate-url-helpers.js +249 -0
- package/dist/esm/resources/_internal/_validate-url-helpers.js.map +1 -0
- package/dist/esm/resources/index.js +1 -0
- package/dist/esm/resources/index.js.map +1 -1
- package/dist/esm/resources/validate-url.js +103 -0
- package/dist/esm/resources/validate-url.js.map +1 -0
- package/dist/esm/types.js +862 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/node/resources/_internal/_validate-url-helpers.js +247 -0
- package/dist/node/resources/_internal/_validate-url-helpers.js.map +1 -0
- package/dist/node/resources/index.js +1 -0
- package/dist/node/resources/index.js.map +1 -1
- package/dist/node/resources/validate-url.js +90 -0
- package/dist/node/resources/validate-url.js.map +1 -0
- package/dist/node/types.js +863 -0
- package/dist/node/types.js.map +1 -1
- package/dist/types/resources/_internal/_validate-url-helpers.d.ts +115 -0
- package/dist/types/resources/index.d.ts +1 -0
- package/dist/types/resources/validate-url.d.ts +8 -0
- package/dist/types/types.d.ts +225 -4
- package/dist/umd/common.umd.js +1217 -5
- package/dist/umd/common.umd.js.map +1 -1
- package/dist/umd/common.umd.min.js +1 -1
- package/dist/umd/common.umd.min.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { ItemType } from "../../types";
|
|
2
|
+
import { Logger } from "../../utils";
|
|
3
|
+
const FEATURE_SERVICE_URL_REGEX = /(feature)server(\/|\/(\d+))?$/i;
|
|
4
|
+
const SERVICE_URL_REGEX = /\/[a-zA-Z]+server(\/|\/(\d+))?$/i;
|
|
5
|
+
/**
|
|
6
|
+
* Feature service / Doc Links Should not have data urls. Let"s exclude them from that.
|
|
7
|
+
*
|
|
8
|
+
* @export
|
|
9
|
+
* @param {string} itemType What type of item is it?
|
|
10
|
+
* @return {*} {boolean}
|
|
11
|
+
*/
|
|
12
|
+
export function shouldHaveDataUrl(itemType) {
|
|
13
|
+
// Specifically we want to avoid FS / DL from having a data url.
|
|
14
|
+
return !["Feature Service", "Document Link"].includes(itemType);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get the file name out of a url. Will return either the
|
|
18
|
+
* hostname, or the pathname if it exists
|
|
19
|
+
*
|
|
20
|
+
* @export
|
|
21
|
+
* @param {string} url Url to get a file name out of
|
|
22
|
+
* @return {*} {string}
|
|
23
|
+
*/
|
|
24
|
+
export function getFileName(url) {
|
|
25
|
+
let filename;
|
|
26
|
+
try {
|
|
27
|
+
const parsed = new URL(url);
|
|
28
|
+
// If the URL pathname exists, return its last segment,
|
|
29
|
+
// otherwise return the hostname
|
|
30
|
+
filename =
|
|
31
|
+
parsed.pathname !== "/"
|
|
32
|
+
? parsed.pathname.split("/").pop()
|
|
33
|
+
: parsed.hostname;
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw new Error(`Error getting file name from data url`);
|
|
37
|
+
}
|
|
38
|
+
return filename;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Is this a valid url?
|
|
42
|
+
*
|
|
43
|
+
* @param {string} url Url to validate
|
|
44
|
+
* @return {*} {boolean}
|
|
45
|
+
*/
|
|
46
|
+
export function isUrl(url) {
|
|
47
|
+
// Use try / catch as a simple string "test" will cause new URL() to throw an error.
|
|
48
|
+
try {
|
|
49
|
+
const result = new URL(url);
|
|
50
|
+
// Cast to bool.
|
|
51
|
+
return !!result;
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
Logger.error(`Error parsing data url`);
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Tests if url string is a feature service / layer.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} url URL to test
|
|
62
|
+
* @return {*} {boolean}
|
|
63
|
+
*/
|
|
64
|
+
export function isFeatureService(url) {
|
|
65
|
+
return FEATURE_SERVICE_URL_REGEX.test(url);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Tests if url string is a service (map, feature, image, etc)
|
|
69
|
+
*
|
|
70
|
+
* @param {string} url Url to test
|
|
71
|
+
* @return {*} {boolean}
|
|
72
|
+
*/
|
|
73
|
+
export function isService(url) {
|
|
74
|
+
return SERVICE_URL_REGEX.test(url);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Is the service a feature service AND is it a layer specifically
|
|
78
|
+
*
|
|
79
|
+
* @param {string} url
|
|
80
|
+
* @return {*} {boolean}
|
|
81
|
+
*/
|
|
82
|
+
export function isFeatureLayer(url) {
|
|
83
|
+
const results = url.match(FEATURE_SERVICE_URL_REGEX);
|
|
84
|
+
return results && !!results[3];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Gets item title from url as a fall back
|
|
88
|
+
*
|
|
89
|
+
* @param {string} url item url
|
|
90
|
+
* @return {*} {string}
|
|
91
|
+
*/
|
|
92
|
+
export function getFeatureServiceTitle(url) {
|
|
93
|
+
return url.match(/\/services\/(.+)\/(feature|map|image)server/i)[1];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Gets item info out of a feature layer item.
|
|
97
|
+
*
|
|
98
|
+
* @export
|
|
99
|
+
* @param {string} url Item URL
|
|
100
|
+
* @param {{
|
|
101
|
+
* name: string,
|
|
102
|
+
* description: string,
|
|
103
|
+
* extent: any
|
|
104
|
+
* }} body Item body.
|
|
105
|
+
* @return {*} {{
|
|
106
|
+
* title: string,
|
|
107
|
+
* description: string,
|
|
108
|
+
* extent: any,
|
|
109
|
+
* url: string
|
|
110
|
+
* }}
|
|
111
|
+
*/
|
|
112
|
+
export function getFeatureLayerItem(url, body) {
|
|
113
|
+
return {
|
|
114
|
+
title: body.name,
|
|
115
|
+
description: body.description,
|
|
116
|
+
extent: body.extent,
|
|
117
|
+
url,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Gets item info out of a feature service response (which is not a specific layer)
|
|
122
|
+
*
|
|
123
|
+
* @export
|
|
124
|
+
* @param {*} url
|
|
125
|
+
* @param {*} body
|
|
126
|
+
* @return {*}
|
|
127
|
+
*/
|
|
128
|
+
export function getFeatureServiceItem(url, body) {
|
|
129
|
+
const description = body.serviceDescription || body.description;
|
|
130
|
+
const title = getFeatureServiceTitle(url);
|
|
131
|
+
const extent = body.fullExtent || body.initialExtent;
|
|
132
|
+
return { title, description, extent, url };
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Ping a non FS url and return response status && headers
|
|
136
|
+
*
|
|
137
|
+
* @export
|
|
138
|
+
* @param {string} url Non FS URL
|
|
139
|
+
* @return {*} {Promise<{ ok: boolean, headers: Headers }>}
|
|
140
|
+
*/
|
|
141
|
+
export async function pingUrl(url) {
|
|
142
|
+
const response = await fetch(url, { method: "HEAD" });
|
|
143
|
+
return {
|
|
144
|
+
ok: response.ok,
|
|
145
|
+
headers: response.headers,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Ping a FS URL and handle matters such as "hidden" success failures.
|
|
150
|
+
*
|
|
151
|
+
* @export
|
|
152
|
+
* @param {string} url
|
|
153
|
+
* @return {*} {Promise<{ ok: boolean, item?: any }>}
|
|
154
|
+
*/
|
|
155
|
+
export async function pingFeatureService(url) {
|
|
156
|
+
// make sure the response is in json format
|
|
157
|
+
const parsed = new URL(url);
|
|
158
|
+
parsed.searchParams.set("f", "json");
|
|
159
|
+
// Since the feature service can return a 200 response with error (e.g. for
|
|
160
|
+
// non-existing layer), we can only request the full metadata by a GET, not HEAD
|
|
161
|
+
// request
|
|
162
|
+
const response = await fetch(parsed.href);
|
|
163
|
+
if (!response.ok) {
|
|
164
|
+
return { ok: false };
|
|
165
|
+
}
|
|
166
|
+
const body = await response.json();
|
|
167
|
+
// Exit if the request returns an error
|
|
168
|
+
if (body.error) {
|
|
169
|
+
return { ok: false };
|
|
170
|
+
}
|
|
171
|
+
const getItem = isFeatureLayer(url)
|
|
172
|
+
? getFeatureLayerItem
|
|
173
|
+
: getFeatureServiceItem;
|
|
174
|
+
const item = getItem(url, body);
|
|
175
|
+
return {
|
|
176
|
+
ok: true,
|
|
177
|
+
item,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
export function detectDataTypeFromHeader(headers) {
|
|
181
|
+
let contentType = headers.get("Content-Type");
|
|
182
|
+
let dataType;
|
|
183
|
+
if (!contentType) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
// Only get the "media-type"
|
|
187
|
+
contentType = contentType.split(";").shift();
|
|
188
|
+
if (contentType === "text/csv") {
|
|
189
|
+
dataType = ItemType.CSV;
|
|
190
|
+
}
|
|
191
|
+
else if (contentType === "application/vnd.ms-excel" ||
|
|
192
|
+
contentType ===
|
|
193
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
|
|
194
|
+
dataType = ItemType["Microsoft Excel"];
|
|
195
|
+
}
|
|
196
|
+
else if (contentType === "application/pdf") {
|
|
197
|
+
dataType = ItemType.PDF;
|
|
198
|
+
}
|
|
199
|
+
else if (contentType === "image/jpeg" ||
|
|
200
|
+
contentType === "image/jpg" ||
|
|
201
|
+
contentType === "image/png") {
|
|
202
|
+
dataType = ItemType.Image;
|
|
203
|
+
}
|
|
204
|
+
else if (contentType === "application/geo+json") {
|
|
205
|
+
dataType = ItemType.GeoJson;
|
|
206
|
+
}
|
|
207
|
+
return dataType;
|
|
208
|
+
}
|
|
209
|
+
export function detectDataTypeFromExtension(url) {
|
|
210
|
+
const contentType = url.toLowerCase().split(".").pop();
|
|
211
|
+
let dataType;
|
|
212
|
+
if (contentType === "csv") {
|
|
213
|
+
dataType = ItemType.CSV;
|
|
214
|
+
}
|
|
215
|
+
else if (contentType === "xls" || contentType === "xlsx") {
|
|
216
|
+
dataType = ItemType["Microsoft Excel"];
|
|
217
|
+
}
|
|
218
|
+
else if (contentType === "pdf") {
|
|
219
|
+
dataType = ItemType.PDF;
|
|
220
|
+
}
|
|
221
|
+
else if (contentType === "jpeg" ||
|
|
222
|
+
contentType === "jpg" ||
|
|
223
|
+
contentType === "png") {
|
|
224
|
+
dataType = ItemType.Image;
|
|
225
|
+
}
|
|
226
|
+
else if (contentType === "geojson") {
|
|
227
|
+
dataType = ItemType.GeoJson;
|
|
228
|
+
}
|
|
229
|
+
return dataType;
|
|
230
|
+
}
|
|
231
|
+
//# sourceMappingURL=_validate-url-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_validate-url-helpers.js","sourceRoot":"","sources":["../../../../src/resources/_internal/_validate-url-helpers.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,yBAAyB,GAAG,gCAAgC,CAAC;AACnE,MAAM,iBAAiB,GAAG,kCAAkC,CAAC;AAE7D;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,QAAgB;IAChD,gEAAgE;IAChE,OAAO,CAAC,CAAC,iBAAiB,EAAE,eAAe,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAClE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,GAAW;IACrC,IAAI,QAAgB,CAAC;IAErB,IAAI;QACF,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QAE5B,uDAAuD;QACvD,gCAAgC;QAChC,QAAQ;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AAQ,CAAC;AAClB,CAAC;AAED,MAAM,UAAU,2BAA2B,CAAC,GAAW;IACrD,MAAM,WAAW,GAAG,GAAG,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;IACvD,IAAI,QAAQ,CAAC;IAEb,IAAI,WAAW,KAAK,KAAK,EAAE;QACzB,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC;KACzB;SAAM,IAAI,WAAW,KAAK,KAAK,IAAI,WAAW,KAAK,MAAM,EAAE;QAC1D,QAAQ,GAAG,QAAQ,CAAC,iBAAiB,CAAC,CAAC;KACxC;SAAM,IAAI,WAAW,KAAK,KAAK,EAAE;QAChC,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC;KACzB;SAAM,IACL,WAAW,KAAK,MAAM;QACtB,WAAW,KAAK,KAAK;QACrB,WAAW,KAAK,KAAK,EACrB;QACA,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC;KAC3B;SAAM,IAAI,WAAW,KAAK,SAAS,EAAE;QACpC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC;KAC7B;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/resources/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,iDAAiD,CAAC;AAChE,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uCAAuC,CAAC;AACtD,cAAc,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/resources/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAC;AAC7C,cAAc,iDAAiD,CAAC;AAChE,cAAc,6BAA6B,CAAC;AAC5C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,uCAAuC,CAAC;AACtD,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ItemType } from "../types";
|
|
2
|
+
import { Logger } from "../utils";
|
|
3
|
+
import { isUrl, isFeatureService, isService, pingFeatureService, pingUrl, detectDataTypeFromHeader, getFileName, detectDataTypeFromExtension, shouldHaveDataUrl, } from "./_internal/_validate-url-helpers";
|
|
4
|
+
/**
|
|
5
|
+
* Takes in a URL and validates it based on valid url, type of item, etc
|
|
6
|
+
*
|
|
7
|
+
* @export
|
|
8
|
+
* @param {string} url
|
|
9
|
+
* @return {*} {Promise<any>}
|
|
10
|
+
*/
|
|
11
|
+
export async function validateUrl(url) {
|
|
12
|
+
// If URL doesn't pass then exit out immediately.
|
|
13
|
+
if (!isUrl(url)) {
|
|
14
|
+
return {
|
|
15
|
+
pass: false,
|
|
16
|
+
error: "invalidFormat",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// Check if it's a FS, Map service, or image service
|
|
20
|
+
const isFeatureServiceUrl = isFeatureService(url);
|
|
21
|
+
const isServiceUrl = isService(url);
|
|
22
|
+
if (isServiceUrl && !isFeatureServiceUrl) {
|
|
23
|
+
return {
|
|
24
|
+
pass: false,
|
|
25
|
+
error: "invalidFormat",
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
// Content type which can be determined bby the url file extnesion or the request response header
|
|
29
|
+
let type;
|
|
30
|
+
let item;
|
|
31
|
+
let pingResult = {};
|
|
32
|
+
try {
|
|
33
|
+
pingResult = isFeatureServiceUrl
|
|
34
|
+
? await pingFeatureService(url)
|
|
35
|
+
: await pingUrl(url);
|
|
36
|
+
// return an error if the response is not okay
|
|
37
|
+
if (!pingResult.ok) {
|
|
38
|
+
return {
|
|
39
|
+
pass: false,
|
|
40
|
+
error: "invalidUrl",
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
// TODO: This is tricky. The fetch() API rejects when a network error
|
|
46
|
+
// happens. This error can be a CORS error, or a 404 error, or a timeout
|
|
47
|
+
// error. While an error like 404 does suggest a bad URL, the CORS occurs
|
|
48
|
+
// because this is a front-end request and the file is likely accessible by
|
|
49
|
+
// the server. Unfortunately, the error doesn't have any information about
|
|
50
|
+
// underline failure type. For now, the network failure is ignored, so the
|
|
51
|
+
// user can paste a URL from any domain and avoid the CORS issue.
|
|
52
|
+
Logger.error(`error requesting url`);
|
|
53
|
+
}
|
|
54
|
+
// Use the metadata from the ping response if exists, otherwise guess the file
|
|
55
|
+
// name from the URL
|
|
56
|
+
if (pingResult.item) {
|
|
57
|
+
item = pingResult.item;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
item = { title: getFileName(url), url };
|
|
61
|
+
}
|
|
62
|
+
if (pingResult.headers) {
|
|
63
|
+
type = detectDataTypeFromHeader(pingResult.headers);
|
|
64
|
+
}
|
|
65
|
+
if (isFeatureServiceUrl) {
|
|
66
|
+
type = ItemType["Feature Service"];
|
|
67
|
+
}
|
|
68
|
+
else if (!type) {
|
|
69
|
+
// Guess the data type from the extension
|
|
70
|
+
type = detectDataTypeFromExtension(url);
|
|
71
|
+
}
|
|
72
|
+
if (type) {
|
|
73
|
+
item.type = type;
|
|
74
|
+
}
|
|
75
|
+
if (item.type && shouldHaveDataUrl(item.type)) {
|
|
76
|
+
item.dataUrl = item.url;
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
pass: true,
|
|
80
|
+
// The type may or may not be true
|
|
81
|
+
type,
|
|
82
|
+
// fetched / calculated item
|
|
83
|
+
item,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=validate-url.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-url.js","sourceRoot":"","sources":["../../../src/resources/validate-url.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EACL,KAAK,EACL,gBAAgB,EAChB,SAAS,EACT,kBAAkB,EAClB,OAAO,EACP,wBAAwB,EACxB,WAAW,EACX,2BAA2B,EAC3B,iBAAiB,GAClB,MAAM,mCAAmC,CAAC;AAE3C;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAW;IAC3C,iDAAiD;IACjD,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;QACf,OAAO;YACL,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,eAAe;SACvB,CAAC;KACH;IAED,oDAAoD;IACpD,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;IAClD,MAAM,YAAY,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;IAEpC,IAAI,YAAY,IAAI,CAAC,mBAAmB,EAAE;QACxC,OAAO;YACL,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,eAAe;SACvB,CAAC;KACH;IAED,iGAAiG;IACjG,IAAI,IAAI,CAAC;IAET,IAAI,IAAI,CAAC;IAET,IAAI,UAAU,GAA4D,EAAE,CAAC;IAE7E,IAAI;QACF,UAAU,GAAG,mBAAmB;YAC9B,CAAC,CAAC,MAAM,kBAAkB,CAAC,GAAG,CAAC;YAC/B,CAAC,CAAC,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC;QAEvB,8CAA8C;QAC9C,IAAI,CAAC,UAAU,CAAC,EAAE,EAAE;YAClB,OAAO;gBACL,IAAI,EAAE,KAAK;gBACX,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