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 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-hid-async

v2.0.1

Published

Async wrapper for node-hid

Downloads

16

Readme

Asynchronous node-hid wrapper

Motivation

Many of node-hid's calls are synchronous, which can be a problem for e.g. Electron applications. On Windows, devices() is the worst offender and can block for several seconds, but calls on devices themselves can also hang up the app. This is not node-hid's fault but rather the OS's. To work around the problem, node-hid-async creates independent processes to run these calls.

API

The API is a subset of node-hid's, using Promises and RxJS Observables as the asynchronous model. Most notably, read, readSync, and readTimeout are absent, since the behavior of read and readTimeout can be obtained using Observable operators.

NodeHidAsync

Main manager/service class; usually there should be one instance per application. It immediately spawns a process for running devices(), and will spawn another process for every device you connect to. All these processes' life cycles are managed for you. It connects a handler to the exit event of the process you start it from, which when it fires will shut down all the child processes it started. You can also manually trigger this by calling destroy() (see below).

Constructor

NodeHidAsync()

  • Creates an instance of the manager class.

Methods

nodeHidAsync.devices()

  • Returns a Promise that resolves to an array of Devices (i.e. the same array that would be returned from a devices() call in node-hid).

nodeHidAsync.open(path)

  • Returns a Promise that resolves to a NodeHidAsyncDevice connected to the device at the specified platform-specific path.

nodeHidAsync.open(vid, pid)

  • Returns a Promise that resolves to a NodeHidAsyncDevice connected to the first device with the specified vendor and product ID.

nodeHidAsync.destroy()

  • Immediately shuts down all processes associated with this instance of NodeHidAsync and releases resources.
  • Subsequently calling any method on the instance results in undefined behavior.

NodeHidAsyncDevice

Represents a HID that has been opened for communication. Created by calling open() on the NodeHidAsync instance.

Methods

device.dataObs()

  • Returns an Observable that emits received data packets. The Observable terminates when the device is closed.
  • The event payload is a Node Buffer. Note that on Windows the actual data will be prepended with a HID report number, as with unwrapped node-hid.

device.errorObs()

  • Returns an Observable that emits errors. The Observable terminates when the device is closed.

device.write(data)

  • Returns a Promise that resolves with the number of bytes actually written to the device.
  • data is an array of numbers or a Node Buffer. Note that on Windows this must be prepended with a HID report number (generally zero), as with unwrapped node-hid.

device.close()

  • Returns a Promise that resolves when the device has been closed. Once the Promise has resolved, the device's worker process has been terminated.
  • Subsequently calling any method on the device results in undefined behavior.

device.pause()

  • Same as node-hid pause(); appears this causes all packets to be dropped until a subsequent call to resume().
  • Not tested.

device.resume()

  • Same as node-hid resume(): restarts packet reception.
  • Note that, unlike node-hid, calling dataObs() does not automatically call this function.
  • Not tested.

device.sendFeatureReport(data)

  • Returns a Promise that resolves with the number of bytes actually written to the device.
  • As with node-hid, the first byte must be a report ID.

device.getFeatureReport(id, length)

  • Returns a Promise that resolves with a Buffer containing the data read.

Notes

Any method that would be synchronous in node-hid necessarily blocks the associated I/O worker process; this means, for example, that a call to devices() will not begin executing until the promise returned from the previous call has resolved. The same applies to write(), close(), sendFeatureReport(), etc..

Support

Please use the github issues page for any questions or issues. Any feedback is welcome, especially regarding architecture and/or API.