react-router-hono-server 3.0.0 → 4.0.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.
@@ -1,11 +1,10 @@
1
- import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-VHYCCAPs.js";
2
2
  import { Env, Hono } from "hono";
3
3
  import { LambdaContext, LambdaEvent } from "hono/aws-lambda";
4
4
  import { BlankEnv } from "hono/types";
5
-
6
5
  //#region src/adapters/aws-lambda.d.ts
7
6
  type InvokeMode = "default" | "stream";
8
- type AWSLambdaHandler = (event: LambdaEvent, lambdaContext: LambdaContext, callback: (error?: Error | string | null, result?: unknown) => void) => unknown | Promise<unknown>;
7
+ type AWSLambdaHandler = (event: LambdaEvent, lambdaContext: LambdaContext, callback: (error?: Error | string | null, result?: unknown) => void) => unknown;
9
8
  interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
10
9
  /**
11
10
  * The invoke mode to use
@@ -1,5 +1,6 @@
1
- import { a as getBuildMode, o as importBuild, r as createGetLoadContext, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
1
+ import { a as getBuildMode, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
3
4
  import { serveStatic } from "@hono/node-server/serve-static";
4
5
  import { Hono } from "hono";
5
6
  import { createMiddleware } from "hono/factory";
@@ -35,7 +36,7 @@ async function createHonoServer(options) {
35
36
  /**
36
37
  * Serve assets files from build/client/assets
37
38
  */
38
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({ root: clientBuildPath }));
39
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({ root: clientBuildPath }));
39
40
  /**
40
41
  * Serve public files
41
42
  */
@@ -49,6 +50,7 @@ async function createHonoServer(options) {
49
50
  * Add optional middleware
50
51
  */
51
52
  await mergedOptions.configure?.(app);
53
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
52
54
  /**
53
55
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
54
56
  */
@@ -69,7 +71,7 @@ async function createHonoServer(options) {
69
71
  /**
70
72
  * Wrap the app in the appropriate AWS handler
71
73
  */
72
- if (PRODUCTION) {
74
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
73
75
  if (mergedOptions.invokeMode === "stream") return streamHandle(app);
74
76
  return handle(app);
75
77
  }
@@ -1,8 +1,7 @@
1
- import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-VHYCCAPs.js";
2
2
  import { Env } from "hono";
3
3
  import { BlankEnv } from "hono/types";
4
4
  import { ServeStaticOptions } from "hono/serve-static";
5
-
6
5
  //#region src/adapters/bun.d.ts
7
6
  type CustomBunServer = Bun.Serve.Options<unknown, string>;
8
7
  interface HonoBunServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
@@ -1,10 +1,11 @@
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";
1
+ import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
3
5
  import { Hono } from "hono";
4
6
  import { createMiddleware } from "hono/factory";
5
7
  import { logger } from "hono/logger";
6
8
  import { createRequestHandler } from "react-router";
7
- import { serveStatic } from "hono/bun";
8
9
  //#region src/adapters/bun.ts
9
10
  let serverInstance;
10
11
  let shutdownCallback;
@@ -19,6 +20,7 @@ async function createHonoServer(options) {
19
20
  const mode = getBuildMode();
20
21
  const PRODUCTION = mode === "production";
21
22
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
23
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
22
24
  if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
25
  const app = new Hono(mergedOptions.app);
24
26
  const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
@@ -33,17 +35,22 @@ async function createHonoServer(options) {
33
35
  /**
34
36
  * Serve assets files from build/client/assets
35
37
  */
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
- }));
38
+ if (!isReactRouterBuildRequest()) {
39
+ const { serveStatic } = await import("hono/bun");
40
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
41
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
42
+ root: clientBuildPath,
43
+ ...mergedOptions.serveStaticOptions?.clientAssets,
44
+ rewriteRequestPath: rewriteClientAssetsPath
45
+ }));
46
+ /**
47
+ * Serve public files
48
+ */
49
+ app.use("*", cache(3600), serveStatic({
50
+ root: PRODUCTION ? clientBuildPath : "./public",
51
+ ...mergedOptions.serveStaticOptions?.publicAssets
52
+ }));
53
+ }
47
54
  /**
48
55
  * Add logger middleware
49
56
  */
