diff --git a/ff-request.js b/ff-request.js index 7fa829c..96ca156 100644 --- a/ff-request.js +++ b/ff-request.js @@ -4,7 +4,7 @@ var oe = (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); +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), A = (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; } @@ -12,7 +12,7 @@ var ue = {}; (function(r) { (function(e) { e([], function() { - var t = -1, n = -2, i = -3, c = -4, f = -5, U = function(h, d) { + var t = -1, n = -2, i = -3, o = -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"); @@ -22,53 +22,53 @@ var ue = {}; floats: [] }; u && console.log("Creating the AST"); - var p = function S(o) { - u && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(o)); - var y = typeof o; - if (o === null) + var p = function S(a) { + u && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(a)); + var y = typeof a; + if (a === null) return { type: "null", index: i }; - if (typeof o > "u") + if (typeof a > "u") return { type: "undefined", index: f }; - if (o instanceof Array) { + if (a instanceof Array) { var O = ["@"]; - for (var b in o) - o.hasOwnProperty(b) && O.push(S(o[b])); + for (var b in a) + a.hasOwnProperty(b) && O.push(S(a[b])); return O; } if (y === "object") { var O = ["$"]; - for (var N in o) - o.hasOwnProperty(N) && (O.push(S(N)), O.push(S(o[N]))); + for (var N in a) + a.hasOwnProperty(N) && (O.push(S(N)), O.push(S(a[N]))); return O; } - if (o === "") + if (a === "") return { type: "empty", - index: c + index: o }; if (y === "string") { - var P = q.call(g.strings, o); - return P == -1 && (g.strings.push(T(o)), P = g.strings.length - 1), { + var P = q.call(g.strings, a); + return P == -1 && (g.strings.push(T(a)), P = g.strings.length - 1), { type: "strings", index: P }; } - if (y === "number" && o % 1 === 0) { - var P = q.call(g.integers, o); - return P == -1 && (g.integers.push(m(o)), P = g.integers.length - 1), { + if (y === "number" && a % 1 === 0) { + var P = q.call(g.integers, a); + return P == -1 && (g.integers.push(m(a)), P = g.integers.length - 1), { type: "integers", index: P }; } if (y === "number") { - var P = q.call(g.floats, o); - return P == -1 && (g.floats.push(o), P = g.floats.length - 1), { + var P = q.call(g.floats, a); + return P == -1 && (g.floats.push(a), P = g.floats.length - 1), { type: "floats", index: P }; @@ -76,20 +76,20 @@ var ue = {}; if (y === "boolean") return { type: "boolean", - index: o ? t : n + index: a ? t : n }; - throw new Error("Unexpected argument of type " + typeof o); + throw new Error("Unexpected argument of type " + typeof a); }(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 S(o) { - if (u && console.log("Calling a recursiveParser with " + this.JSON.stringify(o)), o instanceof Array) { - var y = o.shift(); - for (var O in o) - o.hasOwnProperty(O) && (y += S(o[O]) + "|"); + var _ = g.strings.join("|"); + return _ += "^" + g.integers.join("|"), _ += "^" + g.floats.join("|"), u && console.log("Parsing the structure"), _ += "^" + function S(a) { + if (u && console.log("Calling a recursiveParser with " + this.JSON.stringify(a)), a instanceof Array) { + var y = a.shift(); + for (var O in a) + a.hasOwnProperty(O) && (y += S(a[O]) + "|"); return (y[y.length - 1] === "|" ? y.slice(0, -1) : y) + "]"; } - var b = o.type, N = o.index; + var b = a.type, N = a.index; if (b === "strings") return m(N); if (b === "integers") @@ -97,20 +97,20 @@ var ue = {}; if (b === "floats") return m(E + J + N); if (b === "boolean") - return o.index; + return a.index; if (b === "null") return i; if (b === "undefined") return f; if (b === "empty") - return c; + return o; 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) { + packed: _ + } : _; + }, c = function(h, d) { d = d || {}; var u = h.split("^"); d.verbose && console.log("Building dictionary"); @@ -118,7 +118,7 @@ var ue = {}; 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])); + g.push(C(p[E])); } if (p = u[1], p !== "") { p = p.split("|"), d.verbose && console.log("Parse the integers dictionary"); @@ -131,22 +131,22 @@ var ue = {}; g.push(parseFloat(p[E])); } p = null, d.verbose && console.log("Tokenizing the structure"); - for (var M = "", S = [], o = u[3].length, E = 0; E < o; E++) { + for (var _ = "", S = [], a = u[3].length, E = 0; E < a; E++) { var y = u[3].charAt(E); - y === "|" || y === "$" || y === "@" || y === "]" ? (M && (S.push(I(M)), M = ""), y !== "|" && S.push(y)) : M += y; + y === "|" || y === "$" || y === "@" || y === "]" ? (_ && (S.push(I(_)), _ = ""), y !== "|" && S.push(y)) : _ += y; } var O = S.length, b = 0; return d.verbose && console.log("Starting recursive parser"), function N() { var P = S[b++]; if (d.verbose && console.log("Reading collection type " + (P === "$" ? "object" : "Array")), P === "@") { for (var v = []; b < O; b++) { - var A = S[b]; - if (d.verbose && console.log("Read " + A + " symbol"), A === "]") + var M = S[b]; + if (d.verbose && console.log("Read " + M + " symbol"), M === "]") return v; - if (A === "@" || A === "$") + if (M === "@" || M === "$") v.push(N()); else - switch (A) { + switch (M) { case t: v.push(!0); break; @@ -159,11 +159,11 @@ var ue = {}; case f: v.push(void 0); break; - case c: + case o: v.push(""); break; default: - v.push(g[A]); + v.push(g[M]); } } return d.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; @@ -173,12 +173,12 @@ var ue = {}; var x = S[b]; if (x === "]") return v; - x === c ? x = "" : x = g[x]; - var A = S[++b]; - if (A === "@" || A === "$") + x === o ? x = "" : x = g[x]; + var M = S[++b]; + if (M === "@" || M === "$") v[x] = N(); else - switch (A) { + switch (M) { case t: v[x] = !0; break; @@ -191,11 +191,11 @@ var ue = {}; case f: v[x] = void 0; break; - case c: + case o: v[x] = ""; break; default: - v[x] = g[A]; + v[x] = g[M]; } } return d.verbose && console.log("Parsed " + this.JSON.stringify(v)), v; @@ -212,7 +212,7 @@ var ue = {}; "%": "%25" }[d]; }); - }, _ = function(h) { + }, C = function(h) { return typeof h != "string" ? h : h.replace(/\+|%2B|%7C|%5E|%25/g, function(d) { return { "+": " ", @@ -235,7 +235,7 @@ var ue = {}; return { JSON, pack: U, - unpack: a + unpack: c }; }); })(function(e, t) { @@ -254,7 +254,7 @@ const ie = class ie { * @param {string} url * @param {string} res */ - constructor(e, t, n, i, c) { + constructor(e, t, n, i, o) { /** @type {number} */ l(this, "code", 0); /** @type {string} */ @@ -330,7 +330,7 @@ const ie = class ie { * @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)); + w(this, W, (e) => e === "$echoMsg" ? s(this, k) : e === "then" ? (A(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 消息弹窗后回调 * @@ -354,7 +354,7 @@ const ie = class ie { * fn 内部抛出的异常会变成 reject,而不是同步抛出。 */ l(this, "msg", (e = (n) => n, t = !1) => { - s(this, k).call(this), C(this, H, t); + s(this, k).call(this), A(this, H, t); try { return Promise.resolve( e == null ? void 0 : e(s(this, B).getPrototypeOf()) @@ -390,7 +390,7 @@ const ie = class ie { * @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, $)))); + l(this, "resp", (e, t) => (A(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 * @@ -415,7 +415,7 @@ const ie = class ie { * @returns {void} */ w(this, k, () => { - s(this, j) && (C(this, j, !1), se.onMsg(this.code, this.message)); + s(this, j) && (A(this, j, !1), se.onMsg(this.code, this.message)); }); /** * Promise then @@ -431,7 +431,7 @@ const ie = class ie { * @param {HttpResponseRejectCallback} fn * @returns {Promise} */ - w(this, Z, (e) => Promise.resolve(e(s(this, $)))); + w(this, Z, (e) => [0, 1].includes(this.code) ? Promise.resolve(this.data) : Promise.resolve(e(s(this, $)))); /** * Promise finally * @@ -439,7 +439,7 @@ const ie = class ie { * @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))); + return this.code = e, this.message = t, this.data = n, this.url = i, this.res = o, A(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; @@ -464,8 +464,8 @@ function ve(r) { 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); + for (let o = 0; o < i.length; o++) + n[o] = i[o].charCodeAt(0); return n; }; } @@ -479,10 +479,10 @@ 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), c = new Array(i.length); + var i = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), o = new Array(i.length); for (let f = 0; f < i.length; f++) - c[f] = String.fromCharCode(i[f]); - return decodeURIComponent(escape(c.join(""))); + o[f] = String.fromCharCode(i[f]); + return decodeURIComponent(escape(o.join(""))); } else throw new TypeError("passed argument must be an array buffer view"); }; @@ -492,11 +492,13 @@ var fe = { 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 Ee(r, e = 0) { + let t = 3735928559 ^ e, n = 1103547991 ^ e; + for (let i = 0; i < r.length; i++) { + const o = r.charCodeAt(i); + t = Math.imul(t ^ o, 2654435761), n = Math.imul(n ^ o, 1597334677); + } + return t = Math.imul(t ^ t >>> 16, 2246822507), t ^= Math.imul(n ^ n >>> 13, 3266489909), n = Math.imul(n ^ n >>> 16, 2246822507), n ^= Math.imul(t ^ t >>> 13, 3266489909), (4294967296 * (2097151 & n) + (t >>> 0)).toString(16); } function Pe(r, e) { return r.replace(new RegExp(`^${e}+|${e}+$`, "g"), ""); @@ -504,7 +506,7 @@ function Pe(r, e) { function Te(r) { return ye.encode(r || ""); } -function _e(r) { +function Ce(r) { return be.decode(new DataView(r), {}); } const Se = 62, Q = 30, he = 31, Oe = 63; @@ -543,7 +545,7 @@ class xe { */ decode(e) { if (!e || e.length === 0) return ""; - const t = new Ae(this).decode(e); + const t = new Me(this).decode(e); return new TextDecoder().decode(t); } } @@ -559,7 +561,7 @@ class me { l(this, "src", new Uint8Array()); /** @type {number} */ l(this, "pos", 0); - C(this, z, e); + A(this, z, e); } /** @returns {number} */ get6bits() { @@ -584,14 +586,14 @@ class me { } z = new WeakMap(); var V; -class Ae { +class Me { /** * @param {Base62} b62 */ constructor(e) { /** @type {Base62} */ w(this, V); - C(this, V, e); + A(this, V, e); } /** * @param {string} src @@ -599,14 +601,14 @@ class Ae { */ decode(e) { const t = new Uint8Array(Math.ceil(e.length * 6 / 8) + 1); - let n = t.length, i = 0, c = 0; + let n = t.length, i = 0, o = 0; for (let f = 0; f < e.length; f++) { - const U = e[f].charCodeAt(0), a = s(this, V).decodeMap[U]; - if (a === 255) + const U = e[f].charCodeAt(0), c = s(this, V).decodeMap[U]; + if (c === 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); + f === e.length - 1 ? (o |= c << i, i += Math.ceil(Math.log2(c + 1))) : (c & Q) === Q ? (o |= c << i, i += 5) : (o |= c << i, i += 6), i >= 8 && (n--, t[n] = o & 255, i %= 8, o >>= 8); } - return i > 0 && (n--, t[n] = c & 255), t.slice(n); + return i > 0 && (n--, t[n] = o & 255), t.slice(n); } } V = new WeakMap(); @@ -647,7 +649,7 @@ class se { * @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); + e && A(this, te, e), n && A(this, G, n), t && A(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()); @@ -662,28 +664,28 @@ class se { 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 { url: o, method: f, ...U } = n || {}; + s(this, G).call(this, { url: o, method: f, ...U }).then((c) => { + if (![0, 1].includes(c.code)) + throw ne(c.code ?? -1, 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 T = ( /** @type {CacheEntry} */ - a + c ); 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) => { + }).then((c) => { var T; - return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((_, m, I) => { - _ && _[0](a), delete I[m]; + return (T = s(this, F).get(e)) == null ? void 0 : T.forEach((C, m, I) => { + C && C[0](c), delete I[m]; }); - }).catch((a) => { + }).catch((c) => { 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]; + s(this, R)[t].delete(e), (T = s(this, F).get(e)) == null || T.forEach((C, m, I) => { + C && C[1](c instanceof L ? c : ne(-1, c, null, o)), delete I[m]; }); }).finally(() => s(this, F).delete(e)); } @@ -697,16 +699,16 @@ class se { l(this, "request", ({ url: e = "/", method: t = "GET", ...n }, i = !1) => { var m, I; e = Pe(e, "/"); - const c = ( + const o = ( /** @type {HttpMethod} */ t.toUpperCase() - ), f = Ee(JSON.stringify([e, c, n])), U = ( + ), f = Ee(JSON.stringify([e, o, 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)), /** @type {HttpResponseChainable} */ + ), { promise: c, resolve: T, reject: C } = 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, C]) : (s(this, F).set(f, [[T, C]]), s(this, Y).call(this, f, U, { url: e, method: o, ...n }, i)), /** @type {HttpResponseChainable} */ /** @type {unknown} */ - new Proxy(a, { + new Proxy(c, { // Symbol.for('HttpResponse') 是标记,用于外部 instanceof 识别此 Proxy 为 HttpResponse 类型 get: (q, h) => h === Symbol.for("HttpResponse") ? !0 : (...d) => q.then((u) => ( /** @type {any} */ @@ -835,5 +837,5 @@ export { Ee as signature, Te as str2uint8array, Pe as trim, - _e as uint8array2str + Ce as uint8array2str }; diff --git a/ff-request.umd.cjs b/ff-request.umd.cjs index 82665a4..45e0d9b 100644 --- a/ff-request.umd.cjs +++ b/ff-request.umd.cjs @@ -1,2 +1,2 @@ -(function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o["ff-request"]={}))})(this,function(o){"use strict";var Ae=Object.defineProperty;var ve=o=>{throw TypeError(o)};var Ce=(o,d,P)=>d in o?Ae(o,d,{enumerable:!0,configurable:!0,writable:!0,value:P}):o[d]=P;var f=(o,d,P)=>Ce(o,typeof d!="symbol"?d+"":d,P),ye=(o,d,P)=>d.has(o)||ve("Cannot "+P);var n=(o,d,P)=>(ye(o,d,"read from private field"),P?P.call(o):d.get(o)),b=(o,d,P)=>d.has(o)?ve("Cannot add the same private member more than once"):d instanceof WeakSet?d.add(o):d.set(o,P),N=(o,d,P,V)=>(ye(o,d,"write to private field"),V?V.call(o,P):d.set(o,P),P);var R,Y,G,H,ne,Q,se,ie,oe,W,X,q,K,ae,$,F,L,ce,Z;function d(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,g=-5,B=function(h,p){p=p||{};var l=p.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 v={strings:[],integers:[],floats:[]};l&&console.log("Creating the AST");var y=function x(a){l&&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:g};if(a instanceof Array){var _=["@"];for(var T in a)a.hasOwnProperty(T)&&_.push(x(a[T]));return _}if(E==="object"){var _=["$"];for(var j in a)a.hasOwnProperty(j)&&(_.push(x(j)),_.push(x(a[j])));return _}if(a==="")return{type:"empty",index:u};if(E==="string"){var O=z.call(v.strings,a);return O==-1&&(v.strings.push(m(a)),O=v.strings.length-1),{type:"strings",index:O}}if(E==="number"&&a%1===0){var O=z.call(v.integers,a);return O==-1&&(v.integers.push(C(a)),O=v.integers.length-1),{type:"integers",index:O}}if(E==="number"){var O=z.call(v.floats,a);return O==-1&&(v.floats.push(a),O=v.floats.length-1),{type:"floats",index:O}}if(E==="boolean")return{type:"boolean",index:a?t:r};throw new Error("Unexpected argument of type "+typeof a)}(h),S=v.strings.length,k=v.integers.length;v.floats.length,l&&console.log("Parsing the dictionary");var I=v.strings.join("|");return I+="^"+v.integers.join("|"),I+="^"+v.floats.join("|"),l&&console.log("Parsing the structure"),I+="^"+function x(a){if(l&&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,j=a.index;if(T==="strings")return C(j);if(T==="integers")return C(S+j);if(T==="floats")return C(S+k+j);if(T==="boolean")return a.index;if(T==="null")return i;if(T==="undefined")return g;if(T==="empty")return u;throw new TypeError("The item is alien!")}(y),l&&console.log("Ending parser"),p.debug?{dictionary:v,ast:y,packed:I}:I},c=function(h,p){p=p||{};var l=h.split("^");p.verbose&&console.log("Building dictionary");var v=[],y=l[0];if(y!==""){y=y.split("|"),p.verbose&&console.log("Parse the strings dictionary");for(var S=0,k=y.length;Sn(this,ne).call(this,t),set:(e,t,r)=>{throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${String(t)}"`)},ownKeys:()=>Object.keys(n(this,H).getPrototypeOf()||{}),getPrototypeOf:()=>n(this,Y)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:n(this,H).getPrototypeOf()})});b(this,ne,e=>e==="$echoMsg"?n(this,Q):e==="then"?(N(this,G,!0),n(this,se)):e==="catch"?n(this,ie):e==="finally"?n(this,oe):e==="IS_ECHO_MSG"?n(this,G):Reflect.get(this,e));f(this,"msg",(e=r=>r,t=!1)=>{n(this,Q).call(this),N(this,Y,t);try{return Promise.resolve(e==null?void 0:e(n(this,H).getPrototypeOf()))}catch(r){return Promise.reject(r)}});f(this,"resp",(e,t)=>(N(this,G,!0),[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))));f(this,"toJSON",()=>({code:this.code,message:this.message,data:this.data,url:this.url,res:this.res}));f(this,"toString",()=>this.message);b(this,Q,()=>{n(this,G)&&(N(this,G,!1),re.onMsg(this.code,this.message))});b(this,se,(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)));b(this,ie,e=>Promise.resolve(e(n(this,R))));b(this,oe,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)===ue||(e==null?void 0:e[Symbol.for("HttpResponse")])===!0}};R=new WeakMap,Y=new WeakMap,G=new WeakMap,H=new WeakMap,ne=new WeakMap,Q=new WeakMap,se=new WeakMap,ie=new WeakMap,oe=new WeakMap;let J=ue;const ee=(s,e,t,r="/",i="")=>new J(s,e,t,r,i);var he=["utf8","utf-8","unicode-1-1-utf-8"];function we(s){if(he.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 g=0;g>>0).toString(16)}function de(s,e){return s.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ge(s){return Ee.encode(s||"")}function Pe(s){return Te.decode(new DataView(s),{})}const Se=62,te=30,pe=31,Oe=63;class me{constructor(e){f(this,"encodeTable",[]);f(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==Se)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>pe)&&(r=5),i&=pe),t.push(n(this,W).encodeTable[i].charCodeAt(0)),this.pos-=r}return t}}W=new WeakMap;class _e{constructor(e){b(this,X);N(this,X,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let r=t.length,i=0,u=0;for(let g=0;g=8&&(r--,t[r]=u&255,i%=8,u>>=8)}return i>0&&(r--,t[r]=u&255),t.slice(r)}}X=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){b(this,q);b(this,K);b(this,ae);b(this,$,{"#":new Map,$:new Map});b(this,F,new Map);b(this,L,new Set);f(this,"init",(e,t,r)=>{e&&N(this,ae,e),r&&N(this,q,r),t&&N(this,K,new me(t)),n(this,ce).call(this)});b(this,ce,()=>{!n(this,L).size||!n(this,q)||(n(this,L).forEach(e=>n(this,Z).call(this,...e)),n(this,L).clear())});b(this,Z,(e,t,r,i=!1)=>{if(!n(this,q))n(this,L).add([e,t,r,i]);else{const{url:u,method:g,...B}=r||{};n(this,q).call(this,{url:u,method:g,...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=="#"||g=="GET")&&n(this,$)[t].set(e,m),t!="#"&&g=="GET"&&!i&&setTimeout(()=>{n(this,$)[t].delete(e)},300),m}).then(c=>{var m;return(m=n(this,F).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,F).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,F).delete(e))}});f(this,"request",({url:e="/",method:t="GET",...r},i=!1)=>{var C,D;e=de(e,"/");const u=t.toUpperCase(),g=fe(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(g)?(console.log("cache",e),m(n(this,$)[B].get(g))):n(this,F).has(g)?(D=n(this,F).get(g))==null||D.push([m,U]):(n(this,F).set(g,[[m,U]]),n(this,Z).call(this,g,B,{url:e,method:u,...r},i)),new Proxy(c,{get:(z,h)=>h===Symbol.for("HttpResponse")?!0:(...p)=>z.then(l=>ee(...l)[h](...p)).catch(l=>{if(!(l instanceof J))throw l;return typeof l[h]=="function"?l[h](...p):l[h]}),set:(z,h)=>{throw new TypeError(`request() 返回值是只读的,不能设置属性 "${String(h)}"`)}})});f(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));f(this,"post",(e,t={},r={})=>this.request({url:e,method:"POST",data:t,params:r}));f(this,"put",(e,t={},r={})=>this.request({url:e,method:"PUT",data:t,params:r}));f(this,"del",(e,t={},r={})=>this.request({url:e,method:"DELETE",data:t,params:r}));f(this,"download",(e,t)=>{});f(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));f(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));f(this,"getAppInfo",()=>{});f(this,"refreshCache",(e=!1)=>{e&&n(this,$)["#"].clear(),n(this,$).$.clear()});f(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}});f(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)}}q=new WeakMap,K=new WeakMap,ae=new WeakMap,$=new WeakMap,F=new WeakMap,L=new WeakMap,ce=new WeakMap,Z=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=fe,o.str2uint8array=ge,o.trim=de,o.uint8array2str=Pe,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); +(function(o,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(o=typeof globalThis<"u"?globalThis:o||self,d(o["ff-request"]={}))})(this,function(o){"use strict";var _e=Object.defineProperty;var ve=o=>{throw TypeError(o)};var Ae=(o,d,P)=>d in o?_e(o,d,{enumerable:!0,configurable:!0,writable:!0,value:P}):o[d]=P;var f=(o,d,P)=>Ae(o,typeof d!="symbol"?d+"":d,P),ye=(o,d,P)=>d.has(o)||ve("Cannot "+P);var n=(o,d,P)=>(ye(o,d,"read from private field"),P?P.call(o):d.get(o)),b=(o,d,P)=>d.has(o)?ve("Cannot add the same private member more than once"):d instanceof WeakSet?d.add(o):d.set(o,P),N=(o,d,P,V)=>(ye(o,d,"write to private field"),V?V.call(o,P):d.set(o,P),P);var R,Y,G,H,ne,Q,se,ie,oe,W,X,q,K,ae,$,F,L,ce,Z;function d(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,a=-4,g=-5,B=function(h,p){p=p||{};var l=p.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 v={strings:[],integers:[],floats:[]};l&&console.log("Creating the AST");var y=function x(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:g};if(c instanceof Array){var M=["@"];for(var T in c)c.hasOwnProperty(T)&&M.push(x(c[T]));return M}if(E==="object"){var M=["$"];for(var j in c)c.hasOwnProperty(j)&&(M.push(x(j)),M.push(x(c[j])));return M}if(c==="")return{type:"empty",index:a};if(E==="string"){var O=z.call(v.strings,c);return O==-1&&(v.strings.push(m(c)),O=v.strings.length-1),{type:"strings",index:O}}if(E==="number"&&c%1===0){var O=z.call(v.integers,c);return O==-1&&(v.integers.push(A(c)),O=v.integers.length-1),{type:"integers",index:O}}if(E==="number"){var O=z.call(v.floats,c);return O==-1&&(v.floats.push(c),O=v.floats.length-1),{type:"floats",index:O}}if(E==="boolean")return{type:"boolean",index:c?t:r};throw new Error("Unexpected argument of type "+typeof c)}(h),S=v.strings.length,k=v.integers.length;v.floats.length,l&&console.log("Parsing the dictionary");var I=v.strings.join("|");return I+="^"+v.integers.join("|"),I+="^"+v.floats.join("|"),l&&console.log("Parsing the structure"),I+="^"+function x(c){if(l&&console.log("Calling a recursiveParser with "+this.JSON.stringify(c)),c instanceof Array){var E=c.shift();for(var M in c)c.hasOwnProperty(M)&&(E+=x(c[M])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var T=c.type,j=c.index;if(T==="strings")return A(j);if(T==="integers")return A(S+j);if(T==="floats")return A(S+k+j);if(T==="boolean")return c.index;if(T==="null")return i;if(T==="undefined")return g;if(T==="empty")return a;throw new TypeError("The item is alien!")}(y),l&&console.log("Ending parser"),p.debug?{dictionary:v,ast:y,packed:I}:I},u=function(h,p){p=p||{};var l=h.split("^");p.verbose&&console.log("Building dictionary");var v=[],y=l[0];if(y!==""){y=y.split("|"),p.verbose&&console.log("Parse the strings dictionary");for(var S=0,k=y.length;Sn(this,ne).call(this,t),set:(e,t,r)=>{throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${String(t)}"`)},ownKeys:()=>Object.keys(n(this,H).getPrototypeOf()||{}),getPrototypeOf:()=>n(this,Y)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:n(this,H).getPrototypeOf()})});b(this,ne,e=>e==="$echoMsg"?n(this,Q):e==="then"?(N(this,G,!0),n(this,se)):e==="catch"?n(this,ie):e==="finally"?n(this,oe):e==="IS_ECHO_MSG"?n(this,G):Reflect.get(this,e));f(this,"msg",(e=r=>r,t=!1)=>{n(this,Q).call(this),N(this,Y,t);try{return Promise.resolve(e==null?void 0:e(n(this,H).getPrototypeOf()))}catch(r){return Promise.reject(r)}});f(this,"resp",(e,t)=>(N(this,G,!0),[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))));f(this,"toJSON",()=>({code:this.code,message:this.message,data:this.data,url:this.url,res:this.res}));f(this,"toString",()=>this.message);b(this,Q,()=>{n(this,G)&&(N(this,G,!1),re.onMsg(this.code,this.message))});b(this,se,(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)));b(this,ie,e=>[0,1].includes(this.code)?Promise.resolve(this.data):Promise.resolve(e(n(this,R))));b(this,oe,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=a,N(this,R,new Proxy(this,n(this,H)))}static[Symbol.hasInstance](e){return(e==null?void 0:e.constructor)===ue||(e==null?void 0:e[Symbol.for("HttpResponse")])===!0}};R=new WeakMap,Y=new WeakMap,G=new WeakMap,H=new WeakMap,ne=new WeakMap,Q=new WeakMap,se=new WeakMap,ie=new WeakMap,oe=new WeakMap;let J=ue;const ee=(s,e,t,r="/",i="")=>new J(s,e,t,r,i);var he=["utf8","utf-8","unicode-1-1-utf-8"];function we(s){if(he.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 a=0;a"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),a=new Array(i.length);for(let g=0;g>>16,2246822507),t^=Math.imul(r^r>>>13,3266489909),r=Math.imul(r^r>>>16,2246822507),r^=Math.imul(t^t>>>13,3266489909),(4294967296*(2097151&r)+(t>>>0)).toString(16)}function de(s,e){return s.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ge(s){return Ee.encode(s||"")}function Pe(s){return Te.decode(new DataView(s),{})}const Se=62,te=30,pe=31,Oe=63;class me{constructor(e){f(this,"encodeTable",[]);f(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==Se)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>pe)&&(r=5),i&=pe),t.push(n(this,W).encodeTable[i].charCodeAt(0)),this.pos-=r}return t}}W=new WeakMap;class Me{constructor(e){b(this,X);N(this,X,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let r=t.length,i=0,a=0;for(let g=0;g=8&&(r--,t[r]=a&255,i%=8,a>>=8)}return i>0&&(r--,t[r]=a&255),t.slice(r)}}X=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){b(this,q);b(this,K);b(this,ae);b(this,$,{"#":new Map,$:new Map});b(this,F,new Map);b(this,L,new Set);f(this,"init",(e,t,r)=>{e&&N(this,ae,e),r&&N(this,q,r),t&&N(this,K,new me(t)),n(this,ce).call(this)});b(this,ce,()=>{!n(this,L).size||!n(this,q)||(n(this,L).forEach(e=>n(this,Z).call(this,...e)),n(this,L).clear())});b(this,Z,(e,t,r,i=!1)=>{if(!n(this,q))n(this,L).add([e,t,r,i]);else{const{url:a,method:g,...B}=r||{};n(this,q).call(this,{url:a,method:g,...B}).then(u=>{if(![0,1].includes(u.code))throw ee(u.code??-1,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 m=u;return(t=="#"||g=="GET")&&n(this,$)[t].set(e,m),t!="#"&&g=="GET"&&!i&&setTimeout(()=>{n(this,$)[t].delete(e)},300),m}).then(u=>{var m;return(m=n(this,F).get(e))==null?void 0:m.forEach((U,A,D)=>{U&&U[0](u),delete D[A]})}).catch(u=>{var m;n(this,$)[t].delete(e),(m=n(this,F).get(e))==null||m.forEach((U,A,D)=>{U&&U[1](u instanceof J?u:ee(-1,u,null,a)),delete D[A]})}).finally(()=>n(this,F).delete(e))}});f(this,"request",({url:e="/",method:t="GET",...r},i=!1)=>{var A,D;e=de(e,"/");const a=t.toUpperCase(),g=fe(JSON.stringify([e,a,r])),B=(A=e==null?void 0:e.includes)!=null&&A.call(e,"/_/")?"#":"$",{promise:u,resolve:m,reject:U}=Promise.withResolvers();return n(this,$)[B].has(g)?(console.log("cache",e),m(n(this,$)[B].get(g))):n(this,F).has(g)?(D=n(this,F).get(g))==null||D.push([m,U]):(n(this,F).set(g,[[m,U]]),n(this,Z).call(this,g,B,{url:e,method:a,...r},i)),new Proxy(u,{get:(z,h)=>h===Symbol.for("HttpResponse")?!0:(...p)=>z.then(l=>ee(...l)[h](...p)).catch(l=>{if(!(l instanceof J))throw l;return typeof l[h]=="function"?l[h](...p):l[h]}),set:(z,h)=>{throw new TypeError(`request() 返回值是只读的,不能设置属性 "${String(h)}"`)}})});f(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));f(this,"post",(e,t={},r={})=>this.request({url:e,method:"POST",data:t,params:r}));f(this,"put",(e,t={},r={})=>this.request({url:e,method:"PUT",data:t,params:r}));f(this,"del",(e,t={},r={})=>this.request({url:e,method:"DELETE",data:t,params:r}));f(this,"download",(e,t)=>{});f(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));f(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));f(this,"getAppInfo",()=>{});f(this,"refreshCache",(e=!1)=>{e&&n(this,$)["#"].clear(),n(this,$).$.clear()});f(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}});f(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)}}q=new WeakMap,K=new WeakMap,ae=new WeakMap,$=new WeakMap,F=new WeakMap,L=new WeakMap,ce=new WeakMap,Z=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=fe,o.str2uint8array=ge,o.trim=de,o.uint8array2str=Pe,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); diff --git a/package.json b/package.json index 55cf946..0411883 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ff-request", - "version": "0.12.2", + "version": "0.12.3", "type": "module", "main": "./ff-request.umd.cjs", "module": "./ff-request.js", diff --git a/types/util.d.ts b/types/util.d.ts index 33f808b..7c392a4 100644 --- a/types/util.d.ts +++ b/types/util.d.ts @@ -1,9 +1,10 @@ /** - * DJB2 算法 —— 将字符串散列为十六进制字符串 + * cyrb53 算法 —— 将字符串散列为十六进制字符串 * @param {string} input + * @param {number} [seed=0] * @returns {string} */ -export function signature(input: string): string; +export function signature(input: string, seed?: number): string; /** * 去除字符串首尾指定字符 * @param {string} str