-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added utils.js from js-libp2p-floodsub
- Loading branch information
Showing
1 changed file
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const crypto = require('libp2p-crypto') | ||
const bs58 = require('bs58') | ||
|
||
exports = module.exports | ||
|
||
/** | ||
* Generatea random sequence number. | ||
* | ||
* @returns {Buffer} | ||
* @private | ||
*/ | ||
exports.randomSeqno = () => { | ||
return crypto.randomBytes(20) | ||
} | ||
|
||
/** | ||
* Generate a message id, based on the `from` and `seqno`. | ||
* | ||
* @param {string} from | ||
* @param {Buffer} seqno | ||
* @returns {string} | ||
* @private | ||
*/ | ||
exports.msgId = (from, seqno) => { | ||
return from + seqno.toString('hex') | ||
} | ||
|
||
/** | ||
* Check if any member of the first set is also a member | ||
* of the second set. | ||
* | ||
* @param {Set|Array} a | ||
* @param {Set|Array} b | ||
* @returns {boolean} | ||
* @private | ||
*/ | ||
exports.anyMatch = (a, b) => { | ||
let bHas | ||
if (Array.isArray(b)) { | ||
bHas = (val) => b.indexOf(val) > -1 | ||
} else { | ||
bHas = (val) => b.has(val) | ||
} | ||
|
||
for (let val of a) { | ||
if (bHas(val)) { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* Make everything an array. | ||
* | ||
* @param {any} maybeArray | ||
* @returns {Array} | ||
* @private | ||
*/ | ||
exports.ensureArray = (maybeArray) => { | ||
if (!Array.isArray(maybeArray)) { | ||
return [maybeArray] | ||
} | ||
|
||
return maybeArray | ||
} | ||
|
||
exports.normalizeInRpcMessages = (messages) => { | ||
if (!messages) { | ||
return messages | ||
} | ||
return messages.map((msg) => { | ||
const m = Object.assign({}, msg) | ||
if (Buffer.isBuffer(msg.from)) { | ||
m.from = bs58.encode(msg.from) | ||
} | ||
return m | ||
}) | ||
} | ||
|
||
exports.normalizeOutRpcMessages = (messages) => { | ||
if (!messages) { | ||
return messages | ||
} | ||
return messages.map((msg) => { | ||
const m = Object.assign({}, msg) | ||
if (typeof msg.from === 'string' || msg.from instanceof String) { | ||
m.from = bs58.decode(msg.from) | ||
} | ||
return m | ||
}) | ||
} |