@runeditrun/rer 0.2.78 → 0.2.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli/server-proxy.mjs
CHANGED
|
@@ -5,11 +5,12 @@ import { posix } from 'node:path';
|
|
|
5
5
|
import { listen } from './listen.mjs';
|
|
6
6
|
import { loopback } from './parse.mjs';
|
|
7
7
|
import { fontResponseHeaders } from './font-cache.mjs';
|
|
8
|
+
import { UpstreamAgent } from './upstream-agent.mjs';
|
|
8
9
|
|
|
9
10
|
// The public listener observes transport lifetime without buffering or replacing
|
|
10
11
|
// application responses. Preserve Host, Origin, cookies, status and streaming.
|
|
11
|
-
//
|
|
12
|
-
//
|
|
12
|
+
// Share bounded upstream connections. A runtime closing an idle socket races
|
|
13
|
+
// reuse; retry only a bodyless read, once, before any response bytes arrive.
|
|
13
14
|
// In door mode (`door`: {host, header, secret}) it answers only requests whose
|
|
14
15
|
// Host is its own `<name>.localhost:<port>` (or a subdomain) and that carry the
|
|
15
16
|
// per-start secret the front door adds; cookies are not scoped by port, so
|
|
@@ -24,17 +25,25 @@ import { fontResponseHeaders } from './font-cache.mjs';
|
|
|
24
25
|
// anyway, and an auth check that throws on it must not answer with a crash page.
|
|
25
26
|
export async function trafficProxy({host,port,target,activity,door,devKey,origin}) {
|
|
26
27
|
const probe=randomUUID(),sockets=new Set(),keyed=keyHolder(devKey),configured=origin?new URL(origin).host:null;
|
|
28
|
+
const agent=new UpstreamAgent();let closing=false;
|
|
27
29
|
const server=http.createServer((req,res)=>{
|
|
28
30
|
if(door&&!doorAdmits(req,door)){req.resume();res.writeHead(403,{'content-type':'text/plain; charset=utf-8'});res.end('This checkout answers only through its front door.');return;}
|
|
29
31
|
if(!hostAdmits(req,configured)){req.resume();res.writeHead(421,{'content-type':'text/plain; charset=utf-8'});res.end('This local server answers only its own host.');return;}
|
|
30
32
|
if(!publicPath(req.url)||fsPath(req.url)&&!keyed(req)){req.resume();notFound(res);return;}
|
|
31
33
|
const internal=req.headers['x-rer-probe']===probe;delete req.headers['x-rer-probe'];
|
|
32
|
-
const
|
|
34
|
+
const endActivity=internal?()=>{}:activity.begin();let finished=false;
|
|
35
|
+
const finish=()=>{if(!finished){finished=true;endActivity();}};
|
|
33
36
|
let requestEnded=req.complete,responseEnded=false;
|
|
34
37
|
const completed=()=>{if(requestEnded&&responseEnded)finish();};
|
|
35
38
|
req.once('end',()=>{requestEnded=true;completed();});req.once('aborted',finish);
|
|
36
39
|
res.once('close',()=>{responseEnded=true;if(req.destroyed)finish();else completed();});res.once('finish',()=>{responseEnded=true;completed();});
|
|
37
|
-
|
|
40
|
+
let upstream,bodyStarted=false;
|
|
41
|
+
req.on('data',()=>{bodyStarted=true;});
|
|
42
|
+
const bodyless=['GET','HEAD','OPTIONS'].includes(req.method)&&!req.headers['transfer-encoding']&&(!req.headers['content-length']||req.headers['content-length']==='0');
|
|
43
|
+
function send(retried=false){
|
|
44
|
+
let responseStarted=false,readAtStart;
|
|
45
|
+
const attempt=http.request({host:'127.0.0.1',port:target,path:req.url,method:req.method,headers:req.headers,agent:retried?false:agent},reply=>{
|
|
46
|
+
responseStarted=true;
|
|
38
47
|
// Vite answers a path outside its serving allow list with a 403 page that lists the
|
|
39
48
|
// allow list (checkout and home paths); a request without the dev key gets 404 instead.
|
|
40
49
|
// Any other 403 is the application's own and passes through.
|
|
@@ -51,8 +60,21 @@ export async function trafficProxy({host,port,target,activity,door,devKey,origin
|
|
|
51
60
|
}
|
|
52
61
|
res.writeHead(reply.statusCode,fontResponseHeaders(req.url,reply.statusCode,reply.rawHeaders,req.headers));reply.pipe(res);
|
|
53
62
|
});
|
|
54
|
-
|
|
55
|
-
|
|
63
|
+
upstream=attempt;
|
|
64
|
+
attempt.once('socket',socket=>{readAtStart=socket.bytesRead;});
|
|
65
|
+
attempt.on('error',error=>{
|
|
66
|
+
if(closing||req.aborted||res.destroyed)return;
|
|
67
|
+
const dropped=['ECONNRESET','EPIPE'].includes(error.code)||error.message==='socket hang up';
|
|
68
|
+
if(!retried&&dropped&&attempt.reusedSocket&&!responseStarted&&attempt.socket?.bytesRead===readAtStart&&bodyless&&!bodyStarted&&req.readableEnded){
|
|
69
|
+
req.unpipe(attempt);send(true);return;
|
|
70
|
+
}
|
|
71
|
+
if(!res.headersSent)notFound(res);else res.destroy();
|
|
72
|
+
});
|
|
73
|
+
if(retried)attempt.end();else req.pipe(attempt);
|
|
74
|
+
}
|
|
75
|
+
req.once('aborted',()=>upstream.destroy());
|
|
76
|
+
res.once('close',()=>{if(!res.writableFinished)upstream.destroy();});
|
|
77
|
+
send();
|
|
56
78
|
});
|
|
57
79
|
server.on('connection',socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));});
|
|
58
80
|
server.on('upgrade',(req,socket,head)=>{
|
|
@@ -79,8 +101,8 @@ export async function trafficProxy({host,port,target,activity,door,devKey,origin
|
|
|
79
101
|
}
|
|
80
102
|
upstream.on('error',()=>socket.destroy());socket.on('error',()=>upstream.destroy());socket.on('close',()=>upstream.destroy());upstream.on('close',()=>socket.destroy());
|
|
81
103
|
});
|
|
82
|
-
await listen(server,host,port);
|
|
83
|
-
return {probe,port:server.address().port,async close(){for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));}};
|
|
104
|
+
try{await listen(server,host,port);}catch(error){agent.destroy();throw error;}
|
|
105
|
+
return {probe,port:server.address().port,async close(){closing=true;agent.destroy();for(const socket of sockets)socket.destroy();await new Promise(resolve=>server.close(resolve));}};
|
|
84
106
|
}
|
|
85
107
|
function doorAdmits(req,door){
|
|
86
108
|
const host=String(req.headers.host??'').toLowerCase(),given=Buffer.from(String(req.headers[door.header]??'')),secret=Buffer.from(door.secret);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
|
|
3
|
+
// Expire only idle connections, well before Vite's 5 s keep-alive timeout.
|
|
4
|
+
// Keep a browser's six HTTP/1 connections free between module bursts.
|
|
5
|
+
// Active streams have no deadline imposed by the pool.
|
|
6
|
+
export class UpstreamAgent extends http.Agent {
|
|
7
|
+
constructor(){super({keepAlive:true,maxSockets:16,maxFreeSockets:6});}
|
|
8
|
+
keepSocketAlive(socket){
|
|
9
|
+
if(!super.keepSocketAlive(socket))return false;
|
|
10
|
+
socket.setTimeout(1000);
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
reuseSocket(socket,request){socket.setTimeout(0);super.reuseSocket(socket,request);}
|
|
14
|
+
}
|
package/global/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.79
|
|
4
|
+
|
|
5
|
+
- Reuse bounded keep-alive connections in the local dev proxy and retry stale connections for bodyless reads.
|
|
6
|
+
- Give each native demo visitor a separate 64-hex installation key so dependency signing works.
|
|
7
|
+
|
|
3
8
|
## 0.2.78
|
|
4
9
|
|
|
5
10
|
- Add signed seed test delivery to the optional mail relay, with public Worker routing, bounded target checks and invocation logs.
|
|
@@ -34,7 +34,7 @@ export class NativeDemo extends DurableObject {
|
|
|
34
34
|
// Short TTLs are usable only on the guarded testing host.
|
|
35
35
|
if (/^[123]x0{31}AA$/.test(this.env.TURNSTILE_SECRET_KEY ?? '') && this.env.NATIVE_TEST_TTL_MS) ends = Math.min(ends, started + Number(this.env.NATIVE_TEST_TTL_MS));
|
|
36
36
|
const runtime = { ...bundle.vars, ...settings, RER_DEMO: '1', [originVar]: origin,
|
|
37
|
-
RER_NATIVE_KEY: random(), SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: this.env.DEMO_OWNER_EMAIL || 'owner@demo.invalid',
|
|
37
|
+
RER_NATIVE_KEY: random(), SESSION_SECRET: random(), INSTALLATION_KEY: random(), SETUP_TOKEN: random(), OWNER_EMAIL: this.env.DEMO_OWNER_EMAIL || 'owner@demo.invalid',
|
|
38
38
|
RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
|
|
39
39
|
RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal };
|
|
40
40
|
this.state = { phase: 'starting', runtime, ends, seen: started, savedSeen: started, lastCron: Math.floor(started / 60_000), correlation };
|