react-router-hono-server 3.0.0-next.0 → 4.0.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,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,10 @@
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";
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
- import { serveStatic } from "hono/bun";
8
8
  //#region src/adapters/bun.ts
9
9
  let serverInstance;
10
10
  let shutdownCallback;
@@ -33,17 +33,20 @@ async function createHonoServer(options) {
33
33
  /**
34
34
  * Serve assets files from build/client/assets
35
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
- }));
36
+ if (!isReactRouterBuildRequest()) {
37
+ const { serveStatic } = await import("hono/bun");
38
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
39
+ root: clientBuildPath,
40
+ ...mergedOptions.serveStaticOptions?.clientAssets
41
+ }));
42
+ /**
43
+ * Serve public files
44
+ */
45
+ app.use("*", cache(3600), serveStatic({
46
+ root: PRODUCTION ? clientBuildPath : "./public",
47
+ ...mergedOptions.serveStaticOptions?.publicAssets
48
+ }));
49
+ }
47
50
  /**
48
51
  * Add logger middleware
49
52
  */
@@ -53,6 +56,7 @@ async function createHonoServer(options) {
53
56
  */
54
57
  if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
55
58
  else await mergedOptions.configure?.(app);
59
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
56
60
  /**
57
61
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
58
62
  */
@@ -75,7 +79,7 @@ async function createHonoServer(options) {
75
79
  port: mergedOptions.port,
76
80
  development: !PRODUCTION
77
81
  };
78
- if (PRODUCTION) {
82
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
79
83
  server = injectWebSocket(server);
80
84
  serverInstance = Bun.serve(server);
81
85
  if (shutdownCallback != null) {
@@ -102,17 +106,13 @@ async function createHonoServer(options) {
102
106
  }
103
107
  };
104
108
  if (serverInstance != null) {
105
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
106
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
109
+ process.on("SIGTERM", () => void gracefulShutdown("SIGTERM"));
110
+ process.on("SIGINT", () => void gracefulShutdown("SIGINT"));
107
111
  console.log("✅ Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
108
112
  }
109
113
  }
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");
114
+ } else if (mergedOptions.useWebSocket) {
115
+ if (attachWebSocketToVite(injectWebSocket)) console.log("🚧 Running in development mode");
116
116
  }
117
117
  return server;
118
118
  }
@@ -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,10 @@
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
3
  import { Hono } from "hono";
4
4
  import { createMiddleware } from "hono/factory";
5
5
  import { logger } from "hono/logger";
6
6
  import { createRequestHandler } from "react-router";
7
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
- */
13
8
  async function createHonoServer(options) {
14
9
  const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
15
10
  const mergedOptions = {
@@ -19,6 +14,10 @@ async function createHonoServer(options) {
19
14
  const mode = getBuildMode();
20
15
  const PRODUCTION = mode === "production";
21
16
  const app = new Hono(mergedOptions.app);
17
+ const { upgradeWebSocket } = await createWebSocket({
18
+ app,
19
+ enabled: mergedOptions.useWebSocket ?? false
20
+ });
22
21
  /**
23
22
  * Add optional middleware that runs before any built-in middleware, including assets serving.
24
23
  */
@@ -26,7 +25,7 @@ async function createHonoServer(options) {
26
25
  /**
27
26
  * Serve assets files from build/client/assets
28
27
  */
29
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveCloudflareAssets());
28
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveCloudflareAssets());
30
29
  /**
31
30
  * Serve public files
32
31
  */
@@ -43,7 +42,9 @@ async function createHonoServer(options) {
43
42
  /**
44
43
  * Add optional middleware
45
44
  */
46
- await mergedOptions.configure?.(app);
45
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
46
+ else await mergedOptions.configure?.(app);
47
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
47
48
  /**
48
49
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
49
50
  */
@@ -78,14 +79,15 @@ function serveCloudflareAssets() {
78
79
  warned = true;
79
80
  return next();
80
81
  }
82
+ let response;
81
83
  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;
84
+ response = await binding.fetch(c.req.url, c.req.raw.clone());
86
85
  } catch {
87
86
  return next();
88
87
  }
88
+ if (response.status >= 400) return next();
89
+ response = new Response(response.body, response);
90
+ return response;
89
91
  });
90
92
  }
91
93
  //#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,29 @@
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";
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
- import { serveStatic } from "hono/deno";
8
8
  //#region src/adapters/deno.ts
9
9
  let serverInstance;
10
10
  let shutdownCallback;
11
11
  async function createHonoServer(options) {
12
12
  const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
13
+ const envPort = typeof Deno === "undefined" ? void 0 : Deno.env.get("PORT");
13
14
  const mergedOptions = {
14
15
  ...options,
15
- port: options?.port || Number(Deno.env.get("PORT")) || 3e3,
16
+ port: options?.port || Number(envPort) || 3e3,
16
17
  defaultLogger: options?.defaultLogger ?? true
17
18
  };
18
19
  const mode = getBuildMode();
19
20
  const PRODUCTION = mode === "production";
20
21
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
21
22
  const app = new Hono(mergedOptions.app);
23
+ const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
24
+ app,
25
+ enabled: mergedOptions.useWebSocket ?? false
26
+ });
22
27
  if (PRODUCTION && mergedOptions.onGracefulShutdown) shutdownCallback = mergedOptions.onGracefulShutdown;
