react-router-hono-server 2.25.3 โ†’ 3.0.0-next.0

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.
@@ -1,137 +1,120 @@
1
- import {
2
- bindIncomingRequestSocketInfo,
3
- cleanUpgradeListeners,
4
- createGetLoadContext,
5
- createWebSocket,
6
- getBuildMode,
7
- importBuild,
8
- patchUpgradeListener
9
- } from "../chunk-BH64OQMW.js";
10
- import {
11
- cache
12
- } from "../chunk-YI5FED4X.js";
13
-
14
- // src/adapters/bun.ts
1
+ import { a as getBuildMode, i as createWebSocket, n as cleanUpgradeListeners, o as importBuild, r as createGetLoadContext, s as patchUpgradeListener, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
2
+ import { cache } from "../middleware.js";
15
3
  import { Hono } from "hono";
16
- import { serveStatic } from "hono/bun";
17
4
  import { createMiddleware } from "hono/factory";
18
5
  import { logger } from "hono/logger";
19
6
  import { createRequestHandler } from "react-router";
20
- var serverInstance;
21
- var shutdownCallback;
7
+ import { serveStatic } from "hono/bun";
8
+ //#region src/adapters/bun.ts
9
+ let serverInstance;
10
+ let shutdownCallback;
22
11
  async function createHonoServer(options) {
23
- const build = await importBuild();
24
- const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
25
- const mergedOptions = {
26
- ...options,
27
- port: options?.port || Number(options?.customBunServer?.port) || Number(process.env.PORT) || 3e3,
28
- defaultLogger: options?.defaultLogger ?? true
29
- };
30
- const mode = getBuildMode();
31
- const PRODUCTION = mode === "production";
32
- const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
33
- if (PRODUCTION && mergedOptions.onGracefulShutdown) {
34
- shutdownCallback = mergedOptions.onGracefulShutdown;
35
- }
36
- const app = new Hono(mergedOptions.app);
37
- const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
38
- app,
39
- enabled: mergedOptions.useWebSocket ?? false
40
- });
41
- if (!PRODUCTION) {
42
- app.use(bindIncomingRequestSocketInfo());
43
- }
44
- await mergedOptions.beforeAll?.(app);
45
- app.use(
46
- `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
47
- cache(60 * 60 * 24 * 365),
48
- // 1 year
49
- serveStatic({
50
- root: clientBuildPath,
51
- ...mergedOptions.serveStaticOptions?.clientAssets
52
- })
53
- );
54
- app.use(
55
- "*",
56
- cache(60 * 60),
57
- // 1 hour
58
- serveStatic({
59
- root: PRODUCTION ? clientBuildPath : "./public",
60
- ...mergedOptions.serveStaticOptions?.publicAssets
61
- })
62
- );
63
- if (mergedOptions.defaultLogger) {
64
- app.use("*", logger());
65
- }
66
- if (mergedOptions.useWebSocket) {
67
- await mergedOptions.configure(app, { upgradeWebSocket });
68
- } else {
69
- await mergedOptions.configure?.(app);
70
- }
71
- const reactRouterApp = new Hono({
72
- strict: false
73
- });
74
- reactRouterApp.use((c, next) => {
75
- return createMiddleware(async (c2) => {
76
- const requestHandler = createRequestHandler(build, mode);
77
- const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
78
- return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
79
- })(c, next);
80
- });
81
- app.route(`${basename}`, reactRouterApp);
82
- if (basename) {
83
- app.route(`${basename}.data`, reactRouterApp);
84
- }
85
- let server = {
86
- ...mergedOptions.customBunServer,
87
- fetch: app.fetch,
88
- port: mergedOptions.port,
89
- development: !PRODUCTION
90
- };
91
- if (PRODUCTION) {
92
- server = injectWebSocket(server);
93
- serverInstance = Bun.serve(server);
94
- console.log(`Started server: ${serverInstance.protocol}://${serverInstance.hostname}:${serverInstance.port}`);
95
- if (shutdownCallback != null) {
96
- let gracefulShutdownInProgress = false;
97
- const gracefulShutdown = async (signal) => {
98
- if (gracefulShutdownInProgress) return;
99
- gracefulShutdownInProgress = true;
100
- try {
101
- if (serverInstance != null) {
102
- console.log(`
103
- \u{1F4E1} Received ${signal}, shutting down gracefully...`);
104
- await serverInstance.stop(false);
105
- console.log("Server stopped, all requests completed");
106
- if (shutdownCallback) {
107
- console.log("\u{1F9F9} Running cleanup tasks...");
108
- await shutdownCallback();
109
- console.log("\u2705 Cleanup complete!");
110
- }
111
- console.log("\u{1F44B} Graceful shutdown completed");
112
- }
113
- process.exit(0);
114
- } catch (error) {
115
- console.error("\u274C Shutdown failed:", error);
116
- process.exit(1);
117
- }
118
- };
119
- if (serverInstance != null) {
120
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
121
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
122
- console.log("\u2705 Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
123
- }
124
- }
125
- } else if (globalThis.__viteDevServer?.httpServer) {
126
- const httpServer = globalThis.__viteDevServer.httpServer;
127
- cleanUpgradeListeners(httpServer);
128
- injectWebSocket(httpServer);
129
- patchUpgradeListener(httpServer);
130
- console.log("\u{1F6A7} Running in development mode");
131
- }
132
- return server;
12
+ const build = await importBuild();
13
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
14
+ const mergedOptions = {
15
+ ...options,
16
+ port: options?.port || Number(options?.customBunServer?.port) || Number(process.env.PORT) || 3e3,
17
+ defaultLogger: options?.defaultLogger ?? true
18
+ };
19
+ const mode = getBuildMode();
20
+ const PRODUCTION = mode === "production";
21
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
22
+ if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
+ const app = new Hono(mergedOptions.app);
24
+ const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
25
+ app,
26
+ enabled: mergedOptions.useWebSocket ?? false
27
+ });
28
+ if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
29
+ /**
30
+ * Add optional middleware that runs before any built-in middleware, including assets serving.
31
+ */
32
+ await mergedOptions.beforeAll?.(app);
33
+ /**
34
+ * Serve assets files from build/client/assets
35
+ */
36
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({
37
+ root: clientBuildPath,
38
+ ...mergedOptions.serveStaticOptions?.clientAssets
39
+ }));
40
+ /**
41
+ * Serve public files
42
+ */
43
+ app.use("*", cache(3600), serveStatic({
44
+ root: PRODUCTION ? clientBuildPath : "./public",
45
+ ...mergedOptions.serveStaticOptions?.publicAssets
46
+ }));
47
+ /**
48
+ * Add logger middleware
49
+ */
50
+ if (mergedOptions.defaultLogger) app.use("*", logger());
51
+ /**
52
+ * Add optional middleware
53
+ */
54
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
55
+ else await mergedOptions.configure?.(app);
56
+ /**
57
+ * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
58
+ */
59
+ const reactRouterApp = new Hono({ strict: false });
60
+ reactRouterApp.use((c, next) => {
61
+ return createMiddleware(async (c) => {
62
+ const requestHandler = createRequestHandler(build, mode);
63
+ const loadContext = mergedOptions.getLoadContext?.(c, {
64
+ build,
65
+ mode
66
+ });
67
+ return requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
68
+ })(c, next);
69
+ });
70
+ app.route(`${basename}`, reactRouterApp);
71
+ if (basename) app.route(`${basename}.data`, reactRouterApp);
72
+ let server = {
73
+ ...mergedOptions.customBunServer,
74
+ fetch: app.fetch,
75
+ port: mergedOptions.port,
76
+ development: !PRODUCTION
77
+ };
78
+ if (PRODUCTION) {
79
+ server = injectWebSocket(server);
80
+ serverInstance = Bun.serve(server);
81
+ if (shutdownCallback != null) {
82
+ let gracefulShutdownInProgress = false;
83
+ const gracefulShutdown = async (signal) => {
84
+ if (gracefulShutdownInProgress) return;
85
+ gracefulShutdownInProgress = true;
86
+ try {
87
+ if (serverInstance != null) {
88
+ console.log(`\n๐Ÿ“ก Received ${signal}, shutting down gracefully...`);
89
+ await serverInstance.stop(false);
90
+ console.log("Server stopped, all requests completed");
91
+ if (shutdownCallback) {
92
+ console.log("๐Ÿงน Running cleanup tasks...");
93
+ await shutdownCallback();
94
+ console.log("โœ… Cleanup complete!");
95
+ }
96
+ console.log("๐Ÿ‘‹ Graceful shutdown completed");
97
+ }
98
+ process.exit(0);
99
+ } catch (error) {
100
+ console.error("โŒ Shutdown failed:", error);
101
+ process.exit(1);
102
+ }
103
+ };
104
+ if (serverInstance != null) {
105
+ process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
106
+ process.on("SIGINT", () => gracefulShutdown("SIGINT"));
107
+ console.log("โœ… Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
108
+ }
109
+ }
110
+ } else if (globalThis.__viteDevServer?.httpServer) {
111
+ const httpServer = globalThis.__viteDevServer.httpServer;
112
+ cleanUpgradeListeners(httpServer);
113
+ injectWebSocket(httpServer);
114
+ patchUpgradeListener(httpServer);
115
+ console.log("๐Ÿšง Running in development mode");
116
+ }
117
+ return server;
133
118
  }
