@tourmind-frontend/monitor-plugin-nuxt 1.4.0 → 1.4.2
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/dist/index.cjs +78 -23
- package/dist/index.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -35,7 +35,48 @@ __export(src_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(src_exports);
|
|
36
36
|
var import_node_path = require("path");
|
|
37
37
|
var import_node_fs = require("fs");
|
|
38
|
+
var import_node_http = require("http");
|
|
39
|
+
var import_node_https = require("https");
|
|
40
|
+
var import_node_url = require("url");
|
|
38
41
|
var import_monitor_plugin_webpack = __toESM(require("@tourmind-frontend/monitor-plugin-webpack"));
|
|
42
|
+
function postJson(targetUrl, body) {
|
|
43
|
+
if (typeof fetch === "function") {
|
|
44
|
+
try {
|
|
45
|
+
void fetch(targetUrl, {
|
|
46
|
+
method: "POST",
|
|
47
|
+
headers: { "Content-Type": "application/json" },
|
|
48
|
+
body
|
|
49
|
+
}).catch(() => {
|
|
50
|
+
});
|
|
51
|
+
} catch {
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const u = new import_node_url.URL(targetUrl);
|
|
57
|
+
const isHttps = u.protocol === "https:";
|
|
58
|
+
const req = (isHttps ? import_node_https.request : import_node_http.request)(
|
|
59
|
+
{
|
|
60
|
+
method: "POST",
|
|
61
|
+
hostname: u.hostname,
|
|
62
|
+
port: u.port || (isHttps ? 443 : 80),
|
|
63
|
+
path: `${u.pathname}${u.search}`,
|
|
64
|
+
headers: {
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"Content-Length": Buffer.byteLength(body)
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
(res) => {
|
|
70
|
+
res.resume();
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
req.on("error", () => {
|
|
74
|
+
});
|
|
75
|
+
req.write(body);
|
|
76
|
+
req.end();
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
39
80
|
var LOG_PREFIX = "[frontend-monitor]";
|
|
40
81
|
function extractFirstFrame(stack) {
|
|
41
82
|
const re = /\(?([^()\s][^():\n]*?\.(?:vue|ts|tsx|js|jsx|mjs)):(\d+)(?::(\d+))?\)?/;
|
|
@@ -109,33 +150,47 @@ function MonitorModule(options) {
|
|
|
109
150
|
const ssrReportUrl = `${options.url.replace(/\/+$/, "")}/api/ssr-report`;
|
|
110
151
|
const clientKey = options.clientKey;
|
|
111
152
|
const commit = options.commit;
|
|
153
|
+
const publicUrlBase = (options.publicUrl || "").replace(/\/+$/, "") || null;
|
|
112
154
|
const rootDir = this.options.rootDir || this.options.srcDir || process.cwd();
|
|
113
155
|
this.nuxt.hook("render:errorMiddleware", (app) => {
|
|
114
156
|
app.use((err, req, _res, next) => {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
157
|
+
var _a;
|
|
158
|
+
try {
|
|
159
|
+
const e = err;
|
|
160
|
+
const stack = e && e.stack ? e.stack : String(e && e.message || e);
|
|
161
|
+
const frame = extractFirstFrame(stack);
|
|
162
|
+
const sourceCtx = frame ? readSourceContext(rootDir, frame.file, frame.line, frame.col) : null;
|
|
163
|
+
const refinedStack = sourceCtx && frame && sourceCtx.line !== frame.line ? stack.replace(
|
|
164
|
+
new RegExp(`(${frame.file.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}:)${frame.line}(:)`),
|
|
165
|
+
`$1${sourceCtx.line}$2`
|
|
166
|
+
) : stack;
|
|
167
|
+
const path = req.url || "";
|
|
168
|
+
let fullUrl = path;
|
|
169
|
+
if (publicUrlBase) {
|
|
170
|
+
fullUrl = `${publicUrlBase}${path.startsWith("/") ? path : `/${path}`}`;
|
|
171
|
+
} else {
|
|
172
|
+
const headers = req && req.headers || {};
|
|
173
|
+
const headerValue = (key) => {
|
|
174
|
+
const v = headers[key];
|
|
175
|
+
return Array.isArray(v) ? v[0] : v;
|
|
176
|
+
};
|
|
177
|
+
const host = headerValue("x-forwarded-host") || headerValue("host");
|
|
178
|
+
if (host) {
|
|
179
|
+
const proto = headerValue("x-forwarded-proto") || (((_a = req.connection) == null ? void 0 : _a.encrypted) ? "https" : "http");
|
|
180
|
+
fullUrl = `${proto}://${host}${path.startsWith("/") ? path : `/${path}`}`;
|
|
181
|
+
}
|
|
138
182
|
}
|
|
183
|
+
postJson(
|
|
184
|
+
ssrReportUrl,
|
|
185
|
+
JSON.stringify({
|
|
186
|
+
token: clientKey,
|
|
187
|
+
stack: refinedStack,
|
|
188
|
+
url: fullUrl,
|
|
189
|
+
commit,
|
|
190
|
+
...sourceCtx || {}
|
|
191
|
+
})
|
|
192
|
+
);
|
|
193
|
+
} catch {
|
|
139
194
|
}
|
|
140
195
|
next(err);
|
|
141
196
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,12 @@ export interface MonitorModuleOptions {
|
|
|
7
7
|
url: string;
|
|
8
8
|
/** Commit hash,由调用方自行解析(如 `git rev-parse HEAD`)。 */
|
|
9
9
|
commit: string;
|
|
10
|
+
/**
|
|
11
|
+
* 用户 app 的对外访问 URL(如 `https://app.example.com`),仅用于 SSR 错误上报里把 `req.url`(path)补成完整地址。
|
|
12
|
+
* 不配则按以下顺序探测:`X-Forwarded-Proto + X-Forwarded-Host` → `req.connection.encrypted + req.headers.host`。
|
|
13
|
+
* Docker / k8s 部署若反代未透传 `X-Forwarded-*` header,强烈建议显式配置此项。
|
|
14
|
+
*/
|
|
15
|
+
publicUrl?: string;
|
|
10
16
|
}
|
|
11
17
|
interface NuxtBuildContext {
|
|
12
18
|
isClient: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tourmind-frontend/monitor-plugin-nuxt",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.2",
|
|
4
4
|
"description": "Nuxt 2 module that captures runtime errors and uploads sourcemaps to a frontend-monitor server.",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|