@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,149 @@
1
+ import * as __WEBPACK_EXTERNAL_MODULE_lodash_set__ from "lodash/set";
2
+ import * as __WEBPACK_EXTERNAL_MODULE__lib_app_js__ from "../lib/app.js";
3
+ import * as __WEBPACK_EXTERNAL_MODULE__StatusTexts_js__ from "./StatusTexts.js";
4
+ const binaryMimeTypes = [
5
+ 'application/protobuf',
6
+ 'application/x-protobuf',
7
+ 'application/vnd.google.protobuf',
8
+ 'application/vnd.apple.flatbuffer',
9
+ 'application/grpc',
10
+ 'application/grpc+proto',
11
+ 'application/octet-stream'
12
+ ];
13
+ async function fetch_rslib_entry_fetch(resource, options = {
14
+ timeout: 5
15
+ }) {
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 必须为对象或字符串`);
26
+ if (!params.method) {
27
+ params.method = 'GET';
28
+ if (params.body || params.bodyBytes) params.method = 'POST';
29
+ }
30
+ 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;
35
+ }
36
+ const method = params.method.toLocaleLowerCase();
37
+ if (params.timeout) {
38
+ params.timeout = Number.parseInt(params.timeout.toString(), 10);
39
+ if (params.timeout > 500) params.timeout = Math.round(params.timeout / 1000);
40
+ }
41
+ if ('Quantumult X' === __WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app) {
42
+ if (params.policy) (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(params, 'opts.policy', params.policy);
43
+ if ('boolean' == typeof params['auto-redirect']) (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(params, 'opts.redirection', params['auto-redirect']);
44
+ if (params.body instanceof ArrayBuffer) {
45
+ params.bodyBytes = params.body;
46
+ params.body = void 0;
47
+ } else if (ArrayBuffer.isView(params.body)) {
48
+ 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;
51
+ return Promise.race([
52
+ await $task.fetch(params).then((response)=>{
53
+ var _split, _this, _response_headers, _response_headers1;
54
+ response.ok = /^2\d\d$/.test(response.statusCode);
55
+ response.status = response.statusCode;
56
+ response.statusText = __WEBPACK_EXTERNAL_MODULE__StatusTexts_js__.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;
59
+ return response;
60
+ }, (reason)=>Promise.reject(reason.error)),
61
+ new Promise((_, reject)=>{
62
+ setTimeout(()=>{
63
+ reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
64
+ }, params.timeout);
65
+ })
66
+ ]);
67
+ }
68
+ if ('Node.js' === __WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app) {
69
+ const nodeFetch = globalThis.fetch ? globalThis.fetch : await import("node-fetch").then((module)=>module.default);
70
+ const fetchCookie = globalThis.fetchCookie ? globalThis.fetchCookie : await import("fetch-cookie").then((module)=>module.default);
71
+ const fetch = fetchCookie(nodeFetch);
72
+ params.timeout = (params.timeout ?? 5) * 1000;
73
+ params.redirect = params.redirection ? 'follow' : 'manual';
74
+ const { url, ...options } = params;
75
+ return Promise.race([
76
+ await fetch(url, options).then(async (response)=>{
77
+ const bodyBytes = await response.arrayBuffer();
78
+ let headers;
79
+ try {
80
+ headers = response.headers.raw();
81
+ } catch {
82
+ headers = Array.from(response.headers.entries()).reduce((acc, item)=>{
83
+ const [key, value] = item;
84
+ acc[key] = acc[key] ? [
85
+ ...acc[key],
86
+ value
87
+ ] : [
88
+ value
89
+ ];
90
+ return acc;
91
+ }, {});
92
+ }
93
+ return {
94
+ ok: response.ok ?? /^2\d\d$/.test(response.status),
95
+ status: response.status,
96
+ statusCode: response.status,
97
+ statusText: response.statusText,
98
+ body: new TextDecoder('utf-8').decode(bodyBytes),
99
+ bodyBytes: bodyBytes,
100
+ headers: Object.fromEntries(Object.entries(headers).map(([key, value])=>[
101
+ key,
102
+ 'set-cookie' !== key.toLowerCase() ? value.toString() : value
103
+ ]))
104
+ };
105
+ }).catch((error)=>Promise.reject(error.message)),
106
+ new Promise((resolve, reject)=>{
107
+ setTimeout(()=>{
108
+ reject(new Error(`${Function.name}: 请求超时, 请检查网络后重试`));
109
+ }, params.timeout);
110
+ })
111
+ ]);
112
+ }
113
+ if ('Loon' === __WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app) params.timeout *= 1000;
114
+ if (params.policy) switch(__WEBPACK_EXTERNAL_MODULE__lib_app_js__.$app){
115
+ case 'Loon':
116
+ params.node = params.policy;
117
+ break;
118
+ case 'Stash':
119
+ (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(params, 'headers.X-Stash-Selected-Proxy', encodeURI(params.policy));
120
+ break;
121
+ case 'Shadowrocket':
122
+ (0, __WEBPACK_EXTERNAL_MODULE_lodash_set__["default"])(params, 'headers.X-Surge-Proxy', params.policy);
123
+ break;
124
+ default:
125
+ break;
126
+ }
127
+ if ('boolean' == typeof params.redirection) params['auto-redirect'] = params.redirection;
128
+ if (params.bodyBytes && !params.body) {
129
+ params.body = params.bodyBytes;
130
+ params.bodyBytes = void 0;
131
+ }
132
+ 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;
133
+ return new Promise((resolve, reject)=>{
134
+ $httpClient[method](params, (error, response, body)=>{
135
+ if (error) reject(error);
136
+ else {
137
+ response.ok = /^2\d\d$/.test(response.status);
138
+ response.statusCode = response.status;
139
+ response.statusText = __WEBPACK_EXTERNAL_MODULE__StatusTexts_js__.StatusTexts[response.status];
140
+ if (body) {
141
+ response.body = body;
142
+ if (params['binary-mode']) response.bodyBytes = body;
143
+ }
144
+ resolve(response);
145
+ }
146
+ });
147
+ });
148
+ }
149
+ export { fetch_rslib_entry_fetch as fetch };
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "./Console": function(module) {
4
+ module.exports = require("./Console.cjs");
5
+ },
6
+ "./Lodash": function(module) {
7
+ module.exports = require("./Lodash.cjs");
8
+ },
9
+ "./StatusTexts": function(module) {
10
+ module.exports = require("./StatusTexts.cjs");
11
+ },
12
+ "./Storage": function(module) {
13
+ module.exports = require("./Storage.cjs");
14
+ },
15
+ "./fetch": function(module) {
16
+ module.exports = require("./fetch.cjs");
17
+ }
18
+ };
19
+ var __webpack_module_cache__ = {};
20
+ function __webpack_require__(moduleId) {
21
+ var cachedModule = __webpack_module_cache__[moduleId];
22
+ if (void 0 !== cachedModule) return cachedModule.exports;
23
+ var module = __webpack_module_cache__[moduleId] = {
24
+ exports: {}
25
+ };
26
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
27
+ return module.exports;
28
+ }
29
+ (()=>{
30
+ __webpack_require__.n = function(module) {
31
+ var getter = module && module.__esModule ? function() {
32
+ return module['default'];
33
+ } : function() {
34
+ return module;
35
+ };
36
+ __webpack_require__.d(getter, {
37
+ a: getter
38
+ });
39
+ return getter;
40
+ };
41
+ })();
42
+ (()=>{
43
+ __webpack_require__.d = function(exports1, definition) {
44
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
45
+ enumerable: true,
46
+ get: definition[key]
47
+ });
48
+ };
49
+ })();
50
+ (()=>{
51
+ __webpack_require__.o = function(obj, prop) {
52
+ return Object.prototype.hasOwnProperty.call(obj, prop);
53
+ };
54
+ })();
55
+ (()=>{
56
+ __webpack_require__.r = function(exports1) {
57
+ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
58
+ value: 'Module'
59
+ });
60
+ Object.defineProperty(exports1, '__esModule', {
61
+ value: true
62
+ });
63
+ };
64
+ })();
65
+ var __webpack_exports__ = {};
66
+ __webpack_require__.r(__webpack_exports__);
67
+ var _Console__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./Console");
68
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
69
+ for(var __WEBPACK_IMPORT_KEY__ in _Console__WEBPACK_IMPORTED_MODULE_0__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
70
+ return _Console__WEBPACK_IMPORTED_MODULE_0__[key];
71
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
72
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
73
+ var _fetch__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./fetch");
74
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
75
+ for(var __WEBPACK_IMPORT_KEY__ in _fetch__WEBPACK_IMPORTED_MODULE_1__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
76
+ return _fetch__WEBPACK_IMPORTED_MODULE_1__[key];
77
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
78
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
79
+ var _Lodash__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("./Lodash");
80
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
81
+ for(var __WEBPACK_IMPORT_KEY__ in _Lodash__WEBPACK_IMPORTED_MODULE_2__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
82
+ return _Lodash__WEBPACK_IMPORTED_MODULE_2__[key];
83
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
84
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
85
+ var _StatusTexts__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("./StatusTexts");
86
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
87
+ for(var __WEBPACK_IMPORT_KEY__ in _StatusTexts__WEBPACK_IMPORTED_MODULE_3__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
88
+ return _StatusTexts__WEBPACK_IMPORTED_MODULE_3__[key];
89
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
90
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
91
+ var _Storage__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("./Storage");
92
+ var __WEBPACK_REEXPORT_OBJECT__ = {};
93
+ for(var __WEBPACK_IMPORT_KEY__ in _Storage__WEBPACK_IMPORTED_MODULE_4__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
94
+ return _Storage__WEBPACK_IMPORTED_MODULE_4__[key];
95
+ }).bind(0, __WEBPACK_IMPORT_KEY__);
96
+ __webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
97
+ var __webpack_export_target__ = exports;
98
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
99
+ if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
100
+ value: true
101
+ });
@@ -0,0 +1,5 @@
1
+ export * from './Console';
2
+ export * from './fetch';
3
+ export * from './Lodash';
4
+ export * from './StatusTexts';
5
+ export * from './Storage';
@@ -0,0 +1,5 @@
1
+ export * from "./Console.js";
2
+ export * from "./fetch.js";
3
+ export * from "./Lodash.js";
4
+ export * from "./StatusTexts.js";
5
+ export * from "./Storage.js";
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@nsnanocat/util",
3
+ "description": "Pure JS's util module for well-known iOS network tools",
4
+ "type": "module",
5
+ "author": "VirgilClyne <Virgil@nanocat.me>",
6
+ "homepage": "https://NSNanoCat.github.io/util",
7
+ "keywords": [
8
+ "loon",
9
+ "quantumult",
10
+ "surge",
11
+ "shadowrocket",
12
+ "stash",
13
+ "egern"
14
+ ],
15
+ "license": "Apache-2.0",
16
+ "bugs": "https://github.com/NSNanoCat/util/issues",
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
+ },
26
+ "./getStorage": {
27
+ "types": "./dist/getStorage.d.ts",
28
+ "import": "./dist/getStorage.js",
29
+ "require": "./dist/getStorage.cjs"
30
+ },
31
+ "./polyfill/**": {
32
+ "types": "./dist/polyfill/**.d.ts",
33
+ "import": "./dist/polyfill/**.js",
34
+ "require": "./dist/polyfill/**.cjs"
35
+ },
36
+ "./lib/**": {
37
+ "types": "./dist/lib/**.d.ts",
38
+ "import": "./dist/lib/**.js",
39
+ "require": "./dist/lib/**.cjs"
40
+ }
41
+ },
42
+ "scripts": {
43
+ "build": "rslib build",
44
+ "check": "biome check --write",
45
+ "dev": "rslib build --watch",
46
+ "format": "biome format --write",
47
+ "prepublishOnly": "npm run build"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/NSNanoCat/util.git"
52
+ },
53
+ "files": [
54
+ "dist"
55
+ ],
56
+ "dependencies": {
57
+ "lodash": "^4.17.21"
58
+ },
59
+ "devDependencies": {
60
+ "@biomejs/biome": "^1.9.2",
61
+ "@rslib/core": "^0.2.2",
62
+ "@types/lodash": "^4.17.13",
63
+ "@types/node": "^22.10.2",
64
+ "fetch-cookie": "^3.1.0",
65
+ "node-fetch": "^3.3.2",
66
+ "typescript": "^5.6.3"
67
+ },
68
+ "optionalDependencies": {
69
+ "fetch-cookie": "^3.1.0",
70
+ "node-fetch": "^3.3.2"
71
+ },
72
+ "version": "0.0.0-preview-2c5e49e"
73
+ }