Skip to content

Commit

Permalink
Make whenjs optional (crossbario#516)
Browse files Browse the repository at this point in the history
* Make whenjs optional

* root out whenjs usage in main library

* promisify didnt work, write own thing

* reuse helpers in xbr stuff
  • Loading branch information
om26er authored Jun 29, 2020
1 parent ec747ee commit df99d9c
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 125 deletions.
16 changes: 6 additions & 10 deletions packages/autobahn-xbr/lib/buyer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//
///////////////////////////////////////////////////////////////////////////////

let autobahn = require('autobahn');
var cbor = require('cbor');
var nacl = require('tweetnacl');
nacl.sealedbox = require('tweetnacl-sealedbox-js');
Expand All @@ -23,11 +24,6 @@ var BN = web3.utils.BN;
var util = require('./util.js');
var eip712 = require('./eip712.js');

// https://www.npmjs.com/package/uuid
// const uuid = require('uuid/v4');
// var u = uuid();
// console.log(u);


var SimpleBuyer = function (market_maker_adr, buyer_key, max_price) {
self = this;
Expand All @@ -41,7 +37,7 @@ var SimpleBuyer = function (market_maker_adr, buyer_key, max_price) {
self._keys = {};
self._market_maker_adr = market_maker_adr;
self._max_price = max_price;
self._deferred_factory = util.deferred_factory();
self._deferred_factory = autobahn.util.deferred_factory();

self._pkey_raw = eth_util.toBuffer(buyer_key);
self._acct = new eth_accounts().privateKeyToAccount(buyer_key);
Expand Down Expand Up @@ -86,7 +82,7 @@ SimpleBuyer.prototype.start = function(session, consumerID) {
}
);

return util.promise(d);
return autobahn.util.promise(d);
};

SimpleBuyer.prototype.stop = function () {
Expand All @@ -109,7 +105,7 @@ SimpleBuyer.prototype.balance = function () {
d.reject(error['error']);
}
);
return util.promise(d);
return autobahn.util.promise(d);
};

SimpleBuyer.prototype.openChannel = function (buyerAddr, amount) {
Expand All @@ -132,7 +128,7 @@ SimpleBuyer.prototype.openChannel = function (buyerAddr, amount) {
d.reject(error['error']);
}
);
return util.promise(d);
return autobahn.util.promise(d);
};

SimpleBuyer.prototype.closeChannel = function () {
Expand Down Expand Up @@ -265,7 +261,7 @@ SimpleBuyer.prototype.unwrap = async function (key_id, enc_ser, ciphertext) {
};
waitForPurchase()
}
return util.promise(d);
return autobahn.util.promise(d);
};

exports.SimpleBuyer = SimpleBuyer;
15 changes: 6 additions & 9 deletions packages/autobahn-xbr/lib/seller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
//
///////////////////////////////////////////////////////////////////////////////

//const autobahn = require('autobahn');

const when = require('when');

