@authme/engine 2.2.0 → 2.2.1-rc.1

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 CHANGED
@@ -2,6 +2,26 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ ## [2.2.1-rc.1](https://github.com/AuthMe01/web-client-sdk/compare/v2.2.1-rc.0...v2.2.1-rc.1) (2022-12-28)
6
+
7
+ ### Features
8
+
9
+ - upgrade engine to 5.1.0 ([26e2128](https://github.com/AuthMe01/web-client-sdk/commit/26e2128026edf36c4b54efdf61b339a2f755a528))
10
+
11
+ ### Bug Fixes
12
+
13
+ - inconsistency enum (or remove unused) ([b716f14](https://github.com/AuthMe01/web-client-sdk/commit/b716f142fa20d2eebf0af66ff27f858ef1e825b0))
14
+
15
+ ## [2.2.1-rc.0](https://github.com/AuthMe01/web-client-sdk/compare/v2.2.0...v2.2.1-rc.0) (2022-12-22)
16
+
17
+ ### Features
18
+
19
+ - implement preloadBackground feature ([4b58a16](https://github.com/AuthMe01/web-client-sdk/commit/4b58a16dbdd506d48ce9f9671d8154997f4b9839))
20
+
21
+ ### Bug Fixes
22
+
23
+ - engine destroy fail problem ([e2ffc84](https://github.com/AuthMe01/web-client-sdk/commit/e2ffc843cafb2c414535be6d735957e1a04b7146))
24
+
5
25
  ## [2.2.0](https://github.com/AuthMe01/web-client-sdk/compare/v2.2.0-rc.13...v2.2.0) (2022-12-19)
6
26
 
7
27
  ## [2.2.0-rc.13](https://github.com/AuthMe01/web-client-sdk/compare/v2.2.0-rc.12...v2.2.0-rc.13) (2022-12-19)
@@ -68,8 +68,14 @@
68
68
 
69
69
  // Base
70
70
  const baseFunctions = {
71
- instance: null,
72
- sessions: {},
71
+ // 由於不同 service 間的 _loadedSessions instance 是隔離開來的,
72
+ // 所以不能將 _loadedSessions: {} 直接放到這裡,
73
+ // 否則會造成 destroy 異常。
74
+ // 更好的做法會是用 class 替代現在的做法,只是改的幅度偏大,
75
+ // 所以暫時保留現有的做法。
76
+ _getLoadedModelKey({ modelVersion: { name, version } }) {
77
+ return `${name}#${version}`;
78
+ },
73
79
  _getModelVersions(constructorName) {
74
80
  const versionVector =
75
81
  self.EngineLib[constructorName]['getModelVersion']();
@@ -83,27 +89,34 @@
83
89
  }
84
90
  return models;
85
91
  },
92
+ _getNoLoadedModelVersions(constructorName) {
93
+ const modelVersions = this._getModelVersions(constructorName);
94
+ return modelVersions.filter(
95
+ (modelVersion) =>
96
+ !this._loadedSessions[this._getLoadedModelKey({ modelVersion })]
97
+ );
98
+ },
86
99
 
87
100
  // worker 專心處理 cpu 密集 (model 載入) 的工作,
88
101
  // 至於純 IO 下載的任務就交給 main thread 處理就好。
89
- loadModel({ modelName, modelArrayBuffer }) {
102
+ loadSession({ modelVersion: { name, version }, modelArrayBuffer }) {
90
103
  let fileMounted = false;
91
104
  try {
92
105
  const FS = self.EngineLib.FS;
93
- FS.mkdir(`/${modelName}/`);
106
+ FS.mkdir(`/${name}-${version}/`);
94
107
  FS.mount(
95
108
  FS.filesystems.WORKERFS,
96
109
  {
97
110
  blobs: [
98
111
  {
99
- name: modelName,
112
+ name: `${name}-${version}`,
100
113
  data: new Blob([modelArrayBuffer], {
101
114
  type: 'application/octet-stream',
102
115
  }),
103
116
  },
104
117
  ],
105
118
  },
106
- `/${modelName}/`
119
+ `/${name}-${version}/`
107
120
  );
108
121
  fileMounted = true;
109
122
  } catch (error) {
@@ -116,11 +129,17 @@
116
129
  }
117
130
 
118
131
  if (fileMounted) {
119
- const session = self.EngineLib.createInferenceSession(
120
- modelName,
121
- `/${modelName}/${modelName}`
122
- );
123
- this.sessions[modelName] = session;
132
+ const sessionKey = this._getLoadedModelKey({
133
+ modelVersion: { name, version },
134
+ });
135
+ if (this._loadedSessions[sessionKey]) {
136
+ throw new Error('should be undefined here.');
137
+ }
138
+ this._loadedSessions[sessionKey] =
139
+ self.EngineLib.createInferenceSession(
140
+ name,
141
+ `/${name}-${version}/${name}-${version}`
142
+ );
124
143
  }
125
144
  },
126
145
  async _init(constructorName) {
@@ -131,7 +150,10 @@
131
150
  const instance = new self.EngineLib[constructorName]();
132
151
  instance.initial(
133
152
  ...this._getModelVersions(constructorName).map(
134
- ({ name }) => this.sessions[name]
153
+ (modelVersion) =>
154
+ this._loadedSessions[
155
+ this._getLoadedModelKey({ modelVersion })
156
+ ]
135
157
  )
136
158
  );
137
159
  const engine = instance;
@@ -224,10 +246,10 @@
224
246
  this.instance.delete();
225
247
  this.instance = null;
226
248
  }
227
- for (const session of Object.values(this.sessions)) {
249
+ for (const session of Object.values(this._loadedSessions)) {
228
250
  self.EngineLib.deleteSession(session);
229
251
  }
230
- this.sessions = {};
252
+ this._loadedSessions = {};
231
253
  },
232
254
  getJsonReport() {
233
255
  return this.instance?.getJsonReport();
@@ -236,10 +258,12 @@
236
258
 
237
259
  // PassportService
238
260
  self.PassportService = {
261
+ instance: null,
262
+ _loadedSessions: {},
239
263
  ...baseFunctions,
240
264
 
241
- getModelVersions() {
242
- return this._getModelVersions('PassportService');
265
+ getNoLoadedModelVersions() {
266
+ return this._getNoLoadedModelVersions('PassportService');
243
267
  },
244
268
  init() {
245
269
  return this._init('PassportService');
@@ -272,10 +296,12 @@
272
296
 
273
297
  // Card OCR
274
298
  self.CardOCR = {
299
+ instance: null,
300
+ _loadedSessions: {},
275
301
  ...baseFunctions,
276
302
 
277
- getModelVersions() {
278
- return this._getModelVersions('CardOCR');
303
+ getNoLoadedModelVersions() {
304
+ return this._getNoLoadedModelVersions('CardOCR');
279
305
  },
280
306
  init() {
281
307
  return this._init('CardOCR');
@@ -296,12 +322,14 @@
296
322
  }
297
323
  return result;
298
324
  },
299
- setType(params) {
325
+ setType({ type }) {
300
326
  const engine = this.instance;
301
327
  if (engine) {
302
328
  const engineParams = engine.getParams();
303
329
  engineParams.eTargetCardType =
304
- self.EngineLib.EAuthMeCardClass.values[params.type];
330
+ self.EngineLib.EAuthMeCardClass[
331
+ type.replace(/^EAuthMeCardClass_/, '')
332
+ ];
305
333
  engineParams.fImageBlurTh = 750;
306
334
  engine.setParams(engineParams);
307
335
  return true;
@@ -325,10 +353,12 @@
325
353
 
326
354
  // Id Card Anti Fraud
327
355
  self.IdCardAntiFraud = {
356
+ instance: null,
357
+ _loadedSessions: {},
328
358
  ...baseFunctions,
329
359
 
330
- getModelVersions() {
331
- return this._getModelVersions('IdCardAntiFraudService');
360
+ getNoLoadedModelVersions() {
361
+ return this._getNoLoadedModelVersions('IdCardAntiFraudService');
332
362
  },
333
363
  async init() {
334
364
  await this._init('IdCardAntiFraudService');
@@ -382,10 +412,12 @@
382
412
  };
383
413
 
384
414
  self.Fas = {
415
+ instance: null,
416
+ _loadedSessions: {},
385
417
  ...baseFunctions,
386
418
 
387
- getModelVersions() {
388
- return this._getModelVersions('CFASService');
419
+ getNoLoadedModelVersions() {
420
+ return this._getNoLoadedModelVersions('CFASService');
389
421
  },
390
422
  init() {
391
423
  return this._init('CFASService');
@@ -448,6 +480,27 @@
448
480
  }
449
481
  return false;
450
482
  },
483
+ checkEnumConsistency(enums) {
484
+ const problem = [];
485
+ for (const [enumName, enumDef] of Object.entries(enums)) {
486
+ const actualEnumDef = self.EngineLib[enumName];
487
+ const actualEnumValues = actualEnumDef?.values ?? {};
488
+ const actualEnumMap = Object.fromEntries(
489
+ Object.values(actualEnumValues).map((enumValue) => [
490
+ enumValue.constructor.name,
491
+ enumValue.value,
492
+ ])
493
+ );
494
+
495
+ for (const [key, engineKey] of Object.entries(enumDef)) {
496
+ const actualValue = actualEnumMap[engineKey];
497
+ if (typeof actualValue === 'undefined') {
498
+ problem.push({ enumName, key, engineKey, actualEnumMap });
499
+ }
500
+ }
501
+ }
502
+ return problem;
503
+ },
451
504
  };
452
505
 
453
506
  addEventListener(