@elastic/ems-client 8.3.3 → 8.4.1

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.
@@ -4,94 +4,71 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.TMSService = void 0;
7
-
8
7
  var _lodash = _interopRequireDefault(require("lodash"));
9
-
10
8
  var _ems_service = require("./ems_service");
11
-
12
9
  var _utils = require("./utils");
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
11
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
13
+ function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /*
14
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
15
+ * or more contributor license agreements. Licensed under the Elastic License
16
+ * 2.0; you may not use this file except in compliance with the Elastic License
17
+ * 2.0.
18
+ */
18
19
  class TMSService extends _ems_service.AbstractEmsService {
19
- /*
20
- List of supported languages with labels and OMT codes extracted
21
- from https://openmaptiles.org/languages
22
- Keys are values for i18.locale from Kibana settings
23
- and OMT codes for the rest.
24
- */
25
-
26
- /*
27
- Suggested default operations for the different EMS styles
28
- */
29
20
  constructor(config, emsClient, proxyPath) {
30
21
  super(config, emsClient, proxyPath);
31
-
32
22
  _defineProperty(this, "_config", void 0);
33
-
34
23
  _defineProperty(this, "_getRasterStyleJson", _lodash.default.once(async () => {
35
24
  const rasterUrl = this._getStyleUrlForLocale('raster');
36
-
37
25
  if (rasterUrl) {
38
26
  const url = this._proxyPath + this._getAbsoluteUrl(rasterUrl);
39
-
40
27
  return this._emsClient.getManifest(this._emsClient.extendUrlWithParams(url));
41
28
  } else {
42
29
  return;
43
30
  }
44
31
  }));
45
-
46
32
  _defineProperty(this, "_getVectorStyleJsonRaw", _lodash.default.once(async () => {
47
33
  const vectorUrl = this._getStyleUrlForLocale('vector');
48
-
49
34
  if (vectorUrl) {
50
35
  const url = this._proxyPath + this._getAbsoluteUrl(vectorUrl);
51
-
52
36
  const vectorJson = await this._emsClient.getManifest(this._emsClient.extendUrlWithParams(url));
53
- return { ...vectorJson
37
+ return {
38
+ ...vectorJson
54
39
  };
55
40
  } else {
56
41
  return;
57
42
  }
58
43
  }));
59
-
60
44
  _defineProperty(this, "_getVectorStyleJsonInlined", _lodash.default.once(async () => {
61
45
  const vectorJson = await this._getVectorStyleJsonRaw();
62
-
63
46
  if (vectorJson) {
64
47
  const inlinedSources = {};
65
48
  const {
66
49
  sources
67
50
  } = vectorJson;
68
-
69
51
  for (const sourceName of Object.getOwnPropertyNames(sources)) {
70
52
  const {
71
53
  url
72
54
  } = sources[sourceName];
73
-
74
55
  if (url) {
75
- var _sourceJson$tiles;
76
-
77
56
  const sourceUrl = this._proxyPath + this._getAbsoluteUrl(url);
78
-
79
57
  const extendedUrl = this._emsClient.extendUrlWithParams(sourceUrl);
80
-
81
58
  const sourceJson = await this._emsClient.getManifest(extendedUrl);
82
- const tiles = sourceJson === null || sourceJson === void 0 ? void 0 : (_sourceJson$tiles = sourceJson.tiles) === null || _sourceJson$tiles === void 0 ? void 0 : _sourceJson$tiles.map(tileUrl => {
59
+ const tiles = sourceJson?.tiles?.map(tileUrl => {
83
60
  const directUrl = this._proxyPath + this._getAbsoluteUrl(tileUrl);
84
-
85
61
  return this._emsClient.extendUrlWithParams(directUrl);
86
62
  });
87
- inlinedSources[sourceName] = { ...sourceJson,
63
+ inlinedSources[sourceName] = {
64
+ ...sourceJson,
88
65
  type: 'vector',
89
66
  tiles
90
67
  };
91
68
  }
92
69
  }
93
-
94
- return { ...vectorJson,
70
+ return {
71
+ ...vectorJson,
95
72
  sources: inlinedSources,
96
73
  sprite: await this._getSpriteSheetRootPath(),
97
74
  glyphs: await this._getUrlTemplateForGlyphs()
@@ -100,43 +77,35 @@ class TMSService extends _ems_service.AbstractEmsService {
100
77
  return;
101
78
  }
102
79
  }));
103
-
104
80
  this._config = config;
105
81
  }
82
+
106
83
  /*
107
84
  This method returns an array of objects with the layers and the new
108
85
  layout['text-field'] to apply using map.setLayoutProperty
109
86
  */
110
-
111
-
112
87
  static transformLanguageProperty(layer, lang) {
113
88
  const supportedLang = this.SupportedLanguages.find(l => l.key === lang);
114
-
115
89
  if (layer.type === 'symbol' && layer.layout !== undefined && supportedLang !== undefined) {
116
90
  const omtLang = supportedLang.omt;
117
91
  const textField = layer.layout['text-field'];
118
-
119
92
  if (textField && typeof textField === 'string') {
120
93
  return TMSService._getTextField(textField, omtLang);
121
94
  }
122
95
  }
123
-
124
96
  return;
125
97
  }
98
+
126
99
  /*
127
100
  This method returns an array of objects per layer, containing a list of
128
101
  properties with new colors to apply using map.setPaintProperty
129
102
  */
130
-
131
-
132
103
  static transformColorProperties(layer, color, operation, percentage) {
133
104
  if (['background', 'fill', 'line', 'symbol'].indexOf(layer.type) !== -1 && layer.paint) {
134
105
  const paint = layer.paint;
135
-
136
106
  if (layer.type === 'symbol' && Object.keys(paint).length === 0) {
137
107
  paint['text-color'] = 'rgb(0,0,0)';
138
108
  }
139
-
140
109
  const types = Object.keys(paint).filter(key => {
141
110
  return ['background-color', 'circle-color', 'circle-stroke-color', 'fill-color', 'fill-extrusion-color', 'fill-outline-color', 'icon-color', 'icon-halo-color', 'line-color', 'text-color', 'text-halo-color'].indexOf(key) !== -1;
142
111
  });
@@ -151,29 +120,26 @@ class TMSService extends _ems_service.AbstractEmsService {
151
120
  return [];
152
121
  }
153
122
  }
154
-
155
123
  static _getTextField(label, lang) {
156
124
  // Capture {name:xx} labels
157
125
  const labelMatch = label.match(/\{name([:_])(.{2})\}/);
158
-
159
126
  if (labelMatch && labelMatch[1] != lang) {
160
127
  // Only apply if the languages are different
161
128
  return ['coalesce', ['get', `name:${lang}`], ['get', `name${labelMatch[1]}${labelMatch[2]}`]];
162
129
  } else if (label.includes('latin') && label.includes('nonlatin')) {
163
130
  // Capture the common pattern {name:latin}\n{name:nonlatin}
164
131
  return ['coalesce', ['get', `name:${lang}`], ['concat', ['get', 'name:latin'], '\n', ['get', 'name:nonlatin']]];
165
- } // If no case is found, return the input label
166
-
132
+ }
167
133
 
134
+ // If no case is found, return the input label
168
135
  return label;
169
136
  }
170
-
171
137
  async getDefaultRasterStyle() {
172
138
  const tileJson = await this._getRasterStyleJson();
173
-
174
139
  if (tileJson) {
175
140
  const tiles = tileJson.tiles.map(tile => this._proxyPath + this._getAbsoluteUrl(tile));
176
- return { ...tileJson,
141
+ return {
142
+ ...tileJson,
177
143
  ...{
178
144
  tiles
179
145
  }
@@ -182,49 +148,36 @@ class TMSService extends _ems_service.AbstractEmsService {
182
148
  return;
183
149
  }
184
150
  }
185
-
186
151
  async getUrlTemplate() {
187
152
  const tileJson = await this._getRasterStyleJson();
188
-
189
153
  if (tileJson) {
190
154
  const directUrl = this._proxyPath + this._getAbsoluteUrl(tileJson.tiles[0]);
191
-
192
155
  return this._emsClient.extendUrlWithParams(directUrl);
193
156
  } else {
194
157
  return '';
195
158
  }
196
159
  }
197
-
198
160
  async getUrlTemplateForVector(sourceId) {
199
- var _tileJson$sources$sou, _tileJson$sources$sou2;
200
-
201
161
  const tileJson = await this._getVectorStyleJsonInlined();
202
- const url = tileJson === null || tileJson === void 0 ? void 0 : (_tileJson$sources$sou = tileJson.sources[sourceId]) === null || _tileJson$sources$sou === void 0 ? void 0 : (_tileJson$sources$sou2 = _tileJson$sources$sou.tiles) === null || _tileJson$sources$sou2 === void 0 ? void 0 : _tileJson$sources$sou2.pop();
203
-
162
+ const url = tileJson?.sources[sourceId]?.tiles?.pop();
204
163
  if (url) {
205
164
  const directUrl = this._proxyPath + this._getAbsoluteUrl(url);
206
-
207
165
  return this._emsClient.extendUrlWithParams(directUrl);
208
166
  } else {
209
167
  return '';
210
168
  }
211
169
  }
212
-
213
170
  async getVectorStyleSheet() {
214
171
  return await this._getVectorStyleJsonInlined();
215
172
  }
216
-
217
173
  async getVectorStyleSheetRaw() {
218
174
  return await this._getVectorStyleJsonRaw();
219
175
  }
220
-
221
176
  async getSpriteSheetMeta(isRetina = false) {
222
177
  const metaUrl = await this.getSpriteSheetJsonPath(isRetina);
223
178
  const spritePngs = await this.getSpriteSheetPngPath(isRetina);
224
-
225
179
  if (metaUrl && spritePngs) {
226
180
  const metaUrlExtended = this._emsClient.extendUrlWithParams(metaUrl);
227
-
228
181
  const jsonMeta = await this._emsClient.getManifest(metaUrlExtended);
229
182
  return {
230
183
  png: spritePngs,
@@ -234,10 +187,8 @@ class TMSService extends _ems_service.AbstractEmsService {
234
187
  return;
235
188
  }
236
189
  }
237
-
238
190
  async getSpriteSheetJsonPath(isRetina = false) {
239
191
  const spriteSheetRootPath = await this._getSpriteSheetRootPath();
240
-
241
192
  if (spriteSheetRootPath) {
242
193
  const suffix = isRetina ? '@2x' : '';
243
194
  return spriteSheetRootPath + suffix + '.json';
@@ -245,10 +196,8 @@ class TMSService extends _ems_service.AbstractEmsService {
245
196
  return '';
246
197
  }
247
198
  }
248
-
249
199
  async getSpriteSheetPngPath(isRetina = false) {
250
200
  const spriteSheetRootPath = await this._getSpriteSheetRootPath();
251
-
252
201
  if (spriteSheetRootPath) {
253
202
  const suffix = isRetina ? '@2x' : '';
254
203
  return spriteSheetRootPath + suffix + '.png';
@@ -256,11 +205,9 @@ class TMSService extends _ems_service.AbstractEmsService {
256
205
  return '';
257
206
  }
258
207
  }
259
-
260
208
  getDisplayName() {
261
209
  return this._emsClient.getValueInLanguage(this._config.name);
262
210
  }
263
-
264
211
  async getMinZoom(format = 'vector') {
265
212
  switch (format) {
266
213
  case 'vector':
@@ -272,18 +219,15 @@ class TMSService extends _ems_service.AbstractEmsService {
272
219
  return Math.min(...Object.values(sources).map(({
273
220
  minzoom
274
221
  }) => minzoom).filter(minzoom => Number.isFinite(minzoom)));
275
-
276
222
  case 'raster':
277
223
  const {
278
224
  minzoom
279
225
  } = (await this._getRasterStyleJson()) || {};
280
226
  return minzoom;
281
-
282
227
  default:
283
228
  return;
284
229
  }
285
230
  }
286
-
287
231
  async getMaxZoom(format = 'vector') {
288
232
  switch (format) {
289
233
  case 'vector':
@@ -295,78 +239,66 @@ class TMSService extends _ems_service.AbstractEmsService {
295
239
  return Math.max(...Object.values(sources).map(({
296
240
  maxzoom
297
241
  }) => maxzoom).filter(maxzoom => Number.isFinite(maxzoom)));
298
-
299
242
  case 'raster':
300
243
  const {
301
244
  maxzoom
302
245
  } = (await this._getRasterStyleJson()) || {};
303
246
  return maxzoom;
304
-
305
247
  default:
306
248
  return;
307
249
  }
308
250
  }
309
-
310
251
  getId() {
311
252
  return this._config.id;
312
253
  }
313
-
314
254
  hasId(id) {
315
255
  return this._config.id === id;
316
256
  }
317
-
318
257
  getApiUrl() {
319
258
  return this._emsClient.getTileApiUrl();
320
259
  }
321
-
322
260
  _getStyleUrlForLocale(formatType) {
323
261
  let vectorFormats = this._getFormats(formatType, this._emsClient.getLocale());
324
-
325
262
  if (!vectorFormats.length) {
326
263
  //fallback to default locale
327
264
  vectorFormats = this._getFormats(formatType, this._emsClient.getDefaultLocale());
328
265
  }
329
-
330
266
  if (!vectorFormats.length) {
331
267
  // eslint-disable-next-line max-len
332
268
  throw new Error(`Cannot find ${formatType} tile layer for locale ${this._emsClient.getLocale()} or ${this._emsClient.getDefaultLocale()}`);
333
269
  }
334
-
335
270
  const defaultStyle = vectorFormats[0];
336
-
337
271
  if (defaultStyle && defaultStyle.hasOwnProperty('url')) {
338
272
  return defaultStyle.url;
339
273
  }
340
274
  }
341
-
342
275
  _getFormats(formatType, locale) {
343
276
  return this._config.formats.filter(format => format.locale === locale && format.format === formatType);
344
277
  }
345
-
346
278
  async _getSpriteSheetRootPath() {
347
279
  const vectorStyleJson = await this._getVectorStyleJsonRaw();
348
-
349
- if (vectorStyleJson !== null && vectorStyleJson !== void 0 && vectorStyleJson.sprite) {
280
+ if (vectorStyleJson?.sprite) {
350
281
  return this._proxyPath + this._getAbsoluteUrl(vectorStyleJson.sprite);
351
282
  } else {
352
283
  return '';
353
284
  }
354
285
  }
355
-
356
286
  async _getUrlTemplateForGlyphs() {
357
287
  const vectorStyleJson = await this._getVectorStyleJsonRaw();
358
-
359
- if (vectorStyleJson !== null && vectorStyleJson !== void 0 && vectorStyleJson.glyphs) {
288
+ if (vectorStyleJson?.glyphs) {
360
289
  return this._proxyPath + this._getAbsoluteUrl(vectorStyleJson.glyphs);
361
290
  } else {
362
291
  return '';
363
292
  }
364
293
  }
365
-
366
294
  }
367
-
368
295
  exports.TMSService = TMSService;
369
-
296
+ /*
297
+ List of supported languages with labels and OMT codes extracted
298
+ from https://openmaptiles.org/languages
299
+ Keys are values for i18.locale from Kibana settings
300
+ and OMT codes for the rest.
301
+ */
370
302
  _defineProperty(TMSService, "SupportedLanguages", [{
371
303
  key: 'ar',
372
304
  omt: 'ar'
@@ -545,7 +477,9 @@ _defineProperty(TMSService, "SupportedLanguages", [{
545
477
  key: 'zh-cn',
546
478
  omt: 'zh'
547
479
  }]);
548
-
480
+ /*
481
+ Suggested default operations for the different EMS styles
482
+ */
549
483
  _defineProperty(TMSService, "colorOperationDefaults", [{
550
484
  style: 'road_map',
551
485
  operation: 'mix',
@@ -6,11 +6,8 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.colorizeColor = colorizeColor;
7
7
  exports.desaturateColor = desaturateColor;
8
8
  exports.toAbsoluteUrl = toAbsoluteUrl;
9
-
10
9
  var _chromaJs = _interopRequireDefault(require("chroma-js"));
11
-
12
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
11
  /*
15
12
  * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
16
13
  * or more contributor license agreements. Licensed under the Elastic License
@@ -27,10 +24,8 @@ function toAbsoluteUrl(host, path) {
27
24
  if (!host) {
28
25
  return path;
29
26
  }
30
-
31
27
  const hostEndWithSlash = host[host.length - 1] === '/';
32
28
  const pathStartsWithSlash = path[0] === '/';
33
-
34
29
  if (hostEndWithSlash === true && pathStartsWithSlash === true) {
35
30
  return host + path.slice(1);
36
31
  } else if (hostEndWithSlash !== pathStartsWithSlash) {
@@ -39,10 +34,15 @@ function toAbsoluteUrl(host, path) {
39
34
  return host + '/' + path;
40
35
  }
41
36
  }
37
+
42
38
  /*
43
39
  Type with the possible ways to define a color in maplibre
44
40
  */
45
41
 
42
+ /*
43
+ A selection of the different maplibre-gl-js properties that refer to a color.
44
+ Taken from maplibre https://github.com/maplibre/maplibre-gl-js/blob/main/src/style-spec/types.g.ts
45
+ */
46
46
 
47
47
  /*
48
48
  Function to transform a maplibre color definition by a given function.
@@ -51,7 +51,7 @@ function transformColor(paintColor, func) {
51
51
  if (typeof paintColor == 'string') {
52
52
  const modifiedColor = func(paintColor);
53
53
  return `rgba(${modifiedColor.rgba().join(',')})`;
54
- } else if (typeof paintColor == 'object' && 'stops' in paintColor && Array.isArray(paintColor === null || paintColor === void 0 ? void 0 : paintColor.stops)) {
54
+ } else if (typeof paintColor == 'object' && 'stops' in paintColor && Array.isArray(paintColor?.stops)) {
55
55
  const stops = paintColor['stops'].map(stop => {
56
56
  const newColor = transformColor(stop[1], func);
57
57
  return [stop[0], newColor];
@@ -62,16 +62,16 @@ function transformColor(paintColor, func) {
62
62
  return newPaintColor;
63
63
  } else return paintColor;
64
64
  }
65
+
65
66
  /*
66
67
  This is the function used to generate the current EMS desaturated roadmap
67
68
  */
68
-
69
-
70
69
  function desaturateColor(paintColor) {
71
70
  return transformColor(paintColor, color => {
72
71
  return (0, _chromaJs.default)(color).desaturate(1.1).brighten(0.33);
73
72
  });
74
73
  }
74
+
75
75
  /*
76
76
  Blends an original maplibre color definition with a destination
77
77
  color. Accepts different blending modes and an additional `mix`
@@ -82,8 +82,6 @@ More details:
82
82
  https://gka.github.io/chroma.js/#chroma-blend
83
83
  https://gka.github.io/chroma.js/#chroma-mix
84
84
  */
85
-
86
-
87
85
  function colorizeColor(sourceColor, destColor, operation = 'screen', percentage = 0.5) {
88
86
  return transformColor(sourceColor, color => {
89
87
  if (operation !== 'mix') {
@@ -38,7 +38,7 @@ export declare class TMSService extends AbstractEmsService {
38
38
  }[];
39
39
  static colorOperationDefaults: {
40
40
  style: string;
41
- operation: string;
41
+ operation: blendMode;
42
42
  percentage: number;
43
43
  }[];
44
44
  protected readonly _config: TMSServiceConfig;