schemes.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 'use strict'
  2. const { isUUID } = require('./utils')
  3. const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu
  4. const supportedSchemeNames = /** @type {const} */ (['http', 'https', 'ws',
  5. 'wss', 'urn', 'urn:uuid'])
  6. /** @typedef {supportedSchemeNames[number]} SchemeName */
  7. /**
  8. * @param {string} name
  9. * @returns {name is SchemeName}
  10. */
  11. function isValidSchemeName (name) {
  12. return supportedSchemeNames.indexOf(/** @type {*} */ (name)) !== -1
  13. }
  14. /**
  15. * @callback SchemeFn
  16. * @param {import('../types/index').URIComponent} component
  17. * @param {import('../types/index').Options} options
  18. * @returns {import('../types/index').URIComponent}
  19. */
  20. /**
  21. * @typedef {Object} SchemeHandler
  22. * @property {SchemeName} scheme - The scheme name.
  23. * @property {boolean} [domainHost] - Indicates if the scheme supports domain hosts.
  24. * @property {SchemeFn} parse - Function to parse the URI component for this scheme.
  25. * @property {SchemeFn} serialize - Function to serialize the URI component for this scheme.
  26. * @property {boolean} [skipNormalize] - Indicates if normalization should be skipped for this scheme.
  27. * @property {boolean} [absolutePath] - Indicates if the scheme uses absolute paths.
  28. * @property {boolean} [unicodeSupport] - Indicates if the scheme supports Unicode.
  29. */
  30. /**
  31. * @param {import('../types/index').URIComponent} wsComponent
  32. * @returns {boolean}
  33. */
  34. function wsIsSecure (wsComponent) {
  35. if (wsComponent.secure === true) {
  36. return true
  37. } else if (wsComponent.secure === false) {
  38. return false
  39. } else if (wsComponent.scheme) {
  40. return (
  41. wsComponent.scheme.length === 3 &&
  42. (wsComponent.scheme[0] === 'w' || wsComponent.scheme[0] === 'W') &&
  43. (wsComponent.scheme[1] === 's' || wsComponent.scheme[1] === 'S') &&
  44. (wsComponent.scheme[2] === 's' || wsComponent.scheme[2] === 'S')
  45. )
  46. } else {
  47. return false
  48. }
  49. }
  50. /** @type {SchemeFn} */
  51. function httpParse (component) {
  52. if (!component.host) {
  53. component.error = component.error || 'HTTP URIs must have a host.'
  54. }
  55. return component
  56. }
  57. /** @type {SchemeFn} */
  58. function httpSerialize (component) {
  59. const secure = String(component.scheme).toLowerCase() === 'https'
  60. // normalize the default port
  61. if (component.port === (secure ? 443 : 80) || component.port === '') {
  62. component.port = undefined
  63. }
  64. // normalize the empty path
  65. if (!component.path) {
  66. component.path = '/'
  67. }
  68. // NOTE: We do not parse query strings for HTTP URIs
  69. // as WWW Form Url Encoded query strings are part of the HTML4+ spec,
  70. // and not the HTTP spec.
  71. return component
  72. }
  73. /** @type {SchemeFn} */
  74. function wsParse (wsComponent) {
  75. // indicate if the secure flag is set
  76. wsComponent.secure = wsIsSecure(wsComponent)
  77. // construct resouce name
  78. wsComponent.resourceName = (wsComponent.path || '/') + (wsComponent.query ? '?' + wsComponent.query : '')
  79. wsComponent.path = undefined
  80. wsComponent.query = undefined
  81. return wsComponent
  82. }
  83. /** @type {SchemeFn} */
  84. function wsSerialize (wsComponent) {
  85. // normalize the default port
  86. if (wsComponent.port === (wsIsSecure(wsComponent) ? 443 : 80) || wsComponent.port === '') {
  87. wsComponent.port = undefined
  88. }
  89. // ensure scheme matches secure flag
  90. if (typeof wsComponent.secure === 'boolean') {
  91. wsComponent.scheme = (wsComponent.secure ? 'wss' : 'ws')
  92. wsComponent.secure = undefined
  93. }
  94. // reconstruct path from resource name
  95. if (wsComponent.resourceName) {
  96. const [path, query] = wsComponent.resourceName.split('?')
  97. wsComponent.path = (path && path !== '/' ? path : undefined)
  98. wsComponent.query = query
  99. wsComponent.resourceName = undefined
  100. }
  101. // forbid fragment component
  102. wsComponent.fragment = undefined
  103. return wsComponent
  104. }
  105. /** @type {SchemeFn} */
  106. function urnParse (urnComponent, options) {
  107. if (!urnComponent.path) {
  108. urnComponent.error = 'URN can not be parsed'
  109. return urnComponent
  110. }
  111. const matches = urnComponent.path.match(URN_REG)
  112. if (matches) {
  113. const scheme = options.scheme || urnComponent.scheme || 'urn'
  114. urnComponent.nid = matches[1].toLowerCase()
  115. urnComponent.nss = matches[2]
  116. const urnScheme = `${scheme}:${options.nid || urnComponent.nid}`
  117. const schemeHandler = getSchemeHandler(urnScheme)
  118. urnComponent.path = undefined
  119. if (schemeHandler) {
  120. urnComponent = schemeHandler.parse(urnComponent, options)
  121. }
  122. } else {
  123. urnComponent.error = urnComponent.error || 'URN can not be parsed.'
  124. }
  125. return urnComponent
  126. }
  127. /** @type {SchemeFn} */
  128. function urnSerialize (urnComponent, options) {
  129. if (urnComponent.nid === undefined) {
  130. throw new Error('URN without nid cannot be serialized')
  131. }
  132. const scheme = options.scheme || urnComponent.scheme || 'urn'
  133. const nid = urnComponent.nid.toLowerCase()
  134. const urnScheme = `${scheme}:${options.nid || nid}`
  135. const schemeHandler = getSchemeHandler(urnScheme)
  136. if (schemeHandler) {
  137. urnComponent = schemeHandler.serialize(urnComponent, options)
  138. }
  139. const uriComponent = urnComponent
  140. const nss = urnComponent.nss
  141. uriComponent.path = `${nid || options.nid}:${nss}`
  142. options.skipEscape = true
  143. return uriComponent
  144. }
  145. /** @type {SchemeFn} */
  146. function urnuuidParse (urnComponent, options) {
  147. const uuidComponent = urnComponent
  148. uuidComponent.uuid = uuidComponent.nss
  149. uuidComponent.nss = undefined
  150. if (!options.tolerant && (!uuidComponent.uuid || !isUUID(uuidComponent.uuid))) {
  151. uuidComponent.error = uuidComponent.error || 'UUID is not valid.'
  152. }
  153. return uuidComponent
  154. }
  155. /** @type {SchemeFn} */
  156. function urnuuidSerialize (uuidComponent) {
  157. const urnComponent = uuidComponent
  158. // normalize UUID
  159. urnComponent.nss = (uuidComponent.uuid || '').toLowerCase()
  160. return urnComponent
  161. }
  162. const http = /** @type {SchemeHandler} */ ({
  163. scheme: 'http',
  164. domainHost: true,
  165. parse: httpParse,
  166. serialize: httpSerialize
  167. })
  168. const https = /** @type {SchemeHandler} */ ({
  169. scheme: 'https',
  170. domainHost: http.domainHost,
  171. parse: httpParse,
  172. serialize: httpSerialize
  173. })
  174. const ws = /** @type {SchemeHandler} */ ({
  175. scheme: 'ws',
  176. domainHost: true,
  177. parse: wsParse,
  178. serialize: wsSerialize
  179. })
  180. const wss = /** @type {SchemeHandler} */ ({
  181. scheme: 'wss',
  182. domainHost: ws.domainHost,
  183. parse: ws.parse,
  184. serialize: ws.serialize
  185. })
  186. const urn = /** @type {SchemeHandler} */ ({
  187. scheme: 'urn',
  188. parse: urnParse,
  189. serialize: urnSerialize,
  190. skipNormalize: true
  191. })
  192. const urnuuid = /** @type {SchemeHandler} */ ({
  193. scheme: 'urn:uuid',
  194. parse: urnuuidParse,
  195. serialize: urnuuidSerialize,
  196. skipNormalize: true
  197. })
  198. const SCHEMES = /** @type {Record<SchemeName, SchemeHandler>} */ ({
  199. http,
  200. https,
  201. ws,
  202. wss,
  203. urn,
  204. 'urn:uuid': urnuuid
  205. })
  206. Object.setPrototypeOf(SCHEMES, null)
  207. /**
  208. * @param {string|undefined} scheme
  209. * @returns {SchemeHandler|undefined}
  210. */
  211. function getSchemeHandler (scheme) {
  212. return (
  213. scheme && (
  214. SCHEMES[/** @type {SchemeName} */ (scheme)] ||
  215. SCHEMES[/** @type {SchemeName} */(scheme.toLowerCase())])
  216. ) ||
  217. undefined
  218. }
  219. module.exports = {
  220. wsIsSecure,
  221. SCHEMES,
  222. isValidSchemeName,
  223. getSchemeHandler,
  224. }