@whitesev/utils 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1582,7 +1582,7 @@ System.register('Utils', [], (function (exports) {
1582
1582
  return uuid;
1583
1583
  }
1584
1584
  else {
1585
- console.warn("HttpxRequestHook.addBeforeRequestCallBack: fn is not a function");
1585
+ console.warn("[Httpx-HttpxRequestHook.addBeforeRequestCallBack] fn is not a function");
1586
1586
  }
1587
1587
  },
1588
1588
  /**
@@ -2061,65 +2061,81 @@ System.register('Utils', [], (function (exports) {
2061
2061
  */
2062
2062
  onLoad(details, resolve, reject, argumentsList) {
2063
2063
  /* X浏览器会因为设置了responseType导致不返回responseText */
2064
- let Response = argumentsList[0];
2064
+ let originResponse = argumentsList[0];
2065
2065
  /* responseText为空,response不为空的情况 */
2066
- if (utils.isNull(Response["responseText"]) &&
2067
- utils.isNotNull(Response["response"])) {
2068
- if (typeof Response["response"] === "object") {
2066
+ if (utils.isNull(originResponse["responseText"]) &&
2067
+ utils.isNotNull(originResponse["response"])) {
2068
+ if (typeof originResponse["response"] === "object") {
2069
2069
  utils.tryCatch().run(() => {
2070
- Response["responseText"] = JSON.stringify(Response["response"]);
2070
+ originResponse["responseText"] = JSON.stringify(originResponse["response"]);
2071
2071
  });
2072
2072
  }
2073
2073
  else {
2074
- Response["responseText"] = Response["response"];
2074
+ originResponse["responseText"] = originResponse["response"];
2075
2075
  }
2076
2076
  }
2077
2077
  /* response为空,responseText不为空的情况 */
2078
- if (Response["response"] == null &&
2079
- typeof Response["responseText"] === "string" &&
2080
- Response["responseText"].trim() !== "") {
2081
- let newResponse = Response["responseText"];
2078
+ if (originResponse["response"] == null &&
2079
+ typeof originResponse["responseText"] === "string" &&
2080
+ originResponse["responseText"].trim() !== "") {
2081
+ /** 原始的请求text */
2082
+ let httpxResponseText = originResponse.responseText;
2083
+ // 自定义个新的response
2084
+ let httpxResponse = httpxResponseText;
2082
2085
  if (details.responseType === "json") {
2083
- newResponse = utils.toJSON(Response["responseText"]);
2086
+ httpxResponse = utils.toJSON(httpxResponseText);
2084
2087
  }
2085
2088
  else if (details.responseType === "document") {
2086
2089
  let parser = new DOMParser();
2087
- newResponse = parser.parseFromString(Response["responseText"], "text/html");
2090
+ httpxResponse = parser.parseFromString(httpxResponseText, "text/html");
2088
2091
  }
2089
2092
  else if (details.responseType === "arraybuffer") {
2090
2093
  let encoder = new TextEncoder();
2091
- let arrayBuffer = encoder.encode(Response["responseText"]);
2092
- newResponse = arrayBuffer;
2094
+ let arrayBuffer = encoder.encode(httpxResponseText);
2095
+ httpxResponse = arrayBuffer;
2093
2096
  }
2094
2097
  else if (details.responseType === "blob") {
2095
2098
  let encoder = new TextEncoder();
2096
- let arrayBuffer = encoder.encode(Response["responseText"]);
2097
- newResponse = new Blob([arrayBuffer]);
2098
- }
2099
- else {
2100
- newResponse = Response["responseText"];
2099
+ let arrayBuffer = encoder.encode(httpxResponseText);
2100
+ httpxResponse = new Blob([arrayBuffer]);
2101
2101
  }
2102
+ // 尝试覆盖原response
2102
2103
  try {
2103
- Response["response"] = newResponse;
2104
+ let setStatus = Reflect.set(originResponse, "response", httpxResponse);
2105
+ if (!setStatus) {
2106
+ console.warn("[Httpx-HttpxCallBack.oonLoad] 覆盖原始 response 失败,尝试添加新的httpxResponse");
2107
+ try {
2108
+ Reflect.set(originResponse, "httpxResponse", httpxResponse);
2109
+ }
2110
+ catch (error) {
2111
+ console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
2112
+ }
2113
+ }
2104
2114
  }
2105
2115
  catch (error) {
2106
- console.warn("response 无法被覆盖");
2116
+ console.warn("[Httpx-HttpxCallBack.oonLoad] 原始 response 无法被覆盖,尝试添加新的httpxResponse");
2117
+ try {
2118
+ Reflect.set(originResponse, "httpxResponse", httpxResponse);
2119
+ }
2120
+ catch (error) {
2121
+ console.warn("[Httpx-HttpxCallBack.oonLoad] httpxResponse 无法被覆盖");
2122
+ }
2107
2123
  }
2108
2124
  }
2109
2125
  /* Stay扩展中没有finalUrl,对应的是responseURL */
2110
- if (Response["finalUrl"] == null &&
2111
- Response["responseURL"] != null) {
2112
- Response["finalUrl"] = Response["responseURL"];
2126
+ let originResponseURL = Reflect.get(originResponse, "responseURL");
2127
+ if (originResponse["finalUrl"] == null && originResponseURL != null) {
2128
+ Reflect.set(originResponse, "finalUrl", originResponseURL);
2113
2129
  }
2114
2130
  /* 状态码2xx都是成功的 */
2115
- if (Math.floor(Response.status / 100) === 2) {
2116
- if (this.context.HttpxResponseHook.successResponseCallBack(Response, details) == null) {
2131
+ if (Math.floor(originResponse.status / 100) === 2) {
2132
+ if (this.context.HttpxResponseHook.successResponseCallBack(originResponse, details) == null) {
2117
2133
  // reject(new TypeError("response is intercept with onloada"));
2118
2134
  return;
2119
2135
  }
2120
2136
  resolve({
2121
2137
  status: true,
2122
- data: Response,
2138
+ data: originResponse,
2123
2139
  details: details,
2124
2140
  msg: "请求完毕",
2125
2141
  type: "onload",
@@ -2164,7 +2180,7 @@ System.register('Utils', [], (function (exports) {
2164
2180
  */
2165
2181
  request(details) {
2166
2182
  if (this.context.#LOG_DETAILS) {
2167
- console.log("Httpx请求配置👇", details);
2183
+ console.log("[Httpx-HttpxRequest.request] 请求前的配置👇", details);
2168
2184
  }
2169
2185
  if (typeof this.context.HttpxRequestHook.beforeRequestCallBack ===
2170
2186
  "function") {
@@ -2197,33 +2213,38 @@ System.register('Utils', [], (function (exports) {
2197
2213
  */
2198
2214
  fetch(details, fetchRequestInit, abortController) {
2199
2215
  fetch(details.url, fetchRequestInit)
2200
- .then(async (resp) => {
2201
- /**
2202
- * @type {HttpxAsyncResultData}
2203
- */
2216
+ .then(async (fetchResponse) => {
2217
+ /** 自定义的response */
2204
2218
  let httpxResponse = {
2205
2219
  isFetch: true,
2206
- finalUrl: resp.url,
2220
+ finalUrl: fetchResponse.url,
2207
2221
  readyState: 4,
2208
- status: resp.status,
2209
- statusText: resp.statusText,
2222
+ // @ts-ignore
2223
+ status: fetchResponse.status,
2224
+ statusText: fetchResponse.statusText,
2210
2225
  response: void 0,
2211
- responseFetchHeaders: resp.headers,
2226
+ responseFetchHeaders: fetchResponse.headers,
2212
2227
  responseHeaders: "",
2228
+ // @ts-ignore
2213
2229
  responseText: void 0,
2214
2230
  responseType: details.responseType,
2215
2231
  responseXML: void 0,
2216
2232
  };
2217
2233
  Object.assign(httpxResponse, details.context || {});
2218
- for (const [key, value] of resp.headers.entries()) {
2234
+ // headers转为字符串
2235
+ for (const [key, value] of fetchResponse.headers.entries()) {
2219
2236
  httpxResponse.responseHeaders += `${key}: ${value}\n`;
2220
2237
  }
2221
- /* 如果是流式传输,直接返回 */
2238
+ /** 请求返回的类型 */
2239
+ const fetchResponseType = fetchResponse.headers.get("Content-Type");
2240
+ /* 如果需要stream,且获取到的是stream,那直接返回 */
2222
2241
  if (details.responseType === "stream" ||
2223
- (resp.headers.has("Content-Type") &&
2224
- resp.headers.get("Content-Type").includes("text/event-stream"))) {
2225
- httpxResponse["isStream"] = true;
2226
- httpxResponse.response = resp.body;
2242
+ (fetchResponse.headers.has("Content-Type") &&
2243
+ fetchResponse.headers
2244
+ .get("Content-Type")
2245
+ .includes("text/event-stream"))) {
2246
+ Reflect.set(httpxResponse, "isStream", true);
2247
+ Reflect.set(httpxResponse, "response", fetchResponse.body);
2227
2248
  Reflect.deleteProperty(httpxResponse, "responseText");
2228
2249
  Reflect.deleteProperty(httpxResponse, "responseXML");
2229
2250
  details.onload(httpxResponse);
@@ -2235,42 +2256,57 @@ System.register('Utils', [], (function (exports) {
2235
2256
  let responseText = "";
2236
2257
  /** 响应xml文档 */
2237
2258
  let responseXML = "";
2238
- let arrayBuffer = await resp.arrayBuffer();
2259
+ /** 先获取二进制数据 */
2260
+ let arrayBuffer = await fetchResponse.arrayBuffer();
2261
+ /** 数据编码 */
2239
2262
  let encoding = "utf-8";
2240
- if (resp.headers.has("Content-Type")) {
2241
- let charsetMatched = resp.headers
2263
+ if (fetchResponse.headers.has("Content-Type")) {
2264
+ let charsetMatched = fetchResponse.headers
2242
2265
  .get("Content-Type")
2243
2266
  ?.match(/charset=(.+)/);
2244
2267
  if (charsetMatched) {
2245
2268
  encoding = charsetMatched[1];
2269
+ encoding = encoding.toLowerCase();
2246
2270
  }
2247
2271
  }
2272
+ // Failed to construct 'TextDecoder': The encoding label provided ('"UTF-8"') is invalid.
2273
+ // 去除引号
2274
+ encoding = encoding.replace(/('|")/gi, "");
2275
+ // 编码
2248
2276
  let textDecoder = new TextDecoder(encoding);
2249
2277
  responseText = textDecoder.decode(arrayBuffer);
2250
2278
  response = responseText;
2251
2279
  if (details.responseType === "arraybuffer") {
2280
+ // response返回格式是二进制流
2252
2281
  response = arrayBuffer;
2253
2282
  }
2254
2283
  else if (details.responseType === "blob") {
2284
+ // response返回格式是blob
2255
2285
  response = new Blob([arrayBuffer]);
2256
2286
  }
2287
+ else if (details.responseType === "json" ||
2288
+ (typeof fetchResponseType === "string" &&
2289
+ fetchResponseType.includes("application/json"))) {
2290
+ // response返回格式是JSON格式
2291
+ response = utils.toJSON(responseText);
2292
+ }
2257
2293
  else if (details.responseType === "document" ||
2258
2294
  details.responseType == null) {
2295
+ // response返回格式是文档格式
2259
2296
  let parser = new DOMParser();
2260
2297
  response = parser.parseFromString(responseText, "text/html");
2261
2298
  }
2262
- else if (details.responseType === "json") {
2263
- response = utils.toJSON(responseText);
2264
- }
2299
+ // 转为XML结构
2265
2300
  let parser = new DOMParser();
2266
2301
  responseXML = parser.parseFromString(responseText, "text/xml");
2267
- httpxResponse.response = response;
2268
- httpxResponse.responseText = responseText;
2269
- httpxResponse.responseXML = responseXML;
2302
+ Reflect.set(httpxResponse, "response", response);
2303
+ Reflect.set(httpxResponse, "responseText", responseText);
2304
+ Reflect.set(httpxResponse, "responseXML", responseXML);
2305
+ // 执行回调
2270
2306
  details.onload(httpxResponse);
2271
2307
  })
2272
- .catch((err) => {
2273
- if (err.name === "AbortError") {
2308
+ .catch((error) => {
2309
+ if (error.name === "AbortError") {
2274
2310
  return;
2275
2311
  }
2276
2312
  details.onerror({
@@ -2281,7 +2317,7 @@ System.register('Utils', [], (function (exports) {
2281
2317
  statusText: "",
2282
2318
  responseHeaders: "",
2283
2319
  responseText: "",
2284
- error: err,
2320
+ error: error,
2285
2321
  });
2286
2322
  });
2287
2323
  details.onloadstart({
@@ -2344,7 +2380,7 @@ System.register('Utils', [], (function (exports) {
2344
2380
  */
2345
2381
  constructor(__xmlHttpRequest__) {
2346
2382
  if (typeof __xmlHttpRequest__ !== "function") {
2347
- console.warn("Httpx未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,强制使用window.fetch");
2383
+ console.warn("[Httpx-constructor] 未传入GM_xmlhttpRequest函数或传入的GM_xmlhttpRequest不是Function,将默认使用window.fetch");
2348
2384
  }
2349
2385
  this.interceptors.request.context = this;
2350
2386
  this.interceptors.response.context = this;
@@ -6408,13 +6444,29 @@ System.register('Utils', [], (function (exports) {
6408
6444
  * > ()=>{throw new Error('测试错误')}出现错误
6409
6445
  */
6410
6446
  tryCatch = TryCatch;
6411
- uniqueArray(uniqueArrayData = [], compareArrayData = [], compareFun = (item, item2) => {
6412
- // @ts-ignore
6447
+ uniqueArray(uniqueArrayData = [], compareArrayData, compareFun = (item, item2) => {
6413
6448
  return item === item2;
6414
6449
  }) {
6415
- return Array.from(uniqueArrayData).filter((item) => !Array.from(compareArrayData).some(function (item2) {
6416
- return compareFun(item, item2);
6417
- }));
6450
+ if (typeof compareArrayData === "function") {
6451
+ const compareFn = compareArrayData;
6452
+ const seen = new Set();
6453
+ const result = [];
6454
+ for (const item of uniqueArrayData) {
6455
+ // 使用compareFn函数来获取当前对象的唯一标识
6456
+ const identfier = compareFn(item);
6457
+ // 如果Set中还没有这个标识,则添加到结果数组中,并将其标识存入Set
6458
+ if (!seen.has(identfier)) {
6459
+ seen.add(identfier);
6460
+ result.push(item);
6461
+ }
6462
+ }
6463
+ return result;
6464
+ }
6465
+ else {
6466
+ return Array.from(uniqueArrayData).filter((item) => !Array.from(compareArrayData).some(function (item2) {
6467
+ return compareFun(item, item2);
6468
+ }));
6469
+ }
6418
6470
  }
6419
6471
  waitArrayLoopToEnd(data, handleFunc) {
6420
6472
  let UtilsContext = this;