23
28
  if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
24
29
  /**
@@ -28,17 +33,20 @@ async function createHonoServer(options) {
28
33
  /**
29
34
  * Serve assets files from build/client/assets
30
35
  */
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
- }));
36
+ if (!isReactRouterBuildRequest()) {
37
+ const { serveStatic } = await import("hono/deno");
38
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
39
+ root: clientBuildPath,
40
+ ...mergedOptions.serveStaticOptions?.clientAssets
41
+ }));
42
+ /**
43
+ * Serve public files
44
+ */
45
+ app.use("*", cache(3600), serveStatic({
46
+ root: PRODUCTION ? clientBuildPath : "./public",
47
+ ...mergedOptions.serveStaticOptions?.publicAssets
48
+ }));
49
+ }
42
50
  /**
43
51
  * Add logger middleware
44
52
  */
@@ -46,7 +54,9 @@ async function createHonoServer(options) {
46
54
  /**
47
55
  * Add optional middleware
48
56
  */
49
- await mergedOptions.configure?.(app);
57
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
58
+ else await mergedOptions.configure?.(app);
59
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
50
60
  /**
51
61
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
52
62
  */
@@ -68,7 +78,7 @@ async function createHonoServer(options) {
68
78
  ...mergedOptions.customDenoServer,
69
79
  port: mergedOptions.port
70
80
  };
71
- if (PRODUCTION) {
81
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
72
82
  serverInstance = Deno.serve(server, app.fetch);
73
83
  if (shutdownCallback != null) {
74
84
  let gracefulShutdownInProgress = false;
@@ -94,11 +104,13 @@ async function createHonoServer(options) {
94
104
  }
95
105
  };
96
106
  if (serverInstance != null) {
97
- process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
98
- process.on("SIGINT", () => gracefulShutdown("SIGINT"));
107
+ process.on("SIGTERM", () => void gracefulShutdown("SIGTERM"));
108
+ process.on("SIGINT", () => void gracefulShutdown("SIGINT"));
99
109
  console.log("✅ Graceful shutdown enabled. Press Ctrl+C to shutdown gracefully.");
100
110
  }
101
111
  }
112
+ } else if (mergedOptions.useWebSocket) {
113
+ if (attachWebSocketToVite(injectWebSocket)) console.log("🚧 Running in development mode");
102
114
  }
103
115
  return app;
104
116
  }
@@ -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,6 @@
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";
3
4
  import { serve } from "@hono/node-server";
4
5
  import { serveStatic } from "@hono/node-server/serve-static";
5
6
  import { Hono } from "hono";
@@ -27,7 +28,7 @@ async function createHonoServer(options) {
27
28
  const PRODUCTION = mode === "production";
28
29
  const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
29
30
  const app = new Hono(mergedOptions.app);
30
- const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
31
+ const { upgradeWebSocket, injectWebSocket, nodeWebSocket } = await createWebSocket({
31
32
  app,
32
33
  enabled: mergedOptions.useWebSocket ?? false
33
34
  });
@@ -39,7 +40,7 @@ async function createHonoServer(options) {
39
40
  /**
40
41
  * Serve assets files from build/client/assets
41
42
  */
42
- app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({
43
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(31536e3), serveStatic({
43
44
  root: clientBuildPath,
44
45
  ...mergedOptions.serveStaticOptions?.clientAssets
45
46
  }));
@@ -57,8 +58,14 @@ async function createHonoServer(options) {
57
58
  /**
58
59
  * Add optional middleware
59
60
  */
60
- if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
61
- else await mergedOptions.configure?.(app);
61
+ if (mergedOptions.useWebSocket) {
62
+ if (!nodeWebSocket) throw new Error("Node WebSocket server was not created while WebSockets are enabled");
63
+ await mergedOptions.configure(app, {
64
+ upgradeWebSocket,
65
+ wss: nodeWebSocket.server
66
+ });
67
+ } else await mergedOptions.configure?.(app);
68
+ if (!PRODUCTION) handleChromeDevToolsWorkspaceRequest(app);
62
69
  /**
63
70
  * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
64
71
  */
@@ -78,24 +85,17 @@ async function createHonoServer(options) {
78
85
  /**
79
86
  * Start the production server
80
87
  */
81
- if (PRODUCTION) {
88
+ if (PRODUCTION && !isReactRouterBuildRequest()) {
82
89
  const server = serve({
83
90
  ...app,
84
91
  ...mergedOptions.customNodeServer,
85
92
  port: mergedOptions.port,
86
93
  overrideGlobalObjects: mergedOptions.overrideGlobalObjects,
87
- hostname: mergedOptions.hostname
94
+ hostname: mergedOptions.hostname,
95
+ websocket: nodeWebSocket
88
96
  }, mergedOptions.listeningListener);
89
97
  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
- }
98
+ } else if (attachWebSocketToVite(injectWebSocket, mergedOptions.onServe)) console.log("🚧 Dev server started");
99
99
  return app;
100
100
  }
101
101
  //#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 };