|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import os from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | + |
| 5 | +import { Agent as SecretStreamAgent } from 'secret-stream-http' |
| 6 | +import { describe, it, expect } from 'vitest' |
| 7 | + |
| 8 | +import { createServer, errors } from '../src/index.js' |
| 9 | +import { DEMOTILES_Z2, OSM_BRIGHT_Z6 } from './helpers.js' |
| 10 | + |
| 11 | +/** |
| 12 | + * Create a server backed by a throwaway copy of the custom map fixture. The |
| 13 | + * server is closed and the temp dir removed when the test finishes. |
| 14 | + */ |
| 15 | +async function createTestServer(t: { |
| 16 | + onTestFinished: (fn: () => Promise<void> | void) => void |
| 17 | +}) { |
| 18 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'map-server-test-')) |
| 19 | + const tmpCustomMapPath = path.join(tmpDir, 'custom-map.smp') |
| 20 | + await fs.copyFile(OSM_BRIGHT_Z6, tmpCustomMapPath) |
| 21 | + |
| 22 | + const server = createServer({ |
| 23 | + defaultOnlineStyleUrl: 'https://demotiles.maplibre.org/style.json', |
| 24 | + customMapPath: tmpCustomMapPath, |
| 25 | + fallbackMapPath: DEMOTILES_Z2, |
| 26 | + keyPair: SecretStreamAgent.keyPair(), |
| 27 | + }) |
| 28 | + |
| 29 | + t.onTestFinished(async () => { |
| 30 | + await server.close().catch(() => {}) |
| 31 | + await fs.rm(tmpDir, { recursive: true, force: true }).catch(() => {}) |
| 32 | + }) |
| 33 | + |
| 34 | + return server |
| 35 | +} |
| 36 | + |
| 37 | +describe('Server lifecycle', () => { |
| 38 | + it('listen() is idempotent', async (t) => { |
| 39 | + const server = await createTestServer(t) |
| 40 | + |
| 41 | + const first = await server.listen({ localPort: 0, remotePort: 0 }) |
| 42 | + // A second listen() without a close() in between must not throw. The old |
| 43 | + // code called net.Server.listen() a second time, which throws |
| 44 | + // ERR_SERVER_ALREADY_LISTEN ("Listen method has been called more than |
| 45 | + // once without closing"). It should instead resolve to the same ports. |
| 46 | + const second = await server.listen({ localPort: 0, remotePort: 0 }) |
| 47 | + |
| 48 | + expect(second).toEqual(first) |
| 49 | + |
| 50 | + const response = await fetch( |
| 51 | + `http://127.0.0.1:${first.localPort}/maps/custom/style.json`, |
| 52 | + ) |
| 53 | + expect(response.status).toBe(200) |
| 54 | + }) |
| 55 | + |
| 56 | + it('concurrent listen() calls resolve to the same ports', async (t) => { |
| 57 | + const server = await createTestServer(t) |
| 58 | + |
| 59 | + const [a, b] = await Promise.all([ |
| 60 | + server.listen({ localPort: 0, remotePort: 0 }), |
| 61 | + server.listen({ localPort: 0, remotePort: 0 }), |
| 62 | + ]) |
| 63 | + |
| 64 | + expect(a).toEqual(b) |
| 65 | + |
| 66 | + const response = await fetch( |
| 67 | + `http://127.0.0.1:${a.localPort}/maps/custom/style.json`, |
| 68 | + ) |
| 69 | + expect(response.status).toBe(200) |
| 70 | + }) |
| 71 | + |
| 72 | + it('listen() with different ports while already listening throws', async (t) => { |
| 73 | + const server = await createTestServer(t) |
| 74 | + |
| 75 | + const { localPort } = await server.listen({ localPort: 0, remotePort: 0 }) |
| 76 | + |
| 77 | + // Requesting a different explicit port without closing first is a |
| 78 | + // programming error and must reject rather than silently keep the old port. |
| 79 | + await expect( |
| 80 | + server.listen({ localPort: localPort + 1 }), |
| 81 | + ).rejects.toThrow(/already listening/) |
| 82 | + |
| 83 | + // The original listener is untouched. |
| 84 | + const response = await fetch( |
| 85 | + `http://127.0.0.1:${localPort}/maps/custom/style.json`, |
| 86 | + ) |
| 87 | + expect(response.status).toBe(200) |
| 88 | + }) |
| 89 | + |
| 90 | + it('close() is idempotent', async (t) => { |
| 91 | + const server = await createTestServer(t) |
| 92 | + |
| 93 | + await server.listen({ localPort: 0, remotePort: 0 }) |
| 94 | + |
| 95 | + await server.close() |
| 96 | + // A second close() must resolve cleanly rather than calling |
| 97 | + // net.Server.close() on an already-closed server. |
| 98 | + await expect(server.close()).resolves.toBeUndefined() |
| 99 | + }) |
| 100 | + |
| 101 | + it('a close() racing a listen() on a started server leaves it working (last call wins)', async (t) => { |
| 102 | + const server = await createTestServer(t) |
| 103 | + |
| 104 | + await server.listen({ localPort: 0, remotePort: 0 }) |
| 105 | + |
| 106 | + // Fire close() then listen() without awaiting close() in between. The |
| 107 | + // state machine serialises them so the last call (listen) wins. |
| 108 | + const closePromise = server.close() |
| 109 | + const listenPromise = server.listen({ localPort: 0, remotePort: 0 }) |
| 110 | + |
| 111 | + await closePromise |
| 112 | + const result = await listenPromise |
| 113 | + |
| 114 | + const response = await fetch( |
| 115 | + `http://127.0.0.1:${result.localPort}/maps/custom/style.json`, |
| 116 | + ) |
| 117 | + expect(response.status).toBe(200) |
| 118 | + }) |
| 119 | + |
| 120 | + it('close() called while listen() is in flight settles cleanly and the server can restart', async (t) => { |
| 121 | + const server = await createTestServer(t) |
| 122 | + |
| 123 | + // Start listening but request a close before listen() has resolved. The |
| 124 | + // old code raced net.Server.close() against an in-progress listen() and |
| 125 | + // hung forever waiting on a 'close' event that never fired. The state |
| 126 | + // machine serialises the two so both promises settle (last call wins: |
| 127 | + // the server ends up stopped). |
| 128 | + const listenPromise = server.listen({ localPort: 0, remotePort: 0 }) |
| 129 | + const closePromise = server.close() |
| 130 | + |
| 131 | + await Promise.all([listenPromise, closePromise]) |
| 132 | + |
| 133 | + // The server must be restartable and functional after the race. |
| 134 | + const result = await server.listen({ localPort: 0, remotePort: 0 }) |
| 135 | + const response = await fetch( |
| 136 | + `http://127.0.0.1:${result.localPort}/maps/custom/style.json`, |
| 137 | + ) |
| 138 | + expect(response.status).toBe(200) |
| 139 | + }, 15_000) |
| 140 | + |
| 141 | + it('exposes a typed SERVER_CLOSED error', () => { |
| 142 | + const err = new errors.SERVER_CLOSED() |
| 143 | + expect(err.code).toBe('SERVER_CLOSED') |
| 144 | + expect(err.status).toBe(503) |
| 145 | + expect(err.message).toBe('Server is closed') |
| 146 | + }) |
| 147 | +}) |
0 commit comments