@authme/engine 2.2.1-rc.3 → 2.2.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.
@@ -1 +1,574 @@
1
- !function(){let a=!1,o=!1;async function n({engineJsContent:n,engineWasmContent:i}){return new Promise(function(s,e){var t;a||o?s(!0):(o=!0,self.Module={},self.Module={locateFile:(e,t)=>t+"/"+e,print:e=>{postMessage({module:"DEBUG",command:"DEBUG",id:null,value:e})}},t=n,t=URL.createObjectURL(new Blob([t],{type:"text/javascript"})),importScripts(t),URL.revokeObjectURL(t),async function t(){try{const e=await AuthmeMLEngineLib({wasmBinary:i,print:e=>{console.log(e),postMessage({module:"DEBUG",command:"DEBUG",id:null,value:e})}});(self.EngineLib=e).FS.mkdir("/model"),a=!0,o=!1,s(!0)}catch(e){console.warn("init Authme EngineLib error:",e," Try after 3 second."),n=3e3,await new Promise(e=>{setTimeout(()=>{e(!0)},n)}),await t()}var n}())})}var e={_getLoadedModelKey({modelVersion:{name:e,version:t}}){return e+"#"+t},_getModelVersions(e){const t=self.EngineLib[e].getModelVersion(),n=[];for(let e=0;e<t.size();e++){var s=t.get(e);n.push({name:""+s.szName,version:""+s.szVersion})}return n},_getNoLoadedModelVersions(e){const t=this._getModelVersions(e);return t.filter(e=>!this._loadedSessions[this._getLoadedModelKey({modelVersion:e})])},loadSession({modelVersion:{name:e,version:t},modelArrayBuffer:n}){let s=!1;try{const i=self.EngineLib.FS;i.mkdir(`/${e}-${t}/`),i.mount(i.filesystems.WORKERFS,{blobs:[{name:e+"-"+t,data:new Blob([n],{type:"application/octet-stream"})}]},`/${e}-${t}/`),s=!0}catch(e){if(20!==e?.errno)throw e;s=!0}if(s){n=this._getLoadedModelKey({modelVersion:{name:e,version:t}});if(this._loadedSessions[n])throw new Error("should be undefined here.");this._loadedSessions[n]=self.EngineLib.createInferenceSession(e,`/${e}-${t}/${e}-`+t)}},async _init(e){{const t=new self.EngineLib[e],n=(t.initial(...this._getModelVersions(e).map(e=>this._loadedSessions[this._getLoadedModelKey({modelVersion:e})])),t),s=n.getUIParams();return s.previewPosition.fLeft=0,s.previewPosition.fTop=0,s.previewPosition.fRight=1,s.previewPosition.fBottom=1,n.setUIParams(s),this.instance=t,!0}},start(){return this.instance?.start()},stop(){return this.instance?.stop()},_setFrameSize(e){if(i.frame.width=Math.floor(e.width),i.frame.height=Math.floor(e.height),this.instance){const t=this.instance,n=t.getUIParams();return n.analyzeSize.iHeight=i.frame.height,n.analyzeSize.iWidth=i.frame.width,t.setUIParams(n),!0}},setFrameSize(e){return this._setFrameSize(e)},_recognition(e){const t=this.instance;if(t)try{var n=r(e.data),s=t.run(n.byteOffset,n.length,i.frame.width,i.frame.height);return self.EngineLib._free(n.dataPtr),s}catch(e){return null}console.warn("engine not initialized")},recognition(e){return this._recognition(e)},getParams(){return this.instance.getParams()},setParams(e){return this.instance.setParams(e.params)},getDebugImage(e){e=r(e.data);let t;var n=this.instance.getDebugImage(e.byteOffset,e.length,i.frame.width,i.frame.height);return self.EngineLib._free(e.dataPtr),n&&(t=self.EngineLib.HEAPU8.slice(n,n+i.frame.width*i.frame.height*4),self.EngineLib._free(n)),t},destroy(){this.instance&&(this.instance.delete(),this.instance=null);for(const e of Object.values(this._loadedSessions))self.EngineLib.deleteSession(e);this._loadedSessions={}},getJsonReport(){return this.instance?.getJsonReport()}};self.PassportService={instance:null,_loadedSessions:{},...e,getNoLoadedModelVersions(){return this._getNoLoadedModelVersions("PassportService")},init(){return this._init("PassportService")},toJson(e){return Object.getPrototypeOf(this.instance).constructor.toJson(e)},recognition(e){const t=this._recognition(e);return t&&(t.eStatus=t.eStatus.constructor.name,t.tField=JSON.parse(self.PassportService.toJson(t.tField))),t},setMaskPosition(e){e=e.positions;return self.PassportService.instance?.setMatchROI(e[0][0],e[0][1],e[1][0],e[1][1],e[2][0],e[2][1],e[3][0],e[3][1])}},self.CardOCR={instance:null,_loadedSessions:{},...e,getNoLoadedModelVersions(){return this._getNoLoadedModelVersions("CardOCR")},init(){return this._init("CardOCR")},recognition(e){const t=this._recognition(e);return t&&(0<t.pointer&&(t.imageData=self.EngineLib.HEAPU8.slice(t.pointer,t.pointer+t.iHeight*t.iWidth*4),self.EngineLib._free(t.pointer)),t.eStatus=t.eStatus.constructor.name,t.info.cardClass.eClass=t.info.cardClass.eClass.constructor.name),t},setType({type:e}){const t=this.instance;if(t){const n=t.getParams();return n.eTargetCardType=self.EngineLib.EAuthMeCardClass[e.replace(/^EAuthMeCardClass_/,"")],n.fImageBlurTh=750,t.setParams(n),!0}return!1},setMaskPosition(e){e=e.positions;return this.instance?.setCardMatchROI(e[0][0],e[0][1],e[1][0],e[1][1],e[2][0],e[2][1],e[3][0],e[3][1])}},self.IdCardAntiFraud={instance:null,_loadedSessions:{},...e,getNoLoadedModelVersions(){return this._getNoLoadedModelVersions("IdCardAntiFraudService")},async init(){if(await this._init("IdCardAntiFraudService"),this.instance){const e=this.instance,t=e.getParams();return t.timeoutSec=52,e.setParams(t),!0}},recognition(e){const t=this._recognition(e);return t&&(t.eStatus=t.eStatus.constructor.name,t.eStage=t.eStage.constructor.name,t.info.eCardMatchStatus=t.info.eCardMatchStatus.constructor.name,t.info.cardClass.eClass=t.info.cardClass.eClass.constructor.name,t.info.metalTag.aeStatus=t.info.metalTag.aeStatus.map(e=>e.constructor.name)),t},setMaskPosition(e){e=e.positions;return this.instance?.setFrontalCardVertices(e[0][0],e[0][1],e[1][0],e[1][1],e[2][0],e[2][1],e[3][0],e[3][1])},setStage(e){if(this.instance){const t=new self.EngineLib.FraudStageList;for(const n of e)t.push_back(self.EngineLib.EAuthMeIDCardAntiFraudStage[n]);this.instance.setStage(t)}}},self.Fas={instance:null,_loadedSessions:{},...e,getNoLoadedModelVersions(){return this._getNoLoadedModelVersions("CFASService")},init(){return this._init("CFASService")},setFrameSize(e){if(this._setFrameSize(e),1<=i.frame.width/i.frame.height){const e=this.instance.getParams();e.fFaceRoiPreviewRatioW=.3,this.instance.setParams(e)}},setStage(e){if(this.instance){const t=new self.EngineLib.FASStageList;for(const n of e.stageList)t.push_back(self.EngineLib.EAuthMeFASServiceStage[n]);this.instance.setStage(t)}},getStageParams({stageIndex:e}){return this.instance.getStageParams(e)},setStageParams({stageIndex:e,value:t}){this.instance.setStageParams(t,e)},recognition(e){const t=this._recognition(e);return t&&(t.eStatus=t.eStatus.constructor.name,t.eStage=t.eStage.constructor.name,t.info.eye.eStatus=t.info.eye.eStatus.constructor.name,t.info.fas.eStatus=t.info.fas.eStatus.constructor.name,t.info.mouth.eStatus=t.info.mouth.eStatus.constructor.name,t.info.pose.ePose=t.info.pose.ePose.constructor.name,t.info.miniFAS=t.info.miniFAS.constructor.name),t}},self.Core={async setConfig(e){return self.config={...self.config,...e},!0},async loadEngine({engineJsContent:e,engineWasmContent:t}){return n({engineJsContent:e,engineWasmContent:t})},async verify(t){for(let e=0;e<20;e++)if(self.EngineLib)return self.EngineLib.verifySDK(t.cert,t.authToken).constructor.name;return!1},checkEnumConsistency(e){const t=[];for(var[n,s]of Object.entries(e)){var i,a,o=self.EngineLib[n]?.values??{},r=Object.fromEntries(Object.values(o).map(e=>[e.constructor.name,e.value]));for([i,a]of Object.entries(s))void 0===r[a]&&t.push({enumName:n,key:i,engineKey:a,actualEnumMap:r})}return t}},addEventListener("message",async function({data:e}){var{module:e,command:t,id:n,value:s}=e;const i=self[e][t];i&&(s=await i.call(self[e],s),postMessage({module:e,command:t,id:n,value:s}))}.bind(self));let i={scriptPath:"assets",apiBaseUrl:"/",frame:{width:0,height:0}};function r(e){const t=self.EngineLib;var e=new Uint8Array(e),n=e.length*e.BYTES_PER_ELEMENT,s=t._malloc(n);const i=new Uint8Array(t.HEAPU8.buffer,s,n);return i.set(e),{byteOffset:i.byteOffset,length:e.length,dataPtr:s,dataOnHeap:i,Module:t}}}();
1
+ (function () {
2
+ // Common func
3
+ let wasm_loaded = false;
4
+ let wasm_loading = false;
5
+
6
+ function importScriptsViaJsContent(jsContent) {
7
+ const objectUrl = URL.createObjectURL(
8
+ new Blob([jsContent], { type: 'text/javascript' })
9
+ );
10
+ importScripts(objectUrl);
11
+ URL.revokeObjectURL(objectUrl);
12
+ }
13
+
14
+ async function loadWasm({ engineJsContent, engineWasmContent }) {
15
+ return new Promise(function loadWasmExecutor(res, rej) {
16
+ // console.log('load wasm');
17
+ if (wasm_loaded || wasm_loading) {
18
+ res(true);
19
+ } else {
20
+ wasm_loading = true;
21
+ self['Module'] = {};
22
+ self['Module'] = {
23
+ locateFile: (path, scriptDirectory) => `${scriptDirectory}/${path}`,
24
+ print: (msg) => {
25
+ postMessage({
26
+ module: 'DEBUG',
27
+ command: 'DEBUG',
28
+ id: null,
29
+ value: msg,
30
+ });
31
+ },
32
+ };
33
+ importScriptsViaJsContent(engineJsContent);
34
+ async function tryInitEngineLib() {
35
+ try {
36
+ const module = await AuthmeMLEngineLib({
37
+ wasmBinary: engineWasmContent,
38
+ print: (msg) => {
39
+ console.log(msg);
40
+ postMessage({
41
+ module: 'DEBUG',
42
+ command: 'DEBUG',
43
+ id: null,
44
+ value: msg,
45
+ });
46
+ },
47
+ });
48
+ self.EngineLib = module;
49
+ module.FS.mkdir('/model');
50
+ wasm_loaded = true;
51
+ wasm_loading = false;
52
+ // console.log('load wasm finished');
53
+ res(true);
54
+ } catch (error) {
55
+ console.warn(
56
+ 'init Authme EngineLib error:',
57
+ error,
58
+ ' Try after 3 second.'
59
+ );
60
+ await waitTime(3000);
61
+ await tryInitEngineLib();
62
+ }
63
+ }
64
+ tryInitEngineLib();
65
+ }
66
+ });
67
+ }
68
+
69
+ // Base
70
+ const baseFunctions = {
71
+ // 由於不同 service 間的 _loadedSessions instance 是隔離開來的,
72
+ // 所以不能將 _loadedSessions: {} 直接放到這裡,
73
+ // 否則會造成 destroy 異常。
74
+ // 更好的做法會是用 class 替代現在的做法,只是改的幅度偏大,
75
+ // 所以暫時保留現有的做法。
76
+ _getLoadedModelKey({ modelVersion: { name, version } }) {
77
+ return `${name}#${version}`;
78
+ },
79
+ _getModelVersions(constructorName) {
80
+ const versionVector =
81
+ self.EngineLib[constructorName]['getModelVersion']();
82
+ const models = [];
83
+ for (let i = 0; i < versionVector.size(); i++) {
84
+ let item = versionVector.get(i);
85
+ models.push({
86
+ name: `${item.szName}`,
87
+ version: `${item.szVersion}`,
88
+ });
89
+ }
90
+ return models;
91
+ },
92
+ _getNoLoadedModelVersions(constructorName) {
93
+ const modelVersions = this._getModelVersions(constructorName);
94
+ return modelVersions.filter(
95
+ (modelVersion) =>
96
+ !this._loadedSessions[this._getLoadedModelKey({ modelVersion })]
97
+ );
98
+ },
99
+
100
+ // worker 專心處理 cpu 密集 (model 載入) 的工作,
101
+ // 至於純 IO 下載的任務就交給 main thread 處理就好。
102
+ loadSession({ modelVersion: { name, version }, modelArrayBuffer }) {
103
+ let fileMounted = false;
104
+ try {
105
+ const FS = self.EngineLib.FS;
106
+ FS.mkdir(`/${name}-${version}/`);
107
+ FS.mount(
108
+ FS.filesystems.WORKERFS,
109
+ {
110
+ blobs: [
111
+ {
112
+ name: `${name}-${version}`,
113
+ data: new Blob([modelArrayBuffer], {
114
+ type: 'application/octet-stream',
115
+ }),
116
+ },
117
+ ],
118
+ },
119
+ `/${name}-${version}/`
120
+ );
121
+ fileMounted = true;
122
+ } catch (error) {
123
+ if (error?.errno === 20) {
124
+ // File Exist
125
+ fileMounted = true;
126
+ } else {
127
+ throw error;
128
+ }
129
+ }
130
+
131
+ if (fileMounted) {
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
+ );
143
+ }
144
+ },
145
+ async _init(constructorName) {
146
+ // 暫時將縮排透過遺留下來,減少 review 無變動的資訊。
147
+ {
148
+ {
149
+ {
150
+ const instance = new self.EngineLib[constructorName]();
151
+ instance.initial(
152
+ ...this._getModelVersions(constructorName).map(
153
+ (modelVersion) =>
154
+ this._loadedSessions[
155
+ this._getLoadedModelKey({ modelVersion })
156
+ ]
157
+ )
158
+ );
159
+ const engine = instance;
160
+ const uiParams = engine.getUIParams();
161
+ uiParams.previewPosition.fLeft = 0;
162
+ uiParams.previewPosition.fTop = 0;
163
+ uiParams.previewPosition.fRight = 1;
164
+ uiParams.previewPosition.fBottom = 1;
165
+ engine.setUIParams(uiParams);
166
+ this.instance = instance;
167
+ return true;
168
+ }
169
+ }
170
+ }
171
+ },
172
+ start() {
173
+ return this.instance?.start();
174
+ },
175
+ stop() {
176
+ return this.instance?.stop();
177
+ },
178
+ _setFrameSize(params) {
179
+ config.frame.width = Math.floor(params.width);
180
+ config.frame.height = Math.floor(params.height);
181
+ if (this.instance) {
182
+ const engine = this.instance;
183
+ const uiParams = engine.getUIParams();
184
+ uiParams.analyzeSize.iHeight = config.frame.height;
185
+ uiParams.analyzeSize.iWidth = config.frame.width;
186
+ engine.setUIParams(uiParams);
187
+ return true;
188
+ }
189
+ },
190
+ setFrameSize(params) {
191
+ return this._setFrameSize(params);
192
+ },
193
+ _recognition(params) {
194
+ const engine = this.instance;
195
+ if (engine) {
196
+ try {
197
+ const data = params.data;
198
+ const heapInfo = putOnHeap(data);
199
+ const result = engine.run(
200
+ heapInfo.byteOffset,
201
+ heapInfo.length,
202
+ config.frame.width,
203
+ config.frame.height
204
+ );
205
+ self.EngineLib._free(heapInfo.dataPtr);
206
+ return result;
207
+ } catch (error) {
208
+ // console.warn(error);
209
+ // console.warn('[Recognition Error]: ' + error?.message);
210
+ return null;
211
+ }
212
+ }
213
+ console.warn('engine not initialized');
214
+ },
215
+ recognition(params) {
216
+ return this._recognition(params);
217
+ },
218
+ getParams() {
219
+ return this.instance.getParams();
220
+ },
221
+ setParams(params) {
222
+ return this.instance.setParams(params.params);
223
+ },
224
+ getDebugImage(params) {
225
+ const data = params.data;
226
+ const heapInfo = putOnHeap(data);
227
+ let imageData;
228
+ const pointer = this.instance.getDebugImage(
229
+ heapInfo.byteOffset,
230
+ heapInfo.length,
231
+ config.frame.width,
232
+ config.frame.height
233
+ );
234
+ self.EngineLib._free(heapInfo.dataPtr);
235
+ if (pointer) {
236
+ imageData = self.EngineLib.HEAPU8.slice(
237
+ pointer,
238
+ pointer + config.frame.width * config.frame.height * 4
239
+ );
240
+ self.EngineLib._free(pointer);
241
+ }
242
+ return imageData;
243
+ },
244
+ destroy() {
245
+ if (this.instance) {
246
+ this.instance.delete();
247
+ this.instance = null;
248
+ }
249
+ for (const session of Object.values(this._loadedSessions)) {
250
+ self.EngineLib.deleteSession(session);
251
+ }
252
+ this._loadedSessions = {};
253
+ },
254
+ getJsonReport() {
255
+ return this.instance?.getJsonReport();
256
+ },
257
+ };
258
+
259
+ // PassportService
260
+ self.PassportService = {
261
+ instance: null,
262
+ _loadedSessions: {},
263
+ ...baseFunctions,
264
+
265
+ getNoLoadedModelVersions() {
266
+ return this._getNoLoadedModelVersions('PassportService');
267
+ },
268
+ init() {
269
+ return this._init('PassportService');
270
+ },
271
+ toJson(params) {
272
+ return Object.getPrototypeOf(this.instance).constructor.toJson(params);
273
+ },
274
+ recognition(params) {
275
+ const result = this._recognition(params);
276
+ if (result) {
277
+ result.eStatus = result.eStatus.constructor.name;
278
+ result.tField = JSON.parse(self.PassportService.toJson(result.tField));
279
+ }
280
+ return result;
281
+ },
282
+ setMaskPosition(params) {
283
+ const positions = params.positions;
284
+ return self.PassportService.instance?.setMatchROI(
285
+ positions[0][0],
286
+ positions[0][1],
287
+ positions[1][0],
288
+ positions[1][1],
289
+ positions[2][0],
290
+ positions[2][1],
291
+ positions[3][0],
292
+ positions[3][1]
293
+ );
294
+ },
295
+ getFinalResult() {
296
+ return this.instance.getFinalResult();
297
+ },
298
+ };
299
+
300
+ // Card OCR
301
+ self.CardOCR = {
302
+ instance: null,
303
+ _loadedSessions: {},
304
+ ...baseFunctions,
305
+
306
+ getNoLoadedModelVersions() {
307
+ return this._getNoLoadedModelVersions('CardOCR');
308
+ },
309
+ init() {
310
+ return this._init('CardOCR');
311
+ },
312
+ recognition(params) {
313
+ const result = this._recognition(params);
314
+ if (result) {
315
+ if (result.pointer > 0) {
316
+ result.imageData = self.EngineLib.HEAPU8.slice(
317
+ result.pointer,
318
+ result.pointer + result.iHeight * result.iWidth * 4
319
+ );
320
+ self.EngineLib._free(result.pointer);
321
+ }
322
+ result.eStatus = result.eStatus.constructor.name;
323
+ result.info.cardClass.eClass =
324
+ result.info.cardClass.eClass.constructor.name;
325
+ }
326
+ return result;
327
+ },
328
+ setType({ type }) {
329
+ const engine = this.instance;
330
+ if (engine) {
331
+ const engineParams = engine.getParams();
332
+ engineParams.eTargetCardType =
333
+ self.EngineLib.EAuthMeCardClass[
334
+ type.replace(/^EAuthMeCardClass_/, '')
335
+ ];
336
+ engineParams.fImageBlurTh = 750;
337
+ engine.setParams(engineParams);
338
+ return true;
339
+ }
340
+ return false;
341
+ },
342
+ setMaskPosition(params) {
343
+ const positions = params.positions;
344
+ return this.instance?.setCardMatchROI(
345
+ positions[0][0],
346
+ positions[0][1],
347
+ positions[1][0],
348
+ positions[1][1],
349
+ positions[2][0],
350
+ positions[2][1],
351
+ positions[3][0],
352
+ positions[3][1]
353
+ );
354
+ },
355
+ };
356
+
357
+ // Id Card Anti Fraud
358
+ self.IdCardAntiFraud = {
359
+ instance: null,
360
+ _loadedSessions: {},
361
+ ...baseFunctions,
362
+
363
+ getNoLoadedModelVersions() {
364
+ return this._getNoLoadedModelVersions('IdCardAntiFraudService');
365
+ },
366
+ async init() {
367
+ await this._init('IdCardAntiFraudService');
368
+ if (this.instance) {
369
+ const engine = this.instance;
370
+ const engineParams = engine.getParams();
371
+ engineParams.timeoutSec = 52;
372
+ engine.setParams(engineParams);
373
+ return true;
374
+ }
375
+ },
376
+ recognition(params) {
377
+ const result = this._recognition(params);
378
+ if (result) {
379
+ result.eStatus = result.eStatus.constructor.name;
380
+ result.eStage = result.eStage.constructor.name;
381
+ result.info.eCardMatchStatus =
382
+ result.info.eCardMatchStatus.constructor.name;
383
+ result.info.cardClass.eClass =
384
+ result.info.cardClass.eClass.constructor.name;
385
+ result.info.metalTag.aeStatus = result.info.metalTag.aeStatus.map(
386
+ (x) => x.constructor.name
387
+ );
388
+ }
389
+ return result;
390
+ },
391
+ setMaskPosition(params) {
392
+ const positions = params.positions;
393
+ return this.instance?.setFrontalCardVertices(
394
+ positions[0][0],
395
+ positions[0][1],
396
+ positions[1][0],
397
+ positions[1][1],
398
+ positions[2][0],
399
+ positions[2][1],
400
+ positions[3][0],
401
+ positions[3][1]
402
+ );
403
+ },
404
+ setStage(params) {
405
+ if (this.instance) {
406
+ const stageList = new self.EngineLib.FraudStageList();
407
+ for (const stage of params) {
408
+ stageList.push_back(
409
+ self.EngineLib.EAuthMeIDCardAntiFraudStage[stage]
410
+ );
411
+ }
412
+ this.instance.setStage(stageList);
413
+ }
414
+ },
415
+ };
416
+
417
+ self.Fas = {
418
+ instance: null,
419
+ _loadedSessions: {},
420
+ ...baseFunctions,
421
+
422
+ getNoLoadedModelVersions() {
423
+ return this._getNoLoadedModelVersions('CFASService');
424
+ },
425
+ init() {
426
+ return this._init('CFASService');
427
+ },
428
+ setFrameSize(params) {
429
+ this._setFrameSize(params);
430
+ if (config.frame.width / config.frame.height >= 1) {
431
+ const params = this.instance.getParams();
432
+ params.fFaceRoiPreviewRatioW = 0.3;
433
+ this.instance.setParams(params);
434
+ }
435
+ },
436
+ setStage(params) {
437
+ if (this.instance) {
438
+ const stageList = new self.EngineLib.FASStageList();
439
+ for (const stage of params.stageList) {
440
+ stageList.push_back(self.EngineLib.EAuthMeFASServiceStage[stage]);
441
+ }
442
+ this.instance.setStage(stageList);
443
+ }
444
+ },
445
+ getStageParams({ stageIndex }) {
446
+ return this.instance.getStageParams(stageIndex);
447
+ },
448
+ setStageParams({ stageIndex, value }) {
449
+ this.instance.setStageParams(value, stageIndex);
450
+ },
451
+ recognition(params) {
452
+ const result = this._recognition(params);
453
+ if (result) {
454
+ result.eStatus = result.eStatus.constructor.name;
455
+ result.eStage = result.eStage.constructor.name;
456
+ result.info.eye.eStatus = result.info.eye.eStatus.constructor.name;
457
+ result.info.fas.eStatus = result.info.fas.eStatus.constructor.name;
458
+ result.info.mouth.eStatus = result.info.mouth.eStatus.constructor.name;
459
+ result.info.pose.ePose = result.info.pose.ePose.constructor.name;
460
+ result.info.miniFAS = result.info.miniFAS.constructor.name;
461
+ }
462
+ return result;
463
+ },
464
+ };
465
+
466
+ self.Core = {
467
+ async setConfig(config) {
468
+ self.config = {
469
+ ...self.config,
470
+ ...config,
471
+ };
472
+ return true;
473
+ },
474
+ async loadEngine({ engineJsContent, engineWasmContent }) {
475
+ return await loadWasm({ engineJsContent, engineWasmContent });
476
+ },
477
+ async verify(params) {
478
+ for (let index = 0; index < 20; index++) {
479
+ if (self.EngineLib) {
480
+ let result = self.EngineLib.verifySDK(params.cert, params.authToken);
481
+ return result.constructor.name;
482
+ }
483
+ }
484
+ return false;
485
+ },
486
+ checkEnumConsistency(enums) {
487
+ const problem = [];
488
+ for (const [enumName, enumDef] of Object.entries(enums)) {
489
+ const actualEnumDef = self.EngineLib[enumName];
490
+ const actualEnumValues = actualEnumDef?.values ?? {};
491
+ const actualEnumMap = Object.fromEntries(
492
+ Object.values(actualEnumValues).map((enumValue) => [
493
+ enumValue.constructor.name,
494
+ enumValue.value,
495
+ ])
496
+ );
497
+
498
+ for (const [key, engineKey] of Object.entries(enumDef)) {
499
+ const actualValue = actualEnumMap[engineKey];
500
+ if (typeof actualValue === 'undefined') {
501
+ problem.push({ enumName, key, engineKey, actualEnumMap });
502
+ }
503
+ }
504
+ }
505
+ return problem;
506
+ },
507
+ };
508
+
509
+ addEventListener(
510
+ 'message',
511
+ async function onWorkerMessage({ data }) {
512
+ try {
513
+ const { module, command, id, value } = data;
514
+ const fn = self[module][command];
515
+ if (fn) {
516
+ // console.log(`call ${module}.${command} :`, value);
517
+ const result = await fn.call(self[module], value);
518
+ // console.log(`worker return value for ${command}:`, result);
519
+ postMessage({
520
+ module,
521
+ command,
522
+ id,
523
+ value: result,
524
+ });
525
+ }
526
+ } catch (error) {
527
+ const { module, command, id } = data;
528
+ postMessage({
529
+ module,
530
+ command,
531
+ id,
532
+ value: {
533
+ error: true,
534
+ cause: error,
535
+ },
536
+ });
537
+ throw error;
538
+ }
539
+ }.bind(self)
540
+ );
541
+
542
+ let config = {
543
+ scriptPath: 'assets',
544
+ apiBaseUrl: '/',
545
+ frame: {
546
+ width: 0,
547
+ height: 0,
548
+ },
549
+ };
550
+
551
+ function putOnHeap(data) {
552
+ const Module = self.EngineLib;
553
+ const uint8ArrData = new Uint8Array(data);
554
+ const numBytes = uint8ArrData.length * uint8ArrData.BYTES_PER_ELEMENT;
555
+ const dataPtr = Module._malloc(numBytes);
556
+ const dataOnHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, numBytes);
557
+ dataOnHeap.set(uint8ArrData);
558
+ return {
559
+ byteOffset: dataOnHeap.byteOffset,
560
+ length: uint8ArrData.length,
561
+ dataPtr: dataPtr,
562
+ dataOnHeap: dataOnHeap,
563
+ Module: Module,
564
+ };
565
+ }
566
+
567
+ function waitTime(ms) {
568
+ return new Promise((res) => {
569
+ setTimeout(() => {
570
+ res(true);
571
+ }, ms);
572
+ });
573
+ }
574
+ })();