Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow specification of WebUSB instance to support non-browser environments #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ export function readBlobAsBuffer(blob: Blob): Promise<ArrayBuffer> {

function waitForFrame() {
return new Promise((resolve, _reject) => {
window.requestAnimationFrame(resolve);
if(typeof process !== 'undefined' && typeof process.nextTick === 'function') {
process.nextTick(resolve)
} else {
window.requestAnimationFrame(resolve);
}
});
}

Expand Down
15 changes: 9 additions & 6 deletions src/fastboot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type ReconnectCallback = () => void;
* device connected over USB.
*/
export class FastbootDevice {
usb: USB;
device: USBDevice | null;
epIn: number | null;
epOut: number | null;
Expand All @@ -85,8 +86,10 @@ export class FastbootDevice {
/**
* Create a new fastboot device instance. This doesn't actually connect to
* any USB devices; call {@link connect} to do so.
* @param {USB} [usb=navigator.usb] optionally specify a different WebUSB instance
*/
constructor() {
constructor(usb: USB = navigator.usb) {
this.usb = usb
this.device = null;
this.epIn = null;
this.epOut = null;
Expand Down Expand Up @@ -201,7 +204,7 @@ export class FastbootDevice {
async waitForConnect(onReconnect: ReconnectCallback = () => {}) {
// On Android, we need to request the user to reconnect the device manually
// because there is no support for automatic reconnection.
if (navigator.userAgent.includes("Android")) {
if (typeof navigator !== 'undefined' && navigator.userAgent?.includes("Android")) {
await this.waitForDisconnect();
onReconnect();
}
Expand All @@ -219,7 +222,7 @@ export class FastbootDevice {
* @throws {UsbError}
*/
async connect() {
let devices = await navigator.usb.getDevices();
let devices = await this.usb.getDevices();
common.logDebug("Found paired USB devices:", devices);
if (devices.length === 1) {
this.device = devices[0];
Expand All @@ -230,7 +233,7 @@ export class FastbootDevice {
common.logDebug(
"No or multiple paired devices are connected, requesting one"
);
this.device = await navigator.usb.requestDevice({
this.device = await this.usb.requestDevice({
filters: [
{
classCode: FASTBOOT_USB_CLASS,
Expand All @@ -243,7 +246,7 @@ export class FastbootDevice {
common.logDebug("Using USB device:", this.device);

if (!this._registeredUsbListeners) {
navigator.usb.addEventListener("disconnect", (event) => {
this.usb.addEventListener("disconnect", (event) => {
if (event.device === this.device) {
common.logDebug("USB device disconnected");
if (this._disconnectResolve !== null) {
Expand All @@ -253,7 +256,7 @@ export class FastbootDevice {
}
});

navigator.usb.addEventListener("connect", async (event) => {
this.usb.addEventListener("connect", async (event) => {
common.logDebug("USB device connected");
this.device = event.device;

Expand Down