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