Files
node-metaverse/dist/classes/IPAddress.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-12-13 15:23:50 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ipaddr = require('ipaddr.js');
class IPAddress {
constructor(buf, pos) {
this.ip = null;
this.toString = () => {
try {
return this.ip.toString();
}
catch (ignore) {
return '';
2017-12-13 15:23:50 +00:00
}
};
try {
if (buf !== undefined && buf instanceof Buffer) {
if (pos !== undefined) {
const bytes = buf.slice(pos, 4);
this.ip = ipaddr.fromByteArray(bytes);
}
else {
if (ipaddr.isValid(buf)) {
this.ip = ipaddr.parse(buf);
}
2017-12-13 15:23:50 +00:00
}
}
}
catch (ignore) {
this.ip = ipaddr.parse('0.0.0.0');
}
2017-12-13 15:23:50 +00:00
}
static zero() {
return new IPAddress('0.0.0.0');
}
writeToBuffer(buf, pos) {
const bytes = this.ip.toByteArray();
buf.writeUInt8(bytes[0], pos++);
buf.writeUInt8(bytes[1], pos++);
buf.writeUInt8(bytes[2], pos++);
buf.writeUInt8(bytes[3], pos);
}
}
exports.IPAddress = IPAddress;
//# sourceMappingURL=IPAddress.js.map