Skip to content

fix: return null from configuration when the device is unconfigured - #125

Merged
thegecko merged 2 commits into
node-usb:mainfrom
ychsn:fix/unconfigured-configuration-null
Dec 3, 2026
Merged

thegecko merged 2 commits into
node-usb:mainfrom
ychsn:fix/unconfigured-configuration-null

Conversation

@ychsn

@ychsn ychsn commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

I found this on macOS while using an older USB device, a Sony RC-S380 NFC reader (054c:06c1). The impact is limited: macOS leaves devices with a vendor-specific device class (bDeviceClass 0xFF) unconfigured after they are plugged in, and reading device.configuration in that state throws instead of returning null.

Changes

  • UsbDevice.configuration now returns null instead of throwing configuration error: device is not configured when the device has no active configuration, as the WebUSB spec defines the attribute as nullable.
  • Any ActiveConfigurationError from nusb maps to null, since its configuration value is not public. This matches Chromium, which also returns null when no descriptor matches the active configuration value. Errors from opening the device are still thrown.
  • This restores the behaviour of node-usb v2, whose configuration getter returned null in the same cases.
  • The generated TypeScript type is now USBConfiguration | null, matching @types/w3c-web-usb.

Fixes

Checklist

Have you...

  • Tested the change acts as expected
  • Checked the change doesn't remove or change existing functionality
  • Considered how the feature impacts the product and tested around it (not just the happy paths)
  • Where possible, executed the hardware tests on multiple operating systems

No automated test is added: there is no WebUSB call that puts a device back into the unconfigured state, and the existing hardware tests use a demo device that is already configured. I tested manually on macOS with a Sony RC-S380 (054c:06c1, bDeviceClass 0xFF) right after plugging it in: configuration returns null, then selectConfiguration(1) and claimInterface(0) succeed. On the configured device it still returns the configuration object.

This change was written with the help of Claude Code (an AI coding assistant). I have reviewed the change and tested it on hardware as described above.

ychsn and others added 2 commits December 3, 2026 19:06
`UsbDevice.configuration` threw "configuration error: device is not
configured" when the device had no active configuration (e.g. a
vendor-specific class device on macOS right after it is plugged in, where
the OS does not select a configuration). Per the WebUSB spec the attribute
is nullable, so return null instead, letting callers check it and call
selectConfiguration() as they would in a browser.

Any ActiveConfigurationError from nusb now maps to null, which matches
Chromium: it also reports null when the active configuration value has no
matching descriptor. Errors from opening the device are still thrown.

The generated TypeScript type is updated to `USBConfiguration | null`,
matching @types/w3c-web-usb.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ychsn
ychsn marked this pull request as ready for review December 3, 2026 11:43
@thegecko

thegecko commented Dec 3, 2026

Copy link
Copy Markdown
Member

Thanks for opening this PR @ychsn !

All looks good to me

@thegecko
thegecko merged commit 4debde1 into node-usb:main Dec 3, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants