/** * AtlasMeshController, the cases atlas-mesh.controller.test.ts leaves open: a * read that breaks after the first bytes went out, the world files sent from * the mesh root, and the manifest sent without ranges. */ import { AtlasMeshController } from '../../../src/nest/atlas/atlas-mesh.controller'; import type { Response } from 'express'; import fs from 'node:fs'; import { PassThrough, Readable } from 'node:stream'; import zlib from 'node:zlib'; import { afterEach, describe, expect, it, vi } from 'vitest'; /** A writable stand-in for express's Response that records what the controller does with it. */ class FakeResponse extends PassThrough { headers: Record = {}; sent: { name: string; options: Record } | null = null; statusCode = 200; headersSent = false; body = ''; destroyedWith: Error | null = null; constructor() { super(); this.on('data', (chunk: Buffer) => { this.headersSent = true; this.body += chunk.toString('utf8'); }); } setHeader(name: string, value: string): this { this.headers[name] = value; return this; } sendFile(name: string, options: Record): void { this.sent = { name, options }; } status(code: number): this { this.statusCode = code; return this; } override destroy(error?: Error): this { this.destroyedWith = error ?? null; return super.destroy(error); } } const FILE = 'world-50m.0123456789ab.json'; const asRes = (r: FakeResponse) => r as unknown as Response; afterEach(() => vi.restoreAllMocks()); describe('AtlasMeshController streaming', () => { const controller = new AtlasMeshController(); it('ATLAS2-REST-030: a read that breaks after bytes went out ends the connection instead of a 500', async () => { const json = JSON.stringify({ type: 'Topology', objects: { land: { type: 'GeometryCollection' } } }); const packed = zlib.brotliCompressSync(Buffer.from(json)); async function* brokenAfterFirstChunk() { yield packed; await new Promise((r) => setTimeout(r, 30)); throw new Error('disk went away'); } vi.spyOn(fs, 'existsSync').mockReturnValue(true); vi.spyOn(fs, 'createReadStream').mockReturnValue( Readable.from(brokenAfterFirstChunk()) as unknown as fs.ReadStream, ); const res = new FakeResponse(); // The fake swallows the error it is destroyed with, as a socket's close would. res.on('error', () => undefined); const closed = new Promise((resolve) => res.on('close', resolve)); controller.world(FILE, 'gzip, deflate', asRes(res)); await closed; expect(res.body).toBe(json); expect(res.statusCode).toBe(200); expect(res.destroyedWith?.message).toBe('disk went away'); }); it('ATLAS2-REST-031: a world file goes out of the mesh root itself, without ranges or last-modified', () => { vi.spyOn(fs, 'existsSync').mockReturnValue(true); const res = new FakeResponse(); controller.world(FILE, 'br', asRes(res)); expect(res.headers['Content-Encoding']).toBe('br'); expect(res.headers['Content-Type']).toBe('application/json; charset=utf-8'); expect(res.sent?.name).toBe(`${FILE}.br`); expect(String(res.sent?.options.root)).toMatch(/atlas-mesh$/); expect(res.sent?.options).toMatchObject({ acceptRanges: false, lastModified: false }); }); it('ATLAS2-REST-032: the manifest is sent from the mesh root without ranges', () => { vi.spyOn(fs, 'existsSync').mockReturnValue(true); const res = new FakeResponse(); controller.manifest(asRes(res)); expect(String(res.sent?.options.root)).toMatch(/atlas-mesh$/); expect(res.sent?.options).toMatchObject({ acceptRanges: false }); }); it('ATLAS2-REST-033: "br" must stand as a word in Accept-Encoding, not inside another token', async () => { const json = '{"ok":true}'; vi.spyOn(fs, 'existsSync').mockReturnValue(true); vi.spyOn(fs, 'createReadStream').mockReturnValue( Readable.from([zlib.brotliCompressSync(Buffer.from(json))]) as unknown as fs.ReadStream, ); const res = new FakeResponse(); const done = new Promise((resolve) => { res.on('finish', resolve); res.on('close', resolve); }); controller.regions('FR.0123456789ab.json', 'xbrx', asRes(res)); await done; expect(res.headers['Content-Encoding']).toBeUndefined(); expect(res.body).toBe(json); }); });