@nsnanocat/util 0.0.0-preview-ce381a0 → 0.0.0-preview-1422296

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.
Files changed (39) hide show
  1. package/dist/getStorage.mjs +64 -47
  2. package/dist/index.d.ts +3 -11
  3. package/dist/index.js +3 -11
  4. package/dist/lib/app.mjs +25 -9
  5. package/dist/lib/done.mjs +58 -48
  6. package/dist/lib/environment.mjs +15 -16
  7. package/dist/lib/index.d.mts +5 -0
  8. package/dist/lib/index.mjs +5 -5
  9. package/dist/lib/notification.mjs +50 -29
  10. package/dist/lib/runScript.mjs +27 -18
  11. package/dist/lib/time.mjs +17 -4
  12. package/dist/lib/wait.mjs +5 -3
  13. package/dist/polyfill/Console.mjs +171 -145
  14. package/dist/polyfill/Lodash.mjs +39 -29
  15. package/dist/polyfill/StatusTexts.mjs +2 -3
  16. package/dist/polyfill/Storage.d.mts +12 -0
  17. package/dist/polyfill/Storage.mjs +167 -173
  18. package/dist/polyfill/fetch-node.d.mts +3 -0
  19. package/dist/polyfill/fetch-node.mjs +2 -2
  20. package/dist/polyfill/fetch.mjs +115 -79
  21. package/dist/polyfill/index.d.mts +5 -0
  22. package/dist/polyfill/index.mjs +5 -5
  23. package/package.json +4 -4
  24. package/dist/lib/index.d.ts +0 -5
  25. package/dist/polyfill/Storage.d.ts +0 -8
  26. package/dist/polyfill/fetch-node.d.ts +0 -1
  27. package/dist/polyfill/index.d.ts +0 -5
  28. /package/dist/{getStorage.d.ts → getStorage.d.mts} +0 -0
  29. /package/dist/lib/{app.d.ts → app.d.mts} +0 -0
  30. /package/dist/lib/{done.d.ts → done.d.mts} +0 -0
  31. /package/dist/lib/{environment.d.ts → environment.d.mts} +0 -0
  32. /package/dist/lib/{notification.d.ts → notification.d.mts} +0 -0
  33. /package/dist/lib/{runScript.d.ts → runScript.d.mts} +0 -0
  34. /package/dist/lib/{time.d.ts → time.d.mts} +0 -0
  35. /package/dist/lib/{wait.d.ts → wait.d.mts} +0 -0
  36. /package/dist/polyfill/{Console.d.ts → Console.d.mts} +0 -0
  37. /package/dist/polyfill/{Lodash.d.ts → Lodash.d.mts} +0 -0
  38. /package/dist/polyfill/{StatusTexts.d.ts → StatusTexts.d.mts} +0 -0
  39. /package/dist/polyfill/{fetch.d.ts → fetch.d.mts} +0 -0
@@ -1,188 +1,182 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var _StorageClass_instances, _StorageClass_getNodeModule, _StorageClass_loadData, _StorageClass_writeData;
7
+ import { get, set, unset } from './Lodash.mjs';
1
8
  import { $app } from '../lib/app.mjs';
2
- import { Lodash as _ } from './Lodash.mjs';
3
- export class Storage {
4
- static data = null;
5
- static dataFile = 'box.dat';
6
- static #nameRegex = /^@(?<key>[^.]+)(?:\.(?<path>.*))?$/;
7
- static getItem(keyName, defaultValue = null) {
8
- let keyValue1 = defaultValue;
9
- switch(keyName.startsWith('@')){
10
- case true:
11
- {
12
- const { key, path } = keyName.match(Storage.#nameRegex)?.groups;
13
- keyName = key;
14
- let value = Storage.getItem(keyName, {});
15
- if ('object' != typeof value) value = {};
16
- keyValue1 = _.get(value, path);
17
- try {
18
- keyValue1 = JSON.parse(keyValue1);
19
- } catch (e) {}
20
- break;
21
- }
22
- default:
23
- switch($app){
24
- case 'Surge':
25
- case 'Loon':
26
- case 'Stash':
27
- case 'Egern':
28
- case 'Shadowrocket':
29
- keyValue1 = $persistentStore.read(keyName);
30
- break;
31
- case 'Quantumult X':
32
- keyValue1 = $prefs.valueForKey(keyName);
33
- break;
34
- case 'Node.js':
35
- Storage.data = Storage.#loaddata(Storage.dataFile);
36
- keyValue1 = Storage.data?.[keyName];
37
- break;
38
- default:
39
- keyValue1 = Storage.data?.[keyName] || null;
40
- break;
41
- }
42
- try {
43
- keyValue1 = JSON.parse(keyValue1);
44
- } catch (e) {}
45
- break;
9
+ export class StorageClass {
10
+ constructor() {
11
+ _StorageClass_instances.add(this);
12
+ Object.defineProperty(this, "data", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: null
17
+ });
18
+ Object.defineProperty(this, "dataFile", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: 'box.dat'
23
+ });
24
+ Object.defineProperty(this, "nameRegex", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: /^@(?<key>[^.]+)(?:\.(?<path>.*))?$/
29
+ });
30
+ if ($app === 'Node.js') {
31
+ this.data = __classPrivateFieldGet(this, _StorageClass_instances, "m", _StorageClass_loadData).call(this, this.dataFile);
46
32
  }
47
- return keyValue1 ?? defaultValue;
48
33
  }
49
- static setItem(keyName = new String(), keyValue1 = new String()) {
50
- let result = false;
51
- switch(typeof keyValue1){
52
- case 'object':
53
- keyValue1 = JSON.stringify(keyValue1);
54
- break;
55
- default:
56
- keyValue1 = String(keyValue1);
57
- break;
58
- }
59
- switch(keyName.startsWith('@')){
60
- case true:
61
- {
62
- const { key, path } = keyName.match(Storage.#nameRegex)?.groups;
63
- keyName = key;
64
- let value = Storage.getItem(keyName, {});
65
- if ('object' != typeof value) value = {};
66
- _.set(value, path, keyValue1);
67
- result = Storage.setItem(keyName, value);
68
- break;
34
+ getItem(keyName, defaultValue = null) {
35
+ let keyValue = defaultValue;
36
+ if (keyName.startsWith('@')) {
37
+ const { key, path } = keyName.match(this.nameRegex)?.groups || {};
38
+ if (key) {
39
+ let value = this.getItem(key, {});
40
+ if (typeof value !== 'object') {
41
+ value = {};
69
42
  }
70
- default:
71
- switch($app){
72
- case 'Surge':
73
- case 'Loon':
74
- case 'Stash':
75
- case 'Egern':
76
- case 'Shadowrocket':
77
- result = $persistentStore.write(keyValue1, keyName);
78
- break;
79
- case 'Quantumult X':
80
- result = $prefs.setValueForKey(keyValue1, keyName);
81
- break;
82
- case 'Node.js':
83
- Storage.data = Storage.#loaddata(Storage.dataFile);
84
- Storage.data[keyName] = keyValue1;
85
- Storage.#writedata(Storage.dataFile);
86
- result = true;
87
- break;
88
- default:
89
- result = Storage.data?.[keyName] || null;
90
- break;
43
+ keyValue = get(value, path);
44
+ try {
45
+ keyValue = JSON.parse(keyValue);
46
+ }
47
+ catch {
48
+ // Ignore parse error
91
49
  }
92
- break;
50
+ }
93
51
  }
94
- return result;
95
- }
96
- static removeItem(keyName) {
97
- let result = false;
98
- switch(keyName.startsWith('@')){
99
- case true:
100
- {
101
- const { key, path } = keyName.match(Storage.#nameRegex)?.groups;
102
- keyName = key;
103
- let value = Storage.getItem(keyName);
104
- if ('object' != typeof value) value = {};
105
- keyValue = _.unset(value, path);
106
- result = Storage.setItem(keyName, value);
52
+ else {
53
+ switch ($app) {
54
+ case 'Surge':
55
+ case 'Loon':
56
+ case 'Stash':
57
+ case 'Egern':
58
+ case 'Shadowrocket':
59
+ keyValue = $persistentStore.read(keyName);
107
60
  break;
108
- }
109
- default:
110
- switch($app){
111
- case 'Surge':
112
- case 'Loon':
113
- case 'Stash':
114
- case 'Egern':
115
- case 'Shadowrocket':
116
- result = false;
117
- break;
118
- case 'Quantumult X':
119
- result = $prefs.removeValueForKey(keyName);
120
- break;
121
- case 'Node.js':
122
- result = false;
123
- break;
124
- default:
125
- result = false;
126
- break;
127
- }
128
- break;
61
+ case 'Quantumult X':
62
+ keyValue = $prefs.valueForKey(keyName);
63
+ break;
64
+ case 'Node.js':
65
+ this.data = this.data || {};
66
+ keyValue = this.data[keyName];
67
+ break;
68
+ default:
69
+ keyValue = null;
70
+ break;
71
+ }
72
+ try {
73
+ keyValue = JSON.parse(keyValue);
74
+ }
75
+ catch {
76
+ // Ignore parse error
77
+ }
129
78
  }
130
- return result;
79
+ return keyValue ?? defaultValue;
131
80
  }
132
- static clear() {
133
- let result = false;
134
- switch($app){
135
- case 'Surge':
136
- case 'Loon':
137
- case 'Stash':
138
- case 'Egern':
139
- case 'Shadowrocket':
140
- result = false;
141
- break;
142
- case 'Quantumult X':
143
- result = $prefs.removeAllValues();
144
- break;
145
- case 'Node.js':
146
- result = false;
147
- break;
148
- default:
149
- result = false;
150
- break;
81
+ setItem(keyName, value) {
82
+ let keyValue = value;
83
+ if (typeof keyValue === 'object') {
84
+ keyValue = JSON.stringify(keyValue);
85
+ }
86
+ else {
87
+ keyValue = String(keyValue);
88
+ }
89
+ if (keyName.startsWith('@')) {
90
+ const { key, path } = keyName.match(this.nameRegex)?.groups || {};
91
+ if (key) {
92
+ let value = this.getItem(key, {});
93
+ if (typeof value !== 'object')
94
+ value = {};
95
+ set(value, path, keyValue);
96
+ return this.setItem(keyName, value);
97
+ }
151
98
  }
152
- return result;
99
+ else {
100
+ switch ($app) {
101
+ case 'Surge':
102
+ case 'Loon':
103
+ case 'Stash':
104
+ case 'Egern':
105
+ case 'Shadowrocket':
106
+ return $persistentStore.write(keyValue, keyName);
107
+ case 'Quantumult X':
108
+ return $prefs.setValueForKey(keyValue, keyName);
109
+ case 'Node.js':
110
+ this.data = this.data || {};
111
+ this.data[keyName] = keyValue;
112
+ __classPrivateFieldGet(this, _StorageClass_instances, "m", _StorageClass_writeData).call(this, this.dataFile);
113
+ return true;
114
+ default:
115
+ return false;
116
+ }
117
+ }
118
+ return false;
153
119
  }
154
- static #loaddata = (dataFile)=>{
155
- if ('Node.js' !== $app) return {};
156
- {
157
- this.fs = this.fs ? this.fs : require('node:fs');
158
- this.path = this.path ? this.path : require('node:path');
159
- const curDirDataFilePath = this.path.resolve(dataFile);
160
- const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
161
- const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
162
- const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
163
- if (!isCurDirDataFile && !isRootDirDataFile) return {};
164
- {
165
- const datPath = isCurDirDataFile ? curDirDataFilePath : rootDirDataFilePath;
166
- try {
167
- return JSON.parse(this.fs.readFileSync(datPath));
168
- } catch (e) {
169
- return {};
170
- }
120
+ removeItem(keyName) {
121
+ if (keyName.startsWith('@')) {
122
+ const { key, path } = keyName.match(this.nameRegex)?.groups || {};
123
+ if (key) {
124
+ let value = this.getItem(key);
125
+ if (typeof value !== 'object')
126
+ value = {};
127
+ unset(value, path);
128
+ return this.setItem(key, value);
171
129
  }
172
130
  }
173
- };
174
- static #writedata = (dataFile = this.dataFile)=>{
175
- if ('Node.js' === $app) {
176
- this.fs = this.fs ? this.fs : require('node:fs');
177
- this.path = this.path ? this.path : require('node:path');
178
- const curDirDataFilePath = this.path.resolve(dataFile);
179
- const rootDirDataFilePath = this.path.resolve(process.cwd(), dataFile);
180
- const isCurDirDataFile = this.fs.existsSync(curDirDataFilePath);
181
- const isRootDirDataFile = !isCurDirDataFile && this.fs.existsSync(rootDirDataFilePath);
182
- const jsondata = JSON.stringify(this.data);
183
- if (isCurDirDataFile) this.fs.writeFileSync(curDirDataFilePath, jsondata);
184
- else if (isRootDirDataFile) this.fs.writeFileSync(rootDirDataFilePath, jsondata);
185
- else this.fs.writeFileSync(curDirDataFilePath, jsondata);
131
+ else {
132
+ switch ($app) {
133
+ case 'Quantumult X':
134
+ return $prefs.removeValueForKey(keyName);
135
+ default:
136
+ return false;
137
+ }
186
138
  }
187
- };
139
+ return false;
140
+ }
141
+ clear() {
142
+ switch ($app) {
143
+ case 'Quantumult X':
144
+ return $prefs.removeAllValues();
145
+ default:
146
+ return false;
147
+ }
148
+ }
188
149
  }
150
+ _StorageClass_instances = new WeakSet(), _StorageClass_getNodeModule = function _StorageClass_getNodeModule() {
151
+ if ($app === 'Node.js') {
152
+ // biome-ignore lint/style/useNodejsImportProtocol: <explanation>
153
+ const fs = require('fs');
154
+ // biome-ignore lint/style/useNodejsImportProtocol: <explanation>
155
+ const path = require('path');
156
+ return { fs, path };
157
+ }
158
+ return null;
159
+ }, _StorageClass_loadData = function _StorageClass_loadData(dataFile) {
160
+ const { fs, path } = __classPrivateFieldGet(this, _StorageClass_instances, "m", _StorageClass_getNodeModule).call(this) ?? {};
161
+ if (!fs || !path) {
162
+ return {};
163
+ }
164
+ const curDirDataFilePath = path.resolve(dataFile);
165
+ const rootDirDataFilePath = path.resolve(process.cwd(), dataFile);
166
+ if (fs.existsSync(curDirDataFilePath)) {
167
+ return JSON.parse(fs.readFileSync(curDirDataFilePath, 'utf-8')) || {};
168
+ }
169
+ if (fs.existsSync(rootDirDataFilePath)) {
170
+ return JSON.parse(fs.readFileSync(rootDirDataFilePath, 'utf-8')) || {};
171
+ }
172
+ return {};
173
+ }, _StorageClass_writeData = function _StorageClass_writeData(dataFile) {
174
+ const { fs, path } = __classPrivateFieldGet(this, _StorageClass_instances, "m", _StorageClass_getNodeModule).call(this) ?? {};
175
+ if (!fs || !path) {
176
+ return;
177
+ }
178
+ const dataFilePath = path.resolve(dataFile);
179
+ fs.writeFileSync(dataFilePath, JSON.stringify(this.data), 'utf-8');
180
+ };
181
+ // 导出初始化后的实例
182
+ export const Storage = new StorageClass();
@@ -0,0 +1,3 @@
1
+ export declare const getNodeFetch: () => {
2
+ fetch: any;
3
+ };
@@ -1,8 +1,8 @@
1
- export const getNodeFetch = ()=>{
1
+ export const getNodeFetch = () => {
2
2
  const nodeFetch = globalThis.fetch ? globalThis.fetch : require('node-fetch');
3
3
  const fetchCookie = globalThis.fetchCookie ? globalThis.fetchCookie : require('fetch-cookie').default;
4
4
  const fetch = fetchCookie(nodeFetch);
5
5
  return {
6
- fetch
6
+ fetch,
7
7
  };
8
8
  };
@@ -1,6 +1,7 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__Lodash_mjs__ from "./Lodash.mjs";
2
- import * as __WEBPACK_EXTERNAL_MODULE__lib_app_mjs__ from "../lib/app.mjs";
3
- import * as __WEBPACK_EXTERNAL_MODULE__StatusTexts_mjs__ from "./StatusTexts.mjs";
1
+ import { set } from './Lodash.mjs';
2
+ import { $app } from '../lib/app.mjs';
3
+ import { StatusTexts } from './StatusTexts.mjs';
4
+ // 定义需要二进制模式的 MIME 类型
4
5
  const binaryMimeTypes = [
5
6
  'application/protobuf',
6
7
  'application/x-protobuf',
@@ -8,83 +9,104 @@ const binaryMimeTypes = [
8
9
  'application/vnd.apple.flatbuffer',
9
10
  'application/grpc',
10
11
  'application/grpc+proto',
11
- 'application/octet-stream'
12
+ 'application/octet-stream',
12
13
  ];
13
- async function fetch_rslib_entry_fetch(resource, options = {
14
- timeout: 5
14
+ export async function fetch(resource, options = {
15
+ timeout: 5,
15
16
  }) {
16
- var _split, _this, _params_headers, _params_headers1;
17
- let params = {
18
- ...options
19
- };
20
- if ('string' == typeof resource) params.url = resource;
21
- else if ('object' == typeof resource) params = {
22
- ...params,
23
- ...resource
24
- };
25
- else throw new TypeError(`${Function.name}: 参数类型错误, resource 必须为对象或字符串`);
17
+ let params = { ...options };
18
+ // 初始化参数
19
+ if (typeof resource === 'string') {
20
+ params.url = resource;
21
+ }
22
+ else if (typeof resource === 'object') {
23
+ params = { ...params, ...resource };
24
+ }
25
+ else {
26
+ throw new TypeError(`${Function.name}: 参数类型错误, resource 必须为对象或字符串`);
27
+ }
28
+ // 自动判断请求方法
26
29
  if (!params.method) {
27
30
  params.method = 'GET';
28
- if (params.body || params.bodyBytes) params.method = 'POST';
31
+ if (params.body || params.bodyBytes) {
32
+ params.method = 'POST';
33
+ }
29
34
  }
35
+ // 移除请求头中的部分参数, 让其自动生成
30
36
  if (params.headers) {
31
- params.headers.Host = void 0;
32
- params.headers[':authority'] = void 0;
33
- params.headers['Content-Length'] = void 0;
34
- params.headers['content-length'] = void 0;
37
+ params.headers.Host = undefined;
38
+ params.headers[':authority'] = undefined;
39
+ params.headers['Content-Length'] = undefined;
40
+ params.headers['content-length'] = undefined;
35
41
  }
42
+ // 定义请求方法(小写)
36
43
  const method = params.method.toLocaleLowerCase();
44
+ // 转换请求超时时间参数
37
45
  if (params.timeout) {
38
46
  params.timeout = Number.parseInt(params.timeout.toString(), 10);
39
- if (params.timeout > 500) params.timeout = Math.round(params.timeout / 1000);
47
+ // 转换为秒,大于500视为毫秒,小于等于500视为秒
48
+ if (params.timeout > 500) {
49
+ params.timeout = Math.round(params.timeout / 1000);
50
+ }
40
51
  }
41
- if ('Quantumult X' === __WEBPACK_EXTERNAL_MODULE__lib_app_mjs__.$app) {
42
- if (params.policy) (0, __WEBPACK_EXTERNAL_MODULE__Lodash_mjs__.set)(params, 'opts.policy', params.policy);
43
- if ('boolean' == typeof params['auto-redirect']) (0, __WEBPACK_EXTERNAL_MODULE__Lodash_mjs__.set)(params, 'opts.redirection', params['auto-redirect']);
52
+ if ($app === 'Quantumult X') {
53
+ // 转换请求参数
54
+ if (params.policy) {
55
+ set(params, 'opts.policy', params.policy);
56
+ }
57
+ if (typeof params['auto-redirect'] === 'boolean') {
58
+ set(params, 'opts.redirection', params['auto-redirect']);
59
+ }
60
+ // 转换请求体
44
61
  if (params.body instanceof ArrayBuffer) {
45
62
  params.bodyBytes = params.body;
46
- params.body = void 0;
47
- } else if (ArrayBuffer.isView(params.body)) {
63
+ params.body = undefined;
64
+ }
65
+ else if (ArrayBuffer.isView(params.body)) {
48
66
  params.bodyBytes = params.body.buffer.slice(params.body.byteOffset, params.body.byteLength + params.body.byteOffset);
49
- params.body = void 0;
50
- } else if (params.body) params.bodyBytes = void 0;
67
+ params.body = undefined;
68
+ }
69
+ else if (params.body) {
70
+ params.bodyBytes = undefined;
71
+ }
72
+ // 发送请求
51
73
  return Promise.race([
52
- await $task.fetch(params).then((response)=>{
53
- var _split, _this, _response_headers, _response_headers1;
74
+ await $task.fetch(params).then((response) => {
54
75
  response.ok = /^2\d\d$/.test(response.statusCode);
55
76
  response.status = response.statusCode;
56
- response.statusText = __WEBPACK_EXTERNAL_MODULE__StatusTexts_mjs__.StatusTexts[response.status];
57
- if (binaryMimeTypes.includes(null === (_this = (null === (_response_headers = response.headers) || void 0 === _response_headers ? void 0 : _response_headers['Content-Type']) ?? (null === (_response_headers1 = response.headers) || void 0 === _response_headers1 ? void 0 : _response_headers1['content-type'])) || void 0 === _this ? void 0 : null === (_split = _this.split(';')) || void 0 === _split ? void 0 : _split[0])) response.body = response.bodyBytes;
58
- response.bodyBytes = void 0;
77
+ response.statusText = StatusTexts[response.status];
78
+ if (binaryMimeTypes.includes((response.headers?.['Content-Type'] ?? response.headers?.['content-type'])?.split(';')?.[0])) {
79
+ response.body = response.bodyBytes;
80
+ }
81
+ response.bodyBytes = undefined;
59
82
  return response;
60
- }, (reason)=>Promise.reject(reason.error)),
61
- new Promise((_, reject)=>{
62
- setTimeout(()=>{
83
+ }, (reason) => Promise.reject(reason.error)),
84
+ new Promise((_, reject) => {
85
+ setTimeout(() => {
63
86
  reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
64
87
  }, params.timeout);
65
- })
88
+ }),
66
89
  ]);
67
90
  }
68
- if ('Node.js' === __WEBPACK_EXTERNAL_MODULE__lib_app_mjs__.$app) {
69
- const fetch = await import("./fetch-node.mjs").then((module)=>module.getNodeFetch().then((module)=>module.fetch));
91
+ if ($app === 'Node.js') {
92
+ const fetch = await import('./fetch-node.mjs').then((module) => module.getNodeFetch().fetch);
93
+ // 转换请求参数
70
94
  params.timeout = (params.timeout ?? 5) * 1000;
71
95
  params.redirect = params.redirection ? 'follow' : 'manual';
72
96
  const { url, ...options } = params;
97
+ // 发送请求
73
98
  return Promise.race([
74
- await fetch(url, options).then(async (response)=>{
99
+ await fetch(url, options)
100
+ .then(async (response) => {
75
101
  const bodyBytes = await response.arrayBuffer();
76
102
  let headers;
77
103
  try {
78
104
  headers = response.headers.raw();
79
- } catch {
80
- headers = Array.from(response.headers.entries()).reduce((acc, item)=>{
105
+ }
106
+ catch {
107
+ headers = Array.from(response.headers.entries()).reduce((acc, item) => {
81
108
  const [key, value] = item;
82
- acc[key] = acc[key] ? [
83
- ...acc[key],
84
- value
85
- ] : [
86
- value
87
- ];
109
+ acc[key] = acc[key] ? [...acc[key], value] : [value];
88
110
  return acc;
89
111
  }, {});
90
112
  }
@@ -95,53 +117,67 @@ async function fetch_rslib_entry_fetch(resource, options = {
95
117
  statusText: response.statusText,
96
118
  body: new TextDecoder('utf-8').decode(bodyBytes),
97
119
  bodyBytes: bodyBytes,
98
- headers: Object.fromEntries(Object.entries(headers).map(([key, value])=>[
99
- key,
100
- 'set-cookie' !== key.toLowerCase() ? value.toString() : value
101
- ]))
120
+ headers: Object.fromEntries(Object.entries(headers).map(([key, value]) => [
121
+ key,
122
+ key.toLowerCase() !== 'set-cookie' ? value.toString() : value,
123
+ ])),
102
124
  };
103
- }).catch((error)=>Promise.reject(error.message)),
104
- new Promise((resolve, reject)=>{
105
- setTimeout(()=>{
125
+ })
126
+ .catch((error) => Promise.reject(error.message)),
127
+ new Promise((resolve, reject) => {
128
+ setTimeout(() => {
106
129
  reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
107
130
  }, params.timeout);
108
- })
131
+ }),
109
132
  ]);
110
133
  }
111
- if ('Loon' === __WEBPACK_EXTERNAL_MODULE__lib_app_mjs__.$app) params.timeout *= 1000;
112
- if (params.policy) switch(__WEBPACK_EXTERNAL_MODULE__lib_app_mjs__.$app){
113
- case 'Loon':
114
- params.node = params.policy;
115
- break;
116
- case 'Stash':
117
- (0, __WEBPACK_EXTERNAL_MODULE__Lodash_mjs__.set)(params, 'headers.X-Stash-Selected-Proxy', encodeURI(params.policy));
118
- break;
119
- case 'Shadowrocket':
120
- (0, __WEBPACK_EXTERNAL_MODULE__Lodash_mjs__.set)(params, 'headers.X-Surge-Proxy', params.policy);
121
- break;
122
- default:
123
- break;
134
+ if ($app === 'Loon') {
135
+ params.timeout *= 1000;
136
+ }
137
+ if (params.policy) {
138
+ switch ($app) {
139
+ case 'Loon':
140
+ params.node = params.policy;
141
+ break;
142
+ case 'Stash':
143
+ set(params, 'headers.X-Stash-Selected-Proxy', encodeURI(params.policy));
144
+ break;
145
+ case 'Shadowrocket':
146
+ set(params, 'headers.X-Surge-Proxy', params.policy);
147
+ break;
148
+ default:
149
+ break;
150
+ }
124
151
  }
125
- if ('boolean' == typeof params.redirection) params['auto-redirect'] = params.redirection;
152
+ if (typeof params.redirection === 'boolean') {
153
+ params['auto-redirect'] = params.redirection;
154
+ }
155
+ // 转换请求体
126
156
  if (params.bodyBytes && !params.body) {
127
157
  params.body = params.bodyBytes;
128
- params.bodyBytes = void 0;
158
+ params.bodyBytes = undefined;
159
+ }
160
+ // 判断是否请求二进制响应体
161
+ if (binaryMimeTypes.includes((params.headers?.Accept || params.headers?.accept)?.split(';')?.[0] ?? '')) {
162
+ params['binary-mode'] = true;
129
163
  }
130
- if (binaryMimeTypes.includes((null === (_this = (null === (_params_headers = params.headers) || void 0 === _params_headers ? void 0 : _params_headers.Accept) || (null === (_params_headers1 = params.headers) || void 0 === _params_headers1 ? void 0 : _params_headers1.accept)) || void 0 === _this ? void 0 : null === (_split = _this.split(';')) || void 0 === _split ? void 0 : _split[0]) ?? '')) params['binary-mode'] = true;
131
- return new Promise((resolve, reject)=>{
132
- $httpClient[method](params, (error, response, body)=>{
133
- if (error) reject(error);
164
+ return new Promise((resolve, reject) => {
165
+ $httpClient[method](params, (error, response, body) => {
166
+ if (error) {
167
+ reject(error);
168
+ }
134
169
  else {
135
170
  response.ok = /^2\d\d$/.test(response.status);
136
171
  response.statusCode = response.status;
137
- response.statusText = __WEBPACK_EXTERNAL_MODULE__StatusTexts_mjs__.StatusTexts[response.status];
172
+ response.statusText = StatusTexts[response.status];
138
173
  if (body) {
139
174
  response.body = body;
140
- if (params['binary-mode']) response.bodyBytes = body;
175
+ if (params['binary-mode']) {
176
+ response.bodyBytes = body;
177
+ }
141
178
  }
142
179
  resolve(response);
143
180
  }
144
181
  });
145
182
  });
146
183
  }
147
- export { fetch_rslib_entry_fetch as fetch };
@@ -0,0 +1,5 @@
1
+ export * from './Console.mjs';
2
+ export * from './fetch.mjs';
3
+ export * from './Lodash.mjs';
4
+ export * from './StatusTexts.mjs';
5
+ export * from './Storage.mjs';
@@ -1,5 +1,5 @@
1
- export * from "./Console.mjs";
2
- export * from "./fetch.mjs";
3
- export * from "./Lodash.mjs";
4
- export * from "./StatusTexts.mjs";
5
- export * from "./Storage.mjs";
1
+ export * from './Console.mjs';
2
+ export * from './fetch.mjs';
3
+ export * from './Lodash.mjs';
4
+ export * from './StatusTexts.mjs';
5
+ export * from './Storage.mjs';