[79a0317] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const UUID_REG = /^[\da-f]{8}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{12}$/iu
|
---|
| 4 | const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu
|
---|
| 5 |
|
---|
| 6 | function isSecure (wsComponents) {
|
---|
| 7 | return typeof wsComponents.secure === 'boolean' ? wsComponents.secure : String(wsComponents.scheme).toLowerCase() === 'wss'
|
---|
| 8 | }
|
---|
| 9 |
|
---|
| 10 | function httpParse (components) {
|
---|
| 11 | if (!components.host) {
|
---|
| 12 | components.error = components.error || 'HTTP URIs must have a host.'
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | return components
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | function httpSerialize (components) {
|
---|
| 19 | const secure = String(components.scheme).toLowerCase() === 'https'
|
---|
| 20 |
|
---|
| 21 | // normalize the default port
|
---|
| 22 | if (components.port === (secure ? 443 : 80) || components.port === '') {
|
---|
| 23 | components.port = undefined
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | // normalize the empty path
|
---|
| 27 | if (!components.path) {
|
---|
| 28 | components.path = '/'
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // NOTE: We do not parse query strings for HTTP URIs
|
---|
| 32 | // as WWW Form Url Encoded query strings are part of the HTML4+ spec,
|
---|
| 33 | // and not the HTTP spec.
|
---|
| 34 |
|
---|
| 35 | return components
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | function wsParse (wsComponents) {
|
---|
| 39 | // indicate if the secure flag is set
|
---|
| 40 | wsComponents.secure = isSecure(wsComponents)
|
---|
| 41 |
|
---|
| 42 | // construct resouce name
|
---|
| 43 | wsComponents.resourceName = (wsComponents.path || '/') + (wsComponents.query ? '?' + wsComponents.query : '')
|
---|
| 44 | wsComponents.path = undefined
|
---|
| 45 | wsComponents.query = undefined
|
---|
| 46 |
|
---|
| 47 | return wsComponents
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | function wsSerialize (wsComponents) {
|
---|
| 51 | // normalize the default port
|
---|
| 52 | if (wsComponents.port === (isSecure(wsComponents) ? 443 : 80) || wsComponents.port === '') {
|
---|
| 53 | wsComponents.port = undefined
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // ensure scheme matches secure flag
|
---|
| 57 | if (typeof wsComponents.secure === 'boolean') {
|
---|
| 58 | wsComponents.scheme = (wsComponents.secure ? 'wss' : 'ws')
|
---|
| 59 | wsComponents.secure = undefined
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // reconstruct path from resource name
|
---|
| 63 | if (wsComponents.resourceName) {
|
---|
| 64 | const [path, query] = wsComponents.resourceName.split('?')
|
---|
| 65 | wsComponents.path = (path && path !== '/' ? path : undefined)
|
---|
| 66 | wsComponents.query = query
|
---|
| 67 | wsComponents.resourceName = undefined
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | // forbid fragment component
|
---|
| 71 | wsComponents.fragment = undefined
|
---|
| 72 |
|
---|
| 73 | return wsComponents
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | function urnParse (urnComponents, options) {
|
---|
| 77 | if (!urnComponents.path) {
|
---|
| 78 | urnComponents.error = 'URN can not be parsed'
|
---|
| 79 | return urnComponents
|
---|
| 80 | }
|
---|
| 81 | const matches = urnComponents.path.match(URN_REG)
|
---|
| 82 | if (matches) {
|
---|
| 83 | const scheme = options.scheme || urnComponents.scheme || 'urn'
|
---|
| 84 | urnComponents.nid = matches[1].toLowerCase()
|
---|
| 85 | urnComponents.nss = matches[2]
|
---|
| 86 | const urnScheme = `${scheme}:${options.nid || urnComponents.nid}`
|
---|
| 87 | const schemeHandler = SCHEMES[urnScheme]
|
---|
| 88 | urnComponents.path = undefined
|
---|
| 89 |
|
---|
| 90 | if (schemeHandler) {
|
---|
| 91 | urnComponents = schemeHandler.parse(urnComponents, options)
|
---|
| 92 | }
|
---|
| 93 | } else {
|
---|
| 94 | urnComponents.error = urnComponents.error || 'URN can not be parsed.'
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return urnComponents
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | function urnSerialize (urnComponents, options) {
|
---|
| 101 | const scheme = options.scheme || urnComponents.scheme || 'urn'
|
---|
| 102 | const nid = urnComponents.nid.toLowerCase()
|
---|
| 103 | const urnScheme = `${scheme}:${options.nid || nid}`
|
---|
| 104 | const schemeHandler = SCHEMES[urnScheme]
|
---|
| 105 |
|
---|
| 106 | if (schemeHandler) {
|
---|
| 107 | urnComponents = schemeHandler.serialize(urnComponents, options)
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | const uriComponents = urnComponents
|
---|
| 111 | const nss = urnComponents.nss
|
---|
| 112 | uriComponents.path = `${nid || options.nid}:${nss}`
|
---|
| 113 |
|
---|
| 114 | options.skipEscape = true
|
---|
| 115 | return uriComponents
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | function urnuuidParse (urnComponents, options) {
|
---|
| 119 | const uuidComponents = urnComponents
|
---|
| 120 | uuidComponents.uuid = uuidComponents.nss
|
---|
| 121 | uuidComponents.nss = undefined
|
---|
| 122 |
|
---|
| 123 | if (!options.tolerant && (!uuidComponents.uuid || !UUID_REG.test(uuidComponents.uuid))) {
|
---|
| 124 | uuidComponents.error = uuidComponents.error || 'UUID is not valid.'
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | return uuidComponents
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | function urnuuidSerialize (uuidComponents) {
|
---|
| 131 | const urnComponents = uuidComponents
|
---|
| 132 | // normalize UUID
|
---|
| 133 | urnComponents.nss = (uuidComponents.uuid || '').toLowerCase()
|
---|
| 134 | return urnComponents
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | const http = {
|
---|
| 138 | scheme: 'http',
|
---|
| 139 | domainHost: true,
|
---|
| 140 | parse: httpParse,
|
---|
| 141 | serialize: httpSerialize
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | const https = {
|
---|
| 145 | scheme: 'https',
|
---|
| 146 | domainHost: http.domainHost,
|
---|
| 147 | parse: httpParse,
|
---|
| 148 | serialize: httpSerialize
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | const ws = {
|
---|
| 152 | scheme: 'ws',
|
---|
| 153 | domainHost: true,
|
---|
| 154 | parse: wsParse,
|
---|
| 155 | serialize: wsSerialize
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | const wss = {
|
---|
| 159 | scheme: 'wss',
|
---|
| 160 | domainHost: ws.domainHost,
|
---|
| 161 | parse: ws.parse,
|
---|
| 162 | serialize: ws.serialize
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | const urn = {
|
---|
| 166 | scheme: 'urn',
|
---|
| 167 | parse: urnParse,
|
---|
| 168 | serialize: urnSerialize,
|
---|
| 169 | skipNormalize: true
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | const urnuuid = {
|
---|
| 173 | scheme: 'urn:uuid',
|
---|
| 174 | parse: urnuuidParse,
|
---|
| 175 | serialize: urnuuidSerialize,
|
---|
| 176 | skipNormalize: true
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | const SCHEMES = {
|
---|
| 180 | http,
|
---|
| 181 | https,
|
---|
| 182 | ws,
|
---|
| 183 | wss,
|
---|
| 184 | urn,
|
---|
| 185 | 'urn:uuid': urnuuid
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | module.exports = SCHEMES
|
---|