ph-utils 0.20.0 → 1.0.0
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/browser.d.ts +416 -0
- package/lib/browser.js +858 -0
- package/lib/color-CUZrwrjD.js +206 -0
- package/lib/index.d.ts +615 -61
- package/lib/index.js +1539 -235
- package/lib/node.d.ts +115 -0
- package/lib/node.js +213 -0
- package/package.json +10 -64
- package/lib/array.d.ts +0 -79
- package/lib/array.js +0 -212
- package/lib/color.d.ts +0 -55
- package/lib/color.js +0 -294
- package/lib/config.d.ts +0 -33
- package/lib/config.js +0 -99
- package/lib/copy.d.ts +0 -11
- package/lib/copy.js +0 -101
- package/lib/crypto.d.ts +0 -74
- package/lib/crypto.js +0 -261
- package/lib/crypto_node.d.ts +0 -61
- package/lib/crypto_node.js +0 -133
- package/lib/date.d.ts +0 -66
- package/lib/date.js +0 -202
- package/lib/dom.d.ts +0 -265
- package/lib/dom.js +0 -635
- package/lib/file.d.ts +0 -29
- package/lib/file.js +0 -54
- package/lib/id.d.ts +0 -68
- package/lib/id.js +0 -170
- package/lib/logger.d.ts +0 -62
- package/lib/logger.js +0 -122
- package/lib/server.d.ts +0 -33
- package/lib/server.js +0 -65
- package/lib/storage.d.ts +0 -51
- package/lib/storage.js +0 -73
- package/lib/theme.d.ts +0 -44
- package/lib/theme.js +0 -156
- package/lib/validator.d.ts +0 -71
- package/lib/validator.js +0 -248
- package/lib/web.d.ts +0 -30
- package/lib/web.js +0 -100
package/lib/file.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/** nodejs 文件操作工具类 */
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
/**
|
|
5
|
-
* 读取文件内容
|
|
6
|
-
* @example <caption>1. 读取JSON文件, 内容为字符串列表</caption>
|
|
7
|
-
* read<string[]>('a.json', []);
|
|
8
|
-
* @example <caption>2. 读取JSON文件, 内容为对象</caption>
|
|
9
|
-
* read<{ name: string }>('b.json', {});
|
|
10
|
-
* @param filepath 文件路径
|
|
11
|
-
* @param defaultValue 文件不存在时默认值, 不传则抛异常, 如果传递的是对象形式则会将结果转换为 JSON
|
|
12
|
-
* @returns 文件内容
|
|
13
|
-
*/
|
|
14
|
-
export async function read(filepath, defaultValue) {
|
|
15
|
-
let content;
|
|
16
|
-
try {
|
|
17
|
-
content = await fs.readFile(filepath, "utf8");
|
|
18
|
-
if (defaultValue != null && typeof defaultValue === "object") {
|
|
19
|
-
return JSON.parse(content);
|
|
20
|
-
}
|
|
21
|
-
return content;
|
|
22
|
-
}
|
|
23
|
-
catch (error) {
|
|
24
|
-
if (defaultValue === undefined) {
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
return defaultValue;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 写入 JSON 格式的数据到文件
|
|
32
|
-
* @param file 待写入的文件
|
|
33
|
-
* @param data 待写入的数据
|
|
34
|
-
* @param opts 写入配置
|
|
35
|
-
* @property opts.json 是否写入 JSON 格式的数据,写入数据时对数据进行 JSON 格式化,默认为:true
|
|
36
|
-
* @property opts.format 是否在写入 json 数据时,将 JSON 数据格式化2个空格写入, 默认为 true
|
|
37
|
-
*/
|
|
38
|
-
export async function write(file, data, opts) {
|
|
39
|
-
let writeData = data.toString();
|
|
40
|
-
opts = { json: true, format: true, ...opts };
|
|
41
|
-
if (opts.json === true && typeof data === "object") {
|
|
42
|
-
writeData = JSON.stringify(data, null, opts.format === true ? 2 : 0);
|
|
43
|
-
}
|
|
44
|
-
await fs.writeFile(path.resolve(file), writeData);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* 根据文件的 stat 获取文件的 etag
|
|
48
|
-
* @param filePath 文件地址
|
|
49
|
-
* @returns file stat etag
|
|
50
|
-
*/
|
|
51
|
-
export async function statTag(filePath) {
|
|
52
|
-
let stat = await fs.stat(filePath);
|
|
53
|
-
return `${stat.size.toString(16)}-${stat.mtimeMs.toString(16)}`;
|
|
54
|
-
}
|
package/lib/id.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
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
|
-
};
|
|
10
|
-
/** 雪花ID, 推荐在全局构造一个对象用于生成id */
|
|
11
|
-
export declare class SnowflakeID {
|
|
12
|
-
/** 机器码, 默认为: 1 */
|
|
13
|
-
machineId: bigint;
|
|
14
|
-
/** 起始时间戳, 默认为:1288834974657 */
|
|
15
|
-
epoch: bigint;
|
|
16
|
-
_lastTimestamp: bigint;
|
|
17
|
-
/** 版本号, 默认为: 0 */
|
|
18
|
-
version: number;
|
|
19
|
-
private _sequence;
|
|
20
|
-
/**
|
|
21
|
-
* 构造函数
|
|
22
|
-
*
|
|
23
|
-
* @param machineId 机器标识,默认为1
|
|
24
|
-
* @param epoch 时间戳起始值,默认为1288834974657
|
|
25
|
-
*/
|
|
26
|
-
constructor(machineId?: number, epoch?: number);
|
|
27
|
-
private _getTimestamp;
|
|
28
|
-
private _waitNextMillis;
|
|
29
|
-
/**
|
|
30
|
-
* 生成雪花ID
|
|
31
|
-
*
|
|
32
|
-
* @returns 返回生成的唯一ID字符串
|
|
33
|
-
* @throws 如果时钟回退,抛出错误
|
|
34
|
-
*/
|
|
35
|
-
generate(): string;
|
|
36
|
-
parse(snowflakeID: string, epoch?: number, includeVersion?: boolean): SnowflakeIDInfo;
|
|
37
|
-
}
|
|
38
|
-
/** 将uuid转换为更简单的唯一标记id */
|
|
39
|
-
export declare class ShortUUID {
|
|
40
|
-
private alphabet;
|
|
41
|
-
/**
|
|
42
|
-
* 构造函数,用于初始化字母表
|
|
43
|
-
* @param {string} [alphabet] - 可选参数,用于指定自定义字母表
|
|
44
|
-
* 如果提供了alphabet参数,则将其设置为实例的字母表属性
|
|
45
|
-
* 如果未提供alphabet参数,则使用默认值
|
|
46
|
-
*/
|
|
47
|
-
constructor(alphabet?: string);
|
|
48
|
-
/**
|
|
49
|
-
* 将UUID字符串进行编码处理
|
|
50
|
-
* @param uuid - 需要编码的UUID字符串
|
|
51
|
-
* @returns 编码后的字符串
|
|
52
|
-
*/
|
|
53
|
-
encode(uuid: string, alphabet?: string): string;
|
|
54
|
-
/**
|
|
55
|
-
* 解码短UUID字符串,将其转换为UUID整数和十六进制格式
|
|
56
|
-
* @param shortUUID - 需要解码的短UUID字符串
|
|
57
|
-
* @returns 返回包含UUID整数和十六进制格式的对象
|
|
58
|
-
*/
|
|
59
|
-
decode(shortUUID: string, alphabet?: string): {
|
|
60
|
-
uuidInt: bigint;
|
|
61
|
-
uuid: string;
|
|
62
|
-
};
|
|
63
|
-
private _stringToInt;
|
|
64
|
-
private _intToString;
|
|
65
|
-
private _uuidHexToInt;
|
|
66
|
-
private _uuidIntToHex;
|
|
67
|
-
}
|
|
68
|
-
export {};
|
package/lib/id.js
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
/** 雪花ID, 推荐在全局构造一个对象用于生成id */
|
|
2
|
-
export class SnowflakeID {
|
|
3
|
-
/**
|
|
4
|
-
* 构造函数
|
|
5
|
-
*
|
|
6
|
-
* @param machineId 机器标识,默认为1
|
|
7
|
-
* @param epoch 时间戳起始值,默认为1288834974657
|
|
8
|
-
*/
|
|
9
|
-
constructor(machineId = 1, epoch = 1288834974657) {
|
|
10
|
-
/** 版本号, 默认为: 0 */
|
|
11
|
-
this.version = 0;
|
|
12
|
-
this.machineId = BigInt(machineId);
|
|
13
|
-
this.epoch = BigInt(epoch);
|
|
14
|
-
this._lastTimestamp = BigInt(0);
|
|
15
|
-
this._sequence = BigInt(0);
|
|
16
|
-
}
|
|
17
|
-
_getTimestamp() {
|
|
18
|
-
return BigInt(Date.now());
|
|
19
|
-
}
|
|
20
|
-
_waitNextMillis(cTimestamp) {
|
|
21
|
-
let timestamp = cTimestamp;
|
|
22
|
-
while (timestamp <= this._lastTimestamp) {
|
|
23
|
-
timestamp = BigInt(Date.now());
|
|
24
|
-
}
|
|
25
|
-
return timestamp;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* 生成雪花ID
|
|
29
|
-
*
|
|
30
|
-
* @returns 返回生成的唯一ID字符串
|
|
31
|
-
* @throws 如果时钟回退,抛出错误
|
|
32
|
-
*/
|
|
33
|
-
generate() {
|
|
34
|
-
let cTimestamp = this._getTimestamp();
|
|
35
|
-
if (cTimestamp < this._lastTimestamp) {
|
|
36
|
-
throw new Error("Clock moved backwards");
|
|
37
|
-
}
|
|
38
|
-
if (cTimestamp === this._lastTimestamp) {
|
|
39
|
-
// TODO: increment sequence
|
|
40
|
-
// 雪花id,末尾: 12位序列号(二进制12个1等于16进制4095),最多4096个(从0到4095)
|
|
41
|
-
this._sequence = (this._sequence + 1n) & BigInt("0xFFF");
|
|
42
|
-
if (this._sequence === 0n) {
|
|
43
|
-
// 同一时间戳,序列号溢出,等待下一毫秒
|
|
44
|
-
cTimestamp = this._waitNextMillis(cTimestamp);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
this._sequence = BigInt(0);
|
|
49
|
-
}
|
|
50
|
-
this._lastTimestamp = cTimestamp;
|
|
51
|
-
const timeDiff = cTimestamp - this.epoch;
|
|
52
|
-
let id = (timeDiff << BigInt(22)) |
|
|
53
|
-
(this.machineId << BigInt(12)) |
|
|
54
|
-
this._sequence;
|
|
55
|
-
let idstr = id.toString().padStart(19, "0");
|
|
56
|
-
return `${this.version}${idstr}`;
|
|
57
|
-
}
|
|
58
|
-
parse(snowflakeID, epoch, includeVersion = true) {
|
|
59
|
-
let version = undefined;
|
|
60
|
-
if (includeVersion) {
|
|
61
|
-
snowflakeID = snowflakeID.substring(1);
|
|
62
|
-
version = snowflakeID.substring(0, 1);
|
|
63
|
-
}
|
|
64
|
-
let epochTime = epoch || Number(this.epoch);
|
|
65
|
-
const id = BigInt(snowflakeID);
|
|
66
|
-
// 1FFFFFFFFFF 就是二进制的41个1, 雪花id前面41为为时间戳,间隔部分
|
|
67
|
-
let timeDiff = (id >> BigInt(22)) & BigInt("0x1FFFFFFFFFF");
|
|
68
|
-
const flowTime = Number(timeDiff) + epochTime;
|
|
69
|
-
// 雪花id中间10位为机器标识, 0x3FF(二进制10个1) 进行位运算
|
|
70
|
-
const machineId = (id >> BigInt(12)) & BigInt("0x3FF");
|
|
71
|
-
const sequence = id & BigInt("0xFFF");
|
|
72
|
-
return {
|
|
73
|
-
value: snowflakeID,
|
|
74
|
-
timeOffset: timeDiff,
|
|
75
|
-
timestamp: flowTime,
|
|
76
|
-
machineId: machineId,
|
|
77
|
-
sequence: sequence,
|
|
78
|
-
epoch: epochTime,
|
|
79
|
-
version: version,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
/** 将uuid转换为更简单的唯一标记id */
|
|
84
|
-
export class ShortUUID {
|
|
85
|
-
/**
|
|
86
|
-
* 构造函数,用于初始化字母表
|
|
87
|
-
* @param {string} [alphabet] - 可选参数,用于指定自定义字母表
|
|
88
|
-
* 如果提供了alphabet参数,则将其设置为实例的字母表属性
|
|
89
|
-
* 如果未提供alphabet参数,则使用默认值
|
|
90
|
-
*/
|
|
91
|
-
constructor(alphabet) {
|
|
92
|
-
this.alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
93
|
-
if (alphabet) {
|
|
94
|
-
this.alphabet = alphabet;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* 将UUID字符串进行编码处理
|
|
99
|
-
* @param uuid - 需要编码的UUID字符串
|
|
100
|
-
* @returns 编码后的字符串
|
|
101
|
-
*/
|
|
102
|
-
encode(uuid, alphabet) {
|
|
103
|
-
// 将UUID字符串转换为整数
|
|
104
|
-
const uuidInt = this._uuidHexToInt(uuid);
|
|
105
|
-
// 将整数转换为特定格式的字符串
|
|
106
|
-
return this._intToString(uuidInt, alphabet);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* 解码短UUID字符串,将其转换为UUID整数和十六进制格式
|
|
110
|
-
* @param shortUUID - 需要解码的短UUID字符串
|
|
111
|
-
* @returns 返回包含UUID整数和十六进制格式的对象
|
|
112
|
-
*/
|
|
113
|
-
decode(shortUUID, alphabet) {
|
|
114
|
-
const uuidInt = this._stringToInt(shortUUID, alphabet);
|
|
115
|
-
return {
|
|
116
|
-
uuidInt: uuidInt,
|
|
117
|
-
uuid: this._uuidIntToHex(uuidInt),
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
_stringToInt(str, alphabet) {
|
|
121
|
-
if (!alphabet)
|
|
122
|
-
alphabet = this.alphabet;
|
|
123
|
-
const alphabetlen = BigInt(alphabet.length);
|
|
124
|
-
let result = BigInt(0);
|
|
125
|
-
let multiplier = BigInt(1);
|
|
126
|
-
const strlen = str.length;
|
|
127
|
-
for (let i = strlen - 1; i >= 0; i--) {
|
|
128
|
-
const char = str[i];
|
|
129
|
-
const index = alphabet.indexOf(char);
|
|
130
|
-
if (index === -1) {
|
|
131
|
-
throw new Error(`Character "${char}" not found in alphabet`);
|
|
132
|
-
}
|
|
133
|
-
result += BigInt(index) * multiplier;
|
|
134
|
-
multiplier *= alphabetlen;
|
|
135
|
-
}
|
|
136
|
-
return result;
|
|
137
|
-
}
|
|
138
|
-
_intToString(uuidInt, alphabet) {
|
|
139
|
-
if (!alphabet)
|
|
140
|
-
alphabet = this.alphabet;
|
|
141
|
-
const alphabetlen = BigInt(alphabet.length);
|
|
142
|
-
let num = uuidInt;
|
|
143
|
-
const result = [];
|
|
144
|
-
// uuidInt = d₀ × 1(base的0次方) + d₁ × base + d₂ × base²...
|
|
145
|
-
while (num) {
|
|
146
|
-
const index = Number(num % alphabetlen);
|
|
147
|
-
num = num / alphabetlen;
|
|
148
|
-
result.push(alphabet.charAt(index));
|
|
149
|
-
}
|
|
150
|
-
// 倒序,保证高位在前
|
|
151
|
-
return result.reverse().join("");
|
|
152
|
-
}
|
|
153
|
-
_uuidHexToInt(uuid) {
|
|
154
|
-
const uuidHex = uuid.replaceAll("-", "");
|
|
155
|
-
return BigInt(`0x${uuidHex}`);
|
|
156
|
-
}
|
|
157
|
-
_uuidIntToHex(uuidInt) {
|
|
158
|
-
const uuidHexStr = uuidInt.toString(16);
|
|
159
|
-
if (!/^[0-9a-fA-F]{32}$/.test(uuidHexStr)) {
|
|
160
|
-
throw new Error("Invalid UUID string (must be 32 hex characters)");
|
|
161
|
-
}
|
|
162
|
-
return [
|
|
163
|
-
uuidHexStr.slice(0, 8),
|
|
164
|
-
uuidHexStr.slice(8, 12),
|
|
165
|
-
uuidHexStr.slice(12, 16),
|
|
166
|
-
uuidHexStr.slice(16, 20),
|
|
167
|
-
uuidHexStr.slice(20),
|
|
168
|
-
].join("-");
|
|
169
|
-
}
|
|
170
|
-
}
|
package/lib/logger.d.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
type LoggerLevel = "debug" | "info" | "warn" | "error" | "fatal";
|
|
2
|
-
/** 日志消息类型 */
|
|
3
|
-
type MessageIntf = number | string | boolean | object | any[] | Error;
|
|
4
|
-
/** 日志消息选项 */
|
|
5
|
-
type MessageOption = {
|
|
6
|
-
/** 日志名称 */
|
|
7
|
-
name?: string;
|
|
8
|
-
/** 日志级别 */
|
|
9
|
-
level?: LoggerLevel;
|
|
10
|
-
/** 日志消息 */
|
|
11
|
-
message: MessageIntf;
|
|
12
|
-
/** 当前消息是否美化输出 */
|
|
13
|
-
pretty?: boolean;
|
|
14
|
-
/** 如果消息是 JSON 格式, 转换为字符串时, 是否格式化 JSON */
|
|
15
|
-
jsonSpace?: number;
|
|
16
|
-
};
|
|
17
|
-
type LoggerOption = {
|
|
18
|
-
/** 记录的日志名称, 通常用于表明哪个页面 */
|
|
19
|
-
name?: string;
|
|
20
|
-
/** 记录日志级别, 通常上线时 error 或者禁用日志, 默认: debug */
|
|
21
|
-
level?: LoggerLevel;
|
|
22
|
-
/** 是否需要美化输出, 默认: true */
|
|
23
|
-
pretty?: boolean;
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* 日志记录器
|
|
27
|
-
*/
|
|
28
|
-
export declare class Logger {
|
|
29
|
-
/** 日志级别 */
|
|
30
|
-
levels: string[];
|
|
31
|
-
colors: {
|
|
32
|
-
debug: string;
|
|
33
|
-
info: string;
|
|
34
|
-
warn: string;
|
|
35
|
-
error: string;
|
|
36
|
-
};
|
|
37
|
-
option: LoggerOption & {
|
|
38
|
-
levelNum: number;
|
|
39
|
-
};
|
|
40
|
-
/** 构造日志记录器 */
|
|
41
|
-
constructor(option?: LoggerOption);
|
|
42
|
-
setOption(option: LoggerOption): void;
|
|
43
|
-
log(message: MessageIntf | MessageOption): void;
|
|
44
|
-
info(message: MessageIntf | MessageOption): void;
|
|
45
|
-
debug(message: MessageIntf | MessageOption): void;
|
|
46
|
-
warn(message: MessageIntf | MessageOption): void;
|
|
47
|
-
error(message: MessageIntf | MessageOption): void;
|
|
48
|
-
fatal(message: MessageIntf | MessageOption): void;
|
|
49
|
-
getLevel(): string;
|
|
50
|
-
getName(): string | undefined;
|
|
51
|
-
getOption(): LoggerOption & {
|
|
52
|
-
levelNum: number;
|
|
53
|
-
};
|
|
54
|
-
private generateLog;
|
|
55
|
-
/** 格式化显示 */
|
|
56
|
-
private formatShow;
|
|
57
|
-
}
|
|
58
|
-
/** 默认日志记录器 */
|
|
59
|
-
export declare const logger: Logger;
|
|
60
|
-
/** 获取新的日志记录器, 属性采用之前的 */
|
|
61
|
-
export declare const getLogger: (option?: LoggerOption) => Logger;
|
|
62
|
-
export {};
|
package/lib/logger.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { format } from "./date.js";
|
|
2
|
-
/**
|
|
3
|
-
* 日志记录器
|
|
4
|
-
*/
|
|
5
|
-
export class Logger {
|
|
6
|
-
/** 构造日志记录器 */
|
|
7
|
-
constructor(option) {
|
|
8
|
-
/** 日志级别 */
|
|
9
|
-
this.levels = ["debug", "info", "warn", "error", "fatal"];
|
|
10
|
-
this.colors = {
|
|
11
|
-
debug: "#909399",
|
|
12
|
-
info: "#1677ff",
|
|
13
|
-
warn: "#fadb14",
|
|
14
|
-
error: "#eb2f96",
|
|
15
|
-
};
|
|
16
|
-
this.setOption(option || {});
|
|
17
|
-
}
|
|
18
|
-
setOption(option) {
|
|
19
|
-
let opts = { level: "debug", levelNum: 0, pretty: true, ...option };
|
|
20
|
-
opts.levelNum = this.levels.indexOf(opts.level);
|
|
21
|
-
this.option = opts;
|
|
22
|
-
}
|
|
23
|
-
log(message) {
|
|
24
|
-
let msgInfo = {
|
|
25
|
-
name: this.option.name,
|
|
26
|
-
level: "info",
|
|
27
|
-
time: format(null, "yyyy-mm-dd HH:MM:ss.S"),
|
|
28
|
-
pretty: this.option.pretty,
|
|
29
|
-
jsonSpace: 0,
|
|
30
|
-
};
|
|
31
|
-
let msgContent = message;
|
|
32
|
-
if (typeof message === "object") {
|
|
33
|
-
if (message instanceof Error) {
|
|
34
|
-
msgInfo.level = "error";
|
|
35
|
-
msgContent = msgContent.stack;
|
|
36
|
-
}
|
|
37
|
-
else if (message.message != null) {
|
|
38
|
-
msgContent = message.message;
|
|
39
|
-
msgInfo = { ...msgInfo, ...message };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (typeof msgContent === "object") {
|
|
43
|
-
try {
|
|
44
|
-
msgInfo.message = JSON.stringify(msgContent, null, msgInfo.jsonSpace);
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
msgInfo.message = msgContent.toString();
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
msgInfo.message = msgContent;
|
|
52
|
-
}
|
|
53
|
-
if (msgInfo.pretty == null) {
|
|
54
|
-
msgInfo.pretty = this.option.pretty;
|
|
55
|
-
}
|
|
56
|
-
let levelIndex = this.levels.indexOf(msgInfo.level);
|
|
57
|
-
if (levelIndex >= this.option.levelNum) {
|
|
58
|
-
this.formatShow(msgInfo);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
info(message) {
|
|
62
|
-
this.log(this.generateLog(message, "info"));
|
|
63
|
-
}
|
|
64
|
-
debug(message) {
|
|
65
|
-
this.log(this.generateLog(message, "debug"));
|
|
66
|
-
}
|
|
67
|
-
warn(message) {
|
|
68
|
-
this.log(this.generateLog(message, "warn"));
|
|
69
|
-
}
|
|
70
|
-
error(message) {
|
|
71
|
-
this.log(this.generateLog(message, "error"));
|
|
72
|
-
}
|
|
73
|
-
fatal(message) {
|
|
74
|
-
this.log(this.generateLog(message, "fatal"));
|
|
75
|
-
}
|
|
76
|
-
getLevel() {
|
|
77
|
-
return this.levels[this.option.levelNum];
|
|
78
|
-
}
|
|
79
|
-
getName() {
|
|
80
|
-
return this.option.name;
|
|
81
|
-
}
|
|
82
|
-
getOption() {
|
|
83
|
-
return this.option;
|
|
84
|
-
}
|
|
85
|
-
generateLog(message, level = "info") {
|
|
86
|
-
let msgInfo = {
|
|
87
|
-
message: message,
|
|
88
|
-
};
|
|
89
|
-
if (typeof message === "object") {
|
|
90
|
-
if (message.message != null) {
|
|
91
|
-
msgInfo = message;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
msgInfo["level"] = level;
|
|
95
|
-
return msgInfo;
|
|
96
|
-
}
|
|
97
|
-
/** 格式化显示 */
|
|
98
|
-
formatShow(msg) {
|
|
99
|
-
if (msg.level === "fatal") {
|
|
100
|
-
msg.level = "error";
|
|
101
|
-
}
|
|
102
|
-
const res = [`[${msg.time}]`];
|
|
103
|
-
if (msg.name != null) {
|
|
104
|
-
res.push(`[${msg.name}]`);
|
|
105
|
-
}
|
|
106
|
-
res.push(`- ${msg.message}`);
|
|
107
|
-
if (!msg.pretty) {
|
|
108
|
-
res.splice(1, 0, `${msg.level.toUpperCase()}`);
|
|
109
|
-
console[msg.level](res.join(" "));
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
const color = this.colors[msg.level];
|
|
113
|
-
console.log(`%c ${`${msg.level.toUpperCase()}`} %c ${res.join(" ")} %c`, `background:${color};border:1px solid ${color}; padding: 1px; border-radius: 2px 0 0 2px; color: #fff;`, `border:1px solid ${color}; padding: 1px; border-radius: 0 2px 2px 0; color: ${color};`, "background:transparent");
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/** 默认日志记录器 */
|
|
118
|
-
export const logger = new Logger({ name: "APP" });
|
|
119
|
-
/** 获取新的日志记录器, 属性采用之前的 */
|
|
120
|
-
export const getLogger = (option) => {
|
|
121
|
-
return new Logger({ ...logger.getOption(), ...option });
|
|
122
|
-
};
|
package/lib/server.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { SpawnOptionsWithoutStdio } from "node:child_process";
|
|
2
|
-
/**
|
|
3
|
-
* 执行命令
|
|
4
|
-
* @param command 待执行的命令
|
|
5
|
-
* @param args 命令参数
|
|
6
|
-
*/
|
|
7
|
-
export declare function exec(command: string, args?: string[]): Promise<{
|
|
8
|
-
stdout: string;
|
|
9
|
-
stderr: string;
|
|
10
|
-
}>;
|
|
11
|
-
export declare function exec(command: string, options?: SpawnOptions): Promise<{
|
|
12
|
-
stdout: string;
|
|
13
|
-
stderr: string;
|
|
14
|
-
}>;
|
|
15
|
-
export declare function exec(command: string, args?: string[], options?: SpawnOptions): Promise<{
|
|
16
|
-
stdout: string;
|
|
17
|
-
stderr: string;
|
|
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
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { execFile, spawn as spawnOri } from "node:child_process";
|
|
2
|
-
import { promisify } from "node:util";
|
|
3
|
-
const execFilePromise = promisify(execFile);
|
|
4
|
-
/**
|
|
5
|
-
* 执行命令
|
|
6
|
-
* @param cmd 执行的命令
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
export function exec(command, ...params) {
|
|
10
|
-
let argvs = [];
|
|
11
|
-
const commandItems = command.split(" ");
|
|
12
|
-
const cmd = commandItems.shift();
|
|
13
|
-
if (commandItems.length > 0) {
|
|
14
|
-
argvs = commandItems;
|
|
15
|
-
}
|
|
16
|
-
let opts = { shell: true };
|
|
17
|
-
if (params[0] != null) {
|
|
18
|
-
if (params[0] instanceof Array) {
|
|
19
|
-
argvs.push(...params[0]);
|
|
20
|
-
if (params[1] != null) {
|
|
21
|
-
opts = params[1];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
opts = params[0];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return execFilePromise(cmd, argvs, opts);
|
|
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/storage.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 缓存相关
|
|
3
|
-
*/
|
|
4
|
-
type StorageType = "session" | "local";
|
|
5
|
-
interface StorageSetOption {
|
|
6
|
-
storage?: StorageType;
|
|
7
|
-
/** 数据有效期, 单位秒, 默认: -1 - 永久存储 */
|
|
8
|
-
expire?: number;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 存储值到 Storage 中
|
|
12
|
-
* @param key 设置的 key
|
|
13
|
-
* @param value 设置的值
|
|
14
|
-
* @param [option.storage] session 或 local, 默认: session
|
|
15
|
-
* @param [option.expire] 数据有效期, 单位秒, 默认: -1 - 永久存储
|
|
16
|
-
*
|
|
17
|
-
* @example <caption>1. 存储到 SessionStorage</caption>
|
|
18
|
-
* set("key", "value");
|
|
19
|
-
*
|
|
20
|
-
* @example <caption>2. 存储到 LocalStorage</caption>
|
|
21
|
-
* set("key", "value", { storage: "local" });
|
|
22
|
-
*/
|
|
23
|
-
export declare function set(key: string, value: any, option?: StorageSetOption): void;
|
|
24
|
-
/**
|
|
25
|
-
* 清空所有的缓存内容
|
|
26
|
-
* @param storage 待清空的缓存对象
|
|
27
|
-
*/
|
|
28
|
-
export declare function clear(storage?: StorageType): void;
|
|
29
|
-
/**
|
|
30
|
-
* 删除存储到 Storage 中的数据
|
|
31
|
-
* @param key
|
|
32
|
-
* @param storage
|
|
33
|
-
*/
|
|
34
|
-
export declare function remove(key: string, storage?: StorageType): void;
|
|
35
|
-
/** 从 Storage 中获取数据时的配置 */
|
|
36
|
-
interface StorageQueryOption {
|
|
37
|
-
/** 数据是否持久化, 默认为: false, 设置为 true 则会在每一次取出数据后删除 */
|
|
38
|
-
delete?: boolean;
|
|
39
|
-
/** 存储对象, session、local */
|
|
40
|
-
storage?: StorageType;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* 从 Storage 中取出数据
|
|
44
|
-
* @param key 保存时的 key
|
|
45
|
-
* @param defaultValue 没有数据时的默认值
|
|
46
|
-
* @param [option.delete] 是否在取出后,删除数据,默认:false - 取出后删除数据
|
|
47
|
-
* @param [option.storage] 使用的 Storage ,可以是 localStorage、sessionStorage, 默认: localStorage、sessionStorage
|
|
48
|
-
* @returns Storage 中 key 对应的数据
|
|
49
|
-
*/
|
|
50
|
-
export declare function get<T>(key: string, defaultValue?: T, option?: StorageQueryOption): T;
|
|
51
|
-
export {};
|