@elastic/ems-client 7.17.1 → 7.17.2

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 CHANGED
@@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file.
3
3
 
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
5
5
 
6
+ ## [7.17.2] - 2024-07-05
7
+
8
+ ## Changed
9
+
10
+ - Update dependencies #218 #219 #220 #221 #236 #237
11
+
6
12
  ## [7.17.1] - 2023-11-09
7
13
 
8
14
  ### Changed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elastic/ems-client",
3
- "version": "7.17.1",
3
+ "version": "7.17.2",
4
4
  "description": "JavaScript client library for the Elastic Maps Service",
5
5
  "main": "target/node/index.js",
6
6
  "browser": "target/web/index.js",
@@ -35,13 +35,13 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@types/geojson": "^7946.0.10",
38
+ "@types/geojson": "^7946.0.14",
39
39
  "@types/lru-cache": "^5.1.0",
40
- "@types/topojson-client": "^3.1.1",
41
- "@types/topojson-specification": "^1.0.1",
40
+ "@types/topojson-client": "^3.1.4",
41
+ "@types/topojson-specification": "^1.0.5",
42
42
  "lodash": "^4.17.15",
43
43
  "lru-cache": "^6.0.0",
44
- "semver": "7.5.4",
44
+ "semver": "7.6.2",
45
45
  "topojson-client": "^3.1.0"
46
46
  },
