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,188 +1,267 @@
'use strict'
const UUID_REG = /^[\da-f]{8}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{4}\b-[\da-f]{12}$/iu
const { isUUID } = require('./utils')
const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu
function isSecure (wsComponents) {
return typeof wsComponents.secure === 'boolean' ? wsComponents.secure : String(wsComponents.scheme).toLowerCase() === 'wss'
const supportedSchemeNames = /** @type {const} */ (['http', 'https', 'ws',
'wss', 'urn', 'urn:uuid'])
/** @typedef {supportedSchemeNames[number]} SchemeName */
/**
* @param {string} name
* @returns {name is SchemeName}
*/
function isValidSchemeName (name) {
return supportedSchemeNames.indexOf(/** @type {*} */ (name)) !== -1
}
function httpParse (components) {
if (!components.host) {
components.error = components.error || 'HTTP URIs must have a host.'
/**
* @callback SchemeFn
* @param {import('../types/index').URIComponent} component
* @param {import('../types/index').Options} options
* @returns {import('../types/index').URIComponent}
*/
/**
* @typedef {Object} SchemeHandler
* @property {SchemeName} scheme - The scheme name.
* @property {boolean} [domainHost] - Indicates if the scheme supports domain hosts.
* @property {SchemeFn} parse - Function to parse the URI component for this scheme.
* @property {SchemeFn} serialize - Function to serialize the URI component for this scheme.
* @property {boolean} [skipNormalize] - Indicates if normalization should be skipped for this scheme.
* @property {boolean} [absolutePath] - Indicates if the scheme uses absolute paths.
* @property {boolean} [unicodeSupport] - Indicates if the scheme supports Unicode.
*/
/**
* @param {import('../types/index').URIComponent} wsComponent
* @returns {boolean}
*/
function wsIsSecure (wsComponent) {
if (wsComponent.secure === true) {
return true
} else if (wsComponent.secure === false) {
return false
} else if (wsComponent.scheme) {
return (
wsComponent.scheme.length === 3 &&
(wsComponent.scheme[0] === 'w' || wsComponent.scheme[0] === 'W') &&
(wsComponent.scheme[1] === 's' || wsComponent.scheme[1] === 'S') &&
(wsComponent.scheme[2] === 's' || wsComponent.scheme[2] === 'S')
)
} else {
return false
}
}
/** @type {SchemeFn} */
function httpParse (component) {
if (!component.host) {
component.error = component.error || 'HTTP URIs must have a host.'
}
return components
return component
}
function httpSerialize (components) {
const secure = String(components.scheme).toLowerCase() === 'https'
/** @type {SchemeFn} */
function httpSerialize (component) {
const secure = String(component.scheme).toLowerCase() === 'https'
// normalize the default port
if (components.port === (secure ? 443 : 80) || components.port === '') {
components.port = undefined
if (component.port === (secure ? 443 : 80) || component.port === '') {
component.port = undefined
}
// normalize the empty path
if (!components.path) {
components.path = '/'
if (!component.path) {
component.path = '/'
}
// NOTE: We do not parse query strings for HTTP URIs
// as WWW Form Url Encoded query strings are part of the HTML4+ spec,
// and not the HTTP spec.
return components
return component
}
function wsParse (wsComponents) {
/** @type {SchemeFn} */
function wsParse (wsComponent) {
// indicate if the secure flag is set
wsComponents.secure = isSecure(wsComponents)
wsComponent.secure = wsIsSecure(wsComponent)
// construct resouce name
wsComponents.resourceName = (wsComponents.path || '/') + (wsComponents.query ? '?' + wsComponents.query : '')
wsComponents.path = undefined
wsComponents.query = undefined
wsComponent.resourceName = (wsComponent.path || '/') + (wsComponent.query ? '?' + wsComponent.query : '')
wsComponent.path = undefined
wsComponent.query = undefined
return wsComponents
return wsComponent
}
function wsSerialize (wsComponents) {
/** @type {SchemeFn} */
function wsSerialize (wsComponent) {
// normalize the default port
if (wsComponents.port === (isSecure(wsComponents) ? 443 : 80) || wsComponents.port === '') {
wsComponents.port = undefined
if (wsComponent.port === (wsIsSecure(wsComponent) ? 443 : 80) || wsComponent.port === '') {
wsComponent.port = undefined
}
// ensure scheme matches secure flag
if (typeof wsComponents.secure === 'boolean') {
wsComponents.scheme = (wsComponents.secure ? 'wss' : 'ws')
wsComponents.secure = undefined
if (typeof wsComponent.secure === 'boolean') {
wsComponent.scheme = (wsComponent.secure ? 'wss' : 'ws')
wsComponent.secure = undefined
}
// reconstruct path from resource name
if (wsComponents.resourceName) {
const [path, query] = wsComponents.resourceName.split('?')
wsComponents.path = (path && path !== '/' ? path : undefined)
wsComponents.query = query
wsComponents.resourceName = undefined
if (wsComponent.resourceName) {
const [path, query] = wsComponent.resourceName.split('?')
wsComponent.path = (path && path !== '/' ? path : undefined)
wsComponent.query = query
wsComponent.resourceName = undefined
}
// forbid fragment component
wsComponents.fragment = undefined
wsComponent.fragment = undefined
return wsComponents
return wsComponent
}
function urnParse (urnComponents, options) {
if (!urnComponents.path) {
urnComponents.error = 'URN can not be parsed'
return urnComponents
/** @type {SchemeFn} */
function urnParse (urnComponent, options) {
if (!urnComponent.path) {
urnComponent.error = 'URN can not be parsed'
return urnComponent
}
const matches = urnComponents.path.match(URN_REG)
const matches = urnComponent.path.match(URN_REG)
if (matches) {
const scheme = options.scheme || urnComponents.scheme || 'urn'
urnComponents.nid = matches[1].toLowerCase()
urnComponents.nss = matches[2]
const urnScheme = `${scheme}:${options.nid || urnComponents.nid}`
const schemeHandler = SCHEMES[urnScheme]
urnComponents.path = undefined
const scheme = options.scheme || urnComponent.scheme || 'urn'
urnComponent.nid = matches[1].toLowerCase()
urnComponent.nss = matches[2]
const urnScheme = `${scheme}:${options.nid || urnComponent.nid}`
const schemeHandler = getSchemeHandler(urnScheme)
urnComponent.path = undefined
if (schemeHandler) {
urnComponents = schemeHandler.parse(urnComponents, options)
urnComponent = schemeHandler.parse(urnComponent, options)
}
} else {
urnComponents.error = urnComponents.error || 'URN can not be parsed.'
urnComponent.error = urnComponent.error || 'URN can not be parsed.'
}
return urnComponents
return urnComponent
}
function urnSerialize (urnComponents, options) {
const scheme = options.scheme || urnComponents.scheme || 'urn'
const nid = urnComponents.nid.toLowerCase()
/** @type {SchemeFn} */
function urnSerialize (urnComponent, options) {
if (urnComponent.nid === undefined) {
throw new Error('URN without nid cannot be serialized')
}
const scheme = options.scheme || urnComponent.scheme || 'urn'
const nid = urnComponent.nid.toLowerCase()
const urnScheme = `${scheme}:${options.nid || nid}`
const schemeHandler = SCHEMES[urnScheme]
const schemeHandler = getSchemeHandler(urnScheme)
if (schemeHandler) {
urnComponents = schemeHandler.serialize(urnComponents, options)
urnComponent = schemeHandler.serialize(urnComponent, options)
}
const uriComponents = urnComponents
const nss = urnComponents.nss
uriComponents.path = `${nid || options.nid}:${nss}`
const uriComponent = urnComponent
const nss = urnComponent.nss
uriComponent.path = `${nid || options.nid}:${nss}`
options.skipEscape = true
return uriComponents
return uriComponent
}
function urnuuidParse (urnComponents, options) {
const uuidComponents = urnComponents
uuidComponents.uuid = uuidComponents.nss
uuidComponents.nss = undefined
/** @type {SchemeFn} */
function urnuuidParse (urnComponent, options) {
const uuidComponent = urnComponent
uuidComponent.uuid = uuidComponent.nss
uuidComponent.nss = undefined
if (!options.tolerant && (!uuidComponents.uuid || !UUID_REG.test(uuidComponents.uuid))) {
uuidComponents.error = uuidComponents.error || 'UUID is not valid.'
if (!options.tolerant && (!uuidComponent.uuid || !isUUID(uuidComponent.uuid))) {
uuidComponent.error = uuidComponent.error || 'UUID is not valid.'
}
return uuidComponents
return uuidComponent
}
function urnuuidSerialize (uuidComponents) {
const urnComponents = uuidComponents
/** @type {SchemeFn} */
function urnuuidSerialize (uuidComponent) {
const urnComponent = uuidComponent
// normalize UUID
urnComponents.nss = (uuidComponents.uuid || '').toLowerCase()
return urnComponents
urnComponent.nss = (uuidComponent.uuid || '').toLowerCase()
return urnComponent
}
const http = {
const http = /** @type {SchemeHandler} */ ({
scheme: 'http',
domainHost: true,
parse: httpParse,
serialize: httpSerialize
}
})
const https = {
const https = /** @type {SchemeHandler} */ ({
scheme: 'https',
domainHost: http.domainHost,
parse: httpParse,
serialize: httpSerialize
}
})
const ws = {
const ws = /** @type {SchemeHandler} */ ({
scheme: 'ws',
domainHost: true,
parse: wsParse,
serialize: wsSerialize
}
})
const wss = {
const wss = /** @type {SchemeHandler} */ ({
scheme: 'wss',
domainHost: ws.domainHost,
parse: ws.parse,
serialize: ws.serialize
}
})
const urn = {
const urn = /** @type {SchemeHandler} */ ({
scheme: 'urn',
parse: urnParse,
serialize: urnSerialize,
skipNormalize: true
}
})
const urnuuid = {
const urnuuid = /** @type {SchemeHandler} */ ({
scheme: 'urn:uuid',
parse: urnuuidParse,
serialize: urnuuidSerialize,
skipNormalize: true
}
})
const SCHEMES = {
const SCHEMES = /** @type {Record<SchemeName, SchemeHandler>} */ ({
http,
https,
ws,
wss,
urn,
'urn:uuid': urnuuid
})
Object.setPrototypeOf(SCHEMES, null)
/**
* @param {string|undefined} scheme
* @returns {SchemeHandler|undefined}
*/
function getSchemeHandler (scheme) {
return (
scheme && (
SCHEMES[/** @type {SchemeName} */ (scheme)] ||
SCHEMES[/** @type {SchemeName} */(scheme.toLowerCase())])
) ||
undefined
}
module.exports = SCHEMES
module.exports = {
wsIsSecure,
SCHEMES,
isValidSchemeName,
getSchemeHandler,
}

View File

@@ -1,30 +0,0 @@
'use strict'
const HEX = {
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
a: 10,
A: 10,
b: 11,
B: 11,
c: 12,
C: 12,
d: 13,
D: 13,
e: 14,
E: 14,
f: 15,
F: 15
}
module.exports = {
HEX
}

View File

@@ -1,53 +1,99 @@
'use strict'
const { HEX } = require('./scopedChars')
/** @type {(value: string) => boolean} */
const isUUID = RegExp.prototype.test.bind(/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu)
function normalizeIPv4 (host) {
if (findToken(host, '.') < 3) { return { host, isIPV4: false } }
const matches = host.match(/^(\b[01]?\d{1,2}|\b2[0-4]\d|\b25[0-5])(\.([01]?\d{1,2}|2[0-4]\d|25[0-5])){3}$/u) || []
const [address] = matches
if (address) {
return { host: stripLeadingZeros(address, '.'), isIPV4: true }
} else {
return { host, isIPV4: false }
}
}
/** @type {(value: string) => boolean} */
const isIPv4 = RegExp.prototype.test.bind(/^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/u)
function stringToHexStripped (input) {
/**
* @param {Array<string>} input
* @returns {string}
*/
function stringArrayToHexStripped (input) {
let acc = ''
let strip = true
for (const c of input) {
if (c !== '0' && strip === true) strip = false
if (HEX[c] === undefined) return undefined
if (!strip) acc += c
let code = 0
let i = 0
for (i = 0; i < input.length; i++) {
code = input[i].charCodeAt(0)
if (code === 48) {
continue
}
if (!((code >= 48 && code <= 57) || (code >= 65 && code <= 70) || (code >= 97 && code <= 102))) {
return ''
}
acc += input[i]
break
}
for (i += 1; i < input.length; i++) {
code = input[i].charCodeAt(0)
if (!((code >= 48 && code <= 57) || (code >= 65 && code <= 70) || (code >= 97 && code <= 102))) {
return ''
}
acc += input[i]
}
return acc
}
/**
* @typedef {Object} GetIPV6Result
* @property {boolean} error - Indicates if there was an error parsing the IPv6 address.
* @property {string} address - The parsed IPv6 address.
* @property {string} [zone] - The zone identifier, if present.
*/
/**
* @param {string} value
* @returns {boolean}
*/
const nonSimpleDomain = RegExp.prototype.test.bind(/[^!"$&'()*+,\-.;=_`a-z{}~]/u)
/**
* @param {Array<string>} buffer
* @returns {boolean}
*/
function consumeIsZone (buffer) {
buffer.length = 0
return true
}
/**
* @param {Array<string>} buffer
* @param {Array<string>} address
* @param {GetIPV6Result} output
* @returns {boolean}
*/
function consumeHextets (buffer, address, output) {
if (buffer.length) {
const hex = stringArrayToHexStripped(buffer)
if (hex !== '') {
address.push(hex)
} else {
output.error = true
return false
}
buffer.length = 0
}
return true
}
/**
* @param {string} input
* @returns {GetIPV6Result}
*/
function getIPV6 (input) {
let tokenCount = 0
const output = { error: false, address: '', zone: '' }
/** @type {Array<string>} */
const address = []
/** @type {Array<string>} */
const buffer = []
let isZone = false
let endipv6Encountered = false
let endIpv6 = false
function consume () {
if (buffer.length) {
if (isZone === false) {
const hex = stringToHexStripped(buffer.join(''))
if (hex !== undefined) {
address.push(hex)
} else {
output.error = true
return false
}
}
buffer.length = 0
}
return true
}
let consume = consumeHextets
for (let i = 0; i < input.length; i++) {
const cursor = input[i]
@@ -56,41 +102,51 @@ function getIPV6 (input) {
if (endipv6Encountered === true) {
endIpv6 = true
}
if (!consume()) { break }
tokenCount++
address.push(':')
if (tokenCount > 7) {
if (!consume(buffer, address, output)) { break }
if (++tokenCount > 7) {
// not valid
output.error = true
break
}
if (i - 1 >= 0 && input[i - 1] === ':') {
if (i > 0 && input[i - 1] === ':') {
endipv6Encountered = true
}
address.push(':')
continue
} else if (cursor === '%') {
if (!consume()) { break }
if (!consume(buffer, address, output)) { break }
// switch to zone detection
isZone = true
consume = consumeIsZone
} else {
buffer.push(cursor)
continue
}
}
if (buffer.length) {
if (isZone) {
if (consume === consumeIsZone) {
output.zone = buffer.join('')
} else if (endIpv6) {
address.push(buffer.join(''))
} else {
address.push(stringToHexStripped(buffer.join('')))
address.push(stringArrayToHexStripped(buffer))
}
}
output.address = address.join('')
return output
}
function normalizeIPv6 (host, opts = {}) {
/**
* @typedef {Object} NormalizeIPv6Result
* @property {string} host - The normalized host.
* @property {string} [escapedHost] - The escaped host.
* @property {boolean} isIPV6 - Indicates if the host is an IPv6 address.
*/
/**
* @param {string} host
* @returns {NormalizeIPv6Result}
*/
function normalizeIPv6 (host) {
if (findToken(host, ':') < 2) { return { host, isIPV6: false } }
const ipv6 = getIPV6(host)
@@ -101,35 +157,17 @@ function normalizeIPv6 (host, opts = {}) {
newHost += '%' + ipv6.zone
escapedHost += '%25' + ipv6.zone
}
return { host: newHost, escapedHost, isIPV6: true }
return { host: newHost, isIPV6: true, escapedHost }
} else {
return { host, isIPV6: false }
}
}
function stripLeadingZeros (str, token) {
let out = ''
let skip = true
const l = str.length
for (let i = 0; i < l; i++) {
const c = str[i]
if (c === '0' && skip) {
if ((i + 1 <= l && str[i + 1] === token) || i + 1 === l) {
out += c
skip = false
}
} else {
if (c === token) {
skip = true
} else {
skip = false
}
out += c
}
}
return out
}
/**
* @param {string} str
* @param {string} token
* @returns {number}
*/
function findToken (str, token) {
let ind = 0
for (let i = 0; i < str.length; i++) {
@@ -138,99 +176,161 @@ function findToken (str, token) {
return ind
}
const RDS1 = /^\.\.?\//u
const RDS2 = /^\/\.(?:\/|$)/u
const RDS3 = /^\/\.\.(?:\/|$)/u
const RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/u
function removeDotSegments (input) {
/**
* @param {string} path
* @returns {string}
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
*/
function removeDotSegments (path) {
let input = path
const output = []
let nextSlash = -1
let len = 0
while (input.length) {
if (input.match(RDS1)) {
input = input.replace(RDS1, '')
} else if (input.match(RDS2)) {
input = input.replace(RDS2, '/')
} else if (input.match(RDS3)) {
input = input.replace(RDS3, '/')
output.pop()
} else if (input === '.' || input === '..') {
input = ''
} else {
const im = input.match(RDS5)
if (im) {
const s = im[0]
input = input.slice(s.length)
output.push(s)
// eslint-disable-next-line no-cond-assign
while (len = input.length) {
if (len === 1) {
if (input === '.') {
break
} else if (input === '/') {
output.push('/')
break
} else {
throw new Error('Unexpected dot segment condition')
output.push(input)
break
}
} else if (len === 2) {
if (input[0] === '.') {
if (input[1] === '.') {
break
} else if (input[1] === '/') {
input = input.slice(2)
continue
}
} else if (input[0] === '/') {
if (input[1] === '.' || input[1] === '/') {
output.push('/')
break
}
}
} else if (len === 3) {
if (input === '/..') {
if (output.length !== 0) {
output.pop()
}
output.push('/')
break
}
}
if (input[0] === '.') {
if (input[1] === '.') {
if (input[2] === '/') {
input = input.slice(3)
continue
}
} else if (input[1] === '/') {
input = input.slice(2)
continue
}
} else if (input[0] === '/') {
if (input[1] === '.') {
if (input[2] === '/') {
input = input.slice(2)
continue
} else if (input[2] === '.') {
if (input[3] === '/') {
input = input.slice(3)
if (output.length !== 0) {
output.pop()
}
continue
}
}
}
}
// Rule 2E: Move normal path segment to output
if ((nextSlash = input.indexOf('/', 1)) === -1) {
output.push(input)
break
} else {
output.push(input.slice(0, nextSlash))
input = input.slice(nextSlash)
}
}
return output.join('')
}
function normalizeComponentEncoding (components, esc) {
/**
* @param {import('../types/index').URIComponent} component
* @param {boolean} esc
* @returns {import('../types/index').URIComponent}
*/
function normalizeComponentEncoding (component, esc) {
const func = esc !== true ? escape : unescape
if (components.scheme !== undefined) {
components.scheme = func(components.scheme)
if (component.scheme !== undefined) {
component.scheme = func(component.scheme)
}
if (components.userinfo !== undefined) {
components.userinfo = func(components.userinfo)
if (component.userinfo !== undefined) {
component.userinfo = func(component.userinfo)
}
if (components.host !== undefined) {
components.host = func(components.host)
if (component.host !== undefined) {
component.host = func(component.host)
}
if (components.path !== undefined) {
components.path = func(components.path)
if (component.path !== undefined) {
component.path = func(component.path)
}
if (components.query !== undefined) {
components.query = func(components.query)
if (component.query !== undefined) {
component.query = func(component.query)
}
if (components.fragment !== undefined) {
components.fragment = func(components.fragment)
if (component.fragment !== undefined) {
component.fragment = func(component.fragment)
}
return components
return component
}
function recomposeAuthority (components, options) {
/**
* @param {import('../types/index').URIComponent} component
* @returns {string|undefined}
*/
function recomposeAuthority (component) {
const uriTokens = []
if (components.userinfo !== undefined) {
uriTokens.push(components.userinfo)
if (component.userinfo !== undefined) {
uriTokens.push(component.userinfo)
uriTokens.push('@')
}
if (components.host !== undefined) {
let host = unescape(components.host)
const ipV4res = normalizeIPv4(host)
if (ipV4res.isIPV4) {
host = ipV4res.host
} else {
const ipV6res = normalizeIPv6(ipV4res.host, { isIPV4: false })
if (component.host !== undefined) {
let host = unescape(component.host)
if (!isIPv4(host)) {
const ipV6res = normalizeIPv6(host)
if (ipV6res.isIPV6 === true) {
host = `[${ipV6res.escapedHost}]`
} else {
host = components.host
host = component.host
}
}
uriTokens.push(host)
}
if (typeof components.port === 'number' || typeof components.port === 'string') {
if (typeof component.port === 'number' || typeof component.port === 'string') {
uriTokens.push(':')
uriTokens.push(String(components.port))
uriTokens.push(String(component.port))
}
return uriTokens.length ? uriTokens.join('') : undefined
};
module.exports = {
nonSimpleDomain,
recomposeAuthority,
normalizeComponentEncoding,
removeDotSegments,
normalizeIPv4,
isIPv4,
isUUID,
normalizeIPv6,
stringToHexStripped
stringArrayToHexStripped
}