@@ -53,6 +60,7 @@ async function createHonoServer(options) {
53
60
  */
54
61
  if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
55
62
  else await mergedOptions.configure?.(app);
63
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
56
64
  /**
57
65
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
58
66
  */
@@ -75,7 +83,7 @@ async function createHonoServer(options) {
75
83
  port: mergedOptions.port,
76
84
  development: !PRODUCTION
77
85
  };
78
- if (PRODUCTION) {
86
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
79
87
  server = injectWebSocket(server);
80
88
  serverInstance = Bun.serve(server);
81
89
  if (shutdownCallback != null) {
@@ -102,17 +110,13 @@ async function createHonoServer(options) {
102
110
  }
103
111
  };
104
112
  if (serverInstance != null) {
105
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
106
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
113
+ process.on("SIGTERM", () => void gracefulShutdown("SIGTERM"));
114
+ process.on("SIGINT", () => void gracefulShutdown("SIGINT"));
107
115
  console.log("✅ Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
108
116
  }
109
117
  }
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");
118
+ } else if (mergedOptions.useWebSocket) {
119
+ if (attachWebSocketToVite(injectWebSocket)) console.log("🚧 Running in development mode");
116
120
  }
117
121
  return server;
118
122
  }
@@ -1,15 +1,19 @@
1
- import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-VHYCCAPs.js";
2
2
  import { Env, Hono } from "hono";
3
3
  import { BlankEnv } from "hono/types";
4
-
4
+ import { upgradeWebSocket } from "hono/cloudflare-workers";
5
5
  //#region src/adapters/cloudflare.d.ts
6
6
  interface HonoCloudflareOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {}
7
- type HonoServerOptions<E extends Env = BlankEnv> = HonoCloudflareOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
7
+ type CloudflareUpgradeWebSocket = typeof upgradeWebSocket;
8
+ type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoCloudflareOptions<E> & WithWebsocket<E, CloudflareUpgradeWebSocket>;
9
+ type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoCloudflareOptions<E> & WithoutWebsocket<E>;
10
+ type HonoServerOptions<E extends Env = BlankEnv> = HonoServerOptionsWithWebSocket<E> | HonoServerOptionsWithoutWebSocket<E>;
8
11
  /**
9
12
  * Create a Hono server
10
13
  *
11
14
  * @param config {@link HonoServerOptions} - The configuration options for the server
12
15
  */
13
- declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, import("hono/types").BlankSchema, "/">>;
16
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<Hono<E>>;
17
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithWebSocket<E>): Promise<Hono<E>>;
14
18
  //#endregion
15
19
  export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -1,15 +1,11 @@
