Skip to content

Commit f46bdf4

Browse files
committed
fix: Make js-toxcore-c compatible with ref-napi 2 and higher.
1 parent 8d658a2 commit f46bdf4

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

lib/tox.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ Tox.prototype.inspect = function () {
204204
Object.keys(this).forEach(function (k) {
205205
obj[k] = this[k];
206206
// Hacky fix for StringSlice assert error:
207-
// void node::Buffer::StringSlice(const v8::FunctionCallbackInfo<v8::Value>&) [with node::encoding encoding = (node::encoding)5u]: Assertion `obj_data != __null' failed.
207+
// void node::Buffer::StringSlice(const
208+
// v8::FunctionCallbackInfo<v8::Value>&) [with node::encoding encoding =
209+
// (node::encoding)5u]: Assertion `obj_data != __null' failed.
208210
if (k === "_options") {
209211
// linting is weird and wants us to specifically `.toString()` this
210212
obj[k.toString()] = "[ToxOptions]";
@@ -2346,7 +2348,8 @@ Tox.prototype.getSecretKeyHexSync = function () {
23462348
/**
23472349
* Check if this Tox instance has a handle associated with it.
23482350
* @private
2349-
* @param {Tox~errorCallback} callback - Callback to pass Error object to if no handle
2351+
* @param {Tox~errorCallback} callback - Callback to pass Error object to if no
2352+
* handle
23502353
* @return {Boolean} true if has handle (no error), false if no handle (error)
23512354
*/
23522355
Tox.prototype._checkHandle = function (callback) {
@@ -2516,20 +2519,22 @@ Tox.prototype._fixFileControl = function (control) {
25162519
};
25172520

25182521
/**
2519-
* Fix a send lossless packet value. Adds magic byte(160) to the first byte of data
2522+
* Fix a send lossless packet value. Adds magic byte(160) to the first byte of
2523+
* data
25202524
* @private
25212525
* @param {Buffer} data
25222526
* @return {Buffer} new data
25232527
*/
25242528
Tox.prototype._fixSendLosslessPacket = function (data) {
2525-
//160: magic byte
2529+
// 160: magic byte
25262530
return Buffer.concat([Buffer.from([160]), data]);
25272531
};
25282532

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

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

26262634
/**
2627-
* Store an ffi.Callback. This is to prevent an annoying ffi garbage collection bug.
2635+
* Store an ffi.Callback. This is to prevent an annoying ffi garbage collection
2636+
* bug.
26282637
* @private
26292638
* @param {Object} key - Key
26302639
* @param {ffi.Callback} callback - Callback
@@ -2652,7 +2661,8 @@ Tox.prototype._toFFICallback = function (ffiFunc, callback) {
26522661
/////////////////////
26532662

26542663
/**
2655-
* Used in: Tox#bootstrap(), Tox#bootstrapSync(), Tox#addTCPRelay(), Tox#addTCPRelaySync().
2664+
* Used in: Tox#bootstrap(), Tox#bootstrapSync(), Tox#addTCPRelay(),
2665+
* Tox#addTCPRelaySync().
26562666
* @private
26572667
*/
26582668
Tox.prototype._performBootstrap = function (opts) {
@@ -3274,8 +3284,8 @@ Tox.prototype._initFileRecvChunkCb = function () {
32743284
cb: FileRecvChunkCallback,
32753285
name: "FileRecvChunkCallback",
32763286
wrapper: function (handle, friend, file, position, data, size, userdata) {
3277-
// Apparently data can sometimes be a NULL pointer, set data to undefined if so
3278-
// This should only happen on final chunk?
3287+
// Apparently data can sometimes be a NULL pointer, set data to undefined
3288+
// if so This should only happen on final chunk?
32793289
if (ref.address(data) !== 0) {
32803290
data = Buffer.from(ref.reinterpret(data, size)); // Copy to another Buffer
32813291
} else {
@@ -3297,12 +3307,13 @@ Tox.prototype._initFriendLosslessPacketCb = function () {
32973307
cb: FriendLosslessPacketCallback,
32983308
name: "FriendLosslessPacketCallback",
32993309
wrapper: function (handle, friend, data, length, userdata) {
3300-
//if(ref.address(data) !== 0) {
3301-
// //first byte is magic byte(160) so ignore it
3302-
// data = Buffer.from(ref.reinterpret(data, (length - 1), 1)); // Copy to another Buffer
3303-
//} else {
3304-
// data = undefined;
3305-
//}
3310+
// if (ref.address(data) !== 0) {
3311+
// // first byte is magic byte(160) so ignore it
3312+
// // Copy to another Buffer
3313+
// data = Buffer.from(ref.reinterpret(data, (length - 1), 1));
3314+
// } else {
3315+
// data = undefined;
3316+
// }
33063317
if (ref.address(data) === 0) {
33073318
throw new Error("NULL data packet");
33083319
}

test/tox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("Tox", function () {
3535
toxNoUdp.start();
3636

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

4141
var toxDead = new Tox();

tools/local-dev.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
3+
set -eux
4+
5+
docker run --rm -v "$PWD:/work/js-toxcore-c" --tmpfs /work/js-toxcore-c/node_modules:exec -it toxchat/js-toxcore-c

0 commit comments

Comments
 (0)