Comparing version 0.2.0 to 1.0.0
@@ -8,3 +8,3 @@ { | ||
"parserOptions": { | ||
"ecmaVersion": 8 | ||
"ecmaVersion": 9 | ||
}, | ||
@@ -11,0 +11,0 @@ "root": true, |
114
lib/bloom.js
@@ -10,6 +10,5 @@ /*! | ||
const assert = require('assert'); | ||
const {enforce} = require('bsert'); | ||
const bio = require('bufio'); | ||
const murmur3 = require('mrmr'); | ||
const {encoding} = bio; | ||
@@ -67,11 +66,2 @@ /* | ||
fromOptions(size, n, tweak, update) { | ||
assert(typeof size === 'number', '`size` must be a number.'); | ||
assert(size > 0, '`size` must be greater than zero.'); | ||
assert(Number.isSafeInteger(size), '`size` must be an integer.'); | ||
size -= size % 8; | ||
const filter = Buffer.allocUnsafe(size / 8); | ||
filter.fill(0); | ||
if (tweak == null || tweak === -1) | ||
@@ -85,13 +75,20 @@ tweak = (Math.random() * 0x100000000) >>> 0; | ||
update = BloomFilter.flags[update.toUpperCase()]; | ||
assert(update != null, 'Unknown update flag.'); | ||
enforce(update != null, 'update', 'flag'); | ||
} | ||
assert(size > 0, '`size` must be greater than zero.'); | ||
assert(n > 0, '`n` must be greater than zero.'); | ||
assert(Number.isSafeInteger(n), '`n` must be an integer.'); | ||
assert(typeof tweak === 'number', '`tweak` must be a number.'); | ||
assert(Number.isSafeInteger(tweak), '`tweak` must be an integer.'); | ||
assert(BloomFilter.flagsByVal[update], 'Unknown update flag.'); | ||
enforce(Number.isSafeInteger(size) && size >= 0, 'size', 'integer'); | ||
enforce(Number.isSafeInteger(n) && n >= 0, 'n', 'integer'); | ||
enforce((tweak >>> 0) === tweak, 'tweak', 'integer'); | ||
enforce((update >>> 0) === update, 'update', 'integer'); | ||
enforce(update <= 2, 'update', 'range between 0 and 2'); | ||
this.filter = filter; | ||
if (size < 8) | ||
size = 8; | ||
if (n === 0) | ||
n = 1; | ||
size -= size & 7; | ||
this.filter = Buffer.alloc(size / 8, 0x00); | ||
this.size = size; | ||
@@ -120,3 +117,3 @@ this.n = n; | ||
* Perform the mumur3 hash on data. | ||
* @param {Buffer} val | ||
* @param {Buffer} value | ||
* @param {Number} n | ||
@@ -126,4 +123,4 @@ * @returns {Number} | ||
hash(val, n) { | ||
return murmur3.tweak(val, n, this.tweak) % this.size; | ||
hash(value, n) { | ||
return murmur3.tweak(value, n, this.tweak) % this.size; | ||
} | ||
@@ -145,4 +142,4 @@ | ||
add(val, enc) { | ||
val = toBuffer(val, enc); | ||
add(value, enc) { | ||
const val = toBuffer(value, enc); | ||
@@ -157,3 +154,3 @@ for (let i = 0; i < this.n; i++) { | ||
* Test whether data is present in the filter. | ||
* @param {Buffer|String} val | ||
* @param {Buffer|String} value | ||
* @param {String?} enc - Can be any of the Buffer object's encodings. | ||
@@ -163,4 +160,4 @@ * @returns {Boolean} | ||
test(val, enc) { | ||
val = toBuffer(val, enc); | ||
test(value, enc) { | ||
const val = toBuffer(value, enc); | ||
@@ -179,3 +176,3 @@ for (let i = 0; i < this.n; i++) { | ||
* filter and potentially add data. | ||
* @param {Buffer|String} val | ||
* @param {Buffer|String} value | ||
* @param {String?} enc - Can be any of the Buffer object's encodings. | ||
@@ -185,4 +182,4 @@ * @returns {Boolean} Whether data was added. | ||
added(val, enc) { | ||
val = toBuffer(val, enc); | ||
added(value, enc) { | ||
const val = toBuffer(value, enc); | ||
@@ -212,7 +209,5 @@ let ret = false; | ||
static fromRate(items, rate, update) { | ||
assert(typeof items === 'number', '`items` must be a number.'); | ||
assert(items > 0, '`items` must be greater than zero.'); | ||
assert(Number.isSafeInteger(items), '`items` must be an integer.'); | ||
assert(typeof rate === 'number', '`rate` must be a number.'); | ||
assert(rate >= 0 && rate <= 1, '`rate` must be between 0.0 and 1.0.'); | ||
enforce(Number.isSafeInteger(items) && items > 0, 'items', 'integer'); | ||
enforce(typeof rate === 'number' && isFinite(rate), 'rate', 'number'); | ||
enforce(rate >= 0 && rate <= 1, 'rate', 'range between 0.1 and 1.0.'); | ||
@@ -223,4 +218,4 @@ const bits = (-1 / LN2SQUARED * items * Math.log(rate)) | 0; | ||
if (update !== -1) { | ||
assert(size <= BloomFilter.MAX_BLOOM_FILTER_SIZE * 8, | ||
'Bloom filter size violates policy limits!'); | ||
if (size > BloomFilter.MAX_BLOOM_FILTER_SIZE * 8) | ||
throw new Error('Bloom filter size violates policy limits!'); | ||
} | ||
@@ -231,4 +226,4 @@ | ||
if (update !== -1) { | ||
assert(n <= BloomFilter.MAX_HASH_FUNCS, | ||
'Bloom filter size violates policy limits!'); | ||
if (n > BloomFilter.MAX_HASH_FUNCS) | ||
throw new Error('Bloom filter size violates policy limits!'); | ||
} | ||
@@ -260,3 +255,3 @@ | ||
getSize() { | ||
return encoding.sizeVarBytes(this.filter) + 9; | ||
return bio.sizeVarBytes(this.filter) + 9; | ||
} | ||
@@ -289,3 +284,6 @@ | ||
this.update = br.readU8(); | ||
assert(BloomFilter.flagsByVal[this.update] != null, 'Unknown update flag.'); | ||
if (this.update > 2) | ||
throw new Error('Invalid update flag.'); | ||
return this; | ||
@@ -343,7 +341,7 @@ } | ||
BloomFilter.flagsByVal = { | ||
0: 'NONE', | ||
1: 'ALL', | ||
2: 'PUBKEY_ONLY' | ||
}; | ||
BloomFilter.flagsByVal = [ | ||
'NONE', | ||
'ALL', | ||
'PUBKEY_ONLY' | ||
]; | ||
@@ -354,23 +352,11 @@ /* | ||
const POOL20 = Buffer.allocUnsafe(20); | ||
const POOL32 = Buffer.allocUnsafe(32); | ||
function toBuffer(val, enc) { | ||
if (typeof val !== 'string') | ||
return val; | ||
if (enc !== 'hex') | ||
return Buffer.from(val, enc); | ||
if (val.length === 40) { | ||
POOL20.write(val, 0, 20, 'hex'); | ||
return POOL20; | ||
function toBuffer(value, enc) { | ||
if (typeof value !== 'string') { | ||
enforce(Buffer.isBuffer(value), 'value', 'buffer'); | ||
return value; | ||
} | ||
if (val.length === 64) { | ||
POOL32.write(val, 0, 32, 'hex'); | ||
return POOL32; | ||
} | ||
enforce(typeof enc === 'string', 'enc', 'string'); | ||
return Buffer.from(val, 'hex'); | ||
return Buffer.from(value, enc); | ||
} | ||
@@ -377,0 +363,0 @@ |
@@ -10,4 +10,4 @@ /*! | ||
const assert = require('assert'); | ||
const bio = require('bufio'); | ||
const {enforce} = require('bsert'); | ||
const {encoding} = require('bufio'); | ||
const murmur3 = require('mrmr'); | ||
@@ -51,7 +51,5 @@ const DUMMY = Buffer.alloc(0); | ||
fromRate(items, rate) { | ||
assert(typeof items === 'number', '`items` must be a number.'); | ||
assert(items > 0, '`items` must be greater than zero.'); | ||
assert(Number.isSafeInteger(items), '`items` must be an integer.'); | ||
assert(typeof rate === 'number', '`rate` must be a number.'); | ||
assert(rate > 0 && rate <= 1, '`rate` must be between 0.0 and 1.0.'); | ||
enforce(Number.isSafeInteger(items) && items > 0, 'items', 'integer'); | ||
enforce(typeof rate === 'number' && isFinite(rate), 'rate', 'number'); | ||
enforce(rate >= 0 && rate <= 1, 'rate', 'range between 0.1 and 1.0.'); | ||
@@ -74,4 +72,3 @@ const logRate = Math.log(rate); | ||
const filter = Buffer.allocUnsafe(items * 8); | ||
filter.fill(0); | ||
const filter = Buffer.alloc(items * 8, 0x00); | ||
@@ -101,3 +98,3 @@ this.n = n; | ||
* Perform the mumur3 hash on data. | ||
* @param {Buffer} val | ||
* @param {Buffer} value | ||
* @param {Number} seed | ||
@@ -107,4 +104,4 @@ * @returns {Number} | ||
hash(val, n) { | ||
return murmur3.tweak(val, n, this.tweak); | ||
hash(value, n) { | ||
return murmur3.tweak(value, n, this.tweak); | ||
} | ||
@@ -131,4 +128,4 @@ | ||
add(val, enc) { | ||
val = toBuffer(val, enc); | ||
add(value, enc) { | ||
const val = toBuffer(value, enc); | ||
@@ -184,3 +181,3 @@ if (this.entries === this.limit) { | ||
* Test whether data is present in the filter. | ||
* @param {Buffer|String} val | ||
* @param {Buffer|String} value | ||
* @param {String?} enc - Can be any of the Buffer object's encodings. | ||
@@ -190,7 +187,7 @@ * @returns {Boolean} | ||
test(val, enc) { | ||
test(value, enc) { | ||
if (this.entries === 0) | ||
return false; | ||
val = toBuffer(val, enc); | ||
const val = toBuffer(value, enc); | ||
@@ -219,3 +216,3 @@ for (let i = 0; i < this.n; i++) { | ||
* filter and potentially add data. | ||
* @param {Buffer|String} val | ||
* @param {Buffer|String} value | ||
* @param {String?} enc - Can be any of the Buffer object's encodings. | ||
@@ -225,4 +222,4 @@ * @returns {Boolean} Whether data was added. | ||
added(val, enc) { | ||
val = toBuffer(val, enc); | ||
added(value, enc) { | ||
const val = toBuffer(value, enc); | ||
@@ -250,4 +247,4 @@ if (!this.test(val)) { | ||
function read(data, off) { | ||
const hi = bio.readU32(data, off + 4); | ||
const lo = bio.readU32(data, off); | ||
const hi = encoding.readU32(data, off + 4); | ||
const lo = encoding.readU32(data, off); | ||
return new U64(hi, lo); | ||
@@ -257,27 +254,15 @@ } | ||
function write(data, value, off) { | ||
bio.writeU32(data, value.hi, off + 4); | ||
bio.writeU32(data, value.lo, off); | ||
encoding.writeU32(data, value.hi, off + 4); | ||
encoding.writeU32(data, value.lo, off); | ||
} | ||
const POOL20 = Buffer.allocUnsafe(20); | ||
const POOL32 = Buffer.allocUnsafe(32); | ||
function toBuffer(val, enc) { | ||
if (typeof val !== 'string') | ||
return val; | ||
if (enc !== 'hex') | ||
return Buffer.from(val, enc); | ||
if (val.length === 40) { | ||
POOL20.write(val, 0, 20, 'hex'); | ||
return POOL20; | ||
function toBuffer(value, enc) { | ||
if (typeof value !== 'string') { | ||
enforce(Buffer.isBuffer(value), 'value', 'buffer'); | ||
return value; | ||
} | ||
if (val.length === 64) { | ||
POOL32.write(val, 0, 32, 'hex'); | ||
return POOL32; | ||
} | ||
enforce(typeof enc === 'string', 'enc', 'string'); | ||
return Buffer.from(val, 'hex'); | ||
return Buffer.from(value, enc); | ||
} | ||
@@ -284,0 +269,0 @@ |
{ | ||
"name": "bfilter", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "Bloom filters for javascript", | ||
@@ -19,33 +19,17 @@ "keywords": [ | ||
"scripts": { | ||
"browserify": "browserify -s bfilter lib/bfilter.js | uglifyjs -c > bfilter.js", | ||
"clean": "rm -f bfilter.js", | ||
"lint": "eslint lib/ test/ || exit 0", | ||
"test": "mocha --reporter spec test/*-test.js", | ||
"webpack": "webpack --mode production --config webpack.config.js" | ||
"test": "mocha --reporter spec test/*-test.js" | ||
}, | ||
"dependencies": { | ||
"bufio": "~0.2.0", | ||
"mrmr": "~0.1.0" | ||
"bsert": "~0.0.3", | ||
"bufio": "~1.0.1", | ||
"mrmr": "~0.1.1" | ||
}, | ||
"devDependencies": { | ||
"babelify": "^8.0.0", | ||
"babel-core": "^6.26.3", | ||
"babel-loader": "^7.1.4", | ||
"babel-preset-env": "^1.7.0", | ||
"browserify": "^16.2.2", | ||
"eslint": "^4.19.1", | ||
"mocha": "^5.2.0", | ||
"uglifyjs-webpack-plugin": "^1.2.5", | ||
"uglify-es": "^3.3.9", | ||
"webpack": "^4.11.1", | ||
"webpack-cli": "^3.0.3" | ||
"eslint": "^5.1.0", | ||
"mocha": "^5.2.0" | ||
}, | ||
"engines": { | ||
"node": ">=8.0.0" | ||
}, | ||
"browserify": { | ||
"transform": [ | ||
"babelify" | ||
] | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
2
0
19031
3
8
590
+ Addedbsert@~0.0.3
+ Addedbufio@1.0.7(transitive)
- Removedbufio@0.2.0(transitive)
Updatedbufio@~1.0.1
Updatedmrmr@~0.1.1