var de = Object.defineProperty; var oe = (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 l = (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) { return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r; } var ue = {}; (function(r) { (function(e) { e([], function() { var t = -1, n = -2, i = -3, c = -4, f = -5, U = function(h, d) { d = d || {}; var u = d.verbose || !1; u && console.log("Normalize the JSON Object"), h = typeof h == "string" ? this.JSON.parse(h) : h, u && console.log("Creating a empty dictionary"); var g = { strings: [], integers: [], floats: [] }; u && console.log("Creating the AST"); var p = function O(o) { u && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(o)); var y = typeof o; if (o === null) return { type: "null", index: i }; if (typeof o > "u") return { type: "undefined", index: f }; if (o instanceof Array) { var S = ["@"]; for (var b in o) o.hasOwnProperty(b) && S.push(O(o[b])); return S; } if (y === "object") { var S = ["$"]; for (var N in o) o.hasOwnProperty(N) && (S.push(O(N)), S.push(O(o[N]))); return S; } if (o === "") return { type: "empty", index: c }; if (y === "string") { var P = H.call(g.strings, o); return P == -1 && (g.strings.push(T(o)), P = g.strings.length - 1), { type: "strings", index: P }; } if (y === "number" && o % 1 === 0) { var P = H.call(g.integers, o); return P == -1 && (g.integers.push(m(o)), P = g.integers.length - 1), { type: "integers", index: P }; } if (y === "number") { var P = H.call(g.floats, o); return P == -1 && (g.floats.push(o), P = g.floats.length - 1), { type: "floats", index: P }; } if (y === "boolean") return { type: "boolean", index: o ? t : n }; throw new Error("Unexpected argument of type " + typeof o); }(h), E = g.strings.length, J = g.integers.length; g.floats.length, u && console.log("Parsing the dictionary"); var M = g.strings.join("|"); return M += "^" + g.integers.join("|"), M += "^" + g.floats.join("|"), u && console.log("Parsing the structure"), M += "^" + function O(o) { if (u && 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 = o.type, N = o.index; if (b === "strings") return m(N); if (b === "integers") return m(E + N); if (b === "floats") return m(E + J + N); if (b === "boolean") return o.index; if (b === "null") return i; if (b === "undefined") return f; if (b === "empty") return c; throw new TypeError("The item is alien!"); }(p), u && console.log("Ending parser"), d.debug ? { dictionary: g, ast: p, packed: M } : M; }, a = function(h, d) { d = d || {}; var u = h.split("^"); d.verbose && console.log("Building dictionary"); var g = [], p = u[0]; if (p !== "") { p = p.split("|"), d.verbose && console.log("Parse the strings dictionary"); for (var E = 0, J = p.length; E < J; E++) g.push(_(p[E])); } if (p = u[1], p !== "") { p = p.split("|"), d.verbose && console.log("Parse the integers dictionary"); for (var E = 0, J = p.length; E < J; E++) g.push(I(p[E])); } if (p = u[2], p !== "") { p = p.split("|"), d.verbose && console.log("Parse the floats dictionary"); for (var E = 0, J = p.length; E < J; E++) g.push(parseFloat(p[E])); } p = null, d.verbose && console.log("Tokenizing the structure"); for (var M = "", O = [], o = u[3].length, E = 0; E < o; E++) { var y = u[3].charAt(E); y === "|" || y === "$" || y === "@" || y === "]" ? (M && (O.push(I(M)), M = ""), y !== "|" && O.push(y)) : M += y; } var S = O.length, b = 0; return d.verbose && console.log("Starting recursive parser"), function N() { var P = O[b++]; if (d.verbose && console.log("Reading collection type " + (P === "$" ? "object" : "Array")), P === "@") { for (var v = []; b < S; b++) { var A = O[b]; if (d.verbose && console.log("Read " + A + " symbol"), A === "]") return v; if (A === "@" || A === "$") v.push(N()); else switch (A) { case t: v.push(!0); break; case n: v.push(!1); break; case i: v.push(null); break; case f: v.push(void 0); break; case c: v.push(""); break; default: v.push(g[A]); } } return d.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; } if (P === "$") { for (var v = {}; b < S; b++) { var x = O[b]; if (x === "]") return v; x === c ? x = "" : x = g[x]; var A = O[++b]; if (A === "@" || A === "$") v[x] = N(); else switch (A) { case t: v[x] = !0; break; case n: v[x] = !1; break; case i: v[x] = null; break; case f: v[x] = void 0; break; case c: v[x] = ""; break; default: v[x] = g[A]; } } return d.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; } throw new TypeError("Bad token " + P + " isn't a type"); }(); }, T = function(h) { return typeof h != "string" ? h : h.replace(/[\+ \|\^\%]/g, function(d) { return { " ": "+", "+": "%2B", "|": "%7C", "^": "%5E", "%": "%25" }[d]; }); }, _ = function(h) { return typeof h != "string" ? h : h.replace(/\+|%2B|%7C|%5E|%25/g, function(d) { return { "+": " ", "%2B": "+", "%7C": "|", "%5E": "^", "%25": "%" }[d]; }); }, m = function(h) { return Number.prototype.toString.call(h, 36).toUpperCase(); }, I = function(h) { return parseInt(h, 36); }, H = Array.prototype.indexOf || function(h, d) { for (var u = d || 0, g = this.length; u < g; u++) if (this[u] === h) return u; return -1; }; return { JSON, pack: U, unpack: a }; }); })(function(e, t) { var n = t(); for (var i in n) r[i] = n[i]; }); })(ue); const ce = /* @__PURE__ */ pe(ue); var $, q, j, B, W, k, X, Z, ee; const ie = class ie { /** * @param {number} code * @param {string} message * @param {unknown} data * @param {string} url * @param {string} res */ constructor(e, t, n, i, c) { /** @type {number} */ l(this, "code", 0); /** @type {string} */ l(this, "message", "操作成功"); /** @type {unknown} */ l(this, "data", null); /** @type {string} */ l(this, "url", ""); /** @type {string} */ l(this, "res", ""); /** * Proxy 最终返回的对象 * * @type {HttpResponseProxy} */ w(this, $); /** * 判断回调使用 HttpResponse 还是 data * * @type {boolean} */ w(this, q, !0); /** * 是否已经显示消息 * * @type {boolean} */ w(this, j, !0); /** * Proxy Handler */ w(this, B, { /** * @param {any} _ * @param {string | symbol} key * @returns {unknown} */ get: (e, t) => s(this, W).call(this, t), /** * HttpResponse 是只读对象 * * @param {any} _ * @param {string | symbol} key * @param {unknown} value * @returns {boolean} */ set: (e, t, n) => { throw new TypeError( `HttpResponse 是只读的,不能设置属性 "${String(t)}"` ); }, /** * @returns {(string | symbol)[]} */ ownKeys: () => Object.keys(s(this, B).getPrototypeOf() || {}), /** * @returns {HttpResponse | unknown} */ getPrototypeOf: () => s(this, q) ? this : this.data, /** * @returns {PropertyDescriptor} */ getOwnPropertyDescriptor: () => ({ configurable: !0, enumerable: !0, writable: !0, value: s(this, B).getPrototypeOf() }) }); /** * Proxy 属性访问 * * @param {string | symbol} key * @returns {unknown} */ w(this, W, (e) => e === "$echoMsg" ? s(this, k) : e === "then" ? (C(this, j, !0), s(this, X)) : e === "catch" ? s(this, Z) : e === "finally" ? s(this, ee) : e === "IS_ECHO_MSG" ? s(this, j) : Reflect.get(this, e)); /** * message 消息弹窗后回调 * * @overload * @returns {Promise} * * @overload * @param {(data: unknown) => unknown} fn * @param {false} isResp * @returns {Promise} * * @overload * @param {(response: HttpResponse) => unknown} fn * @param {true} isResp * @returns {Promise} * * @param {(value: unknown | HttpResponse) => unknown} [fn] * @param {boolean} [isResp=false] * @returns {Promise} * * fn 内部抛出的异常会变成 reject,而不是同步抛出。 */ l(this, "msg", (e = (n) => n, t = !1) => { s(this, k).call(this), C(this, q, t); try { return Promise.resolve( e == null ? void 0 : e(s(this, B).getPrototypeOf()) ); } catch (n) { return Promise.reject(n); } }); /** * 使用完整 HttpResponse 对象处理响应 * * 与 msg() 的区别: * * msg(false) -> 回调拿 data * * msg(true) -> 回调拿 HttpResponse * * resp() -> 永远拿 HttpResponse * * @overload * @returns {Promise} * * @overload * @param {(response: HttpResponse) => unknown} fn * @returns {Promise} * * @overload * @param {undefined} fn * @param {(reason: HttpResponse) => unknown} onrejected * @returns {Promise} * * @param {(response: HttpResponse) => unknown} [fn] * @param {(reason: HttpResponse) => unknown} [onrejected] * @returns {Promise} */ l(this, "resp", (e, t) => (C(this, j, !0), [0, 1].includes(this.code) ? Promise.resolve(e == null ? void 0 : e(this)) : t ? Promise.resolve(t(s(this, $))) : Promise.reject(s(this, $)))); /** * 转换成 JSON * * @returns {HttpResponseData} */ l(this, "toJSON", () => ({ code: this.code, message: this.message, data: this.data, url: this.url, res: this.res })); /** * 响应消息字符串 * * @returns {string} */ l(this, "toString", () => this.message); /** * 显示消息 * * @returns {void} */ w(this, k, () => { s(this, j) && (C(this, j, !1), se.onMsg(this.code, this.message)); }); /** * Promise then * * @param {HttpResponseThenCallback | undefined} fn * @param {HttpResponseRejectCallback} [onrejected] * @returns {Promise} */ w(this, X, (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, $))); /** * Promise catch * * @param {HttpResponseRejectCallback} fn * @returns {Promise} */ w(this, Z, (e) => Promise.resolve(e(s(this, $)))); /** * Promise finally * * @param {(() => void) | undefined} [fn] * @returns {Promise} */ w(this, ee, (e) => (e == null || e(), [0, 1].includes(this.code) ? Promise.resolve(s(this, B).getPrototypeOf()) : Promise.reject(s(this, $)))); return this.code = e, this.message = t, this.data = n, this.url = i, this.res = c, C(this, $, new Proxy(this, s(this, B))); } static [Symbol.hasInstance](e) { return (e == null ? void 0 : e.constructor) === ie || (e == null ? void 0 : e[Symbol.for("HttpResponse")]) === !0; } }; $ = new WeakMap(), q = new WeakMap(), j = new WeakMap(), B = new WeakMap(), W = new WeakMap(), k = new WeakMap(), X = new WeakMap(), Z = new WeakMap(), ee = new WeakMap(); let L = ie; const ne = (r, e, t, n = "/", i = "") => ( /** @type {HttpResponseProxy} */ new L(r, e, t, n, i) ); var le = [ "utf8", "utf-8", "unicode-1-1-utf-8" ]; function ve(r) { if (le.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 c = 0; c < i.length; c++) n[c] = i[c].charCodeAt(0); return n; }; } function we(r) { if (le.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") 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), c = new Array(i.length); for (let f = 0; f < i.length; f++) c[f] = String.fromCharCode(i[f]); return decodeURIComponent(escape(c.join(""))); } else throw new TypeError("passed argument must be an array buffer view"); }; } var fe = { TextEncoder: ve, TextDecoder: we }; const ye = new fe.TextEncoder("utf8"), be = new fe.TextDecoder("utf8"); function Ee(r) { let e = 5381; for (let t = 0; t < r.length; t++) e = e * 33 ^ r.charCodeAt(t); return (e >>> 0).toString(16); } function Pe(r, e) { return r.replace(new RegExp(`^${e}+|${e}+$`, "g"), ""); } function Te(r) { return ye.encode(r || ""); } function _e(r) { return be.decode(new DataView(r), {}); } const Oe = 62, Q = 30, he = 31, Se = 63; class xe { /** * @param {string} alphabet 62 位编码字母表 */ constructor(e) { /** @type {string[]} */ l(this, "encodeTable", []); /** @type {Uint8Array} */ l(this, "decodeMap", new Uint8Array(256).fill(255)); if (e.length !== Oe) 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 < e.length; t++) this.decodeMap[e.charCodeAt(t)] = t; } /** * 将字符串编码为 Base62 字符串 * @param {string} src * @returns {string} */ encode(e) { if (!e || e.length === 0) return ""; const t = new me(this).encodeV2(e); return String.fromCharCode(...t); } /** * 将 Base62 字符串解码为原始字符串 * @param {string} src * @returns {string} */ decode(e) { if (!e || e.length === 0) return ""; const t = new Ae(this).decode(e); return new TextDecoder().decode(t); } } var z; class me { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ w(this, z); /** @type {Uint8Array} */ l(this, "src", new Uint8Array()); /** @type {number} */ l(this, "pos", 0); C(this, z, 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; } /** * @param {string} src * @returns {number[]} */ encodeV2(e) { this.src = Te(e), this.pos = this.src.length * 8; const t = []; for (; this.pos > 0; ) { let n = 6, i = this.get6bits(); (i & Q) === Q && ((this.pos > 6 || i > he) && (n = 5), i &= he), t.push(s(this, z).encodeTable[i].charCodeAt(0)), this.pos -= n; } return t; } } z = new WeakMap(); var V; class Ae { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ w(this, V); C(this, V, e); } /** * @param {string} src * @returns {Uint8Array} */ decode(e) { const t = new Uint8Array(Math.ceil(e.length * 6 / 8) + 1); let n = t.length, i = 0, c = 0; for (let f = 0; f < e.length; f++) { const U = e[f].charCodeAt(0), a = s(this, V).decodeMap[U]; if (a === 255) throw new Error(`CorruptInputError at index ${f}`); f === 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] = c & 255), t.slice(n); } } V = 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 G, D, te, R, F, K, re, Y; class se { /** * @param {string} [appKey] * @param {string} [appSecret] * @param {RequestFn} [reqFn] */ constructor(e, t, n) { /** @type {RequestFn | undefined} */ w(this, G); /** @type {Base62 | undefined} */ w(this, D); /** @type {string | undefined} */ w(this, te); w(this, R, { /** @type {Map} */ "#": /* @__PURE__ */ new Map(), /** @type {Map} */ $: /* @__PURE__ */ new Map() }); /** @type {Map>} */ w(this, F, /* @__PURE__ */ new Map()); /** @type {Set<[string, '#' | '$', RequestOptions & { method: string }, boolean]>} */ w(this, K, /* @__PURE__ */ new Set()); /** * 初始化请求实例 * @param {string} [appKey] * @param {string} [appSecret] * @param {RequestFn} [reqFn] */ l(this, "init", (e, t, n) => { e && C(this, te, e), n && C(this, G, n), t && C(this, D, new xe(t)), s(this, re).call(this); }); w(this, re, () => { !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 * @param {'#' | '$'} flag * @param {RequestOptions & { method: string }} params * @param {boolean} [isCacheRequest] */ w(this, Y, (e, t, n, i = !1) => { if (!s(this, G)) s(this, K).add([e, t, n, i]); else { const { url: c, method: f, ...U } = n || {}; s(this, G).call(this, { url: c, method: f, ...U }).then((a) => { if (![0, 1].includes(a.code)) throw ne(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} */ a ); return (t == "#" || f == "GET") && s(this, R)[t].set(e, T), t != "#" && f == "GET" && !i && setTimeout(() => { s(this, R)[t].delete(e); }, 300), T; }).then((a) => { var T; return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((_, m, I) => { _ && _[0](a), delete I[m]; }); }).catch((a) => { var T; s(this, R)[t].delete(e), (T = s(this, F).get(e)) == null || T.forEach((_, m, I) => { _ && _[1](a instanceof L ? a : ne(-1, a, null, c)), delete I[m]; }); }).finally(() => s(this, F).delete(e)); } }); /** * 发起请求,支持缓存与并发合并 * @param {RequestOptions} options * @param {boolean} [isCacheRequest] * @returns {any} 返回 Proxy 包裹的 Promise,支持链式调用 .then / .catch / .msg / .resp */ l(this, "request", ({ url: e = "/", method: t = "GET", ...n }, i = !1) => { var m, I; e = Pe(e, "/"); const c = ( /** @type {HttpMethod} */ t.toUpperCase() ), f = Ee(JSON.stringify([e, c, n])), U = ( /** @type {'#' | '$'} */ (m = e == null ? void 0 : e.includes) != null && m.call(e, "/_/") ? "#" : "$" ), { promise: a, resolve: T, reject: _ } = Promise.withResolvers(); return s(this, R)[U].has(f) ? (console.log("cache", e), T(s(this, R)[U].get(f))) : s(this, F).has(f) ? (I = s(this, F).get(f)) == null || I.push([T, _]) : (s(this, F).set(f, [[T, _]]), s(this, Y).call(this, f, U, { url: e, method: c, ...n }, i)), new Proxy(a, { // Symbol.for('HttpResponse') 是标记,用于外部 instanceof 识别此 Proxy 为 HttpResponse 类型 get: (H, h) => h === Symbol.for("HttpResponse") ? !0 : (...d) => H.then((u) => ( /** @type {any} */ ne( .../** @type {[number, string, unknown, string, string]} */ u )[h](...d) )).catch((u) => { if (!(u instanceof L)) throw u; return typeof /** @type {any} */ u[h] == "function" ? ( /** @type {any} */ u[h](...d) ) : ( /** @type {any} */ u[h] ); }) }); }); /** * GET 请求 * @param {string} url * @param {Record} [params] */ l(this, "get", (e, t = {}) => this.request({ url: e, method: "GET", params: t })); /** * POST 请求 * @param {string} url * @param {unknown} [data] * @param {Record} [params] */ l(this, "post", (e, t = {}, n = {}) => this.request({ url: e, method: "POST", data: t, params: n })); /** * PUT 请求 * @param {string} url * @param {unknown} [data] * @param {Record} [params] */ l(this, "put", (e, t = {}, n = {}) => this.request({ url: e, method: "PUT", data: t, params: n })); /** * DELETE 请求 * @param {string} url * @param {unknown} [data] * @param {Record} [params] */ l(this, "del", (e, t = {}, n = {}) => this.request({ url: e, method: "DELETE", data: t, params: n })); /** * 下载(待实现) * @param {string} url * @param {Record} [params] */ l(this, "download", (e, t) => { }); /** * 永久缓存 GET 请求 * @param {string} url * @param {Record} [params] */ l(this, "cache", (e, t = {}) => this.request({ url: e, method: "GET", params: t }, !0)); /** * 通过 Base62 编码参数请求列表接口 * @param {string} code * @param {Base62Param} [base62param] */ l(this, "list", (e, t = {}) => this.get(`/api/${e}/${this.encode( /** @type {Record} */ t )}`)); l(this, "getAppInfo", () => { }); /** * 清除缓存 * @param {boolean} [isSystem] 是否同时清除系统缓存(`/_/` 路由) */ l(this, "refreshCache", (e = !1) => { e && s(this, R)["#"].clear(), s(this, R).$.clear(); }); /** * 解码 Base62 字符串为 JSON 对象 * @param {string} [str] * @param {object} [defaultValue] * @returns {unknown} */ l(this, "decode", (e = "", t = {}) => { 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); } catch { return console.warn("core.decode", e), t; } }); /** * 将 JSON 对象编码为 Base62 字符串 * @param {Record | unknown[]} [json] * @returns {string} */ l(this, "encode", (e = {}) => { if (!s(this, D)) 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)); }); this.init(e, t, n); } } G = new WeakMap(), D = new WeakMap(), te = new WeakMap(), R = new WeakMap(), F = new WeakMap(), K = new WeakMap(), re = new WeakMap(), Y = new WeakMap(); se.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)); }; se.onMsg = (r, e) => [0, 1].includes(r) ? console.log(e) : console.warn(e); export { L as HttpResponse, ne as NewHttpResponse, se as default, Ee as signature, Te as str2uint8array, Pe as trim, _e as uint8array2str };