Update lib.js

- Added require-support for BuildKBucket
- BuildKBucket now works with and without `new`
- Renamed instance name to KBucket
This commit is contained in:
sairajzero 2022-03-31 22:25:22 +05:30
parent 37edbfc461
commit 5bb0ee8588

19
lib.js
View File

@ -1,5 +1,4 @@
//lib v1.2.1 (function(GLOBAL) { //lib v1.2.2
(function(GLOBAL) {
'use strict'; 'use strict';
/* Utility Libraries required for Standard operations /* Utility Libraries required for Standard operations
* All credits for these codes belong to their respective creators, moderators and owners. * All credits for these codes belong to their respective creators, moderators and owners.
@ -6590,10 +6589,22 @@
//kbucket.js //kbucket.js
(function() { (function() {
const getRandomValues = function(buf) {
if (typeof require === 'function') {
var bytes = require('crypto').randomBytes(buf.length);
buf.set(bytes)
return buf;
} else if (GLOBAL.crypto && GLOBAL.crypto.getRandomValues)
return GLOBAL.crypto.getRandomValues(buf);
else
return null;
}
// Kademlia DHT K-bucket implementation as a binary tree. // Kademlia DHT K-bucket implementation as a binary tree.
// by 'Tristan Slominski' under 'MIT License' // by 'Tristan Slominski' under 'MIT License'
GLOBAL.BuildKBucket = function BuildKBucket(options = {}) { GLOBAL.BuildKBucket = function KBucket(options = {}) {
this.localNodeId = options.localNodeId || window.crypto.getRandomValues(new Uint8Array(20)) if (!(this instanceof KBucket))
return new KBucket(options);
this.localNodeId = options.localNodeId || getRandomValues(new Uint8Array(20))
this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket || 20 this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket || 20
this.numberOfNodesToPing = options.numberOfNodesToPing || 3 this.numberOfNodesToPing = options.numberOfNodesToPing || 3
this.distance = options.distance || this.distance this.distance = options.distance || this.distance