ph-utils 0.16.2 → 0.16.4

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/lib/array.d.ts CHANGED
@@ -59,14 +59,14 @@ export declare function symmetricDifference<T>(...arrs: T[][]): T[];
59
59
  * @param a2
60
60
  * @returns
61
61
  */
62
- export declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): any;
62
+ export declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): boolean;
63
63
  /**
64
64
  * 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。
65
65
  * @param arr1
66
66
  * @param arr2
67
67
  * @returns
68
68
  */
69
- export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
69
+ export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): boolean;
70
70
  /**
71
71
  * 返回一个布尔值,指示此集合是否与给定集合没有公共元素。
72
72
  *
@@ -76,4 +76,4 @@ export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>):
76
76
  * @param arr2
77
77
  * @returns
78
78
  */
79
- export declare function isDisjointFrom<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
79
+ export declare function isDisjointFrom<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): boolean;
package/lib/crypto.d.ts CHANGED
@@ -12,7 +12,7 @@ export declare function bufferToHex(bf: ArrayBuffer | Uint8Array, upper?: boolea
12
12
  * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
13
13
  * @returns
14
14
  */
15
- export declare function sha(message: string, upper?: boolean, algorithm?: string): Promise<string>;
15
+ export declare function sha(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
16
16
  /**
17
17
  * 哈希算法
18
18
  * @param message 待进行 hash 的数据
@@ -20,7 +20,7 @@ export declare function sha(message: string, upper?: boolean, algorithm?: string
20
20
  * @param algorithm hash算法, 支持: SHA-1、SHA-256、SHA-384、SHA-512; 默认为: SHA-256
21
21
  * @returns
22
22
  */
23
- export declare function hash(message: string, upper?: boolean, algorithm?: string): Promise<string>;
23
+ export declare function hash(message: string | ArrayBuffer, upper?: boolean, algorithm?: string): Promise<string>;
24
24
  type HMACAlgorithm = "SHA-256" | "SHA-512";
25
25
  /**
26
26
  * 使用 HMAC 算法计算消息的哈希值
package/lib/crypto.js CHANGED
@@ -59,8 +59,11 @@ function base64ToBuffer(data) {
59
59
  * @returns
60
60
  */
61
61
  export async function sha(message, upper = false, algorithm = "SHA-256") {
62
- const msgUint8 = new TextEncoder().encode(message);
63
- const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgUint8);
62
+ let msgBuffer = message;
63
+ if (typeof message === "string") {
64
+ msgBuffer = new TextEncoder().encode(message);
65
+ }
66
+ const hashBuffer = await globalThis.crypto.subtle.digest(algorithm || "SHA-256", msgBuffer);
64
67
  return bufferToHex(hashBuffer, upper);
65
68
  }
66
69
  /**
@@ -190,7 +193,7 @@ export async function aesEncrypt(message, key, encode = "hex", iv = null) {
190
193
  else if (typeof iv === "string") {
191
194
  iv = hexToBuffer(iv);
192
195
  }
193
- const encodeData = await encrypt({ ...algorithm, iv }, cryptoKey, message, encode);
196
+ const encodeData = await encrypt({ ...algorithm, iv: iv }, cryptoKey, message, encode);
194
197
  ciphertext = encodeData;
195
198
  resIv = bufferToHex(iv);
196
199
  return { ciphertext, iv: resIv, key };
@@ -43,7 +43,7 @@ export declare function aesEncrypt(key: string, input: string, upper?: boolean):
43
43
  * @param iv 向量
44
44
  * @returns
45
45
  */
46
- export declare function aesDecrypt(input: string, key: string, iv: string): string;
46
+ export declare function aesDecrypt(input: string, key: string | Buffer, iv?: string | Buffer, algorithm?: string): string;
47
47
  /**
48
48
  * RSA 公钥加密
49
49
  * @param input 待加密字符串
@@ -73,6 +73,16 @@ export function aesEncrypt(key, input, upper = false) {
73
73
  iv.toString("hex"),
74
74
  ];
75
75
  }
76
+ function aesAlgorithm(key) {
77
+ if (key.startsWith("aes-"))
78
+ return key;
79
+ let prefix = "aes-";
80
+ // 如果 key 不是以数字开头,则加上数字,例如:128,256 等
81
+ if (!/^\d/.test(key)) {
82
+ prefix += "256-";
83
+ }
84
+ return `${prefix}${key}`;
85
+ }
76
86
  /**
77
87
  * AES 解密
78
88
  * @param input 加密后的数据
@@ -80,8 +90,19 @@ export function aesEncrypt(key, input, upper = false) {
80
90
  * @param iv 向量
81
91
  * @returns
82
92
  */
83
- export function aesDecrypt(input, key, iv) {
84
- const cipher = createDecipheriv("aes-256-cbc", Buffer.from(key, "hex"), Buffer.from(iv, "hex"));
93
+ export function aesDecrypt(input, key, iv, algorithm = "aes-256-cbc") {
94
+ let ivBuffer = null;
95
+ if (iv && !Buffer.isBuffer(iv)) {
96
+ ivBuffer = Buffer.from(iv, "hex");
97
+ }
98
+ let keyBuffer;
99
+ if (Buffer.isBuffer(key)) {
100
+ keyBuffer = key;
101
+ }
102
+ else {
103
+ keyBuffer = Buffer.from(key, "hex");
104
+ }
105
+ const cipher = createDecipheriv(aesAlgorithm(algorithm), keyBuffer, ivBuffer);
85
106
  let decryptedData = cipher.update(input, "hex", "utf-8");
86
107
  decryptedData += cipher.final("utf-8");
87
108
  return decryptedData;
package/lib/dom.d.ts CHANGED
@@ -10,14 +10,14 @@
10
10
  type FormatStyleParam = (string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string;
11
11
  type FormatClassParam = (string | undefined | null | boolean)[] | Record<string, boolean | string | undefined | null | boolean> | string;
12
12
  type DocumentContext = HTMLElement | ShadowRoot | Document;
13
- export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
13
+ export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
14
14
  /**
15
15
  * 根据选择器获取 DOM 元素。
16
16
  * @param selector - 选择器字符串或 HTMLElement 实例。
17
17
  * @param dom - 可选参数,指定在哪个 DOM 节点下查找元素,默认为 document。
18
18
  * @returns 返回匹配到的 HTMLElement 实例。
19
19
  */
20
- export declare function $(selector: string | HTMLElement, dom?: DocumentContext): NodeListOf<HTMLElement> | HTMLElement[];
20
+ export declare function $(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
21
21
  /**
22
22
  * 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
23
23
  * @param tag - 元素标签名或 HTML 字符串。
@@ -46,7 +46,7 @@ export declare function $$(tag: string, option?: {
46
46
  * 根据选择器获取匹配的第一个 DOM 元素。
47
47
  * @param selector - 选择器字符串或直接的 HTMLElement。
48
48
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
49
- * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined
49
+ * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null
50
50
  */
51
51
  export declare function $one(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement | null;
52
52
  /**
@@ -75,14 +75,15 @@ export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
75
75
  * @param clazz - 要切换的类名。
76
76
  */
77
77
  export declare function toggleClass(el: HTMLElement, clazz: string): void;
78
+ type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
78
79
  /**
79
80
  * 为节点添加事件处理
80
81
  * @param {HTMLElement} element 添加事件的节点
81
82
  * @param {string} listener 事件名称
82
- * @param {function} event 事件处理函数
83
- * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
83
+ * @param {function} fn 事件处理函数
84
+ * @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
84
85
  */
85
- export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | (AddEventListenerOptions & {
86
+ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | (AddEventListenerOptions & {
86
87
  eventFlag?: string;
87
88
  })): void;
88
89
  /**
@@ -91,7 +92,7 @@ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCo
91
92
  * @param listener - 事件名称。
92
93
  * @param fn - 要移除的事件监听器函数。
93
94
  */
94
- export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventListener, option?: boolean | EventListenerOptions): void;
95
+ export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | EventListenerOptions): void;
95
96
  /**
96
97
  * 判断事件是否应该继续传递。
97
98
  * 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
@@ -118,7 +119,7 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
118
119
  * @param textstr 可选,如果传递该参数,则表示设置;否则表示获取
119
120
  * @returns
120
121
  */
121
- export declare function text(element: HTMLElement, textstr?: string): string | null | undefined;
122
+ export declare function text(element: HTMLElement, textstr?: string): string | undefined;
122
123
  export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
123
124
  export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>, fn: (el: HTMLElement, index: number) => any): void;
124
125
  /**
package/lib/dom.js CHANGED
@@ -55,7 +55,7 @@ export function create(tag, option = {}, ctx) {
55
55
  if (value === true) {
56
56
  $el.setAttribute(key, "");
57
57
  }
58
- else if (typeof value === "string") {
58
+ else {
59
59
  $el.setAttribute(key, value);
60
60
  }
61
61
  }
@@ -75,7 +75,7 @@ export function $$(tag, option = {}, ctx) {
75
75
  * 根据选择器获取匹配的第一个 DOM 元素。
76
76
  * @param selector - 选择器字符串或直接的 HTMLElement。
77
77
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
78
- * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 undefined
78
+ * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null
79
79
  */
80
80
  export function $one(selector, dom) {
81
81
  if (typeof selector === "string") {
@@ -126,8 +126,8 @@ export function toggleClass(el, clazz) {
126
126
  * 为节点添加事件处理
127
127
  * @param {HTMLElement} element 添加事件的节点
128
128
  * @param {string} listener 事件名称
129
- * @param {function} event 事件处理函数
130
- * @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
129
+ * @param {function} fn 事件处理函数
130
+ * @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
131
131
  */
132
132
  export function on(element, listener, fn, option) {
133
133
  if (element.length != null) {
@@ -527,7 +527,7 @@ export function transition(el, nameOrProperties, dir = "enter", finish) {
527
527
  });
528
528
  }
529
529
  }
530
- el.addEventListener("transitionend", (_e) => {
530
+ el.addEventListener("transitionend", () => {
531
531
  if (status === 0) {
532
532
  status = 1;
533
533
  if (nameClass) {
package/lib/file.js CHANGED
@@ -41,7 +41,7 @@ export async function write(file, data, opts) {
41
41
  if (opts.json === true && typeof data === "object") {
42
42
  writeData = JSON.stringify(data, null, opts.format === true ? 2 : 0);
43
43
  }
44
- return await fs.writeFile(path.resolve(file), writeData);
44
+ await fs.writeFile(path.resolve(file), writeData);
45
45
  }
46
46
  /**
47
47
  * 根据文件的 stat 获取文件的 etag
package/lib/id.d.ts CHANGED
@@ -1,3 +1,12 @@
1
+ type SnowflakeIDInfo = {
2
+ value: string;
3
+ timeOffset: bigint;
4
+ timestamp: number;
5
+ machineId: bigint;
6
+ sequence: bigint;
7
+ epoch: number;
8
+ version: string | undefined;
9
+ };
1
10
  /** 雪花ID, 推荐在全局构造一个对象用于生成id */
2
11
  export declare class SnowflakeID {
3
12
  /** 机器码, 默认为: 1 */
@@ -24,15 +33,7 @@ export declare class SnowflakeID {
24
33
  * @throws 如果时钟回退,抛出错误
25
34
  */
26
35
  generate(): string;
27
- parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): {
28
- value: string;
29
- timeOffset: bigint;
30
- timestamp: number;
31
- machineId: bigint;
32
- sequence: bigint;
33
- epoch: number;
34
- version: string | undefined;
35
- };
36
+ parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): SnowflakeIDInfo;
36
37
  }
37
38
  /** 将uuid转换为更简单的唯一标记id */
38
39
  export declare class ShortUUID {
@@ -64,3 +65,4 @@ export declare class ShortUUID {
64
65
  private _uuidHexToInt;
65
66
  private _uuidIntToHex;
66
67
  }
68
+ export {};
package/lib/id.js CHANGED
@@ -76,7 +76,7 @@ export class SnowflakeID {
76
76
  machineId: machineId,
77
77
  sequence: sequence,
78
78
  epoch: epochTime,
79
- version,
79
+ version: version,
80
80
  };
81
81
  }
82
82
  }
package/lib/logger.js CHANGED
@@ -1,4 +1,4 @@
1
- import { format } from "./date";
1
+ import { format } from "./date.js";
2
2
  /**
3
3
  * 日志记录器
4
4
  */
package/lib/server.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- /// <reference types="node" />
2
- import type { SpawnOptions } from "node:child_process";
1
+ import type { SpawnOptionsWithoutStdio } from "node:child_process";
3
2
  /**
4
3
  * 执行命令
5
4
  * @param command 待执行的命令
@@ -17,3 +16,18 @@ export declare function exec(command: string, args?: string[], options?: SpawnOp
17
16
  stdout: string;
18
17
  stderr: string;
19
18
  }>;
19
+ type SpawnOptions = SpawnOptionsWithoutStdio & {
20
+ shell?: 'powershell';
21
+ };
22
+ /**
23
+ * 执行命令并返回执行结果的Promise
24
+ * @param command 要执行的命令
25
+ * @param args 命令参数数组
26
+ * @param options 执行选项,支持指定shell类型
27
+ * @returns Promise对象,成功时resolve包含stdout和stderr的对象,失败时reject包含错误信息
28
+ */
29
+ export declare function spawn(command: string, args?: string[], options?: SpawnOptions): Promise<{
30
+ stdout: string;
31
+ stderr: string;
32
+ }>;
33
+ export {};
package/lib/server.js CHANGED
@@ -1,4 +1,4 @@
1
- import { execFile } from "node:child_process";
1
+ import { execFile, spawn as spawnOri } from "node:child_process";
2
2
  import { promisify } from "node:util";
3
3
  const execFilePromise = promisify(execFile);
4
4
  /**
@@ -27,3 +27,39 @@ export function exec(command, ...params) {
27
27
  }
28
28
  return execFilePromise(cmd, argvs, opts);
29
29
  }
30
+ /**
31
+ * 执行命令并返回执行结果的Promise
32
+ * @param command 要执行的命令
33
+ * @param args 命令参数数组
34
+ * @param options 执行选项,支持指定shell类型
35
+ * @returns Promise对象,成功时resolve包含stdout和stderr的对象,失败时reject包含错误信息
36
+ */
37
+ export function spawn(command, args, options = {}) {
38
+ return new Promise((resolve, reject) => {
39
+ let execArgs = [];
40
+ let cmd;
41
+ // 根据是否指定powershell shell来设置实际执行的命令和参数
42
+ if (options.shell === 'powershell') {
43
+ cmd = 'powershell.exe';
44
+ execArgs = ['-NoProfile', '-Command', command, ...(args || [])];
45
+ }
46
+ else {
47
+ cmd = command;
48
+ execArgs = args || [];
49
+ }
50
+ delete options.shell;
51
+ const child = spawnOri(cmd, execArgs, options);
52
+ let stdout = '', stderr = '';
53
+ child.stdout.on('data', d => stdout += d);
54
+ child.stderr.on('data', d => stderr += d);
55
+ // 监听子进程关闭事件,根据退出码决定resolve或reject
56
+ child.on('close', code => {
57
+ if (code === 0)
58
+ resolve({ stdout, stderr });
59
+ else
60
+ reject(new Error(`spawn failed (${code}): ${stderr}`));
61
+ });
62
+ // 监听子进程错误事件,发生错误时直接reject
63
+ child.on('error', reject);
64
+ });
65
+ }
package/lib/theme.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  /** 获取当前系统的主题 */
2
- export declare function getSystemTheme(): "dark" | "light" | "auto";
2
+ export declare function getSystemTheme(): "light" | "dark" | "auto";
3
3
  /**
4
4
  * 初始化主题, 让网页能够适应系统主题, 同时根据缓存的主题切换主题
5
5
  * @returns 当前应用的主题
6
6
  */
7
- export declare function initTheme(): Promise<unknown>;
7
+ export declare function initTheme(): Promise<"light" | "dark" | "auto">;
8
8
  /**
9
9
  * 切换主题, 通常用于预览
10
10
  * @param theme 切换的主题
11
11
  * @param transition 是否使用过渡动画, 注意浏览器必须支持 document.startViewTransition, 默认: true
12
12
  * @returns 切换后的主题
13
13
  */
14
- export declare function toggleTheme(theme?: "light" | "dark" | "auto", transition?: boolean): Promise<unknown>;
14
+ export declare function toggleTheme(theme?: "light" | "dark" | "auto", transition?: boolean): Promise<"light" | "dark" | "auto">;
15
15
  /** 获取当前主题 */
16
16
  export declare function getTheme(): string;
17
17
  /**
@@ -21,7 +21,7 @@ export declare function getTheme(): string;
21
21
  * @param transition 是否使用过渡动画, 注意浏览器必须支持 document.startViewTransition, 默认: true
22
22
  * @returns 应用的主题
23
23
  */
24
- export declare function applyTheme(theme?: "light" | "dark" | "auto", cache?: boolean, transition?: boolean): Promise<unknown>;
24
+ export declare function applyTheme(theme?: "light" | "dark" | "auto", cache?: boolean, transition?: boolean): Promise<"light" | "dark" | "auto">;
25
25
  /** 获取当前主题色 */
26
26
  export declare function getColorTheme(defaultValue?: string): string | undefined;
27
27
  /**
package/lib/theme.js CHANGED
@@ -19,10 +19,10 @@ export async function initTheme() {
19
19
  if ($themeStyle == null) {
20
20
  $themeStyle = document.createElement("style");
21
21
  $themeStyle.id = "theme-style";
22
- $themeStyle.innerHTML = `
23
- :root{color-scheme:light dark;}
24
- html.light{color-scheme: light;}
25
- html.dark {color-scheme: dark;}
22
+ $themeStyle.innerHTML = `
23
+ :root{color-scheme:light dark;}
24
+ html.light{color-scheme: light;}
25
+ html.dark {color-scheme: dark;}
26
26
  `;
27
27
  document.head.appendChild($themeStyle);
28
28
  }
package/package.json CHANGED
@@ -68,7 +68,7 @@
68
68
  },
69
69
  "./*": "./lib/*"
70
70
  },
71
- "version": "0.16.2",
71
+ "version": "0.16.4",
72
72
  "type": "module",
73
73
  "repository": {
74
74
  "type": "git",
@@ -95,8 +95,5 @@
95
95
  "date",
96
96
  "dom",
97
97
  "file"
98
- ],
99
- "scripts": {
100
- "build": "node scripts/build.js"
101
- }
98
+ ]
102
99
  }