diff --git a/dist/commands/dev/index.js b/dist/commands/dev/index.js index 8584a0a..745fbd3 100644 --- a/dist/commands/dev/index.js +++ b/dist/commands/dev/index.js @@ -4,9 +4,8 @@ import grabDirNames from "../../utils/grab-dir-names"; import writeErrorFile from "../../functions/write-error-file"; import { existsSync, rmSync } from "fs"; const { BUNDLER_CTX_MAP_JSON, SSR_BUNDLER_CTX_MAP_JSON, BUNX_BUNDLER_ERROR_EXIT_FILE, } = grabDirNames(); -let retries = 0; -let timeout; const MAX_RETRIES = 5; +const RETRY_RESET_AFTER_MS = 10000; export default function () { return new Command("dev") .description("Run development server") @@ -24,51 +23,50 @@ export default function () { }); } async function dev() { - clearTimeout(timeout); - if (retries >= MAX_RETRIES) { - console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`); - process.exit(1); + // A loop, not recursion: each restart would otherwise keep the previous + // call (and its subprocess) alive for the supervisor's whole lifetime. + let retries = 0; + while (true) { + if (retries >= MAX_RETRIES) { + console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`); + process.exit(1); + } + const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts"); + const dev_spawn_js_file = path.resolve(__dirname, "dev-spawn.js"); + const final_spawn_file = existsSync(dev_spawn_js_file) + ? dev_spawn_js_file + : dev_spawn_file; + const spawn_options = { + cmd: ["bun", final_spawn_file], + stdio: ["inherit", "inherit", "inherit"], + async onExit(subprocess, exitCode, signalCode, error) { + writeErrorFile({ exitCode, error }); + }, + env: { + ...process.env, + NODE_ENV: "development", + }, + }; + const started_at = Date.now(); + let child_process; + try { + child_process = Bun.spawn(spawn_options); + } + catch (error) { + console.error(`Failed to start dev process:`, error); + retries++; + continue; + } + const exited = await child_process.exited; + // A null exit means the child was killed by a signal (e.g. Ctrl+C + // in the shared terminal), which we must not restart. + if (exited == null) { + return; + } + // Restart on any numeric exit (0 included: the server should never + // exit cleanly). Only consecutive quick crashes count toward the + // limit; a child that ran for a while starts the count over. + retries = + Date.now() - started_at >= RETRY_RESET_AFTER_MS ? 1 : retries + 1; } - const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts"); - const dev_spawn_js_file = path.resolve(__dirname, "dev-spawn.js"); - const final_spawn_file = existsSync(dev_spawn_js_file) - ? dev_spawn_js_file - : dev_spawn_file; - const spawn_options = { - cmd: ["bun", final_spawn_file], - stdio: ["inherit", "inherit", "inherit"], - async onExit(subprocess, exitCode, signalCode, error) { - writeErrorFile({ exitCode, error }); - }, - env: { - ...process.env, - NODE_ENV: "development", - }, - }; - let dev_process; - try { - dev_process = Bun.spawn(spawn_options); - } - catch (error) { - console.error(`Failed to start dev process:`, error); - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await dev(); - } - const exited = await dev_process.exited; - // Restart on any numeric exit (0 included: the server should never exit - // cleanly). A null exit means the child was killed by a signal (e.g. - // Ctrl+C in the shared terminal), which we must not restart. - if (exited != null) { - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await dev(); - } - timeout = setTimeout(() => { - retries = 0; - }, 10000); } diff --git a/dist/commands/start/index.js b/dist/commands/start/index.js index b1f9919..bb9cafd 100644 --- a/dist/commands/start/index.js +++ b/dist/commands/start/index.js @@ -4,9 +4,8 @@ import writeErrorFile from "../../functions/write-error-file"; import grabDirNames from "../../utils/grab-dir-names"; import { existsSync, rmSync } from "fs"; const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); -let retries = 0; -let timeout; const MAX_RETRIES = 5; +const RETRY_RESET_AFTER_MS = 10000; export default function () { return new Command("start") .description("Start production server") @@ -18,50 +17,50 @@ export default function () { }); } async function start() { - clearTimeout(timeout); - if (retries >= MAX_RETRIES) { - console.error(`Production server crashed ${MAX_RETRIES} times. Exiting.`); - process.exit(1); + // A loop, not recursion: each restart would otherwise keep the previous + // call (and its subprocess) alive for the supervisor's whole lifetime. + let retries = 0; + while (true) { + if (retries >= MAX_RETRIES) { + console.error(`Production server crashed ${MAX_RETRIES} times. Exiting.`); + process.exit(1); + } + const prod_spawn_file = path.resolve(__dirname, "prod-spawn.ts"); + const prod_spawn_js_file = path.resolve(__dirname, "prod-spawn.js"); + const final_spawn_file = existsSync(prod_spawn_js_file) + ? prod_spawn_js_file + : prod_spawn_file; + const spawn_options = { + cmd: ["bun", final_spawn_file], + stdio: ["inherit", "inherit", "inherit"], + onExit(subprocess, exitCode, signalCode, error) { + writeErrorFile({ exitCode, error }); + }, + env: { + ...process.env, + NODE_ENV: "production", + }, + }; + const started_at = Date.now(); + let child_process; + try { + child_process = Bun.spawn(spawn_options); + } + catch (error) { + console.error(`Failed to start production process:`, error); + retries++; + continue; + } + const exited = await child_process.exited; + // A null exit means the child was killed by a signal (e.g. Ctrl+C + // in the shared terminal), which we must not restart. + if (exited == null) { + return; + } + // Restart on any numeric exit (0 included: the server should never + // exit cleanly). Only consecutive quick crashes count toward the + // limit; a child that ran for a while starts the count over. + retries = + Date.now() - started_at >= RETRY_RESET_AFTER_MS ? 1 : retries + 1; } - const prod_spawn_file = path.resolve(__dirname, "prod-spawn.ts"); - const prod_spawn_js_file = path.resolve(__dirname, "prod-spawn.js"); - const final_spawn_file = existsSync(prod_spawn_js_file) - ? prod_spawn_js_file - : prod_spawn_file; - const spawn_options = { - cmd: ["bun", final_spawn_file], - stdio: ["inherit", "inherit", "inherit"], - onExit(subprocess, exitCode, signalCode, error) { - writeErrorFile({ exitCode, error }); - }, - env: { - ...process.env, - NODE_ENV: "production", - }, - }; - let dev_process; - try { - dev_process = Bun.spawn(spawn_options); - } - catch (error) { - console.error(`Failed to start production process:`, error); - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await start(); - } - const exited = await dev_process.exited; - // Restart on any numeric exit (0 included). A null exit means the child - // was killed by a signal, which we must not restart. - if (exited != null) { - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await start(); - } - timeout = setTimeout(() => { - retries = 0; - }, 10000); } diff --git a/dist/data/app-data.d.ts b/dist/data/app-data.d.ts index aecab62..bfe6345 100644 --- a/dist/data/app-data.d.ts +++ b/dist/data/app-data.d.ts @@ -8,4 +8,5 @@ export declare const AppData: { readonly BunextTmpFileExt: ".bunext_tmp.tsx"; readonly BunextHMRRetryRoute: "/.bunext/hmr-retry"; readonly DefaultMaxLogs: 50; + readonly DefaultSSRRenderTimeoutMs: 30000; }; diff --git a/dist/data/app-data.js b/dist/data/app-data.js index 45052c0..65351a7 100644 --- a/dist/data/app-data.js +++ b/dist/data/app-data.js @@ -8,4 +8,5 @@ export const AppData = { BunextTmpFileExt: ".bunext_tmp.tsx", BunextHMRRetryRoute: "/.bunext/hmr-retry", DefaultMaxLogs: 50, + DefaultSSRRenderTimeoutMs: 30000, }; diff --git a/dist/functions/bundler/dispose-bundler-contexts.d.ts b/dist/functions/bundler/dispose-bundler-contexts.d.ts new file mode 100644 index 0000000..d3e581e --- /dev/null +++ b/dist/functions/bundler/dispose-bundler-contexts.d.ts @@ -0,0 +1,6 @@ +/** + * Disposes the hydration and SSR esbuild contexts, releasing the incremental + * build state esbuild keeps in memory for them. The bundlers re-create the + * contexts on their next run. + */ +export default function disposeBundlerContexts(): Promise; diff --git a/dist/functions/bundler/dispose-bundler-contexts.js b/dist/functions/bundler/dispose-bundler-contexts.js new file mode 100644 index 0000000..1aa725f --- /dev/null +++ b/dist/functions/bundler/dispose-bundler-contexts.js @@ -0,0 +1,17 @@ +/** + * Disposes the hydration and SSR esbuild contexts, releasing the incremental + * build state esbuild keeps in memory for them. The bundlers re-create the + * contexts on their next run. + */ +export default async function disposeBundlerContexts() { + try { + await global.BUNEXT_BUNDLER_CTX?.dispose(); + } + catch (error) { } + global.BUNEXT_BUNDLER_CTX = undefined; + try { + await global.BUNEXT_SSR_BUNDLER_CTX?.dispose(); + } + catch (error) { } + global.BUNEXT_SSR_BUNDLER_CTX = undefined; +} diff --git a/dist/functions/bundler/pages-ssr-context-bundler.js b/dist/functions/bundler/pages-ssr-context-bundler.js index a950c5a..f86c687 100644 --- a/dist/functions/bundler/pages-ssr-context-bundler.js +++ b/dist/functions/bundler/pages-ssr-context-bundler.js @@ -8,6 +8,7 @@ import grabRootFilePath from "../server/web-pages/grab-root-file-path"; import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin"; import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker"; import cleanupArtifacts from "../server/cleanup-artifacts"; +import { evictDeletedModules } from "../../utils/import-fresh"; import { writeFileSync } from "fs"; import path from "path"; import { log } from "../../utils/log"; @@ -165,6 +166,9 @@ async function doSSRRebuild(params) { global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = false; global.BUNEXT_BUILD_FAILED = false; await cleanupArtifacts({ paradigm: "ssr" }); + // esbuild has now replaced superseded outputs on disk; release the + // ones the server already imported. + evictDeletedModules(BUNX_CWD_MODULE_CACHE_DIR); } catch (error) { global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = true; diff --git a/dist/functions/bunext-init.js b/dist/functions/bunext-init.js index 017d14a..0645e8e 100644 --- a/dist/functions/bunext-init.js +++ b/dist/functions/bunext-init.js @@ -8,6 +8,7 @@ import serverPostBuildFn from "./server/server-post-build-fn"; import reactModulesBundler from "./bundler/react-modules-bundler"; import grabConstants from "../utils/grab-constants"; import watcherEsbuildCTX from "./server/watcher-esbuild-ctx"; +import disposeBundlerContexts from "./bundler/dispose-bundler-contexts"; import { readFileSync } from "fs"; const dirNames = grabDirNames(); const { PAGES_DIR, BUNDLER_CTX_MAP_JSON, SSR_BUNDLER_CTX_MAP_JSON } = dirNames; @@ -86,6 +87,9 @@ export default async function bunextInit(params) { else { log.build(`Building Modules ...`); await allPagesESBuildContextBundler({ start: true }); + // The server only needs the artifact maps; don't keep + // esbuild's build state alive for the rest of the process. + await disposeBundlerContexts(); } cron(); } diff --git a/dist/functions/server/bunext-req-handler.js b/dist/functions/server/bunext-req-handler.js index dc112f1..90d9b3e 100644 --- a/dist/functions/server/bunext-req-handler.js +++ b/dist/functions/server/bunext-req-handler.js @@ -16,15 +16,19 @@ const HMR_RETRY_COOLDOWN_MS = 5000; let lastHmrRetryTime = 0; export default async function bunextRequestHandler({ req: initial_req, server, }) { const is_dev = isDevelopment(); - let req = initial_req.clone(); + const middleware = global.BUNEXT_CONSTANTS.config?.middleware; + // Middleware may consume `initial_req`'s body, so it gets its own copy. + // Cloning tees the body stream and buffers whatever one side doesn't + // read, so only pay for it when middleware is configured. + let req = middleware ? initial_req.clone() : initial_req; try { let url = new URL(req.url); if (checkExcludedPatterns({ path: url.pathname })) { return Response.json({ success: false, msg: `Invalid Path` }, { status: 404 }); } let response = undefined; - if (global.BUNEXT_CONSTANTS.config?.middleware) { - const middleware_res = await global.BUNEXT_CONSTANTS.config.middleware({ + if (middleware) { + const middleware_res = await middleware({ req: initial_req, url, }); diff --git a/dist/functions/server/full-rebuild.js b/dist/functions/server/full-rebuild.js index f30ff7f..b12cdb6 100644 --- a/dist/functions/server/full-rebuild.js +++ b/dist/functions/server/full-rebuild.js @@ -3,6 +3,8 @@ import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context- import serverPostBuildFn from "./server-post-build-fn"; import { restartWatcher } from "./watcher-esbuild-ctx"; import { logBuildErrorsOnce } from "../../utils/build-error-log"; +import disposeBundlerContexts from "../bundler/dispose-bundler-contexts"; +import isDevelopment from "../../utils/is-development"; // Single-flight: error recovery paths (grab-page-component retries, the // watcher, post-build HMR pushes) all call fullRebuild, and each rebuild // triggers serverPostBuildFn which can re-enter error handling. Without this @@ -33,18 +35,17 @@ async function doFullRebuild(params) { log.watch(msg); } global.BUNEXT_ROUTER.reload(); - try { - await global.BUNEXT_BUNDLER_CTX?.dispose(); - global.BUNEXT_BUNDLER_CTX = undefined; - await global.BUNEXT_SSR_BUNDLER_CTX?.dispose(); - global.BUNEXT_SSR_BUNDLER_CTX = undefined; - } - catch (error) { } + await disposeBundlerContexts(); await allPagesESBuildContextBundler({ post_build_fn: async (params) => { await serverPostBuildFn(params); }, }); + // Production never rebuilds incrementally; don't keep esbuild's + // build state alive for the rest of the process. + if (!isDevelopment()) { + await disposeBundlerContexts(); + } } catch (error) { logBuildErrorsOnce([{ text: error?.message || String(error) }], (line) => log.error(line)); diff --git a/dist/functions/server/handle-routes.js b/dist/functions/server/handle-routes.js index 42b0198..9ae9f3f 100644 --- a/dist/functions/server/handle-routes.js +++ b/dist/functions/server/handle-routes.js @@ -8,6 +8,7 @@ import path from "path"; import grabDirNames from "../../utils/grab-dir-names"; import grabRelativePath from "../../utils/grab-relative-path"; import { RequestBodyTooLargeError } from "../../utils/read-request-body"; +import importFresh from "../../utils/import-fresh"; const { ROOT_DIR } = grabDirNames(); export default async function ({ req }) { try { @@ -29,18 +30,14 @@ export default async function ({ req }) { }); } let module; - const now = Date.now(); const relative_match_path = grabRelativePath(match.filePath); if (is_dev && global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[relative_match_path]?.path) { const target_import = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_match_path].path); - module = await import(`${target_import}?t=${now}`); + module = await importFresh(target_import); } else { - const import_path = is_dev - ? `${match.filePath}?t=${now}` - : match.filePath; - module = await import(import_path); + module = await importFresh(match.filePath); } const config = module.config; const maxBodyBytes = config?.max_request_body_mb diff --git a/dist/functions/server/start-server.js b/dist/functions/server/start-server.js index 902caf3..0e49e3d 100644 --- a/dist/functions/server/start-server.js +++ b/dist/functions/server/start-server.js @@ -2,7 +2,7 @@ import _ from "lodash"; import { log } from "../../utils/log"; import serverParamsGen from "./server-params-gen"; import isDevelopment from "../../utils/is-development"; -import { restartWatcher, watcherRestartAvailable, } from "./watcher-esbuild-ctx"; +import { watcherWatchdogTick } from "./watcher-esbuild-ctx"; export default async function startServer() { const serverParams = await serverParamsGen(); const server = Bun.serve(serverParams); @@ -10,15 +10,9 @@ export default async function startServer() { global.BUNEXT_SERVER = server; log.server(`http://${server.hostname}:${server.port}`); if (is_dev) { - // Watchdog: periodically re-create the FS watcher in case it went - // stale and stopped receiving events. Skipped while a recent restart - // (e.g. by fullRebuild) is still fresh so the watcher isn't churned - // twice in a row. - setInterval(() => { - if (watcherRestartAvailable()) { - restartWatcher(); - } - }, 5000); + // Watchdog: re-create the FS watcher if it went stale and stopped + // receiving events. + setInterval(watcherWatchdogTick, 5000); } return server; } diff --git a/dist/functions/server/watcher-esbuild-ctx.d.ts b/dist/functions/server/watcher-esbuild-ctx.d.ts index 75306d7..7ca486a 100644 --- a/dist/functions/server/watcher-esbuild-ctx.d.ts +++ b/dist/functions/server/watcher-esbuild-ctx.d.ts @@ -5,10 +5,12 @@ */ export declare function restartWatcher(): void; /** - * True when the watcher was last (re)created long enough ago that the - * watchdog should consider re-creating it again. + * Called periodically by the dev server. Re-creates the watcher only when it + * failed to report the probe written on the previous tick, i.e. when it went + * stale. A healthy watcher is left alone: re-creating it rescans the whole + * project tree. */ -export declare function watcherRestartAvailable(): boolean; +export declare function watcherWatchdogTick(): void; export default function watcherEsbuildCTX(params?: { pages_snapshot?: string; }): Promise; diff --git a/dist/functions/server/watcher-esbuild-ctx.js b/dist/functions/server/watcher-esbuild-ctx.js index f00c419..1a18b39 100644 --- a/dist/functions/server/watcher-esbuild-ctx.js +++ b/dist/functions/server/watcher-esbuild-ctx.js @@ -1,6 +1,6 @@ import chokidar from "chokidar"; import path from "path"; -import { existsSync } from "fs"; +import { existsSync, mkdirSync, writeFileSync } from "fs"; import grabDirNames from "../../utils/grab-dir-names"; import fullRebuild from "./full-rebuild"; import { AppData } from "../../data/app-data"; @@ -10,10 +10,18 @@ import grabAllPages from "../../utils/grab-all-pages"; import serverPostBuildFn from "./server-post-build-fn"; import { log } from "../../utils/log"; import { logBuildErrorsOnce } from "../../utils/build-error-log"; -const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); -// Tracks the last watcher re-creation so the 5s watchdog in start-server -// doesn't fight fullRebuild (which also restarts the watcher after a rebuild). +const { ROOT_DIR, BUNX_CWD_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); +// The watchdog writes this file and expects the watcher to report it. It +// sits under `.bunext` (otherwise ignored), so only this path is watched. +const WATCH_PROBE_DIR = path.join(BUNX_CWD_DIR, "watch-probe"); +const WATCH_PROBE_FILE = path.join(WATCH_PROBE_DIR, "probe"); +// Dedupes near-simultaneous restarts (fullRebuild, watchdog). let last_watcher_restart = 0; +// The current watcher finished its initial scan; probes written before +// that may not produce events. +let watcher_ready = false; +// A probe was written and the watcher hasn't reported it yet. +let probe_pending = false; /** * Closes and re-creates the FS watcher. Deduped: callers that restart the * watcher around the same time (fullRebuild, reloadWatcher, watchdog) only @@ -48,11 +56,27 @@ function grabPagesSnapshot() { } } /** - * True when the watcher was last (re)created long enough ago that the - * watchdog should consider re-creating it again. + * Called periodically by the dev server. Re-creates the watcher only when it + * failed to report the probe written on the previous tick, i.e. when it went + * stale. A healthy watcher is left alone: re-creating it rescans the whole + * project tree. */ -export function watcherRestartAvailable() { - return Date.now() - last_watcher_restart > 6000; +export function watcherWatchdogTick() { + if (!global.BUNEXT_PAGES_SRC_WATCHER || !watcher_ready) + return; + if (probe_pending) { + restartWatcher(); + return; + } + try { + mkdirSync(WATCH_PROBE_DIR, { recursive: true }); + writeFileSync(WATCH_PROBE_FILE, String(Date.now())); + probe_pending = true; + } + catch (error) { } +} +function isWatchProbePath(p) { + return (p === WATCH_PROBE_FILE || p === WATCH_PROBE_DIR || p === BUNX_CWD_DIR); } const TARGET_FILES_MATCH = /\.(tsx?|jsx?|css)$/; // Rebuild coalescing: editors emit bursts of events per save (change + @@ -170,10 +194,10 @@ function isPathExcluded(filename, full_file_path) { // install watches (and deliver events) for trees the bundler never reads. function buildIgnoredPatterns() { const ignored = [ - /(^|[\/\\])\../, + (p) => !isWatchProbePath(p) && /(^|[\/\\])\../.test(p), /(^|[\/\\])node_modules([\/\\]|$)/, /(^|[\/\\])public([\/\\]|$)/, - /(^|[\/\\])\.bunext([\/\\]|$)/, + (p) => !isWatchProbePath(p) && /(^|[\/\\])\.bunext([\/\\]|$)/.test(p), /(^|[\/\\])\.git([\/\\]|$)/, /(^|[\/\\])dist([\/\\]|$)/, /bun\.lockb$/, @@ -195,6 +219,12 @@ function buildIgnoredPatterns() { return ignored; } export default async function watcherEsbuildCTX(params) { + try { + mkdirSync(WATCH_PROBE_DIR, { recursive: true }); + } + catch (error) { } + watcher_ready = false; + probe_pending = false; const pages_src_watcher = chokidar.watch(ROOT_DIR, { ignored: buildIgnoredPatterns(), persistent: true, @@ -202,18 +232,27 @@ export default async function watcherEsbuildCTX(params) { }); watcherHandlers(pages_src_watcher, (event, filePath) => handleEvent(event, filePath)); const pages_snapshot = params?.pages_snapshot; - if (pages_snapshot !== undefined) { - pages_src_watcher.on("ready", () => { - const current = grabPagesSnapshot(); - if (current !== undefined && current !== pages_snapshot) { - pending_structural = true; - scheduleRebuild(); - } - }); - } + pages_src_watcher.on("ready", () => { + if (global.BUNEXT_PAGES_SRC_WATCHER === pages_src_watcher) { + watcher_ready = true; + } + if (pages_snapshot === undefined) + return; + const current = grabPagesSnapshot(); + if (current !== undefined && current !== pages_snapshot) { + pending_structural = true; + scheduleRebuild(); + } + }); global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher; } function handleEvent(event, filePath) { + if (isWatchProbePath(filePath)) { + if (filePath === WATCH_PROBE_FILE) { + probe_pending = false; + } + return; + } try { const filename = path.relative(ROOT_DIR, filePath); const full_file_path = filePath; diff --git a/dist/functions/server/web-pages/generate-web-html.js b/dist/functions/server/web-pages/generate-web-html.js index d854506..125e69b 100644 --- a/dist/functions/server/web-pages/generate-web-html.js +++ b/dist/functions/server/web-pages/generate-web-html.js @@ -77,8 +77,16 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa let htmlBody; let render_error; let has_render_error = false; + const render_timeout_ms = global.BUNEXT_CONFIG.ssr_render_timeout_ms || + AppData["DefaultSSRRenderTimeoutMs"]; + // Without a deadline, a Suspense boundary that never settles keeps the + // request (and its whole render tree) alive forever. The timer is + // cleared once the render ends so finished requests don't linger. + const render_abort = new AbortController(); + const render_timeout = setTimeout(() => render_abort.abort(new RenderTimeoutError(`Page render timed out after ${render_timeout_ms}ms`)), render_timeout_ms); try { const stream = await renderToReadableStream(final_component, { + signal: render_abort.signal, onError(error) { if (error?.message?.includes('unique "key" prop')) return; @@ -86,12 +94,16 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa has_render_error = true; render_error = error; } - original_console.error(error); + // A timeout has no useful stack; it's logged upstream. + if (!(error instanceof RenderTimeoutError)) { + original_console.error(error); + } }, }); htmlBody = await new Response(stream).text(); } finally { + clearTimeout(render_timeout); restoreConsole(); } // A component crash during SSR must surface as a proper error (handled @@ -103,6 +115,8 @@ export default async function genWebHTML({ component: Main, pageProps, bundledMa html += htmlBody; return html; } +class RenderTimeoutError extends Error { +} let original_console = console; let silenced_renders = 0; /** diff --git a/dist/functions/server/web-pages/grab-file-path-module.d.ts b/dist/functions/server/web-pages/grab-file-path-module.d.ts deleted file mode 100644 index 664e86e..0000000 --- a/dist/functions/server/web-pages/grab-file-path-module.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -type Params = { - file_path: string; - out_file?: string; -}; -export default function grabFilePathModule({ file_path, out_file, }: Params): Promise; -export {}; diff --git a/dist/functions/server/web-pages/grab-file-path-module.js b/dist/functions/server/web-pages/grab-file-path-module.js deleted file mode 100644 index ed90b6a..0000000 --- a/dist/functions/server/web-pages/grab-file-path-module.js +++ /dev/null @@ -1,30 +0,0 @@ -import isDevelopment from "../../../utils/is-development"; -import * as esbuild from "esbuild"; -import grabDirNames from "../../../utils/grab-dir-names"; -import path from "path"; -import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin"; -export default async function grabFilePathModule({ file_path, out_file, }) { - const dev = isDevelopment(); - const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); - const target_cache_file_path = out_file || - path.join(BUNX_CWD_MODULE_CACHE_DIR, `${path.basename(file_path)}.js`); - await esbuild.build({ - entryPoints: [file_path], - bundle: true, - format: "esm", - target: "es2020", - platform: "node", - // external: ["react", "react-dom"], - minify: true, - define: { - "process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"), - }, - metafile: true, - plugins: [tailwindEsbuildPlugin], - jsx: "automatic", - outfile: target_cache_file_path, - }); - // Loader.registry.delete(target_cache_file_path); - const module = await import(`${target_cache_file_path}?t=${Date.now()}`); - return module; -} diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index bd6e334..075f831 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -109,6 +109,11 @@ export type BunextConfig = { * `/robots.txt` disallows all crawlers regardless of `public/robots.txt`. */ disable_seo?: boolean; + /** + * Maximum time (ms) a page's server render may take before it is + * aborted and answered with a 500. Defaults to 30000. + */ + ssr_render_timeout_ms?: number; }; export type BunextConfigMiddlewareParams = { req: Request; diff --git a/dist/utils/import-fresh.d.ts b/dist/utils/import-fresh.d.ts index 4020179..ad33f2c 100644 --- a/dist/utils/import-fresh.d.ts +++ b/dist/utils/import-fresh.d.ts @@ -1,7 +1,24 @@ /** - * Dev: bypass Bun's module registry so rebuilt files are picked up. - * Prod: import by stable path so each module is loaded (and kept) once. - * A unique `?t=` per call creates a new, never-evicted module record, so - * using it in production leaks memory on every request. + * Imports a module by its stable path. + * + * Dev: when the file changed since it was last imported, its registry + * entry is evicted first so the new version is evaluated and the old one + * can be garbage collected. A cache-busting `?t=` query would instead + * create a new, never-evicted module record on every call. + * + * Prod: plain import, so each module is loaded (and kept) once. */ export default function importFresh(file_path: string): Promise; +/** + * Drops a module from Bun's registry so its memory can be reclaimed and the + * next import re-evaluates it. Only the module itself is evicted, not the + * modules it imports. + */ +export declare function evictModule(file_path: string): void; +/** + * Evicts every registry entry under `dir` whose file no longer exists. Bundle + * outputs (entries and shared chunks) are content-hashed and esbuild deletes + * the superseded ones on rebuild, so a missing file means no current module + * imports it anymore. + */ +export declare function evictDeletedModules(dir: string): void; diff --git a/dist/utils/import-fresh.js b/dist/utils/import-fresh.js index c4d1337..486cac9 100644 --- a/dist/utils/import-fresh.js +++ b/dist/utils/import-fresh.js @@ -1,10 +1,65 @@ +import { existsSync, statSync } from "fs"; +import path from "path"; import isDevelopment from "./is-development"; /** - * Dev: bypass Bun's module registry so rebuilt files are picked up. - * Prod: import by stable path so each module is loaded (and kept) once. - * A unique `?t=` per call creates a new, never-evicted module record, so - * using it in production leaks memory on every request. + * File path -> mtime of the version currently held in Bun's module + * registry (dev only). */ -export default function importFresh(file_path) { - return import(isDevelopment() ? `${file_path}?t=${Date.now()}` : file_path); +const loaded_versions = new Map(); +/** + * Imports a module by its stable path. + * + * Dev: when the file changed since it was last imported, its registry + * entry is evicted first so the new version is evaluated and the old one + * can be garbage collected. A cache-busting `?t=` query would instead + * create a new, never-evicted module record on every call. + * + * Prod: plain import, so each module is loaded (and kept) once. + */ +export default async function importFresh(file_path) { + if (!isDevelopment()) { + return import(file_path); + } + let mtime; + try { + mtime = statSync(file_path).mtimeMs; + } + catch { } + if (loaded_versions.has(file_path) && + loaded_versions.get(file_path) !== mtime) { + evictModule(file_path); + } + try { + const mod = await import(file_path); + loaded_versions.set(file_path, mtime); + return mod; + } + catch (error) { + // Don't keep a failed evaluation around: the next request retries. + evictModule(file_path); + throw error; + } +} +/** + * Drops a module from Bun's registry so its memory can be reclaimed and the + * next import re-evaluates it. Only the module itself is evicted, not the + * modules it imports. + */ +export function evictModule(file_path) { + delete require.cache[file_path]; + loaded_versions.delete(file_path); +} +/** + * Evicts every registry entry under `dir` whose file no longer exists. Bundle + * outputs (entries and shared chunks) are content-hashed and esbuild deletes + * the superseded ones on rebuild, so a missing file means no current module + * imports it anymore. + */ +export function evictDeletedModules(dir) { + const prefix = dir.endsWith(path.sep) ? dir : dir + path.sep; + for (const key of Object.keys(require.cache)) { + if (key.startsWith(prefix) && !existsSync(key)) { + evictModule(key); + } + } } diff --git a/package.json b/package.json index 2ba98d1..9d3fb80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.3.1", + "version": "1.2.4", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/commands/dev/index.ts b/src/commands/dev/index.ts index 6c4f2a3..abf183b 100644 --- a/src/commands/dev/index.ts +++ b/src/commands/dev/index.ts @@ -11,9 +11,8 @@ const { BUNX_BUNDLER_ERROR_EXIT_FILE, } = grabDirNames(); -let retries = 0; -let timeout: any; const MAX_RETRIES = 5; +const RETRY_RESET_AFTER_MS = 10000; export default function () { return new Command("dev") @@ -33,58 +32,58 @@ export default function () { } async function dev() { - clearTimeout(timeout); + // A loop, not recursion: each restart would otherwise keep the previous + // call (and its subprocess) alive for the supervisor's whole lifetime. + let retries = 0; - if (retries >= MAX_RETRIES) { - console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`); - process.exit(1); + while (true) { + if (retries >= MAX_RETRIES) { + console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`); + process.exit(1); + } + + const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts"); + const dev_spawn_js_file = path.resolve(__dirname, "dev-spawn.js"); + + const final_spawn_file = existsSync(dev_spawn_js_file) + ? dev_spawn_js_file + : dev_spawn_file; + + const spawn_options: BunSpawnOptions = { + cmd: ["bun", final_spawn_file], + stdio: ["inherit", "inherit", "inherit"], + async onExit(subprocess, exitCode, signalCode, error) { + writeErrorFile({ exitCode, error }); + }, + env: { + ...process.env, + NODE_ENV: "development", + }, + }; + + const started_at = Date.now(); + + let child_process; + try { + child_process = Bun.spawn(spawn_options); + } catch (error) { + console.error(`Failed to start dev process:`, error); + retries++; + continue; + } + + const exited = await child_process.exited; + + // A null exit means the child was killed by a signal (e.g. Ctrl+C + // in the shared terminal), which we must not restart. + if (exited == null) { + return; + } + + // Restart on any numeric exit (0 included: the server should never + // exit cleanly). Only consecutive quick crashes count toward the + // limit; a child that ran for a while starts the count over. + retries = + Date.now() - started_at >= RETRY_RESET_AFTER_MS ? 1 : retries + 1; } - - const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts"); - const dev_spawn_js_file = path.resolve(__dirname, "dev-spawn.js"); - - const final_spawn_file = existsSync(dev_spawn_js_file) - ? dev_spawn_js_file - : dev_spawn_file; - - const spawn_options: BunSpawnOptions = { - cmd: ["bun", final_spawn_file], - stdio: ["inherit", "inherit", "inherit"], - async onExit(subprocess, exitCode, signalCode, error) { - writeErrorFile({ exitCode, error }); - }, - env: { - ...process.env, - NODE_ENV: "development", - }, - }; - - let dev_process; - try { - dev_process = Bun.spawn(spawn_options); - } catch (error) { - console.error(`Failed to start dev process:`, error); - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await dev(); - } - - const exited = await dev_process.exited; - - // Restart on any numeric exit (0 included: the server should never exit - // cleanly). A null exit means the child was killed by a signal (e.g. - // Ctrl+C in the shared terminal), which we must not restart. - if (exited != null) { - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await dev(); - } - - timeout = setTimeout(() => { - retries = 0; - }, 10000); } diff --git a/src/commands/start/index.ts b/src/commands/start/index.ts index 170aad8..cbb4e3b 100644 --- a/src/commands/start/index.ts +++ b/src/commands/start/index.ts @@ -7,9 +7,8 @@ import { existsSync, rmSync } from "fs"; const { BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); -let retries = 0; -let timeout: any; const MAX_RETRIES = 5; +const RETRY_RESET_AFTER_MS = 10000; export default function () { return new Command("start") @@ -24,59 +23,60 @@ export default function () { } async function start() { - clearTimeout(timeout); + // A loop, not recursion: each restart would otherwise keep the previous + // call (and its subprocess) alive for the supervisor's whole lifetime. + let retries = 0; - if (retries >= MAX_RETRIES) { - console.error( - `Production server crashed ${MAX_RETRIES} times. Exiting.`, - ); - process.exit(1); + while (true) { + if (retries >= MAX_RETRIES) { + console.error( + `Production server crashed ${MAX_RETRIES} times. Exiting.`, + ); + process.exit(1); + } + + const prod_spawn_file = path.resolve(__dirname, "prod-spawn.ts"); + const prod_spawn_js_file = path.resolve(__dirname, "prod-spawn.js"); + + const final_spawn_file = existsSync(prod_spawn_js_file) + ? prod_spawn_js_file + : prod_spawn_file; + + const spawn_options: BunSpawnOptions = { + cmd: ["bun", final_spawn_file], + stdio: ["inherit", "inherit", "inherit"], + onExit(subprocess, exitCode, signalCode, error) { + writeErrorFile({ exitCode, error }); + }, + env: { + ...process.env, + NODE_ENV: "production", + }, + }; + + const started_at = Date.now(); + + let child_process; + try { + child_process = Bun.spawn(spawn_options); + } catch (error) { + console.error(`Failed to start production process:`, error); + retries++; + continue; + } + + const exited = await child_process.exited; + + // A null exit means the child was killed by a signal (e.g. Ctrl+C + // in the shared terminal), which we must not restart. + if (exited == null) { + return; + } + + // Restart on any numeric exit (0 included: the server should never + // exit cleanly). Only consecutive quick crashes count toward the + // limit; a child that ran for a while starts the count over. + retries = + Date.now() - started_at >= RETRY_RESET_AFTER_MS ? 1 : retries + 1; } - - const prod_spawn_file = path.resolve(__dirname, "prod-spawn.ts"); - const prod_spawn_js_file = path.resolve(__dirname, "prod-spawn.js"); - - const final_spawn_file = existsSync(prod_spawn_js_file) - ? prod_spawn_js_file - : prod_spawn_file; - - const spawn_options: BunSpawnOptions = { - cmd: ["bun", final_spawn_file], - stdio: ["inherit", "inherit", "inherit"], - onExit(subprocess, exitCode, signalCode, error) { - writeErrorFile({ exitCode, error }); - }, - env: { - ...process.env, - NODE_ENV: "production", - }, - }; - - let dev_process; - try { - dev_process = Bun.spawn(spawn_options); - } catch (error) { - console.error(`Failed to start production process:`, error); - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await start(); - } - - const exited = await dev_process.exited; - - // Restart on any numeric exit (0 included). A null exit means the child - // was killed by a signal, which we must not restart. - if (exited != null) { - retries++; - timeout = setTimeout(() => { - retries = 0; - }, 10000); - return await start(); - } - - timeout = setTimeout(() => { - retries = 0; - }, 10000); } diff --git a/src/data/app-data.ts b/src/data/app-data.ts index 1919e5e..b277954 100644 --- a/src/data/app-data.ts +++ b/src/data/app-data.ts @@ -8,4 +8,5 @@ export const AppData = { BunextTmpFileExt: ".bunext_tmp.tsx", BunextHMRRetryRoute: "/.bunext/hmr-retry", DefaultMaxLogs: 50, + DefaultSSRRenderTimeoutMs: 30000, } as const; diff --git a/src/functions/bundler/dispose-bundler-contexts.ts b/src/functions/bundler/dispose-bundler-contexts.ts new file mode 100644 index 0000000..81d82da --- /dev/null +++ b/src/functions/bundler/dispose-bundler-contexts.ts @@ -0,0 +1,16 @@ +/** + * Disposes the hydration and SSR esbuild contexts, releasing the incremental + * build state esbuild keeps in memory for them. The bundlers re-create the + * contexts on their next run. + */ +export default async function disposeBundlerContexts() { + try { + await global.BUNEXT_BUNDLER_CTX?.dispose(); + } catch (error) {} + global.BUNEXT_BUNDLER_CTX = undefined; + + try { + await global.BUNEXT_SSR_BUNDLER_CTX?.dispose(); + } catch (error) {} + global.BUNEXT_SSR_BUNDLER_CTX = undefined; +} diff --git a/src/functions/bundler/pages-ssr-context-bundler.ts b/src/functions/bundler/pages-ssr-context-bundler.ts index 80f95c9..f174308 100644 --- a/src/functions/bundler/pages-ssr-context-bundler.ts +++ b/src/functions/bundler/pages-ssr-context-bundler.ts @@ -9,6 +9,7 @@ import grabRootFilePath from "../server/web-pages/grab-root-file-path"; import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin"; import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker"; import cleanupArtifacts from "../server/cleanup-artifacts"; +import { evictDeletedModules } from "../../utils/import-fresh"; import { writeFileSync } from "fs"; import path from "path"; import { log } from "../../utils/log"; @@ -220,6 +221,10 @@ async function doSSRRebuild(params?: RebuildParams) { global.BUNEXT_BUILD_FAILED = false; await cleanupArtifacts({ paradigm: "ssr" }); + + // esbuild has now replaced superseded outputs on disk; release the + // ones the server already imported. + evictDeletedModules(BUNX_CWD_MODULE_CACHE_DIR); } catch (error: any) { global.BUNEXT_SSR_BUNDLER_CTX_DISPOSED = true; global.BUNEXT_BUILD_FAILED = true; diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 538be5e..4811ec9 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -16,6 +16,7 @@ import serverPostBuildFn from "./server/server-post-build-fn"; import reactModulesBundler from "./bundler/react-modules-bundler"; import grabConstants from "../utils/grab-constants"; import watcherEsbuildCTX from "./server/watcher-esbuild-ctx"; +import disposeBundlerContexts from "./bundler/dispose-bundler-contexts"; import { readFileSync } from "fs"; /** @@ -162,6 +163,9 @@ export default async function bunextInit(params?: Params) { } else { log.build(`Building Modules ...`); await allPagesESBuildContextBundler({ start: true }); + // The server only needs the artifact maps; don't keep + // esbuild's build state alive for the rest of the process. + await disposeBundlerContexts(); } cron(); diff --git a/src/functions/server/bunext-req-handler.ts b/src/functions/server/bunext-req-handler.ts index 33aa81b..417dd47 100644 --- a/src/functions/server/bunext-req-handler.ts +++ b/src/functions/server/bunext-req-handler.ts @@ -26,7 +26,12 @@ export default async function bunextRequestHandler({ server, }: Params): Promise { const is_dev = isDevelopment(); - let req = initial_req.clone(); + const middleware = global.BUNEXT_CONSTANTS.config?.middleware; + + // Middleware may consume `initial_req`'s body, so it gets its own copy. + // Cloning tees the body stream and buffers whatever one side doesn't + // read, so only pay for it when middleware is configured. + let req = middleware ? initial_req.clone() : initial_req; try { let url = new URL(req.url); @@ -40,12 +45,11 @@ export default async function bunextRequestHandler({ let response: Response | undefined = undefined; - if (global.BUNEXT_CONSTANTS.config?.middleware) { - const middleware_res = - await global.BUNEXT_CONSTANTS.config.middleware({ - req: initial_req, - url, - }); + if (middleware) { + const middleware_res = await middleware({ + req: initial_req, + url, + }); if (middleware_res instanceof Response) { return middleware_res; diff --git a/src/functions/server/full-rebuild.ts b/src/functions/server/full-rebuild.ts index 29bfda9..9837b46 100644 --- a/src/functions/server/full-rebuild.ts +++ b/src/functions/server/full-rebuild.ts @@ -3,6 +3,8 @@ import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context- import serverPostBuildFn from "./server-post-build-fn"; import { restartWatcher } from "./watcher-esbuild-ctx"; import { logBuildErrorsOnce } from "../../utils/build-error-log"; +import disposeBundlerContexts from "../bundler/dispose-bundler-contexts"; +import isDevelopment from "../../utils/is-development"; // Single-flight: error recovery paths (grab-page-component retries, the // watcher, post-build HMR pushes) all call fullRebuild, and each rebuild @@ -42,19 +44,19 @@ async function doFullRebuild(params?: { msg?: string }) { global.BUNEXT_ROUTER.reload(); - try { - await global.BUNEXT_BUNDLER_CTX?.dispose(); - global.BUNEXT_BUNDLER_CTX = undefined; - - await global.BUNEXT_SSR_BUNDLER_CTX?.dispose(); - global.BUNEXT_SSR_BUNDLER_CTX = undefined; - } catch (error) {} + await disposeBundlerContexts(); await allPagesESBuildContextBundler({ post_build_fn: async (params) => { await serverPostBuildFn(params); }, }); + + // Production never rebuilds incrementally; don't keep esbuild's + // build state alive for the rest of the process. + if (!isDevelopment()) { + await disposeBundlerContexts(); + } } catch (error: any) { logBuildErrorsOnce( [{ text: error?.message || String(error) }], diff --git a/src/functions/server/handle-routes.ts b/src/functions/server/handle-routes.ts index 6feb2e2..b0819e1 100644 --- a/src/functions/server/handle-routes.ts +++ b/src/functions/server/handle-routes.ts @@ -13,6 +13,7 @@ import path from "path"; import grabDirNames from "../../utils/grab-dir-names"; import grabRelativePath from "../../utils/grab-relative-path"; import { RequestBodyTooLargeError } from "../../utils/read-request-body"; +import importFresh from "../../utils/import-fresh"; const { ROOT_DIR } = grabDirNames(); @@ -50,7 +51,6 @@ export default async function ({ req }: Params): Promise { } let module: any; - const now = Date.now(); const relative_match_path = grabRelativePath(match.filePath); @@ -63,12 +63,9 @@ export default async function ({ req }: Params): Promise { global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_match_path].path, ); - module = await import(`${target_import}?t=${now}`); + module = await importFresh(target_import); } else { - const import_path = is_dev - ? `${match.filePath}?t=${now}` - : match.filePath; - module = await import(import_path); + module = await importFresh(match.filePath); } const config = module.config as BunextServerRouteConfig | undefined; diff --git a/src/functions/server/start-server.ts b/src/functions/server/start-server.ts index af1fd5e..d346b0e 100644 --- a/src/functions/server/start-server.ts +++ b/src/functions/server/start-server.ts @@ -2,10 +2,7 @@ import _ from "lodash"; import { log } from "../../utils/log"; import serverParamsGen from "./server-params-gen"; import isDevelopment from "../../utils/is-development"; -import { - restartWatcher, - watcherRestartAvailable, -} from "./watcher-esbuild-ctx"; +import { watcherWatchdogTick } from "./watcher-esbuild-ctx"; export default async function startServer() { const serverParams = await serverParamsGen(); @@ -18,15 +15,9 @@ export default async function startServer() { log.server(`http://${server.hostname}:${server.port}`); if (is_dev) { - // Watchdog: periodically re-create the FS watcher in case it went - // stale and stopped receiving events. Skipped while a recent restart - // (e.g. by fullRebuild) is still fresh so the watcher isn't churned - // twice in a row. - setInterval(() => { - if (watcherRestartAvailable()) { - restartWatcher(); - } - }, 5000); + // Watchdog: re-create the FS watcher if it went stale and stopped + // receiving events. + setInterval(watcherWatchdogTick, 5000); } return server; diff --git a/src/functions/server/watcher-esbuild-ctx.ts b/src/functions/server/watcher-esbuild-ctx.ts index b924869..78daed6 100644 --- a/src/functions/server/watcher-esbuild-ctx.ts +++ b/src/functions/server/watcher-esbuild-ctx.ts @@ -1,6 +1,6 @@ import chokidar from "chokidar"; import path from "path"; -import { existsSync } from "fs"; +import { existsSync, mkdirSync, writeFileSync } from "fs"; import grabDirNames from "../../utils/grab-dir-names"; import fullRebuild from "./full-rebuild"; import { AppData } from "../../data/app-data"; @@ -13,11 +13,21 @@ import serverPostBuildFn from "./server-post-build-fn"; import { log } from "../../utils/log"; import { logBuildErrorsOnce } from "../../utils/build-error-log"; -const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames(); +const { ROOT_DIR, BUNX_CWD_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = + grabDirNames(); -// Tracks the last watcher re-creation so the 5s watchdog in start-server -// doesn't fight fullRebuild (which also restarts the watcher after a rebuild). +// The watchdog writes this file and expects the watcher to report it. It +// sits under `.bunext` (otherwise ignored), so only this path is watched. +const WATCH_PROBE_DIR = path.join(BUNX_CWD_DIR, "watch-probe"); +const WATCH_PROBE_FILE = path.join(WATCH_PROBE_DIR, "probe"); + +// Dedupes near-simultaneous restarts (fullRebuild, watchdog). let last_watcher_restart = 0; +// The current watcher finished its initial scan; probes written before +// that may not produce events. +let watcher_ready = false; +// A probe was written and the watcher hasn't reported it yet. +let probe_pending = false; /** * Closes and re-creates the FS watcher. Deduped: callers that restart the @@ -54,11 +64,30 @@ function grabPagesSnapshot(): string | undefined { } /** - * True when the watcher was last (re)created long enough ago that the - * watchdog should consider re-creating it again. + * Called periodically by the dev server. Re-creates the watcher only when it + * failed to report the probe written on the previous tick, i.e. when it went + * stale. A healthy watcher is left alone: re-creating it rescans the whole + * project tree. */ -export function watcherRestartAvailable() { - return Date.now() - last_watcher_restart > 6000; +export function watcherWatchdogTick() { + if (!global.BUNEXT_PAGES_SRC_WATCHER || !watcher_ready) return; + + if (probe_pending) { + restartWatcher(); + return; + } + + try { + mkdirSync(WATCH_PROBE_DIR, { recursive: true }); + writeFileSync(WATCH_PROBE_FILE, String(Date.now())); + probe_pending = true; + } catch (error) {} +} + +function isWatchProbePath(p: string) { + return ( + p === WATCH_PROBE_FILE || p === WATCH_PROBE_DIR || p === BUNX_CWD_DIR + ); } const TARGET_FILES_MATCH = /\.(tsx?|jsx?|css)$/; @@ -191,10 +220,11 @@ function isPathExcluded(filename: string, full_file_path: string): boolean { // install watches (and deliver events) for trees the bundler never reads. function buildIgnoredPatterns() { const ignored: Array boolean)> = [ - /(^|[\/\\])\../, + (p: string) => !isWatchProbePath(p) && /(^|[\/\\])\../.test(p), /(^|[\/\\])node_modules([\/\\]|$)/, /(^|[\/\\])public([\/\\]|$)/, - /(^|[\/\\])\.bunext([\/\\]|$)/, + (p: string) => + !isWatchProbePath(p) && /(^|[\/\\])\.bunext([\/\\]|$)/.test(p), /(^|[\/\\])\.git([\/\\]|$)/, /(^|[\/\\])dist([\/\\]|$)/, /bun\.lockb$/, @@ -222,6 +252,13 @@ function buildIgnoredPatterns() { export default async function watcherEsbuildCTX(params?: { pages_snapshot?: string; }) { + try { + mkdirSync(WATCH_PROBE_DIR, { recursive: true }); + } catch (error) {} + + watcher_ready = false; + probe_pending = false; + const pages_src_watcher = chokidar.watch(ROOT_DIR, { ignored: buildIgnoredPatterns(), persistent: true, @@ -234,15 +271,19 @@ export default async function watcherEsbuildCTX(params?: { ); const pages_snapshot = params?.pages_snapshot; - if (pages_snapshot !== undefined) { - pages_src_watcher.on("ready", () => { - const current = grabPagesSnapshot(); - if (current !== undefined && current !== pages_snapshot) { - pending_structural = true; - scheduleRebuild(); - } - }); - } + pages_src_watcher.on("ready", () => { + if (global.BUNEXT_PAGES_SRC_WATCHER === pages_src_watcher) { + watcher_ready = true; + } + + if (pages_snapshot === undefined) return; + + const current = grabPagesSnapshot(); + if (current !== undefined && current !== pages_snapshot) { + pending_structural = true; + scheduleRebuild(); + } + }); global.BUNEXT_PAGES_SRC_WATCHER = pages_src_watcher as any; } @@ -251,6 +292,13 @@ function handleEvent( event: "add" | "change" | "unlink" | "addDir" | "unlinkDir", filePath: string, ) { + if (isWatchProbePath(filePath)) { + if (filePath === WATCH_PROBE_FILE) { + probe_pending = false; + } + return; + } + try { const filename = path.relative(ROOT_DIR, filePath); const full_file_path = filePath; diff --git a/src/functions/server/web-pages/generate-web-html.tsx b/src/functions/server/web-pages/generate-web-html.tsx index e0013ad..e584239 100644 --- a/src/functions/server/web-pages/generate-web-html.tsx +++ b/src/functions/server/web-pages/generate-web-html.tsx @@ -180,8 +180,27 @@ export default async function genWebHTML({ let render_error: any; let has_render_error = false; + const render_timeout_ms = + global.BUNEXT_CONFIG.ssr_render_timeout_ms || + AppData["DefaultSSRRenderTimeoutMs"]; + + // Without a deadline, a Suspense boundary that never settles keeps the + // request (and its whole render tree) alive forever. The timer is + // cleared once the render ends so finished requests don't linger. + const render_abort = new AbortController(); + const render_timeout = setTimeout( + () => + render_abort.abort( + new RenderTimeoutError( + `Page render timed out after ${render_timeout_ms}ms`, + ), + ), + render_timeout_ms, + ); + try { const stream = await renderToReadableStream(final_component, { + signal: render_abort.signal, onError(error: any) { if (error?.message?.includes('unique "key" prop')) return; @@ -190,12 +209,16 @@ export default async function genWebHTML({ render_error = error; } - original_console.error(error); + // A timeout has no useful stack; it's logged upstream. + if (!(error instanceof RenderTimeoutError)) { + original_console.error(error); + } }, }); htmlBody = await new Response(stream).text(); } finally { + clearTimeout(render_timeout); restoreConsole(); } @@ -211,6 +234,8 @@ export default async function genWebHTML({ return html; } +class RenderTimeoutError extends Error {} + let original_console: Pick< Console, "log" | "warn" | "error" | "info" | "debug" diff --git a/src/functions/server/web-pages/grab-file-path-module.tsx b/src/functions/server/web-pages/grab-file-path-module.tsx deleted file mode 100644 index 35a4f25..0000000 --- a/src/functions/server/web-pages/grab-file-path-module.tsx +++ /dev/null @@ -1,45 +0,0 @@ -import isDevelopment from "../../../utils/is-development"; -import * as esbuild from "esbuild"; -import grabDirNames from "../../../utils/grab-dir-names"; -import path from "path"; -import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin"; - -type Params = { - file_path: string; - out_file?: string; -}; - -export default async function grabFilePathModule({ - file_path, - out_file, -}: Params): Promise { - const dev = isDevelopment(); - const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames(); - const target_cache_file_path = - out_file || - path.join(BUNX_CWD_MODULE_CACHE_DIR, `${path.basename(file_path)}.js`); - - await esbuild.build({ - entryPoints: [file_path], - bundle: true, - format: "esm", - target: "es2020", - platform: "node", - // external: ["react", "react-dom"], - minify: true, - define: { - "process.env.NODE_ENV": JSON.stringify( - dev ? "development" : "production", - ), - }, - metafile: true, - plugins: [tailwindEsbuildPlugin], - jsx: "automatic", - outfile: target_cache_file_path, - }); - - // Loader.registry.delete(target_cache_file_path); - const module = await import(`${target_cache_file_path}?t=${Date.now()}`); - - return module as T; -} diff --git a/src/types/index.ts b/src/types/index.ts index 47c8266..bb921d3 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -126,6 +126,11 @@ export type BunextConfig = { * `/robots.txt` disallows all crawlers regardless of `public/robots.txt`. */ disable_seo?: boolean; + /** + * Maximum time (ms) a page's server render may take before it is + * aborted and answered with a 500. Defaults to 30000. + */ + ssr_render_timeout_ms?: number; }; export type BunextConfigMiddlewareParams = { diff --git a/src/utils/import-fresh.ts b/src/utils/import-fresh.ts index 018ea0c..bb5f623 100644 --- a/src/utils/import-fresh.ts +++ b/src/utils/import-fresh.ts @@ -1,13 +1,75 @@ +import { existsSync, statSync } from "fs"; +import path from "path"; import isDevelopment from "./is-development"; /** - * Dev: bypass Bun's module registry so rebuilt files are picked up. - * Prod: import by stable path so each module is loaded (and kept) once. - * A unique `?t=` per call creates a new, never-evicted module record, so - * using it in production leaks memory on every request. + * File path -> mtime of the version currently held in Bun's module + * registry (dev only). */ -export default function importFresh(file_path: string): Promise { - return import( - isDevelopment() ? `${file_path}?t=${Date.now()}` : file_path - ); +const loaded_versions = new Map(); + +/** + * Imports a module by its stable path. + * + * Dev: when the file changed since it was last imported, its registry + * entry is evicted first so the new version is evaluated and the old one + * can be garbage collected. A cache-busting `?t=` query would instead + * create a new, never-evicted module record on every call. + * + * Prod: plain import, so each module is loaded (and kept) once. + */ +export default async function importFresh( + file_path: string, +): Promise { + if (!isDevelopment()) { + return import(file_path); + } + + let mtime: number | undefined; + try { + mtime = statSync(file_path).mtimeMs; + } catch {} + + if ( + loaded_versions.has(file_path) && + loaded_versions.get(file_path) !== mtime + ) { + evictModule(file_path); + } + + try { + const mod = await import(file_path); + loaded_versions.set(file_path, mtime); + return mod; + } catch (error) { + // Don't keep a failed evaluation around: the next request retries. + evictModule(file_path); + throw error; + } +} + +/** + * Drops a module from Bun's registry so its memory can be reclaimed and the + * next import re-evaluates it. Only the module itself is evicted, not the + * modules it imports. + */ +export function evictModule(file_path: string) { + delete require.cache[file_path]; + loaded_versions.delete(file_path); +} + +/** + * Evicts every registry entry under `dir` whose file no longer exists. Bundle + * outputs (entries and shared chunks) are content-hashed and esbuild deletes + * the superseded ones on rebuild, so a missing file means no current module + * imports it anymore. + */ +export function evictDeletedModules(dir: string) { + const prefix = dir.endsWith(path.sep) ? dir : dir + path.sep; + + for (const key of Object.keys(require.cache)) { + if (key.startsWith(prefix) && !existsSync(key)) { + evictModule(key); + } + } }