@stacksjs/rpx 0.11.13 → 0.11.15
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/dist/bin/cli.js +178 -174
- package/dist/chunk-0zdj72ps.js +161 -0
- package/dist/{chunk-zs1tyy8z.js → chunk-hf6e07v4.js} +1 -1
- package/dist/{chunk-747af2w4.js → chunk-kv17r01q.js} +1 -1
- package/dist/chunk-pjwm8py7.js +1 -0
- package/dist/daemon-runner.d.ts +3 -2
- package/dist/daemon.d.ts +7 -1
- package/dist/host-match.d.ts +23 -0
- package/dist/index.d.ts +15 -2
- package/dist/index.js +6 -6
- package/dist/on-demand.d.ts +40 -0
- package/dist/proxy-handler.d.ts +18 -3
- package/dist/registry.d.ts +3 -2
- package/dist/sni.d.ts +20 -0
- package/dist/static-files.d.ts +46 -0
- package/dist/types.d.ts +64 -1
- package/package.json +2 -2
- package/src/daemon-runner.ts +12 -4
- package/src/daemon.ts +154 -21
- package/src/host-match.ts +52 -0
- package/src/index.ts +19 -2
- package/src/on-demand.ts +264 -0
- package/src/proxy-handler.ts +184 -21
- package/src/registry.ts +11 -3
- package/src/sni.ts +93 -0
- package/src/start.ts +66 -19
- package/src/static-files.ts +201 -0
- package/src/types.ts +138 -1
- package/dist/chunk-kbnzcycw.js +0 -1
- package/dist/chunk-pncxrxde.js +0 -157
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static-file serving for proxy routes.
|
|
3
|
+
*
|
|
4
|
+
* A route configured with `static` serves files from a local directory instead
|
|
5
|
+
* of forwarding to an upstream. Path resolution is split into a pure function
|
|
6
|
+
* (`resolveStaticFile`) so it's trivially unit-testable, and a thin `Bun.file`
|
|
7
|
+
* wrapper (`serveStaticFile`) that does the actual I/O.
|
|
8
|
+
*/
|
|
9
|
+
import type { PathRewriteStyle, StaticRouteConfig } from './types'
|
|
10
|
+
import * as path from 'node:path'
|
|
11
|
+
|
|
12
|
+
/** Normalized static-route config (shorthand string already expanded). */
|
|
13
|
+
export interface ResolvedStaticRoute {
|
|
14
|
+
dir: string
|
|
15
|
+
spa: boolean
|
|
16
|
+
pathRewriteStyle: PathRewriteStyle
|
|
17
|
+
maxAge: number
|
|
18
|
+
cleanUrls: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function resolveStaticRoute(
|
|
22
|
+
cfg: string | StaticRouteConfig,
|
|
23
|
+
cleanUrls: boolean,
|
|
24
|
+
): ResolvedStaticRoute {
|
|
25
|
+
if (typeof cfg === 'string')
|
|
26
|
+
return { dir: cfg, spa: false, pathRewriteStyle: 'directory', maxAge: 0, cleanUrls }
|
|
27
|
+
return {
|
|
28
|
+
dir: cfg.dir,
|
|
29
|
+
spa: cfg.spa ?? false,
|
|
30
|
+
pathRewriteStyle: cfg.pathRewriteStyle ?? 'directory',
|
|
31
|
+
maxAge: cfg.maxAge ?? 0,
|
|
32
|
+
cleanUrls,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A minimal extension → MIME map covering the common web asset types. */
|
|
37
|
+
const MIME_TYPES: Record<string, string> = {
|
|
38
|
+
'.html': 'text/html; charset=utf-8',
|
|
39
|
+
'.htm': 'text/html; charset=utf-8',
|
|
40
|
+
'.css': 'text/css; charset=utf-8',
|
|
41
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
42
|
+
'.mjs': 'text/javascript; charset=utf-8',
|
|
43
|
+
'.json': 'application/json; charset=utf-8',
|
|
44
|
+
'.map': 'application/json; charset=utf-8',
|
|
45
|
+
'.svg': 'image/svg+xml',
|
|
46
|
+
'.png': 'image/png',
|
|
47
|
+
'.jpg': 'image/jpeg',
|
|
48
|
+
'.jpeg': 'image/jpeg',
|
|
49
|
+
'.gif': 'image/gif',
|
|
50
|
+
'.webp': 'image/webp',
|
|
51
|
+
'.avif': 'image/avif',
|
|
52
|
+
'.ico': 'image/x-icon',
|
|
53
|
+
'.woff': 'font/woff',
|
|
54
|
+
'.woff2': 'font/woff2',
|
|
55
|
+
'.ttf': 'font/ttf',
|
|
56
|
+
'.otf': 'font/otf',
|
|
57
|
+
'.eot': 'application/vnd.ms-fontobject',
|
|
58
|
+
'.txt': 'text/plain; charset=utf-8',
|
|
59
|
+
'.xml': 'application/xml; charset=utf-8',
|
|
60
|
+
'.pdf': 'application/pdf',
|
|
61
|
+
'.wasm': 'application/wasm',
|
|
62
|
+
'.mp4': 'video/mp4',
|
|
63
|
+
'.webm': 'video/webm',
|
|
64
|
+
'.mp3': 'audio/mpeg',
|
|
65
|
+
'.wav': 'audio/wav',
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function contentTypeFor(filePath: string): string {
|
|
69
|
+
const ext = path.extname(filePath).toLowerCase()
|
|
70
|
+
return MIME_TYPES[ext] ?? 'application/octet-stream'
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Decode + normalize a URL pathname into a safe relative path.
|
|
75
|
+
*
|
|
76
|
+
* Traversal safety: normalizing against a leading `/` collapses every `..`
|
|
77
|
+
* segment and clamps at the root, so the returned relative path never contains
|
|
78
|
+
* `..` and `path.join(root, rel)` can't escape `root`. Backslash, NUL and
|
|
79
|
+
* malformed percent-encoding are rejected outright (return `null`); the
|
|
80
|
+
* residual `..` guard is belt-and-suspenders.
|
|
81
|
+
*/
|
|
82
|
+
export function safeRelativePath(pathname: string): string | null {
|
|
83
|
+
let decoded: string
|
|
84
|
+
try {
|
|
85
|
+
decoded = decodeURIComponent(pathname)
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
// Reject NUL and backslash (Windows-style) escapes outright.
|
|
91
|
+
if (decoded.includes('\0') || decoded.includes('\\'))
|
|
92
|
+
return null
|
|
93
|
+
|
|
94
|
+
// `path.posix.normalize` collapses `..`/`.`; a leading `/` keeps it rooted so
|
|
95
|
+
// a normalized result that still contains `..` means traversal above root.
|
|
96
|
+
const normalized = path.posix.normalize(`/${decoded}`)
|
|
97
|
+
if (normalized.includes('..'))
|
|
98
|
+
return null
|
|
99
|
+
// Strip the leading slash to get a path relative to the static root.
|
|
100
|
+
return normalized.replace(/^\/+/, '')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface StaticResolution {
|
|
104
|
+
/** Absolute file path to attempt to serve. */
|
|
105
|
+
filePath: string
|
|
106
|
+
/** When set, the request should 301-redirect to this clean URL. */
|
|
107
|
+
redirectTo?: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Pure resolution of an incoming request pathname to a candidate file path on
|
|
112
|
+
* disk. Does no I/O; the caller checks existence and may fall back (SPA).
|
|
113
|
+
*
|
|
114
|
+
* Rules:
|
|
115
|
+
* - A trailing `/` (or root) resolves to `index.html` in that directory.
|
|
116
|
+
* - `cleanUrls` + a `.html` request → 301 to the extensionless URL.
|
|
117
|
+
* - Extensionless paths resolve per `pathRewriteStyle`:
|
|
118
|
+
* - `directory`: `/about` → `about/index.html`
|
|
119
|
+
* - `flat`: `/about` → `about.html`
|
|
120
|
+
* - Paths with a real extension (`.css`, `.png`, …) map straight through.
|
|
121
|
+
*
|
|
122
|
+
* Returns `null` when the path is unsafe (traversal attempt).
|
|
123
|
+
*/
|
|
124
|
+
export function resolveStaticFile(
|
|
125
|
+
pathname: string,
|
|
126
|
+
route: ResolvedStaticRoute,
|
|
127
|
+
): StaticResolution | null {
|
|
128
|
+
const rel = safeRelativePath(pathname)
|
|
129
|
+
if (rel === null)
|
|
130
|
+
return null
|
|
131
|
+
|
|
132
|
+
const ext = path.posix.extname(rel)
|
|
133
|
+
|
|
134
|
+
// `cleanUrls`: redirect explicit `.html` requests to the clean URL.
|
|
135
|
+
if (route.cleanUrls && ext === '.html') {
|
|
136
|
+
const clean = pathname.replace(/\/index\.html$/i, '/').replace(/\.html$/i, '')
|
|
137
|
+
return { filePath: path.join(route.dir, rel), redirectTo: clean || '/' }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Directory or root request → index.html.
|
|
141
|
+
if (rel === '' || pathname.endsWith('/'))
|
|
142
|
+
return { filePath: path.join(route.dir, rel, 'index.html') }
|
|
143
|
+
|
|
144
|
+
// Asset with a concrete extension → serve directly.
|
|
145
|
+
if (ext !== '')
|
|
146
|
+
return { filePath: path.join(route.dir, rel) }
|
|
147
|
+
|
|
148
|
+
// Extensionless route → resolve by SSG style.
|
|
149
|
+
if (route.pathRewriteStyle === 'flat')
|
|
150
|
+
return { filePath: path.join(route.dir, `${rel}.html`) }
|
|
151
|
+
return { filePath: path.join(route.dir, rel, 'index.html') }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Serve a static file for the matched route. Returns a 301 for clean-URL
|
|
156
|
+
* redirects, the file with the right `Content-Type`/`Cache-Control` when it
|
|
157
|
+
* exists, the SPA `index.html` fallback when configured, or 404.
|
|
158
|
+
*/
|
|
159
|
+
export async function serveStaticFile(
|
|
160
|
+
pathname: string,
|
|
161
|
+
route: ResolvedStaticRoute,
|
|
162
|
+
): Promise<Response> {
|
|
163
|
+
const resolution = resolveStaticFile(pathname, route)
|
|
164
|
+
if (!resolution)
|
|
165
|
+
return new Response('Forbidden', { status: 403 })
|
|
166
|
+
|
|
167
|
+
if (resolution.redirectTo)
|
|
168
|
+
return new Response(null, { status: 301, headers: { Location: resolution.redirectTo } })
|
|
169
|
+
|
|
170
|
+
const cacheControl = route.maxAge > 0
|
|
171
|
+
? `public, max-age=${route.maxAge}`
|
|
172
|
+
: 'no-cache'
|
|
173
|
+
|
|
174
|
+
const file = Bun.file(resolution.filePath)
|
|
175
|
+
if (await file.exists()) {
|
|
176
|
+
return new Response(file, {
|
|
177
|
+
status: 200,
|
|
178
|
+
headers: {
|
|
179
|
+
'Content-Type': contentTypeFor(resolution.filePath),
|
|
180
|
+
'Cache-Control': cacheControl,
|
|
181
|
+
},
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// SPA fallback: serve the root index.html so client-side routing works.
|
|
186
|
+
if (route.spa) {
|
|
187
|
+
const indexPath = path.join(route.dir, 'index.html')
|
|
188
|
+
const index = Bun.file(indexPath)
|
|
189
|
+
if (await index.exists()) {
|
|
190
|
+
return new Response(index, {
|
|
191
|
+
status: 200,
|
|
192
|
+
headers: {
|
|
193
|
+
'Content-Type': 'text/html; charset=utf-8',
|
|
194
|
+
'Cache-Control': 'no-cache',
|
|
195
|
+
},
|
|
196
|
+
})
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return new Response('Not Found', { status: 404 })
|
|
201
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -24,11 +24,47 @@ export interface PathRewrite {
|
|
|
24
24
|
stripPrefix?: boolean
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* How a static-file route maps request paths to files on disk.
|
|
29
|
+
*
|
|
30
|
+
* - `directory` (default): `/about` → `<root>/about/index.html` (SSG dir style).
|
|
31
|
+
* - `flat`: `/about` → `<root>/about.html` (flat-file style).
|
|
32
|
+
*/
|
|
33
|
+
export type PathRewriteStyle = 'directory' | 'flat'
|
|
34
|
+
|
|
35
|
+
export interface StaticRouteConfig {
|
|
36
|
+
/** Absolute path to the directory served for this route. */
|
|
37
|
+
dir: string
|
|
38
|
+
/**
|
|
39
|
+
* Single-page-app fallback: serve `index.html` for any path that doesn't
|
|
40
|
+
* resolve to a real file (so client-side routing works). Default: `false`.
|
|
41
|
+
*/
|
|
42
|
+
spa?: boolean
|
|
43
|
+
/**
|
|
44
|
+
* Extensionless-URL resolution style for `.html` files. Default: `directory`.
|
|
45
|
+
*/
|
|
46
|
+
pathRewriteStyle?: PathRewriteStyle
|
|
47
|
+
/**
|
|
48
|
+
* `Cache-Control` max-age (seconds) for served files. Default: `0`.
|
|
49
|
+
*/
|
|
50
|
+
maxAge?: number
|
|
51
|
+
}
|
|
52
|
+
|
|
27
53
|
export interface BaseProxyConfig {
|
|
28
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Upstream `host:port` to forward to (e.g. `localhost:5173`). Optional when
|
|
56
|
+
* `static` is set (the route serves files from disk instead of proxying).
|
|
57
|
+
*/
|
|
58
|
+
from?: string // localhost:5173
|
|
29
59
|
to: string // stacks.localhost
|
|
30
60
|
start?: StartOptions
|
|
31
61
|
pathRewrites?: PathRewrite[]
|
|
62
|
+
/**
|
|
63
|
+
* Serve a local directory for this route instead of proxying to `from`.
|
|
64
|
+
* Provide an absolute directory path (shorthand) or a {@link StaticRouteConfig}.
|
|
65
|
+
* When set, `from` is optional; exactly one of `from`/`static` must be present.
|
|
66
|
+
*/
|
|
67
|
+
static?: string | StaticRouteConfig
|
|
32
68
|
/**
|
|
33
69
|
* Stable id used when registering this proxy with the rpx daemon. Derived
|
|
34
70
|
* from `to` if omitted. Must match `/^[a-zA-Z0-9._-]+$/` and be ≤128 chars.
|
|
@@ -48,6 +84,89 @@ export interface CleanupConfig {
|
|
|
48
84
|
|
|
49
85
|
export type CleanupOptions = Partial<CleanupConfig>
|
|
50
86
|
|
|
87
|
+
/**
|
|
88
|
+
* A real PEM cert+key pair on disk for one SNI server name.
|
|
89
|
+
*/
|
|
90
|
+
export interface DomainCert {
|
|
91
|
+
/** Absolute path to the PEM certificate (fullchain). */
|
|
92
|
+
certPath: string
|
|
93
|
+
/** Absolute path to the PEM private key. */
|
|
94
|
+
keyPath: string
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Production TLS using real certs (e.g. Let's Encrypt) served per-domain via
|
|
99
|
+
* SNI on a single listener. Provide either an explicit `domains` map or a
|
|
100
|
+
* `certsDir` convention.
|
|
101
|
+
*/
|
|
102
|
+
export interface ProductionTlsConfig {
|
|
103
|
+
/**
|
|
104
|
+
* Explicit per-domain cert/key files keyed by SNI server name. Use
|
|
105
|
+
* `*.example.com` for a wildcard server name.
|
|
106
|
+
*/
|
|
107
|
+
domains?: Record<string, DomainCert>
|
|
108
|
+
/**
|
|
109
|
+
* Directory of PEM files following the convention `<domain>.crt` /
|
|
110
|
+
* `<domain>.key`. A wildcard pair `_wildcard.<apex>.crt` /
|
|
111
|
+
* `_wildcard.<apex>.key` is registered under server name `*.<apex>`.
|
|
112
|
+
*/
|
|
113
|
+
certsDir?: string
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* On-demand TLS: issue a real (Let's Encrypt, http-01) certificate for an
|
|
118
|
+
* unknown host the first time it's needed, gated by an `ask` callback and/or an
|
|
119
|
+
* `allowedSuffixes` allowlist to prevent abuse.
|
|
120
|
+
*
|
|
121
|
+
* ## Why this is "ask-gated issuance + listener recreate", not at-handshake
|
|
122
|
+
*
|
|
123
|
+
* Bun (verified on 1.3.14 + 1.4.0) has **no working SNICallback** and
|
|
124
|
+
* `server.reload({ tls })` does **not** update certs at runtime. So rpx cannot
|
|
125
|
+
* mint a cert during the TLS handshake the way Caddy's on-demand TLS does.
|
|
126
|
+
* Instead rpx:
|
|
127
|
+
* 1. Sees the first plaintext request for the host on its `:80` listener.
|
|
128
|
+
* 2. Asks `ask(host)` / checks `allowedSuffixes`; if approved, drives the
|
|
129
|
+
* ACME http-01 flow (serving the challenge from its own `:80`).
|
|
130
|
+
* 3. Writes the PEMs into `certsDir` and rebuilds the `:443` listener with the
|
|
131
|
+
* augmented SNI cert set (a sub-second `server.stop()` + re-`Bun.serve`).
|
|
132
|
+
* The subsequent HTTPS request then finds the freshly-issued cert.
|
|
133
|
+
*
|
|
134
|
+
* Issuance can also be triggered programmatically via the manager's
|
|
135
|
+
* `ensureCert(host)` (e.g. a tunnel server pre-warming a subdomain's cert on
|
|
136
|
+
* registration) so the cert exists before the first browser hit.
|
|
137
|
+
*/
|
|
138
|
+
export interface OnDemandTlsConfig {
|
|
139
|
+
/** Master switch. On-demand TLS is opt-in; default `false`. */
|
|
140
|
+
enabled?: boolean
|
|
141
|
+
/**
|
|
142
|
+
* Gate issuance for a given hostname. Return `true` to allow rpx to obtain a
|
|
143
|
+
* cert, `false` to refuse. Combined with {@link allowedSuffixes} (a host is
|
|
144
|
+
* approved if either the suffix allowlist matches OR `ask` returns true). If
|
|
145
|
+
* neither is provided, on-demand issuance refuses every host.
|
|
146
|
+
*/
|
|
147
|
+
ask?: (host: string) => boolean | Promise<boolean>
|
|
148
|
+
/**
|
|
149
|
+
* Allowlist of domain suffixes that may be auto-issued without consulting
|
|
150
|
+
* `ask`. A host matches a suffix when it equals it or ends with `.<suffix>`
|
|
151
|
+
* (so `example.com` allows `example.com` and `a.example.com`).
|
|
152
|
+
*/
|
|
153
|
+
allowedSuffixes?: string[]
|
|
154
|
+
/** Contact email for the ACME account (recommended by Let's Encrypt). */
|
|
155
|
+
email?: string
|
|
156
|
+
/**
|
|
157
|
+
* Use Let's Encrypt **staging** (untrusted but un-rate-limited) instead of
|
|
158
|
+
* production. Default `false` (real, trusted, rate-limited certs).
|
|
159
|
+
*/
|
|
160
|
+
staging?: boolean
|
|
161
|
+
/**
|
|
162
|
+
* Directory where issued PEMs are written (`<host>.crt` / `<host>.key`) and
|
|
163
|
+
* from which existing certs are loaded. Should match the SNI `certsDir` so
|
|
164
|
+
* issued certs survive restarts. Defaults to the daemon's productionCerts
|
|
165
|
+
* `certsDir` when wired through the daemon.
|
|
166
|
+
*/
|
|
167
|
+
certsDir?: string
|
|
168
|
+
}
|
|
169
|
+
|
|
51
170
|
export interface SharedProxyConfig {
|
|
52
171
|
https: boolean | TlsOption
|
|
53
172
|
cleanup: boolean | CleanupOptions
|
|
@@ -64,6 +183,24 @@ export interface SharedProxyConfig {
|
|
|
64
183
|
* `:443` (Valet-style). Default: `false` for backward compatibility.
|
|
65
184
|
*/
|
|
66
185
|
viaDaemon?: boolean
|
|
186
|
+
/**
|
|
187
|
+
* Master switch for all `/etc/hosts` reads/writes. Set to `false` on a real
|
|
188
|
+
* server with real DNS so rpx never touches `/etc/hosts`. When omitted, the
|
|
189
|
+
* legacy behavior applies (driven by `cleanup.hosts`). `cleanup: { hosts:
|
|
190
|
+
* false }` also disables hosts management.
|
|
191
|
+
*/
|
|
192
|
+
hostsManagement?: boolean
|
|
193
|
+
/**
|
|
194
|
+
* Production per-domain SNI certs (Let's Encrypt PEMs already on disk). When
|
|
195
|
+
* provided, the listener serves a different real cert per SNI server name
|
|
196
|
+
* instead of the dev self-signed shared cert.
|
|
197
|
+
*/
|
|
198
|
+
productionCerts?: ProductionTlsConfig
|
|
199
|
+
/**
|
|
200
|
+
* On-demand TLS: lazily issue a real cert for an unknown (but approved) host
|
|
201
|
+
* the first time it's needed. Opt-in — see {@link OnDemandTlsConfig}.
|
|
202
|
+
*/
|
|
203
|
+
onDemandTls?: OnDemandTlsConfig
|
|
67
204
|
}
|
|
68
205
|
|
|
69
206
|
export type SharedProxyOptions = Partial<SharedProxyConfig>
|
package/dist/chunk-kbnzcycw.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{$ as j,S as a,T as b,U as c,V as d,W as e,X as f,Y as g,Z as h,_ as i,aa as k,ba as l,ca as m,da as n}from"./chunk-pncxrxde.js";import"./chunk-zs1tyy8z.js";export{l as tearDownDevelopmentDns,k as syncDevelopmentDnsFromRegistry,d as stopDnsServer,c as startDnsServer,h as setupResolver,j as setupDevelopmentDns,f as resolverFilePath,m as removeResolver,i as removeLegacyTldResolvers,n as reconcileStaleDevelopmentDns,e as isDnsServerRunning,g as contentLooksLikeRpxResolver,b as RPX_RESOLVER_MARKER,a as DNS_PORT};
|