lint: enforce function names.

This commit is contained in:
Christopher Jeffrey 2017-08-06 21:36:48 -07:00
parent 62152a1005
commit 3bc47f5a3c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
28 changed files with 169 additions and 174 deletions

View File

@ -24,7 +24,7 @@
"eqeqeq": ["error", "always", {
"null": "ignore"
}],
"func-name-matching": "off",
"func-name-matching": "error",
"indent": ["error", 2, {
"SwitchCase": 1,
"CallExpression": {

View File

@ -63,10 +63,10 @@ AEAD.prototype.init = function init(key, iv) {
* @param {Buffer} aad
*/
AEAD.prototype.aad = function _aad(aad) {
AEAD.prototype.aad = function aad(data) {
assert(this.cipherLen === 0, 'Cannot update aad.');
this.poly1305.update(aad);
this.aadLen += aad.length;
this.poly1305.update(data);
this.aadLen += data.length;
};
/**

View File

@ -22,8 +22,8 @@ const native = require('../native').binding;
*/
exports.encipher = function encipher(data, key, iv) {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
return Buffer.concat([cipher.update(data), cipher.final()]);
const ctx = crypto.createCipheriv('aes-256-cbc', key, iv);
return Buffer.concat([ctx.update(data), ctx.final()]);
};
/**
@ -34,10 +34,10 @@ exports.encipher = function encipher(data, key, iv) {
* @returns {Buffer}
*/
exports.decipher = function _decipher(data, key, iv) {
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
exports.decipher = function decipher(data, key, iv) {
const ctx = crypto.createDecipheriv('aes-256-cbc', key, iv);
try {
return Buffer.concat([decipher.update(data), decipher.final()]);
return Buffer.concat([ctx.update(data), ctx.final()]);
} catch (e) {
throw new Error('Bad key for decryption.');
}

View File

@ -13,7 +13,7 @@
const assert = require('assert');
const hashjs = require('hash.js');
const sha256 = require('./sha256');
const SHA256 = require('./sha256');
const POOL64 = Buffer.allocUnsafe(64);
/**
@ -23,15 +23,15 @@ const POOL64 = Buffer.allocUnsafe(64);
* @returns {Buffer}
*/
exports.hash = function _hash(alg, data) {
exports.hash = function hash(alg, data) {
if (alg === 'sha256')
return sha256.digest(data);
return SHA256.digest(data);
const hash = hashjs[alg];
const algo = hashjs[alg];
assert(hash != null, 'Unknown algorithm.');
assert(algo != null, 'Unknown algorithm.');
return Buffer.from(hash().update(data).digest());
return Buffer.from(algo().update(data).digest());
};
/**
@ -60,8 +60,8 @@ exports.sha1 = function sha1(data) {
* @returns {Buffer}
*/
exports.sha256 = function _sha256(data) {
return sha256.digest(data);
exports.sha256 = function sha256(data) {
return SHA256.digest(data);
};
/**
@ -71,7 +71,7 @@ exports.sha256 = function _sha256(data) {
*/
exports.hash160 = function hash160(data) {
return exports.hash('ripemd160', sha256.digest(data));
return exports.hash('ripemd160', SHA256.digest(data));
};
/**
@ -81,7 +81,7 @@ exports.hash160 = function hash160(data) {
*/
exports.hash256 = function hash256(data) {
return sha256.hash256(data);
return SHA256.hash256(data);
};
/**
@ -111,12 +111,12 @@ exports.root256 = function root256(left, right) {
* @returns {Buffer} HMAC
*/
exports.hmac = function _hmac(alg, data, key) {
const hash = hashjs[alg];
exports.hmac = function hmac(alg, data, key) {
const algo = hashjs[alg];
assert(hash != null, 'Unknown algorithm.');
assert(algo != null, 'Unknown algorithm.');
const hmac = hashjs.hmac(hash, key);
const ctx = hashjs.hmac(algo, key);
return Buffer.from(hmac.update(data).digest());
return Buffer.from(ctx.update(data).digest());
};

View File

@ -103,9 +103,9 @@ exports.root256 = function root256(left, right) {
* @returns {Buffer} HMAC
*/
exports.hmac = function _hmac(alg, data, key) {
const hmac = crypto.createHmac(alg, key);
return hmac.update(data).digest();
exports.hmac = function hmac(alg, data, key) {
const ctx = crypto.createHmac(alg, key);
return ctx.update(data).digest();
};
if (native) {

View File

@ -28,7 +28,7 @@ const schnorr = exports;
* @returns {Buffer}
*/
schnorr.hash = function _hash(msg, r) {
schnorr.hash = function hash(msg, r) {
const R = r.toBuffer('be', 32);
const B = POOL64;

View File

@ -480,8 +480,8 @@ LowlevelUp.prototype.range = async function range(options) {
* @returns {Promise} - Returns Array.
*/
LowlevelUp.prototype.keys = async function _keys(options) {
const keys = [];
LowlevelUp.prototype.keys = async function keys(options) {
const items = [];
const parse = options.parse;
const iter = this.iterator({
@ -509,10 +509,10 @@ LowlevelUp.prototype.keys = async function _keys(options) {
}
if (key)
keys.push(key);
items.push(key);
}
return keys;
return items;
};
/**
@ -522,8 +522,8 @@ LowlevelUp.prototype.keys = async function _keys(options) {
* @returns {Promise} - Returns Array.
*/
LowlevelUp.prototype.values = async function _values(options) {
const values = [];
LowlevelUp.prototype.values = async function values(options) {
const items = [];
const parse = options.parse;
const iter = this.iterator({
@ -551,10 +551,10 @@ LowlevelUp.prototype.values = async function _values(options) {
}
if (value)
values.push(value);
items.push(value);
}
return values;
return items;
};
/**

View File

@ -216,21 +216,21 @@ MemDB.prototype.del = function del(key, options, callback) {
* @param {Function} callback
*/
MemDB.prototype.batch = function _batch(ops, options, callback) {
MemDB.prototype.batch = function batch(ops, options, callback) {
if (!callback) {
callback = options;
options = null;
}
const batch = new Batch(this, options);
const b = new Batch(this, options);
if (ops) {
batch.ops = ops;
batch.write(callback);
b.ops = ops;
b.write(callback);
return undefined;
}
return batch;
return b;
};
/**

View File

@ -1111,20 +1111,20 @@ Route.prototype.compile = function compile() {
this.regex = new RegExp('^' + path + '$');
};
Route.prototype.match = function _match(pathname) {
Route.prototype.match = function match(pathname) {
this.compile();
assert(this.regex);
const match = this.regex.exec(pathname);
const matches = this.regex.exec(pathname);
if (!match)
if (!matches)
return null;
const params = Object.create(null);
for (let i = 1; i < match.length; i++) {
const item = match[i];
for (let i = 1; i < matches.length; i++) {
const item = matches[i];
const key = this.map[i - 1];
if (key)

View File

@ -530,20 +530,20 @@ function request(options) {
options.buffer = true;
return new Promise((resolve, reject) => {
const stream = new Request(options);
const req = new Request(options);
stream.on('error', err => reject(err));
stream.on('end', () => resolve(stream));
req.on('error', err => reject(err));
req.on('end', () => resolve(req));
stream.start();
stream.end();
req.start();
req.end();
});
}
request.stream = function _stream(options) {
const stream = new Request(options);
stream.start();
return stream;
request.stream = function stream(options) {
const req = new Request(options);
req.start();
return req;
};
/*

View File

@ -185,7 +185,7 @@ RPC.prototype.getInfo = async function getInfo(args, help) {
};
};
RPC.prototype.help = async function _help(args, help) {
RPC.prototype.help = async function help(args, _help) {
if (args.length === 0)
return 'Select a command.';

View File

@ -1874,7 +1874,7 @@ Mempool.prototype.getSize = function getSize() {
* @param {Amount} fee
*/
Mempool.prototype.prioritise = function _prioritise(entry, pri, fee) {
Mempool.prototype.prioritise = function prioritise(entry, pri, fee) {
if (-pri > entry.priority)
pri = -entry.priority;
@ -1886,12 +1886,12 @@ Mempool.prototype.prioritise = function _prioritise(entry, pri, fee) {
if (fee === 0)
return;
this.updateAncestors(entry, preprioritise);
this.updateAncestors(entry, prePrioritise);
entry.deltaFee += fee;
entry.descFee += fee;
this.updateAncestors(entry, prioritise);
this.updateAncestors(entry, postPrioritise);
};
/**
@ -2514,11 +2514,11 @@ function removeFee(parent, child) {
parent.descSize -= child.descSize;
}
function preprioritise(parent, child) {
function prePrioritise(parent, child) {
parent.descFee -= child.deltaFee;
}
function prioritise(parent, child) {
function postPrioritise(parent, child) {
parent.descFee += child.deltaFee;
}

View File

@ -595,7 +595,7 @@ BIP151.prototype.packetSize = function packetSize(cmd, body) {
* @returns {Buffer} Ciphertext payload
*/
BIP151.prototype.packet = function _packet(cmd, body) {
BIP151.prototype.packet = function packet(cmd, body) {
const size = this.packetSize(cmd, body);
const bw = new StaticWriter(size);
const payloadSize = size - 20;
@ -606,17 +606,17 @@ BIP151.prototype.packet = function _packet(cmd, body) {
bw.writeBytes(body);
bw.seek(16);
const packet = bw.render();
const payload = packet.slice(4, 4 + payloadSize);
const msg = bw.render();
const payload = msg.slice(4, 4 + payloadSize);
this.maybeRekey(packet);
this.maybeRekey(msg);
this.output.encryptSize(packet);
this.output.encryptSize(msg);
this.output.encrypt(payload);
this.output.finish().copy(packet, 4 + payloadSize);
this.output.finish().copy(msg, 4 + payloadSize);
this.output.sequence();
return packet;
return msg;
};
/**

View File

@ -29,37 +29,38 @@ function Framer(network) {
* Frame a payload with a header.
* @param {String} cmd - Packet type.
* @param {Buffer} payload
* @param {Buffer?} checksum - Precomputed checksum.
* @returns {Buffer} Payload with header prepended.
*/
Framer.prototype.packet = function _packet(cmd, payload, checksum) {
Framer.prototype.packet = function packet(cmd, payload, checksum) {
assert(payload, 'No payload.');
assert(cmd.length < 12);
assert(payload.length <= 0xffffffff);
const packet = Buffer.allocUnsafe(24 + payload.length);
const msg = Buffer.allocUnsafe(24 + payload.length);
// Magic value
packet.writeUInt32LE(this.network.magic, 0, true);
msg.writeUInt32LE(this.network.magic, 0, true);
// Command
packet.write(cmd, 4, 'ascii');
msg.write(cmd, 4, 'ascii');
for (let i = 4 + cmd.length; i < 16; i++)
packet[i] = 0;
msg[i] = 0;
// Payload length
packet.writeUInt32LE(payload.length, 16, true);
msg.writeUInt32LE(payload.length, 16, true);
if (!checksum)
checksum = digest.hash256(payload);
// Checksum
checksum.copy(packet, 20, 0, 4);
checksum.copy(msg, 20, 0, 4);
payload.copy(packet, 24);
payload.copy(msg, 24);
return packet;
return msg;
};
/*

View File

@ -1309,16 +1309,15 @@ HostEntry.prototype.key = function key() {
* @returns {Number}
*/
HostEntry.prototype.chance = function _chance(now) {
const attempts = this.attempts;
let chance = 1;
HostEntry.prototype.chance = function chance(now) {
let c = 1;
if (now - this.lastAttempt < 60 * 10)
chance *= 0.01;
c *= 0.01;
chance *= Math.pow(0.66, Math.min(attempts, 8));
c *= Math.pow(0.66, Math.min(this.attempts, 8));
return chance;
return c;
};
/**

View File

@ -7,7 +7,7 @@
'use strict';
const assert = require('assert');
const path = require('path');
const Path = require('path');
const os = require('os');
const fs = require('../utils/fs');
const util = require('../utils/util');
@ -29,7 +29,7 @@ function Config(module) {
this.module = module;
this.network = 'main';
this.prefix = path.join(HOME, `.${module}`);
this.prefix = Path.join(HOME, `.${module}`);
this.options = Object.create(null);
this.data = Object.create(null);
@ -601,7 +601,7 @@ Config.prototype.func = function func(key, fallback) {
* @returns {String|null}
*/
Config.prototype.path = function _path(key, fallback) {
Config.prototype.path = function path(key, fallback) {
let value = this.str(key);
if (fallback === undefined)
@ -612,16 +612,16 @@ Config.prototype.path = function _path(key, fallback) {
switch (value[0]) {
case '~': // home dir
value = path.join(HOME, value.substring(1));
value = Path.join(HOME, value.substring(1));
break;
case '@': // prefix
value = path.join(this.prefix, value.substring(1));
value = Path.join(this.prefix, value.substring(1));
break;
default: // cwd
break;
}
return path.normalize(value);
return Path.normalize(value);
};
/**
@ -671,21 +671,21 @@ Config.prototype.getPrefix = function getPrefix() {
if (prefix) {
if (prefix[0] === '~')
prefix = path.join(HOME, prefix.substring(1));
prefix = Path.join(HOME, prefix.substring(1));
return prefix;
}
prefix = path.join(HOME, `.${this.module}`);
prefix = Path.join(HOME, `.${this.module}`);
const network = this.str('network');
if (network) {
assert(isAlpha(network), 'Bad network.');
if (network !== 'main')
prefix = path.join(prefix, network);
prefix = Path.join(prefix, network);
}
return path.normalize(prefix);
return Path.normalize(prefix);
};
/**
@ -701,7 +701,7 @@ Config.prototype.getFile = function getFile(file) {
if (name)
return name;
return path.join(this.prefix, file);
return Path.join(this.prefix, file);
};
/**
@ -723,7 +723,7 @@ Config.prototype.ensure = function ensure() {
*/
Config.prototype.location = function location(file) {
return path.join(this.prefix, file);
return Path.join(this.prefix, file);
};
/**

View File

@ -507,15 +507,15 @@ Logger.prototype.log = function log(level, module, args) {
* @returns {LoggerContext}
*/
Logger.prototype.context = function _context(module) {
let context = this.contexts[module];
Logger.prototype.context = function context(module) {
let ctx = this.contexts[module];
if (!context) {
context = new LoggerContext(this, module);
this.contexts[module] = context;
if (!ctx) {
ctx = new LoggerContext(this, module);
this.contexts[module] = ctx;
}
return context;
return ctx;
};
/**

View File

@ -134,26 +134,26 @@ AbstractBlock.prototype.refresh = function refresh() {
* @returns {Hash|Buffer} hash
*/
AbstractBlock.prototype.hash = function _hash(enc) {
let hash = this._hash;
AbstractBlock.prototype.hash = function hash(enc) {
let h = this._hash;
if (!hash) {
hash = digest.hash256(this.toHead());
if (!h) {
h = digest.hash256(this.toHead());
if (!this.mutable)
this._hash = hash;
this._hash = h;
}
if (enc === 'hex') {
let hex = this._hhash;
if (!hex) {
hex = hash.toString('hex');
hex = h.toString('hex');
if (!this.mutable)
this._hhash = hex;
}
hash = hex;
h = hex;
}
return hash;
return h;
};
/**

View File

@ -175,26 +175,26 @@ TX.prototype.refresh = function refresh() {
* @returns {Hash|Buffer} hash
*/
TX.prototype.hash = function _hash(enc) {
let hash = this._hash;
TX.prototype.hash = function hash(enc) {
let h = this._hash;
if (!hash) {
hash = digest.hash256(this.toNormal());
if (!h) {
h = digest.hash256(this.toNormal());
if (!this.mutable)
this._hash = hash;
this._hash = h;
}
if (enc === 'hex') {
let hex = this._hhash;
if (!hex) {
hex = hash.toString('hex');
hex = h.toString('hex');
if (!this.mutable)
this._hhash = hex;
}
hash = hex;
h = hex;
}
return hash;
return h;
};
/**

View File

@ -9,9 +9,8 @@
const assert = require('assert');
const Int64 = require('./int64');
const digest = require('../crypto/digest');
const siphash24 = require('../crypto/siphash');
const SCRATCH = Buffer.allocUnsafe(64);
const DUMMY = Buffer.allocUnsafe(0);
const siphash = require('../crypto/siphash');
const DUMMY = Buffer.alloc(0);
const EOF = new Int64(-1);
/**
@ -27,22 +26,18 @@ function GCSFilter() {
this.data = DUMMY;
}
GCSFilter.prototype.hash = function _hash(enc) {
const hash = digest.hash256(this.data);
return enc === 'hex' ? hash.toString('hex') : hash;
GCSFilter.prototype.hash = function hash(enc) {
const h = digest.hash256(this.data);
return enc === 'hex' ? h.toString('hex') : h;
};
GCSFilter.prototype.header = function header(prev) {
const data = SCRATCH;
const hash = this.hash();
hash.copy(data, 0);
prev.copy(data, 32);
return digest.hash256(data);
return digest.root256(this.hash(), prev);
};
GCSFilter.prototype.match = function match(key, data) {
const br = new BitReader(this.data);
const term = siphash(data, key).imod(this.m);
const term = siphash24(data, key).imod(this.m);
let last = new Int64(0);
while (last.lt(term)) {
@ -70,7 +65,7 @@ GCSFilter.prototype.matchAny = function matchAny(key, items) {
const values = [];
for (const item of items) {
const hash = siphash(item, key).imod(this.m);
const hash = siphash24(item, key).imod(this.m);
values.push(hash);
}
@ -177,7 +172,7 @@ GCSFilter.prototype.fromItems = function fromItems(P, key, items) {
for (const item of items) {
assert(Buffer.isBuffer(item));
const hash = siphash(item, key).imod(this.m);
const hash = siphash24(item, key).imod(this.m);
values.push(hash);
}
@ -515,8 +510,8 @@ function compare(a, b) {
return a.cmp(b) < 0 ? -1 : 1;
}
function siphash(data, key) {
const [hi, lo] = siphash24(data, key);
function siphash24(data, key) {
const [hi, lo] = siphash(data, key);
return new Int64().join(hi, lo);
}

View File

@ -288,8 +288,8 @@ LRU.prototype._removeList = function _removeList(item) {
* @returns {String[]}
*/
LRU.prototype.keys = function _keys() {
const keys = [];
LRU.prototype.keys = function keys() {
const items = [];
for (let item = this.head; item; item = item.next) {
if (item === this.head)
@ -298,10 +298,10 @@ LRU.prototype.keys = function _keys() {
assert(item === this.head);
if (!item.next)
assert(item === this.tail);
keys.push(item.key);
items.push(item.key);
}
return keys;
return items;
};
/**
@ -309,13 +309,13 @@ LRU.prototype.keys = function _keys() {
* @returns {String[]}
*/
LRU.prototype.values = function _values() {
const values = [];
LRU.prototype.values = function values() {
const items = [];
for (let item = this.head; item; item = item.next)
values.push(item.value);
items.push(item.value);
return values;
return items;
};
/**

View File

@ -37,8 +37,8 @@ function ProtoReader(data, zeroCopy) {
Object.setPrototypeOf(ProtoReader.prototype, BufferReader.prototype);
ProtoReader.prototype.readVarint = function _readVarint() {
const {size, value} = readVarint(this.data, this.offset);
ProtoReader.prototype.readVarint = function readVarint() {
const {size, value} = _readVarint(this.data, this.offset);
this.offset += size;
return value;
};
@ -159,7 +159,7 @@ ProtoReader.prototype.readField = function readField(tag, opt) {
* Encoding
*/
function readVarint(data, off) {
function _readVarint(data, off) {
let num = 0;
let ch = 0x80;
let size = 0;

View File

@ -41,7 +41,7 @@ function ProtoWriter() {
Object.setPrototypeOf(ProtoWriter.prototype, BufferWriter.prototype);
ProtoWriter.prototype.writeVarint = function _writeVarint(num) {
ProtoWriter.prototype.writeVarint = function writeVarint(num) {
const size = sizeVarint(num);
// Avoid an extra allocation until
@ -83,7 +83,7 @@ ProtoWriter.prototype.writeVarint = function _writeVarint(num) {
}
default: {
const value = Buffer.allocUnsafe(size);
writeVarint(value, num, 0);
_writeVarint(value, num, 0);
this.writeBytes(value);
break;
}
@ -123,7 +123,7 @@ ProtoWriter.prototype.writeFieldString = function writeFieldString(tag, data, en
* Encoding
*/
function writeVarint(data, num, off) {
function _writeVarint(data, num, off) {
assert(Number.isSafeInteger(num), 'Number exceeds 2^53-1.');
do {

View File

@ -446,31 +446,31 @@ util.random = function random(min, max) {
* @returns {Buffer}
*/
util.nonce = function _nonce(size) {
let n, nonce;
util.nonce = function nonce(size) {
let n, data;
if (!size)
size = 8;
switch (size) {
case 8:
nonce = Buffer.allocUnsafe(8);
data = Buffer.allocUnsafe(8);
n = util.random(0, 0x100000000);
nonce.writeUInt32LE(n, 0, true);
data.writeUInt32LE(n, 0, true);
n = util.random(0, 0x100000000);
nonce.writeUInt32LE(n, 4, true);
data.writeUInt32LE(n, 4, true);
break;
case 4:
nonce = Buffer.allocUnsafe(4);
data = Buffer.allocUnsafe(4);
n = util.random(0, 0x100000000);
nonce.writeUInt32LE(n, 0, true);
data.writeUInt32LE(n, 0, true);
break;
default:
assert(false, 'Bad nonce size.');
break;
}
return nonce;
return data;
};
/**

View File

@ -169,12 +169,12 @@ MasterKey.fromOptions = function fromOptions(options) {
* @returns {Promise} - Returns {@link HDPrivateKey}.
*/
MasterKey.prototype.unlock = async function _unlock(passphrase, timeout) {
const unlock = await this.locker.lock();
MasterKey.prototype.unlock = async function unlock(passphrase, timeout) {
const _unlock = await this.locker.lock();
try {
return await this._unlock(passphrase, timeout);
} finally {
unlock();
_unlock();
}
};
@ -310,7 +310,7 @@ MasterKey.prototype.decipher = function decipher(data, iv) {
* @returns {Promise}
*/
MasterKey.prototype.lock = async function _lock() {
MasterKey.prototype.lock = async function lock() {
const unlock = await this.locker.lock();
try {
return await this._lock();

View File

@ -110,7 +110,7 @@ RPC.prototype.init = function init() {
this.add('setloglevel', this.setLogLevel);
};
RPC.prototype.help = async function _help(args, help) {
RPC.prototype.help = async function help(args, _help) {
if (args.length === 0)
return 'Select a command.';

View File

@ -20,22 +20,22 @@ function Framer() {
return new Framer();
}
Framer.prototype.packet = function _packet(packet) {
const size = 10 + packet.getSize();
Framer.prototype.packet = function packet(payload) {
const size = 10 + payload.getSize();
const bw = new StaticWriter(size);
bw.writeU32(packet.id);
bw.writeU8(packet.cmd);
bw.writeU32(payload.id);
bw.writeU8(payload.cmd);
bw.seek(4);
packet.toWriter(bw);
payload.toWriter(bw);
bw.writeU8(0x0a);
const data = bw.render();
data.writeUInt32LE(data.length - 10, 5, true);
const msg = bw.render();
msg.writeUInt32LE(msg.length - 10, 5, true);
return data;
return msg;
};
/*

View File

@ -7,8 +7,8 @@
'use strict';
const secp256k1 = require('../crypto/secp256k1');
const scrypt = require('../crypto/scrypt');
const mine = require('../mining/mine');
const {derive} = require('../crypto/scrypt');
const hashcash = require('../mining/mine');
const packets = require('./packets');
/**
@ -169,8 +169,8 @@ jobs.ecSign = function ecSign(msg, key) {
* @returns {Number}
*/
jobs.mine = function _mine(data, target, min, max) {
const nonce = mine(data, target, min, max);
jobs.mine = function mine(data, target, min, max) {
const nonce = hashcash(data, target, min, max);
return new packets.MineResultPacket(nonce);
};
@ -186,7 +186,7 @@ jobs.mine = function _mine(data, target, min, max) {
* @returns {Buffer}
*/
jobs.scrypt = function _scrypt(passwd, salt, N, r, p, len) {
const key = scrypt.derive(passwd, salt, N, r, p, len);
jobs.scrypt = function scrypt(passwd, salt, N, r, p, len) {
const key = derive(passwd, salt, N, r, p, len);
return new packets.ScryptResultPacket(key);
};