put every http sidecar on the proxy factory

createSidecarProxy arrived with the wallet but nothing else moved onto it, so five sidecars
still carried their own copy of the same two files: a sidecar-server.ts that remembered a
port announced as `<name>:server`, and a router.ts that forwarded the subpath. Byte for
byte identical once the app name was normalised away — which is exactly what the factory's
own header said it existed to end.

headscale, transmission, invoiceshelf, slskd and music are now wallet-shaped: create the
proxy, export the router and the URL getter. 386 lines deleted against 163 added, and the
five feature directories go from ~70 lines each to ~18.

Two deviations were real and moved INTO the factory rather than being dropped, because both
are HTTP concerns rather than app knowledge:

- Range and If-None-Match are now forwarded for every sidecar. music needed both (seeking,
  and ETag revalidation returning a cheap 304 instead of a cover image) and slskd needed
  Range. Forwarding them everywhere costs nothing and removes the reason to hand-roll.
- timeoutSeconds, used only by music at 1800. A from-scratch reindex holds the proxied
  connection open for minutes with no bytes flowing, which the 60s idle timeout would drop.
  It applies to the whole prefix — the proxy must not know which of a sidecar's routes are
  slow.

The five side-effect imports in hono.ts are gone with them: the port listener now registers
when createSidecarProxy runs inside the router this file already imports. Vault keeps its
hand-rolled pair and its side-effect import — it is off-limits by standing instruction, and
is the one sidecar this commit deliberately does not touch.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-31 11:46:29 +00:00
co-authored by Claude Opus 5
parent 7129cd82e6
commit aaf0161620
13 changed files with 100 additions and 362 deletions
+26 -3
View File
@@ -27,6 +27,13 @@ export type SidecarProxyParams = {
prefix: string;
/** Extra work when the sidecar reports in. Rare — most sidecars only need the port remembered. */
onRegister?: (port: number) => void;
/**
* Per-request idle timeout, seconds. The main server drops a proxied connection after 60s with no bytes
* flowing, which is wrong for a sidecar route that thinks before it answers (a from-scratch music
* reindex holds the socket open for minutes). Applied to the whole prefix: the proxy must not know which
* of a sidecar's routes are slow.
*/
timeoutSeconds?: number;
};
export type SidecarProxy = {
@@ -38,7 +45,7 @@ export type SidecarProxy = {
getWsUrl: () => string | null;
};
export function createSidecarProxy({ name, prefix, onRegister }: SidecarProxyParams): SidecarProxy {
export function createSidecarProxy({ name, prefix, onRegister, timeoutSeconds }: SidecarProxyParams): SidecarProxy {
let serverPort: number | null = null;
sidecar.on(`${name}:server`, (msg) => {
@@ -62,10 +69,26 @@ export function createSidecarProxy({ name, prefix, onRegister }: SidecarProxyPar
const subpath = url.pathname.slice(prefix.length) || '/';
const target = `${baseUrl}${subpath}${url.search}`;
if (timeoutSeconds) {
// Bun passes the server as Hono's env. Older Bun has no per-request timeout; the request still
// completes, it just risks the default idle drop.
const server = ctx.env as { timeout?: (req: Request, seconds: number) => void } | undefined;
try {
server?.timeout?.(ctx.req.raw, timeoutSeconds);
} catch {
/* no per-request timeout available */
}
}
const method = ctx.req.method;
const headers: Record<string, string> = {};
const contentType = ctx.req.header('content-type');
if (contentType) headers['Content-Type'] = contentType;
// Transport headers, forwarded for every sidecar because they are HTTP, not app knowledge: Range so a
// sidecar can answer 206 for media seeking, and If-None-Match so its ETag revalidation can return a
// cheap 304 instead of the whole body.
for (const header of ['content-type', 'range', 'if-none-match'] as const) {
const value = ctx.req.header(header);
if (value) headers[header] = value;
}
// The authenticated owner, so the sidecar can scope its data. The sidecar binds loopback only, so it
// trusts this header — which is exactly why nothing but this proxy may set it.
headers['X-Officer-User'] = String(ctx.get('user').id);