@vlian/infrastructure 0.9.10 → 0.9.12
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/CHANGELOG.md +44 -0
- package/dist/cjs/managers/AuthManager/modules/core/StorageManager.js +70 -3
- package/dist/cjs/managers/AuthManager/modules/core/StorageManager.js.map +1 -1
- package/dist/esm/managers/AuthManager/modules/core/StorageManager.js +70 -3
- package/dist/esm/managers/AuthManager/modules/core/StorageManager.js.map +1 -1
- package/dist/types/managers/AuthManager/modules/core/StorageManager.d.ts +4 -0
- package/dist/types/managers/AuthManager/modules/core/StorageManager.d.ts.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,50 @@
|
|
|
5
5
|
格式基于 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.0.0/),
|
|
6
6
|
并且本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
|
7
7
|
|
|
8
|
+
## [0.9.12] - 2026-02-26
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- 增强 StorageManager 存储实例解析能力:
|
|
12
|
+
- 新增对 storage.instance 的标准化处理,统一支持 StorageInstance、浏览器 Storage API(getItem/setItem/removeItem)与 storage 容器对象
|
|
13
|
+
- 新增 isStorageInstance 与 isBrowserStorage 识别逻辑,并通过 normalizeStorageInstance 递归解析策略实例
|
|
14
|
+
- 在解析 @vlian/framework 存储实例后增加接口校验,解析失败时抛出明确错误,避免运行期静默异常
|
|
15
|
+
- 调整依赖配置,将 @vlian/framework 从发布版本依赖切换为本地文件依赖 file:/Users/xiaowenli/lowcode/Secra/code/secra-framework
|
|
16
|
+
- 移除 package.json 中 peerDependencies 的 @vlian/framework 声明,仅保留 peerDependenciesMeta 元数据配置
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
- 修复 AuthManager 在传入浏览器 Storage API 实例时无法直接作为存储后端使用的问题
|
|
20
|
+
- 修复 AuthManager 在 globalThis.vlianFramework.storage 为未初始化容器时的实例获取问题,自动尝试初始化并回退到可用策略存储
|
|
21
|
+
- 补充 AuthManager 测试用例与 Mock:
|
|
22
|
+
- 新增浏览器 Storage API 兼容性测试
|
|
23
|
+
- 新增未初始化 framework storage 容器兼容性测试
|
|
24
|
+
- 在测试清理阶段统一移除 globalThis.vlianFramework,避免测试间污染
|
|
25
|
+
|
|
26
|
+
### Infrastructure
|
|
27
|
+
- 更新 pnpm-lock.yaml 锁文件,同步本地 @vlian/framework 依赖来源及其传递依赖变化
|
|
28
|
+
|
|
29
|
+
## [0.9.11] - 2026-01-12
|
|
30
|
+
|
|
31
|
+
### Changed
|
|
32
|
+
- 更新 package.json 发布配置:
|
|
33
|
+
- 添加 `private: false` 配置,允许发布到 npm
|
|
34
|
+
- 添加 `publishConfig` 配置,设置为公开访问
|
|
35
|
+
- 添加 `author` 信息(name 和 email)
|
|
36
|
+
- 更新 `files` 字段,包含 CHANGELOG.md 和 LICENSE
|
|
37
|
+
- 添加 `prepack` 脚本,确保发布前构建
|
|
38
|
+
- 扩展 `keywords` 列表,包含所有模块名称
|
|
39
|
+
- 更新 `peerDependenciesMeta` 配置格式
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
- 修复 AuthManager 测试中的未处理 Promise rejection 问题
|
|
43
|
+
- 修复 AuthManager 测试中的异步操作等待问题,确保测试稳定性
|
|
44
|
+
- 修复 UserManager 并发测试中的异步等待问题,增加超时时间
|
|
45
|
+
- 修复 UserManager 状态订阅测试中的键名问题,使用正确的命名空间格式
|
|
46
|
+
|
|
47
|
+
### Infrastructure
|
|
48
|
+
- 添加 .npmignore 文件,控制发布内容
|
|
49
|
+
- 添加 LICENSE 文件
|
|
50
|
+
- 添加 README.md 文件
|
|
51
|
+
|
|
8
52
|
## [0.9.10] - 2026-01-12
|
|
9
53
|
|
|
10
54
|
### Added
|
|
@@ -29,12 +29,16 @@ let StorageManager = class StorageManager {
|
|
|
29
29
|
/**
|
|
30
30
|
* 初始化存储实例(带缓存优化)
|
|
31
31
|
*/ initStorage(storageConfig) {
|
|
32
|
+
const strategy = storageConfig.strategy || 'local';
|
|
32
33
|
// 如果直接提供了实例,直接使用(最快路径)
|
|
33
34
|
if (storageConfig.instance) {
|
|
34
|
-
|
|
35
|
+
const normalizedInstance = this.normalizeStorageInstance(storageConfig.instance, strategy);
|
|
36
|
+
if (normalizedInstance) {
|
|
37
|
+
return normalizedInstance;
|
|
38
|
+
}
|
|
39
|
+
throw new _types.AuthError('Invalid storage.instance. Expected StorageInstance, Web Storage API, or a storage container with local/session/indexedDB.', _types.AuthErrorCode.CONFIG_ERROR);
|
|
35
40
|
}
|
|
36
41
|
// 如果没有提供实例,尝试从 @vlian/framework 获取
|
|
37
|
-
const strategy = storageConfig.strategy || 'local';
|
|
38
42
|
const cacheKey = `${strategy}_${this.environment}`;
|
|
39
43
|
// 检查缓存
|
|
40
44
|
if (StorageManager.storageCache.has(cacheKey)) {
|
|
@@ -111,7 +115,11 @@ let StorageManager = class StorageManager {
|
|
|
111
115
|
session: storage.session,
|
|
112
116
|
indexedDB: storage.indexedDB
|
|
113
117
|
};
|
|
114
|
-
const
|
|
118
|
+
const storageCandidate = strategyMap[strategy] || storage.local || storage;
|
|
119
|
+
const storageInstance = this.normalizeStorageInstance(storageCandidate, strategy);
|
|
120
|
+
if (!storageInstance) {
|
|
121
|
+
throw new _types.AuthError('Resolved storage does not implement get/set/remove methods', _types.AuthErrorCode.ENVIRONMENT_ERROR);
|
|
122
|
+
}
|
|
115
123
|
// 缓存存储实例
|
|
116
124
|
StorageManager.storageCache.set(cacheKey, storageInstance);
|
|
117
125
|
return storageInstance;
|
|
@@ -119,6 +127,60 @@ let StorageManager = class StorageManager {
|
|
|
119
127
|
throw new _types.AuthError(`Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` + 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.', _types.AuthErrorCode.ENVIRONMENT_ERROR, undefined, error instanceof Error ? error : new Error(String(error)));
|
|
120
128
|
}
|
|
121
129
|
}
|
|
130
|
+
normalizeStorageInstance(candidate, strategy) {
|
|
131
|
+
if (!candidate) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
if (this.isStorageInstance(candidate)) {
|
|
135
|
+
return candidate;
|
|
136
|
+
}
|
|
137
|
+
if (this.isBrowserStorage(candidate)) {
|
|
138
|
+
return {
|
|
139
|
+
async get (key) {
|
|
140
|
+
return candidate.getItem(key);
|
|
141
|
+
},
|
|
142
|
+
async set (key, value) {
|
|
143
|
+
candidate.setItem(key, value);
|
|
144
|
+
},
|
|
145
|
+
async remove (key) {
|
|
146
|
+
candidate.removeItem(key);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
if (typeof candidate === 'object') {
|
|
151
|
+
const storageContainer = candidate;
|
|
152
|
+
const selected = storageContainer[strategy] || storageContainer.local;
|
|
153
|
+
if (selected) {
|
|
154
|
+
return this.normalizeStorageInstance(selected, strategy);
|
|
155
|
+
}
|
|
156
|
+
if (typeof storageContainer.initialize === 'function') {
|
|
157
|
+
try {
|
|
158
|
+
storageContainer.initialize();
|
|
159
|
+
const initializedSelected = storageContainer[strategy] || storageContainer.local;
|
|
160
|
+
if (initializedSelected) {
|
|
161
|
+
return this.normalizeStorageInstance(initializedSelected, strategy);
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
this.logger?.warn('Failed to initialize storage container:', error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
isStorageInstance(candidate) {
|
|
171
|
+
if (!candidate || typeof candidate !== 'object') {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
const target = candidate;
|
|
175
|
+
return StorageManager.STORAGE_METHODS.every((method)=>typeof target[method] === 'function');
|
|
176
|
+
}
|
|
177
|
+
isBrowserStorage(candidate) {
|
|
178
|
+
if (!candidate || typeof candidate !== 'object') {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
const target = candidate;
|
|
182
|
+
return typeof target.getItem === 'function' && typeof target.setItem === 'function' && typeof target.removeItem === 'function';
|
|
183
|
+
}
|
|
122
184
|
/**
|
|
123
185
|
* 从存储中恢复认证状态
|
|
124
186
|
*/ async restoreAuthState() {
|
|
@@ -257,5 +319,10 @@ let StorageManager = class StorageManager {
|
|
|
257
319
|
};
|
|
258
320
|
// 存储初始化缓存(优化性能)
|
|
259
321
|
_define_property(StorageManager, "storageCache", new Map());
|
|
322
|
+
_define_property(StorageManager, "STORAGE_METHODS", [
|
|
323
|
+
'get',
|
|
324
|
+
'set',
|
|
325
|
+
'remove'
|
|
326
|
+
]);
|
|
260
327
|
|
|
261
328
|
//# sourceMappingURL=StorageManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../src/managers/AuthManager/modules/core/StorageManager.ts"],"sourcesContent":["/**\n * 存储管理模块\n * 负责存储的初始化、数据的保存和恢复\n */\n\nimport type { StorageInstance, StorageStrategy, Environment, AuthData } from '../../types';\nimport { AuthError, AuthErrorCode } from '../../types';\nimport { EncryptionService } from './EncryptionService';\n\n/**\n * 存储管理器\n */\nexport class StorageManager {\n private storage: StorageInstance;\n private encryptionService: EncryptionService;\n private authDataKey: string;\n private environment: Environment;\n private logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n \n // 存储初始化缓存(优化性能)\n private static storageCache: Map<string, StorageInstance> = new Map();\n\n // 批量写入相关\n private batchWriteTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingWrite: (() => Promise<void>) | null = null;\n private isWriting: boolean = false;\n private batchWriteDelay: number;\n\n constructor(\n options: {\n storage: { instance?: StorageInstance; strategy?: StorageStrategy };\n encryptionService: EncryptionService;\n authDataKey: string;\n environment: Environment;\n batchWriteDelay: number;\n logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n }\n ) {\n this.encryptionService = options.encryptionService;\n this.authDataKey = options.authDataKey;\n this.environment = options.environment;\n this.batchWriteDelay = options.batchWriteDelay;\n this.logger = options.logger;\n this.storage = this.initStorage(options.storage);\n }\n\n /**\n * 初始化存储实例(带缓存优化)\n */\n private initStorage(storageConfig: { instance?: StorageInstance; strategy?: StorageStrategy }): StorageInstance {\n // 如果直接提供了实例,直接使用(最快路径)\n if (storageConfig.instance) {\n return storageConfig.instance;\n }\n\n // 如果没有提供实例,尝试从 @vlian/framework 获取\n const strategy = storageConfig.strategy || 'local';\n const cacheKey = `${strategy}_${this.environment}`;\n \n // 检查缓存\n if (StorageManager.storageCache.has(cacheKey)) {\n return StorageManager.storageCache.get(cacheKey)!;\n }\n \n try {\n // SSR 环境特殊处理\n if (this.environment === 'ssr') {\n // SSR 环境应该使用内存存储或服务端存储\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n\n // Worker 环境特殊处理\n if (this.environment === 'worker') {\n // Worker 环境不支持 localStorage,使用 IndexedDB 或内存存储\n if (strategy === 'local' || strategy === 'session') {\n this.logger?.warn(\n `${strategy} storage is not available in Worker environment, falling back to memory storage`\n );\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n }\n\n // 优化的获取逻辑:按优先级检查\n let storageModule: any;\n \n // 1. 检查全局变量(最快,无需动态导入)\n if (typeof globalThis !== 'undefined') {\n const global = globalThis as any;\n storageModule = global.window?.vlianFramework || global.vlianFramework;\n }\n \n // 2. 如果全局变量不存在,尝试使用 require(仅 Node.js/CommonJS 环境)\n if (!storageModule && typeof require !== 'undefined') {\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n storageModule = require('@vlian/framework');\n } catch {\n // require 失败,继续抛出错误\n }\n }\n \n // 如果仍然没有找到,抛出错误\n if (!storageModule) {\n throw new AuthError(\n '@vlian/framework is not available. ' +\n 'Please provide storage.instance or ensure @vlian/framework is installed and accessible.',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 获取 storage 对象(优化:减少属性访问)\n const storage = storageModule.storage || storageModule.default?.storage;\n if (!storage) {\n throw new AuthError(\n 'Storage not found in @vlian/framework',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 根据策略返回对应的存储实例(优化:使用映射表)\n const strategyMap: Record<string, StorageInstance | undefined> = {\n local: storage.local,\n session: storage.session,\n indexedDB: storage.indexedDB,\n };\n \n const storageInstance = strategyMap[strategy] || storage.local || storage;\n \n // 缓存存储实例\n StorageManager.storageCache.set(cacheKey, storageInstance);\n \n return storageInstance;\n } catch (error) {\n throw new AuthError(\n `Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` +\n 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.',\n AuthErrorCode.ENVIRONMENT_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 从存储中恢复认证状态\n */\n async restoreAuthState(): Promise<AuthData | null> {\n try {\n const encryptedData = await this.storage.get(this.authDataKey);\n if (encryptedData) {\n // 解密数据(如果配置了加密)\n const data = await this.encryptionService.decryptData(encryptedData);\n return JSON.parse(data) as AuthData;\n }\n return null;\n } catch (error) {\n this.logger?.error('Failed to restore auth state:', error);\n return null;\n }\n }\n\n /**\n * 保存认证状态到存储(内部实现,支持批量写入)\n */\n private async saveAuthStateImmediate(authData: AuthData | null): Promise<void> {\n try {\n if (authData) {\n const data = JSON.stringify(authData);\n // 加密数据(如果配置了加密)\n const encryptedData = await this.encryptionService.encryptData(data);\n await this.storage.set(this.authDataKey, encryptedData);\n } else {\n await this.storage.remove(this.authDataKey);\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.logger?.error('Failed to save auth state:', errorMessage);\n throw new AuthError(\n errorMessage,\n AuthErrorCode.STORAGE_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 保存认证状态到存储(支持批量写入优化)\n */\n async saveAuthState(authData: AuthData | null): Promise<void> {\n // 如果禁用了批量写入或延迟为0,立即写入\n if (this.batchWriteDelay === 0) {\n return this.saveAuthStateImmediate(authData);\n }\n\n // 清除之前的定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 创建待写入任务\n this.pendingWrite = () => {\n this.isWriting = true;\n const currentAuthData = authData;\n this.pendingWrite = null;\n return this.saveAuthStateImmediate(currentAuthData)\n .finally(() => {\n this.isWriting = false;\n });\n };\n\n // 设置延迟写入\n this.batchWriteTimer = setTimeout(() => {\n this.batchWriteTimer = null;\n if (this.pendingWrite) {\n this.pendingWrite().catch((error) => {\n this.logger?.error('Batch write failed:', error);\n });\n }\n }, this.batchWriteDelay);\n\n // 如果当前没有正在进行的写入,立即执行(首次写入)\n if (!this.isWriting) {\n // 使用微任务确保在当前同步操作完成后执行\n Promise.resolve().then(() => {\n if (this.pendingWrite && !this.isWriting) {\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n this.pendingWrite?.().catch((error) => {\n this.logger?.error('Immediate write failed:', error);\n });\n }\n });\n }\n }\n\n /**\n * 强制立即保存认证状态(用于关键操作,如登出)\n */\n async saveAuthStateForce(authData: AuthData | null): Promise<void> {\n // 清除批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 等待当前写入完成\n if (this.isWriting && this.pendingWrite) {\n await this.pendingWrite();\n }\n\n // 执行立即写入\n return this.saveAuthStateImmediate(authData);\n }\n\n /**\n * 获取存储实例(用于健康检查等)\n */\n getStorage(): StorageInstance {\n return this.storage;\n }\n\n /**\n * 清理资源\n */\n destroy(): void {\n // 清理批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n \n // 等待待处理的写入完成(如果正在写入)\n if (this.isWriting && this.pendingWrite) {\n // 注意:这里不等待 Promise 完成,因为 destroy 是同步方法\n // 但我们会标记为不再处理新的写入\n this.pendingWrite = null;\n }\n \n this.isWriting = false;\n this.pendingWrite = null;\n }\n}\n"],"names":["StorageManager","initStorage","storageConfig","instance","strategy","cacheKey","environment","storageCache","has","get","data","Map","memoryStorage","key","set","value","remove","delete","logger","warn","storageModule","globalThis","global","window","vlianFramework","require","AuthError","AuthErrorCode","ENVIRONMENT_ERROR","storage","default","strategyMap","local","session","indexedDB","storageInstance","error","Error","message","String","undefined","restoreAuthState","encryptedData","authDataKey","encryptionService","decryptData","JSON","parse","saveAuthStateImmediate","authData","stringify","encryptData","errorMessage","STORAGE_ERROR","saveAuthState","batchWriteDelay","batchWriteTimer","clearTimeout","pendingWrite","isWriting","currentAuthData","finally","setTimeout","catch","Promise","resolve","then","saveAuthStateForce","getStorage","destroy","options"],"mappings":"AAAA;;;CAGC;;;;+BASYA;;;eAAAA;;;uBAN4B;;;;;;;;;;;;;;AAMlC,IAAA,AAAMA,iBAAN,MAAMA;IAkCX;;GAEC,GACD,AAAQC,YAAYC,aAAyE,EAAmB;QAC9G,uBAAuB;QACvB,IAAIA,cAAcC,QAAQ,EAAE;YAC1B,OAAOD,cAAcC,QAAQ;QAC/B;QAEA,mCAAmC;QACnC,MAAMC,WAAWF,cAAcE,QAAQ,IAAI;QAC3C,MAAMC,WAAW,GAAGD,SAAS,CAAC,EAAE,IAAI,CAACE,WAAW,EAAE;QAElD,OAAO;QACP,IAAIN,eAAeO,Y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CAACG,SAAS,IAAI,IAAI,CAACD,YAAY,EAAE;YACvC,uCAAuC;YACvC,kBAAkB;YAClB,IAAI,CAACA,YAAY,GAAG;QACtB;QAEA,IAAI,CAACC,SAAS,GAAG;QACjB,IAAI,CAACD,YAAY,GAAG;IACtB;IA1RA,YACEY,OAOC,CACD;QAxBF,uBAAQzC,WAAR,KAAA;QACA,uBAAQe,qBAAR,KAAA;QACA,uBAAQD,eAAR,KAAA;QACA,uBAAQrC,eAAR,KAAA;QACA,uBAAQY,UAAR,KAAA;QAKA,SAAS;QACT,uBAAQsC,mBAAwD;QAChE,uBAAQE,gBAA6C;QACrD,uBAAQC,aAAqB;QAC7B,uBAAQJ,mBAAR,KAAA;QAYE,IAAI,CAACX,iBAAiB,GAAG0B,QAAQ1B,iBAAiB;QAClD,IAAI,CAACD,WAAW,GAAG2B,QAAQ3B,WAAW;QACtC,IAAI,CAACrC,WAAW,GAAGgE,QAAQhE,WAAW;QACtC,IAAI,CAACiD,eAAe,GAAGe,QAAQf,eAAe;QAC9C,IAAI,CAACrC,MAAM,GAAGoD,QAAQpD,MAAM;QAC5B,IAAI,CAACW,OAAO,GAAG,IAAI,CAAC5B,WAAW,CAACqE,QAAQzC,OAAO;IACjD;AA2QF;AApSE,gBAAgB;AAChB,iBARW7B,gBAQIO,gBAA6C,IAAII"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../src/managers/AuthManager/modules/core/StorageManager.ts"],"sourcesContent":["/**\n * 存储管理模块\n * 负责存储的初始化、数据的保存和恢复\n */\n\nimport type { StorageInstance, StorageStrategy, Environment, AuthData } from '../../types';\nimport { AuthError, AuthErrorCode } from '../../types';\nimport { EncryptionService } from './EncryptionService';\n\n/**\n * 存储管理器\n */\nexport class StorageManager {\n private storage: StorageInstance;\n private encryptionService: EncryptionService;\n private authDataKey: string;\n private environment: Environment;\n private logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n \n // 存储初始化缓存(优化性能)\n private static storageCache: Map<string, StorageInstance> = new Map();\n\n // 批量写入相关\n private batchWriteTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingWrite: (() => Promise<void>) | null = null;\n private isWriting: boolean = false;\n private batchWriteDelay: number;\n\n private static readonly STORAGE_METHODS = ['get', 'set', 'remove'] as const;\n\n constructor(\n options: {\n storage: { instance?: StorageInstance; strategy?: StorageStrategy };\n encryptionService: EncryptionService;\n authDataKey: string;\n environment: Environment;\n batchWriteDelay: number;\n logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n }\n ) {\n this.encryptionService = options.encryptionService;\n this.authDataKey = options.authDataKey;\n this.environment = options.environment;\n this.batchWriteDelay = options.batchWriteDelay;\n this.logger = options.logger;\n this.storage = this.initStorage(options.storage);\n }\n\n /**\n * 初始化存储实例(带缓存优化)\n */\n private initStorage(storageConfig: { instance?: StorageInstance; strategy?: StorageStrategy }): StorageInstance {\n const strategy = storageConfig.strategy || 'local';\n\n // 如果直接提供了实例,直接使用(最快路径)\n if (storageConfig.instance) {\n const normalizedInstance = this.normalizeStorageInstance(storageConfig.instance, strategy);\n if (normalizedInstance) {\n return normalizedInstance;\n }\n throw new AuthError(\n 'Invalid storage.instance. Expected StorageInstance, Web Storage API, or a storage container with local/session/indexedDB.',\n AuthErrorCode.CONFIG_ERROR\n );\n }\n\n // 如果没有提供实例,尝试从 @vlian/framework 获取\n const cacheKey = `${strategy}_${this.environment}`;\n \n // 检查缓存\n if (StorageManager.storageCache.has(cacheKey)) {\n return StorageManager.storageCache.get(cacheKey)!;\n }\n \n try {\n // SSR 环境特殊处理\n if (this.environment === 'ssr') {\n // SSR 环境应该使用内存存储或服务端存储\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n\n // Worker 环境特殊处理\n if (this.environment === 'worker') {\n // Worker 环境不支持 localStorage,使用 IndexedDB 或内存存储\n if (strategy === 'local' || strategy === 'session') {\n this.logger?.warn(\n `${strategy} storage is not available in Worker environment, falling back to memory storage`\n );\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n }\n\n // 优化的获取逻辑:按优先级检查\n let storageModule: any;\n \n // 1. 检查全局变量(最快,无需动态导入)\n if (typeof globalThis !== 'undefined') {\n const global = globalThis as any;\n storageModule = global.window?.vlianFramework || global.vlianFramework;\n }\n \n // 2. 如果全局变量不存在,尝试使用 require(仅 Node.js/CommonJS 环境)\n if (!storageModule && typeof require !== 'undefined') {\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n storageModule = require('@vlian/framework');\n } catch {\n // require 失败,继续抛出错误\n }\n }\n \n // 如果仍然没有找到,抛出错误\n if (!storageModule) {\n throw new AuthError(\n '@vlian/framework is not available. ' +\n 'Please provide storage.instance or ensure @vlian/framework is installed and accessible.',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 获取 storage 对象(优化:减少属性访问)\n const storage = storageModule.storage || storageModule.default?.storage;\n if (!storage) {\n throw new AuthError(\n 'Storage not found in @vlian/framework',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 根据策略返回对应的存储实例(优化:使用映射表)\n const strategyMap: Record<string, unknown> = {\n local: storage.local,\n session: storage.session,\n indexedDB: storage.indexedDB,\n };\n \n const storageCandidate = strategyMap[strategy] || storage.local || storage;\n const storageInstance = this.normalizeStorageInstance(storageCandidate, strategy);\n if (!storageInstance) {\n throw new AuthError(\n 'Resolved storage does not implement get/set/remove methods',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 缓存存储实例\n StorageManager.storageCache.set(cacheKey, storageInstance);\n \n return storageInstance;\n } catch (error) {\n throw new AuthError(\n `Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` +\n 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.',\n AuthErrorCode.ENVIRONMENT_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n private normalizeStorageInstance(candidate: unknown, strategy: StorageStrategy): StorageInstance | null {\n if (!candidate) {\n return null;\n }\n\n if (this.isStorageInstance(candidate)) {\n return candidate;\n }\n\n if (this.isBrowserStorage(candidate)) {\n return {\n async get(key: string): Promise<string | null> {\n return candidate.getItem(key);\n },\n async set(key: string, value: string): Promise<void> {\n candidate.setItem(key, value);\n },\n async remove(key: string): Promise<void> {\n candidate.removeItem(key);\n },\n };\n }\n\n if (typeof candidate === 'object') {\n const storageContainer = candidate as {\n local?: unknown;\n session?: unknown;\n indexedDB?: unknown;\n initialize?: (options?: unknown) => void;\n };\n const selected = storageContainer[strategy] || storageContainer.local;\n if (selected) {\n return this.normalizeStorageInstance(selected, strategy);\n }\n\n if (typeof storageContainer.initialize === 'function') {\n try {\n storageContainer.initialize();\n const initializedSelected = storageContainer[strategy] || storageContainer.local;\n if (initializedSelected) {\n return this.normalizeStorageInstance(initializedSelected, strategy);\n }\n } catch (error) {\n this.logger?.warn('Failed to initialize storage container:', error);\n }\n }\n }\n\n return null;\n }\n\n private isStorageInstance(candidate: unknown): candidate is StorageInstance {\n if (!candidate || typeof candidate !== 'object') {\n return false;\n }\n const target = candidate as Record<string, unknown>;\n return StorageManager.STORAGE_METHODS.every((method) => typeof target[method] === 'function');\n }\n\n private isBrowserStorage(candidate: unknown): candidate is {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n } {\n if (!candidate || typeof candidate !== 'object') {\n return false;\n }\n const target = candidate as Record<string, unknown>;\n return (\n typeof target.getItem === 'function' &&\n typeof target.setItem === 'function' &&\n typeof target.removeItem === 'function'\n );\n }\n\n /**\n * 从存储中恢复认证状态\n */\n async restoreAuthState(): Promise<AuthData | null> {\n try {\n const encryptedData = await this.storage.get(this.authDataKey);\n if (encryptedData) {\n // 解密数据(如果配置了加密)\n const data = await this.encryptionService.decryptData(encryptedData);\n return JSON.parse(data) as AuthData;\n }\n return null;\n } catch (error) {\n this.logger?.error('Failed to restore auth state:', error);\n return null;\n }\n }\n\n /**\n * 保存认证状态到存储(内部实现,支持批量写入)\n */\n private async saveAuthStateImmediate(authData: AuthData | null): Promise<void> {\n try {\n if (authData) {\n const data = JSON.stringify(authData);\n // 加密数据(如果配置了加密)\n const encryptedData = await this.encryptionService.encryptData(data);\n await this.storage.set(this.authDataKey, encryptedData);\n } else {\n await this.storage.remove(this.authDataKey);\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.logger?.error('Failed to save auth state:', errorMessage);\n throw new AuthError(\n errorMessage,\n AuthErrorCode.STORAGE_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 保存认证状态到存储(支持批量写入优化)\n */\n async saveAuthState(authData: AuthData | null): Promise<void> {\n // 如果禁用了批量写入或延迟为0,立即写入\n if (this.batchWriteDelay === 0) {\n return this.saveAuthStateImmediate(authData);\n }\n\n // 清除之前的定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 创建待写入任务\n this.pendingWrite = () => {\n this.isWriting = true;\n const currentAuthData = authData;\n this.pendingWrite = null;\n return this.saveAuthStateImmediate(currentAuthData)\n .finally(() => {\n this.isWriting = false;\n });\n };\n\n // 设置延迟写入\n this.batchWriteTimer = setTimeout(() => {\n this.batchWriteTimer = null;\n if (this.pendingWrite) {\n this.pendingWrite().catch((error) => {\n this.logger?.error('Batch write failed:', error);\n });\n }\n }, this.batchWriteDelay);\n\n // 如果当前没有正在进行的写入,立即执行(首次写入)\n if (!this.isWriting) {\n // 使用微任务确保在当前同步操作完成后执行\n Promise.resolve().then(() => {\n if (this.pendingWrite && !this.isWriting) {\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n this.pendingWrite?.().catch((error) => {\n this.logger?.error('Immediate write failed:', error);\n });\n }\n });\n }\n }\n\n /**\n * 强制立即保存认证状态(用于关键操作,如登出)\n */\n async saveAuthStateForce(authData: AuthData | null): Promise<void> {\n // 清除批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 等待当前写入完成\n if (this.isWriting && this.pendingWrite) {\n await this.pendingWrite();\n }\n\n // 执行立即写入\n return this.saveAuthStateImmediate(authData);\n }\n\n /**\n * 获取存储实例(用于健康检查等)\n */\n getStorage(): StorageInstance {\n return this.storage;\n }\n\n /**\n * 清理资源\n */\n destroy(): void {\n // 清理批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n \n // 等待待处理的写入完成(如果正在写入)\n if (this.isWriting && this.pendingWrite) {\n // 注意:这里不等待 Promise 完成,因为 destroy 是同步方法\n // 但我们会标记为不再处理新的写入\n this.pendingWrite = null;\n }\n \n this.isWriting = false;\n this.pendingWrite = null;\n }\n}\n"],"names":["StorageManager","initStorage","storageConfig","strategy","instance","normalizedInstance","normalizeStorageInstance","AuthError","AuthErrorCode","CONFIG_ERROR","cacheKey","environment","storageCache","has","get","data","Map","memoryStorage","key","set","value","remove","delete","logger","warn","storageModule","globalThis","global","window","vlianFramework","require","ENVIRONMENT_ERROR","storage","default","strategyMap","local","session","indexedDB","storageCandidate","storageInstance","error","Error","message","String","undefined","candidate","isStorageInstance","isBrowserStorage","getItem","setItem","removeItem","storageContainer","selected","initialize","initializedSelected","target","STORAGE_METHODS","every","method","restoreAuthState","encryptedData","authDataKey","encryptionService","decryptData","JSON","parse","saveAuthStateImmediate","authData","stringify","encryptData","errorMessage","STORAGE_ERROR","saveAuthState","batchWriteDelay","batchWriteTimer","clearTimeout","pendingWrite","isWriting","currentAuthData","finally","setTimeout","catch","Promise","resolve","then","saveAuthStateForce","getStorage","destroy","options"],"mappings":"AAAA;;;CAGC;;;;+BASYA;;;eAAAA;;;uBAN4B;;;;;;;;;;;;;;AAMlC,IAAA,AAAMA,iBAAN,MAAMA;IAoCX;;GAEC,GACD,AAAQC,YAAYC,aAAyE,EAAmB;QAC9G,MAAMC,WAAWD,cAAcC,QAAQ,IAAI;QAE3C,uBAAuB;QACvB,IAAID,cAAcE,QAAQ,EAAE;YAC1B,MAAMC,qBAAqB,IAAI,CAACC,wBAAwB,CAACJ,cAAcE,QAAQ,EAAED;YACjF,IAAIE,oBAAoB;gBACtB,OAAOA;YACT;YACA,MAAM,IAAIE,gBAAS,CACjB,6HACAC,oBAAa,CAACC,YAAY;QAE9B;QAEA,mCAAmC;QACnC,MAAMC,WAAW,GAAGP,SAAS,CAAC,EAAE,IAAI,CAACQ,WAAW,EAAE;QAElD,OAAO;QACP,IAAIX,eAAeY,YAAY,CAACC,GAAG,CAACH,WAAW;YAC7C,OAAOV,eAAeY,YAAY,CAACE,GAAG,CAACJ;QACzC;QAEA,IAAI;YACF,aAAa;YACb,IAAI,IAAI,CAACC,WAAW,KAAK,OAAO;gBAC9B,uBAAuB;gBACvB,MAAMI,OAAO,IAAIC;gBACjB,MAAMC,gBAAiC;oBACrC,MAAMH,KAAII,GAAW;wBACnB,OAAOH,KAAKD,GAAG,CAACI,QAAQ;oBAC1B;oBACA,MAAMC,KAAID,GAAW,EAAEE,KAAa;wBAClCL,KAAKI,GAAG,CAACD,KAAKE;oBAChB;oBACA,MAAMC,QAAOH,GAAW;wBACtBH,KAAKO,MAAM,CAACJ;oBACd;gBACF;gBACAlB,eAAeY,YAAY,CAACO,GAAG,CAACT,UAAUO;gBAC1C,OAAOA;YACT;YAEA,gBAAgB;YAChB,IAAI,IAA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GAAG;IACtB;IApXA,YACEY,OAOC,CACD;QA1BF,uBAAQxD,WAAR,KAAA;QACA,uBAAQ8B,qBAAR,KAAA;QACA,uBAAQD,eAAR,KAAA;QACA,uBAAQlD,eAAR,KAAA;QACA,uBAAQY,UAAR,KAAA;QAKA,SAAS;QACT,uBAAQmD,mBAAwD;QAChE,uBAAQE,gBAA6C;QACrD,uBAAQC,aAAqB;QAC7B,uBAAQJ,mBAAR,KAAA;QAcE,IAAI,CAACX,iBAAiB,GAAG0B,QAAQ1B,iBAAiB;QAClD,IAAI,CAACD,WAAW,GAAG2B,QAAQ3B,WAAW;QACtC,IAAI,CAAClD,WAAW,GAAG6E,QAAQ7E,WAAW;QACtC,IAAI,CAAC8D,eAAe,GAAGe,QAAQf,eAAe;QAC9C,IAAI,CAAClD,MAAM,GAAGiE,QAAQjE,MAAM;QAC5B,IAAI,CAACS,OAAO,GAAG,IAAI,CAAC/B,WAAW,CAACuF,QAAQxD,OAAO;IACjD;AAqWF;AAhYE,gBAAgB;AAChB,iBARWhC,gBAQIY,gBAA6C,IAAII;AAQhE,iBAhBWhB,gBAgBawD,mBAAkB;IAAC;IAAO;IAAO;CAAS"}
|
|
@@ -21,12 +21,16 @@ import { AuthError, AuthErrorCode } from "../../types";
|
|
|
21
21
|
/**
|
|
22
22
|
* 初始化存储实例(带缓存优化)
|
|
23
23
|
*/ initStorage(storageConfig) {
|
|
24
|
+
const strategy = storageConfig.strategy || 'local';
|
|
24
25
|
// 如果直接提供了实例,直接使用(最快路径)
|
|
25
26
|
if (storageConfig.instance) {
|
|
26
|
-
|
|
27
|
+
const normalizedInstance = this.normalizeStorageInstance(storageConfig.instance, strategy);
|
|
28
|
+
if (normalizedInstance) {
|
|
29
|
+
return normalizedInstance;
|
|
30
|
+
}
|
|
31
|
+
throw new AuthError('Invalid storage.instance. Expected StorageInstance, Web Storage API, or a storage container with local/session/indexedDB.', AuthErrorCode.CONFIG_ERROR);
|
|
27
32
|
}
|
|
28
33
|
// 如果没有提供实例,尝试从 @vlian/framework 获取
|
|
29
|
-
const strategy = storageConfig.strategy || 'local';
|
|
30
34
|
const cacheKey = `${strategy}_${this.environment}`;
|
|
31
35
|
// 检查缓存
|
|
32
36
|
if (StorageManager.storageCache.has(cacheKey)) {
|
|
@@ -103,7 +107,11 @@ import { AuthError, AuthErrorCode } from "../../types";
|
|
|
103
107
|
session: storage.session,
|
|
104
108
|
indexedDB: storage.indexedDB
|
|
105
109
|
};
|
|
106
|
-
const
|
|
110
|
+
const storageCandidate = strategyMap[strategy] || storage.local || storage;
|
|
111
|
+
const storageInstance = this.normalizeStorageInstance(storageCandidate, strategy);
|
|
112
|
+
if (!storageInstance) {
|
|
113
|
+
throw new AuthError('Resolved storage does not implement get/set/remove methods', AuthErrorCode.ENVIRONMENT_ERROR);
|
|
114
|
+
}
|
|
107
115
|
// 缓存存储实例
|
|
108
116
|
StorageManager.storageCache.set(cacheKey, storageInstance);
|
|
109
117
|
return storageInstance;
|
|
@@ -111,6 +119,60 @@ import { AuthError, AuthErrorCode } from "../../types";
|
|
|
111
119
|
throw new AuthError(`Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` + 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.', AuthErrorCode.ENVIRONMENT_ERROR, undefined, error instanceof Error ? error : new Error(String(error)));
|
|
112
120
|
}
|
|
113
121
|
}
|
|
122
|
+
normalizeStorageInstance(candidate, strategy) {
|
|
123
|
+
if (!candidate) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
if (this.isStorageInstance(candidate)) {
|
|
127
|
+
return candidate;
|
|
128
|
+
}
|
|
129
|
+
if (this.isBrowserStorage(candidate)) {
|
|
130
|
+
return {
|
|
131
|
+
async get (key) {
|
|
132
|
+
return candidate.getItem(key);
|
|
133
|
+
},
|
|
134
|
+
async set (key, value) {
|
|
135
|
+
candidate.setItem(key, value);
|
|
136
|
+
},
|
|
137
|
+
async remove (key) {
|
|
138
|
+
candidate.removeItem(key);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (typeof candidate === 'object') {
|
|
143
|
+
const storageContainer = candidate;
|
|
144
|
+
const selected = storageContainer[strategy] || storageContainer.local;
|
|
145
|
+
if (selected) {
|
|
146
|
+
return this.normalizeStorageInstance(selected, strategy);
|
|
147
|
+
}
|
|
148
|
+
if (typeof storageContainer.initialize === 'function') {
|
|
149
|
+
try {
|
|
150
|
+
storageContainer.initialize();
|
|
151
|
+
const initializedSelected = storageContainer[strategy] || storageContainer.local;
|
|
152
|
+
if (initializedSelected) {
|
|
153
|
+
return this.normalizeStorageInstance(initializedSelected, strategy);
|
|
154
|
+
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
this.logger?.warn('Failed to initialize storage container:', error);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
isStorageInstance(candidate) {
|
|
163
|
+
if (!candidate || typeof candidate !== 'object') {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
const target = candidate;
|
|
167
|
+
return StorageManager.STORAGE_METHODS.every((method)=>typeof target[method] === 'function');
|
|
168
|
+
}
|
|
169
|
+
isBrowserStorage(candidate) {
|
|
170
|
+
if (!candidate || typeof candidate !== 'object') {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const target = candidate;
|
|
174
|
+
return typeof target.getItem === 'function' && typeof target.setItem === 'function' && typeof target.removeItem === 'function';
|
|
175
|
+
}
|
|
114
176
|
/**
|
|
115
177
|
* 从存储中恢复认证状态
|
|
116
178
|
*/ async restoreAuthState() {
|
|
@@ -249,5 +311,10 @@ import { AuthError, AuthErrorCode } from "../../types";
|
|
|
249
311
|
}
|
|
250
312
|
// 存储初始化缓存(优化性能)
|
|
251
313
|
_define_property(StorageManager, "storageCache", new Map());
|
|
314
|
+
_define_property(StorageManager, "STORAGE_METHODS", [
|
|
315
|
+
'get',
|
|
316
|
+
'set',
|
|
317
|
+
'remove'
|
|
318
|
+
]);
|
|
252
319
|
|
|
253
320
|
//# sourceMappingURL=StorageManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../src/managers/AuthManager/modules/core/StorageManager.ts"],"sourcesContent":["/**\n * 存储管理模块\n * 负责存储的初始化、数据的保存和恢复\n */\n\nimport type { StorageInstance, StorageStrategy, Environment, AuthData } from '../../types';\nimport { AuthError, AuthErrorCode } from '../../types';\nimport { EncryptionService } from './EncryptionService';\n\n/**\n * 存储管理器\n */\nexport class StorageManager {\n private storage: StorageInstance;\n private encryptionService: EncryptionService;\n private authDataKey: string;\n private environment: Environment;\n private logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n \n // 存储初始化缓存(优化性能)\n private static storageCache: Map<string, StorageInstance> = new Map();\n\n // 批量写入相关\n private batchWriteTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingWrite: (() => Promise<void>) | null = null;\n private isWriting: boolean = false;\n private batchWriteDelay: number;\n\n constructor(\n options: {\n storage: { instance?: StorageInstance; strategy?: StorageStrategy };\n encryptionService: EncryptionService;\n authDataKey: string;\n environment: Environment;\n batchWriteDelay: number;\n logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n }\n ) {\n this.encryptionService = options.encryptionService;\n this.authDataKey = options.authDataKey;\n this.environment = options.environment;\n this.batchWriteDelay = options.batchWriteDelay;\n this.logger = options.logger;\n this.storage = this.initStorage(options.storage);\n }\n\n /**\n * 初始化存储实例(带缓存优化)\n */\n private initStorage(storageConfig: { instance?: StorageInstance; strategy?: StorageStrategy }): StorageInstance {\n // 如果直接提供了实例,直接使用(最快路径)\n if (storageConfig.instance) {\n return storageConfig.instance;\n }\n\n // 如果没有提供实例,尝试从 @vlian/framework 获取\n const strategy = storageConfig.strategy || 'local';\n const cacheKey = `${strategy}_${this.environment}`;\n \n // 检查缓存\n if (StorageManager.storageCache.has(cacheKey)) {\n return StorageManager.storageCache.get(cacheKey)!;\n }\n \n try {\n // SSR 环境特殊处理\n if (this.environment === 'ssr') {\n // SSR 环境应该使用内存存储或服务端存储\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n\n // Worker 环境特殊处理\n if (this.environment === 'worker') {\n // Worker 环境不支持 localStorage,使用 IndexedDB 或内存存储\n if (strategy === 'local' || strategy === 'session') {\n this.logger?.warn(\n `${strategy} storage is not available in Worker environment, falling back to memory storage`\n );\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n }\n\n // 优化的获取逻辑:按优先级检查\n let storageModule: any;\n \n // 1. 检查全局变量(最快,无需动态导入)\n if (typeof globalThis !== 'undefined') {\n const global = globalThis as any;\n storageModule = global.window?.vlianFramework || global.vlianFramework;\n }\n \n // 2. 如果全局变量不存在,尝试使用 require(仅 Node.js/CommonJS 环境)\n if (!storageModule && typeof require !== 'undefined') {\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n storageModule = require('@vlian/framework');\n } catch {\n // require 失败,继续抛出错误\n }\n }\n \n // 如果仍然没有找到,抛出错误\n if (!storageModule) {\n throw new AuthError(\n '@vlian/framework is not available. ' +\n 'Please provide storage.instance or ensure @vlian/framework is installed and accessible.',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 获取 storage 对象(优化:减少属性访问)\n const storage = storageModule.storage || storageModule.default?.storage;\n if (!storage) {\n throw new AuthError(\n 'Storage not found in @vlian/framework',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 根据策略返回对应的存储实例(优化:使用映射表)\n const strategyMap: Record<string, StorageInstance | undefined> = {\n local: storage.local,\n session: storage.session,\n indexedDB: storage.indexedDB,\n };\n \n const storageInstance = strategyMap[strategy] || storage.local || storage;\n \n // 缓存存储实例\n StorageManager.storageCache.set(cacheKey, storageInstance);\n \n return storageInstance;\n } catch (error) {\n throw new AuthError(\n `Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` +\n 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.',\n AuthErrorCode.ENVIRONMENT_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 从存储中恢复认证状态\n */\n async restoreAuthState(): Promise<AuthData | null> {\n try {\n const encryptedData = await this.storage.get(this.authDataKey);\n if (encryptedData) {\n // 解密数据(如果配置了加密)\n const data = await this.encryptionService.decryptData(encryptedData);\n return JSON.parse(data) as AuthData;\n }\n return null;\n } catch (error) {\n this.logger?.error('Failed to restore auth state:', error);\n return null;\n }\n }\n\n /**\n * 保存认证状态到存储(内部实现,支持批量写入)\n */\n private async saveAuthStateImmediate(authData: AuthData | null): Promise<void> {\n try {\n if (authData) {\n const data = JSON.stringify(authData);\n // 加密数据(如果配置了加密)\n const encryptedData = await this.encryptionService.encryptData(data);\n await this.storage.set(this.authDataKey, encryptedData);\n } else {\n await this.storage.remove(this.authDataKey);\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.logger?.error('Failed to save auth state:', errorMessage);\n throw new AuthError(\n errorMessage,\n AuthErrorCode.STORAGE_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 保存认证状态到存储(支持批量写入优化)\n */\n async saveAuthState(authData: AuthData | null): Promise<void> {\n // 如果禁用了批量写入或延迟为0,立即写入\n if (this.batchWriteDelay === 0) {\n return this.saveAuthStateImmediate(authData);\n }\n\n // 清除之前的定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 创建待写入任务\n this.pendingWrite = () => {\n this.isWriting = true;\n const currentAuthData = authData;\n this.pendingWrite = null;\n return this.saveAuthStateImmediate(currentAuthData)\n .finally(() => {\n this.isWriting = false;\n });\n };\n\n // 设置延迟写入\n this.batchWriteTimer = setTimeout(() => {\n this.batchWriteTimer = null;\n if (this.pendingWrite) {\n this.pendingWrite().catch((error) => {\n this.logger?.error('Batch write failed:', error);\n });\n }\n }, this.batchWriteDelay);\n\n // 如果当前没有正在进行的写入,立即执行(首次写入)\n if (!this.isWriting) {\n // 使用微任务确保在当前同步操作完成后执行\n Promise.resolve().then(() => {\n if (this.pendingWrite && !this.isWriting) {\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n this.pendingWrite?.().catch((error) => {\n this.logger?.error('Immediate write failed:', error);\n });\n }\n });\n }\n }\n\n /**\n * 强制立即保存认证状态(用于关键操作,如登出)\n */\n async saveAuthStateForce(authData: AuthData | null): Promise<void> {\n // 清除批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 等待当前写入完成\n if (this.isWriting && this.pendingWrite) {\n await this.pendingWrite();\n }\n\n // 执行立即写入\n return this.saveAuthStateImmediate(authData);\n }\n\n /**\n * 获取存储实例(用于健康检查等)\n */\n getStorage(): StorageInstance {\n return this.storage;\n }\n\n /**\n * 清理资源\n */\n destroy(): void {\n // 清理批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n \n // 等待待处理的写入完成(如果正在写入)\n if (this.isWriting && this.pendingWrite) {\n // 注意:这里不等待 Promise 完成,因为 destroy 是同步方法\n // 但我们会标记为不再处理新的写入\n this.pendingWrite = null;\n }\n \n this.isWriting = false;\n this.pendingWrite = null;\n }\n}\n"],"names":["AuthError","AuthErrorCode","StorageManager","initStorage","storageConfig","instance","strategy","cacheKey","environment","storageCache","has","get","data","Map","memoryStorage","key","set","value","remove","delete","logger","warn","storageModule","globalThis","global","window","vlianFramework","require","ENVIRONMENT_ERROR","storage","default","strategyMap","local","session","indexedDB","storageInstance","error","Error","message","String","undefined","restoreAuthState","encryptedData","authDataKey","encryptionService","decryptData","JSON","parse","saveAuthStateImmediate","authData","stringify","encryptData","errorMessage","STORAGE_ERROR","saveAuthState","batchWriteDelay","batchWriteTimer","clearTimeout","pendingWrite","isWriting","currentAuthData","finally","setTimeout","catch","Promise","resolve","then","saveAuthStateForce","getStorage","destroy","options"],"mappings":"AAAA;;;CAGC;;;;;;;;;;;;;AAGD,SAASA,SAAS,EAAEC,aAAa,QAAQ,cAAc;AAGvD;;CAEC,GACD,OAAO,MAAMC;IAkCX;;GAEC,GACD,AAAQC,YAAYC,aAAyE,EAAmB;QAC9G,uBAAuB;QACvB,IAAIA,cAAcC,QAAQ,EAAE;YAC1B,OAAOD,cAAcC,QAAQ;QAC/B;QAEA,mCAAmC;QACnC,MAAMC,WAAWF,cAAcE,QAAQ,IAAI;QAC3C,MAAMC,WAAW,GAAGD,SAAS,CAAC,EAAE,IAAI,CAACE,WAAW,EAAE;QAElD,OAAO;QACP,IAAIN,eAAeO,YAAY,CAACC,GAAG,CAACH,WAAW;YAC7C,OAAOL,eAAeO,YAAY,CAACE,GAAG,CAACJ;QACzC;QAEA,IAAI;YACF,aAAa;YACb,IAAI,IAAI,CAACC,WAAW,KAAK,OAAO;gBAC9B,uBAAuB;gBACvB,MAAMI,OAAO,IAAIC;gBACjB,MAAMC,gBAAiC;oBACrC,MAAMH,KAAII,GAAW;wBACnB,OAAOH,KAAKD,GAAG,CAACI,QAAQ;oBAC1B;oBACA,MAAMC,KAAID,GAAW,EAAEE,KAAa;wBAClCL,KAAKI,GAAG,CAACD,KAAKE;oBAChB;oBACA,MAAMC,QAAOH,GAAW;wBACtB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ACX,iBAAiB,GAAG0B,QAAQ1B,iBAAiB;QAClD,IAAI,CAACD,WAAW,GAAG2B,QAAQ3B,WAAW;QACtC,IAAI,CAACnC,WAAW,GAAG8D,QAAQ9D,WAAW;QACtC,IAAI,CAAC+C,eAAe,GAAGe,QAAQf,eAAe;QAC9C,IAAI,CAACnC,MAAM,GAAGkD,QAAQlD,MAAM;QAC5B,IAAI,CAACS,OAAO,GAAG,IAAI,CAAC1B,WAAW,CAACmE,QAAQzC,OAAO;IACjD;AA2QF;AApSE,gBAAgB;AAChB,iBARW3B,gBAQIO,gBAA6C,IAAII"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../src/managers/AuthManager/modules/core/StorageManager.ts"],"sourcesContent":["/**\n * 存储管理模块\n * 负责存储的初始化、数据的保存和恢复\n */\n\nimport type { StorageInstance, StorageStrategy, Environment, AuthData } from '../../types';\nimport { AuthError, AuthErrorCode } from '../../types';\nimport { EncryptionService } from './EncryptionService';\n\n/**\n * 存储管理器\n */\nexport class StorageManager {\n private storage: StorageInstance;\n private encryptionService: EncryptionService;\n private authDataKey: string;\n private environment: Environment;\n private logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n \n // 存储初始化缓存(优化性能)\n private static storageCache: Map<string, StorageInstance> = new Map();\n\n // 批量写入相关\n private batchWriteTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingWrite: (() => Promise<void>) | null = null;\n private isWriting: boolean = false;\n private batchWriteDelay: number;\n\n private static readonly STORAGE_METHODS = ['get', 'set', 'remove'] as const;\n\n constructor(\n options: {\n storage: { instance?: StorageInstance; strategy?: StorageStrategy };\n encryptionService: EncryptionService;\n authDataKey: string;\n environment: Environment;\n batchWriteDelay: number;\n logger?: { warn: (message: string, ...args: unknown[]) => void; error: (message: string, ...args: unknown[]) => void };\n }\n ) {\n this.encryptionService = options.encryptionService;\n this.authDataKey = options.authDataKey;\n this.environment = options.environment;\n this.batchWriteDelay = options.batchWriteDelay;\n this.logger = options.logger;\n this.storage = this.initStorage(options.storage);\n }\n\n /**\n * 初始化存储实例(带缓存优化)\n */\n private initStorage(storageConfig: { instance?: StorageInstance; strategy?: StorageStrategy }): StorageInstance {\n const strategy = storageConfig.strategy || 'local';\n\n // 如果直接提供了实例,直接使用(最快路径)\n if (storageConfig.instance) {\n const normalizedInstance = this.normalizeStorageInstance(storageConfig.instance, strategy);\n if (normalizedInstance) {\n return normalizedInstance;\n }\n throw new AuthError(\n 'Invalid storage.instance. Expected StorageInstance, Web Storage API, or a storage container with local/session/indexedDB.',\n AuthErrorCode.CONFIG_ERROR\n );\n }\n\n // 如果没有提供实例,尝试从 @vlian/framework 获取\n const cacheKey = `${strategy}_${this.environment}`;\n \n // 检查缓存\n if (StorageManager.storageCache.has(cacheKey)) {\n return StorageManager.storageCache.get(cacheKey)!;\n }\n \n try {\n // SSR 环境特殊处理\n if (this.environment === 'ssr') {\n // SSR 环境应该使用内存存储或服务端存储\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n\n // Worker 环境特殊处理\n if (this.environment === 'worker') {\n // Worker 环境不支持 localStorage,使用 IndexedDB 或内存存储\n if (strategy === 'local' || strategy === 'session') {\n this.logger?.warn(\n `${strategy} storage is not available in Worker environment, falling back to memory storage`\n );\n const data = new Map<string, string>();\n const memoryStorage: StorageInstance = {\n async get(key: string): Promise<string | null> {\n return data.get(key) || null;\n },\n async set(key: string, value: string): Promise<void> {\n data.set(key, value);\n },\n async remove(key: string): Promise<void> {\n data.delete(key);\n },\n };\n StorageManager.storageCache.set(cacheKey, memoryStorage);\n return memoryStorage;\n }\n }\n\n // 优化的获取逻辑:按优先级检查\n let storageModule: any;\n \n // 1. 检查全局变量(最快,无需动态导入)\n if (typeof globalThis !== 'undefined') {\n const global = globalThis as any;\n storageModule = global.window?.vlianFramework || global.vlianFramework;\n }\n \n // 2. 如果全局变量不存在,尝试使用 require(仅 Node.js/CommonJS 环境)\n if (!storageModule && typeof require !== 'undefined') {\n try {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n storageModule = require('@vlian/framework');\n } catch {\n // require 失败,继续抛出错误\n }\n }\n \n // 如果仍然没有找到,抛出错误\n if (!storageModule) {\n throw new AuthError(\n '@vlian/framework is not available. ' +\n 'Please provide storage.instance or ensure @vlian/framework is installed and accessible.',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 获取 storage 对象(优化:减少属性访问)\n const storage = storageModule.storage || storageModule.default?.storage;\n if (!storage) {\n throw new AuthError(\n 'Storage not found in @vlian/framework',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 根据策略返回对应的存储实例(优化:使用映射表)\n const strategyMap: Record<string, unknown> = {\n local: storage.local,\n session: storage.session,\n indexedDB: storage.indexedDB,\n };\n \n const storageCandidate = strategyMap[strategy] || storage.local || storage;\n const storageInstance = this.normalizeStorageInstance(storageCandidate, strategy);\n if (!storageInstance) {\n throw new AuthError(\n 'Resolved storage does not implement get/set/remove methods',\n AuthErrorCode.ENVIRONMENT_ERROR\n );\n }\n \n // 缓存存储实例\n StorageManager.storageCache.set(cacheKey, storageInstance);\n \n return storageInstance;\n } catch (error) {\n throw new AuthError(\n `Failed to initialize storage: ${error instanceof Error ? error.message : String(error)}. ` +\n 'Please provide storage.instance or ensure @vlian/framework is installed and provides storage API.',\n AuthErrorCode.ENVIRONMENT_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n private normalizeStorageInstance(candidate: unknown, strategy: StorageStrategy): StorageInstance | null {\n if (!candidate) {\n return null;\n }\n\n if (this.isStorageInstance(candidate)) {\n return candidate;\n }\n\n if (this.isBrowserStorage(candidate)) {\n return {\n async get(key: string): Promise<string | null> {\n return candidate.getItem(key);\n },\n async set(key: string, value: string): Promise<void> {\n candidate.setItem(key, value);\n },\n async remove(key: string): Promise<void> {\n candidate.removeItem(key);\n },\n };\n }\n\n if (typeof candidate === 'object') {\n const storageContainer = candidate as {\n local?: unknown;\n session?: unknown;\n indexedDB?: unknown;\n initialize?: (options?: unknown) => void;\n };\n const selected = storageContainer[strategy] || storageContainer.local;\n if (selected) {\n return this.normalizeStorageInstance(selected, strategy);\n }\n\n if (typeof storageContainer.initialize === 'function') {\n try {\n storageContainer.initialize();\n const initializedSelected = storageContainer[strategy] || storageContainer.local;\n if (initializedSelected) {\n return this.normalizeStorageInstance(initializedSelected, strategy);\n }\n } catch (error) {\n this.logger?.warn('Failed to initialize storage container:', error);\n }\n }\n }\n\n return null;\n }\n\n private isStorageInstance(candidate: unknown): candidate is StorageInstance {\n if (!candidate || typeof candidate !== 'object') {\n return false;\n }\n const target = candidate as Record<string, unknown>;\n return StorageManager.STORAGE_METHODS.every((method) => typeof target[method] === 'function');\n }\n\n private isBrowserStorage(candidate: unknown): candidate is {\n getItem: (key: string) => string | null;\n setItem: (key: string, value: string) => void;\n removeItem: (key: string) => void;\n } {\n if (!candidate || typeof candidate !== 'object') {\n return false;\n }\n const target = candidate as Record<string, unknown>;\n return (\n typeof target.getItem === 'function' &&\n typeof target.setItem === 'function' &&\n typeof target.removeItem === 'function'\n );\n }\n\n /**\n * 从存储中恢复认证状态\n */\n async restoreAuthState(): Promise<AuthData | null> {\n try {\n const encryptedData = await this.storage.get(this.authDataKey);\n if (encryptedData) {\n // 解密数据(如果配置了加密)\n const data = await this.encryptionService.decryptData(encryptedData);\n return JSON.parse(data) as AuthData;\n }\n return null;\n } catch (error) {\n this.logger?.error('Failed to restore auth state:', error);\n return null;\n }\n }\n\n /**\n * 保存认证状态到存储(内部实现,支持批量写入)\n */\n private async saveAuthStateImmediate(authData: AuthData | null): Promise<void> {\n try {\n if (authData) {\n const data = JSON.stringify(authData);\n // 加密数据(如果配置了加密)\n const encryptedData = await this.encryptionService.encryptData(data);\n await this.storage.set(this.authDataKey, encryptedData);\n } else {\n await this.storage.remove(this.authDataKey);\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n this.logger?.error('Failed to save auth state:', errorMessage);\n throw new AuthError(\n errorMessage,\n AuthErrorCode.STORAGE_ERROR,\n undefined,\n error instanceof Error ? error : new Error(String(error))\n );\n }\n }\n\n /**\n * 保存认证状态到存储(支持批量写入优化)\n */\n async saveAuthState(authData: AuthData | null): Promise<void> {\n // 如果禁用了批量写入或延迟为0,立即写入\n if (this.batchWriteDelay === 0) {\n return this.saveAuthStateImmediate(authData);\n }\n\n // 清除之前的定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 创建待写入任务\n this.pendingWrite = () => {\n this.isWriting = true;\n const currentAuthData = authData;\n this.pendingWrite = null;\n return this.saveAuthStateImmediate(currentAuthData)\n .finally(() => {\n this.isWriting = false;\n });\n };\n\n // 设置延迟写入\n this.batchWriteTimer = setTimeout(() => {\n this.batchWriteTimer = null;\n if (this.pendingWrite) {\n this.pendingWrite().catch((error) => {\n this.logger?.error('Batch write failed:', error);\n });\n }\n }, this.batchWriteDelay);\n\n // 如果当前没有正在进行的写入,立即执行(首次写入)\n if (!this.isWriting) {\n // 使用微任务确保在当前同步操作完成后执行\n Promise.resolve().then(() => {\n if (this.pendingWrite && !this.isWriting) {\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n this.pendingWrite?.().catch((error) => {\n this.logger?.error('Immediate write failed:', error);\n });\n }\n });\n }\n }\n\n /**\n * 强制立即保存认证状态(用于关键操作,如登出)\n */\n async saveAuthStateForce(authData: AuthData | null): Promise<void> {\n // 清除批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n\n // 等待当前写入完成\n if (this.isWriting && this.pendingWrite) {\n await this.pendingWrite();\n }\n\n // 执行立即写入\n return this.saveAuthStateImmediate(authData);\n }\n\n /**\n * 获取存储实例(用于健康检查等)\n */\n getStorage(): StorageInstance {\n return this.storage;\n }\n\n /**\n * 清理资源\n */\n destroy(): void {\n // 清理批量写入定时器\n if (this.batchWriteTimer) {\n clearTimeout(this.batchWriteTimer);\n this.batchWriteTimer = null;\n }\n \n // 等待待处理的写入完成(如果正在写入)\n if (this.isWriting && this.pendingWrite) {\n // 注意:这里不等待 Promise 完成,因为 destroy 是同步方法\n // 但我们会标记为不再处理新的写入\n this.pendingWrite = null;\n }\n \n this.isWriting = false;\n this.pendingWrite = null;\n }\n}\n"],"names":["AuthError","AuthErrorCode","StorageManager","initStorage","storageConfig","strategy","instance","normalizedInstance","normalizeStorageInstance","CONFIG_ERROR","cacheKey","environment","storageCache","has","get","data","Map","memoryStorage","key","set","value","remove","delete","logger","warn","storageModule","globalThis","global","window","vlianFramework","require","ENVIRONMENT_ERROR","storage","default","strategyMap","local","session","indexedDB","storageCandidate","storageInstance","error","Error","message","String","undefined","candidate","isStorageInstance","isBrowserStorage","getItem","setItem","removeItem","storageContainer","selected","initialize","initializedSelected","target","STORAGE_METHODS","every","method","restoreAuthState","encryptedData","authDataKey","encryptionService","decryptData","JSON","parse","saveAuthStateImmediate","authData","stringify","encryptData","errorMessage","STORAGE_ERROR","saveAuthState","batchWriteDelay","batchWriteTimer","clearTimeout","pendingWrite","isWriting","currentAuthData","finally","setTimeout","catch","Promise","resolve","then","saveAuthStateForce","getStorage","destroy","options"],"mappings":"AAAA;;;CAGC;;;;;;;;;;;;;AAGD,SAASA,SAAS,EAAEC,aAAa,QAAQ,cAAc;AAGvD;;CAEC,GACD,OAAO,MAAMC;IAoCX;;GAEC,GACD,AAAQC,YAAYC,aAAyE,EAAmB;QAC9G,MAAMC,WAAWD,cAAcC,QAAQ,IAAI;QAE3C,uBAAuB;QACvB,IAAID,cAAcE,QAAQ,EAAE;YAC1B,MAAMC,qBAAqB,IAAI,CAACC,wB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AC5B,IAAI,CAACS,OAAO,GAAG,IAAI,CAAC7B,WAAW,CAACqF,QAAQxD,OAAO;IACjD;AAqWF;AAhYE,gBAAgB;AAChB,iBARW9B,gBAQIU,gBAA6C,IAAII;AAQhE,iBAhBWd,gBAgBasD,mBAAkB;IAAC;IAAO;IAAO;CAAS"}
|
|
@@ -18,6 +18,7 @@ export declare class StorageManager {
|
|
|
18
18
|
private pendingWrite;
|
|
19
19
|
private isWriting;
|
|
20
20
|
private batchWriteDelay;
|
|
21
|
+
private static readonly STORAGE_METHODS;
|
|
21
22
|
constructor(options: {
|
|
22
23
|
storage: {
|
|
23
24
|
instance?: StorageInstance;
|
|
@@ -36,6 +37,9 @@ export declare class StorageManager {
|
|
|
36
37
|
* 初始化存储实例(带缓存优化)
|
|
37
38
|
*/
|
|
38
39
|
private initStorage;
|
|
40
|
+
private normalizeStorageInstance;
|
|
41
|
+
private isStorageInstance;
|
|
42
|
+
private isBrowserStorage;
|
|
39
43
|
/**
|
|
40
44
|
* 从存储中恢复认证状态
|
|
41
45
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StorageManager.d.ts","sourceRoot":"","sources":["../../../../../../src/managers/AuthManager/modules/core/StorageManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,eAAe,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE3F,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAkB;IACjC,OAAO,CAAC,iBAAiB,CAAoB;IAC7C,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,WAAW,CAAc;IACjC,OAAO,CAAC,MAAM,CAAC,CAAgH;IAG/H,OAAO,CAAC,MAAM,CAAC,YAAY,CAA2C;IAGtE,OAAO,CAAC,eAAe,CAA8C;IACrE,OAAO,CAAC,Y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AAE,CAAC;KACxH;IAUH;;OAEG;IACH,OAAO,CAAC,WAAW;IAuInB,OAAO,CAAC,wBAAwB;IAmDhC,OAAO,CAAC,iBAAiB;IAQzB,OAAO,CAAC,gBAAgB;IAgBxB;;OAEG;IACG,gBAAgB,IAAI,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAelD;;OAEG;YACW,sBAAsB;IAsBpC;;OAEG;IACG,aAAa,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAkD7D;;OAEG;IACG,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBlE;;OAEG;IACH,UAAU,IAAI,eAAe;IAI7B;;OAEG;IACH,OAAO,IAAI,IAAI;CAiBhB"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vlian/infrastructure",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.12",
|
|
4
4
|
"description": "通用业务基础设施包,提供业务无关的通用能力",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"private": false,
|
|
6
7
|
"main": "./dist/cjs/index.js",
|
|
7
8
|
"module": "./dist/esm/index.js",
|
|
8
9
|
"types": "./dist/types/index.d.ts",
|
|
@@ -35,8 +36,7 @@
|
|
|
35
36
|
"test:watch": "vitest",
|
|
36
37
|
"test:coverage": "vitest run --coverage",
|
|
37
38
|
"prepublishOnly": "pnpm run build",
|
|
38
|
-
"prepack": "pnpm run build"
|
|
39
|
-
"publish": "npm publish --access public"
|
|
39
|
+
"prepack": "pnpm run build"
|
|
40
40
|
},
|
|
41
41
|
"keywords": [
|
|
42
42
|
"infrastructure",
|
|
@@ -64,13 +64,13 @@
|
|
|
64
64
|
"@swc/core": "^1.3.92",
|
|
65
65
|
"@types/node": "^20.10.0",
|
|
66
66
|
"typescript": "^5.3.3",
|
|
67
|
-
"@vlian/framework": "
|
|
67
|
+
"@vlian/framework": "file:/Users/xiaowenli/lowcode/Secra/code/secra-framework",
|
|
68
68
|
"chokidar": "^3.6.0",
|
|
69
69
|
"vitest": "^2.1.8",
|
|
70
70
|
"@vitest/coverage-v8": "^2.1.8"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
|
-
"@vlian/framework": "^1.
|
|
73
|
+
"@vlian/framework": "^1.2.19"
|
|
74
74
|
},
|
|
75
75
|
"peerDependenciesMeta": {
|
|
76
76
|
"@vlian/framework": {
|
|
@@ -78,4 +78,4 @@
|
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
"packageManager": "pnpm@10.27.0+sha512.72d699da16b1179c14ba9e64dc71c9a40988cbdc65c264cb0e489db7de917f20dcf4d64d8723625f2969ba52d4b7e2a1170682d9ac2a5dcaeaab732b7e16f04a"
|
|
81
|
-
}
|
|
81
|
+
}
|