From f70fc0d13ae4c66dd0ccc78730269067841c7f93 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 2 Jan 2024 11:27:00 +0000 Subject: [PATCH] fix: Make js-toxcore-c compatible with ref-napi 2 and higher. --- lib/tox.js | 41 ++++++++++++++++++++++++++--------------- test/tox.js | 2 +- tools/local-dev.sh | 5 +++++ 3 files changed, 32 insertions(+), 16 deletions(-) create mode 100755 tools/local-dev.sh diff --git a/lib/tox.js b/lib/tox.js index 9309d07..7440cb8 100644 --- a/lib/tox.js +++ b/lib/tox.js @@ -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&) [with node::encoding encoding = (node::encoding)5u]: Assertion `obj_data != __null' failed. + // void node::Buffer::StringSlice(const + // v8::FunctionCallbackInfo&) [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]"; @@ -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) { @@ -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 @@ -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 @@ -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 @@ -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) { @@ -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 { @@ -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"); } diff --git a/test/tox.js b/test/tox.js index 8cb47ca..0af6973 100644 --- a/test/tox.js +++ b/test/tox.js @@ -36,7 +36,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(); diff --git a/tools/local-dev.sh b/tools/local-dev.sh new file mode 100755 index 0000000..eca84e4 --- /dev/null +++ b/tools/local-dev.sh @@ -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