From 422d2d41331e6d0d747d7a526c1840326cd13366 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 6 Aug 2017 03:31:58 -0700 Subject: [PATCH] config: consistency changes. --- lib/node/config.js | 11 +++++++---- lib/utils/validator.js | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/node/config.js b/lib/node/config.js index bf016b92..29f7cce3 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -463,9 +463,12 @@ Config.prototype.bool = function bool(key, fallback) { * @returns {Buffer|null} */ -Config.prototype.buf = function buf(key, fallback) { +Config.prototype.buf = function buf(key, fallback, enc) { const value = this.get(key); + if (!enc) + enc = 'hex'; + if (fallback === undefined) fallback = null; @@ -478,9 +481,9 @@ Config.prototype.buf = function buf(key, fallback) { return value; } - const data = Buffer.from(value, 'hex'); + const data = Buffer.from(value, enc); - if (data.length !== value.length / 2) + if (data.length !== Buffer.byteLength(value, enc)) throw new Error(`${fmt(key)} must be a hex string.`); return data; @@ -559,7 +562,7 @@ Config.prototype.func = function func(key, fallback) { if (value === null) return fallback; - if (!value || typeof value !== 'function') + if (typeof value !== 'function') throw new Error(`${fmt(key)} must be a function.`); return value; diff --git a/lib/utils/validator.js b/lib/utils/validator.js index a9b5110a..ea89ec91 100644 --- a/lib/utils/validator.js +++ b/lib/utils/validator.js @@ -49,7 +49,7 @@ Validator.prototype.init = function init(data) { Validator.prototype.has = function has(key) { assert(typeof key === 'string' || typeof key === 'number', - 'Key must be a string.'); + 'Key must be a string or number.'); for (const map of this.data) { const value = map[key]; @@ -82,7 +82,7 @@ Validator.prototype.get = function get(key, fallback) { } assert(typeof key === 'string' || typeof key === 'number', - 'Key must be a string.'); + 'Key must be a string or number.'); for (const map of this.data) { if (!map || typeof map !== 'object')