nuxt-gin-tools 0.2.12 → 0.2.13

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-gin-tools",
3
- "version": "0.2.12",
3
+ "version": "0.2.13",
4
4
  "description": "This project is used as a dependency for [nuxt-gin-starter](https://github.com/RapboyGao/nuxt-gin-starter.git)",
5
5
  "bin": {
6
6
  "nuxt-gin": "index.js"
@@ -1,6 +1,5 @@
1
1
  import type { NuxtConfig } from "nuxt/config";
2
- import { type IncomingMessage, type ServerResponse } from "node:http";
3
- import type { Socket } from "node:net";
2
+ import type { IncomingMessage, ServerResponse } from "node:http";
4
3
  export interface ServerConfigJson {
5
4
  /** 前端基础 URL */
6
5
  baseUrl: string;
@@ -10,6 +9,11 @@ export interface ServerConfigJson {
10
9
  ginPort: number;
11
10
  }
12
11
  export interface MyNuxtConfig {
12
+ /**
13
+ * 开发期是否将访问 Nuxt 端口(baseUrl 下的页面请求)重定向到 Gin 端口。
14
+ * 默认 true。
15
+ */
16
+ redirectNuxtToGinInDev?: boolean;
13
17
  /** 服务器配置 */
14
18
  serverConfig: ServerConfigJson;
15
19
  }
@@ -1,7 +1,6 @@
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");
5
4
  function normalizePathPrefix(value) {
6
5
  const withLeadingSlash = value.startsWith("/") ? value : `/${value}`;
7
6
  if (withLeadingSlash !== "/" && withLeadingSlash.endsWith("/")) {
@@ -24,64 +23,10 @@ function isBaseUrlRequest(baseUrl, requestPath) {
24
23
  }
25
24
  return requestPath === baseUrl || requestPath.startsWith(`${baseUrl}/`);
26
25
  }
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
- }
81
26
  /**
82
27
  * 创建默认的 Nuxt 配置。
83
28
  */
84
- function createDefaultConfig({ serverConfig, }) {
29
+ function createDefaultConfig({ serverConfig, redirectNuxtToGinInDev = true, }) {
85
30
  const baseUrl = normalizeBaseUrl(serverConfig.baseUrl);
86
31
  return {
87
32
  buildDir: "vue/.nuxt",
@@ -110,28 +55,27 @@ function createDefaultConfig({ serverConfig, }) {
110
55
  server: {},
111
56
  plugins: [
112
57
  {
113
- name: "nuxt-gin-base-url-proxy",
58
+ name: "nuxt-gin-base-url-redirect",
114
59
  configureServer(server) {
115
60
  server.middlewares.use((req, res, next) => {
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);
61
+ var _a, _b;
62
+ const requestUrl = (_a = req.url) !== null && _a !== void 0 ? _a : "/";
63
+ const requestPath = requestUrl.split("?")[0] || "/";
64
+ const method = (_b = req.method) !== null && _b !== void 0 ? _b : "GET";
65
+ const acceptsHtml = typeof req.headers.accept === "string" &&
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();
121
75
  return;
122
76
  }
123
77
  next();
124
78
  });
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
- }
135
79
  },
136
80
  },
137
81
  ],