UNPKG

@node-lightning/wire

Version:
39 lines 1.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ipv6StringToBuffer = void 0; const bufio_1 = require("@node-lightning/bufio"); /** * Converts an IPv6 address in string notation to the * byte representation. */ function ipv6StringToBuffer(host) { // replace start or end expansion with single part to retain correct // number of parts (8) that are used in the remainder of the logic. // ie: ::1:2:3:4:5:6:7 would split to ['','',1,2,3,4,5,6,7] and // result in overflows if (host.startsWith("::")) host = host.substr(1); else if (host.endsWith("::")) host = host.substr(0, host.length - 1); const parts = host.split(":"); const writer = new bufio_1.BufferWriter(Buffer.alloc(16)); const expandBy = 8 - parts.length; let needsExpansion = expandBy > 0; for (const part of parts) { if (needsExpansion && part === "") { const b = Buffer.alloc((expandBy + 1) * 2); writer.writeBytes(b); needsExpansion = false; } else { const b = Buffer.from(expandZeros(part), "hex"); writer.writeBytes(b); } } return writer.toBuffer(); } exports.ipv6StringToBuffer = ipv6StringToBuffer; function expandZeros(part) { return part.padStart(4, "0"); } //# sourceMappingURL=ipv6StringToBuffer.js.map