@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,62 +1,79 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__ from "./polyfill/Lodash.mjs";
2
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Storage_mjs__ from "./polyfill/Storage.mjs";
3
- function getStorage(key, names, database) {
4
- var _database_Default, _database_Default1;
5
- const nameList = Array.isArray(names) ? names : [
6
- names
7
- ];
1
+ import { set } from './polyfill/Lodash.mjs';
2
+ import { Storage } from './polyfill/Storage.mjs';
3
+ export function getStorage(key, names, database) {
4
+ const nameList = Array.isArray(names) ? names : [names];
8
5
  const Store = {
9
- Settings: (null == database ? void 0 : null === (_database_Default = database.Default) || void 0 === _database_Default ? void 0 : _database_Default.Settings) || {},
10
- Configs: (null == database ? void 0 : null === (_database_Default1 = database.Default) || void 0 === _database_Default1 ? void 0 : _database_Default1.Configs) || {},
11
- Caches: {}
6
+ Settings: database?.Default?.Settings || {},
7
+ Configs: database?.Default?.Configs || {},
8
+ Caches: {},
12
9
  };
13
- nameList.forEach((name)=>{
14
- var _database_name, _database_name1;
15
- Store.Settings = {
16
- ...Store.Settings,
17
- ...null == database ? void 0 : null === (_database_name = database[name]) || void 0 === _database_name ? void 0 : _database_name.Settings
18
- };
19
- Store.Configs = {
20
- ...Store.Configs,
21
- ...null == database ? void 0 : null === (_database_name1 = database[name]) || void 0 === _database_name1 ? void 0 : _database_name1.Configs
22
- };
10
+ nameList.forEach((name) => {
11
+ Store.Settings = { ...Store.Settings, ...database?.[name]?.Settings };
12
+ Store.Configs = { ...Store.Configs, ...database?.[name]?.Configs };
23
13
  });
24
- if ('string' == typeof $argument) {
25
- const parsedArgument = Object.fromEntries($argument.split('&').map((item)=>item.split('=', 2).map((i)=>i.replace(/\"/g, ''))));
26
- Object.keys(parsedArgument).forEach((key)=>(0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(Store.Settings, key, parsedArgument[key]));
27
- } else if ('object' == typeof $argument) Object.keys($argument).forEach((key)=>(0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(Store.Settings, key, $argument[key]));
28
- const BoxJs = __WEBPACK_EXTERNAL_MODULE__polyfill_Storage_mjs__.Storage.getItem(key);
29
- if (BoxJs) nameList.forEach((name)=>{
30
- var _BoxJs_name, _BoxJs_name1;
31
- const boxSettings = null == BoxJs ? void 0 : null === (_BoxJs_name = BoxJs[name]) || void 0 === _BoxJs_name ? void 0 : _BoxJs_name.Settings;
32
- const boxCaches = null == BoxJs ? void 0 : null === (_BoxJs_name1 = BoxJs[name]) || void 0 === _BoxJs_name1 ? void 0 : _BoxJs_name1.Caches;
33
- if ('string' == typeof boxSettings) BoxJs[name].Settings = JSON.parse(boxSettings || '{}');
34
- if (boxSettings) Store.Settings = {
35
- ...Store.Settings,
36
- ...BoxJs[name].Settings
37
- };
38
- if ('string' == typeof boxCaches) BoxJs[name].Caches = JSON.parse(boxCaches || '{}');
39
- if (boxCaches) Store.Caches = {
40
- ...Store.Caches,
41
- ...BoxJs[name].Caches
42
- };
43
- });
44
- traverseObject(Store.Settings, (key, value)=>{
14
+ if (typeof $argument === 'string') {
15
+ const parsedArgument = Object.fromEntries($argument.split('&').map((item) => item.split('=', 2).map((i) => i.replace(/\"/g, ''))));
16
+ Object.keys(parsedArgument).forEach((key) => set(Store.Settings, key, parsedArgument[key]));
17
+ }
18
+ else if (typeof $argument === 'object') {
19
+ Object.keys($argument).forEach((key) => set(Store.Settings, key, $argument[key]));
20
+ }
21
+ /***************** BoxJs *****************/
22
+ // 包装为局部变量,用完释放内存
23
+ // BoxJs的清空操作返回假值空字符串, 逻辑或操作符会在左侧操作数为假值时返回右侧操作数。
24
+ const BoxJs = Storage.getItem(key);
25
+ if (BoxJs) {
26
+ //Console.debug("BoxJs", `BoxJs类型: ${typeof BoxJs}`, `BoxJs内容: ${JSON.stringify(BoxJs || {})}`);
27
+ nameList.forEach((name) => {
28
+ const boxSettings = BoxJs?.[name]?.Settings;
29
+ const boxCaches = BoxJs?.[name]?.Caches;
30
+ if (typeof boxSettings === 'string') {
31
+ BoxJs[name].Settings = JSON.parse(boxSettings || '{}');
32
+ }
33
+ if (boxSettings) {
34
+ Store.Settings = { ...Store.Settings, ...BoxJs[name].Settings };
35
+ }
36
+ if (typeof boxCaches === 'string') {
37
+ BoxJs[name].Caches = JSON.parse(boxCaches || '{}');
38
+ }
39
+ if (boxCaches) {
40
+ Store.Caches = { ...Store.Caches, ...BoxJs[name].Caches };
41
+ }
42
+ });
43
+ }
44
+ /***************** traverseObject *****************/
45
+ traverseObject(Store.Settings, (key, value) => {
46
+ //Console.debug("☑️ traverseObject", `${key}: ${typeof value}`, `${key}: ${JSON.stringify(value)}`);
45
47
  let transformedValue = value;
46
- if ('true' === transformedValue || 'false' === transformedValue) transformedValue = JSON.parse(transformedValue);
47
- else if ('string' == typeof transformedValue) transformedValue = transformedValue.includes(',') ? transformedValue.split(',').map((item)=>string2number(item)) : string2number(transformedValue);
48
+ if (transformedValue === 'true' || transformedValue === 'false')
49
+ transformedValue = JSON.parse(transformedValue); // 字符串转Boolean
50
+ else if (typeof transformedValue === 'string') {
51
+ if (transformedValue.includes(','))
52
+ transformedValue = transformedValue.split(',').map((item) => string2number(item)); // 字符串转数组转数字
53
+ else
54
+ transformedValue = string2number(transformedValue); // 字符串转数字
55
+ }
48
56
  return transformedValue;
49
57
  });
50
58
  return Store;
51
59
  }
60
+ /**
61
+ * Recursively traverse and transform object properties.
62
+ */
52
63
  function traverseObject(obj, callback) {
53
- Object.entries(obj).forEach(([key, value])=>{
54
- if (value && 'object' == typeof value) obj[key] = traverseObject(value, callback);
55
- else obj[key] = callback(key, value);
64
+ Object.entries(obj).forEach(([key, value]) => {
65
+ if (value && typeof value === 'object') {
66
+ obj[key] = traverseObject(value, callback);
67
+ }
68
+ else {
69
+ obj[key] = callback(key, value);
70
+ }
56
71
  });
57
72
  return obj;
58
73
  }
74
+ /**
75
+ * Convert string to number if applicable.
76
+ */
59
77
  function string2number(value) {
60
78
  return /^\d+$/.test(value) ? Number(value) : value;
61
79
  }
62
- export { getStorage };
package/dist/index.d.ts CHANGED
@@ -1,11 +1,3 @@
1
- export * from './lib/app';
2
- export * from './lib/done';
3
- export * from './lib/notification';
4
- export * from './lib/time';
5
- export * from './lib/wait';
6
- export * from './polyfill/Console';
7
- export * from './polyfill/fetch';
8
- export * from './polyfill/Lodash';
9
- export * from './polyfill/StatusTexts';
10
- export * from './polyfill/Storage';
11
- export * from './getStorage';
1
+ export * from './lib/index.mjs';
2
+ export * from './polyfill/index.mjs';
3
+ export * from './getStorage.mjs';
package/dist/index.js CHANGED
@@ -1,11 +1,3 @@
1
- export * from "./lib/app.mjs";
2
- export * from "./lib/done.mjs";
3
- export * from "./lib/notification.mjs";
4
- export * from "./lib/time.mjs";
5
- export * from "./lib/wait.mjs";
6
- export * from "./polyfill/Console.mjs";
7
- export * from "./polyfill/fetch.mjs";
8
- export * from "./polyfill/Lodash.mjs";
9
- export * from "./polyfill/StatusTexts.mjs";
10
- export * from "./polyfill/Storage.mjs";
11
- export * from "./getStorage.mjs";
1
+ export * from './lib/index.mjs';
2
+ export * from './polyfill/index.mjs';
3
+ export * from './getStorage.mjs';
package/dist/lib/app.mjs CHANGED
@@ -1,12 +1,28 @@
1
- const $app = (()=>{
2
- if ('$task' in globalThis) return 'Quantumult X';
3
- if ('$loon' in globalThis) return 'Loon';
4
- if ('$rocket' in globalThis) return 'Shadowrocket';
5
- if ('Egern' in globalThis) return 'Egern';
1
+ /**
2
+ * Current app name
3
+ */
4
+ export const $app = (() => {
5
+ if ('$task' in globalThis) {
6
+ return 'Quantumult X';
7
+ }
8
+ if ('$loon' in globalThis) {
9
+ return 'Loon';
10
+ }
11
+ if ('$rocket' in globalThis) {
12
+ return 'Shadowrocket';
13
+ }
14
+ if ('Egern' in globalThis) {
15
+ return 'Egern';
16
+ }
6
17
  if ('$environment' in globalThis) {
7
- if (globalThis.$environment['surge-version']) return 'Surge';
8
- if (globalThis.$environment['stash-version']) return 'Stash';
18
+ if (globalThis.$environment['surge-version']) {
19
+ return 'Surge';
20
+ }
21
+ if (globalThis.$environment['stash-version']) {
22
+ return 'Stash';
23
+ }
24
+ }
25
+ if (typeof module !== 'undefined') {
26
+ return 'Node.js';
9
27
  }
10
- return 'Node.js';
11
28
  })();
12
- export { $app };
package/dist/lib/done.mjs CHANGED
@@ -1,46 +1,62 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__ from "../polyfill/Lodash.mjs";
2
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__ from "../polyfill/Console.mjs";
3
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_StatusTexts_mjs__ from "../polyfill/StatusTexts.mjs";
4
- import * as __WEBPACK_EXTERNAL_MODULE__app_mjs__ from "./app.mjs";
5
- const transformQuantumultXBody = (object)=>{
6
- if (object.body instanceof ArrayBuffer) return {
7
- bodyBytes: object.body,
8
- body: void 0
9
- };
10
- if (ArrayBuffer.isView(object.body)) return {
11
- bodyBytes: object.body.buffer.slice(object.body.byteOffset, object.body.byteOffset + object.body.byteLength),
12
- body: void 0
13
- };
14
- return {
15
- bodyBytes: void 0
16
- };
1
+ import { pick, set } from '../polyfill/Lodash.mjs';
2
+ import { Console } from '../polyfill/Console.mjs';
3
+ import { StatusTexts } from '../polyfill/StatusTexts.mjs';
4
+ import { $app } from './app.mjs';
5
+ const transformQuantumultXBody = (object) => {
6
+ if (object.body instanceof ArrayBuffer) {
7
+ return { bodyBytes: object.body, body: undefined };
8
+ }
9
+ if (ArrayBuffer.isView(object.body)) {
10
+ return {
11
+ bodyBytes: object.body.buffer.slice(object.body.byteOffset, object.body.byteOffset + object.body.byteLength),
12
+ body: undefined,
13
+ };
14
+ }
15
+ return { bodyBytes: undefined };
17
16
  };
18
- const transformQuantumultXStatus = (object)=>{
19
- if ('number' == typeof object.status) return {
20
- status: `HTTP/1.1 ${object.status} ${__WEBPACK_EXTERNAL_MODULE__polyfill_StatusTexts_mjs__.StatusTexts[object.status]}`
21
- };
22
- if ('string' != typeof object.status && void 0 !== object.status) throw new TypeError(`${done.name}: 参数类型错误, status 必须为数字或字符串`);
17
+ const transformQuantumultXStatus = (object) => {
18
+ if (typeof object.status === 'number') {
19
+ return {
20
+ status: `HTTP/1.1 ${object.status} ${StatusTexts[object.status]}`,
21
+ };
22
+ }
23
+ if (typeof object.status !== 'string' && object.status !== undefined) {
24
+ throw new TypeError(`${done.name}: 参数类型错误, status 必须为数字或字符串`);
25
+ }
23
26
  return {};
24
27
  };
25
- const handleDoneFactory = (startTime)=>(result)=>{
26
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.log('🚩 执行结束!', startTime ? `🕛 ${((Date.now() - startTime) / 1000).toFixed(3)} 秒` : void 0);
28
+ const handleDoneFactory = (startTime) => {
29
+ return (result) => {
30
+ Console.log('🚩 执行结束!', startTime ? `🕛 ${((Date.now() - startTime) / 1000).toFixed(3)} 秒` : undefined);
27
31
  $done(result);
28
32
  };
29
- function done(object = {}) {
30
- let startTime = null == $script ? void 0 : $script.startTime;
31
- if ('Surge' === __WEBPACK_EXTERNAL_MODULE__app_mjs__.$app) startTime *= 1000;
33
+ };
34
+ /**
35
+ * Complete the script execution
36
+ */
37
+ export function done(object = {}) {
38
+ let startTime = $script?.startTime;
39
+ if ($app === 'Surge') {
40
+ startTime *= 1000;
41
+ }
32
42
  const handleDone = handleDoneFactory(startTime);
33
- switch(__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app){
43
+ switch ($app) {
34
44
  case 'Surge':
35
- if (object.policy) (0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(object, 'headers.X-Surge-Policy', object.policy);
45
+ if (object.policy) {
46
+ set(object, 'headers.X-Surge-Policy', object.policy);
47
+ }
36
48
  handleDone(object);
37
49
  break;
38
50
  case 'Loon':
39
- if (object.policy) object.node = object.policy;
51
+ if (object.policy) {
52
+ object.node = object.policy;
53
+ }
40
54
  handleDone(object);
41
55
  break;
42
56
  case 'Stash':
43
- if (object.policy) (0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(object, 'headers.X-Stash-Selected-Proxy', encodeURI(object.policy));
57
+ if (object.policy) {
58
+ set(object, 'headers.X-Stash-Selected-Proxy', encodeURI(object.policy));
59
+ }
44
60
  handleDone(object);
45
61
  break;
46
62
  case 'Egern':
@@ -49,27 +65,21 @@ function done(object = {}) {
49
65
  case 'Shadowrocket':
50
66
  handleDone(object);
51
67
  break;
52
- case 'Quantumult X':
53
- {
54
- const transformedObject = {
55
- ...(0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.pick)(object, [
56
- 'status',
57
- 'url',
58
- 'headers',
59
- 'body',
60
- 'bodyBytes'
61
- ]),
62
- ...transformQuantumultXStatus(object),
63
- ...transformQuantumultXBody(object)
64
- };
65
- if (object.policy) (0, __WEBPACK_EXTERNAL_MODULE__polyfill_Lodash_mjs__.set)(transformedObject, 'opts.policy', object.policy);
66
- handleDone(transformedObject);
67
- break;
68
+ case 'Quantumult X': {
69
+ const transformedObject = {
70
+ ...pick(object, ['status', 'url', 'headers', 'body', 'bodyBytes']),
71
+ ...transformQuantumultXStatus(object),
72
+ ...transformQuantumultXBody(object),
73
+ };
74
+ if (object.policy) {
75
+ set(transformedObject, 'opts.policy', object.policy);
68
76
  }
77
+ handleDone(transformedObject);
78
+ break;
79
+ }
69
80
  case 'Node.js':
70
81
  default:
71
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.log('🚩 执行结束!');
82
+ Console.log('🚩 执行结束!');
72
83
  process.exit(1);
73
84
  }
74
85
  }
75
- export { done };
@@ -1,6 +1,6 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__app_mjs__ from "./app.mjs";
2
- function environment_rslib_entry_environment() {
3
- switch(__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app){
1
+ import { $app } from './app.mjs';
2
+ export function environment() {
3
+ switch ($app) {
4
4
  case 'Surge':
5
5
  $environment.app = 'Surge';
6
6
  return $environment;
@@ -10,27 +10,26 @@ function environment_rslib_entry_environment() {
10
10
  case 'Egern':
11
11
  $environment.app = 'Egern';
12
12
  return $environment;
13
- case 'Loon':
14
- {
15
- const environment = $loon.split(' ');
16
- return {
17
- device: environment[0],
18
- ios: environment[1],
19
- 'loon-version': environment[2],
20
- app: 'Loon'
21
- };
22
- }
13
+ case 'Loon': {
14
+ const environment = $loon.split(' ');
15
+ return {
16
+ device: environment[0],
17
+ ios: environment[1],
18
+ 'loon-version': environment[2],
19
+ app: 'Loon',
20
+ };
21
+ }
23
22
  case 'Quantumult X':
24
23
  return {
25
- app: 'Quantumult X'
24
+ app: 'Quantumult X',
26
25
  };
27
26
  case 'Node.js':
28
27
  return {
29
28
  ...process.env,
30
- app: 'Node.js'
29
+ app: 'Node.js',
31
30
  };
32
31
  default:
33
32
  return {};
34
33
  }
35
34
  }
36
- export { environment_rslib_entry_environment as environment };
35
+ // export const $environment = environment();
@@ -0,0 +1,5 @@
1
+ export * from './app.mjs';
2
+ export * from './done.mjs';
3
+ export * from './notification.mjs';
4
+ export * from './time.mjs';
5
+ export * from './wait.mjs';
@@ -1,5 +1,5 @@
1
- export * from "./app.mjs";
2
- export * from "./done.mjs";
3
- export * from "./notification.mjs";
4
- export * from "./time.mjs";
5
- export * from "./wait.mjs";
1
+ export * from './app.mjs';
2
+ export * from './done.mjs';
3
+ export * from './notification.mjs';
4
+ export * from './time.mjs';
5
+ export * from './wait.mjs';
@@ -1,8 +1,11 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__ from "../polyfill/Console.mjs";
2
- import * as __WEBPACK_EXTERNAL_MODULE__app_mjs__ from "./app.mjs";
3
- function notification(title = `ℹ️ ${__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app} 通知`, subtitle = '', body = '', content = {}) {
1
+ import { Console } from '../polyfill/Console.mjs';
2
+ import { $app } from './app.mjs';
3
+ /**
4
+ * 系统通知
5
+ */
6
+ export function notification(title = `ℹ️ ${$app} 通知`, subtitle = '', body = '', content = {}) {
4
7
  const mutableContent = getMutableContent(content);
5
- switch(__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app){
8
+ switch ($app) {
6
9
  case 'Quantumult X':
7
10
  $notify(title, subtitle, body, mutableContent);
8
11
  break;
@@ -12,29 +15,31 @@ function notification(title = `ℹ️ ${__WEBPACK_EXTERNAL_MODULE__app_mjs__.$ap
12
15
  $notification.post(title, subtitle, body, mutableContent);
13
16
  break;
14
17
  }
15
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.group('📣 系统通知');
16
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.log(title, subtitle, body, JSON.stringify(mutableContent, null, 2));
17
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.groupEnd();
18
+ Console.group('📣 系统通知');
19
+ Console.log(title, subtitle, body, JSON.stringify(mutableContent, null, 2));
20
+ Console.groupEnd();
18
21
  }
19
22
  function getMutableContent(content) {
20
23
  const mutableContent = {};
21
- switch(typeof content){
24
+ switch (typeof content) {
22
25
  case 'string':
23
26
  case 'number':
24
27
  case 'boolean':
25
28
  assignSimpleContent(mutableContent, content);
26
29
  break;
27
30
  case 'object':
28
- if (content) assignObjectContent(mutableContent, content);
31
+ if (content) {
32
+ assignObjectContent(mutableContent, content);
33
+ }
29
34
  break;
30
35
  default:
31
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.error(`不支持的通知参数类型: ${typeof content}`);
36
+ Console.error(`不支持的通知参数类型: ${typeof content}`);
32
37
  break;
33
38
  }
34
39
  return mutableContent;
35
40
  }
36
41
  function assignSimpleContent(mutableContent, content) {
37
- switch(__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app){
42
+ switch ($app) {
38
43
  case 'Quantumult X':
39
44
  mutableContent['open-url'] = content;
40
45
  break;
@@ -51,15 +56,20 @@ function assignObjectContent(mutableContent, content) {
51
56
  const openUrl = content.open || content['open-url'] || content.url || content.openUrl;
52
57
  const copyUrl = content.copy || content['update-pasteboard'] || content.updatePasteboard;
53
58
  const mediaUrl = content.media || content['media-url'] || content.mediaUrl;
54
- switch(__WEBPACK_EXTERNAL_MODULE__app_mjs__.$app){
59
+ switch ($app) {
55
60
  case 'Quantumult X':
56
- if (openUrl) mutableContent['open-url'] = openUrl;
57
- if (null == mediaUrl ? void 0 : mediaUrl.startsWith('http')) mutableContent['media-url'] = mediaUrl;
58
- if (copyUrl) mutableContent['update-pasteboard'] = copyUrl;
61
+ if (openUrl)
62
+ mutableContent['open-url'] = openUrl;
63
+ if (mediaUrl?.startsWith('http'))
64
+ mutableContent['media-url'] = mediaUrl;
65
+ if (copyUrl)
66
+ mutableContent['update-pasteboard'] = copyUrl;
59
67
  break;
60
68
  case 'Loon':
61
- if (openUrl) mutableContent.openUrl = openUrl;
62
- if (null == mediaUrl ? void 0 : mediaUrl.startsWith('http')) mutableContent.mediaUrl = mediaUrl;
69
+ if (openUrl)
70
+ mutableContent.openUrl = openUrl;
71
+ if (mediaUrl?.startsWith('http'))
72
+ mutableContent.mediaUrl = mediaUrl;
63
73
  break;
64
74
  default:
65
75
  if (openUrl) {
@@ -70,32 +80,43 @@ function assignObjectContent(mutableContent, content) {
70
80
  mutableContent.action = 'clipboard';
71
81
  mutableContent.text = copyUrl;
72
82
  }
73
- if (mediaUrl) handleMediaContent(mutableContent, mediaUrl, content.mime);
74
- if (content['auto-dismiss']) mutableContent['auto-dismiss'] = content['auto-dismiss'];
75
- if (content.sound) mutableContent.sound = content.sound;
83
+ if (mediaUrl) {
84
+ handleMediaContent(mutableContent, mediaUrl, content.mime);
85
+ }
86
+ if (content['auto-dismiss'])
87
+ mutableContent['auto-dismiss'] = content['auto-dismiss'];
88
+ if (content.sound)
89
+ mutableContent.sound = content.sound;
76
90
  break;
77
91
  }
78
92
  }
79
93
  function handleMediaContent(mutableContent, mediaUrl, mime) {
80
- if (mediaUrl.startsWith('http')) mutableContent['media-url'] = mediaUrl;
94
+ if (mediaUrl.startsWith('http')) {
95
+ mutableContent['media-url'] = mediaUrl;
96
+ }
81
97
  else if (mediaUrl.startsWith('data:')) {
82
98
  const base64RegExp = /^data:(?<MIME>\w+\/\w+);base64,(?<Base64>.+)/;
83
99
  const match = mediaUrl.match(base64RegExp);
84
- if (null == match ? void 0 : match.groups) {
100
+ if (match?.groups) {
85
101
  mutableContent['media-base64'] = match.groups.Base64;
86
102
  mutableContent['media-base64-mime'] = mime || match.groups.MIME;
87
103
  }
88
- } else {
104
+ }
105
+ else {
89
106
  mutableContent['media-base64'] = mediaUrl;
90
107
  mutableContent['media-base64-mime'] = detectMimeType(mediaUrl);
91
108
  }
92
109
  }
93
110
  function detectMimeType(base64) {
94
- if (base64.startsWith('JVBERi0')) return 'application/pdf';
95
- if (base64.startsWith('R0lGODdh') || base64.startsWith('R0lGODlh')) return 'image/gif';
96
- if (base64.startsWith('iVBORw0KGgo')) return 'image/png';
97
- if (base64.startsWith('/9j/')) return 'image/jpeg';
98
- if (base64.startsWith('Qk02U')) return 'image/bmp';
111
+ if (base64.startsWith('JVBERi0'))
112
+ return 'application/pdf';
113
+ if (base64.startsWith('R0lGODdh') || base64.startsWith('R0lGODlh'))
114
+ return 'image/gif';
115
+ if (base64.startsWith('iVBORw0KGgo'))
116
+ return 'image/png';
117
+ if (base64.startsWith('/9j/'))
118
+ return 'image/jpeg';
119
+ if (base64.startsWith('Qk02U'))
120
+ return 'image/bmp';
99
121
  return 'application/octet-stream';
100
122
  }
101
- export { notification };
@@ -1,33 +1,42 @@
1
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__ from "../polyfill/Console.mjs";
2
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_Storage_mjs__ from "../polyfill/Storage.mjs";
3
- import * as __WEBPACK_EXTERNAL_MODULE__polyfill_fetch_mjs__ from "../polyfill/fetch.mjs";
4
- async function runScript(script, runOpts = {}) {
1
+ import { Console } from '../polyfill/Console.mjs';
2
+ import { Storage } from '../polyfill/Storage.mjs';
3
+ import { fetch } from '../polyfill/fetch.mjs';
4
+ export async function runScript(script, runOpts = {}) {
5
5
  try {
6
- var _Storage_getItem_replace, _Storage_getItem;
7
- const httpapi = null === (_Storage_getItem = __WEBPACK_EXTERNAL_MODULE__polyfill_Storage_mjs__.Storage.getItem('@chavy_boxjs_userCfgs.httpapi')) || void 0 === _Storage_getItem ? void 0 : null === (_Storage_getItem_replace = _Storage_getItem.replace(/\n/g, '')) || void 0 === _Storage_getItem_replace ? void 0 : _Storage_getItem_replace.trim();
8
- if (!httpapi) throw new Error('httpapi 配置未找到,请检查配置项!');
9
- const httpapiTimeoutFromConfig = Number.parseInt(__WEBPACK_EXTERNAL_MODULE__polyfill_Storage_mjs__.Storage.getItem('@chavy_boxjs_userCfgs.httpapi_timeout') || '20', 10);
6
+ // 获取 httpapi 配置
7
+ const httpapi = Storage.getItem('@chavy_boxjs_userCfgs.httpapi')?.replace(/\n/g, '')?.trim();
8
+ if (!httpapi) {
9
+ throw new Error('httpapi 配置未找到,请检查配置项!');
10
+ }
11
+ // 设置超时时间,优先使用参数传入的值
12
+ const httpapiTimeoutFromConfig = Number.parseInt(Storage.getItem('@chavy_boxjs_userCfgs.httpapi_timeout') || '20', 10);
10
13
  const timeout = runOpts.timeout ?? httpapiTimeoutFromConfig;
14
+ // 解析 httpapi 的地址和密码
11
15
  const [password, address] = httpapi.split('@');
12
- if (!password || !address) throw new Error('httpapi 配置格式错误,应为 password@address 格式!');
16
+ if (!password || !address) {
17
+ throw new Error('httpapi 配置格式错误,应为 password@address 格式!');
18
+ }
19
+ // 构建请求对象
13
20
  const request = {
14
21
  url: `http://${address}/v1/scripting/evaluate`,
15
22
  body: JSON.stringify({
16
23
  script_text: script,
17
24
  mock_type: 'cron',
18
- timeout
25
+ timeout,
19
26
  }),
20
27
  headers: {
21
28
  'X-Key': password,
22
- Accept: '*/*'
29
+ Accept: '*/*',
23
30
  },
24
- timeout
31
+ timeout,
25
32
  };
26
- const response = await (0, __WEBPACK_EXTERNAL_MODULE__polyfill_fetch_mjs__.fetch)(request);
27
- return response.body;
28
- } catch (error) {
29
- __WEBPACK_EXTERNAL_MODULE__polyfill_Console_mjs__.Console.error('运行脚本时发生错误:', error.message);
30
- throw error;
33
+ // 发起请求
34
+ const response = await fetch(request);
35
+ return response.body; // 返回响应体
36
+ }
37
+ catch (error) {
38
+ // 捕获错误并打印
39
+ Console.error('运行脚本时发生错误:', error.message);
40
+ throw error; // 如果需要,可以重新抛出错误
31
41
  }
32
42
  }
33
- export { runScript };
package/dist/lib/time.mjs CHANGED
@@ -1,4 +1,16 @@
1
- function time(format, ts) {
1
+ /**
2
+ * time
3
+ * 时间格式化
4
+ * [version of ISO8601]{@link https://262.ecma-international.org/5.1/#sec-15.9.1.15}
5
+ * 示例:time("yyyy-MM-dd qq HH:mm:ss.S") YYYY-MM-DDTHH:mm:ss.sssZ
6
+ * :time("yyyyMMddHHmmssS")
7
+ * YY:年 MM:月 dd:日 S:季 HH:时 m:分 ss:秒 sss:毫秒 Z:时区
8
+ * 其中y可选0-4位占位符、S可选0-1位占位符,其余可选0-2位占位符
9
+ * @param {string} format 格式化参数
10
+ * @param {number} ts 可选: 根据指定时间戳返回格式化日期
11
+ *
12
+ */
13
+ export function time(format, ts) {
2
14
  const date = ts ? new Date(ts) : new Date();
3
15
  const Time = {
4
16
  YY: date.getFullYear().toString().substring(3),
@@ -9,10 +21,11 @@ function time(format, ts) {
9
21
  mm: date.getMinutes().toString().padStart(2, '0'),
10
22
  sss: date.getMilliseconds().toString().padStart(3, '0'),
11
23
  ss: date.getSeconds().toString().padStart(2, '0'),
12
- S: `${Math.floor(date.getMonth() / 3) + 1}`
24
+ S: `${Math.floor(date.getMonth() / 3) + 1}`,
13
25
  };
14
26
  let result = format;
15
- for (const [key, value] of Object.entries(Time))result = result.replace(key, value);
27
+ for (const [key, value] of Object.entries(Time)) {
28
+ result = result.replace(key, value);
29
+ }
16
30
  return result;
17
31
  }
18
- export { time };