@nsnanocat/util 2.1.0 → 2.1.1
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/README.md +601 -1
- package/getStorage.mjs +40 -5
- package/index.js +12 -1
- package/lib/app.mjs +13 -2
- package/lib/argument.mjs +18 -0
- package/lib/done.mjs +23 -3
- package/lib/environment.mjs +22 -0
- package/lib/index.js +12 -0
- package/lib/notification.mjs +40 -11
- package/lib/runScript.mjs +21 -0
- package/lib/time.mjs +9 -9
- package/lib/wait.mjs +4 -4
- package/package.json +1 -1
- package/polyfill/Console.mjs +124 -0
- package/polyfill/Lodash.mjs +87 -0
- package/polyfill/StatusTexts.mjs +17 -0
- package/polyfill/Storage.mjs +68 -29
- package/polyfill/fetch.mjs +61 -6
package/polyfill/Storage.mjs
CHANGED
|
@@ -2,36 +2,70 @@ import { $app } from "../lib/app.mjs";
|
|
|
2
2
|
import { Lodash as _ } from "./Lodash.mjs";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 跨平台持久化存储适配器。
|
|
6
|
+
* Cross-platform persistent storage adapter.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* 设计目标:
|
|
9
|
+
* Design goal:
|
|
10
|
+
* - 仿照 Web Storage (`Storage`) 接口设计
|
|
11
|
+
* - Modeled after Web Storage (`Storage`) interface
|
|
12
|
+
* - 统一 VPN App 脚本环境中的持久化读写接口
|
|
13
|
+
* - Unify persistence APIs across VPN app script environments
|
|
14
|
+
*
|
|
15
|
+
* 支持后端:
|
|
16
|
+
* Supported backends:
|
|
17
|
+
* - Surge/Loon/Stash/Egern/Shadowrocket: `$persistentStore`
|
|
18
|
+
* - Quantumult X: `$prefs`
|
|
19
|
+
* - Node.js: 本地 `box.dat`
|
|
20
|
+
* - Node.js: local `box.dat`
|
|
21
|
+
*
|
|
22
|
+
* 支持路径键:
|
|
23
|
+
* Supports path key:
|
|
24
|
+
* - `@root.path.to.value`
|
|
25
|
+
*
|
|
26
|
+
* 与 Web Storage 的已知差异:
|
|
27
|
+
* Known differences from Web Storage:
|
|
28
|
+
* - 支持 `@key.path` 深路径读写(Web Storage 原生不支持)
|
|
29
|
+
* - Supports `@key.path` deep-path access (not native in Web Storage)
|
|
30
|
+
* - `removeItem/clear` 并非所有平台都可用
|
|
31
|
+
* - `removeItem/clear` are not available on every platform
|
|
32
|
+
* - 读取时会尝试 `JSON.parse`,写入对象会 `JSON.stringify`
|
|
33
|
+
* - Reads try `JSON.parse`, writes stringify objects
|
|
34
|
+
*
|
|
35
|
+
* @link https://developer.mozilla.org/en-US/docs/Web/API/Storage
|
|
36
|
+
* @link https://developer.mozilla.org/zh-CN/docs/Web/API/Storage
|
|
11
37
|
*/
|
|
12
38
|
export class Storage {
|
|
13
39
|
/**
|
|
14
|
-
*
|
|
40
|
+
* Node.js 环境下的内存数据缓存。
|
|
41
|
+
* In-memory data cache for Node.js runtime.
|
|
15
42
|
*
|
|
16
|
-
* @
|
|
17
|
-
* @type {file}
|
|
43
|
+
* @type {Record<string, any>|null}
|
|
18
44
|
*/
|
|
19
45
|
static data = null;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Node.js 持久化文件名。
|
|
49
|
+
* Data file name used in Node.js.
|
|
50
|
+
*
|
|
51
|
+
* @type {string}
|
|
52
|
+
*/
|
|
20
53
|
static dataFile = "box.dat";
|
|
54
|
+
|
|
21
55
|
/**
|
|
22
|
-
*
|
|
56
|
+
* `@key.path` 解析正则。
|
|
57
|
+
* Regex for `@key.path` parsing.
|
|
23
58
|
*
|
|
24
|
-
* @
|
|
25
|
-
* @type {regexp}
|
|
59
|
+
* @type {RegExp}
|
|
26
60
|
*/
|
|
27
61
|
static #nameRegex = /^@(?<key>[^.]+)(?:\.(?<path>.*))?$/;
|
|
28
62
|
|
|
29
63
|
/**
|
|
30
|
-
*
|
|
64
|
+
* 读取存储值。
|
|
65
|
+
* Read value from persistent storage.
|
|
31
66
|
*
|
|
32
|
-
* @
|
|
33
|
-
* @param {
|
|
34
|
-
* @param {*} [defaultValue]
|
|
67
|
+
* @param {string} keyName 键名或路径键 / Key or path key.
|
|
68
|
+
* @param {*} [defaultValue=null] 默认值 / Default value when key is missing.
|
|
35
69
|
* @returns {*}
|
|
36
70
|
*/
|
|
37
71
|
static getItem(keyName, defaultValue = null) {
|
|
@@ -80,11 +114,11 @@ export class Storage {
|
|
|
80
114
|
}
|
|
81
115
|
|
|
82
116
|
/**
|
|
83
|
-
*
|
|
117
|
+
* 写入存储值。
|
|
118
|
+
* Write value into persistent storage.
|
|
84
119
|
*
|
|
85
|
-
* @
|
|
86
|
-
* @param {
|
|
87
|
-
* @param {*} keyValue
|
|
120
|
+
* @param {string} keyName 键名或路径键 / Key or path key.
|
|
121
|
+
* @param {*} keyValue 写入值 / Value to store.
|
|
88
122
|
* @returns {boolean}
|
|
89
123
|
*/
|
|
90
124
|
static setItem(keyName = new String(), keyValue = new String()) {
|
|
@@ -135,10 +169,10 @@ export class Storage {
|
|
|
135
169
|
}
|
|
136
170
|
|
|
137
171
|
/**
|
|
138
|
-
*
|
|
172
|
+
* 删除存储值。
|
|
173
|
+
* Remove value from persistent storage.
|
|
139
174
|
*
|
|
140
|
-
* @
|
|
141
|
-
* @param {string} keyName
|
|
175
|
+
* @param {string} keyName 键名或路径键 / Key or path key.
|
|
142
176
|
* @returns {boolean}
|
|
143
177
|
*/
|
|
144
178
|
static removeItem(keyName) {
|
|
@@ -178,9 +212,9 @@ export class Storage {
|
|
|
178
212
|
}
|
|
179
213
|
|
|
180
214
|
/**
|
|
181
|
-
*
|
|
215
|
+
* 清空存储(仅 Quantumult X 支持)。
|
|
216
|
+
* Clear storage (supported by Quantumult X only).
|
|
182
217
|
*
|
|
183
|
-
* @static
|
|
184
218
|
* @returns {boolean}
|
|
185
219
|
*/
|
|
186
220
|
static clear() {
|
|
@@ -207,10 +241,12 @@ export class Storage {
|
|
|
207
241
|
}
|
|
208
242
|
|
|
209
243
|
/**
|
|
210
|
-
*
|
|
244
|
+
* 从 Node.js 数据文件加载 JSON。
|
|
245
|
+
* Load JSON data from Node.js data file.
|
|
211
246
|
*
|
|
212
|
-
* @
|
|
213
|
-
* @
|
|
247
|
+
* @private
|
|
248
|
+
* @param {string} dataFile 数据文件名 / Data file name.
|
|
249
|
+
* @returns {Record<string, any>}
|
|
214
250
|
*/
|
|
215
251
|
static #loaddata = dataFile => {
|
|
216
252
|
if ($app === "Node.js") {
|
|
@@ -232,9 +268,12 @@ export class Storage {
|
|
|
232
268
|
};
|
|
233
269
|
|
|
234
270
|
/**
|
|
235
|
-
*
|
|
271
|
+
* 将内存数据写入 Node.js 数据文件。
|
|
272
|
+
* Persist in-memory data to Node.js data file.
|
|
236
273
|
*
|
|
237
|
-
* @
|
|
274
|
+
* @private
|
|
275
|
+
* @param {string} [dataFile=this.dataFile] 数据文件名 / Data file name.
|
|
276
|
+
* @returns {void}
|
|
238
277
|
*/
|
|
239
278
|
static #writedata = (dataFile = this.dataFile) => {
|
|
240
279
|
if ($app === "Node.js") {
|
package/polyfill/fetch.mjs
CHANGED
|
@@ -4,14 +4,69 @@ import { Lodash as _ } from "./Lodash.mjs";
|
|
|
4
4
|
import { StatusTexts } from "./StatusTexts.mjs";
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* 统一请求参数。
|
|
8
|
+
* Unified request payload.
|
|
8
9
|
*
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
10
|
+
* @typedef {object} FetchRequest
|
|
11
|
+
* @property {string} url 请求地址 / Request URL.
|
|
12
|
+
* @property {string} [method] 请求方法 / HTTP method.
|
|
13
|
+
* @property {Record<string, any>} [headers] 请求头 / Request headers.
|
|
14
|
+
* @property {string|ArrayBuffer|ArrayBufferView|object} [body] 请求体 / Request body.
|
|
15
|
+
* @property {ArrayBuffer} [bodyBytes] 二进制请求体 / Binary request body.
|
|
16
|
+
* @property {number|string} [timeout] 超时(秒或毫秒)/ Timeout (seconds or milliseconds).
|
|
17
|
+
* @property {string} [policy] 指定策略 / Preferred policy.
|
|
18
|
+
* @property {boolean} [redirection] 是否跟随重定向 / Whether to follow redirects.
|
|
19
|
+
* @property {boolean} ["auto-redirect"] 平台重定向字段 / Platform redirect flag.
|
|
20
|
+
* @property {Record<string, any>} [opts] 平台扩展字段 / Platform extension fields.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 统一响应结构。
|
|
25
|
+
* Unified response payload.
|
|
26
|
+
*
|
|
27
|
+
* @typedef {object} FetchResponse
|
|
28
|
+
* @property {boolean} ok 请求是否成功 / Whether request is successful.
|
|
29
|
+
* @property {number} status 状态码 / HTTP status code.
|
|
30
|
+
* @property {number} [statusCode] 状态码别名 / Status code alias.
|
|
31
|
+
* @property {string} [statusText] 状态文本 / HTTP status text.
|
|
32
|
+
* @property {Record<string, any>} [headers] 响应头 / Response headers.
|
|
33
|
+
* @property {string|ArrayBuffer} [body] 响应体 / Response body.
|
|
34
|
+
* @property {ArrayBuffer} [bodyBytes] 二进制响应体 / Binary response body.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 跨平台 `fetch` 适配层。
|
|
39
|
+
* Cross-platform `fetch` adapter.
|
|
40
|
+
*
|
|
41
|
+
* 设计目标:
|
|
42
|
+
* Design goal:
|
|
43
|
+
* - 仿照 Web API `fetch`(`Window.fetch`)接口设计
|
|
44
|
+
* - Modeled after Web API `fetch` (`Window.fetch`)
|
|
45
|
+
* - 统一 VPN App 与 Node.js 环境中的请求调用
|
|
46
|
+
* - Unify request calls across VPN apps and Node.js
|
|
47
|
+
*
|
|
48
|
+
* 功能:
|
|
49
|
+
* Features:
|
|
50
|
+
* - 统一 Quantumult X / Loon / Surge / Stash / Egern / Shadowrocket / Node.js 请求接口
|
|
51
|
+
* - Normalize request APIs across Quantumult X / Loon / Surge / Stash / Egern / Shadowrocket / Node.js
|
|
52
|
+
* - 统一返回体字段(`ok/status/statusText/body/bodyBytes`)
|
|
53
|
+
* - Normalize response fields (`ok/status/statusText/body/bodyBytes`)
|
|
54
|
+
*
|
|
55
|
+
* 与 Web `fetch` 的已知差异:
|
|
56
|
+
* Known differences from Web `fetch`:
|
|
57
|
+
* - 支持 `policy`、`auto-redirect` 等平台扩展字段
|
|
58
|
+
* - Supports platform extension fields like `policy` and `auto-redirect`
|
|
59
|
+
* - 非浏览器平台通过 `$httpClient/$task` 实现,不是原生 Fetch 实现
|
|
60
|
+
* - Non-browser platforms use `$httpClient/$task` instead of native Fetch engine
|
|
61
|
+
* - 返回结构包含 `statusCode/bodyBytes` 等兼容字段
|
|
62
|
+
* - Response includes compatibility fields like `statusCode/bodyBytes`
|
|
63
|
+
*
|
|
64
|
+
* @link https://developer.mozilla.org/en-US/docs/Web/API/Window/fetch
|
|
65
|
+
* @link https://developer.mozilla.org/zh-CN/docs/Web/API/Window/fetch
|
|
11
66
|
* @async
|
|
12
|
-
* @param {
|
|
13
|
-
* @param {
|
|
14
|
-
* @returns {Promise<
|
|
67
|
+
* @param {FetchRequest|string} resource 请求对象或 URL / Request object or URL string.
|
|
68
|
+
* @param {Partial<FetchRequest>} [options={}] 追加参数 / Extra options.
|
|
69
|
+
* @returns {Promise<FetchResponse>}
|
|
15
70
|
*/
|
|
16
71
|
export async function fetch(resource, options = {}) {
|
|
17
72
|
// 初始化参数
|