ipaddr.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. (function() {
  2. var expandIPv6, ipaddr, ipv4Part, ipv4Regexes, ipv6Part, ipv6Regexes, matchCIDR, root, zoneIndex;
  3. ipaddr = {};
  4. root = this;
  5. if ((typeof module !== "undefined" && module !== null) && module.exports) {
  6. module.exports = ipaddr;
  7. } else {
  8. root['ipaddr'] = ipaddr;
  9. }
  10. matchCIDR = function(first, second, partSize, cidrBits) {
  11. var part, shift;
  12. if (first.length !== second.length) {
  13. throw new Error("ipaddr: cannot match CIDR for objects with different lengths");
  14. }
  15. part = 0;
  16. while (cidrBits > 0) {
  17. shift = partSize - cidrBits;
  18. if (shift < 0) {
  19. shift = 0;
  20. }
  21. if (first[part] >> shift !== second[part] >> shift) {
  22. return false;
  23. }
  24. cidrBits -= partSize;
  25. part += 1;
  26. }
  27. return true;
  28. };
  29. ipaddr.subnetMatch = function(address, rangeList, defaultName) {
  30. var k, len, rangeName, rangeSubnets, subnet;
  31. if (defaultName == null) {
  32. defaultName = 'unicast';
  33. }
  34. for (rangeName in rangeList) {
  35. rangeSubnets = rangeList[rangeName];
  36. if (rangeSubnets[0] && !(rangeSubnets[0] instanceof Array)) {
  37. rangeSubnets = [rangeSubnets];
  38. }
  39. for (k = 0, len = rangeSubnets.length; k < len; k++) {
  40. subnet = rangeSubnets[k];
  41. if (address.kind() === subnet[0].kind()) {
  42. if (address.match.apply(address, subnet)) {
  43. return rangeName;
  44. }
  45. }
  46. }
  47. }
  48. return defaultName;
  49. };
  50. ipaddr.IPv4 = (function() {
  51. function IPv4(octets) {
  52. var k, len, octet;
  53. if (octets.length !== 4) {
  54. throw new Error("ipaddr: ipv4 octet count should be 4");
  55. }
  56. for (k = 0, len = octets.length; k < len; k++) {
  57. octet = octets[k];
  58. if (!((0 <= octet && octet <= 255))) {
  59. throw new Error("ipaddr: ipv4 octet should fit in 8 bits");
  60. }
  61. }
  62. this.octets = octets;
  63. }
  64. IPv4.prototype.kind = function() {
  65. return 'ipv4';
  66. };
  67. IPv4.prototype.toString = function() {
  68. return this.octets.join(".");
  69. };
  70. IPv4.prototype.toNormalizedString = function() {
  71. return this.toString();
  72. };
  73. IPv4.prototype.toByteArray = function() {
  74. return this.octets.slice(0);
  75. };
  76. IPv4.prototype.match = function(other, cidrRange) {
  77. var ref;
  78. if (cidrRange === void 0) {
  79. ref = other, other = ref[0], cidrRange = ref[1];
  80. }
  81. if (other.kind() !== 'ipv4') {
  82. throw new Error("ipaddr: cannot match ipv4 address with non-ipv4 one");
  83. }
  84. return matchCIDR(this.octets, other.octets, 8, cidrRange);
  85. };
  86. IPv4.prototype.SpecialRanges = {
  87. unspecified: [[new IPv4([0, 0, 0, 0]), 8]],
  88. broadcast: [[new IPv4([255, 255, 255, 255]), 32]],
  89. multicast: [[new IPv4([224, 0, 0, 0]), 4]],
  90. linkLocal: [[new IPv4([169, 254, 0, 0]), 16]],
  91. loopback: [[new IPv4([127, 0, 0, 0]), 8]],
  92. carrierGradeNat: [[new IPv4([100, 64, 0, 0]), 10]],
  93. "private": [[new IPv4([10, 0, 0, 0]), 8], [new IPv4([172, 16, 0, 0]), 12], [new IPv4([192, 168, 0, 0]), 16]],
  94. reserved: [[new IPv4([192, 0, 0, 0]), 24], [new IPv4([192, 0, 2, 0]), 24], [new IPv4([192, 88, 99, 0]), 24], [new IPv4([198, 51, 100, 0]), 24], [new IPv4([203, 0, 113, 0]), 24], [new IPv4([240, 0, 0, 0]), 4]]
  95. };
  96. IPv4.prototype.range = function() {
  97. return ipaddr.subnetMatch(this, this.SpecialRanges);
  98. };
  99. IPv4.prototype.toIPv4MappedAddress = function() {
  100. return ipaddr.IPv6.parse("::ffff:" + (this.toString()));
  101. };
  102. IPv4.prototype.prefixLengthFromSubnetMask = function() {
  103. var cidr, i, k, octet, stop, zeros, zerotable;
  104. zerotable = {
  105. 0: 8,
  106. 128: 7,
  107. 192: 6,
  108. 224: 5,
  109. 240: 4,
  110. 248: 3,
  111. 252: 2,
  112. 254: 1,
  113. 255: 0
  114. };
  115. cidr = 0;
  116. stop = false;
  117. for (i = k = 3; k >= 0; i = k += -1) {
  118. octet = this.octets[i];
  119. if (octet in zerotable) {
  120. zeros = zerotable[octet];
  121. if (stop && zeros !== 0) {
  122. return null;
  123. }
  124. if (zeros !== 8) {
  125. stop = true;
  126. }
  127. cidr += zeros;
  128. } else {
  129. return null;
  130. }
  131. }
  132. return 32 - cidr;
  133. };
  134. return IPv4;
  135. })();
  136. ipv4Part = "(0?\\d+|0x[a-f0-9]+)";
  137. ipv4Regexes = {
  138. fourOctet: new RegExp("^" + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "$", 'i'),
  139. longValue: new RegExp("^" + ipv4Part + "$", 'i')
  140. };
  141. ipaddr.IPv4.parser = function(string) {
  142. var match, parseIntAuto, part, shift, value;
  143. parseIntAuto = function(string) {
  144. if (string[0] === "0" && string[1] !== "x") {
  145. return parseInt(string, 8);
  146. } else {
  147. return parseInt(string);
  148. }
  149. };
  150. if (match = string.match(ipv4Regexes.fourOctet)) {
  151. return (function() {
  152. var k, len, ref, results;
  153. ref = match.slice(1, 6);
  154. results = [];
  155. for (k = 0, len = ref.length; k < len; k++) {
  156. part = ref[k];
  157. results.push(parseIntAuto(part));
  158. }
  159. return results;
  160. })();
  161. } else if (match = string.match(ipv4Regexes.longValue)) {
  162. value = parseIntAuto(match[1]);
  163. if (value > 0xffffffff || value < 0) {
  164. throw new Error("ipaddr: address outside defined range");
  165. }
  166. return ((function() {
  167. var k, results;
  168. results = [];
  169. for (shift = k = 0; k <= 24; shift = k += 8) {
  170. results.push((value >> shift) & 0xff);
  171. }
  172. return results;
  173. })()).reverse();
  174. } else {
  175. return null;
  176. }
  177. };
  178. ipaddr.IPv6 = (function() {
  179. function IPv6(parts, zoneId) {
  180. var i, k, l, len, part, ref;
  181. if (parts.length === 16) {
  182. this.parts = [];
  183. for (i = k = 0; k <= 14; i = k += 2) {
  184. this.parts.push((parts[i] << 8) | parts[i + 1]);
  185. }
  186. } else if (parts.length === 8) {
  187. this.parts = parts;
  188. } else {
  189. throw new Error("ipaddr: ipv6 part count should be 8 or 16");
  190. }
  191. ref = this.parts;
  192. for (l = 0, len = ref.length; l < len; l++) {
  193. part = ref[l];
  194. if (!((0 <= part && part <= 0xffff))) {
  195. throw new Error("ipaddr: ipv6 part should fit in 16 bits");
  196. }
  197. }
  198. if (zoneId) {
  199. this.zoneId = zoneId;
  200. }
  201. }
  202. IPv6.prototype.kind = function() {
  203. return 'ipv6';
  204. };
  205. IPv6.prototype.toString = function() {
  206. return this.toNormalizedString().replace(/((^|:)(0(:|$))+)/, '::');
  207. };
  208. IPv6.prototype.toRFC5952String = function() {
  209. var bestMatchIndex, bestMatchLength, match, regex, string;
  210. regex = /((^|:)(0(:|$)){2,})/g;
  211. string = this.toNormalizedString();
  212. bestMatchIndex = 0;
  213. bestMatchLength = -1;
  214. while ((match = regex.exec(string))) {
  215. if (match[0].length > bestMatchLength) {
  216. bestMatchIndex = match.index;
  217. bestMatchLength = match[0].length;
  218. }
  219. }
  220. if (bestMatchLength < 0) {
  221. return string;
  222. }
  223. return string.substring(0, bestMatchIndex) + '::' + string.substring(bestMatchIndex + bestMatchLength);
  224. };
  225. IPv6.prototype.toByteArray = function() {
  226. var bytes, k, len, part, ref;
  227. bytes = [];
  228. ref = this.parts;
  229. for (k = 0, len = ref.length; k < len; k++) {
  230. part = ref[k];
  231. bytes.push(part >> 8);
  232. bytes.push(part & 0xff);
  233. }
  234. return bytes;
  235. };
  236. IPv6.prototype.toNormalizedString = function() {
  237. var addr, part, suffix;
  238. addr = ((function() {
  239. var k, len, ref, results;
  240. ref = this.parts;
  241. results = [];
  242. for (k = 0, len = ref.length; k < len; k++) {
  243. part = ref[k];
  244. results.push(part.toString(16));
  245. }
  246. return results;
  247. }).call(this)).join(":");
  248. suffix = '';
  249. if (this.zoneId) {
  250. suffix = '%' + this.zoneId;
  251. }
  252. return addr + suffix;
  253. };
  254. IPv6.prototype.toFixedLengthString = function() {
  255. var addr, part, suffix;
  256. addr = ((function() {
  257. var k, len, ref, results;
  258. ref = this.parts;
  259. results = [];
  260. for (k = 0, len = ref.length; k < len; k++) {
  261. part = ref[k];
  262. results.push(part.toString(16).padStart(4, '0'));
  263. }
  264. return results;
  265. }).call(this)).join(":");
  266. suffix = '';
  267. if (this.zoneId) {
  268. suffix = '%' + this.zoneId;
  269. }
  270. return addr + suffix;
  271. };
  272. IPv6.prototype.match = function(other, cidrRange) {
  273. var ref;
  274. if (cidrRange === void 0) {
  275. ref = other, other = ref[0], cidrRange = ref[1];
  276. }
  277. if (other.kind() !== 'ipv6') {
  278. throw new Error("ipaddr: cannot match ipv6 address with non-ipv6 one");
  279. }
  280. return matchCIDR(this.parts, other.parts, 16, cidrRange);
  281. };
  282. IPv6.prototype.SpecialRanges = {
  283. unspecified: [new IPv6([0, 0, 0, 0, 0, 0, 0, 0]), 128],
  284. linkLocal: [new IPv6([0xfe80, 0, 0, 0, 0, 0, 0, 0]), 10],
  285. multicast: [new IPv6([0xff00, 0, 0, 0, 0, 0, 0, 0]), 8],
  286. loopback: [new IPv6([0, 0, 0, 0, 0, 0, 0, 1]), 128],
  287. uniqueLocal: [new IPv6([0xfc00, 0, 0, 0, 0, 0, 0, 0]), 7],
  288. ipv4Mapped: [new IPv6([0, 0, 0, 0, 0, 0xffff, 0, 0]), 96],
  289. rfc6145: [new IPv6([0, 0, 0, 0, 0xffff, 0, 0, 0]), 96],
  290. rfc6052: [new IPv6([0x64, 0xff9b, 0, 0, 0, 0, 0, 0]), 96],
  291. '6to4': [new IPv6([0x2002, 0, 0, 0, 0, 0, 0, 0]), 16],
  292. teredo: [new IPv6([0x2001, 0, 0, 0, 0, 0, 0, 0]), 32],
  293. reserved: [[new IPv6([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0]), 32]]
  294. };
  295. IPv6.prototype.range = function() {
  296. return ipaddr.subnetMatch(this, this.SpecialRanges);
  297. };
  298. IPv6.prototype.isIPv4MappedAddress = function() {
  299. return this.range() === 'ipv4Mapped';
  300. };
  301. IPv6.prototype.toIPv4Address = function() {
  302. var high, low, ref;
  303. if (!this.isIPv4MappedAddress()) {
  304. throw new Error("ipaddr: trying to convert a generic ipv6 address to ipv4");
  305. }
  306. ref = this.parts.slice(-2), high = ref[0], low = ref[1];
  307. return new ipaddr.IPv4([high >> 8, high & 0xff, low >> 8, low & 0xff]);
  308. };
  309. IPv6.prototype.prefixLengthFromSubnetMask = function() {
  310. var cidr, i, k, part, stop, zeros, zerotable;
  311. zerotable = {
  312. 0: 16,
  313. 32768: 15,
  314. 49152: 14,
  315. 57344: 13,
  316. 61440: 12,
  317. 63488: 11,
  318. 64512: 10,
  319. 65024: 9,
  320. 65280: 8,
  321. 65408: 7,
  322. 65472: 6,
  323. 65504: 5,
  324. 65520: 4,
  325. 65528: 3,
  326. 65532: 2,
  327. 65534: 1,
  328. 65535: 0
  329. };
  330. cidr = 0;
  331. stop = false;
  332. for (i = k = 7; k >= 0; i = k += -1) {
  333. part = this.parts[i];
  334. if (part in zerotable) {
  335. zeros = zerotable[part];
  336. if (stop && zeros !== 0) {
  337. return null;
  338. }
  339. if (zeros !== 16) {
  340. stop = true;
  341. }
  342. cidr += zeros;
  343. } else {
  344. return null;
  345. }
  346. }
  347. return 128 - cidr;
  348. };
  349. return IPv6;
  350. })();
  351. ipv6Part = "(?:[0-9a-f]+::?)+";
  352. zoneIndex = "%[0-9a-z]{1,}";
  353. ipv6Regexes = {
  354. zoneIndex: new RegExp(zoneIndex, 'i'),
  355. "native": new RegExp("^(::)?(" + ipv6Part + ")?([0-9a-f]+)?(::)?(" + zoneIndex + ")?$", 'i'),
  356. transitional: new RegExp(("^((?:" + ipv6Part + ")|(?:::)(?:" + ipv6Part + ")?)") + (ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part + "\\." + ipv4Part) + ("(" + zoneIndex + ")?$"), 'i')
  357. };
  358. expandIPv6 = function(string, parts) {
  359. var colonCount, lastColon, part, replacement, replacementCount, zoneId;
  360. if (string.indexOf('::') !== string.lastIndexOf('::')) {
  361. return null;
  362. }
  363. zoneId = (string.match(ipv6Regexes['zoneIndex']) || [])[0];
  364. if (zoneId) {
  365. zoneId = zoneId.substring(1);
  366. string = string.replace(/%.+$/, '');
  367. }
  368. colonCount = 0;
  369. lastColon = -1;
  370. while ((lastColon = string.indexOf(':', lastColon + 1)) >= 0) {
  371. colonCount++;
  372. }
  373. if (string.substr(0, 2) === '::') {
  374. colonCount--;
  375. }
  376. if (string.substr(-2, 2) === '::') {
  377. colonCount--;
  378. }
  379. if (colonCount > parts) {
  380. return null;
  381. }
  382. replacementCount = parts - colonCount;
  383. replacement = ':';
  384. while (replacementCount--) {
  385. replacement += '0:';
  386. }
  387. string = string.replace('::', replacement);
  388. if (string[0] === ':') {
  389. string = string.slice(1);
  390. }
  391. if (string[string.length - 1] === ':') {
  392. string = string.slice(0, -1);
  393. }
  394. parts = (function() {
  395. var k, len, ref, results;
  396. ref = string.split(":");
  397. results = [];
  398. for (k = 0, len = ref.length; k < len; k++) {
  399. part = ref[k];
  400. results.push(parseInt(part, 16));
  401. }
  402. return results;
  403. })();
  404. return {
  405. parts: parts,
  406. zoneId: zoneId
  407. };
  408. };
  409. ipaddr.IPv6.parser = function(string) {
  410. var addr, k, len, match, octet, octets, zoneId;
  411. if (ipv6Regexes['native'].test(string)) {
  412. return expandIPv6(string, 8);
  413. } else if (match = string.match(ipv6Regexes['transitional'])) {
  414. zoneId = match[6] || '';
  415. addr = expandIPv6(match[1].slice(0, -1) + zoneId, 6);
  416. if (addr.parts) {
  417. octets = [parseInt(match[2]), parseInt(match[3]), parseInt(match[4]), parseInt(match[5])];
  418. for (k = 0, len = octets.length; k < len; k++) {
  419. octet = octets[k];
  420. if (!((0 <= octet && octet <= 255))) {
  421. return null;
  422. }
  423. }
  424. addr.parts.push(octets[0] << 8 | octets[1]);
  425. addr.parts.push(octets[2] << 8 | octets[3]);
  426. return {
  427. parts: addr.parts,
  428. zoneId: addr.zoneId
  429. };
  430. }
  431. }
  432. return null;
  433. };
  434. ipaddr.IPv4.isIPv4 = ipaddr.IPv6.isIPv6 = function(string) {
  435. return this.parser(string) !== null;
  436. };
  437. ipaddr.IPv4.isValid = function(string) {
  438. var e;
  439. try {
  440. new this(this.parser(string));
  441. return true;
  442. } catch (error1) {
  443. e = error1;
  444. return false;
  445. }
  446. };
  447. ipaddr.IPv4.isValidFourPartDecimal = function(string) {
  448. if (ipaddr.IPv4.isValid(string) && string.match(/^(0|[1-9]\d*)(\.(0|[1-9]\d*)){3}$/)) {
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. };
  454. ipaddr.IPv6.isValid = function(string) {
  455. var addr, e;
  456. if (typeof string === "string" && string.indexOf(":") === -1) {
  457. return false;
  458. }
  459. try {
  460. addr = this.parser(string);
  461. new this(addr.parts, addr.zoneId);
  462. return true;
  463. } catch (error1) {
  464. e = error1;
  465. return false;
  466. }
  467. };
  468. ipaddr.IPv4.parse = function(string) {
  469. var parts;
  470. parts = this.parser(string);
  471. if (parts === null) {
  472. throw new Error("ipaddr: string is not formatted like ip address");
  473. }
  474. return new this(parts);
  475. };
  476. ipaddr.IPv6.parse = function(string) {
  477. var addr;
  478. addr = this.parser(string);
  479. if (addr.parts === null) {
  480. throw new Error("ipaddr: string is not formatted like ip address");
  481. }
  482. return new this(addr.parts, addr.zoneId);
  483. };
  484. ipaddr.IPv4.parseCIDR = function(string) {
  485. var maskLength, match, parsed;
  486. if (match = string.match(/^(.+)\/(\d+)$/)) {
  487. maskLength = parseInt(match[2]);
  488. if (maskLength >= 0 && maskLength <= 32) {
  489. parsed = [this.parse(match[1]), maskLength];
  490. Object.defineProperty(parsed, 'toString', {
  491. value: function() {
  492. return this.join('/');
  493. }
  494. });
  495. return parsed;
  496. }
  497. }
  498. throw new Error("ipaddr: string is not formatted like an IPv4 CIDR range");
  499. };
  500. ipaddr.IPv4.subnetMaskFromPrefixLength = function(prefix) {
  501. var filledOctetCount, j, octets;
  502. prefix = parseInt(prefix);
  503. if (prefix < 0 || prefix > 32) {
  504. throw new Error('ipaddr: invalid IPv4 prefix length');
  505. }
  506. octets = [0, 0, 0, 0];
  507. j = 0;
  508. filledOctetCount = Math.floor(prefix / 8);
  509. while (j < filledOctetCount) {
  510. octets[j] = 255;
  511. j++;
  512. }
  513. if (filledOctetCount < 4) {
  514. octets[filledOctetCount] = Math.pow(2, prefix % 8) - 1 << 8 - (prefix % 8);
  515. }
  516. return new this(octets);
  517. };
  518. ipaddr.IPv4.broadcastAddressFromCIDR = function(string) {
  519. var cidr, error, i, ipInterfaceOctets, octets, subnetMaskOctets;
  520. try {
  521. cidr = this.parseCIDR(string);
  522. ipInterfaceOctets = cidr[0].toByteArray();
  523. subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
  524. octets = [];
  525. i = 0;
  526. while (i < 4) {
  527. octets.push(parseInt(ipInterfaceOctets[i], 10) | parseInt(subnetMaskOctets[i], 10) ^ 255);
  528. i++;
  529. }
  530. return new this(octets);
  531. } catch (error1) {
  532. error = error1;
  533. throw new Error('ipaddr: the address does not have IPv4 CIDR format');
  534. }
  535. };
  536. ipaddr.IPv4.networkAddressFromCIDR = function(string) {
  537. var cidr, error, i, ipInterfaceOctets, octets, subnetMaskOctets;
  538. try {
  539. cidr = this.parseCIDR(string);
  540. ipInterfaceOctets = cidr[0].toByteArray();
  541. subnetMaskOctets = this.subnetMaskFromPrefixLength(cidr[1]).toByteArray();
  542. octets = [];
  543. i = 0;
  544. while (i < 4) {
  545. octets.push(parseInt(ipInterfaceOctets[i], 10) & parseInt(subnetMaskOctets[i], 10));
  546. i++;
  547. }
  548. return new this(octets);
  549. } catch (error1) {
  550. error = error1;
  551. throw new Error('ipaddr: the address does not have IPv4 CIDR format');
  552. }
  553. };
  554. ipaddr.IPv6.parseCIDR = function(string) {
  555. var maskLength, match, parsed;
  556. if (match = string.match(/^(.+)\/(\d+)$/)) {
  557. maskLength = parseInt(match[2]);
  558. if (maskLength >= 0 && maskLength <= 128) {
  559. parsed = [this.parse(match[1]), maskLength];
  560. Object.defineProperty(parsed, 'toString', {
  561. value: function() {
  562. return this.join('/');
  563. }
  564. });
  565. return parsed;
  566. }
  567. }
  568. throw new Error("ipaddr: string is not formatted like an IPv6 CIDR range");
  569. };
  570. ipaddr.isValid = function(string) {
  571. return ipaddr.IPv6.isValid(string) || ipaddr.IPv4.isValid(string);
  572. };
  573. ipaddr.parse = function(string) {
  574. if (ipaddr.IPv6.isValid(string)) {
  575. return ipaddr.IPv6.parse(string);
  576. } else if (ipaddr.IPv4.isValid(string)) {
  577. return ipaddr.IPv4.parse(string);
  578. } else {
  579. throw new Error("ipaddr: the address has neither IPv6 nor IPv4 format");
  580. }
  581. };
  582. ipaddr.parseCIDR = function(string) {
  583. var e;
  584. try {
  585. return ipaddr.IPv6.parseCIDR(string);
  586. } catch (error1) {
  587. e = error1;
  588. try {
  589. return ipaddr.IPv4.parseCIDR(string);
  590. } catch (error1) {
  591. e = error1;
  592. throw new Error("ipaddr: the address has neither IPv6 nor IPv4 CIDR format");
  593. }
  594. }
  595. };
  596. ipaddr.fromByteArray = function(bytes) {
  597. var length;
  598. length = bytes.length;
  599. if (length === 4) {
  600. return new ipaddr.IPv4(bytes);
  601. } else if (length === 16) {
  602. return new ipaddr.IPv6(bytes);
  603. } else {
  604. throw new Error("ipaddr: the binary input is neither an IPv6 nor IPv4 address");
  605. }
  606. };
  607. ipaddr.process = function(string) {
  608. var addr;
  609. addr = this.parse(string);
  610. if (addr.kind() === 'ipv6' && addr.isIPv4MappedAddress()) {
  611. return addr.toIPv4Address();
  612. } else {
  613. return addr;
  614. }
  615. };
  616. }).call(this);