1
- import { a as getBuildMode, o as importBuild, r as createGetLoadContext, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
1
+ import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { i as viteGeneratedAssetsRoute, r as stripVitePathnamePrefix, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
3
4
  import { Hono } from "hono";
4
5
  import { createMiddleware } from "hono/factory";
5
6
  import { logger } from "hono/logger";
6
7
  import { createRequestHandler } from "react-router";
7
8
  //#region src/adapters/cloudflare.ts
8
- /**
9
- * Create a Hono server
10
- *
11
- * @param config {@link HonoServerOptions} - The configuration options for the server
12
- */
13
9
  async function createHonoServer(options) {
14
10
  const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
15
11
  const mergedOptions = {
@@ -18,7 +14,12 @@ async function createHonoServer(options) {
18
14
  };
19
15
  const mode = getBuildMode();
20
16
  const PRODUCTION = mode === "production";
17
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
21
18
  const app = new Hono(mergedOptions.app);
19
+ const { upgradeWebSocket } = await createWebSocket({
20
+ app,
21
+ enabled: mergedOptions.useWebSocket ?? false
22
+ });
22
23
  /**
23
24
  * Add optional middleware that runs before any built-in middleware, including assets serving.
24
25
  */
@@ -26,7 +27,7 @@ async function createHonoServer(options) {
26
27
  /**
27
28
  * Serve assets files from build/client/assets
28
29
  */
29
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveCloudflareAssets());
30
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveCloudflareAssets(vitePublicPath));
30
31
  /**
31
32
  * Serve public files
32
33
  */
@@ -43,7 +44,9 @@ async function createHonoServer(options) {
43
44
  /**
44
45
  * Add optional middleware
45
46
  */
46
- await mergedOptions.configure?.(app);
47
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
48
+ else await mergedOptions.configure?.(app);
49
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
47
50
  /**
48
51
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
49
52
  */
@@ -70,7 +73,7 @@ let warned = false;
70
73
  *
71
74
  * https://github.com/sergiodxa/remix-hono/blob/main/src/cloudflare.ts
72
75
  */
73
- function serveCloudflareAssets() {
76
+ function serveCloudflareAssets(vitePublicPath) {
74
77
  return createMiddleware(async (c, next) => {
75
78
  const binding = c.env?.ASSETS;
76
79
  if (!binding) {
@@ -78,14 +81,17 @@ function serveCloudflareAssets() {
78
81
  warned = true;
79
82
  return next();
80
83
  }
84
+ let response;
81
85
  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
+ const url = new URL(c.req.url);
87
+ if (vitePublicPath) url.pathname = stripVitePathnamePrefix(url.pathname, vitePublicPath);
88
+ response = await binding.fetch(url.toString(), c.req.raw.clone());
86
89
  } catch {
87
90
  return next();
88
91
  }
92
+ if (response.status >= 400) return next();
93
+ response = new Response(response.body, response);
94
+ return response;
89
95
  });
90
96
  }
91
97
  //#endregion
@@ -1,8 +1,7 @@
1
- import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-VHYCCAPs.js";
2
2
  import { Env, Hono } from "hono";
3
3
  import { BlankEnv } from "hono/types";
4
4
  import { ServeStaticOptions } from "hono/serve-static";
5
-
6
5
  //#region src/adapters/deno.d.ts
7
6
  type CustomDenoServer = Deno.ServeTcpOptions;
8
7
  interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
@@ -40,12 +39,14 @@ interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOpti
40
39
  };
41
40
  }
42
41
  type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & WithoutWebsocket<E>;
43
- type HonoServerOptions<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
42
+ type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoDenoServerOptions<E> & WithWebsocket<E>;
43
+ type HonoServerOptions<E extends Env = BlankEnv> = HonoServerOptionsWithWebSocket<E> | HonoServerOptionsWithoutWebSocket<E>;
44
44
  /**
45
45
  * Create a Hono server
46
46
  *
47
47
  * @param config {@link HonoServerOptions} - The configuration options for the server
48
48
  */
49
49
  declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<Hono<E>>;
50
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithWebSocket<E>): Promise<Hono<E>>;
50
51
  //#endregion
51
52
  export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -1,24 +1,31 @@
1
- import { a as getBuildMode, o as importBuild, r as createGetLoadContext, t as bindIncomingRequestSocketInfo } from "../helpers-DpeZU9Bf.js";
1
+ import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
3
5
  import { Hono } from "hono";
4
6
  import { createMiddleware } from "hono/factory";
5
7
  import { logger } from "hono/logger";
6
8
  import { createRequestHandler } from "react-router";
7
- import { serveStatic } from "hono/deno";
8
9
  //#region src/adapters/deno.ts
9
10
  let serverInstance;
10
11
  let shutdownCallback;
11
12
  async function createHonoServer(options) {
12
13
  const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
14
+ const envPort = typeof Deno === "undefined" ? void 0 : Deno.env.get("PORT");
13
15
  const mergedOptions = {
14
16
  ...options,
15
- port: options?.port || Number(Deno.env.get("PORT")) || 3e3,
17
+ port: options?.port || Number(envPort) || 3e3,
16
18
  defaultLogger: options?.defaultLogger ?? true
17
19
  };
18
20
  const mode = getBuildMode();
19
21
  const PRODUCTION = mode === "production";
20
22
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
23
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
21
24
  const app = new Hono(mergedOptions.app);
25
+ const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
26
+ app,
27
+ enabled: mergedOptions.useWebSocket ?? false
28
+ });
22
29
  if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
30
  if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
24
31
  /**
@@ -28,17 +35,22 @@ async function createHonoServer(options) {
28
35
  /**
29
36
  * Serve assets files from build/client/assets
30
37
  */
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
- }));
38
+ if (!isReactRouterBuildRequest()) {
39
+ const { serveStatic } = await import("hono/deno");
40
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
41
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
42
+ root: clientBuildPath,
43
+ ...mergedOptions.serveStaticOptions?.clientAssets,
44
+ rewriteRequestPath: rewriteClientAssetsPath
45
+ }));
46
+ /**
47
+ * Serve public files
48
+ */
49
+ app.use("*", cache(3600), serveStatic({
50
+ root: PRODUCTION ? clientBuildPath : "./public",
51
+ ...mergedOptions.serveStaticOptions?.publicAssets
52
+ }));
53
+ }
42
54
  /**
43
55
  * Add logger middleware
44
56
  */
