From 73998cbe1930cb90ce636fe8d1de31ec2a421721 Mon Sep 17 00:00:00 2001 From: Yuichi Sano Date: Thu, 3 Dec 2026 19:06:07 +0900 Subject: [PATCH 1/2] fix: return null from configuration when the device is unconfigured `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 --- src/webusb_device.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/webusb_device.rs b/src/webusb_device.rs index dd70374..94a158d 100644 --- a/src/webusb_device.rs +++ b/src/webusb_device.rs @@ -779,18 +779,21 @@ impl UsbDevice { self.device_guard().is_some() } - #[napi(getter, ts_return_type = "USBConfiguration")] + #[napi(getter, ts_return_type = "USBConfiguration | null")] pub fn configuration(&self) -> Result> { let device = match self.device_guard().as_ref() { Some(device) => device.clone(), None => self._open()?, }; - let config = device - .active_configuration() - .map_err(|e| napi::Error::from_reason(format!("configuration error: {e}")))?; - - Ok(Some(UsbConfiguration::new(&self, &device, config)?)) + // Per the WebUSB spec, `configuration` is null when the device has no active + // configuration (e.g. it is unconfigured, bConfigurationValue 0) instead of throwing. + // This also matches Chromium, which returns null when the active configuration value + // has no matching descriptor. + match device.active_configuration() { + Ok(config) => Ok(Some(UsbConfiguration::new(&self, &device, config)?)), + Err(_) => Ok(None), + } } #[napi(getter, ts_return_type = "Array")] From 2cea95909801dae3b8348f2f64ccc38a2d22ee1d Mon Sep 17 00:00:00 2001 From: Yuichi Sano Date: Thu, 3 Dec 2026 20:32:33 +0900 Subject: [PATCH 2/2] chore: shorten configuration comment Co-Authored-By: Claude Opus 5.5 --- src/webusb_device.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/webusb_device.rs b/src/webusb_device.rs index 94a158d..756d647 100644 --- a/src/webusb_device.rs +++ b/src/webusb_device.rs @@ -786,10 +786,7 @@ impl UsbDevice { None => self._open()?, }; - // Per the WebUSB spec, `configuration` is null when the device has no active - // configuration (e.g. it is unconfigured, bConfigurationValue 0) instead of throwing. - // This also matches Chromium, which returns null when the active configuration value - // has no matching descriptor. + // Null when the device is unconfigured, as per WebUSB spec match device.active_configuration() { Ok(config) => Ok(Some(UsbConfiguration::new(&self, &device, config)?)), Err(_) => Ok(None),