-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to provide URL to zxing-wasm
#354
Comments
Good catch, thanks 👍 This is very annoying. Users shouldn't have to deal with this. There's a similar issue with providing web workers in packages. Ideally you would just bundle the wasm file with the package and somehow "include" it opaquely. But I don't know if there is an easy way to do that. |
Yep I didn't bundle the wasm file inside If vue-qrcode-reader wants to hide this option away from the users and to not use a third-party server, the easy way would be downloading the wasm file from the CDN (version pinning is recommended), saving it somewhere in the project considered as assets, and importing it as a base64 url with Vite's asset importing query strings. A small demo looks like this: import wasmFile from "../public/zxing_reader.wasm?url";
import { setZXingModuleOverrides } from "@sec-ant/barcode-detector";
setZXingModuleOverrides({
locateFile: (path, prefix) => {
if (path.endsWith(".wasm")) {
return wasmFile;
}
return prefix + path;
},
});
const barcodeDetector: BarcodeDetector = new BarcodeDetector({
formats: ["qr_code"],
});
fetch(
"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Hello%20world!"
)
.then((resp) => resp.blob())
.then((imageFile) => barcodeDetector.detect(imageFile))
.then(console.log); However, I suggest reexporting this PS: |
@gruhn @Sec-ant Thanks for taking care and the extensive explanation and example code. I think exposing the If I see it correctly, the wasm files are available as GitHub releases from https://github.com/Sec-ant/zxing-wasm-build/releases. So maybe it would even be possible to configure vite/webpack to handle the downloading automatically, somehow, for people that don't want to use the CDN, but their custom server. |
@raimund-schluessler Those wasm files are automatically built by GitHub actions, they are kept to sync with the upstream ZXing Cpp source code. That means not all of the builds are consumed in the downstream Back to the subject, actually, you don't have to manually download the wasm file. The package manager should've already downloaded them for you when you install any of the downstream packages, e.g. import wasmFile from "../node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm?url"; |
The BarcodeDetector polyfill fetches a WASM file at runtime from a CDN. That's a problem for offline applications or applications that run in a network with strict CSP policy. As a workaround the polyfill exports a function to configure the WASM file URL. So we re-export the function here. Also, by default the BarcodeDetector polyfill installs itself by setting `globalThis.BarcodeDetector` but only if there is no native support. This is likely not SSR compatible. Also, native support `BarcodeDetector` is inconsistent. For example, Chrome on MacOS does support `BarcodeDetector` but does not support `Blob` inputs, violating the specification. To have a more consistent support we simply always use the polyfill on all platforms. That likely comes as at a performance cost in raw scanning speed on supporting platforms but the download penalty is paid either way. See #354 #353
@Sec-ant Thanks a lot for the insights. I followed your suggestions and prepared a PR with updated packages and |
The BarcodeDetector polyfill fetches a WASM file at runtime from a CDN. That's a problem for offline applications or applications that run in a network with strict CSP policy. As a workaround the polyfill exports a function to configure the WASM file URL. So we re-export the function here. Also, by default the BarcodeDetector polyfill installs itself by setting `globalThis.BarcodeDetector` but only if there is no native support. This is likely not SSR compatible. Also, native support `BarcodeDetector` is inconsistent. For example, Chrome on MacOS does support `BarcodeDetector` but does not support `Blob` inputs, violating the specification. To have a more consistent support we simply always use the polyfill on all platforms. That likely comes as at a performance cost in raw scanning speed on supporting platforms but the download penalty is paid either way. See #354 #353
The BarcodeDetector polyfill fetches a WASM file at runtime from a CDN. That's a problem for offline applications or applications that run in a network with strict CSP policy. As a workaround the polyfill exports a function to configure the WASM file URL. So we re-export the function here. Also, by default the BarcodeDetector polyfill installs itself by setting `globalThis.BarcodeDetector` but only if there is no native support. This is likely not SSR compatible. Also, native support `BarcodeDetector` is inconsistent. For example, Chrome on MacOS does support `BarcodeDetector` but does not support `Blob` inputs, violating the specification. To have a more consistent support we simply always use the polyfill on all platforms. That likely comes as at a performance cost in raw scanning speed on supporting platforms but the download penalty is paid either way. See #354 #353
The BarcodeDetector polyfill fetches a WASM file at runtime from a CDN. That's a problem for offline applications or applications that run in a network with strict CSP policy. As a workaround the polyfill exports a function to configure the WASM file URL. So we re-export the function here. Also, by default the BarcodeDetector polyfill installs itself by setting `globalThis.BarcodeDetector` but only if there is no native support. This is likely not SSR compatible. Also, native support `BarcodeDetector` is inconsistent. For example, Chrome on MacOS does support `BarcodeDetector` but does not support `Blob` inputs, violating the specification. To have a more consistent support we simply always use the polyfill on all platforms. That likely comes as at a performance cost in raw scanning speed on supporting platforms but the download penalty is paid either way. See #354 #353
released in v5.2.0: https://github.com/gruhn/vue-qrcode-reader/releases/tag/v5.2.0 |
Just a little addendum how to copy the required //webpack.config.js
const CopyPlugin = require("copy-webpack-plugin")
webpackConfig.plugins.push(
new CopyPlugin({
patterns: [
{ from: "node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm", to: "zxing_reader.wasm" },
],
}),
) |
I think there's an underlying problem regarding the versions of
As I don't think this is an issue of But So, what I want to suggest here is that:
|
Pinning dependencies rocks |
Sounds good 👍 |
use exact version rather than semver range to prevent users from unexpectedly hosting a wrong `.wasm` file from `node_modules` see: #354 (comment)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [vue-qrcode-reader](https://vue-qrcode-reader.netlify.app) ([source](https://togithub.com/gruhn/vue-qrcode-reader)) | [`5.3.4` -> `5.3.5`](https://renovatebot.com/diffs/npm/vue-qrcode-reader/5.3.4/5.3.5) | [![age](https://developer.mend.io/api/mc/badges/age/npm/vue-qrcode-reader/5.3.5?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/vue-qrcode-reader/5.3.5?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/vue-qrcode-reader/5.3.4/5.3.5?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/vue-qrcode-reader/5.3.4/5.3.5?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>gruhn/vue-qrcode-reader (vue-qrcode-reader)</summary> ### [`v5.3.5`](https://togithub.com/gruhn/vue-qrcode-reader/releases/tag/v5.3.5) [Compare Source](https://togithub.com/gruhn/vue-qrcode-reader/compare/v5.3.4...v5.3.5) ##### Bug Fixes - **wasm:** pin `barcode-detector` ([be19c85](https://togithub.com/gruhn/vue-qrcode-reader/commit/be19c8553c8b59907adf154d430a45be8b631970)), closes [/github.com/gruhn/vue-qrcode-reader/issues/354#issuecomment-1722257982](https://togithub.com//github.com/gruhn/vue-qrcode-reader/issues/354/issues/issuecomment-1722257982) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/maevsi/maevsi). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44My4wIiwidXBkYXRlZEluVmVyIjoiMzYuODMuMCIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->
Since
[email protected]
the library uses@sec-ant/barcode-detector
. This in turn loads a@sec-ant/zxing-wasm
web assembly file via a HTTP request to a third-party server. This is problematic for servers with a strictCSP
policy preventing such requests.I see that
@sec-ant/barcode-detector
allows to set a custom URL to load the wasm file from, viasetZXingModuleOverrides
, see https://github.com/Sec-ant/barcode-detector#setzxingmoduleoverrides.Is there a way to use
vue-qrcode-reader
and set the custom URL for zxing?The text was updated successfully, but these errors were encountered: