Skip to content

Commit 08e6a33

Browse files
authored
Readme tweaks, and move isIP* static methods (#128)
* Update README.md * move isIP static methods
1 parent 337f088 commit 08e6a33

File tree

5 files changed

+57
-37
lines changed

5 files changed

+57
-37
lines changed

README.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ stream2.on('close', function () {
7070

7171
## API
7272

73-
### `const udx = new UDX()`
73+
#### `const udx = new UDX()`
7474

7575
Creates a new UDX instance.
7676

77-
### `const socket = udx.createSocket()`
77+
#### `const bool = UDX.isIPv4(host)`
78+
79+
Returns `true` if host is an IPv4 address.
80+
81+
#### `const bool = UDX.isIPv6(host)`
82+
83+
Returns `true` if host is an IPv6 address.
84+
85+
#### `const family = UDX.isIP(host)`
86+
87+
Returns the address family (`4` or `6`). Returns `0` if invalid.
88+
89+
## Sockets
90+
91+
#### `const socket = udx.createSocket()`
7892

7993
Creates a new socket instance.
8094

@@ -158,7 +172,9 @@ Emitted if the socket becomes busy (at least one active stream).
158172

159173
Emitted after a succesfull `bind()` call.
160174

161-
### `const stream = udx.createStream(id, [options])`
175+
## Streams
176+
177+
#### `const stream = udx.createStream(id, [options])`
162178

163179
Creates a new stream instance that is a Duplex stream.
164180

@@ -274,7 +290,9 @@ Emitted if the stream receives a message.
274290

275291
Emitted only once if you write data that exceeds the MTU.
276292

277-
### `const interfaces = udx.networkInterfaces()`
293+
## Network interfaces
294+
295+
#### `const interfaces = udx.networkInterfaces()`
278296

279297
Returns an array of network interfaces, for example:
280298
```js
@@ -286,7 +304,7 @@ Returns an array of network interfaces, for example:
286304
]
287305
```
288306

289-
### `const watcher = udx.watchNetworkInterfaces([onchange])`
307+
#### `const watcher = udx.watchNetworkInterfaces([onchange])`
290308

291309
Listens to changes in the network interfaces. The `watcher` object is iterable.
292310

@@ -314,7 +332,9 @@ Emitted after a network interface change.
314332

315333
Emitted after the watcher is closed.
316334

317-
### `const address = udx.lookup(host, [options])`
335+
## DNS
336+
337+
#### `const address = await udx.lookup(host, [options])`
318338

319339
It does a DNS lookup for the IP address. Returns `{ host, family }`.
320340

lib/socket.js

-12
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ module.exports = class UDXSocket extends events.EventEmitter {
2424
this.userData = null
2525
}
2626

27-
static isIPv4 (host) {
28-
return ip.isIPv4(host)
29-
}
30-
31-
static isIPv6 (host) {
32-
return ip.isIPv6(host)
33-
}
34-
35-
static isIP (host) {
36-
return ip.isIP(host)
37-
}
38-
3927
get bound () {
4028
return this._port !== 0
4129
}

lib/udx.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const b4a = require('b4a')
22
const binding = require('./binding')
3+
const ip = require('./ip')
34
const Socket = require('./socket')
45
const Stream = require('./stream')
56
const NetworkInterfaces = require('./network-interfaces')
@@ -18,6 +19,18 @@ module.exports = class UDX {
1819
binding.udx_napi_init(this._handle, this._buffer)
1920
}
2021

22+
static isIPv4 (host) {
23+
return ip.isIPv4(host)
24+
}
25+
26+
static isIPv6 (host) {
27+
return ip.isIPv6(host)
28+
}
29+
30+
static isIP (host) {
31+
return ip.isIP(host)
32+
}
33+
2134
_consumeMessage (len) {
2235
const next = this._buffer.subarray(0, len)
2336
this._buffer = this._buffer.subarray(len)

test/socket.js

-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const test = require('brittle')
22
const b4a = require('b4a')
33
const UDX = require('../')
4-
const UDXSocket = require('../lib/socket')
54

65
test('can bind and close', async function (t) {
76
const u = new UDX()
@@ -648,21 +647,3 @@ test('set send buffer size', async function (t) {
648647

649648
await a.close()
650649
})
651-
652-
test('UDXSocket - isIPv4', function (t) {
653-
t.is(UDXSocket.isIPv4('127.0.0.1'), true)
654-
t.is(UDXSocket.isIPv4('::1'), false)
655-
t.is(UDXSocket.isIPv4('0.-1.0.0'), false)
656-
})
657-
658-
test('UDXSocket - isIPv6', function (t) {
659-
t.is(UDXSocket.isIPv6('127.0.0.1'), false)
660-
t.is(UDXSocket.isIPv6('::1'), true)
661-
t.is(UDXSocket.isIPv6('0.-1.0.0'), false)
662-
})
663-
664-
test('UDXSocket - isIP', function (t) {
665-
t.is(UDXSocket.isIP('127.0.0.1'), 4)
666-
t.is(UDXSocket.isIP('::1'), 6)
667-
t.is(UDXSocket.isIP('0.-1.0.0'), 0)
668-
})

test/udx.js

+18
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,21 @@ test('network interfaces - watch, unwatch and destroy twice', async function (t)
6565
t.pass()
6666
})
6767
})
68+
69+
test('UDX - isIPv4', function (t) {
70+
t.is(UDX.isIPv4('127.0.0.1'), true)
71+
t.is(UDX.isIPv4('::1'), false)
72+
t.is(UDX.isIPv4('0.-1.0.0'), false)
73+
})
74+
75+
test('UDX - isIPv6', function (t) {
76+
t.is(UDX.isIPv6('127.0.0.1'), false)
77+
t.is(UDX.isIPv6('::1'), true)
78+
t.is(UDX.isIPv6('0.-1.0.0'), false)
79+
})
80+
81+
test('UDX - isIP', function (t) {
82+
t.is(UDX.isIP('127.0.0.1'), 4)
83+
t.is(UDX.isIP('::1'), 6)
84+
t.is(UDX.isIP('0.-1.0.0'), 0)
85+
})

0 commit comments

Comments
 (0)