@microblink/blinkid-core 7.2.1 → 7.3.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.
Files changed (29) hide show
  1. package/dist/blinkid-core.js +60 -53
  2. package/dist/blinkid-core.js.map +1 -1
  3. package/dist/resources/blinkid-worker.js +138 -16
  4. package/dist/resources/full/advanced/BlinkIdModule.data +0 -0
  5. package/dist/resources/full/advanced/BlinkIdModule.js +132 -144
  6. package/dist/resources/full/advanced/BlinkIdModule.wasm +0 -0
  7. package/dist/resources/full/advanced-threads/BlinkIdModule.data +0 -0
  8. package/dist/resources/full/advanced-threads/BlinkIdModule.js +158 -164
  9. package/dist/resources/full/advanced-threads/BlinkIdModule.wasm +0 -0
  10. package/dist/resources/full/basic/BlinkIdModule.data +0 -0
  11. package/dist/resources/full/basic/BlinkIdModule.js +131 -144
  12. package/dist/resources/full/basic/BlinkIdModule.wasm +0 -0
  13. package/dist/resources/lightweight/advanced/BlinkIdModule.data +0 -0
  14. package/dist/resources/lightweight/advanced/BlinkIdModule.js +131 -144
  15. package/dist/resources/lightweight/advanced/BlinkIdModule.wasm +0 -0
  16. package/dist/resources/lightweight/advanced-threads/BlinkIdModule.data +0 -0
  17. package/dist/resources/lightweight/advanced-threads/BlinkIdModule.js +158 -163
  18. package/dist/resources/lightweight/advanced-threads/BlinkIdModule.wasm +0 -0
  19. package/dist/resources/lightweight/basic/BlinkIdModule.data +0 -0
  20. package/dist/resources/lightweight/basic/BlinkIdModule.js +130 -144
  21. package/dist/resources/lightweight/basic/BlinkIdModule.wasm +0 -0
  22. package/package.json +1 -1
  23. package/types/createProxyWorker.d.ts.map +1 -1
  24. package/types/getCrossOriginWorkerURL.d.ts.map +1 -1
  25. package/types/getCrossOriginWorkerURL.test.d.ts +5 -0
  26. package/types/getCrossOriginWorkerURL.test.d.ts.map +1 -0
  27. package/types/index.rollup.d.ts +40 -5
  28. package/dist/resources/full/advanced-threads/BlinkIdModule.worker.mjs +0 -1
  29. package/dist/resources/lightweight/advanced-threads/BlinkIdModule.worker.mjs +0 -1
