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.
- package/README.md +569 -977
- package/dist/adapters/aws-lambda.d.ts +2 -3
- package/dist/adapters/aws-lambda.js +5 -3
- package/dist/adapters/bun.d.ts +1 -2
- package/dist/adapters/bun.js +26 -22
- package/dist/adapters/cloudflare.d.ts +8 -4
- package/dist/adapters/cloudflare.js +19 -13
- package/dist/adapters/deno.d.ts +4 -3
- package/dist/adapters/deno.js +34 -18
- package/dist/adapters/node.d.ts +15 -3
- package/dist/adapters/node.js +21 -17
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +24 -20
- package/dist/dev.d.ts +1 -13
- package/dist/dev.js +75 -61
- package/dist/{helpers-DpeZU9Bf.js → helpers-D9Y2AQRk.js} +60 -11
- package/dist/{helpers-CvvzWAWX.d.ts → helpers-VHYCCAPs.d.ts} +13 -25
- package/dist/http.d.ts +0 -1
- package/dist/http.js +2 -2
- package/dist/react-router-build-request-_jlIVza5.js +7 -0
- package/dist/vite-public-path-Aqd7hZ0d.js +59 -0
- package/package.json +80 -59
package/dist/dev.js
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { t as isReactRouterBuildRequest } from "./react-router-build-request-_jlIVza5.js";
|
|
2
|
+
import { a as vitePathnamePrefix, t as classifyVitePublicPath } from "./vite-public-path-Aqd7hZ0d.js";
|
|
3
3
|
import honoDevServer from "@hono/vite-dev-server";
|
|
4
4
|
import bunAdapter from "@hono/vite-dev-server/bun";
|
|
5
5
|
import nodeAdapter from "@hono/vite-dev-server/node";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { pathToFileURL } from "node:url";
|
|
6
9
|
//#region src/dev.ts
|
|
7
10
|
const REACT_ROUTER_SERVER_BUILD_MODULE_ID = "\0virtual:react-router/server-build";
|
|
8
11
|
const VIRTUAL_MODULE_ID = "\0virtual:react-router-hono-server/server";
|
|
9
|
-
const REACT_ROUTER_EXPORT = "export { serverManifest as assets, assetsBuildDirectory, basename, entry, future, isSpaMode, prerender, publicPath, routes, ssr, routeDiscovery };";
|
|
10
12
|
function reactRouterHonoServer(options = {}) {
|
|
11
13
|
const runtime = options.runtime || "node";
|
|
14
|
+
const adapterEntryPoint = runtime === "aws" ? "aws-lambda" : runtime;
|
|
12
15
|
let pluginConfig;
|
|
13
16
|
let devServerPlugin;
|
|
17
|
+
let previewAppPromise;
|
|
14
18
|
return {
|
|
15
19
|
name: "react-router-hono-server",
|
|
16
20
|
enforce: "post",
|
|
@@ -20,27 +24,31 @@ function reactRouterHonoServer(options = {}) {
|
|
|
20
24
|
load(id) {
|
|
21
25
|
if (id === VIRTUAL_MODULE_ID) return `
|
|
22
26
|
// generated by react-router-hono-server/dev
|
|
23
|
-
import { createHonoServer } from "react-router-hono-server/${
|
|
27
|
+
import { createHonoServer } from "react-router-hono-server/${adapterEntryPoint}";
|
|
24
28
|
|
|
25
29
|
export default await createHonoServer();
|
|
26
30
|
`;
|
|
27
31
|
},
|
|
28
|
-
|
|
32
|
+
configResolved(config) {
|
|
33
|
+
if (runtime === "cloudflare" && !config.plugins.some((plugin) => plugin.name === "vite-plugin-cloudflare" || plugin.name.startsWith("vite-plugin-cloudflare:"))) throw new Error("Missing cloudflare() in vite.config.ts. Add it with: import { cloudflare } from \"@cloudflare/vite-plugin\".");
|
|
34
|
+
},
|
|
35
|
+
config(config, env) {
|
|
29
36
|
pluginConfig = resolvePluginConfig(config, options);
|
|
30
37
|
if (!pluginConfig) return;
|
|
31
38
|
const serverEntryPoint = pluginConfig.serverEntryPoint;
|
|
32
|
-
|
|
39
|
+
const denoDevReactExternals = runtime === "deno" && env.mode === "development" ? ["react", "react-dom"] : [];
|
|
33
40
|
const baseConfig = {
|
|
34
41
|
define: {
|
|
35
42
|
"import.meta.env.REACT_ROUTER_HONO_SERVER_BUILD_DIRECTORY": JSON.stringify(pluginConfig.buildDirectory),
|
|
36
43
|
"import.meta.env.REACT_ROUTER_HONO_SERVER_ASSETS_DIR": JSON.stringify(pluginConfig.assetsDir),
|
|
37
44
|
"import.meta.env.REACT_ROUTER_HONO_SERVER_RUNTIME": JSON.stringify(runtime),
|
|
38
|
-
"import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME": JSON.stringify(pluginConfig.basename)
|
|
45
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_BASENAME": JSON.stringify(pluginConfig.basename),
|
|
46
|
+
"import.meta.env.REACT_ROUTER_HONO_SERVER_VITE_BASE": JSON.stringify(pluginConfig.viteBase)
|
|
39
47
|
},
|
|
40
48
|
ssr: {
|
|
41
49
|
target: runtime === "cloudflare" ? "webworker" : void 0,
|
|
42
50
|
noExternal: ["react-router-hono-server"],
|
|
43
|
-
external: [
|
|
51
|
+
...runtime === "cloudflare" ? {} : { external: [...denoDevReactExternals] },
|
|
44
52
|
optimizeDeps: runtime === "bun" ? {
|
|
45
53
|
include: ["react-dom/server"],
|
|
46
54
|
exclude: ["react"]
|
|
@@ -50,13 +58,16 @@ function reactRouterHonoServer(options = {}) {
|
|
|
50
58
|
let reactRouterBuildFile = pluginConfig.serverBuildFile;
|
|
51
59
|
if (reactRouterBuildFile === "index.js") reactRouterBuildFile = "assets/server-build.js";
|
|
52
60
|
const resolve = {};
|
|
53
|
-
if (runtime === "cloudflare") resolve.alias = { "react-dom/server":
|
|
54
|
-
if (runtime === "bun") resolve.alias = { "react-dom/server": "react-dom/server.
|
|
55
|
-
if (runtime === "deno") resolve.alias = { "react-dom/server": "react-dom/server.
|
|
61
|
+
if (runtime === "cloudflare") resolve.alias = { "react-dom/server": "react-dom/server.edge" };
|
|
62
|
+
if (runtime === "bun") resolve.alias = { "react-dom/server": "react-dom/server.browser" };
|
|
63
|
+
if (runtime === "deno" && env.mode !== "development") resolve.alias = { "react-dom/server": "react-dom/server.browser" };
|
|
56
64
|
const ssrConfig = {
|
|
57
|
-
resolve
|
|
65
|
+
resolve: {
|
|
66
|
+
...resolve,
|
|
67
|
+
...denoDevReactExternals.length > 0 ? { external: denoDevReactExternals } : {}
|
|
68
|
+
},
|
|
58
69
|
build: { rollupOptions: {
|
|
59
|
-
input: serverEntryPoint,
|
|
70
|
+
...runtime === "cloudflare" ? {} : { input: serverEntryPoint },
|
|
60
71
|
output: {
|
|
61
72
|
entryFileNames: (chunk) => {
|
|
62
73
|
chunk.facadeModuleId = REACT_ROUTER_SERVER_BUILD_MODULE_ID;
|
|
@@ -68,8 +79,7 @@ function reactRouterHonoServer(options = {}) {
|
|
|
68
79
|
},
|
|
69
80
|
manualChunks: runtime !== "cloudflare" ? (id, meta) => {
|
|
70
81
|
if (meta.getModuleInfo(id)?.importers.some((id) => id.includes(serverEntryPoint))) return path.basename(id, path.extname(id));
|
|
71
|
-
} : void 0
|
|
72
|
-
footer: runtime === "cloudflare" ? REACT_ROUTER_EXPORT : void 0
|
|
82
|
+
} : void 0
|
|
73
83
|
}
|
|
74
84
|
} }
|
|
75
85
|
};
|
|
@@ -79,33 +89,13 @@ function reactRouterHonoServer(options = {}) {
|
|
|
79
89
|
environments: { ssr: ssrConfig }
|
|
80
90
|
};
|
|
81
91
|
},
|
|
82
|
-
|
|
83
|
-
if (!pluginConfig?.isSsrBuild || runtime !== "cloudflare") return;
|
|
84
|
-
console.log("Cleaning up server exports...");
|
|
85
|
-
const buildPath = path.join(pluginConfig.rootDirectory, pluginConfig.buildDirectory, "server", "index.js");
|
|
86
|
-
let serverFile = await fs.promises.readFile(buildPath, "utf-8").catch((e) => {
|
|
87
|
-
if (e.code === "ENOENT") {
|
|
88
|
-
console.error("\x1B[31m\nThe server was not built\x1B[0m");
|
|
89
|
-
if (pluginConfig?.flag?.force_react_19) console.error("\x1B[31m\nYou are forcing React 19 but the server failed to be built by React Router.\nCheck your dependencies, but it's possible that your project is still using React 18 from a node_modules root (monorepo?)\n\x1B[0m");
|
|
90
|
-
}
|
|
91
|
-
return null;
|
|
92
|
-
});
|
|
93
|
-
if (!serverFile) return;
|
|
94
|
-
serverFile = serverFile.replace(REACT_ROUTER_EXPORT, "");
|
|
95
|
-
if (await getReactVersion(pluginConfig) >= 19) serverFile = serverFile.replace(/react-dom\/server\.browser/g, "react-dom/server.edge");
|
|
96
|
-
await fs.promises.writeFile(buildPath, serverFile);
|
|
97
|
-
console.log("\x1B[32mAll done!\x1B[0m");
|
|
98
|
-
},
|
|
99
|
-
async configureServer(server) {
|
|
92
|
+
configureServer(server) {
|
|
100
93
|
globalThis.__viteDevServer = server;
|
|
101
94
|
if (!pluginConfig) return;
|
|
95
|
+
if (runtime === "cloudflare") return;
|
|
102
96
|
if (devServerPlugin) return;
|
|
103
97
|
let adapter = nodeAdapter;
|
|
104
98
|
if (runtime === "bun") adapter = bunAdapter;
|
|
105
|
-
if (runtime === "cloudflare") {
|
|
106
|
-
const { cloudflareAdapter } = await import("@hono/vite-dev-server/cloudflare");
|
|
107
|
-
adapter = cloudflareAdapter;
|
|
108
|
-
}
|
|
109
99
|
if (runtime === "deno") adapter = () => {
|
|
110
100
|
if (typeof globalThis.navigator === "undefined") globalThis.navigator = { userAgent: "Deno" };
|
|
111
101
|
else Object.defineProperty(globalThis.navigator, "userAgent", {
|
|
@@ -114,19 +104,27 @@ function reactRouterHonoServer(options = {}) {
|
|
|
114
104
|
});
|
|
115
105
|
return { env: Deno.env.toObject() };
|
|
116
106
|
};
|
|
107
|
+
const vitePrefix = vitePathnamePrefix(classifyVitePublicPath(pluginConfig.viteBase));
|
|
108
|
+
const appDirectory = pluginConfig.appDirectory.replace(/^[/\\]+|[/\\]+$/g, "").replaceAll(/[/\\]+/g, "/");
|
|
109
|
+
const sourceDirectory = appDirectory.split("/")[0];
|
|
110
|
+
const appPath = `${vitePrefix}/${appDirectory}`;
|
|
111
|
+
const sourcePath = `${vitePrefix}/${sourceDirectory}`;
|
|
112
|
+
const escapedAppPath = escapeRegExp(appPath);
|
|
113
|
+
const escapedSourcePath = escapeRegExp(sourcePath);
|
|
114
|
+
const escapedVitePrefix = escapeRegExp(vitePrefix);
|
|
117
115
|
devServerPlugin = honoDevServer({
|
|
118
116
|
adapter,
|
|
119
117
|
injectClientScript: false,
|
|
120
118
|
entry: pluginConfig.serverEntryPoint,
|
|
121
119
|
export: options.dev?.export || "default",
|
|
122
120
|
exclude: [
|
|
123
|
-
new RegExp(`^(
|
|
124
|
-
new RegExp(`^(
|
|
121
|
+
new RegExp(`^(?=${escapedAppPath}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
|
|
122
|
+
new RegExp(`^(?=${escapedSourcePath}\\/)((?!.*\\.data(\\?|$)).*\\..*(\\?.*)?$)`),
|
|
125
123
|
/\?import(\?.*)?$/,
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
`^(
|
|
129
|
-
`^(
|
|
124
|
+
new RegExp(`^${escapedVitePrefix}\\/@.+$`),
|
|
125
|
+
new RegExp(`^${escapedVitePrefix}\\/node_modules\\/.*`),
|
|
126
|
+
`^(?=${appPath}/**/.*/**)`,
|
|
127
|
+
`^(?=${sourcePath}/**/.*/**)`,
|
|
130
128
|
...pluginConfig.dev?.exclude || []
|
|
131
129
|
]
|
|
132
130
|
});
|
|
@@ -141,6 +139,35 @@ function reactRouterHonoServer(options = {}) {
|
|
|
141
139
|
console.error("Dev server plugin configureServer hook is not a function. This is likely a bug, I guess 😅\n");
|
|
142
140
|
throw new Error("Cannot apply dev server plugin configureServer hook");
|
|
143
141
|
}
|
|
142
|
+
},
|
|
143
|
+
configurePreviewServer(server) {
|
|
144
|
+
server.middlewares.use((req, res, next) => {
|
|
145
|
+
if (runtime === "cloudflare" || !isReactRouterBuildRequest() || !pluginConfig) {
|
|
146
|
+
next();
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
(async () => {
|
|
150
|
+
try {
|
|
151
|
+
previewAppPromise ??= import(pathToFileURL(path.resolve(pluginConfig.rootDirectory, pluginConfig.buildDirectory, "server/index.js")).href).then((module) => module.default);
|
|
152
|
+
const app = await previewAppPromise;
|
|
153
|
+
const headers = new Headers();
|
|
154
|
+
for (const [name, value] of Object.entries(req.headers)) for (const item of Array.isArray(value) ? value : [value]) if (item !== void 0) headers.append(name, item);
|
|
155
|
+
const request = new Request(`http://${req.headers.host ?? "localhost"}${req.url ?? "/"}`, {
|
|
156
|
+
method: req.method,
|
|
157
|
+
headers
|
|
158
|
+
});
|
|
159
|
+
const response = await app.fetch(request);
|
|
160
|
+
res.statusCode = response.status;
|
|
161
|
+
response.headers.forEach((value, name) => {
|
|
162
|
+
res.setHeader(name, value);
|
|
163
|
+
});
|
|
164
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error(error);
|
|
167
|
+
next(error);
|
|
168
|
+
}
|
|
169
|
+
})();
|
|
170
|
+
});
|
|
144
171
|
}
|
|
145
172
|
};
|
|
146
173
|
}
|
|
@@ -154,7 +181,7 @@ function resolvePluginConfig(config, options) {
|
|
|
154
181
|
const serverEntryPoint = options.serverEntryPoint || findDefaultServerEntry(appDirectory);
|
|
155
182
|
const serverBuildFile = reactRouterConfig.serverBuildFile;
|
|
156
183
|
const basename = reactRouterConfig.basename;
|
|
157
|
-
const
|
|
184
|
+
const viteBase = config.base ?? "/";
|
|
158
185
|
return {
|
|
159
186
|
rootDirectory,
|
|
160
187
|
buildDirectory,
|
|
@@ -165,11 +192,13 @@ function resolvePluginConfig(config, options) {
|
|
|
165
192
|
dev: options.dev,
|
|
166
193
|
serverBuildFile,
|
|
167
194
|
basename,
|
|
168
|
-
|
|
169
|
-
flag: isCloudflareOptions(options) ? options.flag : {}
|
|
195
|
+
viteBase
|
|
170
196
|
};
|
|
171
197
|
}
|
|
172
198
|
let warned = false;
|
|
199
|
+
function escapeRegExp(value) {
|
|
200
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
201
|
+
}
|
|
173
202
|
function findDefaultServerEntry(appDirectory) {
|
|
174
203
|
const fileWay = `${appDirectory}/server.ts`;
|
|
175
204
|
const folderWay = `${appDirectory}/server/index.ts`;
|
|
@@ -181,20 +210,5 @@ function findDefaultServerEntry(appDirectory) {
|
|
|
181
210
|
}
|
|
182
211
|
return VIRTUAL_MODULE_ID;
|
|
183
212
|
}
|
|
184
|
-
/**
|
|
185
|
-
* Retrieves the major React version from node_modules
|
|
186
|
-
*/
|
|
187
|
-
async function getReactVersion(pluginConfig) {
|
|
188
|
-
if (pluginConfig?.flag?.force_react_19) return 19;
|
|
189
|
-
const reactPackage = await import("react");
|
|
190
|
-
const version = reactPackage.default.version || reactPackage.version;
|
|
191
|
-
return Number.parseInt(version.split(".")[0], 10);
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Type guard to check if options is the Cloudflare variant
|
|
195
|
-
*/
|
|
196
|
-
function isCloudflareOptions(options) {
|
|
197
|
-
return options.runtime === "cloudflare";
|
|
198
|
-
}
|
|
199
213
|
//#endregion
|
|
200
214
|
export { reactRouterHonoServer };
|
|
@@ -4,12 +4,20 @@ const defaultWebSocket = {
|
|
|
4
4
|
upgradeWebSocket: (() => {}),
|
|
5
5
|
injectWebSocket: (server) => server
|
|
6
6
|
};
|
|
7
|
+
async function importNodeWebSocket(runtime, mode) {
|
|
8
|
+
try {
|
|
9
|
+
return await import("ws");
|
|
10
|
+
} catch (cause) {
|
|
11
|
+
throw new Error(`WebSocket support for the "${runtime}" runtime in ${mode} requires the optional "ws" peer dependency. Install "ws" before enabling useWebSocket.`, { cause });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
7
14
|
/**
|
|
8
15
|
* Create WebSocket factory
|
|
9
16
|
*
|
|
10
|
-
* It harmonizes the WebSocket implementation between
|
|
17
|
+
* It harmonizes the WebSocket implementation between supported runtimes.
|
|
11
18
|
*
|
|
12
|
-
*
|
|
19
|
+
* Node, Bun development, and Deno development use `@hono/node-server`.
|
|
20
|
+
* Cloudflare always uses Workerd's native implementation.
|
|
13
21
|
*
|
|
14
22
|
* **Implementation details: It will strip unused code from other runtimes at build time**
|
|
15
23
|
*
|
|
@@ -17,17 +25,30 @@ const defaultWebSocket = {
|
|
|
17
25
|
*/
|
|
18
26
|
async function createWebSocket({ app, enabled }) {
|
|
19
27
|
if (!enabled) return defaultWebSocket;
|
|
20
|
-
const
|
|
28
|
+
const mode = process.env.NODE_ENV === "development" ? "development" : "production";
|
|
29
|
+
const DEV = mode === "development";
|
|
21
30
|
const runtime = import.meta.env.REACT_ROUTER_HONO_SERVER_RUNTIME;
|
|
22
|
-
if (
|
|
23
|
-
const {
|
|
24
|
-
|
|
31
|
+
if (runtime === "cloudflare") {
|
|
32
|
+
const { upgradeWebSocket } = await import("hono/cloudflare-workers");
|
|
33
|
+
return {
|
|
34
|
+
upgradeWebSocket,
|
|
35
|
+
injectWebSocket: (server) => server
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (runtime === "node" || DEV && (runtime === "bun" || runtime === "deno")) {
|
|
39
|
+
const [{ createAdaptorServer, upgradeWebSocket }, { WebSocketServer }] = await Promise.all([import("@hono/node-server"), importNodeWebSocket(runtime, mode)]);
|
|
40
|
+
const websocket = { server: new WebSocketServer({ noServer: true }) };
|
|
25
41
|
return {
|
|
26
42
|
upgradeWebSocket,
|
|
27
43
|
injectWebSocket(server) {
|
|
28
|
-
|
|
44
|
+
createAdaptorServer({
|
|
45
|
+
fetch: app.fetch,
|
|
46
|
+
websocket,
|
|
47
|
+
createServer: (() => server)
|
|
48
|
+
});
|
|
29
49
|
return server;
|
|
30
|
-
}
|
|
50
|
+
},
|
|
51
|
+
nodeWebSocket: websocket
|
|
31
52
|
};
|
|
32
53
|
}
|
|
33
54
|
if (runtime === "bun") {
|
|
@@ -43,12 +64,31 @@ async function createWebSocket({ app, enabled }) {
|
|
|
43
64
|
}
|
|
44
65
|
};
|
|
45
66
|
}
|
|
67
|
+
if (runtime === "deno") {
|
|
68
|
+
const { upgradeWebSocket } = await import("hono/deno");
|
|
69
|
+
return {
|
|
70
|
+
upgradeWebSocket,
|
|
71
|
+
injectWebSocket: (server) => server
|
|
72
|
+
};
|
|
73
|
+
}
|
|
46
74
|
return defaultWebSocket;
|
|
47
75
|
}
|
|
48
76
|
/**
|
|
77
|
+
* Attach the Node WebSocket bridge to Vite's HTTP server without intercepting HMR.
|
|
78
|
+
*/
|
|
79
|
+
function attachWebSocketToVite(injectWebSocket, onServe) {
|
|
80
|
+
const httpServer = globalThis.__viteDevServer?.httpServer;
|
|
81
|
+
if (!httpServer) return false;
|
|
82
|
+
cleanUpgradeListeners(httpServer);
|
|
83
|
+
onServe?.(httpServer);
|
|
84
|
+
injectWebSocket(httpServer);
|
|
85
|
+
patchUpgradeListener(httpServer);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
49
89
|
* Clean all user-defined upgrade listeners, except HMR
|
|
50
90
|
*
|
|
51
|
-
* Avoid conflicts on
|
|
91
|
+
* Avoid conflicts on already-upgraded connections when using Node WebSockets in dev.
|
|
52
92
|
*
|
|
53
93
|
*/
|
|
54
94
|
function cleanUpgradeListeners(httpServer) {
|
|
@@ -87,10 +127,19 @@ function bindIncomingRequestSocketInfo() {
|
|
|
87
127
|
});
|
|
88
128
|
}
|
|
89
129
|
/**
|
|
130
|
+
* Prevent Chrome DevTools workspace discovery from reaching React Router's catch-all route.
|
|
131
|
+
*
|
|
132
|
+
* Public files and user-defined routes are registered first so applications can opt into
|
|
133
|
+
* automatic workspace discovery.
|
|
134
|
+
*/
|
|
135
|
+
function handleChromeDevToolsWorkspaceRequest(app) {
|
|
136
|
+
app.get("/.well-known/appspecific/com.chrome.devtools.json", (c) => c.notFound());
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
90
139
|
* Import React Router server build
|
|
91
140
|
*/
|
|
92
141
|
async function importBuild() {
|
|
93
|
-
return
|
|
142
|
+
return import("virtual:react-router/server-build");
|
|
94
143
|
}
|
|
95
144
|
/**
|
|
96
145
|
* Helper to create a getLoadContext function fully typed
|
|
@@ -105,4 +154,4 @@ function getBuildMode() {
|
|
|
105
154
|
return process.env.NODE_ENV === "development" ? "development" : "production";
|
|
106
155
|
}
|
|
107
156
|
//#endregion
|
|
108
|
-
export { getBuildMode as a, createWebSocket as i,
|
|
157
|
+
export { getBuildMode as a, createWebSocket as i, bindIncomingRequestSocketInfo as n, handleChromeDevToolsWorkspaceRequest as o, createGetLoadContext as r, importBuild as s, attachWebSocketToVite as t };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { ServerType } from "@hono/node-server";
|
|
2
2
|
import { Context, Env, Hono } from "hono";
|
|
3
|
-
import {
|
|
3
|
+
import { RouterContextProvider, ServerBuild } from "react-router";
|
|
4
4
|
import { UpgradeWebSocket } from "hono/ws";
|
|
5
|
-
|
|
5
|
+
import { WebSocketServer } from "ws";
|
|
6
6
|
//#region src/types/hono-server-options-base.d.ts
|
|
7
|
-
type ReactRouterHonoServerAppLoadContext =
|
|
7
|
+
type ReactRouterHonoServerAppLoadContext = RouterContextProvider;
|
|
8
8
|
interface HonoServerOptionsBase<E extends Env> {
|
|
9
9
|
/**
|
|
10
10
|
* The base Hono app to use as a replacement for the default one created automatically
|
|
@@ -28,27 +28,15 @@ interface HonoServerOptionsBase<E extends Env> {
|
|
|
28
28
|
*/
|
|
29
29
|
port?: number;
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
31
|
+
* Create the React Router load context.
|
|
32
32
|
*
|
|
33
|
-
*
|
|
33
|
+
* React Router 8 requires a `RouterContextProvider`. Define values with
|
|
34
|
+
* `createContext` and set them on the provider.
|
|
34
35
|
*
|
|
35
36
|
* ```ts
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
* whatever: string;
|
|
40
|
-
* }
|
|
41
|
-
* }
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* **To make the typing works correctly, in your `react-router.config.ts` or where you want, add future v8_middleware flag type to true.**
|
|
45
|
-
*
|
|
46
|
-
* ```ts
|
|
47
|
-
* declare module "react-router" {
|
|
48
|
-
* interface Future {
|
|
49
|
-
* v8_middleware: true; // 👈 Enable middleware types
|
|
50
|
-
* }
|
|
51
|
-
* }
|
|
37
|
+
* const context = new RouterContextProvider();
|
|
38
|
+
* context.set(userContext, user);
|
|
39
|
+
* return context;
|
|
52
40
|
* ```
|
|
53
41
|
*/
|
|
54
42
|
getLoadContext?: (c: Context<E>, options: {
|
|
@@ -62,11 +50,11 @@ interface HonoServerOptionsBase<E extends Env> {
|
|
|
62
50
|
*/
|
|
63
51
|
beforeAll?: (app: Hono<E>) => Promise<void> | void;
|
|
64
52
|
}
|
|
65
|
-
interface WithWebsocket<E extends Env> {
|
|
53
|
+
interface WithWebsocket<E extends Env, TUpgradeWebSocket = UpgradeWebSocket> {
|
|
66
54
|
/**
|
|
67
55
|
* Enable WebSockets support in `configure`
|
|
68
56
|
*
|
|
69
|
-
*
|
|
57
|
+
* Development uses the runtime's native implementation or the `@hono/node-server` bridge.
|
|
70
58
|
*
|
|
71
59
|
* Defaults to `false`
|
|
72
60
|
*/
|
|
@@ -77,14 +65,14 @@ interface WithWebsocket<E extends Env> {
|
|
|
77
65
|
* It is applied after the default middleware and before the React Router middleware
|
|
78
66
|
*/
|
|
79
67
|
configure: (app: Hono<E>, options: {
|
|
80
|
-
upgradeWebSocket:
|
|
68
|
+
upgradeWebSocket: TUpgradeWebSocket;
|
|
81
69
|
}) => Promise<void> | void;
|
|
82
70
|
}
|
|
83
71
|
interface WithoutWebsocket<E extends Env> {
|
|
84
72
|
/**
|
|
85
73
|
* Enable WebSockets support in `configure`
|
|
86
74
|
*
|
|
87
|
-
*
|
|
75
|
+
* Development uses the runtime's native implementation or the `@hono/node-server` bridge.
|
|
88
76
|
*
|
|
89
77
|
* Defaults to `false`
|
|
90
78
|
*/
|
package/dist/http.d.ts
CHANGED
package/dist/http.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
function reactRouterRedirect(location) {
|
|
10
10
|
return new Response(new ReadableStream({ start(controller) {
|
|
11
|
-
controller.enqueue(JSON.stringify([
|
|
11
|
+
controller.enqueue(new TextEncoder().encode(JSON.stringify([
|
|
12
12
|
["SingleFetchRedirect", 1],
|
|
13
13
|
{
|
|
14
14
|
_2: 3,
|
|
@@ -26,7 +26,7 @@ function reactRouterRedirect(location) {
|
|
|
26
26
|
"reload",
|
|
27
27
|
true,
|
|
28
28
|
"replace"
|
|
29
|
-
]));
|
|
29
|
+
])));
|
|
30
30
|
controller.close();
|
|
31
31
|
} }), {
|
|
32
32
|
status: 202,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/react-router-build-request.ts
|
|
2
|
+
/** Whether React Router is rendering a request as part of its build. */
|
|
3
|
+
function isReactRouterBuildRequest() {
|
|
4
|
+
return process.env.IS_RR_BUILD_REQUEST === "yes";
|
|
5
|
+
}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { isReactRouterBuildRequest as t };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/vite-public-path.ts
|
|
2
|
+
function classifyVitePublicPath(raw) {
|
|
3
|
+
if (raw === "" || raw === "./") return {
|
|
4
|
+
kind: "relative",
|
|
5
|
+
raw,
|
|
6
|
+
pathname: "/"
|
|
7
|
+
};
|
|
8
|
+
if (raw.startsWith("/")) {
|
|
9
|
+
const pathname = normalizeAbsolutePathname(raw);
|
|
10
|
+
return pathname === "/" ? {
|
|
11
|
+
kind: "root",
|
|
12
|
+
raw,
|
|
13
|
+
pathname
|
|
14
|
+
} : {
|
|
15
|
+
kind: "absolute",
|
|
16
|
+
raw,
|
|
17
|
+
pathname
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
return {
|
|
22
|
+
kind: "url",
|
|
23
|
+
raw,
|
|
24
|
+
pathname: normalizeAbsolutePathname(new URL(raw).pathname)
|
|
25
|
+
};
|
|
26
|
+
} catch {
|
|
27
|
+
return {
|
|
28
|
+
kind: "relative",
|
|
29
|
+
raw,
|
|
30
|
+
pathname: "/"
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function vitePathnamePrefix(publicPath) {
|
|
35
|
+
return publicPath.pathname === "/" ? "" : publicPath.pathname.slice(0, -1);
|
|
36
|
+
}
|
|
37
|
+
function viteGeneratedAssetsRoute(publicPath, assetsDir) {
|
|
38
|
+
return `${publicPath.kind === "absolute" ? vitePathnamePrefix(publicPath) : ""}/${assetsDir.replace(/^\/+|\/+$/g, "")}/*`;
|
|
39
|
+
}
|
|
40
|
+
function stripVitePathnamePrefix(pathname, publicPath) {
|
|
41
|
+
if (publicPath.kind !== "absolute" || publicPath.pathname === "/") return pathname;
|
|
42
|
+
const prefix = vitePathnamePrefix(publicPath);
|
|
43
|
+
if (pathname === prefix) return "/";
|
|
44
|
+
if (pathname.startsWith(`${prefix}/`)) return pathname.slice(prefix.length);
|
|
45
|
+
return pathname;
|
|
46
|
+
}
|
|
47
|
+
function composeViteAssetRewriteRequestPath(publicPath, userRewrite) {
|
|
48
|
+
return (requestPath, context) => {
|
|
49
|
+
const logicalPath = stripVitePathnamePrefix(requestPath, publicPath);
|
|
50
|
+
if (!userRewrite) return logicalPath;
|
|
51
|
+
return context === void 0 ? userRewrite(logicalPath) : userRewrite(logicalPath, context);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function normalizeAbsolutePathname(pathname) {
|
|
55
|
+
const withLeadingSlash = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
56
|
+
return withLeadingSlash === "/" ? "/" : `${withLeadingSlash.replace(/\/+$/, "")}/`;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { vitePathnamePrefix as a, viteGeneratedAssetsRoute as i, composeViteAssetRewriteRequestPath as n, stripVitePathnamePrefix as r, classifyVitePublicPath as t };
|