Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

API Draft

Bruce Williams edited this page Aug 21, 2015 · 6 revisions

Module Structure

Should be built as a Node (CommonJS) module, so the following should work:

var usbDrive = require('usb-drive');

Functions returning promises

Get a list of attached devices

Support getAll() (or list(), or similar); return a promise.

usbDrive.getAll().then(function(devices) {
  /* ... do something with devices ... */
}).catch(function(err) { /* ... error handling */ });

devices should be an array of device objects.

Get a device by ID

Support get() (or similar); return a promise.

usbDrive.get(deviceId).then(function(device) {
  /* ... do something with device ... */
}).catch(function(err) { /* ... error handling ... */ });

device should be a device object.

Unmount a drive

Support unmount() (or similar); return a promise.

Note that failure (a promise rejection) should only occur if a mounted volume could not be unmounted. If it's already unmounted, the promise should resolve.

usbDrive.unmount(deviceId).then(function() {
  /* ... success ... */
}).catch(function(err) { /* ... failure ... */ });

deviceId should be the id provided in the device objects from get() or getAll().

Events

Watch for attach events

usbDrive.on('attach', function(device) { /* ... */ });

device should be a device object.

Note that this should be emitted before a separate mount event (if any), as described below. (So, when this is emitted, the device would likely be missing the mount property.)

Watch for detach events

usbDrive.on('detach', function(device) { /* ... */ });

device should be a device object.

Watch for mount events

When a USB device is mounted.

usbDrive.on('mount', function(device) { /* ... */ });

device should be a device object.

Watch for unmount events

When a USB device is unmounted.

usbDrive.on('unmount', function(device) { /* ... */ });

device should be a device object.