compact-encoding-net
Advanced tools
Comparing version 1.0.1 to 1.1.0
153
index.js
@@ -28,3 +28,21 @@ const c = require('compact-encoding') | ||
}, | ||
encode: encodeIPv4, | ||
encode (state, string) { | ||
const start = state.start | ||
const end = start + 4 | ||
let i = 0 | ||
while (i < string.length) { | ||
let n = 0 | ||
let c | ||
while (i < string.length && (c = string.charCodeAt(i++)) !== /* . */ 0x2e) { | ||
n = n * 10 + (c - /* 0 */ 0x30) | ||
} | ||
state.buffer[state.start++] = n | ||
} | ||
state.start = end | ||
}, | ||
decode (state) { | ||
@@ -47,3 +65,37 @@ if (state.end - state.start < 4) throw new Error('Out of bounds') | ||
}, | ||
encode: encodeIPv6, | ||
encode (state, string) { | ||
const start = state.start | ||
const end = start + 16 | ||
let i = 0 | ||
let split = null | ||
while (i < string.length) { | ||
let n = 0 | ||
let c | ||
while (i < string.length && (c = string.charCodeAt(i++)) !== /* : */ 0x3a) { | ||
if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30) | ||
else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10) | ||
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10) | ||
} | ||
state.buffer[state.start++] = n >>> 8 | ||
state.buffer[state.start++] = n | ||
if (i < string.length && string.charCodeAt(i) === /* : */ 0x3a) { | ||
i++ | ||
split = state.start | ||
} | ||
} | ||
if (split !== null) { | ||
const offset = end - state.start | ||
state.buffer | ||
.copyWithin(split + offset, split) | ||
.fill(0, split, split + offset) | ||
} | ||
state.start = end | ||
}, | ||
decode (state) { | ||
@@ -66,64 +118,49 @@ if (state.end - state.start < 16) throw new Error('Out of bounds') | ||
module.exports = { | ||
port, | ||
ipv4, | ||
ipv4Address, | ||
ipv6, | ||
ipv6Address | ||
const ip = { | ||
preencode (state, string) { | ||
const family = string.includes(':') ? 6 : 4 | ||
c.uint8.preencode(state, family) | ||
if (family === 4) ipv4.preencode(state) | ||
else ipv6.preencode(state) | ||
}, | ||
encode (state, string) { | ||
const family = string.includes(':') ? 6 : 4 | ||
c.uint8.encode(state, family) | ||
if (family === 4) ipv4.encode(state, string) | ||
else ipv6.encode(state, string) | ||
}, | ||
decode (state) { | ||
const family = c.uint8.decode(state) | ||
if (family === 4) return ipv4.decode(state) | ||
else return ipv6.decode(state) | ||
} | ||
} | ||
function encodeIPv4 (state, string) { | ||
const start = state.start | ||
const end = start + 4 | ||
let i = 0 | ||
while (i < string.length) { | ||
let n = 0 | ||
let c | ||
while (i < string.length && (c = string.charCodeAt(i++)) !== /* . */ 0x2e) { | ||
n = n * 10 + (c - /* 0 */ 0x30) | ||
const ipAddress = { | ||
preencode (state, m) { | ||
ip.preencode(state, m.host) | ||
port.preencode(state, m.port) | ||
}, | ||
encode (state, m) { | ||
ip.encode(state, m.host) | ||
port.encode(state, m.port) | ||
}, | ||
decode (state) { | ||
const family = c.uint8.decode(state) | ||
return { | ||
host: family === 4 ? ipv4.decode(state) : ipv6.decode(state), | ||
family, | ||
port: port.decode(state) | ||
} | ||
state.buffer[state.start++] = n | ||
} | ||
state.start = end | ||
} | ||
function encodeIPv6 (state, string) { | ||
const start = state.start | ||
const end = start + 16 | ||
let i = 0 | ||
let split = null | ||
while (i < string.length) { | ||
let n = 0 | ||
let c | ||
while (i < string.length && (c = string.charCodeAt(i++)) !== /* : */ 0x3a) { | ||
if (c >= 0x30 && c <= 0x39) n = n * 0x10 + (c - /* 0 */ 0x30) | ||
else if (c >= 0x41 && c <= 0x46) n = n * 0x10 + (c - /* A */ 0x41 + 10) | ||
else if (c >= 0x61 && c <= 0x66) n = n * 0x10 + (c - /* a */ 0x61 + 10) | ||
} | ||
state.buffer[state.start++] = n >>> 8 | ||
state.buffer[state.start++] = n | ||
if (i < string.length && string.charCodeAt(i) === /* : */ 0x3a) { | ||
i++ | ||
split = state.start | ||
} | ||
} | ||
if (split !== null) { | ||
const offset = end - state.start | ||
state.buffer | ||
.copyWithin(split + offset, split) | ||
.fill(0, split, split + offset) | ||
} | ||
state.start = end | ||
module.exports = { | ||
port, | ||
ipv4, | ||
ipv4Address, | ||
ipv6, | ||
ipv6Address, | ||
ip, | ||
ipAddress | ||
} |
{ | ||
"name": "compact-encoding-net", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Compact codecs for net types", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -122,4 +122,48 @@ # compact-encoding-net | ||
### `ip` | ||
Codec for dual IPv4/6 addresses. | ||
> :warning: The codec is only defined for valid IPv4 and IPv6 addresses. | ||
```js | ||
const { ip } = require('compact-encoding-net') | ||
``` | ||
#### Encoding | ||
```js | ||
const buffer = cenc.encode(ip, '::1') | ||
``` | ||
#### Decoding | ||
```js | ||
cenc.decode(ip, buffer) | ||
// '0:0:0:0:0:0:0:1' | ||
``` | ||
### `ipAddress` | ||
Codec for dual IPv4/6 addresses plus a port. | ||
```js | ||
const { ipAddress } = require('compact-encoding-net') | ||
``` | ||
#### Encoding | ||
```js | ||
const buffer = cenc.encode(ipAddress, { host: '::1', port: 8080 }) | ||
``` | ||
#### Decoding | ||
```js | ||
cenc.decode(ipv6Address, buffer) | ||
// { host: '0:0:0:0:0:0:0:1', family: 6, port: 8080 } | ||
``` | ||
## License | ||
ISC |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
11541
268
169
1