From 442b3f0a56b1577bd0bb3a8ce161b20c9af528cf Mon Sep 17 00:00:00 2001 From: Pierre MARTIN Date: Mon, 23 Nov 2026 03:00:34 +0100 Subject: [PATCH] =?UTF-8?q?Test=20navigateur=20isol=C3=A9=20du=20r=C3=A9se?= =?UTF-8?q?au=20de=20la=20machine=20;=20zstd=20seulement=20si=20Node=20le?= =?UTF-8?q?=20sait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le conteneur Playwright partageait le réseau de la machine : quand d'autres conteneurs démarraient, Chromium abandonnait ses chargements (net::ERR_NETWORK_CHANGED). Il a maintenant son réseau, et le vrai serveur tourne à côté de lui. Ce Node (20) n'a pas zstd : le serveur renvoyait 500. Co-Authored-By: Claude Opus 5.5 --- Makefile | 14 ++++++-------- server/static.js | 11 ++++++++--- tests/browser/run.sh | 24 ++++++++++++++++++++++++ tests/server/static.test.js | 5 ++++- 4 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 tests/browser/run.sh diff --git a/Makefile b/Makefile index 03c8216..d6d9bc5 100644 --- a/Makefile +++ b/Makefile @@ -110,15 +110,13 @@ test-browser: $(PLAYWRIGHT) $(QR_READER) ## Parcours Playwright de l'app, captur @tmp="$$(mktemp -d)"; data="$$tmp/data"; site="$$tmp/build/site"; \ trap 'kill $$pid 2>/dev/null; rm -rf -- "$$tmp"' EXIT; \ $(MAKE) --no-print-directory site SITE_BUILD="$$tmp/build" >/dev/null || exit 1; \ - port="$$(node -e 'const s=require("node:net").createServer().listen(0,"127.0.0.1",()=>{console.log(s.address().port);s.close()})')"; \ - PORT="$$port" SITE_DIR="$$site" DATA_DIR="$$data" PUBLIC_URL="http://127.0.0.1:$$port" \ - node server/main.js >"$$tmp/server.log" 2>&1 & pid=$$!; \ - for _ in $$(seq 50); do curl -sf -o /dev/null "http://127.0.0.1:$$port/" && break; sleep 0.1; done; \ - curl -sf -o /dev/null "http://127.0.0.1:$$port/" || { echo "Le serveur ne démarre pas :"; cat "$$tmp/server.log"; exit 1; }; \ - docker run --rm --init --ipc=host --network host -u "$$(id -u):$$(id -g)" -e HOME=/tmp \ - -v "$(CURDIR)/$(APP_DIR):/app:ro" -v "$(CURDIR)/tests/browser:/tests:ro" -v "$$site:/site" \ + mkdir -p "$$data"; \ + docker run --rm --init --ipc=host --network bridge -u "$$(id -u):$$(id -g)" -e HOME=/tmp \ + -v "$(CURDIR)/$(APP_DIR):/repo/app:ro" -v "$(CURDIR)/server:/repo/server:ro" -v "$(CURDIR)/package.json:/repo/package.json:ro" \ + -v "$(CURDIR)/tests/browser:/tests:ro" \ + -v "$$site:/site" -v "$$data:/data" \ -v "$(CURDIR)/$(BROWSER_TOOLS):/tools:ro" -v "$(CURDIR)/build/shots:/shots" \ - $(PLAYWRIGHT_IMAGE) node /tests/parcours.cjs /app /shots "http://127.0.0.1:$$port/" + $(PLAYWRIGHT_IMAGE) sh /tests/run.sh test-site: ## Construit l'image de production en local et vérifie ce qu'elle sert (Docker) @tests/test_site.sh diff --git a/server/static.js b/server/static.js index 8a4ff34..d2b6d50 100644 --- a/server/static.js +++ b/server/static.js @@ -34,12 +34,17 @@ const COMPRESSIBLE = /^(text\/|application\/json|application\/manifest\+json|ima /** Comme Caddy : en dessous, la compression ne fait rien gagner. */ const MIN_COMPRESS = 512; +/** zstd n'existe dans Node qu'à partir de 22.15 (l'image de production a Node 24). */ +export const HAS_ZSTD = typeof (/** @type {any} */ (zlib)).zstdCompressSync === "function"; + /** - * Encodage choisi d'après Accept-Encoding : zstd, sinon gzip, sinon aucun. + * Encodage choisi d'après Accept-Encoding : zstd (si ce Node le sait), sinon gzip, + * sinon aucun. * @param {string|undefined} header + * @param {boolean} [zstd] ce Node sait compresser en zstd * @returns {"zstd"|"gzip"|null} */ -export function pickEncoding(header) { +export function pickEncoding(header, zstd = HAS_ZSTD) { /** @type {Map} */ const accepted = new Map(); for (const part of (header ?? "").split(",")) { @@ -49,7 +54,7 @@ export function pickEncoding(header) { accepted.set(name, q ? Number(q[1]) : 1); } const ok = (/** @type {string} */ name) => (accepted.get(name) ?? accepted.get("*") ?? 0) > 0; - if (ok("zstd")) return "zstd"; + if (zstd && ok("zstd")) return "zstd"; if (ok("gzip")) return "gzip"; return null; } diff --git a/tests/browser/run.sh b/tests/browser/run.sh new file mode 100644 index 0000000..144ef65 --- /dev/null +++ b/tests/browser/run.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# Lancé par make test-browser dans le conteneur Playwright, sur son propre réseau +# (--network bridge) : il a ses interfaces à lui. Avant, le conteneur partageait le réseau de la machine +# (--network host) : quand d'autres conteneurs démarraient ou s'arrêtaient, Chromium +# voyait les interfaces changer et abandonnait ses chargements +# (net::ERR_NETWORK_CHANGED, page restée vide après un rechargement). Sans réseau du +# tout (--network none), Chromium se croit hors ligne et la synchro ne part pas. +# Le vrai serveur (synchro, MCP, site assemblé) tourne donc ici, à côté du navigateur. +set -eu +port=8787 +PORT="$port" SITE_DIR=/site DATA_DIR=/data PUBLIC_URL="http://127.0.0.1:$port" \ + node /repo/server/main.js >/data/server.log 2>&1 & +pid=$! +trap 'kill $pid 2>/dev/null || true' EXIT +# Attendre que le serveur réponde (pas de curl dans l'image : node suffit). +node -e ' + const url = process.argv[1]; + const until = Date.now() + 10000; + (async function wait() { + try { if ((await fetch(url)).ok) process.exit(0); } catch {} + if (Date.now() > until) process.exit(1); + setTimeout(wait, 100); + })();' "http://127.0.0.1:$port/" || { echo "Le serveur ne démarre pas :"; cat /data/server.log; exit 1; } +node /tests/parcours.cjs /repo/app /shots "http://127.0.0.1:$port/" diff --git a/tests/server/static.test.js b/tests/server/static.test.js index 35e3e6c..5ebbaa2 100644 --- a/tests/server/static.test.js +++ b/tests/server/static.test.js @@ -50,7 +50,10 @@ test("site : compression zstd ou gzip des textes, pas des polices", async () => const f = /** @type {any} */ (await get("/police.woff2", "gzip")); assert.equal(f.headers["content-encoding"], undefined); assert.equal(pickEncoding("gzip;q=0, zstd;q=0"), null); - assert.equal(pickEncoding("*"), "zstd"); + assert.equal(pickEncoding("*", true), "zstd"); + // Un Node sans zstd (avant 22.15) : gzip, plutôt qu'une erreur 500. + assert.equal(pickEncoding("zstd, gzip", false), "gzip"); + assert.equal(pickEncoding("zstd", false), null); assert.equal(pickEncoding(undefined), null); } finally { await s.close();