134
- export {
135
- createGetLoadContext,
136
- createHonoServer
137
- };
119
+ //#endregion
120
+ export { createGetLoadContext, createHonoServer };
@@ -1,19 +1,15 @@
1
- import * as hono_types from 'hono/types';
2
- import { BlankEnv } from 'hono/types';
3
- import { Env, Hono } from 'hono';
4
- import { H as HonoServerOptionsBase, W as WithoutWebsocket } from '../helpers-BXdVojwF.js';
5
- export { c as createGetLoadContext } from '../helpers-BXdVojwF.js';
6
- import 'hono/ws';
7
- import 'react-router';
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
2
+ import { Env, Hono } from "hono";
3
+ import { BlankEnv } from "hono/types";
8
4
 
9
- interface HonoCloudflareOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
10
- }
5
+ //#region src/adapters/cloudflare.d.ts
6
+ interface HonoCloudflareOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {}
11
7
  type HonoServerOptions<E extends Env = BlankEnv> = HonoCloudflareOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
12
8
  /**
13
9
  * Create a Hono server
14
10
  *
15
11
  * @param config {@link HonoServerOptions} - The configuration options for the server
16
12
  */
17
- declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, hono_types.BlankSchema, "/">>;
18
-
19
- export { type HonoServerOptions, createHonoServer };
13
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, import("hono/types").BlankSchema, "/">>;
14
+ //#endregion
15
+ export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -1,106 +1,92 @@
1
- import {
2
- bindIncomingRequestSocketInfo,
3
- createGetLoadContext,
4
- getBuildMode,
5
- importBuild
6
- } from "../chunk-BH64OQMW.js";
7
- import {
8
- cache
9
- } from "../chunk-YI5FED4X.js";
10
-
11
- // src/adapters/cloudflare.ts
1
+ import { a as getBuildMode, o as importBuild, r as createGetLoadContext, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
2
+ import { cache } from "../middleware.js";
12
3
  import { Hono } from "hono";
13
4
  import { createMiddleware } from "hono/factory";
14
5
  import { logger } from "hono/logger";
15
6
  import { createRequestHandler } from "react-router";
7
+ //#region src/adapters/cloudflare.ts
8
+ /**
9
+ * Create a Hono server
10
+ *
11
+ * @param config {@link HonoServerOptions} - The configuration options for the server
12
+ */
16
13
  async function createHonoServer(options) {
17
- const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
18
- const mergedOptions = {
19
- ...options,
20
- defaultLogger: options?.defaultLogger ?? true
21
- };
22
- const mode = getBuildMode();
23
- const PRODUCTION = mode === "production";
24
- const app = new Hono(mergedOptions.app);
25
- await mergedOptions.beforeAll?.(app);
26
- app.use(
27
- // https://developers.cloudflare.com/workers/static-assets/binding/#experimental_serve_directly
28
- `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
29
- cache(60 * 60 * 24 * 365),
30
- // 1 year
31
- serveCloudflareAssets()
32
- );
33
- if (PRODUCTION) {
34
- app.use(
35
- // https://developers.cloudflare.com/workers/static-assets/binding/#experimental_serve_directly
36
- "*",
37
- cache(60 * 60),
38
- // 1 hour
39
- serveCloudflareAssets()
40
- );
41
- } else {
42
- const { serveStatic } = await import("@hono/node-server/serve-static");
43
- app.use(
44
- "*",
45
- cache(60 * 60),
46
- // 1 hour
47
- serveStatic({ root: "./public" })
48
- );
49
- app.use(bindIncomingRequestSocketInfo());
50
- }
51
- if (mergedOptions.defaultLogger) {
52
- app.use("*", logger());
53
- }
54
- await mergedOptions.configure?.(app);
55
- const reactRouterApp = new Hono({
56
- strict: false
57
- });
58
- reactRouterApp.use(async (c, next) => {
59
- const build = await importBuild();
60
- return createMiddleware(async (c2) => {
61
- const requestHandler = createRequestHandler(build, mode);
62
- const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
63
- return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
64
- })(c, next);
65
- });
66
- app.route(`${basename}`, reactRouterApp);
67
- if (basename) {
68
- app.route(`${basename}.data`, reactRouterApp);
69
- }
70
- if (!PRODUCTION) {
71
- console.log("\u{1F6A7} Running in development mode");
72
- }
73
- return app;
14
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
15
+ const mergedOptions = {
16
+ ...options,
17
+ defaultLogger: options?.defaultLogger ?? true
18
+ };
19
+ const mode = getBuildMode();
20
+ const PRODUCTION = mode === "production";
21
+ const app = new Hono(mergedOptions.app);
22
+ /**
23
+ * Add optional middleware that runs before any built-in middleware, including assets serving.
24
+ */
25
+ await mergedOptions.beforeAll?.(app);
26
+ /**
27
+ * Serve assets files from build/client/assets
28
+ */
29
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveCloudflareAssets());
30
+ /**
31
+ * Serve public files
32
+ */
33
+ if (PRODUCTION) app.use("*", cache(3600), serveCloudflareAssets());
34
+ else {
35
+ const { serveStatic } = await import("@hono/node-server/serve-static");
36
+ app.use("*", cache(3600), serveStatic({ root: "./public" }));
37
+ app.use(bindIncomingRequestSocketInfo());
38
+ }
39
+ /**
40
+ * Add logger middleware
41
+ */
42
+ if (mergedOptions.defaultLogger) app.use("*", logger());
43
+ /**
44
+ * Add optional middleware
45
+ */
46
+ await mergedOptions.configure?.(app);
47
+ /**
48
+ * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
49
+ */
50
+ const reactRouterApp = new Hono({ strict: false });
51
+ reactRouterApp.use(async (c, next) => {
52
+ const build = await importBuild();
53
+ return createMiddleware(async (c) => {
54
+ const requestHandler = createRequestHandler(build, mode);
55
+ const loadContext = mergedOptions.getLoadContext?.(c, {
56
+ build,
57
+ mode
58
+ });
59
+ return requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
60
+ })(c, next);
61
+ });
62
+ app.route(`${basename}`, reactRouterApp);
63
+ if (basename) app.route(`${basename}.data`, reactRouterApp);
64
+ if (!PRODUCTION) console.log("๐Ÿšง Running in development mode");
65
+ return app;
74
66
  }
75
- var warned = false;
67
+ let warned = false;
68
+ /**
69
+ * Serve static assets
70
+ *
71
+ * https://github.com/sergiodxa/remix-hono/blob/main/src/cloudflare.ts
72
+ */
76
73
  function serveCloudflareAssets() {
77
- return createMiddleware(async (c, next) => {
78
- const binding = c.env?.ASSETS;
79
- if (!binding) {
80
- if (!warned) {
81
- console.info(
82
- "\x1B[33m\nThe binding ASSETS is not set. Falling back to Cloudflare serving.\nhttps://developers.cloudflare.com/workers/static-assets/binding/#binding\n\x1B[0m"
83
- );
84
- }
85
- warned = true;
86
- return next();
87
- }
88
- try {
89
- let response = await binding.fetch(
90
- c.req.url,
91
- c.req.raw.clone()
92
- );
93
- if (response.status >= 400) {
94
- return next();
95
- }
96
- response = new Response(response.body, response);
97
- return response;
98
- } catch {
99
- return next();
100
- }
101
- });
74
+ return createMiddleware(async (c, next) => {
75
+ const binding = c.env?.ASSETS;
76
+ if (!binding) {
77
+ if (!warned) console.info("\x1B[33m\nThe binding ASSETS is not set. Falling back to Cloudflare serving.\nhttps://developers.cloudflare.com/workers/static-assets/binding/#binding\n\x1B[0m");
78
+ warned = true;
79
+ return next();
80
+ }
81
+ try {
82
+ let response = await binding.fetch(c.req.url, c.req.raw.clone());
83
+ if (response.status >= 400) return next();
84
+ response = new Response(response.body, response);
85
+ return response;
86
+ } catch {
87
+ return next();
88
+ }
89
+ });
102
90
  }
103
- export {
104
- createGetLoadContext,
105
- createHonoServer
106
- };
91
+ //#endregion
92
+ export { createGetLoadContext, createHonoServer };
@@ -0,0 +1,51 @@
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
2
+ import { Env, Hono } from "hono";
3
+ import { BlankEnv } from "hono/types";
4
+ import { ServeStaticOptions } from "hono/serve-static";
5
+
6
+ //#region src/adapters/deno.d.ts
7
+ type CustomDenoServer = Deno.ServeTcpOptions;
8
+ interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
9
+ /**
10
+ * Customize the deno server
11
+ *
12
+ * {@link https://docs.deno.com/api/deno/~/Deno.serve#parameters}
13
+ */
14
+ customDenoServer?: Partial<CustomDenoServer>;
15
+ /**
16
+ * Callback executed after server has closed and all inflight requests completed,
17
+ * before process exit. Only applicable in production mode.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * export default createHonoServer({
22
+ * onGracefulShutdown: async () => {
23
+ * await db.close();
24
+ * },
25
+ * });
26
+ * ```
27
+ */
28
+ onGracefulShutdown?: () => Promise<void> | void;
29
+ serveStaticOptions?: {
30
+ /**
31
+ * Customize the public assets (what's in your `public` directory) serve static options.
32
+ *
33
+ */
34
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
35
+ /**
36
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
37
+ *
38
+ */
39
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
40
+ };
41
+ }
42
+ type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & WithoutWebsocket<E>;
43
+ type HonoServerOptions<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
44
+ /**
45
+ * Create a Hono server
46
+ *
47
+ * @param config {@link HonoServerOptions} - The configuration options for the server
48
+ */
49
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<Hono<E>>;
50
+ //#endregion
51
+ export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -0,0 +1,106 @@
1
+ import { a as getBuildMode, o as importBuild, r as createGetLoadContext, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
2
+ import { cache } from "../middleware.js";
3
+ import { Hono } from "hono";
4
+ import { createMiddleware } from "hono/factory";
5
+ import { logger } from "hono/logger";
6
+ import { createRequestHandler } from "react-router";
7
+ import { serveStatic } from "hono/deno";
8
+ //#region src/adapters/deno.ts
9
+ let serverInstance;
10
+ let shutdownCallback;
11
+ async function createHonoServer(options) {
12
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
13
+ const mergedOptions = {
14
+ ...options,
15
+ port: options?.port || Number(Deno.env.get("PORT")) || 3e3,
16
+ defaultLogger: options?.defaultLogger ?? true
17
+ };
18
+ const mode = getBuildMode();
19
+ const PRODUCTION = mode === "production";
20
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
21
+ const app = new Hono(mergedOptions.app);
22
+ if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
+ if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
24
+ /**
25
+ * Add optional middleware that runs before any built-in middleware, including assets serving.
26
+ */
27
+ await mergedOptions.beforeAll?.(app);
28
+ /**
29
+ * Serve assets files from build/client/assets
30
+ */
31
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({
32
+ root: clientBuildPath,
33
+ ...mergedOptions.serveStaticOptions?.clientAssets
34
+ }));
35
+ /**
36
+ * Serve public files
37
+ */
38
+ app.use("*", cache(3600), serveStatic({
39
+ root: PRODUCTION ? clientBuildPath : "./public",
40
+ ...mergedOptions.serveStaticOptions?.publicAssets
41
+ }));
42
+ /**
43
+ * Add logger middleware
44
+ */
45
+ if (mergedOptions.defaultLogger) app.use("*", logger());
46
+ /**
47
+ * Add optional middleware
48
+ */
49
+ await mergedOptions.configure?.(app);
50
+ /**
51
+ * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
52
+ */
53
+ const reactRouterApp = new Hono({ strict: false });
54
+ reactRouterApp.use(async (c, next) => {
55
+ const build = await importBuild();
56
+ return createMiddleware(async (c) => {
57
+ const requestHandler = createRequestHandler(build, mode);
58
+ const loadContext = mergedOptions.getLoadContext?.(c, {
59
+ build,
60
+ mode
61
+ });
62
+ return requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
63
+ })(c, next);
64
+ });
65
+ app.route(`${basename}`, reactRouterApp);
66
+ if (basename) app.route(`${basename}.data`, reactRouterApp);
67
+ const server = {
68
+ ...mergedOptions.customDenoServer,
69
+ port: mergedOptions.port
70
+ };
71
+ if (PRODUCTION) {
72
+ serverInstance = Deno.serve(server, app.fetch);
73
+ if (shutdownCallback != null) {
74
+ let gracefulShutdownInProgress = false;
75
+ const gracefulShutdown = async (signal) => {
76
+ if (gracefulShutdownInProgress) return;
77
+ gracefulShutdownInProgress = true;
78
+ try {
79
+ if (serverInstance != null) {
80
+ console.log(`\n๐Ÿ“ก Received ${signal}, shutting down gracefully...`);
81
+ await serverInstance.shutdown();
82
+ console.log("Server stopped, all requests completed");
83
+ if (shutdownCallback) {
84
+ console.log("๐Ÿงน Running cleanup tasks...");
85
+ await shutdownCallback();
86
+ console.log("โœ… Cleanup complete!");
87
+ }
88
+ console.log("๐Ÿ‘‹ Graceful shutdown completed");
89
+ }
90
+ process.exit(0);
91
+ } catch (error) {
92
+ console.error("โŒ Shutdown failed:", error);
93
+ process.exit(1);
94
+ }
95
+ };
96
+ if (serverInstance != null) {
97
+ process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
98
+ process.on("SIGINT", () => gracefulShutdown("SIGINT"));
99
+ console.log("โœ… Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
100
+ }
101
+ }
102
+ }
103
+ return app;
104
+ }
105
+ //#endregion
106
+ export { createGetLoadContext, createHonoServer };