config: add querystring parsing.

This commit is contained in:
Christopher Jeffrey 2016-08-27 14:31:50 -07:00
parent 69fa80d353
commit a143c78369
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 100 additions and 43 deletions

View File

@ -105,7 +105,7 @@ more bitcoin magic).</small>
var newaddr = document.getElementById('newaddr'); var newaddr = document.getElementById('newaddr');
var items = []; var items = [];
var scrollback = 0; var scrollback = 0;
var logger, node; var logger, node, options;
body.onmouseup = function() { body.onmouseup = function() {
floating.style.display = 'none'; floating.style.display = 'none';
@ -257,32 +257,17 @@ more bitcoin magic).</small>
}); });
} }
var query = (function() { options = bcoin.config({
var query = {}; query: true,
var search = (window.location.search + '').substring(1); network: 'segnet4',
var s = search.split('&'); useWorkers: true,
var i, parts; coinCache: true,
logger: logger
for (i = 0; i < s.length; i++) {
parts = s[i].split('=');
if (parts[0])
query[parts[0]] = parts[1];
}
return query;
})();
bcoin.set({
network: query.network || 'segnet4',
useWorkers: true
}); });
node = new bcoin.fullnode({ bcoin.set(options);
prune: query.prune === 'true' || query.prune === '1',
logger: logger, node = new bcoin.fullnode(options);
db: query.db || 'leveldb',
coinCache: true
});
node.on('error', function(err) { node.on('error', function(err) {
; ;

View File

@ -194,6 +194,10 @@ Peer.prototype._init = function init() {
case 'ECONNRESET': case 'ECONNRESET':
self.ignore(); self.ignore();
break; break;
default:
if (!self.connected)
self.ignore();
break;
} }
}); });

View File

@ -44,10 +44,8 @@ config.alias = {
*/ */
config.parse = function parse(options) { config.parse = function parse(options) {
var env = {};
var arg = {};
var data = {}; var data = {};
var conf, prefix, filename, dirname; var arg, conf, prefix, filename, dirname;
if (!options) if (!options)
options = {}; options = {};
@ -55,8 +53,8 @@ config.parse = function parse(options) {
merge(data, options); merge(data, options);
if (options.env) { if (options.env) {
env = config.parseEnv(); arg = config.parseEnv();
merge(data, env); merge(data, arg);
} }
if (options.arg) { if (options.arg) {
@ -64,7 +62,12 @@ config.parse = function parse(options) {
merge(data, arg); merge(data, arg);
} }
if (data.config) { if (options.query) {
arg = config.parseQuery();
merge(data, arg);
}
if (data.config && !utils.isBrowser) {
prefix = config.getPrefix(data); prefix = config.getPrefix(data);
filename = data.config; filename = data.config;
@ -272,7 +275,7 @@ config.parseConfig = function parseConfig(text, prefix, dirname) {
if (eq === -1) { if (eq === -1) {
key = line.trim(); key = line.trim();
value = null; value = '';
} else { } else {
key = line.substring(0, eq).trim(); key = line.substring(0, eq).trim();
value = line.substring(eq + 1).trim(); value = line.substring(eq + 1).trim();
@ -330,15 +333,15 @@ config.parseArg = function parseArg(argv) {
key = key.replace(/\-/g, ''); key = key.replace(/\-/g, '');
alias = config.alias.arg[key]; if (key.length === 0)
if (alias) continue;
key = alias;
if (value.length === 0) if (value.length === 0)
continue; continue;
if (key.length === 0) alias = config.alias.arg[key];
continue; if (alias)
key = alias;
data[key] = value; data[key] = value;
@ -404,16 +407,71 @@ config.parseEnv = function parseEnv(env) {
key = key.substring(6); key = key.substring(6);
key = key.replace(/_/g, '').toLowerCase(); key = key.replace(/_/g, '').toLowerCase();
alias = config.alias.env[key];
if (alias)
key = alias;
if (key.length === 0) if (key.length === 0)
continue; continue;
if (value.length === 0) if (value.length === 0)
continue; continue;
alias = config.alias.env[key];
if (alias)
key = alias;
data[key] = value;
}
return config.parseData(data);
};
/**
* Parse querystring variables.
* @param {String} query
* @returns {Object}
*/
config.parseQuery = function parseQuery(query) {
var data = {};
var i, parts, index, pair, key, value, alias;
if (!utils.isBrowser)
return data;
if (query == null) {
query = utils.global.location.search;
if (typeof query !== 'string')
return data;
query = query.substring(1);
}
parts = query.split('&');
for (i = 0; i < parts.length; i++) {
pair = parts[i];
index = pair.indexOf('=');
if (index === -1) {
key = pair;
value = '';
} else {
key = pair.substring(0, index);
value = pair.substring(index + 1);
}
key = unescape(key);
key = key.replace(/\-/g, '').toLowerCase();
if (key.length === 0)
continue;
value = unescape(value);
if (value.length === 0)
continue;
alias = config.alias.env[key];
if (alias)
key = alias;
data[key] = value; data[key] = value;
} }
@ -646,6 +704,14 @@ function merge(a, b) {
return a; return a;
} }
function unescape(str) {
try {
str = decodeURIComponent(str).replace(/\+/g, ' ');
} finally {
return str.replace(/\0/g, '');
}
}
/* /*
* Expose * Expose
*/ */

View File

@ -160,8 +160,10 @@ function parsePairs(str) {
if (key.length === 0) if (key.length === 0)
continue; continue;
if (value.length > 0) value = unescape(value);
value = unescape(value);
if (value.length === 0)
continue;
data[key] = value; data[key] = value;
} }