-
Notifications
You must be signed in to change notification settings - Fork 272
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
port to node addon api #399
Conversation
return obj; | ||
} | ||
} | ||
|
||
static NAN_METHOD(deviceConstructor) { | ||
Napi::Value Device::Constructor(const Napi::CallbackInfo& info) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved to the C++ constructor but was kept to minimize the diff
@@ -22,7 +23,8 @@ Transfer::~Transfer(){ | |||
} | |||
|
|||
// new Transfer(device, endpointAddr, type, timeout) | |||
NAN_METHOD(Transfer_constructor) { | |||
Napi::Value Transfer::Constructor(const Napi::CallbackInfo& info) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved to the C++ constructor but was kept to minimize the diff
inline void ref(){Ref();} | ||
inline void unref(){Unref();} | ||
inline void ref(){ refs_ = Ref();} | ||
inline void unref(){ refs_ = Unref();} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure about this... nan exposed the refs_ count directly
Pushed another commit which ports the pieces where libuv was used to use napi too, as libuv doesn't provide an ABI stability guarantee across major node versions. Since |
Thanks @geovie! This looks great and is much-needed. I've been hoping someone would do this for a while. What changes are required for CI / release scripts? I assume prebuild can be configured to build only one binary library per platform instead of per version? Unfortunately, I'm not really using or maintaining this library anymore and don't have time to review / test / release this right now. If you're interested in helping, I'd be happy to give you access. See #401. |
@kevinmehall For the CI the only thing that would change is that instead of specifying targets you would use the napi runtime And some small adjustments to the Happy to create a PR with those changes too, but first this PR should land, so if anyone could review/test this that would be great. |
@geovie I've checked out your branch and tried it locally. All of the device tests pass, but there is an error when closing the device at the end: Module
✓ should describe basic constants
✓ should handle abuse without crashing
setDebugLevel
✓ should throw when passed invalid args
✓ should succeed with good args
getDeviceList
✓ should return at least one device
findByIds
✓ should return an array with length > 0
Device
✓ should have sane properties
✓ should have a deviceDescriptor property
✓ should have a configDescriptor property
✓ should open
✓ gets string descriptors
control transfer
✓ should OUT transfer when the IN bit is not set
✓ should fail when bmRequestType doesn't match buffer / length
✓ should IN transfer when the IN bit is set
✓ should signal errors
Interface
✓ should have one interface
✓ should be the same as the interfaceNo 0
IN endpoint
✓ should be able to get the endpoint
✓ should be able to get the endpoint by address
✓ should have the IN direction flag
✓ should have a descriptor
✓ should fail to write
✓ should support read
✓ times out
✓ polls the device (1685ms)
OUT endpoint
✓ should be able to get the endpoint
✓ should be able to get the endpoint by address
✓ should have the OUT direction flag
✓ should support write
✓ times out
1) "after all" hook for "should be the same as the interfaceNo 0"
30 passing (2s)
1 failing
1) Device
Interface
"after all" hook for "should be the same as the interfaceNo 0":
TypeError: self.device.__releaseInterface is not a function
at next (usb.js:311:15)
at Interface.release (usb.js:295:3)
at Context.<anonymous> (test/usb.coffee:187:10)
at processImmediate (internal/timers.js:461:21)
Any idea what that could be?
Would be great to see the prebuild file updated accordingly. |
@thegecko Should be fixed now. Could you please test it again 🙏 ? |
I think it makes sense to include the changes in this PR. |
I can confirm all device tests now pass (checked with Node 10 and Node 12) 🎉 > usb@1.6.3 full-test /Users/thegecko/Projects/node-usb
> mocha --require coffeescript/register test/*
Module
✓ should describe basic constants
✓ should handle abuse without crashing
setDebugLevel
✓ should throw when passed invalid args
✓ should succeed with good args
getDeviceList
✓ should return at least one device
findByIds
✓ should return an array with length > 0
Device
✓ should have sane properties
✓ should have a deviceDescriptor property
✓ should have a configDescriptor property
✓ should open
✓ gets string descriptors
control transfer
✓ should OUT transfer when the IN bit is not set
✓ should fail when bmRequestType doesn't match buffer / length
✓ should IN transfer when the IN bit is set
✓ should signal errors
Interface
✓ should have one interface
✓ should be the same as the interfaceNo 0
IN endpoint
✓ should be able to get the endpoint
✓ should be able to get the endpoint by address
✓ should have the IN direction flag
✓ should have a descriptor
✓ should fail to write
✓ should support read
✓ times out
✓ polls the device (1674ms)
OUT endpoint
✓ should be able to get the endpoint
✓ should be able to get the endpoint by address
✓ should have the OUT direction flag
✓ should support write
✓ times out
30 passing (2s)
Oddly, it now takes the node runtime 3-4 seconds to exit after completing the tests compared to master (which instantly exits). I'm not too familiar with the cleanup processing in node, perhaps there are some listeners hanging or other garbage collection going on? |
@geovie Any ideas why the builds are failing? I tried your branch locally and got the same result. |
@kwngo It should work for all node version >= v10.16.0 as NAPI_VERSION 4 is required |
@geovie did you find any time to update the prebuild steps for this PR? |
@thegecko Pushed my changes now, it includes the napi prebuild The |
Thanks @geovie! I'm thinking of bumping the minor version and publishing a pre-release version for testing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of questions on the prebuild changes
Thanks @geovie The N-API version is now published on npm for testing purposes. To install it:
|
The NODE-API build is failing on Apple M1 hardware (darwin/arm64). Any idea how to fix it? thegecko@Robs-MacBook-Air node-usb % npm run prebuild
> usb@1.7.0-alpha.1 prebuild /Users/thegecko/Projects/node-usb
> prebuild --runtime napi -t 4 --force --strip --verbose
prebuild info begin Prebuild version 10.0.1
prebuild info build Preparing to prebuild usb@1.7.0-alpha.1 for napi 4 on darwin-arm64 using node-gyp
prebuild verb starting build process node-gyp
prebuild verb execute node-gyp with `node index.js rebuild --napi_build_version=4 --target_arch=arm64`
make: *** No rule to make target `Release/obj.target/libusb/libusb/libusb/core.o', needed by `Release/usb.a'. Stop.
prebuild ERR! build error
prebuild ERR! stack Error: `make` failed with exit code: 2
prebuild ERR! stack at ChildProcess.onExit (/Users/thegecko/Projects/node-usb/node_modules/node-gyp/lib/build.js:194:23)
prebuild ERR! stack at ChildProcess.emit (events.js:314:20)
prebuild ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
prebuild ERR! not ok
prebuild ERR! build Error: `make` failed with exit code: 2
prebuild ERR! build at ChildProcess.onExit (/Users/thegecko/Projects/node-usb/node_modules/node-gyp/lib/build.js:194:23)
prebuild ERR! build at ChildProcess.emit (events.js:314:20)
prebuild ERR! build at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! usb@1.7.0-alpha.1 prebuild: `prebuild --runtime napi -t 4 --force --strip --verbose`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the usb@1.7.0-alpha.1 prebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/thegecko/.npm/_logs/2021-04-04T10_28_33_739Z-debug.log
thegecko@Robs-MacBook-Air node-usb % |
Do you have the libusb submodule checked out? ( |
Man, this could be embarrassing.... |
Thanks @kevinmehall, I always forget that step. Have added a |
Testing Results Node.js
Note: Uses Node-API v4 supported from Node.js v10 Electron
Arch/OS
|
This PR ports node-usb to use node addon api instead of nan.
The migration was mostly done using the migration tool and some manual adjustments.
One upside of this, besides of using a more recent api, is that node addon api allows prebuilds to be build once and be used by various node (and electron) versions which support the necessary node addon api (version 3 is used in this PR which is supported by most node versions).