File tree 3 files changed +33
-3
lines changed
3 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -103,7 +103,7 @@ const anybaseDecoder = (function () {
103
103
104
104
function ip2bytes ( ipString : string ) {
105
105
if ( ! ip . isIP ( ipString ) ) {
106
- throw new Error ( ' invalid ip address' )
106
+ throw new Error ( ` invalid ip address " ${ ipString } "` )
107
107
}
108
108
return ip . toBytes ( ipString )
109
109
}
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ export const isV6 = isIPv6
10
10
export const toBytes = function ( ip : string ) : Uint8Array {
11
11
let offset = 0
12
12
let result
13
+ ip = ip . trim ( )
13
14
14
15
if ( isV4 ( ip ) ) {
15
16
result = new Uint8Array ( offset + 4 )
@@ -56,14 +57,14 @@ export const toBytes = function (ip: string): Uint8Array {
56
57
}
57
58
58
59
if ( result == null ) {
59
- throw Error ( 'Invalid ip address: ' + ip )
60
+ throw new Error ( `invalid ip address " ${ ip } "` )
60
61
}
61
62
62
63
return result
63
64
}
64
65
65
66
// Copied from https://github.com/indutny/node-ip/blob/master/lib/ip.js#L63
66
- export const toString = function ( buf : Uint8Array , offset : number , length : number ) {
67
+ export const toString = function ( buf : Uint8Array , offset : number = 0 , length ? : number ) {
67
68
offset = ~ ~ offset
68
69
length = length ?? ( buf . length - offset )
69
70
Original file line number Diff line number Diff line change
1
+ /* eslint max-nested-callbacks: ["error", 8] */
2
+ /* eslint-env mocha */
3
+ import { expect } from 'aegir/chai'
4
+ import { toBytes , toString } from '../src/ip.js'
5
+
6
+ describe ( 'ip' , ( ) => {
7
+ describe ( 'toBytes' , ( ) => {
8
+ it ( 'should handle extra characters' , ( ) => {
9
+ const address = '127.0.0.1 '
10
+ const bytes = toBytes ( address )
11
+
12
+ expect ( toString ( bytes ) ) . to . equal ( address . trim ( ) )
13
+ } )
14
+
15
+ it ( 'should turn loopback into bytes' , ( ) => {
16
+ const address = '127.0.0.1'
17
+ const bytes = toBytes ( address )
18
+
19
+ expect ( toString ( bytes ) ) . to . equal ( address )
20
+ } )
21
+
22
+ it ( 'should turn private address into bytes' , ( ) => {
23
+ const address = '192.168.1.1'
24
+ const bytes = toBytes ( address )
25
+
26
+ expect ( toString ( bytes ) ) . to . equal ( address )
27
+ } )
28
+ } )
29
+ } )
You can’t perform that action at this time.
0 commit comments