@tourmind-frontend/monitor-plugin-nuxt 1.4.1 → 1.5.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/dist/index.cjs +76 -39
- package/dist/index.d.ts +1 -1
- 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+))?\)?/;
|
|
@@ -106,7 +147,7 @@ function MonitorModule(options) {
|
|
|
106
147
|
clientKey: options.clientKey
|
|
107
148
|
}
|
|
108
149
|
});
|
|
109
|
-
const
|
|
150
|
+
const serverReportUrl = `${options.url.replace(/\/+$/, "")}/api/server-report`;
|
|
110
151
|
const clientKey = options.clientKey;
|
|
111
152
|
const commit = options.commit;
|
|
112
153
|
const publicUrlBase = (options.publicUrl || "").replace(/\/+$/, "") || null;
|
|
@@ -114,46 +155,42 @@ function MonitorModule(options) {
|
|
|
114
155
|
this.nuxt.hook("render:errorMiddleware", (app) => {
|
|
115
156
|
app.use((err, req, _res, next) => {
|
|
116
157
|
var _a;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
fullUrl = `${proto}://${host}${path.startsWith("/") ? path : `/${path}`}`;
|
|
141
|
-
}
|
|
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}`}`;
|
|
142
181
|
}
|
|
143
|
-
fetch(ssrReportUrl, {
|
|
144
|
-
method: "POST",
|
|
145
|
-
headers: { "Content-Type": "application/json" },
|
|
146
|
-
body: JSON.stringify({
|
|
147
|
-
token: clientKey,
|
|
148
|
-
stack: refinedStack,
|
|
149
|
-
url: fullUrl,
|
|
150
|
-
commit,
|
|
151
|
-
...sourceCtx || {}
|
|
152
|
-
})
|
|
153
|
-
}).catch(() => {
|
|
154
|
-
});
|
|
155
|
-
} catch {
|
|
156
182
|
}
|
|
183
|
+
postJson(
|
|
184
|
+
serverReportUrl,
|
|
185
|
+
JSON.stringify({
|
|
186
|
+
token: clientKey,
|
|
187
|
+
stack: refinedStack,
|
|
188
|
+
url: fullUrl,
|
|
189
|
+
commit,
|
|
190
|
+
...sourceCtx || {}
|
|
191
|
+
})
|
|
192
|
+
);
|
|
193
|
+
} catch {
|
|
157
194
|
}
|
|
158
195
|
next(err);
|
|
159
196
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export interface MonitorModuleOptions {
|
|
|
3
3
|
clientKey: string;
|
|
4
4
|
/** monitor 项目 auth token(前缀 `fm_at_`),仅供构建机上传 sourcemap 使用。 */
|
|
5
5
|
authToken: string;
|
|
6
|
-
/** monitor 服务的 base URL,例如 `https://monitor.example.com`。`/api/report`、`/api/upload` 由下游 tracking / plugin-webpack
|
|
6
|
+
/** monitor 服务的 base URL,例如 `https://monitor.example.com`。`/api/report`、`/api/upload` 由下游 tracking / plugin-webpack 内部拼接,`/api/server-report` 由本模块拼接。 */
|
|
7
7
|
url: string;
|
|
8
8
|
/** Commit hash,由调用方自行解析(如 `git rev-parse HEAD`)。 */
|
|
9
9
|
commit: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tourmind-frontend/monitor-plugin-nuxt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
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",
|