UNPKG

@privateid/ultra-web-sdk-alpha

Version:
148 lines 7.72 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /* eslint-disable lines-between-class-members */ /* eslint-disable import/prefer-default-export */ import { proxy } from 'comlink'; import { checkBrowserCompatibility, checkLoadSimdBuild, checkSimdSupport, handleCacheContent } from './wasm.utils'; import { getDebugType, printLogs } from '../../global/shared.utils'; import { isLoad, ultraEnroll, ultraPredict, compareEmbeddings, pkiEncrypt, checkIfModelsLoaded, ultraScanBackDocument, ultraScanFrontDocument, ultraCompareEmbeddings, ultraAgeEstimate, ultraDocumentOcr, freeMemory, } from '../../../../utils'; export class WasmService { constructor() { this.isInitialized = false; this.hasSimdSupport = false; this.checkIfModelsLoaded = (isEnroll) => __awaiter(this, void 0, void 0, function* () { var _a; if (!this.isInitialized || !((_a = this.workerFunctions) === null || _a === void 0 ? void 0 : _a.checkIfModelsLoaded)) { return false; } try { return yield this.workerFunctions.checkIfModelsLoaded(isEnroll); } catch (error) { return false; } }); this.predict = (images, isSimd, config, callback) => __awaiter(this, void 0, void 0, function* () { var _b; if (!((_b = this.workerFunctions) === null || _b === void 0 ? void 0 : _b.ultraPredict)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraPredict(images, isSimd, config, callback); }); this.estimateAge = (images, isSimd, config, callback) => __awaiter(this, void 0, void 0, function* () { var _c; if (!((_c = this.workerFunctions) === null || _c === void 0 ? void 0 : _c.ultraAgeEstimate)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraAgeEstimate(images, isSimd, config, callback); }); this.compareEmbeddings = (embeddingsA, embeddingsB, config, callback) => __awaiter(this, void 0, void 0, function* () { var _d; if (!((_d = this.workerFunctions) === null || _d === void 0 ? void 0 : _d.ultraCompareEmbeddings)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraCompareEmbeddings(embeddingsA, embeddingsB, config, callback); }); this.scanFrontDocument = (imageData, isSimd, config, callback) => __awaiter(this, void 0, void 0, function* () { var _e; if (!((_e = this.workerFunctions) === null || _e === void 0 ? void 0 : _e.ultraScanFrontDocument)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraScanFrontDocument(imageData, isSimd, config, callback); }); this.scanBackDocument = (imageData, isSimd, config, callback) => __awaiter(this, void 0, void 0, function* () { var _f; if (!((_f = this.workerFunctions) === null || _f === void 0 ? void 0 : _f.ultraScanBackDocument)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraScanBackDocument(imageData, isSimd, config, callback); }); this.documentOcr = (imageData, config, callback) => __awaiter(this, void 0, void 0, function* () { var _g; if (!((_g = this.workerFunctions) === null || _g === void 0 ? void 0 : _g.ultraDocumentOcr)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraDocumentOcr(imageData, config, callback); }); this.pkiEncrypt = (payload) => __awaiter(this, void 0, void 0, function* () { var _h; if (!((_h = this.workerFunctions) === null || _h === void 0 ? void 0 : _h.pkiEncrypt)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.pkiEncrypt(payload); }); // TODO: Bring worker functions to this service. this.workerFunctions = { isLoad, ultraEnroll, ultraPredict, compareEmbeddings, pkiEncrypt, checkIfModelsLoaded, ultraScanBackDocument, ultraScanFrontDocument, ultraCompareEmbeddings, ultraAgeEstimate, ultraDocumentOcr, freeMemory, }; } initialize() { return __awaiter(this, void 0, void 0, function* () { this.hasSimdSupport = yield checkSimdSupport(); }); } // TODO: consider `loadModule` to be called within the initialize method loadModule(options) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const compatibility = checkBrowserCompatibility(); if (!compatibility.support) { return compatibility; } const cacheContent = yield handleCacheContent(); const shouldLoadSimdBuild = yield checkLoadSimdBuild(); const wasmErrorCallback = proxy(createCallbackError({ callback: options.wasmFailureCallback })); try { if (!((_a = this.workerFunctions) === null || _a === void 0 ? void 0 : _a.isLoad)) { const message = 'WASM worker not initialized - isLoad function not available'; printLogs('[WasmService]', message, 'ERROR'); return { support: false, message }; } const loaded = yield this.workerFunctions.isLoad(shouldLoadSimdBuild, options.apiUrl, options.sessionToken, options.publicKey, getDebugType(), true, options.timeout, (_b = options.useCdn) !== null && _b !== void 0 ? _b : true, options.shouldRegenerateSession || false, wasmErrorCallback, options.usageScenario || '', cacheContent); this.isInitialized = true; printLogs('[WasmService] loadModule', loaded); return { support: true }; } catch (error) { return { support: false, message: `Failed to load WASM module: ${error.message}`, }; } }); } enroll(imageData, config, callback) { var _a; return __awaiter(this, void 0, void 0, function* () { if (!((_a = this.workerFunctions) === null || _a === void 0 ? void 0 : _a.ultraEnroll)) { throw new Error('WASM worker not initialized'); } return this.workerFunctions.ultraEnroll(imageData, this.hasSimdSupport, config, callback); }); } } function createCallbackError({ callback }) { return (errorCode) => { printLogs(`Callback error! Error code: `, errorCode, 'ERROR'); callback(errorCode); }; } //# sourceMappingURL=WasmService.js.map