var pe = Object.defineProperty; var ce = (r) => { throw TypeError(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 fe = {}; (function(r) { (function(e) { e([], function() { var t = -1, n = -2, i = -3, c = -4, u = -5, U = function(h, f) { f = f || {}; 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: [] }; 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 o > "u") return { type: "undefined", index: u }; 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(d.strings, o); return P == -1 && (d.strings.push(T(o)), P = d.strings.length - 1), { type: "strings", index: P }; } 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: P }; } if (y === "number") { var P = H.call(d.floats, o); return P == -1 && (d.floats.push(o), P = d.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 = 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 = 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 u; if (b === "empty") return c; throw new TypeError("The item is alien!"); }(v), l && console.log("Ending parser"), f.debug ? { dictionary: d, ast: v, packed: M } : M; }, a = function(h, f) { f = f || {}; var l = h.split("^"); f.verbose && console.log("Building dictionary"); 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 (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 (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])); } 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 = 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 w; if (A === "@" || A === "$") w.push(N()); else switch (A) { case t: w.push(!0); break; case n: w.push(!1); break; case i: w.push(null); break; case u: w.push(void 0); break; case c: w.push(""); break; default: w.push(d[A]); } } return f.verbose && console.log("Parsed " + this.JSON.stringify(w)), w; } if (P === "$") { for (var w = {}; b < S; b++) { var x = O[b]; if (x === "]") return w; x === c ? x = "" : x = d[x]; var A = O[++b]; if (A === "@" || A === "$") w[x] = N(); else switch (A) { case t: w[x] = !0; break; case n: w[x] = !1; break; case i: w[x] = null; break; case u: w[x] = void 0; break; case c: w[x] = ""; break; default: w[x] = d[A]; } } return f.verbose && console.log("Parsed " + this.JSON.stringify(w)), w; } throw new TypeError("Bad token " + P + " isn't a type"); }(); }, T = function(h) { return typeof h != "string" ? h : h.replace(/[\+ \|\^\%]/g, function(f) { return { " ": "+", "+": "%2B", "|": "%7C", "^": "%5E", "%": "%25" }[f]; }); }, C = function(h) { return typeof h != "string" ? h : h.replace(/\+|%2B|%7C|%5E|%25/g, function(f) { return { "+": " ", "%2B": "+", "%7C": "|", "%5E": "^", "%25": "%" }[f]; }); }, 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: U, unpack: a }; }); })(function(e, t) { var n = t(); for (var i in n) r[i] = n[i]; }); })(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 * @param {unknown} data * @param {string} url * @param {string} res */ constructor(e, t, n, i, c) { /** @type {number} */ g(this, "code", 0); /** @type {string} */ g(this, "message", "操作成功"); /** @type {unknown} */ g(this, "data", null); /** @type {string} */ g(this, "url", ""); /** @type {string} */ g(this, "res", ""); /** @type {HttpResponse} */ p(this, $); // 判断返回 Response 对象, 还是 Response 中的 data /** @type {boolean} */ p(this, q, !0); // 判断是否已经给出错误提示 /** @type {boolean} */ p(this, B, !0); p(this, D, { /** @param {any} __ @param {string} key */ 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, D).getPrototypeOf() || {}), /** @returns {HttpResponse | unknown} */ getPrototypeOf: () => s(this, q) ? this : this.data, /** @returns {PropertyDescriptor} */ getOwnPropertyDescriptor: () => ({ configurable: !0, enumerable: !0, writable: !0, value: s(this, D).getPrototypeOf() }) }); /** * @param {string} key * @returns {unknown} */ 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} */ 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} */ 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} */ p(this, te, (e) => Promise.resolve(e(s(this, $)))); /** * @param {(() => void) | undefined} [fn] * @returns {Promise} */ 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))); } 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 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 c = 0; c < i.length; c++) n[c] = i[c].charCodeAt(0); return n; }; } 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") 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 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 ge = { TextEncoder: ye, TextDecoder: be }; 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); return (e >>> 0).toString(16); } function Oe(r, e) { return r.replace(new RegExp(`^${e}+|${e}+$`, "g"), ""); } function Se(r) { return Ee.encode(r || ""); } function Ne(r) { return Pe.decode(new DataView(r), {}); } const xe = 62, Q = 30, ue = 31, me = 63; class Ae { /** * @param {string} alphabet 62 位编码字母表 */ constructor(e) { /** @type {string[]} */ g(this, "encodeTable", []); /** @type {Uint8Array} */ g(this, "decodeMap", new Uint8Array(256).fill(255)); if (e.length !== xe) 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 _e(this).encodeV2(e); return String.fromCharCode(...t); } /** * 将 Base62 字符串解码为原始字符串 * @param {string} src * @returns {string} */ decode(e) { if (!e || e.length === 0) return ""; const t = new Ce(this).decode(e); return new TextDecoder().decode(t); } } var V; class _e { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ p(this, V); /** @type {Uint8Array} */ g(this, "src", new Uint8Array()); /** @type {number} */ g(this, "pos", 0); _(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 & me; } /** * @param {string} src * @returns {number[]} */ encodeV2(e) { this.src = Se(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 > ue) && (n = 5), i &= ue), t.push(s(this, V).encodeTable[i].charCodeAt(0)), this.pos -= n; } return t; } } V = new WeakMap(); var k; class Ce { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ p(this, k); _(this, k, 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 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] = c & 255), t.slice(n); } } 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 G, j, ne, R, F, K, se, Y; class oe { /** * @param {string} [appKey] * @param {string} [appSecret] * @param {RequestFn} [reqFn] */ constructor(e, t, n) { /** @type {RequestFn | undefined} */ p(this, G); /** @type {Base62 | undefined} */ p(this, j); /** @type {string | undefined} */ p(this, ne); p(this, R, { /** @type {Map} */ "#": /* @__PURE__ */ new Map(), /** @type {Map} */ $: /* @__PURE__ */ new Map() }); /** @type {Map>} */ p(this, F, /* @__PURE__ */ new Map()); /** @type {Set<[string, '#' | '$', RequestOptions & { method: string }, boolean]>} */ p(this, K, /* @__PURE__ */ new Set()); /** * 初始化请求实例 * @param {string} [appKey] * @param {string} [appSecret] * @param {RequestFn} [reqFn] */ g(this, "init", (e, t, n) => { e && _(this, ne, e), n && _(this, G, n), t && _(this, j, new Ae(t)), s(this, se).call(this); }); 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 * @param {'#' | '$'} flag * @param {RequestOptions & { method: string }} params * @param {boolean} [isCacheRequest] */ p(this, Y, (e, t, n, i = !1) => { if (!s(this, G)) s(this, K).add([e, t, n, i]); else { 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} */ a ); 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((a) => { var T; return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((C, m, I) => { C && C[0](a), delete I[m]; }); }).catch((a) => { var T; 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)); } }); /** * 发起请求,支持缓存与并发合并 * @param {RequestOptions} options * @param {boolean} [isCacheRequest] * @returns {any} 返回 Proxy 包裹的 Promise,支持链式调用 .then / .catch / .msg / .resp */ g(this, "request", ({ url: e = "/", method: t = "GET", ...n }, i = !1) => { var m, I; e = Oe(e, "/"); const c = ( /** @type {HttpMethod} */ t.toUpperCase() ), u = Te(JSON.stringify([e, c, n])), U = ( /** @type {'#' | '$'} */ (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} */ ie( .../** @type {[number, string, unknown, string, string]} */ l )[h](...f) )).catch((l) => { if (!(l instanceof L)) throw l; return typeof /** @type {any} */ l[h] == "function" ? ( /** @type {any} */ l[h](...f) ) : ( /** @type {any} */ l[h] ); }) }); }); /** * GET 请求 * @param {string} url * @param {Record} [params] */ g(this, "get", (e, t = {}) => this.request({ url: e, method: "GET", params: t })); /** * POST 请求 * @param {string} url * @param {unknown} [data] * @param {Record} [params] */ g(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] */ g(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] */ g(this, "del", (e, t = {}, n = {}) => this.request({ url: e, method: "DELETE", data: t, params: n })); /** * 下载(待实现) * @param {string} url * @param {Record} [params] */ g(this, "download", (e, t) => { }); /** * 永久缓存 GET 请求 * @param {string} url * @param {Record} [params] */ g(this, "cache", (e, t = {}) => this.request({ url: e, method: "GET", params: t }, !0)); /** * 通过 Base62 编码参数请求列表接口 * @param {string} code * @param {Base62Param} [base62param] */ g(this, "list", (e, t = {}) => this.get(`/api/${e}/${this.encode( /** @type {Record} */ t )}`)); g(this, "getAppInfo", () => { }); /** * 清除缓存 * @param {boolean} [isSystem] 是否同时清除系统缓存(`/_/` 路由) */ g(this, "refreshCache", (e = !1) => { e && s(this, R)["#"].clear(), s(this, R).$.clear(); }); /** * 解码 Base62 字符串为 JSON 对象 * @param {string} [str] * @param {object} [defaultValue] * @returns {unknown} */ g(this, "decode", (e = "", t = {}) => { if (e === void 0 || e === "" || e === null) return t; try { 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; } }); /** * 将 JSON 对象编码为 Base62 字符串 * @param {Record | unknown[]} [json] * @returns {string} */ g(this, "encode", (e = {}) => { 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, j).encode(le.pack(t)); }); this.init(e, t, n); } } 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)); }; oe.onMsg = (r, e) => [0, 1].includes(r) ? console.log(e) : console.warn(e); export { L as HttpResponse, ie as NewHttpResponse, oe as default, Te as signature, Se as str2uint8array, Oe as trim, Ne as uint8array2str };