47
47
  "devDependencies": {
@@ -51,7 +51,7 @@
51
51
  "@babel/preset-env": "^7.5.5",
52
52
  "@babel/preset-typescript": "^7.9.0",
53
53
  "@types/jest": "^26.0.10",
54
- "@types/lodash": "^4.14.150",
54
+ "@types/lodash": "4.17.5",
55
55
  "@types/node": "^13.13.5",
56
56
  "@types/node-fetch": "^2.5.7",
57
57
  "@types/semver": "^7.1.0",
@@ -1,10 +1,10 @@
1
1
  import { TMSService } from './tms_service';
2
2
  import { EMSFormatType, FileLayer } from './file_layer';
3
3
  import { FeatureCollection } from 'geojson';
4
- declare type LocalizedStrings = {
4
+ type LocalizedStrings = {
5
5
  [key: string]: string;
6
6
  };
7
- declare type BaseClientConfig = {
7
+ type BaseClientConfig = {
8
8
  appName?: string;
9
9
  manifestServiceUrl?: string;
10
10
  tileApiUrl: string;
@@ -17,35 +17,35 @@ declare type BaseClientConfig = {
17
17
  proxyPath?: string;
18
18
  cacheSize?: number;
19
19
  };
20
- declare type DeprecatedClientConfig = BaseClientConfig & {
20
+ type DeprecatedClientConfig = BaseClientConfig & {
21
21
  kbnVersion: string;
22
22
  };
23
- declare type ClientConfig = BaseClientConfig & {
23
+ type ClientConfig = BaseClientConfig & {
24
24
  appVersion: string;
25
25
  };
26
- declare type EmsCatalogService = {
26
+ type EmsCatalogService = {
27
27
  id?: string;
28
28
  name?: string;
29
29
  manifest: string;
30
30
  type: string;
31
31
  };
32
- declare type EmsCatalogManifest = {
32
+ type EmsCatalogManifest = {
33
33
  version?: string;
34
34
  services: EmsCatalogService[];
35
35
  };
36
- declare type EmsTmsCatalog = {
36
+ type EmsTmsCatalog = {
37
37
  version?: string;
38
38
  services: TMSServiceConfig[];
39
39
  };
40
- export declare type EmsTmsFormat = {
40
+ export type EmsTmsFormat = {
41
41
  locale: string;
42
42
  format: string;
43
43
  url: string;
44
44
  };
45
- export declare type BaseEmsServiceConfig = {
45
+ export type BaseEmsServiceConfig = {
46
46
  attribution: EmsLayerAttribution[];
47
47
  };
48
- export declare type FileLayerConfig = BaseEmsServiceConfig & {
48
+ export type FileLayerConfig = BaseEmsServiceConfig & {
49
49
  layer_id: string;
50
50
  created_at: string;
51
51
  formats: (EmsFileLayerFormatGeoJson | EmsFileLayerFormatTopoJson)[];
@@ -53,7 +53,7 @@ export declare type FileLayerConfig = BaseEmsServiceConfig & {
53
53
  legacy_ids: string[];
54
54
  layer_name: LocalizedStrings;
55
55
  };
56
- export declare type FileLayerField = {
56
+ export type FileLayerField = {
57
57
  type: string;
58
58
  id: string;
59
59
  label: LocalizedStrings;
@@ -61,27 +61,27 @@ export declare type FileLayerField = {
61
61
  regex?: string;
62
62
  alias?: string[];
63
63
  };
64
- export declare type TMSServiceConfig = BaseEmsServiceConfig & {
64
+ export type TMSServiceConfig = BaseEmsServiceConfig & {
65
65
  id: string;
66
66
  name: {
67
67
  en: string;
68
68
  };
69
69
  formats: EmsTmsFormat[];
70
70
  };
71
- declare type EmsFileCatalog = {
71
+ type EmsFileCatalog = {
72
72
  version?: string;
73
73
  layers: FileLayerConfig[];
74
74
  };
75
- export declare type EmsLayerAttribution = {
75
+ export type EmsLayerAttribution = {
76
76
  label: LocalizedStrings;
77
77
  url: LocalizedStrings;
78
78
  };
79
- export declare type EmsFileLayerFormatGeoJson = {
79
+ export type EmsFileLayerFormatGeoJson = {
80
80
  type: EMSFormatType.geojson;
81
81
  url: string;
82
82
  legacy_default: boolean;
83
83
  };
84
- export declare type EmsFileLayerFormatTopoJson = {
84
+ export type EmsFileLayerFormatTopoJson = {
85
85
  type: EMSFormatType.topojson;
86
86
  url: string;
87
87
  legacy_default: boolean;
@@ -0,0 +1,318 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.EMSClient = exports.LATEST_API_URL_PATH = void 0;
16
+ /*
17
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
18
+ * or more contributor license agreements. Licensed under the Elastic License
19
+ * 2.0; you may not use this file except in compliance with the Elastic License
20
+ * 2.0.
21
+ */
22
+ const lodash_1 = __importDefault(require("lodash"));
23
+ const tms_service_1 = require("./tms_service");
24
+ const file_layer_1 = require("./file_layer");
25
+ const coerce_1 = __importDefault(require("semver/functions/coerce"));
26
+ const valid_1 = __importDefault(require("semver/functions/valid"));
27
+ const major_1 = __importDefault(require("semver/functions/major"));
28
+ const minor_1 = __importDefault(require("semver/functions/minor"));
29
+ const url_1 = require("url");
30
+ const utils_1 = require("./utils");
31
+ const lru_cache_1 = __importDefault(require("lru-cache"));
32
+ const REST_API_REGEX = /\d{4}-\d{2}-\d{2}/;
33
+ exports.LATEST_API_URL_PATH = 'latest';
34
+ /**
35
+ * plugins cannot have upstream dependencies on core/*-kibana.
36
+ * Work-around by copy-pasting modifyUrl routine here.
37
+ * @param url
38
+ * @param block
39
+ */
40
+ function modifyUrlLocal(url, block) {
41
+ const parsed = (0, url_1.parse)(url, true);
42
+ // copy over the most specific version of each
43
+ // property. By default, the parsed url includes
44
+ // several conflicting properties (like path and
45
+ // pathname + search, or search and query) and keeping
46
+ // track of which property is actually used when they
47
+ // are formatted is harder than necessary
48
+ const meaningfulParts = {
49
+ protocol: parsed.protocol,
50
+ slashes: parsed.slashes,
51
+ auth: parsed.auth,
52
+ hostname: parsed.hostname,
53
+ port: parsed.port,
54
+ pathname: parsed.pathname,
55
+ query: parsed.query || {},
56
+ hash: parsed.hash,
57
+ };
58
+ // the block modifies the meaningfulParts object, or returns a new one
59
+ const modifiedParts = block(meaningfulParts) || meaningfulParts;
60
+ // format the modified/replaced meaningfulParts back into a url
61
+ return (0, url_1.format)({
62
+ protocol: modifiedParts.protocol,
63
+ slashes: modifiedParts.slashes,
64
+ auth: modifiedParts.auth,
65
+ hostname: modifiedParts.hostname,
66
+ port: modifiedParts.port,
67
+ pathname: modifiedParts.pathname,
68
+ query: modifiedParts.query,
69
+ hash: modifiedParts.hash,
70
+ });
71
+ }
72
+ const extendUrl = (url, props) => modifyUrlLocal(url, (parsed) => lodash_1.default.merge(parsed, props));
73
+ /**
74
+ * Unescape a url template that was escaped by encodeURI() so leaflet
75
+ * will be able to correctly locate the variables in the template
76
+ * @param {String} url
77
+ * @return {String}
78
+ */
79
+ const unescapeTemplateVars = (url) => {
80
+ const ENCODED_TEMPLATE_VARS_RE = /%7B(\w+?)%7D/g;
81
+ return url.replace(ENCODED_TEMPLATE_VARS_RE, (total, varName) => `{${varName}}`);
82
+ };
83
+ //this is not the default locale from Kibana, but the default locale supported by the Elastic Maps Service
84
+ const DEFAULT_LANGUAGE = 'en';
85
+ class EMSClient {
86
+ constructor(config) {
87
+ this.EMS_LOAD_TIMEOUT = 32000;
88
+ this._isRestApi = false;
89
+ // Remove kbnVersion in 8.0
90
+ if ('kbnVersion' in config) {
91
+ console.warn('The "kbnVersion" parameter for ems-client is deprecated. Please use "appVersion" instead.');
92
+ this._appVersion = config.kbnVersion;
93
+ }
94
+ else {
95
+ this._appVersion = config.appVersion;
96
+ }
97
+ this._queryParams = {
98
+ elastic_tile_service_tos: 'agree',
99
+ my_app_name: config.appName || 'kibana',
100
+ my_app_version: this._appVersion,
101
+ };
102
+ this._sanitizer = config.htmlSanitizer ? config.htmlSanitizer : (x) => x;
103
+ this._tileApiUrl = config.tileApiUrl;
104
+ this._fileApiUrl = config.fileApiUrl;
105
+ this._emsVersion = this._getEmsVersion(config.emsVersion);
106
+ this._isRestApi = this._emsVersion == config.emsVersion;
107
+ this._emsLandingPageUrl = config.landingPageUrl || '';
108
+ this._language = config.language || DEFAULT_LANGUAGE;
109
+ this._fetchFunction = config.fetchFunction;
110
+ this._proxyPath = config.proxyPath || '';
111
+ this._cache = new lru_cache_1.default({
112
+ max: config.cacheSize || 10,
113
+ });
114
+ this._invalidateSettings();
115
+ }
116
+ getDefaultLocale() {
117
+ return DEFAULT_LANGUAGE;
118
+ }
119
+ getLocale() {
120
+ return this._language;
121
+ }
122
+ getValueInLanguage(i18nObject) {
123
+ if (!i18nObject) {
124
+ return '';
125
+ }
126
+ return i18nObject[this._language] ? i18nObject[this._language] : i18nObject[DEFAULT_LANGUAGE];
127
+ }
128
+ /**
129
+ * this internal method is overridden by the tests to simulate custom manifest.
130
+ */
131
+ getManifest(endpointUrl) {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ try {
134
+ const url = extendUrl(endpointUrl, { query: this._queryParams });
135
+ const response = yield this._fetchWithTimeout(url);
136
+ if (!response.ok) {
137
+ throw new Error(response.statusText);
138
+ }
139
+ if (response) {
140
+ return (yield response.json());
141
+ }
142
+ else {
143
+ throw new Error('Response not found');
144
+ }
145
+ }
146
+ catch (e) {
147
+ if (e instanceof Error) {
148
+ throw e;
149
+ }
150
+ else {
151
+ throw new Error('Unknown error');
152
+ }
153
+ }
154
+ });
155
+ }
156
+ /**
157
+ * Add optional query-parameters to all requests
158
+ *
159
+ * @param additionalQueryParams
160
+ */
161
+ addQueryParams(additionalQueryParams) {
162
+ for (const key in additionalQueryParams) {
163
+ if (Object.prototype.hasOwnProperty.call(additionalQueryParams, key)) {
164
+ if (additionalQueryParams[key] !== this._queryParams[key]) {
165
+ //changes detected.
166
+ this._queryParams = lodash_1.default.assign({}, this._queryParams, additionalQueryParams);
167
+ this._invalidateSettings();
168
+ break;
169
+ }
170
+ }
171
+ }
172
+ }
173
+ getMainManifest() {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ return yield this._getMainCatalog();
176
+ });
177
+ }
178
+ getDefaultFileManifest() {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ return yield this._getDefaultFileCatalog();
181
+ });
182
+ }
183
+ getDefaultTMSManifest() {
184
+ return __awaiter(this, void 0, void 0, function* () {
185
+ return yield this._getDefaultTMSCatalog();
186
+ });
187
+ }
188
+ getFileLayers() {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ return yield this._loadFileLayers();
191
+ });
192
+ }
193
+ getTMSServices() {
194
+ return __awaiter(this, void 0, void 0, function* () {
195
+ return yield this._loadTMSServices();
196
+ });
197
+ }
198
+ cacheGeoJson(layerId, geoJson) {
199
+ this._cache.set(layerId, geoJson);
200
+ return;
201
+ }
202
+ getCachedGeoJson(layerId) {
203
+ return this._cache.get(layerId);
204
+ }
205
+ getEmsVersion() {
206
+ return this._emsVersion;
207
+ }
208
+ getTileApiUrl() {
209
+ return this._tileApiUrl;
210
+ }
211
+ getFileApiUrl() {
212
+ return this._fileApiUrl;
213
+ }
214
+ getLandingPageUrl() {
215
+ return this._emsLandingPageUrl;
216
+ }
217
+ sanitizeHtml(html) {
218
+ return this._sanitizer(html);
219
+ }
220
+ extendUrlWithParams(url) {
221
+ return unescapeTemplateVars(extendUrl(url, {
222
+ query: this._queryParams,
223
+ }));
224
+ }
225
+ findFileLayerById(id) {
226
+ return __awaiter(this, void 0, void 0, function* () {
227
+ const fileLayers = yield this.getFileLayers();
228
+ for (let i = 0; i < fileLayers.length; i++) {
229
+ if (fileLayers[i].hasId(id)) {
230
+ return fileLayers[i];
231
+ }
232
+ }
233
+ });
234
+ }
235
+ findTMSServiceById(id) {
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ const tmsServices = yield this.getTMSServices();
238
+ for (let i = 0; i < tmsServices.length; i++) {
239
+ if (tmsServices[i].hasId(id)) {
240
+ return tmsServices[i];
241
+ }
242
+ }
243
+ });
244
+ }
245
+ _getEmsVersion(version) {
246
+ if (version === null || version === void 0 ? void 0 : version.match(REST_API_REGEX)) {
247
+ return version;
248
+ }
249
+ const semverVersion = (0, valid_1.default)((0, coerce_1.default)(version));
250
+ if (semverVersion) {
251
+ return `v${(0, major_1.default)(semverVersion)}.${(0, minor_1.default)(semverVersion)}`;
252
+ }
253
+ else {
254
+ throw new Error(`Invalid version: ${version}`);
255
+ }
256
+ }
257
+ _fetchWithTimeout(url) {
258
+ return new Promise((resolve, reject) => {
259
+ const timer = setTimeout(() => reject(new Error(`Request to ${url} timed out`)), this.EMS_LOAD_TIMEOUT);
260
+ this._fetchFunction(url).then((response) => {
261
+ clearTimeout(timer);
262
+ resolve(response);
263
+ }, (err) => {
264
+ clearTimeout(timer);
265
+ reject(err);
266
+ });
267
+ });
268
+ }
269
+ _invalidateSettings() {
270
+ this._cache.reset();
271
+ this._getMainCatalog = lodash_1.default.once(() => __awaiter(this, void 0, void 0, function* () {
272
+ const services = [];
273
+ if (this._tileApiUrl) {
274
+ const version = this._isRestApi ? exports.LATEST_API_URL_PATH : this._emsVersion;
275
+ services.push({
276
+ type: 'tms',
277
+ manifest: (0, utils_1.toAbsoluteUrl)(this._tileApiUrl, `${version}/manifest`),
278
+ });
279
+ }
280
+ if (this._fileApiUrl) {
281
+ const version = this._isRestApi ? exports.LATEST_API_URL_PATH : this._emsVersion;
282
+ services.push({
283
+ type: 'file',
284
+ manifest: (0, utils_1.toAbsoluteUrl)(this._fileApiUrl, `${version}/manifest`),
285
+ });
286
+ }
287
+ return { services: services };
288
+ }));
289
+ this._getDefaultTMSCatalog = lodash_1.default.once(() => __awaiter(this, void 0, void 0, function* () {
290
+ const catalogue = yield this._getMainCatalog();
291
+ const firstService = catalogue.services.find((service) => service.type === 'tms');
292
+ if (!firstService) {
293
+ return { services: [] };
294
+ }
295
+ const url = this._proxyPath + firstService.manifest;
296
+ return yield this.getManifest(url);
297
+ }));
298
+ this._getDefaultFileCatalog = lodash_1.default.once(() => __awaiter(this, void 0, void 0, function* () {
299
+ const catalogue = yield this._getMainCatalog();
300
+ const firstService = catalogue.services.find((service) => service.type === 'file');
301
+ if (!firstService) {
302
+ return { layers: [] };
303
+ }
304
+ const url = this._proxyPath + firstService.manifest;
305
+ return yield this.getManifest(url);
306
+ }));
307
+ //Cache the actual instances of TMSService as these in turn cache sub-manifests for the style-files
308
+ this._loadTMSServices = lodash_1.default.once(() => __awaiter(this, void 0, void 0, function* () {
309
+ const tmsManifest = yield this._getDefaultTMSCatalog();
310
+ return tmsManifest.services.map((serviceConfig) => new tms_service_1.TMSService(serviceConfig, this, this._proxyPath));
311
+ }));
312
+ this._loadFileLayers = lodash_1.default.once(() => __awaiter(this, void 0, void 0, function* () {
313
+ const fileManifest = yield this._getDefaultFileCatalog();
314
+ return fileManifest.layers.map((layerConfig) => new file_layer_1.FileLayer(layerConfig, this, this._proxyPath));
315
+ }));
316
+ }
317
+ }
318
+ exports.EMSClient = EMSClient;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
4
+ * or more contributor license agreements. Licensed under the Elastic License
5
+ * 2.0; you may not use this file except in compliance with the Elastic License
6
+ * 2.0.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AbstractEmsService = void 0;
10
+ const utils_1 = require("./utils");
11
+ class AbstractEmsService {
12
+ constructor(config, emsClient, proxyPath) {
13
+ /**
14
+ * Checks if url is absolute. If not, prepend the basePath.
15
+ */
16
+ this._getAbsoluteUrl = (url) => {
17
+ if (/^https?:\/\//.test(url)) {
18
+ return url;
19
+ }
20
+ else {
21
+ return (0, utils_1.toAbsoluteUrl)(this.getApiUrl(), url);
22
+ }
23
+ };
24
+ this._config = config;
25
+ this._emsClient = emsClient;
26
+ this._proxyPath = proxyPath;
27
+ }
28
+ getAttributions() {
29
+ return this._config.attribution.map((attribution) => {
30
+ const url = this._emsClient.getValueInLanguage(attribution.url);
31
+ const label = this._emsClient.getValueInLanguage(attribution.label);
32
+ return {
33
+ url: url,
34
+ label: label,
35
+ };
36
+ });
37
+ }
38
+ getMarkdownAttribution() {
39
+ const attributions = this._config.attribution.map((attribution) => {
40
+ const url = this._emsClient.getValueInLanguage(attribution.url);
41
+ const label = this._emsClient.getValueInLanguage(attribution.label);
42
+ return `[${label}](${url})`;
43
+ });
44
+ return attributions.join('|');
45
+ }
46
+ }
47
+ exports.AbstractEmsService = AbstractEmsService;
@@ -5,7 +5,7 @@ export declare enum EMSFormatType {
5
5
  geojson = "geojson",
6
6
  topojson = "topojson"
7
7
  }
8
- export declare type EMSFormatTypeStrings = keyof typeof EMSFormatType;
8
+ export type EMSFormatTypeStrings = keyof typeof EMSFormatType;
9
9
  export declare class FileLayer extends AbstractEmsService {
10
10
  protected readonly _config: FileLayerConfig;
11
11
  constructor(config: FileLayerConfig, emsClient: EMSClient, proxyPath: string);