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

fix: Make js-toxcore-c compatible with ref-napi 2 and higher. #175

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 26 additions & 15 deletions lib/tox.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ Tox.prototype.inspect = function () {
Object.keys(this).forEach(function (k) {
obj[k] = this[k];
// Hacky fix for StringSlice assert error:
// void node::Buffer::StringSlice(const v8::FunctionCallbackInfo<v8::Value>&) [with node::encoding encoding = (node::encoding)5u]: Assertion `obj_data != __null' failed.
// void node::Buffer::StringSlice(const
// v8::FunctionCallbackInfo<v8::Value>&) [with node::encoding encoding =
// (node::encoding)5u]: Assertion `obj_data != __null' failed.
if (k === "_options") {
// linting is weird and wants us to specifically `.toString()` this
obj[k.toString()] = "[ToxOptions]";
Expand Down Expand Up @@ -2346,7 +2348,8 @@ Tox.prototype.getSecretKeyHexSync = function () {
/**
* Check if this Tox instance has a handle associated with it.
* @private
* @param {Tox~errorCallback} callback - Callback to pass Error object to if no handle
* @param {Tox~errorCallback} callback - Callback to pass Error object to if no
* handle
* @return {Boolean} true if has handle (no error), false if no handle (error)
*/
Tox.prototype._checkHandle = function (callback) {
Expand Down Expand Up @@ -2516,20 +2519,22 @@ Tox.prototype._fixFileControl = function (control) {
};

/**
* Fix a send lossless packet value. Adds magic byte(160) to the first byte of data
* Fix a send lossless packet value. Adds magic byte(160) to the first byte of
* data
* @private
* @param {Buffer} data
* @return {Buffer} new data
*/
Tox.prototype._fixSendLosslessPacket = function (data) {
//160: magic byte
// 160: magic byte
return Buffer.concat([Buffer.from([160]), data]);
};

/**
* Fix a lossless/lossy packet buffer by prepending an id byte.
* @private
* @param {Number} id - Byte to prepend, according to tox.h it should be in the range
* @param {Number} id - Byte to prepend, according to tox.h it should be in the
* range
* [160, 191] if lossless and [200, 254] if lossy.
* @param {Buffer} data - Data buffer to prepend to
* @return {Buffer} new data
Expand Down Expand Up @@ -2613,6 +2618,9 @@ Tox.prototype._setProxyToToxOptions = function (opts, options) {
// Store in "this' so it isn"t GC-d?
this._proxyAddress = Buffer.from(proxy.address + "\0");
options.proxy_address = this._proxyAddress;
// TODO(iphydf): Weird hack here to make sure getOptions works.
// Remove this and see tests fail.
ref.reinterpretUntilZeros(this._proxyAddress, ref.types.char.size);
}

// Set port
Expand All @@ -2624,7 +2632,8 @@ Tox.prototype._setProxyToToxOptions = function (opts, options) {
};

/**
* Store an ffi.Callback. This is to prevent an annoying ffi garbage collection bug.
* Store an ffi.Callback. This is to prevent an annoying ffi garbage collection
* bug.
* @private
* @param {Object} key - Key
* @param {ffi.Callback} callback - Callback
Expand Down Expand Up @@ -2652,7 +2661,8 @@ Tox.prototype._toFFICallback = function (ffiFunc, callback) {
/////////////////////

/**
* Used in: Tox#bootstrap(), Tox#bootstrapSync(), Tox#addTCPRelay(), Tox#addTCPRelaySync().
* Used in: Tox#bootstrap(), Tox#bootstrapSync(), Tox#addTCPRelay(),
* Tox#addTCPRelaySync().
* @private
*/
Tox.prototype._performBootstrap = function (opts) {
Expand Down Expand Up @@ -3274,8 +3284,8 @@ Tox.prototype._initFileRecvChunkCb = function () {
cb: FileRecvChunkCallback,
name: "FileRecvChunkCallback",
wrapper: function (handle, friend, file, position, data, size, userdata) {
// Apparently data can sometimes be a NULL pointer, set data to undefined if so
// This should only happen on final chunk?
// Apparently data can sometimes be a NULL pointer, set data to undefined
// if so This should only happen on final chunk?
if (ref.address(data) !== 0) {
data = Buffer.from(ref.reinterpret(data, size)); // Copy to another Buffer
} else {
Expand All @@ -3297,12 +3307,13 @@ Tox.prototype._initFriendLosslessPacketCb = function () {
cb: FriendLosslessPacketCallback,
name: "FriendLosslessPacketCallback",
wrapper: function (handle, friend, data, length, userdata) {
//if(ref.address(data) !== 0) {
// //first byte is magic byte(160) so ignore it
// data = Buffer.from(ref.reinterpret(data, (length - 1), 1)); // Copy to another Buffer
//} else {
// data = undefined;
//}
// if (ref.address(data) !== 0) {
// // first byte is magic byte(160) so ignore it
// // Copy to another Buffer
// data = Buffer.from(ref.reinterpret(data, (length - 1), 1));
// } else {
// data = undefined;
// }
if (ref.address(data) === 0) {
throw new Error("NULL data packet");
}
Expand Down
67 changes: 44 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"ffi-napi": "^4.0.3",
"ref-array-napi": "^1.2.0",
"ref-napi": "^1.4.3",
"ref-napi": "^3.0.3",
"ref-struct-napi": "^1.1.0",
"underscore": "^1.12.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/tox.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Tox", function () {
toxNoUdp.start();

var customPort = 33510;
var toxCustomPort = new Tox({ startPort: customPort, endPort: customPort });
var toxCustomPort = new Tox({ startPort: customPort, endPort: customPort + 100 });
toxCustomPort.start();

var toxDead = new Tox();
Expand Down
5 changes: 5 additions & 0 deletions tools/local-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

set -eux

docker run --rm -v "$PWD:/work/js-toxcore-c" --tmpfs /work/js-toxcore-c/node_modules:exec -it toxchat/js-toxcore-c
Loading