Generates the nginx rules for prerendered pages
Pages now map to their html file, other forms get one redirect, unknown URLs get a real 404 and responses are compressed. A browser-free test suite runs the rules against a real nginx, in CI only so far. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,376 @@
|
||||
import { request } from 'node:http';
|
||||
import { connect } from 'node:net';
|
||||
import { gunzipSync } from 'node:zlib';
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
||||
import { securityHeaders } from '../config/securityHeaders.ts';
|
||||
import { createFixtureSite, type FixtureSite } from './support/fixtureSite.ts';
|
||||
import {
|
||||
findNginx,
|
||||
startNginx,
|
||||
type MimeVariant,
|
||||
type RunningNginx,
|
||||
} from './support/nginx.ts';
|
||||
|
||||
// BR-HST-24: de nginx-regels worden bewezen door een echte nginx, niet door de
|
||||
// tekst te lezen. De site is een kleine fixture (geen build nodig); dezelfde
|
||||
// regels voor de previewserver staan in previewServer.test.ts.
|
||||
|
||||
const nginxBin = findNginx();
|
||||
const inCi = Boolean(process.env.CI);
|
||||
|
||||
// Zonder nginx: in CI een duidelijke fout, lokaal een zichtbare melding
|
||||
if (!nginxBin) {
|
||||
if (inCi) {
|
||||
describe('serverregels (nginx)', () => {
|
||||
it('vindt nginx', () => {
|
||||
throw new Error('nginx ontbreekt in CI (BR-HST-24)');
|
||||
});
|
||||
});
|
||||
} else {
|
||||
describe('serverregels (nginx)', () => {
|
||||
it('nginx niet gevonden: serverregeltests overgeslagen (zie BR-HST-24)', (context) => {
|
||||
console.log(context.task.name);
|
||||
context.skip();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
interface Reply {
|
||||
status: number;
|
||||
headers: Record<string, string>;
|
||||
rawHeaders: string[];
|
||||
body: Buffer;
|
||||
}
|
||||
|
||||
// node:http en niet fetch: fetch voegt dubbele headers samen en pakt gzip uit
|
||||
function get(
|
||||
origin: string,
|
||||
path: string,
|
||||
options: { method?: string; headers?: Record<string, string> } = {},
|
||||
): Promise<Reply> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = request(
|
||||
origin + path,
|
||||
{ method: options.method ?? 'GET', headers: options.headers },
|
||||
(res) => {
|
||||
const chunks: Buffer[] = [];
|
||||
res.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
res.on('end', () =>
|
||||
resolve({
|
||||
status: res.statusCode ?? 0,
|
||||
headers: Object.fromEntries(
|
||||
Object.entries(res.headers).map(([k, v]) => [
|
||||
k,
|
||||
String(v),
|
||||
]),
|
||||
),
|
||||
rawHeaders: res.rawHeaders,
|
||||
body: Buffer.concat(chunks),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
const countHeader = (reply: Reply, name: string) =>
|
||||
reply.rawHeaders.filter(
|
||||
(value, i) => i % 2 === 0 && value.toLowerCase() === name.toLowerCase(),
|
||||
).length;
|
||||
|
||||
// Alle productieheaders, X-Robots-Tag en precies één Cache-Control
|
||||
function expectFullHeaders(reply: Reply, cacheControl = 'no-cache') {
|
||||
for (const [name, value] of Object.entries(securityHeaders('production'))) {
|
||||
expect(countHeader(reply, name), name).toBe(1);
|
||||
expect(reply.headers[name.toLowerCase()], name).toBe(value);
|
||||
}
|
||||
expect(countHeader(reply, 'X-Robots-Tag')).toBe(1);
|
||||
expect(reply.headers['x-robots-tag']).toBe('noindex, nofollow');
|
||||
expect(countHeader(reply, 'Cache-Control')).toBe(1);
|
||||
expect(reply.headers['cache-control']).toBe(cacheControl);
|
||||
}
|
||||
|
||||
// Een ruw HTTP/1.0-verzoek, zoals de proxy-Pi dat naar de hosting-Pi stuurt
|
||||
function getHttp10(origin: string, path: string, headers: string[]) {
|
||||
const { port } = new URL(origin);
|
||||
return new Promise<{ head: string; body: Buffer }>((resolve, reject) => {
|
||||
const socket = connect(Number(port), '127.0.0.1');
|
||||
const chunks: Buffer[] = [];
|
||||
socket.on('connect', () =>
|
||||
socket.write(
|
||||
[
|
||||
`GET ${path} HTTP/1.0`,
|
||||
'Host: test.andreaslop.nl',
|
||||
...headers,
|
||||
'',
|
||||
'',
|
||||
].join('\r\n'),
|
||||
),
|
||||
);
|
||||
socket.on('data', (chunk: Buffer) => chunks.push(chunk));
|
||||
socket.on('error', reject);
|
||||
socket.on('close', () => {
|
||||
const all = Buffer.concat(chunks);
|
||||
const split = all.indexOf('\r\n\r\n');
|
||||
resolve({
|
||||
head: all.subarray(0, split).toString('latin1'),
|
||||
body: all.subarray(split + 4),
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const mimeVariants: MimeVariant[] = ['new', 'old'];
|
||||
|
||||
const rulesSuite = nginxBin ? describe : describe.skip;
|
||||
rulesSuite.each(mimeVariants)(
|
||||
'serverregels op een echte nginx (.js als %s mime-type)',
|
||||
(variant) => {
|
||||
let site: FixtureSite;
|
||||
let nginx: RunningNginx;
|
||||
const markerOf = (file: string) => site.markers.get(file) ?? file;
|
||||
|
||||
beforeAll(async () => {
|
||||
site = await createFixtureSite();
|
||||
nginx = await startNginx({
|
||||
siteDir: site.dir,
|
||||
mimeVariant: variant,
|
||||
});
|
||||
console.log(
|
||||
`[serverRules] nginx -v: ${nginx.version} (${nginx.origin})`,
|
||||
);
|
||||
});
|
||||
afterAll(async () => {
|
||||
await nginx?.stop();
|
||||
await site?.remove();
|
||||
});
|
||||
|
||||
const page = (path: string, init?: Parameters<typeof get>[2]) =>
|
||||
get(nginx.origin, path, init);
|
||||
|
||||
describe("pagina's en bestanden (BR-HST-01, V3)", () => {
|
||||
it.each([
|
||||
['/', 'index.html'],
|
||||
['/over-mij', 'over-mij.html'],
|
||||
['/soorten-fotoshoots', 'soorten-fotoshoots.html'],
|
||||
[
|
||||
'/soorten-fotoshoots/newborn',
|
||||
'soorten-fotoshoots/newborn.html',
|
||||
],
|
||||
['/over-mij?x=1', 'over-mij.html'],
|
||||
['/?x=1', 'index.html'],
|
||||
])('%s geeft 200 met %s', async (path, file) => {
|
||||
const reply = await page(path);
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.body.toString()).toContain(markerOf(file));
|
||||
expectFullHeaders(reply);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['/assets/index-AbC123xYz.js', 'assets/index-AbC123xYz.js'],
|
||||
['/sitemap.xml', 'sitemap.xml'],
|
||||
])('%s geeft 200 met het bestand', async (path, file) => {
|
||||
const reply = await page(path);
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.body.toString()).toContain(markerOf(file));
|
||||
});
|
||||
|
||||
it('de map soorten-fotoshoots/ naast het paginabestand stuurt niet door en toont geen lijst (V3)', async () => {
|
||||
const file = await page('/soorten-fotoshoots');
|
||||
expect(file.status).toBe(200);
|
||||
expect(file.body.toString()).toContain(
|
||||
markerOf('soorten-fotoshoots.html'),
|
||||
);
|
||||
const dir = await page('/soorten-fotoshoots/');
|
||||
expect(dir.status).toBe(301);
|
||||
expect(dir.headers.location).toBe('/soorten-fotoshoots');
|
||||
});
|
||||
|
||||
it('/ werkt zonder index-directive (V2)', async () => {
|
||||
const reply = await page('/');
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.body.toString()).toContain(markerOf('index.html'));
|
||||
});
|
||||
|
||||
it('/robots.txt verbiedt alles op de testsite', async () => {
|
||||
const reply = await page('/robots.txt');
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.body.toString()).toContain('Disallow: /');
|
||||
expect(reply.body.toString()).not.toContain('fixture:');
|
||||
expectFullHeaders(reply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('doorverwijzingen (BR-HST-03, 04, 06, V2)', () => {
|
||||
it.each([
|
||||
['/over-mij/', '/over-mij'],
|
||||
['/over-mij///', '/over-mij'],
|
||||
['/soorten-fotoshoots/', '/soorten-fotoshoots'],
|
||||
['/over-mij/?x=1', '/over-mij?x=1'],
|
||||
['/soorten-fotoshoots/?x=1', '/soorten-fotoshoots?x=1'],
|
||||
['/over-mij.html', '/over-mij'],
|
||||
[
|
||||
'/soorten-fotoshoots/newborn.html',
|
||||
'/soorten-fotoshoots/newborn',
|
||||
],
|
||||
['/index', '/'],
|
||||
['/index.html', '/'],
|
||||
['/index/', '/'],
|
||||
['/Over-Mij', '/over-mij'],
|
||||
['/OVER-MIJ.HTML', '/over-mij'],
|
||||
['/Over-Mij/', '/over-mij'],
|
||||
['/Over-Mij?x=1&y=%20z', '/over-mij?x=1&y=%20z'],
|
||||
['/onbekend/', '/onbekend'],
|
||||
['/assets/', '/assets'],
|
||||
])('%s gaat met één 301 naar %s', async (path, target) => {
|
||||
const reply = await page(path);
|
||||
expect(reply.status).toBe(301);
|
||||
// Relatief: absolute_redirect staat uit
|
||||
expect(reply.headers.location).toBe(target);
|
||||
expectFullHeaders(reply);
|
||||
});
|
||||
});
|
||||
|
||||
describe('404 (BR-HST-02, 05, V4)', () => {
|
||||
it.each([
|
||||
'/404',
|
||||
'/404.html',
|
||||
'/onbekend',
|
||||
'/Onbekend',
|
||||
'/x.html',
|
||||
'/assets/missing.js',
|
||||
'/assets',
|
||||
])('%s geeft 404 met de 404-pagina', async (path) => {
|
||||
const reply = await page(path);
|
||||
expect(reply.status).toBe(404);
|
||||
expect(reply.body.toString()).toContain(markerOf('404.html'));
|
||||
// Ook de 404 van een ontbrekend asset is niet immutable
|
||||
expectFullHeaders(reply);
|
||||
});
|
||||
|
||||
it('HEAD op een pagina en op een onbekende URL', async () => {
|
||||
const ok = await page('/over-mij', { method: 'HEAD' });
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.body).toHaveLength(0);
|
||||
expectFullHeaders(ok);
|
||||
const missing = await page('/onbekend', { method: 'HEAD' });
|
||||
expect(missing.status).toBe(404);
|
||||
expect(missing.body).toHaveLength(0);
|
||||
expectFullHeaders(missing);
|
||||
});
|
||||
});
|
||||
|
||||
describe('caching (BR-HST-12)', () => {
|
||||
it('/assets/* heeft precies één Cache-Control: immutable', async () => {
|
||||
for (const path of [
|
||||
'/assets/index-AbC123xYz.js',
|
||||
'/assets/style-Qw9.css',
|
||||
'/assets/font-Ab.woff2',
|
||||
]) {
|
||||
const reply = await page(path);
|
||||
expect(reply.status, path).toBe(200);
|
||||
expectFullHeaders(
|
||||
reply,
|
||||
'public, max-age=31536000, immutable',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('al het andere revalideert', async () => {
|
||||
for (const path of ['/', '/sitemap.xml', '/favicon.png']) {
|
||||
expectFullHeaders(await page(path));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('compressie (BR-HST-09, V5)', () => {
|
||||
const compressed = [
|
||||
'/',
|
||||
'/over-mij',
|
||||
'/assets/index-AbC123xYz.js',
|
||||
'/assets/style-Qw9.css',
|
||||
'/assets/icon-Zz1.svg',
|
||||
'/assets/data.json',
|
||||
'/sitemap.xml',
|
||||
'/assets/readme.txt',
|
||||
];
|
||||
const plain = ['/assets/font-Ab.woff2', '/favicon.png'];
|
||||
|
||||
it.each(compressed)('%s krijgt gzip', async (path) => {
|
||||
const reply = await page(path, {
|
||||
headers: { 'Accept-Encoding': 'gzip' },
|
||||
});
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.headers['content-encoding']).toBe('gzip');
|
||||
expect(reply.headers.vary).toContain('Accept-Encoding');
|
||||
expect(gunzipSync(reply.body).length).toBeGreaterThan(256);
|
||||
});
|
||||
|
||||
it.each(plain)('%s krijgt geen gzip', async (path) => {
|
||||
const reply = await page(path, {
|
||||
headers: { 'Accept-Encoding': 'gzip' },
|
||||
});
|
||||
expect(reply.status).toBe(200);
|
||||
expect(reply.headers['content-encoding']).toBeUndefined();
|
||||
});
|
||||
|
||||
it.each(compressed)(
|
||||
'%s krijgt gzip over HTTP/1.0 (zoals de proxy het vraagt)',
|
||||
async (path) => {
|
||||
const reply = await getHttp10(nginx.origin, path, [
|
||||
'Accept-Encoding: gzip',
|
||||
]);
|
||||
expect(reply.head).toMatch(/^HTTP\/1\.\d 200/);
|
||||
expect(reply.head).toMatch(/content-encoding: gzip/i);
|
||||
expect(gunzipSync(reply.body).length).toBeGreaterThan(256);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(plain)(
|
||||
'%s krijgt over HTTP/1.0 geen gzip',
|
||||
async (path) => {
|
||||
const reply = await getHttp10(nginx.origin, path, [
|
||||
'Accept-Encoding: gzip',
|
||||
]);
|
||||
expect(reply.head).toMatch(/^HTTP\/1\.\d 200/);
|
||||
expect(reply.head).not.toMatch(/content-encoding/i);
|
||||
},
|
||||
);
|
||||
|
||||
it('zonder Accept-Encoding blijft het antwoord onbewerkt', async () => {
|
||||
const reply = await page('/assets/index-AbC123xYz.js');
|
||||
expect(reply.headers['content-encoding']).toBeUndefined();
|
||||
expect(reply.body.toString()).toContain(
|
||||
markerOf('assets/index-AbC123xYz.js'),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('vijandige paden (BR-HST-06)', () => {
|
||||
it.each([
|
||||
'//evil.example/',
|
||||
'//evil.example',
|
||||
'/\\evil.example/',
|
||||
'/\\evil.example',
|
||||
'/%5Cevil.example/',
|
||||
'/%2F%2Fevil.example/',
|
||||
'/%0d%0aSet-Cookie:%20x=1/',
|
||||
'/over-mij%0d%0aSet-Cookie:%20x=1/',
|
||||
'/%2e%2e/',
|
||||
])('%s leidt niet naar een ander domein', async (path) => {
|
||||
const reply = await page(path);
|
||||
const location = reply.headers.location;
|
||||
if (reply.status >= 300 && reply.status < 400) {
|
||||
expect(location).toBeDefined();
|
||||
expect(location?.startsWith('//')).toBe(false);
|
||||
expect(location?.startsWith('/\\')).toBe(false);
|
||||
expect(location).not.toMatch(/[\r\n]/);
|
||||
}
|
||||
expect(reply.headers['set-cookie']).toBeUndefined();
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,88 @@
|
||||
import { randomBytes } from 'node:crypto';
|
||||
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { pagePaths } from '../../config/canonicalUrls.ts';
|
||||
import { NOT_FOUND_FILE, pageFile } from '../../config/pageFiles.ts';
|
||||
|
||||
// Een kleine site in de tijdelijke map met dezelfde vorm als dist/client, voor
|
||||
// de serverregeltests: elk paginabestand, de map soorten-fotoshoots/ naast
|
||||
// soorten-fotoshoots.html (BR-HST-07) en assets met hoofdletters in de naam.
|
||||
// Zo hangen die tests niet af van een build.
|
||||
|
||||
export interface FixtureSite {
|
||||
dir: string;
|
||||
// Bestand (relatief aan dir) naar de tekst die erin staat, zodat een test
|
||||
// aan het antwoord kan zien welk bestand geserveerd is
|
||||
markers: ReadonlyMap<string, string>;
|
||||
remove(): Promise<void>;
|
||||
}
|
||||
|
||||
// Alles boven de 256 bytes van gzip_min_length, anders blijft het onbewerkt
|
||||
const padding = ' vulling'.repeat(80);
|
||||
|
||||
export const markerOf = (file: string) => `fixture:${file}`;
|
||||
|
||||
export async function createFixtureSite(): Promise<FixtureSite> {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'andreaslop-fixture-'));
|
||||
const markers = new Map<string, string>();
|
||||
|
||||
const write = async (file: string, content: string | Buffer) => {
|
||||
const target = join(dir, file);
|
||||
await mkdir(dirname(target), { recursive: true });
|
||||
await writeFile(target, content);
|
||||
};
|
||||
const writeMarked = async (
|
||||
file: string,
|
||||
wrap: (text: string) => string,
|
||||
) => {
|
||||
markers.set(file, markerOf(file));
|
||||
await write(file, wrap(markerOf(file)));
|
||||
};
|
||||
|
||||
for (const path of [...pagePaths, null]) {
|
||||
const file = path === null ? NOT_FOUND_FILE : pageFile(path);
|
||||
await writeMarked(
|
||||
file,
|
||||
(marker) =>
|
||||
`<!doctype html><html><body><p>${marker}</p><!--${padding}--></body></html>`,
|
||||
);
|
||||
}
|
||||
await writeMarked(
|
||||
'robots.txt',
|
||||
(marker) => `${marker}\nUser-agent: *\nAllow: /\n`,
|
||||
);
|
||||
await writeMarked(
|
||||
'sitemap.xml',
|
||||
(marker) =>
|
||||
`<?xml version="1.0"?><urlset><!--${marker}${padding}--></urlset>`,
|
||||
);
|
||||
await write('favicon.png', randomBytes(64));
|
||||
await writeMarked(
|
||||
'assets/index-AbC123xYz.js',
|
||||
(marker) => `// ${marker}\n${'console.log("regel");\n'.repeat(200)}`,
|
||||
);
|
||||
await writeMarked(
|
||||
'assets/style-Qw9.css',
|
||||
(marker) => `/* ${marker} */\n${'.a{color:red}\n'.repeat(200)}`,
|
||||
);
|
||||
await writeMarked(
|
||||
'assets/icon-Zz1.svg',
|
||||
(marker) =>
|
||||
`<svg xmlns="http://www.w3.org/2000/svg"><!--${marker}${padding}--></svg>`,
|
||||
);
|
||||
await writeMarked('assets/data.json', (marker) =>
|
||||
JSON.stringify({ marker, vulling: padding }),
|
||||
);
|
||||
await writeMarked(
|
||||
'assets/readme.txt',
|
||||
(marker) => `${marker}\n${'tekst\n'.repeat(200)}`,
|
||||
);
|
||||
await write('assets/font-Ab.woff2', randomBytes(2048));
|
||||
|
||||
return {
|
||||
dir,
|
||||
markers,
|
||||
remove: () => rm(dir, { recursive: true, force: true }),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
import { execFile, spawn, type ChildProcess } from 'node:child_process';
|
||||
import { existsSync, readFileSync, rmSync } from 'node:fs';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { createServer } from 'node:net';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { promisify } from 'node:util';
|
||||
import { nginxTestConf } from '../../config/serverConfig.ts';
|
||||
|
||||
// Start een echte, onbevoorrechte nginx met het gegenereerde testblok
|
||||
// (BR-HST-24). Alleen voor de serverregeltests: eigen prefixmap, eigen poort,
|
||||
// geen systeemconfiguratie.
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
// Waar nginx kan staan: NGINX_BIN, het PATH, de twee bekende Windows-plekken
|
||||
const windowsPaths = [
|
||||
'C:\\nginx\\nginx.exe',
|
||||
'C:\\Users\\Bryan\\scoop\\apps\\nginx\\current\\nginx.exe',
|
||||
];
|
||||
|
||||
export function findNginx(): string | undefined {
|
||||
const fromEnv = process.env.NGINX_BIN;
|
||||
if (fromEnv) return existsSync(fromEnv) ? fromEnv : undefined;
|
||||
const names = process.platform === 'win32' ? ['nginx.exe'] : ['nginx'];
|
||||
const separator = process.platform === 'win32' ? ';' : ':';
|
||||
for (const dir of (process.env.PATH ?? '').split(separator)) {
|
||||
for (const name of names) {
|
||||
if (dir && existsSync(join(dir, name))) return join(dir, name);
|
||||
}
|
||||
}
|
||||
// Op Linux staat nginx soms in sbin, buiten het PATH van een gebruiker
|
||||
for (const path of ['/usr/sbin/nginx', ...windowsPaths]) {
|
||||
if (existsSync(path)) return path;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// nginx 1.24 kent .js als application/javascript, vanaf 1.25.2 als
|
||||
// text/javascript (de Pi draait 1.30.6). Beide tabellen worden getest.
|
||||
export type MimeVariant = 'new' | 'old';
|
||||
|
||||
const mimeTable = (variant: MimeVariant) => `types {
|
||||
text/html html;
|
||||
text/css css;
|
||||
${variant === 'old' ? 'application/javascript' : 'text/javascript'} js;
|
||||
image/svg+xml svg;
|
||||
application/json json;
|
||||
text/xml xml;
|
||||
text/plain txt;
|
||||
image/png png;
|
||||
font/woff2 woff2;
|
||||
}`;
|
||||
|
||||
// Alle nog lopende instanties, voor het opruimen als het proces stopt
|
||||
const running = new Set<{ child: ChildProcess; tmp: string }>();
|
||||
process.on('exit', () => {
|
||||
for (const { child, tmp } of running) {
|
||||
child.kill();
|
||||
try {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Opruimen bij afsluiten mag falen
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// nginx onder Windows wil voorwaartse slashes in de configuratie
|
||||
const slashed = (path: string) => path.replace(/\\/g, '/');
|
||||
|
||||
async function freePort(): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const probe = createServer();
|
||||
probe.once('error', reject);
|
||||
probe.listen(0, '127.0.0.1', () => {
|
||||
const address = probe.address();
|
||||
probe.close(() =>
|
||||
typeof address === 'object' && address
|
||||
? resolve(address.port)
|
||||
: reject(new Error('geen poort gekregen')),
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export interface RunningNginx {
|
||||
origin: string;
|
||||
version: string;
|
||||
stop(): Promise<void>;
|
||||
}
|
||||
|
||||
export async function startNginx(options: {
|
||||
siteDir: string;
|
||||
mimeVariant?: MimeVariant;
|
||||
}): Promise<RunningNginx> {
|
||||
const bin = findNginx();
|
||||
if (!bin) throw new Error('nginx niet gevonden');
|
||||
const { mimeVariant = 'new' } = options;
|
||||
|
||||
const tmp = await mkdtemp(join(tmpdir(), 'andreaslop-nginx-'));
|
||||
const port = await freePort();
|
||||
const serverFile = join(tmp, 'andreaslop-test.conf');
|
||||
const confFile = join(tmp, 'nginx.conf');
|
||||
const errorLog = join(tmp, 'error.log');
|
||||
const temp = (name: string) => `"${slashed(join(tmp, name))}"`;
|
||||
|
||||
await writeFile(
|
||||
serverFile,
|
||||
nginxTestConf({
|
||||
listen: `127.0.0.1:${port}`,
|
||||
root: slashed(options.siteDir),
|
||||
}),
|
||||
);
|
||||
await writeFile(
|
||||
confFile,
|
||||
`daemon off;
|
||||
pid "${slashed(join(tmp, 'nginx.pid'))}";
|
||||
error_log "${slashed(errorLog)}";
|
||||
events {}
|
||||
http {
|
||||
${mimeTable(mimeVariant)}
|
||||
client_body_temp_path ${temp('client_body')};
|
||||
proxy_temp_path ${temp('proxy')};
|
||||
fastcgi_temp_path ${temp('fastcgi')};
|
||||
uwsgi_temp_path ${temp('uwsgi')};
|
||||
scgi_temp_path ${temp('scgi')};
|
||||
access_log off;
|
||||
include "${slashed(serverFile)}";
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
// nginx -v schrijft naar stderr
|
||||
const { stdout, stderr } = await execFileAsync(bin, ['-v']);
|
||||
const version = (stdout + stderr).trim();
|
||||
|
||||
await execFileAsync(bin, [
|
||||
'-t',
|
||||
'-p',
|
||||
`${slashed(tmp)}/`,
|
||||
'-c',
|
||||
slashed(confFile),
|
||||
'-e',
|
||||
slashed(errorLog),
|
||||
]).catch((error: { stderr?: string }) => {
|
||||
throw new Error(`nginx -t keurt de configuratie af:\n${error.stderr}`);
|
||||
});
|
||||
|
||||
const child = spawn(
|
||||
bin,
|
||||
[
|
||||
'-p',
|
||||
`${slashed(tmp)}/`,
|
||||
'-c',
|
||||
slashed(confFile),
|
||||
'-e',
|
||||
slashed(errorLog),
|
||||
],
|
||||
{ stdio: 'ignore' },
|
||||
);
|
||||
const entry = { child, tmp };
|
||||
running.add(entry);
|
||||
let exited = false;
|
||||
child.once('exit', () => {
|
||||
exited = true;
|
||||
});
|
||||
|
||||
const stop = async () => {
|
||||
if (!exited) {
|
||||
const done = new Promise<void>((resolve) =>
|
||||
child.once('exit', () => resolve()),
|
||||
);
|
||||
child.kill();
|
||||
// Mocht nginx op SIGTERM niet reageren, dan alsnog hard stoppen
|
||||
const timer = setTimeout(() => child.kill('SIGKILL'), 5000);
|
||||
await done;
|
||||
clearTimeout(timer);
|
||||
}
|
||||
running.delete(entry);
|
||||
await rm(tmp, { recursive: true, force: true });
|
||||
};
|
||||
|
||||
const origin = `http://127.0.0.1:${port}`;
|
||||
const deadline = Date.now() + 5000;
|
||||
for (;;) {
|
||||
if (exited) {
|
||||
const log = existsSync(errorLog)
|
||||
? readFileSync(errorLog, 'utf8')
|
||||
: '(geen foutlog)';
|
||||
await stop();
|
||||
throw new Error(`nginx stopte bij het starten:\n${log}`);
|
||||
}
|
||||
try {
|
||||
await fetch(origin + '/');
|
||||
break;
|
||||
} catch {
|
||||
if (Date.now() > deadline) {
|
||||
await stop();
|
||||
throw new Error('nginx antwoordde niet binnen 5 seconden');
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
}
|
||||
}
|
||||
|
||||
return { origin, version, stop };
|
||||
}
|
||||
Reference in New Issue
Block a user