const autobahn = require('autobahn');
const eth_accounts = require("web3-eth-accounts");
const eth_util = require("ethereumjs-util");
const nacl = require('tweetnacl');
Expand All @@ -37,7 +34,7 @@ var Seller = function (market_maker_adr, seller_key) {
self._provider_id = eth_util.bufferToHex(eth_util.privateToPublic(seller_key));
self._session = null;
self._session_regs = [];
self._deferred_factory = util.deferred_factory();
self._deferred_factory = autobahn.util.deferred_factory();

self._pkey_raw = eth_util.toBuffer(seller_key);
self._acct = new eth_accounts().privateKeyToAccount(seller_key);
Expand Down Expand Up @@ -69,7 +66,7 @@ Seller.prototype.start = function (session) {
for (var topic in topics) {
pl1.push(session.subscribe('xbr.provider.' + self._provider_id + '.' + topic, topics[topic]));
}
var d1 = when.all(pl1).then(
var d1 = Promise.all(pl1).then(
function (subscriptions) {
self._session_subs = subscriptions;
},
Expand All @@ -88,7 +85,7 @@ Seller.prototype.start = function (session) {
for (var proc in endpoints) {
pl2.push(session.register('xbr.provider.' + self._provider_id + '.' + proc, endpoints[proc]));
}
var d2 = when.all(pl2).then(
var d2 = Promise.all(pl2).then(
function (registrations) {
self._session_regs = registrations;
for (var key in self.keys) {
Expand All @@ -101,7 +98,7 @@ Seller.prototype.start = function (session) {
}
);

when.all([d1, d2]).then(
Promise.all([d1, d2]).then(
function () {
d.resolve(self._balance);
},
Expand All @@ -122,7 +119,7 @@ Seller.prototype.start = function (session) {
}
);

return util.promise(d);
return autobahn.util.promise(d);
};


Expand Down
66 changes: 3 additions & 63 deletions packages/autobahn-xbr/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,16 @@
//
///////////////////////////////////////////////////////////////////////////////

var assert = require('assert');
var when = require('when');
var web3 = require('web3');
var BN = web3.utils.BN;
const assert = require('assert');
const web3 = require('web3');
const BN = web3.utils.BN;

// https://www.npmjs.com/package/uuid
const uuidv4 = require('uuid/v4');

// https://www.npmjs.com/package/uuid-parse
const uuid_parse = require('uuid-parse');

var log = require('./log.js');


var deferred_factory = function(options) {
var defer = null;

if (options && options.use_es6_promises) {

if ('Promise' in global) {
// ES6-based deferred factory
//
defer = function () {
var deferred = {};

deferred.promise = new Promise(function (resolve, reject) {
deferred.resolve = resolve;
deferred.reject = reject;
});

return deferred;
};
} else {

log.debug("Warning: ES6 promises requested, but not found! Falling back to whenjs.");

// whenjs-based deferred factory
//
defer = when.defer;
}

} else if (options && options.use_deferred) {

// use explicit deferred factory, e.g. jQuery.Deferred or Q.defer
//
defer = options.use_deferred;

} else {

// whenjs-based deferred factory
//
defer = when.defer;
}

return defer;
};


var promise = function(d) {
if (d.promise.then) {
// whenjs has the actual user promise in an attribute
return d.promise;
} else {
return d;
}
};


function pack_uint256 (value) {
assert(BN.isBN(value));
Expand Down Expand Up @@ -137,8 +80,5 @@ function uuid (value) {
}
}


exports.deferred_factory = deferred_factory;
exports.promise = promise;
exports.pack_uint256 = pack_uint256;
exports.uuid = uuid;
13 changes: 7 additions & 6 deletions packages/autobahn-xbr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
"node": ">= 4.2.6"
},
"dependencies": {
"when": ">= 3.7.7",
"ws": ">= 1.1.4",
"sha3": ">=2.0.1",
"uuid": ">=3.3.2",
"uuid-parse": ">=1.1.0",
"@truffle/contract": ">=4.1.5",
"autobahn": "^20.4.1",
"cbor": ">=3.0.0",
"crypto-js": ">=3.1.8",
"eth-sig-util": ">=2.4.4",
"ethereumjs-util": ">=6.1.0",
"msgpack5": ">= 3.6.0",
"sha3": ">=2.0.1",
"tweetnacl": ">= 0.14.3",
"tweetnacl-sealedbox-js": ">=1.1.0",
"uuid": ">=3.3.2",
"uuid-parse": ">=1.1.0",
"web3": ">=1.2.1",
"@truffle/contract": ">=4.1.5"
"when": ">= 3.7.7",
"ws": ">= 1.1.4"
},
"browser": {
"ws": false
Expand Down
13 changes: 11 additions & 2 deletions packages/autobahn/lib/autobahn.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ require('./polyfill.js');

var pjson = require('../package.json');

var when = require('when');
let when;
let HAS_WHEN;
try {
when = require('when');
HAS_WHEN = true;
} catch (e) {
HAS_WHEN = false;
}

var msgpack = require('msgpack5');
var cbor = require('cbor');
Expand Down Expand Up @@ -62,7 +69,9 @@ exports.auth_persona = persona.auth;
exports.auth_cra = cra;
exports.auth_cryptosign = cryptosign;

exports.when = when;
if (HAS_WHEN) {
exports.when = when;
}
exports.msgpack = msgpack;
exports.cbor = cbor;
exports.nacl = nacl;
Expand Down
15 changes: 4 additions & 11 deletions packages/autobahn/lib/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
// require('assert') would be nice .. but it does not
// work with Google Closure after Browserify

var when_fn = require("when/function");

var log = require('./log.js');
var util = require('./util.js');
const log = require('./log.js');
const util = require('./util.js');

// IE fallback (http://afuchs.tumblr.com/post/23550124774/date-now-in-ie8)
Date.now = Date.now || function() { return +new Date; };
Expand Down Expand Up @@ -792,12 +790,7 @@ var Session = function (socket, defer, onchallenge, on_user_error, on_internal_e
details.caller_authrole
);

// We use the following whenjs call wrapper, which automatically
// wraps a plain, non-promise value in a (immediately resolved) promise
//
// See: https://github.com/cujojs/when/blob/master/docs/api.md#fncall
//
when_fn.call(reg.endpoint, args, kwargs, cd).then(
util.as_promise(reg.endpoint, args, kwargs, cd).then(

function (res) {
// construct YIELD message
Expand Down Expand Up @@ -939,7 +932,7 @@ var Session = function (socket, defer, onchallenge, on_user_error, on_internal_e
var method = msg[1];
var extra = msg[2];

when_fn.call(self._onchallenge, self, method, extra).then(
util.as_promise(self._onchallenge, self, method, extra).then(
function (signature) {

if(typeof signature === "string"){
Expand Down
Loading

0 comments on commit df99d9c

Please sign in to comment.