Skip to content

Commit 35d76b3

Browse files
committed
Add deprecation warnings to request constructors
* The first argument to request constructor functions should be an object containing request properties * Improve Remote test coverage
1 parent 2833a7b commit 35d76b3

7 files changed

+2551
-591
lines changed

src/js/ripple/remote.js

+88-24
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,22 @@ var log = require('./log').internal.sub('remote');
4040
/**
4141
* Interface to manage connections to rippled servers
4242
*
43-
* @param {Object} Connection options.
43+
* @param {Object} Options
4444
*/
4545

4646
function Remote(opts) {
4747
EventEmitter.call(this);
4848

4949
var self = this;
50-
var opts = lodash.extend(this, Remote.DEFAULTS, opts);
50+
var opts = opts || { };
51+
52+
Object.keys(Remote.DEFAULTS).forEach(function(config) {
53+
if (opts.hasOwnProperty(config)) {
54+
this[config] = opts[config];
55+
} else {
56+
this[config] = Remote.DEFAULTS[config];
57+
}
58+
}, this);
5159

5260
this.state = 'offline'; // 'online', 'offline'
5361
this._server_fatal = false; // server exited
@@ -102,13 +110,13 @@ function Remote(opts) {
102110
}
103111
};
104112

105-
if (typeof opts.trusted !== 'boolean') {
113+
if (typeof this.trusted !== 'boolean') {
106114
throw new TypeError('trusted must be a boolean');
107115
}
108-
if (typeof opts.trace !== 'boolean') {
116+
if (typeof this.trace !== 'boolean') {
109117
throw new TypeError('trace must be a boolean');
110118
}
111-
if (typeof opts.allow_partial_history !== 'boolean') {
119+
if (typeof this.allow_partial_history !== 'boolean') {
112120
throw new TypeError('allow_partial_history must be a boolean');
113121
}
114122
if (typeof this.max_fee !== 'number') {
@@ -138,21 +146,21 @@ function Remote(opts) {
138146
if (typeof this.last_ledger_offset !== 'number') {
139147
throw new TypeError('last_ledger_offset must be a number');
140148
}
141-
if (!Array.isArray(opts.servers)) {
149+
if (!Array.isArray(this.servers)) {
142150
throw new TypeError('servers must be an array');
143151
}
144152

145153
this.setMaxListeners(this.max_listeners);
146154

147-
this.servers.forEach(function(server) {
148-
var connection = self.addServer(server);
149-
connection.setMaxListeners(self.max_listeners);
155+
this.servers.forEach(function(serverOptions) {
156+
var server = self.addServer(serverOptions);
157+
server.setMaxListeners(self.max_listeners);
150158
});
151159

152160
function listenersModified(action, event) {
153161
// Automatically subscribe and unsubscribe to orderbook
154162
// on the basis of existing event listeners
155-
if (~Remote.TRANSACTION_EVENTS.indexOf(event)) {
163+
if (lodash.contains(Remote.TRANSACTION_EVENTS, event)) {
156164
switch (action) {
157165
case 'add':
158166
if (++self._transaction_listeners === 1) {
@@ -441,22 +449,13 @@ Remote.prototype.reconnect = function() {
441449
* @api public
442450
*/
443451

444-
Remote.prototype.connect = function(online) {
452+
Remote.prototype.connect = function(callback) {
445453
if (!this._servers.length) {
446454
throw new Error('No servers available.');
447455
}
448456

449-
switch (typeof online) {
450-
case 'undefined':
451-
break;
452-
case 'function':
453-
this.once('connect', online);
454-
break;
455-
default:
456-
// Downwards compatibility
457-
if (!Boolean(online)) {
458-
return this.disconnect();
459-
}
457+
if (typeof callback === 'function') {
458+
this.once('connect', callback);
460459
}
461460

462461
this._should_connect = true;
@@ -482,13 +481,13 @@ Remote.prototype.disconnect = function(callback) {
482481

483482
var callback = (typeof callback === 'function') ? callback : function(){};
484483

484+
this._should_connect = false;
485+
485486
if (!this.isConnected()) {
486487
callback();
487488
return this;
488489
}
489490

490-
this._should_connect = false;
491-
492491
this.once('disconnect', callback);
493492

494493
this._servers.forEach(function(server) {
@@ -865,6 +864,9 @@ Remote.prototype.requestLedger = function(options, callback) {
865864
case 'accounts':
866865
request.message[o] = true;
867866
break;
867+
case 'ledger':
868+
request.selectLedger(options.ledger);
869+
break;
868870
case 'ledger_index':
869871
case 'ledger_hash':
870872
request.message[o] = options[o];
@@ -1113,6 +1115,14 @@ Remote.prototype.requestTransactionEntry = function(hash, ledgerHash, callback)
11131115
// utils.assert(this.trusted);
11141116
var request = new Request(this, 'transaction_entry');
11151117

1118+
if (typeof hash === 'object') {
1119+
ledgerHash = hash.ledger || hash.ledger_hash || hash.ledger_index;
1120+
hash = hash.hash || hash.tx || hash.transaction;
1121+
} else {
1122+
console.error('DEPRECATED: First argument to request constructor should be'
1123+
+ ' an object containing request properties');
1124+
}
1125+
11161126
request.txHash(hash);
11171127

11181128
switch (typeof ledgerHash) {
@@ -1152,6 +1162,8 @@ Remote.prototype.requestTx = function(hash, callback) {
11521162

11531163
if (typeof hash === 'string') {
11541164
options = { hash: hash };
1165+
console.error('DEPRECATED: First argument to request constructor should be'
1166+
+ ' an object containing request properties');
11551167
} else {
11561168
options = hash;
11571169
}
@@ -1206,6 +1218,9 @@ Remote.accountRequest = function(type, options, callback) {
12061218
peer = options.peer;
12071219
limit = options.limit;
12081220
marker = options.marker;
1221+
} else {
1222+
console.error('DEPRECATED: First argument to request constructor should be'
1223+
+ ' an object containing request properties');
12091224
}
12101225

12111226
// if a marker is given, we need a ledger
@@ -1530,6 +1545,13 @@ Remote.prototype.requestTxHistory = function(start, callback) {
15301545

15311546
var request = new Request(this, 'tx_history');
15321547

1548+
if (typeof start === 'object') {
1549+
start = start.start;
1550+
} else {
1551+
console.error('DEPRECATED: First argument to request constructor should be'
1552+
+ ' an object containing request properties');
1553+
}
1554+
15331555
request.message.start = start;
15341556
request.callback(callback);
15351557

@@ -1565,6 +1587,9 @@ Remote.prototype.requestBookOffers = function(gets, pays, taker, callback) {
15651587
gets = options.gets || options.taker_gets;
15661588
ledger = options.ledger;
15671589
limit = options.limit;
1590+
} else {
1591+
console.error('DEPRECATED: First argument to request constructor should be'
1592+
+ ' an object containing request properties');
15681593
}
15691594

15701595
if (typeof lastArg === 'function') {
@@ -1625,6 +1650,14 @@ Remote.prototype.requestWalletAccounts = function(seed, callback) {
16251650
utils.assert(this.trusted); // Don't send secrets.
16261651

16271652
var request = new Request(this, 'wallet_accounts');
1653+
1654+
if (typeof seed === 'object') {
1655+
seed = seed.seed;
1656+
} else {
1657+
console.error('DEPRECATED: First argument to request constructor should be'
1658+
+ ' an object containing request properties');
1659+
}
1660+
16281661
request.message.seed = seed;
16291662
request.callback(callback);
16301663

@@ -1644,6 +1677,15 @@ Remote.prototype.requestSign = function(secret, tx_json, callback) {
16441677
utils.assert(this.trusted); // Don't send secrets.
16451678

16461679
var request = new Request(this, 'sign');
1680+
1681+
if (typeof secret === 'object') {
1682+
tx_json = secret.tx_json;
1683+
secret = secret.secret;
1684+
} else {
1685+
console.error('DEPRECATED: First argument to request constructor should be'
1686+
+ ' an object containing request properties');
1687+
}
1688+
16471689
request.message.secret = secret;
16481690
request.message.tx_json = tx_json;
16491691
request.callback(callback);
@@ -1762,6 +1804,9 @@ Remote.accountRootRequest = function(type, responseFilter, account, ledger, call
17621804
callback = ledger;
17631805
ledger = account.ledger;
17641806
account = account.account;
1807+
} else {
1808+
console.error('DEPRECATED: First argument to request constructor should be'
1809+
+ ' an object containing request properties');
17651810
}
17661811

17671812
var lastArg = arguments[arguments.length - 1];
@@ -1911,6 +1956,9 @@ Remote.prototype.createPathFind = function(src_account, dst_account, dst_amount,
19111956
dst_amount = options.dst_amount;
19121957
dst_account = options.dst_account;
19131958
src_account = options.src_account;
1959+
} else {
1960+
console.error('DEPRECATED: First argument to request constructor should be'
1961+
+ ' an object containing request properties');
19141962
}
19151963

19161964
var pathFind = new PathFind(this,
@@ -1949,6 +1997,9 @@ Remote.prototype.createOrderBook = function(currency_gets, issuer_gets, currency
19491997
currency_pays = options.currency_pays;
19501998
issuer_gets = options.issuer_gets;
19511999
currency_gets = options.currency_gets;
2000+
} else {
2001+
console.error('DEPRECATED: First argument to request constructor should be'
2002+
+ ' an object containing request properties');
19522003
}
19532004

19542005
var gets = Remote.prepareTrade(currency_gets, issuer_gets);
@@ -2029,6 +2080,9 @@ Remote.prototype.accountSeqCache = function(account, ledger, callback) {
20292080
callback = ledger;
20302081
ledger = options.ledger;
20312082
account = options.account;
2083+
} else {
2084+
console.error('DEPRECATED: First argument to request constructor should be'
2085+
+ ' an object containing request properties');
20322086
}
20332087

20342088
if (!this.accounts.hasOwnProperty(account)) {
@@ -2135,6 +2189,9 @@ Remote.prototype.requestRippleBalance = function(account, issuer, currency, ledg
21352189
currency = options.currency;
21362190
issuer = options.issuer;
21372191
account = options.account;
2192+
} else {
2193+
console.error('DEPRECATED: First argument to request constructor should be'
2194+
+ ' an object containing request properties');
21382195
}
21392196

21402197
// YYY Could be cached per ledger.
@@ -2189,6 +2246,7 @@ Remote.prototype.requestRippleBalance = function(account, issuer, currency, ledg
21892246
return request;
21902247
};
21912248

2249+
Remote.prepareCurrency =
21922250
Remote.prepareCurrencies = function(currency) {
21932251
var newCurrency = { };
21942252

@@ -2220,6 +2278,9 @@ Remote.prototype.requestRipplePathFind = function(src_account, dst_account, dst_
22202278
dst_amount = options.dst_amount;
22212279
dst_account = options.dst_account;
22222280
src_account = options.src_account;
2281+
} else {
2282+
console.error('DEPRECATED: First argument to request constructor should be'
2283+
+ ' an object containing request properties');
22232284
}
22242285

22252286
var request = new Request(this, 'ripple_path_find');
@@ -2254,6 +2315,9 @@ Remote.prototype.requestPathFindCreate = function(src_account, dst_account, dst_
22542315
dst_amount = options.dst_amount;
22552316
dst_account = options.dst_account;
22562317
src_account = options.src_account;
2318+
} else {
2319+
console.error('DEPRECATED: First argument to request constructor should be'
2320+
+ ' an object containing request properties');
22572321
}
22582322

22592323
var request = new Request(this, 'path_find');

src/js/ripple/request.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,7 @@ Request.prototype.setServer = function(server) {
289289
break;
290290
};
291291

292-
if (selected instanceof Server) {
293-
this.server = selected;
294-
}
292+
this.server = selected;
295293

296294
return this;
297295
};

0 commit comments

Comments
 (0)