-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transport): add nodeusb transport
- Loading branch information
Showing
10 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { AbstractTransport } from './abstract'; | ||
|
||
import { WRONG_ENVIRONMENT } from '../errors'; | ||
import { empty, emptyAbortable, emptySync } from '../utils/resultEmpty'; | ||
|
||
// this class loads in browser environment only in case of accidental use of NodeUsbTransport | ||
|
||
export class NodeUsbTransport extends AbstractTransport { | ||
public name = 'NodeUsbTransport' as const; | ||
|
||
constructor(params: ConstructorParameters<typeof AbstractTransport>[0]) { | ||
super(params); | ||
console.error(WRONG_ENVIRONMENT); | ||
} | ||
|
||
init = emptyAbortable; | ||
acquire = emptyAbortable; | ||
enumerate = emptyAbortable; | ||
call = emptyAbortable; | ||
receive = emptyAbortable; | ||
send = emptyAbortable; | ||
release = emptyAbortable; | ||
stop = empty; | ||
releaseDevice = empty; | ||
listen = emptySync; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { WebUSB } from 'usb'; | ||
import { AbstractTransport } from './abstract'; | ||
import { AbstractUsbTransport } from './abstractUsb'; | ||
import { SessionsClient } from '../sessions/client'; | ||
import { SessionsBackground } from '../sessions/background'; | ||
import { UsbInterface } from '../interfaces/usb'; | ||
|
||
// notes: | ||
// to make it work on Linux I needed to run `sudo chmod -R 777 /dev/bus/usb/` which is obviously not | ||
// the way to go. | ||
|
||
export class NodeUsbTransport extends AbstractUsbTransport { | ||
public name = 'NodeUsbTransport' as const; | ||
|
||
constructor({ messages, logger }: ConstructorParameters<typeof AbstractTransport>[0]) { | ||
const sessionsBackground = new SessionsBackground(); | ||
|
||
// in nodeusb there is no synchronization yet. this is a followup and needs to be decided | ||
// so far, sessionsClient has direct access to sessionBackground | ||
const sessionsClient = new SessionsClient({ | ||
requestFn: args => sessionsBackground.handleMessage(args), | ||
registerBackgroundCallbacks: () => {}, | ||
}); | ||
|
||
sessionsBackground.on('descriptors', descriptors => { | ||
sessionsClient.emit('descriptors', descriptors); | ||
}); | ||
|
||
super({ | ||
messages, | ||
usbInterface: new UsbInterface({ | ||
usbInterface: new WebUSB({ | ||
allowAllDevices: true, // return all devices, not only authorized | ||
}), | ||
logger, | ||
}), | ||
|
||
sessionsClient, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { WRONG_ENVIRONMENT } from '../errors'; | ||
import { error } from './result'; | ||
|
||
export const emptyAbortable = () => ({ | ||
promise: Promise.resolve(error({ error: WRONG_ENVIRONMENT })), | ||
abort: () => {}, | ||
}); | ||
|
||
export const empty = () => Promise.resolve(error({ error: WRONG_ENVIRONMENT })); | ||
|
||
export const emptySync = () => error({ error: WRONG_ENVIRONMENT }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters