Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -1,28 +1,49 @@
'use strict'
const { normalizeIPv6, normalizeIPv4, removeDotSegments, recomposeAuthority, normalizeComponentEncoding } = require('./lib/utils')
const SCHEMES = require('./lib/schemes')
const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizeComponentEncoding, isIPv4, nonSimpleDomain } = require('./lib/utils')
const { SCHEMES, getSchemeHandler } = require('./lib/schemes')
/**
* @template {import('./types/index').URIComponent|string} T
* @param {T} uri
* @param {import('./types/index').Options} [options]
* @returns {T}
*/
function normalize (uri, options) {
if (typeof uri === 'string') {
uri = serialize(parse(uri, options), options)
uri = /** @type {T} */ (serialize(parse(uri, options), options))
} else if (typeof uri === 'object') {
uri = parse(serialize(uri, options), options)
uri = /** @type {T} */ (parse(serialize(uri, options), options))
}
return uri
}
/**
* @param {string} baseURI
* @param {string} relativeURI
* @param {import('./types/index').Options} [options]
* @returns {string}
*/
function resolve (baseURI, relativeURI, options) {
const schemelessOptions = Object.assign({ scheme: 'null' }, options)
const resolved = resolveComponents(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true)
return serialize(resolved, { ...schemelessOptions, skipEscape: true })
const schemelessOptions = options ? Object.assign({ scheme: 'null' }, options) : { scheme: 'null' }
const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true)
schemelessOptions.skipEscape = true
return serialize(resolved, schemelessOptions)
}
function resolveComponents (base, relative, options, skipNormalization) {
/**
* @param {import ('./types/index').URIComponent} base
* @param {import ('./types/index').URIComponent} relative
* @param {import('./types/index').Options} [options]
* @param {boolean} [skipNormalization=false]
* @returns {import ('./types/index').URIComponent}
*/
function resolveComponent (base, relative, options, skipNormalization) {
/** @type {import('./types/index').URIComponent} */
const target = {}
if (!skipNormalization) {
base = parse(serialize(base, options), options) // normalize base components
relative = parse(serialize(relative, options), options) // normalize relative components
base = parse(serialize(base, options), options) // normalize base component
relative = parse(serialize(relative, options), options) // normalize relative component
}
options = options || {}
@@ -51,7 +72,7 @@ function resolveComponents (base, relative, options, skipNormalization) {
target.query = base.query
}
} else {
if (relative.path.charAt(0) === '/') {
if (relative.path[0] === '/') {
target.path = removeDotSegments(relative.path)
} else {
if ((base.userinfo !== undefined || base.host !== undefined || base.port !== undefined) && !base.path) {
@@ -78,6 +99,12 @@ function resolveComponents (base, relative, options, skipNormalization) {
return target
}
/**
* @param {import ('./types/index').URIComponent|string} uriA
* @param {import ('./types/index').URIComponent|string} uriB
* @param {import ('./types/index').Options} options
* @returns {boolean}
*/
function equal (uriA, uriB, options) {
if (typeof uriA === 'string') {
uriA = unescape(uriA)
@@ -96,8 +123,13 @@ function equal (uriA, uriB, options) {
return uriA.toLowerCase() === uriB.toLowerCase()
}
/**
* @param {Readonly<import('./types/index').URIComponent>} cmpts
* @param {import('./types/index').Options} [opts]
* @returns {string}
*/
function serialize (cmpts, opts) {
const components = {
const component = {
host: cmpts.host,
scheme: cmpts.scheme,
userinfo: cmpts.userinfo,
@@ -117,29 +149,28 @@ function serialize (cmpts, opts) {
const uriTokens = []
// find scheme handler
const schemeHandler = SCHEMES[(options.scheme || components.scheme || '').toLowerCase()]
const schemeHandler = getSchemeHandler(options.scheme || component.scheme)
// perform scheme specific serialization
if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(components, options)
if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(component, options)
if (components.path !== undefined) {
if (component.path !== undefined) {
if (!options.skipEscape) {
components.path = escape(components.path)
component.path = escape(component.path)
if (components.scheme !== undefined) {
components.path = components.path.split('%3A').join(':')
if (component.scheme !== undefined) {
component.path = component.path.split('%3A').join(':')
}
} else {
components.path = unescape(components.path)
component.path = unescape(component.path)
}
}
if (options.reference !== 'suffix' && components.scheme) {
uriTokens.push(components.scheme)
uriTokens.push(':')
if (options.reference !== 'suffix' && component.scheme) {
uriTokens.push(component.scheme, ':')
}
const authority = recomposeAuthority(components, options)
const authority = recomposeAuthority(component)
if (authority !== undefined) {
if (options.reference !== 'suffix') {
uriTokens.push('//')
@@ -147,53 +178,49 @@ function serialize (cmpts, opts) {
uriTokens.push(authority)
if (components.path && components.path.charAt(0) !== '/') {
if (component.path && component.path[0] !== '/') {
uriTokens.push('/')
}
}
if (components.path !== undefined) {
let s = components.path
if (component.path !== undefined) {
let s = component.path
if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) {
s = removeDotSegments(s)
}
if (authority === undefined) {
s = s.replace(/^\/\//u, '/%2F') // don't allow the path to start with "//"
if (
authority === undefined &&
s[0] === '/' &&
s[1] === '/'
) {
// don't allow the path to start with "//"
s = '/%2F' + s.slice(2)
}
uriTokens.push(s)
}
if (components.query !== undefined) {
uriTokens.push('?')
uriTokens.push(components.query)
if (component.query !== undefined) {
uriTokens.push('?', component.query)
}
if (components.fragment !== undefined) {
uriTokens.push('#')
uriTokens.push(components.fragment)
if (component.fragment !== undefined) {
uriTokens.push('#', component.fragment)
}
return uriTokens.join('')
}
const hexLookUp = Array.from({ length: 127 }, (v, k) => /[^!"$&'()*+,\-.;=_`a-z{}~]/u.test(String.fromCharCode(k)))
function nonSimpleDomain (value) {
let code = 0
for (let i = 0, len = value.length; i < len; ++i) {
code = value.charCodeAt(i)
if (code > 126 || hexLookUp[code]) {
return true
}
}
return false
}
const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u
/**
* @param {string} uri
* @param {import('./types/index').Options} [opts]
* @returns
*/
function parse (uri, opts) {
const options = Object.assign({}, opts)
/** @type {import('./types/index').URIComponent} */
const parsed = {
scheme: undefined,
userinfo: undefined,
@@ -203,8 +230,15 @@ function parse (uri, opts) {
query: undefined,
fragment: undefined
}
const gotEncoding = uri.indexOf('%') !== -1
if (options.reference === 'suffix') uri = (options.scheme ? options.scheme + ':' : '') + '//' + uri
let isIP = false
if (options.reference === 'suffix') {
if (options.scheme) {
uri = options.scheme + ':' + uri
} else {
uri = '//' + uri
}
}
const matches = uri.match(URI_PARSE)
@@ -223,14 +257,16 @@ function parse (uri, opts) {
parsed.port = matches[5]
}
if (parsed.host) {
const ipv4result = normalizeIPv4(parsed.host)
if (ipv4result.isIPV4 === false) {
parsed.host = normalizeIPv6(ipv4result.host, { isIPV4: false }).host.toLowerCase()
const ipv4result = isIPv4(parsed.host)
if (ipv4result === false) {
const ipv6result = normalizeIPv6(parsed.host)
parsed.host = ipv6result.host.toLowerCase()
isIP = ipv6result.isIPV6
} else {
parsed.host = ipv4result.host
isIP = true
}
}
if (parsed.scheme === undefined && parsed.userinfo === undefined && parsed.host === undefined && parsed.port === undefined && !parsed.path && parsed.query === undefined) {
if (parsed.scheme === undefined && parsed.userinfo === undefined && parsed.host === undefined && parsed.port === undefined && parsed.query === undefined && !parsed.path) {
parsed.reference = 'same-document'
} else if (parsed.scheme === undefined) {
parsed.reference = 'relative'
@@ -246,12 +282,12 @@ function parse (uri, opts) {
}
// find scheme handler
const schemeHandler = SCHEMES[(options.scheme || parsed.scheme || '').toLowerCase()]
const schemeHandler = getSchemeHandler(options.scheme || parsed.scheme)
// check if scheme can't handle IRIs
if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) {
// if host component is a domain name
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && nonSimpleDomain(parsed.host)) {
if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) {
// convert Unicode IDN -> ASCII IDN
try {
parsed.host = URL.domainToASCII(parsed.host.toLowerCase())
@@ -263,20 +299,19 @@ function parse (uri, opts) {
}
if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) {
if (gotEncoding && parsed.scheme !== undefined) {
parsed.scheme = unescape(parsed.scheme)
if (uri.indexOf('%') !== -1) {
if (parsed.scheme !== undefined) {
parsed.scheme = unescape(parsed.scheme)
}
if (parsed.host !== undefined) {
parsed.host = unescape(parsed.host)
}
}
if (gotEncoding && parsed.userinfo !== undefined) {
parsed.userinfo = unescape(parsed.userinfo)
}
if (gotEncoding && parsed.host !== undefined) {
parsed.host = unescape(parsed.host)
}
if (parsed.path !== undefined && parsed.path.length) {
if (parsed.path) {
parsed.path = escape(unescape(parsed.path))
}
if (parsed.fragment !== undefined && parsed.fragment.length) {
parsed.fragment = encodeURI(decodeURI(parsed.fragment))
if (parsed.fragment) {
parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment))
}
}
@@ -291,9 +326,10 @@ function parse (uri, opts) {
}
const fastUri = {
SCHEMES,
normalize,
resolve,
resolveComponents,
resolveComponent,
equal,
serialize,
parse