Gathering detailed insights and metrics for @sec-ant/barcode-detector
Gathering detailed insights and metrics for @sec-ant/barcode-detector
Gathering detailed insights and metrics for @sec-ant/barcode-detector
Gathering detailed insights and metrics for @sec-ant/barcode-detector
A Barcode Detection API polyfill that uses ZXing-C++ WebAssembly under the hood.
npm install @sec-ant/barcode-detector
Typescript
Module System
Node Version
NPM Version
58.5
Supply Chain
95.1
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
52,260
Last Day
17
Last Week
104
Last Month
373
Last Year
18,764
110 Stars
251 Commits
8 Forks
2 Watching
1 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
1.3.3
Package Id
@sec-ant/barcode-detector@1.3.3
Unpacked Size
261.32 kB
Size
84.27 kB
File Count
23
NPM Version
9.8.1
Node Version
20.6.1
Publised On
16 Sept 2023
Cumulative downloads
Total Downloads
Last day
-37%
17
Compared to previous day
Last week
-3.7%
104
Compared to previous week
Last month
-48.1%
373
Compared to previous month
Last year
-44%
18,764
Compared to previous year
A Barcode Detection API polyfill that uses ZXing webassembly under the hood.
This package is now released under the name
barcode-detector
on the npm registry starting from version 2.0.0. The original name@sec-ant/barcode-detector
will continue to be used for versions prior to 2.0.0, and will be retained solely for maintenance purposes. Eventually, it will be deprecated at an appropriate juncture.
To install, run the following command:
1npm i @sec-ant/barcode-detector
This package can be imported in three different ways:
1import { BarcodeDetector } from "@sec-ant/barcode-detector/pure";
To avoid potential namespace collisions, you can also rename the export:
1import { BarcodeDetector as BarcodeDetectorPolyfill } from "@sec-ant/barcode-detector/pure";
This approach is beneficial when you want to use a package to detect barcodes without polluting globalThis
, or when your runtime already provides an implementation of the Barcode Detection API, but you still want this package to function.
1import "@sec-ant/barcode-detector/side-effects";
This approach is beneficial when you need a drop-in polyfill. If there's already an implementation of Barcode Detection API on globalThis
, this won't take effect (type declarations will, as we cannot optionally declare types). In such cases, please use the pure module instead.
1import { BarcodeDetector } from "@sec-ant/barcode-detector";
This approach combines the pure module and side effects.
For modern browsers that support ES modules, this package can be imported via the <script type="module">
tags:
Include side effects:
1<!-- register --> 2<script 3 type="module" 4 src="https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/es/side-effects.min.js" 5></script> 6 7<!-- use --> 8<script type="module"> 9 const barcodeDetector = new BarcodeDetector(); 10</script>
Script scoped access:
1<script type="module"> 2 import { BarcodeDetector } from "https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/es/pure.min.js"; 3 const barcodeDetector = new BarcodeDetector(); 4</script>
With import maps:
1<!-- import map --> 2<script type="importmap"> 3 { 4 "imports": { 5 "@sec-ant/barcode-detector/pure": "https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/es/pure.min.js" 6 } 7 } 8</script> 9 10<!-- script scoped access --> 11<script type="module"> 12 import { BarcodeDetector } from "@sec-ant/barcode-detector/pure"; 13 const barcodeDetector = new BarcodeDetector(); 14</script>
Starting from v1.2, this package supports IIFE and CJS build outputs for use cases that require legacy compatibility.
For legacy browsers that lack support for module type <script>
tags, or for userscripts, IIFE is the preferred choice. Upon executing the IIFE script, a variable named BarcodeDetectionAPI
will be registered in the global.
1<!-- 2 IIFE pure.js registers: 3 window.BarcodeDetectionAPI.BarcodeDetector 4 window.BarcodeDetectionAPI.setZXingModuleOverrides 5 --> 6<script src="https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/iife/pure.min.js"></script> 7 8<!-- 9 IIFE side-effects.js registers: 10 window.BarcodeDetector 11 window.BarcodeDetectionAPI.setZXingModuleOverrides 12 --> 13<script src="https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/iife/side-effects.min.js"></script> 14 15<!-- 16 IIFE index.js registers: 17 window.BarcodeDetector 18 window.BarcodeDetectionAPI.BarcodeDetector 19 window.BarcodeDetectionAPI.setZXingModuleOverrides 20 --> 21<script src="https://cdn.jsdelivr.net/npm/@sec-ant/barcode-detector@1.3/dist/iife/index.min.js"></script>
This package can also be consumed as a commonjs package:
Vanilla Javascript:
1// src/index.js 2const { BarcodeDetector } = require("@sec-ant/barcode-detector/pure");
With Typescript:
1// src/index.ts 2import { BarcodeDetector } from "@sec-ant/barcode-detector/pure";
tsconfig.json
:
1{ 2 "compilerOptions": { 3 "module": "CommonJS", 4 "moduleResolution": "Node", 5 "skipLibCheck": true 6 }, 7 "include": ["src"] 8}
setZXingModuleOverrides
In addition to BarcodeDetector
, this package exports another function called setZXingModuleOverrides
.
This package employs Sec-ant/zxing-wasm to enable the core barcode reading functionality. As a result, a .wasm
binary file is fetched at runtime. The default fetch path for this binary file is:
https://cdn.jsdelivr.net/npm/@sec-ant/zxing-wasm@<version>/dist/reader/zxing_reader.wasm
The setZXingModuleOverrides
function allows you to govern where the .wasm
binary is served from, thereby enabling offline use of the package, use within a local network, or within a site having strict CSP rules.
For instance, should you want to inline this .wasm
file in your build output for offline usage, with the assistance of build tools, you could try:
1// src/index.ts 2import wasmFile from "../node_modules/@sec-ant/zxing-wasm/dist/reader/zxing_reader.wasm?url"; 3 4import { 5 setZXingModuleOverrides, 6 BarcodeDetector, 7} from "@sec-ant/barcode-detector/pure"; 8 9setZXingModuleOverrides({ 10 locateFile: (path, prefix) => { 11 if (path.endsWith(".wasm")) { 12 return wasmFile; 13 } 14 return prefix + path; 15 }, 16}); 17 18const barcodeDetector = new BarcodeDetector(); 19 20// detect barcodes 21// ...
Alternatively, the .wasm
file could be copied to your dist folder to be served from your local server, without incorporating it into the output as an extensive base64 data URL.
It's noteworthy that you'll always want to choose the correct version of the .wasm
file, so the APIs exported by it are exactly what the js code expects.
For more information on how to use this function, you can check the notes here and discussions here.
This function is exported from all the subpaths, including the side effects.
Please check the spec, MDN doc and Chromium implementation for more information.
The BarcodeDetector
provided by this package also extends class EventTarget
and provides 2 lifecycle events: load
and error
. You can use addEventListener
and removeEventListener
to register and remove callback hooks on these events.
load
EventThe load
event, which is a CustomEvent
, will be dispatched on the successful instanciation of ZXing wasm module. For advanced usage, the instanciated module is passed as the detail
parameter.
1import { BarcodeDetector } from "@sec-ant/barcode-detector/pure"; 2 3const barcodeDetector = new BarcodeDetector(); 4 5barcodeDetector.addEventListener("load", ({ detail }) => { 6 console.log(detail); // zxing wasm module 7});
error
EventThe error
event, which is a CustomEvent
, will be dispatched if the instanciation of ZXing wasm module is failed. An error is passed as the detail
parameter.
1import { BarcodeDetector } from "@sec-ant/barcode-detector/pure"; 2 3const barcodeDetector = new BarcodeDetector(); 4 5barcodeDetector.addEventListener("error", ({ detail }) => { 6 console.log(detail); // an error 7});
1import { BarcodeDetector } from "@sec-ant/barcode-detector/pure"; 2 3const barcodeDetector: BarcodeDetector = new BarcodeDetector({ 4 formats: ["qr_code"], 5}); 6 7const imageFile = await fetch( 8 "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Hello%20world!", 9).then((resp) => resp.blob()); 10 11barcodeDetector.detect(imageFile).then(console.log);
The source code in this repository, as well as the build output, except for the parts listed below, is licensed under the MIT license.
Test samples and resources are collected from zxing-cpp/zxing-cpp, which is licensed under the Apache-2.0 license, and web-platform-tests/wpt, which is licensed under the 3-Clause BSD license.
This package has an indirect dependency on Sec-ant/zxing-wasm-build, which is licensed under the Apache-2.0 license.
No vulnerabilities found.
No security vulnerabilities found.