fix(app): retain idle preview sockets and release auth listeners

This commit is contained in:
2026-10-21 13:37:18 +01:00
parent 5cd269fca4
commit ae352bb9c6
4 changed files with 17 additions and 5 deletions
+5
View File
@@ -1,5 +1,10 @@
# F5 preview server candidate review # F5 preview server candidate review
Manager reran all 6 fixture tests and added an idle-websocket regression past the
five-second establishment deadline. Healthy idle sockets now keep their session;
auth-check listeners are removed when finished. Default preview port is 8096,
separate from the raw API at 8080. HTTPS certificate paths remain untested.
Candidate `e3a82b0`'s static preview route was retained, with stricter decoded Candidate `e3a82b0`'s static preview route was retained, with stricter decoded
path validation and canonical-path checks for files and SPA fallback. The proxy path validation and canonical-path checks for files and SPA fallback. The proxy
now uses `http.request` / `https.request` streams so Range, HEAD, content-encoding, now uses `http.request` / `https.request` streams so Range, HEAD, content-encoding,
+2
View File
@@ -117,6 +117,8 @@ test('authenticates and maps same-origin WebSocket Origin; bytes flow both direc
const upgraded = seen.find((item) => item.upgraded); const upgraded = seen.find((item) => item.upgraded);
assert.equal(upgraded.headers.origin, new URL(`http://${seen[0].headers.host}`).origin); assert.equal(upgraded.headers.origin, new URL(`http://${seen[0].headers.host}`).origin);
assert.equal(upgraded.headers.cookie, 'sid=yes'); assert.equal(upgraded.headers.cookie, 'sid=yes');
await new Promise((resolve) => setTimeout(resolve, 5200));
assert.equal(socket.destroyed, false, 'healthy idle websocket must not time out');
const payload = Buffer.from([0x81, 0x02, 0x6f, 0x6b]); const payload = Buffer.from([0x81, 0x02, 0x6f, 0x6b]);
socket.write(payload); socket.write(payload);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
+1 -1
View File
@@ -4,7 +4,7 @@ Build and run from `app/`:
```sh ```sh
/Users/alan/development/flutter/bin/flutter build web /Users/alan/development/flutter/bin/flutter build web
UPSTREAM=http://127.0.0.1:8095 node tool/preview/server.mjs --port 8080 UPSTREAM=http://127.0.0.1:8095 node tool/preview/server.mjs --port 8096
``` ```
The preview binds to loopback by default and authenticates static requests by The preview binds to loopback by default and authenticates static requests by
+9 -4
View File
@@ -81,8 +81,11 @@ function authCheck(req, target, callback) {
const finish = (result) => { const finish = (result) => {
if (completed) return; if (completed) return;
completed = true; completed = true;
req.removeListener('aborted', abort);
req.socket.removeListener('close', abort);
callback(result); callback(result);
}; };
const abort = () => check.destroy();
const check = upstreamRequest(target, { method: 'GET', path: '/', headers: { const check = upstreamRequest(target, { method: 'GET', path: '/', headers: {
cookie: req.headers.cookie ?? '', accept: 'text/html', 'accept-encoding': 'identity', cookie: req.headers.cookie ?? '', accept: 'text/html', 'accept-encoding': 'identity',
} }, (response) => { } }, (response) => {
@@ -91,8 +94,8 @@ function authCheck(req, target, callback) {
}); });
check.setTimeout(timeoutMs, () => check.destroy(new Error('authentication check timeout'))); check.setTimeout(timeoutMs, () => check.destroy(new Error('authentication check timeout')));
check.on('error', () => finish(null)); check.on('error', () => finish(null));
req.on('aborted', () => check.destroy()); req.once('aborted', abort);
req.socket.once('close', () => { if (!completed) check.destroy(); }); req.socket.once('close', abort);
check.end(); check.end();
} }
@@ -100,7 +103,7 @@ function reject(socket, status, phrase = 'Forbidden') {
if (!socket.destroyed) socket.end(`HTTP/1.1 ${status} ${phrase}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`); if (!socket.destroyed) socket.end(`HTTP/1.1 ${status} ${phrase}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
} }
export async function start({ root = resolve('build/web'), upstream = 'http://127.0.0.1:8095', host = '127.0.0.1', port = 8080 } = {}) { export async function start({ root = resolve('build/web'), upstream = 'http://127.0.0.1:8095', host = '127.0.0.1', port = 8096 } = {}) {
const base = await realpath(root); const base = await realpath(root);
const target = new URL(upstream); const target = new URL(upstream);
if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) throw new Error('UPSTREAM must be credential-free HTTP(S)'); if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) throw new Error('UPSTREAM must be credential-free HTTP(S)');
@@ -157,6 +160,8 @@ export async function start({ root = resolve('build/web'), upstream = 'http://12
upgrades.add(upstreamSocket); upgrades.add(upstreamSocket);
upstreamSocket.on('close', () => upgrades.delete(upstreamSocket)); upstreamSocket.on('close', () => upgrades.delete(upstreamSocket));
upstreamSocket.setTimeout(timeoutMs, () => upstreamSocket.destroy(new Error('upgrade timeout'))); upstreamSocket.setTimeout(timeoutMs, () => upstreamSocket.destroy(new Error('upgrade timeout')));
// Bound establishment only. A healthy, idle chat socket stays connected.
upstreamSocket.once('data', () => upstreamSocket.setTimeout(0));
upstreamSocket.once(target.protocol === 'https:' ? 'secureConnect' : 'connect', () => { upstreamSocket.once(target.protocol === 'https:' ? 'secureConnect' : 'connect', () => {
const headers = { ...req.headers, host: target.host, origin: target.origin }; const headers = { ...req.headers, host: target.host, origin: target.origin };
const lines = Object.entries(headers).filter(([, value]) => value != null) const lines = Object.entries(headers).filter(([, value]) => value != null)
@@ -191,7 +196,7 @@ if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).
if (value.startsWith('--')) result.push([value.slice(2), all[index + 1]]); if (value.startsWith('--')) result.push([value.slice(2), all[index + 1]]);
return result; return result;
}, [])); }, []));
start({ root: args.root ?? 'build/web', upstream: process.env.UPSTREAM ?? args.upstream ?? 'http://127.0.0.1:8095', host: args.host ?? '127.0.0.1', port: Number(args.port ?? 8080) }) start({ root: args.root ?? 'build/web', upstream: process.env.UPSTREAM ?? args.upstream ?? 'http://127.0.0.1:8095', host: args.host ?? '127.0.0.1', port: Number(args.port ?? 8096) })
.then((preview) => console.log(`preview http://${args.host ?? '127.0.0.1'}:${preview.port}`)) .then((preview) => console.log(`preview http://${args.host ?? '127.0.0.1'}:${preview.port}`))
.catch((error) => { console.error(error); process.exitCode = 1; }); .catch((error) => { console.error(error); process.exitCode = 1; });
} }