forked from libp2p/js-libp2p-interfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
147 lines (134 loc) · 3.06 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
// @ts-ignore libp2p crypto has no types
const randomBytes = require('libp2p-crypto/src/random-bytes')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayFromString = require('uint8arrays/from-string')
const PeerId = require('peer-id')
const multihash = require('multihashes')
/**
* @typedef {import('./message/rpc').RPC.IMessage} IMessage
* @typedef {import('./message/rpc').RPC.Message} Message
* @typedef {import('.').InMessage} NormalizedIMessage
*/
/**
* Generatea random sequence number.
*
* @returns {Uint8Array}
* @private
*/
const randomSeqno = () => {
return randomBytes(8)
}
/**
* Generate a message id, based on the `from` and `seqno`.
*
* @param {string} from
* @param {Uint8Array} seqno
* @returns {Uint8Array}
* @private
*/
const msgId = (from, seqno) => {
const fromBytes = PeerId.createFromB58String(from).id
const msgId = new Uint8Array(fromBytes.length + seqno.length)
msgId.set(fromBytes, 0)
msgId.set(seqno, fromBytes.length)
return msgId
}
/**
* Generate a message id, based on message `data`.
*
* @param {Uint8Array} data
* @returns {Uint8Array}
* @private
*/
const noSignMsgId = (data) => multihash.encode(data, 'sha2-256')
/**
* Check if any member of the first set is also a member
* of the second set.
*
* @param {Set<number>|Array<number>} a
* @param {Set<number>|Array<number>} b
* @returns {boolean}
* @private
*/
const anyMatch = (a, b) => {
let bHas
if (Array.isArray(b)) {
/**
* @param {number} val
*/
bHas = (val) => b.indexOf(val) > -1
} else {
/**
* @param {number} val
*/
bHas = (val) => b.has(val)
}
for (const val of a) {
if (bHas(val)) {
return true
}
}
return false
}
/**
* Make everything an array.
*
* @template T
* @param {T|T[]} maybeArray
* @returns {T[]}
* @private
*/
const ensureArray = (maybeArray) => {
if (!Array.isArray(maybeArray)) {
return [maybeArray]
}
return maybeArray
}
/**
* Ensures `message.from` is base58 encoded
*
* @template {{from?:any}} T
* @param {T & IMessage} message
* @param {string} [peerId]
* @returns {NormalizedIMessage}
*/
const normalizeInRpcMessage = (message, peerId) => {
/** @type {NormalizedIMessage} */
// @ts-ignore receivedFrom not yet defined
const m = Object.assign({}, message)
if (message.from instanceof Uint8Array) {
m.from = uint8ArrayToString(message.from, 'base58btc')
}
if (peerId) {
m.receivedFrom = peerId
}
return m
}
/**
* @template {{from?:any, data?:any}} T
*
* @param {T & NormalizedIMessage} message
* @returns {Message}
*/
const normalizeOutRpcMessage = (message) => {
/** @type {Message} */
// @ts-ignore from not yet defined
const m = Object.assign({}, message)
if (typeof message.from === 'string') {
m.from = uint8ArrayFromString(message.from, 'base58btc')
}
if (typeof message.data === 'string') {
m.data = uint8ArrayFromString(message.data)
}
return m
}
module.exports = {
randomSeqno,
msgId,
noSignMsgId,
anyMatch,
ensureArray,
normalizeInRpcMessage,
normalizeOutRpcMessage
}