id-scanner-lib 1.5.0 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +378 -59
  2. package/dist/id-scanner-lib.esm.js +195 -10
  3. package/dist/id-scanner-lib.esm.js.map +1 -1
  4. package/dist/id-scanner-lib.js +4812 -14709
  5. package/dist/id-scanner-lib.js.map +1 -1
  6. package/dist/types/browser-image-compression.d.ts +19 -0
  7. package/dist/types/tesseract.d.ts +280 -0
  8. package/package.json +21 -11
  9. package/src/core/camera-manager.ts +16 -1
  10. package/src/core/config.ts +37 -0
  11. package/src/core/errors.ts +3 -3
  12. package/src/core/event-emitter.test.ts +42 -0
  13. package/src/core/loading-state.test.ts +67 -0
  14. package/src/core/loading-state.ts +156 -0
  15. package/src/core/logger.test.ts +49 -0
  16. package/src/core/module-manager.ts +2 -4
  17. package/src/core/scanner-factory.ts +8 -9
  18. package/src/index.ts +3 -2
  19. package/src/modules/face/face-detector.ts +123 -66
  20. package/src/modules/id-card/anti-fake-detector.ts +2 -2
  21. package/src/modules/id-card/ocr-worker.ts +1 -1
  22. package/src/modules/qrcode/qr-code-scanner.ts +2 -1
  23. package/src/modules/qrcode/types.ts +111 -7
  24. package/src/types/common.test.ts +99 -0
  25. package/src/types/common.ts +166 -0
  26. package/src/utils/camera.test.ts +30 -0
  27. package/src/utils/camera.ts +4 -1
  28. package/src/utils/error-handler.test.ts +137 -0
  29. package/src/utils/error-handler.ts +110 -0
  30. package/src/utils/index.test.ts +186 -0
  31. package/src/utils/index.ts +3 -0
  32. package/src/utils/retry.test.ts +142 -0
  33. package/src/utils/retry.ts +282 -0
  34. package/src/utils/utils.test.ts +171 -0
  35. package/src/version.ts +1 -1
  36. package/dist/types/core/base-module.d.ts +0 -44
  37. package/dist/types/core/camera-manager.d.ts +0 -258
  38. package/dist/types/core/config.d.ts +0 -88
  39. package/dist/types/core/errors.d.ts +0 -111
  40. package/dist/types/core/event-emitter.d.ts +0 -55
  41. package/dist/types/core/logger.d.ts +0 -277
  42. package/dist/types/core/module-manager.d.ts +0 -78
  43. package/dist/types/core/plugin-manager.d.ts +0 -158
  44. package/dist/types/core/resource-manager.d.ts +0 -246
  45. package/dist/types/core/result.d.ts +0 -83
  46. package/dist/types/core/scanner-factory.d.ts +0 -93
  47. package/dist/types/index.bundle.d.ts +0 -1303
  48. package/dist/types/index.d.ts +0 -86
  49. package/dist/types/interfaces/external-types.d.ts +0 -174
  50. package/dist/types/interfaces/face-detection.d.ts +0 -293
  51. package/dist/types/interfaces/scanner-module.d.ts +0 -280
  52. package/dist/types/modules/face/face-detector.d.ts +0 -170
  53. package/dist/types/modules/face/index.d.ts +0 -56
  54. package/dist/types/modules/face/liveness-detector.d.ts +0 -177
  55. package/dist/types/modules/face/types.d.ts +0 -136
  56. package/dist/types/modules/id-card/anti-fake-detector.d.ts +0 -170
  57. package/dist/types/modules/id-card/id-card-detector.d.ts +0 -131
  58. package/dist/types/modules/id-card/index.d.ts +0 -89
  59. package/dist/types/modules/id-card/ocr-processor.d.ts +0 -110
  60. package/dist/types/modules/id-card/ocr-worker.d.ts +0 -31
  61. package/dist/types/modules/id-card/types.d.ts +0 -181
  62. package/dist/types/modules/qrcode/index.d.ts +0 -51
  63. package/dist/types/modules/qrcode/qr-code-scanner.d.ts +0 -64
  64. package/dist/types/modules/qrcode/types.d.ts +0 -67
  65. package/dist/types/utils/camera.d.ts +0 -81
  66. package/dist/types/utils/image-processing.d.ts +0 -176
  67. package/dist/types/utils/index.d.ts +0 -175
  68. package/dist/types/utils/performance.d.ts +0 -81
  69. package/dist/types/utils/resource-manager.d.ts +0 -53
  70. package/dist/types/utils/types.d.ts +0 -166
  71. package/dist/types/utils/worker.d.ts +0 -52
  72. package/dist/types/version.d.ts +0 -7
@@ -1,246 +0,0 @@
1
- /**
2
- * @file 资源管理器
3
- * @description 提供资源加载、缓存和释放功能
4
- * @module core/resource-manager
5
- */
6
- import { EventEmitter } from './event-emitter';
7
- import { Result } from './result';
8
- /**
9
- * 资源类型枚举
10
- */
11
- export declare enum ResourceType {
12
- MODEL = "model",// 模型文件
13
- WASM = "wasm",// WebAssembly文件
14
- IMAGE = "image",// 图片文件
15
- JSON = "json",// JSON文件
16
- TEXT = "text",// 文本文件
17
- ARRAYBUFFER = "buffer",// 二进制数据
18
- WORKER = "worker",// Web Worker脚本
19
- OTHER = "other"
20
- }
21
- /**
22
- * 资源接口
23
- */
24
- export interface Resource<T = any> {
25
- /** 资源ID */
26
- id: string;
27
- /** 资源类型 */
28
- type: ResourceType;
29
- /** 资源URL或数据 */
30
- url: string;
31
- /** 是否已加载 */
32
- loaded: boolean;
33
- /** 加载的数据 */
34
- data?: T;
35
- /** 上次使用时间戳 */
36
- lastUsed: number;
37
- /** 是否为永久资源(不自动释放) */
38
- permanent: boolean;
39
- /** 获取资源大小(如果可计算) */
40
- getSize(): number;
41
- }
42
- /**
43
- * 资源加载选项
44
- */
45
- export interface ResourceLoadOptions {
46
- /** 是否缓存 */
47
- cache?: boolean;
48
- /** 是否为永久资源(不自动释放) */
49
- permanent?: boolean;
50
- /** 资源类型(自动推断) */
51
- type?: ResourceType;
52
- /** 加载超时(ms) */
53
- timeout?: number;
54
- /** 是否替换现有资源 */
55
- forceReload?: boolean;
56
- /** 是否使用凭证 */
57
- credentials?: RequestCredentials;
58
- /** 自定义请求头 */
59
- headers?: Record<string, string>;
60
- }
61
- /**
62
- * 资源统计信息
63
- */
64
- export interface ResourceStats {
65
- /** 总资源数 */
66
- totalCount: number;
67
- /** 总内存使用(字节) */
68
- totalSize: number;
69
- /** 各类型资源数 */
70
- byType: Record<ResourceType, number>;
71
- /** 各类型资源大小(字节) */
72
- sizeByType: Record<ResourceType, number>;
73
- }
74
- /**
75
- * 资源管理器事件
76
- */
77
- export declare enum ResourceManagerEvent {
78
- /** 资源加载开始 */
79
- LOAD_START = "resource:load:start",
80
- /** 资源加载成功 */
81
- LOAD_SUCCESS = "resource:load:success",
82
- /** 资源加载失败 */
83
- LOAD_ERROR = "resource:load:error",
84
- /** 资源加载进度 */
85
- LOAD_PROGRESS = "resource:load:progress",
86
- /** 资源被释放 */
87
- RESOURCE_RELEASED = "resource:released",
88
- /** 资源统计更新 */
89
- STATS_UPDATED = "resource:stats:updated"
90
- }
91
- /**
92
- * 资源管理器
93
- * 提供统一的资源加载、缓存和管理功能
94
- */
95
- export declare class ResourceManager extends EventEmitter {
96
- /** 单例实例 */
97
- private static instance;
98
- /** 资源映射表 */
99
- private resources;
100
- /** 配置管理器 */
101
- private config;
102
- /** 日志记录器 */
103
- private logger;
104
- /** 缓存清理计时器ID */
105
- private cleanupTimerId;
106
- /** 默认基础路径 */
107
- private basePath;
108
- /** 加载中的资源请求 */
109
- private pendingRequests;
110
- private initialized;
111
- /**
112
- * 私有构造函数
113
- */
114
- private constructor();
115
- /**
116
- * 获取单例实例
117
- */
118
- static getInstance(): ResourceManager;
119
- /**
120
- * 设置基础路径
121
- * @param path 基础路径
122
- */
123
- setBasePath(path: string): void;
124
- /**
125
- * 获取资源完整URL
126
- */
127
- private getFullUrl;
128
- /**
129
- * 从URL推断资源类型
130
- */
131
- private inferResourceType;
132
- /**
133
- * 加载资源
134
- * @param id 资源ID
135
- * @param url 资源URL
136
- * @param options 加载选项
137
- */
138
- load<T = any>(id: string, url: string, options?: ResourceLoadOptions): Promise<Result<T>>;
139
- /**
140
- * 加载图片资源
141
- */
142
- private loadImage;
143
- /**
144
- * 加载JSON资源
145
- */
146
- private loadJson;
147
- /**
148
- * 加载文本资源
149
- */
150
- private loadText;
151
- /**
152
- * 加载二进制数据
153
- */
154
- private loadArrayBuffer;
155
- /**
156
- * 加载WebAssembly模块
157
- */
158
- private loadWasm;
159
- /**
160
- * 加载通用资源
161
- */
162
- private loadGeneric;
163
- /**
164
- * 预加载多个资源
165
- * @param resources 资源配置数组,每项包含id和url
166
- */
167
- preload(resources: Array<{
168
- id: string;
169
- url: string;
170
- options?: ResourceLoadOptions;
171
- }>): Promise<Result<Record<string, any>>>;
172
- /**
173
- * 获取资源
174
- * @param id 资源ID
175
- */
176
- get<T = any>(id: string): T | undefined;
177
- /**
178
- * 检查资源是否存在
179
- * @param id 资源ID
180
- */
181
- has(id: string): boolean;
182
- /**
183
- * 获取资源
184
- * 如果不存在,则使用工厂函数创建并缓存
185
- *
186
- * @param id 资源ID
187
- * @param factory 资源工厂函数
188
- * @param type 资源类型
189
- * @param permanent 是否永久保留
190
- */
191
- getOrCreate<T = any>(id: string, factory: () => T | Promise<T>, type?: ResourceType, permanent?: boolean): Promise<T>;
192
- /**
193
- * 释放资源
194
- * @param id 资源ID
195
- */
196
- release(id: string): boolean;
197
- /**
198
- * 释放资源组
199
- * @param pattern 资源ID匹配模式,可以是字符串前缀或正则表达式
200
- */
201
- releaseGroup(pattern: string | RegExp): number;
202
- /**
203
- * 释放所有非永久资源
204
- */
205
- releaseAll(): number;
206
- /**
207
- * 释放过期资源
208
- * @param maxAge 最大闲置时间(毫秒)
209
- */
210
- releaseExpired(maxAge: number): number;
211
- /**
212
- * 计算资源大小
213
- * @param data 资源数据
214
- * @param type 资源类型
215
- */
216
- private calculateResourceSize;
217
- /**
218
- * 清理特定资源
219
- * @param resource 资源对象
220
- */
221
- private cleanupResource;
222
- /**
223
- * 设置资源清理定时器
224
- */
225
- private setupCleanupTimer;
226
- /**
227
- * 获取资源统计信息
228
- */
229
- getStats(): ResourceStats;
230
- /**
231
- * 更新并发布资源统计信息
232
- */
233
- private updateStats;
234
- /**
235
- * 初始化资源管理器
236
- * @param options 初始化选项
237
- */
238
- initialize(options?: {
239
- basePath?: string;
240
- preloadResources?: Array<{
241
- id: string;
242
- url: string;
243
- type?: ResourceType;
244
- }>;
245
- }): Promise<void>;
246
- }
@@ -1,83 +0,0 @@
1
- /**
2
- * @file 结果包装类
3
- * @description 提供统一的操作结果封装
4
- * @module core/result
5
- */
6
- /**
7
- * 结果类型
8
- * 用于封装操作的成功或失败结果
9
- */
10
- export declare class Result<T = any> {
11
- /** 结果数据 */
12
- private _data?;
13
- /** 错误对象 */
14
- private _error?;
15
- /** 是否成功 */
16
- private _success;
17
- /** 额外元数据 */
18
- private _meta?;
19
- /**
20
- * 构造函数
21
- * @param success 是否成功
22
- * @param data 结果数据
23
- * @param error 错误对象
24
- * @param meta 元数据
25
- */
26
- constructor(success: boolean, data?: T, error?: Error, meta?: Record<string, any>);
27
- /**
28
- * 创建成功结果
29
- * @param data 结果数据
30
- * @param meta 元数据
31
- */
32
- static success<T>(data?: T, meta?: Record<string, any>): Result<T>;
33
- /**
34
- * 创建失败结果
35
- * @param error 错误对象
36
- * @param meta 元数据
37
- */
38
- static failure<T>(error: Error, meta?: Record<string, any>): Result<T>;
39
- /**
40
- * 检查结果是否成功
41
- */
42
- isSuccess(): boolean;
43
- /**
44
- * 检查结果是否失败
45
- */
46
- isFailure(): boolean;
47
- /**
48
- * 获取结果数据
49
- */
50
- get data(): T | undefined;
51
- /**
52
- * 获取错误对象
53
- */
54
- get error(): Error | undefined;
55
- /**
56
- * 获取元数据
57
- */
58
- get meta(): Record<string, any> | undefined;
59
- /**
60
- * 映射结果(如果成功)
61
- * @param fn 映射函数
62
- */
63
- map<U>(fn: (data: T) => U): Result<U>;
64
- /**
65
- * 如果成功,则执行函数
66
- * @param fn 要执行的函数
67
- */
68
- onSuccess(fn: (data?: T) => void): Result<T>;
69
- /**
70
- * 如果失败,则执行函数
71
- * @param fn 要执行的函数
72
- */
73
- onFailure(fn: (error: Error) => void): Result<T>;
74
- /**
75
- * 无论成功失败,都执行函数
76
- * @param fn 要执行的函数
77
- */
78
- onFinally(fn: () => void): Result<T>;
79
- /**
80
- * 转换为字符串
81
- */
82
- toString(): string;
83
- }
@@ -1,93 +0,0 @@
1
- /**
2
- * @file 扫描器工厂
3
- * @description 提供统一的组件创建和访问接口
4
- * @module core/scanner-factory
5
- */
6
- import { ConfigManager } from './config';
7
- import { Logger } from './logger';
8
- import { ResourceManager } from './resource-manager';
9
- import { EventEmitter } from './event-emitter';
10
- /**
11
- * 扫描器初始化选项
12
- */
13
- export interface ScannerFactoryOptions {
14
- /** 配置选项 */
15
- config?: Record<string, any>;
16
- /** 资源基础路径 */
17
- resourceBasePath?: string;
18
- /** 调试模式 */
19
- debug?: boolean;
20
- /** 自动初始化模块 */
21
- autoInitModules?: boolean;
22
- }
23
- /**
24
- * 扫描器工厂
25
- * 作为整个库的核心入口点,管理组件生命周期并提供统一接口
26
- */
27
- export declare class ScannerFactory extends EventEmitter {
28
- /** 单例实例 */
29
- private static instance;
30
- /** 配置管理器 */
31
- private readonly config;
32
- /** 日志记录器 */
33
- private readonly logger;
34
- /** 资源管理器 */
35
- private readonly resources;
36
- /** 是否已初始化 */
37
- private initialized;
38
- /** 初始化锁,防止多次调用 */
39
- private initializing;
40
- /**
41
- * 私有构造函数
42
- */
43
- private constructor();
44
- /**
45
- * 获取单例实例
46
- */
47
- static getInstance(): ScannerFactory;
48
- /**
49
- * 初始化扫描器工厂
50
- * @param options 初始化选项
51
- */
52
- initialize(options?: ScannerFactoryOptions): Promise<boolean>;
53
- /**
54
- * 初始化已启用的模块
55
- */
56
- private initEnabledModules;
57
- /**
58
- * 初始化人脸识别模块
59
- */
60
- private initFaceModule;
61
- /**
62
- * 初始化二维码扫描模块
63
- */
64
- private initQRModule;
65
- /**
66
- * 初始化身份证扫描模块
67
- */
68
- private initIDCardModule;
69
- /**
70
- * 初始化OCR模块
71
- */
72
- private initOCRModule;
73
- /**
74
- * 销毁实例,释放资源
75
- */
76
- destroy(): void;
77
- /**
78
- * 获取配置管理器
79
- */
80
- getConfig(): ConfigManager;
81
- /**
82
- * 获取日志记录器
83
- */
84
- getLogger(): Logger;
85
- /**
86
- * 获取资源管理器
87
- */
88
- getResources(): ResourceManager;
89
- /**
90
- * 检查是否已初始化
91
- */
92
- isInitialized(): boolean;
93
- }