Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/config/src/memory/memory.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ function removeUndefined(obj: unknown): void {
export class ConfigProviderMemory extends BasemapsConfigProvider {
override type = 'memory' as const;

static is(cfg: BasemapsConfigProvider): cfg is ConfigProviderMemory {
return cfg.type === 'memory';
}

/** Optional id of the configuration */
id?: string;
/** Optional hash of the config if the config was loaded from JSON */
hash?: string;

Imagery = new MemoryConfigObject<ConfigImagery>(this, ConfigPrefix.Imagery);
Style = new MemoryConfigObject<ConfigVectorStyle>(this, ConfigPrefix.Style);
Expand Down Expand Up @@ -245,6 +251,7 @@ export class ConfigProviderMemory extends BasemapsConfigProvider {

mem.assets = cfg.assets;
mem.id = cfg.id;
mem.hash = cfg.hash;

return mem;
}
Expand Down
32 changes: 30 additions & 2 deletions packages/lambda-tiler/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import assert from 'node:assert';
import { afterEach, describe, it } from 'node:test';

import { ConfigProviderMemory } from '@basemaps/config';

import { handler } from '../index.js';
import { ConfigLoader } from '../util/config.loader.js';
import { mockRequest } from './xyz.util.js';

describe('LambdaXyz index', () => {
Expand All @@ -17,7 +20,10 @@ describe('LambdaXyz index', () => {
delete process.env['BUILD_ID'];
});

it('should return version', async () => {
it('should return version', async (t) => {
const config = new ConfigProviderMemory();
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);

process.env['GIT_VERSION'] = '1.2.3';
process.env['GIT_HASH'] = 'abc456';

Expand All @@ -32,7 +38,10 @@ describe('LambdaXyz index', () => {
});
});

it('should include buildId if exists', async () => {
it('should include buildId if exists', async (t) => {
const config = new ConfigProviderMemory();
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);

process.env['GIT_VERSION'] = '1.2.3';
process.env['BUILD_ID'] = '1658821493-3';

Expand All @@ -46,6 +55,25 @@ describe('LambdaXyz index', () => {
buildId: '1658821493-3',
});
});

it('should return config information if present', async (t) => {
const config = new ConfigProviderMemory();
t.mock.method(ConfigLoader, 'getDefaultConfig', () => config);
config.id = 'config-id';

const response = await handler.router.handle(mockRequest('/v1/version'));
assert.deepEqual(JSON.parse(response.body), {
version: 'dev',
config: { id: 'config-id' },
});

config.hash = 'config-hash';
const responseHash = await handler.router.handle(mockRequest('/v1/version'));
assert.deepEqual(JSON.parse(responseHash.body), {
version: 'dev',
config: { id: 'config-id', hash: 'config-hash' },
});
});
});

it('should respond to /ping', async () => {
Expand Down
24 changes: 22 additions & 2 deletions packages/lambda-tiler/src/routes/version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { HttpHeader, LambdaHttpResponse } from '@linzjs/lambda';
import { BasemapsConfigProvider, ConfigProviderMemory } from '@basemaps/config';
import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';

export function versionGet(): Promise<LambdaHttpResponse> {
import { ConfigLoader } from '../util/config.loader.js';

function getConfigHash(cfg: BasemapsConfigProvider): { id: string; hash?: string } | undefined {
if (!ConfigProviderMemory.is(cfg)) return undefined;
if (cfg.id == null) return undefined;
return { id: cfg.id, hash: cfg.hash };
}

export async function versionGet(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
const config = await ConfigLoader.load(req);
const response = new LambdaHttpResponse(200, 'ok');
response.header(HttpHeader.CacheControl, 'no-store');
response.json({
Expand All @@ -9,16 +19,26 @@ export function versionGet(): Promise<LambdaHttpResponse> {
* @example "v6.42.1"
*/
version: process.env['GIT_VERSION'] ?? 'dev',

/**
* Full git commit hash
* @example "e4231b1ee62c276c8657c56677ced02681dfe5d6"
*/
hash: process.env['GIT_HASH'],

/**
*
* The exact build that this release was run from
* @example "1658821493-3"
*/
buildId: process.env['BUILD_ID'],

/**
* Configuration id that was used to power this config
* @example { "id": "cb_01JTQ7ZK49F8EY4N5DRJ3XFT73", hash: "HcByZ8WS2zpaTxFJp6wSKg2eUpwahLqAGEQdcDxKxqp6" }
*/
config: getConfigHash(config),
});

return Promise.resolve(response);
}
Loading