@@ -323,6 +323,48 @@ function requestResponseMessage(ep, pendingListeners, msg, transfers) {
323
323
  function generateUUID() {
324
324
  return new Array(4).fill(0).map(() => Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(16)).join("-");
325
325
  }
326
+ const type = "application/javascript";
327
+ const getCrossOriginWorkerURL = (originalWorkerUrl, _options = {}) => {
328
+ const options = {
329
+ skipSameOrigin: true,
330
+ useBlob: true,
331
+ ..._options
332
+ };
333
+ if (options.skipSameOrigin && new URL(originalWorkerUrl).origin === self.location.origin) {
334
+ return Promise.resolve(originalWorkerUrl);
335
+ }
336
+ let signal;
337
+ try {
338
+ const controller = new AbortController();
339
+ signal = controller.signal;
340
+ const timeout = setTimeout(() => {
341
+ controller.abort();
342
+ }, 3e3);
343
+ const cleanup = () => {
344
+ clearTimeout(timeout);
345
+ controller.abort();
346
+ };
347
+ signal.addEventListener("abort", cleanup);
348
+ } catch (error) {
349
+ }
350
+ return new Promise(
351
+ (resolve, reject) => void fetch(originalWorkerUrl, {
352
+ // abort if the worker is not fetched in a reasonable time
353
+ signal
354
+ }).then((res) => res.text()).then((codeString) => {
355
+ let finalURL = "";
356
+ if (options.useBlob) {
357
+ const blob = new Blob([codeString], { type });
358
+ finalURL = URL.createObjectURL(blob);
359
+ } else {
360
+ finalURL = `data:${type},` + encodeURIComponent(codeString);
361
+ }
362
+ resolve(finalURL);
363
+ }).catch(() => {
364
+ reject(new Error(`Failed to fetch worker from ${originalWorkerUrl}`));
365
+ })
366
+ );
367
+ };
326
368
  var _createClass = /* @__PURE__ */ function() {
327
369
  function defineProperties(target, props) {
328
370
  for (var i = 0; i < props.length; i++) {
@@ -596,60 +638,17 @@ new TemplateTag(inlineArrayTransformer({ separator: ",", conjunction: "and" }),
596
638
  new TemplateTag(inlineArrayTransformer, stripIndentTransformer, trimResultTransformer);
597
639
  new TemplateTag(inlineArrayTransformer, replaceResultTransformer(/(?:\s+)/g, " "), trimResultTransformer);
598
640
  new TemplateTag(stripIndentTransformer, trimResultTransformer);
599
- var stripIndents = new TemplateTag(stripIndentTransformer("all"), trimResultTransformer);
600
- const type = "application/javascript";
601
- const getCrossOriginWorkerURL = (originalWorkerUrl, _options = {}) => {
602
- const options = {
603
- skipSameOrigin: true,
604
- useBlob: true,
605
- ..._options
606
- };
607
- if (options.skipSameOrigin && new URL(originalWorkerUrl).origin === self.location.origin) {
608
- return Promise.resolve(originalWorkerUrl);
609
- }
610
- let signal;
611
- try {
612
- const controller = new AbortController();
613
- signal = controller.signal;
614
- const timeout = setTimeout(() => {
615
- controller.abort();
616
- }, 3e3);
617
- const cleanup = () => {
618
- clearTimeout(timeout);
619
- controller.abort();
620
- };
621
- signal.addEventListener("abort", cleanup);
622
- } catch (error) {
623
- }
624
- return new Promise(
625
- (resolve, reject) => void fetch(originalWorkerUrl, {
626
- // abort if the worker is not fetched in a reasonable time
627
- signal
628
- }).then((res) => res.text()).then((codeString) => {
629
- const workerPath = new URL(originalWorkerUrl).href.split("/");
630
- workerPath.pop();
631
- const importScriptsFix = stripIndents`
632
- const _importScripts = importScripts;
633
- const _fixImports = (url) => new URL(url, '${workerPath.join("/") + "/"}').href;
634
- importScripts = (...urls) => _importScripts(...urls.map(_fixImports));
635
- `;
636
- let finalURL = "";
637
- if (options.useBlob) {
638
- const blob = new Blob([importScriptsFix + codeString], { type });
639
- finalURL = URL.createObjectURL(blob);
640
- } else {
641
- finalURL = `data:${type},` + encodeURIComponent(importScriptsFix + codeString);
642
- }
643
- resolve(finalURL);
644
- }).catch(() => {
645
- reject(new Error(`Failed to fetch worker from ${originalWorkerUrl}`));
646
- })
647
- );
641
+ new TemplateTag(stripIndentTransformer("all"), trimResultTransformer);
642
+ const isDataUrl = (url) => {
643
+ return url.startsWith("data:");
648
644
  };
649
- const createProxyWorker = async (resourcesLocation = window.location.href) => {
650
- const workerUrl = await getCrossOriginWorkerURL(
651
- new URL("resources/blinkid-worker.js", resourcesLocation).toString()
652
- );
645
+ const isBlobUrl = (url) => {
646
+ return url.startsWith("blob:");
647
+ };
648
+ const needsHttpValidation = (url) => {
649
+ return !isDataUrl(url) && !isBlobUrl(url);
650
+ };
651
+ const validateHttpWorkerFile = async (workerUrl) => {
653
652
  const response = await fetch(workerUrl, { method: "HEAD" });
654
653
  const contentType = response.headers.get("content-type");
655
654
  const isJavascript = contentType?.includes("javascript");
@@ -669,6 +668,14 @@ const createProxyWorker = async (resourcesLocation = window.location.href) => {
669
668
  `Worker file not found or inaccessible: ${response.status} ${response.statusText}`
670
669
  );
671
670
  }
671
+ };
672
+ const createProxyWorker = async (resourcesLocation = window.location.href) => {
673
+ const workerUrl = await getCrossOriginWorkerURL(
674
+ new URL("resources/blinkid-worker.js", resourcesLocation).toString()
675
+ );
676
+ if (needsHttpValidation(workerUrl)) {
677
+ await validateHttpWorkerFile(workerUrl);
678
+ }
672
679
  const worker = new Worker(workerUrl, {
673
680
  type: "module"
674
681
  });