@basemaps/lambda-tiler 6.34.0 → 6.35.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 (124) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/build/__tests__/config.data.d.ts +2 -1
  3. package/build/__tests__/config.data.d.ts.map +1 -1
  4. package/build/__tests__/config.data.js +14 -1
  5. package/build/__tests__/config.data.js.map +1 -1
  6. package/build/__tests__/tile.style.json.test.js +17 -3
  7. package/build/__tests__/tile.style.json.test.js.map +1 -1
  8. package/build/__tests__/wmts.capability.test.js +17 -0
  9. package/build/__tests__/wmts.capability.test.js.map +1 -1
  10. package/build/arcgis/__tests__/arcgis.style.json.test.js +18 -15
  11. package/build/arcgis/__tests__/arcgis.style.json.test.js.map +1 -1
  12. package/build/arcgis/__tests__/vector.tiler.server.test.js +14 -7
  13. package/build/arcgis/__tests__/vector.tiler.server.test.js.map +1 -1
  14. package/build/arcgis/arcgis.style.json.d.ts.map +1 -1
  15. package/build/arcgis/arcgis.style.json.js +6 -4
  16. package/build/arcgis/arcgis.style.json.js.map +1 -1
  17. package/build/arcgis/vector.tile.server.d.ts.map +1 -1
  18. package/build/arcgis/vector.tile.server.js +4 -2
  19. package/build/arcgis/vector.tile.server.js.map +1 -1
  20. package/build/index.d.ts +1 -3
  21. package/build/index.d.ts.map +1 -1
  22. package/build/index.js +7 -8
  23. package/build/index.js.map +1 -1
  24. package/build/routes/__tests__/attribution.test.js +3 -2
  25. package/build/routes/__tests__/attribution.test.js.map +1 -1
  26. package/build/routes/__tests__/fonts.test.js +36 -7
  27. package/build/routes/__tests__/fonts.test.js.map +1 -1
  28. package/build/routes/__tests__/health.test.js +4 -2
  29. package/build/routes/__tests__/health.test.js.map +1 -1
  30. package/build/routes/__tests__/sprites.test.js +3 -4
  31. package/build/routes/__tests__/sprites.test.js.map +1 -1
  32. package/build/routes/__tests__/tile.json.test.js +24 -2
  33. package/build/routes/__tests__/tile.json.test.js.map +1 -1
  34. package/build/routes/__tests__/tile.style.json.test.js +58 -7
  35. package/build/routes/__tests__/tile.style.json.test.js.map +1 -1
  36. package/build/routes/__tests__/wmts.test.js +17 -14
  37. package/build/routes/__tests__/wmts.test.js.map +1 -1
  38. package/build/routes/__tests__/xyz.test.js +4 -2
  39. package/build/routes/__tests__/xyz.test.js.map +1 -1
  40. package/build/routes/attribution.d.ts.map +1 -1
  41. package/build/routes/attribution.js +20 -10
  42. package/build/routes/attribution.js.map +1 -1
  43. package/build/routes/config.d.ts +22 -0
  44. package/build/routes/config.d.ts.map +1 -0
  45. package/build/routes/config.js +63 -0
  46. package/build/routes/config.js.map +1 -0
  47. package/build/routes/health.d.ts.map +1 -1
  48. package/build/routes/health.js +3 -2
  49. package/build/routes/health.js.map +1 -1
  50. package/build/routes/imagery.d.ts.map +1 -1
  51. package/build/routes/imagery.js +3 -2
  52. package/build/routes/imagery.js.map +1 -1
  53. package/build/routes/tile.json.d.ts.map +1 -1
  54. package/build/routes/tile.json.js +7 -4
  55. package/build/routes/tile.json.js.map +1 -1
  56. package/build/routes/tile.style.json.d.ts +4 -3
  57. package/build/routes/tile.style.json.d.ts.map +1 -1
  58. package/build/routes/tile.style.json.js +53 -12
  59. package/build/routes/tile.style.json.js.map +1 -1
  60. package/build/routes/tile.wmts.d.ts.map +1 -1
  61. package/build/routes/tile.wmts.js +7 -4
  62. package/build/routes/tile.wmts.js.map +1 -1
  63. package/build/routes/tile.xyz.d.ts.map +1 -1
  64. package/build/routes/tile.xyz.js +4 -2
  65. package/build/routes/tile.xyz.js.map +1 -1
  66. package/build/routes/tile.xyz.raster.d.ts.map +1 -1
  67. package/build/routes/tile.xyz.raster.js +4 -2
  68. package/build/routes/tile.xyz.raster.js.map +1 -1
  69. package/build/util/__test__/config.loader.test.d.ts +2 -0
  70. package/build/util/__test__/config.loader.test.d.ts.map +1 -0
  71. package/build/util/__test__/config.loader.test.js +79 -0
  72. package/build/util/__test__/config.loader.test.js.map +1 -0
  73. package/build/util/assets.provider.d.ts +1 -4
  74. package/build/util/assets.provider.d.ts.map +1 -1
  75. package/build/util/assets.provider.js +23 -10
  76. package/build/util/assets.provider.js.map +1 -1
  77. package/build/util/config.cache.d.ts +3 -3
  78. package/build/util/config.cache.d.ts.map +1 -1
  79. package/build/util/config.cache.js +14 -15
  80. package/build/util/config.cache.js.map +1 -1
  81. package/build/util/config.loader.d.ts +10 -0
  82. package/build/util/config.loader.d.ts.map +1 -0
  83. package/build/util/config.loader.js +47 -0
  84. package/build/util/config.loader.js.map +1 -0
  85. package/build/wmts.capability.d.ts +3 -1
  86. package/build/wmts.capability.d.ts.map +1 -1
  87. package/build/wmts.capability.js +5 -15
  88. package/build/wmts.capability.js.map +1 -1
  89. package/dist/index.js +66 -66
  90. package/dist/node_modules/.package-lock.json +1 -1
  91. package/dist/package-lock.json +2 -2
  92. package/dist/package.json +1 -1
  93. package/package.json +8 -9
  94. package/src/__tests__/config.data.ts +25 -1
  95. package/src/__tests__/tile.style.json.test.ts +19 -3
  96. package/src/__tests__/wmts.capability.test.ts +21 -0
  97. package/src/arcgis/__tests__/arcgis.style.json.test.ts +21 -17
  98. package/src/arcgis/__tests__/vector.tiler.server.test.ts +17 -8
  99. package/src/arcgis/arcgis.style.json.ts +6 -4
  100. package/src/arcgis/vector.tile.server.ts +5 -2
  101. package/src/index.ts +9 -10
  102. package/src/routes/__tests__/attribution.test.ts +4 -2
  103. package/src/routes/__tests__/fonts.test.ts +44 -7
  104. package/src/routes/__tests__/health.test.ts +4 -2
  105. package/src/routes/__tests__/sprites.test.ts +3 -4
  106. package/src/routes/__tests__/tile.json.test.ts +30 -2
  107. package/src/routes/__tests__/tile.style.json.test.ts +68 -9
  108. package/src/routes/__tests__/wmts.test.ts +23 -17
  109. package/src/routes/__tests__/xyz.test.ts +4 -2
  110. package/src/routes/attribution.ts +23 -8
  111. package/src/routes/config.ts +83 -0
  112. package/src/routes/health.ts +4 -2
  113. package/src/routes/imagery.ts +3 -2
  114. package/src/routes/tile.json.ts +10 -4
  115. package/src/routes/tile.style.json.ts +58 -12
  116. package/src/routes/tile.wmts.ts +9 -4
  117. package/src/routes/tile.xyz.raster.ts +4 -2
  118. package/src/routes/tile.xyz.ts +5 -2
  119. package/src/util/__test__/config.loader.test.ts +116 -0
  120. package/src/util/assets.provider.ts +11 -12
  121. package/src/util/config.cache.ts +18 -18
  122. package/src/util/config.loader.ts +50 -0
  123. package/src/wmts.capability.ts +9 -15
  124. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,116 @@
1
+ import { base58, ConfigProviderMemory } from '@basemaps/config';
2
+ import { fsa } from '@basemaps/shared';
3
+ import { LambdaHttpResponse } from '@linzjs/lambda';
4
+ import o from 'ospec';
5
+ import { createSandbox } from 'sinon';
6
+ import { FsMemory } from '../../routes/__tests__/memory.fs.js';
7
+ import { FakeData } from '../../__tests__/config.data.js';
8
+ import { Api, mockRequest, mockUrlRequest } from '../../__tests__/xyz.util.js';
9
+ import { CachedConfig } from '../config.cache.js';
10
+ import { ConfigLoader } from '../config.loader.js';
11
+
12
+ o.spec('ConfigLoader', () => {
13
+ const memory = new FsMemory();
14
+ const config = new ConfigProviderMemory();
15
+ const sandbox = createSandbox();
16
+
17
+ o.before(() => {
18
+ fsa.register('memory://', memory);
19
+ });
20
+
21
+ o.beforeEach(() => {
22
+ sandbox.stub(ConfigLoader, 'getDefaultConfig').resolves(config);
23
+ });
24
+
25
+ o.afterEach(() => {
26
+ sandbox.restore();
27
+ config.objects.clear();
28
+ memory.files.clear();
29
+ CachedConfig.cache.clear();
30
+ });
31
+
32
+ o('should return default config', async () => {
33
+ const provider = await ConfigLoader.load(mockRequest('/v1/fonts.json'));
34
+ o(provider).deepEquals(config);
35
+ });
36
+
37
+ o('should Not working with wrong config url', async () => {
38
+ const error = await ConfigLoader.load(
39
+ mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=notapath`, Api.header),
40
+ )
41
+ .then(() => null)
42
+ .catch((e) => e);
43
+
44
+ o(error instanceof LambdaHttpResponse).equals(true);
45
+ o((error as LambdaHttpResponse).status).equals(400);
46
+ o((error as LambdaHttpResponse).statusDescription).equals('Invalid config location');
47
+ });
48
+
49
+ o('should Not working with wrong protocol', async () => {
50
+ const error = await ConfigLoader.load(
51
+ mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=memory1://linz-basemaps/config`, Api.header),
52
+ )
53
+ .then(() => null)
54
+ .catch((e) => e);
55
+
56
+ o(error instanceof LambdaHttpResponse).equals(true);
57
+ o((error as LambdaHttpResponse).status).equals(400);
58
+ o((error as LambdaHttpResponse).statusDescription).equals('Invalid configuration location protocol:memory1');
59
+ });
60
+
61
+ o('should Not working with wrong s3 bucket', async () => {
62
+ const error = await ConfigLoader.load(
63
+ mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=s3://wrong-bucket/config`, Api.header),
64
+ )
65
+ .then(() => null)
66
+ .catch((e) => e);
67
+
68
+ o(error instanceof LambdaHttpResponse).equals(true);
69
+ o((error as LambdaHttpResponse).status).equals(400);
70
+ o((error as LambdaHttpResponse).statusDescription).equals(
71
+ 'Bucket: "wrong-bucket" is not a allowed bucket location',
72
+ );
73
+ });
74
+
75
+ const location = 'memory://linz-basemaps/config.json';
76
+
77
+ o('should Not working with no file in the path', async () => {
78
+ const error = await ConfigLoader.load(
79
+ mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${location}`, Api.header),
80
+ )
81
+ .then(() => null)
82
+ .catch((e) => e);
83
+
84
+ o(error instanceof LambdaHttpResponse).equals(true);
85
+ o((error as LambdaHttpResponse).status).equals(404);
86
+ o((error as LambdaHttpResponse).statusDescription).equals(`Config not found at ${location}`);
87
+ });
88
+
89
+ o('should get expected config file', async () => {
90
+ const expectedConfig = new ConfigProviderMemory();
91
+ expectedConfig.put(FakeData.tileSetRaster('aerial'));
92
+ await fsa.write(location, Buffer.from(JSON.stringify(expectedConfig.toJson())));
93
+
94
+ const provider = await ConfigLoader.load(
95
+ mockUrlRequest('/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json', `?config=${location}`, Api.header),
96
+ );
97
+
98
+ o(await provider.Imagery.get('aerial')).deepEquals(await expectedConfig.Imagery.get('aerial'));
99
+ });
100
+
101
+ o('should get expected config file with base58 location', async () => {
102
+ const expectedConfig = new ConfigProviderMemory();
103
+ expectedConfig.put(FakeData.tileSetVector('topographic'));
104
+ await fsa.write(location, Buffer.from(JSON.stringify(expectedConfig.toJson())));
105
+
106
+ const provider = await ConfigLoader.load(
107
+ mockUrlRequest(
108
+ '/v1/tiles/🦄 🌈/NZTM2000Quad/tile.json',
109
+ `?config=${base58.encode(Buffer.from(location))}`,
110
+ Api.header,
111
+ ),
112
+ );
113
+
114
+ o(await provider.Imagery.get('topographic')).deepEquals(await expectedConfig.Imagery.get('topographic'));
115
+ });
116
+ });
@@ -1,5 +1,7 @@
1
+ import { Env } from '@basemaps/shared';
1
2
  import { fsa } from '@chunkd/fs';
2
3
  import { LambdaHttpResponse, LambdaHttpRequest, HttpHeader } from '@linzjs/lambda';
4
+ import { ConfigLoader } from './config.loader.js';
3
5
  import { isGzip } from './cotar.serve.js';
4
6
  import { Etag } from './etag.js';
5
7
  import { NotFound, NotModified } from './response.js';
@@ -15,21 +17,13 @@ export class AssetProvider {
15
17
  * s3://linz-basemaps/assets/assets-b4ff211a.tar.co # Remote Cotar
16
18
  */
17
19
 
18
- /** Path of the assets location */
19
- path: string | undefined;
20
-
21
- set(path?: string): void {
22
- this.path = path;
23
- }
24
-
25
- async get(fileName: string): Promise<Buffer | null> {
26
- if (this.path == null) return null;
20
+ async get(path: string, fileName: string): Promise<Buffer | null> {
27
21
  // get assets file from cotar
28
- if (this.path.endsWith('.tar.co')) return await this.getFromCotar(this.path, fileName);
22
+ if (path.endsWith('.tar.co')) return await this.getFromCotar(path, fileName);
29
23
 
30
24
  // get assets file for directory
31
25
  try {
32
- const filePath = fsa.join(this.path, fileName);
26
+ const filePath = fsa.join(path, fileName);
33
27
  return await fsa.read(filePath);
34
28
  } catch (e: any) {
35
29
  if (e.code === 404) return null;
@@ -51,7 +45,12 @@ export class AssetProvider {
51
45
  * - Content-Type from the parameter contentType
52
46
  */
53
47
  async serve(req: LambdaHttpRequest, file: string, contentType: string): Promise<LambdaHttpResponse> {
54
- const buf = await assetProvider.get(file);
48
+ const config = await ConfigLoader.load(req);
49
+ let assetLocation = Env.get(Env.AssetLocation);
50
+ if (config == null) return NotFound();
51
+ if (config.assets != null) assetLocation = config.assets;
52
+ if (assetLocation == null) return NotFound();
53
+ const buf = await assetProvider.get(assetLocation, file);
55
54
  if (buf == null) return NotFound();
56
55
  const cacheKey = Etag.key(buf);
57
56
  if (Etag.isNotModified(req, cacheKey)) return NotModified();
@@ -1,16 +1,21 @@
1
1
  import { ConfigBundled, ConfigProviderMemory } from '@basemaps/config';
2
- import { fsa } from '@chunkd/fs';
2
+ import { fsa } from '@basemaps/shared';
3
3
  import { SwappingLru } from './swapping.lru.js';
4
4
 
5
5
  class LruConfig {
6
- configProvider: Promise<ConfigProviderMemory>;
6
+ configProvider: Promise<ConfigProviderMemory | null>;
7
7
 
8
8
  constructor(config: Promise<ConfigBundled>) {
9
- this.configProvider = config.then((c) => {
10
- const configProvider = ConfigProviderMemory.fromJson(c);
11
- configProvider.createVirtualTileSets();
12
- return configProvider;
13
- });
9
+ this.configProvider = config
10
+ .then((c) => {
11
+ const configProvider = ConfigProviderMemory.fromJson(c);
12
+ configProvider.createVirtualTileSets();
13
+ return configProvider;
14
+ })
15
+ .catch((e) => {
16
+ if (e.code === 404) return null;
17
+ throw e;
18
+ });
14
19
  }
15
20
 
16
21
  get size(): number {
@@ -25,20 +30,15 @@ export class ConfigCache {
25
30
  this.cache = new SwappingLru<LruConfig>(maxSize);
26
31
  }
27
32
 
28
- getConfig(location: string): Promise<ConfigProviderMemory | null> {
33
+ get(location: string): Promise<ConfigProviderMemory | null> {
29
34
  const existing = this.cache.get(location)?.configProvider;
30
35
  if (existing != null) return existing;
31
- try {
32
- const configJson = fsa.readJson<ConfigBundled>(location);
33
- const config = new LruConfig(configJson);
34
- this.cache.set(location, config);
35
- return config.configProvider;
36
- } catch (e: any) {
37
- if (e.code === 404) return Promise.resolve(null);
38
- throw e;
39
- }
36
+ const configJson = fsa.readJson<ConfigBundled>(location);
37
+ const config = new LruConfig(configJson);
38
+ this.cache.set(location, config);
39
+ return config.configProvider;
40
40
  }
41
41
  }
42
42
 
43
- /** Cache 20 configs(Around 500KB each)*/
43
+ /** Cache 20 configs (Around <30KB -> 5MB each)*/
44
44
  export const CachedConfig = new ConfigCache(20);
@@ -0,0 +1,50 @@
1
+ import { base58, BasemapsConfigProvider, isBase58 } from '@basemaps/config';
2
+ import { LambdaHttpResponse } from '@linzjs/lambda';
3
+ import { parseUri } from '@chunkd/core';
4
+ import { LambdaHttpRequest } from '@linzjs/lambda';
5
+ import { CachedConfig } from './config.cache.js';
6
+ import { getDefaultConfig } from '@basemaps/shared';
7
+
8
+ // FIXME load this from process.env COG BUCKETS?
9
+ const SafeBuckets = new Set(['linz-workflow-artifacts', 'linz-basemaps']);
10
+ const SafeProtocols = new Set(['s3', 'memory']);
11
+
12
+ export class ConfigLoader {
13
+ /** Exposed for testing */
14
+ static async getDefaultConfig(): Promise<BasemapsConfigProvider> {
15
+ return getDefaultConfig();
16
+ }
17
+
18
+ /** Lookup the config path from a request and return a standardized location */
19
+ static extract(req: LambdaHttpRequest): string | null {
20
+ const rawLocation = req.query.get('config');
21
+ if (rawLocation == null) return null;
22
+ if (rawLocation.includes('/')) return base58.encode(Buffer.from(rawLocation));
23
+ return rawLocation;
24
+ }
25
+
26
+ static async load(req: LambdaHttpRequest): Promise<BasemapsConfigProvider> {
27
+ const rawLocation = req.query.get('config');
28
+ if (rawLocation == null) return this.getDefaultConfig();
29
+
30
+ const configLocation = isBase58(rawLocation) ? Buffer.from(base58.decode(rawLocation)).toString() : rawLocation;
31
+
32
+ const r = parseUri(configLocation);
33
+
34
+ if (r == null) throw new LambdaHttpResponse(400, 'Invalid config location');
35
+ if (!SafeProtocols.has(r.protocol)) {
36
+ throw new LambdaHttpResponse(400, `Invalid configuration location protocol:${r.protocol}`);
37
+ }
38
+ if (!SafeBuckets.has(r.bucket)) {
39
+ throw new LambdaHttpResponse(400, `Bucket: "${r.bucket}" is not a allowed bucket location`);
40
+ }
41
+
42
+ req.set('config', configLocation);
43
+ req.timer.start('config:load');
44
+ return CachedConfig.get(configLocation).then((f) => {
45
+ req.timer.end('config:load');
46
+ if (f == null) throw new LambdaHttpResponse(404, `Config not found at ${configLocation}`);
47
+ return f;
48
+ });
49
+ }
50
+ }
@@ -1,6 +1,6 @@
1
- import { Config, ConfigImagery, ConfigLayer, ConfigTileSet, standardizeLayerName } from '@basemaps/config';
1
+ import { ConfigImagery, ConfigLayer, ConfigTileSet, standardizeLayerName } from '@basemaps/config';
2
2
  import { Bounds, GoogleTms, ImageFormat, TileMatrixSet, WmtsProvider } from '@basemaps/geo';
3
- import { Projection, V, VNodeElement } from '@basemaps/shared';
3
+ import { Projection, toQueryString, V, VNodeElement } from '@basemaps/shared';
4
4
  import { ImageFormatOrder } from '@basemaps/tiler';
5
5
  import { BoundingBox } from '@cogeotiff/core';
6
6
  import { BBox } from '@linzjs/geojson';
@@ -36,6 +36,8 @@ export interface WmtsCapabilitiesParams {
36
36
  apiKey?: string;
37
37
  /** Limit the output to the following image formats other wise @see ImageFormatOrder */
38
38
  formats?: ImageFormat[] | null;
39
+ /** Config location */
40
+ config?: string | null;
39
41
  }
40
42
 
41
43
  /** Number of decimal places to use in lat lng */
@@ -56,6 +58,7 @@ export class WmtsCapabilities {
56
58
  provider?: WmtsProvider;
57
59
  tileSet: ConfigTileSet;
58
60
  apiKey?: string;
61
+ config?: string | null;
59
62
  tileMatrixSets = new Map<string, TileMatrixSet>();
60
63
  imagery: Map<string, ConfigImagery>;
61
64
  formats: ImageFormat[];
@@ -65,6 +68,7 @@ export class WmtsCapabilities {
65
68
  this.httpBase = params.httpBase;
66
69
  this.provider = params.provider;
67
70
  this.tileSet = params.tileSet;
71
+ this.config = params.config;
68
72
  this.isIndividualLayers = params.isIndividualLayers;
69
73
  for (const tms of params.tileMatrix) this.tileMatrixSets.set(tms.identifier, tms);
70
74
  this.apiKey = params.apiKey;
@@ -72,17 +76,6 @@ export class WmtsCapabilities {
72
76
  this.imagery = params.imagery;
73
77
  }
74
78
 
75
- async loadImagery(): Promise<void> {
76
- const ids = new Set<string>();
77
- for (const tms of this.tileMatrixSets.values()) {
78
- for (const layer of this.tileSet.layers) {
79
- const layerId = layer[tms.projection.code];
80
- if (layerId != null) ids.add(layerId);
81
- }
82
- }
83
- this.imagery = await Config.Imagery.getAll(ids);
84
- }
85
-
86
79
  buildWgs84BoundingBox(tms: TileMatrixSet, layers: Bounds[]): VNodeElement {
87
80
  let bbox: BBox;
88
81
  if (layers.length > 0) {
@@ -164,7 +157,8 @@ export class WmtsCapabilities {
164
157
  }
165
158
 
166
159
  buildTileUrl(tileSetId: string, suffix: string): string {
167
- const apiSuffix = this.apiKey ? `?api=${this.apiKey}` : '';
160
+ const query = toQueryString({ api: this.apiKey, config: this.config });
161
+
168
162
  return [
169
163
  this.httpBase,
170
164
  'v1',
@@ -173,7 +167,7 @@ export class WmtsCapabilities {
173
167
  '{TileMatrixSet}',
174
168
  '{TileMatrix}',
175
169
  '{TileCol}',
176
- `{TileRow}.${suffix}${apiSuffix}`,
170
+ `{TileRow}.${suffix}${query}`,
177
171
  ].join('/');
178
172
  }
179
173