Skip to content

Instantly share code, notes, and snippets.

@danneu
Created October 25, 2016 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danneu/7309f47c816a334b69a2704c1c30c1c8 to your computer and use it in GitHub Desktop.
Save danneu/7309f47c816a334b69a2704c1c30c1c8 to your computer and use it in GitHub Desktop.
// mimic our ip_root sql function
exports.ipRoot = (function () {
const {Address4, Address6} = require('ip-address')
return function (str) {
let ip = new Address6(str)
if (ip.v4 && !ip.valid) {
// it is v4
ip = new Address4(str)
return Address4.fromInteger(Number.parseInt(_.padEnd(ip.mask(24), 32, '0'), 2)).correctForm()
}
// not an ip
if (!ip.valid) return
// must be ipv6
const fakeBigint = Number.parseInt(_.padEnd(ip.mask(48), 128, '0'), 2)
return Address6.fromBigInteger(fakeBigint).correctForm()
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment