@nsnanocat/util 0.0.0-preview-2c5e49e

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 (53) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +1 -0
  3. package/dist/env.d.cjs +6 -0
  4. package/dist/env.d.js +0 -0
  5. package/dist/getStorage.cjs +110 -0
  6. package/dist/getStorage.d.ts +8 -0
  7. package/dist/getStorage.js +62 -0
  8. package/dist/index.cjs +155 -0
  9. package/dist/index.d.ts +11 -0
  10. package/dist/index.js +11 -0
  11. package/dist/lib/app.cjs +48 -0
  12. package/dist/lib/app.d.ts +4 -0
  13. package/dist/lib/app.js +12 -0
  14. package/dist/lib/done.cjs +125 -0
  15. package/dist/lib/done.d.ts +17 -0
  16. package/dist/lib/done.js +76 -0
  17. package/dist/lib/environment.cjs +70 -0
  18. package/dist/lib/environment.d.ts +8 -0
  19. package/dist/lib/environment.js +36 -0
  20. package/dist/lib/index.cjs +101 -0
  21. package/dist/lib/index.d.ts +5 -0
  22. package/dist/lib/index.js +5 -0
  23. package/dist/lib/notification.cjs +135 -0
  24. package/dist/lib/notification.d.ts +20 -0
  25. package/dist/lib/notification.js +101 -0
  26. package/dist/lib/runScript.cjs +67 -0
  27. package/dist/lib/runScript.d.ts +3 -0
  28. package/dist/lib/runScript.js +33 -0
  29. package/dist/lib/time.cjs +54 -0
  30. package/dist/lib/time.d.ts +13 -0
  31. package/dist/lib/time.js +18 -0
  32. package/dist/lib/wait.cjs +40 -0
  33. package/dist/lib/wait.d.ts +4 -0
  34. package/dist/lib/wait.js +4 -0
  35. package/dist/polyfill/Console.cjs +250 -0
  36. package/dist/polyfill/Console.d.ts +21 -0
  37. package/dist/polyfill/Console.js +216 -0
  38. package/dist/polyfill/Lodash.cjs +74 -0
  39. package/dist/polyfill/Lodash.d.ts +41 -0
  40. package/dist/polyfill/Lodash.js +19 -0
  41. package/dist/polyfill/StatusTexts.cjs +99 -0
  42. package/dist/polyfill/StatusTexts.d.ts +62 -0
  43. package/dist/polyfill/StatusTexts.js +63 -0
  44. package/dist/polyfill/Storage.cjs +221 -0
  45. package/dist/polyfill/Storage.d.ts +12 -0
  46. package/dist/polyfill/Storage.js +173 -0
  47. package/dist/polyfill/fetch.cjs +214 -0
  48. package/dist/polyfill/fetch.d.ts +25 -0
  49. package/dist/polyfill/fetch.js +149 -0
  50. package/dist/polyfill/index.cjs +101 -0
  51. package/dist/polyfill/index.d.ts +5 -0
  52. package/dist/polyfill/index.js +5 -0
  53. package/package.json +73 -0
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "node:fs": function(module) {
4
+ module.exports = require("node:fs");
5
+ },
6
+ "node:path": function(module) {
7
+ module.exports = require("node:path");
8
+ }
9
+ };
10
+ var __webpack_module_cache__ = {};
11
+ function __webpack_require__(moduleId) {
12
+ var cachedModule = __webpack_module_cache__[moduleId];
13
+ if (void 0 !== cachedModule) return cachedModule.exports;
14
+ var module = __webpack_module_cache__[moduleId] = {
15
+ exports: {}
16
+ };
17
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
18
+ return module.exports;
19
+ }
20
+ (()=>{
21
+ __webpack_require__.n = function(module) {
22
+ var getter = module && module.__esModule ? function() {
23
+ return module['default'];
24
+ } : function() {
25
+ return module;
26
+ };
27
+ __webpack_require__.d(getter, {
28
+ a: getter
29
+ });
30
+ return getter;
31
+ };
32
+ })();
33
+ (()=>{
34
+ __webpack_require__.d = function(exports1, definition) {
35
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
36
+ enumerable: true,
37
+ get: definition[key]
38
+ });
39
+ };
40
+ })();
41
+ (()=>{
42
+ __webpack_require__.o = function(obj, prop) {
43
+ return Object.prototype.hasOwnProperty.call(obj, prop);
44
+ };
45
+ })();
46
+ (()=>{
47
+ __webpack_require__.r = function(exports1) {
48
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
49
+ value: 'Module'
50
+ });
51
+ Object.defineProperty(exports1, '__esModule', {
52
+ value: true
53
+ });
54
+ };
55
+ })();
56
+ var __webpack_exports__ = {};
57
+ __webpack_require__.r(__webpack_exports__);
58
+ __webpack_require__.d(__webpack_exports__, {
59
+ StorageClass: ()=>StorageClass,
60
+ Storage: ()=>Storage
61
+ });
62
+ const unset_namespaceObject = require("lodash/unset");
63
+ var unset_default = /*#__PURE__*/ __webpack_require__.n(unset_namespaceObject);
64
+ const set_namespaceObject = require("lodash/set");
65
+ var set_default = /*#__PURE__*/ __webpack_require__.n(set_namespaceObject);
66
+ const get_namespaceObject = require("lodash/get");
67
+ var get_default = /*#__PURE__*/ __webpack_require__.n(get_namespaceObject);
68
+ const app_cjs_namespaceObject = require("../lib/app.cjs");
69
+ function _check_private_redeclaration(obj, privateCollection) {
70
+ if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
71
+ }
72
+ function _class_private_method_get(receiver, privateSet, fn) {
73
+ if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");
74
+ return fn;
75
+ }
76
+ function _class_private_method_init(obj, privateSet) {
77
+ _check_private_redeclaration(obj, privateSet);
78
+ privateSet.add(obj);
79
+ }
80
+ function _define_property(obj, key, value) {
81
+ if (key in obj) Object.defineProperty(obj, key, {
82
+ value: value,
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true
86
+ });
87
+ else obj[key] = value;
88
+ return obj;
89
+ }
90
+ var _loadData = /*#__PURE__*/ new WeakSet(), _writeData = /*#__PURE__*/ new WeakSet();
91
+ class StorageClass {
92
+ getItem(keyName, defaultValue = null) {
93
+ let keyValue = defaultValue;
94
+ if (keyName.startsWith('@')) {
95
+ var _keyName_match;
96
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
97
+ if (key) {
98
+ let value = this.getItem(key, {});
99
+ if ('object' != typeof value) value = {};
100
+ keyValue = get_default()(value, path);
101
+ try {
102
+ keyValue = JSON.parse(keyValue);
103
+ } catch {}
104
+ }
105
+ } else {
106
+ switch(app_cjs_namespaceObject.$app){
107
+ case 'Surge':
108
+ case 'Loon':
109
+ case 'Stash':
110
+ case 'Egern':
111
+ case 'Shadowrocket':
112
+ keyValue = $persistentStore.read(keyName);
113
+ break;
114
+ case 'Quantumult X':
115
+ keyValue = $prefs.valueForKey(keyName);
116
+ break;
117
+ case 'Node.js':
118
+ this.data = this.data || {};
119
+ keyValue = this.data[keyName];
120
+ break;
121
+ default:
122
+ keyValue = null;
123
+ break;
124
+ }
125
+ try {
126
+ keyValue = JSON.parse(keyValue);
127
+ } catch {}
128
+ }
129
+ return keyValue ?? defaultValue;
130
+ }
131
+ setItem(keyName, value) {
132
+ let keyValue = value;
133
+ keyValue = 'object' == typeof keyValue ? JSON.stringify(keyValue) : String(keyValue);
134
+ if (keyName.startsWith('@')) {
135
+ var _keyName_match;
136
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
137
+ if (key) {
138
+ let value = this.getItem(key, {});
139
+ if ('object' != typeof value) value = {};
140
+ set_default()(value, path, keyValue);
141
+ return this.setItem(keyName, value);
142
+ }
143
+ } else switch(app_cjs_namespaceObject.$app){
144
+ case 'Surge':
145
+ case 'Loon':
146
+ case 'Stash':
147
+ case 'Egern':
148
+ case 'Shadowrocket':
149
+ return $persistentStore.write(keyValue, keyName);
150
+ case 'Quantumult X':
151
+ return $prefs.setValueForKey(keyValue, keyName);
152
+ case 'Node.js':
153
+ this.data = this.data || {};
154
+ this.data[keyName] = keyValue;
155
+ _class_private_method_get(this, _writeData, writeData).call(this, this.dataFile);
156
+ return true;
157
+ default:
158
+ break;
159
+ }
160
+ return false;
161
+ }
162
+ removeItem(keyName) {
163
+ if (keyName.startsWith('@')) {
164
+ var _keyName_match;
165
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
166
+ if (key) {
167
+ let value = this.getItem(key);
168
+ if ('object' != typeof value) value = {};
169
+ unset_default()(value, path);
170
+ return this.setItem(key, value);
171
+ }
172
+ } else switch(app_cjs_namespaceObject.$app){
173
+ case 'Quantumult X':
174
+ return $prefs.removeValueForKey(keyName);
175
+ default:
176
+ break;
177
+ }
178
+ return false;
179
+ }
180
+ clear() {
181
+ switch(app_cjs_namespaceObject.$app){
182
+ case 'Quantumult X':
183
+ return $prefs.removeAllValues();
184
+ default:
185
+ return false;
186
+ }
187
+ }
188
+ constructor(){
189
+ _class_private_method_init(this, _loadData);
190
+ _class_private_method_init(this, _writeData);
191
+ _define_property(this, "data", null);
192
+ _define_property(this, "dataFile", 'box.dat');
193
+ _define_property(this, "nameRegex", /^@(?<key>[^.]+)(?:\.(?<path>.*))?$/);
194
+ if ('Node.js' === app_cjs_namespaceObject.$app) {
195
+ const fs = __webpack_require__("node:fs");
196
+ const path = __webpack_require__("node:path");
197
+ this.data = _class_private_method_get(this, _loadData, loadData).call(this, this.dataFile, fs, path);
198
+ }
199
+ }
200
+ }
201
+ function loadData(dataFile, fs, path) {
202
+ const curDirDataFilePath = path.resolve(dataFile);
203
+ const rootDirDataFilePath = path.resolve(process.cwd(), dataFile);
204
+ if (fs.existsSync(curDirDataFilePath)) return JSON.parse(fs.readFileSync(curDirDataFilePath, 'utf-8')) || {};
205
+ if (fs.existsSync(rootDirDataFilePath)) return JSON.parse(fs.readFileSync(rootDirDataFilePath, 'utf-8')) || {};
206
+ return {};
207
+ }
208
+ function writeData(dataFile) {
209
+ if ('Node.js' === app_cjs_namespaceObject.$app) {
210
+ const fs = __webpack_require__("node:fs");
211
+ const path = __webpack_require__("node:path");
212
+ const dataFilePath = path.resolve(dataFile);
213
+ fs.writeFileSync(dataFilePath, JSON.stringify(this.data), 'utf-8');
214
+ }
215
+ }
216
+ const Storage = new StorageClass();
217
+ var __webpack_export_target__ = exports;
218
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
219
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
220
+ value: true
221
+ });
@@ -0,0 +1,12 @@
1
+ export declare class StorageClass {
2
+ #private;
3
+ private data;
4
+ private readonly dataFile;
5
+ private readonly nameRegex;
6
+ constructor();
7
+ getItem<T = any>(keyName: string, defaultValue?: T): T;
8
+ setItem(keyName: string, value: any): boolean;
9
+ removeItem(keyName: string): boolean;
10
+ clear(): boolean;
11
+ }
12
+ export declare const Storage: StorageClass;
@@ -0,0 +1,173 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
3
+ import * as __WEBPACK_EXTERNAL_MODULE_lodash_unset__ from "lodash/unset";
4
+ import * as __WEBPACK_EXTERNAL_MODULE_lodash_set__ from "lodash/set";
5
+ import * as __WEBPACK_EXTERNAL_MODULE_lodash_get__ from "lodash/get";
6
+ import * as __WEBPACK_EXTERNAL_MODULE__lib_app_js__ from "../lib/app.js";
7
+ var __webpack_modules__ = {
8
+ "node:fs": function(module) {
9
+ module.exports = __WEBPACK_EXTERNAL_MODULE_node_fs__;
10
+ },
11
+ "node:path": function(module) {
12
+ module.exports = __WEBPACK_EXTERNAL_MODULE_node_path__;
13
+ }
14
+ };
15
+ var __webpack_module_cache__ = {};
16
+ function __webpack_require__(moduleId) {
17
+ var cachedModule = __webpack_module_cache__[moduleId];
18
+ if (void 0 !== cachedModule) return cachedModule.exports;
19
+ var module = __webpack_module_cache__[moduleId] = {
20
+ exports: {}
21
+ };
22
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
23
+ return module.exports;
24
+ }
25
+ function _check_private_redeclaration(obj, privateCollection) {
26
+ if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
27
+ }
28
+ function _class_private_method_get(receiver, privateSet, fn) {
29
+ if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance");
30
+ return fn;
31
+ }
32
+ function _class_private_method_init(obj, privateSet) {
33
+ _check_private_redeclaration(obj, privateSet);
34
+ privateSet.add(obj);
35
+ }
36
+ function _define_property(obj, key, value) {
37
+ if (key in obj) Object.defineProperty(obj, key, {
38
+ value: value,
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true
42
+ });
43
+ else obj[key] = value;
44
+ return obj;
45
+ }
46
+ var _loadData = /*#__PURE__*/ new WeakSet(), _writeData = /*#__PURE__*/ new WeakSet();
47
+ class StorageClass {
48
+ getItem(keyName, defaultValue = null) {
49
+ let keyValue = defaultValue;
50
+ if (keyName.startsWith('@')) {
51
+ var _keyName_match;
52
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
53
+ if (key) {
54
+ let value = this.getItem(key, {});
55
+ if ('object' != typeof value) value = {};
56
+ keyValue = (0, __WEBPACK_EXTERNAL_MODULE_lodash_get__["default"])(value, path);
57
+ try {
58
+ keyValue = JSON.parse(keyValue);
59
+ } catch {}
60
+ }
61
+ } else {
62
+ switch(__WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app){
63
+ case 'Surge':
64
+ case 'Loon':
65
+ case 'Stash':
66
+ case 'Egern':
67
+ case 'Shadowrocket':
68
+ keyValue = $persistentStore.read(keyName);
69
+ break;
70
+ case 'Quantumult X':
71
+ keyValue = $prefs.valueForKey(keyName);
72
+ break;
73
+ case 'Node.js':
74
+ this.data = this.data || {};
75
+ keyValue = this.data[keyName];
76
+ break;
77
+ default:
78
+ keyValue = null;
79
+ break;
80
+ }
81
+ try {
82
+ keyValue = JSON.parse(keyValue);
83
+ } catch {}
84
+ }
85
+ return keyValue ?? defaultValue;
86
+ }
87
+ setItem(keyName, value) {
88
+ let keyValue = value;
89
+ keyValue = 'object' == typeof keyValue ? JSON.stringify(keyValue) : String(keyValue);
90
+ if (keyName.startsWith('@')) {
91
+ var _keyName_match;
92
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
93
+ if (key) {
94
+ let value = this.getItem(key, {});
95
+ if ('object' != typeof value) value = {};
96
+ (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(value, path, keyValue);
97
+ return this.setItem(keyName, value);
98
+ }
99
+ } else switch(__WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app){
100
+ case 'Surge':
101
+ case 'Loon':
102
+ case 'Stash':
103
+ case 'Egern':
104
+ case 'Shadowrocket':
105
+ return $persistentStore.write(keyValue, keyName);
106
+ case 'Quantumult X':
107
+ return $prefs.setValueForKey(keyValue, keyName);
108
+ case 'Node.js':
109
+ this.data = this.data || {};
110
+ this.data[keyName] = keyValue;
111
+ _class_private_method_get(this, _writeData, writeData).call(this, this.dataFile);
112
+ return true;
113
+ default:
114
+ break;
115
+ }
116
+ return false;
117
+ }
118
+ removeItem(keyName) {
119
+ if (keyName.startsWith('@')) {
120
+ var _keyName_match;
121
+ const { key, path } = (null === (_keyName_match = keyName.match(this.nameRegex)) || void 0 === _keyName_match ? void 0 : _keyName_match.groups) || {};
122
+ if (key) {
123
+ let value = this.getItem(key);
124
+ if ('object' != typeof value) value = {};
125
+ (0, __WEBPACK_EXTERNAL_MODULE_lodash_unset__["default"])(value, path);
126
+ return this.setItem(key, value);
127
+ }
128
+ } else switch(__WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app){
129
+ case 'Quantumult X':
130
+ return $prefs.removeValueForKey(keyName);
131
+ default:
132
+ break;
133
+ }
134
+ return false;
135
+ }
136
+ clear() {
137
+ switch(__WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app){
138
+ case 'Quantumult X':
139
+ return $prefs.removeAllValues();
140
+ default:
141
+ return false;
142
+ }
143
+ }
144
+ constructor(){
145
+ _class_private_method_init(this, _loadData);
146
+ _class_private_method_init(this, _writeData);
147
+ _define_property(this, "data", null);
148
+ _define_property(this, "dataFile", 'box.dat');
149
+ _define_property(this, "nameRegex", /^@(?<key>[^.]+)(?:\.(?<path>.*))?$/);
150
+ if ('Node.js' === __WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app) {
151
+ const fs = __webpack_require__("node:fs");
152
+ const path = __webpack_require__("node:path");
153
+ this.data = _class_private_method_get(this, _loadData, loadData).call(this, this.dataFile, fs, path);
154
+ }
155
+ }
156
+ }
157
+ function loadData(dataFile, fs, path) {
158
+ const curDirDataFilePath = path.resolve(dataFile);
159
+ const rootDirDataFilePath = path.resolve(process.cwd(), dataFile);
160
+ if (fs.existsSync(curDirDataFilePath)) return JSON.parse(fs.readFileSync(curDirDataFilePath, 'utf-8')) || {};
161
+ if (fs.existsSync(rootDirDataFilePath)) return JSON.parse(fs.readFileSync(rootDirDataFilePath, 'utf-8')) || {};
162
+ return {};
163
+ }
164
+ function writeData(dataFile) {
165
+ if ('Node.js' === __WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app) {
166
+ const fs = __webpack_require__("node:fs");
167
+ const path = __webpack_require__("node:path");
168
+ const dataFilePath = path.resolve(dataFile);
169
+ fs.writeFileSync(dataFilePath, JSON.stringify(this.data), 'utf-8');
170
+ }
171
+ }
172
+ const Storage = new StorageClass();
173
+ export { Storage, StorageClass };
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "fetch-cookie": function(module) {
4
+ module.exports = import("fetch-cookie");
5
+ },
6
+ "node-fetch": function(module) {
7
+ module.exports = import("node-fetch");
8
+ }
9
+ };
10
+ var __webpack_module_cache__ = {};
11
+ function __webpack_require__(moduleId) {
12
+ var cachedModule = __webpack_module_cache__[moduleId];
13
+ if (void 0 !== cachedModule) return cachedModule.exports;
14
+ var module = __webpack_module_cache__[moduleId] = {
15
+ exports: {}
16
+ };
17
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
18
+ return module.exports;
19
+ }
20
+ (()=>{
21
+ __webpack_require__.n = function(module) {
22
+ var getter = module && module.__esModule ? function() {
23
+ return module['default'];
24
+ } : function() {
25
+ return module;
26
+ };
27
+ __webpack_require__.d(getter, {
28
+ a: getter
29
+ });
30
+ return getter;
31
+ };
32
+ })();
33
+ (()=>{
34
+ __webpack_require__.d = function(exports1, definition) {
35
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
36
+ enumerable: true,
37
+ get: definition[key]
38
+ });
39
+ };
40
+ })();
41
+ (()=>{
42
+ __webpack_require__.o = function(obj, prop) {
43
+ return Object.prototype.hasOwnProperty.call(obj, prop);
44
+ };
45
+ })();
46
+ (()=>{
47
+ __webpack_require__.r = function(exports1) {
48
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
49
+ value: 'Module'
50
+ });
51
+ Object.defineProperty(exports1, '__esModule', {
52
+ value: true
53
+ });
54
+ };
55
+ })();
56
+ var __webpack_exports__ = {};
57
+ __webpack_require__.r(__webpack_exports__);
58
+ __webpack_require__.d(__webpack_exports__, {
59
+ fetch: ()=>fetch_rslib_entry_fetch
60
+ });
61
+ const set_namespaceObject = require("lodash/set");
62
+ var set_default = /*#__PURE__*/ __webpack_require__.n(set_namespaceObject);
63
+ const app_cjs_namespaceObject = require("../lib/app.cjs");
64
+ const external_StatusTexts_cjs_namespaceObject = require("./StatusTexts.cjs");
65
+ const binaryMimeTypes = [
66
+ 'application/protobuf',
67
+ 'application/x-protobuf',
68
+ 'application/vnd.google.protobuf',
69
+ 'application/vnd.apple.flatbuffer',
70
+ 'application/grpc',
71
+ 'application/grpc+proto',
72
+ 'application/octet-stream'
73
+ ];
74
+ async function fetch_rslib_entry_fetch(resource, options = {
75
+ timeout: 5
76
+ }) {
77
+ var _split, _this, _params_headers, _params_headers1;
78
+ let params = {
79
+ ...options
80
+ };
81
+ if ('string' == typeof resource) params.url = resource;
82
+ else if ('object' == typeof resource) params = {
83
+ ...params,
84
+ ...resource
85
+ };
86
+ else throw new TypeError(`${Function.name}: 参数类型错误, resource 必须为对象或字符串`);
87
+ if (!params.method) {
88
+ params.method = 'GET';
89
+ if (params.body || params.bodyBytes) params.method = 'POST';
90
+ }
91
+ if (params.headers) {
92
+ params.headers.Host = void 0;
93
+ params.headers[':authority'] = void 0;
94
+ params.headers['Content-Length'] = void 0;
95
+ params.headers['content-length'] = void 0;
96
+ }
97
+ const method = params.method.toLocaleLowerCase();
98
+ if (params.timeout) {
99
+ params.timeout = Number.parseInt(params.timeout.toString(), 10);
100
+ if (params.timeout > 500) params.timeout = Math.round(params.timeout / 1000);
101
+ }
102
+ if ('Quantumult X' === app_cjs_namespaceObject.$app) {
103
+ if (params.policy) set_default()(params, 'opts.policy', params.policy);
104
+ if ('boolean' == typeof params['auto-redirect']) set_default()(params, 'opts.redirection', params['auto-redirect']);
105
+ if (params.body instanceof ArrayBuffer) {
106
+ params.bodyBytes = params.body;
107
+ params.body = void 0;
108
+ } else if (ArrayBuffer.isView(params.body)) {
109
+ params.bodyBytes = params.body.buffer.slice(params.body.byteOffset, params.body.byteLength + params.body.byteOffset);
110
+ params.body = void 0;
111
+ } else if (params.body) params.bodyBytes = void 0;
112
+ return Promise.race([
113
+ await $task.fetch(params).then((response)=>{
114
+ var _split, _this, _response_headers, _response_headers1;
115
+ response.ok = /^2\d\d$/.test(response.statusCode);
116
+ response.status = response.statusCode;
117
+ response.statusText = external_StatusTexts_cjs_namespaceObject.StatusTexts[response.status];
118
+ 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;
119
+ response.bodyBytes = void 0;
120
+ return response;
121
+ }, (reason)=>Promise.reject(reason.error)),
122
+ new Promise((_, reject)=>{
123
+ setTimeout(()=>{
124
+ reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
125
+ }, params.timeout);
126
+ })
127
+ ]);
128
+ }
129
+ if ('Node.js' === app_cjs_namespaceObject.$app) {
130
+ const nodeFetch = globalThis.fetch ? globalThis.fetch : await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "node-fetch")).then((module)=>module.default);
131
+ const fetchCookie = globalThis.fetchCookie ? globalThis.fetchCookie : await Promise.resolve().then(__webpack_require__.bind(__webpack_require__, "fetch-cookie")).then((module)=>module.default);
132
+ const fetch = fetchCookie(nodeFetch);
133
+ params.timeout = (params.timeout ?? 5) * 1000;
134
+ params.redirect = params.redirection ? 'follow' : 'manual';
135
+ const { url, ...options } = params;
136
+ return Promise.race([
137
+ await fetch(url, options).then(async (response)=>{
138
+ const bodyBytes = await response.arrayBuffer();
139
+ let headers;
140
+ try {
141
+ headers = response.headers.raw();
142
+ } catch {
143
+ headers = Array.from(response.headers.entries()).reduce((acc, item)=>{
144
+ const [key, value] = item;
145
+ acc[key] = acc[key] ? [
146
+ ...acc[key],
147
+ value
148
+ ] : [
149
+ value
150
+ ];
151
+ return acc;
152
+ }, {});
153
+ }
154
+ return {
155
+ ok: response.ok ?? /^2\d\d$/.test(response.status),
156
+ status: response.status,
157
+ statusCode: response.status,
158
+ statusText: response.statusText,
159
+ body: new TextDecoder('utf-8').decode(bodyBytes),
160
+ bodyBytes: bodyBytes,
161
+ headers: Object.fromEntries(Object.entries(headers).map(([key, value])=>[
162
+ key,
163
+ 'set-cookie' !== key.toLowerCase() ? value.toString() : value
164
+ ]))
165
+ };
166
+ }).catch((error)=>Promise.reject(error.message)),
167
+ new Promise((resolve, reject)=>{
168
+ setTimeout(()=>{
169
+ reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
170
+ }, params.timeout);
171
+ })
172
+ ]);
173
+ }
174
+ if ('Loon' === app_cjs_namespaceObject.$app) params.timeout *= 1000;
175
+ if (params.policy) switch(app_cjs_namespaceObject.$app){
176
+ case 'Loon':
177
+ params.node = params.policy;
178
+ break;
179
+ case 'Stash':
180
+ set_default()(params, 'headers.X-Stash-Selected-Proxy', encodeURI(params.policy));
181
+ break;
182
+ case 'Shadowrocket':
183
+ set_default()(params, 'headers.X-Surge-Proxy', params.policy);
184
+ break;
185
+ default:
186
+ break;
187
+ }
188
+ if ('boolean' == typeof params.redirection) params['auto-redirect'] = params.redirection;
189
+ if (params.bodyBytes && !params.body) {
190
+ params.body = params.bodyBytes;
191
+ params.bodyBytes = void 0;
192
+ }
193
+ 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;
194
+ return new Promise((resolve, reject)=>{
195
+ $httpClient[method](params, (error, response, body)=>{
196
+ if (error) reject(error);
197
+ else {
198
+ response.ok = /^2\d\d$/.test(response.status);
199
+ response.statusCode = response.status;
200
+ response.statusText = external_StatusTexts_cjs_namespaceObject.StatusTexts[response.status];
201
+ if (body) {
202
+ response.body = body;
203
+ if (params['binary-mode']) response.bodyBytes = body;
204
+ }
205
+ resolve(response);
206
+ }
207
+ });
208
+ });
209
+ }
210
+ var __webpack_export_target__ = exports;
211
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
212
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
213
+ value: true
214
+ });
@@ -0,0 +1,25 @@
1
+ interface FetchOptions {
2
+ url?: string;
3
+ method?: string;
4
+ headers?: Record<string, string | undefined>;
5
+ body?: any;
6
+ bodyBytes?: ArrayBuffer;
7
+ timeout: number;
8
+ policy?: string;
9
+ node?: string;
10
+ redirection?: boolean;
11
+ redirect?: string;
12
+ 'auto-redirect'?: boolean;
13
+ 'binary-mode'?: boolean;
14
+ }
15
+ interface FetchResponse<T = any> {
16
+ ok: boolean;
17
+ status: number;
18
+ statusCode: number;
19
+ statusText: string;
20
+ body: T;
21
+ bodyBytes?: ArrayBuffer;
22
+ headers: Record<string, string>;
23
+ }
24
+ export declare function fetch<T>(resource: string | FetchOptions, options?: FetchOptions): Promise<FetchResponse<T>>;
25
+ export {};