智慧水务管理系统 - 精河县供水工程综合管理平台

uri-utils.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.isDomainLabelStartChar = exports.isSchemeStartChar = exports.tldUrlHostRe = exports.schemeUrlRe = exports.invalidSchemeRe = exports.httpSchemePrefixRe = exports.httpSchemeRe = void 0;
  4. exports.isSchemeChar = isSchemeChar;
  5. exports.isDomainLabelChar = isDomainLabelChar;
  6. exports.isPathChar = isPathChar;
  7. exports.isUrlSuffixStartChar = isUrlSuffixStartChar;
  8. exports.isKnownTld = isKnownTld;
  9. exports.isValidSchemeUrl = isValidSchemeUrl;
  10. exports.isValidTldMatch = isValidTldMatch;
  11. exports.isValidIpV4Address = isValidIpV4Address;
  12. var char_utils_1 = require("../char-utils");
  13. var known_tlds_1 = require("./known-tlds");
  14. /**
  15. * Regular expression to match an http:// or https:// scheme.
  16. */
  17. exports.httpSchemeRe = /https?:\/\//i;
  18. /**
  19. * Regular expression to match an http:// or https:// scheme as the prefix of
  20. * a string.
  21. */
  22. exports.httpSchemePrefixRe = new RegExp('^' + exports.httpSchemeRe.source, 'i');
  23. /**
  24. * A regular expression used to determine the schemes we should not autolink
  25. */
  26. exports.invalidSchemeRe = /^(javascript|vbscript):/i;
  27. // A regular expression used to determine if the URL is a scheme match (such as
  28. // 'http://google.com', and as opposed to a "TLD match"). This regular
  29. // expression is used to parse out the host along with if the URL has an
  30. // authority component (i.e. '//')
  31. //
  32. // Capturing groups:
  33. // 1. '//' if the URL has an authority component, empty string otherwise
  34. // 2. The host (if one exists). Ex: 'google.com'
  35. //
  36. // See https://www.rfc-editor.org/rfc/rfc3986#appendix-A for terminology
  37. exports.schemeUrlRe = /^[A-Za-z][-.+A-Za-z0-9]*:(\/\/)?([^:/]*)/;
  38. // A regular expression used to determine if the URL is a TLD match (such as
  39. // 'google.com', and as opposed to a "scheme match"). This regular
  40. // expression is used to help parse out the TLD (top-level domain) of the host.
  41. //
  42. // See https://www.rfc-editor.org/rfc/rfc3986#appendix-A for terminology
  43. exports.tldUrlHostRe = /^(?:\/\/)?([^/#?:]+)/; // optionally prefixed with protocol-relative '//' chars
  44. /**
  45. * Determines if the given character code represents a character that may start
  46. * a scheme (ex: the 'h' in 'http')
  47. */
  48. exports.isSchemeStartChar = char_utils_1.isAsciiLetterChar; // Equivalent to checking the RegExp `/[A-Za-z]/`, but aliased for clarity and maintainability
  49. /**
  50. * Determines if the given character is a valid character in a scheme (such as
  51. * 'http' or 'ssh+git'), but only after the start char (which is handled by
  52. * {@link isSchemeStartChar}.
  53. */
  54. function isSchemeChar(charCode) {
  55. return ((0, char_utils_1.isAsciiLetterChar)(charCode) ||
  56. (0, char_utils_1.isDigitChar)(charCode) ||
  57. charCode === 43 /* Char.Plus */ || // '+'
  58. charCode === 45 /* Char.Dash */ || // '-'
  59. charCode === 46 /* Char.Dot */ // '.'
  60. );
  61. }
  62. /**
  63. * Determines if the character can begin a domain label, which must be an
  64. * alphanumeric character and not an underscore or dash.
  65. *
  66. * A domain label is a segment of a hostname such as subdomain.google.com.
  67. */
  68. exports.isDomainLabelStartChar = char_utils_1.isAlphaNumericOrMarkChar; // alias function for clarity
  69. /**
  70. * Determines if the character is part of a domain label (but not a domain label
  71. * start character).
  72. *
  73. * A domain label is a segment of a hostname such as subdomain.google.com.
  74. */
  75. function isDomainLabelChar(charCode) {
  76. return charCode === 95 /* Char.Underscore */ || (0, exports.isDomainLabelStartChar)(charCode);
  77. }
  78. /**
  79. * Determines if the character is a path character ("pchar") as defined by
  80. * https://tools.ietf.org/html/rfc3986#appendix-A
  81. *
  82. * pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  83. *
  84. * unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
  85. * pct-encoded = "%" HEXDIG HEXDIG
  86. * sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
  87. * / "*" / "+" / "," / ";" / "="
  88. *
  89. * Note that this implementation doesn't follow the spec exactly, but rather
  90. * follows URL path characters found out in the wild (spec might be out of date?)
  91. */
  92. function isPathChar(charCode) {
  93. return ((0, char_utils_1.isAlphaNumericOrMarkChar)(charCode) ||
  94. (0, char_utils_1.isUrlSuffixAllowedSpecialChar)(charCode) ||
  95. (0, char_utils_1.isUrlSuffixNotAllowedAsFinalChar)(charCode) // characters in addition to those allowed by isUrlSuffixAllowedSpecialChar()
  96. );
  97. }
  98. /**
  99. * Determines if the character given may begin the "URL Suffix" section of a
  100. * URI (i.e. the path, query, or hash section). These are the '/', '?' and '#'
  101. * characters.
  102. *
  103. * See https://tools.ietf.org/html/rfc3986#appendix-A
  104. */
  105. function isUrlSuffixStartChar(charCode) {
  106. return (charCode === 47 /* Char.Slash */ || // '/'
  107. charCode === 63 /* Char.Question */ || // '?'
  108. charCode === 35 /* Char.NumberSign */ // '#'
  109. );
  110. }
  111. /**
  112. * Determines if the top-level domain (TLD) read in the host is a known TLD.
  113. *
  114. * Example: 'com' would be a known TLD (for a host of 'google.com'), but
  115. * 'local' would not (for a domain name of 'my-computer.local').
  116. */
  117. function isKnownTld(tld) {
  118. return known_tlds_1.tldRegex.test(tld.toLowerCase()); // make sure the tld is lowercase for the regex
  119. }
  120. /**
  121. * Determines if the given `url` is a valid scheme-prefixed URL.
  122. */
  123. function isValidSchemeUrl(url) {
  124. // If the scheme is 'javascript:' or 'vbscript:', these link
  125. // types can be dangerous. Don't link them.
  126. if (exports.invalidSchemeRe.test(url)) {
  127. return false;
  128. }
  129. var schemeMatch = url.match(exports.schemeUrlRe);
  130. if (!schemeMatch) {
  131. return false;
  132. }
  133. var isAuthorityMatch = !!schemeMatch[1];
  134. var host = schemeMatch[2];
  135. if (isAuthorityMatch) {
  136. // Any match that has an authority ('//' chars) after the scheme is
  137. // valid, such as 'http://anything'
  138. return true;
  139. }
  140. // If there's no authority ('//' chars), check that we have a hostname
  141. // that looks valid.
  142. //
  143. // The host must contain at least one '.' char and have a domain label
  144. // with at least one letter to be considered valid.
  145. //
  146. // Accept:
  147. // - git:domain.com (scheme followed by a host
  148. // Do not accept:
  149. // - git:something ('something' doesn't look like a host)
  150. // - version:1.0 ('1.0' doesn't look like a host)
  151. if (host.indexOf('.') === -1 || !/[A-Za-z]/.test(host)) {
  152. // `letterRe` RegExp checks for a letter anywhere in the host string
  153. return false;
  154. }
  155. return true;
  156. }
  157. /**
  158. * Determines if the given `url` is a match with a valid TLD.
  159. */
  160. function isValidTldMatch(url) {
  161. // TLD URL such as 'google.com', we need to confirm that we have a valid
  162. // top-level domain
  163. var tldUrlHostMatch = url.match(exports.tldUrlHostRe);
  164. if (!tldUrlHostMatch) {
  165. // At this point, if the URL didn't match our TLD re, it must be invalid
  166. // (highly unlikely to happen, but just in case)
  167. return false;
  168. }
  169. var host = tldUrlHostMatch[0];
  170. var hostLabels = host.split('.');
  171. if (hostLabels.length < 2) {
  172. // 0 or 1 host label, there's no TLD. Ex: 'localhost'
  173. return false;
  174. }
  175. var tld = hostLabels[hostLabels.length - 1];
  176. if (!isKnownTld(tld)) {
  177. return false;
  178. }
  179. // TODO: Implement these conditions for TLD matcher:
  180. // (
  181. // this.longestDomainLabelLength <= 63 &&
  182. // this.domainNameLength <= 255
  183. // );
  184. return true;
  185. }
  186. // Regular expression to confirm a valid IPv4 address (ex: '192.168.0.1')
  187. // TODO: encode this into the state machine so that we don't need to run this
  188. // regexp separately to confirm the match
  189. var ipV4Re = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
  190. // Regular expression used to split the IPv4 address itself from any port/path/query/hash
  191. var ipV4PartRe = /[:/?#]/;
  192. /**
  193. * Determines if the given URL is a valid IPv4-prefixed URL.
  194. */
  195. function isValidIpV4Address(url) {
  196. // Grab just the IP address
  197. var ipV4Part = url.split(ipV4PartRe, 1)[0]; // only 1 result needed
  198. return ipV4Re.test(ipV4Part);
  199. }
  200. //# sourceMappingURL=uri-utils.js.map