Skip to content

Commit

Permalink
fix: Make js-toxcore-c compatible with ref-napi 2 and higher.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 2, 2024
1 parent 8d658a2 commit f46bdf4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
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
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

0 comments on commit f46bdf4

Please sign in to comment.