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,88 +1,83 @@
1
- import { AddressInfo } from 'node:net';
2
- import { ServerType } from '@hono/node-server';
3
- import { ServeStaticOptions } from '@hono/node-server/serve-static';
4
- import { Env, Hono } from 'hono';
5
- import { BlankEnv } from 'hono/types';
6
- import { H as HonoServerOptionsBase, a as WithWebsocket, W as WithoutWebsocket } from '../helpers-BXdVojwF.js';
7
- export { c as createGetLoadContext } from '../helpers-BXdVojwF.js';
8
- import { ServerOptions, createServer } from 'node:http';
9
- import { ServerOptions as ServerOptions$2, createServer as createServer$2, SecureServerOptions, createSecureServer } from 'node:http2';
10
- import { ServerOptions as ServerOptions$1, createServer as createServer$1 } from 'node:https';
11
- import 'hono/ws';
12
- import 'react-router';
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
2
+ import { ServerType } from "@hono/node-server";
3
+ import { ServeStaticOptions } from "@hono/node-server/serve-static";
4
+ import { Env, Hono } from "hono";
5
+ import { BlankEnv } from "hono/types";
6
+ import { ServerOptions, createServer } from "node:http";
7
+ import { SecureServerOptions, ServerOptions as ServerOptions$1, createSecureServer, createServer as createServer$1 } from "node:http2";
8
+ import { AddressInfo } from "node:net";
9
+ import { ServerOptions as ServerOptions$2, createServer as createServer$2 } from "node:https";
13
10
 
11
+ //#region src/types/node.https.d.ts
14
12
  type createHttpOptions = {
15
13
  serverOptions?: ServerOptions;
16
14
  createServer?: typeof createServer;
17
15
  };
18
16
  type createHttpsOptions = {
19
- serverOptions?: ServerOptions$1;
20
- createServer?: typeof createServer$1;
21
- };
22
- type createHttp2Options = {
23
17
  serverOptions?: ServerOptions$2;
24
18
  createServer?: typeof createServer$2;
25
19
  };
20
+ type createHttp2Options = {
21
+ serverOptions?: ServerOptions$1;
22
+ createServer?: typeof createServer$1;
23
+ };
26
24
  type createSecureHttp2Options = {
27
25
  serverOptions?: SecureServerOptions;
28
26
  createServer?: typeof createSecureServer;
29
27
  };
30
- type CreateNodeServerOptions =
31
- | createHttpOptions
32
- | createHttpsOptions
33
- | createHttp2Options
34
- | createSecureHttp2Options;
35
-
28
+ type CreateNodeServerOptions = createHttpOptions | createHttpsOptions | createHttp2Options | createSecureHttp2Options;
29
+ //#endregion
30
+ //#region src/adapters/node.d.ts
36
31
  interface HonoNodeServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
