ip.js 873 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. // Load modules
  3. const RFC3986 = require('./rfc3986');
  4. // Declare internals
  5. const internals = {
  6. Ip: {
  7. cidrs: {
  8. required: '\\/(?:' + RFC3986.cidr + ')',
  9. optional: '(?:\\/(?:' + RFC3986.cidr + '))?',
  10. forbidden: ''
  11. },
  12. versions: {
  13. ipv4: RFC3986.IPv4address,
  14. ipv6: RFC3986.IPv6address,
  15. ipvfuture: RFC3986.IPvFuture
  16. }
  17. }
  18. };
  19. internals.Ip.createIpRegex = function (versions, cidr) {
  20. let regex;
  21. for (let i = 0; i < versions.length; ++i) {
  22. const version = versions[i];
  23. if (!regex) {
  24. regex = '^(?:' + internals.Ip.versions[version];
  25. }
  26. regex = regex + '|' + internals.Ip.versions[version];
  27. }
  28. return new RegExp(regex + ')' + internals.Ip.cidrs[cidr] + '$');
  29. };
  30. module.exports = internals.Ip;