@alibarbar/common 1.1.1 → 1.1.3
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 +10 -0
- package/dist/crypto.cjs +184 -248
- package/dist/crypto.d.mts +50 -103
- package/dist/crypto.d.ts +50 -103
- package/dist/crypto.js +170 -234
- package/dist/{index-DchqyDBQ.d.mts → index-CqWtZgzO.d.mts} +1 -1
- package/dist/{index-DchqyDBQ.d.ts → index-CqWtZgzO.d.ts} +1 -1
- package/dist/index.cjs +432 -951
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +423 -930
- package/dist/services.cjs +82 -60
- package/dist/services.d.mts +1 -1
- package/dist/services.d.ts +1 -1
- package/dist/services.js +82 -60
- package/dist/storage.cjs +331 -854
- package/dist/storage.d.mts +58 -128
- package/dist/storage.d.ts +58 -128
- package/dist/storage.js +323 -848
- package/dist/upload.d.mts +1 -1
- package/dist/upload.d.ts +1 -1
- package/package.json +32 -27
package/dist/storage.d.mts
CHANGED
|
@@ -1,153 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* 存储配置选项
|
|
9
|
-
*/
|
|
10
|
-
interface StorageOptions {
|
|
11
|
-
/** RSA公钥(用于加密) */
|
|
12
|
-
publicKey?: CryptoKey;
|
|
13
|
-
/** RSA私钥(用于解密) */
|
|
14
|
-
privateKey?: CryptoKey;
|
|
15
|
-
/** 是否启用加密,默认为 true */
|
|
16
|
-
encrypt?: boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 存储初始化配置
|
|
20
|
-
*/
|
|
21
|
-
interface StorageInitOptions {
|
|
22
|
-
/** 是否自动生成密钥对(如果未设置),默认为 true */
|
|
23
|
-
autoGenerateKeys?: boolean;
|
|
24
|
-
/** 是否持久化密钥到 localStorage,默认为 false */
|
|
25
|
-
persistKeys?: boolean;
|
|
26
|
-
/** 密钥存储键名(用于持久化),如果未提供将自动生成随机键名 */
|
|
27
|
-
keyStorageKey?: string;
|
|
28
|
-
/** 用于加密持久化私钥的密码(如果提供,将使用 PBKDF2 + AES-GCM 加密) */
|
|
29
|
-
keyEncryptionPassword?: string;
|
|
30
|
-
/** PBKDF2 迭代次数,默认为 100000 */
|
|
31
|
-
pbkdf2Iterations?: number;
|
|
32
|
-
/** RSA密钥长度,默认为 2048 */
|
|
33
|
-
keyModulusLength?: number;
|
|
34
|
-
/** 是否启用HMAC数据完整性校验,默认为 true */
|
|
35
|
-
enableHMAC?: boolean;
|
|
36
|
-
/** 是否启用时间戳验证(防重放攻击),默认为 true */
|
|
37
|
-
enableTimestampValidation?: boolean;
|
|
38
|
-
/** 时间戳有效期(毫秒),默认 7 天,0 表示不验证 */
|
|
39
|
-
timestampMaxAge?: number;
|
|
40
|
-
/** 是否为生产环境(影响日志输出),默认为 false */
|
|
41
|
-
isProduction?: boolean;
|
|
42
|
-
/** 是否强制重新初始化(即使已经初始化过),默认为 false */
|
|
43
|
-
forceReinitialize?: boolean;
|
|
1
|
+
interface SecureStorageOptions {
|
|
2
|
+
/** 存储后端:localStorage 或 sessionStorage */
|
|
3
|
+
storage?: Storage;
|
|
4
|
+
/** 命名空间前缀 */
|
|
5
|
+
namespace?: string;
|
|
6
|
+
/** 默认过期时间(毫秒) */
|
|
7
|
+
defaultExpire?: number | null;
|
|
44
8
|
}
|
|
45
9
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @param options - 初始化选项
|
|
48
|
-
* @returns Promise<void>
|
|
49
|
-
*/
|
|
50
|
-
declare function initializeStorageKeys(options?: StorageInitOptions): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* 设置全局RSA密钥对
|
|
53
|
-
* @param keyPair - RSA密钥对
|
|
54
|
-
*/
|
|
55
|
-
declare function setStorageKeyPair(keyPair: RSAKeyPair): void;
|
|
56
|
-
/**
|
|
57
|
-
* 获取全局密钥对
|
|
58
|
-
* @returns 全局密钥对或null
|
|
59
|
-
*/
|
|
60
|
-
declare function getStorageKeyPair(): RSAKeyPair | null;
|
|
61
|
-
/**
|
|
62
|
-
* 清除持久化的密钥(如果存在)
|
|
63
|
-
*/
|
|
64
|
-
declare function clearPersistedKeys(): void;
|
|
65
|
-
/**
|
|
66
|
-
* 获取密钥使用计数
|
|
67
|
-
*/
|
|
68
|
-
declare function getKeyUsageCount(): number;
|
|
69
|
-
/**
|
|
70
|
-
* 重置密钥使用计数
|
|
71
|
-
*/
|
|
72
|
-
declare function resetKeyUsageCount(): void;
|
|
73
|
-
/**
|
|
74
|
-
* Cookie操作封装
|
|
10
|
+
* RSA 混合加密存储类(单例模式)
|
|
75
11
|
*/
|
|
76
|
-
declare
|
|
12
|
+
declare class SecureStorage {
|
|
13
|
+
private static _instances;
|
|
14
|
+
static get instances(): Map<string, SecureStorage>;
|
|
15
|
+
private readonly namespace;
|
|
16
|
+
private readonly storage;
|
|
17
|
+
private readonly defaultExpire;
|
|
18
|
+
private readonly instanceKey;
|
|
19
|
+
constructor(options: SecureStorageOptions);
|
|
77
20
|
/**
|
|
78
|
-
*
|
|
79
|
-
* @param key - 键
|
|
80
|
-
* @param value - 值
|
|
81
|
-
* @param options - Cookie选项
|
|
21
|
+
* 获取单例实例(静态方法)
|
|
82
22
|
*/
|
|
83
|
-
|
|
84
|
-
expires?: number | Date;
|
|
85
|
-
path?: string;
|
|
86
|
-
domain?: string;
|
|
87
|
-
secure?: boolean;
|
|
88
|
-
sameSite?: "strict" | "lax" | "none";
|
|
89
|
-
}): void;
|
|
23
|
+
static getInstance(options: SecureStorageOptions): SecureStorage;
|
|
90
24
|
/**
|
|
91
|
-
*
|
|
92
|
-
* @param key - 键
|
|
93
|
-
* @returns Cookie值
|
|
25
|
+
* 清除所有单例实例(用于测试或重置)
|
|
94
26
|
*/
|
|
95
|
-
|
|
27
|
+
static clearInstances(): void;
|
|
96
28
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param key - 键
|
|
99
|
-
* @param options - Cookie选项
|
|
29
|
+
* 获取当前所有实例的数量(用于调试)
|
|
100
30
|
*/
|
|
101
|
-
|
|
102
|
-
path?: string;
|
|
103
|
-
domain?: string;
|
|
104
|
-
}): void;
|
|
31
|
+
static getInstanceCount(): number;
|
|
105
32
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @returns Cookie对象
|
|
33
|
+
* 获取完整键名(带命名空间)
|
|
108
34
|
*/
|
|
109
|
-
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* 统一安全存储接口(自动选择存储方式,支持RSA加密)
|
|
113
|
-
* 这是推荐的主要导出接口
|
|
114
|
-
*/
|
|
115
|
-
declare const secureStorage: {
|
|
35
|
+
private _getFullKey;
|
|
116
36
|
/**
|
|
117
|
-
*
|
|
118
|
-
* @param key - 键
|
|
119
|
-
* @param value - 值
|
|
120
|
-
* @param options - 选项(过期时间、公钥等)
|
|
121
|
-
* @returns Promise<void>
|
|
37
|
+
* 检查数据是否过期
|
|
122
38
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
39
|
+
private _isExpired;
|
|
40
|
+
/**
|
|
41
|
+
* 设置值
|
|
42
|
+
*/
|
|
43
|
+
set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
|
|
127
44
|
/**
|
|
128
45
|
* 获取值
|
|
129
|
-
* @param key - 键
|
|
130
|
-
* @param options - 选项(默认值、私钥等)
|
|
131
|
-
* @returns Promise<T | undefined> 值或默认值
|
|
132
46
|
*/
|
|
133
|
-
get<T>(key: string,
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
47
|
+
get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
|
|
48
|
+
/**
|
|
49
|
+
* 删除值
|
|
50
|
+
*/
|
|
51
|
+
remove(key: string): boolean;
|
|
137
52
|
/**
|
|
138
|
-
*
|
|
139
|
-
* @param key - 键
|
|
53
|
+
* 检查键是否存在
|
|
140
54
|
*/
|
|
141
|
-
|
|
55
|
+
has(key: string): Promise<boolean>;
|
|
142
56
|
/**
|
|
143
|
-
*
|
|
57
|
+
* 清空所有数据
|
|
144
58
|
*/
|
|
145
|
-
clear():
|
|
59
|
+
clear(): number;
|
|
146
60
|
/**
|
|
147
61
|
* 获取所有键名
|
|
148
|
-
* @returns 键名数组
|
|
149
62
|
*/
|
|
150
63
|
keys(): string[];
|
|
151
|
-
|
|
64
|
+
/**
|
|
65
|
+
* 获取数据条数
|
|
66
|
+
*/
|
|
67
|
+
size(): number;
|
|
68
|
+
/**
|
|
69
|
+
* 清理过期数据
|
|
70
|
+
*/
|
|
71
|
+
clearExpired(): Promise<number>;
|
|
72
|
+
/**
|
|
73
|
+
* 获取实例的唯一标识
|
|
74
|
+
*/
|
|
75
|
+
getInstanceKey(): string;
|
|
76
|
+
/**
|
|
77
|
+
* 检查是否为单例实例
|
|
78
|
+
*/
|
|
79
|
+
isSingleton(): boolean;
|
|
80
|
+
}
|
|
81
|
+
declare const secureStorage: SecureStorage;
|
|
152
82
|
|
|
153
|
-
export {
|
|
83
|
+
export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };
|
package/dist/storage.d.ts
CHANGED
|
@@ -1,153 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* 存储配置选项
|
|
9
|
-
*/
|
|
10
|
-
interface StorageOptions {
|
|
11
|
-
/** RSA公钥(用于加密) */
|
|
12
|
-
publicKey?: CryptoKey;
|
|
13
|
-
/** RSA私钥(用于解密) */
|
|
14
|
-
privateKey?: CryptoKey;
|
|
15
|
-
/** 是否启用加密,默认为 true */
|
|
16
|
-
encrypt?: boolean;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 存储初始化配置
|
|
20
|
-
*/
|
|
21
|
-
interface StorageInitOptions {
|
|
22
|
-
/** 是否自动生成密钥对(如果未设置),默认为 true */
|
|
23
|
-
autoGenerateKeys?: boolean;
|
|
24
|
-
/** 是否持久化密钥到 localStorage,默认为 false */
|
|
25
|
-
persistKeys?: boolean;
|
|
26
|
-
/** 密钥存储键名(用于持久化),如果未提供将自动生成随机键名 */
|
|
27
|
-
keyStorageKey?: string;
|
|
28
|
-
/** 用于加密持久化私钥的密码(如果提供,将使用 PBKDF2 + AES-GCM 加密) */
|
|
29
|
-
keyEncryptionPassword?: string;
|
|
30
|
-
/** PBKDF2 迭代次数,默认为 100000 */
|
|
31
|
-
pbkdf2Iterations?: number;
|
|
32
|
-
/** RSA密钥长度,默认为 2048 */
|
|
33
|
-
keyModulusLength?: number;
|
|
34
|
-
/** 是否启用HMAC数据完整性校验,默认为 true */
|
|
35
|
-
enableHMAC?: boolean;
|
|
36
|
-
/** 是否启用时间戳验证(防重放攻击),默认为 true */
|
|
37
|
-
enableTimestampValidation?: boolean;
|
|
38
|
-
/** 时间戳有效期(毫秒),默认 7 天,0 表示不验证 */
|
|
39
|
-
timestampMaxAge?: number;
|
|
40
|
-
/** 是否为生产环境(影响日志输出),默认为 false */
|
|
41
|
-
isProduction?: boolean;
|
|
42
|
-
/** 是否强制重新初始化(即使已经初始化过),默认为 false */
|
|
43
|
-
forceReinitialize?: boolean;
|
|
1
|
+
interface SecureStorageOptions {
|
|
2
|
+
/** 存储后端:localStorage 或 sessionStorage */
|
|
3
|
+
storage?: Storage;
|
|
4
|
+
/** 命名空间前缀 */
|
|
5
|
+
namespace?: string;
|
|
6
|
+
/** 默认过期时间(毫秒) */
|
|
7
|
+
defaultExpire?: number | null;
|
|
44
8
|
}
|
|
45
9
|
/**
|
|
46
|
-
*
|
|
47
|
-
* @param options - 初始化选项
|
|
48
|
-
* @returns Promise<void>
|
|
49
|
-
*/
|
|
50
|
-
declare function initializeStorageKeys(options?: StorageInitOptions): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* 设置全局RSA密钥对
|
|
53
|
-
* @param keyPair - RSA密钥对
|
|
54
|
-
*/
|
|
55
|
-
declare function setStorageKeyPair(keyPair: RSAKeyPair): void;
|
|
56
|
-
/**
|
|
57
|
-
* 获取全局密钥对
|
|
58
|
-
* @returns 全局密钥对或null
|
|
59
|
-
*/
|
|
60
|
-
declare function getStorageKeyPair(): RSAKeyPair | null;
|
|
61
|
-
/**
|
|
62
|
-
* 清除持久化的密钥(如果存在)
|
|
63
|
-
*/
|
|
64
|
-
declare function clearPersistedKeys(): void;
|
|
65
|
-
/**
|
|
66
|
-
* 获取密钥使用计数
|
|
67
|
-
*/
|
|
68
|
-
declare function getKeyUsageCount(): number;
|
|
69
|
-
/**
|
|
70
|
-
* 重置密钥使用计数
|
|
71
|
-
*/
|
|
72
|
-
declare function resetKeyUsageCount(): void;
|
|
73
|
-
/**
|
|
74
|
-
* Cookie操作封装
|
|
10
|
+
* RSA 混合加密存储类(单例模式)
|
|
75
11
|
*/
|
|
76
|
-
declare
|
|
12
|
+
declare class SecureStorage {
|
|
13
|
+
private static _instances;
|
|
14
|
+
static get instances(): Map<string, SecureStorage>;
|
|
15
|
+
private readonly namespace;
|
|
16
|
+
private readonly storage;
|
|
17
|
+
private readonly defaultExpire;
|
|
18
|
+
private readonly instanceKey;
|
|
19
|
+
constructor(options: SecureStorageOptions);
|
|
77
20
|
/**
|
|
78
|
-
*
|
|
79
|
-
* @param key - 键
|
|
80
|
-
* @param value - 值
|
|
81
|
-
* @param options - Cookie选项
|
|
21
|
+
* 获取单例实例(静态方法)
|
|
82
22
|
*/
|
|
83
|
-
|
|
84
|
-
expires?: number | Date;
|
|
85
|
-
path?: string;
|
|
86
|
-
domain?: string;
|
|
87
|
-
secure?: boolean;
|
|
88
|
-
sameSite?: "strict" | "lax" | "none";
|
|
89
|
-
}): void;
|
|
23
|
+
static getInstance(options: SecureStorageOptions): SecureStorage;
|
|
90
24
|
/**
|
|
91
|
-
*
|
|
92
|
-
* @param key - 键
|
|
93
|
-
* @returns Cookie值
|
|
25
|
+
* 清除所有单例实例(用于测试或重置)
|
|
94
26
|
*/
|
|
95
|
-
|
|
27
|
+
static clearInstances(): void;
|
|
96
28
|
/**
|
|
97
|
-
*
|
|
98
|
-
* @param key - 键
|
|
99
|
-
* @param options - Cookie选项
|
|
29
|
+
* 获取当前所有实例的数量(用于调试)
|
|
100
30
|
*/
|
|
101
|
-
|
|
102
|
-
path?: string;
|
|
103
|
-
domain?: string;
|
|
104
|
-
}): void;
|
|
31
|
+
static getInstanceCount(): number;
|
|
105
32
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @returns Cookie对象
|
|
33
|
+
* 获取完整键名(带命名空间)
|
|
108
34
|
*/
|
|
109
|
-
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* 统一安全存储接口(自动选择存储方式,支持RSA加密)
|
|
113
|
-
* 这是推荐的主要导出接口
|
|
114
|
-
*/
|
|
115
|
-
declare const secureStorage: {
|
|
35
|
+
private _getFullKey;
|
|
116
36
|
/**
|
|
117
|
-
*
|
|
118
|
-
* @param key - 键
|
|
119
|
-
* @param value - 值
|
|
120
|
-
* @param options - 选项(过期时间、公钥等)
|
|
121
|
-
* @returns Promise<void>
|
|
37
|
+
* 检查数据是否过期
|
|
122
38
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
39
|
+
private _isExpired;
|
|
40
|
+
/**
|
|
41
|
+
* 设置值
|
|
42
|
+
*/
|
|
43
|
+
set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
|
|
127
44
|
/**
|
|
128
45
|
* 获取值
|
|
129
|
-
* @param key - 键
|
|
130
|
-
* @param options - 选项(默认值、私钥等)
|
|
131
|
-
* @returns Promise<T | undefined> 值或默认值
|
|
132
46
|
*/
|
|
133
|
-
get<T>(key: string,
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
47
|
+
get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
|
|
48
|
+
/**
|
|
49
|
+
* 删除值
|
|
50
|
+
*/
|
|
51
|
+
remove(key: string): boolean;
|
|
137
52
|
/**
|
|
138
|
-
*
|
|
139
|
-
* @param key - 键
|
|
53
|
+
* 检查键是否存在
|
|
140
54
|
*/
|
|
141
|
-
|
|
55
|
+
has(key: string): Promise<boolean>;
|
|
142
56
|
/**
|
|
143
|
-
*
|
|
57
|
+
* 清空所有数据
|
|
144
58
|
*/
|
|
145
|
-
clear():
|
|
59
|
+
clear(): number;
|
|
146
60
|
/**
|
|
147
61
|
* 获取所有键名
|
|
148
|
-
* @returns 键名数组
|
|
149
62
|
*/
|
|
150
63
|
keys(): string[];
|
|
151
|
-
|
|
64
|
+
/**
|
|
65
|
+
* 获取数据条数
|
|
66
|
+
*/
|
|
67
|
+
size(): number;
|
|
68
|
+
/**
|
|
69
|
+
* 清理过期数据
|
|
70
|
+
*/
|
|
71
|
+
clearExpired(): Promise<number>;
|
|
72
|
+
/**
|
|
73
|
+
* 获取实例的唯一标识
|
|
74
|
+
*/
|
|
75
|
+
getInstanceKey(): string;
|
|
76
|
+
/**
|
|
77
|
+
* 检查是否为单例实例
|
|
78
|
+
*/
|
|
79
|
+
isSingleton(): boolean;
|
|
80
|
+
}
|
|
81
|
+
declare const secureStorage: SecureStorage;
|
|
152
82
|
|
|
153
|
-
export {
|
|
83
|
+
export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };
|