32
+ /**
33
+ * Listening listener (production mode only)
34
+ *
35
+ * It is called when the server is listening
36
+ *
37
+ * Defaults log the port
38
+ */
39
+ listeningListener?: (info: AddressInfo) => void;
40
+ /**
41
+ * Customize the node server (ex: using http2)
42
+ *
43
+ * {@link https://hono.dev/docs/getting-started/nodejs#http2}
44
+ */
45
+ customNodeServer?: CreateNodeServerOptions;
46
+ /**
47
+ * Callback executed just after `serve` from `@hono/node-server`
48
+ */
49
+ onServe?: (server: ServerType) => void;
50
+ /**
51
+ * The Node.js Adapter rewrites the global Request/Response and uses a lightweight Request/Response to improve performance.
52
+ *
53
+ * If you this behavior, set it to `true`
54
+ *
55
+ * 🚨 Setting this to `true` can break `request.clone()` if you later check `instanceof Request`.
56
+ *
57
+ * {@link https://github.com/honojs/node-server?tab=readme-ov-file#overrideglobalobjects}
58
+ *
59
+ * @default false
60
+ */
61
+ overrideGlobalObjects?: boolean;
62
+ /**
63
+ * Customize the hostname of the node server
64
+ */
65
+ hostname?: string;
66
+ /**
67
+ * Customize the serve static options
68
+ */
69
+ serveStaticOptions?: {
37
70
  /**
38
- * Listening listener (production mode only)
71
+ * Customize the public assets (what's in your `public` directory) serve static options.
39
72
  *
40
- * It is called when the server is listening
41
- *
42
- * Defaults log the port
43
73
  */
44
- listeningListener?: (info: AddressInfo) => void;
74
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
45
75
  /**
46
- * Customize the node server (ex: using http2)
76
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
47
77
  *
48
- * {@link https://hono.dev/docs/getting-started/nodejs#http2}
49
- */
50
- customNodeServer?: CreateNodeServerOptions;
51
- /**
52
- * Callback executed just after `serve` from `@hono/node-server`
53
78
  */
54
- onServe?: (server: ServerType) => void;
55
- /**
56
- * The Node.js Adapter rewrites the global Request/Response and uses a lightweight Request/Response to improve performance.
57
- *
58
- * If you this behavior, set it to `true`
59
- *
60
- * 🚨 Setting this to `true` can break `request.clone()` if you later check `instanceof Request`.
61
- *
62
- * {@link https://github.com/honojs/node-server?tab=readme-ov-file#overrideglobalobjects}
63
- *
64
- * @default false
65
- */
66
- overrideGlobalObjects?: boolean;
67
- /**
68
- * Customize the hostname of the node server
69
- */
70
- hostname?: string;
71
- /**
72
- * Customize the serve static options
73
- */
74
- serveStaticOptions?: {
75
- /**
76
- * Customize the public assets (what's in your `public` directory) serve static options.
77
- *
78
- */
79
- publicAssets?: Omit<ServeStaticOptions<E>, "root">;
80
- /**
81
- * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
82
- *
83
- */
84
- clientAssets?: Omit<ServeStaticOptions<E>, "root">;
85
- };
79
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
80
+ };
86
81
  }
87
82
  type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoNodeServerOptions<E> & WithWebsocket<E>;
88
83
  type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoNodeServerOptions<E> & WithoutWebsocket<E>;
@@ -94,5 +89,5 @@ type HonoServerOptions<E extends Env = BlankEnv> = HonoServerOptionsWithWebSocke
94
89
  */
95
90
  declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<Hono<E>>;
96
91
  declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithWebSocket<E>): Promise<Hono<E>>;
97
-
98
- export { type HonoServerOptions, createHonoServer };
92
+ //#endregion
93
+ export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -1,111 +1,102 @@
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/node.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 { serve } from "@hono/node-server";
16
4
  import { serveStatic } from "@hono/node-server/serve-static";
17
5
  import { Hono } from "hono";
18
6
  import { createMiddleware } from "hono/factory";
19
7
  import { logger } from "hono/logger";
20
8
  import { createRequestHandler } from "react-router";
9
+ //#region src/adapters/node.ts
21
10
  async function createHonoServer(options) {
22
- const startTime = Date.now();
23
- const build = await importBuild();
24
- const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
25
- const mergedOptions = {
26
- ...options,
27
- listeningListener: options?.listeningListener || ((info) => {
28
- console.log(`\u{1F680} Server started on port ${info.port}`);
29
- console.log(`\u{1F30D} http://127.0.0.1:${info.port}`);
30
- if (basename !== "/") {
31
- console.log(`\u{1F517} http://127.0.0.1:${info.port}${basename}`);
32
- }
33
- console.log(`\u{1F3CE}\uFE0F Server started in ${Date.now() - startTime}ms`);
34
- }),
35
- port: options?.port || Number(process.env.PORT) || 3e3,
36
- defaultLogger: options?.defaultLogger ?? true,
37
- overrideGlobalObjects: options?.overrideGlobalObjects ?? false
38
- };
39
- const mode = getBuildMode();
40
- const PRODUCTION = mode === "production";
41
- const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
42
- const app = new Hono(mergedOptions.app);
43
- const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
44
- app,
45
- enabled: mergedOptions.useWebSocket ?? false
46
- });
47
- if (!PRODUCTION) {
48
- app.use(bindIncomingRequestSocketInfo());
49
- }
50
- await mergedOptions.beforeAll?.(app);
51
- app.use(
52
- `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
53
- cache(60 * 60 * 24 * 365),
54
- // 1 year
55
- serveStatic({ root: clientBuildPath, ...mergedOptions.serveStaticOptions?.clientAssets })
56
- );
57
- app.use(
58
- "*",
59
- cache(60 * 60),
60
- // 1 hour
61
- serveStatic({ root: PRODUCTION ? clientBuildPath : "./public", ...mergedOptions.serveStaticOptions?.publicAssets })
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
- if (PRODUCTION) {
86
- const server = serve(
87
- {
88
- ...app,
89
- ...mergedOptions.customNodeServer,
90
- port: mergedOptions.port,
91
- overrideGlobalObjects: mergedOptions.overrideGlobalObjects,
92
- hostname: mergedOptions.hostname
93
- },
94
- mergedOptions.listeningListener
95
- );
96
- mergedOptions.onServe?.(server);
97
- injectWebSocket(server);
98
- } else if (globalThis.__viteDevServer?.httpServer) {
99
- const httpServer = globalThis.__viteDevServer.httpServer;
100
- cleanUpgradeListeners(httpServer);
101
- mergedOptions.onServe?.(httpServer);
102
- injectWebSocket(httpServer);
103
- patchUpgradeListener(httpServer);
104
- console.log("\u{1F6A7} Dev server started");
105
- }
106
- return app;
11
+ const startTime = Date.now();
12
+ const build = await importBuild();
13
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
14
+ const mergedOptions = {
15
+ ...options,
16
+ listeningListener: options?.listeningListener || ((info) => {
17
+ console.log(`🚀 Server started on port ${info.port}`);
18
+ console.log(`🌍 http://127.0.0.1:${info.port}`);
19
+ if (basename !== "/") console.log(`🔗 http://127.0.0.1:${info.port}${basename}`);
20
+ console.log(`🏎️ Server started in ${Date.now() - startTime}ms`);
21
+ }),
22
+ port: options?.port || Number(process.env.PORT) || 3e3,
23
+ defaultLogger: options?.defaultLogger ?? true,
24
+ overrideGlobalObjects: options?.overrideGlobalObjects ?? false
25
+ };
26
+ const mode = getBuildMode();
27
+ const PRODUCTION = mode === "production";
28
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
29
+ const app = new Hono(mergedOptions.app);
30
+ const { upgradeWebSocket, injectWebSocket } = await createWebSocket({
31
+ app,
32
+ enabled: mergedOptions.useWebSocket ?? false
33
+ });
34
+ if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
35
+ /**
36
+ * Add optional middleware that runs before any built-in middleware, including assets serving.
37
+ */
38
+ await mergedOptions.beforeAll?.(app);
39
+ /**
40
+ * Serve assets files from build/client/assets
41
+ */
42
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({
43
+ root: clientBuildPath,
44
+ ...mergedOptions.serveStaticOptions?.clientAssets
45
+ }));
46
+ /**
47
+ * Serve public files
48
+ */
49
+ app.use("*", cache(3600), serveStatic({
50
+ root: PRODUCTION ? clientBuildPath : "./public",
51
+ ...mergedOptions.serveStaticOptions?.publicAssets
52
+ }));
53
+ /**
54
+ * Add logger middleware
55
+ */
56
+ if (mergedOptions.defaultLogger) app.use("*", logger());
57
+ /**
58
+ * Add optional middleware
59
+ */
60
+ if (mergedOptions.useWebSocket) await mergedOptions.configure(app, { upgradeWebSocket });
61
+ else await mergedOptions.configure?.(app);
62
+ /**
63
+ * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
64
+ */
65
+ const reactRouterApp = new Hono({ strict: false });
66
+ reactRouterApp.use((c, next) => {
67
+ return createMiddleware(async (c) => {
68
+ const requestHandler = createRequestHandler(build, mode);
69
+ const loadContext = mergedOptions.getLoadContext?.(c, {
70
+ build,
71
+ mode
72
+ });
73
+ return requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
74
+ })(c, next);
75
+ });
76
+ app.route(`${basename}`, reactRouterApp);
77
+ if (basename) app.route(`${basename}.data`, reactRouterApp);
78
+ /**
79
+ * Start the production server
80
+ */
81
+ if (PRODUCTION) {
82
+ const server = serve({
83
+ ...app,
84
+ ...mergedOptions.customNodeServer,
85
+ port: mergedOptions.port,
86
+ overrideGlobalObjects: mergedOptions.overrideGlobalObjects,
87
+ hostname: mergedOptions.hostname
88
+ }, mergedOptions.listeningListener);
89
+ 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
+ }
99
+ return app;
107
100
  }
