Skip to content

Commit

Permalink
feat: allow nodejs impls to use webusb/webble backends
Browse files Browse the repository at this point in the history
  • Loading branch information
septs committed Oct 24, 2024
1 parent bc4e060 commit 56b7ad7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/backends/WebBLEBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class WebBLEBackend implements EventListenerObject, Backend {
}
}

static async requestDevice() {
static async requestDevice(bluetooth = navigator.bluetooth) {
if (!(await this.getAvailability())) throw new Error('WebBLE is not available')
return navigator.bluetooth.requestDevice(this.getRequestOptions())
return bluetooth.requestDevice(this.getRequestOptions())
}

static async getAvailability() {
if (!('bluetooth' in navigator)) return false
if (!('getAvailability' in navigator.bluetooth)) return false
return navigator.bluetooth.getAvailability()
static async getAvailability(bluetooth = navigator.bluetooth) {
if (!bluetooth) return false
if (!('getAvailability' in bluetooth)) return false
return bluetooth.getAvailability()
}

static async open(device: BluetoothDevice, options?: WebBLEBackend.Options): Promise<WebBLEBackend> {
Expand Down
10 changes: 5 additions & 5 deletions src/backends/WebUSBBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const CHUNK_SIZE = 2 << 13 // 16 KiB
export class WebUSBBackend implements Backend {
private readonly abortController = new AbortController()

static async requestDevice() {
if (!(await this.getAvailability())) throw new Error('WebUSB is not available')
return navigator.usb.requestDevice({
static async requestDevice(usb = navigator.usb) {
if (usb === undefined) throw new Error('WebUSB is not available')
return usb.requestDevice({
filters: [
// ESTKme-RED (10/2024)
{ productId: 0x0165, vendorId: 0x0bda, classCode: 0xff },
],
})
}

static async getAvailability() {
return 'usb' in navigator
static async getAvailability(usb = navigator.usb) {
return usb !== undefined
}

static async open(device: USBDevice, options?: WebUSBBackend.Options): Promise<WebUSBBackend> {
Expand Down

1 comment on commit 56b7ad7

@septs
Copy link
Collaborator Author

@septs septs commented on 56b7ad7 Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { webusb } from "usb";
import { WebUSBBackend } from "@estkme-group/notccid";

WebUSBBackend.requestDevice(webusb)

see https://npm.im/usb

import { bluetooth } from "webbluetooth";
import { WebBLEBackend } from "@estkme-group/notccid";

WebBLEBackend.requestDevice(bluetooth)

see https://npm.im/webbluetooth

Please sign in to comment.