ipaddr.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var ipaddr_exports = {};
  19. __export(ipaddr_exports, {
  20. convertIPv4BinaryToString: () => convertIPv4BinaryToString,
  21. convertIPv4MappedIPv6ToIPv4: () => convertIPv4MappedIPv6ToIPv4,
  22. convertIPv4ToBinary: () => convertIPv4ToBinary,
  23. convertIPv6BinaryToString: () => convertIPv6BinaryToString,
  24. convertIPv6ToBinary: () => convertIPv6ToBinary,
  25. distinctRemoteAddr: () => distinctRemoteAddr,
  26. expandIPv6: () => expandIPv6,
  27. isIPv4MappedIPv6: () => isIPv4MappedIPv6
  28. });
  29. module.exports = __toCommonJS(ipaddr_exports);
  30. const expandIPv6 = (ipV6) => {
  31. const sections = ipV6.split(":");
  32. if (IPV4_REGEX.test(sections.at(-1))) {
  33. sections.splice(
  34. -1,
  35. 1,
  36. ...convertIPv6BinaryToString(convertIPv4ToBinary(sections.at(-1))).substring(2).split(":")
  37. // => ['7f00', '0001']
  38. );
  39. }
  40. for (let i = 0; i < sections.length; i++) {
  41. const node = sections[i];
  42. if (node !== "") {
  43. sections[i] = node.padStart(4, "0");
  44. } else {
  45. sections[i + 1] === "" && sections.splice(i + 1, 1);
  46. sections[i] = new Array(8 - sections.length + 1).fill("0000").join(":");
  47. }
  48. }
  49. return sections.join(":");
  50. };
  51. const IPV4_OCTET_PART = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])";
  52. const IPV4_REGEX = new RegExp(`^(?:${IPV4_OCTET_PART}\\.){3}${IPV4_OCTET_PART}$`);
  53. const distinctRemoteAddr = (remoteAddr) => {
  54. if (IPV4_REGEX.test(remoteAddr)) {
  55. return "IPv4";
  56. }
  57. if (remoteAddr.includes(":")) {
  58. return "IPv6";
  59. }
  60. };
  61. const convertIPv4ToBinary = (ipv4) => {
  62. const parts = ipv4.split(".");
  63. let result = 0n;
  64. for (let i = 0; i < 4; i++) {
  65. result <<= 8n;
  66. result += BigInt(parts[i]);
  67. }
  68. return result;
  69. };
  70. const convertIPv6ToBinary = (ipv6) => {
  71. const sections = expandIPv6(ipv6).split(":");
  72. let result = 0n;
  73. for (let i = 0; i < 8; i++) {
  74. result <<= 16n;
  75. result += BigInt(parseInt(sections[i], 16));
  76. }
  77. return result;
  78. };
  79. const convertIPv4BinaryToString = (ipV4) => {
  80. const sections = [];
  81. for (let i = 0; i < 4; i++) {
  82. sections.push(ipV4 >> BigInt(8 * (3 - i)) & 0xffn);
  83. }
  84. return sections.join(".");
  85. };
  86. const isIPv4MappedIPv6 = (ipv6binary) => ipv6binary >> 32n === 0xffffn;
  87. const convertIPv4MappedIPv6ToIPv4 = (ipv6binary) => ipv6binary & 0xffffffffn;
  88. const convertIPv6BinaryToString = (ipV6) => {
  89. if (isIPv4MappedIPv6(ipV6)) {
  90. return `::ffff:${convertIPv4BinaryToString(convertIPv4MappedIPv6ToIPv4(ipV6))}`;
  91. }
  92. const sections = [];
  93. for (let i = 0; i < 8; i++) {
  94. sections.push((ipV6 >> BigInt(16 * (7 - i)) & 0xffffn).toString(16));
  95. }
  96. let currentZeroStart = -1;
  97. let maxZeroStart = -1;
  98. let maxZeroEnd = -1;
  99. for (let i = 0; i < 8; i++) {
  100. if (sections[i] === "0") {
  101. if (currentZeroStart === -1) {
  102. currentZeroStart = i;
  103. }
  104. } else {
  105. if (currentZeroStart > -1) {
  106. if (i - currentZeroStart > maxZeroEnd - maxZeroStart) {
  107. maxZeroStart = currentZeroStart;
  108. maxZeroEnd = i;
  109. }
  110. currentZeroStart = -1;
  111. }
  112. }
  113. }
  114. if (currentZeroStart > -1) {
  115. if (8 - currentZeroStart > maxZeroEnd - maxZeroStart) {
  116. maxZeroStart = currentZeroStart;
  117. maxZeroEnd = 8;
  118. }
  119. }
  120. if (maxZeroStart !== -1) {
  121. sections.splice(maxZeroStart, maxZeroEnd - maxZeroStart, ":");
  122. }
  123. return sections.join(":").replace(/:{2,}/g, "::");
  124. };
  125. // Annotate the CommonJS export names for ESM import in node:
  126. 0 && (module.exports = {
  127. convertIPv4BinaryToString,
  128. convertIPv4MappedIPv6ToIPv4,
  129. convertIPv4ToBinary,
  130. convertIPv6BinaryToString,
  131. convertIPv6ToBinary,
  132. distinctRemoteAddr,
  133. expandIPv6,
  134. isIPv4MappedIPv6
  135. });