@yysports/tools 3.1.0 → 3.1.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/dist/Track/Log.js +26 -13
- package/dist/Utils/file.js +9 -2
- package/dist/Utils/route.js +9 -8
- package/lib/Track/Log.js +13 -0
- package/lib/Utils/file.js +8 -2
- package/lib/Utils/route.js +7 -8
- package/package.json +2 -2
package/dist/Track/Log.js
CHANGED
|
@@ -6,6 +6,7 @@ import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
|
6
6
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
7
7
|
import { TRACK_CONTENT_TYPE, TRACK_LIMITS } from "../interfaces/protocol";
|
|
8
8
|
import Beacon from "./Beacon";
|
|
9
|
+
import Config from "./Config";
|
|
9
10
|
import { buildEnvelopeBase, buildEventBase, trackUrl, truncate } from "./envelope";
|
|
10
11
|
import Replay from "./Replay";
|
|
11
12
|
|
|
@@ -70,6 +71,11 @@ var Log = /*#__PURE__*/function () {
|
|
|
70
71
|
}, {
|
|
71
72
|
key: "eventLog",
|
|
72
73
|
value: function eventLog(content) {
|
|
74
|
+
// 非字符串会被原样入队,服务端校验失败会连坐同批;与超长处理一致,丢弃并告警
|
|
75
|
+
if (typeof content !== 'string') {
|
|
76
|
+
console.warn('[Track] eventLog 的 content 不是字符串(服务端会整批 400),已丢弃该条日志');
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
73
79
|
if (this.isContentOverlong(content)) return;
|
|
74
80
|
Beacon.push(_objectSpread(_objectSpread({}, buildEventBase()), {}, {
|
|
75
81
|
type: 'event',
|
|
@@ -100,13 +106,20 @@ var Log = /*#__PURE__*/function () {
|
|
|
100
106
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
101
107
|
while (1) switch (_context.prev = _context.next) {
|
|
102
108
|
case 0:
|
|
103
|
-
if (
|
|
109
|
+
if (Config.data.appId) {
|
|
104
110
|
_context.next = 3;
|
|
105
111
|
break;
|
|
106
112
|
}
|
|
107
|
-
console.warn('[Track]
|
|
113
|
+
console.warn('[Track] 尚未调用 Track.init(appId 为空),该条错误上报已丢弃');
|
|
108
114
|
return _context.abrupt("return");
|
|
109
115
|
case 3:
|
|
116
|
+
if (!(typeof message !== 'string' || !message.trim())) {
|
|
117
|
+
_context.next = 6;
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
console.warn('[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报');
|
|
121
|
+
return _context.abrupt("return");
|
|
122
|
+
case 6:
|
|
110
123
|
replay = Replay.snapshot();
|
|
111
124
|
pureError = _objectSpread(_objectSpread({}, buildEnvelopeBase()), {}, {
|
|
112
125
|
error: _objectSpread(_objectSpread({}, buildEventBase()), {}, {
|
|
@@ -118,8 +131,8 @@ var Log = /*#__PURE__*/function () {
|
|
|
118
131
|
payload = replay.length ? _objectSpread(_objectSpread({}, pureError), {}, {
|
|
119
132
|
replay: replay
|
|
120
133
|
}) : pureError;
|
|
121
|
-
_context.prev =
|
|
122
|
-
_context.next =
|
|
134
|
+
_context.prev = 9;
|
|
135
|
+
_context.next = 12;
|
|
123
136
|
return fetch(trackUrl('/error'), {
|
|
124
137
|
method: 'POST',
|
|
125
138
|
headers: {
|
|
@@ -127,21 +140,21 @@ var Log = /*#__PURE__*/function () {
|
|
|
127
140
|
},
|
|
128
141
|
body: JSON.stringify(payload)
|
|
129
142
|
});
|
|
130
|
-
case
|
|
143
|
+
case 12:
|
|
131
144
|
res = _context.sent;
|
|
132
145
|
if (res.ok) {
|
|
133
|
-
_context.next =
|
|
146
|
+
_context.next = 15;
|
|
134
147
|
break;
|
|
135
148
|
}
|
|
136
149
|
throw new Error("HTTP ".concat(res.status));
|
|
137
|
-
case
|
|
150
|
+
case 15:
|
|
138
151
|
// 上报成功后才移除已携带的回放事件,继续录制(不 stop)
|
|
139
152
|
Replay.commitSnapshot(replay);
|
|
140
|
-
_context.next =
|
|
153
|
+
_context.next = 21;
|
|
141
154
|
break;
|
|
142
|
-
case
|
|
143
|
-
_context.prev =
|
|
144
|
-
_context.t0 = _context["catch"](
|
|
155
|
+
case 18:
|
|
156
|
+
_context.prev = 18;
|
|
157
|
+
_context.t0 = _context["catch"](9);
|
|
145
158
|
try {
|
|
146
159
|
(_navigator = navigator) === null || _navigator === void 0 || (_navigator$sendBeacon = _navigator.sendBeacon) === null || _navigator$sendBeacon === void 0 || _navigator$sendBeacon.call(_navigator, trackUrl('/error'), new Blob([JSON.stringify(pureError)], {
|
|
147
160
|
type: TRACK_CONTENT_TYPE
|
|
@@ -149,11 +162,11 @@ var Log = /*#__PURE__*/function () {
|
|
|
149
162
|
} catch (beaconError) {
|
|
150
163
|
// beacon 也不可用(如页面已卸载),放弃本次错误上报,不阻断业务
|
|
151
164
|
}
|
|
152
|
-
case
|
|
165
|
+
case 21:
|
|
153
166
|
case "end":
|
|
154
167
|
return _context.stop();
|
|
155
168
|
}
|
|
156
|
-
}, _callee, this, [[
|
|
169
|
+
}, _callee, this, [[9, 18]]);
|
|
157
170
|
}));
|
|
158
171
|
function errorLog(_x, _x2) {
|
|
159
172
|
return _errorLog.apply(this, arguments);
|
package/dist/Utils/file.js
CHANGED
|
@@ -35,14 +35,21 @@ export var checkFileType = function checkFileType(file, accept) {
|
|
|
35
35
|
* @param file File 图片文件对象
|
|
36
36
|
*/
|
|
37
37
|
export var getImageResolution = function getImageResolution(file) {
|
|
38
|
-
return new Promise(function (resolve) {
|
|
38
|
+
return new Promise(function (resolve, reject) {
|
|
39
39
|
var img = new Image();
|
|
40
|
-
|
|
40
|
+
var url = URL.createObjectURL(file);
|
|
41
41
|
img.onload = function () {
|
|
42
|
+
URL.revokeObjectURL(url);
|
|
42
43
|
resolve({
|
|
43
44
|
width: img.width,
|
|
44
45
|
height: img.height
|
|
45
46
|
});
|
|
46
47
|
};
|
|
48
|
+
// 不处理 error 会让 Promise 永远挂起,调用方的 await 无法结束
|
|
49
|
+
img.onerror = function () {
|
|
50
|
+
URL.revokeObjectURL(url);
|
|
51
|
+
reject(new Error('getImageResolution: 图片加载失败'));
|
|
52
|
+
};
|
|
53
|
+
img.src = url;
|
|
47
54
|
});
|
|
48
55
|
};
|
package/dist/Utils/route.js
CHANGED
|
@@ -9,10 +9,11 @@ export var getModuleNameByUmi = function getModuleNameByUmi(onRouteChangeArgs, m
|
|
|
9
9
|
routes = onRouteChangeArgs.routes,
|
|
10
10
|
basename = onRouteChangeArgs.basename;
|
|
11
11
|
var pathname = location.pathname;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
// 只去前缀:裸 replace 会把路径中段出现的 basename 一并替换掉;
|
|
13
|
+
// 末尾斜杠先规整,且要求是完整路径段,避免 /app 误匹配 /application
|
|
14
|
+
var basePath = basename.replace(/\/+$/, '');
|
|
15
|
+
var isUnderBase = basePath !== '' && basePath !== '/' && (pathname === basePath || pathname.startsWith("".concat(basePath, "/")));
|
|
16
|
+
var routePath = isUnderBase ? pathname.slice(basePath.length) || '/' : pathname;
|
|
16
17
|
var currentRouteKey = Object.keys(routes).find(function (v) {
|
|
17
18
|
return routes[v].path === routePath;
|
|
18
19
|
});
|
|
@@ -43,14 +44,14 @@ export var getModuleNameByUmi = function getModuleNameByUmi(onRouteChangeArgs, m
|
|
|
43
44
|
}
|
|
44
45
|
|
|
45
46
|
// 构建完整的菜单显示名称数组
|
|
47
|
+
var nameMap = menuNameMap !== null && menuNameMap !== void 0 ? menuNameMap : {};
|
|
46
48
|
var menuDisplayNames = [];
|
|
47
49
|
for (var i = 1; i < menuHierarchyKeys.length; i++) {
|
|
48
50
|
var menuKey = menuHierarchyKeys.slice(0, i + 1).join('.');
|
|
49
|
-
var displayName =
|
|
51
|
+
var displayName = nameMap[menuKey];
|
|
52
|
+
// 缺映射时整段不可用:拼进 "undefined" 会让 visitLog 记成脏模块名
|
|
53
|
+
if (!displayName) return;
|
|
50
54
|
menuDisplayNames.push(displayName);
|
|
51
55
|
}
|
|
52
|
-
if (menuDisplayNames.length === 0) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
56
|
return menuDisplayNames.join('-');
|
|
56
57
|
};
|
package/lib/Track/Log.js
CHANGED
|
@@ -34,6 +34,7 @@ __export(Log_exports, {
|
|
|
34
34
|
module.exports = __toCommonJS(Log_exports);
|
|
35
35
|
var import_protocol = require("../interfaces/protocol");
|
|
36
36
|
var import_Beacon = __toESM(require("./Beacon"));
|
|
37
|
+
var import_Config = __toESM(require("./Config"));
|
|
37
38
|
var import_envelope = require("./envelope");
|
|
38
39
|
var import_Replay = __toESM(require("./Replay"));
|
|
39
40
|
var Log = class {
|
|
@@ -80,6 +81,12 @@ var Log = class {
|
|
|
80
81
|
});
|
|
81
82
|
}
|
|
82
83
|
static eventLog(content) {
|
|
84
|
+
if (typeof content !== "string") {
|
|
85
|
+
console.warn(
|
|
86
|
+
"[Track] eventLog 的 content 不是字符串(服务端会整批 400),已丢弃该条日志"
|
|
87
|
+
);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
83
90
|
if (this.isContentOverlong(content))
|
|
84
91
|
return;
|
|
85
92
|
import_Beacon.default.push({
|
|
@@ -104,6 +111,12 @@ var Log = class {
|
|
|
104
111
|
*/
|
|
105
112
|
static async errorLog(message, stack) {
|
|
106
113
|
var _a;
|
|
114
|
+
if (!import_Config.default.data.appId) {
|
|
115
|
+
console.warn(
|
|
116
|
+
"[Track] 尚未调用 Track.init(appId 为空),该条错误上报已丢弃"
|
|
117
|
+
);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
107
120
|
if (typeof message !== "string" || !message.trim()) {
|
|
108
121
|
console.warn(
|
|
109
122
|
"[Track] errorLog 的 message 为空(服务端会整条 400),已丢弃该条错误上报"
|
package/lib/Utils/file.js
CHANGED
|
@@ -44,12 +44,18 @@ var checkFileType = (file, accept) => {
|
|
|
44
44
|
return true;
|
|
45
45
|
};
|
|
46
46
|
var getImageResolution = (file) => {
|
|
47
|
-
return new Promise((resolve) => {
|
|
47
|
+
return new Promise((resolve, reject) => {
|
|
48
48
|
const img = new Image();
|
|
49
|
-
|
|
49
|
+
const url = URL.createObjectURL(file);
|
|
50
50
|
img.onload = () => {
|
|
51
|
+
URL.revokeObjectURL(url);
|
|
51
52
|
resolve({ width: img.width, height: img.height });
|
|
52
53
|
};
|
|
54
|
+
img.onerror = () => {
|
|
55
|
+
URL.revokeObjectURL(url);
|
|
56
|
+
reject(new Error("getImageResolution: 图片加载失败"));
|
|
57
|
+
};
|
|
58
|
+
img.src = url;
|
|
53
59
|
});
|
|
54
60
|
};
|
|
55
61
|
// Annotate the CommonJS export names for ESM import in node:
|
package/lib/Utils/route.js
CHANGED
|
@@ -25,10 +25,9 @@ module.exports = __toCommonJS(route_exports);
|
|
|
25
25
|
var getModuleNameByUmi = (onRouteChangeArgs, menuNameMap) => {
|
|
26
26
|
const { location, routes, basename } = onRouteChangeArgs;
|
|
27
27
|
const { pathname } = location;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
28
|
+
const basePath = basename.replace(/\/+$/, "");
|
|
29
|
+
const isUnderBase = basePath !== "" && basePath !== "/" && (pathname === basePath || pathname.startsWith(`${basePath}/`));
|
|
30
|
+
const routePath = isUnderBase ? pathname.slice(basePath.length) || "/" : pathname;
|
|
32
31
|
const currentRouteKey = Object.keys(routes).find(
|
|
33
32
|
(v) => routes[v].path === routePath
|
|
34
33
|
);
|
|
@@ -49,15 +48,15 @@ var getModuleNameByUmi = (onRouteChangeArgs, menuNameMap) => {
|
|
|
49
48
|
if (menuHierarchyKeys.length === 1) {
|
|
50
49
|
return;
|
|
51
50
|
}
|
|
51
|
+
const nameMap = menuNameMap ?? {};
|
|
52
52
|
const menuDisplayNames = [];
|
|
53
53
|
for (let i = 1; i < menuHierarchyKeys.length; i++) {
|
|
54
54
|
const menuKey = menuHierarchyKeys.slice(0, i + 1).join(".");
|
|
55
|
-
const displayName =
|
|
55
|
+
const displayName = nameMap[menuKey];
|
|
56
|
+
if (!displayName)
|
|
57
|
+
return;
|
|
56
58
|
menuDisplayNames.push(displayName);
|
|
57
59
|
}
|
|
58
|
-
if (menuDisplayNames.length === 0) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
60
|
return menuDisplayNames.join("-");
|
|
62
61
|
};
|
|
63
62
|
// Annotate the CommonJS export names for ESM import in node:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yysports/tools",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "常用工具函数库",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"author": "daiwenyu",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"lint:es": "eslint \"{src,test}/**/*.{js,jsx,ts,tsx}\"",
|
|
27
27
|
"prepare": "husky && dumi setup",
|
|
28
28
|
"prepublishOnly": "father doctor && npm run build && npm test",
|
|
29
|
-
"release": "npm run build && npm publish --access public --registry https://registry.npmjs.org",
|
|
29
|
+
"release": "npm run build && npm login --registry https://registry.npmjs.org && npm publish --access public --registry https://registry.npmjs.org",
|
|
30
30
|
"start": "npm run dev",
|
|
31
31
|
"test": "vitest run"
|
|
32
32
|
},
|