From 1bc87ba26227831eb7f312e59eb15f9ed47497e1 Mon Sep 17 00:00:00 2001 From: Johan Nyman Date: Mon, 4 Dec 2023 08:42:52 +0100 Subject: [PATCH] fix: remove support for HID.HID and HID.Async devices in setupXKeysPanel. This is because previously we used a hack where we used .devicePath for these. This property (as far as I can tell) doesn't exist anymore, so we should not support it. As a backwards compatibility, a blind implementation for supporting { devicePath: string } was added. --- packages/node/src/lib.ts | 11 +---------- packages/node/src/methods.ts | 37 ++++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/node/src/lib.ts b/packages/node/src/lib.ts index 314ce3a..897079c 100644 --- a/packages/node/src/lib.ts +++ b/packages/node/src/lib.ts @@ -3,7 +3,7 @@ import type * as HID from 'node-hid' * This file contains internal convenience functions */ -export function isHID_Device(device: HID.Device | HID.HIDAsync | string): device is HID.Device { +export function isHID_Device(device: HID.Device | HID.HID | HID.HIDAsync | string): device is HID.Device { return ( typeof device === 'object' && (device as HID.Device).vendorId !== undefined && @@ -11,12 +11,3 @@ export function isHID_Device(device: HID.Device | HID.HIDAsync | string): device (device as HID.Device).interface !== undefined ) } -type HID_HID = HID.HIDAsync & { devicePath: string } -export function isHID_HID(device: HID.Device | HID.HIDAsync | string): device is HID_HID { - return ( - typeof device === 'object' && - (device as HID_HID).write !== undefined && - (device as HID_HID).getFeatureReport !== undefined && - (device as HID_HID).devicePath !== undefined // yes, HID.HID exposes this, we're using that - ) -} diff --git a/packages/node/src/methods.ts b/packages/node/src/methods.ts index d2724ca..14bc103 100644 --- a/packages/node/src/methods.ts +++ b/packages/node/src/methods.ts @@ -3,16 +3,21 @@ import { PRODUCTS } from '@xkeys-lib/core' import * as HID from 'node-hid' import { NodeHIDDevice } from './node-hid-wrapper' -import { isHID_Device, isHID_HID } from './lib' +import { isHID_Device } from './lib' import { HID_Device } from './api' -/** Sets up a connection to a HID device (the X-keys panel) */ +/** + * Sets up a connection to a HID device (the X-keys panel) + * + * If called without arguments, it will select any connected X-keys panel. + */ export function setupXkeysPanel(): Promise export function setupXkeysPanel(HIDDevice: HID.Device): Promise -export function setupXkeysPanel(HIDDevice: HID.HIDAsync): Promise export function setupXkeysPanel(devicePath: string): Promise -export async function setupXkeysPanel(devicePathOrHIDDevice?: HID.Device | HID.HIDAsync | string): Promise { +export async function setupXkeysPanel( + devicePathOrHIDDevice?: HID.Device | HID.HID | HID.HIDAsync | string +): Promise { let devicePath: string let device: HID.HIDAsync | undefined let deviceInfo: @@ -52,18 +57,30 @@ export async function setupXkeysPanel(devicePathOrHIDDevice?: HID.Device | HID.H productId: devicePathOrHIDDevice.productId, interface: devicePathOrHIDDevice.interface, } - } else if (isHID_HID(devicePathOrHIDDevice)) { - // is HID.HID + } else if ( + typeof devicePathOrHIDDevice === 'object' && + typeof (devicePathOrHIDDevice as any).devicePath === 'string' + ) { + // (backwards compatibility): has { devicePath: string } - device = devicePathOrHIDDevice - devicePath = devicePathOrHIDDevice.devicePath - // deviceInfo is set later + devicePath = (devicePathOrHIDDevice as any).devicePath + device = await HID.HIDAsync.open(devicePath) } else if (typeof devicePathOrHIDDevice === 'string') { // is string (path) devicePath = devicePathOrHIDDevice device = await HID.HIDAsync.open(devicePath) // deviceInfo is set later + } else if (devicePathOrHIDDevice instanceof HID.HID) { + // Can't use this, since devicePath is missing + throw new Error( + 'HID.HID not supported as argument to setupXkeysPanel, use HID.devices() to find the device and provide that instead.' + ) + } else if (devicePathOrHIDDevice instanceof HID.HIDAsync) { + // Can't use this, since devicePath is missing + throw new Error( + 'HID.HIDAsync not supported as argument to setupXkeysPanel, use HID.devicesAsync() to find the device and provide that instead.' + ) } else { throw new Error('setupXkeysPanel: invalid arguments') } @@ -71,7 +88,7 @@ export async function setupXkeysPanel(devicePathOrHIDDevice?: HID.Device | HID.H if (!deviceInfo) { // @ts-expect-error getDeviceInfo missing in typings const nodeHidInfo: HID.Device = await device.getDeviceInfo() - // Look through HID.devices(), bevause HID.Device contains the productId + // Look through HID.devices(), because HID.Device contains the productId deviceInfo = { product: nodeHidInfo.product, productId: nodeHidInfo.productId,