@@ -46,7 +58,9 @@ async function createHonoServer(options) {
46
58
  /**
47
59
  * Add optional middleware
48
60
  */
49
- await mergedOptions.configure?.(app);
61
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
62
+ else await mergedOptions.configure?.(app);
63
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
50
64
  /**
51
65
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
52
66
  */
@@ -68,7 +82,7 @@ async function createHonoServer(options) {
68
82
  ...mergedOptions.customDenoServer,
69
83
  port: mergedOptions.port
70
84
  };
71
- if (PRODUCTION) {
85
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
72
86
  serverInstance = Deno.serve(server, app.fetch);
73
87
  if (shutdownCallback != null) {
74
88
  let gracefulShutdownInProgress = false;
@@ -94,11 +108,13 @@ async function createHonoServer(options) {
94
108
  }
95
109
  };
96
110
  if (serverInstance != null) {
97
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
98
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
111
+ process.on("SIGTERM", () => void gracefulShutdown("SIGTERM"));
112
+ process.on("SIGINT", () => void gracefulShutdown("SIGINT"));
99
113
  console.log("✅ Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
100
114
  }
101
115
  }
116
+ } else if (mergedOptions.useWebSocket) {
117
+ if (attachWebSocketToVite(injectWebSocket)) console.log("🚧 Running in development mode");
102
118
  }
103
119
  return app;
104
120
  }
@@ -1,13 +1,14 @@
1
- import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-VHYCCAPs.js";
2
2
  import { ServerType } from "@hono/node-server";
3
3
  import { ServeStaticOptions } from "@hono/node-server/serve-static";
4
4
  import { Env, Hono } from "hono";
5
5
  import { BlankEnv } from "hono/types";
6
+ import { UpgradeWebSocket } from "hono/ws";
6
7
  import { ServerOptions, createServer } from "node:http";
7
8
  import { SecureServerOptions, ServerOptions as ServerOptions$1, createSecureServer, createServer as createServer$1 } from "node:http2";
9
+ import { WebSocketServer } from "ws";
8
10
  import { AddressInfo } from "node:net";
9
11
  import { ServerOptions as ServerOptions$2, createServer as createServer$2 } from "node:https";
10
-
11
12
  //#region src/types/node.https.d.ts
12
13
  type createHttpOptions = {
13
14
  serverOptions?: ServerOptions;
@@ -79,7 +80,18 @@ interface HonoNodeServerOptions<E extends Env = BlankEnv> extends HonoServerOpti
79
80
  clientAssets?: Omit<ServeStaticOptions<E>, "root">;
80
81
  };
81
82
  }
82
- type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoNodeServerOptions<E> & WithWebsocket<E>;
83
+ type WithNodeWebsocket<E extends Env> = Omit<WithWebsocket<E>, "configure"> & {
84
+ /**
85
+ * Customize the Hono server and access the underlying Node WebSocket server.
86
+ *
87
+ * It is applied after the default middleware and before the React Router middleware.
88
+ */
89
+ configure: (app: Hono<E>, options: {
90
+ upgradeWebSocket: UpgradeWebSocket;
91
+ wss: WebSocketServer;
92
+ }) => Promise<void> | void;
93
+ };
94
+ type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoNodeServerOptions<E> & WithNodeWebsocket<E>;
83
95
  type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoNodeServerOptions<E> & WithoutWebsocket<E>;
84
96
  type HonoServerOptions<E extends Env = BlankEnv> = HonoServerOptionsWithWebSocket<E> | HonoServerOptionsWithoutWebSocket<E>;
85
97
  /**
@@ -1,5 +1,7 @@
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";
1
+ import { a as getBuildMode, i as createWebSocket, n as bindIncomingRequestSocketInfo, o as handleChromeDevToolsWorkspaceRequest, r as createGetLoadContext, s as importBuild, t as attachWebSocketToVite } from "../helpers-D9Y2AQRk.js";
2
2
  import { cache } from "../middleware.js";
3
+ import { t as isReactRouterBuildRequest } from "../react-router-build-request-_jlIVza5.js";
4
+ import { i as viteGeneratedAssetsRoute, n as composeViteAssetRewriteRequestPath, t as classifyVitePublicPath } from "../vite-public-path-Aqd7hZ0d.js";
3
5
  import { serve } from "@hono/node-server";
4
6
  import { serveStatic } from "@hono/node-server/serve-static";
5
7
  import { Hono } from "hono";
@@ -26,8 +28,9 @@ async function createHonoServer(options) {
26
28
  const mode = getBuildMode();
27
29
  const PRODUCTION = mode === "production";
28
30
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
31
+ const vitePublicPath = classifyVitePublicPath(import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE);
29
32
  const app = new Hono(mergedOptions.app);
30
- const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
33
+ const { upgradeWebSocket, injectWebSocket, nodeWebSocket } = await createWebSocket({
31
34
  app,
32
35
  enabled: mergedOptions.useWebSocket ?? false
33
36
  });
@@ -39,9 +42,11 @@ async function createHonoServer(options) {
39
42
  /**
40
43
  * Serve assets files from build/client/assets
41
44
  */
