Skip to content

Commit 77f063a

Browse files
authored
fix: fix converting strings to IP addresses and back again (#270)
Partial fix for the issues observed in the ipfs interop suite - windows seems to lose it's reference to the results variable during node startup, but only windows. Instead return the result from the scope in which is is created.
1 parent 36de98f commit 77f063a

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

src/convert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const anybaseDecoder = (function () {
103103

104104
function ip2bytes (ipString: string) {
105105
if (!ip.isIP(ipString)) {
106-
throw new Error(`invalid ip address "${ipString}"`)
106+
throw new Error('invalid ip address')
107107
}
108108
return ip.toBytes(ipString)
109109
}
@@ -114,7 +114,7 @@ function bytes2ip (ipBuff: Uint8Array) {
114114
throw new Error('ipBuff is required')
115115
}
116116
if (!ip.isIP(ipString)) {
117-
throw new Error(`invalid ip address "${ipString}"`)
117+
throw new Error('invalid ip address')
118118
}
119119
return ipString
120120
}

src/ip.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ export const isV6 = isIPv6
99
// but with buf/offset args removed because we don't use them
1010
export const toBytes = function (ip: string): Uint8Array {
1111
let offset = 0
12-
let result
13-
ip = ip.trim()
12+
ip = ip.toString().trim()
1413

1514
if (isV4(ip)) {
16-
result = new Uint8Array(offset + 4)
15+
const bytes = new Uint8Array(offset + 4)
16+
1717
ip.split(/\./g).forEach((byte) => {
18-
result[offset++] = parseInt(byte, 10) & 0xff
18+
bytes[offset++] = parseInt(byte, 10) & 0xff
1919
})
20-
} else if (isV6(ip)) {
20+
21+
return bytes
22+
}
23+
24+
if (isV6(ip)) {
2125
const sections = ip.split(':', 8)
2226

2327
let i
@@ -48,44 +52,50 @@ export const toBytes = function (ip: string): Uint8Array {
4852
sections.splice.apply(sections, argv)
4953
}
5054

51-
result = new Uint8Array(offset + 16)
55+
const bytes = new Uint8Array(offset + 16)
56+
5257
for (i = 0; i < sections.length; i++) {
5358
const word = parseInt(sections[i], 16)
54-
result[offset++] = (word >> 8) & 0xff
55-
result[offset++] = word & 0xff
59+
bytes[offset++] = (word >> 8) & 0xff
60+
bytes[offset++] = word & 0xff
5661
}
57-
}
5862

59-
if (result == null) {
60-
throw new Error(`invalid ip address "${ip}"`)
63+
return bytes
6164
}
6265

63-
return result
66+
throw new Error('invalid ip address')
6467
}
6568

6669
// Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L63
6770
export const toString = function (buf: Uint8Array, offset: number = 0, length?: number) {
6871
offset = ~~offset
6972
length = length ?? (buf.length - offset)
7073

71-
const result = []
72-
let string = ''
7374
const view = new DataView(buf.buffer)
75+
7476
if (length === 4) {
77+
const result = []
78+
7579
// IPv4
7680
for (let i = 0; i < length; i++) {
7781
result.push(buf[offset + i])
7882
}
79-
string = result.join('.')
80-
} else if (length === 16) {
83+
84+
return result.join('.')
85+
}
86+
87+
if (length === 16) {
88+
const result = []
89+
8190
// IPv6
8291
for (let i = 0; i < length; i += 2) {
8392
result.push(view.getUint16(offset + i).toString(16))
8493
}
85-
string = result.join(':')
86-
string = string.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3')
87-
string = string.replace(/:{3,4}/, '::')
94+
95+
return result.join(':')
96+
.replace(/(^|:)0(:0)*:0(:|$)/, '$1::$3')
97+
.replace(/:{3,4}/, '::')
8898
}
8999

90-
return string
100+
return ''
91101
}

0 commit comments

Comments
 (0)