ranuts 0.1.0-alpha → 0.1.0-alpha-22
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/LICENSE +21 -0
- package/dist/build/build.es.d.ts +2 -0
- package/dist/build/build.umd.d.ts +2 -0
- package/dist/build/build.umd.ml.d.ts +2 -0
- package/dist/build/build.umd.node.d.ts +2 -0
- package/dist/build/build.umd.react.d.ts +2 -0
- package/dist/build/build.umd.utils.d.ts +2 -0
- package/dist/build/build.umd.wasm.d.ts +2 -0
- package/dist/color-48RIDBG4.js +82262 -0
- package/dist/examples/clone-deep-example.d.ts +1 -0
- package/dist/examples/is-equal-example.d.ts +1 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.js +151 -84
- package/dist/plugins/vite-plugins-banner.d.ts +2 -0
- package/dist/reactify-Z-V9Vblb.js +83 -0
- package/dist/src/arithmetic/index.d.ts +186 -0
- package/dist/src/cache/expires.d.ts +1 -0
- package/dist/src/ml/index.d.ts +2 -0
- package/dist/src/ml/index.js +1243 -0
- package/dist/src/ml/ocr.d.ts +11 -0
- package/dist/src/node/appendFile.d.ts +7 -0
- package/dist/src/node/body.d.ts +9 -0
- package/dist/src/node/color.d.ts +7 -0
- package/dist/src/node/command.d.ts +8 -0
- package/dist/src/node/ctx2req.d.ts +5 -0
- package/dist/src/node/fileInfo.d.ts +7 -0
- package/dist/src/node/fs.d.ts +7 -0
- package/dist/src/node/get.d.ts +10 -0
- package/dist/src/node/getIPAdress.d.ts +1 -0
- package/dist/src/node/index.d.ts +25 -0
- package/dist/src/node/index.js +54 -0
- package/dist/src/node/isColorSupported.d.ts +2 -0
- package/dist/src/node/paresUrl.d.ts +19 -0
- package/dist/src/node/readDir.d.ts +6 -0
- package/dist/src/node/readFile.d.ts +9 -0
- package/dist/src/node/router.d.ts +42 -0
- package/dist/src/node/send.d.ts +7 -0
- package/dist/src/node/server.d.ts +18 -0
- package/dist/src/node/startTask.d.ts +2 -0
- package/dist/src/node/stream.d.ts +15 -0
- package/dist/src/node/taskEnd.d.ts +2 -0
- package/dist/src/node/traverse.d.ts +17 -0
- package/dist/src/node/watchFile.d.ts +8 -0
- package/dist/src/node/websocket.d.ts +57 -0
- package/dist/src/node/writeFile.d.ts +8 -0
- package/dist/src/node/ws.d.ts +8 -0
- package/dist/src/optimize/index.d.ts +36 -0
- package/dist/src/optimize/promise.d.ts +0 -0
- package/dist/src/ran/commit.d.ts +0 -0
- package/dist/src/ran/dom.d.ts +0 -0
- package/dist/src/ran/hooks.d.ts +0 -0
- package/dist/src/ran/index.d.ts +20 -0
- package/dist/src/ran/min.d.ts +32 -0
- package/dist/src/ran/reconcile.d.ts +0 -0
- package/dist/src/ran/schedule.d.ts +0 -0
- package/dist/src/react/index.d.ts +2 -0
- package/dist/src/react/index.js +4 -0
- package/dist/src/react/reactify.d.ts +2 -0
- package/dist/src/sort/bubble.d.ts +7 -0
- package/dist/src/sort/bucket.d.ts +7 -0
- package/dist/src/sort/count.d.ts +7 -0
- package/dist/src/sort/heap.d.ts +7 -0
- package/dist/src/sort/index.d.ts +0 -0
- package/dist/src/sort/insert.d.ts +7 -0
- package/dist/src/sort/merge.d.ts +7 -0
- package/dist/src/sort/quick.d.ts +7 -0
- package/dist/src/sort/radix.d.ts +7 -0
- package/dist/src/sort/randomArray.d.ts +9 -0
- package/dist/src/sort/select.d.ts +7 -0
- package/dist/src/sort/shell.d.ts +7 -0
- package/dist/src/utils/audioRecorder.d.ts +16 -0
- package/dist/src/utils/behavior.d.ts +15 -0
- package/dist/src/utils/bom.d.ts +230 -0
- package/dist/src/utils/color.d.ts +71 -0
- package/dist/src/utils/compose.d.ts +10 -0
- package/dist/src/utils/console.d.ts +1 -0
- package/dist/src/utils/debounce.d.ts +7 -0
- package/dist/src/utils/device.d.ts +25 -0
- package/dist/src/utils/dom.d.ts +105 -0
- package/dist/src/utils/error.d.ts +1 -0
- package/dist/src/utils/func.d.ts +1 -0
- package/dist/src/utils/img.d.ts +16 -0
- package/dist/src/utils/index.d.ts +35 -0
- package/dist/src/utils/index.js +4551 -0
- package/dist/src/utils/memoize.d.ts +7 -0
- package/dist/src/utils/mimeType.d.ts +9 -0
- package/dist/src/utils/monitor.d.ts +38 -0
- package/dist/src/utils/network.d.ts +39 -0
- package/dist/src/utils/noop.d.ts +2 -0
- package/dist/src/utils/number.d.ts +45 -0
- package/dist/src/utils/obj.d.ts +83 -0
- package/dist/src/utils/performance.d.ts +21 -0
- package/dist/src/utils/queue.d.ts +33 -0
- package/dist/src/utils/report.d.ts +8 -0
- package/dist/src/utils/request.d.ts +17 -0
- package/dist/src/utils/script.d.ts +7 -0
- package/dist/src/utils/signal.d.ts +6 -0
- package/dist/src/utils/storage.d.ts +2 -0
- package/dist/src/utils/str.d.ts +212 -0
- package/dist/src/utils/subscribe.d.ts +46 -0
- package/dist/src/utils/throttle.d.ts +15 -0
- package/dist/src/utils/time.d.ts +20 -0
- package/dist/src/utils/totp/sha/common.d.ts +151 -0
- package/dist/src/utils/totp/sha/converters.d.ts +88 -0
- package/dist/src/utils/totp/sha/custom_types.d.ts +60 -0
- package/dist/src/utils/totp/sha/primitives_32.d.ts +98 -0
- package/dist/src/utils/totp/sha/primitives_64.d.ts +116 -0
- package/dist/src/utils/totp/sha/sha.d.ts +103 -0
- package/dist/src/utils/totp/sha/sha1.d.ts +18 -0
- package/dist/src/utils/totp/sha/sha256.d.ts +20 -0
- package/dist/src/utils/totp/sha/sha3.d.ts +53 -0
- package/dist/src/utils/totp/sha/sha512.d.ts +21 -0
- package/dist/src/utils/totp/totp.d.ts +23 -0
- package/dist/src/utils/visual/application.d.ts +14 -0
- package/dist/src/utils/visual/demo/index.d.ts +1 -0
- package/dist/src/utils/visual/demo/sankey.d.ts +2 -0
- package/dist/src/utils/visual/enums.d.ts +27 -0
- package/dist/src/utils/visual/event/boundary.d.ts +22 -0
- package/dist/src/utils/visual/event/event.d.ts +14 -0
- package/dist/src/utils/visual/event/index.d.ts +6 -0
- package/dist/src/utils/visual/event/types.d.ts +32 -0
- package/dist/src/utils/visual/graphics/graphics.d.ts +81 -0
- package/dist/src/utils/visual/graphics/graphicsData.d.ts +13 -0
- package/dist/src/utils/visual/graphics/graphicsGeometry.d.ts +35 -0
- package/dist/src/utils/visual/graphics/index.d.ts +4 -0
- package/dist/src/utils/visual/index.d.ts +7 -0
- package/dist/src/utils/visual/math/bezier.d.ts +2 -0
- package/dist/src/utils/visual/math/enums.d.ts +3 -0
- package/dist/src/utils/visual/math/index.d.ts +5 -0
- package/dist/src/utils/visual/math/matrix.d.ts +113 -0
- package/dist/src/utils/visual/math/transform.d.ts +29 -0
- package/dist/src/utils/visual/render/batchRenderer.d.ts +72 -0
- package/dist/src/utils/visual/render/canvasRenderer.d.ts +10 -0
- package/dist/src/utils/visual/render/index.d.ts +3 -0
- package/dist/src/utils/visual/render/render.d.ts +15 -0
- package/dist/src/utils/visual/render/utils/batch/index.d.ts +90 -0
- package/dist/src/utils/visual/render/utils/float.d.ts +63 -0
- package/dist/src/utils/visual/render/utils/index.d.ts +3 -0
- package/dist/src/utils/visual/render/utils/verticy.d.ts +13 -0
- package/dist/src/utils/visual/render/utils/webgl/batchPool.d.ts +28 -0
- package/dist/src/utils/visual/render/utils/webgl/initShader.d.ts +2 -0
- package/dist/src/utils/visual/render/utils/webgl/shaders.d.ts +2 -0
- package/dist/src/utils/visual/render/utils/webgpu/shaders.d.ts +8 -0
- package/dist/src/utils/visual/render/webGPURenderer.d.ts +49 -0
- package/dist/src/utils/visual/render/webGlRenderer.d.ts +17 -0
- package/dist/src/utils/visual/shape/circle.d.ts +11 -0
- package/dist/src/utils/visual/shape/ellipse.d.ts +12 -0
- package/dist/src/utils/visual/shape/index.d.ts +7 -0
- package/dist/src/utils/visual/shape/polygon.d.ts +11 -0
- package/dist/src/utils/visual/shape/rectangle.d.ts +12 -0
- package/dist/src/utils/visual/shape/roundedRectangle.d.ts +13 -0
- package/dist/src/utils/visual/shape/shape.d.ts +6 -0
- package/dist/src/utils/visual/style/fill.d.ts +8 -0
- package/dist/src/utils/visual/style/index.d.ts +3 -0
- package/dist/src/utils/visual/style/line.d.ts +10 -0
- package/dist/src/utils/visual/types.d.ts +17 -0
- package/dist/src/utils/visual/vertex/container.d.ts +73 -0
- package/dist/src/utils/visual/vertex/index.d.ts +5 -0
- package/dist/src/utils/visual/vertex/point.d.ts +18 -0
- package/dist/src/utils/visual/vertex/vertex.d.ts +36 -0
- package/dist/src/vnode/chainDom.d.ts +47 -0
- package/dist/src/vnode/h.d.ts +6 -0
- package/dist/src/vnode/hooks.d.ts +23 -0
- package/dist/src/vnode/htmlDomApi.d.ts +33 -0
- package/dist/src/vnode/init.d.ts +2 -0
- package/dist/src/vnode/is.d.ts +5 -0
- package/dist/src/vnode/modules/attributes.d.ts +8 -0
- package/dist/src/vnode/modules/class.d.ts +8 -0
- package/dist/src/vnode/modules/index.d.ts +3 -0
- package/dist/src/vnode/modules/listeners.d.ts +12 -0
- package/dist/src/vnode/modules/props.d.ts +8 -0
- package/dist/src/vnode/modules/style.d.ts +14 -0
- package/dist/src/vnode/vnode.d.ts +31 -0
- package/dist/src/wasm/index.d.ts +2 -0
- package/dist/src/wasm/index.js +346 -0
- package/dist/src/wasm/word.d.ts +17 -0
- package/dist/src/wicket/bridge.d.ts +4 -0
- package/dist/src/wicket/index.d.ts +29 -0
- package/dist/test/arithmetic.test.d.ts +1 -0
- package/dist/test/cloneDeep.test.d.ts +1 -0
- package/dist/test/encodeUrl.test.d.ts +1 -0
- package/dist/test/escapeHtml.test.d.ts +1 -0
- package/dist/test/is-equal.test.d.ts +1 -0
- package/dist/test/mimeType.test.d.ts +1 -0
- package/dist/test/server.test.d.ts +1 -0
- package/dist/test/sort.test.d.ts +1 -0
- package/dist/test/status.test.d.ts +1 -0
- package/dist/test/totp.test.d.ts +1 -0
- package/dist/test/utils/compose.test.d.ts +1 -0
- package/dist/test/utils/mergeObj.test.d.ts +1 -0
- package/dist/test/utils/ocr.test.d.ts +1 -0
- package/dist/test/visual/math.test.d.ts +1 -0
- package/dist/test/vnode/index.d.ts +1 -0
- package/dist/test/vnode/server.d.ts +2 -0
- package/dist/test/vnode/vnode.d.ts +1 -0
- package/dist/test/websocket.test.d.ts +1 -0
- package/dist/test/writeFile.test.d.ts +1 -0
- package/dist/umd/index.umd.cjs +1 -0
- package/dist/umd/ml/ml.umd.cjs +1 -0
- package/dist/umd/node/node.umd.cjs +1 -0
- package/dist/umd/react/react.umd.cjs +1 -0
- package/dist/umd/utils/utils.umd.cjs +1 -0
- package/dist/umd/wasm/wasm.umd.cjs +1 -0
- package/dist/vite.config.d.ts +11 -0
- package/dist/ws-CUTzWNwq.js +703 -0
- package/package.json +70 -13
- package/readme.md +91 -0
- package/typings.d.ts +28 -0
- package/dist/index.js.map +0 -1
- package/dist/index.umd.cjs +0 -2
- package/dist/index.umd.cjs.map +0 -1
|
@@ -0,0 +1,4551 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import { y as toString, o as isString, D as getRandomString, E as MessageCodec, p as randomString } from "../../color-48RIDBG4.js";
|
|
5
|
+
import { C, a, F, H, b, M, R, c, d, z, e, f, g, G, x, h, i, j, k, l, m, n, B, r, q, s, t, u, v, w, A } from "../../color-48RIDBG4.js";
|
|
6
|
+
const NEW_LISTENER = "NEW_LISTENER";
|
|
7
|
+
class SyncHook {
|
|
8
|
+
constructor() {
|
|
9
|
+
__publicField(this, "_events", /* @__PURE__ */ new Map());
|
|
10
|
+
/**
|
|
11
|
+
* @description: 订阅事件
|
|
12
|
+
* @param {EventName} eventName
|
|
13
|
+
* @param {EventItem} eventItem
|
|
14
|
+
* @return {void}
|
|
15
|
+
*/
|
|
16
|
+
__publicField(this, "tap", (eventName, eventItem) => {
|
|
17
|
+
if (this._events.get(eventName) && eventName !== Symbol.for(NEW_LISTENER)) {
|
|
18
|
+
this.call(Symbol.for(NEW_LISTENER), eventName);
|
|
19
|
+
}
|
|
20
|
+
const callbacks = this._events.get(eventName) || /* @__PURE__ */ new Set();
|
|
21
|
+
if (typeof eventItem === "function") {
|
|
22
|
+
callbacks.add({
|
|
23
|
+
name: eventName,
|
|
24
|
+
callback: eventItem
|
|
25
|
+
});
|
|
26
|
+
} else {
|
|
27
|
+
callbacks.add(eventItem);
|
|
28
|
+
}
|
|
29
|
+
this._events.set(eventName, callbacks);
|
|
30
|
+
return this;
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* @description: 触发事件
|
|
34
|
+
* @param {EventName} eventName
|
|
35
|
+
* @param {array} args
|
|
36
|
+
* @return {void}
|
|
37
|
+
*/
|
|
38
|
+
__publicField(this, "call", (eventName, ...args) => {
|
|
39
|
+
const callbacks = this._events.get(eventName) || /* @__PURE__ */ new Set();
|
|
40
|
+
callbacks.forEach((item) => {
|
|
41
|
+
const { callback } = item;
|
|
42
|
+
callback(...args);
|
|
43
|
+
});
|
|
44
|
+
return this;
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* @description: 同步触发事件
|
|
48
|
+
* @param {EventName} eventName
|
|
49
|
+
* @param {array} args
|
|
50
|
+
* @return {Promise<void>}
|
|
51
|
+
*/
|
|
52
|
+
__publicField(this, "callSync", async (eventName, ...args) => {
|
|
53
|
+
const callbacks = this._events.get(eventName) || /* @__PURE__ */ new Set();
|
|
54
|
+
for (const item of callbacks) {
|
|
55
|
+
const { callback } = item;
|
|
56
|
+
await callback(...args);
|
|
57
|
+
}
|
|
58
|
+
return this;
|
|
59
|
+
});
|
|
60
|
+
/**
|
|
61
|
+
* @description: 只订阅一次事件,触发后就移除事件
|
|
62
|
+
* @param {EventName} eventName
|
|
63
|
+
* @param {EventItem} eventItem
|
|
64
|
+
* @return {void}
|
|
65
|
+
*/
|
|
66
|
+
__publicField(this, "once", (eventName, eventItem) => {
|
|
67
|
+
let one;
|
|
68
|
+
if (typeof eventItem === "function") {
|
|
69
|
+
one = {
|
|
70
|
+
name: eventName,
|
|
71
|
+
callback: (...args) => {
|
|
72
|
+
eventItem(...args);
|
|
73
|
+
this.off(eventName, one);
|
|
74
|
+
},
|
|
75
|
+
initialCallback: eventItem
|
|
76
|
+
};
|
|
77
|
+
} else {
|
|
78
|
+
const { callback } = eventItem;
|
|
79
|
+
one = {
|
|
80
|
+
name: eventName,
|
|
81
|
+
callback: (...args) => {
|
|
82
|
+
callback(...args);
|
|
83
|
+
this.off(eventName, one);
|
|
84
|
+
},
|
|
85
|
+
initialCallback: callback
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
this.tap(eventName, one);
|
|
89
|
+
return this;
|
|
90
|
+
});
|
|
91
|
+
/**
|
|
92
|
+
* @description: 移除订阅的事件
|
|
93
|
+
* @param {EventName} eventName
|
|
94
|
+
* @param {EventItem} eventItem
|
|
95
|
+
* @return {void}
|
|
96
|
+
*/
|
|
97
|
+
__publicField(this, "off", (eventName, eventItem) => {
|
|
98
|
+
const callbacks = this._events.get(eventName) || /* @__PURE__ */ new Set();
|
|
99
|
+
const newCallbacks = [...callbacks].filter((item) => {
|
|
100
|
+
if (typeof eventItem === "function") {
|
|
101
|
+
return item.callback !== eventItem && item.initialCallback !== eventItem;
|
|
102
|
+
} else {
|
|
103
|
+
const { callback } = eventItem;
|
|
104
|
+
return item.callback !== callback && item.initialCallback !== callback;
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
this._events.set(eventName, new Set(newCallbacks));
|
|
108
|
+
return this;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const perToNum = (str = "") => {
|
|
113
|
+
if (str.length === 0) return 0;
|
|
114
|
+
if (str.endsWith("%")) {
|
|
115
|
+
const value = Number(str.replace("%", ""));
|
|
116
|
+
return value > 1 ? value / 100 : value;
|
|
117
|
+
} else {
|
|
118
|
+
return Number(str);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const range = (num, min = 0, max = 1) => {
|
|
122
|
+
return Math.min(max, Math.max(min, num));
|
|
123
|
+
};
|
|
124
|
+
const _Mathjs = class _Mathjs {
|
|
125
|
+
constructor() {
|
|
126
|
+
/**
|
|
127
|
+
* 获取数字小数点的长度
|
|
128
|
+
* @param {number} n 数字
|
|
129
|
+
*/
|
|
130
|
+
__publicField(this, "getDecimalLength", (n2) => {
|
|
131
|
+
const [_, decimal] = n2.toString().split(".");
|
|
132
|
+
return decimal ? decimal.length : 0;
|
|
133
|
+
});
|
|
134
|
+
__publicField(this, "amend", (n2, precision = 15) => parseFloat(Number(n2).toPrecision(precision)));
|
|
135
|
+
__publicField(this, "power", (a2, b2) => Math.pow(10, Math.max(this.getDecimalLength(a2), this.getDecimalLength(b2))));
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
__publicField(_Mathjs, "handleMethod", (l2, r2) => {
|
|
139
|
+
const mathjs2 = new _Mathjs();
|
|
140
|
+
const { power, amend } = mathjs2;
|
|
141
|
+
const pow = power(l2, r2);
|
|
142
|
+
const a2 = amend(l2 * pow);
|
|
143
|
+
const b2 = amend(r2 * pow);
|
|
144
|
+
return (type) => {
|
|
145
|
+
switch (type) {
|
|
146
|
+
case "+":
|
|
147
|
+
return (a2 + b2) / pow;
|
|
148
|
+
case "-":
|
|
149
|
+
return (a2 - b2) / pow;
|
|
150
|
+
case "*":
|
|
151
|
+
return a2 * b2 / (pow * pow);
|
|
152
|
+
case "/":
|
|
153
|
+
return a2 / b2;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
__publicField(_Mathjs, "add", (a2, b2) => {
|
|
158
|
+
return _Mathjs.handleMethod(a2, b2)("+");
|
|
159
|
+
});
|
|
160
|
+
__publicField(_Mathjs, "divide", (a2, b2) => {
|
|
161
|
+
return _Mathjs.handleMethod(a2, b2)("/");
|
|
162
|
+
});
|
|
163
|
+
__publicField(_Mathjs, "multiply", (a2, b2) => {
|
|
164
|
+
return _Mathjs.handleMethod(a2, b2)("*");
|
|
165
|
+
});
|
|
166
|
+
__publicField(_Mathjs, "subtract", (a2, b2) => {
|
|
167
|
+
return _Mathjs.handleMethod(a2, b2)("-");
|
|
168
|
+
});
|
|
169
|
+
let Mathjs = _Mathjs;
|
|
170
|
+
function mathjs(a2, type, b2) {
|
|
171
|
+
function getDecimalLength(n2) {
|
|
172
|
+
const [_, decimal] = n2.toString().split(".");
|
|
173
|
+
return decimal ? decimal.length : 0;
|
|
174
|
+
}
|
|
175
|
+
const amend = (n2, precision = 15) => parseFloat(Number(n2).toPrecision(precision));
|
|
176
|
+
const power = Math.pow(10, Math.max(getDecimalLength(a2), getDecimalLength(b2)));
|
|
177
|
+
let result = 0;
|
|
178
|
+
a2 = amend(a2 * power);
|
|
179
|
+
b2 = amend(b2 * power);
|
|
180
|
+
switch (type) {
|
|
181
|
+
case "+":
|
|
182
|
+
result = (a2 + b2) / power;
|
|
183
|
+
break;
|
|
184
|
+
case "-":
|
|
185
|
+
result = (a2 - b2) / power;
|
|
186
|
+
break;
|
|
187
|
+
case "*":
|
|
188
|
+
result = a2 * b2 / (power * power);
|
|
189
|
+
break;
|
|
190
|
+
case "/":
|
|
191
|
+
result = a2 / b2;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
result = amend(result);
|
|
195
|
+
return {
|
|
196
|
+
/** 计算结果 */
|
|
197
|
+
result,
|
|
198
|
+
/**
|
|
199
|
+
* 继续计算
|
|
200
|
+
* @param {"+"|"-"|"*"|"/"} nextType 继续计算方式
|
|
201
|
+
* @param {number} nextValue 继续计算的值
|
|
202
|
+
*/
|
|
203
|
+
next: (nextType, nextValue) => {
|
|
204
|
+
return mathjs(result, nextType, nextValue);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const transformNumber = (value, locale = "zh-CN", precision = 2, fixed = 2) => {
|
|
209
|
+
var _a;
|
|
210
|
+
let units = ["", "K", "M", "B", "T"];
|
|
211
|
+
let setpSize = 3;
|
|
212
|
+
if (locale === "zh-CN") {
|
|
213
|
+
units = ["", " 万", " 亿", " 万亿"];
|
|
214
|
+
setpSize = 4;
|
|
215
|
+
}
|
|
216
|
+
if (locale === "zh-HK") {
|
|
217
|
+
units = ["", " 萬", " 億", " 萬億"];
|
|
218
|
+
setpSize = 4;
|
|
219
|
+
}
|
|
220
|
+
if (!/^[+-]?\d+(?:\.\d+)?$/.test(value)) {
|
|
221
|
+
return "--";
|
|
222
|
+
}
|
|
223
|
+
const length = parseInt(value).toString().length;
|
|
224
|
+
const unitIndex = Math.min(Math.floor((length - 1) / setpSize), units.length - 1);
|
|
225
|
+
const formattedValue = (_a = Mathjs.divide(Number(value), 10 ** (unitIndex * setpSize))) == null ? void 0 : _a.toFixed(precision);
|
|
226
|
+
return Number(formattedValue).toFixed(fixed) + units[unitIndex];
|
|
227
|
+
};
|
|
228
|
+
const addNumSym = (value, flag) => {
|
|
229
|
+
if (toString(value).startsWith("+") || toString(value).startsWith("-")) {
|
|
230
|
+
return toString(value);
|
|
231
|
+
}
|
|
232
|
+
if (flag) {
|
|
233
|
+
return Number(flag || 0) > 0 ? `+${toString(value)}` : toString(value);
|
|
234
|
+
}
|
|
235
|
+
return Number(value || 0) > 0 ? `+${toString(value)}` : toString(value);
|
|
236
|
+
};
|
|
237
|
+
const isFunction = (handler) => {
|
|
238
|
+
return typeof handler === "function";
|
|
239
|
+
};
|
|
240
|
+
const noop = () => {
|
|
241
|
+
};
|
|
242
|
+
function formatDuration(time) {
|
|
243
|
+
return time < 10 ? `0${time}` : time;
|
|
244
|
+
}
|
|
245
|
+
function timestampToTime(timestamp) {
|
|
246
|
+
let date = /* @__PURE__ */ new Date();
|
|
247
|
+
if (timestamp) {
|
|
248
|
+
date = new Date(timestamp);
|
|
249
|
+
}
|
|
250
|
+
date.format = (format = "YYYY-MM-DD HH:mm:ss") => {
|
|
251
|
+
const year = date.getFullYear();
|
|
252
|
+
const month = formatDuration(date.getMonth() + 1);
|
|
253
|
+
const day = formatDuration(date.getDate());
|
|
254
|
+
const hour = formatDuration(date.getHours());
|
|
255
|
+
const minute = formatDuration(date.getMinutes());
|
|
256
|
+
const second = formatDuration(date.getSeconds());
|
|
257
|
+
return format.replace(/Y+/gi, `${year}`).replace(/M+/g, `${month}`).replace(/D+/gi, `${day}`).replace(/H+/gi, `${hour}`).replace(/m+/g, `${minute}`).replace(/S+/gi, `${second}`);
|
|
258
|
+
};
|
|
259
|
+
return date;
|
|
260
|
+
}
|
|
261
|
+
const timeFormat = (time) => {
|
|
262
|
+
if (time === 0) return "00:00";
|
|
263
|
+
if (!time) return "";
|
|
264
|
+
const hour = Math.trunc(time / 3600);
|
|
265
|
+
const minute = Math.trunc(time % 3600 / 60);
|
|
266
|
+
const second = formatDuration(Math.trunc(time - hour * 3600 - minute * 60));
|
|
267
|
+
if (hour === 0) {
|
|
268
|
+
return `${formatDuration(minute)}:${second}`;
|
|
269
|
+
}
|
|
270
|
+
return `${formatDuration(hour)}:${formatDuration(minute)}:${second}`;
|
|
271
|
+
};
|
|
272
|
+
const performanceTime = () => {
|
|
273
|
+
if (typeof document !== "undefined") {
|
|
274
|
+
return performance.now();
|
|
275
|
+
}
|
|
276
|
+
if (typeof process !== "undefined") {
|
|
277
|
+
const [seconds, nanosecond] = process.hrtime();
|
|
278
|
+
return seconds * 1e3 + nanosecond / 1e6;
|
|
279
|
+
}
|
|
280
|
+
return Date.now();
|
|
281
|
+
};
|
|
282
|
+
var DEVICE = /* @__PURE__ */ ((DEVICE2) => {
|
|
283
|
+
DEVICE2["IPAD"] = "ipad";
|
|
284
|
+
DEVICE2["ANDROID"] = "android";
|
|
285
|
+
DEVICE2["IPhONE"] = "iphone";
|
|
286
|
+
DEVICE2["PC"] = "pc";
|
|
287
|
+
return DEVICE2;
|
|
288
|
+
})(DEVICE || {});
|
|
289
|
+
const currentDevice = () => {
|
|
290
|
+
if (typeof window !== "undefined") {
|
|
291
|
+
const ua = navigator.userAgent.toLowerCase();
|
|
292
|
+
if (/ipad|ipod/.test(ua)) return "ipad";
|
|
293
|
+
if (/android/.test(ua)) return "android";
|
|
294
|
+
if (/iphone/.test(ua)) return "iphone";
|
|
295
|
+
return "pc";
|
|
296
|
+
}
|
|
297
|
+
return "pc";
|
|
298
|
+
};
|
|
299
|
+
const isClient = typeof window !== "undefined";
|
|
300
|
+
const isWeiXin = () => {
|
|
301
|
+
if (isClient) {
|
|
302
|
+
const ua = window.navigator.userAgent.toLowerCase();
|
|
303
|
+
return ua.includes("micromessenger");
|
|
304
|
+
}
|
|
305
|
+
return false;
|
|
306
|
+
};
|
|
307
|
+
const isMobile = () => {
|
|
308
|
+
if (!isClient) return false;
|
|
309
|
+
const ua = window.navigator.userAgent;
|
|
310
|
+
if (/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(ua)) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
return false;
|
|
314
|
+
};
|
|
315
|
+
const isBangDevice = () => {
|
|
316
|
+
if (!isClient) return false;
|
|
317
|
+
const iphone = /iphone/i.test(window.navigator.userAgent);
|
|
318
|
+
const ratio2 = window.devicePixelRatio && window.devicePixelRatio === 2;
|
|
319
|
+
const ratio3 = window.devicePixelRatio && window.devicePixelRatio === 3;
|
|
320
|
+
const mini12 = window.screen.width === 360 && window.screen.height === 780;
|
|
321
|
+
const pro11 = window.screen.width === 375 && window.screen.height === 812;
|
|
322
|
+
const pro12 = window.screen.width === 390 && window.screen.height === 844;
|
|
323
|
+
const promax11 = window.screen.width === 414 && window.screen.height === 896;
|
|
324
|
+
const promax12 = window.screen.width === 428 && window.screen.height === 926;
|
|
325
|
+
switch (true) {
|
|
326
|
+
case (iphone && ratio3 && mini12):
|
|
327
|
+
// 12 mini
|
|
328
|
+
case (iphone && ratio3 && pro11):
|
|
329
|
+
// X Xs 11pro
|
|
330
|
+
case (iphone && ratio3 && pro12):
|
|
331
|
+
// 12 12pro
|
|
332
|
+
case (iphone && ratio2 && promax11):
|
|
333
|
+
// XR 11
|
|
334
|
+
case (iphone && ratio3 && promax11):
|
|
335
|
+
// Xsm 11promax
|
|
336
|
+
case (iphone && ratio3 && promax12):
|
|
337
|
+
return true;
|
|
338
|
+
default:
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
const retain = (callback = noop) => {
|
|
343
|
+
const historyReturnCb = () => {
|
|
344
|
+
callback();
|
|
345
|
+
isClient && window.removeEventListener("popstate", historyReturnCb);
|
|
346
|
+
};
|
|
347
|
+
isClient && window.history.pushState(null, "", window.location.href);
|
|
348
|
+
setTimeout(() => {
|
|
349
|
+
isClient && window.addEventListener("popstate", historyReturnCb);
|
|
350
|
+
}, 500);
|
|
351
|
+
};
|
|
352
|
+
const getCookie = (objName) => {
|
|
353
|
+
const arrStr = document.cookie.split("; ");
|
|
354
|
+
for (let i2 = 0; i2 < arrStr.length; i2++) {
|
|
355
|
+
const item = arrStr[i2].split("=");
|
|
356
|
+
if (item[0] === objName) {
|
|
357
|
+
return decodeURIComponent(item[1]);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return "";
|
|
361
|
+
};
|
|
362
|
+
const requestUrlToBuffer = (src, options) => {
|
|
363
|
+
return new Promise(function(resolve, reject) {
|
|
364
|
+
const xhr = new XMLHttpRequest();
|
|
365
|
+
xhr.open(options.method || "GET", src, true);
|
|
366
|
+
xhr.responseType = options.responseType || "arraybuffer";
|
|
367
|
+
xhr.onload = function() {
|
|
368
|
+
if (xhr.status === 200) {
|
|
369
|
+
resolve({ success: true, data: xhr.response, message: "" });
|
|
370
|
+
} else {
|
|
371
|
+
reject({
|
|
372
|
+
success: false,
|
|
373
|
+
data: xhr.status,
|
|
374
|
+
message: `The request status is${xhr.status}`
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
xhr.onerror = function(e2) {
|
|
379
|
+
reject({ success: false, data: e2, message: `` });
|
|
380
|
+
};
|
|
381
|
+
xhr.onprogress = (event) => {
|
|
382
|
+
options.onProgress && options.onProgress(event);
|
|
383
|
+
};
|
|
384
|
+
xhr.withCredentials = options.withCredentials || false;
|
|
385
|
+
if (options.headers) {
|
|
386
|
+
Object.keys(options.headers).forEach(function(key) {
|
|
387
|
+
var _a;
|
|
388
|
+
((_a = options.headers) == null ? void 0 : _a[key]) && xhr.setRequestHeader(key, options.headers[key]);
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
xhr.send(options.body);
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
const getPixelRatio = (context) => {
|
|
395
|
+
const backingStore = context.backingStorePixelRatio || context.webkitBackingStorePixelRatio || context.mozBackingStorePixelRatio || context.msBackingStorePixelRatio || context.oBackingStorePixelRatio || 1;
|
|
396
|
+
return (isClient && window.devicePixelRatio || 1) / backingStore;
|
|
397
|
+
};
|
|
398
|
+
const createObjectURL = async (src) => {
|
|
399
|
+
if (typeof src === "string") {
|
|
400
|
+
return src;
|
|
401
|
+
} else if (src instanceof Blob) {
|
|
402
|
+
return URL.createObjectURL(src);
|
|
403
|
+
} else if (src instanceof ArrayBuffer) {
|
|
404
|
+
return URL.createObjectURL(new Blob([src]));
|
|
405
|
+
} else if (src instanceof Response) {
|
|
406
|
+
const result = await src.blob();
|
|
407
|
+
return URL.createObjectURL(result);
|
|
408
|
+
} else {
|
|
409
|
+
return src;
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
const getFrame = (n2 = 10) => {
|
|
413
|
+
const frameList = [];
|
|
414
|
+
let lastFrame = 0;
|
|
415
|
+
let requestAnimationFrameRef;
|
|
416
|
+
return new Promise((resolve) => {
|
|
417
|
+
const a2 = () => {
|
|
418
|
+
const now = performanceTime();
|
|
419
|
+
const frame = now - lastFrame;
|
|
420
|
+
if (lastFrame !== 0) {
|
|
421
|
+
frameList.push(frame);
|
|
422
|
+
}
|
|
423
|
+
lastFrame = now;
|
|
424
|
+
if (frameList.length > n2) {
|
|
425
|
+
const num = frameList.reduce((i2, j2) => i2 + j2);
|
|
426
|
+
resolve(1 / (num / n2));
|
|
427
|
+
cancelAnimationFrame(requestAnimationFrameRef);
|
|
428
|
+
}
|
|
429
|
+
requestAnimationFrameRef = requestAnimationFrame(a2);
|
|
430
|
+
};
|
|
431
|
+
if (frameList.length <= n2) {
|
|
432
|
+
requestAnimationFrameRef = requestAnimationFrame(a2);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
};
|
|
436
|
+
const getHost = (env) => {
|
|
437
|
+
if (typeof window !== "undefined") {
|
|
438
|
+
let host = "";
|
|
439
|
+
if (env && isString(env)) {
|
|
440
|
+
if (/trunk|neibu|release/.test(env)) {
|
|
441
|
+
host = `.${env}`;
|
|
442
|
+
} else if (/test/.test(env)) {
|
|
443
|
+
host = env;
|
|
444
|
+
} else if (/prod/.test(env)) {
|
|
445
|
+
host = "";
|
|
446
|
+
} else {
|
|
447
|
+
host = "";
|
|
448
|
+
}
|
|
449
|
+
} else {
|
|
450
|
+
const env2 = /\w(\.trunk|\.neibu|\.release|test)\./.exec(window.location.hostname);
|
|
451
|
+
if (env2) {
|
|
452
|
+
host = env2[1];
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return `//log.${host}`;
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
const getAllQueryString = (url) => {
|
|
459
|
+
if (typeof window !== "undefined") {
|
|
460
|
+
const r2 = {};
|
|
461
|
+
const href = url || window.location.href;
|
|
462
|
+
if (href.split("?")[1]) {
|
|
463
|
+
const str = href.split("?")[1];
|
|
464
|
+
const strList = str.split("&");
|
|
465
|
+
strList.forEach((item) => {
|
|
466
|
+
const [key, val] = item.split("=");
|
|
467
|
+
if (key && val) {
|
|
468
|
+
r2[key] = decodeURIComponent(val);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
return r2;
|
|
473
|
+
}
|
|
474
|
+
return {};
|
|
475
|
+
};
|
|
476
|
+
const getQuery = (url) => {
|
|
477
|
+
if (typeof window !== "undefined") {
|
|
478
|
+
const r2 = {};
|
|
479
|
+
const href = url || window.location.href;
|
|
480
|
+
if (href.split("?")[1]) {
|
|
481
|
+
const str = href.split("?")[1];
|
|
482
|
+
const strList = str.split("&");
|
|
483
|
+
strList.forEach((item) => {
|
|
484
|
+
const [key, val] = item.split("=");
|
|
485
|
+
if (key && val) {
|
|
486
|
+
r2[key] = decodeURIComponent(val);
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
return r2;
|
|
491
|
+
}
|
|
492
|
+
return {};
|
|
493
|
+
};
|
|
494
|
+
function appendUrl(url, params = {}) {
|
|
495
|
+
let _url = url;
|
|
496
|
+
if (_url.indexOf("//") === 0) {
|
|
497
|
+
_url = _url.replace("//", "https://");
|
|
498
|
+
}
|
|
499
|
+
const urlObj = new URL(_url);
|
|
500
|
+
if (params) {
|
|
501
|
+
Object.keys(params).forEach((key) => {
|
|
502
|
+
if (params[key]) {
|
|
503
|
+
urlObj.searchParams.set(key, params[key]);
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
return urlObj.href;
|
|
508
|
+
}
|
|
509
|
+
const removeGhosting = (event) => {
|
|
510
|
+
const dragIcon = document.createElement("img");
|
|
511
|
+
const url = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
|
512
|
+
dragIcon.src = url;
|
|
513
|
+
dragIcon.width = 0;
|
|
514
|
+
dragIcon.height = 0;
|
|
515
|
+
dragIcon.style.opacity = "0";
|
|
516
|
+
if (event.dataTransfer) {
|
|
517
|
+
event.dataTransfer.setDragImage(dragIcon, 0, 0);
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
function getCookieByName(name) {
|
|
521
|
+
if (typeof window !== "undefined") {
|
|
522
|
+
const cookieList = new RegExp(`(^| )${name}(?:=([^;]*))?(;|$)`).exec(document.cookie);
|
|
523
|
+
if (cookieList && cookieList[2]) return cookieList[2];
|
|
524
|
+
}
|
|
525
|
+
return "";
|
|
526
|
+
}
|
|
527
|
+
const getWindow = () => {
|
|
528
|
+
if (typeof window !== "undefined") {
|
|
529
|
+
return {
|
|
530
|
+
width: window.innerWidth,
|
|
531
|
+
height: window.innerHeight
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
return {
|
|
535
|
+
width: 0,
|
|
536
|
+
height: 0
|
|
537
|
+
};
|
|
538
|
+
};
|
|
539
|
+
const connection = () => {
|
|
540
|
+
if (typeof window !== "undefined") {
|
|
541
|
+
return window.navigator.connection;
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
const ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x7E]|%(?:[^\da-f]|[\da-f][^\da-f]|$))+/gi;
|
|
545
|
+
const UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
|
|
546
|
+
const UNMATCHED_SURROGATE_PAIR_REPLACE = "$1�$2";
|
|
547
|
+
function encodeUrl(url) {
|
|
548
|
+
return String(url).replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE).replace(ENCODE_CHARS_REGEXP, encodeURI);
|
|
549
|
+
}
|
|
550
|
+
const imageRequest = (url) => {
|
|
551
|
+
return new Promise((resolve, reject) => {
|
|
552
|
+
const img = new Image();
|
|
553
|
+
const startTime = (/* @__PURE__ */ new Date()).getTime();
|
|
554
|
+
img.src = url ? url : `https://github.com/favicon.ico?d=${startTime}`;
|
|
555
|
+
img.onload = () => {
|
|
556
|
+
const endTime = (/* @__PURE__ */ new Date()).getTime();
|
|
557
|
+
const delta = endTime - startTime;
|
|
558
|
+
resolve(delta);
|
|
559
|
+
};
|
|
560
|
+
img.onerror = (err) => {
|
|
561
|
+
reject(err);
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
};
|
|
565
|
+
const durationHandler = (handler, ...params) => (duration) => new Promise((resolve, reject) => {
|
|
566
|
+
setTimeout(async () => {
|
|
567
|
+
try {
|
|
568
|
+
const result = await handler(...params);
|
|
569
|
+
resolve(result);
|
|
570
|
+
} catch (error) {
|
|
571
|
+
reject(error);
|
|
572
|
+
}
|
|
573
|
+
}, duration);
|
|
574
|
+
});
|
|
575
|
+
const networkSpeed = async (options) => {
|
|
576
|
+
const { url, duration = 3e3, count = 5 } = options;
|
|
577
|
+
let jitter = 0;
|
|
578
|
+
let ping = 0;
|
|
579
|
+
const pingList = [];
|
|
580
|
+
for (let i2 = 0; i2 < count; i2++) {
|
|
581
|
+
const handler = durationHandler(imageRequest, url);
|
|
582
|
+
const delta = await handler(duration);
|
|
583
|
+
pingList.push(delta);
|
|
584
|
+
}
|
|
585
|
+
const maxPing = Math.max(...pingList);
|
|
586
|
+
const minPing = Math.min(...pingList);
|
|
587
|
+
jitter = maxPing - minPing;
|
|
588
|
+
ping = pingList.reduce((a2, b2) => a2 + b2) / pingList.length;
|
|
589
|
+
return { ping, jitter };
|
|
590
|
+
};
|
|
591
|
+
const isSafari = () => {
|
|
592
|
+
if (typeof navigator === "undefined") {
|
|
593
|
+
return void 0;
|
|
594
|
+
}
|
|
595
|
+
return (
|
|
596
|
+
// eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
597
|
+
navigator.vendor && // eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
598
|
+
navigator.vendor.indexOf("Apple") > -1 && // eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
599
|
+
navigator.userAgent && // eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
600
|
+
!navigator.userAgent.includes("CriOS") && // eslint-disable-next-line n/no-unsupported-features/node-builtins
|
|
601
|
+
!navigator.userAgent.includes("FxiOS")
|
|
602
|
+
);
|
|
603
|
+
};
|
|
604
|
+
const DEFAULT_TIMEOUT = 2e4;
|
|
605
|
+
class PostMessageBridge {
|
|
606
|
+
constructor(targetWindow = window, targetOrigin = "*") {
|
|
607
|
+
__publicField(this, "targetWindow");
|
|
608
|
+
__publicField(this, "targetOrigin");
|
|
609
|
+
__publicField(this, "messageHandlers");
|
|
610
|
+
__publicField(this, "pendingRequests");
|
|
611
|
+
__publicField(this, "handleMessage", (event) => {
|
|
612
|
+
if (this.targetOrigin !== "*" && event.origin !== this.targetOrigin) return;
|
|
613
|
+
const dataCopy = typeof event.data === "string" ? String(event.data) : event.data;
|
|
614
|
+
const decodedData = MessageCodec.decode(dataCopy);
|
|
615
|
+
if (!decodedData) return;
|
|
616
|
+
const { type, payload, id, isResponse } = decodedData;
|
|
617
|
+
if (isResponse && id) {
|
|
618
|
+
const pendingRequest = this.pendingRequests.get(id);
|
|
619
|
+
if (pendingRequest) {
|
|
620
|
+
pendingRequest.resolve(payload);
|
|
621
|
+
this.pendingRequests.delete(id);
|
|
622
|
+
}
|
|
623
|
+
return;
|
|
624
|
+
}
|
|
625
|
+
const handler = this.messageHandlers.get(type);
|
|
626
|
+
if (handler) {
|
|
627
|
+
Promise.resolve(handler(payload)).then((response) => {
|
|
628
|
+
if (id) {
|
|
629
|
+
this.targetWindow.postMessage(
|
|
630
|
+
MessageCodec.encode({
|
|
631
|
+
type,
|
|
632
|
+
payload: response,
|
|
633
|
+
id,
|
|
634
|
+
isResponse: true
|
|
635
|
+
}),
|
|
636
|
+
this.targetOrigin
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
}).catch((error) => {
|
|
640
|
+
if (id) {
|
|
641
|
+
this.targetWindow.postMessage(
|
|
642
|
+
MessageCodec.encode({
|
|
643
|
+
type,
|
|
644
|
+
payload: error.message,
|
|
645
|
+
id,
|
|
646
|
+
isResponse: true,
|
|
647
|
+
isError: true
|
|
648
|
+
}),
|
|
649
|
+
this.targetOrigin
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
// 注册消息处理器
|
|
656
|
+
__publicField(this, "on", (type, handler) => {
|
|
657
|
+
this.messageHandlers.set(type, handler);
|
|
658
|
+
});
|
|
659
|
+
// 移除消息处理器
|
|
660
|
+
__publicField(this, "off", (type) => {
|
|
661
|
+
this.messageHandlers.delete(type);
|
|
662
|
+
});
|
|
663
|
+
// 发送消息并等待响应
|
|
664
|
+
__publicField(this, "send", async (type, payload) => {
|
|
665
|
+
const id = Math.random().toString(36).slice(2, 11);
|
|
666
|
+
return new Promise((rs, reject) => {
|
|
667
|
+
this.targetWindow.postMessage(
|
|
668
|
+
MessageCodec.encode({
|
|
669
|
+
type,
|
|
670
|
+
payload,
|
|
671
|
+
id
|
|
672
|
+
}),
|
|
673
|
+
this.targetOrigin
|
|
674
|
+
);
|
|
675
|
+
const timeout = setTimeout(() => {
|
|
676
|
+
if (this.pendingRequests.has(id)) {
|
|
677
|
+
this.pendingRequests.delete(id);
|
|
678
|
+
reject(new Error("Request timeout"));
|
|
679
|
+
}
|
|
680
|
+
}, DEFAULT_TIMEOUT);
|
|
681
|
+
const resolve = (payload2) => {
|
|
682
|
+
clearTimeout(timeout);
|
|
683
|
+
rs(payload2);
|
|
684
|
+
};
|
|
685
|
+
this.pendingRequests.set(id, { resolve, reject });
|
|
686
|
+
});
|
|
687
|
+
});
|
|
688
|
+
// 广播消息
|
|
689
|
+
__publicField(this, "broadcast", (data) => {
|
|
690
|
+
const { type, payload } = data;
|
|
691
|
+
this.targetWindow.postMessage(
|
|
692
|
+
MessageCodec.encode({
|
|
693
|
+
type,
|
|
694
|
+
payload
|
|
695
|
+
}),
|
|
696
|
+
this.targetOrigin
|
|
697
|
+
);
|
|
698
|
+
});
|
|
699
|
+
// 清理
|
|
700
|
+
__publicField(this, "destroy", () => {
|
|
701
|
+
window.removeEventListener("message", this.handleMessage);
|
|
702
|
+
this.messageHandlers.clear();
|
|
703
|
+
this.pendingRequests.clear();
|
|
704
|
+
});
|
|
705
|
+
this.targetWindow = targetWindow;
|
|
706
|
+
this.targetOrigin = targetOrigin;
|
|
707
|
+
this.messageHandlers = /* @__PURE__ */ new Map();
|
|
708
|
+
this.pendingRequests = /* @__PURE__ */ new Map();
|
|
709
|
+
window.addEventListener("message", this.handleMessage);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
const _BridgeManager = class _BridgeManager {
|
|
713
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
714
|
+
constructor() {
|
|
715
|
+
__publicField(this, "bridges", /* @__PURE__ */ new Map());
|
|
716
|
+
// 创建新的 bridge
|
|
717
|
+
__publicField(this, "connectClient", ({
|
|
718
|
+
id,
|
|
719
|
+
targetOrigin,
|
|
720
|
+
targetWindow
|
|
721
|
+
}) => {
|
|
722
|
+
const bridge = new PostMessageBridge(targetWindow, targetOrigin);
|
|
723
|
+
if (!id) {
|
|
724
|
+
id = getRandomString(10);
|
|
725
|
+
}
|
|
726
|
+
if (this.bridges.has(id)) {
|
|
727
|
+
throw new Error(`Bridge ${id} already exists`);
|
|
728
|
+
}
|
|
729
|
+
this.bridges.set(id, bridge);
|
|
730
|
+
return { bridge, id };
|
|
731
|
+
});
|
|
732
|
+
// 获取指定 bridge
|
|
733
|
+
__publicField(this, "getClient", (id) => {
|
|
734
|
+
return this.bridges.get(id);
|
|
735
|
+
});
|
|
736
|
+
// 移除 bridge
|
|
737
|
+
__publicField(this, "removeClient", (id) => {
|
|
738
|
+
const bridge = this.bridges.get(id);
|
|
739
|
+
if (bridge) {
|
|
740
|
+
bridge.destroy();
|
|
741
|
+
this.bridges.delete(id);
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
// 移除所有 bridge
|
|
745
|
+
__publicField(this, "removeAllClient", () => {
|
|
746
|
+
this.bridges.forEach((bridge) => {
|
|
747
|
+
bridge.destroy();
|
|
748
|
+
});
|
|
749
|
+
this.bridges.clear();
|
|
750
|
+
});
|
|
751
|
+
// 广播消息到所有 bridge
|
|
752
|
+
__publicField(this, "broadcast", (payload) => {
|
|
753
|
+
this.bridges.forEach((bridge) => {
|
|
754
|
+
bridge.broadcast(payload);
|
|
755
|
+
});
|
|
756
|
+
});
|
|
757
|
+
// 发送消息到指定 bridge
|
|
758
|
+
__publicField(this, "sendTo", (id, type, payload) => {
|
|
759
|
+
const bridge = this.getClient(id);
|
|
760
|
+
if (!bridge) {
|
|
761
|
+
return Promise.reject(new Error(`Bridge ${id} not found`));
|
|
762
|
+
}
|
|
763
|
+
return bridge.send(type, payload);
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
static getInstance() {
|
|
767
|
+
if (!_BridgeManager.instance) {
|
|
768
|
+
_BridgeManager.instance = new _BridgeManager();
|
|
769
|
+
}
|
|
770
|
+
return _BridgeManager.instance;
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
__publicField(_BridgeManager, "instance");
|
|
774
|
+
let BridgeManager = _BridgeManager;
|
|
775
|
+
const bridgeManager = BridgeManager.getInstance();
|
|
776
|
+
const Client = {
|
|
777
|
+
// 连接 Client
|
|
778
|
+
connect: ({ id, targetWindow, targetOrigin }) => {
|
|
779
|
+
return bridgeManager.connectClient({ id, targetWindow, targetOrigin });
|
|
780
|
+
},
|
|
781
|
+
// 移除 Client 连接
|
|
782
|
+
remove: (id) => {
|
|
783
|
+
if (!id) return;
|
|
784
|
+
bridgeManager.removeClient(id);
|
|
785
|
+
},
|
|
786
|
+
// 移除所有 Client 连接
|
|
787
|
+
removeAll: () => {
|
|
788
|
+
bridgeManager.removeAllClient();
|
|
789
|
+
},
|
|
790
|
+
/** 广播消息到所有 Platform */
|
|
791
|
+
broadcast: (payload) => {
|
|
792
|
+
return bridgeManager.broadcast(payload);
|
|
793
|
+
},
|
|
794
|
+
/** 发送消息到指定 Platform */
|
|
795
|
+
call: ({ id, type, payload }) => {
|
|
796
|
+
return bridgeManager.sendTo(id, type, payload);
|
|
797
|
+
},
|
|
798
|
+
/** 广播消息到所有所有可以接收消息的窗口 (为了安全考虑,不建议使用) */
|
|
799
|
+
broadcastToAll: (payload) => {
|
|
800
|
+
return window.postMessage(MessageCodec.encode(payload), "*");
|
|
801
|
+
}
|
|
802
|
+
};
|
|
803
|
+
const initPlatform = (events) => {
|
|
804
|
+
const initBridge = async (event) => {
|
|
805
|
+
var _a;
|
|
806
|
+
if (typeof event.data !== "string") return;
|
|
807
|
+
const str = String(event.data);
|
|
808
|
+
const decodedData = MessageCodec.decode(str);
|
|
809
|
+
if (!decodedData) return;
|
|
810
|
+
const { type, payload, id } = decodedData;
|
|
811
|
+
const handler = events[type];
|
|
812
|
+
if (!isFunction(handler)) return;
|
|
813
|
+
const result = await handler(payload);
|
|
814
|
+
const encodedData = MessageCodec.encode({ type, payload: result, id, isResponse: true });
|
|
815
|
+
(_a = event.source) == null ? void 0 : _a.postMessage(encodedData, { targetOrigin: event.origin });
|
|
816
|
+
};
|
|
817
|
+
window.removeEventListener("message", initBridge);
|
|
818
|
+
window.addEventListener("message", initBridge);
|
|
819
|
+
const destroy = () => {
|
|
820
|
+
window.removeEventListener("message", initBridge);
|
|
821
|
+
};
|
|
822
|
+
return {
|
|
823
|
+
destroy
|
|
824
|
+
};
|
|
825
|
+
};
|
|
826
|
+
const Platform = {
|
|
827
|
+
init: initPlatform
|
|
828
|
+
};
|
|
829
|
+
const memoize = (fn) => {
|
|
830
|
+
let cache = false;
|
|
831
|
+
let result = void 0;
|
|
832
|
+
return (...args) => {
|
|
833
|
+
if (cache) {
|
|
834
|
+
return result;
|
|
835
|
+
} else {
|
|
836
|
+
result = typeof fn === "function" ? fn(...args) : fn;
|
|
837
|
+
cache = true;
|
|
838
|
+
fn = void 0;
|
|
839
|
+
return result;
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
};
|
|
843
|
+
function querystring(data = {}) {
|
|
844
|
+
if (typeof data !== "object") {
|
|
845
|
+
throw new TypeError("param must be object");
|
|
846
|
+
}
|
|
847
|
+
return Object.entries(data).reduce(
|
|
848
|
+
(searchParams, [name, value]) => value === void 0 || value == null ? searchParams : (searchParams.append(decodeURIComponent(name), decodeURIComponent(value)), searchParams),
|
|
849
|
+
new URLSearchParams()
|
|
850
|
+
).toString();
|
|
851
|
+
}
|
|
852
|
+
const transitionJsonToString = (jsonObj, callback = () => {
|
|
853
|
+
}) => {
|
|
854
|
+
let _jsonObj = "";
|
|
855
|
+
if (Object.prototype.toString.call(jsonObj) !== "[object String]") {
|
|
856
|
+
try {
|
|
857
|
+
_jsonObj = JSON.stringify(jsonObj);
|
|
858
|
+
} catch (error) {
|
|
859
|
+
callback(error);
|
|
860
|
+
}
|
|
861
|
+
} else {
|
|
862
|
+
try {
|
|
863
|
+
jsonObj = typeof jsonObj === "string" ? jsonObj.replace(/'/g, '"') : JSON.stringify(jsonObj);
|
|
864
|
+
_jsonObj = JSON.stringify(JSON.parse(jsonObj));
|
|
865
|
+
} catch (error) {
|
|
866
|
+
callback(error);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
return _jsonObj;
|
|
870
|
+
};
|
|
871
|
+
const formatJson = (jsonObj, callback = () => {
|
|
872
|
+
}) => {
|
|
873
|
+
let formatted = "";
|
|
874
|
+
let pad = 0;
|
|
875
|
+
const PADDING = " ";
|
|
876
|
+
let jsonString = transitionJsonToString(jsonObj, callback);
|
|
877
|
+
if (!jsonString) {
|
|
878
|
+
return jsonString;
|
|
879
|
+
}
|
|
880
|
+
const _index = [];
|
|
881
|
+
let _indexStart = null;
|
|
882
|
+
let _indexEnd = null;
|
|
883
|
+
let jsonArray = [];
|
|
884
|
+
jsonString = jsonString.replace(/([{}])/g, "\r\n$1\r\n");
|
|
885
|
+
jsonString = jsonString.replace(/([[\]])/g, "\r\n$1\r\n");
|
|
886
|
+
jsonString = jsonString.replace(/,/g, ",\r\n");
|
|
887
|
+
jsonString = jsonString.replace(/\r\n\r\n/g, "\r\n");
|
|
888
|
+
jsonString = jsonString.replace(/\r\n,/g, ",");
|
|
889
|
+
jsonArray = jsonString.split("\r\n");
|
|
890
|
+
jsonArray.forEach(function(node, index) {
|
|
891
|
+
var _a;
|
|
892
|
+
const num = node.match(/"/g) ? ((_a = node.match(/"/g)) == null ? void 0 : _a.length) || 0 : 0;
|
|
893
|
+
if (num % 2 && !_indexStart) {
|
|
894
|
+
_indexStart = index;
|
|
895
|
+
}
|
|
896
|
+
if (num % 2 && _indexStart && _indexStart !== index) {
|
|
897
|
+
_indexEnd = index;
|
|
898
|
+
}
|
|
899
|
+
if (_indexStart && _indexEnd) {
|
|
900
|
+
_index.push({
|
|
901
|
+
start: _indexStart,
|
|
902
|
+
end: _indexEnd
|
|
903
|
+
});
|
|
904
|
+
_indexStart = null;
|
|
905
|
+
_indexEnd = null;
|
|
906
|
+
}
|
|
907
|
+
});
|
|
908
|
+
_index.reverse().forEach(function(item) {
|
|
909
|
+
const newArray = jsonArray.slice(item.start, item.end + 1);
|
|
910
|
+
jsonArray.splice(item.start, item.end + 1 - item.start, newArray.join(""));
|
|
911
|
+
});
|
|
912
|
+
jsonString = jsonArray.join("\r\n");
|
|
913
|
+
jsonString = jsonString.replace(/:\r\n\{/g, ":{");
|
|
914
|
+
jsonString = jsonString.replace(/:\r\n\[/g, ":[");
|
|
915
|
+
jsonArray = jsonString.split("\r\n");
|
|
916
|
+
jsonArray.forEach(function(item) {
|
|
917
|
+
let i2 = 0;
|
|
918
|
+
let indent = 0;
|
|
919
|
+
let padding = "";
|
|
920
|
+
if (/\{$/.test(item) || /\[$/.test(item)) {
|
|
921
|
+
indent += 1;
|
|
922
|
+
} else if (/\}$/.test(item) || /\]$/.test(item) || /\},$/.test(item) || /\],$/.test(item)) {
|
|
923
|
+
if (pad !== 0) {
|
|
924
|
+
pad -= 1;
|
|
925
|
+
}
|
|
926
|
+
} else {
|
|
927
|
+
indent = 0;
|
|
928
|
+
}
|
|
929
|
+
for (i2 = 0; i2 < pad; i2++) {
|
|
930
|
+
padding += PADDING;
|
|
931
|
+
}
|
|
932
|
+
formatted += padding + item + "\r\n";
|
|
933
|
+
pad += indent;
|
|
934
|
+
});
|
|
935
|
+
return formatted.trim();
|
|
936
|
+
};
|
|
937
|
+
const filterObj = (obj, list) => {
|
|
938
|
+
const result = {};
|
|
939
|
+
Object.keys(obj).forEach((item) => {
|
|
940
|
+
if (!list.includes(item)) {
|
|
941
|
+
result[item] = obj[item];
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
return result;
|
|
945
|
+
};
|
|
946
|
+
const merge = (a2, b2) => {
|
|
947
|
+
if (a2 && b2) {
|
|
948
|
+
for (const key in b2) {
|
|
949
|
+
a2[key] = b2[key];
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return a2;
|
|
953
|
+
};
|
|
954
|
+
function replaceOld(source, name, replacement, isForced) {
|
|
955
|
+
if (typeof source === "undefined") return;
|
|
956
|
+
if (name in source || isForced) {
|
|
957
|
+
const original = source[name];
|
|
958
|
+
const wrapped = replacement(original);
|
|
959
|
+
if (typeof wrapped === "function") {
|
|
960
|
+
source[name] = wrapped;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
const mergeExports = (obj, exports) => {
|
|
965
|
+
const descriptors = Object.getOwnPropertyDescriptors(exports);
|
|
966
|
+
for (const name of Object.keys(descriptors)) {
|
|
967
|
+
const descriptor = descriptors[name];
|
|
968
|
+
if (descriptor.get) {
|
|
969
|
+
const fn = descriptor.get;
|
|
970
|
+
Object.defineProperty(obj, name, {
|
|
971
|
+
configurable: false,
|
|
972
|
+
enumerable: true,
|
|
973
|
+
get: memoize(fn)
|
|
974
|
+
});
|
|
975
|
+
} else if (typeof descriptor.value === "object") {
|
|
976
|
+
Object.defineProperty(obj, name, {
|
|
977
|
+
configurable: false,
|
|
978
|
+
enumerable: true,
|
|
979
|
+
writable: false,
|
|
980
|
+
value: mergeExports({}, descriptor.value)
|
|
981
|
+
});
|
|
982
|
+
} else {
|
|
983
|
+
throw new Error("Exposed values must be either a getter or an nested object");
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
return Object.freeze(obj);
|
|
987
|
+
};
|
|
988
|
+
const setAttributeByGlobal = (name, value) => {
|
|
989
|
+
if (typeof window !== "undefined") {
|
|
990
|
+
window[name] = value;
|
|
991
|
+
}
|
|
992
|
+
if (typeof global !== "undefined") {
|
|
993
|
+
global[name] = value;
|
|
994
|
+
}
|
|
995
|
+
};
|
|
996
|
+
function getTag(value) {
|
|
997
|
+
return Object.prototype.toString.call(value);
|
|
998
|
+
}
|
|
999
|
+
function isEqual(value, other, seen = /* @__PURE__ */ new Map()) {
|
|
1000
|
+
if (value === other) {
|
|
1001
|
+
return true;
|
|
1002
|
+
}
|
|
1003
|
+
if (value == null || other == null) {
|
|
1004
|
+
return value === other;
|
|
1005
|
+
}
|
|
1006
|
+
if (value !== value && other !== other) {
|
|
1007
|
+
return true;
|
|
1008
|
+
}
|
|
1009
|
+
const valueType = typeof value;
|
|
1010
|
+
const otherType = typeof other;
|
|
1011
|
+
if (valueType !== otherType) {
|
|
1012
|
+
return false;
|
|
1013
|
+
}
|
|
1014
|
+
if (valueType !== "object") {
|
|
1015
|
+
return value === other;
|
|
1016
|
+
}
|
|
1017
|
+
const valueTag = getTag(value);
|
|
1018
|
+
const otherTag = getTag(other);
|
|
1019
|
+
if (valueTag !== otherTag) {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
if (seen.has(value)) {
|
|
1023
|
+
return seen.get(value) === other;
|
|
1024
|
+
}
|
|
1025
|
+
seen.set(value, other);
|
|
1026
|
+
if (valueTag === "[object Array]") {
|
|
1027
|
+
if (value.length !== other.length) {
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
for (let i2 = 0; i2 < value.length; i2++) {
|
|
1031
|
+
if (!isEqual(value[i2], other[i2], seen)) {
|
|
1032
|
+
return false;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
return true;
|
|
1036
|
+
}
|
|
1037
|
+
if (valueTag === "[object Date]") {
|
|
1038
|
+
return +value === +other;
|
|
1039
|
+
}
|
|
1040
|
+
if (valueTag === "[object RegExp]") {
|
|
1041
|
+
return value.toString() === other.toString();
|
|
1042
|
+
}
|
|
1043
|
+
if (valueTag === "[object Map]") {
|
|
1044
|
+
if (value.size !== other.size) {
|
|
1045
|
+
return false;
|
|
1046
|
+
}
|
|
1047
|
+
let matched = true;
|
|
1048
|
+
value.forEach((val, key) => {
|
|
1049
|
+
if (matched) {
|
|
1050
|
+
if (!other.has(key) || !isEqual(val, other.get(key), seen)) {
|
|
1051
|
+
matched = false;
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
});
|
|
1055
|
+
return matched;
|
|
1056
|
+
}
|
|
1057
|
+
if (valueTag === "[object Set]") {
|
|
1058
|
+
if (value.size !== other.size) {
|
|
1059
|
+
return false;
|
|
1060
|
+
}
|
|
1061
|
+
const valueArray = Array.from(value);
|
|
1062
|
+
const otherArray = Array.from(other);
|
|
1063
|
+
return valueArray.every((item) => {
|
|
1064
|
+
return otherArray.some((otherItem) => isEqual(item, otherItem, seen));
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
const valueKeys = Object.keys(value);
|
|
1068
|
+
const otherKeys = Object.keys(other);
|
|
1069
|
+
if (valueKeys.length !== otherKeys.length) {
|
|
1070
|
+
return false;
|
|
1071
|
+
}
|
|
1072
|
+
for (const key of valueKeys) {
|
|
1073
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) || !isEqual(value[key], other[key], seen)) {
|
|
1074
|
+
return false;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
return true;
|
|
1078
|
+
}
|
|
1079
|
+
const cloneDeep = (value, cloneMap = /* @__PURE__ */ new WeakMap()) => {
|
|
1080
|
+
if (value === null || typeof value !== "object") {
|
|
1081
|
+
return value;
|
|
1082
|
+
}
|
|
1083
|
+
if (cloneMap.has(value)) {
|
|
1084
|
+
return cloneMap.get(value);
|
|
1085
|
+
}
|
|
1086
|
+
if (value instanceof Date) {
|
|
1087
|
+
return new Date(value.getTime());
|
|
1088
|
+
}
|
|
1089
|
+
if (value instanceof RegExp) {
|
|
1090
|
+
return new RegExp(value.source, value.flags);
|
|
1091
|
+
}
|
|
1092
|
+
if (Array.isArray(value)) {
|
|
1093
|
+
const result = [];
|
|
1094
|
+
cloneMap.set(value, result);
|
|
1095
|
+
for (let i2 = 0; i2 < value.length; i2++) {
|
|
1096
|
+
result[i2] = cloneDeep(value[i2], cloneMap);
|
|
1097
|
+
}
|
|
1098
|
+
return result;
|
|
1099
|
+
}
|
|
1100
|
+
if (value instanceof Map) {
|
|
1101
|
+
const result = /* @__PURE__ */ new Map();
|
|
1102
|
+
cloneMap.set(value, result);
|
|
1103
|
+
value.forEach((val, key) => {
|
|
1104
|
+
result.set(typeof key === "object" && key !== null ? cloneDeep(key, cloneMap) : key, cloneDeep(val, cloneMap));
|
|
1105
|
+
});
|
|
1106
|
+
return result;
|
|
1107
|
+
}
|
|
1108
|
+
if (value instanceof Set) {
|
|
1109
|
+
const result = /* @__PURE__ */ new Set();
|
|
1110
|
+
cloneMap.set(value, result);
|
|
1111
|
+
value.forEach((val) => {
|
|
1112
|
+
result.add(cloneDeep(val, cloneMap));
|
|
1113
|
+
});
|
|
1114
|
+
return result;
|
|
1115
|
+
}
|
|
1116
|
+
if (typeof value === "object" && value.constructor === Object) {
|
|
1117
|
+
const result = {};
|
|
1118
|
+
cloneMap.set(value, result);
|
|
1119
|
+
[...Object.getOwnPropertyNames(value), ...Object.getOwnPropertySymbols(value)].forEach((key) => {
|
|
1120
|
+
const objValue = value;
|
|
1121
|
+
result[key] = cloneDeep(objValue[key], cloneMap);
|
|
1122
|
+
});
|
|
1123
|
+
return result;
|
|
1124
|
+
}
|
|
1125
|
+
try {
|
|
1126
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1127
|
+
const Constructor = prototype.constructor;
|
|
1128
|
+
const result = new Constructor();
|
|
1129
|
+
cloneMap.set(value, result);
|
|
1130
|
+
for (const key in value) {
|
|
1131
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
1132
|
+
const objValue = value;
|
|
1133
|
+
result[key] = cloneDeep(objValue[key], cloneMap);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
return result;
|
|
1137
|
+
} catch (_error) {
|
|
1138
|
+
console.warn(
|
|
1139
|
+
`Unable to deeply clone object of type ${Object.prototype.toString.call(value)}. Fallback to shallow copy.`
|
|
1140
|
+
);
|
|
1141
|
+
return { ...value };
|
|
1142
|
+
}
|
|
1143
|
+
};
|
|
1144
|
+
const sections = {
|
|
1145
|
+
172351395: { name: "EBML", type: "Container" },
|
|
1146
|
+
646: { name: "EBMLVersion", type: "Uint" },
|
|
1147
|
+
759: { name: "EBMLReadVersion", type: "Uint" },
|
|
1148
|
+
754: { name: "EBMLMaxIDLength", type: "Uint" },
|
|
1149
|
+
755: { name: "EBMLMaxSizeLength", type: "Uint" },
|
|
1150
|
+
642: { name: "DocType", type: "String" },
|
|
1151
|
+
647: { name: "DocTypeVersion", type: "Uint" },
|
|
1152
|
+
645: { name: "DocTypeReadVersion", type: "Uint" },
|
|
1153
|
+
108: { name: "Void", type: "Binary" },
|
|
1154
|
+
63: { name: "CRC-32", type: "Binary" },
|
|
1155
|
+
190023271: { name: "SignatureSlot", type: "Container" },
|
|
1156
|
+
16010: { name: "SignatureAlgo", type: "Uint" },
|
|
1157
|
+
16026: { name: "SignatureHash", type: "Uint" },
|
|
1158
|
+
16037: { name: "SignaturePublicKey", type: "Binary" },
|
|
1159
|
+
16053: { name: "Signature", type: "Binary" },
|
|
1160
|
+
15963: { name: "SignatureElements", type: "Container" },
|
|
1161
|
+
15995: { name: "SignatureElementList", type: "Container" },
|
|
1162
|
+
9522: { name: "SignedElement", type: "Binary" },
|
|
1163
|
+
139690087: { name: "Segment", type: "Container" },
|
|
1164
|
+
21863284: { name: "SeekHead", type: "Container" },
|
|
1165
|
+
3515: { name: "Seek", type: "Container" },
|
|
1166
|
+
5035: { name: "SeekID", type: "Binary" },
|
|
1167
|
+
5036: { name: "SeekPosition", type: "Uint" },
|
|
1168
|
+
88713574: { name: "Info", type: "Container" },
|
|
1169
|
+
13220: { name: "SegmentUID", type: "Binary" },
|
|
1170
|
+
13188: { name: "SegmentFilename", type: "String" },
|
|
1171
|
+
1882403: { name: "PrevUID", type: "Binary" },
|
|
1172
|
+
1868715: { name: "PrevFilename", type: "String" },
|
|
1173
|
+
2013475: { name: "NextUID", type: "Binary" },
|
|
1174
|
+
1999803: { name: "NextFilename", type: "String" },
|
|
1175
|
+
1092: { name: "SegmentFamily", type: "Binary" },
|
|
1176
|
+
10532: { name: "ChapterTranslate", type: "Container" },
|
|
1177
|
+
10748: { name: "ChapterTranslateEditionUID", type: "Uint" },
|
|
1178
|
+
10687: { name: "ChapterTranslateCodec", type: "Uint" },
|
|
1179
|
+
10661: { name: "ChapterTranslateID", type: "Binary" },
|
|
1180
|
+
710577: { name: "TimecodeScale", type: "Uint" },
|
|
1181
|
+
1161: { name: "Duration", type: "Float" },
|
|
1182
|
+
1121: { name: "DateUTC", type: "Date" },
|
|
1183
|
+
15273: { name: "Title", type: "String" },
|
|
1184
|
+
3456: { name: "MuxingApp", type: "String" },
|
|
1185
|
+
5953: { name: "WritingApp", type: "String" },
|
|
1186
|
+
// 0xf43b675: { name: 'Cluster', type: 'Container' },
|
|
1187
|
+
103: { name: "Timecode", type: "Uint" },
|
|
1188
|
+
6228: { name: "SilentTracks", type: "Container" },
|
|
1189
|
+
6359: { name: "SilentTrackNumber", type: "Uint" },
|
|
1190
|
+
39: { name: "Position", type: "Uint" },
|
|
1191
|
+
43: { name: "PrevSize", type: "Uint" },
|
|
1192
|
+
35: { name: "SimpleBlock", type: "Binary" },
|
|
1193
|
+
32: { name: "BlockGroup", type: "Container" },
|
|
1194
|
+
33: { name: "Block", type: "Binary" },
|
|
1195
|
+
34: { name: "BlockVirtual", type: "Binary" },
|
|
1196
|
+
13729: { name: "BlockAdditions", type: "Container" },
|
|
1197
|
+
38: { name: "BlockMore", type: "Container" },
|
|
1198
|
+
110: { name: "BlockAddID", type: "Uint" },
|
|
1199
|
+
37: { name: "BlockAdditional", type: "Binary" },
|
|
1200
|
+
27: { name: "BlockDuration", type: "Uint" },
|
|
1201
|
+
122: { name: "ReferencePriority", type: "Uint" },
|
|
1202
|
+
123: { name: "ReferenceBlock", type: "Int" },
|
|
1203
|
+
125: { name: "ReferenceVirtual", type: "Int" },
|
|
1204
|
+
36: { name: "CodecState", type: "Binary" },
|
|
1205
|
+
13730: { name: "DiscardPadding", type: "Int" },
|
|
1206
|
+
14: { name: "Slices", type: "Container" },
|
|
1207
|
+
104: { name: "TimeSlice", type: "Container" },
|
|
1208
|
+
76: { name: "LaceNumber", type: "Uint" },
|
|
1209
|
+
77: { name: "FrameNumber", type: "Uint" },
|
|
1210
|
+
75: { name: "BlockAdditionID", type: "Uint" },
|
|
1211
|
+
78: { name: "Delay", type: "Uint" },
|
|
1212
|
+
79: { name: "SliceDuration", type: "Uint" },
|
|
1213
|
+
72: { name: "ReferenceFrame", type: "Container" },
|
|
1214
|
+
73: { name: "ReferenceOffset", type: "Uint" },
|
|
1215
|
+
74: { name: "ReferenceTimeCode", type: "Uint" },
|
|
1216
|
+
47: { name: "EncryptedBlock", type: "Binary" },
|
|
1217
|
+
106212971: { name: "Tracks", type: "Container" },
|
|
1218
|
+
46: { name: "TrackEntry", type: "Container" },
|
|
1219
|
+
87: { name: "TrackNumber", type: "Uint" },
|
|
1220
|
+
13253: { name: "TrackUID", type: "Uint" },
|
|
1221
|
+
3: { name: "TrackType", type: "Uint" },
|
|
1222
|
+
57: { name: "FlagEnabled", type: "Uint" },
|
|
1223
|
+
8: { name: "FlagDefault", type: "Uint" },
|
|
1224
|
+
5546: { name: "FlagForced", type: "Uint" },
|
|
1225
|
+
28: { name: "FlagLacing", type: "Uint" },
|
|
1226
|
+
11751: { name: "MinCache", type: "Uint" },
|
|
1227
|
+
11768: { name: "MaxCache", type: "Uint" },
|
|
1228
|
+
254851: { name: "DefaultDuration", type: "Uint" },
|
|
1229
|
+
216698: { name: "DefaultDecodedFieldDuration", type: "Uint" },
|
|
1230
|
+
209231: { name: "TrackTimecodeScale", type: "Float" },
|
|
1231
|
+
4991: { name: "TrackOffset", type: "Int" },
|
|
1232
|
+
5614: { name: "MaxBlockAdditionID", type: "Uint" },
|
|
1233
|
+
4974: { name: "Name", type: "String" },
|
|
1234
|
+
177564: { name: "Language", type: "String" },
|
|
1235
|
+
6: { name: "CodecID", type: "String" },
|
|
1236
|
+
9122: { name: "CodecPrivate", type: "Binary" },
|
|
1237
|
+
362120: { name: "CodecName", type: "String" },
|
|
1238
|
+
13382: { name: "AttachmentLink", type: "Uint" },
|
|
1239
|
+
1742487: { name: "CodecSettings", type: "String" },
|
|
1240
|
+
1785920: { name: "CodecInfoURL", type: "String" },
|
|
1241
|
+
438848: { name: "CodecDownloadURL", type: "String" },
|
|
1242
|
+
42: { name: "CodecDecodeAll", type: "Uint" },
|
|
1243
|
+
12203: { name: "TrackOverlay", type: "Uint" },
|
|
1244
|
+
5802: { name: "CodecDelay", type: "Uint" },
|
|
1245
|
+
5819: { name: "SeekPreRoll", type: "Uint" },
|
|
1246
|
+
9764: { name: "TrackTranslate", type: "Container" },
|
|
1247
|
+
9980: { name: "TrackTranslateEditionUID", type: "Uint" },
|
|
1248
|
+
9919: { name: "TrackTranslateCodec", type: "Uint" },
|
|
1249
|
+
9893: { name: "TrackTranslateTrackID", type: "Binary" },
|
|
1250
|
+
96: { name: "Video", type: "Container" },
|
|
1251
|
+
26: { name: "FlagInterlaced", type: "Uint" },
|
|
1252
|
+
5048: { name: "StereoMode", type: "Uint" },
|
|
1253
|
+
5056: { name: "AlphaMode", type: "Uint" },
|
|
1254
|
+
5049: { name: "OldStereoMode", type: "Uint" },
|
|
1255
|
+
48: { name: "PixelWidth", type: "Uint" },
|
|
1256
|
+
58: { name: "PixelHeight", type: "Uint" },
|
|
1257
|
+
5290: { name: "PixelCropBottom", type: "Uint" },
|
|
1258
|
+
5307: { name: "PixelCropTop", type: "Uint" },
|
|
1259
|
+
5324: { name: "PixelCropLeft", type: "Uint" },
|
|
1260
|
+
5341: { name: "PixelCropRight", type: "Uint" },
|
|
1261
|
+
5296: { name: "DisplayWidth", type: "Uint" },
|
|
1262
|
+
5306: { name: "DisplayHeight", type: "Uint" },
|
|
1263
|
+
5298: { name: "DisplayUnit", type: "Uint" },
|
|
1264
|
+
5299: { name: "AspectRatioType", type: "Uint" },
|
|
1265
|
+
963876: { name: "ColourSpace", type: "Binary" },
|
|
1266
|
+
1029411: { name: "GammaValue", type: "Float" },
|
|
1267
|
+
230371: { name: "FrameRate", type: "Float" },
|
|
1268
|
+
97: { name: "Audio", type: "Container" },
|
|
1269
|
+
53: { name: "SamplingFrequency", type: "Float" },
|
|
1270
|
+
14517: { name: "OutputSamplingFrequency", type: "Float" },
|
|
1271
|
+
31: { name: "Channels", type: "Uint" },
|
|
1272
|
+
15739: { name: "ChannelPositions", type: "Binary" },
|
|
1273
|
+
8804: { name: "BitDepth", type: "Uint" },
|
|
1274
|
+
98: { name: "TrackOperation", type: "Container" },
|
|
1275
|
+
99: { name: "TrackCombinePlanes", type: "Container" },
|
|
1276
|
+
100: { name: "TrackPlane", type: "Container" },
|
|
1277
|
+
101: { name: "TrackPlaneUID", type: "Uint" },
|
|
1278
|
+
102: { name: "TrackPlaneType", type: "Uint" },
|
|
1279
|
+
105: { name: "TrackJoinBlocks", type: "Container" },
|
|
1280
|
+
109: { name: "TrackJoinUID", type: "Uint" },
|
|
1281
|
+
64: { name: "TrickTrackUID", type: "Uint" },
|
|
1282
|
+
65: { name: "TrickTrackSegmentUID", type: "Binary" },
|
|
1283
|
+
70: { name: "TrickTrackFlag", type: "Uint" },
|
|
1284
|
+
71: { name: "TrickMasterTrackUID", type: "Uint" },
|
|
1285
|
+
68: { name: "TrickMasterTrackSegmentUID", type: "Binary" },
|
|
1286
|
+
11648: { name: "ContentEncodings", type: "Container" },
|
|
1287
|
+
8768: { name: "ContentEncoding", type: "Container" },
|
|
1288
|
+
4145: { name: "ContentEncodingOrder", type: "Uint" },
|
|
1289
|
+
4146: { name: "ContentEncodingScope", type: "Uint" },
|
|
1290
|
+
4147: { name: "ContentEncodingType", type: "Uint" },
|
|
1291
|
+
4148: { name: "ContentCompression", type: "Container" },
|
|
1292
|
+
596: { name: "ContentCompAlgo", type: "Uint" },
|
|
1293
|
+
597: { name: "ContentCompSettings", type: "Binary" },
|
|
1294
|
+
4149: { name: "ContentEncryption", type: "Container" },
|
|
1295
|
+
2017: { name: "ContentEncAlgo", type: "Uint" },
|
|
1296
|
+
2018: { name: "ContentEncKeyID", type: "Binary" },
|
|
1297
|
+
2019: { name: "ContentSignature", type: "Binary" },
|
|
1298
|
+
2020: { name: "ContentSigKeyID", type: "Binary" },
|
|
1299
|
+
2021: { name: "ContentSigAlgo", type: "Uint" },
|
|
1300
|
+
2022: { name: "ContentSigHashAlgo", type: "Uint" },
|
|
1301
|
+
206814059: { name: "Cues", type: "Container" },
|
|
1302
|
+
59: { name: "CuePoint", type: "Container" },
|
|
1303
|
+
51: { name: "CueTime", type: "Uint" },
|
|
1304
|
+
55: { name: "CueTrackPositions", type: "Container" },
|
|
1305
|
+
119: { name: "CueTrack", type: "Uint" },
|
|
1306
|
+
113: { name: "CueClusterPosition", type: "Uint" },
|
|
1307
|
+
112: { name: "CueRelativePosition", type: "Uint" },
|
|
1308
|
+
50: { name: "CueDuration", type: "Uint" },
|
|
1309
|
+
4984: { name: "CueBlockNumber", type: "Uint" },
|
|
1310
|
+
106: { name: "CueCodecState", type: "Uint" },
|
|
1311
|
+
91: { name: "CueReference", type: "Container" },
|
|
1312
|
+
22: { name: "CueRefTime", type: "Uint" },
|
|
1313
|
+
23: { name: "CueRefCluster", type: "Uint" },
|
|
1314
|
+
4959: { name: "CueRefNumber", type: "Uint" },
|
|
1315
|
+
107: { name: "CueRefCodecState", type: "Uint" },
|
|
1316
|
+
155296873: { name: "Attachments", type: "Container" },
|
|
1317
|
+
8615: { name: "AttachedFile", type: "Container" },
|
|
1318
|
+
1662: { name: "FileDescription", type: "String" },
|
|
1319
|
+
1646: { name: "FileName", type: "String" },
|
|
1320
|
+
1632: { name: "FileMimeType", type: "String" },
|
|
1321
|
+
1628: { name: "FileData", type: "Binary" },
|
|
1322
|
+
1710: { name: "FileUID", type: "Uint" },
|
|
1323
|
+
1653: { name: "FileReferral", type: "Binary" },
|
|
1324
|
+
1633: { name: "FileUsedStartTime", type: "Uint" },
|
|
1325
|
+
1634: { name: "FileUsedEndTime", type: "Uint" },
|
|
1326
|
+
4433776: { name: "Chapters", type: "Container" },
|
|
1327
|
+
1465: { name: "EditionEntry", type: "Container" },
|
|
1328
|
+
1468: { name: "EditionUID", type: "Uint" },
|
|
1329
|
+
1469: { name: "EditionFlagHidden", type: "Uint" },
|
|
1330
|
+
1499: { name: "EditionFlagDefault", type: "Uint" },
|
|
1331
|
+
1501: { name: "EditionFlagOrdered", type: "Uint" },
|
|
1332
|
+
54: { name: "ChapterAtom", type: "Container" },
|
|
1333
|
+
13252: { name: "ChapterUID", type: "Uint" },
|
|
1334
|
+
5716: { name: "ChapterStringUID", type: "String" },
|
|
1335
|
+
17: { name: "ChapterTimeStart", type: "Uint" },
|
|
1336
|
+
18: { name: "ChapterTimeEnd", type: "Uint" },
|
|
1337
|
+
24: { name: "ChapterFlagHidden", type: "Uint" },
|
|
1338
|
+
1432: { name: "ChapterFlagEnabled", type: "Uint" },
|
|
1339
|
+
11879: { name: "ChapterSegmentUID", type: "Binary" },
|
|
1340
|
+
11964: { name: "ChapterSegmentEditionUID", type: "Uint" },
|
|
1341
|
+
9155: { name: "ChapterPhysicalEquiv", type: "Uint" },
|
|
1342
|
+
15: { name: "ChapterTrack", type: "Container" },
|
|
1343
|
+
9: { name: "ChapterTrackNumber", type: "Uint" },
|
|
1344
|
+
0: { name: "ChapterDisplay", type: "Container" },
|
|
1345
|
+
5: { name: "ChapString", type: "String" },
|
|
1346
|
+
892: { name: "ChapLanguage", type: "String" },
|
|
1347
|
+
894: { name: "ChapCountry", type: "String" },
|
|
1348
|
+
10564: { name: "ChapProcess", type: "Container" },
|
|
1349
|
+
10581: { name: "ChapProcessCodecID", type: "Uint" },
|
|
1350
|
+
1293: { name: "ChapProcessPrivate", type: "Binary" },
|
|
1351
|
+
10513: { name: "ChapProcessCommand", type: "Container" },
|
|
1352
|
+
10530: { name: "ChapProcessTime", type: "Uint" },
|
|
1353
|
+
10547: { name: "ChapProcessData", type: "Binary" },
|
|
1354
|
+
39109479: { name: "Tags", type: "Container" },
|
|
1355
|
+
13171: { name: "Tag", type: "Container" },
|
|
1356
|
+
9152: { name: "Targets", type: "Container" },
|
|
1357
|
+
10442: { name: "TargetTypeValue", type: "Uint" },
|
|
1358
|
+
9162: { name: "TargetType", type: "String" },
|
|
1359
|
+
9157: { name: "TagTrackUID", type: "Uint" },
|
|
1360
|
+
9161: { name: "TagEditionUID", type: "Uint" },
|
|
1361
|
+
9156: { name: "TagChapterUID", type: "Uint" },
|
|
1362
|
+
9158: { name: "TagAttachmentUID", type: "Uint" },
|
|
1363
|
+
10184: { name: "SimpleTag", type: "Container" },
|
|
1364
|
+
1443: { name: "TagName", type: "String" },
|
|
1365
|
+
1146: { name: "TagLanguage", type: "String" },
|
|
1366
|
+
1156: { name: "TagDefault", type: "Uint" },
|
|
1367
|
+
1159: { name: "TagString", type: "String" },
|
|
1368
|
+
1157: { name: "TagBinary", type: "Binary" }
|
|
1369
|
+
};
|
|
1370
|
+
class WebmBase {
|
|
1371
|
+
constructor(name = "Unknown", type = "Unknown") {
|
|
1372
|
+
__publicField(this, "source");
|
|
1373
|
+
__publicField(this, "data");
|
|
1374
|
+
this.name = name;
|
|
1375
|
+
this.type = type;
|
|
1376
|
+
}
|
|
1377
|
+
updateBySource() {
|
|
1378
|
+
}
|
|
1379
|
+
setSource(source) {
|
|
1380
|
+
this.source = source;
|
|
1381
|
+
this.updateBySource();
|
|
1382
|
+
}
|
|
1383
|
+
updateByData() {
|
|
1384
|
+
}
|
|
1385
|
+
setData(data) {
|
|
1386
|
+
this.data = data;
|
|
1387
|
+
this.updateByData();
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
function padHex(hex) {
|
|
1391
|
+
return hex.length % 2 === 1 ? "0" + hex : hex;
|
|
1392
|
+
}
|
|
1393
|
+
class WebmUint extends WebmBase {
|
|
1394
|
+
constructor(name, type) {
|
|
1395
|
+
super(name, type || "Uint");
|
|
1396
|
+
}
|
|
1397
|
+
updateBySource() {
|
|
1398
|
+
this.data = "";
|
|
1399
|
+
for (let i2 = 0; i2 < this.source.length; i2++) {
|
|
1400
|
+
const hex = this.source[i2].toString(16);
|
|
1401
|
+
this.data += padHex(hex);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
updateByData() {
|
|
1405
|
+
const length = this.data.length / 2;
|
|
1406
|
+
this.source = new Uint8Array(length);
|
|
1407
|
+
for (let i2 = 0; i2 < length; i2++) {
|
|
1408
|
+
const hex = this.data.substr(i2 * 2, 2);
|
|
1409
|
+
this.source[i2] = parseInt(hex, 16);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
getValue() {
|
|
1413
|
+
return parseInt(this.data, 16);
|
|
1414
|
+
}
|
|
1415
|
+
setValue(value) {
|
|
1416
|
+
this.setData(padHex(value.toString(16)));
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
class WebmFloat extends WebmBase {
|
|
1420
|
+
constructor(name, type) {
|
|
1421
|
+
super(name, type || "Float");
|
|
1422
|
+
}
|
|
1423
|
+
getFloatArrayType() {
|
|
1424
|
+
return this.source && this.source.length === 4 ? Float32Array : Float64Array;
|
|
1425
|
+
}
|
|
1426
|
+
updateBySource() {
|
|
1427
|
+
const byteArray = this.source.reverse();
|
|
1428
|
+
const floatArrayType = this.getFloatArrayType();
|
|
1429
|
+
const floatArray = new floatArrayType(byteArray.buffer);
|
|
1430
|
+
this.data = floatArray[0];
|
|
1431
|
+
}
|
|
1432
|
+
updateByData() {
|
|
1433
|
+
const floatArrayType = this.getFloatArrayType();
|
|
1434
|
+
const floatArray = new floatArrayType([this.data]);
|
|
1435
|
+
const byteArray = new Uint8Array(floatArray.buffer);
|
|
1436
|
+
this.source = byteArray.reverse();
|
|
1437
|
+
}
|
|
1438
|
+
getValue() {
|
|
1439
|
+
return this.data;
|
|
1440
|
+
}
|
|
1441
|
+
setValue(value) {
|
|
1442
|
+
this.setData(value);
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
class WebmContainer extends WebmBase {
|
|
1446
|
+
constructor(name, type) {
|
|
1447
|
+
super(name, type || "Container");
|
|
1448
|
+
__publicField(this, "offset", 0);
|
|
1449
|
+
__publicField(this, "data", []);
|
|
1450
|
+
}
|
|
1451
|
+
readByte() {
|
|
1452
|
+
return this.source[this.offset++];
|
|
1453
|
+
}
|
|
1454
|
+
readUint() {
|
|
1455
|
+
const firstByte = this.readByte();
|
|
1456
|
+
const bytes = 8 - firstByte.toString(2).length;
|
|
1457
|
+
let value = firstByte - (1 << 7 - bytes);
|
|
1458
|
+
for (let i2 = 0; i2 < bytes; i2++) {
|
|
1459
|
+
value *= 256;
|
|
1460
|
+
value += this.readByte();
|
|
1461
|
+
}
|
|
1462
|
+
return value;
|
|
1463
|
+
}
|
|
1464
|
+
updateBySource() {
|
|
1465
|
+
let end = void 0;
|
|
1466
|
+
this.data = [];
|
|
1467
|
+
for (this.offset = 0; this.offset < this.source.length; this.offset = end) {
|
|
1468
|
+
const id = this.readUint();
|
|
1469
|
+
const len = this.readUint();
|
|
1470
|
+
end = Math.min(this.offset + len, this.source.length);
|
|
1471
|
+
const data = this.source.slice(this.offset, end);
|
|
1472
|
+
const info = sections[id] || { name: "Unknown", type: "Unknown" };
|
|
1473
|
+
let ctr = WebmBase;
|
|
1474
|
+
switch (info.type) {
|
|
1475
|
+
case "Container":
|
|
1476
|
+
ctr = WebmContainer;
|
|
1477
|
+
break;
|
|
1478
|
+
case "Uint":
|
|
1479
|
+
ctr = WebmUint;
|
|
1480
|
+
break;
|
|
1481
|
+
case "Float":
|
|
1482
|
+
ctr = WebmFloat;
|
|
1483
|
+
break;
|
|
1484
|
+
}
|
|
1485
|
+
const section = new ctr(info.name, info.type);
|
|
1486
|
+
section.setSource(data);
|
|
1487
|
+
this.data.push({
|
|
1488
|
+
id,
|
|
1489
|
+
idHex: id.toString(16),
|
|
1490
|
+
data: section
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
writeUint(x2, draft = false) {
|
|
1495
|
+
let bytes = 1, flag = 128;
|
|
1496
|
+
while (x2 >= flag && bytes < 8) {
|
|
1497
|
+
bytes++, flag *= 128;
|
|
1498
|
+
}
|
|
1499
|
+
if (!draft) {
|
|
1500
|
+
let value = flag + x2;
|
|
1501
|
+
for (let i2 = bytes - 1; i2 >= 0; i2--) {
|
|
1502
|
+
const c2 = value % 256;
|
|
1503
|
+
this.source[this.offset + i2] = c2;
|
|
1504
|
+
value = (value - c2) / 256;
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
this.offset += bytes;
|
|
1508
|
+
}
|
|
1509
|
+
writeSections(draft = false) {
|
|
1510
|
+
this.offset = 0;
|
|
1511
|
+
for (let i2 = 0; i2 < this.data.length; i2++) {
|
|
1512
|
+
const section = this.data[i2], content = section.data.source, contentLength = content.length;
|
|
1513
|
+
this.writeUint(section.id, draft);
|
|
1514
|
+
this.writeUint(contentLength, draft);
|
|
1515
|
+
if (!draft) {
|
|
1516
|
+
this.source.set(content, this.offset);
|
|
1517
|
+
}
|
|
1518
|
+
this.offset += contentLength;
|
|
1519
|
+
}
|
|
1520
|
+
return this.offset;
|
|
1521
|
+
}
|
|
1522
|
+
updateByData() {
|
|
1523
|
+
const length = this.writeSections(true);
|
|
1524
|
+
this.source = new Uint8Array(length);
|
|
1525
|
+
this.writeSections();
|
|
1526
|
+
}
|
|
1527
|
+
getSectionById(id) {
|
|
1528
|
+
for (let i2 = 0; i2 < this.data.length; i2++) {
|
|
1529
|
+
const section = this.data[i2];
|
|
1530
|
+
if (section.id === id) {
|
|
1531
|
+
return section.data;
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
return void 0;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
class WebmFile extends WebmContainer {
|
|
1538
|
+
constructor(source) {
|
|
1539
|
+
super("File", "File");
|
|
1540
|
+
this.setSource(source);
|
|
1541
|
+
}
|
|
1542
|
+
fixDuration(duration) {
|
|
1543
|
+
const segmentSection = this.getSectionById(139690087);
|
|
1544
|
+
if (!segmentSection) {
|
|
1545
|
+
return false;
|
|
1546
|
+
}
|
|
1547
|
+
const infoSection = segmentSection.getSectionById(88713574);
|
|
1548
|
+
if (!infoSection) {
|
|
1549
|
+
return false;
|
|
1550
|
+
}
|
|
1551
|
+
const timeScaleSection = infoSection.getSectionById(710577);
|
|
1552
|
+
if (!timeScaleSection) {
|
|
1553
|
+
return false;
|
|
1554
|
+
}
|
|
1555
|
+
let durationSection = infoSection.getSectionById(1161);
|
|
1556
|
+
if (durationSection) {
|
|
1557
|
+
if (durationSection.getValue() <= 0) {
|
|
1558
|
+
durationSection.setValue(duration);
|
|
1559
|
+
} else {
|
|
1560
|
+
return false;
|
|
1561
|
+
}
|
|
1562
|
+
} else {
|
|
1563
|
+
durationSection = new WebmFloat("Duration", "Float");
|
|
1564
|
+
durationSection.setValue(duration);
|
|
1565
|
+
infoSection.data.push({
|
|
1566
|
+
id: 1161,
|
|
1567
|
+
data: durationSection
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
timeScaleSection.setValue(1e6);
|
|
1571
|
+
infoSection.updateByData();
|
|
1572
|
+
segmentSection.updateByData();
|
|
1573
|
+
this.updateByData();
|
|
1574
|
+
return true;
|
|
1575
|
+
}
|
|
1576
|
+
toBlob(type = "video/webm") {
|
|
1577
|
+
return new Blob([this.source.buffer], { type });
|
|
1578
|
+
}
|
|
1579
|
+
}
|
|
1580
|
+
const webmFixDuration = (blob, duration, type = "video/webm") => {
|
|
1581
|
+
return new Promise((resolve, reject) => {
|
|
1582
|
+
try {
|
|
1583
|
+
const reader = new FileReader();
|
|
1584
|
+
reader.addEventListener("loadend", () => {
|
|
1585
|
+
try {
|
|
1586
|
+
const result = reader.result;
|
|
1587
|
+
const file = new WebmFile(new Uint8Array(result));
|
|
1588
|
+
if (file.fixDuration(duration)) {
|
|
1589
|
+
resolve(file.toBlob(type));
|
|
1590
|
+
} else {
|
|
1591
|
+
resolve(blob);
|
|
1592
|
+
}
|
|
1593
|
+
} catch (ex) {
|
|
1594
|
+
reject(ex);
|
|
1595
|
+
}
|
|
1596
|
+
});
|
|
1597
|
+
reader.addEventListener("error", () => reject());
|
|
1598
|
+
reader.readAsArrayBuffer(blob);
|
|
1599
|
+
} catch (ex) {
|
|
1600
|
+
reject(ex);
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
};
|
|
1604
|
+
function getMimeType() {
|
|
1605
|
+
const types = ["audio/webm", "audio/mp4", "audio/ogg", "audio/wav", "audio/aac"];
|
|
1606
|
+
for (let i2 = 0; i2 < types.length; i2++) {
|
|
1607
|
+
if (MediaRecorder.isTypeSupported(types[i2])) {
|
|
1608
|
+
return types[i2];
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
return void 0;
|
|
1612
|
+
}
|
|
1613
|
+
class AudioRecorder {
|
|
1614
|
+
constructor() {
|
|
1615
|
+
__publicField(this, "startTime");
|
|
1616
|
+
__publicField(this, "chunks");
|
|
1617
|
+
__publicField(this, "mediaRecorder");
|
|
1618
|
+
__publicField(this, "blob");
|
|
1619
|
+
this.startTime = Date.now();
|
|
1620
|
+
this.chunks = [];
|
|
1621
|
+
navigator.mediaDevices.getUserMedia({
|
|
1622
|
+
audio: true
|
|
1623
|
+
}).then((stream) => {
|
|
1624
|
+
this.init(stream);
|
|
1625
|
+
}).catch((error) => {
|
|
1626
|
+
});
|
|
1627
|
+
}
|
|
1628
|
+
init(stream) {
|
|
1629
|
+
var _a, _b;
|
|
1630
|
+
const mimeType = getMimeType();
|
|
1631
|
+
this.mediaRecorder = new MediaRecorder(stream, {
|
|
1632
|
+
mimeType
|
|
1633
|
+
});
|
|
1634
|
+
(_a = this.mediaRecorder) == null ? void 0 : _a.addEventListener("dataavailable", async (event) => {
|
|
1635
|
+
var _a2;
|
|
1636
|
+
if (event.data.size > 0) {
|
|
1637
|
+
this.chunks.push(event.data);
|
|
1638
|
+
}
|
|
1639
|
+
if (((_a2 = this.mediaRecorder) == null ? void 0 : _a2.state) === "inactive") {
|
|
1640
|
+
const duration = Date.now() - this.startTime;
|
|
1641
|
+
this.blob = new Blob(this.chunks, { type: mimeType });
|
|
1642
|
+
if (mimeType === "audio/webm") {
|
|
1643
|
+
this.blob = await webmFixDuration(this.blob, duration, this.blob.type);
|
|
1644
|
+
}
|
|
1645
|
+
this.chunks = [];
|
|
1646
|
+
}
|
|
1647
|
+
});
|
|
1648
|
+
(_b = this.mediaRecorder) == null ? void 0 : _b.start();
|
|
1649
|
+
}
|
|
1650
|
+
start() {
|
|
1651
|
+
var _a;
|
|
1652
|
+
if (((_a = this.mediaRecorder) == null ? void 0 : _a.state) === "paused") {
|
|
1653
|
+
this.mediaRecorder.resume();
|
|
1654
|
+
}
|
|
1655
|
+
return this.mediaRecorder;
|
|
1656
|
+
}
|
|
1657
|
+
pause() {
|
|
1658
|
+
var _a;
|
|
1659
|
+
if (((_a = this.mediaRecorder) == null ? void 0 : _a.state) === "recording") {
|
|
1660
|
+
this.mediaRecorder.pause();
|
|
1661
|
+
}
|
|
1662
|
+
return this.mediaRecorder;
|
|
1663
|
+
}
|
|
1664
|
+
stop() {
|
|
1665
|
+
var _a;
|
|
1666
|
+
(_a = this.mediaRecorder) == null ? void 0 : _a.stop();
|
|
1667
|
+
return this.blob;
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
const subscribers = new SyncHook();
|
|
1671
|
+
const createSignal = (value, options) => {
|
|
1672
|
+
const signal = {
|
|
1673
|
+
value,
|
|
1674
|
+
compare: cloneDeep(value),
|
|
1675
|
+
// 订阅者
|
|
1676
|
+
subscribers,
|
|
1677
|
+
comparator: options == null ? void 0 : options.equals
|
|
1678
|
+
};
|
|
1679
|
+
const { subscriber } = options || {};
|
|
1680
|
+
const getter = () => {
|
|
1681
|
+
return signal.value;
|
|
1682
|
+
};
|
|
1683
|
+
const updateSignal = (newValue) => {
|
|
1684
|
+
if (!isEqual(signal.compare, newValue)) {
|
|
1685
|
+
signal.value = newValue;
|
|
1686
|
+
signal.compare = cloneDeep(newValue);
|
|
1687
|
+
if (subscriber) {
|
|
1688
|
+
signal.subscribers.call(subscriber);
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1691
|
+
};
|
|
1692
|
+
const setter = (newValue) => {
|
|
1693
|
+
const { comparator } = signal;
|
|
1694
|
+
if (comparator instanceof Function) {
|
|
1695
|
+
return !comparator(signal.value, newValue) && updateSignal(newValue);
|
|
1696
|
+
}
|
|
1697
|
+
if (comparator === void 0) {
|
|
1698
|
+
if (signal.value !== newValue) {
|
|
1699
|
+
updateSignal(newValue);
|
|
1700
|
+
}
|
|
1701
|
+
} else {
|
|
1702
|
+
!comparator && updateSignal(newValue);
|
|
1703
|
+
}
|
|
1704
|
+
};
|
|
1705
|
+
return [getter, setter];
|
|
1706
|
+
};
|
|
1707
|
+
const handleClick = (hooks = noop) => {
|
|
1708
|
+
if (typeof document !== "undefined") {
|
|
1709
|
+
document.addEventListener(
|
|
1710
|
+
"click",
|
|
1711
|
+
function(event) {
|
|
1712
|
+
hooks(event);
|
|
1713
|
+
},
|
|
1714
|
+
true
|
|
1715
|
+
);
|
|
1716
|
+
}
|
|
1717
|
+
};
|
|
1718
|
+
const webglVendor = () => {
|
|
1719
|
+
const glCanvas = document.createElement("canvas");
|
|
1720
|
+
const gl = glCanvas.getContext("webgl2");
|
|
1721
|
+
function getHardwareInfo(gl2) {
|
|
1722
|
+
const debugInfo = gl2.getExtension("WEBGL_debug_renderer_info");
|
|
1723
|
+
if (!debugInfo) {
|
|
1724
|
+
return null;
|
|
1725
|
+
}
|
|
1726
|
+
const vendor = gl2.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL);
|
|
1727
|
+
const renderer = gl2.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL);
|
|
1728
|
+
return {
|
|
1729
|
+
vendor,
|
|
1730
|
+
renderer
|
|
1731
|
+
};
|
|
1732
|
+
}
|
|
1733
|
+
if (gl) {
|
|
1734
|
+
return getHardwareInfo(gl);
|
|
1735
|
+
}
|
|
1736
|
+
return null;
|
|
1737
|
+
};
|
|
1738
|
+
const canvasVendor = () => {
|
|
1739
|
+
const outScreenCanvas = document.createElement("canvas");
|
|
1740
|
+
const ctx = outScreenCanvas.getContext("2d");
|
|
1741
|
+
const txt = "BrowserLeaks,com <canvas> 1.0";
|
|
1742
|
+
if (!ctx) return null;
|
|
1743
|
+
ctx.textBaseline = "top";
|
|
1744
|
+
ctx.font = "14px 'Arial'";
|
|
1745
|
+
ctx.textBaseline = "alphabetic";
|
|
1746
|
+
ctx.fillStyle = "#f60";
|
|
1747
|
+
ctx.fillRect(125, 1, 62, 20);
|
|
1748
|
+
ctx.fillStyle = "#069";
|
|
1749
|
+
ctx.fillText(txt, 2, 15);
|
|
1750
|
+
ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
|
|
1751
|
+
ctx.fillText(txt, 4, 17);
|
|
1752
|
+
const canvasImageData = outScreenCanvas.toDataURL();
|
|
1753
|
+
return canvasImageData;
|
|
1754
|
+
};
|
|
1755
|
+
const audioVendor = () => {
|
|
1756
|
+
return new Promise((resolve, reject) => {
|
|
1757
|
+
if (!isClient) reject("window is undefined");
|
|
1758
|
+
const each = function(obj, iterator) {
|
|
1759
|
+
if (Array.prototype.forEach && obj.forEach === Array.prototype.forEach) {
|
|
1760
|
+
obj.forEach(iterator);
|
|
1761
|
+
} else if (obj.length === +obj.length) {
|
|
1762
|
+
for (let i2 = 0, l2 = obj.length; i2 < l2; i2++) {
|
|
1763
|
+
iterator(obj[i2], i2, obj);
|
|
1764
|
+
}
|
|
1765
|
+
} else {
|
|
1766
|
+
for (const key in obj) {
|
|
1767
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
1768
|
+
iterator(obj[key], Number(key), obj);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
};
|
|
1773
|
+
const AudioContext = window.OfflineAudioContext || window.webkitOfflineAudioContext;
|
|
1774
|
+
const context = new AudioContext(1, 44100, 44100);
|
|
1775
|
+
const oscillator = context.createOscillator();
|
|
1776
|
+
oscillator.type = "triangle";
|
|
1777
|
+
oscillator.frequency.setValueAtTime(1e4, context.currentTime);
|
|
1778
|
+
const compressor = context.createDynamicsCompressor();
|
|
1779
|
+
each(
|
|
1780
|
+
[
|
|
1781
|
+
["threshold", -50],
|
|
1782
|
+
["knee", 40],
|
|
1783
|
+
["ratio", 12],
|
|
1784
|
+
["reduction", -20],
|
|
1785
|
+
["attack", 0],
|
|
1786
|
+
["release", 0.25]
|
|
1787
|
+
],
|
|
1788
|
+
function(item) {
|
|
1789
|
+
if (compressor[item[0]] && typeof compressor[item[0]] !== "number") {
|
|
1790
|
+
const { setValueAtTime } = compressor[item[0]];
|
|
1791
|
+
setValueAtTime(item[1], context.currentTime);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
);
|
|
1795
|
+
oscillator.connect(compressor);
|
|
1796
|
+
compressor.connect(context.destination);
|
|
1797
|
+
oscillator.start(0);
|
|
1798
|
+
context.startRendering();
|
|
1799
|
+
const audioTimeoutId = setTimeout(function() {
|
|
1800
|
+
context.oncomplete = () => {
|
|
1801
|
+
};
|
|
1802
|
+
reject("audioTimeout");
|
|
1803
|
+
return "audioTimeout";
|
|
1804
|
+
}, 100);
|
|
1805
|
+
context.oncomplete = (event) => {
|
|
1806
|
+
try {
|
|
1807
|
+
clearTimeout(audioTimeoutId);
|
|
1808
|
+
const result = event.renderedBuffer.getChannelData(0).slice(4500, 5e3).reduce(function(acc, val) {
|
|
1809
|
+
return acc + Math.abs(val);
|
|
1810
|
+
}, 0).toString();
|
|
1811
|
+
oscillator.disconnect();
|
|
1812
|
+
compressor.disconnect();
|
|
1813
|
+
resolve(result);
|
|
1814
|
+
return result;
|
|
1815
|
+
} catch (error) {
|
|
1816
|
+
reject(error);
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
};
|
|
1820
|
+
});
|
|
1821
|
+
};
|
|
1822
|
+
const b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1823
|
+
const arraybuffer_error = "ARRAYBUFFER not supported by this environment";
|
|
1824
|
+
const uint8array_error = "UINT8ARRAY not supported by this environment";
|
|
1825
|
+
function str2packed(str, utfType, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1826
|
+
let codePnt, codePntArr, byteCnt = 0, i2, j2, intOffset, byteOffset, shiftModifier, transposeBytes;
|
|
1827
|
+
existingPackedLen = existingPackedLen || 0;
|
|
1828
|
+
const packed = existingPacked || [0], existingByteLen = existingPackedLen >>> 3;
|
|
1829
|
+
if ("UTF8" === utfType) {
|
|
1830
|
+
shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
1831
|
+
for (i2 = 0; i2 < str.length; i2 += 1) {
|
|
1832
|
+
codePnt = str.charCodeAt(i2);
|
|
1833
|
+
codePntArr = [];
|
|
1834
|
+
if (128 > codePnt) {
|
|
1835
|
+
codePntArr.push(codePnt);
|
|
1836
|
+
} else if (2048 > codePnt) {
|
|
1837
|
+
codePntArr.push(192 | codePnt >>> 6);
|
|
1838
|
+
codePntArr.push(128 | codePnt & 63);
|
|
1839
|
+
} else if (55296 > codePnt || 57344 <= codePnt) {
|
|
1840
|
+
codePntArr.push(224 | codePnt >>> 12, 128 | codePnt >>> 6 & 63, 128 | codePnt & 63);
|
|
1841
|
+
} else {
|
|
1842
|
+
i2 += 1;
|
|
1843
|
+
codePnt = 65536 + ((codePnt & 1023) << 10 | str.charCodeAt(i2) & 1023);
|
|
1844
|
+
codePntArr.push(
|
|
1845
|
+
240 | codePnt >>> 18,
|
|
1846
|
+
128 | codePnt >>> 12 & 63,
|
|
1847
|
+
128 | codePnt >>> 6 & 63,
|
|
1848
|
+
128 | codePnt & 63
|
|
1849
|
+
);
|
|
1850
|
+
}
|
|
1851
|
+
for (j2 = 0; j2 < codePntArr.length; j2 += 1) {
|
|
1852
|
+
byteOffset = byteCnt + existingByteLen;
|
|
1853
|
+
intOffset = byteOffset >>> 2;
|
|
1854
|
+
while (packed.length <= intOffset) {
|
|
1855
|
+
packed.push(0);
|
|
1856
|
+
}
|
|
1857
|
+
packed[intOffset] |= codePntArr[j2] << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1858
|
+
byteCnt += 1;
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
} else {
|
|
1862
|
+
shiftModifier = bigEndianMod === -1 ? 2 : 0;
|
|
1863
|
+
transposeBytes = "UTF16LE" === utfType && bigEndianMod !== 1 || "UTF16LE" !== utfType && bigEndianMod === 1;
|
|
1864
|
+
for (i2 = 0; i2 < str.length; i2 += 1) {
|
|
1865
|
+
codePnt = str.charCodeAt(i2);
|
|
1866
|
+
if (transposeBytes === true) {
|
|
1867
|
+
j2 = codePnt & 255;
|
|
1868
|
+
codePnt = j2 << 8 | codePnt >>> 8;
|
|
1869
|
+
}
|
|
1870
|
+
byteOffset = byteCnt + existingByteLen;
|
|
1871
|
+
intOffset = byteOffset >>> 2;
|
|
1872
|
+
while (packed.length <= intOffset) {
|
|
1873
|
+
packed.push(0);
|
|
1874
|
+
}
|
|
1875
|
+
packed[intOffset] |= codePnt << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1876
|
+
byteCnt += 2;
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
return { value: packed, binLen: byteCnt * 8 + existingPackedLen };
|
|
1880
|
+
}
|
|
1881
|
+
function hex2packed(str, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1882
|
+
let i2, num, intOffset, byteOffset;
|
|
1883
|
+
if (0 !== str.length % 2) {
|
|
1884
|
+
throw new Error("String of HEX type must be in byte increments");
|
|
1885
|
+
}
|
|
1886
|
+
existingPackedLen = existingPackedLen || 0;
|
|
1887
|
+
const packed = existingPacked || [0], existingByteLen = existingPackedLen >>> 3, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
1888
|
+
for (i2 = 0; i2 < str.length; i2 += 2) {
|
|
1889
|
+
num = parseInt(str.substr(i2, 2), 16);
|
|
1890
|
+
if (!isNaN(num)) {
|
|
1891
|
+
byteOffset = (i2 >>> 1) + existingByteLen;
|
|
1892
|
+
intOffset = byteOffset >>> 2;
|
|
1893
|
+
while (packed.length <= intOffset) {
|
|
1894
|
+
packed.push(0);
|
|
1895
|
+
}
|
|
1896
|
+
packed[intOffset] |= num << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1897
|
+
} else {
|
|
1898
|
+
throw new Error("String of HEX type contains invalid characters");
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
return { value: packed, binLen: str.length * 4 + existingPackedLen };
|
|
1902
|
+
}
|
|
1903
|
+
function bytes2packed(str, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1904
|
+
let codePnt, i2, intOffset, byteOffset;
|
|
1905
|
+
existingPackedLen = existingPackedLen || 0;
|
|
1906
|
+
const packed = existingPacked || [0], existingByteLen = existingPackedLen >>> 3, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
1907
|
+
for (i2 = 0; i2 < str.length; i2 += 1) {
|
|
1908
|
+
codePnt = str.charCodeAt(i2);
|
|
1909
|
+
byteOffset = i2 + existingByteLen;
|
|
1910
|
+
intOffset = byteOffset >>> 2;
|
|
1911
|
+
if (packed.length <= intOffset) {
|
|
1912
|
+
packed.push(0);
|
|
1913
|
+
}
|
|
1914
|
+
packed[intOffset] |= codePnt << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1915
|
+
}
|
|
1916
|
+
return { value: packed, binLen: str.length * 8 + existingPackedLen };
|
|
1917
|
+
}
|
|
1918
|
+
function b642packed(str, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1919
|
+
let byteCnt = 0, index, i2, j2, tmpInt, strPart, intOffset, byteOffset;
|
|
1920
|
+
existingPackedLen = existingPackedLen || 0;
|
|
1921
|
+
const packed = existingPacked || [0], existingByteLen = existingPackedLen >>> 3, shiftModifier = bigEndianMod === -1 ? 3 : 0, firstEqual = str.indexOf("=");
|
|
1922
|
+
if (-1 === str.search(/^[a-z\d=+/]+$/i)) {
|
|
1923
|
+
throw new Error("Invalid character in base-64 string");
|
|
1924
|
+
}
|
|
1925
|
+
str = str.replace(/=/g, "");
|
|
1926
|
+
if (-1 !== firstEqual && firstEqual < str.length) {
|
|
1927
|
+
throw new Error("Invalid '=' found in base-64 string");
|
|
1928
|
+
}
|
|
1929
|
+
for (i2 = 0; i2 < str.length; i2 += 4) {
|
|
1930
|
+
strPart = str.substr(i2, 4);
|
|
1931
|
+
tmpInt = 0;
|
|
1932
|
+
for (j2 = 0; j2 < strPart.length; j2 += 1) {
|
|
1933
|
+
index = b64Tab.indexOf(strPart.charAt(j2));
|
|
1934
|
+
tmpInt |= index << 18 - 6 * j2;
|
|
1935
|
+
}
|
|
1936
|
+
for (j2 = 0; j2 < strPart.length - 1; j2 += 1) {
|
|
1937
|
+
byteOffset = byteCnt + existingByteLen;
|
|
1938
|
+
intOffset = byteOffset >>> 2;
|
|
1939
|
+
while (packed.length <= intOffset) {
|
|
1940
|
+
packed.push(0);
|
|
1941
|
+
}
|
|
1942
|
+
packed[intOffset] |= (tmpInt >>> 16 - j2 * 8 & 255) << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1943
|
+
byteCnt += 1;
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
return { value: packed, binLen: byteCnt * 8 + existingPackedLen };
|
|
1947
|
+
}
|
|
1948
|
+
function uint8array2packed(arr, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1949
|
+
let i2, intOffset, byteOffset;
|
|
1950
|
+
existingPackedLen = existingPackedLen || 0;
|
|
1951
|
+
const packed = existingPacked || [0], existingByteLen = existingPackedLen >>> 3, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
1952
|
+
for (i2 = 0; i2 < arr.length; i2 += 1) {
|
|
1953
|
+
byteOffset = i2 + existingByteLen;
|
|
1954
|
+
intOffset = byteOffset >>> 2;
|
|
1955
|
+
if (packed.length <= intOffset) {
|
|
1956
|
+
packed.push(0);
|
|
1957
|
+
}
|
|
1958
|
+
packed[intOffset] |= arr[i2] << 8 * (shiftModifier + bigEndianMod * (byteOffset % 4));
|
|
1959
|
+
}
|
|
1960
|
+
return { value: packed, binLen: arr.length * 8 + existingPackedLen };
|
|
1961
|
+
}
|
|
1962
|
+
function arraybuffer2packed(arr, existingPacked, existingPackedLen, bigEndianMod) {
|
|
1963
|
+
return uint8array2packed(new Uint8Array(arr), existingPacked, existingPackedLen, bigEndianMod);
|
|
1964
|
+
}
|
|
1965
|
+
function getStrConverter(format, utfType, bigEndianMod) {
|
|
1966
|
+
switch (utfType) {
|
|
1967
|
+
case "UTF8":
|
|
1968
|
+
/* Fallthrough */
|
|
1969
|
+
case "UTF16BE":
|
|
1970
|
+
/* Fallthrough */
|
|
1971
|
+
case "UTF16LE":
|
|
1972
|
+
break;
|
|
1973
|
+
default:
|
|
1974
|
+
throw new Error("encoding must be UTF8, UTF16BE, or UTF16LE");
|
|
1975
|
+
}
|
|
1976
|
+
switch (format) {
|
|
1977
|
+
case "HEX":
|
|
1978
|
+
return function(str, existingBin, existingBinLen) {
|
|
1979
|
+
return hex2packed(str, existingBin, existingBinLen, bigEndianMod);
|
|
1980
|
+
};
|
|
1981
|
+
case "TEXT":
|
|
1982
|
+
return function(str, existingBin, existingBinLen) {
|
|
1983
|
+
return str2packed(str, utfType, existingBin, existingBinLen, bigEndianMod);
|
|
1984
|
+
};
|
|
1985
|
+
case "B64":
|
|
1986
|
+
return function(str, existingBin, existingBinLen) {
|
|
1987
|
+
return b642packed(str, existingBin, existingBinLen, bigEndianMod);
|
|
1988
|
+
};
|
|
1989
|
+
case "BYTES":
|
|
1990
|
+
return function(str, existingBin, existingBinLen) {
|
|
1991
|
+
return bytes2packed(str, existingBin, existingBinLen, bigEndianMod);
|
|
1992
|
+
};
|
|
1993
|
+
case "ARRAYBUFFER":
|
|
1994
|
+
try {
|
|
1995
|
+
new ArrayBuffer(0);
|
|
1996
|
+
} catch (_ignore) {
|
|
1997
|
+
throw new Error(arraybuffer_error);
|
|
1998
|
+
}
|
|
1999
|
+
return function(arr, existingBin, existingBinLen) {
|
|
2000
|
+
return arraybuffer2packed(arr, existingBin, existingBinLen, bigEndianMod);
|
|
2001
|
+
};
|
|
2002
|
+
case "UINT8ARRAY":
|
|
2003
|
+
try {
|
|
2004
|
+
new Uint8Array(0);
|
|
2005
|
+
} catch (_ignore) {
|
|
2006
|
+
throw new Error(uint8array_error);
|
|
2007
|
+
}
|
|
2008
|
+
return function(arr, existingBin, existingBinLen) {
|
|
2009
|
+
return uint8array2packed(arr, existingBin, existingBinLen, bigEndianMod);
|
|
2010
|
+
};
|
|
2011
|
+
default:
|
|
2012
|
+
throw new Error("format must be HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY");
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
function packed2hex(packed, outputLength, bigEndianMod, formatOpts) {
|
|
2016
|
+
const hex_tab = "0123456789abcdef";
|
|
2017
|
+
let str = "", i2, srcByte;
|
|
2018
|
+
const length = outputLength / 8, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
2019
|
+
for (i2 = 0; i2 < length; i2 += 1) {
|
|
2020
|
+
srcByte = packed[i2 >>> 2] >>> 8 * (shiftModifier + bigEndianMod * (i2 % 4));
|
|
2021
|
+
str += hex_tab.charAt(srcByte >>> 4 & 15) + hex_tab.charAt(srcByte & 15);
|
|
2022
|
+
}
|
|
2023
|
+
return formatOpts["outputUpper"] ? str.toUpperCase() : str;
|
|
2024
|
+
}
|
|
2025
|
+
function packed2b64(packed, outputLength, bigEndianMod, formatOpts) {
|
|
2026
|
+
let str = "", i2, j2, triplet, int1, int2;
|
|
2027
|
+
const length = outputLength / 8, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
2028
|
+
for (i2 = 0; i2 < length; i2 += 3) {
|
|
2029
|
+
int1 = i2 + 1 < length ? packed[i2 + 1 >>> 2] : 0;
|
|
2030
|
+
int2 = i2 + 2 < length ? packed[i2 + 2 >>> 2] : 0;
|
|
2031
|
+
triplet = (packed[i2 >>> 2] >>> 8 * (shiftModifier + bigEndianMod * (i2 % 4)) & 255) << 16 | (int1 >>> 8 * (shiftModifier + bigEndianMod * ((i2 + 1) % 4)) & 255) << 8 | int2 >>> 8 * (shiftModifier + bigEndianMod * ((i2 + 2) % 4)) & 255;
|
|
2032
|
+
for (j2 = 0; j2 < 4; j2 += 1) {
|
|
2033
|
+
if (i2 * 8 + j2 * 6 <= outputLength) {
|
|
2034
|
+
str += b64Tab.charAt(triplet >>> 6 * (3 - j2) & 63);
|
|
2035
|
+
} else {
|
|
2036
|
+
str += formatOpts["b64Pad"];
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
return str;
|
|
2041
|
+
}
|
|
2042
|
+
function packed2bytes(packed, outputLength, bigEndianMod) {
|
|
2043
|
+
let str = "", i2, srcByte;
|
|
2044
|
+
const length = outputLength / 8, shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
2045
|
+
for (i2 = 0; i2 < length; i2 += 1) {
|
|
2046
|
+
srcByte = packed[i2 >>> 2] >>> 8 * (shiftModifier + bigEndianMod * (i2 % 4)) & 255;
|
|
2047
|
+
str += String.fromCharCode(srcByte);
|
|
2048
|
+
}
|
|
2049
|
+
return str;
|
|
2050
|
+
}
|
|
2051
|
+
function packed2arraybuffer(packed, outputLength, bigEndianMod) {
|
|
2052
|
+
let i2;
|
|
2053
|
+
const length = outputLength / 8, retVal = new ArrayBuffer(length), arrView = new Uint8Array(retVal), shiftModifier = bigEndianMod === -1 ? 3 : 0;
|
|
2054
|
+
for (i2 = 0; i2 < length; i2 += 1) {
|
|
2055
|
+
arrView[i2] = packed[i2 >>> 2] >>> 8 * (shiftModifier + bigEndianMod * (i2 % 4)) & 255;
|
|
2056
|
+
}
|
|
2057
|
+
return retVal;
|
|
2058
|
+
}
|
|
2059
|
+
function packed2uint8array(packed, outputLength, bigEndianMod) {
|
|
2060
|
+
let i2;
|
|
2061
|
+
const length = outputLength / 8, shiftModifier = bigEndianMod === -1 ? 3 : 0, retVal = new Uint8Array(length);
|
|
2062
|
+
for (i2 = 0; i2 < length; i2 += 1) {
|
|
2063
|
+
retVal[i2] = packed[i2 >>> 2] >>> 8 * (shiftModifier + bigEndianMod * (i2 % 4)) & 255;
|
|
2064
|
+
}
|
|
2065
|
+
return retVal;
|
|
2066
|
+
}
|
|
2067
|
+
function getOutputConverter(format, outputBinLen, bigEndianMod, outputOptions) {
|
|
2068
|
+
switch (format) {
|
|
2069
|
+
case "HEX":
|
|
2070
|
+
return function(binarray) {
|
|
2071
|
+
return packed2hex(binarray, outputBinLen, bigEndianMod, outputOptions);
|
|
2072
|
+
};
|
|
2073
|
+
case "B64":
|
|
2074
|
+
return function(binarray) {
|
|
2075
|
+
return packed2b64(binarray, outputBinLen, bigEndianMod, outputOptions);
|
|
2076
|
+
};
|
|
2077
|
+
case "BYTES":
|
|
2078
|
+
return function(binarray) {
|
|
2079
|
+
return packed2bytes(binarray, outputBinLen, bigEndianMod);
|
|
2080
|
+
};
|
|
2081
|
+
case "ARRAYBUFFER":
|
|
2082
|
+
try {
|
|
2083
|
+
new ArrayBuffer(0);
|
|
2084
|
+
} catch (_ignore) {
|
|
2085
|
+
throw new Error(arraybuffer_error);
|
|
2086
|
+
}
|
|
2087
|
+
return function(binarray) {
|
|
2088
|
+
return packed2arraybuffer(binarray, outputBinLen, bigEndianMod);
|
|
2089
|
+
};
|
|
2090
|
+
case "UINT8ARRAY":
|
|
2091
|
+
try {
|
|
2092
|
+
new Uint8Array(0);
|
|
2093
|
+
} catch (_ignore) {
|
|
2094
|
+
throw new Error(uint8array_error);
|
|
2095
|
+
}
|
|
2096
|
+
return function(binarray) {
|
|
2097
|
+
return packed2uint8array(binarray, outputBinLen, bigEndianMod);
|
|
2098
|
+
};
|
|
2099
|
+
default:
|
|
2100
|
+
throw new Error("format must be HEX, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY");
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
const TWO_PWR_32 = 4294967296;
|
|
2104
|
+
const K_sha2 = [
|
|
2105
|
+
1116352408,
|
|
2106
|
+
1899447441,
|
|
2107
|
+
3049323471,
|
|
2108
|
+
3921009573,
|
|
2109
|
+
961987163,
|
|
2110
|
+
1508970993,
|
|
2111
|
+
2453635748,
|
|
2112
|
+
2870763221,
|
|
2113
|
+
3624381080,
|
|
2114
|
+
310598401,
|
|
2115
|
+
607225278,
|
|
2116
|
+
1426881987,
|
|
2117
|
+
1925078388,
|
|
2118
|
+
2162078206,
|
|
2119
|
+
2614888103,
|
|
2120
|
+
3248222580,
|
|
2121
|
+
3835390401,
|
|
2122
|
+
4022224774,
|
|
2123
|
+
264347078,
|
|
2124
|
+
604807628,
|
|
2125
|
+
770255983,
|
|
2126
|
+
1249150122,
|
|
2127
|
+
1555081692,
|
|
2128
|
+
1996064986,
|
|
2129
|
+
2554220882,
|
|
2130
|
+
2821834349,
|
|
2131
|
+
2952996808,
|
|
2132
|
+
3210313671,
|
|
2133
|
+
3336571891,
|
|
2134
|
+
3584528711,
|
|
2135
|
+
113926993,
|
|
2136
|
+
338241895,
|
|
2137
|
+
666307205,
|
|
2138
|
+
773529912,
|
|
2139
|
+
1294757372,
|
|
2140
|
+
1396182291,
|
|
2141
|
+
1695183700,
|
|
2142
|
+
1986661051,
|
|
2143
|
+
2177026350,
|
|
2144
|
+
2456956037,
|
|
2145
|
+
2730485921,
|
|
2146
|
+
2820302411,
|
|
2147
|
+
3259730800,
|
|
2148
|
+
3345764771,
|
|
2149
|
+
3516065817,
|
|
2150
|
+
3600352804,
|
|
2151
|
+
4094571909,
|
|
2152
|
+
275423344,
|
|
2153
|
+
430227734,
|
|
2154
|
+
506948616,
|
|
2155
|
+
659060556,
|
|
2156
|
+
883997877,
|
|
2157
|
+
958139571,
|
|
2158
|
+
1322822218,
|
|
2159
|
+
1537002063,
|
|
2160
|
+
1747873779,
|
|
2161
|
+
1955562222,
|
|
2162
|
+
2024104815,
|
|
2163
|
+
2227730452,
|
|
2164
|
+
2361852424,
|
|
2165
|
+
2428436474,
|
|
2166
|
+
2756734187,
|
|
2167
|
+
3204031479,
|
|
2168
|
+
3329325298
|
|
2169
|
+
];
|
|
2170
|
+
const H_trunc = [3238371032, 914150663, 812702999, 4144912697, 4290775857, 1750603025, 1694076839, 3204075428];
|
|
2171
|
+
const H_full = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225];
|
|
2172
|
+
const sha_variant_error = "Chosen SHA variant is not supported";
|
|
2173
|
+
const mac_rounds_error = "Cannot set numRounds with MAC";
|
|
2174
|
+
function packedLEConcat(a2, b2) {
|
|
2175
|
+
let i2, arrOffset;
|
|
2176
|
+
const aByteLen = a2["binLen"] >>> 3, bByteLen = b2["binLen"] >>> 3, leftShiftAmount = aByteLen << 3, rightShiftAmount = 4 - aByteLen << 3;
|
|
2177
|
+
if (aByteLen % 4 !== 0) {
|
|
2178
|
+
for (i2 = 0; i2 < bByteLen; i2 += 4) {
|
|
2179
|
+
arrOffset = aByteLen + i2 >>> 2;
|
|
2180
|
+
a2["value"][arrOffset] |= b2["value"][i2 >>> 2] << leftShiftAmount;
|
|
2181
|
+
a2["value"].push(0);
|
|
2182
|
+
a2["value"][arrOffset + 1] |= b2["value"][i2 >>> 2] >>> rightShiftAmount;
|
|
2183
|
+
}
|
|
2184
|
+
if ((a2["value"].length << 2) - 4 >= bByteLen + aByteLen) {
|
|
2185
|
+
a2["value"].pop();
|
|
2186
|
+
}
|
|
2187
|
+
return { value: a2["value"], binLen: a2["binLen"] + b2["binLen"] };
|
|
2188
|
+
} else {
|
|
2189
|
+
return { value: a2["value"].concat(b2["value"]), binLen: a2["binLen"] + b2["binLen"] };
|
|
2190
|
+
}
|
|
2191
|
+
}
|
|
2192
|
+
function getOutputOpts(options) {
|
|
2193
|
+
const retVal = { outputUpper: false, b64Pad: "=", outputLen: -1 }, outputOptions = options || {}, lenErrstr = "Output length must be a multiple of 8";
|
|
2194
|
+
retVal["outputUpper"] = outputOptions["outputUpper"] || false;
|
|
2195
|
+
if (outputOptions["b64Pad"]) {
|
|
2196
|
+
retVal["b64Pad"] = outputOptions["b64Pad"];
|
|
2197
|
+
}
|
|
2198
|
+
if (outputOptions["outputLen"]) {
|
|
2199
|
+
if (outputOptions["outputLen"] % 8 !== 0) {
|
|
2200
|
+
throw new Error(lenErrstr);
|
|
2201
|
+
}
|
|
2202
|
+
retVal["outputLen"] = outputOptions["outputLen"];
|
|
2203
|
+
} else if (outputOptions["shakeLen"]) {
|
|
2204
|
+
if (outputOptions["shakeLen"] % 8 !== 0) {
|
|
2205
|
+
throw new Error(lenErrstr);
|
|
2206
|
+
}
|
|
2207
|
+
retVal["outputLen"] = outputOptions["shakeLen"];
|
|
2208
|
+
}
|
|
2209
|
+
if ("boolean" !== typeof retVal["outputUpper"]) {
|
|
2210
|
+
throw new Error("Invalid outputUpper formatting option");
|
|
2211
|
+
}
|
|
2212
|
+
if ("string" !== typeof retVal["b64Pad"]) {
|
|
2213
|
+
throw new Error("Invalid b64Pad formatting option");
|
|
2214
|
+
}
|
|
2215
|
+
return retVal;
|
|
2216
|
+
}
|
|
2217
|
+
function parseInputOption(key, value, bigEndianMod, fallback) {
|
|
2218
|
+
const errStr = key + " must include a value and format";
|
|
2219
|
+
if (!value) {
|
|
2220
|
+
if (!fallback) {
|
|
2221
|
+
throw new Error(errStr);
|
|
2222
|
+
}
|
|
2223
|
+
return fallback;
|
|
2224
|
+
}
|
|
2225
|
+
if (typeof value["value"] === "undefined" || !value["format"]) {
|
|
2226
|
+
throw new Error(errStr);
|
|
2227
|
+
}
|
|
2228
|
+
return getStrConverter(
|
|
2229
|
+
value["format"],
|
|
2230
|
+
// @ts-expect-error - the value of encoding gets value checked by getStrConverter
|
|
2231
|
+
value["encoding"] || "UTF8",
|
|
2232
|
+
bigEndianMod
|
|
2233
|
+
)(value["value"]);
|
|
2234
|
+
}
|
|
2235
|
+
class jsSHABase {
|
|
2236
|
+
constructor(variant, inputFormat, options) {
|
|
2237
|
+
/**
|
|
2238
|
+
* @param variant The desired SHA variant.
|
|
2239
|
+
* @param inputFormat The input format to be used in future `update` calls.
|
|
2240
|
+
* @param options Hashmap of extra input options.
|
|
2241
|
+
*/
|
|
2242
|
+
/* Needed inputs */
|
|
2243
|
+
__publicField(this, "shaVariant");
|
|
2244
|
+
__publicField(this, "inputFormat");
|
|
2245
|
+
__publicField(this, "utfType");
|
|
2246
|
+
__publicField(this, "numRounds");
|
|
2247
|
+
__publicField(this, "keyWithIPad");
|
|
2248
|
+
__publicField(this, "keyWithOPad");
|
|
2249
|
+
__publicField(this, "remainder");
|
|
2250
|
+
__publicField(this, "remainderLen");
|
|
2251
|
+
__publicField(this, "updateCalled");
|
|
2252
|
+
__publicField(this, "processedLen");
|
|
2253
|
+
__publicField(this, "macKeySet");
|
|
2254
|
+
const inputOptions = options || {};
|
|
2255
|
+
this.inputFormat = inputFormat;
|
|
2256
|
+
this.utfType = inputOptions["encoding"] || "UTF8";
|
|
2257
|
+
this.numRounds = inputOptions["numRounds"] || 1;
|
|
2258
|
+
if (isNaN(this.numRounds) || this.numRounds !== parseInt(this.numRounds, 10) || 1 > this.numRounds) {
|
|
2259
|
+
throw new Error("numRounds must a integer >= 1");
|
|
2260
|
+
}
|
|
2261
|
+
this.shaVariant = variant;
|
|
2262
|
+
this.remainder = [];
|
|
2263
|
+
this.remainderLen = 0;
|
|
2264
|
+
this.updateCalled = false;
|
|
2265
|
+
this.processedLen = 0;
|
|
2266
|
+
this.macKeySet = false;
|
|
2267
|
+
this.keyWithIPad = [];
|
|
2268
|
+
this.keyWithOPad = [];
|
|
2269
|
+
}
|
|
2270
|
+
/**
|
|
2271
|
+
* Hashes as many blocks as possible. Stores the rest for either a future update or getHash call.
|
|
2272
|
+
*
|
|
2273
|
+
* @param srcString The input to be hashed.
|
|
2274
|
+
* @returns A reference to the object.
|
|
2275
|
+
*/
|
|
2276
|
+
update(srcString) {
|
|
2277
|
+
let i2, updateProcessedLen = 0;
|
|
2278
|
+
const variantBlockIntInc = this.variantBlockSize >>> 5, convertRet = this.converterFunc(srcString, this.remainder, this.remainderLen), chunkBinLen = convertRet["binLen"], chunk = convertRet["value"], chunkIntLen = chunkBinLen >>> 5;
|
|
2279
|
+
for (i2 = 0; i2 < chunkIntLen; i2 += variantBlockIntInc) {
|
|
2280
|
+
if (updateProcessedLen + this.variantBlockSize <= chunkBinLen) {
|
|
2281
|
+
this.intermediateState = this.roundFunc(chunk.slice(i2, i2 + variantBlockIntInc), this.intermediateState);
|
|
2282
|
+
updateProcessedLen += this.variantBlockSize;
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
this.processedLen += updateProcessedLen;
|
|
2286
|
+
this.remainder = chunk.slice(updateProcessedLen >>> 5);
|
|
2287
|
+
this.remainderLen = chunkBinLen % this.variantBlockSize;
|
|
2288
|
+
this.updateCalled = true;
|
|
2289
|
+
return this;
|
|
2290
|
+
}
|
|
2291
|
+
getHash(format, options) {
|
|
2292
|
+
let i2, finalizedState, outputBinLen = this.outputBinLen;
|
|
2293
|
+
const outputOptions = getOutputOpts(options);
|
|
2294
|
+
if (this.isVariableLen) {
|
|
2295
|
+
if (outputOptions["outputLen"] === -1) {
|
|
2296
|
+
throw new Error("Output length must be specified in options");
|
|
2297
|
+
}
|
|
2298
|
+
outputBinLen = outputOptions["outputLen"];
|
|
2299
|
+
}
|
|
2300
|
+
const formatFunc = getOutputConverter(format, outputBinLen, this.bigEndianMod, outputOptions);
|
|
2301
|
+
if (this.macKeySet && this.getMAC) {
|
|
2302
|
+
return formatFunc(this.getMAC(outputOptions));
|
|
2303
|
+
}
|
|
2304
|
+
finalizedState = this.finalizeFunc(
|
|
2305
|
+
this.remainder.slice(),
|
|
2306
|
+
this.remainderLen,
|
|
2307
|
+
this.processedLen,
|
|
2308
|
+
this.stateCloneFunc(this.intermediateState),
|
|
2309
|
+
outputBinLen
|
|
2310
|
+
);
|
|
2311
|
+
for (i2 = 1; i2 < this.numRounds; i2 += 1) {
|
|
2312
|
+
if (this.isVariableLen && outputBinLen % 32 !== 0) {
|
|
2313
|
+
finalizedState[finalizedState.length - 1] &= 16777215 >>> 24 - outputBinLen % 32;
|
|
2314
|
+
}
|
|
2315
|
+
finalizedState = this.finalizeFunc(
|
|
2316
|
+
finalizedState,
|
|
2317
|
+
outputBinLen,
|
|
2318
|
+
0,
|
|
2319
|
+
this.newStateFunc(this.shaVariant),
|
|
2320
|
+
outputBinLen
|
|
2321
|
+
);
|
|
2322
|
+
}
|
|
2323
|
+
return formatFunc(finalizedState);
|
|
2324
|
+
}
|
|
2325
|
+
setHMACKey(key, inputFormat, options) {
|
|
2326
|
+
if (!this.HMACSupported) {
|
|
2327
|
+
throw new Error("Variant does not support HMAC");
|
|
2328
|
+
}
|
|
2329
|
+
if (this.updateCalled) {
|
|
2330
|
+
throw new Error("Cannot set MAC key after calling update");
|
|
2331
|
+
}
|
|
2332
|
+
const keyOptions = options || {}, keyConverterFunc = getStrConverter(inputFormat, keyOptions["encoding"] || "UTF8", this.bigEndianMod);
|
|
2333
|
+
this._setHMACKey(keyConverterFunc(key));
|
|
2334
|
+
}
|
|
2335
|
+
/**
|
|
2336
|
+
* Internal function that sets the MAC key.
|
|
2337
|
+
*
|
|
2338
|
+
* @param key The packed MAC key to use
|
|
2339
|
+
*/
|
|
2340
|
+
_setHMACKey(key) {
|
|
2341
|
+
const blockByteSize = this.variantBlockSize >>> 3, lastArrayIndex = blockByteSize / 4 - 1;
|
|
2342
|
+
let i2;
|
|
2343
|
+
if (this.numRounds !== 1) {
|
|
2344
|
+
throw new Error(mac_rounds_error);
|
|
2345
|
+
}
|
|
2346
|
+
if (this.macKeySet) {
|
|
2347
|
+
throw new Error("MAC key already set");
|
|
2348
|
+
}
|
|
2349
|
+
if (blockByteSize < key["binLen"] / 8) {
|
|
2350
|
+
key["value"] = this.finalizeFunc(
|
|
2351
|
+
key["value"],
|
|
2352
|
+
key["binLen"],
|
|
2353
|
+
0,
|
|
2354
|
+
this.newStateFunc(this.shaVariant),
|
|
2355
|
+
this.outputBinLen
|
|
2356
|
+
);
|
|
2357
|
+
}
|
|
2358
|
+
while (key["value"].length <= lastArrayIndex) {
|
|
2359
|
+
key["value"].push(0);
|
|
2360
|
+
}
|
|
2361
|
+
for (i2 = 0; i2 <= lastArrayIndex; i2 += 1) {
|
|
2362
|
+
this.keyWithIPad[i2] = key["value"][i2] ^ 909522486;
|
|
2363
|
+
this.keyWithOPad[i2] = key["value"][i2] ^ 1549556828;
|
|
2364
|
+
}
|
|
2365
|
+
this.intermediateState = this.roundFunc(this.keyWithIPad, this.intermediateState);
|
|
2366
|
+
this.processedLen = this.variantBlockSize;
|
|
2367
|
+
this.macKeySet = true;
|
|
2368
|
+
}
|
|
2369
|
+
getHMAC(format, options) {
|
|
2370
|
+
const outputOptions = getOutputOpts(options), formatFunc = getOutputConverter(format, this.outputBinLen, this.bigEndianMod, outputOptions);
|
|
2371
|
+
return formatFunc(this._getHMAC());
|
|
2372
|
+
}
|
|
2373
|
+
/**
|
|
2374
|
+
* Internal function that returns the "raw" HMAC
|
|
2375
|
+
*/
|
|
2376
|
+
_getHMAC() {
|
|
2377
|
+
let finalizedState;
|
|
2378
|
+
if (!this.macKeySet) {
|
|
2379
|
+
throw new Error("Cannot call getHMAC without first setting MAC key");
|
|
2380
|
+
}
|
|
2381
|
+
const firstHash = this.finalizeFunc(
|
|
2382
|
+
this.remainder.slice(),
|
|
2383
|
+
this.remainderLen,
|
|
2384
|
+
this.processedLen,
|
|
2385
|
+
this.stateCloneFunc(this.intermediateState),
|
|
2386
|
+
this.outputBinLen
|
|
2387
|
+
);
|
|
2388
|
+
finalizedState = this.roundFunc(this.keyWithOPad, this.newStateFunc(this.shaVariant));
|
|
2389
|
+
finalizedState = this.finalizeFunc(
|
|
2390
|
+
firstHash,
|
|
2391
|
+
this.outputBinLen,
|
|
2392
|
+
this.variantBlockSize,
|
|
2393
|
+
finalizedState,
|
|
2394
|
+
this.outputBinLen
|
|
2395
|
+
);
|
|
2396
|
+
return finalizedState;
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
function rotl_32(x2, n2) {
|
|
2400
|
+
return x2 << n2 | x2 >>> 32 - n2;
|
|
2401
|
+
}
|
|
2402
|
+
function rotr_32(x2, n2) {
|
|
2403
|
+
return x2 >>> n2 | x2 << 32 - n2;
|
|
2404
|
+
}
|
|
2405
|
+
function shr_32(x2, n2) {
|
|
2406
|
+
return x2 >>> n2;
|
|
2407
|
+
}
|
|
2408
|
+
function parity_32(x2, y, z2) {
|
|
2409
|
+
return x2 ^ y ^ z2;
|
|
2410
|
+
}
|
|
2411
|
+
function ch_32(x2, y, z2) {
|
|
2412
|
+
return x2 & y ^ ~x2 & z2;
|
|
2413
|
+
}
|
|
2414
|
+
function maj_32(x2, y, z2) {
|
|
2415
|
+
return x2 & y ^ x2 & z2 ^ y & z2;
|
|
2416
|
+
}
|
|
2417
|
+
function sigma0_32(x2) {
|
|
2418
|
+
return rotr_32(x2, 2) ^ rotr_32(x2, 13) ^ rotr_32(x2, 22);
|
|
2419
|
+
}
|
|
2420
|
+
function safeAdd_32_2(a2, b2) {
|
|
2421
|
+
const lsw = (a2 & 65535) + (b2 & 65535), msw = (a2 >>> 16) + (b2 >>> 16) + (lsw >>> 16);
|
|
2422
|
+
return (msw & 65535) << 16 | lsw & 65535;
|
|
2423
|
+
}
|
|
2424
|
+
function safeAdd_32_4(a2, b2, c2, d2) {
|
|
2425
|
+
const lsw = (a2 & 65535) + (b2 & 65535) + (c2 & 65535) + (d2 & 65535), msw = (a2 >>> 16) + (b2 >>> 16) + (c2 >>> 16) + (d2 >>> 16) + (lsw >>> 16);
|
|
2426
|
+
return (msw & 65535) << 16 | lsw & 65535;
|
|
2427
|
+
}
|
|
2428
|
+
function safeAdd_32_5(a2, b2, c2, d2, e2) {
|
|
2429
|
+
const lsw = (a2 & 65535) + (b2 & 65535) + (c2 & 65535) + (d2 & 65535) + (e2 & 65535), msw = (a2 >>> 16) + (b2 >>> 16) + (c2 >>> 16) + (d2 >>> 16) + (e2 >>> 16) + (lsw >>> 16);
|
|
2430
|
+
return (msw & 65535) << 16 | lsw & 65535;
|
|
2431
|
+
}
|
|
2432
|
+
function gamma1_32(x2) {
|
|
2433
|
+
return rotr_32(x2, 17) ^ rotr_32(x2, 19) ^ shr_32(x2, 10);
|
|
2434
|
+
}
|
|
2435
|
+
function gamma0_32(x2) {
|
|
2436
|
+
return rotr_32(x2, 7) ^ rotr_32(x2, 18) ^ shr_32(x2, 3);
|
|
2437
|
+
}
|
|
2438
|
+
function sigma1_32(x2) {
|
|
2439
|
+
return rotr_32(x2, 6) ^ rotr_32(x2, 11) ^ rotr_32(x2, 25);
|
|
2440
|
+
}
|
|
2441
|
+
function getNewState$1(_variant) {
|
|
2442
|
+
return [1732584193, 4023233417, 2562383102, 271733878, 3285377520];
|
|
2443
|
+
}
|
|
2444
|
+
function roundSHA1(block, H2) {
|
|
2445
|
+
let a2, b2, c2, d2, e2, T, t2;
|
|
2446
|
+
const W = [];
|
|
2447
|
+
a2 = H2[0];
|
|
2448
|
+
b2 = H2[1];
|
|
2449
|
+
c2 = H2[2];
|
|
2450
|
+
d2 = H2[3];
|
|
2451
|
+
e2 = H2[4];
|
|
2452
|
+
for (t2 = 0; t2 < 80; t2 += 1) {
|
|
2453
|
+
if (t2 < 16) {
|
|
2454
|
+
W[t2] = block[t2];
|
|
2455
|
+
} else {
|
|
2456
|
+
W[t2] = rotl_32(W[t2 - 3] ^ W[t2 - 8] ^ W[t2 - 14] ^ W[t2 - 16], 1);
|
|
2457
|
+
}
|
|
2458
|
+
if (t2 < 20) {
|
|
2459
|
+
T = safeAdd_32_5(rotl_32(a2, 5), ch_32(b2, c2, d2), e2, 1518500249, W[t2]);
|
|
2460
|
+
} else if (t2 < 40) {
|
|
2461
|
+
T = safeAdd_32_5(rotl_32(a2, 5), parity_32(b2, c2, d2), e2, 1859775393, W[t2]);
|
|
2462
|
+
} else if (t2 < 60) {
|
|
2463
|
+
T = safeAdd_32_5(rotl_32(a2, 5), maj_32(b2, c2, d2), e2, 2400959708, W[t2]);
|
|
2464
|
+
} else {
|
|
2465
|
+
T = safeAdd_32_5(rotl_32(a2, 5), parity_32(b2, c2, d2), e2, 3395469782, W[t2]);
|
|
2466
|
+
}
|
|
2467
|
+
e2 = d2;
|
|
2468
|
+
d2 = c2;
|
|
2469
|
+
c2 = rotl_32(b2, 30);
|
|
2470
|
+
b2 = a2;
|
|
2471
|
+
a2 = T;
|
|
2472
|
+
}
|
|
2473
|
+
H2[0] = safeAdd_32_2(a2, H2[0]);
|
|
2474
|
+
H2[1] = safeAdd_32_2(b2, H2[1]);
|
|
2475
|
+
H2[2] = safeAdd_32_2(c2, H2[2]);
|
|
2476
|
+
H2[3] = safeAdd_32_2(d2, H2[3]);
|
|
2477
|
+
H2[4] = safeAdd_32_2(e2, H2[4]);
|
|
2478
|
+
return H2;
|
|
2479
|
+
}
|
|
2480
|
+
function finalizeSHA1(remainder, remainderBinLen, processedBinLen, H2) {
|
|
2481
|
+
let i2;
|
|
2482
|
+
const offset = (remainderBinLen + 65 >>> 9 << 4) + 15, totalLen = remainderBinLen + processedBinLen;
|
|
2483
|
+
while (remainder.length <= offset) {
|
|
2484
|
+
remainder.push(0);
|
|
2485
|
+
}
|
|
2486
|
+
remainder[remainderBinLen >>> 5] |= 128 << 24 - remainderBinLen % 32;
|
|
2487
|
+
remainder[offset] = totalLen & 4294967295;
|
|
2488
|
+
remainder[offset - 1] = totalLen / TWO_PWR_32 | 0;
|
|
2489
|
+
for (i2 = 0; i2 < remainder.length; i2 += 16) {
|
|
2490
|
+
H2 = roundSHA1(remainder.slice(i2, i2 + 16), H2);
|
|
2491
|
+
}
|
|
2492
|
+
return H2;
|
|
2493
|
+
}
|
|
2494
|
+
let jsSHA$4 = class jsSHA extends jsSHABase {
|
|
2495
|
+
constructor(variant, inputFormat, options) {
|
|
2496
|
+
if ("SHA-1" !== variant) {
|
|
2497
|
+
throw new Error(sha_variant_error);
|
|
2498
|
+
}
|
|
2499
|
+
super(variant, inputFormat, options);
|
|
2500
|
+
__publicField(this, "intermediateState");
|
|
2501
|
+
__publicField(this, "variantBlockSize");
|
|
2502
|
+
__publicField(this, "bigEndianMod");
|
|
2503
|
+
__publicField(this, "outputBinLen");
|
|
2504
|
+
__publicField(this, "isVariableLen");
|
|
2505
|
+
__publicField(this, "HMACSupported");
|
|
2506
|
+
__publicField(this, "converterFunc");
|
|
2507
|
+
__publicField(this, "roundFunc");
|
|
2508
|
+
__publicField(this, "finalizeFunc");
|
|
2509
|
+
__publicField(this, "stateCloneFunc");
|
|
2510
|
+
__publicField(this, "newStateFunc");
|
|
2511
|
+
__publicField(this, "getMAC");
|
|
2512
|
+
const resolvedOptions = options || {};
|
|
2513
|
+
this.HMACSupported = true;
|
|
2514
|
+
this.getMAC = this._getHMAC;
|
|
2515
|
+
this.bigEndianMod = -1;
|
|
2516
|
+
this.converterFunc = getStrConverter(this.inputFormat, this.utfType, this.bigEndianMod);
|
|
2517
|
+
this.roundFunc = roundSHA1;
|
|
2518
|
+
this.stateCloneFunc = function(state) {
|
|
2519
|
+
return state.slice();
|
|
2520
|
+
};
|
|
2521
|
+
this.newStateFunc = getNewState$1;
|
|
2522
|
+
this.finalizeFunc = finalizeSHA1;
|
|
2523
|
+
this.intermediateState = getNewState$1();
|
|
2524
|
+
this.variantBlockSize = 512;
|
|
2525
|
+
this.outputBinLen = 160;
|
|
2526
|
+
this.isVariableLen = false;
|
|
2527
|
+
if (resolvedOptions["hmacKey"]) {
|
|
2528
|
+
this._setHMACKey(parseInputOption("hmacKey", resolvedOptions["hmacKey"], this.bigEndianMod));
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
};
|
|
2532
|
+
function getNewState256(variant) {
|
|
2533
|
+
let retVal;
|
|
2534
|
+
if ("SHA-224" === variant) {
|
|
2535
|
+
retVal = H_trunc.slice();
|
|
2536
|
+
} else {
|
|
2537
|
+
retVal = H_full.slice();
|
|
2538
|
+
}
|
|
2539
|
+
return retVal;
|
|
2540
|
+
}
|
|
2541
|
+
function roundSHA256(block, H2) {
|
|
2542
|
+
let a2, b2, c2, d2, e2, f2, g2, h2, T1, T2, t2;
|
|
2543
|
+
const W = [];
|
|
2544
|
+
a2 = H2[0];
|
|
2545
|
+
b2 = H2[1];
|
|
2546
|
+
c2 = H2[2];
|
|
2547
|
+
d2 = H2[3];
|
|
2548
|
+
e2 = H2[4];
|
|
2549
|
+
f2 = H2[5];
|
|
2550
|
+
g2 = H2[6];
|
|
2551
|
+
h2 = H2[7];
|
|
2552
|
+
for (t2 = 0; t2 < 64; t2 += 1) {
|
|
2553
|
+
if (t2 < 16) {
|
|
2554
|
+
W[t2] = block[t2];
|
|
2555
|
+
} else {
|
|
2556
|
+
W[t2] = safeAdd_32_4(gamma1_32(W[t2 - 2]), W[t2 - 7], gamma0_32(W[t2 - 15]), W[t2 - 16]);
|
|
2557
|
+
}
|
|
2558
|
+
T1 = safeAdd_32_5(h2, sigma1_32(e2), ch_32(e2, f2, g2), K_sha2[t2], W[t2]);
|
|
2559
|
+
T2 = safeAdd_32_2(sigma0_32(a2), maj_32(a2, b2, c2));
|
|
2560
|
+
h2 = g2;
|
|
2561
|
+
g2 = f2;
|
|
2562
|
+
f2 = e2;
|
|
2563
|
+
e2 = safeAdd_32_2(d2, T1);
|
|
2564
|
+
d2 = c2;
|
|
2565
|
+
c2 = b2;
|
|
2566
|
+
b2 = a2;
|
|
2567
|
+
a2 = safeAdd_32_2(T1, T2);
|
|
2568
|
+
}
|
|
2569
|
+
H2[0] = safeAdd_32_2(a2, H2[0]);
|
|
2570
|
+
H2[1] = safeAdd_32_2(b2, H2[1]);
|
|
2571
|
+
H2[2] = safeAdd_32_2(c2, H2[2]);
|
|
2572
|
+
H2[3] = safeAdd_32_2(d2, H2[3]);
|
|
2573
|
+
H2[4] = safeAdd_32_2(e2, H2[4]);
|
|
2574
|
+
H2[5] = safeAdd_32_2(f2, H2[5]);
|
|
2575
|
+
H2[6] = safeAdd_32_2(g2, H2[6]);
|
|
2576
|
+
H2[7] = safeAdd_32_2(h2, H2[7]);
|
|
2577
|
+
return H2;
|
|
2578
|
+
}
|
|
2579
|
+
function finalizeSHA256(remainder, remainderBinLen, processedBinLen, H2, variant) {
|
|
2580
|
+
let i2, retVal;
|
|
2581
|
+
const offset = (remainderBinLen + 65 >>> 9 << 4) + 15, binaryStringInc = 16, totalLen = remainderBinLen + processedBinLen;
|
|
2582
|
+
while (remainder.length <= offset) {
|
|
2583
|
+
remainder.push(0);
|
|
2584
|
+
}
|
|
2585
|
+
remainder[remainderBinLen >>> 5] |= 128 << 24 - remainderBinLen % 32;
|
|
2586
|
+
remainder[offset] = totalLen & 4294967295;
|
|
2587
|
+
remainder[offset - 1] = totalLen / TWO_PWR_32 | 0;
|
|
2588
|
+
for (i2 = 0; i2 < remainder.length; i2 += binaryStringInc) {
|
|
2589
|
+
H2 = roundSHA256(remainder.slice(i2, i2 + binaryStringInc), H2);
|
|
2590
|
+
}
|
|
2591
|
+
if ("SHA-224" === variant) {
|
|
2592
|
+
retVal = [H2[0], H2[1], H2[2], H2[3], H2[4], H2[5], H2[6]];
|
|
2593
|
+
} else {
|
|
2594
|
+
retVal = H2;
|
|
2595
|
+
}
|
|
2596
|
+
return retVal;
|
|
2597
|
+
}
|
|
2598
|
+
let jsSHA$3 = class jsSHA2 extends jsSHABase {
|
|
2599
|
+
constructor(variant, inputFormat, options) {
|
|
2600
|
+
if (!("SHA-224" === variant || "SHA-256" === variant)) {
|
|
2601
|
+
throw new Error(sha_variant_error);
|
|
2602
|
+
}
|
|
2603
|
+
super(variant, inputFormat, options);
|
|
2604
|
+
__publicField(this, "intermediateState");
|
|
2605
|
+
__publicField(this, "variantBlockSize");
|
|
2606
|
+
__publicField(this, "bigEndianMod");
|
|
2607
|
+
__publicField(this, "outputBinLen");
|
|
2608
|
+
__publicField(this, "isVariableLen");
|
|
2609
|
+
__publicField(this, "HMACSupported");
|
|
2610
|
+
__publicField(this, "converterFunc");
|
|
2611
|
+
__publicField(this, "roundFunc");
|
|
2612
|
+
__publicField(this, "finalizeFunc");
|
|
2613
|
+
__publicField(this, "stateCloneFunc");
|
|
2614
|
+
__publicField(this, "newStateFunc");
|
|
2615
|
+
__publicField(this, "getMAC");
|
|
2616
|
+
const resolvedOptions = options || {};
|
|
2617
|
+
this.getMAC = this._getHMAC;
|
|
2618
|
+
this.HMACSupported = true;
|
|
2619
|
+
this.bigEndianMod = -1;
|
|
2620
|
+
this.converterFunc = getStrConverter(this.inputFormat, this.utfType, this.bigEndianMod);
|
|
2621
|
+
this.roundFunc = roundSHA256;
|
|
2622
|
+
this.stateCloneFunc = function(state) {
|
|
2623
|
+
return state.slice();
|
|
2624
|
+
};
|
|
2625
|
+
this.newStateFunc = getNewState256;
|
|
2626
|
+
this.finalizeFunc = function(remainder, remainderBinLen, processedBinLen, H2) {
|
|
2627
|
+
return finalizeSHA256(remainder, remainderBinLen, processedBinLen, H2, variant);
|
|
2628
|
+
};
|
|
2629
|
+
this.intermediateState = getNewState256(variant);
|
|
2630
|
+
this.variantBlockSize = 512;
|
|
2631
|
+
this.outputBinLen = "SHA-224" === variant ? 224 : 256;
|
|
2632
|
+
this.isVariableLen = false;
|
|
2633
|
+
if (resolvedOptions["hmacKey"]) {
|
|
2634
|
+
this._setHMACKey(parseInputOption("hmacKey", resolvedOptions["hmacKey"], this.bigEndianMod));
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
};
|
|
2638
|
+
class Int_64 {
|
|
2639
|
+
constructor(msint_32, lsint_32) {
|
|
2640
|
+
/**
|
|
2641
|
+
* @param msint_32 The most significant 32-bits of a 64-bit number.
|
|
2642
|
+
* @param lsint_32 The least significant 32-bits of a 64-bit number.
|
|
2643
|
+
*/
|
|
2644
|
+
__publicField(this, "highOrder");
|
|
2645
|
+
__publicField(this, "lowOrder");
|
|
2646
|
+
this.highOrder = msint_32;
|
|
2647
|
+
this.lowOrder = lsint_32;
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
function rotl_64(x2, n2) {
|
|
2651
|
+
let tmp;
|
|
2652
|
+
if (n2 > 32) {
|
|
2653
|
+
tmp = 64 - n2;
|
|
2654
|
+
return new Int_64(x2.lowOrder << n2 | x2.highOrder >>> tmp, x2.highOrder << n2 | x2.lowOrder >>> tmp);
|
|
2655
|
+
} else if (0 !== n2) {
|
|
2656
|
+
tmp = 32 - n2;
|
|
2657
|
+
return new Int_64(x2.highOrder << n2 | x2.lowOrder >>> tmp, x2.lowOrder << n2 | x2.highOrder >>> tmp);
|
|
2658
|
+
} else {
|
|
2659
|
+
return x2;
|
|
2660
|
+
}
|
|
2661
|
+
}
|
|
2662
|
+
function rotr_64(x2, n2) {
|
|
2663
|
+
let tmp;
|
|
2664
|
+
if (n2 < 32) {
|
|
2665
|
+
tmp = 32 - n2;
|
|
2666
|
+
return new Int_64(x2.highOrder >>> n2 | x2.lowOrder << tmp, x2.lowOrder >>> n2 | x2.highOrder << tmp);
|
|
2667
|
+
} else {
|
|
2668
|
+
tmp = 64 - n2;
|
|
2669
|
+
return new Int_64(x2.lowOrder >>> n2 | x2.highOrder << tmp, x2.highOrder >>> n2 | x2.lowOrder << tmp);
|
|
2670
|
+
}
|
|
2671
|
+
}
|
|
2672
|
+
function shr_64(x2, n2) {
|
|
2673
|
+
return new Int_64(x2.highOrder >>> n2, x2.lowOrder >>> n2 | x2.highOrder << 32 - n2);
|
|
2674
|
+
}
|
|
2675
|
+
function ch_64(x2, y, z2) {
|
|
2676
|
+
return new Int_64(
|
|
2677
|
+
x2.highOrder & y.highOrder ^ ~x2.highOrder & z2.highOrder,
|
|
2678
|
+
x2.lowOrder & y.lowOrder ^ ~x2.lowOrder & z2.lowOrder
|
|
2679
|
+
);
|
|
2680
|
+
}
|
|
2681
|
+
function maj_64(x2, y, z2) {
|
|
2682
|
+
return new Int_64(
|
|
2683
|
+
x2.highOrder & y.highOrder ^ x2.highOrder & z2.highOrder ^ y.highOrder & z2.highOrder,
|
|
2684
|
+
x2.lowOrder & y.lowOrder ^ x2.lowOrder & z2.lowOrder ^ y.lowOrder & z2.lowOrder
|
|
2685
|
+
);
|
|
2686
|
+
}
|
|
2687
|
+
function sigma0_64(x2) {
|
|
2688
|
+
const rotr28 = rotr_64(x2, 28), rotr34 = rotr_64(x2, 34), rotr39 = rotr_64(x2, 39);
|
|
2689
|
+
return new Int_64(
|
|
2690
|
+
rotr28.highOrder ^ rotr34.highOrder ^ rotr39.highOrder,
|
|
2691
|
+
rotr28.lowOrder ^ rotr34.lowOrder ^ rotr39.lowOrder
|
|
2692
|
+
);
|
|
2693
|
+
}
|
|
2694
|
+
function safeAdd_64_2(x2, y) {
|
|
2695
|
+
let lsw, msw;
|
|
2696
|
+
lsw = (x2.lowOrder & 65535) + (y.lowOrder & 65535);
|
|
2697
|
+
msw = (x2.lowOrder >>> 16) + (y.lowOrder >>> 16) + (lsw >>> 16);
|
|
2698
|
+
const lowOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2699
|
+
lsw = (x2.highOrder & 65535) + (y.highOrder & 65535) + (msw >>> 16);
|
|
2700
|
+
msw = (x2.highOrder >>> 16) + (y.highOrder >>> 16) + (lsw >>> 16);
|
|
2701
|
+
const highOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2702
|
+
return new Int_64(highOrder, lowOrder);
|
|
2703
|
+
}
|
|
2704
|
+
function safeAdd_64_4(a2, b2, c2, d2) {
|
|
2705
|
+
let lsw, msw;
|
|
2706
|
+
lsw = (a2.lowOrder & 65535) + (b2.lowOrder & 65535) + (c2.lowOrder & 65535) + (d2.lowOrder & 65535);
|
|
2707
|
+
msw = (a2.lowOrder >>> 16) + (b2.lowOrder >>> 16) + (c2.lowOrder >>> 16) + (d2.lowOrder >>> 16) + (lsw >>> 16);
|
|
2708
|
+
const lowOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2709
|
+
lsw = (a2.highOrder & 65535) + (b2.highOrder & 65535) + (c2.highOrder & 65535) + (d2.highOrder & 65535) + (msw >>> 16);
|
|
2710
|
+
msw = (a2.highOrder >>> 16) + (b2.highOrder >>> 16) + (c2.highOrder >>> 16) + (d2.highOrder >>> 16) + (lsw >>> 16);
|
|
2711
|
+
const highOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2712
|
+
return new Int_64(highOrder, lowOrder);
|
|
2713
|
+
}
|
|
2714
|
+
function safeAdd_64_5(a2, b2, c2, d2, e2) {
|
|
2715
|
+
let lsw, msw;
|
|
2716
|
+
lsw = (a2.lowOrder & 65535) + (b2.lowOrder & 65535) + (c2.lowOrder & 65535) + (d2.lowOrder & 65535) + (e2.lowOrder & 65535);
|
|
2717
|
+
msw = (a2.lowOrder >>> 16) + (b2.lowOrder >>> 16) + (c2.lowOrder >>> 16) + (d2.lowOrder >>> 16) + (e2.lowOrder >>> 16) + (lsw >>> 16);
|
|
2718
|
+
const lowOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2719
|
+
lsw = (a2.highOrder & 65535) + (b2.highOrder & 65535) + (c2.highOrder & 65535) + (d2.highOrder & 65535) + (e2.highOrder & 65535) + (msw >>> 16);
|
|
2720
|
+
msw = (a2.highOrder >>> 16) + (b2.highOrder >>> 16) + (c2.highOrder >>> 16) + (d2.highOrder >>> 16) + (e2.highOrder >>> 16) + (lsw >>> 16);
|
|
2721
|
+
const highOrder = (msw & 65535) << 16 | lsw & 65535;
|
|
2722
|
+
return new Int_64(highOrder, lowOrder);
|
|
2723
|
+
}
|
|
2724
|
+
function xor_64_2(a2, b2) {
|
|
2725
|
+
return new Int_64(a2.highOrder ^ b2.highOrder, a2.lowOrder ^ b2.lowOrder);
|
|
2726
|
+
}
|
|
2727
|
+
function xor_64_5(a2, b2, c2, d2, e2) {
|
|
2728
|
+
return new Int_64(
|
|
2729
|
+
a2.highOrder ^ b2.highOrder ^ c2.highOrder ^ d2.highOrder ^ e2.highOrder,
|
|
2730
|
+
a2.lowOrder ^ b2.lowOrder ^ c2.lowOrder ^ d2.lowOrder ^ e2.lowOrder
|
|
2731
|
+
);
|
|
2732
|
+
}
|
|
2733
|
+
function gamma1_64(x2) {
|
|
2734
|
+
const rotr19 = rotr_64(x2, 19), rotr61 = rotr_64(x2, 61), shr6 = shr_64(x2, 6);
|
|
2735
|
+
return new Int_64(
|
|
2736
|
+
rotr19.highOrder ^ rotr61.highOrder ^ shr6.highOrder,
|
|
2737
|
+
rotr19.lowOrder ^ rotr61.lowOrder ^ shr6.lowOrder
|
|
2738
|
+
);
|
|
2739
|
+
}
|
|
2740
|
+
function gamma0_64(x2) {
|
|
2741
|
+
const rotr1 = rotr_64(x2, 1), rotr8 = rotr_64(x2, 8), shr7 = shr_64(x2, 7);
|
|
2742
|
+
return new Int_64(
|
|
2743
|
+
rotr1.highOrder ^ rotr8.highOrder ^ shr7.highOrder,
|
|
2744
|
+
rotr1.lowOrder ^ rotr8.lowOrder ^ shr7.lowOrder
|
|
2745
|
+
);
|
|
2746
|
+
}
|
|
2747
|
+
function sigma1_64(x2) {
|
|
2748
|
+
const rotr14 = rotr_64(x2, 14), rotr18 = rotr_64(x2, 18), rotr41 = rotr_64(x2, 41);
|
|
2749
|
+
return new Int_64(
|
|
2750
|
+
rotr14.highOrder ^ rotr18.highOrder ^ rotr41.highOrder,
|
|
2751
|
+
rotr14.lowOrder ^ rotr18.lowOrder ^ rotr41.lowOrder
|
|
2752
|
+
);
|
|
2753
|
+
}
|
|
2754
|
+
const K_sha512 = [
|
|
2755
|
+
new Int_64(K_sha2[0], 3609767458),
|
|
2756
|
+
new Int_64(K_sha2[1], 602891725),
|
|
2757
|
+
new Int_64(K_sha2[2], 3964484399),
|
|
2758
|
+
new Int_64(K_sha2[3], 2173295548),
|
|
2759
|
+
new Int_64(K_sha2[4], 4081628472),
|
|
2760
|
+
new Int_64(K_sha2[5], 3053834265),
|
|
2761
|
+
new Int_64(K_sha2[6], 2937671579),
|
|
2762
|
+
new Int_64(K_sha2[7], 3664609560),
|
|
2763
|
+
new Int_64(K_sha2[8], 2734883394),
|
|
2764
|
+
new Int_64(K_sha2[9], 1164996542),
|
|
2765
|
+
new Int_64(K_sha2[10], 1323610764),
|
|
2766
|
+
new Int_64(K_sha2[11], 3590304994),
|
|
2767
|
+
new Int_64(K_sha2[12], 4068182383),
|
|
2768
|
+
new Int_64(K_sha2[13], 991336113),
|
|
2769
|
+
new Int_64(K_sha2[14], 633803317),
|
|
2770
|
+
new Int_64(K_sha2[15], 3479774868),
|
|
2771
|
+
new Int_64(K_sha2[16], 2666613458),
|
|
2772
|
+
new Int_64(K_sha2[17], 944711139),
|
|
2773
|
+
new Int_64(K_sha2[18], 2341262773),
|
|
2774
|
+
new Int_64(K_sha2[19], 2007800933),
|
|
2775
|
+
new Int_64(K_sha2[20], 1495990901),
|
|
2776
|
+
new Int_64(K_sha2[21], 1856431235),
|
|
2777
|
+
new Int_64(K_sha2[22], 3175218132),
|
|
2778
|
+
new Int_64(K_sha2[23], 2198950837),
|
|
2779
|
+
new Int_64(K_sha2[24], 3999719339),
|
|
2780
|
+
new Int_64(K_sha2[25], 766784016),
|
|
2781
|
+
new Int_64(K_sha2[26], 2566594879),
|
|
2782
|
+
new Int_64(K_sha2[27], 3203337956),
|
|
2783
|
+
new Int_64(K_sha2[28], 1034457026),
|
|
2784
|
+
new Int_64(K_sha2[29], 2466948901),
|
|
2785
|
+
new Int_64(K_sha2[30], 3758326383),
|
|
2786
|
+
new Int_64(K_sha2[31], 168717936),
|
|
2787
|
+
new Int_64(K_sha2[32], 1188179964),
|
|
2788
|
+
new Int_64(K_sha2[33], 1546045734),
|
|
2789
|
+
new Int_64(K_sha2[34], 1522805485),
|
|
2790
|
+
new Int_64(K_sha2[35], 2643833823),
|
|
2791
|
+
new Int_64(K_sha2[36], 2343527390),
|
|
2792
|
+
new Int_64(K_sha2[37], 1014477480),
|
|
2793
|
+
new Int_64(K_sha2[38], 1206759142),
|
|
2794
|
+
new Int_64(K_sha2[39], 344077627),
|
|
2795
|
+
new Int_64(K_sha2[40], 1290863460),
|
|
2796
|
+
new Int_64(K_sha2[41], 3158454273),
|
|
2797
|
+
new Int_64(K_sha2[42], 3505952657),
|
|
2798
|
+
new Int_64(K_sha2[43], 106217008),
|
|
2799
|
+
new Int_64(K_sha2[44], 3606008344),
|
|
2800
|
+
new Int_64(K_sha2[45], 1432725776),
|
|
2801
|
+
new Int_64(K_sha2[46], 1467031594),
|
|
2802
|
+
new Int_64(K_sha2[47], 851169720),
|
|
2803
|
+
new Int_64(K_sha2[48], 3100823752),
|
|
2804
|
+
new Int_64(K_sha2[49], 1363258195),
|
|
2805
|
+
new Int_64(K_sha2[50], 3750685593),
|
|
2806
|
+
new Int_64(K_sha2[51], 3785050280),
|
|
2807
|
+
new Int_64(K_sha2[52], 3318307427),
|
|
2808
|
+
new Int_64(K_sha2[53], 3812723403),
|
|
2809
|
+
new Int_64(K_sha2[54], 2003034995),
|
|
2810
|
+
new Int_64(K_sha2[55], 3602036899),
|
|
2811
|
+
new Int_64(K_sha2[56], 1575990012),
|
|
2812
|
+
new Int_64(K_sha2[57], 1125592928),
|
|
2813
|
+
new Int_64(K_sha2[58], 2716904306),
|
|
2814
|
+
new Int_64(K_sha2[59], 442776044),
|
|
2815
|
+
new Int_64(K_sha2[60], 593698344),
|
|
2816
|
+
new Int_64(K_sha2[61], 3733110249),
|
|
2817
|
+
new Int_64(K_sha2[62], 2999351573),
|
|
2818
|
+
new Int_64(K_sha2[63], 3815920427),
|
|
2819
|
+
new Int_64(3391569614, 3928383900),
|
|
2820
|
+
new Int_64(3515267271, 566280711),
|
|
2821
|
+
new Int_64(3940187606, 3454069534),
|
|
2822
|
+
new Int_64(4118630271, 4000239992),
|
|
2823
|
+
new Int_64(116418474, 1914138554),
|
|
2824
|
+
new Int_64(174292421, 2731055270),
|
|
2825
|
+
new Int_64(289380356, 3203993006),
|
|
2826
|
+
new Int_64(460393269, 320620315),
|
|
2827
|
+
new Int_64(685471733, 587496836),
|
|
2828
|
+
new Int_64(852142971, 1086792851),
|
|
2829
|
+
new Int_64(1017036298, 365543100),
|
|
2830
|
+
new Int_64(1126000580, 2618297676),
|
|
2831
|
+
new Int_64(1288033470, 3409855158),
|
|
2832
|
+
new Int_64(1501505948, 4234509866),
|
|
2833
|
+
new Int_64(1607167915, 987167468),
|
|
2834
|
+
new Int_64(1816402316, 1246189591)
|
|
2835
|
+
];
|
|
2836
|
+
function getNewState512(variant) {
|
|
2837
|
+
if ("SHA-384" === variant) {
|
|
2838
|
+
return [
|
|
2839
|
+
new Int_64(3418070365, H_trunc[0]),
|
|
2840
|
+
new Int_64(1654270250, H_trunc[1]),
|
|
2841
|
+
new Int_64(2438529370, H_trunc[2]),
|
|
2842
|
+
new Int_64(355462360, H_trunc[3]),
|
|
2843
|
+
new Int_64(1731405415, H_trunc[4]),
|
|
2844
|
+
new Int_64(41048885895, H_trunc[5]),
|
|
2845
|
+
new Int_64(3675008525, H_trunc[6]),
|
|
2846
|
+
new Int_64(1203062813, H_trunc[7])
|
|
2847
|
+
];
|
|
2848
|
+
} else {
|
|
2849
|
+
return [
|
|
2850
|
+
new Int_64(H_full[0], 4089235720),
|
|
2851
|
+
new Int_64(H_full[1], 2227873595),
|
|
2852
|
+
new Int_64(H_full[2], 4271175723),
|
|
2853
|
+
new Int_64(H_full[3], 1595750129),
|
|
2854
|
+
new Int_64(H_full[4], 2917565137),
|
|
2855
|
+
new Int_64(H_full[5], 725511199),
|
|
2856
|
+
new Int_64(H_full[6], 4215389547),
|
|
2857
|
+
new Int_64(H_full[7], 327033209)
|
|
2858
|
+
];
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
function roundSHA512(block, H2) {
|
|
2862
|
+
let a2, b2, c2, d2, e2, f2, g2, h2, T1, T2, t2, offset;
|
|
2863
|
+
const W = [];
|
|
2864
|
+
a2 = H2[0];
|
|
2865
|
+
b2 = H2[1];
|
|
2866
|
+
c2 = H2[2];
|
|
2867
|
+
d2 = H2[3];
|
|
2868
|
+
e2 = H2[4];
|
|
2869
|
+
f2 = H2[5];
|
|
2870
|
+
g2 = H2[6];
|
|
2871
|
+
h2 = H2[7];
|
|
2872
|
+
for (t2 = 0; t2 < 80; t2 += 1) {
|
|
2873
|
+
if (t2 < 16) {
|
|
2874
|
+
offset = t2 * 2;
|
|
2875
|
+
W[t2] = new Int_64(block[offset], block[offset + 1]);
|
|
2876
|
+
} else {
|
|
2877
|
+
W[t2] = safeAdd_64_4(gamma1_64(W[t2 - 2]), W[t2 - 7], gamma0_64(W[t2 - 15]), W[t2 - 16]);
|
|
2878
|
+
}
|
|
2879
|
+
T1 = safeAdd_64_5(h2, sigma1_64(e2), ch_64(e2, f2, g2), K_sha512[t2], W[t2]);
|
|
2880
|
+
T2 = safeAdd_64_2(sigma0_64(a2), maj_64(a2, b2, c2));
|
|
2881
|
+
h2 = g2;
|
|
2882
|
+
g2 = f2;
|
|
2883
|
+
f2 = e2;
|
|
2884
|
+
e2 = safeAdd_64_2(d2, T1);
|
|
2885
|
+
d2 = c2;
|
|
2886
|
+
c2 = b2;
|
|
2887
|
+
b2 = a2;
|
|
2888
|
+
a2 = safeAdd_64_2(T1, T2);
|
|
2889
|
+
}
|
|
2890
|
+
H2[0] = safeAdd_64_2(a2, H2[0]);
|
|
2891
|
+
H2[1] = safeAdd_64_2(b2, H2[1]);
|
|
2892
|
+
H2[2] = safeAdd_64_2(c2, H2[2]);
|
|
2893
|
+
H2[3] = safeAdd_64_2(d2, H2[3]);
|
|
2894
|
+
H2[4] = safeAdd_64_2(e2, H2[4]);
|
|
2895
|
+
H2[5] = safeAdd_64_2(f2, H2[5]);
|
|
2896
|
+
H2[6] = safeAdd_64_2(g2, H2[6]);
|
|
2897
|
+
H2[7] = safeAdd_64_2(h2, H2[7]);
|
|
2898
|
+
return H2;
|
|
2899
|
+
}
|
|
2900
|
+
function finalizeSHA512(remainder, remainderBinLen, processedBinLen, H2, variant) {
|
|
2901
|
+
let i2, retVal;
|
|
2902
|
+
const offset = (remainderBinLen + 129 >>> 10 << 5) + 31, binaryStringInc = 32, totalLen = remainderBinLen + processedBinLen;
|
|
2903
|
+
while (remainder.length <= offset) {
|
|
2904
|
+
remainder.push(0);
|
|
2905
|
+
}
|
|
2906
|
+
remainder[remainderBinLen >>> 5] |= 128 << 24 - remainderBinLen % 32;
|
|
2907
|
+
remainder[offset] = totalLen & 4294967295;
|
|
2908
|
+
remainder[offset - 1] = totalLen / TWO_PWR_32 | 0;
|
|
2909
|
+
for (i2 = 0; i2 < remainder.length; i2 += binaryStringInc) {
|
|
2910
|
+
H2 = roundSHA512(remainder.slice(i2, i2 + binaryStringInc), H2);
|
|
2911
|
+
}
|
|
2912
|
+
if ("SHA-384" === variant) {
|
|
2913
|
+
H2 = H2;
|
|
2914
|
+
retVal = [
|
|
2915
|
+
H2[0].highOrder,
|
|
2916
|
+
H2[0].lowOrder,
|
|
2917
|
+
H2[1].highOrder,
|
|
2918
|
+
H2[1].lowOrder,
|
|
2919
|
+
H2[2].highOrder,
|
|
2920
|
+
H2[2].lowOrder,
|
|
2921
|
+
H2[3].highOrder,
|
|
2922
|
+
H2[3].lowOrder,
|
|
2923
|
+
H2[4].highOrder,
|
|
2924
|
+
H2[4].lowOrder,
|
|
2925
|
+
H2[5].highOrder,
|
|
2926
|
+
H2[5].lowOrder
|
|
2927
|
+
];
|
|
2928
|
+
} else {
|
|
2929
|
+
retVal = [
|
|
2930
|
+
H2[0].highOrder,
|
|
2931
|
+
H2[0].lowOrder,
|
|
2932
|
+
H2[1].highOrder,
|
|
2933
|
+
H2[1].lowOrder,
|
|
2934
|
+
H2[2].highOrder,
|
|
2935
|
+
H2[2].lowOrder,
|
|
2936
|
+
H2[3].highOrder,
|
|
2937
|
+
H2[3].lowOrder,
|
|
2938
|
+
H2[4].highOrder,
|
|
2939
|
+
H2[4].lowOrder,
|
|
2940
|
+
H2[5].highOrder,
|
|
2941
|
+
H2[5].lowOrder,
|
|
2942
|
+
H2[6].highOrder,
|
|
2943
|
+
H2[6].lowOrder,
|
|
2944
|
+
H2[7].highOrder,
|
|
2945
|
+
H2[7].lowOrder
|
|
2946
|
+
];
|
|
2947
|
+
}
|
|
2948
|
+
return retVal;
|
|
2949
|
+
}
|
|
2950
|
+
let jsSHA$2 = class jsSHA3 extends jsSHABase {
|
|
2951
|
+
constructor(variant, inputFormat, options) {
|
|
2952
|
+
if (!("SHA-384" === variant || "SHA-512" === variant)) {
|
|
2953
|
+
throw new Error(sha_variant_error);
|
|
2954
|
+
}
|
|
2955
|
+
super(variant, inputFormat, options);
|
|
2956
|
+
__publicField(this, "intermediateState");
|
|
2957
|
+
__publicField(this, "variantBlockSize");
|
|
2958
|
+
__publicField(this, "bigEndianMod");
|
|
2959
|
+
__publicField(this, "outputBinLen");
|
|
2960
|
+
__publicField(this, "isVariableLen");
|
|
2961
|
+
__publicField(this, "HMACSupported");
|
|
2962
|
+
__publicField(this, "converterFunc");
|
|
2963
|
+
__publicField(this, "roundFunc");
|
|
2964
|
+
__publicField(this, "finalizeFunc");
|
|
2965
|
+
__publicField(this, "stateCloneFunc");
|
|
2966
|
+
__publicField(this, "newStateFunc");
|
|
2967
|
+
__publicField(this, "getMAC");
|
|
2968
|
+
const resolvedOptions = options || {};
|
|
2969
|
+
this.getMAC = this._getHMAC;
|
|
2970
|
+
this.HMACSupported = true;
|
|
2971
|
+
this.bigEndianMod = -1;
|
|
2972
|
+
this.converterFunc = getStrConverter(this.inputFormat, this.utfType, this.bigEndianMod);
|
|
2973
|
+
this.roundFunc = roundSHA512;
|
|
2974
|
+
this.stateCloneFunc = function(state) {
|
|
2975
|
+
return state.slice();
|
|
2976
|
+
};
|
|
2977
|
+
this.newStateFunc = getNewState512;
|
|
2978
|
+
this.finalizeFunc = function(remainder, remainderBinLen, processedBinLen, H2) {
|
|
2979
|
+
return finalizeSHA512(remainder, remainderBinLen, processedBinLen, H2, variant);
|
|
2980
|
+
};
|
|
2981
|
+
this.intermediateState = getNewState512(variant);
|
|
2982
|
+
this.variantBlockSize = 1024;
|
|
2983
|
+
this.outputBinLen = "SHA-384" === variant ? 384 : 512;
|
|
2984
|
+
this.isVariableLen = false;
|
|
2985
|
+
if (resolvedOptions["hmacKey"]) {
|
|
2986
|
+
this._setHMACKey(parseInputOption("hmacKey", resolvedOptions["hmacKey"], this.bigEndianMod));
|
|
2987
|
+
}
|
|
2988
|
+
}
|
|
2989
|
+
};
|
|
2990
|
+
const rc_sha3 = [
|
|
2991
|
+
new Int_64(0, 1),
|
|
2992
|
+
new Int_64(0, 32898),
|
|
2993
|
+
new Int_64(2147483648, 32906),
|
|
2994
|
+
new Int_64(2147483648, 2147516416),
|
|
2995
|
+
new Int_64(0, 32907),
|
|
2996
|
+
new Int_64(0, 2147483649),
|
|
2997
|
+
new Int_64(2147483648, 2147516545),
|
|
2998
|
+
new Int_64(2147483648, 32777),
|
|
2999
|
+
new Int_64(0, 138),
|
|
3000
|
+
new Int_64(0, 136),
|
|
3001
|
+
new Int_64(0, 2147516425),
|
|
3002
|
+
new Int_64(0, 2147483658),
|
|
3003
|
+
new Int_64(0, 2147516555),
|
|
3004
|
+
new Int_64(2147483648, 139),
|
|
3005
|
+
new Int_64(2147483648, 32905),
|
|
3006
|
+
new Int_64(2147483648, 32771),
|
|
3007
|
+
new Int_64(2147483648, 32770),
|
|
3008
|
+
new Int_64(2147483648, 128),
|
|
3009
|
+
new Int_64(0, 32778),
|
|
3010
|
+
new Int_64(2147483648, 2147483658),
|
|
3011
|
+
new Int_64(2147483648, 2147516545),
|
|
3012
|
+
new Int_64(2147483648, 32896),
|
|
3013
|
+
new Int_64(0, 2147483649),
|
|
3014
|
+
new Int_64(2147483648, 2147516424)
|
|
3015
|
+
];
|
|
3016
|
+
const r_sha3 = [
|
|
3017
|
+
[0, 36, 3, 41, 18],
|
|
3018
|
+
[1, 44, 10, 45, 2],
|
|
3019
|
+
[62, 6, 43, 15, 61],
|
|
3020
|
+
[28, 55, 25, 21, 56],
|
|
3021
|
+
[27, 20, 39, 8, 14]
|
|
3022
|
+
];
|
|
3023
|
+
function getNewState(_variant) {
|
|
3024
|
+
let i2;
|
|
3025
|
+
const retVal = [];
|
|
3026
|
+
for (i2 = 0; i2 < 5; i2 += 1) {
|
|
3027
|
+
retVal[i2] = [new Int_64(0, 0), new Int_64(0, 0), new Int_64(0, 0), new Int_64(0, 0), new Int_64(0, 0)];
|
|
3028
|
+
}
|
|
3029
|
+
return retVal;
|
|
3030
|
+
}
|
|
3031
|
+
function cloneSHA3State(state) {
|
|
3032
|
+
let i2;
|
|
3033
|
+
const clone = [];
|
|
3034
|
+
for (i2 = 0; i2 < 5; i2 += 1) {
|
|
3035
|
+
clone[i2] = state[i2].slice();
|
|
3036
|
+
}
|
|
3037
|
+
return clone;
|
|
3038
|
+
}
|
|
3039
|
+
function roundSHA3(block, state) {
|
|
3040
|
+
let round, x2, y, B2;
|
|
3041
|
+
const C2 = [], D = [];
|
|
3042
|
+
if (null != block) {
|
|
3043
|
+
for (x2 = 0; x2 < block.length; x2 += 2) {
|
|
3044
|
+
state[(x2 >>> 1) % 5][(x2 >>> 1) / 5 | 0] = xor_64_2(
|
|
3045
|
+
state[(x2 >>> 1) % 5][(x2 >>> 1) / 5 | 0],
|
|
3046
|
+
new Int_64(block[x2 + 1], block[x2])
|
|
3047
|
+
);
|
|
3048
|
+
}
|
|
3049
|
+
}
|
|
3050
|
+
for (round = 0; round < 24; round += 1) {
|
|
3051
|
+
B2 = getNewState();
|
|
3052
|
+
for (x2 = 0; x2 < 5; x2 += 1) {
|
|
3053
|
+
C2[x2] = xor_64_5(state[x2][0], state[x2][1], state[x2][2], state[x2][3], state[x2][4]);
|
|
3054
|
+
}
|
|
3055
|
+
for (x2 = 0; x2 < 5; x2 += 1) {
|
|
3056
|
+
D[x2] = xor_64_2(C2[(x2 + 4) % 5], rotl_64(C2[(x2 + 1) % 5], 1));
|
|
3057
|
+
}
|
|
3058
|
+
for (x2 = 0; x2 < 5; x2 += 1) {
|
|
3059
|
+
for (y = 0; y < 5; y += 1) {
|
|
3060
|
+
state[x2][y] = xor_64_2(state[x2][y], D[x2]);
|
|
3061
|
+
}
|
|
3062
|
+
}
|
|
3063
|
+
for (x2 = 0; x2 < 5; x2 += 1) {
|
|
3064
|
+
for (y = 0; y < 5; y += 1) {
|
|
3065
|
+
B2[y][(2 * x2 + 3 * y) % 5] = rotl_64(state[x2][y], r_sha3[x2][y]);
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
for (x2 = 0; x2 < 5; x2 += 1) {
|
|
3069
|
+
for (y = 0; y < 5; y += 1) {
|
|
3070
|
+
state[x2][y] = xor_64_2(
|
|
3071
|
+
B2[x2][y],
|
|
3072
|
+
new Int_64(
|
|
3073
|
+
~B2[(x2 + 1) % 5][y].highOrder & B2[(x2 + 2) % 5][y].highOrder,
|
|
3074
|
+
~B2[(x2 + 1) % 5][y].lowOrder & B2[(x2 + 2) % 5][y].lowOrder
|
|
3075
|
+
)
|
|
3076
|
+
);
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
state[0][0] = xor_64_2(state[0][0], rc_sha3[round]);
|
|
3080
|
+
}
|
|
3081
|
+
return state;
|
|
3082
|
+
}
|
|
3083
|
+
function finalizeSHA3(remainder, remainderBinLen, _processedBinLen, state, blockSize, delimiter, outputLen) {
|
|
3084
|
+
let i2, state_offset = 0, temp;
|
|
3085
|
+
const retVal = [], binaryStringInc = blockSize >>> 5, remainderIntLen = remainderBinLen >>> 5;
|
|
3086
|
+
for (i2 = 0; i2 < remainderIntLen && remainderBinLen >= blockSize; i2 += binaryStringInc) {
|
|
3087
|
+
state = roundSHA3(remainder.slice(i2, i2 + binaryStringInc), state);
|
|
3088
|
+
remainderBinLen -= blockSize;
|
|
3089
|
+
}
|
|
3090
|
+
remainder = remainder.slice(i2);
|
|
3091
|
+
remainderBinLen = remainderBinLen % blockSize;
|
|
3092
|
+
while (remainder.length < binaryStringInc) {
|
|
3093
|
+
remainder.push(0);
|
|
3094
|
+
}
|
|
3095
|
+
i2 = remainderBinLen >>> 3;
|
|
3096
|
+
remainder[i2 >> 2] ^= delimiter << 8 * (i2 % 4);
|
|
3097
|
+
remainder[binaryStringInc - 1] ^= 2147483648;
|
|
3098
|
+
state = roundSHA3(remainder, state);
|
|
3099
|
+
while (retVal.length * 32 < outputLen) {
|
|
3100
|
+
temp = state[state_offset % 5][state_offset / 5 | 0];
|
|
3101
|
+
retVal.push(temp.lowOrder);
|
|
3102
|
+
if (retVal.length * 32 >= outputLen) {
|
|
3103
|
+
break;
|
|
3104
|
+
}
|
|
3105
|
+
retVal.push(temp.highOrder);
|
|
3106
|
+
state_offset += 1;
|
|
3107
|
+
if (0 === state_offset * 64 % blockSize) {
|
|
3108
|
+
roundSHA3(null, state);
|
|
3109
|
+
state_offset = 0;
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
return retVal;
|
|
3113
|
+
}
|
|
3114
|
+
function left_encode(x2) {
|
|
3115
|
+
let byteOffset, byte, numEncodedBytes = 0;
|
|
3116
|
+
const retVal = [0, 0], x_64 = [x2 & 4294967295, x2 / TWO_PWR_32 & 2097151];
|
|
3117
|
+
for (byteOffset = 6; byteOffset >= 0; byteOffset--) {
|
|
3118
|
+
byte = x_64[byteOffset >> 2] >>> 8 * byteOffset & 255;
|
|
3119
|
+
if (byte !== 0 || numEncodedBytes !== 0) {
|
|
3120
|
+
retVal[numEncodedBytes + 1 >> 2] |= byte << (numEncodedBytes + 1) * 8;
|
|
3121
|
+
numEncodedBytes += 1;
|
|
3122
|
+
}
|
|
3123
|
+
}
|
|
3124
|
+
numEncodedBytes = numEncodedBytes !== 0 ? numEncodedBytes : 1;
|
|
3125
|
+
retVal[0] |= numEncodedBytes;
|
|
3126
|
+
return { value: numEncodedBytes + 1 > 4 ? retVal : [retVal[0]], binLen: 8 + numEncodedBytes * 8 };
|
|
3127
|
+
}
|
|
3128
|
+
function right_encode(x2) {
|
|
3129
|
+
let byteOffset, byte, numEncodedBytes = 0;
|
|
3130
|
+
const retVal = [0, 0], x_64 = [x2 & 4294967295, x2 / TWO_PWR_32 & 2097151];
|
|
3131
|
+
for (byteOffset = 6; byteOffset >= 0; byteOffset--) {
|
|
3132
|
+
byte = x_64[byteOffset >> 2] >>> 8 * byteOffset & 255;
|
|
3133
|
+
if (byte !== 0 || numEncodedBytes !== 0) {
|
|
3134
|
+
retVal[numEncodedBytes >> 2] |= byte << numEncodedBytes * 8;
|
|
3135
|
+
numEncodedBytes += 1;
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
numEncodedBytes = numEncodedBytes !== 0 ? numEncodedBytes : 1;
|
|
3139
|
+
retVal[numEncodedBytes >> 2] |= numEncodedBytes << numEncodedBytes * 8;
|
|
3140
|
+
return { value: numEncodedBytes + 1 > 4 ? retVal : [retVal[0]], binLen: 8 + numEncodedBytes * 8 };
|
|
3141
|
+
}
|
|
3142
|
+
function encode_string(input) {
|
|
3143
|
+
return packedLEConcat(left_encode(input["binLen"]), input);
|
|
3144
|
+
}
|
|
3145
|
+
function byte_pad(packed, outputByteLen) {
|
|
3146
|
+
let encodedLen = left_encode(outputByteLen), i2;
|
|
3147
|
+
encodedLen = packedLEConcat(encodedLen, packed);
|
|
3148
|
+
const outputIntLen = outputByteLen >>> 2, intsToAppend = (outputIntLen - encodedLen["value"].length % outputIntLen) % outputIntLen;
|
|
3149
|
+
for (i2 = 0; i2 < intsToAppend; i2++) {
|
|
3150
|
+
encodedLen["value"].push(0);
|
|
3151
|
+
}
|
|
3152
|
+
return encodedLen["value"];
|
|
3153
|
+
}
|
|
3154
|
+
function resolveCSHAKEOptions(options) {
|
|
3155
|
+
const resolvedOptions = options || {};
|
|
3156
|
+
return {
|
|
3157
|
+
funcName: parseInputOption("funcName", resolvedOptions["funcName"], 1, { value: [], binLen: 0 }),
|
|
3158
|
+
customization: parseInputOption("Customization", resolvedOptions["customization"], 1, { value: [], binLen: 0 })
|
|
3159
|
+
};
|
|
3160
|
+
}
|
|
3161
|
+
function resolveKMACOptions(options) {
|
|
3162
|
+
const resolvedOptions = options || {};
|
|
3163
|
+
return {
|
|
3164
|
+
kmacKey: parseInputOption("kmacKey", resolvedOptions["kmacKey"], 1),
|
|
3165
|
+
/* This is little-endian packed "KMAC" */
|
|
3166
|
+
funcName: { value: [1128353099], binLen: 32 },
|
|
3167
|
+
customization: parseInputOption("Customization", resolvedOptions["customization"], 1, { value: [], binLen: 0 })
|
|
3168
|
+
};
|
|
3169
|
+
}
|
|
3170
|
+
let jsSHA$1 = class jsSHA4 extends jsSHABase {
|
|
3171
|
+
constructor(variant, inputFormat, options) {
|
|
3172
|
+
let delimiter = 6, variantBlockSize = 0;
|
|
3173
|
+
super(variant, inputFormat, options);
|
|
3174
|
+
__publicField(this, "intermediateState");
|
|
3175
|
+
__publicField(this, "variantBlockSize");
|
|
3176
|
+
__publicField(this, "bigEndianMod");
|
|
3177
|
+
__publicField(this, "outputBinLen");
|
|
3178
|
+
__publicField(this, "isVariableLen");
|
|
3179
|
+
__publicField(this, "HMACSupported");
|
|
3180
|
+
__publicField(this, "converterFunc");
|
|
3181
|
+
__publicField(this, "roundFunc");
|
|
3182
|
+
__publicField(this, "finalizeFunc");
|
|
3183
|
+
__publicField(this, "stateCloneFunc");
|
|
3184
|
+
__publicField(this, "newStateFunc");
|
|
3185
|
+
__publicField(this, "getMAC");
|
|
3186
|
+
const resolvedOptions = options || {};
|
|
3187
|
+
if (this.numRounds !== 1) {
|
|
3188
|
+
if (resolvedOptions["kmacKey"] || resolvedOptions["hmacKey"]) {
|
|
3189
|
+
throw new Error(mac_rounds_error);
|
|
3190
|
+
} else if (this.shaVariant === "CSHAKE128" || this.shaVariant === "CSHAKE256") {
|
|
3191
|
+
throw new Error("Cannot set numRounds for CSHAKE variants");
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
this.bigEndianMod = 1;
|
|
3195
|
+
this.converterFunc = getStrConverter(this.inputFormat, this.utfType, this.bigEndianMod);
|
|
3196
|
+
this.roundFunc = roundSHA3;
|
|
3197
|
+
this.stateCloneFunc = cloneSHA3State;
|
|
3198
|
+
this.newStateFunc = getNewState;
|
|
3199
|
+
this.intermediateState = getNewState();
|
|
3200
|
+
this.isVariableLen = false;
|
|
3201
|
+
switch (variant) {
|
|
3202
|
+
case "SHA3-224":
|
|
3203
|
+
this.variantBlockSize = variantBlockSize = 1152;
|
|
3204
|
+
this.outputBinLen = 224;
|
|
3205
|
+
this.HMACSupported = true;
|
|
3206
|
+
this.getMAC = this._getHMAC;
|
|
3207
|
+
break;
|
|
3208
|
+
case "SHA3-256":
|
|
3209
|
+
this.variantBlockSize = variantBlockSize = 1088;
|
|
3210
|
+
this.outputBinLen = 256;
|
|
3211
|
+
this.HMACSupported = true;
|
|
3212
|
+
this.getMAC = this._getHMAC;
|
|
3213
|
+
break;
|
|
3214
|
+
case "SHA3-384":
|
|
3215
|
+
this.variantBlockSize = variantBlockSize = 832;
|
|
3216
|
+
this.outputBinLen = 384;
|
|
3217
|
+
this.HMACSupported = true;
|
|
3218
|
+
this.getMAC = this._getHMAC;
|
|
3219
|
+
break;
|
|
3220
|
+
case "SHA3-512":
|
|
3221
|
+
this.variantBlockSize = variantBlockSize = 576;
|
|
3222
|
+
this.outputBinLen = 512;
|
|
3223
|
+
this.HMACSupported = true;
|
|
3224
|
+
this.getMAC = this._getHMAC;
|
|
3225
|
+
break;
|
|
3226
|
+
case "SHAKE128":
|
|
3227
|
+
delimiter = 31;
|
|
3228
|
+
this.variantBlockSize = variantBlockSize = 1344;
|
|
3229
|
+
this.outputBinLen = -1;
|
|
3230
|
+
this.isVariableLen = true;
|
|
3231
|
+
this.HMACSupported = false;
|
|
3232
|
+
this.getMAC = null;
|
|
3233
|
+
break;
|
|
3234
|
+
case "SHAKE256":
|
|
3235
|
+
delimiter = 31;
|
|
3236
|
+
this.variantBlockSize = variantBlockSize = 1088;
|
|
3237
|
+
this.outputBinLen = -1;
|
|
3238
|
+
this.isVariableLen = true;
|
|
3239
|
+
this.HMACSupported = false;
|
|
3240
|
+
this.getMAC = null;
|
|
3241
|
+
break;
|
|
3242
|
+
case "KMAC128":
|
|
3243
|
+
delimiter = 4;
|
|
3244
|
+
this.variantBlockSize = variantBlockSize = 1344;
|
|
3245
|
+
this._initializeKMAC(options);
|
|
3246
|
+
this.outputBinLen = -1;
|
|
3247
|
+
this.isVariableLen = true;
|
|
3248
|
+
this.HMACSupported = false;
|
|
3249
|
+
this.getMAC = this._getKMAC;
|
|
3250
|
+
break;
|
|
3251
|
+
case "KMAC256":
|
|
3252
|
+
delimiter = 4;
|
|
3253
|
+
this.variantBlockSize = variantBlockSize = 1088;
|
|
3254
|
+
this._initializeKMAC(options);
|
|
3255
|
+
this.outputBinLen = -1;
|
|
3256
|
+
this.isVariableLen = true;
|
|
3257
|
+
this.HMACSupported = false;
|
|
3258
|
+
this.getMAC = this._getKMAC;
|
|
3259
|
+
break;
|
|
3260
|
+
case "CSHAKE128":
|
|
3261
|
+
this.variantBlockSize = variantBlockSize = 1344;
|
|
3262
|
+
delimiter = this._initializeCSHAKE(options);
|
|
3263
|
+
this.outputBinLen = -1;
|
|
3264
|
+
this.isVariableLen = true;
|
|
3265
|
+
this.HMACSupported = false;
|
|
3266
|
+
this.getMAC = null;
|
|
3267
|
+
break;
|
|
3268
|
+
case "CSHAKE256":
|
|
3269
|
+
this.variantBlockSize = variantBlockSize = 1088;
|
|
3270
|
+
delimiter = this._initializeCSHAKE(options);
|
|
3271
|
+
this.outputBinLen = -1;
|
|
3272
|
+
this.isVariableLen = true;
|
|
3273
|
+
this.HMACSupported = false;
|
|
3274
|
+
this.getMAC = null;
|
|
3275
|
+
break;
|
|
3276
|
+
default:
|
|
3277
|
+
throw new Error(sha_variant_error);
|
|
3278
|
+
}
|
|
3279
|
+
this.finalizeFunc = function(remainder, remainderBinLen, processedBinLen, state, outputBinLen) {
|
|
3280
|
+
return finalizeSHA3(
|
|
3281
|
+
remainder,
|
|
3282
|
+
remainderBinLen,
|
|
3283
|
+
processedBinLen,
|
|
3284
|
+
state,
|
|
3285
|
+
variantBlockSize,
|
|
3286
|
+
delimiter,
|
|
3287
|
+
outputBinLen
|
|
3288
|
+
);
|
|
3289
|
+
};
|
|
3290
|
+
if (resolvedOptions["hmacKey"]) {
|
|
3291
|
+
this._setHMACKey(parseInputOption("hmacKey", resolvedOptions["hmacKey"], this.bigEndianMod));
|
|
3292
|
+
}
|
|
3293
|
+
}
|
|
3294
|
+
/**
|
|
3295
|
+
* Initialize CSHAKE variants.
|
|
3296
|
+
*
|
|
3297
|
+
* @param options Options containing CSHAKE params.
|
|
3298
|
+
* @param funcNameOverride Overrides any "funcName" present in `options` (used with KMAC)
|
|
3299
|
+
* @returns The delimiter to be used
|
|
3300
|
+
*/
|
|
3301
|
+
_initializeCSHAKE(options, funcNameOverride) {
|
|
3302
|
+
const resolvedOptions = resolveCSHAKEOptions(options || {});
|
|
3303
|
+
if (funcNameOverride) {
|
|
3304
|
+
resolvedOptions["funcName"] = funcNameOverride;
|
|
3305
|
+
}
|
|
3306
|
+
const packedParams = packedLEConcat(
|
|
3307
|
+
encode_string(resolvedOptions["funcName"]),
|
|
3308
|
+
encode_string(resolvedOptions["customization"])
|
|
3309
|
+
);
|
|
3310
|
+
if (resolvedOptions["customization"]["binLen"] !== 0 || resolvedOptions["funcName"]["binLen"] !== 0) {
|
|
3311
|
+
const byte_pad_out = byte_pad(packedParams, this.variantBlockSize >>> 3);
|
|
3312
|
+
for (let i2 = 0; i2 < byte_pad_out.length; i2 += this.variantBlockSize >>> 5) {
|
|
3313
|
+
this.intermediateState = this.roundFunc(
|
|
3314
|
+
byte_pad_out.slice(i2, i2 + (this.variantBlockSize >>> 5)),
|
|
3315
|
+
this.intermediateState
|
|
3316
|
+
);
|
|
3317
|
+
this.processedLen += this.variantBlockSize;
|
|
3318
|
+
}
|
|
3319
|
+
return 4;
|
|
3320
|
+
} else {
|
|
3321
|
+
return 31;
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
/**
|
|
3325
|
+
* Initialize KMAC variants.
|
|
3326
|
+
*
|
|
3327
|
+
* @param options Options containing KMAC params.
|
|
3328
|
+
*/
|
|
3329
|
+
_initializeKMAC(options) {
|
|
3330
|
+
const resolvedOptions = resolveKMACOptions(options || {});
|
|
3331
|
+
this._initializeCSHAKE(options, resolvedOptions["funcName"]);
|
|
3332
|
+
const byte_pad_out = byte_pad(encode_string(resolvedOptions["kmacKey"]), this.variantBlockSize >>> 3);
|
|
3333
|
+
for (let i2 = 0; i2 < byte_pad_out.length; i2 += this.variantBlockSize >>> 5) {
|
|
3334
|
+
this.intermediateState = this.roundFunc(
|
|
3335
|
+
byte_pad_out.slice(i2, i2 + (this.variantBlockSize >>> 5)),
|
|
3336
|
+
this.intermediateState
|
|
3337
|
+
);
|
|
3338
|
+
this.processedLen += this.variantBlockSize;
|
|
3339
|
+
}
|
|
3340
|
+
this.macKeySet = true;
|
|
3341
|
+
}
|
|
3342
|
+
/**
|
|
3343
|
+
* Returns the the KMAC in the specified format.
|
|
3344
|
+
*
|
|
3345
|
+
* @param options Hashmap of extra outputs options. `outputLen` must be specified.
|
|
3346
|
+
* @returns The KMAC in the format specified.
|
|
3347
|
+
*/
|
|
3348
|
+
_getKMAC(options) {
|
|
3349
|
+
const concatedRemainder = packedLEConcat(
|
|
3350
|
+
{ value: this.remainder.slice(), binLen: this.remainderLen },
|
|
3351
|
+
right_encode(options["outputLen"])
|
|
3352
|
+
);
|
|
3353
|
+
return this.finalizeFunc(
|
|
3354
|
+
concatedRemainder["value"],
|
|
3355
|
+
concatedRemainder["binLen"],
|
|
3356
|
+
this.processedLen,
|
|
3357
|
+
this.stateCloneFunc(this.intermediateState),
|
|
3358
|
+
options["outputLen"]
|
|
3359
|
+
);
|
|
3360
|
+
}
|
|
3361
|
+
};
|
|
3362
|
+
class jsSHA5 {
|
|
3363
|
+
constructor(variant, inputFormat, options) {
|
|
3364
|
+
__publicField(this, "shaObj");
|
|
3365
|
+
if ("SHA-1" === variant) {
|
|
3366
|
+
this.shaObj = new jsSHA$4(variant, inputFormat, options);
|
|
3367
|
+
} else if ("SHA-224" === variant || "SHA-256" === variant) {
|
|
3368
|
+
this.shaObj = new jsSHA$3(variant, inputFormat, options);
|
|
3369
|
+
} else if ("SHA-384" === variant || "SHA-512" === variant) {
|
|
3370
|
+
this.shaObj = new jsSHA$2(variant, inputFormat, options);
|
|
3371
|
+
} else if ("SHA3-224" === variant || "SHA3-256" === variant || "SHA3-384" === variant || "SHA3-512" === variant || "SHAKE128" === variant || "SHAKE256" === variant || "CSHAKE128" === variant || "CSHAKE256" === variant || "KMAC128" === variant || "KMAC256" === variant) {
|
|
3372
|
+
this.shaObj = new jsSHA$1(variant, inputFormat, options);
|
|
3373
|
+
} else {
|
|
3374
|
+
throw new Error(sha_variant_error);
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
/**
|
|
3378
|
+
* Takes `input` and hashes as many blocks as possible. Stores the rest for either a future `update` or `getHash` call.
|
|
3379
|
+
*
|
|
3380
|
+
* @param input The input to be hashed.
|
|
3381
|
+
* @returns A reference to the object.
|
|
3382
|
+
*/
|
|
3383
|
+
update(input) {
|
|
3384
|
+
this.shaObj.update(input);
|
|
3385
|
+
return this;
|
|
3386
|
+
}
|
|
3387
|
+
getHash(format, options) {
|
|
3388
|
+
return this.shaObj.getHash(format, options);
|
|
3389
|
+
}
|
|
3390
|
+
setHMACKey(key, inputFormat, options) {
|
|
3391
|
+
this.shaObj.setHMACKey(key, inputFormat, options);
|
|
3392
|
+
}
|
|
3393
|
+
getHMAC(format, options) {
|
|
3394
|
+
return this.shaObj.getHMAC(format, options);
|
|
3395
|
+
}
|
|
3396
|
+
}
|
|
3397
|
+
class TOTP {
|
|
3398
|
+
static generate(key, options) {
|
|
3399
|
+
const _options = {
|
|
3400
|
+
digits: 6,
|
|
3401
|
+
algorithm: "SHA-1",
|
|
3402
|
+
period: 30,
|
|
3403
|
+
timestamp: Date.now(),
|
|
3404
|
+
...options
|
|
3405
|
+
};
|
|
3406
|
+
const epoch = Math.floor(_options.timestamp / 1e3);
|
|
3407
|
+
const time = this.leftpad(this.dec2hex(Math.floor(epoch / _options.period)), 16, "0");
|
|
3408
|
+
const shaObj = new jsSHA5(_options.algorithm, "HEX");
|
|
3409
|
+
shaObj.setHMACKey(this.base32tohex(key), "HEX");
|
|
3410
|
+
shaObj.update(time);
|
|
3411
|
+
const hmac = shaObj.getHMAC("HEX");
|
|
3412
|
+
const offset = this.hex2dec(hmac.substring(hmac.length - 1));
|
|
3413
|
+
let otp = (this.hex2dec(hmac.slice(offset * 2, offset * 2 + 8)) & this.hex2dec("7fffffff")) + "";
|
|
3414
|
+
const start = Math.max(otp.length - _options.digits, 0);
|
|
3415
|
+
otp = otp.substring(start, start + _options.digits);
|
|
3416
|
+
const expires = Math.ceil((_options.timestamp + 1) / (_options.period * 1e3)) * _options.period * 1e3;
|
|
3417
|
+
return { otp, expires };
|
|
3418
|
+
}
|
|
3419
|
+
static hex2dec(hex) {
|
|
3420
|
+
return parseInt(hex, 16);
|
|
3421
|
+
}
|
|
3422
|
+
static dec2hex(dec) {
|
|
3423
|
+
return (dec < 15.5 ? "0" : "") + Math.round(dec).toString(16);
|
|
3424
|
+
}
|
|
3425
|
+
static base32tohex(base32) {
|
|
3426
|
+
const base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
3427
|
+
let bits = "";
|
|
3428
|
+
let hex = "";
|
|
3429
|
+
const _base32 = base32.endsWith("=") ? base32.slice(0, base32.lastIndexOf("=")) : base32;
|
|
3430
|
+
for (let i2 = 0; i2 < _base32.length; i2++) {
|
|
3431
|
+
const val = base32chars.indexOf(base32.charAt(i2).toUpperCase());
|
|
3432
|
+
if (val === -1) throw new Error("Invalid base32 character in key");
|
|
3433
|
+
bits += this.leftpad(val.toString(2), 5, "0");
|
|
3434
|
+
}
|
|
3435
|
+
for (let i2 = 0; i2 + 8 <= bits.length; i2 += 8) {
|
|
3436
|
+
const chunk = bits.slice(i2, i2 + 8);
|
|
3437
|
+
hex = hex + this.leftpad(parseInt(chunk, 2).toString(16), 2, "0");
|
|
3438
|
+
}
|
|
3439
|
+
return hex;
|
|
3440
|
+
}
|
|
3441
|
+
static leftpad(str, len, pad) {
|
|
3442
|
+
if (len + 1 >= str.length) {
|
|
3443
|
+
str = Array(len + 1 - str.length).join(pad) + str;
|
|
3444
|
+
}
|
|
3445
|
+
return str;
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
const localStorageSetItem = (name, value) => {
|
|
3449
|
+
if (!isClient) return;
|
|
3450
|
+
localStorage.setItem(name, value);
|
|
3451
|
+
};
|
|
3452
|
+
const localStorageGetItem = (name) => {
|
|
3453
|
+
if (!isClient) return "";
|
|
3454
|
+
return localStorage.getItem(name) || "";
|
|
3455
|
+
};
|
|
3456
|
+
function compose(middleware) {
|
|
3457
|
+
if (!Array.isArray(middleware)) throw new TypeError("Middleware stack must be an array!");
|
|
3458
|
+
for (const fn of middleware) {
|
|
3459
|
+
if (typeof fn !== "function") throw new TypeError("Middleware must be composed of functions!");
|
|
3460
|
+
}
|
|
3461
|
+
return function(context, next) {
|
|
3462
|
+
let index = -1;
|
|
3463
|
+
return dispatch(0);
|
|
3464
|
+
function dispatch(i2) {
|
|
3465
|
+
if (i2 <= index) return Promise.reject(new Error("next() called multiple times"));
|
|
3466
|
+
index = i2;
|
|
3467
|
+
let fn = middleware[i2];
|
|
3468
|
+
if (i2 === middleware.length && next) fn = next;
|
|
3469
|
+
if (!fn) return Promise.resolve();
|
|
3470
|
+
try {
|
|
3471
|
+
return Promise.resolve(fn(context, dispatch.bind(null, i2 + 1)));
|
|
3472
|
+
} catch (err) {
|
|
3473
|
+
return Promise.reject(err);
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
};
|
|
3477
|
+
}
|
|
3478
|
+
const handleConsole = (hooks = noop) => {
|
|
3479
|
+
const consoleTypeList = ["log", "info", "warn", "error", "assert"];
|
|
3480
|
+
consoleTypeList.forEach((type) => {
|
|
3481
|
+
const replacement = (originalConsole) => {
|
|
3482
|
+
return function(...args) {
|
|
3483
|
+
if (originalConsole) {
|
|
3484
|
+
hooks(type, ...args);
|
|
3485
|
+
originalConsole.apply(console, args);
|
|
3486
|
+
}
|
|
3487
|
+
};
|
|
3488
|
+
};
|
|
3489
|
+
replaceOld(console, type, replacement);
|
|
3490
|
+
});
|
|
3491
|
+
};
|
|
3492
|
+
const debounce = (fn, ms = 500) => {
|
|
3493
|
+
let timeout;
|
|
3494
|
+
return (...args) => {
|
|
3495
|
+
clearTimeout(timeout);
|
|
3496
|
+
timeout = setTimeout(() => {
|
|
3497
|
+
fn.apply(void 0, args);
|
|
3498
|
+
}, ms);
|
|
3499
|
+
};
|
|
3500
|
+
};
|
|
3501
|
+
const addClassToElement = (element, addClass) => {
|
|
3502
|
+
if (typeof document === "undefined") return void 0;
|
|
3503
|
+
const classList = element.classList;
|
|
3504
|
+
if (!classList.contains(addClass)) {
|
|
3505
|
+
classList.add(addClass);
|
|
3506
|
+
}
|
|
3507
|
+
};
|
|
3508
|
+
const removeClassToElement = (element, removeClass) => {
|
|
3509
|
+
if (typeof document === "undefined") return void 0;
|
|
3510
|
+
const classList = element.classList;
|
|
3511
|
+
if (classList.contains(removeClass)) {
|
|
3512
|
+
classList.remove(removeClass);
|
|
3513
|
+
}
|
|
3514
|
+
};
|
|
3515
|
+
const createDocumentFragment = (list) => {
|
|
3516
|
+
if (typeof document === "undefined") return void 0;
|
|
3517
|
+
const Fragment = document.createDocumentFragment();
|
|
3518
|
+
list.forEach((item) => Fragment.appendChild(item));
|
|
3519
|
+
return Fragment;
|
|
3520
|
+
};
|
|
3521
|
+
const matchHtmlRegExp = /["'&<>]/;
|
|
3522
|
+
function escapeHtml(string) {
|
|
3523
|
+
const str = "" + string;
|
|
3524
|
+
const match = matchHtmlRegExp.exec(str);
|
|
3525
|
+
if (!match) {
|
|
3526
|
+
return str;
|
|
3527
|
+
}
|
|
3528
|
+
let escape;
|
|
3529
|
+
let html = "";
|
|
3530
|
+
let index = 0;
|
|
3531
|
+
let lastIndex = 0;
|
|
3532
|
+
for (index = match.index; index < str.length; index++) {
|
|
3533
|
+
switch (str.charCodeAt(index)) {
|
|
3534
|
+
case 34:
|
|
3535
|
+
escape = """;
|
|
3536
|
+
break;
|
|
3537
|
+
case 38:
|
|
3538
|
+
escape = "&";
|
|
3539
|
+
break;
|
|
3540
|
+
case 39:
|
|
3541
|
+
escape = "'";
|
|
3542
|
+
break;
|
|
3543
|
+
case 60:
|
|
3544
|
+
escape = "<";
|
|
3545
|
+
break;
|
|
3546
|
+
case 62:
|
|
3547
|
+
escape = ">";
|
|
3548
|
+
break;
|
|
3549
|
+
default:
|
|
3550
|
+
continue;
|
|
3551
|
+
}
|
|
3552
|
+
if (lastIndex !== index) {
|
|
3553
|
+
html += str.substring(lastIndex, index);
|
|
3554
|
+
}
|
|
3555
|
+
lastIndex = index + 1;
|
|
3556
|
+
html += escape;
|
|
3557
|
+
}
|
|
3558
|
+
return lastIndex !== index ? html + str.substring(lastIndex, index) : html;
|
|
3559
|
+
}
|
|
3560
|
+
const setFontSize2html = (designWidth = 375) => {
|
|
3561
|
+
let base = designWidth;
|
|
3562
|
+
const { documentElement } = document;
|
|
3563
|
+
const mediaQuery = window.matchMedia("(orientation: portrait)");
|
|
3564
|
+
let timer;
|
|
3565
|
+
let standardRatio = 667 / 375;
|
|
3566
|
+
if (currentDevice() === DEVICE.IPAD) {
|
|
3567
|
+
standardRatio = 1024 / 768;
|
|
3568
|
+
base = 768;
|
|
3569
|
+
}
|
|
3570
|
+
function setFontSize() {
|
|
3571
|
+
const isLandscape = !mediaQuery.matches;
|
|
3572
|
+
let screenWidth = window.screen.width;
|
|
3573
|
+
let screenHeight = window.screen.height;
|
|
3574
|
+
if (screenWidth < screenHeight) {
|
|
3575
|
+
[screenWidth, screenHeight] = [screenHeight, screenWidth];
|
|
3576
|
+
}
|
|
3577
|
+
let width = documentElement.clientWidth;
|
|
3578
|
+
let height = screenHeight;
|
|
3579
|
+
const realRatio = width / height;
|
|
3580
|
+
if (realRatio >= standardRatio) {
|
|
3581
|
+
width = height * standardRatio;
|
|
3582
|
+
documentElement.classList.remove("adjustHeight");
|
|
3583
|
+
documentElement.classList.add("adjustWidth");
|
|
3584
|
+
} else {
|
|
3585
|
+
height = width / standardRatio;
|
|
3586
|
+
documentElement.classList.remove("adjustWidth");
|
|
3587
|
+
documentElement.classList.add("adjustHeight");
|
|
3588
|
+
}
|
|
3589
|
+
let target = width / base * 16;
|
|
3590
|
+
if (isLandscape) {
|
|
3591
|
+
target /= standardRatio;
|
|
3592
|
+
}
|
|
3593
|
+
documentElement.style.fontSize = `${target}px`;
|
|
3594
|
+
const currentSize = window.getComputedStyle(documentElement).fontSize.replace("px", "") || 0;
|
|
3595
|
+
if (target !== currentSize) {
|
|
3596
|
+
documentElement.style.fontSize = `${target / Number(currentSize) * target}px`;
|
|
3597
|
+
}
|
|
3598
|
+
}
|
|
3599
|
+
window.addEventListener(
|
|
3600
|
+
"resize",
|
|
3601
|
+
function() {
|
|
3602
|
+
clearTimeout(timer);
|
|
3603
|
+
timer = setTimeout(setFontSize, 300);
|
|
3604
|
+
},
|
|
3605
|
+
false
|
|
3606
|
+
);
|
|
3607
|
+
window.addEventListener(
|
|
3608
|
+
"pageshow",
|
|
3609
|
+
function(e2) {
|
|
3610
|
+
if (e2.persisted) {
|
|
3611
|
+
clearTimeout(timer);
|
|
3612
|
+
timer = setTimeout(setFontSize, 300);
|
|
3613
|
+
}
|
|
3614
|
+
},
|
|
3615
|
+
false
|
|
3616
|
+
);
|
|
3617
|
+
window.addEventListener(
|
|
3618
|
+
"orientationchange",
|
|
3619
|
+
function() {
|
|
3620
|
+
setFontSize();
|
|
3621
|
+
},
|
|
3622
|
+
false
|
|
3623
|
+
);
|
|
3624
|
+
setFontSize();
|
|
3625
|
+
};
|
|
3626
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
3627
|
+
const SVG_TAG_NAMES = [
|
|
3628
|
+
"svg",
|
|
3629
|
+
"path",
|
|
3630
|
+
"g",
|
|
3631
|
+
"circle",
|
|
3632
|
+
"rect",
|
|
3633
|
+
"line",
|
|
3634
|
+
"polyline",
|
|
3635
|
+
"polygon",
|
|
3636
|
+
"ellipse",
|
|
3637
|
+
"text",
|
|
3638
|
+
"tspan",
|
|
3639
|
+
"textPath",
|
|
3640
|
+
"defs",
|
|
3641
|
+
"marker",
|
|
3642
|
+
"radialGradient",
|
|
3643
|
+
"stop",
|
|
3644
|
+
"linearGradient",
|
|
3645
|
+
"clipPath",
|
|
3646
|
+
"mask",
|
|
3647
|
+
"pattern",
|
|
3648
|
+
"image",
|
|
3649
|
+
"use",
|
|
3650
|
+
"symbol",
|
|
3651
|
+
"foreignObject",
|
|
3652
|
+
"feGaussianBlur",
|
|
3653
|
+
"feColorMatrix"
|
|
3654
|
+
];
|
|
3655
|
+
class Chain {
|
|
3656
|
+
constructor(tagName, options) {
|
|
3657
|
+
__publicField(this, "listener");
|
|
3658
|
+
__publicField(this, "element");
|
|
3659
|
+
/**
|
|
3660
|
+
* @description: 创建元素
|
|
3661
|
+
* @param {string} tagName
|
|
3662
|
+
* @param {ElementCreationOptions} options
|
|
3663
|
+
* @return {Chain}
|
|
3664
|
+
*/
|
|
3665
|
+
__publicField(this, "create", (tagName, options) => {
|
|
3666
|
+
if (SVG_TAG_NAMES.includes(tagName)) {
|
|
3667
|
+
return document.createElementNS(SVG_NS, tagName, options);
|
|
3668
|
+
}
|
|
3669
|
+
return document.createElement(tagName, options);
|
|
3670
|
+
});
|
|
3671
|
+
/**
|
|
3672
|
+
* @description: 设置当前元素的属性
|
|
3673
|
+
* @param {string} name
|
|
3674
|
+
* @param {string} value
|
|
3675
|
+
* @return {Chain}
|
|
3676
|
+
*/
|
|
3677
|
+
__publicField(this, "setAttribute", (name, value) => {
|
|
3678
|
+
this.element.setAttribute(name, value);
|
|
3679
|
+
return this;
|
|
3680
|
+
});
|
|
3681
|
+
/**
|
|
3682
|
+
* @description: 移除当前元素的属性
|
|
3683
|
+
* @param {string} name
|
|
3684
|
+
* @return {Chain}
|
|
3685
|
+
*/
|
|
3686
|
+
__publicField(this, "removeAttribute", (name) => {
|
|
3687
|
+
this.element.removeAttribute(name);
|
|
3688
|
+
return this;
|
|
3689
|
+
});
|
|
3690
|
+
/**
|
|
3691
|
+
* @description: 当前元素添加子元素
|
|
3692
|
+
* @param {HTMLElement} child
|
|
3693
|
+
* @return {ChainElement}
|
|
3694
|
+
*/
|
|
3695
|
+
__publicField(this, "append", (child) => {
|
|
3696
|
+
this.element.appendChild(child);
|
|
3697
|
+
return this;
|
|
3698
|
+
});
|
|
3699
|
+
/**
|
|
3700
|
+
* @description: 当前元素移除子元素
|
|
3701
|
+
* @param {HTMLElement} child
|
|
3702
|
+
* @return {Chain}
|
|
3703
|
+
*/
|
|
3704
|
+
__publicField(this, "remove", (child) => {
|
|
3705
|
+
this.element.removeChild(child);
|
|
3706
|
+
return this;
|
|
3707
|
+
});
|
|
3708
|
+
/**
|
|
3709
|
+
* @description: 给当前元素设置文本内容
|
|
3710
|
+
* @param {string} text
|
|
3711
|
+
* @return {Chain}
|
|
3712
|
+
*/
|
|
3713
|
+
__publicField(this, "setTextContent", (text) => {
|
|
3714
|
+
this.element.textContent = text;
|
|
3715
|
+
return this;
|
|
3716
|
+
});
|
|
3717
|
+
/**
|
|
3718
|
+
* @description: 给当前元素设置样式
|
|
3719
|
+
* @param {string} name
|
|
3720
|
+
* @param {string} value
|
|
3721
|
+
* @return {Chain}
|
|
3722
|
+
*/
|
|
3723
|
+
__publicField(this, "setStyle", (name, value) => {
|
|
3724
|
+
this.element.style.setProperty(name, value);
|
|
3725
|
+
return this;
|
|
3726
|
+
});
|
|
3727
|
+
// 根据不同的子元素类型,添加元素
|
|
3728
|
+
__publicField(this, "addElementByType", (item, parent) => {
|
|
3729
|
+
if (item instanceof Chain) {
|
|
3730
|
+
parent.appendChild(item.element);
|
|
3731
|
+
}
|
|
3732
|
+
if (item instanceof HTMLElement) {
|
|
3733
|
+
parent.appendChild(item);
|
|
3734
|
+
}
|
|
3735
|
+
});
|
|
3736
|
+
/**
|
|
3737
|
+
* @description: 给当前元素添加子元素
|
|
3738
|
+
* @return {Chain}
|
|
3739
|
+
*/
|
|
3740
|
+
__publicField(this, "addChild", (child) => {
|
|
3741
|
+
if (Array.isArray(child)) {
|
|
3742
|
+
const Fragment = document.createDocumentFragment();
|
|
3743
|
+
child.forEach((item) => {
|
|
3744
|
+
this.addElementByType(item, Fragment);
|
|
3745
|
+
});
|
|
3746
|
+
this.element.appendChild(Fragment);
|
|
3747
|
+
} else {
|
|
3748
|
+
this.addElementByType(child, this.element);
|
|
3749
|
+
}
|
|
3750
|
+
return this;
|
|
3751
|
+
});
|
|
3752
|
+
/**
|
|
3753
|
+
* @description: 给当前元素添加事件监听
|
|
3754
|
+
* @param {string} type
|
|
3755
|
+
* @param {EventListener} listener
|
|
3756
|
+
* @return {Chain}
|
|
3757
|
+
*/
|
|
3758
|
+
__publicField(this, "listen", (type, listener, options) => {
|
|
3759
|
+
let event = this.listener.get(type);
|
|
3760
|
+
if (!event) {
|
|
3761
|
+
event = /* @__PURE__ */ new Map();
|
|
3762
|
+
this.listener.set(type, event);
|
|
3763
|
+
}
|
|
3764
|
+
const value = event.get(listener.name);
|
|
3765
|
+
if (value === listener) {
|
|
3766
|
+
console.warn(`${value.name} listener has been added to ${type} event, please remove it first.`);
|
|
3767
|
+
}
|
|
3768
|
+
this.element.addEventListener(type, listener, options);
|
|
3769
|
+
event.set(listener.name, listener);
|
|
3770
|
+
return this;
|
|
3771
|
+
});
|
|
3772
|
+
/**
|
|
3773
|
+
* @description: 移除当前元素的事件监听
|
|
3774
|
+
* @param {string} type
|
|
3775
|
+
* @return {Chain}
|
|
3776
|
+
*/
|
|
3777
|
+
__publicField(this, "clearListener", (type, listener, options) => {
|
|
3778
|
+
this.element.removeEventListener(type, listener, options);
|
|
3779
|
+
const event = this.listener.get(type);
|
|
3780
|
+
if (event) {
|
|
3781
|
+
event.delete(listener.name);
|
|
3782
|
+
} else {
|
|
3783
|
+
console.warn(`No ${type} event listener has been added.`);
|
|
3784
|
+
}
|
|
3785
|
+
return this;
|
|
3786
|
+
});
|
|
3787
|
+
/**
|
|
3788
|
+
* @description: 移除当前元素的所有事件监听
|
|
3789
|
+
* @return {Chain}
|
|
3790
|
+
*/
|
|
3791
|
+
__publicField(this, "clearAllListener", () => {
|
|
3792
|
+
for (const [key, value] of this.listener) {
|
|
3793
|
+
for (const [k2, v2] of value) {
|
|
3794
|
+
this.element.removeEventListener(key, v2);
|
|
3795
|
+
value.delete(k2);
|
|
3796
|
+
}
|
|
3797
|
+
this.listener.delete(key);
|
|
3798
|
+
}
|
|
3799
|
+
return this;
|
|
3800
|
+
});
|
|
3801
|
+
this.element = this.create(tagName, options);
|
|
3802
|
+
this.listener = /* @__PURE__ */ new Map();
|
|
3803
|
+
}
|
|
3804
|
+
}
|
|
3805
|
+
const create = (tagName, options) => {
|
|
3806
|
+
return new Chain(tagName, options);
|
|
3807
|
+
};
|
|
3808
|
+
const handleError = (hooks = noop) => {
|
|
3809
|
+
if (typeof window !== "undefined") {
|
|
3810
|
+
window.addEventListener(
|
|
3811
|
+
"unhandledrejection",
|
|
3812
|
+
(error) => {
|
|
3813
|
+
hooks(error);
|
|
3814
|
+
},
|
|
3815
|
+
true
|
|
3816
|
+
);
|
|
3817
|
+
window.addEventListener(
|
|
3818
|
+
"error",
|
|
3819
|
+
(error) => {
|
|
3820
|
+
hooks(error);
|
|
3821
|
+
return false;
|
|
3822
|
+
},
|
|
3823
|
+
true
|
|
3824
|
+
);
|
|
3825
|
+
}
|
|
3826
|
+
};
|
|
3827
|
+
const isImageSize = (file, width, height) => {
|
|
3828
|
+
return new Promise((resolve, reject) => {
|
|
3829
|
+
if (!isClient) reject("window is undefined");
|
|
3830
|
+
const _URL = window.URL || window.webkitURL;
|
|
3831
|
+
const img = new Image();
|
|
3832
|
+
img.onload = function() {
|
|
3833
|
+
let valid = false;
|
|
3834
|
+
if (width) {
|
|
3835
|
+
valid = img.width === width;
|
|
3836
|
+
}
|
|
3837
|
+
if (height) {
|
|
3838
|
+
valid = img.height === height;
|
|
3839
|
+
}
|
|
3840
|
+
_URL.revokeObjectURL(img.src);
|
|
3841
|
+
resolve(valid);
|
|
3842
|
+
};
|
|
3843
|
+
img.src = _URL.createObjectURL(file);
|
|
3844
|
+
});
|
|
3845
|
+
};
|
|
3846
|
+
const convertImageToBase64 = (file) => {
|
|
3847
|
+
return new Promise((resolve, reject) => {
|
|
3848
|
+
const reader = new FileReader();
|
|
3849
|
+
reader.onload = function() {
|
|
3850
|
+
resolve({ success: true, data: reader.result, message: "" });
|
|
3851
|
+
};
|
|
3852
|
+
reader.onerror = (e2) => {
|
|
3853
|
+
reject({ success: false, data: e2, message: "" });
|
|
3854
|
+
};
|
|
3855
|
+
reader.readAsDataURL(file);
|
|
3856
|
+
});
|
|
3857
|
+
};
|
|
3858
|
+
function getPerformance() {
|
|
3859
|
+
if (typeof window !== "undefined") {
|
|
3860
|
+
const [performanceNavigationTiming] = performance.getEntriesByType("navigation");
|
|
3861
|
+
const [firstPaint = {}, firstContentfulPaint = {}] = performance.getEntriesByType("paint");
|
|
3862
|
+
const { startTime: fp } = firstPaint;
|
|
3863
|
+
const { startTime: fcp } = firstContentfulPaint;
|
|
3864
|
+
const {
|
|
3865
|
+
domainLookupEnd,
|
|
3866
|
+
domainLookupStart,
|
|
3867
|
+
connectEnd,
|
|
3868
|
+
connectStart,
|
|
3869
|
+
secureConnectionStart,
|
|
3870
|
+
loadEventStart,
|
|
3871
|
+
domInteractive,
|
|
3872
|
+
domContentLoadedEventEnd,
|
|
3873
|
+
duration,
|
|
3874
|
+
responseStart,
|
|
3875
|
+
requestStart,
|
|
3876
|
+
responseEnd,
|
|
3877
|
+
fetchStart,
|
|
3878
|
+
transferSize,
|
|
3879
|
+
encodedBodySize,
|
|
3880
|
+
redirectEnd,
|
|
3881
|
+
redirectStart,
|
|
3882
|
+
redirectCount
|
|
3883
|
+
} = performanceNavigationTiming;
|
|
3884
|
+
return {
|
|
3885
|
+
// DNS
|
|
3886
|
+
dnsSearch: domainLookupEnd - domainLookupStart,
|
|
3887
|
+
// TCP
|
|
3888
|
+
tcpConnect: connectEnd - connectStart,
|
|
3889
|
+
sslConnect: connectEnd - secureConnectionStart,
|
|
3890
|
+
request: responseStart - requestStart,
|
|
3891
|
+
response: responseEnd - responseStart,
|
|
3892
|
+
parseDomTree: domInteractive - responseEnd,
|
|
3893
|
+
resource: loadEventStart - domContentLoadedEventEnd,
|
|
3894
|
+
domReady: domContentLoadedEventEnd - fetchStart,
|
|
3895
|
+
interactive: domInteractive - fetchStart,
|
|
3896
|
+
complete: loadEventStart - fetchStart,
|
|
3897
|
+
httpHead: transferSize - encodedBodySize,
|
|
3898
|
+
redirect: redirectCount,
|
|
3899
|
+
// redirect
|
|
3900
|
+
redirectTime: redirectEnd - redirectStart,
|
|
3901
|
+
duration,
|
|
3902
|
+
fp,
|
|
3903
|
+
fcp
|
|
3904
|
+
};
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
const sendBeacon = ({ url = "", type = "application/json; charset=UTF-8", payload = {} }) => {
|
|
3908
|
+
const requestUrl = url ? url : getHost();
|
|
3909
|
+
if (navigator.sendBeacon && requestUrl) {
|
|
3910
|
+
const param = new Blob([JSON.stringify(payload)], { type });
|
|
3911
|
+
return navigator.sendBeacon(requestUrl, param);
|
|
3912
|
+
}
|
|
3913
|
+
};
|
|
3914
|
+
const sendImage = ({ url = "", payload = {} }) => {
|
|
3915
|
+
const requestUrl = url ? url : getHost();
|
|
3916
|
+
if (typeof document !== "undefined" && requestUrl) {
|
|
3917
|
+
const image = new Image();
|
|
3918
|
+
image.width = 1;
|
|
3919
|
+
image.height = 1;
|
|
3920
|
+
image.src = `${requestUrl}?${querystring(payload)}`;
|
|
3921
|
+
}
|
|
3922
|
+
};
|
|
3923
|
+
const report = ({
|
|
3924
|
+
url = "",
|
|
3925
|
+
type = "application/json; charset=UTF-8",
|
|
3926
|
+
payload = {}
|
|
3927
|
+
}) => {
|
|
3928
|
+
const requestUrl = url ? url : getHost();
|
|
3929
|
+
if (typeof navigator !== "undefined") {
|
|
3930
|
+
return sendBeacon({ url: requestUrl, type, payload });
|
|
3931
|
+
}
|
|
3932
|
+
return sendImage({ url: requestUrl, payload });
|
|
3933
|
+
};
|
|
3934
|
+
function createData(params = {}) {
|
|
3935
|
+
if (typeof window !== "undefined") {
|
|
3936
|
+
const { width, height } = getWindow();
|
|
3937
|
+
return Object.assign(
|
|
3938
|
+
{},
|
|
3939
|
+
{
|
|
3940
|
+
id: randomString(),
|
|
3941
|
+
path: window.location.href,
|
|
3942
|
+
time: Date.now(),
|
|
3943
|
+
referrer: document.referrer,
|
|
3944
|
+
ip: window.returnCitySN || { cid: "", cip: "", cname: "" },
|
|
3945
|
+
userId: getCookie("chaxus_prod"),
|
|
3946
|
+
ratio: `${width}x${height}`,
|
|
3947
|
+
userAgent: window.navigator.userAgent
|
|
3948
|
+
},
|
|
3949
|
+
params
|
|
3950
|
+
);
|
|
3951
|
+
}
|
|
3952
|
+
return {};
|
|
3953
|
+
}
|
|
3954
|
+
const handleFetchHook = (options = {}) => {
|
|
3955
|
+
if (typeof window !== "undefined") {
|
|
3956
|
+
const { requestHook = noop, responseHook = noop, errorHook = noop } = options;
|
|
3957
|
+
const replacement = (originalFetch) => {
|
|
3958
|
+
return (url, config) => {
|
|
3959
|
+
requestHook(url, config);
|
|
3960
|
+
return originalFetch.apply(window, [url, config]).then((response) => {
|
|
3961
|
+
responseHook(url, config, response);
|
|
3962
|
+
return response;
|
|
3963
|
+
}).catch((error) => {
|
|
3964
|
+
errorHook(url, error);
|
|
3965
|
+
throw error;
|
|
3966
|
+
});
|
|
3967
|
+
};
|
|
3968
|
+
};
|
|
3969
|
+
replaceOld(window, "fetch", replacement);
|
|
3970
|
+
}
|
|
3971
|
+
};
|
|
3972
|
+
const handleXhrHook = (options = {}) => {
|
|
3973
|
+
if (typeof window !== "undefined") {
|
|
3974
|
+
const originalXhrProto = XMLHttpRequest.prototype;
|
|
3975
|
+
const { requestHook = noop, responseHook = noop, errorHook = noop } = options;
|
|
3976
|
+
const replacementXhrOpen = (originalOpen) => {
|
|
3977
|
+
return function(...args) {
|
|
3978
|
+
requestHook(args);
|
|
3979
|
+
originalOpen.apply(this, args);
|
|
3980
|
+
};
|
|
3981
|
+
};
|
|
3982
|
+
replaceOld(originalXhrProto, "open", replacementXhrOpen);
|
|
3983
|
+
const replacementXhrSend = (originalSend) => {
|
|
3984
|
+
return function(...args) {
|
|
3985
|
+
this.addEventListener("loadend", function() {
|
|
3986
|
+
responseHook(this);
|
|
3987
|
+
});
|
|
3988
|
+
this.addEventListener("error", function() {
|
|
3989
|
+
errorHook(this);
|
|
3990
|
+
});
|
|
3991
|
+
originalSend.apply(this, args);
|
|
3992
|
+
};
|
|
3993
|
+
};
|
|
3994
|
+
replaceOld(originalXhrProto, "send", replacementXhrSend);
|
|
3995
|
+
}
|
|
3996
|
+
};
|
|
3997
|
+
function throttle(func, delay = 300) {
|
|
3998
|
+
let lastCallTime = 0;
|
|
3999
|
+
let timeoutId = null;
|
|
4000
|
+
return function(...args) {
|
|
4001
|
+
const now = Date.now();
|
|
4002
|
+
if (now - lastCallTime >= delay) {
|
|
4003
|
+
if (timeoutId != null) {
|
|
4004
|
+
clearTimeout(timeoutId);
|
|
4005
|
+
timeoutId = null;
|
|
4006
|
+
}
|
|
4007
|
+
lastCallTime = now;
|
|
4008
|
+
func.apply(this, args);
|
|
4009
|
+
} else if (timeoutId == null) {
|
|
4010
|
+
timeoutId = window.setTimeout(
|
|
4011
|
+
() => {
|
|
4012
|
+
lastCallTime = Date.now();
|
|
4013
|
+
func.apply(this, args);
|
|
4014
|
+
timeoutId = null;
|
|
4015
|
+
},
|
|
4016
|
+
delay - (now - lastCallTime)
|
|
4017
|
+
);
|
|
4018
|
+
}
|
|
4019
|
+
};
|
|
4020
|
+
}
|
|
4021
|
+
const generateThrottle = () => {
|
|
4022
|
+
let lastCallTime = 0;
|
|
4023
|
+
let timeoutId = null;
|
|
4024
|
+
return function(func, delay = 300) {
|
|
4025
|
+
return function(...args) {
|
|
4026
|
+
const now = Date.now();
|
|
4027
|
+
if (now - lastCallTime >= delay) {
|
|
4028
|
+
if (timeoutId != null) {
|
|
4029
|
+
clearTimeout(timeoutId);
|
|
4030
|
+
timeoutId = null;
|
|
4031
|
+
}
|
|
4032
|
+
lastCallTime = now;
|
|
4033
|
+
func.apply(this, args);
|
|
4034
|
+
} else if (timeoutId == null) {
|
|
4035
|
+
timeoutId = window.setTimeout(
|
|
4036
|
+
() => {
|
|
4037
|
+
lastCallTime = Date.now();
|
|
4038
|
+
func.apply(this, args);
|
|
4039
|
+
timeoutId = null;
|
|
4040
|
+
},
|
|
4041
|
+
delay - (now - lastCallTime)
|
|
4042
|
+
);
|
|
4043
|
+
}
|
|
4044
|
+
};
|
|
4045
|
+
};
|
|
4046
|
+
};
|
|
4047
|
+
class Monitor {
|
|
4048
|
+
constructor() {
|
|
4049
|
+
__publicField(this, "init", () => {
|
|
4050
|
+
this.reportClick();
|
|
4051
|
+
this.reportError();
|
|
4052
|
+
this.reportFetch();
|
|
4053
|
+
this.reportPerformance();
|
|
4054
|
+
this.reportXhr();
|
|
4055
|
+
});
|
|
4056
|
+
this.initialize();
|
|
4057
|
+
}
|
|
4058
|
+
/**
|
|
4059
|
+
* @description: 页面加载性能上报
|
|
4060
|
+
* @param {Payload} param1
|
|
4061
|
+
*/
|
|
4062
|
+
reportPerformance() {
|
|
4063
|
+
const params = getPerformance();
|
|
4064
|
+
const payload = createData();
|
|
4065
|
+
report({
|
|
4066
|
+
payload: {
|
|
4067
|
+
...params,
|
|
4068
|
+
...payload
|
|
4069
|
+
}
|
|
4070
|
+
});
|
|
4071
|
+
}
|
|
4072
|
+
/**
|
|
4073
|
+
* @description: 手动触发的上报
|
|
4074
|
+
* @param {Record} payload
|
|
4075
|
+
* @param {*} unknown
|
|
4076
|
+
*/
|
|
4077
|
+
log(payload) {
|
|
4078
|
+
report({ payload });
|
|
4079
|
+
}
|
|
4080
|
+
/**
|
|
4081
|
+
* @description: 点击上报
|
|
4082
|
+
* @return {*}
|
|
4083
|
+
*/
|
|
4084
|
+
reportClick() {
|
|
4085
|
+
const throttleReport = throttle(report);
|
|
4086
|
+
const payload = createData();
|
|
4087
|
+
const hook = (event) => {
|
|
4088
|
+
const { pageX, pageY, screenX, screenY, type } = event;
|
|
4089
|
+
throttleReport({
|
|
4090
|
+
payload: {
|
|
4091
|
+
...payload,
|
|
4092
|
+
data: { pageX, pageY, screenX, screenY, type },
|
|
4093
|
+
type: "click"
|
|
4094
|
+
}
|
|
4095
|
+
});
|
|
4096
|
+
};
|
|
4097
|
+
handleClick(hook);
|
|
4098
|
+
}
|
|
4099
|
+
// ajax 上报
|
|
4100
|
+
reportXhr() {
|
|
4101
|
+
const throttleReport = throttle(report);
|
|
4102
|
+
const payload = createData();
|
|
4103
|
+
const requestHook = (...args) => {
|
|
4104
|
+
throttleReport({
|
|
4105
|
+
payload: { ...payload, data: { ...args }, type: "xhrRequest" }
|
|
4106
|
+
});
|
|
4107
|
+
};
|
|
4108
|
+
const responseHook = (...args) => {
|
|
4109
|
+
throttleReport({
|
|
4110
|
+
payload: { ...payload, data: { ...args }, type: "xhrResponse" }
|
|
4111
|
+
});
|
|
4112
|
+
};
|
|
4113
|
+
const errorHook = (...args) => {
|
|
4114
|
+
throttleReport({
|
|
4115
|
+
payload: { ...payload, data: { ...args }, type: "xhrError" }
|
|
4116
|
+
});
|
|
4117
|
+
};
|
|
4118
|
+
handleXhrHook({ requestHook, responseHook, errorHook });
|
|
4119
|
+
}
|
|
4120
|
+
/**
|
|
4121
|
+
* @description: fetch上报
|
|
4122
|
+
*/
|
|
4123
|
+
reportFetch() {
|
|
4124
|
+
const throttleReport = throttle(report);
|
|
4125
|
+
const payload = createData();
|
|
4126
|
+
const requestHook = (...args) => {
|
|
4127
|
+
throttleReport({
|
|
4128
|
+
payload: { ...payload, data: { ...args }, type: "fetchRequest" }
|
|
4129
|
+
});
|
|
4130
|
+
};
|
|
4131
|
+
const responseHook = (...args) => {
|
|
4132
|
+
throttleReport({
|
|
4133
|
+
payload: { ...payload, data: { ...args }, type: "fetchResponse" }
|
|
4134
|
+
});
|
|
4135
|
+
};
|
|
4136
|
+
const errorHook = (...args) => {
|
|
4137
|
+
throttleReport({
|
|
4138
|
+
payload: { ...payload, data: { ...args }, type: "fetchError" }
|
|
4139
|
+
});
|
|
4140
|
+
};
|
|
4141
|
+
handleFetchHook({ requestHook, responseHook, errorHook });
|
|
4142
|
+
}
|
|
4143
|
+
/**
|
|
4144
|
+
* @description: 错误上报
|
|
4145
|
+
*/
|
|
4146
|
+
reportError() {
|
|
4147
|
+
const throttleReport = throttle(report);
|
|
4148
|
+
const payload = createData();
|
|
4149
|
+
const hook = (...args) => {
|
|
4150
|
+
throttleReport({
|
|
4151
|
+
payload: { ...payload, data: { ...args }, type: "error" }
|
|
4152
|
+
});
|
|
4153
|
+
};
|
|
4154
|
+
handleError(hook);
|
|
4155
|
+
}
|
|
4156
|
+
/**
|
|
4157
|
+
* @description: 上报console
|
|
4158
|
+
*/
|
|
4159
|
+
reportConsole() {
|
|
4160
|
+
const throttleReport = throttle(report);
|
|
4161
|
+
const payload = createData();
|
|
4162
|
+
const hook = (...args) => {
|
|
4163
|
+
throttleReport({
|
|
4164
|
+
payload: { ...payload, data: { ...args }, type: "console" }
|
|
4165
|
+
});
|
|
4166
|
+
};
|
|
4167
|
+
handleConsole(hook);
|
|
4168
|
+
}
|
|
4169
|
+
initialize() {
|
|
4170
|
+
if (typeof window !== "undefined" && !window.ranlog) {
|
|
4171
|
+
window.ranlog = true;
|
|
4172
|
+
this.init();
|
|
4173
|
+
}
|
|
4174
|
+
if (typeof process !== "undefined" && !process.ranlog) {
|
|
4175
|
+
process.ranlog = true;
|
|
4176
|
+
this.init();
|
|
4177
|
+
}
|
|
4178
|
+
}
|
|
4179
|
+
}
|
|
4180
|
+
const codes = /* @__PURE__ */ new Map([
|
|
4181
|
+
[100, "Continue"],
|
|
4182
|
+
[101, "Switching Protocols"],
|
|
4183
|
+
[102, "Processing"],
|
|
4184
|
+
[103, "Early Hints"],
|
|
4185
|
+
[200, "OK"],
|
|
4186
|
+
[201, "Created"],
|
|
4187
|
+
[202, "Accepted"],
|
|
4188
|
+
[203, "Non-Authoritative Information"],
|
|
4189
|
+
[204, "No Content"],
|
|
4190
|
+
[205, "Reset Content"],
|
|
4191
|
+
[206, "Partial Content"],
|
|
4192
|
+
[207, "Multi-Status"],
|
|
4193
|
+
[208, "Already Reported"],
|
|
4194
|
+
[226, "IM Used"],
|
|
4195
|
+
[300, "Multiple Choices"],
|
|
4196
|
+
[301, "Moved Permanently"],
|
|
4197
|
+
[302, "Found"],
|
|
4198
|
+
[303, "See Other"],
|
|
4199
|
+
[304, "Not Modified"],
|
|
4200
|
+
[305, "Use Proxy"],
|
|
4201
|
+
[307, "Temporary Redirect"],
|
|
4202
|
+
[308, "Permanent Redirect"],
|
|
4203
|
+
[400, "Bad Request"],
|
|
4204
|
+
[401, "Unauthorized"],
|
|
4205
|
+
[402, "Payment Required"],
|
|
4206
|
+
[403, "Forbidden"],
|
|
4207
|
+
[404, "Not Found"],
|
|
4208
|
+
[405, "Method Not Allowed"],
|
|
4209
|
+
[406, "Not Acceptable"],
|
|
4210
|
+
[407, "Proxy Authentication Required"],
|
|
4211
|
+
[408, "Request Timeout"],
|
|
4212
|
+
[409, "Conflict"],
|
|
4213
|
+
[410, "Gone"],
|
|
4214
|
+
[411, "Length Required"],
|
|
4215
|
+
[412, "Precondition Failed"],
|
|
4216
|
+
[413, "Payload Too Large"],
|
|
4217
|
+
[414, "URI Too Long"],
|
|
4218
|
+
[415, "Unsupported Media Type"],
|
|
4219
|
+
[416, "Range Not Satisfiable"],
|
|
4220
|
+
[417, "Expectation Failed"],
|
|
4221
|
+
[418, "I'm a Teapot"],
|
|
4222
|
+
[421, "Misdirected Request"],
|
|
4223
|
+
[422, "Unprocessable Entity"],
|
|
4224
|
+
[423, "Locked"],
|
|
4225
|
+
[424, "Failed Dependency"],
|
|
4226
|
+
[425, "Too Early"],
|
|
4227
|
+
[426, "Upgrade Required"],
|
|
4228
|
+
[428, "Precondition Required"],
|
|
4229
|
+
[429, "Too Many Requests"],
|
|
4230
|
+
[431, "Request Header Fields Too Large"],
|
|
4231
|
+
[451, "Unavailable For Legal Reasons"],
|
|
4232
|
+
[500, "Internal Server Error"],
|
|
4233
|
+
[501, "Not Implemented"],
|
|
4234
|
+
[502, "Bad Gateway"],
|
|
4235
|
+
[503, "Service Unavailable"],
|
|
4236
|
+
[504, "Gateway Timeout"],
|
|
4237
|
+
[505, "HTTP Version Not Supported"],
|
|
4238
|
+
[506, "Variant Also Negotiates"],
|
|
4239
|
+
[507, "Insufficient Storage"],
|
|
4240
|
+
[508, "Loop Detected"],
|
|
4241
|
+
[509, "Bandwidth Limit Exceeded"],
|
|
4242
|
+
[510, "Not Extended"],
|
|
4243
|
+
[511, "Network Authentication Required"]
|
|
4244
|
+
]);
|
|
4245
|
+
const status = {
|
|
4246
|
+
// status code to message map
|
|
4247
|
+
message: codes,
|
|
4248
|
+
// status message (lower-case) to code map
|
|
4249
|
+
code: createMessageToStatusCodeMap(codes),
|
|
4250
|
+
// array of status codes
|
|
4251
|
+
codes: createStatusCodeList(codes),
|
|
4252
|
+
// status codes for redirects
|
|
4253
|
+
redirect: {
|
|
4254
|
+
300: true,
|
|
4255
|
+
301: true,
|
|
4256
|
+
302: true,
|
|
4257
|
+
303: true,
|
|
4258
|
+
305: true,
|
|
4259
|
+
307: true,
|
|
4260
|
+
308: true
|
|
4261
|
+
},
|
|
4262
|
+
// status codes for empty bodies
|
|
4263
|
+
empty: {
|
|
4264
|
+
204: true,
|
|
4265
|
+
205: true,
|
|
4266
|
+
304: true
|
|
4267
|
+
},
|
|
4268
|
+
// status codes for when you should retry the request
|
|
4269
|
+
retry: {
|
|
4270
|
+
502: true,
|
|
4271
|
+
503: true,
|
|
4272
|
+
504: true
|
|
4273
|
+
}
|
|
4274
|
+
};
|
|
4275
|
+
function createMessageToStatusCodeMap(codes2) {
|
|
4276
|
+
const map = /* @__PURE__ */ new Map();
|
|
4277
|
+
for (const [status2, message] of codes2) {
|
|
4278
|
+
map.set(message.toLowerCase(), status2);
|
|
4279
|
+
}
|
|
4280
|
+
return map;
|
|
4281
|
+
}
|
|
4282
|
+
function createStatusCodeList(codes2) {
|
|
4283
|
+
const codeList = [];
|
|
4284
|
+
for (const [status2, _] of codes2) {
|
|
4285
|
+
codeList.push(status2);
|
|
4286
|
+
}
|
|
4287
|
+
return codeList;
|
|
4288
|
+
}
|
|
4289
|
+
function getStatusCode(message) {
|
|
4290
|
+
const msg = message.toLowerCase();
|
|
4291
|
+
status.code.has(msg);
|
|
4292
|
+
if (!status.code.has(msg)) {
|
|
4293
|
+
throw new Error('invalid status message: "' + message + '"');
|
|
4294
|
+
}
|
|
4295
|
+
return status.code.get(msg);
|
|
4296
|
+
}
|
|
4297
|
+
function getStatusMessage(code) {
|
|
4298
|
+
if (!status.message.has(code)) {
|
|
4299
|
+
throw new Error("invalid status code: " + code);
|
|
4300
|
+
}
|
|
4301
|
+
return status.message.get(code);
|
|
4302
|
+
}
|
|
4303
|
+
function getStatus(code) {
|
|
4304
|
+
if (typeof code === "number") {
|
|
4305
|
+
return getStatusMessage(code);
|
|
4306
|
+
}
|
|
4307
|
+
if (typeof code !== "string") {
|
|
4308
|
+
throw new TypeError("code must be a number or string");
|
|
4309
|
+
}
|
|
4310
|
+
const n2 = parseInt(code, 10);
|
|
4311
|
+
if (!isNaN(n2) && status.codes.includes(n2)) {
|
|
4312
|
+
return getStatusMessage(n2);
|
|
4313
|
+
}
|
|
4314
|
+
return getStatusCode(code);
|
|
4315
|
+
}
|
|
4316
|
+
class QuestQueue {
|
|
4317
|
+
constructor({ simultaneous, total }) {
|
|
4318
|
+
__publicField(this, "current");
|
|
4319
|
+
__publicField(this, "queue");
|
|
4320
|
+
__publicField(this, "simultaneous");
|
|
4321
|
+
// 并发的请求
|
|
4322
|
+
__publicField(this, "executed");
|
|
4323
|
+
__publicField(this, "total");
|
|
4324
|
+
/**
|
|
4325
|
+
* @description: 传入异步函数,添加到队列并执行
|
|
4326
|
+
* @param {Fun} asynchronous
|
|
4327
|
+
* @return {*}
|
|
4328
|
+
*/
|
|
4329
|
+
__publicField(this, "add", (asynchronous) => {
|
|
4330
|
+
if (typeof asynchronous !== "function") return;
|
|
4331
|
+
const task = () => {
|
|
4332
|
+
return new Promise((resolve, reject) => {
|
|
4333
|
+
this.current++;
|
|
4334
|
+
asynchronous().then(resolve).catch(reject).finally(() => {
|
|
4335
|
+
this.current--;
|
|
4336
|
+
this.running();
|
|
4337
|
+
});
|
|
4338
|
+
});
|
|
4339
|
+
};
|
|
4340
|
+
this.queue.push(task);
|
|
4341
|
+
});
|
|
4342
|
+
/**
|
|
4343
|
+
* @description: 执行异步函数
|
|
4344
|
+
* @param {*} Promise
|
|
4345
|
+
* @return {*}
|
|
4346
|
+
*/
|
|
4347
|
+
__publicField(this, "running", () => {
|
|
4348
|
+
return new Promise((resolve, reject) => {
|
|
4349
|
+
if (this.current <= this.simultaneous && this.queue.length) {
|
|
4350
|
+
const task = this.queue.pop();
|
|
4351
|
+
if (task) {
|
|
4352
|
+
task().then(resolve).catch(reject).finally(() => {
|
|
4353
|
+
this.executed++;
|
|
4354
|
+
});
|
|
4355
|
+
}
|
|
4356
|
+
} else {
|
|
4357
|
+
resolve({});
|
|
4358
|
+
}
|
|
4359
|
+
});
|
|
4360
|
+
});
|
|
4361
|
+
/**
|
|
4362
|
+
* @description: 并发执行所有的异步函数,并返回所有的结果
|
|
4363
|
+
* @param {*} Promise
|
|
4364
|
+
* @return {*}
|
|
4365
|
+
*/
|
|
4366
|
+
__publicField(this, "allSettled", () => {
|
|
4367
|
+
let index = 0;
|
|
4368
|
+
return new Promise((resolve, reject) => {
|
|
4369
|
+
const result = [];
|
|
4370
|
+
if (this.current < this.simultaneous && this.queue.length) {
|
|
4371
|
+
const task = this.queue.pop();
|
|
4372
|
+
index++;
|
|
4373
|
+
if (task) {
|
|
4374
|
+
task().then((x2) => {
|
|
4375
|
+
result[index] = x2;
|
|
4376
|
+
resolve(x2);
|
|
4377
|
+
}).catch((x2) => {
|
|
4378
|
+
result[index] = x2;
|
|
4379
|
+
reject(x2);
|
|
4380
|
+
}).finally(() => {
|
|
4381
|
+
this.executed++;
|
|
4382
|
+
});
|
|
4383
|
+
}
|
|
4384
|
+
}
|
|
4385
|
+
if (this.executed >= this.total) {
|
|
4386
|
+
resolve(result);
|
|
4387
|
+
}
|
|
4388
|
+
});
|
|
4389
|
+
});
|
|
4390
|
+
this.current = 0;
|
|
4391
|
+
this.queue = [];
|
|
4392
|
+
this.simultaneous = simultaneous;
|
|
4393
|
+
this.executed = 0;
|
|
4394
|
+
this.total = total;
|
|
4395
|
+
}
|
|
4396
|
+
}
|
|
4397
|
+
const scriptOnLoad = (urls, append, callback) => {
|
|
4398
|
+
urls = Array.isArray(urls) ? urls : [urls];
|
|
4399
|
+
const array = urls.map((src) => {
|
|
4400
|
+
const isCss = src.toLowerCase().endsWith(".css");
|
|
4401
|
+
let script;
|
|
4402
|
+
if (isCss) {
|
|
4403
|
+
const link = document.createElement("link");
|
|
4404
|
+
link.type = "text/css";
|
|
4405
|
+
link.rel = "stylesheet";
|
|
4406
|
+
link.href = src;
|
|
4407
|
+
script = link;
|
|
4408
|
+
} else {
|
|
4409
|
+
script = document.createElement("script");
|
|
4410
|
+
script.type = "text/javascript";
|
|
4411
|
+
script.src = src;
|
|
4412
|
+
}
|
|
4413
|
+
const bodyElement = document.getElementsByTagName("body")[0];
|
|
4414
|
+
const currentAppend = append || bodyElement;
|
|
4415
|
+
currentAppend.appendChild(script);
|
|
4416
|
+
return new Promise((resolve) => {
|
|
4417
|
+
script.onload = () => {
|
|
4418
|
+
resolve();
|
|
4419
|
+
};
|
|
4420
|
+
});
|
|
4421
|
+
});
|
|
4422
|
+
return new Promise((resolve) => {
|
|
4423
|
+
Promise.all(array).then(() => {
|
|
4424
|
+
if (typeof callback === "function") {
|
|
4425
|
+
callback();
|
|
4426
|
+
}
|
|
4427
|
+
resolve();
|
|
4428
|
+
});
|
|
4429
|
+
});
|
|
4430
|
+
};
|
|
4431
|
+
export {
|
|
4432
|
+
AudioRecorder,
|
|
4433
|
+
BridgeManager,
|
|
4434
|
+
Chain,
|
|
4435
|
+
Client,
|
|
4436
|
+
C as Color,
|
|
4437
|
+
a as ColorScheme,
|
|
4438
|
+
F as FMT,
|
|
4439
|
+
H as Hsl,
|
|
4440
|
+
b as Hsla,
|
|
4441
|
+
Mathjs,
|
|
4442
|
+
MessageCodec,
|
|
4443
|
+
M as MimeType,
|
|
4444
|
+
Monitor,
|
|
4445
|
+
Platform,
|
|
4446
|
+
PostMessageBridge,
|
|
4447
|
+
QuestQueue,
|
|
4448
|
+
R as Rgb,
|
|
4449
|
+
c as Rgba,
|
|
4450
|
+
SyncHook,
|
|
4451
|
+
TOTP,
|
|
4452
|
+
addClassToElement,
|
|
4453
|
+
addNumSym,
|
|
4454
|
+
appendUrl,
|
|
4455
|
+
audioVendor,
|
|
4456
|
+
bridgeManager,
|
|
4457
|
+
canvasVendor,
|
|
4458
|
+
d as changeHumpToLowerCase,
|
|
4459
|
+
z as checkEncoding,
|
|
4460
|
+
e as clearBr,
|
|
4461
|
+
f as clearStr,
|
|
4462
|
+
cloneDeep,
|
|
4463
|
+
g as componentToHex,
|
|
4464
|
+
compose,
|
|
4465
|
+
connection,
|
|
4466
|
+
convertImageToBase64,
|
|
4467
|
+
create,
|
|
4468
|
+
createData,
|
|
4469
|
+
createDocumentFragment,
|
|
4470
|
+
createObjectURL,
|
|
4471
|
+
createSignal,
|
|
4472
|
+
currentDevice,
|
|
4473
|
+
debounce,
|
|
4474
|
+
durationHandler,
|
|
4475
|
+
encodeUrl,
|
|
4476
|
+
escapeHtml,
|
|
4477
|
+
filterObj,
|
|
4478
|
+
formatJson,
|
|
4479
|
+
generateThrottle,
|
|
4480
|
+
getAllQueryString,
|
|
4481
|
+
getCookie,
|
|
4482
|
+
getCookieByName,
|
|
4483
|
+
G as getExtensions,
|
|
4484
|
+
getFrame,
|
|
4485
|
+
getHost,
|
|
4486
|
+
x as getMatchingSentences,
|
|
4487
|
+
h as getMime,
|
|
4488
|
+
getPerformance,
|
|
4489
|
+
getPixelRatio,
|
|
4490
|
+
getQuery,
|
|
4491
|
+
getRandomString,
|
|
4492
|
+
getStatus,
|
|
4493
|
+
getWindow,
|
|
4494
|
+
handleConsole,
|
|
4495
|
+
handleError,
|
|
4496
|
+
handleFetchHook,
|
|
4497
|
+
i as hexToRgb,
|
|
4498
|
+
j as hsbToRgb,
|
|
4499
|
+
k as hslToRgb,
|
|
4500
|
+
l as hsvToHsl,
|
|
4501
|
+
m as hsvToRgb,
|
|
4502
|
+
n as hue2rgb,
|
|
4503
|
+
imageRequest,
|
|
4504
|
+
isBangDevice,
|
|
4505
|
+
isClient,
|
|
4506
|
+
isEqual,
|
|
4507
|
+
isImageSize,
|
|
4508
|
+
isMobile,
|
|
4509
|
+
isSafari,
|
|
4510
|
+
isString,
|
|
4511
|
+
isWeiXin,
|
|
4512
|
+
localStorageGetItem,
|
|
4513
|
+
localStorageSetItem,
|
|
4514
|
+
mathjs,
|
|
4515
|
+
B as md5,
|
|
4516
|
+
memoize,
|
|
4517
|
+
merge,
|
|
4518
|
+
mergeExports,
|
|
4519
|
+
networkSpeed,
|
|
4520
|
+
noop,
|
|
4521
|
+
perToNum,
|
|
4522
|
+
performanceTime,
|
|
4523
|
+
querystring,
|
|
4524
|
+
r as randomColor,
|
|
4525
|
+
randomString,
|
|
4526
|
+
range,
|
|
4527
|
+
removeClassToElement,
|
|
4528
|
+
removeGhosting,
|
|
4529
|
+
replaceOld,
|
|
4530
|
+
report,
|
|
4531
|
+
requestUrlToBuffer,
|
|
4532
|
+
retain,
|
|
4533
|
+
q as rgbToHex,
|
|
4534
|
+
s as rgbToHsb,
|
|
4535
|
+
t as rgbToHsl,
|
|
4536
|
+
scriptOnLoad,
|
|
4537
|
+
setAttributeByGlobal,
|
|
4538
|
+
setFontSize2html,
|
|
4539
|
+
u as setMime,
|
|
4540
|
+
status,
|
|
4541
|
+
v as str2Xml,
|
|
4542
|
+
w as strParse,
|
|
4543
|
+
subscribers,
|
|
4544
|
+
throttle,
|
|
4545
|
+
timeFormat,
|
|
4546
|
+
timestampToTime,
|
|
4547
|
+
toString,
|
|
4548
|
+
transformNumber,
|
|
4549
|
+
A as transformText,
|
|
4550
|
+
webglVendor
|
|
4551
|
+
};
|