@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.
- package/dist/getStorage.mjs +64 -47
- package/dist/index.d.ts +3 -11
- package/dist/index.js +3 -11
- package/dist/lib/app.mjs +25 -9
- package/dist/lib/done.mjs +58 -48
- package/dist/lib/environment.mjs +15 -16
- package/dist/lib/index.d.mts +5 -0
- package/dist/lib/index.mjs +5 -5
- package/dist/lib/notification.mjs +50 -29
- package/dist/lib/runScript.mjs +27 -18
- package/dist/lib/time.mjs +17 -4
- package/dist/lib/wait.mjs +5 -3
- package/dist/polyfill/Console.mjs +171 -145
- package/dist/polyfill/Lodash.mjs +39 -29
- package/dist/polyfill/StatusTexts.mjs +2 -3
- package/dist/polyfill/Storage.d.mts +12 -0
- package/dist/polyfill/Storage.mjs +167 -173
- package/dist/polyfill/fetch-node.d.mts +3 -0
- package/dist/polyfill/fetch-node.mjs +2 -2
- package/dist/polyfill/fetch.mjs +115 -79
- package/dist/polyfill/index.d.mts +5 -0
- package/dist/polyfill/index.mjs +5 -5
- package/package.json +4 -4
- package/dist/lib/index.d.ts +0 -5
- package/dist/polyfill/Storage.d.ts +0 -8
- package/dist/polyfill/fetch-node.d.ts +0 -1
- package/dist/polyfill/index.d.ts +0 -5
- /package/dist/{getStorage.d.ts → getStorage.d.mts} +0 -0
- /package/dist/lib/{app.d.ts → app.d.mts} +0 -0
- /package/dist/lib/{done.d.ts → done.d.mts} +0 -0
- /package/dist/lib/{environment.d.ts → environment.d.mts} +0 -0
- /package/dist/lib/{notification.d.ts → notification.d.mts} +0 -0
- /package/dist/lib/{runScript.d.ts → runScript.d.mts} +0 -0
- /package/dist/lib/{time.d.ts → time.d.mts} +0 -0
- /package/dist/lib/{wait.d.ts → wait.d.mts} +0 -0
- /package/dist/polyfill/{Console.d.ts → Console.d.mts} +0 -0
- /package/dist/polyfill/{Lodash.d.ts → Lodash.d.mts} +0 -0
- /package/dist/polyfill/{StatusTexts.d.ts → StatusTexts.d.mts} +0 -0
- /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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
50
|
-
let
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
50
|
+
}
|
|
93
51
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
|
79
|
+
return keyValue ?? defaultValue;
|
|
131
80
|
}
|
|
132
|
-
|
|
133
|
-
let
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
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
|
-
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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();
|
|
@@ -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
|
};
|
package/dist/polyfill/fetch.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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
|
|
14
|
-
timeout: 5
|
|
14
|
+
export async function fetch(resource, options = {
|
|
15
|
+
timeout: 5,
|
|
15
16
|
}) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
else if (
|
|
22
|
-
...params,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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)
|
|
31
|
+
if (params.body || params.bodyBytes) {
|
|
32
|
+
params.method = 'POST';
|
|
33
|
+
}
|
|
29
34
|
}
|
|
35
|
+
// 移除请求头中的部分参数, 让其自动生成
|
|
30
36
|
if (params.headers) {
|
|
31
|
-
params.headers.Host =
|
|
32
|
-
params.headers[':authority'] =
|
|
33
|
-
params.headers['Content-Length'] =
|
|
34
|
-
params.headers['content-length'] =
|
|
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
|
-
|
|
47
|
+
// 转换为秒,大于500视为毫秒,小于等于500视为秒
|
|
48
|
+
if (params.timeout > 500) {
|
|
49
|
+
params.timeout = Math.round(params.timeout / 1000);
|
|
50
|
+
}
|
|
40
51
|
}
|
|
41
|
-
if ('Quantumult X'
|
|
42
|
-
|
|
43
|
-
if (
|
|
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 =
|
|
47
|
-
}
|
|
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 =
|
|
50
|
-
}
|
|
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 =
|
|
57
|
-
if (binaryMimeTypes.includes(
|
|
58
|
-
|
|
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'
|
|
69
|
-
const fetch = await import(
|
|
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)
|
|
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
|
-
}
|
|
80
|
-
|
|
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
|
-
|
|
100
|
-
|
|
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
|
-
})
|
|
104
|
-
|
|
105
|
-
|
|
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'
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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 (
|
|
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 =
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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 =
|
|
172
|
+
response.statusText = StatusTexts[response.status];
|
|
138
173
|
if (body) {
|
|
139
174
|
response.body = body;
|
|
140
|
-
if (params['binary-mode'])
|
|
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 };
|
package/dist/polyfill/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
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';
|