Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

@carbon/serve: assets from memory, standard libraries, three modules (#1858)

Sidffbd4c7202a0
diff --git a/packages/serve/AGENTS.md b/packages/serve/AGENTS.md
index 531537d379..cf791bde26 100644
--- a/packages/serve/AGENTS.md
+++ b/packages/serve/AGENTS.md
@@ -25,10 +25,16 @@ options, so nothing could be put between a request and React Router.
Vercel's) writes a `.br` beside each asset and keeps the original. The
`.br` goes as it is to anything that accepts Brotli; the original to a
caller that does not. Both answers carry `Vary: Accept-Encoding`.
-- **Which files exist is read once, at start.** `@fastify/static` is only
- used to send the file that index names (`serve: false`, `reply.sendFile`).
- Left to find files itself it lists the parent directory on every request,
- which with two thousand chunks was slower than compressing on the fly.
+- **Files are read once, at start** (`src/assets.ts`). Every Brotli copy,
+ and every uncompressed file up to 1 MB that has none, is held in memory
+ with its headers already made; serving one is a lookup and a write.
+ Read per request, each cost a stat, an open and a stream on libuv's four
+ threads — the same four that compress rendered pages. Larger files, and
+ the original of a compressed one for a caller that refuses Brotli, are
+ sent from disk by `reply.sendFile`, which answers ranges. Nothing here
+ may read a file while handling a request.
+- **`/assets/*` and `/_vercel/*` are routes of their own.** A file or a
+ plain 404, never a rendered page.
- **React Router is a route, not the not-found handler.** `@fastify/compress`
attaches to routes only; from the not-found handler pages went out
uncompressed.
@@ -36,11 +42,17 @@ options, so nothing could be put between a request and React Router.
request stream reaches React Router whole.
- **The request URL is joined as text**, never resolved against a base: a
path of `//other.host/x` would name another host, and React Router checks
- a form's `Origin` against that URL.
+ a form's `Origin` against that URL. `@mjackson/node-fetch-server`
+ resolves, which is why `src/request.ts` is ours.
- **Fastify's defaults are not all kept.** `keepAliveTimeout` is above any
proxy's idle timeout, and `requestTimeout` is Node's 300s (Fastify sets
none). `bodyLimit` does not apply: no body is parsed here. A request whose `Content-Type` is not a
media type at all is a 415 from Fastify before React Router sees it.
+- **What a library already answers is left to it.** `negotiator` reads
+ `Accept-Encoding` (a `br;q=0` is a refusal), `@fastify/send` names a
+ file's content type — the same code that sends the original, so both
+ copies of an asset are called one thing — and `close-with-grace` shuts
+ the server down: requests in flight finish, bounded at twenty seconds.
- Routes that should not go through React Router at all belong here, in front
of the catch-all — not as a path check inside a route.
diff --git a/packages/serve/package.json b/packages/serve/package.json
index 072ae21f96..6d17be728c 100644
--- a/packages/serve/package.json
+++ b/packages/serve/package.json
@@ -2,12 +2,16 @@
"name": "@carbon/serve",
"dependencies": {
"@fastify/compress": "catalog:",
+ "@fastify/send": "4.1.1",
"@fastify/static": "catalog:",
+ "close-with-grace": "2.5.0",
"fastify": "catalog:",
+ "negotiator": "1.0.0",
"react-router": "catalog:"
},
"devDependencies": {
"@carbon/config": "workspace:*",
+ "@types/negotiator": "0.6.5",
"@types/node": "20.19.39",
"typescript": "catalog:",
"vitest": "catalog:"
diff --git a/packages/serve/src/assets.ts b/packages/serve/src/assets.ts
new file mode 100644
index 0000000000..c52e682e31
--- /dev/null
+++ b/packages/serve/src/assets.ts
@@ -0,0 +1,97 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+// The built client, read once when the server starts.
+//
+// An image's files never change under it, so everything a request for a
+// file needs is worked out here: which files exist, what each is called,
+// and — for all but the largest — the bytes themselves. Serving one is
+// then a lookup and a write. Read from disk per request, each cost a
+// stat, an open and a stream on libuv's four threads, which are the same
+// four that compress every rendered page.
+
+import { createHash } from "node:crypto";
+import { readdirSync, readFileSync, statSync } from "node:fs";
+import path from "node:path";
+import { isUtf8MimeType, mime } from "@fastify/send";
+
+/** One answer to a request for a file, ready to send. */
+export type Asset = {
+ body: Buffer;
+ headers: Record<string, string>;
+};
+
+export type AssetEntry = {
+ /** The Brotli copy the build left beside the file, when it left one. */
+ brotli: Asset | null;
+ /**
+ * The file as it is, when that is what most callers get and it is small
+ * enough to hold. Otherwise it is on disk, and sent from there.
+ */
+ plain: Asset | null;
+};
+
+// Past this a file stays on disk: a 3D model is megabytes, fetched rarely,
+// and wants byte ranges, which the file sender answers.
+const HELD_IN_MEMORY = 1024 * 1024;
+
+// Fingerprinted by the build: the name changes when the bytes do.
+const IMMUTABLE = "public, max-age=31536000, immutable";
+const REVALIDATED = "public, max-age=3600";
+
+export const cacheControl = (name: string) =>
+ name.startsWith("assets/") ? IMMUTABLE : REVALIDATED;
+
+/** By the library that sends the files, so both copies of one are called one thing. */
+function contentType(name: string): string {
+ const type = mime.getType(name) ?? "application/octet-stream";
+ return isUtf8MimeType(type) ? `${type}; charset=utf-8` : type;
+}
+
+function held(file: string, headers: Record<string, string>): Asset {
+ const body = readFileSync(file);
+ const etag = `"${createHash("sha1").update(body).digest("base64url")}"`;
+ return { body, headers: { ...headers, etag } };
+}
+
+/** Every file under `root`, by its path from it. */
+export function loadAssets(root: string): Map<string, AssetEntry> {
+ const names = readdirSync(root, { recursive: true, withFileTypes: true })
+ .filter((entry) => entry.isFile())
+ .map((entry) =>
+ path
+ .relative(root, path.join(entry.parentPath, entry.name))
+ .split(path.sep)
+ .join("/")
+ )
+ .filter((name) => !name.split("/").some((part) => part.startsWith(".")));
+ const onDisk = new Set(names);
+
+ const assets = new Map<string, AssetEntry>();
+ for (const name of names) {
+ // A copy of the file before it, not a file of its own.
+ if (name.endsWith(".br") && onDisk.has(name.slice(0, -3))) continue;
+
+ const file = path.join(root, name);
+ const headers = {
+ "content-type": contentType(name),
+ "cache-control": cacheControl(name)
+ };
+ const compressed = onDisk.has(`${name}.br`);
+ assets.set(name, {
+ brotli: compressed
+ ? held(`${file}.br`, {
+ ...headers,
+ "content-encoding": "br",
+ vary: "Accept-Encoding"
+ })
+ : null,
+ plain:
+ !compressed && statSync(file).size <= HELD_IN_MEMORY
+ ? held(file, headers)
+ : null
+ });
+ }
+ return assets;
+}
diff --git a/packages/serve/src/index.test.ts b/packages/serve/src/index.test.ts
index 380a85d9f9..3da8040b60 100644
--- a/packages/serve/src/index.test.ts
+++ b/packages/serve/src/index.test.ts
@@ -7,9 +7,10 @@ import { tmpdir } from "node:os";
import path from "node:path";
import { brotliCompressSync } from "node:zlib";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
-import { allowedActionOrigins, createApp, takesBrotli } from "./index.ts";
+import { allowedActionOrigins, createApp } from "./index.ts";
const SCRIPT = "console.log('carbon');\n".repeat(200);
+const MODEL = Buffer.alloc(2 * 1024 * 1024, 7);
describe("allowedActionOrigins", () => {
it("is the host of the configured site URL", () => {
@@ -28,17 +29,6 @@ describe("allowedActionOrigins", () => {
});
});
-describe("takesBrotli", () => {
- it("is what the header says, a refusal included", () => {
- expect(takesBrotli("gzip, deflate, br")).toBe(true);
- expect(takesBrotli("BR;q=0.5, gzip")).toBe(true);
- expect(takesBrotli("gzip, br;q=0")).toBe(false);
- expect(takesBrotli("gzip, br; q=0.0")).toBe(false);
- expect(takesBrotli("gzip, brotli")).toBe(false);
- expect(takesBrotli(undefined)).toBe(false);
- });
-});
-
describe("createApp", () => {
let root: string;
let app: Awaited<ReturnType<typeof createApp>>;
@@ -55,9 +45,12 @@ describe("createApp", () => {
);
// Too small to be worth compressing, so it stays as it is.
writeFileSync(path.join(root, "robots.txt"), "User-agent: *\n");
+ // Too large to hold in memory: sent from disk, where ranges work.
+ writeFileSync(path.join(root, "assets", "model-abc123.glb"), MODEL);
app = await createApp({
clientDirectory: root,
+ logger: false,
handleRequest: async (request: Request) => {
seen.push(request);
const headers = new Headers({ "content-type": "text/plain" });
@@ -98,18 +91,28 @@ describe("createApp", () => {
expect(res.statusCode).toBe(200);
expect(res.headers["content-encoding"]).toBe("br");
expect(res.headers["cache-control"]).toContain("immutable");
- expect(res.headers["content-type"]).toContain("text/javascript");
+ expect(res.headers["content-type"]).toBe(
+ "application/javascript; charset=utf-8"
+ );
expect(res.headers.vary).toContain("Accept-Encoding");
expect(res.rawPayload.length).toBeLessThan(SCRIPT.length / 10);
});
- it("sends the file itself to a caller that refuses Brotli", async () => {
- const res = await app.inject({
- url: "/assets/entry-abc123.js",
- headers: { "accept-encoding": "br;q=0" }
- });
- expect(res.headers["content-encoding"]).toBeUndefined();
- expect(res.payload).toBe(SCRIPT);
+ it("reads Accept-Encoding as the header means it", async () => {
+ const encoding = async (header: string) =>
+ (
+ await app.inject({
+ url: "/assets/entry-abc123.js",
+ headers: { "accept-encoding": header }
+ })
+ ).headers["content-encoding"];
+ expect(await encoding("BR;q=0.5")).toBe("br");
+ expect(await encoding("*")).toBe("br");
+ // A refusal, alone and beside a wildcard that would otherwise allow it.
+ expect(await encoding("br;q=0")).toBeUndefined();
+ expect(await encoding("brotli")).toBeUndefined();
+ // The wildcard still allows gzip, which the original is then sent as.
+ expect(await encoding("br;q=0, *")).toBe("gzip");
});
it("sends the file itself to a caller that cannot take Brotli", async () => {
@@ -146,6 +149,43 @@ describe("createApp", () => {
expect(res.headers["content-type"]).toBe("text/html; charset=utf-8");
});
+ it("answers a conditional request without the body", async () => {
+ const headers = { "accept-encoding": "br" };
+ const first = await app.inject({ url: "/assets/entry-abc123.js", headers });
+ const again = await app.inject({
+ url: "/assets/entry-abc123.js",
+ headers: { ...headers, "if-none-match": String(first.headers.etag) }
+ });
+ expect(again.statusCode).toBe(304);
+ expect(again.rawPayload.length).toBe(0);
+ expect(again.headers["cache-control"]).toContain("immutable");
+ });
+
+ it("answers HEAD with the headers and nothing else", async () => {
+ const res = await app.inject({
+ method: "HEAD",
+ url: "/assets/entry-abc123.js",
+ headers: { "accept-encoding": "br" }
+ });
+ expect(res.statusCode).toBe(200);
+ expect(res.headers["content-encoding"]).toBe("br");
+ expect(Number(res.headers["content-length"])).toBeGreaterThan(0);
+ expect(res.rawPayload.length).toBe(0);
+ });
+
+ it("sends a large file from disk, a range of it when asked", async () => {
+ const whole = await app.inject({ url: "/assets/model-abc123.glb" });
+ expect(whole.statusCode).toBe(200);
+ expect(whole.rawPayload.length).toBe(MODEL.length);
+ expect(whole.headers["cache-control"]).toContain("immutable");
+ const part = await app.inject({
+ url: "/assets/model-abc123.glb",
+ headers: { range: "bytes=0-99" }
+ });
+ expect(part.statusCode).toBe(206);
+ expect(part.rawPayload.length).toBe(100);
+ });
+
it("answers a missing asset itself", async () => {
seen.length = 0;
for (const url of [
diff --git a/packages/serve/src/index.ts b/packages/serve/src/index.ts
index 49eb158c54..703f02eb41 100644
--- a/packages/serve/src/index.ts
+++ b/packages/serve/src/index.ts
@@ -2,35 +2,51 @@
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
-// The server a self-hosted app runs on. `node` runs this file as it is, by
-// stripping the types: the runtime image has no TypeScript toolchain. So
-// only syntax that can be erased — no enums, no parameter properties, and
-// `import type` for anything that is only a type.
+// The server a self-hosted app runs on. `node` runs these files as they
+// are, by stripping the types: the runtime image has no TypeScript
+// toolchain. So only syntax that can be erased, `import type` for types,
+// and `.ts` on relative imports.
//
-// It replaces `react-router-serve`, which loads the build itself and so
-// leaves no place to stand between a request and React Router. Two things
-// need that place:
-//
-// - React Router refuses a form submit whose `Origin` is not the origin of
-// `request.url`. Behind a proxy that ends TLS the app is called over
-// http, the browser says https, and every submit was a 400. The
-// deployment already says where the app lives (`siteUrl`); that host is
-// allowed, through the option React Router has for it.
-// - Static files compressed once at build (a `.br` beside each one) are
-// sent as they are instead of being compressed again on every request.
+// It replaces `react-router-serve`, which loads the build itself and leaves
+// no place to stand between a request and React Router. Two things need
+// that place: the app's own URL as an origin React Router accepts
+// (`allowedActionOrigins`), and files sent as the build compressed them
+// (`assets.ts`).
-import { readdirSync } from "node:fs";
import path from "node:path";
-import { Readable } from "node:stream";
import { constants } from "node:zlib";
import fastifyCompress from "@fastify/compress";
import fastifyStatic from "@fastify/static";
-import Fastify, { type FastifyReply, type FastifyRequest } from "fastify";
+import closeWithGrace from "close-with-grace";
+import Fastify, {
+ type FastifyError,
+ type FastifyReply,
+ type FastifyRequest,
+ type FastifyServerOptions,
+ LogController
+} from "fastify";
+import Negotiator from "negotiator";
import {
createRequestHandler,
RouterContextProvider,
type ServerBuild
} from "react-router";
+import { type Asset, cacheControl, loadAssets } from "./assets.ts";
+import { toFetchRequest } from "./request.ts";
+
+// Longer than any proxy in front holds an idle connection (an ALB 60s,
+// Traefik 90s). Shorter, and the proxy reuses a connection this side has
+// just closed: an occasional 502 on a request that was never read.
+const KEEP_ALIVE_MS = 100_000;
+// Node's own limit on how long a request may take to arrive, which Fastify
+// turns off. Without it a client that never finishes sending holds its
+// connection for good.
+const REQUEST_MS = 300_000;
+// How long a shutdown waits for requests in flight: under the thirty
+// seconds Kubernetes and ECS wait before they kill the process.
+const SHUTDOWN_MS = 20_000;
+
+const PLAIN = "text/plain; charset=utf-8";
/**
* The hosts whose forms React Router should accept: the app's own, read
@@ -47,147 +63,42 @@ export function allowedActionOrigins(siteUrl: string | undefined): string[] {
}
}
-// What the build compresses, and so the only types a `.br` can be of.
-const TYPES: Record<string, string> = {
- ".css": "text/css; charset=utf-8",
- ".html": "text/html; charset=utf-8",
- ".js": "text/javascript; charset=utf-8",
- ".json": "application/json; charset=utf-8",
- ".mjs": "text/javascript; charset=utf-8",
- ".svg": "image/svg+xml",
- ".toml": "text/plain; charset=utf-8",
- ".txt": "text/plain; charset=utf-8",
- ".wasm": "application/wasm",
- ".xml": "application/xml; charset=utf-8",
- ".yaml": "text/plain; charset=utf-8",
- ".yml": "text/plain; charset=utf-8"
-};
-
-// Fingerprinted by the build: the name changes when the bytes do.
-const IMMUTABLE = "public, max-age=31536000, immutable";
-const SHORT = "public, max-age=3600";
-
-// Paths that are a file or nothing.
-const NEVER_A_PAGE = /^\/(assets|_vercel)\//;
-
-/**
- * Whether an `Accept-Encoding` header names Brotli and does not rule it out:
- * `br;q=0` is a caller saying it cannot take it.
- */
-export function takesBrotli(header: string | string[] | undefined): boolean {
- for (const part of String(header ?? "").split(",")) {
- const [name, ...params] = part.split(";").map((piece) => piece.trim());
- if (name?.toLowerCase() !== "br") continue;
- const quality = params.find((param) =>
- param.toLowerCase().startsWith("q=")
- );
- return quality === undefined || Number(quality.slice(2)) > 0;
- }
- return false;
-}
-
-const contentType = (file: string) =>
- TYPES[path.extname(file).toLowerCase()] ?? "application/octet-stream";
-
-const cacheControl = (urlPath: string) =>
- urlPath.startsWith("/assets/") ? IMMUTABLE : SHORT;
-
-/**
- * Every file in the built client, by its path from the root. Read once: an
- * image's files do not change under it, and asking the disk on each request
- * is what made a directory of two thousand chunks slow to serve from.
- */
-function indexFiles(root: string): Set<string> {
- const files = new Set<string>();
- for (const entry of readdirSync(root, {
- recursive: true,
- withFileTypes: true
- })) {
- if (!entry.isFile()) continue;
- const rel = path
- .relative(root, path.join(entry.parentPath, entry.name))
- .split(path.sep)
- .join("/");
- if (!rel.split("/").some((segment) => segment.startsWith(".")))
- files.add(rel);
- }
- return files;
-}
-
-/**
- * A Node request as the Fetch `Request` React Router takes. The URL is the
- * one the app was called on — host from the `Host` header, scheme from the
- * socket — exactly as `react-router-serve` built it.
- */
-function toRequest(req: FastifyRequest, signal: AbortSignal): Request {
- const raw = req.raw;
- const encrypted = "encrypted" in raw.socket && raw.socket.encrypted;
- // Joined as text, never resolved against a base: a path of `//other.host/x`
- // resolved that way names another host, and React Router checks a form's
- // `Origin` against this URL.
- const url = new URL(
- `${encrypted ? "https" : "http"}://${raw.headers.host ?? "localhost"}${raw.url ?? "/"}`
- );
- const headers = new Headers();
- for (const [name, value] of Object.entries(raw.headers)) {
- if (value === undefined) continue;
- if (Array.isArray(value)) for (const v of value) headers.append(name, v);
- else headers.set(name, value);
- }
- const hasBody = raw.method !== "GET" && raw.method !== "HEAD";
- return new Request(url, {
- method: raw.method,
- headers,
- signal,
- // Node's stream type and the DOM's disagree on a detail of byte
- // readers; at runtime they are the one class.
- ...(hasBody
- ? { body: Readable.toWeb(raw) as unknown as BodyInit, duplex: "half" }
- : {})
- } as RequestInit);
-}
-
/** The server, not yet listening — which is what a test wants. */
export async function createApp({
handleRequest,
- clientDirectory
+ clientDirectory,
+ logger = { level: "info" }
}: {
/** Everything that is not a file: React Router, usually. */
handleRequest: (request: Request) => Promise<Response> | Response;
/** The built client, served at `/`. */
clientDirectory: string;
+ logger?: FastifyServerOptions["logger"];
}) {
const root = path.resolve(clientDirectory);
- // The apps log their own requests, with the request id; a second line
- // from here said the same thing without one.
+ const assets = loadAssets(root);
+
const app = Fastify({
- logger: false,
- // Longer than any proxy in front holds an idle connection (an ALB 60s,
- // Traefik 90s). Shorter, and the proxy reuses a connection this side
- // has just closed: an occasional 502 on a request that was never read.
- keepAliveTimeout: 100_000,
- // Node's own limit on how long a request may take to arrive, which
- // Fastify turns off. Without it a client that never finishes sending
- // holds its connection for good.
- requestTimeout: 300_000,
- // A URL the router cannot decode. Fastify's own answer is JSON naming
- // its error code.
+ logger,
+ // The apps log their own requests, with the request id.
+ logController: new LogController({ disableRequestLogging: true }),
+ keepAliveTimeout: KEEP_ALIVE_MS,
+ requestTimeout: REQUEST_MS,
+ // A URL the router cannot decode. Fastify's own answer names its
+ // error code.
frameworkErrors: (_error, _req, reply) =>
- (reply as FastifyReply)
- .code(400)
- .type("text/plain; charset=utf-8")
- .send("Bad Request")
+ (reply as FastifyReply).code(400).type(PLAIN).send("Bad Request")
});
- // Bodies are React Router's to read. Parsed here, the stream it is handed
- // would already be empty.
+ // Bodies are React Router's to read. Parsed here, the stream it is
+ // handed would already be empty.
app.removeAllContentTypeParsers();
app.addContentTypeParser("*", (_req, _payload, done) => done(null));
- // For what is rendered per request. A file compressed at build already
- // carries its encoding and is left alone. Flushed as it is written:
- // React streams a page in pieces, and a compressor that waits for a full
- // buffer holds the first of them back.
+ // For what is rendered per request; a file compressed at build carries
+ // its encoding and is left alone. Flushed as it is written: React
+ // streams a page in pieces, and a compressor waiting for a full buffer
+ // holds the first of them back.
await app.register(fastifyCompress, {
global: true,
encodings: ["br", "gzip"],
@@ -199,98 +110,112 @@ export async function createApp({
zlibOptions: { flush: constants.Z_SYNC_FLUSH }
});
- // Only for `reply.sendFile`, which answers ranges and conditional
- // requests. Which file is ours to say, from the index: left to find it
- // itself, the plugin lists the directory on every request.
- const files = indexFiles(root);
+ // For the files left on disk: `reply.sendFile`, which answers ranges.
+ // It serves nothing by itself — left to find files, it lists their
+ // directory on every request.
await app.register(fastifyStatic, {
root,
serve: false,
cacheControl: false,
- setHeaders(reply, filePath) {
- const urlPath = `/${path.relative(root, filePath).split(path.sep).join("/")}`;
- reply.header("Cache-Control", cacheControl(urlPath));
- if (!filePath.endsWith(".br")) return;
- // Say what it is a file of, not that it is Brotli.
- reply
- .header("Content-Type", contentType(filePath.slice(0, -3)))
- .header("Content-Encoding", "br");
+ setHeaders: (reply, file) => {
+ reply.header("cache-control", cacheControl(path.relative(root, file)));
+ }
+ });
+
+ function held(req: FastifyRequest, reply: FastifyReply, asset: Asset) {
+ reply.headers(asset.headers);
+ if (req.headers["if-none-match"] === asset.headers.etag) {
+ return reply.code(304).send();
+ }
+ if (req.method === "HEAD") {
+ return reply.header("content-length", asset.body.length).send();
}
+ return reply.send(asset.body);
+ }
+
+ /** The file `name`, sent; or nothing when there is no such file. */
+ function file(req: FastifyRequest, reply: FastifyReply, name: string) {
+ const asset = assets.get(name);
+ if (!asset) return undefined;
+ // Every browser accepts Brotli; a monitor or a script may not, and
+ // `br;q=0` is one saying so.
+ if (asset.brotli) {
+ const [accepted] = new Negotiator(req.raw).encodings(["br"]);
+ if (accepted === "br") return held(req, reply, asset.brotli);
+ // Which copy is sent depends on the caller: a cache in between has
+ // to know that of this one too.
+ reply.header("vary", "Accept-Encoding");
+ }
+ return asset.plain ? held(req, reply, asset.plain) : reply.sendFile(name);
+ }
+
+ // A chunk from before the last deploy, or Vercel's analytics script off
+ // Vercel. There is no page for these, and rendering the app's 404 for
+ // each cost a pass through React Router.
+ const gone = (reply: FastifyReply) =>
+ reply
+ .code(404)
+ .header("cache-control", "no-store")
+ .type(PLAIN)
+ .send("Not found");
+
+ type Path = { Params: { "*": string } };
+
+ app.route<Path>({
+ method: ["GET", "HEAD"],
+ url: "/assets/*",
+ handler: (req, reply) =>
+ file(req, reply, `assets/${req.params["*"]}`) ?? gone(reply)
});
- // A route, not the not-found handler: the compression above attaches to
- // routes, and pages rendered from the not-found handler went out as they
- // were.
- app.all("/*", async (req, reply) => {
- const { method } = req.raw;
- const urlPath = (req.raw.url ?? "/").split("?")[0] ?? "/";
+ app.route({
+ method: ["GET", "HEAD"],
+ url: "/_vercel/*",
+ handler: (_req, reply) => gone(reply)
+ });
- if (method === "GET" || method === "HEAD") {
- let rel = "";
- try {
- rel = decodeURIComponent(urlPath).slice(1);
- } catch {
- // Not a path at all: React Router's to refuse.
- }
- if (files.has(rel)) {
- if (!files.has(`${rel}.br`)) return reply.sendFile(rel);
- // Which of the two is sent depends on the caller, and a cache in
- // between has to know that — whichever one it is handed.
- reply.header("Vary", "Accept-Encoding");
- // Every browser takes Brotli; a monitor or a script may not.
- const brotli = takesBrotli(req.headers["accept-encoding"]);
- return reply.sendFile(brotli ? `${rel}.br` : rel);
- }
- // A chunk from before the last deploy, or Vercel's analytics script
- // off Vercel: there is no page to render for these, and rendering the
- // app's 404 for each cost a full pass through React Router.
- if (NEVER_A_PAGE.test(urlPath))
- return reply
- .code(404)
- .header("Cache-Control", "no-store")
- .type("text/plain; charset=utf-8")
- .send("Not found");
+ // A route, not the not-found handler: compression attaches to routes.
+ app.all<Path>("/*", async (req, reply) => {
+ if (req.method === "GET" || req.method === "HEAD") {
+ const sent = file(req, reply, req.params["*"]);
+ if (sent) return sent;
}
- // Stops the render when the browser goes away, as the stock server did.
+ // Stops the render when the browser goes away.
const controller = new AbortController();
- reply.raw.on("close", () => {
+ reply.raw.once("close", () => {
if (!reply.raw.writableFinished) controller.abort();
});
- const response = await handleRequest(toRequest(req, controller.signal));
- // The stock server named the charset on text it was handed without one.
+ const response = await handleRequest(
+ toFetchRequest(req.raw, controller.signal)
+ );
+ // The stock server named the charset on text handed to it without one.
const type = response.headers.get("content-type");
- if (type && /^text\/[^;]+$/.test(type))
+ if (type?.startsWith("text/") && !type.includes(";")) {
response.headers.set("content-type", `${type}; charset=utf-8`);
+ }
return reply.send(response);
});
- // Fastify's own answer carries the error's message; what failed is for
- // the log, not the caller.
- app.setErrorHandler((error: { statusCode?: unknown }, _req, reply) => {
- const status =
- typeof error.statusCode === "number" && error.statusCode >= 400
- ? error.statusCode
- : 500;
- if (status >= 500) console.error("[carbon] request failed", error);
- reply
- .code(status)
- .type("text/plain; charset=utf-8")
- .send(status >= 500 ? "Internal Server Error" : "Bad Request");
+ // What failed is for the log. Fastify's own answer hands the caller the
+ // error's message.
+ app.setErrorHandler((error: FastifyError, req, reply) => {
+ const status = error.statusCode ?? 500;
+ if (status < 500) return reply.code(status).type(PLAIN).send("Bad Request");
+ req.log.error({ err: error }, "request failed");
+ return reply.code(500).type(PLAIN).send("Internal Server Error");
});
return app;
}
-/**
- * Starts a built React Router app.
- */
+/** Starts a built React Router app. */
export async function serve({
build,
clientDirectory,
siteUrl,
- port,
- host
+ port = Number(process.env.PORT ?? 3000),
+ host = process.env.HOST ?? "0.0.0.0"
}: {
build: ServerBuild;
clientDirectory: string;
@@ -318,16 +243,13 @@ export async function serve({
handler(request, new RouterContextProvider())
});
- for (const signal of ["SIGTERM", "SIGINT"]) {
- process.once(signal, () => {
- app.close().finally(() => process.exit(0));
- });
- }
-
- const address = await app.listen({
- port: port ?? Number(process.env.PORT ?? 3000),
- host: host ?? process.env.HOST ?? "0.0.0.0"
+ // On a signal, or an error nothing caught: stop taking requests, let
+ // those in flight finish, exit.
+ closeWithGrace({ delay: SHUTDOWN_MS }, async ({ err }) => {
+ if (err) app.log.error({ err }, "shutting down after an error");
+ await app.close();
});
- console.log(`[carbon] ${address}`);
+
+ await app.listen({ port, host });
return app;
}
diff --git a/packages/serve/src/request.ts b/packages/serve/src/request.ts
new file mode 100644
index 0000000000..6694cfd158
--- /dev/null
+++ b/packages/serve/src/request.ts
@@ -0,0 +1,43 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
+// including ports, remain AGPLv3; serving them over a network requires releasing their source.
+
+import type { IncomingMessage } from "node:http";
+import { Readable } from "node:stream";
+
+/**
+ * A Node request as the Fetch `Request` React Router takes.
+ *
+ * The URL is the one the app was called on: host from the `Host` header,
+ * scheme from the socket. It is joined as text and never resolved against
+ * a base — resolved, a path of `//other.host/x` names another host, and
+ * React Router checks a form's `Origin` against this URL. That is why this
+ * is not `@mjackson/node-fetch-server`, which resolves.
+ */
+export function toFetchRequest(
+ raw: IncomingMessage,
+ signal: AbortSignal
+): Request {
+ const scheme =
+ "encrypted" in raw.socket && raw.socket.encrypted ? "https" : "http";
+ const url = `${scheme}://${raw.headers.host ?? "localhost"}${raw.url ?? "/"}`;
+
+ const headers = new Headers();
+ for (let i = 0; i < raw.rawHeaders.length; i += 2) {
+ headers.append(raw.rawHeaders[i] ?? "", raw.rawHeaders[i + 1] ?? "");
+ }
+
+ if (raw.method === "GET" || raw.method === "HEAD") {
+ return new Request(url, { method: raw.method, headers, signal });
+ }
+ return new Request(url, {
+ method: raw.method,
+ headers,
+ signal,
+ // The body is handed over unread, as a stream. Node's stream type and
+ // the DOM's disagree on a detail of byte readers; at runtime they are
+ // the one class.
+ body: Readable.toWeb(raw) as unknown as BodyInit,
+ duplex: "half"
+ } as RequestInit);
+}
diff --git a/packages/serve/tsconfig.json b/packages/serve/tsconfig.json
index 5593836731..77e3350171 100644
--- a/packages/serve/tsconfig.json
+++ b/packages/serve/tsconfig.json
@@ -2,6 +2,7 @@
"compilerOptions": {
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
+ "isolatedModules": true,
"noEmit": true,
"verbatimModuleSyntax": true
},
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b6f25ac396..74335be114 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -3625,12 +3625,21 @@ importers:
'@fastify/compress':
specifier: 'catalog:'
version: 9.2.0
+ '@fastify/send':
+ specifier: 4.1.1
+ version: 4.1.1
'@fastify/static':
specifier: 'catalog:'
version: 10.1.5
+ close-with-grace:
+ specifier: 2.5.0
+ version: 2.5.0
fastify:
specifier: 'catalog:'
version: 5.12.5
+ negotiator:
+ specifier: 1.0.0
+ version: 1.0.0
react-router:
specifier: 'catalog:'
@@ -3638,6 +3647,9 @@ importers:
'@carbon/config':
specifier: workspace:*
version: link:../config
+ '@types/negotiator':
+ specifier: 0.6.5
+ version: 0.6.5
'@types/node':
specifier: 20.19.39
version: 20.19.39
@@ -10791,6 +10803,9 @@ packages:
resolution: {integrity: sha512-YfWiV16IY0OeBfBCk8+hXKmdTKrKlwKN1MNKAPBu5JYxLwBEZl7QzeEpGnlZb3VMGJrrGmB84gXiH+ofs/TezA==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-MPOlB48mfWhoUlynY0ga7CFsXIPcH6vGPkjzXMn2p+4PH1QUyn2KPtw0hrLLmO6SaX4zse3X6h2x/083vveAlA==}
+
resolution: {integrity: sha512-orrrD74MBUyK8jOAD/r0+lfa1I2MO6I+vAkmAWzMYbCcgrN4lCrmK52gRFQq/JRxfYPfonkr4b0jcY7Olqdqbw==}
@@ -11885,6 +11900,9 @@ packages:
resolution: {integrity: sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==}
engines: {node: '>=0.8'}
+ resolution: {integrity: sha512-MewUtZQU6N4YVHIne63zGtjIQzTINgr6lQp2Y0CutaCw2FsdYahW57dH1Wdz+aV5ipbBzEBZD5znwX2NooS+IA==}
+
resolution: {integrity: sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==}
engines: {node: '>=6'}
@@ -25555,6 +25573,8 @@ snapshots:
dependencies:
'@types/node': 22.19.7
+ '@types/[email protected]': {}
+
dependencies:
undici-types: 6.21.0
@@ -26762,6 +26782,8 @@ snapshots:
+