108
- export {
109
- createGetLoadContext,
110
- createHonoServer
111
- };
101
+ //#endregion
102
+ export { createGetLoadContext, createHonoServer };
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/cli.js CHANGED
@@ -1,45 +1,38 @@
1
1
  #!/usr/bin/env node
2
- // src/cli.ts
3
- import fs from "fs";
4
- import path from "path";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
5
4
  import { command, run } from "@drizzle-team/brocli";
6
- var runtimeRegex = /runtime:\s*(['"])([^'"]*)\1/;
5
+ //#region src/cli.ts
6
+ const runtimeRegex = /runtime:\s*(['"])([^'"]*)\1/;
7
7
  function guessRuntime() {
8
- try {
9
- const viteConfig = fs.readFileSync(path.resolve(process.cwd(), "vite.config.ts"), "utf-8");
10
- return viteConfig.match(runtimeRegex)?.[2] || "node";
11
- } catch {
12
- return "node";
13
- }
8
+ try {
9
+ return fs.readFileSync(path.resolve(process.cwd(), "vite.config.ts"), "utf-8").match(runtimeRegex)?.[2] || "node";
10
+ } catch {
11
+ return "node";
12
+ }
14
13
  }
15
- var runtime = guessRuntime();
16
- var serverFile = `// generated by react-router-hono-server/dev
17
- import { createHonoServer } from "react-router-hono-server/${runtime}";
14
+ const serverFile = `// generated by react-router-hono-server/dev
15
+ import { createHonoServer } from "react-router-hono-server/${guessRuntime()}";
18
16
 
19
17
  export default await createHonoServer();`;
20
- var file = 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\u2728 File created at ${serverFilePath}\x1B[0m`);
26
- }
27
- });
28
- var folder = command({
29
- name: "folder",
30
- handler() {
31
- const serverFilePath = path.resolve(process.cwd(), "app", "server", "index.ts");
32
- if (!fs.existsSync(serverFilePath)) {
33
- fs.mkdirSync(path.dirname(serverFilePath), { recursive: true });
34
- }
35
- fs.writeFileSync(serverFilePath, serverFile);
36
- console.log(`\x1B[33m\u2728 Folder and file created at ${serverFilePath}\x1B[0m`);
37
- }
38
- });
39
- var reveal = command({
40
- name: "reveal",
41
- subcommands: [file, folder]
42
- });
43
- run([reveal], {
44
- name: "React Router Hono Server CLI"
45
- });
18
+ run([command({
19
+ 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" });
37
+ //#endregion
38
+ export {};
package/dist/dev.d.ts CHANGED
@@ -1,59 +1,61 @@
1
- import { DevServerOptions } from '@hono/vite-dev-server';
2
- import { Plugin } from 'vite';
3
-
4
- type Runtime = "node" | "bun" | "cloudflare" | "aws";
1
+ import { DevServerOptions } from "@hono/vite-dev-server";
2
+ import { Plugin } from "vite";
5
3
 
4
+ //#region src/types/runtime.d.ts
5
+ type Runtime = "node" | "bun" | "cloudflare" | "aws" | "deno";
6
+ //#endregion
7
+ //#region src/dev.d.ts
6
8
  type ReactRouterHonoServerPluginOptionsBase = {
7
- /**
8
- * The path to the server file, relative to `vite.config.ts`.
9
- *
10
- * If it is a folder (`app/server`), it will look for an `index.ts` file.
11
- *
12
- * Defaults to `${appDirectory}/server[.ts | /index.ts]` if present.
13
- *
14
- * Fallback to a virtual module `virtual:react-router-hono-server/server`.`
15
- */
16
- serverEntryPoint?: string;
9
+ /**
10
+ * The path to the server file, relative to `vite.config.ts`.
11
+ *
12
+ * If it is a folder (`app/server`), it will look for an `index.ts` file.
13
+ *
14
+ * Defaults to `${appDirectory}/server[.ts | /index.ts]` if present.
15
+ *
16
+ * Fallback to a virtual module `virtual:react-router-hono-server/server`.`
17
+ */
18
+ serverEntryPoint?: string;
19
+ /**
20
+ * The paths that are not served by the dev-server.
21
+ *
22
+ * Defaults include `appDirectory` content.
23
+ */
24
+ dev?: {
17
25
  /**
18
26
  * The paths that are not served by the dev-server.
19
27
  *
20
28
  * Defaults include `appDirectory` content.
21
29
  */
22
- dev?: {
23
- /**
24
- * The paths that are not served by the dev-server.
25
- *
26
- * Defaults include `appDirectory` content.
27
- */
28
- exclude?: DevServerOptions["exclude"];
29
- /**
30
- * The name of the export to use for the server.
31
- *
32
- * Defaults to `default`.
33
- */
34
- export?: DevServerOptions["export"];
35
- };
36
- };
37
- type ReactRouterHonoServerPluginOptionsDefault = ReactRouterHonoServerPluginOptionsBase & {
30
+ exclude?: DevServerOptions["exclude"];
38
31
  /**
39
- * The runtime to use for the server.
32
+ * The name of the export to use for the server.
40
33
  *
41
- * Defaults to `node`.
34
+ * Defaults to `default`.
42
35
  */
43
- runtime?: Runtime;
36
+ export?: DevServerOptions["export"];
37
+ };
38
+ };
39
+ type ReactRouterHonoServerPluginOptionsDefault = ReactRouterHonoServerPluginOptionsBase & {
40
+ /**
41
+ * The runtime to use for the server.
42
+ *
43
+ * Defaults to `node`.
44
+ */
45
+ runtime?: Runtime;
44
46
  };
45
47
  type ReactRouterHonoServerPluginOptionsCloudflare = ReactRouterHonoServerPluginOptionsBase & {
46
- /**
47
- * The runtime to use for the server.
48
- *
49
- * Defaults to `node`.
50
- */
51
- runtime: Extract<Runtime, "cloudflare">;
52
- flag?: {
53
- force_react_19?: boolean;
54
- };
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
+ };
55
57
  };
56
58
  type ReactRouterHonoServerPluginOptions = ReactRouterHonoServerPluginOptionsDefault | ReactRouterHonoServerPluginOptionsCloudflare;
57
59
  declare function reactRouterHonoServer(options?: ReactRouterHonoServerPluginOptions): Plugin;
58
-
59
- export { reactRouterHonoServer };
60
+ //#endregion
61
+ export { reactRouterHonoServer };