npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-usb-scanner

v1.0.1

Published

A React hook for working with USB scanners via the Web Serial API. Easily connect, disconnect, and manage USB scanners in your app, with full support for parsing data (e.g., barcodes, ID cards) using a custom parser function.

Downloads

13

Readme

use-usb-scanner

A React hook for working with USB scanners via the Web Serial API. Easily connect, disconnect, and manage USB scanners in your app, with full support for parsing data (e.g., barcodes, ID cards) using a custom parser function.

Installation

You can install the hook via npm or yarn:

Using npm:

bash Copiar

npm install use-usb-scanner

Using yarn:

yarn add use-usb-scanner

Usage

Here is an example of how to use useUsbScanner to connect to a USB scanner, automatically reconnect on page reload, and parse data (like a Colombian ID card (Cédula)):

Example Usage:

import "./App.css";
import { useUsbScanner } from "use-usb-scanner";
import { parsearCedulaColombiana } from "./parseCedulaColombiana";

function App() {
    const { connectToPort, disconnectPort, status, parsedData, error, savedPortInfo } = useUsbScanner({
        serial: {
            baudRate: 9600, // You can adjust the baudRate as needed
        },
        filter: {
            usbVendorId: 1659, // Optionally filter by Vendor ID
            usbProductId: 8963, // Optionally filter by Product ID
        },
        parser: parsearCedulaColombiana, // Your custom parser function
    });

    return (
        <>
            <div>
                <pre>{JSON.stringify({ status, parsedData, error, savedPortInfo }, null, 2)}</pre>
                <button onClick={() => (status === "connected" ? disconnectPort() : connectToPort())}>
                    {status === "connected" ? "Disconnect" : "Connect"}
                </button>
            </div>
        </>
    );
}

export default App;

Explanation:

  • status: The current connection status of the USB scanner (e.g., disconnected, connecting, or connected).
  • parsedData: The data parsed from the scanner, based on your custom parser function (in this case, parsearCedulaColombiana). {rawData:string} if parser not provided.
  • error: Any errors encountered during connection or data parsing.
  • savedPortInfo: Information about the connected USB device, such as the port name.

Key Features:

  • Automatic Reconnection: When the page is reloaded, the hook will attempt to automatically reconnect to the previously connected device.
  • Device Filtering: Optionally filter devices by usbVendorId and usbProductId. These values can be obtained by connecting the device once, but are not strictly required.
  • Custom Parser: The hook allows you to define a custom parser function to handle the raw data sent by the USB scanner. This is useful for parsing barcodes, ID cards, PDF417, etc.

Custom Parser Function

You can define your own parser function to interpret the raw data received from the USB scanner. Here’s an example of how to parse a Colombian ID card (Cédula):

Example Parser (Colombian ID card):

export function parsearCedulaColombiana(rawData: string) {
    // Align the string if necessary
    const pubDskIndex = rawData.indexOf("PubDSK_1");
    if (pubDskIndex !== -1) {
        const leadingSpaces = 25 - pubDskIndex; // Calculate how many leading spaces are needed
        if (leadingSpaces > 0) {
            rawData = " ".repeat(leadingSpaces) + rawData; // Add leading spaces
        }
    } else {
        throw new Error("Invalid Colombian ID card");
    }

    // Clean up the data by removing invalid characters and null spaces
    const cleanedData = rawData
        .replace(/\u0000/g, " ") // Replace null characters with spaces
        .replace(/[^a-zA-Z0-9\sáéíóúÁÉÍÓÚñÑçÇ+-]/g, ""); // Remove non-alphanumeric characters

    // Extract relevant fields from the ID card
    const afisCode = cleanedData.substring(2, 10).trim();
    const fingerCard = cleanedData.substring(40, 48).trim();
    const documentNumber = cleanedData.substring(48, 58).trim();
    const lastName = cleanedData.substring(58, 80).trim();
    const secondLastName = cleanedData.substring(81, 104).trim();
    const firstName = cleanedData.substring(104, 127).trim();
    const middleName = cleanedData.substring(127, 150).trim();
    const gender = cleanedData.substring(151, 152).trim();

    // Extract and format birthdate
    const birthdayYear = cleanedData.substring(152, 156).trim();
    const birthdayMonth = cleanedData.substring(156, 158).trim();
    const birthdayDay = cleanedData.substring(158, 160).trim();
    const birthday = `${birthdayYear}/${birthdayMonth}/${birthdayDay}`;

    // Extract municipality code, department code, and blood type
    const municipalityCode = cleanedData.substring(160, 162).trim();
    const departmentCode = cleanedData.substring(162, 165).trim();
    const bloodType = cleanedData.substring(166, 168).trim();

    // Return the parsed data as an object
    return {
        afisCode,
        fingerCard,
        documentNumber: parseInt(documentNumber) + "",
        lastName,
        secondLastName,
        firstName,
        middleName,
        gender,
        birthday,
        municipalityCode,
        departmentCode,
        bloodType,
    };
}

This parser function is just an example; you can write similar functions to handle different types of data, such as barcodes or PDF417.

Web Serial API

This hook leverages the Web Serial API, which is supported in modern browsers like Chrome and Edge. The Web Serial API allows web applications to communicate with serial devices like USB scanners directly from the browser, without requiring additional plugins or software.

How It Works:

The hook requests access to the USB device using the Web Serial API. After successful connection, it reads raw data from the device and passes it through a custom parser function. The parsed data is returned to your app for display or further processing.

Notes

The filter options (usbVendorId and usbProductId) are optional but can help ensure automatic connection to the correct device. The hook tries to automatically reconnect to the scanner when the page is reloaded, as long as the browser supports the Web Serial API and LocalStorage API