Skip to content

Commit a3948cf

Browse files
committed
fix: move static function from ip to the parse-udp module
1 parent b4557a7 commit a3948cf

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/server/parse-udp.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
import ipLib from 'ip'
21
import common from '../common.js'
32
import { equal } from 'uint8-util'
43

4+
function ipToString (buff, offset, length) {
5+
offset = ~~offset
6+
length = length || (buff.length - offset)
7+
8+
let result = []
9+
if (length === 4) {
10+
// IPv4
11+
for (let i = 0; i < length; i++) {
12+
result.push(buff[offset + i])
13+
}
14+
result = result.join('.')
15+
} else if (length === 16) {
16+
// IPv6
17+
for (let i = 0; i < length; i += 2) {
18+
result.push(buff.readUInt16BE(offset + i).toString(16))
19+
}
20+
result = result.join(':')
21+
result = result.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3')
22+
result = result.replace(/:{3,4}/, '::')
23+
}
24+
25+
return result
26+
};
27+
528
export default function (msg, rinfo) {
629
if (msg.length < 16) throw new Error('received packet is too short')
730

@@ -30,7 +53,7 @@ export default function (msg, rinfo) {
3053

3154
const ip = msg.readUInt32BE(84) // optional
3255
params.ip = ip
33-
? ipLib.toString(ip)
56+
? ipToString(ip)
3457
: rinfo.address
3558

3659
params.key = msg.readUInt32BE(88) // Optional: unique random key from client

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"compact2string": "^1.4.1",
3636
"cross-fetch-ponyfill": "^1.0.3",
3737
"debug": "^4.3.4",
38-
"ip": "^2.0.1",
3938
"lru": "^3.1.0",
4039
"minimist": "^1.2.8",
4140
"once": "^1.4.0",

0 commit comments

Comments
 (0)