@rsc-kit/core 0.22.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/vite.d.ts CHANGED
@@ -429,6 +429,23 @@ export type TypecheckOutcome = {
429
429
  * is the message, because that is what the developer would have read.
430
430
  */
431
431
  export declare function typecheckProject(root: string): TypecheckOutcome;
432
+ /**
433
+ * A Nitro runtime plugin, so a rollout stops a server cleanly.
434
+ *
435
+ * On SIGTERM - a rollout, docker stop - srvx stops taking connections and
436
+ * gives in-flight requests SERVER_SHUTDOWN_TIMEOUT seconds (5) to finish. It
437
+ * does not exit: the process ends only once nothing holds it open, and an app
438
+ * with a database pool or a queue client always has something, so it waited
439
+ * for its SIGKILL and lost the log lines written meanwhile. This waits out the
440
+ * same window, calls instrumentation.ts's shutdown() if the app has one -
441
+ * bounded, so a close that hangs cannot hold the pod - and exits.
442
+ *
443
+ * Unref'd throughout: an app with nothing open exits the moment srvx has
444
+ * drained, as it did before. Built servers only - not a Worker, which has no
445
+ * process to signal, and not the dev server or the build, where Ctrl+C should
446
+ * stop at once.
447
+ */
448
+ export declare const SHUTDOWN_PLUGIN = "export default function rscKitShutdown() {\n if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') return\n if (typeof process === 'undefined' || typeof process.once !== 'function') return\n\n const unref = (timer) => (timer && typeof timer.unref === 'function' ? timer.unref() : timer)\n let stopping = false\n\n const stop = () => {\n if (stopping) return\n stopping = true\n\n const drain = (Number(process.env.SERVER_SHUTDOWN_TIMEOUT) || 5) * 1000\n const limit = (Number(process.env.RSC_SHUTDOWN_TIMEOUT) || 10) * 1000\n\n unref(setTimeout(() => {\n const hook = globalThis.__rscKitShutdown\n\n Promise.race([\n Promise.resolve().then(() => (typeof hook === 'function' ? hook() : undefined)),\n new Promise((resolve) => unref(setTimeout(resolve, limit))),\n ])\n .catch((error) => console.error('[rsc-kit] instrumentation.ts shutdown() failed:', error))\n .finally(() => process.exit(0))\n }, drain + 250))\n }\n\n process.once('SIGTERM', stop)\n process.once('SIGINT', stop)\n}\n";
432
449
  /**
433
450
  * The PUBLIC_ variables an app still sets, and where each one is set.
434
451
  *
package/dist/vite.js CHANGED
@@ -2475,13 +2475,14 @@ function instrumentationFile() {
2475
2475
  const file = join(sourceDir, `instrumentation.${ext}`);
2476
2476
  if (!existsSync(file))
2477
2477
  continue;
2478
- // Both halves are optional: a file of imports is a bootstrap, a file
2479
- // with register() is a hook. The entry must only name `register` when it
2478
+ // Every part is optional: a file of imports is a bootstrap, a file with
2479
+ // register() or shutdown() has hooks. The entry must only name one when it
2480
2480
  // exists - a namespace import's missing member is a bundler warning on
2481
2481
  // every build (IMPORT_IS_UNDEFINED), which is the app being told off for
2482
2482
  // a file written exactly as documented.
2483
- const hasRegister = /export\s+(?:async\s+)?(?:function\s+register\b|const\s+register\b|let\s+register\b|\{[^}]*\bregister\b[^}]*\})/.test(readFileSync(file, "utf-8"));
2484
- return { file, hasRegister };
2483
+ const source = readFileSync(file, "utf-8");
2484
+ const exports = (name) => new RegExp(`export\\s+(?:async\\s+)?(?:function\\s+${name}\\b|const\\s+${name}\\b|let\\s+${name}\\b|\\{[^}]*\\b${name}\\b[^}]*\\})`).test(source);
2485
+ return { file, hasRegister: exports("register"), hasShutdown: exports("shutdown") };
2485
2486
  }
2486
2487
  return null;
2487
2488
  }
@@ -2551,11 +2552,11 @@ ${
2551
2552
  // First, before any page: an import's side effects run in import order,
2552
2553
  // and a package configured here has to be configured before a page module
2553
2554
  // that reads it at evaluation time.
2554
- instrumentation?.hasRegister
2555
+ instrumentation?.hasRegister || instrumentation?.hasShutdown
2555
2556
  ? `import * as __instrumentation from ${JSON.stringify(instrumentation.file)}`
2556
2557
  : instrumentation
2557
- ? `import ${JSON.stringify(instrumentation.file)}\nconst __instrumentation: { register?: () => unknown } = {}`
2558
- : "const __instrumentation: { register?: () => unknown } = {}"}
2558
+ ? `import ${JSON.stringify(instrumentation.file)}\nconst __instrumentation: { register?: () => unknown; shutdown?: () => unknown } = {}`
2559
+ : "const __instrumentation: { register?: () => unknown; shutdown?: () => unknown } = {}"}
2559
2560
  import { SegmentBoundary } from ${JSON.stringify(join(packageDir, "js/SegmentBoundary"))}
2560
2561
  import { LoadingBoundary } from ${JSON.stringify(join(packageDir, "js/LoadingBoundary"))}
2561
2562
  import { DocumentTitle } from ${JSON.stringify(join(packageDir, "js/DocumentTitle"))}
@@ -2998,6 +2999,15 @@ function instrumented(): Promise<void> {
2998
2999
  // start, start now.
2999
3000
  const isolateRuntime = typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers'
3000
3001
 
3002
+ // instrumentation.ts's shutdown(), for the server's shutdown plugin
3003
+ // (SHUTDOWN_PLUGIN in vite.ts), which calls it once in-flight requests have
3004
+ // drained and then exits. Handed over on globalThis rather than through a
3005
+ // request: every request reaches this module, and one carrying a header must
3006
+ // not be able to close the app's database pool.
3007
+ if (!isolateRuntime) {
3008
+ ;(globalThis as { __rscKitShutdown?: () => unknown }).__rscKitShutdown = () => __instrumentation.shutdown?.()
3009
+ }
3010
+
3001
3011
  if (__instrumentation.register && !isolateRuntime) {
3002
3012
  instrumented().catch((error) => {
3003
3013
  console.error('[rsc-kit] instrumentation.ts register() failed:', error)
@@ -5882,6 +5892,52 @@ export default function rscKitStartup() {
5882
5892
  })
5883
5893
  }
5884
5894
  `;
5895
+ /**
5896
+ * A Nitro runtime plugin, so a rollout stops a server cleanly.
5897
+ *
5898
+ * On SIGTERM - a rollout, docker stop - srvx stops taking connections and
5899
+ * gives in-flight requests SERVER_SHUTDOWN_TIMEOUT seconds (5) to finish. It
5900
+ * does not exit: the process ends only once nothing holds it open, and an app
5901
+ * with a database pool or a queue client always has something, so it waited
5902
+ * for its SIGKILL and lost the log lines written meanwhile. This waits out the
5903
+ * same window, calls instrumentation.ts's shutdown() if the app has one -
5904
+ * bounded, so a close that hangs cannot hold the pod - and exits.
5905
+ *
5906
+ * Unref'd throughout: an app with nothing open exits the moment srvx has
5907
+ * drained, as it did before. Built servers only - not a Worker, which has no
5908
+ * process to signal, and not the dev server or the build, where Ctrl+C should
5909
+ * stop at once.
5910
+ */
5911
+ export const SHUTDOWN_PLUGIN = `export default function rscKitShutdown() {
5912
+ if (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') return
5913
+ if (typeof process === 'undefined' || typeof process.once !== 'function') return
5914
+
5915
+ const unref = (timer) => (timer && typeof timer.unref === 'function' ? timer.unref() : timer)
5916
+ let stopping = false
5917
+
5918
+ const stop = () => {
5919
+ if (stopping) return
5920
+ stopping = true
5921
+
5922
+ const drain = (Number(process.env.SERVER_SHUTDOWN_TIMEOUT) || 5) * 1000
5923
+ const limit = (Number(process.env.RSC_SHUTDOWN_TIMEOUT) || 10) * 1000
5924
+
5925
+ unref(setTimeout(() => {
5926
+ const hook = globalThis.__rscKitShutdown
5927
+
5928
+ Promise.race([
5929
+ Promise.resolve().then(() => (typeof hook === 'function' ? hook() : undefined)),
5930
+ new Promise((resolve) => unref(setTimeout(resolve, limit))),
5931
+ ])
5932
+ .catch((error) => console.error('[rsc-kit] instrumentation.ts shutdown() failed:', error))
5933
+ .finally(() => process.exit(0))
5934
+ }, drain + 250))
5935
+ }
5936
+
5937
+ process.once('SIGTERM', stop)
5938
+ process.once('SIGINT', stop)
5939
+ }
5940
+ `;
5885
5941
  /**
5886
5942
  * Where NODE_ENV=development came from, and what to do about it.
5887
5943
  *
@@ -6088,6 +6144,12 @@ export function rscKit(options = {}) {
6088
6144
  "\nexport default () => {};\n";
6089
6145
  nitro.options.plugins = ["#rsc-kit/polyfills", ...(nitro.options.plugins ?? [])];
6090
6146
  }
6147
+ // Every built server, instrumentation.ts or not: the hang it prevents
6148
+ // is any open connection, wherever the app opened it.
6149
+ if (!nitro.options.dev) {
6150
+ nitro.options.virtual["#rsc-kit/shutdown"] = SHUTDOWN_PLUGIN;
6151
+ nitro.options.plugins = [...(nitro.options.plugins ?? []), "#rsc-kit/shutdown"];
6152
+ }
6091
6153
  if (!instrumentationFile())
6092
6154
  return;
6093
6155
  nitro.options.virtual["#rsc-kit/startup"] = STARTUP_PLUGIN;