@bawdo/blinkstick
v0.1.4
Published
Drive BlinkStick Nano and Square USB LEDs from the browser (WebHID) and Node (node-hid).
Maintainers
Readme
@bawdo/blinkstick
Drive BlinkStick Nano and Square USB LEDs from the browser (WebHID) and from Node (node-hid).
This package is unofficial. It is not made by, or affiliated with, Agile Innovative, the makers of BlinkStick. See CAPABILITIES.md for what it supports.
Install
# Node (node-hid is an optional peer dependency)
npm i @bawdo/blinkstick node-hid
# Browser (Chrome or Edge, no node-hid)
npm i @bawdo/blinkstickNode 22 or later. Import from @bawdo/blinkstick/node or @bawdo/blinkstick/webhid.
Quick start: Node
import { open } from "@bawdo/blinkstick/node";
const stick = await open();
try {
await stick.setAll({ r: 255, g: 0, b: 0 });
await stick.pulse({ r: 0, g: 255, b: 0 }, { duration: 1000, iterations: 2 });
} finally {
await stick.close();
}await using stick = await open(); also works on Node 24+ or in TypeScript.
Quick start: browser
Pair from a click, then open. Pairing persists, so later visits can skip pair().
import { open, pair, parseColor } from "@bawdo/blinkstick/webhid";
button.onclick = async () => {
await pair();
const stick = await open();
await stick.blink(parseColor("orange"), { iterations: 3 });
await stick.close();
};Colours are { r, g, b }. parseColor takes hex, r,g,b or a CSS name.
More than one stick
import { list, open } from "@bawdo/blinkstick/node";
for (const info of await list()) console.log(info.serial, info.model);
const desk = await open({ serial: "BS072777-3.0" });Give a stick a name once with setName("desk"), then open({ name: "desk" }). In the browser
there are no serials, so names are how you tell sticks apart. list({ names: true }) adds each
name, and marks sticks as busy when something else holds them or their name cannot be read.
Good to know
setInfoBlock,setNameandsetModewrite the stick's EEPROM, which wears out. Call them rarely. Nothing else writes it.- After a mode write the stick refuses transfers while it stores the mode, so
setModewaits for it to answer again (typically 12 to 15 ms, at most 250 ms) before it resolves. - macOS opens sticks exclusively. If another app (for example Chrome with the Blinky extension)
holds a stick,
openfails with "exclusive access". - npm 11 may warn that node-hid's install script was skipped. That is harmless: node-hid loads its shipped prebuild.
- The types reference
esnext.disposable(forawait using), which adds the disposable globals to your type checking. - In the browser
manufactureris always "", because WebHID exposes no manufacturer string. Detection uses the product string and descriptor, so passmodel: "nano"or"square"toopenif a stick comes up unknown. - A Node stick with no serial cannot be reconnected.
- On macOS a replugged stick occasionally fails to re-enumerate at the OS level. Replug it again.
- In an MV3 service worker, open the stick, use it, and close it within the same event. Do not hold it open across events. Pair from a page, not the worker.
- If a stick is unplugged, the next call reopens it and repaints the last colours. If it is still
missing you get
BlinkStickErrorwith codeDISCONNECTED. - Set
stick.brightnessLimit(0 to 255) to cap brightness. A Square at full white draws about 500 mA.
Linux
Sticks are owned by root by default, so opening one needs a udev rule. Install the one shipped in the package:
sudo cp node_modules/@bawdo/blinkstick/udev/60-blinkstick.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules
sudo udevadm triggerThen unplug and replug the stick (or reboot). This grants access to whoever is logged in at the
desktop (via systemd-logind's uaccess), so no group membership is needed. It covers the browser
too.
Development
npm test # unit tests, no hardware
npm run lint
npm run typecheck
npm run build
npm run test:hardware # needs real sticks; never writes EEPROM
npm run test:browser # local page for manual WebHID checksAutomated tests must never write EEPROM on a real stick.
Verifying a release
Every release from 0.1.1 has npm provenance, which npm audit signatures checks. Releases after
0.1.3 also carry GitHub build provenance. To check that a tarball came from this repository's
release workflow, with the GitHub CLI:
npm pack @bawdo/[email protected]
gh attestation verify bawdo-blinkstick-0.1.4.tgz --repo bawdo/js-blinkstick \
--signer-workflow bawdo/js-blinkstick/.github/workflows/release.ymlSwap in the version you are checking. SECURITY.md has the details.
Versioning
Releases follow semantic versioning. Until 1.0.0 the API may change between minor releases. See CHANGELOG.md.
Licence
MIT. See LICENSE.
