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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # React Router v7 Hono Server
1
+ # React Router v8 Hono Server
2
2
 
3
3
  <!-- ![GitHub Repo stars](https://img.shields.io/github/stars/rphlmr/react-router-hono-server?style=social)
4
4
  ![npm](https://img.shields.io/npm/v/open-source-stack?style=plastic)
@@ -287,6 +287,26 @@ export default defineConfig({
287
287
  });
288
288
  ```
289
289
 
290
+ #### Deno
291
+ > [!TIP]
292
+ > Check this [example](./examples/deno/simple/) to see how to use it.
293
+
294
+ ```ts
295
+ // vite.config.ts
296
+ import { reactRouter } from "@react-router/dev/vite";
297
+ import { reactRouterHonoServer } from "react-router-hono-server/dev"; // add this
298
+ import { defineConfig } from "vite";
299
+ import tsconfigPaths from "vite-tsconfig-paths";
300
+
301
+ export default defineConfig({
302
+ plugins: [
303
+ reactRouterHonoServer({ runtime: "deno" }), // add this
304
+ reactRouter(),
305
+ tsconfigPaths()
306
+ ],
307
+ });
308
+ ```
309
+
290
310
  ### Create the server
291
311
  > [!TIP]
292
312
  > You can use the CLI to create the server file for you.
@@ -402,6 +422,17 @@ It is not an error, you can keep the React Router defaults for `build` and `dev`
402
422
  },
403
423
  ```
404
424
 
425
+
426
+ ### Deno
427
+ It is not an error, you can keep the React Router defaults for `build`!
428
+ ```json
429
+ "scripts": {
430
+ "build": "react-router build",
431
+ "dev": "deno run -A npm:react-router dev",
432
+ "start": "deno run -A ./build/server/index.js",
433
+ },
434
+ ```
435
+
405
436
  ## How it works
406
437
 
407
438
  This helper works differently depending on the environment.
@@ -422,7 +453,7 @@ That's all!
422
453
 
423
454
  #### `reactRouterHonoServer` (Vite Plugin)
424
455
  ```ts
425
- type Runtime = "node" | "bun" | "cloudflare" | "aws";
456
+ type Runtime = "node" | "bun" | "cloudflare" | "aws" | "deno";
426
457
 
427
458
  type ReactRouterHonoServerPluginOptions = {
428
459
  /**
@@ -717,6 +748,50 @@ interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServer
717
748
  }
718
749
  ```
719
750
 
751
+
752
+ ##### Deno
753
+ ```ts
754
+ interface HonoDenoServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
755
+ /**
756
+ * Customize the deno server
757
+ *
758
+ * {@link https://docs.deno.com/api/deno/~/Deno.serve#parameters}
759
+ */
760
+ customDenoServer?: Deno.ServeOptions;
761
+
762
+ /**
763
+ * Callback executed after server has closed and all inflight requests completed,
764
+ * before process exit. Only applicable in production mode.
765
+ *
766
+ * @example
767
+ * ```ts
768
+ * export default createHonoServer({
769
+ * onGracefulShutdown: async () => {
770
+ * await db.close();
771
+ * },
772
+ * });
773
+ * ```
774
+ */
775
+
776
+ onGracefulShutdown?: () => Promise<void> | void;
777
+ /**
778
+ * Customize the serve static options
779
+ */
780
+ serveStaticOptions?: {
781
+ /**
782
+ * Customize the public assets (what's in your `public` directory) serve static options.
783
+ *
784
+ */
785
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
786
+ /**
787
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
788
+ *
789
+ */
790
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
791
+ };
792
+ }
793
+ ```
794
+
720
795
  ## Middleware
721
796
 
722
797
  🚨 Redirecting from a middleware
@@ -886,12 +961,15 @@ You can use React Router middleware with this package.
886
961
 
887
962
  Then, you can use the `use` function from `react-router` to access the context in your loaders and actions.
888
963
 
889
- ```ts
890
-
891
-
892
964
  ### Using WebSockets
893
965
  #### Node
894
- This package has a built-in helper to use `@hono/node-ws`
966
+ This package has a built-in helper to use `@hono/node-ws`.
967
+
968
+ When using WebSockets with the Node adapter, install `@hono/node-ws` in your app:
969
+
970
+ ```bash
971
+ npm install @hono/node-ws
972
+ ```
895
973
 
896
974
  > [!TIP]
897
975
  > Check this [example](./examples/node/websocket/) to see how to use it.
@@ -1,65 +1,20 @@
1
- import * as hono_aws_lambda from 'hono/aws-lambda';
2
- import * as hono_types from 'hono/types';
3
- import { BlankEnv } from 'hono/types';
4
- import { Env, Hono } from 'hono';
5
- import { H as HonoServerOptionsBase, W as WithoutWebsocket } from '../helpers-BXdVojwF.js';
6
- export { c as createGetLoadContext } from '../helpers-BXdVojwF.js';
7
- import 'hono/ws';
8
- import 'react-router';
9
-
10
- interface CognitoIdentity {
11
- cognitoIdentityId: string;
12
- cognitoIdentityPoolId: string;
13
- }
14
- interface ClientContext {
15
- client: ClientContextClient;
16
- Custom?: any;
17
- env: ClientContextEnv;
18
- }
19
- interface ClientContextClient {
20
- installationId: string;
21
- appTitle: string;
22
- appVersionName: string;
23
- appVersionCode: string;
24
- appPackageName: string;
25
- }
26
- interface ClientContextEnv {
27
- platformVersion: string;
28
- platform: string;
29
- make: string;
30
- model: string;
31
- locale: string;
32
- }
33
- /**
34
- * {@link Handler} context parameter.
35
- * See {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html AWS documentation}.
36
- */
37
- interface LambdaContext {
38
- callbackWaitsForEmptyEventLoop: boolean;
39
- functionName: string;
40
- functionVersion: string;
41
- invokedFunctionArn: string;
42
- memoryLimitInMB: string;
43
- awsRequestId: string;
44
- logGroupName: string;
45
- logStreamName: string;
46
- identity?: CognitoIdentity | undefined;
47
- clientContext?: ClientContext | undefined;
48
- getRemainingTimeInMillis(): number;
49
- }
50
- type Callback<TResult = any> = (error?: Error | string | null, result?: TResult) => void;
51
- type Handler<TEvent = any, TResult = any> = (event: TEvent, context: LambdaContext, callback: Callback<TResult>) => void | Promise<TResult>;
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
2
+ import { Env, Hono } from "hono";
3
+ import { LambdaContext, LambdaEvent } from "hono/aws-lambda";
4
+ import { BlankEnv } from "hono/types";
52
5
 
6
+ //#region src/adapters/aws-lambda.d.ts
53
7
  type InvokeMode = "default" | "stream";
8
+ type AWSLambdaHandler = (event: LambdaEvent, lambdaContext: LambdaContext, callback: (error?: Error | string | null, result?: unknown) => void) => unknown | Promise<unknown>;
54
9
  interface HonoAWSServerOptions<E extends Env = BlankEnv> extends Omit<HonoServerOptionsBase<E>, "port"> {
55
- /**
56
- * The invoke mode to use
57
- *
58
- * Defaults to `default`
59
- *
60
- * {@link https://hono.dev/docs/getting-started/aws-lambda#lambda-response-streaming}
61
- */
62
- invokeMode: InvokeMode;
10
+ /**
11
+ * The invoke mode to use
12
+ *
13
+ * Defaults to `default`
14
+ *
15
+ * {@link https://hono.dev/docs/getting-started/aws-lambda#lambda-response-streaming}
16
+ */
17
+ invokeMode: InvokeMode;
63
18
  }
64
19
  type HonoServerOptions<E extends Env = BlankEnv> = HonoAWSServerOptions<E> & Omit<WithoutWebsocket<E>, "useWebSocket">;
65
20
  /**
@@ -67,14 +22,6 @@ type HonoServerOptions<E extends Env = BlankEnv> = HonoAWSServerOptions<E> & Omi
67
22
  *
68
23
  * @param config {@link HonoServerOptions} - The configuration options for the server
69
24
  */
70
- declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E, hono_types.BlankSchema, "/"> | Handler | (<L extends hono_aws_lambda.LambdaEvent>(event: L, lambdaContext?: hono_aws_lambda.LambdaContext) => Promise<hono_aws_lambda.APIGatewayProxyResult & (L extends {
71
- multiValueHeaders: Record<string, string[]>;
72
- } ? {
73
- headers?: undefined;
74
- multiValueHeaders: Record<string, string[]>;
75
- } : {
76
- headers: Record<string, string>;
77
- multiValueHeaders?: undefined;
78
- })>)>;
79
-
80
- export { type HonoServerOptions, createHonoServer };
25
+ declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptions<E>): Promise<Hono<E> | AWSLambdaHandler>;
26
+ //#endregion
27
+ export { HonoServerOptions, createGetLoadContext, createHonoServer };
@@ -1,78 +1,80 @@
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/aws-lambda.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 { serveStatic } from "@hono/node-server/serve-static";
13
4
  import { Hono } from "hono";
14
- import { handle, streamHandle } from "hono/aws-lambda";
15
5
  import { createMiddleware } from "hono/factory";
16
6
  import { logger } from "hono/logger";
17
7
  import { createRequestHandler } from "react-router";
8
+ import { handle, streamHandle } from "hono/aws-lambda";
9
+ //#region src/adapters/aws-lambda.ts
10
+ /**
11
+ * Create a Hono server
12
+ *
13
+ * @param config {@link HonoServerOptions} - The configuration options for the server
14
+ */
18
15
  async function createHonoServer(options) {
19
- const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
20
- const mergedOptions = {
21
- ...options,
22
- invokeMode: options?.invokeMode ?? "default",
23
- defaultLogger: options?.defaultLogger ?? true
24
- };
25
- const mode = getBuildMode();
26
- const PRODUCTION = mode === "production";
27
- const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
28
- const app = new Hono(mergedOptions.app);
29
- if (!PRODUCTION) {
30
- app.use(bindIncomingRequestSocketInfo());
31
- }
32
- await mergedOptions.beforeAll?.(app);
33
- if (!PRODUCTION) {
34
- app.use(
35
- `/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`,
36
- cache(60 * 60 * 24 * 365),
37
- // 1 year
38
- serveStatic({ root: clientBuildPath })
39
- );
40
- app.use(
41
- "*",
42
- cache(60 * 60),
43
- // 1 hour
44
- serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" })
45
- );
46
- }
47
- if (mergedOptions.defaultLogger) {
48
- app.use("*", logger());
49
- }
50
- await mergedOptions.configure?.(app);
51
- const reactRouterApp = new Hono({
52
- strict: false
53
- });
54
- reactRouterApp.use(async (c, next) => {
55
- const build = await importBuild();
56
- return createMiddleware(async (c2) => {
57
- const requestHandler = createRequestHandler(build, mode);
58
- const loadContext = mergedOptions.getLoadContext?.(c2, { build, mode });
59
- return requestHandler(c2.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
60
- })(c, next);
61
- });
62
- app.route(`${basename}`, reactRouterApp);
63
- if (basename) {
64
- app.route(`${basename}.data`, reactRouterApp);
65
- }
66
- if (PRODUCTION) {
67
- if (mergedOptions.invokeMode === "stream") {
68
- return streamHandle(app);
69
- }
70
- return handle(app);
71
- }
72
- console.log("\u{1F6A7} Dev server started");
73
- return app;
16
+ const basename = import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME;
17
+ const mergedOptions = {
18
+ ...options,
19
+ invokeMode: options?.invokeMode ?? "default",
20
+ defaultLogger: options?.defaultLogger ?? true
21
+ };
22
+ const mode = getBuildMode();
23
+ const PRODUCTION = mode === "production";
24
+ const clientBuildPath = `${import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY}/client`;
25
+ const app = new Hono(mergedOptions.app);
26
+ if (!PRODUCTION) app.use(bindIncomingRequestSocketInfo());
27
+ /**
28
+ * Add optional middleware that runs before any built-in middleware, including assets serving.
29
+ */
30
+ await mergedOptions.beforeAll?.(app);
31
+ /**
32
+ * In production, you may use CloudFront or any other AWS services to serve assets
33
+ */
34
+ if (!PRODUCTION) {
35
+ /**
36
+ * Serve assets files from build/client/assets
37
+ */
38
+ app.use(`/${import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR}/*`, cache(3600 * 24 * 365), serveStatic({ root: clientBuildPath }));
39
+ /**
40
+ * Serve public files
41
+ */
42
+ app.use("*", cache(3600), serveStatic({ root: PRODUCTION ? clientBuildPath : "./public" }));
43
+ }
44
+ /**
45
+ * Add logger middleware
46
+ */
47
+ if (mergedOptions.defaultLogger) app.use("*", logger());
48
+ /**
49
+ * Add optional middleware
50
+ */
51
+ await mergedOptions.configure?.(app);
52
+ /**
53
+ * Create a React Router Hono app and bind it to the root Hono server using the React Router basename
54
+ */
55
+ const reactRouterApp = new Hono({ strict: false });
56
+ reactRouterApp.use(async (c, next) => {
57
+ const build = await importBuild();
58
+ return createMiddleware(async (c) => {
59
+ const requestHandler = createRequestHandler(build, mode);
60
+ const loadContext = mergedOptions.getLoadContext?.(c, {
61
+ build,
62
+ mode
63
+ });
64
+ return requestHandler(c.req.raw, loadContext instanceof Promise ? await loadContext : loadContext);
65
+ })(c, next);
66
+ });
67
+ app.route(`${basename}`, reactRouterApp);
68
+ if (basename) app.route(`${basename}.data`, reactRouterApp);
69
+ /**
70
+ * Wrap the app in the appropriate AWS handler
71
+ */
72
+ if (PRODUCTION) {
73
+ if (mergedOptions.invokeMode === "stream") return streamHandle(app);
74
+ return handle(app);
75
+ }
76
+ console.log("🚧 Dev server started");
77
+ return app;
74
78
  }
