mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
build(atlas): build the globe geometry from the bundled boundaries
scripts/build-atlas-mesh.mjs turns the admin-0 and admin-1 GeoJSON the server already ships into TopoJSON at three levels of detail per world and two per country's regions, smoothed, quantized and Brotli-packed under content-hashed names, plus a manifest with every country's anchor, box and region index. It runs at build time (npm run build, npm run dev when the sources changed, and the Docker server stage); the output is not committed. /api/addons/atlas/mesh serves the manifest and the files as they are (immutable, a year), decompressing as a stream for a client without br.
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
|
||||
import { Controller, Get, Headers, NotFoundException, Param, Res, UseGuards } from '@nestjs/common';
|
||||
|
||||
import type { Response } from 'express';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { pipeline } from 'node:stream';
|
||||
import zlib from 'node:zlib';
|
||||
|
||||
// __dirname is server/dist/nest/atlas at runtime and server/src/nest/atlas under vitest;
|
||||
// both resolve ../../../assets to server/assets.
|
||||
const MESH_DIR = path.join(__dirname, '..', '..', '..', 'assets', 'atlas-mesh');
|
||||
|
||||
/** A hashed geometry file name as the build script writes it; nothing else names a file. */
|
||||
const FILE_NAME = /^[A-Za-z0-9-]{2,24}\.[0-9a-f]{12}\.json$/;
|
||||
|
||||
/**
|
||||
* /api/addons/atlas/mesh: the geometry of the globe Atlas, built by
|
||||
* scripts/build-atlas-mesh.mjs. The manifest is revalidated on every load; the
|
||||
* topology files carry their content hash in the name and are cached for a year.
|
||||
* Files are stored Brotli-compressed and sent as they are; a client that does not
|
||||
* accept br gets them decompressed as a stream. Nothing is held in memory. Same guard as the rest of the Atlas routes.
|
||||
*/
|
||||
@Controller('api/addons/atlas/mesh')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
export class AtlasMeshController {
|
||||
// response-contract-exempt: sends the built manifest file through @Res() as it is.
|
||||
@Get('manifest')
|
||||
manifest(@Res() res: Response): void {
|
||||
const file = path.join(MESH_DIR, 'manifest.json');
|
||||
if (!fs.existsSync(file)) throw new NotFoundException({ error: 'Atlas geometry not built' });
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.sendFile('manifest.json', { root: MESH_DIR, acceptRanges: false });
|
||||
}
|
||||
|
||||
// response-contract-exempt: sends a pre-compressed topology file through @Res() as it is.
|
||||
@Get('world/:file')
|
||||
world(@Param('file') file: string, @Headers('accept-encoding') enc: string | undefined, @Res() res: Response): void {
|
||||
this.send('', file, enc, res);
|
||||
}
|
||||
|
||||
// response-contract-exempt: sends a pre-compressed topology file through @Res() as it is.
|
||||
@Get('regions/:file')
|
||||
regions(
|
||||
@Param('file') file: string,
|
||||
@Headers('accept-encoding') enc: string | undefined,
|
||||
@Res() res: Response,
|
||||
): void {
|
||||
this.send('regions', file, enc, res);
|
||||
}
|
||||
|
||||
private send(dir: string, file: string, enc: string | undefined, res: Response): void {
|
||||
if (!FILE_NAME.test(file)) throw new NotFoundException({ error: 'Not found' });
|
||||
const full = path.join(MESH_DIR, dir, `${file}.br`);
|
||||
if (!fs.existsSync(full)) throw new NotFoundException({ error: 'Not found' });
|
||||
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
||||
res.setHeader('Vary', 'Accept-Encoding');
|
||||
if (/\bbr\b/.test(enc ?? '')) {
|
||||
res.setHeader('Content-Encoding', 'br');
|
||||
res.sendFile(`${file}.br`, { root: path.join(MESH_DIR, dir), acceptRanges: false, lastModified: false });
|
||||
return;
|
||||
}
|
||||
// No br (a plain-http install: browsers offer it over HTTPS only): decompress
|
||||
// as a stream, so neither the packed nor the unpacked file sits in memory and
|
||||
// the event loop never waits on a whole file.
|
||||
pipeline(fs.createReadStream(full), zlib.createBrotliDecompress(), res, (err) => {
|
||||
if (err && !res.headersSent) res.status(500).end();
|
||||
else if (err) res.destroy(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user