thegecko / webusb

Node.js implementation of the WebUSB Specification
https://thegecko.github.io/webusb/
MIT License
183 stars 27 forks source link

Electron crash on start #66

Open gavinbenda opened 4 years ago

gavinbenda commented 4 years ago

Can't find much on this around the net, but including webusb within an electron app (just using the blank/quick-start guide to keep it simple), and installing webusb, then running the webusb example code from the docs, causes an immediate crash on attempting to start the app.

Unfortunately there doesn't seem to be much in the way of errors.

Node.js 12.13.0, Chromium 80.0.3987.141, and Electron 8.1.1 on OSX

thegecko commented 4 years ago

I've not tried this in electron, do you see an error when starting electron from the command line?

gavinbenda commented 4 years ago

Unfortunately all of the debugging disconnects... It's the same issue as this comment I believe which is the only other mention of it on the web! https://github.com/electron/electron/issues/14545#issuecomment-569835883

thegecko commented 4 years ago

I assume you are using it in the backend process? The underlying usb package wont be available in the browser process.

brandonros commented 4 years ago

you can require('usb') from the renderer, you just need init in the non-renderer process to look a little bit like this

const electron = require('electron')

const createWindow = () => {
  const mainWindow = new electron.BrowserWindow({
    width: 1024,
    height: 768,
    title: 'OBD Flasher',
    webPreferences: {
      nodeIntegration: true,
      backgroundThrottling: false
    }
  })
  mainWindow.loadURL(`file://${__dirname}/index.html`)
}

const run = async () => {
  // power saver
  electron.powerSaveBlocker.start('prevent-display-sleep')
  // native modules
  electron.app.allowRendererProcessReuse = false
  // no backgrounding
  electron.app.commandLine.appendSwitch('disable-renderer-backgrounding')
  // init
  await electron.app.whenReady()
  createWindow()
  electron.app.on('activate', () => {
    if (electron.BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
  electron.app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      electron.app.quit()
    }
  })
}

run()
brandonros commented 4 years ago

then from my renderer.js i call require('usb') and it works fine.

thegecko and i just whipped up a new release that fixes a segfault on windows during device opening, i bet you it will fix this electron issue too

see https://github.com/thegecko/webusb/issues/68