@scandit/web-datacapture-barcode
Version:
Scandit Data Capture SDK for the Web
1 lines • 2.06 kB
JavaScript
import{DataCaptureLoader as e}from"@scandit/web-datacapture-core";var t=`barcode-worker-8.5.0.js`;function n(e){try{var n=e.endsWith(`/`)?e:e+`/`;return new URL(t,n).href}catch(n){var r=n instanceof Error?n.message:String(n);throw Error(`Invalid worker URL: `+e+`/`+t+` (`+r+`)`)}}function r(e){try{return new URL(e).origin===location.origin}catch(n){var t=n instanceof Error?n.message:String(n);return console.warn(`Could not determine origin for worker URL "`+e+`": `+t+`. Defaulting to cross-origin path.`),!1}}function i(e,t){/javascript|ecmascript/i.test(e.type)||(e=new Blob([e],{type:`application/javascript`}));var n=URL.createObjectURL(e),r=new Worker(n,{name:t});return setTimeout(function(){URL.revokeObjectURL(n)},0),r}async function a(e){if(!e||!e.libraryLocation)throw Error(`ExternalWorker requires options.libraryLocation`);var t=n(e.libraryLocation);try{var a=await fetch(t,r(t)?{}:{mode:`cors`});if(!a.ok)throw Error(`Failed to load worker script: `+t+` (status `+a.status+`)`);var o=await a.blob();return r(t)?new Worker(t,{name:e.name}):i(o,e.name)}catch(e){throw e instanceof Error&&e.message.startsWith(`Failed to load worker script:`)?e:Error(`Failed to load worker script: `+t+` (`+(e instanceof Error?e.message:e)+`)`)}}var o=class t extends e{constructor(...e){super(...e),this._name=`BarcodeCapture`}get dataCaptureWorker(){if(!this._dataCaptureWorker)throw Error(`Worker not initialized. Use BarcodeCaptureWorkerManager.create() to instantiate.`);return this._dataCaptureWorker}get name(){return this._name}async createDataCaptureWorker(){return await a({name:this.name,libraryLocation:this.libraryLocation})}static async create(e){let n=new t(e);return n._dataCaptureWorker=await n.createDataCaptureWorker(),n.addWorkerMessageListener(),await n.workerCommand(`setLogLevel`,{level:n.logLevel}),await n.load(),n}async workerCommand(e,t,n){return new Promise((r,i)=>{let a=this.workerCommandId++;this.workerTasks.set(a,{resolve:r,reject:i,command:e});let o={...t,command:e,id:a};this.dataCaptureWorker.postMessage(o,n)})}};export{o as t};