barcode-detector: Barcode detection service unavailable

https://github.com/Sec-ant/barcode-detector/blob/ab876a6a19b610ed3ce4b69f8216ed3c878421f4/src/BarcodeDetector.ts#L140-L143

i used vue-qrcode-reader at https website

<qrcode-stream @detect="onDetect"></qrcode-stream>

it shows "Barcode detection service unavailable. Use 'setZXingModuleOverrides' in offline or strict CSP environments.", "NotSupportedError"

the host already set Strict-Transport-Security: max-age=31536000; includeSubdomains; preload

but the static js file not set

i see the demo website https://vue-qrcode-reader.netlify.app. it’s doc and static js both set Strict-Transport-Security

how can i solve the detect error?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 47 (24 by maintainers)

Most upvoted comments

Oooh, I’m stupid 😅 I forgot I had disabled console & debugger on build…

This is the actual error: image

And the line it occurs: image

The error stack has very little information. The root cause was supposed to be printed along with the uncaught exception according to this line of code, I wonder if there is any?

Nope, nothing else is printed on console

The wasm file is lazily loaded on demand by default, so it will only be fetched on its first detection (there’s a switch to change this behavior in zxing-wasm, but they’re not exposed to barcode-detector or vue-qrcode-reader iirc). Probably this is the reason?

I thought so too, but the thing is… The Network Request doesn’t show when it’s working successfully with no error.

Yeah, I thought so, probably some kind of header issue. I’ll have to check with dev-ops tomorrow…

Thanks for all the help 🙏

Sadly, he AWS belongs to a client, so I can’t really provide a direct link to the server right now. I’ll check if I can figure a way to provide you with a link to a problematic instance.

Sure, here you go, it’s quite basic really: https://github.com/crazyrabbit0/TestVueQrcode

Mind the fact, that when I build it locally it works fine, even with separate asset files, but when I push it to the AWS server, I get the error mentioned above.

Ok you decide not to fetch the .wasm file from jsDelivr. There’re some gotchas, so I’d like to explain more:


First, a question: I wonder if you want to inline the .wasm file inside your script output files or you want to host it as a binary file on your site, and fetch it from your site at runtime?

Inline

Inlining means encoding the .wasm file as a Base64 data url and hardcode this data url inside your script files. If you use a syntax like this:

import wasmFile from "...?url"

then you’re inlining it. ?url is a syntax provided by build tools, like Vite, to transform assets into data urls. I’m not sure if vue-cli supports it. If it is supported, then the wasmFile will be a huge base64 encoded data url.

Afterwards, you use setZXingModuleOverrides + locateFile to relocate the fetching path to this data url:

setZXingModuleOverrides({
  locateFile: (path, prefix) => {
    if (path.endsWith(".wasm")) {
      return wasmFile; // <= a huge Base64 encoded data url
    }
    return prefix + path;
  },
});

A data url occupies a larger space than its binary form, and parsing a data url into binary data also consumes more CPU clocks. So, ideally, I suggest you to not use the inline form.

Binary

If you want to host it as a binary file on your site, then you need not to import it. You can just copy the .wasm file to your website’s assets folder, e.g. public or assets, and use setZXingModuleOverrides + locateFile to relocate the fetching path like this:

setZXingModuleOverrides({
  locateFile: (path, prefix) => {
    if (path.endsWith(".wasm")) {
      return "https://<your website host>/public/zxing_reader.wasm"; // <= where the .wasm file is at
    }
    return prefix + path;
  },
});

Either way, CSP still applies. You have to make sure content-src and script-src are correctly configured to allow you fetching and executing it.


Second question: where is the .wasm file?

Normally, when you add vue-qrcode-reader as a dependency in your project, it will also download its dependencies inside node_modules folder, which includes barcode-detector. And barcode-detector in turn will download its dependency @sec-ant/zxing-wasm. This process is handled by the package manager automatically so you don’t need to worry.

The .wasm file is published as an asset included in the package @sec-ant/zxing-wasm, therefore, the .wasm file can be found at this path:

<project root>/node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm

BUT, I made a mistake not pinning the version. So, the .wasm file you found in <project root>/node_modules/@sec-ant/zxing-wasm may not be what vue-qrcode-reader expects (Sorry for this mistake, I’m going to fix this in the next version). Therefore, for now, I recommend you manually download the .wasm file with the correct version, from the CDN.

If you’re using vue-qrcode-reader@v5.3.4, the matching version is @sec-ant/zxing-wasm@2.1.3, so you can download the zxing_reader.wasm file from any of the following CDN paths:

Once you’ve downloaded the correct version of the .wasm file, you can inline or host it as you wish in your project.

Again, I’m really sorry for the inconvenience. Once the version problem is fixed and released in vue-qrcode-reader, you won’t have to worry about downloading it with the matching version. You can just import or copy it from <project root>/node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm.

Hey, louie

I don’t think Strict-Transport-Security is relevant here. “CSP” is short for “Content Security Policy”.

This package uses WebAssembly to provide the core function of detecting barcodes. And the browser must instantiate and execute the WebAssembly module to use it. However, per the spec, websites can control whether to block the WebAssembly execution by setting the script-src directive in CSP, specifically the wasm-unsafe-eval or unsafe-eval source value. If it is blocked, the error Barcode detection service unavailable will get thrown.

The directive connect-src can also affect this package. To prevent bloating the size, this package will load the .wasm file from the cdn.jsdelivr.net CDN at runtime (using fetch). connect-src or default-src can restrict the URLs which can be loaded with fetch. So if this connection is somewhat get blocked, the service unavailable error will also be thrown.

There’re corresponding ways in which you can solve this problem:

  • If you have control on the website where you’re using this package, you can modify the CSP of the site to allow wasm-unsafe-eval. and make sure connect-src or default-src includes https://cdn.jsdelivr.net. A detailed explanation can be found at https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
  • If you don’t want to load the .wasm file from a CDN. You can use setZXingModuleOverrides exported by vue-qrcode-reader to change the location where the .wasm file is hosted. For example, you can host the file on your website as an asset, etc.

If neither solves your problem, it would be great if you can provide the malfunctioning website so that I can dig into it and check whether there’re some other bugs to fix.