75
- export {
76
- createGetLoadContext,
77
- createHonoServer
78
- };
79
+ //#endregion
80
+ export { createGetLoadContext, createHonoServer };
@@ -1,49 +1,46 @@
1
- import { Serve } from 'bun';
2
- import { Env } from 'hono';
3
- import { ServeStaticOptions } from 'hono/serve-static';
4
- import { BlankEnv } from 'hono/types';
5
- import { H as HonoServerOptionsBase, a as WithWebsocket, W as WithoutWebsocket } from '../helpers-BXdVojwF.js';
6
- export { c as createGetLoadContext } from '../helpers-BXdVojwF.js';
7
- import 'hono/ws';
8
- import 'react-router';
1
+ import { i as WithoutWebsocket, n as HonoServerOptionsBase, r as WithWebsocket, t as createGetLoadContext } from "../helpers-CvvzWAWX.js";
2
+ import { Env } from "hono";
3
+ import { BlankEnv } from "hono/types";
4
+ import { ServeStaticOptions } from "hono/serve-static";
9
5
 
10
- type CustomBunServer = Serve.Options<unknown, string>;
6
+ //#region src/adapters/bun.d.ts
7
+ type CustomBunServer = Bun.Serve.Options<unknown, string>;
11
8
  interface HonoBunServerOptions<E extends Env = BlankEnv> extends HonoServerOptionsBase<E> {
9
+ /**
10
+ * Customize the bun server
11
+ *
12
+ * {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
13
+ */
14
+ customBunServer?: Partial<CustomBunServer>;
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
+ /**
30
+ * Customize the serve static options
31
+ */
32
+ serveStaticOptions?: {
12
33
  /**
13
- * Customize the bun server
34
+ * Customize the public assets (what's in your `public` directory) serve static options.
14
35
  *
15
- * {@link https://bun.sh/docs/api/http#start-a-server-bun-serve}
16
36
  */
17
- customBunServer?: Partial<CustomBunServer>;
37
+ publicAssets?: Omit<ServeStaticOptions<E>, "root">;
18
38
  /**
19
- * Callback executed after server has closed and all inflight requests completed,
20
- * before process exit. Only applicable in production mode.
39
+ * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
21
40
  *
22
- * @example
23
- * ```ts
24
- * export default createHonoServer({
25
- * onGracefulShutdown: async () => {
26
- * await db.close();
27
- * },
28
- * });
29
- * ```
30
41
  */
31
- onGracefulShutdown?: () => Promise<void> | void;
32
- /**
33
- * Customize the serve static options
34
- */
35
- serveStaticOptions?: {
36
- /**
37
- * Customize the public assets (what's in your `public` directory) serve static options.
38
- *
39
- */
40
- publicAssets?: Omit<ServeStaticOptions<E>, "root">;
41
- /**
42
- * Customize the client assets (what's in your `build/client/assets` directory - React Router) serve static options.
43
- *
44
- */
45
- clientAssets?: Omit<ServeStaticOptions<E>, "root">;
46
- };
42
+ clientAssets?: Omit<ServeStaticOptions<E>, "root">;
43
+ };
47
44
  }
48
45
  type HonoServerOptionsWithWebSocket<E extends Env = BlankEnv> = HonoBunServerOptions<E> & WithWebsocket<E>;
49
46
  type HonoServerOptionsWithoutWebSocket<E extends Env = BlankEnv> = HonoBunServerOptions<E> & WithoutWebsocket<E>;
@@ -55,5 +52,5 @@ type HonoServerOptions<E extends Env = BlankEnv> = HonoServerOptionsWithWebSocke
55
52
  */
56
53
  declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithoutWebSocket<E>): Promise<CustomBunServer>;
57
54
  declare function createHonoServer<E extends Env = BlankEnv>(options?: HonoServerOptionsWithWebSocket<E>): Promise<CustomBunServer>;
58
-
59
- export { type HonoServerOptions, createHonoServer };
55
+ //#endregion
56
+ export { HonoServerOptions, createGetLoadContext, createHonoServer };