@authme/engine 2.2.0-rc.8 → 2.2.0

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.
@@ -3,8 +3,6 @@
3
3
  let wasm_loaded = false;
4
4
  let wasm_loading = false;
5
5
 
6
- let modelSession = {};
7
-
8
6
  function importScriptsViaJsContent(jsContent) {
9
7
  const objectUrl = URL.createObjectURL(
10
8
  new Blob([jsContent], { type: 'text/javascript' })
@@ -13,40 +11,8 @@
13
11
  URL.revokeObjectURL(objectUrl);
14
12
  }
15
13
 
16
- function base64toBlob(data) {
17
- const byteChars = self.atob(data);
18
- const byteNumbers = new Array(byteChars.length);
19
- for (let i = 0; i < byteChars.length; i++) {
20
- byteNumbers[i] = byteChars.charCodeAt(i);
21
- }
22
- const byteArray = new Uint8Array(byteNumbers);
23
- return new Blob([byteArray], { type: 'application/octet-stream' });
24
- }
25
-
26
- async function fetchModel(code) {
27
- const method = self.config.dataTransferMethod || 'binary';
28
-
29
- const resp = await fetch(
30
- `${self.config.apiBaseUrl}/api/model-management/v1/model/${code}${
31
- method === 'binary' ? '/file' : ''
32
- }`,
33
- {
34
- headers: {
35
- Authorization: 'Bearer ' + self.config.token,
36
- },
37
- }
38
- );
39
-
40
- if (method === 'binary') {
41
- return resp.blob();
42
- } else {
43
- const data = await resp.json();
44
- return base64toBlob(data.data);
45
- }
46
- }
47
-
48
14
  async function loadWasm({ engineJsContent, engineWasmContent }) {
49
- return new Promise((res, rej) => {
15
+ return new Promise(function loadWasmExecutor(res, rej) {
50
16
  // console.log('load wasm');
51
17
  if (wasm_loaded || wasm_loading) {
52
18
  res(true);
@@ -65,7 +31,7 @@
65
31
  },
66
32
  };
67
33
  importScriptsViaJsContent(engineJsContent);
68
- let tryInitEngineLib = async () => {
34
+ async function tryInitEngineLib() {
69
35
  try {
70
36
  const module = await AuthmeMLEngineLib({
71
37
  wasmBinary: engineWasmContent,
@@ -89,64 +55,207 @@
89
55
  console.warn(
90
56
  'init Authme EngineLib error:',
91
57
  error,
92
- ' Try after 1 second.'
58
+ ' Try after 3 second.'
93
59
  );
94
- await waitTime(500);
60
+ await waitTime(3000);
95
61
  await tryInitEngineLib();
96
62
  }
97
- };
63
+ }
98
64
  tryInitEngineLib();
99
65
  }
100
66
  });
101
67
  }
102
68
 
103
- // PassportService
104
- self.PassportService = {
69
+ // Base
70
+ const baseFunctions = {
105
71
  instance: null,
106
- init: async function () {
107
- return await self.Base.init(self.PassportService, 'PassportService');
72
+ sessions: {},
73
+ _getModelVersions(constructorName) {
74
+ const versionVector =
75
+ self.EngineLib[constructorName]['getModelVersion']();
76
+ const models = [];
77
+ for (let i = 0; i < versionVector.size(); i++) {
78
+ let item = versionVector.get(i);
79
+ models.push({
80
+ name: `${item.szName}`,
81
+ version: `${item.szVersion}`,
82
+ });
83
+ }
84
+ return models;
108
85
  },
109
- start: function () {
110
- return self.Base.start(self.PassportService);
86
+
87
+ // worker 專心處理 cpu 密集 (model 載入) 的工作,
88
+ // 至於純 IO 下載的任務就交給 main thread 處理就好。
89
+ loadModel({ modelName, modelArrayBuffer }) {
90
+ let fileMounted = false;
91
+ try {
92
+ const FS = self.EngineLib.FS;
93
+ FS.mkdir(`/${modelName}/`);
94
+ FS.mount(
95
+ FS.filesystems.WORKERFS,
96
+ {
97
+ blobs: [
98
+ {
99
+ name: modelName,
100
+ data: new Blob([modelArrayBuffer], {
101
+ type: 'application/octet-stream',
102
+ }),
103
+ },
104
+ ],
105
+ },
106
+ `/${modelName}/`
107
+ );
108
+ fileMounted = true;
109
+ } catch (error) {
110
+ if (error?.errno === 20) {
111
+ // File Exist
112
+ fileMounted = true;
113
+ } else {
114
+ throw error;
115
+ }
116
+ }
117
+
118
+ if (fileMounted) {
119
+ const session = self.EngineLib.createInferenceSession(
120
+ modelName,
121
+ `/${modelName}/${modelName}`
122
+ );
123
+ this.sessions[modelName] = session;
124
+ }
111
125
  },
112
- stop: function () {
113
- return self.Base.stop(self.PassportService);
126
+ async _init(constructorName) {
127
+ // 暫時將縮排透過遺留下來,減少 review 無變動的資訊。
128
+ {
129
+ {
130
+ {
131
+ const instance = new self.EngineLib[constructorName]();
132
+ instance.initial(
133
+ ...this._getModelVersions(constructorName).map(
134
+ ({ name }) => this.sessions[name]
135
+ )
136
+ );
137
+ const engine = instance;
138
+ const uiParams = engine.getUIParams();
139
+ uiParams.previewPosition.fLeft = 0;
140
+ uiParams.previewPosition.fTop = 0;
141
+ uiParams.previewPosition.fRight = 1;
142
+ uiParams.previewPosition.fBottom = 1;
143
+ engine.setUIParams(uiParams);
144
+ this.instance = instance;
145
+ return true;
146
+ }
147
+ }
148
+ }
114
149
  },
115
- getJsonReport: function () {
116
- return self.Base.getJsonReport(self.PassportService);
150
+ start() {
151
+ return this.instance?.start();
117
152
  },
118
- setFrameSize: function (params) {
119
- return self.Base.setFrameSize(self.PassportService, params);
153
+ stop() {
154
+ return this.instance?.stop();
120
155
  },
121
- getFinalResult: function (params) {
122
- return self.PassportService.instance.getFinalResult();
156
+ _setFrameSize(params) {
157
+ config.frame.width = Math.floor(params.width);
158
+ config.frame.height = Math.floor(params.height);
159
+ if (this.instance) {
160
+ const engine = this.instance;
161
+ const uiParams = engine.getUIParams();
162
+ uiParams.analyzeSize.iHeight = config.frame.height;
163
+ uiParams.analyzeSize.iWidth = config.frame.width;
164
+ engine.setUIParams(uiParams);
165
+ return true;
166
+ }
123
167
  },
124
- toJson: function (params) {
125
- return Object.getPrototypeOf(
126
- self.PassportService.instance
127
- ).constructor.toJson(params);
168
+ setFrameSize(params) {
169
+ return this._setFrameSize(params);
128
170
  },
129
- recognition: function (params) {
130
- const result = self.Base.recognition(self.PassportService, params);
131
- if (result) {
132
- result.eStatus = result.eStatus.constructor.name;
133
- result.tField = JSON.parse(self.PassportService.toJson(result.tField));
171
+ _recognition(params) {
172
+ const engine = this.instance;
173
+ if (engine) {
174
+ try {
175
+ const data = params.data;
176
+ const heapInfo = putOnHeap(data);
177
+ const result = engine.run(
178
+ heapInfo.byteOffset,
179
+ heapInfo.length,
180
+ config.frame.width,
181
+ config.frame.height
182
+ );
183
+ self.EngineLib._free(heapInfo.dataPtr);
184
+ return result;
185
+ } catch (error) {
186
+ // console.warn(error);
187
+ // console.warn('[Recognition Error]: ' + error?.message);
188
+ return null;
189
+ }
134
190
  }
135
- return result;
191
+ console.warn('engine not initialized');
136
192
  },
137
- getParams: function () {
138
- return self.Base.getParams(self.PassportService);
193
+ recognition(params) {
194
+ return this._recognition(params);
139
195
  },
140
- setParams: function (params) {
141
- return self.Base.setParams(self.PassportService, params);
196
+ getParams() {
197
+ return this.instance.getParams();
142
198
  },
143
- getDebugImage: function (params) {
144
- return self.Base.getDebugImage(self.PassportService, params);
199
+ setParams(params) {
200
+ return this.instance.setParams(params.params);
145
201
  },
146
- destroy: function () {
147
- return self.Base.destroy(self.PassportService);
202
+ getDebugImage(params) {
203
+ const data = params.data;
204
+ const heapInfo = putOnHeap(data);
205
+ let imageData;
206
+ const pointer = this.instance.getDebugImage(
207
+ heapInfo.byteOffset,
208
+ heapInfo.length,
209
+ config.frame.width,
210
+ config.frame.height
211
+ );
212
+ self.EngineLib._free(heapInfo.dataPtr);
213
+ if (pointer) {
214
+ imageData = self.EngineLib.HEAPU8.slice(
215
+ pointer,
216
+ pointer + config.frame.width * config.frame.height * 4
217
+ );
218
+ self.EngineLib._free(pointer);
219
+ }
220
+ return imageData;
221
+ },
222
+ destroy() {
223
+ if (this.instance) {
224
+ this.instance.delete();
225
+ this.instance = null;
226
+ }
227
+ for (const session of Object.values(this.sessions)) {
228
+ self.EngineLib.deleteSession(session);
229
+ }
230
+ this.sessions = {};
231
+ },
232
+ getJsonReport() {
233
+ return this.instance?.getJsonReport();
234
+ },
235
+ };
236
+
237
+ // PassportService
238
+ self.PassportService = {
239
+ ...baseFunctions,
240
+
241
+ getModelVersions() {
242
+ return this._getModelVersions('PassportService');
243
+ },
244
+ init() {
245
+ return this._init('PassportService');
246
+ },
247
+ toJson(params) {
248
+ return Object.getPrototypeOf(this.instance).constructor.toJson(params);
249
+ },
250
+ recognition(params) {
251
+ const result = this._recognition(params);
252
+ if (result) {
253
+ result.eStatus = result.eStatus.constructor.name;
254
+ result.tField = JSON.parse(self.PassportService.toJson(result.tField));
255
+ }
256
+ return result;
148
257
  },
149
- setMaskPosition: function (params) {
258
+ setMaskPosition(params) {
150
259
  const positions = params.positions;
151
260
  return self.PassportService.instance?.setMatchROI(
152
261
  positions[0][0],
@@ -163,25 +272,16 @@
163
272
 
164
273
  // Card OCR
165
274
  self.CardOCR = {
166
- instance: null,
167
- init: async function () {
168
- let result = await self.Base.init(self.CardOCR, 'CardOCR');
169
- return result;
170
- },
171
- start: function () {
172
- return self.Base.start(self.CardOCR);
173
- },
174
- stop: function () {
175
- return self.Base.stop(self.CardOCR);
176
- },
177
- getJsonReport: function () {
178
- return self.Base.getJsonReport(self.CardOCR);
275
+ ...baseFunctions,
276
+
277
+ getModelVersions() {
278
+ return this._getModelVersions('CardOCR');
179
279
  },
180
- setFrameSize: function (params) {
181
- return self.Base.setFrameSize(self.CardOCR, params);
280
+ init() {
281
+ return this._init('CardOCR');
182
282
  },
183
- recognition: function (params) {
184
- const result = self.Base.recognition(self.CardOCR, params);
283
+ recognition(params) {
284
+ const result = this._recognition(params);
185
285
  if (result) {
186
286
  if (result.pointer > 0) {
187
287
  result.imageData = self.EngineLib.HEAPU8.slice(
@@ -196,11 +296,8 @@
196
296
  }
197
297
  return result;
198
298
  },
199
- getDebugImage: function (params) {
200
- return self.Base.getDebugImage(self.CardOCR, params);
201
- },
202
- setType: function (params) {
203
- const engine = self.CardOCR.instance;
299
+ setType(params) {
300
+ const engine = this.instance;
204
301
  if (engine) {
205
302
  const engineParams = engine.getParams();
206
303
  engineParams.eTargetCardType =
@@ -211,9 +308,9 @@
211
308
  }
212
309
  return false;
213
310
  },
214
- setMaskPosition: function (params) {
311
+ setMaskPosition(params) {
215
312
  const positions = params.positions;
216
- return self.CardOCR.instance?.setCardMatchROI(
313
+ return this.instance?.setCardMatchROI(
217
314
  positions[0][0],
218
315
  positions[0][1],
219
316
  positions[1][0],
@@ -224,44 +321,27 @@
224
321
  positions[3][1]
225
322
  );
226
323
  },
227
- getParams: function () {
228
- return self.Base.getParams(self.CardOCR);
229
- },
230
- setParams: function (params) {
231
- return self.Base.setParams(self.CardOCR, params);
232
- },
233
- destroy: function () {
234
- return self.Base.destroy(self.CardOCR);
235
- },
236
324
  };
237
325
 
238
326
  // Id Card Anti Fraud
239
327
  self.IdCardAntiFraud = {
240
- instance: null,
241
- init: async function () {
242
- await self.Base.init(self.IdCardAntiFraud, 'IdCardAntiFraudService');
243
- if (self.IdCardAntiFraud.instance) {
244
- const engine = self.IdCardAntiFraud.instance;
328
+ ...baseFunctions,
329
+
330
+ getModelVersions() {
331
+ return this._getModelVersions('IdCardAntiFraudService');
332
+ },
333
+ async init() {
334
+ await this._init('IdCardAntiFraudService');
335
+ if (this.instance) {
336
+ const engine = this.instance;
245
337
  const engineParams = engine.getParams();
246
338
  engineParams.timeoutSec = 52;
247
339
  engine.setParams(engineParams);
248
340
  return true;
249
341
  }
250
342
  },
251
- start: function () {
252
- return self.Base.start(self.IdCardAntiFraud);
253
- },
254
- stop: function () {
255
- return self.Base.stop(self.IdCardAntiFraud);
256
- },
257
- getJsonReport: function () {
258
- return self.Base.getJsonReport(self.IdCardAntiFraud);
259
- },
260
- setFrameSize: function (params) {
261
- return self.Base.setFrameSize(self.IdCardAntiFraud, params);
262
- },
263
- recognition: function (params) {
264
- const result = self.Base.recognition(self.IdCardAntiFraud, params);
343
+ recognition(params) {
344
+ const result = this._recognition(params);
265
345
  if (result) {
266
346
  result.eStatus = result.eStatus.constructor.name;
267
347
  result.eStage = result.eStage.constructor.name;
@@ -275,9 +355,9 @@
275
355
  }
276
356
  return result;
277
357
  },
278
- setMaskPosition: function (params) {
358
+ setMaskPosition(params) {
279
359
  const positions = params.positions;
280
- return self.IdCardAntiFraud.instance?.setFrontalCardVertices(
360
+ return this.instance?.setFrontalCardVertices(
281
361
  positions[0][0],
282
362
  positions[0][1],
283
363
  positions[1][0],
@@ -288,62 +368,53 @@
288
368
  positions[3][1]
289
369
  );
290
370
  },
291
- setStage: function (params) {
292
- if (self.IdCardAntiFraud.instance) {
371
+ setStage(params) {
372
+ if (this.instance) {
293
373
  const stageList = new self.EngineLib.FraudStageList();
294
- params.forEach((x) => {
295
- stageList.push_back(self.EngineLib.EAuthMeIDCardAntiFraudStage[x]);
296
- });
297
- self.IdCardAntiFraud.instance.setStage(stageList);
374
+ for (const stage of params) {
375
+ stageList.push_back(
376
+ self.EngineLib.EAuthMeIDCardAntiFraudStage[stage]
377
+ );
378
+ }
379
+ this.instance.setStage(stageList);
298
380
  }
299
381
  },
300
- getParams: function () {
301
- return self.Base.getParams(self.IdCardAntiFraud);
302
- },
303
- setParams: function (params) {
304
- return self.Base.setParams(self.IdCardAntiFraud, params);
305
- },
306
- getDebugImage: function (params) {
307
- return self.Base.getDebugImage(self.IdCardAntiFraud, params);
308
- },
309
- destroy: function () {
310
- return self.Base.destroy(self.IdCardAntiFraud);
311
- },
312
382
  };
313
383
 
314
384
  self.Fas = {
315
- instance: null,
316
- init: async function () {
317
- await self.Base.init(self.Fas, 'CFASService');
318
- },
319
- start: function () {
320
- return self.Base.start(self.Fas);
321
- },
322
- stop: function () {
323
- return self.Base.stop(self.Fas);
385
+ ...baseFunctions,
386
+
387
+ getModelVersions() {
388
+ return this._getModelVersions('CFASService');
324
389
  },
325
- getJsonReport: function () {
326
- return self.Base.getJsonReport(self.Fas);
390
+ init() {
391
+ return this._init('CFASService');
327
392
  },
328
- setFrameSize: function (params) {
329
- self.Base.setFrameSize(self.Fas, params);
393
+ setFrameSize(params) {
394
+ this._setFrameSize(params);
330
395
  if (config.frame.width / config.frame.height >= 1) {
331
- const params = self.Fas.instance.getParams();
396
+ const params = this.instance.getParams();
332
397
  params.fFaceRoiPreviewRatioW = 0.3;
333
- self.Fas.instance.setParams(params);
398
+ this.instance.setParams(params);
334
399
  }
335
400
  },
336
- setStage: function (params) {
337
- if (self.Fas.instance) {
401
+ setStage(params) {
402
+ if (this.instance) {
338
403
  const stageList = new self.EngineLib.FASStageList();
339
- params.stageList.forEach((x) => {
340
- stageList.push_back(self.EngineLib.EAuthMeFASServiceStage[x]);
341
- });
342
- self.Fas.instance.setStage(stageList);
404
+ for (const stage of params.stageList) {
405
+ stageList.push_back(self.EngineLib.EAuthMeFASServiceStage[stage]);
406
+ }
407
+ this.instance.setStage(stageList);
343
408
  }
344
409
  },
345
- recognition: function (params) {
346
- const result = self.Base.recognition(self.Fas, params);
410
+ getStageParams({ stageIndex }) {
411
+ return this.instance.getStageParams(stageIndex);
412
+ },
413
+ setStageParams({ stageIndex, value }) {
414
+ this.instance.setStageParams(value, stageIndex);
415
+ },
416
+ recognition(params) {
417
+ const result = this._recognition(params);
347
418
  if (result) {
348
419
  result.eStatus = result.eStatus.constructor.name;
349
420
  result.eStage = result.eStage.constructor.name;
@@ -355,175 +426,25 @@
355
426
  }
356
427
  return result;
357
428
  },
358
- getParams: function () {
359
- return self.Base.getParams(self.Fas);
360
- },
361
- setParams: function (params) {
362
- return self.Base.setParams(self.Fas, params);
363
- },
364
- getDebugImage: function (params) {
365
- return self.Base.getDebugImage(self.Fas, params);
366
- },
367
- destroy: function () {
368
- return self.Base.destroy(self.Fas);
369
- },
370
- };
371
-
372
- // Base
373
- self.Base = {
374
- init: async function (target, constructorName) {
375
- return new Promise((res, rej) => {
376
- try {
377
- setTimeout(async () => {
378
- const models = [];
379
- const versionVector =
380
- self.EngineLib[constructorName]['getModelVersion']();
381
- target.sessionUsed = [];
382
- for (let i = 0; i < versionVector.size(); i++) {
383
- let item = versionVector.get(i);
384
- target.sessionUsed.push(item.szName);
385
- models.push({
386
- name: item.szName,
387
- version: item.szVersion,
388
- });
389
- }
390
-
391
- let queryModelString = models
392
- .map((x) => 'models=' + x.name + '[' + x.version + ']')
393
- .join('&');
394
- let modelPath = await loadModels(queryModelString);
395
- let modelPointer = models.map(({ name }, index) => {
396
- if (modelSession[name]) {
397
- return modelSession[name];
398
- } else {
399
- modelSession[name] = self.EngineLib.createInferenceSession(
400
- name,
401
- modelPath[index]
402
- );
403
- return modelSession[name];
404
- }
405
- });
406
- const instance = new self.EngineLib[constructorName]();
407
- instance.initial(...modelPointer);
408
- const engine = instance;
409
- const uiParams = engine.getUIParams();
410
- uiParams.previewPosition.fLeft = 0;
411
- uiParams.previewPosition.fTop = 0;
412
- uiParams.previewPosition.fRight = 1;
413
- uiParams.previewPosition.fBottom = 1;
414
- engine.setUIParams(uiParams);
415
- target.instance = instance;
416
- res(true);
417
- }, 10);
418
- } catch (error) {
419
- rej(error.message);
420
- }
421
- });
422
- },
423
- start: function (target) {
424
- return target?.instance?.start();
425
- },
426
- stop: function (target) {
427
- return target?.instance?.stop();
428
- },
429
- setFrameSize: function (target, params) {
430
- config.frame.width = Math.floor(params.width);
431
- config.frame.height = Math.floor(params.height);
432
- if (target.instance) {
433
- const engine = target.instance;
434
- const uiParams = engine.getUIParams();
435
- uiParams.analyzeSize.iHeight = config.frame.height;
436
- uiParams.analyzeSize.iWidth = config.frame.width;
437
- engine.setUIParams(uiParams);
438
- return true;
439
- }
440
- },
441
- recognition: function (target, params) {
442
- const engine = target.instance;
443
- if (engine) {
444
- try {
445
- const data = params.data;
446
- const heapInfo = putOnHeap(data);
447
- const result = engine.run(
448
- heapInfo.byteOffset,
449
- heapInfo.length,
450
- config.frame.width,
451
- config.frame.height
452
- );
453
- self.EngineLib._free(heapInfo.dataPtr);
454
- return result;
455
- } catch (error) {
456
- // console.warn(error);
457
- // console.warn('[Recognition Error]: ' + error?.message);
458
- return null;
459
- }
460
- }
461
- console.warn('engine not initialized');
462
- },
463
- getParams: function (target) {
464
- return target.instance.getParams();
465
- },
466
- setParams: function (target, params) {
467
- return target.instance.setParams(params.params);
468
- },
469
- getDebugImage: function (target, params) {
470
- const data = params.data;
471
- const heapInfo = putOnHeap(data);
472
- let imageData;
473
- const pointer = target.instance.getDebugImage(
474
- heapInfo.byteOffset,
475
- heapInfo.length,
476
- config.frame.width,
477
- config.frame.height
478
- );
479
- self.EngineLib._free(heapInfo.dataPtr);
480
- if (pointer) {
481
- imageData = self.EngineLib.HEAPU8.slice(
482
- pointer,
483
- pointer + config.frame.width * config.frame.height * 4
484
- );
485
- self.EngineLib._free(pointer);
486
- }
487
- return imageData;
488
- },
489
- destroy: function (target) {
490
- if (target.instance) {
491
- target.instance.delete();
492
- target.instance = null;
493
- }
494
- if (target.sessionUsed) {
495
- target.sessionUsed.forEach((name) => {
496
- if (modelSession[name]) {
497
- self.EngineLib.deleteSession(modelSession[name]);
498
- modelSession[name] = null;
499
- }
500
- });
501
- target.sessionUsed = [];
502
- }
503
- },
504
- getJsonReport: function (target) {
505
- return target?.instance?.getJsonReport();
506
- },
507
429
  };
508
430
 
509
431
  self.Core = {
510
- setConfig: async function (config) {
432
+ async setConfig(config) {
511
433
  self.config = {
512
434
  ...self.config,
513
435
  ...config,
514
436
  };
515
437
  return true;
516
438
  },
517
- loadEngine: async function ({ engineJsContent, engineWasmContent }) {
439
+ async loadEngine({ engineJsContent, engineWasmContent }) {
518
440
  return await loadWasm({ engineJsContent, engineWasmContent });
519
441
  },
520
- verify: async function (params) {
442
+ async verify(params) {
521
443
  for (let index = 0; index < 20; index++) {
522
444
  if (self.EngineLib) {
523
445
  let result = self.EngineLib.verifySDK(params.cert, params.authToken);
524
446
  return result.constructor.name;
525
447
  }
526
- await waitTime(500);
527
448
  }
528
449
  return false;
529
450
  },
@@ -531,12 +452,12 @@
531
452
 
532
453
  addEventListener(
533
454
  'message',
534
- async function ({ data }) {
455
+ async function onWorkerMessage({ data }) {
535
456
  const { module, command, id, value } = data;
536
457
  const fn = self[module][command];
537
458
  if (fn) {
538
459
  // console.log(`call ${module}.${command} :`, value);
539
- const result = await fn(value);
460
+ const result = await fn.call(self[module], value);
540
461
  // console.log(`worker return value for ${command}:`, result);
541
462
  postMessage({
542
463
  module,
@@ -557,60 +478,6 @@
557
478
  },
558
479
  };
559
480
 
560
- async function loadModel(modelInfo) {
561
- const { name, code } = modelInfo;
562
- const modelData = await fetchModel(code);
563
- console.log(modelData);
564
- try {
565
- const FS = self.EngineLib.FS;
566
- FS.mkdir(name);
567
- FS.mount(
568
- FS.filesystems.WORKERFS,
569
- {
570
- blobs: [
571
- {
572
- name,
573
- data: modelData,
574
- },
575
- ],
576
- },
577
- '/' + name
578
- );
579
- } catch (error) {
580
- if (error?.errno === 20) {
581
- // File Exist
582
- return `/${name}/${name}`;
583
- } else {
584
- throw error;
585
- }
586
- }
587
-
588
- return `/${name}/${name}`;
589
- }
590
-
591
- async function loadModels(modelString) {
592
- const start = performance.now();
593
- const resp = await fetch(
594
- self.config.apiBaseUrl + '/api/model-management/v1/model?' + modelString,
595
- {
596
- headers: {
597
- Authorization: 'Bearer ' + self.config.token,
598
- },
599
- }
600
- );
601
- if (!resp.ok) {
602
- throw new Error('[HTTP Error] Status: ' + resp.status);
603
- }
604
- const result = await resp.json();
605
- const modelPath = [];
606
- for (let i = 0; i < result.models.length; i++) {
607
- modelPath.push(await loadModel(result.models[i]));
608
- }
609
- const end = performance.now();
610
- // console.log('Load Model Time: ', +end - start + ' ms');
611
- return modelPath;
612
- }
613
-
614
481
  function putOnHeap(data) {
615
482
  const Module = self.EngineLib;
616
483
  const uint8ArrData = new Uint8Array(data);
@@ -627,7 +494,7 @@
627
494
  };
628
495
  }
629
496
 
630
- async function waitTime(ms) {
497
+ function waitTime(ms) {
631
498
  return new Promise((res) => {
632
499
  setTimeout(() => {
633
500
  res(true);