42
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({
45
+ const rewriteClientAssetsPath = composeViteAssetRewriteRequestPath(vitePublicPath, mergedOptions.serveStaticOptions?.clientAssets?.rewriteRequestPath);
46
+ app.use(viteGeneratedAssetsRoute(vitePublicPath, import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR), cache(31536e3), serveStatic({
43
47
  root: clientBuildPath,
44
- ...mergedOptions.serveStaticOptions?.clientAssets
48
+ ...mergedOptions.serveStaticOptions?.clientAssets,
49
+ rewriteRequestPath: rewriteClientAssetsPath
45
50
  }));
46
51
  /**
47
52
  * Serve public files
@@ -57,8 +62,14 @@ async function createHonoServer(options) {
57
62
  /**
58
63
  * Add optional middleware
59
64
  */
60
- if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
61
- else await mergedOptions.configure?.(app);
65
+ if (mergedOptions.useWebSocket) {
66
+ if (!nodeWebSocket) throw new Error("Node WebSocket server was not created while WebSockets are enabled");
67
+ await mergedOptions.configure(app, {
68
+ upgradeWebSocket,
69
+ wss: nodeWebSocket.server
70
+ });
71
+ } else await mergedOptions.configure?.(app);
72
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
62
73
  /**
63
74
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
64
75
  */
@@ -78,24 +89,17 @@ async function createHonoServer(options) {
78
89
  /**
79
90
  * Start the production server
80
91
  */
81
- if (PRODUCTION) {
92
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
82
93
  const server = serve({
83
94
  ...app,
84
95
  ...mergedOptions.customNodeServer,
85
96
  port: mergedOptions.port,
86
97
  overrideGlobalObjects: mergedOptions.overrideGlobalObjects,
87
- hostname: mergedOptions.hostname
98
+ hostname: mergedOptions.hostname,
99
+ websocket: nodeWebSocket
88
100
  }, mergedOptions.listeningListener);
89
101
  mergedOptions.onServe?.(server);
90
- injectWebSocket(server);
91
- } else if (globalThis.__viteDevServer?.httpServer) {
92
- const httpServer = globalThis.__viteDevServer.httpServer;
93
- cleanUpgradeListeners(httpServer);
94
- mergedOptions.onServe?.(httpServer);
95
- injectWebSocket(httpServer);
96
- patchUpgradeListener(httpServer);
97
- console.log("🚧 Dev server started");
98
- }
102
+ } else if (attachWebSocketToVite(injectWebSocket, mergedOptions.onServe)) console.log("🚧 Dev server started");
99
103
  return app;
100
104
  }
101
105
  //#endregion
package/dist/cli.d.ts CHANGED
@@ -1 +1 @@
1
- export { };
1
+ export {}
package/dist/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
+ import { command, run } from "@drizzle-team/brocli";
2
3
  import fs from "node:fs";
3
4
  import path from "node:path";
4
- import { command, run } from "@drizzle-team/brocli";
5
5
  //#region src/cli.ts
6
6
  const runtimeRegex = /runtime:\s*(['"])([^'"]*)\1/;
7
7
  function guessRuntime() {
@@ -11,28 +11,32 @@ function guessRuntime() {
11
11
  return "node";
12
12
  }
13
13
  }
14
+ const runtime = guessRuntime();
14
15
  const serverFile = `// generated by react-router-hono-server/dev
15
- import { createHonoServer } from "react-router-hono-server/${guessRuntime()}";
16
+ import { createHonoServer } from "react-router-hono-server/${runtime === "aws" ? "aws-lambda" : runtime}";
16
17
 
17
18
  export default await createHonoServer();`;
18
- run([command({
19
+ const file = command({
20
+ name: "file",
21
+ handler() {
22
+ const serverFilePath = path.resolve(process.cwd(), "app", "server.ts");
23
+ fs.writeFileSync(serverFilePath, serverFile);
24
+ console.log(`\x1b[33m✨ File created at ${serverFilePath}\x1b[0m`);
25
+ }
26
+ });
27
+ const folder = command({
28
+ name: "folder",
29
+ handler() {
30
+ const serverFilePath = path.resolve(process.cwd(), "app", "server", "index.ts");
31
+ if (!fs.existsSync(serverFilePath)) fs.mkdirSync(path.dirname(serverFilePath), { recursive: true });
32
+ fs.writeFileSync(serverFilePath, serverFile);
33
+ console.log(`\x1b[33m✨ Folder and file created at ${serverFilePath}\x1b[0m`);
34
+ }
35
+ });
36
+ const reveal = command({
19
37
  name: "reveal",
20
- subcommands: [command({
21
- name: "file",
22
- handler() {
23
- const serverFilePath = path.resolve(process.cwd(), "app", "server.ts");
24
- fs.writeFileSync(serverFilePath, serverFile);
25
- console.log(`\x1b[33m✨ File created at ${serverFilePath}\x1b[0m`);
26
- }
27
- }), command({
28
- name: "folder",
29
- handler() {
30
- const serverFilePath = path.resolve(process.cwd(), "app", "server", "index.ts");
31
- if (!fs.existsSync(serverFilePath)) fs.mkdirSync(path.dirname(serverFilePath), { recursive: true });
32
- fs.writeFileSync(serverFilePath, serverFile);
33
- console.log(`\x1b[33m✨ Folder and file created at ${serverFilePath}\x1b[0m`);
34
- }
35
- })]
36
- })], { name: "React Router Hono Server CLI" });
38
+ subcommands: [file, folder]
39
+ });
40
+ run([reveal], { name: "React Router Hono Server CLI" });
37
41
  //#endregion
38
42
  export {};
package/dist/dev.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { DevServerOptions } from "@hono/vite-dev-server";
2
2
  import { Plugin } from "vite";
3
-
4
3
  //#region src/types/runtime.d.ts
5
4
  type Runtime = "node" | "bun" | "cloudflare" | "aws" | "deno";
6
5
  //#endregion
@@ -44,18 +43,7 @@ type ReactRouterHonoServerPluginOptionsDefault = ReactRouterHonoServerPluginOpti
44
43
  */
45
44
  runtime?: Runtime;
46
45
  };
47
- type ReactRouterHonoServerPluginOptionsCloudflare = ReactRouterHonoServerPluginOptionsBase & {
48
- /**
49
- * The runtime to use for the server.
50
- *
51
- * Defaults to `node`.
52
- */
53
- runtime: Extract<Runtime, "cloudflare">;
54
- flag?: {
55
- force_react_19?: boolean;
56
- };
57
- };
58
- type ReactRouterHonoServerPluginOptions = ReactRouterHonoServerPluginOptionsDefault | ReactRouterHonoServerPluginOptionsCloudflare;
46
+ type ReactRouterHonoServerPluginOptions = ReactRouterHonoServerPluginOptionsDefault;
59
47
  declare function reactRouterHonoServer(options?: ReactRouterHonoServerPluginOptions): Plugin;
60
48
  //#endregion
61
49
  export { reactRouterHonoServer };