nuxt-gin-tools 0.2.13 → 0.2.14
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/package.json +1 -1
- package/src/nuxt-config.d.ts +2 -6
- package/src/nuxt-config.js +72 -16
package/package.json
CHANGED
package/src/nuxt-config.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { NuxtConfig } from "nuxt/config";
|
|
2
|
-
import type
|
|
2
|
+
import { type IncomingMessage, type ServerResponse } from "node:http";
|
|
3
|
+
import type { Socket } from "node:net";
|
|
3
4
|
export interface ServerConfigJson {
|
|
4
5
|
/** 前端基础 URL */
|
|
5
6
|
baseUrl: string;
|
|
@@ -9,11 +10,6 @@ export interface ServerConfigJson {
|
|
|
9
10
|
ginPort: number;
|
|
10
11
|
}
|
|
11
12
|
export interface MyNuxtConfig {
|
|
12
|
-
/**
|
|
13
|
-
* 开发期是否将访问 Nuxt 端口(baseUrl 下的页面请求)重定向到 Gin 端口。
|
|
14
|
-
* 默认 true。
|
|
15
|
-
*/
|
|
16
|
-
redirectNuxtToGinInDev?: boolean;
|
|
17
13
|
/** 服务器配置 */
|
|
18
14
|
serverConfig: ServerConfigJson;
|
|
19
15
|
}
|
package/src/nuxt-config.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createDefaultConfig = createDefaultConfig;
|
|
4
|
+
const node_http_1 = require("node:http");
|
|
4
5
|
function normalizePathPrefix(value) {
|
|
5
6
|
const withLeadingSlash = value.startsWith("/") ? value : `/${value}`;
|
|
6
7
|
if (withLeadingSlash !== "/" && withLeadingSlash.endsWith("/")) {
|
|
@@ -23,10 +24,64 @@ function isBaseUrlRequest(baseUrl, requestPath) {
|
|
|
23
24
|
}
|
|
24
25
|
return requestPath === baseUrl || requestPath.startsWith(`${baseUrl}/`);
|
|
25
26
|
}
|
|
27
|
+
function forwardToGin(ginPort, req, res) {
|
|
28
|
+
var _a;
|
|
29
|
+
const proxyReq = (0, node_http_1.request)({
|
|
30
|
+
hostname: "127.0.0.1",
|
|
31
|
+
port: ginPort,
|
|
32
|
+
path: (_a = req.url) !== null && _a !== void 0 ? _a : "/",
|
|
33
|
+
method: req.method,
|
|
34
|
+
headers: Object.assign(Object.assign({}, req.headers), { host: `127.0.0.1:${ginPort}` }),
|
|
35
|
+
}, (proxyRes) => {
|
|
36
|
+
var _a;
|
|
37
|
+
res.writeHead((_a = proxyRes.statusCode) !== null && _a !== void 0 ? _a : 502, proxyRes.headers);
|
|
38
|
+
proxyRes.pipe(res);
|
|
39
|
+
});
|
|
40
|
+
proxyReq.on("error", (error) => {
|
|
41
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
42
|
+
res.statusCode = 502;
|
|
43
|
+
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
44
|
+
res.end(JSON.stringify({ error: "Bad Gateway", message }));
|
|
45
|
+
});
|
|
46
|
+
req.pipe(proxyReq);
|
|
47
|
+
}
|
|
48
|
+
function forwardWebSocketToGin(ginPort, req, clientSocket, clientHead) {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
const proxyReq = (0, node_http_1.request)({
|
|
51
|
+
hostname: "127.0.0.1",
|
|
52
|
+
port: ginPort,
|
|
53
|
+
path: (_a = req.url) !== null && _a !== void 0 ? _a : "/",
|
|
54
|
+
method: (_b = req.method) !== null && _b !== void 0 ? _b : "GET",
|
|
55
|
+
headers: Object.assign(Object.assign({}, req.headers), { host: `127.0.0.1:${ginPort}`, connection: "Upgrade" }),
|
|
56
|
+
});
|
|
57
|
+
proxyReq.on("upgrade", (proxyRes, proxySocket, proxyHead) => {
|
|
58
|
+
var _a, _b;
|
|
59
|
+
const statusLine = `HTTP/1.1 ${(_a = proxyRes.statusCode) !== null && _a !== void 0 ? _a : 101} ${(_b = proxyRes.statusMessage) !== null && _b !== void 0 ? _b : "Switching Protocols"}\r\n`;
|
|
60
|
+
const headers = proxyRes.rawHeaders;
|
|
61
|
+
let headerText = "";
|
|
62
|
+
for (let i = 0; i < headers.length; i += 2) {
|
|
63
|
+
headerText += `${headers[i]}: ${headers[i + 1]}\r\n`;
|
|
64
|
+
}
|
|
65
|
+
clientSocket.write(`${statusLine}${headerText}\r\n`);
|
|
66
|
+
if (clientHead.length > 0) {
|
|
67
|
+
proxySocket.write(clientHead);
|
|
68
|
+
}
|
|
69
|
+
if (proxyHead.length > 0) {
|
|
70
|
+
clientSocket.write(proxyHead);
|
|
71
|
+
}
|
|
72
|
+
proxySocket.pipe(clientSocket).pipe(proxySocket);
|
|
73
|
+
});
|
|
74
|
+
proxyReq.on("error", () => {
|
|
75
|
+
if (!clientSocket.destroyed) {
|
|
76
|
+
clientSocket.destroy();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
proxyReq.end();
|
|
80
|
+
}
|
|
26
81
|
/**
|
|
27
82
|
* 创建默认的 Nuxt 配置。
|
|
28
83
|
*/
|
|
29
|
-
function createDefaultConfig({ serverConfig,
|
|
84
|
+
function createDefaultConfig({ serverConfig, }) {
|
|
30
85
|
const baseUrl = normalizeBaseUrl(serverConfig.baseUrl);
|
|
31
86
|
return {
|
|
32
87
|
buildDir: "vue/.nuxt",
|
|
@@ -55,27 +110,28 @@ function createDefaultConfig({ serverConfig, redirectNuxtToGinInDev = true, }) {
|
|
|
55
110
|
server: {},
|
|
56
111
|
plugins: [
|
|
57
112
|
{
|
|
58
|
-
name: "nuxt-gin-base-url-
|
|
113
|
+
name: "nuxt-gin-base-url-proxy",
|
|
59
114
|
configureServer(server) {
|
|
60
115
|
server.middlewares.use((req, res, next) => {
|
|
61
|
-
var _a
|
|
62
|
-
const
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
req.headers.accept.includes("text/html");
|
|
67
|
-
if (redirectNuxtToGinInDev === true &&
|
|
68
|
-
!isViteInternalRequest(requestPath) &&
|
|
69
|
-
isBaseUrlRequest(baseUrl, requestPath) &&
|
|
70
|
-
(method === "GET" || method === "HEAD") &&
|
|
71
|
-
acceptsHtml) {
|
|
72
|
-
res.statusCode = 307;
|
|
73
|
-
res.setHeader("Location", `http://127.0.0.1:${serverConfig.ginPort}${requestUrl}`);
|
|
74
|
-
res.end();
|
|
116
|
+
var _a;
|
|
117
|
+
const requestPath = ((_a = req.url) !== null && _a !== void 0 ? _a : "/").split("?")[0] || "/";
|
|
118
|
+
const shouldForward = !isViteInternalRequest(requestPath) && !isBaseUrlRequest(baseUrl, requestPath);
|
|
119
|
+
if (shouldForward) {
|
|
120
|
+
forwardToGin(serverConfig.ginPort, req, res);
|
|
75
121
|
return;
|
|
76
122
|
}
|
|
77
123
|
next();
|
|
78
124
|
});
|
|
125
|
+
if (server.httpServer) {
|
|
126
|
+
server.httpServer.on("upgrade", (req, socket, head) => {
|
|
127
|
+
var _a;
|
|
128
|
+
const requestPath = ((_a = req.url) !== null && _a !== void 0 ? _a : "/").split("?")[0] || "/";
|
|
129
|
+
const shouldForward = !isViteInternalRequest(requestPath) && !isBaseUrlRequest(baseUrl, requestPath);
|
|
130
|
+
if (shouldForward) {
|
|
131
|
+
forwardWebSocketToGin(serverConfig.ginPort, req, socket, head);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
79
135
|
},
|
|
80
136
|
},
|
|
81
137
|
],
|