diff --git a/ff-request.js b/ff-request.js index 165865e..5bede71 100644 --- a/ff-request.js +++ b/ff-request.js @@ -1,209 +1,209 @@ -var de = Object.defineProperty; -var oe = (r) => { +var pe = Object.defineProperty; +var ce = (r) => { throw TypeError(r); }; -var ge = (r, e, t) => e in r ? de(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t; -var g = (r, e, t) => ge(r, typeof e != "symbol" ? e + "" : e, t), ae = (r, e, t) => e.has(r) || oe("Cannot " + t); -var s = (r, e, t) => (ae(r, e, "read from private field"), t ? t.call(r) : e.get(r)), w = (r, e, t) => e.has(r) ? oe("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(r) : e.set(r, t), C = (r, e, t, n) => (ae(r, e, "write to private field"), n ? n.call(r, t) : e.set(r, t), t); -function pe(r) { +var ve = (r, e, t) => e in r ? pe(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t; +var g = (r, e, t) => ve(r, typeof e != "symbol" ? e + "" : e, t), he = (r, e, t) => e.has(r) || ce("Cannot " + t); +var s = (r, e, t) => (he(r, e, "read from private field"), t ? t.call(r) : e.get(r)), p = (r, e, t) => e.has(r) ? ce("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(r) : e.set(r, t), _ = (r, e, t, n) => (he(r, e, "write to private field"), n ? n.call(r, t) : e.set(r, t), t); +function we(r) { return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r; } -var he = {}; +var fe = {}; (function(r) { (function(e) { e([], function() { - var t = -1, n = -2, i = -3, o = -4, l = -5, R = function(u, f) { + var t = -1, n = -2, i = -3, c = -4, u = -5, U = function(h, f) { f = f || {}; - var h = f.verbose || !1; - h && console.log("Normalize the JSON Object"), u = typeof u == "string" ? this.JSON.parse(u) : u, h && console.log("Creating a empty dictionary"); + var l = f.verbose || !1; + l && console.log("Normalize the JSON Object"), h = typeof h == "string" ? this.JSON.parse(h) : h, l && console.log("Creating a empty dictionary"); var d = { strings: [], integers: [], floats: [] }; - h && console.log("Creating the AST"); - var p = function P(a) { - h && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(a)); - var y = typeof a; - if (a === null) + l && console.log("Creating the AST"); + var v = function O(o) { + l && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(o)); + var y = typeof o; + if (o === null) return { type: "null", index: i }; - if (typeof a > "u") + if (typeof o > "u") return { type: "undefined", - index: l + index: u }; - if (a instanceof Array) { + if (o instanceof Array) { var S = ["@"]; - for (var b in a) - a.hasOwnProperty(b) && S.push(P(a[b])); + for (var b in o) + o.hasOwnProperty(b) && S.push(O(o[b])); return S; } if (y === "object") { var S = ["$"]; - for (var m in a) - a.hasOwnProperty(m) && (S.push(P(m)), S.push(P(a[m]))); + for (var N in o) + o.hasOwnProperty(N) && (S.push(O(N)), S.push(O(o[N]))); return S; } - if (a === "") + if (o === "") return { type: "empty", - index: o + index: c }; if (y === "string") { - var O = H.call(d.strings, a); - return O == -1 && (d.strings.push(T(a)), O = d.strings.length - 1), { + var P = H.call(d.strings, o); + return P == -1 && (d.strings.push(T(o)), P = d.strings.length - 1), { type: "strings", - index: O + index: P }; } - if (y === "number" && a % 1 === 0) { - var O = H.call(d.integers, a); - return O == -1 && (d.integers.push(_(a)), O = d.integers.length - 1), { + if (y === "number" && o % 1 === 0) { + var P = H.call(d.integers, o); + return P == -1 && (d.integers.push(m(o)), P = d.integers.length - 1), { type: "integers", - index: O + index: P }; } if (y === "number") { - var O = H.call(d.floats, a); - return O == -1 && (d.floats.push(a), O = d.floats.length - 1), { + var P = H.call(d.floats, o); + return P == -1 && (d.floats.push(o), P = d.floats.length - 1), { type: "floats", - index: O + index: P }; } if (y === "boolean") return { type: "boolean", - index: a ? t : n + index: o ? t : n }; - throw new Error("Unexpected argument of type " + typeof a); - }(u), E = d.strings.length, j = d.integers.length; - d.floats.length, h && console.log("Parsing the dictionary"); - var N = d.strings.join("|"); - return N += "^" + d.integers.join("|"), N += "^" + d.floats.join("|"), h && console.log("Parsing the structure"), N += "^" + function P(a) { - if (h && console.log("Calling a recursiveParser with " + this.JSON.stringify(a)), a instanceof Array) { - var y = a.shift(); - for (var S in a) - a.hasOwnProperty(S) && (y += P(a[S]) + "|"); + throw new Error("Unexpected argument of type " + typeof o); + }(h), E = d.strings.length, J = d.integers.length; + d.floats.length, l && console.log("Parsing the dictionary"); + var M = d.strings.join("|"); + return M += "^" + d.integers.join("|"), M += "^" + d.floats.join("|"), l && console.log("Parsing the structure"), M += "^" + function O(o) { + if (l && console.log("Calling a recursiveParser with " + this.JSON.stringify(o)), o instanceof Array) { + var y = o.shift(); + for (var S in o) + o.hasOwnProperty(S) && (y += O(o[S]) + "|"); return (y[y.length - 1] === "|" ? y.slice(0, -1) : y) + "]"; } - var b = a.type, m = a.index; + var b = o.type, N = o.index; if (b === "strings") - return _(m); + return m(N); if (b === "integers") - return _(E + m); + return m(E + N); if (b === "floats") - return _(E + j + m); + return m(E + J + N); if (b === "boolean") - return a.index; + return o.index; if (b === "null") return i; if (b === "undefined") - return l; + return u; if (b === "empty") - return o; + return c; throw new TypeError("The item is alien!"); - }(p), h && console.log("Ending parser"), f.debug ? { + }(v), l && console.log("Ending parser"), f.debug ? { dictionary: d, - ast: p, - packed: N - } : N; - }, c = function(u, f) { + ast: v, + packed: M + } : M; + }, a = function(h, f) { f = f || {}; - var h = u.split("^"); + var l = h.split("^"); f.verbose && console.log("Building dictionary"); - var d = [], p = h[0]; - if (p !== "") { - p = p.split("|"), f.verbose && console.log("Parse the strings dictionary"); - for (var E = 0, j = p.length; E < j; E++) - d.push(M(p[E])); + var d = [], v = l[0]; + if (v !== "") { + v = v.split("|"), f.verbose && console.log("Parse the strings dictionary"); + for (var E = 0, J = v.length; E < J; E++) + d.push(C(v[E])); } - if (p = h[1], p !== "") { - p = p.split("|"), f.verbose && console.log("Parse the integers dictionary"); - for (var E = 0, j = p.length; E < j; E++) - d.push(U(p[E])); + if (v = l[1], v !== "") { + v = v.split("|"), f.verbose && console.log("Parse the integers dictionary"); + for (var E = 0, J = v.length; E < J; E++) + d.push(I(v[E])); } - if (p = h[2], p !== "") { - p = p.split("|"), f.verbose && console.log("Parse the floats dictionary"); - for (var E = 0, j = p.length; E < j; E++) - d.push(parseFloat(p[E])); + if (v = l[2], v !== "") { + v = v.split("|"), f.verbose && console.log("Parse the floats dictionary"); + for (var E = 0, J = v.length; E < J; E++) + d.push(parseFloat(v[E])); } - p = null, f.verbose && console.log("Tokenizing the structure"); - for (var N = "", P = [], a = h[3].length, E = 0; E < a; E++) { - var y = h[3].charAt(E); - y === "|" || y === "$" || y === "@" || y === "]" ? (N && (P.push(U(N)), N = ""), y !== "|" && P.push(y)) : N += y; + v = null, f.verbose && console.log("Tokenizing the structure"); + for (var M = "", O = [], o = l[3].length, E = 0; E < o; E++) { + var y = l[3].charAt(E); + y === "|" || y === "$" || y === "@" || y === "]" ? (M && (O.push(I(M)), M = ""), y !== "|" && O.push(y)) : M += y; } - var S = P.length, b = 0; - return f.verbose && console.log("Starting recursive parser"), function m() { - var O = P[b++]; - if (f.verbose && console.log("Reading collection type " + (O === "$" ? "object" : "Array")), O === "@") { - for (var v = []; b < S; b++) { - var A = P[b]; + var S = O.length, b = 0; + return f.verbose && console.log("Starting recursive parser"), function N() { + var P = O[b++]; + if (f.verbose && console.log("Reading collection type " + (P === "$" ? "object" : "Array")), P === "@") { + for (var w = []; b < S; b++) { + var A = O[b]; if (f.verbose && console.log("Read " + A + " symbol"), A === "]") - return v; + return w; if (A === "@" || A === "$") - v.push(m()); + w.push(N()); else switch (A) { case t: - v.push(!0); + w.push(!0); break; case n: - v.push(!1); + w.push(!1); break; case i: - v.push(null); + w.push(null); break; - case l: - v.push(void 0); + case u: + w.push(void 0); break; - case o: - v.push(""); + case c: + w.push(""); break; default: - v.push(d[A]); + w.push(d[A]); } } - return f.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; + return f.verbose && console.log("Parsed " + this.JSON.stringify(w)), w; } - if (O === "$") { - for (var v = {}; b < S; b++) { - var x = P[b]; + if (P === "$") { + for (var w = {}; b < S; b++) { + var x = O[b]; if (x === "]") - return v; - x === o ? x = "" : x = d[x]; - var A = P[++b]; + return w; + x === c ? x = "" : x = d[x]; + var A = O[++b]; if (A === "@" || A === "$") - v[x] = m(); + w[x] = N(); else switch (A) { case t: - v[x] = !0; + w[x] = !0; break; case n: - v[x] = !1; + w[x] = !1; break; case i: - v[x] = null; + w[x] = null; break; - case l: - v[x] = void 0; + case u: + w[x] = void 0; break; - case o: - v[x] = ""; + case c: + w[x] = ""; break; default: - v[x] = d[A]; + w[x] = d[A]; } } - return f.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; + return f.verbose && console.log("Parsed " + this.JSON.stringify(w)), w; } - throw new TypeError("Bad token " + O + " isn't a type"); + throw new TypeError("Bad token " + P + " isn't a type"); }(); - }, T = function(u) { - return typeof u != "string" ? u : u.replace(/[\+ \|\^\%]/g, function(f) { + }, T = function(h) { + return typeof h != "string" ? h : h.replace(/[\+ \|\^\%]/g, function(f) { return { " ": "+", "+": "%2B", @@ -212,8 +212,8 @@ var he = {}; "%": "%25" }[f]; }); - }, M = function(u) { - return typeof u != "string" ? u : u.replace(/\+|%2B|%7C|%5E|%25/g, function(f) { + }, C = function(h) { + return typeof h != "string" ? h : h.replace(/\+|%2B|%7C|%5E|%25/g, function(f) { return { "+": " ", "%2B": "+", @@ -222,20 +222,20 @@ var he = {}; "%25": "%" }[f]; }); - }, _ = function(u) { - return Number.prototype.toString.call(u, 36).toUpperCase(); - }, U = function(u) { - return parseInt(u, 36); - }, H = Array.prototype.indexOf || function(u, f) { - for (var h = f || 0, d = this.length; h < d; h++) - if (this[h] === u) - return h; + }, m = function(h) { + return Number.prototype.toString.call(h, 36).toUpperCase(); + }, I = function(h) { + return parseInt(h, 36); + }, H = Array.prototype.indexOf || function(h, f) { + for (var l = f || 0, d = this.length; l < d; l++) + if (this[l] === h) + return l; return -1; }; return { JSON, - pack: R, - unpack: c + pack: U, + unpack: a }; }); })(function(e, t) { @@ -243,10 +243,10 @@ var he = {}; for (var i in n) r[i] = n[i]; }); -})(he); -const ce = /* @__PURE__ */ pe(he); -var B, L, G, I, Q, W, q, X, Z, ee; -class se { +})(fe); +const le = /* @__PURE__ */ we(fe); +var $, q, B, D, W, X, z, Z, ee, te, re; +const ae = class ae { /** * @param {number} code * @param {string} message @@ -254,7 +254,7 @@ class se { * @param {string} url * @param {string} res */ - constructor(e, t, n, i, o) { + constructor(e, t, n, i, c) { /** @type {number} */ g(this, "code", 0); /** @type {string} */ @@ -266,82 +266,99 @@ class se { /** @type {string} */ g(this, "res", ""); /** @type {HttpResponse} */ - w(this, B); + p(this, $); // 判断返回 Response 对象, 还是 Response 中的 data /** @type {boolean} */ - w(this, L, !0); + p(this, q, !0); // 判断是否已经给出错误提示 /** @type {boolean} */ - w(this, G, !0); - w(this, I, { + p(this, B, !0); + p(this, D, { /** @param {any} __ @param {string} key */ - get: (e, t) => s(this, Q).call(this, t), + get: (e, t) => s(this, W).call(this, t), + /** @param {any} __ @param {string} key */ + set: (e, t) => { + throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${t}"`); + }, /** @returns {string[]} */ - ownKeys: () => Object.keys(s(this, I).getPrototypeOf() || {}), + ownKeys: () => Object.keys(s(this, D).getPrototypeOf() || {}), /** @returns {HttpResponse | unknown} */ - getPrototypeOf: () => s(this, L) ? this : this.data, + getPrototypeOf: () => s(this, q) ? this : this.data, /** @returns {PropertyDescriptor} */ getOwnPropertyDescriptor: () => ({ configurable: !0, enumerable: !0, writable: !0, - value: s(this, I).getPrototypeOf() + value: s(this, D).getPrototypeOf() }) }); /** * @param {string} key * @returns {unknown} */ - w(this, Q, (e) => e === "$echoMsg" ? s(this, q) : e === "msg" ? s(this, W) : e === "then" || e === "resp" ? (C(this, G, !0), C(this, L, e === "resp"), s(this, X)) : e === "catch" ? s(this, Z) : e === "finally" ? s(this, ee) : e === "IS_ECHO_MSG" ? s(this, G) : Reflect.get(this, e)); + p(this, W, (e) => e === "$echoMsg" ? s(this, z) : e === "msg" ? s(this, X) : e === "then" ? (_(this, B, !0), s(this, Z)) : e === "resp" ? (_(this, B, !0), s(this, ee)) : e === "catch" ? s(this, te) : e === "finally" ? s(this, re) : e === "IS_ECHO_MSG" ? s(this, B) : Reflect.get(this, e)); /** * @param {(value: unknown) => unknown} [fn] * @param {boolean} [isResp] * @returns {Promise} */ - w(this, W, (e = (n) => n, t = !1) => (s(this, q).call(this), C(this, L, t), Promise.resolve(e == null ? void 0 : e(s(this, I).getPrototypeOf())))); - w(this, q, () => { - s(this, G) && (C(this, G, !1), ie.onMsg(this.code, this.message)); + p(this, X, (e = (n) => n, t = !1) => (s(this, z).call(this), _(this, q, t), Promise.resolve(e == null ? void 0 : e(s(this, D).getPrototypeOf())))); + g(this, "toJSON", () => ({ code: this.code, message: this.message, data: this.data, url: this.url, res: this.res })); + g(this, "toString", () => this.message); + p(this, z, () => { + s(this, B) && (_(this, B, !1), oe.onMsg(this.code, this.message)); }); /** * @param {(value: unknown) => unknown} [fn] + * @param {((reason: unknown) => unknown) | undefined} [onrejected] * @returns {Promise} */ - w(this, X, (e) => [0, 1].includes(this.code) ? Promise.resolve(e == null ? void 0 : e(s(this, I).getPrototypeOf())) : Promise.reject(s(this, B))); + p(this, Z, (e, t) => [0, 1].includes(this.code) ? Promise.resolve(e == null ? void 0 : e(this.data)) : t ? Promise.resolve(t(s(this, $))) : Promise.reject(s(this, $))); + /** + * @param {(value: HttpResponse) => unknown} [fn] + * @param {((reason: unknown) => unknown) | undefined} [onrejected] + * @returns {Promise} + */ + p(this, ee, (e, t) => [0, 1].includes(this.code) ? Promise.resolve(e == null ? void 0 : e(this)) : t ? Promise.resolve(t(s(this, $))) : Promise.reject(s(this, $))); /** * @param {(reason: HttpResponse) => unknown} fn * @returns {Promise} */ - w(this, Z, (e) => Promise.resolve(e(s(this, B)))); + p(this, te, (e) => Promise.resolve(e(s(this, $)))); /** * @param {(() => void) | undefined} [fn] * @returns {Promise} */ - w(this, ee, (e) => (e == null || e(), [0, 1].includes(this.code) ? Promise.resolve(s(this, I).getPrototypeOf()) : Promise.reject(s(this, B)))); - return this.code = e, this.message = t, this.data = n, this.url = i, this.res = o, C(this, B, new Proxy(this, s(this, I))); + p(this, re, (e) => (e == null || e(), [0, 1].includes(this.code) ? Promise.resolve(s(this, D).getPrototypeOf()) : Promise.reject(s(this, $)))); + return this.code = e, this.message = t, this.data = n, this.url = i, this.res = c, _(this, $, new Proxy(this, s(this, D))); } -} -B = new WeakMap(), L = new WeakMap(), G = new WeakMap(), I = new WeakMap(), Q = new WeakMap(), W = new WeakMap(), q = new WeakMap(), X = new WeakMap(), Z = new WeakMap(), ee = new WeakMap(); -const ne = (r, e, t, n = "/", i = "") => new se(r, e, t, n, i); -var ue = [ + static [Symbol.hasInstance](e) { + return (e == null ? void 0 : e.constructor) === ae || (e == null ? void 0 : e[Symbol.for("HttpResponse")]) === !0; + } +}; +$ = new WeakMap(), q = new WeakMap(), B = new WeakMap(), D = new WeakMap(), W = new WeakMap(), X = new WeakMap(), z = new WeakMap(), Z = new WeakMap(), ee = new WeakMap(), te = new WeakMap(), re = new WeakMap(); +let L = ae; +const ie = (r, e, t, n = "/", i = "") => new L(r, e, t, n, i); +var de = [ "utf8", "utf-8", "unicode-1-1-utf-8" ]; -function ve(r) { - if (ue.indexOf(r) < 0 && typeof r < "u" && r != null) +function ye(r) { + if (de.indexOf(r) < 0 && typeof r < "u" && r != null) throw new RangeError("Invalid encoding type. Only utf-8 is supported"); this.encoding = "utf-8", this.encode = function(e) { if (typeof e != "string") throw new TypeError("passed argument must be of tye string"); var t = unescape(encodeURIComponent(e)), n = new Uint8Array(t.length); const i = t.split(""); - for (let o = 0; o < i.length; o++) - n[o] = i[o].charCodeAt(0); + for (let c = 0; c < i.length; c++) + n[c] = i[c].charCodeAt(0); return n; }; } -function we(r) { - if (ue.indexOf(r) < 0 && typeof r < "u" && r != null) +function be(r) { + if (de.indexOf(r) < 0 && typeof r < "u" && r != null) throw new RangeError("Invalid encoding type. Only utf-8 is supported"); this.encoding = "utf-8", this.decode = function(e, t) { if (typeof e > "u") @@ -350,20 +367,20 @@ function we(r) { if (typeof n != "boolean") throw new TypeError("stream option must be boolean"); if (ArrayBuffer.isView(e)) { - var i = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), o = new Array(i.length); - for (let l = 0; l < i.length; l++) - o[l] = String.fromCharCode(i[l]); - return decodeURIComponent(escape(o.join(""))); + var i = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), c = new Array(i.length); + for (let u = 0; u < i.length; u++) + c[u] = String.fromCharCode(i[u]); + return decodeURIComponent(escape(c.join(""))); } else throw new TypeError("passed argument must be an array buffer view"); }; } -var fe = { - TextEncoder: ve, - TextDecoder: we +var ge = { + TextEncoder: ye, + TextDecoder: be }; -const ye = new fe.TextEncoder("utf8"), be = new fe.TextDecoder("utf8"); -function Ee(r) { +const Ee = new ge.TextEncoder("utf8"), Pe = new ge.TextDecoder("utf8"); +function Te(r) { let e = 5381; for (let t = 0; t < r.length; t++) e = e * 33 ^ r.charCodeAt(t); @@ -372,14 +389,14 @@ function Ee(r) { function Oe(r, e) { return r.replace(new RegExp(`^${e}+|${e}+$`, "g"), ""); } -function Te(r) { - return ye.encode(r || ""); +function Se(r) { + return Ee.encode(r || ""); } -function Me(r) { - return be.decode(new DataView(r), {}); +function Ne(r) { + return Pe.decode(new DataView(r), {}); } -const Pe = 62, Y = 30, le = 31, Se = 63; -class xe { +const xe = 62, Q = 30, ue = 31, me = 63; +class Ae { /** * @param {string} alphabet 62 位编码字母表 */ @@ -388,7 +405,7 @@ class xe { g(this, "encodeTable", []); /** @type {Uint8Array} */ g(this, "decodeMap", new Uint8Array(256).fill(255)); - if (e.length !== Pe) + if (e.length !== xe) throw new Error("Encoding alphabet must be 62 characters long"); if (e.includes(` `) || e.includes("\r")) @@ -414,55 +431,55 @@ class xe { */ decode(e) { if (!e || e.length === 0) return ""; - const t = new Ae(this).decode(e); + const t = new Ce(this).decode(e); return new TextDecoder().decode(t); } } -var z; +var V; class _e { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ - w(this, z); + p(this, V); /** @type {Uint8Array} */ g(this, "src", new Uint8Array()); /** @type {number} */ g(this, "pos", 0); - C(this, z, e); + _(this, V, e); } /** @returns {number} */ get6bits() { let e = this.pos & 7, t = this.pos >> 3; e === 0 && (t -= 1, e = 8); let n = this.src[t] >> 8 - e; - return e < 6 && t > 0 && (n |= this.src[t - 1] << e), n & Se; + return e < 6 && t > 0 && (n |= this.src[t - 1] << e), n & me; } /** * @param {string} src * @returns {number[]} */ encodeV2(e) { - this.src = Te(e), this.pos = this.src.length * 8; + this.src = Se(e), this.pos = this.src.length * 8; const t = []; for (; this.pos > 0; ) { let n = 6, i = this.get6bits(); - (i & Y) === Y && ((this.pos > 6 || i > le) && (n = 5), i &= le), t.push(s(this, z).encodeTable[i].charCodeAt(0)), this.pos -= n; + (i & Q) === Q && ((this.pos > 6 || i > ue) && (n = 5), i &= ue), t.push(s(this, V).encodeTable[i].charCodeAt(0)), this.pos -= n; } return t; } } -z = new WeakMap(); -var V; -class Ae { +V = new WeakMap(); +var k; +class Ce { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ - w(this, V); - C(this, V, e); + p(this, k); + _(this, k, e); } /** * @param {string} src @@ -470,25 +487,25 @@ class Ae { */ decode(e) { const t = new Uint8Array(Math.ceil(e.length * 6 / 8) + 1); - let n = t.length, i = 0, o = 0; - for (let l = 0; l < e.length; l++) { - const R = e[l].charCodeAt(0), c = s(this, V).decodeMap[R]; - if (c === 255) - throw new Error(`CorruptInputError at index ${l}`); - l === e.length - 1 ? (o |= c << i, i += Math.ceil(Math.log2(c + 1))) : (c & Y) === Y ? (o |= c << i, i += 5) : (o |= c << i, i += 6), i >= 8 && (n--, t[n] = o & 255, i %= 8, o >>= 8); + let n = t.length, i = 0, c = 0; + for (let u = 0; u < e.length; u++) { + const U = e[u].charCodeAt(0), a = s(this, k).decodeMap[U]; + if (a === 255) + throw new Error(`CorruptInputError at index ${u}`); + u === e.length - 1 ? (c |= a << i, i += Math.ceil(Math.log2(a + 1))) : (a & Q) === Q ? (c |= a << i, i += 5) : (c |= a << i, i += 6), i >= 8 && (n--, t[n] = c & 255, i %= 8, c >>= 8); } - return i > 0 && (n--, t[n] = o & 255), t.slice(n); + return i > 0 && (n--, t[n] = c & 255), t.slice(n); } } -V = new WeakMap(); +k = new WeakMap(); Promise.withResolvers || (Promise.withResolvers = function() { let r, e; return { promise: new Promise((n, i) => { r = n, e = i; }), resolve: r, reject: e }; }); -var J, D, te, $, F, K, re, k; -class ie { +var G, j, ne, R, F, K, se, Y; +class oe { /** * @param {string} [appKey] * @param {string} [appSecret] @@ -496,21 +513,21 @@ class ie { */ constructor(e, t, n) { /** @type {RequestFn | undefined} */ - w(this, J); + p(this, G); /** @type {Base62 | undefined} */ - w(this, D); + p(this, j); /** @type {string | undefined} */ - w(this, te); - w(this, $, { + p(this, ne); + p(this, R, { /** @type {Map} */ "#": /* @__PURE__ */ new Map(), /** @type {Map} */ $: /* @__PURE__ */ new Map() }); /** @type {Map>} */ - w(this, F, /* @__PURE__ */ new Map()); + p(this, F, /* @__PURE__ */ new Map()); /** @type {Set<[string, '#' | '$', RequestOptions & { method: string }, boolean]>} */ - w(this, K, /* @__PURE__ */ new Set()); + p(this, K, /* @__PURE__ */ new Set()); /** * 初始化请求实例 * @param {string} [appKey] @@ -518,10 +535,10 @@ class ie { * @param {RequestFn} [reqFn] */ g(this, "init", (e, t, n) => { - e && C(this, te, e), n && C(this, J, n), t && C(this, D, new xe(t)), s(this, re).call(this); + e && _(this, ne, e), n && _(this, G, n), t && _(this, j, new Ae(t)), s(this, se).call(this); }); - w(this, re, () => { - !s(this, K).size || !s(this, J) || (s(this, K).forEach((e) => s(this, k).call(this, ...e)), s(this, K).clear()); + p(this, se, () => { + !s(this, K).size || !s(this, G) || (s(this, K).forEach((e) => s(this, Y).call(this, ...e)), s(this, K).clear()); }); /** * @param {string} hash @@ -529,32 +546,32 @@ class ie { * @param {RequestOptions & { method: string }} params * @param {boolean} [isCacheRequest] */ - w(this, k, (e, t, n, i = !1) => { - if (!s(this, J)) + p(this, Y, (e, t, n, i = !1) => { + if (!s(this, G)) s(this, K).add([e, t, n, i]); else { - const { url: o, method: l, ...R } = n || {}; - s(this, J).call(this, { url: o, method: l, ...R }).then((c) => { - if (![0, 1].includes(c.code)) - throw ne(c.code, c.msg, c.data, o, c == null ? void 0 : c.res); - return [c.code, c.msg, c.data, o, (c == null ? void 0 : c.res) ?? ""]; - }).then((c) => { + const { url: c, method: u, ...U } = n || {}; + s(this, G).call(this, { url: c, method: u, ...U }).then((a) => { + if (![0, 1].includes(a.code)) + throw ie(a.code ?? -1, a.msg ?? "响应格式错误", a.data, c, a == null ? void 0 : a.res); + return [a.code, a.msg, a.data, c, (a == null ? void 0 : a.res) ?? ""]; + }).then((a) => { const T = ( /** @type {CacheEntry} */ - c + a ); - return (t == "#" || l == "GET") && s(this, $)[t].set(e, T), t != "#" && l == "GET" && !i && setTimeout(() => { - s(this, $)[t].delete(e); + return (t == "#" || u == "GET") && s(this, R)[t].set(e, T), t != "#" && u == "GET" && !i && setTimeout(() => { + s(this, R)[t].delete(e); }, 300), T; - }).then((c) => { + }).then((a) => { var T; - return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((M, _, U) => { - M && M[0](c), delete U[_]; + return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((C, m, I) => { + C && C[0](a), delete I[m]; }); - }).catch((c) => { + }).catch((a) => { var T; - s(this, $)[t].delete(e), (T = s(this, F).get(e)) == null || T.forEach((M, _, U) => { - M && M[1](c instanceof se ? c : ne(-1, c, null, o)), delete U[_]; + s(this, R)[t].delete(e), (T = s(this, F).get(e)) == null || T.forEach((C, m, I) => { + C && C[1](a instanceof L ? a : ie(-1, a, null, c)), delete I[m]; }); }).finally(() => s(this, F).delete(e)); } @@ -566,31 +583,32 @@ class ie { * @returns {any} 返回 Proxy 包裹的 Promise,支持链式调用 .then / .catch / .msg / .resp */ g(this, "request", ({ url: e = "/", method: t = "GET", ...n }, i = !1) => { - var _, U; + var m, I; e = Oe(e, "/"); - const o = ( + const c = ( /** @type {HttpMethod} */ t.toUpperCase() - ), l = Ee(JSON.stringify([e, o, n])), R = ( + ), u = Te(JSON.stringify([e, c, n])), U = ( /** @type {'#' | '$'} */ - (_ = e == null ? void 0 : e.includes) != null && _.call(e, "/_/") ? "#" : "$" - ), { promise: c, resolve: T, reject: M } = Promise.withResolvers(); - return s(this, $)[R].has(l) ? (console.log("cache", e), T(s(this, $)[R].get(l))) : s(this, F).has(l) ? (U = s(this, F).get(l)) == null || U.push([T, M]) : (s(this, F).set(l, [[T, M]]), s(this, k).call(this, l, R, { url: e, method: o, ...n }, i)), new Proxy(c, { - get: (H, u) => (...f) => H.then((h) => ( + (m = e == null ? void 0 : e.includes) != null && m.call(e, "/_/") ? "#" : "$" + ), { promise: a, resolve: T, reject: C } = Promise.withResolvers(); + return s(this, R)[U].has(u) ? (console.log("cache", e), T(s(this, R)[U].get(u))) : s(this, F).has(u) ? (I = s(this, F).get(u)) == null || I.push([T, C]) : (s(this, F).set(u, [[T, C]]), s(this, Y).call(this, u, U, { url: e, method: c, ...n }, i)), new Proxy(a, { + // Symbol.for('HttpResponse') 是标记,用于外部 instanceof 识别此 Proxy 为 HttpResponse 类型 + get: (H, h) => h === Symbol.for("HttpResponse") ? !0 : (...f) => H.then((l) => ( /** @type {any} */ - ne( + ie( .../** @type {[number, string, unknown, string, string]} */ - h - )[u](...f) - )).catch((h) => { - if (!(h instanceof se)) throw h; + l + )[h](...f) + )).catch((l) => { + if (!(l instanceof L)) throw l; return typeof /** @type {any} */ - h[u] == "function" ? ( + l[h] == "function" ? ( /** @type {any} */ - h[u](...f) + l[h](...f) ) : ( /** @type {any} */ - h[u] + l[h] ); }) }); @@ -651,7 +669,7 @@ class ie { * @param {boolean} [isSystem] 是否同时清除系统缓存(`/_/` 路由) */ g(this, "refreshCache", (e = !1) => { - e && s(this, $)["#"].clear(), s(this, $).$.clear(); + e && s(this, R)["#"].clear(), s(this, R).$.clear(); }); /** * 解码 Base62 字符串为 JSON 对象 @@ -663,9 +681,9 @@ class ie { if (e === void 0 || e === "" || e === null) return t; try { - if (!s(this, D)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); - const n = s(this, D).decode(e); - return ce.unpack(n); + if (!s(this, j)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); + const n = s(this, j).decode(e); + return le.unpack(n); } catch { return console.warn("core.decode", e), t; } @@ -676,25 +694,26 @@ class ie { * @returns {string} */ g(this, "encode", (e = {}) => { - if (!s(this, D)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); + if (!s(this, j)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); const t = JSON.stringify(e, (n, i) => i === void 0 ? null : i); - return s(this, D).encode(ce.pack(t)); + return s(this, j).encode(le.pack(t)); }); this.init(e, t, n); } } -J = new WeakMap(), D = new WeakMap(), te = new WeakMap(), $ = new WeakMap(), F = new WeakMap(), K = new WeakMap(), re = new WeakMap(), k = new WeakMap(); -ie.onUnhandledRejection = (r) => { - var e, t, n, i, o, l; - r != null && r.IS_ECHO_MSG ? ((e = r == null ? void 0 : r.$echoMsg) == null || e.call(r), (t = r == null ? void 0 : r.preventDefault) == null || t.call(r)) : (n = r.reason) != null && n.IS_ECHO_MSG && ((o = (i = r.reason) == null ? void 0 : i.$echoMsg) == null || o.call(i), (l = r == null ? void 0 : r.preventDefault) == null || l.call(r)); +G = new WeakMap(), j = new WeakMap(), ne = new WeakMap(), R = new WeakMap(), F = new WeakMap(), K = new WeakMap(), se = new WeakMap(), Y = new WeakMap(); +oe.onUnhandledRejection = (r) => { + var t; + const e = r instanceof L ? r : (r == null ? void 0 : r.reason) instanceof L ? r.reason : null; + e != null && e.IS_ECHO_MSG && (e.$echoMsg(), (t = r == null ? void 0 : r.preventDefault) == null || t.call(r)); }; -ie.onMsg = (r, e) => [0, 1].includes(r) ? console.log(e) : console.warn(e); +oe.onMsg = (r, e) => [0, 1].includes(r) ? console.log(e) : console.warn(e); export { - se as HttpResponse, - ne as NewHttpResponse, - ie as default, - Ee as signature, - Te as str2uint8array, + L as HttpResponse, + ie as NewHttpResponse, + oe as default, + Te as signature, + Se as str2uint8array, Oe as trim, - Me as uint8array2str + Ne as uint8array2str }; diff --git a/ff-request.umd.cjs b/ff-request.umd.cjs index af51181..42d1351 100644 --- a/ff-request.umd.cjs +++ b/ff-request.umd.cjs @@ -1,2 +1,2 @@ -(function(o,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(o=typeof globalThis<"u"?globalThis:o||self,h(o["ff-request"]={}))})(this,function(o){"use strict";var Ce=Object.defineProperty;var ve=o=>{throw TypeError(o)};var Me=(o,h,O)=>h in o?Ce(o,h,{enumerable:!0,configurable:!0,writable:!0,value:O}):o[h]=O;var v=(o,h,O)=>Me(o,typeof h!="symbol"?h+"":h,O),we=(o,h,O)=>h.has(o)||ve("Cannot "+O);var s=(o,h,O)=>(we(o,h,"read from private field"),O?O.call(o):h.get(o)),b=(o,h,O)=>h.has(o)?ve("Cannot add the same private member more than once"):h instanceof WeakSet?h.add(o):h.set(o,O),N=(o,h,O,V)=>(we(o,h,"write to private field"),V?V.call(o,O):h.set(o,O),O);var J,z,H,j,ne,se,k,ie,oe,ae,Y,Q,K,G,ce,R,F,L,ue,W;function h(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var O={};(function(r){(function(e){e([],function(){var t=-1,n=-2,i=-3,a=-4,f=-5,D=function(d,g){g=g||{};var l=g.verbose||!1;l&&console.log("Normalize the JSON Object"),d=typeof d=="string"?this.JSON.parse(d):d,l&&console.log("Creating a empty dictionary");var p={strings:[],integers:[],floats:[]};l&&console.log("Creating the AST");var w=function _(c){l&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(c));var E=typeof c;if(c===null)return{type:"null",index:i};if(typeof c>"u")return{type:"undefined",index:f};if(c instanceof Array){var A=["@"];for(var T in c)c.hasOwnProperty(T)&&A.push(_(c[T]));return A}if(E==="object"){var A=["$"];for(var I in c)c.hasOwnProperty(I)&&(A.push(_(I)),A.push(_(c[I])));return A}if(c==="")return{type:"empty",index:a};if(E==="string"){var S=X.call(p.strings,c);return S==-1&&(p.strings.push(x(c)),S=p.strings.length-1),{type:"strings",index:S}}if(E==="number"&&c%1===0){var S=X.call(p.integers,c);return S==-1&&(p.integers.push(M(c)),S=p.integers.length-1),{type:"integers",index:S}}if(E==="number"){var S=X.call(p.floats,c);return S==-1&&(p.floats.push(c),S=p.floats.length-1),{type:"floats",index:S}}if(E==="boolean")return{type:"boolean",index:c?t:n};throw new Error("Unexpected argument of type "+typeof c)}(d),P=p.strings.length,q=p.integers.length;p.floats.length,l&&console.log("Parsing the dictionary");var U=p.strings.join("|");return U+="^"+p.integers.join("|"),U+="^"+p.floats.join("|"),l&&console.log("Parsing the structure"),U+="^"+function _(c){if(l&&console.log("Calling a recursiveParser with "+this.JSON.stringify(c)),c instanceof Array){var E=c.shift();for(var A in c)c.hasOwnProperty(A)&&(E+=_(c[A])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var T=c.type,I=c.index;if(T==="strings")return M(I);if(T==="integers")return M(P+I);if(T==="floats")return M(P+q+I);if(T==="boolean")return c.index;if(T==="null")return i;if(T==="undefined")return f;if(T==="empty")return a;throw new TypeError("The item is alien!")}(w),l&&console.log("Ending parser"),g.debug?{dictionary:p,ast:w,packed:U}:U},u=function(d,g){g=g||{};var l=d.split("^");g.verbose&&console.log("Building dictionary");var p=[],w=l[0];if(w!==""){w=w.split("|"),g.verbose&&console.log("Parse the strings dictionary");for(var P=0,q=w.length;Ps(this,ne).call(this,t),ownKeys:()=>Object.keys(s(this,j).getPrototypeOf()||{}),getPrototypeOf:()=>s(this,z)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:s(this,j).getPrototypeOf()})});b(this,ne,e=>e==="$echoMsg"?s(this,k):e==="msg"?s(this,se):e==="then"||e==="resp"?(N(this,H,!0),N(this,z,e==="resp"),s(this,ie)):e==="catch"?s(this,oe):e==="finally"?s(this,ae):e==="IS_ECHO_MSG"?s(this,H):Reflect.get(this,e));b(this,se,(e=n=>n,t=!1)=>(s(this,k).call(this),N(this,z,t),Promise.resolve(e==null?void 0:e(s(this,j).getPrototypeOf()))));b(this,k,()=>{s(this,H)&&(N(this,H,!1),re.onMsg(this.code,this.message))});b(this,ie,e=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(s(this,j).getPrototypeOf())):Promise.reject(s(this,J)));b(this,oe,e=>Promise.resolve(e(s(this,J))));b(this,ae,e=>(e==null||e(),[0,1].includes(this.code)?Promise.resolve(s(this,j).getPrototypeOf()):Promise.reject(s(this,J))));return this.code=e,this.message=t,this.data=n,this.url=i,this.res=a,N(this,J,new Proxy(this,s(this,j)))}}J=new WeakMap,z=new WeakMap,H=new WeakMap,j=new WeakMap,ne=new WeakMap,se=new WeakMap,k=new WeakMap,ie=new WeakMap,oe=new WeakMap,ae=new WeakMap;const ee=(r,e,t,n="/",i="")=>new Z(r,e,t,n,i);var fe=["utf8","utf-8","unicode-1-1-utf-8"];function ye(r){if(fe.indexOf(r)<0&&typeof r<"u"&&r!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var t=unescape(encodeURIComponent(e)),n=new Uint8Array(t.length);const i=t.split("");for(let a=0;a"u")return"";var n=typeof t<"u"&&n in t?t.stream:!1;if(typeof n!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var i=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),a=new Array(i.length);for(let f=0;f>>0).toString(16)}function de(r,e){return r.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ge(r){return Ee.encode(r||"")}function Oe(r){return Te.decode(new DataView(r),{})}const Pe=62,te=30,pe=31,Se=63;class xe{constructor(e){v(this,"encodeTable",[]);v(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==Pe)throw new Error("Encoding alphabet must be 62 characters long");if(e.includes(` -`)||e.includes("\r"))throw new Error("Encoding alphabet contains newline character");this.encodeTable=e.split(""),this.decodeMap=new Uint8Array(256).fill(255);for(let t=0;t>3;e===0&&(t-=1,e=8);let n=this.src[t]>>8-e;return e<6&&t>0&&(n|=this.src[t-1]<0;){let n=6,i=this.get6bits();(i&te)===te&&((this.pos>6||i>pe)&&(n=5),i&=pe),t.push(s(this,Y).encodeTable[i].charCodeAt(0)),this.pos-=n}return t}}Y=new WeakMap;class Ae{constructor(e){b(this,Q);N(this,Q,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let n=t.length,i=0,a=0;for(let f=0;f=8&&(n--,t[n]=a&255,i%=8,a>>=8)}return i>0&&(n--,t[n]=a&255),t.slice(n)}}Q=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let r,e;return{promise:new Promise((n,i)=>{r=n,e=i}),resolve:r,reject:e}});class re{constructor(e,t,n){b(this,K);b(this,G);b(this,ce);b(this,R,{"#":new Map,$:new Map});b(this,F,new Map);b(this,L,new Set);v(this,"init",(e,t,n)=>{e&&N(this,ce,e),n&&N(this,K,n),t&&N(this,G,new xe(t)),s(this,ue).call(this)});b(this,ue,()=>{!s(this,L).size||!s(this,K)||(s(this,L).forEach(e=>s(this,W).call(this,...e)),s(this,L).clear())});b(this,W,(e,t,n,i=!1)=>{if(!s(this,K))s(this,L).add([e,t,n,i]);else{const{url:a,method:f,...D}=n||{};s(this,K).call(this,{url:a,method:f,...D}).then(u=>{if(![0,1].includes(u.code))throw ee(u.code,u.msg,u.data,a,u==null?void 0:u.res);return[u.code,u.msg,u.data,a,(u==null?void 0:u.res)??""]}).then(u=>{const x=u;return(t=="#"||f=="GET")&&s(this,R)[t].set(e,x),t!="#"&&f=="GET"&&!i&&setTimeout(()=>{s(this,R)[t].delete(e)},300),x}).then(u=>{var x;return(x=s(this,F).get(e))==null?void 0:x.forEach(($,M,B)=>{$&&$[0](u),delete B[M]})}).catch(u=>{var x;s(this,R)[t].delete(e),(x=s(this,F).get(e))==null||x.forEach(($,M,B)=>{$&&$[1](u instanceof Z?u:ee(-1,u,null,a)),delete B[M]})}).finally(()=>s(this,F).delete(e))}});v(this,"request",({url:e="/",method:t="GET",...n},i=!1)=>{var M,B;e=de(e,"/");const a=t.toUpperCase(),f=he(JSON.stringify([e,a,n])),D=(M=e==null?void 0:e.includes)!=null&&M.call(e,"/_/")?"#":"$",{promise:u,resolve:x,reject:$}=Promise.withResolvers();return s(this,R)[D].has(f)?(console.log("cache",e),x(s(this,R)[D].get(f))):s(this,F).has(f)?(B=s(this,F).get(f))==null||B.push([x,$]):(s(this,F).set(f,[[x,$]]),s(this,W).call(this,f,D,{url:e,method:a,...n},i)),new Proxy(u,{get:(X,d)=>(...g)=>X.then(l=>ee(...l)[d](...g)).catch(l=>{if(!(l instanceof Z))throw l;return typeof l[d]=="function"?l[d](...g):l[d]})})});v(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));v(this,"post",(e,t={},n={})=>this.request({url:e,method:"POST",data:t,params:n}));v(this,"put",(e,t={},n={})=>this.request({url:e,method:"PUT",data:t,params:n}));v(this,"del",(e,t={},n={})=>this.request({url:e,method:"DELETE",data:t,params:n}));v(this,"download",(e,t)=>{});v(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));v(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));v(this,"getAppInfo",()=>{});v(this,"refreshCache",(e=!1)=>{e&&s(this,R)["#"].clear(),s(this,R).$.clear()});v(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{if(!s(this,G))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const n=s(this,G).decode(e);return V.unpack(n)}catch{return console.warn("core.decode",e),t}});v(this,"encode",(e={})=>{if(!s(this,G))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const t=JSON.stringify(e,(n,i)=>i===void 0?null:i);return s(this,G).encode(V.pack(t))});this.init(e,t,n)}}K=new WeakMap,G=new WeakMap,ce=new WeakMap,R=new WeakMap,F=new WeakMap,L=new WeakMap,ue=new WeakMap,W=new WeakMap,re.onUnhandledRejection=r=>{var e,t,n,i,a,f;r!=null&&r.IS_ECHO_MSG?((e=r==null?void 0:r.$echoMsg)==null||e.call(r),(t=r==null?void 0:r.preventDefault)==null||t.call(r)):(n=r.reason)!=null&&n.IS_ECHO_MSG&&((a=(i=r.reason)==null?void 0:i.$echoMsg)==null||a.call(i),(f=r==null?void 0:r.preventDefault)==null||f.call(r))},re.onMsg=(r,e)=>[0,1].includes(r)?console.log(e):console.warn(e),o.HttpResponse=Z,o.NewHttpResponse=ee,o.default=re,o.signature=he,o.str2uint8array=ge,o.trim=de,o.uint8array2str=Oe,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); +(function(o,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(o=typeof globalThis<"u"?globalThis:o||self,f(o["ff-request"]={}))})(this,function(o){"use strict";var Me=Object.defineProperty;var we=o=>{throw TypeError(o)};var Ne=(o,f,P)=>f in o?Me(o,f,{enumerable:!0,configurable:!0,writable:!0,value:P}):o[f]=P;var v=(o,f,P)=>Ne(o,typeof f!="symbol"?f+"":f,P),be=(o,f,P)=>f.has(o)||we("Cannot "+P);var n=(o,f,P)=>(be(o,f,"read from private field"),P?P.call(o):f.get(o)),y=(o,f,P)=>f.has(o)?we("Cannot add the same private member more than once"):f instanceof WeakSet?f.add(o):f.set(o,P),N=(o,f,P,V)=>(be(o,f,"write to private field"),V?V.call(o,P):f.set(o,P),P);var R,k,G,H,ne,se,Y,ie,oe,ae,ce,Q,W,L,K,ue,$,j,q,le,X;function f(s){return s&&s.__esModule&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}var P={};(function(s){(function(e){e([],function(){var t=-1,r=-2,i=-3,u=-4,d=-5,B=function(l,g){g=g||{};var h=g.verbose||!1;h&&console.log("Normalize the JSON Object"),l=typeof l=="string"?this.JSON.parse(l):l,h&&console.log("Creating a empty dictionary");var p={strings:[],integers:[],floats:[]};h&&console.log("Creating the AST");var w=function x(a){h&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(a));var E=typeof a;if(a===null)return{type:"null",index:i};if(typeof a>"u")return{type:"undefined",index:d};if(a instanceof Array){var _=["@"];for(var T in a)a.hasOwnProperty(T)&&_.push(x(a[T]));return _}if(E==="object"){var _=["$"];for(var F in a)a.hasOwnProperty(F)&&(_.push(x(F)),_.push(x(a[F])));return _}if(a==="")return{type:"empty",index:u};if(E==="string"){var S=Z.call(p.strings,a);return S==-1&&(p.strings.push(m(a)),S=p.strings.length-1),{type:"strings",index:S}}if(E==="number"&&a%1===0){var S=Z.call(p.integers,a);return S==-1&&(p.integers.push(C(a)),S=p.integers.length-1),{type:"integers",index:S}}if(E==="number"){var S=Z.call(p.floats,a);return S==-1&&(p.floats.push(a),S=p.floats.length-1),{type:"floats",index:S}}if(E==="boolean")return{type:"boolean",index:a?t:r};throw new Error("Unexpected argument of type "+typeof a)}(l),O=p.strings.length,z=p.integers.length;p.floats.length,h&&console.log("Parsing the dictionary");var I=p.strings.join("|");return I+="^"+p.integers.join("|"),I+="^"+p.floats.join("|"),h&&console.log("Parsing the structure"),I+="^"+function x(a){if(h&&console.log("Calling a recursiveParser with "+this.JSON.stringify(a)),a instanceof Array){var E=a.shift();for(var _ in a)a.hasOwnProperty(_)&&(E+=x(a[_])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var T=a.type,F=a.index;if(T==="strings")return C(F);if(T==="integers")return C(O+F);if(T==="floats")return C(O+z+F);if(T==="boolean")return a.index;if(T==="null")return i;if(T==="undefined")return d;if(T==="empty")return u;throw new TypeError("The item is alien!")}(w),h&&console.log("Ending parser"),g.debug?{dictionary:p,ast:w,packed:I}:I},c=function(l,g){g=g||{};var h=l.split("^");g.verbose&&console.log("Building dictionary");var p=[],w=h[0];if(w!==""){w=w.split("|"),g.verbose&&console.log("Parse the strings dictionary");for(var O=0,z=w.length;On(this,ne).call(this,t),set:(e,t)=>{throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${t}"`)},ownKeys:()=>Object.keys(n(this,H).getPrototypeOf()||{}),getPrototypeOf:()=>n(this,k)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:n(this,H).getPrototypeOf()})});y(this,ne,e=>e==="$echoMsg"?n(this,Y):e==="msg"?n(this,se):e==="then"?(N(this,G,!0),n(this,ie)):e==="resp"?(N(this,G,!0),n(this,oe)):e==="catch"?n(this,ae):e==="finally"?n(this,ce):e==="IS_ECHO_MSG"?n(this,G):Reflect.get(this,e));y(this,se,(e=r=>r,t=!1)=>(n(this,Y).call(this),N(this,k,t),Promise.resolve(e==null?void 0:e(n(this,H).getPrototypeOf()))));v(this,"toJSON",()=>({code:this.code,message:this.message,data:this.data,url:this.url,res:this.res}));v(this,"toString",()=>this.message);y(this,Y,()=>{n(this,G)&&(N(this,G,!1),re.onMsg(this.code,this.message))});y(this,ie,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this.data)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,oe,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,ae,e=>Promise.resolve(e(n(this,R))));y(this,ce,e=>(e==null||e(),[0,1].includes(this.code)?Promise.resolve(n(this,H).getPrototypeOf()):Promise.reject(n(this,R))));return this.code=e,this.message=t,this.data=r,this.url=i,this.res=u,N(this,R,new Proxy(this,n(this,H)))}static[Symbol.hasInstance](e){return(e==null?void 0:e.constructor)===he||(e==null?void 0:e[Symbol.for("HttpResponse")])===!0}};R=new WeakMap,k=new WeakMap,G=new WeakMap,H=new WeakMap,ne=new WeakMap,se=new WeakMap,Y=new WeakMap,ie=new WeakMap,oe=new WeakMap,ae=new WeakMap,ce=new WeakMap;let J=he;const ee=(s,e,t,r="/",i="")=>new J(s,e,t,r,i);var fe=["utf8","utf-8","unicode-1-1-utf-8"];function Ee(s){if(fe.indexOf(s)<0&&typeof s<"u"&&s!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);const i=t.split("");for(let u=0;u"u")return"";var r=typeof t<"u"&&r in t?t.stream:!1;if(typeof r!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var i=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),u=new Array(i.length);for(let d=0;d>>0).toString(16)}function pe(s,e){return s.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ve(s){return Pe.encode(s||"")}function Se(s){return Oe.decode(new DataView(s),{})}const me=62,te=30,ye=31,xe=63;class _e{constructor(e){v(this,"encodeTable",[]);v(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==me)throw new Error("Encoding alphabet must be 62 characters long");if(e.includes(` +`)||e.includes("\r"))throw new Error("Encoding alphabet contains newline character");this.encodeTable=e.split(""),this.decodeMap=new Uint8Array(256).fill(255);for(let t=0;t>3;e===0&&(t-=1,e=8);let r=this.src[t]>>8-e;return e<6&&t>0&&(r|=this.src[t-1]<0;){let r=6,i=this.get6bits();(i&te)===te&&((this.pos>6||i>ye)&&(r=5),i&=ye),t.push(n(this,Q).encodeTable[i].charCodeAt(0)),this.pos-=r}return t}}Q=new WeakMap;class Ce{constructor(e){y(this,W);N(this,W,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let r=t.length,i=0,u=0;for(let d=0;d=8&&(r--,t[r]=u&255,i%=8,u>>=8)}return i>0&&(r--,t[r]=u&255),t.slice(r)}}W=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let s,e;return{promise:new Promise((r,i)=>{s=r,e=i}),resolve:s,reject:e}});class re{constructor(e,t,r){y(this,L);y(this,K);y(this,ue);y(this,$,{"#":new Map,$:new Map});y(this,j,new Map);y(this,q,new Set);v(this,"init",(e,t,r)=>{e&&N(this,ue,e),r&&N(this,L,r),t&&N(this,K,new _e(t)),n(this,le).call(this)});y(this,le,()=>{!n(this,q).size||!n(this,L)||(n(this,q).forEach(e=>n(this,X).call(this,...e)),n(this,q).clear())});y(this,X,(e,t,r,i=!1)=>{if(!n(this,L))n(this,q).add([e,t,r,i]);else{const{url:u,method:d,...B}=r||{};n(this,L).call(this,{url:u,method:d,...B}).then(c=>{if(![0,1].includes(c.code))throw ee(c.code??-1,c.msg??"响应格式错误",c.data,u,c==null?void 0:c.res);return[c.code,c.msg,c.data,u,(c==null?void 0:c.res)??""]}).then(c=>{const m=c;return(t=="#"||d=="GET")&&n(this,$)[t].set(e,m),t!="#"&&d=="GET"&&!i&&setTimeout(()=>{n(this,$)[t].delete(e)},300),m}).then(c=>{var m;return(m=n(this,j).get(e))==null?void 0:m.forEach((U,C,D)=>{U&&U[0](c),delete D[C]})}).catch(c=>{var m;n(this,$)[t].delete(e),(m=n(this,j).get(e))==null||m.forEach((U,C,D)=>{U&&U[1](c instanceof J?c:ee(-1,c,null,u)),delete D[C]})}).finally(()=>n(this,j).delete(e))}});v(this,"request",({url:e="/",method:t="GET",...r},i=!1)=>{var C,D;e=pe(e,"/");const u=t.toUpperCase(),d=ge(JSON.stringify([e,u,r])),B=(C=e==null?void 0:e.includes)!=null&&C.call(e,"/_/")?"#":"$",{promise:c,resolve:m,reject:U}=Promise.withResolvers();return n(this,$)[B].has(d)?(console.log("cache",e),m(n(this,$)[B].get(d))):n(this,j).has(d)?(D=n(this,j).get(d))==null||D.push([m,U]):(n(this,j).set(d,[[m,U]]),n(this,X).call(this,d,B,{url:e,method:u,...r},i)),new Proxy(c,{get:(Z,l)=>l===Symbol.for("HttpResponse")?!0:(...g)=>Z.then(h=>ee(...h)[l](...g)).catch(h=>{if(!(h instanceof J))throw h;return typeof h[l]=="function"?h[l](...g):h[l]})})});v(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));v(this,"post",(e,t={},r={})=>this.request({url:e,method:"POST",data:t,params:r}));v(this,"put",(e,t={},r={})=>this.request({url:e,method:"PUT",data:t,params:r}));v(this,"del",(e,t={},r={})=>this.request({url:e,method:"DELETE",data:t,params:r}));v(this,"download",(e,t)=>{});v(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));v(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));v(this,"getAppInfo",()=>{});v(this,"refreshCache",(e=!1)=>{e&&n(this,$)["#"].clear(),n(this,$).$.clear()});v(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const r=n(this,K).decode(e);return V.unpack(r)}catch{return console.warn("core.decode",e),t}});v(this,"encode",(e={})=>{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const t=JSON.stringify(e,(r,i)=>i===void 0?null:i);return n(this,K).encode(V.pack(t))});this.init(e,t,r)}}L=new WeakMap,K=new WeakMap,ue=new WeakMap,$=new WeakMap,j=new WeakMap,q=new WeakMap,le=new WeakMap,X=new WeakMap,re.onUnhandledRejection=s=>{var t;const e=s instanceof J?s:(s==null?void 0:s.reason)instanceof J?s.reason:null;e!=null&&e.IS_ECHO_MSG&&(e.$echoMsg(),(t=s==null?void 0:s.preventDefault)==null||t.call(s))},re.onMsg=(s,e)=>[0,1].includes(s)?console.log(e):console.warn(e),o.HttpResponse=J,o.NewHttpResponse=ee,o.default=re,o.signature=ge,o.str2uint8array=ve,o.trim=pe,o.uint8array2str=Se,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); diff --git a/package.json b/package.json index 620c2ff..94238c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ff-request", - "version": "0.11.2", + "version": "0.11.3", "type": "module", "main": "./ff-request.umd.cjs", "module": "./ff-request.js", diff --git a/types/index.d.ts b/types/index.d.ts index 5e9295d..0c61c0c 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -127,6 +127,7 @@ export default HttpRequest; * @property {string} res */ export declare class HttpResponse { + static [Symbol.hasInstance](instance: any): boolean; /** * @param {number} code * @param {string} message @@ -145,6 +146,14 @@ export declare class HttpResponse { url: string; /** @type {string} */ res: string; + toJSON: () => { + code: number; + message: string; + data: unknown; + url: string; + res: string; + }; + toString: () => string; #private; }