fatsify核心功能示例测试!!!
This commit is contained in:
267
node_modules/fast-uri/lib/schemes.js
generated
vendored
Normal file
267
node_modules/fast-uri/lib/schemes.js
generated
vendored
Normal file
@@ -0,0 +1,267 @@
|
||||
'use strict'
|
||||
|
||||
const { isUUID } = require('./utils')
|
||||
const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 component
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function httpSerialize (component) {
|
||||
const secure = String(component.scheme).toLowerCase() === 'https'
|
||||
|
||||
// normalize the default port
|
||||
if (component.port === (secure ? 443 : 80) || component.port === '') {
|
||||
component.port = undefined
|
||||
}
|
||||
|
||||
// normalize the empty 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 component
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function wsParse (wsComponent) {
|
||||
// indicate if the secure flag is set
|
||||
wsComponent.secure = wsIsSecure(wsComponent)
|
||||
|
||||
// construct resouce name
|
||||
wsComponent.resourceName = (wsComponent.path || '/') + (wsComponent.query ? '?' + wsComponent.query : '')
|
||||
wsComponent.path = undefined
|
||||
wsComponent.query = undefined
|
||||
|
||||
return wsComponent
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function wsSerialize (wsComponent) {
|
||||
// normalize the default port
|
||||
if (wsComponent.port === (wsIsSecure(wsComponent) ? 443 : 80) || wsComponent.port === '') {
|
||||
wsComponent.port = undefined
|
||||
}
|
||||
|
||||
// ensure scheme matches secure flag
|
||||
if (typeof wsComponent.secure === 'boolean') {
|
||||
wsComponent.scheme = (wsComponent.secure ? 'wss' : 'ws')
|
||||
wsComponent.secure = undefined
|
||||
}
|
||||
|
||||
// reconstruct path from resource name
|
||||
if (wsComponent.resourceName) {
|
||||
const [path, query] = wsComponent.resourceName.split('?')
|
||||
wsComponent.path = (path && path !== '/' ? path : undefined)
|
||||
wsComponent.query = query
|
||||
wsComponent.resourceName = undefined
|
||||
}
|
||||
|
||||
// forbid fragment component
|
||||
wsComponent.fragment = undefined
|
||||
|
||||
return wsComponent
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function urnParse (urnComponent, options) {
|
||||
if (!urnComponent.path) {
|
||||
urnComponent.error = 'URN can not be parsed'
|
||||
return urnComponent
|
||||
}
|
||||
const matches = urnComponent.path.match(URN_REG)
|
||||
if (matches) {
|
||||
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) {
|
||||
urnComponent = schemeHandler.parse(urnComponent, options)
|
||||
}
|
||||
} else {
|
||||
urnComponent.error = urnComponent.error || 'URN can not be parsed.'
|
||||
}
|
||||
|
||||
return urnComponent
|
||||
}
|
||||
|
||||
/** @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 = getSchemeHandler(urnScheme)
|
||||
|
||||
if (schemeHandler) {
|
||||
urnComponent = schemeHandler.serialize(urnComponent, options)
|
||||
}
|
||||
|
||||
const uriComponent = urnComponent
|
||||
const nss = urnComponent.nss
|
||||
uriComponent.path = `${nid || options.nid}:${nss}`
|
||||
|
||||
options.skipEscape = true
|
||||
return uriComponent
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function urnuuidParse (urnComponent, options) {
|
||||
const uuidComponent = urnComponent
|
||||
uuidComponent.uuid = uuidComponent.nss
|
||||
uuidComponent.nss = undefined
|
||||
|
||||
if (!options.tolerant && (!uuidComponent.uuid || !isUUID(uuidComponent.uuid))) {
|
||||
uuidComponent.error = uuidComponent.error || 'UUID is not valid.'
|
||||
}
|
||||
|
||||
return uuidComponent
|
||||
}
|
||||
|
||||
/** @type {SchemeFn} */
|
||||
function urnuuidSerialize (uuidComponent) {
|
||||
const urnComponent = uuidComponent
|
||||
// normalize UUID
|
||||
urnComponent.nss = (uuidComponent.uuid || '').toLowerCase()
|
||||
return urnComponent
|
||||
}
|
||||
|
||||
const http = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'http',
|
||||
domainHost: true,
|
||||
parse: httpParse,
|
||||
serialize: httpSerialize
|
||||
})
|
||||
|
||||
const https = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'https',
|
||||
domainHost: http.domainHost,
|
||||
parse: httpParse,
|
||||
serialize: httpSerialize
|
||||
})
|
||||
|
||||
const ws = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'ws',
|
||||
domainHost: true,
|
||||
parse: wsParse,
|
||||
serialize: wsSerialize
|
||||
})
|
||||
|
||||
const wss = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'wss',
|
||||
domainHost: ws.domainHost,
|
||||
parse: ws.parse,
|
||||
serialize: ws.serialize
|
||||
})
|
||||
|
||||
const urn = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'urn',
|
||||
parse: urnParse,
|
||||
serialize: urnSerialize,
|
||||
skipNormalize: true
|
||||
})
|
||||
|
||||
const urnuuid = /** @type {SchemeHandler} */ ({
|
||||
scheme: 'urn:uuid',
|
||||
parse: urnuuidParse,
|
||||
serialize: urnuuidSerialize,
|
||||
skipNormalize: true
|
||||
})
|
||||
|
||||
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 = {
|
||||
wsIsSecure,
|
||||
SCHEMES,
|
||||
isValidSchemeName,
|
||||
getSchemeHandler,
|
||||
}
|
||||
336
node_modules/fast-uri/lib/utils.js
generated
vendored
Normal file
336
node_modules/fast-uri/lib/utils.js
generated
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
'use strict'
|
||||
|
||||
/** @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)
|
||||
|
||||
/** @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)
|
||||
|
||||
/**
|
||||
* @param {Array<string>} input
|
||||
* @returns {string}
|
||||
*/
|
||||
function stringArrayToHexStripped (input) {
|
||||
let acc = ''
|
||||
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 endipv6Encountered = false
|
||||
let endIpv6 = false
|
||||
|
||||
let consume = consumeHextets
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const cursor = input[i]
|
||||
if (cursor === '[' || cursor === ']') { continue }
|
||||
if (cursor === ':') {
|
||||
if (endipv6Encountered === true) {
|
||||
endIpv6 = true
|
||||
}
|
||||
if (!consume(buffer, address, output)) { break }
|
||||
if (++tokenCount > 7) {
|
||||
// not valid
|
||||
output.error = true
|
||||
break
|
||||
}
|
||||
if (i > 0 && input[i - 1] === ':') {
|
||||
endipv6Encountered = true
|
||||
}
|
||||
address.push(':')
|
||||
continue
|
||||
} else if (cursor === '%') {
|
||||
if (!consume(buffer, address, output)) { break }
|
||||
// switch to zone detection
|
||||
consume = consumeIsZone
|
||||
} else {
|
||||
buffer.push(cursor)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if (buffer.length) {
|
||||
if (consume === consumeIsZone) {
|
||||
output.zone = buffer.join('')
|
||||
} else if (endIpv6) {
|
||||
address.push(buffer.join(''))
|
||||
} else {
|
||||
address.push(stringArrayToHexStripped(buffer))
|
||||
}
|
||||
}
|
||||
output.address = address.join('')
|
||||
return output
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
|
||||
if (!ipv6.error) {
|
||||
let newHost = ipv6.address
|
||||
let escapedHost = ipv6.address
|
||||
if (ipv6.zone) {
|
||||
newHost += '%' + ipv6.zone
|
||||
escapedHost += '%25' + ipv6.zone
|
||||
}
|
||||
return { host: newHost, isIPV6: true, escapedHost }
|
||||
} else {
|
||||
return { host, isIPV6: false }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} str
|
||||
* @param {string} token
|
||||
* @returns {number}
|
||||
*/
|
||||
function findToken (str, token) {
|
||||
let ind = 0
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
if (str[i] === token) ind++
|
||||
}
|
||||
return ind
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
// eslint-disable-next-line no-cond-assign
|
||||
while (len = input.length) {
|
||||
if (len === 1) {
|
||||
if (input === '.') {
|
||||
break
|
||||
} else if (input === '/') {
|
||||
output.push('/')
|
||||
break
|
||||
} else {
|
||||
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('')
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 (component.scheme !== undefined) {
|
||||
component.scheme = func(component.scheme)
|
||||
}
|
||||
if (component.userinfo !== undefined) {
|
||||
component.userinfo = func(component.userinfo)
|
||||
}
|
||||
if (component.host !== undefined) {
|
||||
component.host = func(component.host)
|
||||
}
|
||||
if (component.path !== undefined) {
|
||||
component.path = func(component.path)
|
||||
}
|
||||
if (component.query !== undefined) {
|
||||
component.query = func(component.query)
|
||||
}
|
||||
if (component.fragment !== undefined) {
|
||||
component.fragment = func(component.fragment)
|
||||
}
|
||||
return component
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('../types/index').URIComponent} component
|
||||
* @returns {string|undefined}
|
||||
*/
|
||||
function recomposeAuthority (component) {
|
||||
const uriTokens = []
|
||||
|
||||
if (component.userinfo !== undefined) {
|
||||
uriTokens.push(component.userinfo)
|
||||
uriTokens.push('@')
|
||||
}
|
||||
|
||||
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 = component.host
|
||||
}
|
||||
}
|
||||
uriTokens.push(host)
|
||||
}
|
||||
|
||||
if (typeof component.port === 'number' || typeof component.port === 'string') {
|
||||
uriTokens.push(':')
|
||||
uriTokens.push(String(component.port))
|
||||
}
|
||||
|
||||
return uriTokens.length ? uriTokens.join('') : undefined
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
nonSimpleDomain,
|
||||
recomposeAuthority,
|
||||
normalizeComponentEncoding,
|
||||
removeDotSegments,
|
||||
isIPv4,
|
||||
isUUID,
|
||||
normalizeIPv6,
|
||||
stringArrayToHexStripped
|
||||
}
|
||||
Reference in New Issue
Block a user