1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | 47x 47x 47x 47x 47x 47x 47x 18x 47x 2x 47x 1197x 47x 4313x 47x 77x 47x 47x 25x 22x 47x 1637x 47x 10295x 47x 5257x 5257x 47x 1587x 1587x 47x 1739x 47x 1342x 1315x 1014x 1014x 1010x 1010x 1010x 1009x 1x 4x 301x 301x 301x 89x 212x 27x 47x 1189x 1189x 522x 522x 667x 1189x 1189x 47x 821x 821x 821x 608x 608x 213x 213x 821x 47x 4320x 2758x 1562x 643x 1562x 47x 726x 726x 2x 2x 2x 724x 5x 4x 4x 4x 1x 720x 720x 4320x 4320x 720x 47x 276x 276x 276x 276x 276x 25x 276x 249x 276x 276x 276x 276x 276x 276x 83x 1x 1x 1x 82x 82x 25x 275x 1x 1x 1x 274x 274x 57x 274x 30x 274x 47x 80x 16x 47x 47x 47x 47x 1x 46x 64x 31x 91x 91x 91x 13x 91x 91x 55x 36x 33x 33x 33x 75x 75x 75x 75x 33x 33x 42x 47x 61x 289x 289x 289x 289x 47x 138x 80x 138x 61x 138x 47x 43x 43x 99x 99x 99x 99x 47x 128x 128x 242x 242x 242x 47x 138x 79x 79x 47x 138x 138x 138x 43x 138x 138x | import Protocol from './Protocol.js'; import Domain from './Domain.js'; import Port from './Port.js'; import Path from './Path.js'; import Query from './Query.js'; import Hash from './Hash.js'; import PathComponent from './PathComponent.js'; import QueryComponent from './QueryComponent.js'; import MatchFragment from './MatchFragment.js'; import RouterError from './RouterError.js'; import ParamProtocolTemplate from './ParamProtocolTemplate.js'; import ParamDomainTemplate from './ParamDomainTemplate.js'; import ParamPortTemplate from './ParamPortTemplate.js'; import ParamPathTemplate from './ParamPathTemplate.js'; import ParamQueryTemplate from './ParamQueryTemplate.js'; import ParamHashTemplate from './ParamHashTemplate.js'; import ConstProtocolTemplate from './ConstProtocolTemplate.js'; import ConstDomainTemplate from './ConstDomainTemplate.js'; import ConstPortTemplate from './ConstPortTemplate.js'; import ConstPathTemplate from './ConstPathTemplate.js'; import ConstQueryTemplate from './ConstQueryTemplate.js'; import ConstHashTemplate from './ConstHashTemplate.js'; const partTemplates = { ParamProtocolTemplate, ParamDomainTemplate, ParamPortTemplate, ParamPathTemplate, ParamQueryTemplate, ParamHashTemplate, ConstProtocolTemplate, ConstDomainTemplate, ConstPortTemplate, ConstPathTemplate, ConstQueryTemplate, ConstHashTemplate }; const partNames = [ 'protocol', 'domain', 'port', 'path', 'query', 'hash' ]; const partConstructors = { protocol: Protocol, domain: Domain, port: Port, path: Path, query: Query, hash: Hash, queryComponent: QueryComponent, pathComponent: PathComponent }; const portByProtocol = { ftp: 21, ssh: 22, telnet: 23, smtp: 25, dns: 53, dhcp: 68, tftp: 69, http: 80, pop: 110, ntp: 123, imap: 143, snmp: 162, bgp: 179, https: 443, ldaps: 636 }; const regExps = { uri: /^(?:([+A-z0-9]+:)?\/\/([-.A-zА-я0-9.]+)(:[0-9]+)?)?([^?#]*)(\?[^#]*)?(#.*)?$/i, template: /^(?:((?:[+A-z0-9]+|{[A-z0-9]+}):)?\/\/((?:[-.A-zА-я0-9]+|{[A-z0-9]+}))(:(?:[0-9]+|{[A-z0-9]+}))?)?([^?#]*)(\?[^#]*)?(#.*)?$/i, param: /^{([^*}]*)(\*)?}$/ }; const splittedUriPartRegExps = { protocol: /:$/, domain: null, port: /^:/, path: null, query: /^\?/, hash: /^#/ }; export const toLowerCase = (value='') => { return value.toLowerCase(); }; export const toUpperCase = (value='') => { return value.toUpperCase(); }; export const toTitleCase = (value='') => { return value.substr(0, 1).toUpperCase() + value.substr(1); }; export const isEmpty = (value) => { return value === null || value === undefined; }; export const isMatchGenerate = (value) => { return value && typeof value === 'object' && (value.match || value.generate); }; export const isMatchFragment = (value) => { if (value === null) { return true; } return value && value.hasOwnProperty('value') && value.hasOwnProperty('params'); }; export const forEachPartName = (callback) => { partNames.forEach(callback); }; export const getPartConstructor = (partName) => { return partConstructors[partName]; }; export const getDefaultPart = (partName) => { const PartConstructor = getPartConstructor(partName); return new PartConstructor(); }; export const getPortByProtocol = (protocol) => { protocol = (protocol + '').toLowerCase(); return new Port(portByProtocol[protocol] || null); }; export const getRegExp = (regExpName) => { return regExps[regExpName]; }; export const getPortByParsedUri = ({domain, protocol, port}, {router}) => { if (port.isEmpty()) { if (domain.isEmpty()) { const defaultPort = router.getDefaultPart('port'); if (defaultPort.isEmpty()) { const defaultProtocol = router.getDefaultPart('protocol'); const portByDefaultProtocol = getPortByProtocol(defaultProtocol.toLowerCase(true).toString()); if (portByDefaultProtocol.isEmpty()) { return defaultPort; } else { return portByDefaultProtocol; } } else { return defaultPort; } } else { const currentProtocol = protocol.isEmpty() ? router.getDefaultPart('protocol') : protocol; const portByCurrentProtocol = getPortByProtocol(currentProtocol.toLowerCase(true).toString()); if (portByCurrentProtocol.isEmpty()) { return port; } else { return portByCurrentProtocol; } } } else { return port; } }; export const chooseTemplate = (templateUri, partName) => { let templateType; const rawTemplate = templateUri.getSplittedUri(partName); if (rawTemplate) { const paramTest = getRegExp('param').test(rawTemplate); templateType = paramTest ? 'Param' : 'Const'; } else { templateType = 'Const'; } const templateName = templateType + toTitleCase(partName) + 'Template'; return partTemplates[templateName]; }; export const emulateParsedUri = (templateUri, parsedUri) => { const emulatedParsedUri = { port: templateUri.getParsedUri('port') }; const templateUriDomain = templateUri.getParsedUri('domain'); if (templateUriDomain.isEmpty()) { emulatedParsedUri.protocol = templateUri.getParsedUri('protocol'); emulatedParsedUri.domain = templateUriDomain; } else { emulatedParsedUri.protocol = parsedUri.protocol; emulatedParsedUri.domain = parsedUri.domain; } return emulatedParsedUri; }; export const parseSplittedUriPart = (rawUriPart, regExp) => { if (!rawUriPart) { return null; } if (regExp) { rawUriPart = rawUriPart.replace(regExp, ''); } return rawUriPart || null; }; export const splitUri = (rawUri, regExp) => { const uriMatch = rawUri.match(regExp); if (!uriMatch) { const uriStructureError = new Error('URI has invalid structure and cannot be splitted!'); uriStructureError.code = 'INVALID_URI_STRUCTURE'; throw uriStructureError; } if (uriMatch[2] && uriMatch[4][0] !== '/' && !getRegExp('param').test(uriMatch[4])) { if (uriMatch[4]) { const pathStructureError = new Error('Path must be absolute when domain is specified!'); pathStructureError.code = 'INVALID_PATH_STRUCTURE'; throw pathStructureError; } else { uriMatch[4] = '/'; } } const splittedUri = {}; forEachPartName((partName, p) => { const regExp = splittedUriPartRegExps[partName]; splittedUri[partName] = parseSplittedUriPart(uriMatch[p + 1], regExp); }); return splittedUri; }; export const joinUri = (parsedUri, {router}) => { let { protocol, domain, port, path, query, hash } = parsedUri; let rawUri = ''; const defaultProtocol = router.getDefaultPart('protocol'); const canProtocolBeOmitted = protocol.isEmpty() || Protocol.isEqual(protocol, defaultProtocol); if (!canProtocolBeOmitted) { rawUri += protocol + ':'; } if (protocol.isEmpty()) { protocol = defaultProtocol; } const defaultDomain = router.getDefaultPart('domain'); const canDomainBeOmitted = domain.isEmpty() || Domain.isEqual(domain, defaultDomain); const portByProtocol = getPortByProtocol(protocol.toLowerCase(true).toString()); const defaultPort = router.getDefaultPart('port'); const canPortBeOmitted = false || port.isEmpty() || Port.isEqual(port, portByProtocol) || (canProtocolBeOmitted && canDomainBeOmitted && Port.isEqual(port, defaultPort)); if (!canProtocolBeOmitted || !canDomainBeOmitted || !canPortBeOmitted) { if (domain.isEmpty()) { const domainExpectedError = new Error('Domain must be not empty when protocol or port are specified.'); domainExpectedError.code = 'DOMAIN_EXPECTED'; throw domainExpectedError; } else { rawUri += '//' + domain; } if (!canPortBeOmitted) { rawUri += ':' + port; } } if (!domain.isEmpty() && !path.isAbsolute()) { const pathStructureError = new Error('Path must be absolute when domain is specified!'); pathStructureError.code = 'INVALID_PATH_STRUCTURE'; throw pathStructureError; } rawUri += path; if (!query.isEmpty()) { rawUri += '?' + query; } if (!hash.isEmpty()) { rawUri += '#' + hash; } return rawUri; }; export const getMatchFunctionByItem = (matchItem, {partName, paramName, getUserUriPart}) => { if (typeof matchItem === 'function') { return (userUri, contextOptions) => { contextOptions.paramName = paramName; contextOptions.MatchFragment = MatchFragment; const matchFragment = matchItem(userUri, contextOptions); if (!isMatchFragment(matchFragment)) { throw new RouterError(RouterError.MATCH_FRAGMENT_EXPECTED); } return matchFragment; }; } else if (matchItem instanceof RegExp) { return (userUri, contextOptions) => { const userUriPart = getUserUriPart(userUri, partName); const isCaseSensitive = userUriPart.isCaseSensitive() && contextOptions.caseSensitive; if (!isCaseSensitive && matchItem.flags.indexOf('i') === -1) { matchItem = new RegExp(matchItem.source, matchItem.flags + 'i'); } const rawValue = (userUriPart.toString() || '') + ''; if (!matchItem.test(rawValue)) { return null; } return new MatchFragment(rawValue, {[paramName]: rawValue}); }; } else { const PartConstructor = getPartConstructor(partName); const templateUriPart = new PartConstructor(matchItem); return (userUri, contextOptions) => { const templateUriPartString = templateUriPart.toLowerCase(!contextOptions.caseSensitive).toString(); const userUriPart = getUserUriPart(userUri, partName); const userUriPartString = userUriPart.toLowerCase(!contextOptions.caseSensitive).toString(); if (userUriPartString === templateUriPartString) { const rawValue = userUriPart.toString(); return new MatchFragment(rawValue, {[paramName]: rawValue}); } return null; }; } }; export const getFallbackMatchFunction = ({partName, paramName, getUserUriPart}) => { return (userUri, contextOptions) => { const matchParams = {}; const userUriPart = getUserUriPart(userUri, partName); matchParams[paramName] = userUriPart.toString(); return new MatchFragment(matchParams[paramName], matchParams); }; }; export const convertMatchItemsToFunctions = (matchItems, converterOptions) => { const matchFunctions = matchItems.map((matchItem) => { return getMatchFunctionByItem(matchItem, converterOptions); }); if (!matchFunctions.length) { matchFunctions.push(getFallbackMatchFunction(converterOptions)); } return matchFunctions; }; export const getGenerateFunctionByItem = (generateItem, {partName, paramName}) => { const PartConstructor = getPartConstructor(partName); return (userParams, contextOptions) => { contextOptions.paramName = paramName; const rawValue = typeof generateItem === 'function' ? generateItem(userParams, contextOptions) : generateItem; const parsedValue = new PartConstructor(rawValue); return parsedValue; }; }; export const getParamGenerateFunction = ({partName, paramName}) => { const PartConstructor = getPartConstructor(partName); return (userParams) => { const userParam = userParams[paramName]; const parsedValue = new PartConstructor(userParam); return parsedValue; }; }; export const getFallbackGenerateFunction = ({partName, paramName}) => { return (userParams, contextOptions) => { const parsedValue = contextOptions.router.getDefaultPart(partName); return parsedValue; }; }; export const convertGenerateItemsToFunctions = (generateItems, converterOptions) => { const hasFunction = generateItems.some((generateItem) => typeof generateItem === 'function'); let generateFunctions = hasFunction ? [] : [getParamGenerateFunction(converterOptions)]; generateFunctions = generateFunctions.concat(generateItems.map((generateItem) => { return getGenerateFunctionByItem(generateItem, converterOptions); })); generateFunctions.push(getFallbackGenerateFunction(converterOptions)); return generateFunctions; }; |