├── .npmrc ├── .gitattributes ├── eslint.config.js ├── tsconfig.json ├── .github ├── tests_checker.yml ├── dependabot.yml ├── workflows │ ├── package-manager-ci.yml │ └── ci.yml └── .stale.yml ├── benchmark ├── package.json ├── non-simple-domain.mjs ├── string-array-to-hex-stripped.mjs ├── equal.mjs ├── ws-is-secure.mjs └── benchmark.mjs ├── types ├── index.test-d.ts └── index.d.ts ├── test ├── ajv.test.js ├── uri-js-compatibility.test.js ├── util.test.js ├── fixtures │ ├── uri-js-serialize.json │ └── uri-js-parse.json ├── equal.test.js ├── resolve.test.js ├── rfc-3986.test.js ├── serialize.test.js ├── parse.test.js └── uri-js.test.js ├── LICENSE ├── package.json ├── .gitignore ├── lib ├── schemes.js └── utils.js ├── README.md └── index.js /.npmrc: -------------------------------------------------------------------------------- 1 | ignore-scripts=true 2 | package-lock=false 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behavior to automatically convert line endings 2 | * text=auto eol=lf 3 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = require('neostandard')({ 4 | ignores: require('neostandard').resolveIgnoresFromGitignore(), 5 | ts: true 6 | }) 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "allowJs": true, 4 | "checkJs": true, 5 | "strict": true, 6 | "noImplicitAny": true, 7 | "target": "es2015" 8 | } 9 | } -------------------------------------------------------------------------------- /.github/tests_checker.yml: -------------------------------------------------------------------------------- 1 | comment: | 2 | Hello! Thank you for contributing! 3 | It appears that you have changed the code, but the tests that verify your change are missing. Could you please add them? 4 | fileExtensions: 5 | - '.ts' 6 | - '.js' 7 | 8 | testDir: 'test' -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "github-actions" 4 | directory: "/" 5 | schedule: 6 | interval: "monthly" 7 | open-pull-requests-limit: 10 8 | 9 | - package-ecosystem: "npm" 10 | directory: "/" 11 | schedule: 12 | interval: "monthly" 13 | open-pull-requests-limit: 10 14 | -------------------------------------------------------------------------------- /benchmark/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "benchmark", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "private": true, 7 | "scripts": { 8 | "bench": "node benchmark.mjs" 9 | }, 10 | "keywords": [], 11 | "author": "", 12 | "license": "ISC", 13 | "dependencies": { 14 | "tinybench": "^5.0.0", 15 | "uri-js": "^4.4.1" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/workflows/package-manager-ci.yml: -------------------------------------------------------------------------------- 1 | name: package-manager-ci 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - next 8 | - 'v*' 9 | paths-ignore: 10 | - 'docs/**' 11 | - '*.md' 12 | pull_request: 13 | paths-ignore: 14 | - 'docs/**' 15 | - '*.md' 16 | 17 | permissions: 18 | contents: read 19 | 20 | jobs: 21 | test: 22 | permissions: 23 | contents: read 24 | uses: fastify/workflows/.github/workflows/plugins-ci-package-manager.yml@v5 25 | -------------------------------------------------------------------------------- /types/index.test-d.ts: -------------------------------------------------------------------------------- 1 | import uri, { URIComponents, URIComponent, Options, options } from '..' 2 | import { expectDeprecated, expectType } from 'tsd' 3 | 4 | const parsed = uri.parse('foo') 5 | expectType(parsed) 6 | const parsed2 = uri.parse('foo', { 7 | domainHost: true, 8 | scheme: 'https', 9 | unicodeSupport: false 10 | }) 11 | expectType(parsed2) 12 | 13 | expectType({} as URIComponents) 14 | expectDeprecated({} as URIComponents) 15 | 16 | expectType({} as options) 17 | expectDeprecated({} as options) 18 | -------------------------------------------------------------------------------- /.github/.stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 15 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - "discussion" 8 | - "feature request" 9 | - "bug" 10 | - "help wanted" 11 | - "plugin suggestion" 12 | - "good first issue" 13 | # Label to use when marking an issue as stale 14 | staleLabel: stale 15 | # Comment to post when marking an issue as stale. Set to `false` to disable 16 | markComment: > 17 | This issue has been automatically marked as stale because it has not had 18 | recent activity. It will be closed if no further activity occurs. Thank you 19 | for your contributions. 20 | # Comment to post when closing a stale issue. Set to `false` to disable 21 | closeComment: false -------------------------------------------------------------------------------- /benchmark/non-simple-domain.mjs: -------------------------------------------------------------------------------- 1 | import { Bench } from 'tinybench' 2 | import { nonSimpleDomain } from '../lib/utils.js' 3 | 4 | const benchNonSimpleDomain = new Bench({ name: 'nonSimpleDomain' }) 5 | 6 | const exampleCom = 'example.com' 7 | const exaumlmpleCom = 'exämple.com' 8 | const longDomain = 'abc'.repeat(100) + '.com' 9 | 10 | console.assert(nonSimpleDomain(exampleCom) === false, 'example.com should be a simple domain') 11 | console.assert(nonSimpleDomain(exaumlmpleCom) === true, 'exämple.com should not be a simple domain') 12 | console.assert(nonSimpleDomain(longDomain) === false, `${longDomain} should be a simple domain?`) 13 | 14 | benchNonSimpleDomain.add('nonSimpleDomain', function () { 15 | nonSimpleDomain(exampleCom) 16 | nonSimpleDomain(exaumlmpleCom) 17 | nonSimpleDomain(longDomain) 18 | }) 19 | 20 | await benchNonSimpleDomain.run() 21 | console.log(benchNonSimpleDomain.name) 22 | console.table(benchNonSimpleDomain.table()) 23 | -------------------------------------------------------------------------------- /benchmark/string-array-to-hex-stripped.mjs: -------------------------------------------------------------------------------- 1 | import { Bench } from 'tinybench' 2 | import { stringArrayToHexStripped } from '../lib/utils.js' 3 | 4 | const benchStringArrayToHexStripped = new Bench({ name: 'stringArrayToHexStripped' }) 5 | 6 | const case1 = ['0', '0', '0', '0'] 7 | const case2 = ['0', '0', '0', '1'] 8 | const case3 = ['0', '0', '1', '0'] 9 | const case4 = ['0', '1', '0', '0'] 10 | const case5 = ['1', '0', '0', '0'] 11 | const case6 = ['1', '0', '0', '1'] 12 | 13 | benchStringArrayToHexStripped.add('stringArrayToHexStripped', function () { 14 | stringArrayToHexStripped(case1) 15 | stringArrayToHexStripped(case2) 16 | stringArrayToHexStripped(case3) 17 | stringArrayToHexStripped(case4) 18 | stringArrayToHexStripped(case5) 19 | stringArrayToHexStripped(case6) 20 | }) 21 | 22 | await benchStringArrayToHexStripped.run() 23 | console.log(benchStringArrayToHexStripped.name) 24 | console.table(benchStringArrayToHexStripped.table()) 25 | -------------------------------------------------------------------------------- /test/ajv.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | const AJV = require('ajv') 7 | 8 | const ajv = new AJV({ 9 | uriResolver: fastURI // comment this line to see it works with uri-js 10 | }) 11 | 12 | test('ajv', t => { 13 | t.plan(1) 14 | const schema = { 15 | $ref: '#/definitions/Record%3Cstring%2CPerson%3E', 16 | definitions: { 17 | Person: { 18 | type: 'object', 19 | properties: { 20 | firstName: { 21 | type: 'string' 22 | } 23 | } 24 | }, 25 | 'Record': { 26 | type: 'object', 27 | additionalProperties: { 28 | $ref: '#/definitions/Person' 29 | } 30 | } 31 | } 32 | } 33 | 34 | const data = { 35 | joe: { 36 | firstName: 'Joe' 37 | } 38 | 39 | } 40 | 41 | const validate = ajv.compile(schema) 42 | t.ok(validate(data)) 43 | }) 44 | -------------------------------------------------------------------------------- /test/uri-js-compatibility.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('../') 5 | 6 | const uriJsParseFixtures = require('./fixtures/uri-js-parse.json') 7 | const uriJsSerializeFixtures = require('./fixtures/uri-js-serialize.json') 8 | 9 | test('uri-js compatibility Parse', (t) => { 10 | uriJsParseFixtures.forEach(( 11 | [value, expected] 12 | ) => { 13 | if (value === '//10.10.000.10') { 14 | return t.skip('Skipping //10.10.000.10 as it is not a valid URI per URI spec: https://datatracker.ietf.org/doc/html/rfc5954#section-4.1') 15 | } 16 | if (value.slice(0, 6) === 'mailto') { 17 | return t.skip('Skipping mailto schema test as it is not supported by fastifyURI') 18 | } 19 | t.same(JSON.parse(JSON.stringify(fastURI.parse(value))), expected, 'Compatibility parse: ' + value) 20 | }) 21 | t.end() 22 | }) 23 | 24 | test('uri-js compatibility serialize', (t) => { 25 | uriJsSerializeFixtures.forEach(([value, expected]) => { 26 | t.same( 27 | fastURI.serialize(value), 28 | expected, 29 | 'Compatibility serialize: ' + JSON.stringify(value) 30 | ) 31 | }) 32 | t.end() 33 | }) 34 | -------------------------------------------------------------------------------- /test/util.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const { 5 | stringArrayToHexStripped, 6 | removeDotSegments 7 | } = require('../lib/utils') 8 | 9 | test('stringArrayToHexStripped', (t) => { 10 | const testCases = [ 11 | [['0', '0', '0', '0'], ''], 12 | [['0', '0', '0', '1'], '1'], 13 | [['0', '0', '1', '0'], '10'], 14 | [['0', '1', '0', '0'], '100'], 15 | [['1', '0', '0', '0'], '1000'], 16 | [['1', '0', '0', '1'], '1001'], 17 | ] 18 | 19 | t.plan(testCases.length) 20 | 21 | testCases.forEach(([input, expected]) => { 22 | t.same(stringArrayToHexStripped(input), expected) 23 | }) 24 | }) 25 | 26 | // Just fixtures, because this function already tested by resolve 27 | test('removeDotSegments', (t) => { 28 | const testCases = [] 29 | // https://github.com/fastify/fast-uri/issues/139 30 | testCases.push(['WS:/WS://1305G130505:1&%0D:1&C(XXXXX*)))))))XXX130505:UUVUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$aaaaaaaaaaaa13a', 31 | 'WS:/WS://1305G130505:1&%0D:1&C(XXXXX*)))))))XXX130505:UUVUaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$aaaaaaaaaaaa13a']) 32 | 33 | t.plan(testCases.length) 34 | 35 | testCases.forEach(([input, expected]) => { 36 | t.same(removeDotSegments(input), expected) 37 | }) 38 | }) 39 | -------------------------------------------------------------------------------- /benchmark/equal.mjs: -------------------------------------------------------------------------------- 1 | import { Bench } from 'tinybench' 2 | import { fastUri } from '../index.js' 3 | 4 | const { 5 | equal: fastUriEqual, 6 | parse: fastUriParse, 7 | } = fastUri 8 | 9 | const stringA = 'example://a/b/c/%7Bfoo%7D' 10 | const stringB = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d' 11 | 12 | const componentA = fastUriParse(stringA) 13 | const componentB = fastUriParse(stringB) 14 | 15 | const benchFastUri = new Bench({ name: 'fast-uri equal' }) 16 | 17 | benchFastUri.add('equal string with string', function () { 18 | fastUriEqual(stringA, stringA) 19 | }) 20 | 21 | benchFastUri.add('equal component with component', function () { 22 | fastUriEqual(componentA, componentA) 23 | }) 24 | 25 | benchFastUri.add('equal component with string', function () { 26 | fastUriEqual(componentA, stringA) 27 | }) 28 | 29 | benchFastUri.add('equal string with component', function () { 30 | fastUriEqual(stringA, componentA) 31 | }) 32 | 33 | benchFastUri.add('not equal string with string', function () { 34 | fastUriEqual(stringA, stringB) 35 | }) 36 | 37 | benchFastUri.add('not equal component with component', function () { 38 | fastUriEqual(componentA, componentB) 39 | }) 40 | 41 | benchFastUri.add('not equal component with string', function () { 42 | fastUriEqual(componentA, stringB) 43 | }) 44 | 45 | benchFastUri.add('not equal string with component', function () { 46 | fastUriEqual(stringA, componentB) 47 | }) 48 | 49 | await benchFastUri.run() 50 | console.log(benchFastUri.name) 51 | console.table(benchFastUri.table()) 52 | -------------------------------------------------------------------------------- /types/index.d.ts: -------------------------------------------------------------------------------- 1 | type FastUri = typeof fastUri 2 | 3 | declare namespace fastUri { 4 | export interface URIComponent { 5 | scheme?: string; 6 | userinfo?: string; 7 | host?: string; 8 | port?: number | string; 9 | path?: string; 10 | query?: string; 11 | fragment?: string; 12 | reference?: string; 13 | nid?: string; 14 | nss?: string; 15 | resourceName?: string; 16 | secure?: boolean; 17 | uuid?: string; 18 | error?: string; 19 | } 20 | export interface Options { 21 | scheme?: string; 22 | reference?: string; 23 | unicodeSupport?: boolean; 24 | domainHost?: boolean; 25 | absolutePath?: boolean; 26 | tolerant?: boolean; 27 | skipEscape?: boolean; 28 | nid?: string; 29 | } 30 | 31 | /** 32 | * @deprecated Use Options instead 33 | */ 34 | export type options = Options 35 | /** 36 | * @deprecated Use URIComponent instead 37 | */ 38 | export type URIComponents = URIComponent 39 | 40 | export function normalize (uri: string, opts?: Options): string 41 | export function normalize (uri: URIComponent, opts?: Options): URIComponent 42 | export function normalize (uri: any, opts?: Options): any 43 | 44 | export function resolve (baseURI: string, relativeURI: string, options?: Options): string 45 | 46 | export function resolveComponent (base: URIComponent, relative: URIComponent, options?: Options, skipNormalization?: boolean): URIComponent 47 | 48 | export function parse (uri: string, opts?: Options): URIComponent 49 | 50 | export function serialize (component: URIComponent, opts?: Options): string 51 | 52 | export function equal (uriA: string, uriB: string): boolean 53 | 54 | export function resolve (base: string, path: string): string 55 | 56 | export const fastUri: FastUri 57 | export { fastUri as default } 58 | } 59 | 60 | export = fastUri 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2021, Gary Court until https://github.com/garycourt/uri-js/commit/a1acf730b4bba3f1097c9f52e7d9d3aba8cdcaae 2 | Copyright (c) 2021-present The Fastify team 3 | All rights reserved. 4 | 5 | The Fastify team members are listed at https://github.com/fastify/fastify#team. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | * The names of any contributors may not be used to endorse or promote 15 | products derived from this software without specific prior written 16 | permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY 22 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | * * * 30 | 31 | The complete list of contributors can be found at: 32 | - https://github.com/garycourt/uri-js/graphs/contributors -------------------------------------------------------------------------------- /benchmark/ws-is-secure.mjs: -------------------------------------------------------------------------------- 1 | import { Bench } from 'tinybench' 2 | import { wsIsSecure } from '../lib/schemes.js' 3 | 4 | const benchWsIsSecure = new Bench({ name: 'wsIsSecure' }) 5 | 6 | const wsComponentAttributeSecureTrue = { 7 | scheme: 'ws', 8 | secure: true, 9 | } 10 | 11 | const wsComponentAttributeSecureFalse = { 12 | scheme: 'ws', 13 | secure: false, 14 | } 15 | 16 | const wssComponent = { 17 | scheme: 'wss', 18 | } 19 | 20 | const wssComponentMixedCase = { 21 | scheme: 'Wss', 22 | } 23 | 24 | const wssComponentUpperCase = { 25 | scheme: 'WSS', 26 | } 27 | 28 | const httpComponent = { 29 | scheme: 'http', 30 | } 31 | 32 | console.assert(wsIsSecure(wsComponentAttributeSecureTrue) === true, 'wsComponentAttributeSecureTrue should be secure') 33 | console.assert(wsIsSecure(wsComponentAttributeSecureFalse) === false, 'wsComponentAttributeSecureFalse should not be secure') 34 | console.assert(wsIsSecure(wssComponent) === true, 'wssComponent should be secure') 35 | console.assert(wsIsSecure(wssComponentMixedCase) === true, 'wssComponentMixedCase should be secure') 36 | console.assert(wsIsSecure(wssComponentUpperCase) === true, 'wssComponentUpperCase should be secure') 37 | console.assert(wsIsSecure(httpComponent) === false, 'httpComponent should not be secure') 38 | 39 | benchWsIsSecure.add(JSON.stringify(wsComponentAttributeSecureFalse), function () { 40 | wsIsSecure(wsComponentAttributeSecureFalse) 41 | }) 42 | 43 | benchWsIsSecure.add(JSON.stringify(wsComponentAttributeSecureTrue), function () { 44 | wsIsSecure(wsComponentAttributeSecureTrue) 45 | }) 46 | 47 | benchWsIsSecure.add(JSON.stringify(wssComponent), function () { 48 | wsIsSecure(wssComponent) 49 | }) 50 | 51 | benchWsIsSecure.add(JSON.stringify(wssComponentMixedCase), function () { 52 | wsIsSecure(wssComponentMixedCase) 53 | }) 54 | 55 | benchWsIsSecure.add(JSON.stringify(wssComponentUpperCase), function () { 56 | wsIsSecure(wssComponentUpperCase) 57 | }) 58 | 59 | benchWsIsSecure.add(JSON.stringify(httpComponent), function () { 60 | wsIsSecure(httpComponent) 61 | }) 62 | 63 | await benchWsIsSecure.run() 64 | console.log(benchWsIsSecure.name) 65 | console.table(benchWsIsSecure.table()) 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fast-uri", 3 | "description": "Dependency-free RFC 3986 URI toolbox", 4 | "version": "3.1.0", 5 | "main": "index.js", 6 | "type": "commonjs", 7 | "types": "types/index.d.ts", 8 | "license": "BSD-3-Clause", 9 | "author": "Vincent Le Goff (https://github.com/zekth)", 10 | "contributors": [ 11 | { 12 | "name": "Matteo Collina", 13 | "email": "hello@matteocollina.com" 14 | }, 15 | { 16 | "name": "Gürgün Dayıoğlu", 17 | "email": "hey@gurgun.day", 18 | "url": "https://heyhey.to/G" 19 | }, 20 | { 21 | "name": "Aras Abbasi", 22 | "email": "aras.abbasi@gmail.com" 23 | }, 24 | { 25 | "name": "Frazer Smith", 26 | "email": "frazer.dev@icloud.com", 27 | "url": "https://github.com/fdawgs" 28 | } 29 | ], 30 | "repository": { 31 | "type": "git", 32 | "url": "git+https://github.com/fastify/fast-uri.git" 33 | }, 34 | "bugs": { 35 | "url": "https://github.com/fastify/fast-uri/issues" 36 | }, 37 | "homepage": "https://github.com/fastify/fast-uri", 38 | "funding": [ 39 | { 40 | "type": "github", 41 | "url": "https://github.com/sponsors/fastify" 42 | }, 43 | { 44 | "type": "opencollective", 45 | "url": "https://opencollective.com/fastify" 46 | } 47 | ], 48 | "scripts": { 49 | "lint": "eslint", 50 | "lint:fix": "eslint --fix", 51 | "test": "npm run test:unit && npm run test:typescript", 52 | "test:browser:chromium": "playwright-test ./test/* --runner tape --browser=chromium", 53 | "test:browser:firefox": "playwright-test ./test/* --runner tape --browser=firefox", 54 | "test:browser:webkit": "playwright-test ./test/* --runner tape --browser=webkit", 55 | "test:browser": "npm run test:browser:chromium && npm run test:browser:firefox && npm run test:browser:webkit", 56 | "test:unit": "tape test/**/*.js", 57 | "test:unit:dev": "npm run test:unit -- --coverage-report=html", 58 | "test:typescript": "tsd" 59 | }, 60 | "devDependencies": { 61 | "ajv": "^8.16.0", 62 | "eslint": "^9.17.0", 63 | "neostandard": "^0.12.0", 64 | "playwright-test": "^14.1.12", 65 | "tape": "^5.8.1", 66 | "tsd": "^0.33.0" 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /test/fixtures/uri-js-serialize.json: -------------------------------------------------------------------------------- 1 | [ 2 | [ 3 | { 4 | "host": "10.10.10.10.example.com" 5 | }, 6 | "//10.10.10.10.example.com" 7 | ], 8 | [ 9 | { 10 | "host": "2001:db8::7" 11 | }, 12 | "//[2001:db8::7]" 13 | ], 14 | [ 15 | { 16 | "host": "::ffff:129.144.52.38" 17 | }, 18 | "//[::ffff:129.144.52.38]" 19 | ], 20 | [ 21 | { 22 | "host": "2606:2800:220:1:248:1893:25c8:1946" 23 | }, 24 | "//[2606:2800:220:1:248:1893:25c8:1946]" 25 | ], 26 | [ 27 | { 28 | "host": "10.10.10.10.example.com" 29 | }, 30 | "//10.10.10.10.example.com" 31 | ], 32 | [ 33 | { 34 | "host": "10.10.10.10" 35 | }, 36 | "//10.10.10.10" 37 | ], 38 | [ 39 | { 40 | "path": "?query" 41 | }, 42 | "%3Fquery" 43 | ], 44 | [ 45 | { 46 | "path": "foo:bar" 47 | }, 48 | "foo%3Abar" 49 | ], 50 | [ 51 | { 52 | "path": "//path" 53 | }, 54 | "/%2Fpath" 55 | ], 56 | [ 57 | { 58 | "scheme": "uri", 59 | "host": "example.com", 60 | "port": "9000" 61 | }, 62 | "uri://example.com:9000" 63 | ], 64 | [ 65 | { 66 | "scheme": "uri", 67 | "userinfo": "foo:bar", 68 | "host": "example.com", 69 | "port": 1, 70 | "path": "path", 71 | "query": "query", 72 | "fragment": "fragment" 73 | }, 74 | "uri://foo:bar@example.com:1/path?query#fragment" 75 | ], 76 | [ 77 | { 78 | "scheme": "", 79 | "userinfo": "", 80 | "host": "", 81 | "port": 0, 82 | "path": "", 83 | "query": "", 84 | "fragment": "" 85 | }, 86 | "//@:0?#" 87 | ], 88 | [ 89 | {}, 90 | "" 91 | ], 92 | [ 93 | { 94 | "host": "fe80::a%en1" 95 | }, 96 | "//[fe80::a%25en1]" 97 | ], 98 | [ 99 | { 100 | "host": "fe80::a%25en1" 101 | }, 102 | "//[fe80::a%25en1]" 103 | ], 104 | [ 105 | { 106 | "scheme": "wss", 107 | "host": "example.com", 108 | "path": "/foo", 109 | "query": "bar" 110 | }, 111 | "wss://example.com/foo?bar" 112 | ], 113 | [ 114 | { 115 | "scheme": "scheme", 116 | "path": "with:colon" 117 | }, 118 | "scheme:with:colon" 119 | ] 120 | ] -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | .pnpm-debug.log* 9 | 10 | # Diagnostic reports (https://nodejs.org/api/report.html) 11 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | *.pid.lock 18 | 19 | # Directory for instrumented libs generated by jscoverage/JSCover 20 | lib-cov 21 | 22 | # Coverage directory used by tools like istanbul 23 | coverage 24 | *.lcov 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # Snowpack dependency directory (https://snowpack.dev/) 46 | web_modules/ 47 | 48 | # TypeScript cache 49 | *.tsbuildinfo 50 | 51 | # Optional npm cache directory 52 | .npm 53 | 54 | # Optional eslint cache 55 | .eslintcache 56 | 57 | # Optional stylelint cache 58 | .stylelintcache 59 | 60 | # Microbundle cache 61 | .rpt2_cache/ 62 | .rts2_cache_cjs/ 63 | .rts2_cache_es/ 64 | .rts2_cache_umd/ 65 | 66 | # Optional REPL history 67 | .node_repl_history 68 | 69 | # Output of 'npm pack' 70 | *.tgz 71 | 72 | # Yarn Integrity file 73 | .yarn-integrity 74 | 75 | # dotenv environment variable files 76 | .env 77 | .env.development.local 78 | .env.test.local 79 | .env.production.local 80 | .env.local 81 | 82 | # parcel-bundler cache (https://parceljs.org/) 83 | .cache 84 | .parcel-cache 85 | 86 | # Next.js build output 87 | .next 88 | out 89 | 90 | # Nuxt.js build / generate output 91 | .nuxt 92 | dist 93 | 94 | # Gatsby files 95 | .cache/ 96 | # Comment in the public line in if your project uses Gatsby and not Next.js 97 | # https://nextjs.org/blog/next-9-1#public-directory-support 98 | # public 99 | 100 | # vuepress build output 101 | .vuepress/dist 102 | 103 | # vuepress v2.x temp and cache directory 104 | .temp 105 | .cache 106 | 107 | # Docusaurus cache and generated files 108 | .docusaurus 109 | 110 | # Serverless directories 111 | .serverless/ 112 | 113 | # FuseBox cache 114 | .fusebox/ 115 | 116 | # DynamoDB Local files 117 | .dynamodb/ 118 | 119 | # TernJS port file 120 | .tern-port 121 | 122 | # Stores VSCode versions used for testing VSCode extensions 123 | .vscode-test 124 | 125 | # yarn v2 126 | .yarn/cache 127 | .yarn/unplugged 128 | .yarn/build-state.yml 129 | .yarn/install-state.gz 130 | .pnp.* 131 | 132 | # Vim swap files 133 | *.swp 134 | 135 | # macOS files 136 | .DS_Store 137 | 138 | # Clinic 139 | .clinic 140 | 141 | # lock files 142 | bun.lockb 143 | package-lock.json 144 | pnpm-lock.yaml 145 | yarn.lock 146 | 147 | # editor files 148 | .vscode 149 | .idea 150 | 151 | #tap files 152 | .tap/ 153 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | - next 8 | - 'v*' 9 | paths-ignore: 10 | - 'docs/**' 11 | - '*.md' 12 | pull_request: 13 | paths-ignore: 14 | - 'docs/**' 15 | - '*.md' 16 | 17 | # This allows a subsequently queued workflow run to interrupt previous runs 18 | concurrency: 19 | group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}" 20 | cancel-in-progress: true 21 | 22 | permissions: 23 | contents: read 24 | 25 | jobs: 26 | test-regression-check-node10: 27 | name: Test compatibility with Node.js 10 28 | runs-on: ubuntu-latest 29 | permissions: 30 | contents: read 31 | steps: 32 | - uses: actions/checkout@v6 33 | with: 34 | persist-credentials: false 35 | 36 | - uses: actions/setup-node@v6 37 | with: 38 | node-version: '10' 39 | cache: 'npm' 40 | cache-dependency-path: package.json 41 | check-latest: true 42 | 43 | - name: Install 44 | run: | 45 | npm install --ignore-scripts 46 | 47 | - name: Copy project as fast-uri to node_node_modules 48 | run: | 49 | rm -rf ./node_modules/fast-uri/lib && 50 | rm -rf ./node_modules/fast-uri/index.js && 51 | cp -r ./lib ./node_modules/fast-uri/lib && 52 | cp ./index.js ./node_modules/fast-uri/index.js 53 | 54 | - name: Run tests 55 | run: | 56 | npm run test:unit 57 | env: 58 | NODE_OPTIONS: no-network-family-autoselection 59 | 60 | test-browser: 61 | name: Test browser compatibility 62 | runs-on: ${{ matrix.os }} 63 | strategy: 64 | fail-fast: false 65 | matrix: 66 | os: ['ubuntu-latest', 'windows-latest', 'macos-latest'] 67 | browser: ['chromium', 'firefox', 'webkit'] 68 | exclude: 69 | - os: ubuntu-latest 70 | browser: webkit 71 | permissions: 72 | contents: read 73 | steps: 74 | - uses: actions/checkout@v6 75 | with: 76 | persist-credentials: false 77 | 78 | - uses: actions/setup-node@v6 79 | with: 80 | node-version: '24' 81 | cache: 'npm' 82 | cache-dependency-path: package.json 83 | check-latest: true 84 | 85 | - name: Install dependencies 86 | run: | 87 | npm install --ignore-scripts 88 | 89 | - if: ${{ matrix.os == 'windows-latest' }} 90 | run: npx playwright install winldd 91 | 92 | - name: Run browser tests 93 | run: | 94 | npm run test:browser:${{ matrix.browser }} 95 | 96 | test: 97 | needs: 98 | - test-regression-check-node10 99 | permissions: 100 | contents: write 101 | pull-requests: write 102 | uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5 103 | with: 104 | license-check: true 105 | lint: true 106 | node-versions: '["16", "18", "20", "22", "24"]' 107 | -------------------------------------------------------------------------------- /test/equal.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | const fn = fastURI.equal 7 | const runTest = (t, suite) => { 8 | suite.forEach(s => { 9 | const operator = s.result ? '==' : '!=' 10 | t.equal(fn(s.pair[0], s.pair[1]), s.result, `${s.pair[0]} ${operator} ${s.pair[1]}`) 11 | t.equal(fn(s.pair[1], s.pair[0]), s.result, `${s.pair[1]} ${operator} ${s.pair[0]}`) 12 | }) 13 | } 14 | 15 | test('URI Equals', (t) => { 16 | const suite = [ 17 | { pair: ['example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'], result: true }, // test from RFC 3986 18 | { pair: ['http://example.org/~user', 'http://example.org/%7euser'], result: true } // test from RFC 3987 19 | ] 20 | runTest(t, suite) 21 | t.end() 22 | }) 23 | 24 | // test('IRI Equals', (t) => { 25 | // // example from RFC 3987 26 | // t.equal(URI.equal('example://a/b/c/%7Bfoo%7D/ros\xE9', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9', IRI_OPTION), true) 27 | // t.end() 28 | // }) 29 | 30 | test('HTTP Equals', (t) => { 31 | const suite = [ 32 | // test from RFC 2616 33 | { pair: ['http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'], result: true }, 34 | { pair: [{ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'], result: true }, 35 | { pair: ['http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'], result: true }, 36 | { pair: ['http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'], result: true }, 37 | { pair: ['HTTP://ABC.COM', 'http://abc.com/'], result: true }, 38 | // test from RFC 3986 39 | { pair: ['http://example.com:/', 'http://example.com:80/'], result: true } 40 | ] 41 | runTest(t, suite) 42 | t.end() 43 | }) 44 | 45 | test('HTTPS Equals', (t) => { 46 | const suite = [ 47 | { pair: ['https://example.com', 'https://example.com:443/'], result: true }, 48 | { pair: ['https://example.com:/', 'https://example.com:443/'], result: true } 49 | ] 50 | runTest(t, suite) 51 | t.end() 52 | }) 53 | 54 | test('URN Equals', (t) => { 55 | const suite = [ 56 | // test from RFC 2141 57 | { pair: ['urn:foo:a123,456', 'urn:foo:a123,456'], result: true }, 58 | { pair: ['urn:foo:a123,456', 'URN:foo:a123,456'], result: true }, 59 | { pair: ['urn:foo:a123,456', 'urn:FOO:a123,456'], result: true } 60 | ] 61 | 62 | // Disabling for now as the whole equal logic might need 63 | // to be refactored 64 | // t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false) 65 | // t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true) 66 | 67 | runTest(t, suite) 68 | 69 | t.throws(() => { 70 | fn('urn:', 'urn:FOO:a123,456') 71 | }, 'URN without nid cannot be serialized') 72 | 73 | t.end() 74 | }) 75 | 76 | test('UUID Equals', (t) => { 77 | const suite = [ 78 | { pair: ['URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'], result: true } 79 | ] 80 | 81 | runTest(t, suite) 82 | t.end() 83 | }) 84 | 85 | // test('Mailto Equals', (t) => { 86 | // // tests from RFC 6068 87 | // t.equal(URI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true) 88 | // t.equal(URI.equal('mailto:?to=addr1@an.example,addr2@an.example', 'mailto:addr1@an.example?to=addr2@an.example'), true) 89 | // t.end() 90 | // }) 91 | 92 | test('WS Equal', (t) => { 93 | const suite = [ 94 | { pair: ['WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'], result: true } 95 | ] 96 | 97 | runTest(t, suite) 98 | t.end() 99 | }) 100 | 101 | test('WSS Equal', (t) => { 102 | const suite = [ 103 | { pair: ['WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'], result: true } 104 | ] 105 | 106 | runTest(t, suite) 107 | t.end() 108 | }) 109 | -------------------------------------------------------------------------------- /test/resolve.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | test('URI Resolving', (t) => { 7 | // normal examples from RFC 3986 8 | const base = 'uri://a/b/c/d;p?q' 9 | t.equal(fastURI.resolve(base, 'g:h'), 'g:h', 'g:h') 10 | t.equal(fastURI.resolve(base, 'g:h'), 'g:h', 'g:h') 11 | t.equal(fastURI.resolve(base, 'g'), 'uri://a/b/c/g', 'g') 12 | t.equal(fastURI.resolve(base, './g'), 'uri://a/b/c/g', './g') 13 | t.equal(fastURI.resolve(base, 'g/'), 'uri://a/b/c/g/', 'g/') 14 | t.equal(fastURI.resolve(base, '/g'), 'uri://a/g', '/g') 15 | t.equal(fastURI.resolve(base, '//g'), 'uri://g', '//g') 16 | t.equal(fastURI.resolve(base, '?y'), 'uri://a/b/c/d;p?y', '?y') 17 | t.equal(fastURI.resolve(base, 'g?y'), 'uri://a/b/c/g?y', 'g?y') 18 | t.equal(fastURI.resolve(base, '#s'), 'uri://a/b/c/d;p?q#s', '#s') 19 | t.equal(fastURI.resolve(base, 'g#s'), 'uri://a/b/c/g#s', 'g#s') 20 | t.equal(fastURI.resolve(base, 'g?y#s'), 'uri://a/b/c/g?y#s', 'g?y#s') 21 | t.equal(fastURI.resolve(base, ';x'), 'uri://a/b/c/;x', ';x') 22 | t.equal(fastURI.resolve(base, 'g;x'), 'uri://a/b/c/g;x', 'g;x') 23 | t.equal(fastURI.resolve(base, 'g;x?y#s'), 'uri://a/b/c/g;x?y#s', 'g;x?y#s') 24 | t.equal(fastURI.resolve(base, ''), 'uri://a/b/c/d;p?q', '') 25 | t.equal(fastURI.resolve(base, '.'), 'uri://a/b/c/', '.') 26 | t.equal(fastURI.resolve(base, './'), 'uri://a/b/c/', './') 27 | t.equal(fastURI.resolve(base, '..'), 'uri://a/b/', '..') 28 | t.equal(fastURI.resolve(base, '../'), 'uri://a/b/', '../') 29 | t.equal(fastURI.resolve(base, '../g'), 'uri://a/b/g', '../g') 30 | t.equal(fastURI.resolve(base, '../..'), 'uri://a/', '../..') 31 | t.equal(fastURI.resolve(base, '../../'), 'uri://a/', '../../') 32 | t.equal(fastURI.resolve(base, '../../g'), 'uri://a/g', '../../g') 33 | 34 | // abnormal examples from RFC 3986 35 | t.equal(fastURI.resolve(base, '../../../g'), 'uri://a/g', '../../../g') 36 | t.equal(fastURI.resolve(base, '../../../../g'), 'uri://a/g', '../../../../g') 37 | 38 | t.equal(fastURI.resolve(base, '/./g'), 'uri://a/g', '/./g') 39 | t.equal(fastURI.resolve(base, '/../g'), 'uri://a/g', '/../g') 40 | t.equal(fastURI.resolve(base, 'g.'), 'uri://a/b/c/g.', 'g.') 41 | t.equal(fastURI.resolve(base, '.g'), 'uri://a/b/c/.g', '.g') 42 | t.equal(fastURI.resolve(base, 'g..'), 'uri://a/b/c/g..', 'g..') 43 | t.equal(fastURI.resolve(base, '..g'), 'uri://a/b/c/..g', '..g') 44 | 45 | t.equal(fastURI.resolve(base, './../g'), 'uri://a/b/g', './../g') 46 | t.equal(fastURI.resolve(base, './g/.'), 'uri://a/b/c/g/', './g/.') 47 | t.equal(fastURI.resolve(base, 'g/./h'), 'uri://a/b/c/g/h', 'g/./h') 48 | t.equal(fastURI.resolve(base, 'g/../h'), 'uri://a/b/c/h', 'g/../h') 49 | t.equal(fastURI.resolve(base, 'g;x=1/./y'), 'uri://a/b/c/g;x=1/y', 'g;x=1/./y') 50 | t.equal(fastURI.resolve(base, 'g;x=1/../y'), 'uri://a/b/c/y', 'g;x=1/../y') 51 | 52 | t.equal(fastURI.resolve(base, 'g?y/./x'), 'uri://a/b/c/g?y/./x', 'g?y/./x') 53 | t.equal(fastURI.resolve(base, 'g?y/../x'), 'uri://a/b/c/g?y/../x', 'g?y/../x') 54 | t.equal(fastURI.resolve(base, 'g#s/./x'), 'uri://a/b/c/g#s/./x', 'g#s/./x') 55 | t.equal(fastURI.resolve(base, 'g#s/../x'), 'uri://a/b/c/g#s/../x', 'g#s/../x') 56 | 57 | t.equal(fastURI.resolve(base, 'uri:g'), 'uri:g', 'uri:g') 58 | t.equal(fastURI.resolve(base, 'uri:g', {}), 'uri:g', 'uri:g') 59 | t.equal(fastURI.resolve(base, 'uri:g', { tolerant: undefined }), 'uri:g', 'uri:g') 60 | t.equal(fastURI.resolve(base, 'uri:g', { tolerant: false }), 'uri:g', 'uri:g') 61 | t.equal(fastURI.resolve(base, 'uri:g', { tolerant: true }), 'uri://a/b/c/g', 'uri:g') 62 | 63 | // examples by PAEz 64 | // example was provided to avoid infinite loop within regex 65 | // this is not the case anymore 66 | // t.equal(URI.resolve('//www.g.com/', '/adf\ngf'), '//www.g.com/adf%0Agf', '/adf\\ngf') 67 | // t.equal(URI.resolve('//www.g.com/error\n/bleh/bleh', '..'), '//www.g.com/error%0A/', '//www.g.com/error\\n/bleh/bleh') 68 | t.end() 69 | }) 70 | 71 | test('URN Resolving', (t) => { 72 | // example from epoberezkin 73 | t.equal(fastURI.resolve('', 'urn:some:ip:prop'), 'urn:some:ip:prop', 'urn:some:ip:prop') 74 | t.equal(fastURI.resolve('#', 'urn:some:ip:prop'), 'urn:some:ip:prop', 'urn:some:ip:prop') 75 | t.equal(fastURI.resolve('urn:some:ip:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop', 'urn:some:ip:prop') 76 | t.equal(fastURI.resolve('urn:some:other:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop', 'urn:some:ip:prop') 77 | t.end() 78 | }) 79 | -------------------------------------------------------------------------------- /benchmark/benchmark.mjs: -------------------------------------------------------------------------------- 1 | import { Bench } from 'tinybench' 2 | import { fastUri } from '../index.js' 3 | import { parse as uriJsParse, serialize as uriJsSerialize, resolve as uriJsResolve, equal as uriJsEqual } from 'uri-js' 4 | 5 | const base = 'uri://a/b/c/d;p?q' 6 | 7 | const domain = 'https://example.com/foo#bar$fiz' 8 | const ipv4 = '//10.10.10.10' 9 | const ipv6 = '//[2001:db8::7]' 10 | const urn = 'urn:foo:a123,456' 11 | const urnuuid = 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 12 | 13 | const urnuuidComponent = { 14 | scheme: 'urn', 15 | nid: 'uuid', 16 | uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 17 | } 18 | 19 | const { 20 | parse: fastUriParse, 21 | serialize: fastUriSerialize, 22 | resolve: fastUriResolve, 23 | equal: fastUriEqual, 24 | } = fastUri 25 | 26 | // Initialization as there is a lot to parse at first 27 | // eg: regexes 28 | fastUriParse(domain) 29 | uriJsParse(domain) 30 | 31 | const benchFastUri = new Bench({ name: 'fast-uri benchmark' }) 32 | const benchUriJs = new Bench({ name: 'uri-js benchmark' }) 33 | const benchWHATWG = new Bench({ name: 'WHATWG URL benchmark' }) 34 | 35 | benchFastUri.add('fast-uri: parse domain', function () { 36 | fastUriParse(domain) 37 | }) 38 | benchUriJs.add('urijs: parse domain', function () { 39 | uriJsParse(domain) 40 | }) 41 | benchWHATWG.add('WHATWG URL: parse domain', function () { 42 | // eslint-disable-next-line 43 | new URL(domain) 44 | }) 45 | benchFastUri.add('fast-uri: parse IPv4', function () { 46 | fastUriParse(ipv4) 47 | }) 48 | benchUriJs.add('urijs: parse IPv4', function () { 49 | uriJsParse(ipv4) 50 | }) 51 | benchFastUri.add('fast-uri: parse IPv6', function () { 52 | fastUriParse(ipv6) 53 | }) 54 | benchUriJs.add('urijs: parse IPv6', function () { 55 | uriJsParse(ipv6) 56 | }) 57 | benchFastUri.add('fast-uri: parse URN', function () { 58 | fastUriParse(urn) 59 | }) 60 | benchUriJs.add('urijs: parse URN', function () { 61 | uriJsParse(urn) 62 | }) 63 | benchWHATWG.add('WHATWG URL: parse URN', function () { 64 | // eslint-disable-next-line 65 | new URL(urn) 66 | }) 67 | benchFastUri.add('fast-uri: parse URN uuid', function () { 68 | fastUriParse(urnuuid) 69 | }) 70 | benchUriJs.add('urijs: parse URN uuid', function () { 71 | uriJsParse(urnuuid) 72 | }) 73 | benchFastUri.add('fast-uri: serialize URN uuid', function () { 74 | fastUriSerialize(urnuuidComponent) 75 | }) 76 | benchUriJs.add('uri-js: serialize URN uuid', function () { 77 | uriJsSerialize(urnuuidComponent) 78 | }) 79 | benchFastUri.add('fast-uri: serialize uri', function () { 80 | fastUriSerialize({ 81 | scheme: 'uri', 82 | userinfo: 'foo:bar', 83 | host: 'example.com', 84 | port: 1, 85 | path: 'path', 86 | query: 'query', 87 | fragment: 'fragment' 88 | }) 89 | }) 90 | benchUriJs.add('urijs: serialize uri', function () { 91 | uriJsSerialize({ 92 | scheme: 'uri', 93 | userinfo: 'foo:bar', 94 | host: 'example.com', 95 | port: 1, 96 | path: 'path', 97 | query: 'query', 98 | fragment: 'fragment' 99 | }) 100 | }) 101 | benchFastUri.add('fast-uri: serialize long uri with dots', function () { 102 | fastUriSerialize({ 103 | scheme: 'uri', 104 | userinfo: 'foo:bar', 105 | host: 'example.com', 106 | port: 1, 107 | path: './a/./b/c/../.././d/../e/f/.././/', 108 | query: 'query', 109 | fragment: 'fragment' 110 | }) 111 | }) 112 | benchUriJs.add('urijs: serialize long uri with dots', function () { 113 | uriJsSerialize({ 114 | scheme: 'uri', 115 | userinfo: 'foo:bar', 116 | host: 'example.com', 117 | port: 1, 118 | path: './a/./b/c/../.././d/../e/f/.././/', 119 | query: 'query', 120 | fragment: 'fragment' 121 | }) 122 | }) 123 | benchFastUri.add('fast-uri: serialize IPv6', function () { 124 | fastUriSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' }) 125 | }) 126 | benchUriJs.add('urijs: serialize IPv6', function () { 127 | uriJsSerialize({ host: '2606:2800:220:1:248:1893:25c8:1946' }) 128 | }) 129 | benchFastUri.add('fast-uri: serialize ws', function () { 130 | fastUriSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true }) 131 | }) 132 | benchUriJs.add('urijs: serialize ws', function () { 133 | uriJsSerialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true }) 134 | }) 135 | benchFastUri.add('fast-uri: resolve', function () { 136 | fastUriResolve(base, '../../../g') 137 | }) 138 | benchUriJs.add('urijs: resolve', function () { 139 | uriJsResolve(base, '../../../g') 140 | }) 141 | 142 | benchFastUri.add('fast-uri: equal', function () { 143 | fastUriEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d') 144 | }) 145 | benchUriJs.add('urijs: equal', function () { 146 | uriJsEqual('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d') 147 | }) 148 | 149 | await benchFastUri.run() 150 | console.log(benchFastUri.name) 151 | console.table(benchFastUri.table()) 152 | 153 | await benchUriJs.run() 154 | console.log(benchUriJs.name) 155 | console.table(benchUriJs.table()) 156 | 157 | await benchWHATWG.run() 158 | console.log(benchWHATWG.name) 159 | console.table(benchWHATWG.table()) 160 | -------------------------------------------------------------------------------- /test/rfc-3986.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | test('RFC 3986', (t) => { 7 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/', secure: true }), 8 | 'http://example.com/', 'http://example.com/') 9 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/foo', secure: true }), 10 | 'http://example.com/foo', 'http://example.com/foo') 11 | 12 | // A. If the input buffer begins with a prefix of "../" or "./", 13 | // then remove that prefix from the input buffer; otherwise, 14 | 15 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '../', secure: true }), 16 | 'http://example.com/', 'http://example.com/') 17 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: './', secure: true }), 18 | 'http://example.com/', 'http://example.com/') 19 | 20 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '../../', secure: true }), 21 | 'http://example.com/', 'http://example.com/') 22 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '././', secure: true }), 23 | 'http://example.com/', 'http://example.com/') 24 | 25 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: './../', secure: true }), 26 | 'http://example.com/', 'http://example.com/') 27 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '.././', secure: true }), 28 | 'http://example.com/', 'http://example.com/') 29 | 30 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '../foo', secure: true }), 31 | 'http://example.com/foo', 'http://example.com/foo') 32 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: './foo', secure: true }), 33 | 'http://example.com/foo', 'http://example.com/foo') 34 | 35 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '../../foo', secure: true }), 36 | 'http://example.com/foo', 'http://example.com/foo') 37 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '././foo', secure: true }), 38 | 'http://example.com/foo', 'http://example.com/foo') 39 | 40 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: './../foo', secure: true }), 41 | 'http://example.com/foo', 'http://example.com/foo') 42 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '.././foo', secure: true }), 43 | 'http://example.com/foo', 'http://example.com/foo') 44 | 45 | // B. if the input buffer begins with a prefix of "/./" or "/.", 46 | // where "." is a complete path segment, then replace that 47 | // prefix with "/" in the input buffer; otherwise, 48 | 49 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/./', secure: true }), 50 | 'http://example.com/', 'http://example.com/') 51 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/.', secure: true }), 52 | 'http://example.com/', 'http://example.com/') 53 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/./foo', secure: true }), 54 | 'http://example.com/foo', 'http://example.com/foo') 55 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/.././foo', secure: true }), 56 | 'http://example.com/foo', 'http://example.com/foo') 57 | 58 | // C. if the input buffer begins with a prefix of "/../" or "/..", 59 | // where ".." is a complete path segment, then replace that 60 | // prefix with "/" in the input buffer and remove the last 61 | // segment and its preceding "/" (if any) from the output 62 | // buffer; otherwise, 63 | 64 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/../', secure: true }), 65 | 'http://example.com/', 'http://example.com/') 66 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/..', secure: true }), 67 | 'http://example.com/', 'http://example.com/') 68 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/../foo', secure: true }), 69 | 'http://example.com/foo', 'http://example.com/foo') 70 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/foo/..', secure: true }), 71 | 'http://example.com/', 'http://example.com/') 72 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/foo/bar/..', secure: true }), 73 | 'http://example.com/foo/', 'http://example.com/foo/') 74 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/foo/../bar/..', secure: true }), 75 | 'http://example.com/', 'http://example.com/') 76 | 77 | // D. if the input buffer consists only of "." or "..", then remove 78 | // that from the input buffer; otherwise, 79 | 80 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/.', secure: true }), 81 | 'http://example.com/', 'http://example.com/') 82 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '/..', secure: true }), 83 | 'http://example.com/', 'http://example.com/') 84 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '.', secure: true }), 85 | 'http://example.com/', 'http://example.com/') 86 | t.strictEqual(fastURI.serialize({ scheme: 'http', host: 'example.com', path: '..', secure: true }), 87 | 'http://example.com/', 'http://example.com/') 88 | 89 | t.end() 90 | }) 91 | -------------------------------------------------------------------------------- /test/serialize.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | test('URI Serialize', (t) => { 7 | let components = { 8 | scheme: undefined, 9 | userinfo: undefined, 10 | host: undefined, 11 | port: undefined, 12 | path: undefined, 13 | query: undefined, 14 | fragment: undefined 15 | } 16 | t.equal(fastURI.serialize(components), '', 'Undefined Components') 17 | 18 | components = { 19 | scheme: '', 20 | userinfo: '', 21 | host: '', 22 | port: 0, 23 | path: '', 24 | query: '', 25 | fragment: '' 26 | } 27 | t.equal(fastURI.serialize(components), '//@:0?#', 'Empty Components') 28 | 29 | components = { 30 | scheme: 'uri', 31 | userinfo: 'foo:bar', 32 | host: 'example.com', 33 | port: 1, 34 | path: 'path', 35 | query: 'query', 36 | fragment: 'fragment' 37 | } 38 | t.equal(fastURI.serialize(components), 'uri://foo:bar@example.com:1/path?query#fragment', 'All Components') 39 | 40 | components = { 41 | scheme: 'uri', 42 | host: 'example.com', 43 | port: '9000' 44 | } 45 | t.equal(fastURI.serialize(components), 'uri://example.com:9000', 'String port') 46 | 47 | t.equal(fastURI.serialize({ path: '//path' }), '/%2Fpath', 'Double slash path') 48 | t.equal(fastURI.serialize({ path: 'foo:bar' }), 'foo%3Abar', 'Colon path') 49 | t.equal(fastURI.serialize({ path: '?query' }), '%3Fquery', 'Query path') 50 | 51 | t.equal(fastURI.serialize({ host: '10.10.10.10' }), '//10.10.10.10', 'IPv4address') 52 | 53 | // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4) 54 | t.equal(fastURI.serialize({ host: '10.10.10.10.example.com' }), '//10.10.10.10.example.com', 'Mixed IPv4address & reg-name') 55 | 56 | // IPv6address 57 | t.equal(fastURI.serialize({ host: '2001:db8::7' }), '//[2001:db8::7]', 'IPv6 Host') 58 | t.equal(fastURI.serialize({ host: '::ffff:129.144.52.38' }), '//[::ffff:129.144.52.38]', 'IPv6 Mixed Host') 59 | t.equal(fastURI.serialize({ host: '2606:2800:220:1:248:1893:25c8:1946' }), '//[2606:2800:220:1:248:1893:25c8:1946]', 'IPv6 Full Host') 60 | 61 | // IPv6address with zone identifier, RFC 6874 62 | t.equal(fastURI.serialize({ host: 'fe80::a%en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Unescaped Host') 63 | t.equal(fastURI.serialize({ host: 'fe80::a%25en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Escaped Host') 64 | 65 | t.end() 66 | }) 67 | 68 | test('WS serialize', (t) => { 69 | t.equal(fastURI.serialize({ scheme: 'ws' }), 'ws:') 70 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com' }), 'ws://example.com') 71 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/' }), 'ws:') 72 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/foo' }), 'ws:/foo') 73 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/foo?bar' }), 'ws:/foo?bar') 74 | t.equal(fastURI.serialize({ scheme: 'ws', secure: false }), 'ws:') 75 | t.equal(fastURI.serialize({ scheme: 'ws', secure: true }), 'wss:') 76 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo' }), 'ws://example.com/foo') 77 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar' }), 'ws://example.com/foo?bar') 78 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', secure: false }), 'ws://example.com') 79 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', secure: true }), 'wss://example.com') 80 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar') 81 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar') 82 | t.end() 83 | }) 84 | 85 | test('WSS serialize', (t) => { 86 | t.equal(fastURI.serialize({ scheme: 'wss' }), 'wss:') 87 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com' }), 'wss://example.com') 88 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/' }), 'wss:') 89 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/foo' }), 'wss:/foo') 90 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/foo?bar' }), 'wss:/foo?bar') 91 | t.equal(fastURI.serialize({ scheme: 'wss', secure: false }), 'ws:') 92 | t.equal(fastURI.serialize({ scheme: 'wss', secure: true }), 'wss:') 93 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo' }), 'wss://example.com/foo') 94 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar' }), 'wss://example.com/foo?bar') 95 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', secure: false }), 'ws://example.com') 96 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', secure: true }), 'wss://example.com') 97 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar') 98 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar') 99 | 100 | t.end() 101 | }) 102 | 103 | test('URN serialize', (t) => { 104 | // example from RFC 2141 105 | const components = { 106 | scheme: 'urn', 107 | nid: 'foo', 108 | nss: 'a123,456' 109 | } 110 | t.equal(fastURI.serialize(components), 'urn:foo:a123,456') 111 | // example from RFC 4122 112 | let uuidcomponents = { 113 | scheme: 'urn', 114 | nid: 'uuid', 115 | uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 116 | } 117 | t.equal(fastURI.serialize(uuidcomponents), 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6') 118 | 119 | uuidcomponents = { 120 | scheme: 'urn', 121 | nid: 'uuid', 122 | uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6' 123 | } 124 | t.equal(fastURI.serialize(uuidcomponents), 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6') 125 | 126 | uuidcomponents = { 127 | scheme: 'urn', 128 | nid: undefined, 129 | uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6' 130 | } 131 | t.throws(() => { fastURI.serialize(uuidcomponents) }, 'URN without nid cannot be serialized') 132 | 133 | t.end() 134 | }) 135 | test('URN NID Override', (t) => { 136 | let components = fastURI.parse('urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', { nid: 'uuid' }) 137 | t.equal(components.error, undefined, 'errors') 138 | t.equal(components.scheme, 'urn', 'scheme') 139 | t.equal(components.path, undefined, 'path') 140 | t.equal(components.nid, 'foo', 'nid') 141 | t.equal(components.nss, undefined, 'nss') 142 | t.equal(components.uuid, 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'uuid') 143 | 144 | components = { 145 | scheme: 'urn', 146 | nid: 'foo', 147 | uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 148 | } 149 | t.equal(fastURI.serialize(components, { nid: 'uuid' }), 'urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6') 150 | t.end() 151 | }) 152 | -------------------------------------------------------------------------------- /lib/schemes.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { isUUID } = require('./utils') 4 | const URN_REG = /([\da-z][\d\-a-z]{0,31}):((?:[\w!$'()*+,\-.:;=@]|%[\da-f]{2})+)/iu 5 | 6 | const supportedSchemeNames = /** @type {const} */ (['http', 'https', 'ws', 7 | 'wss', 'urn', 'urn:uuid']) 8 | 9 | /** @typedef {supportedSchemeNames[number]} SchemeName */ 10 | 11 | /** 12 | * @param {string} name 13 | * @returns {name is SchemeName} 14 | */ 15 | function isValidSchemeName (name) { 16 | return supportedSchemeNames.indexOf(/** @type {*} */ (name)) !== -1 17 | } 18 | 19 | /** 20 | * @callback SchemeFn 21 | * @param {import('../types/index').URIComponent} component 22 | * @param {import('../types/index').Options} options 23 | * @returns {import('../types/index').URIComponent} 24 | */ 25 | 26 | /** 27 | * @typedef {Object} SchemeHandler 28 | * @property {SchemeName} scheme - The scheme name. 29 | * @property {boolean} [domainHost] - Indicates if the scheme supports domain hosts. 30 | * @property {SchemeFn} parse - Function to parse the URI component for this scheme. 31 | * @property {SchemeFn} serialize - Function to serialize the URI component for this scheme. 32 | * @property {boolean} [skipNormalize] - Indicates if normalization should be skipped for this scheme. 33 | * @property {boolean} [absolutePath] - Indicates if the scheme uses absolute paths. 34 | * @property {boolean} [unicodeSupport] - Indicates if the scheme supports Unicode. 35 | */ 36 | 37 | /** 38 | * @param {import('../types/index').URIComponent} wsComponent 39 | * @returns {boolean} 40 | */ 41 | function wsIsSecure (wsComponent) { 42 | if (wsComponent.secure === true) { 43 | return true 44 | } else if (wsComponent.secure === false) { 45 | return false 46 | } else if (wsComponent.scheme) { 47 | return ( 48 | wsComponent.scheme.length === 3 && 49 | (wsComponent.scheme[0] === 'w' || wsComponent.scheme[0] === 'W') && 50 | (wsComponent.scheme[1] === 's' || wsComponent.scheme[1] === 'S') && 51 | (wsComponent.scheme[2] === 's' || wsComponent.scheme[2] === 'S') 52 | ) 53 | } else { 54 | return false 55 | } 56 | } 57 | 58 | /** @type {SchemeFn} */ 59 | function httpParse (component) { 60 | if (!component.host) { 61 | component.error = component.error || 'HTTP URIs must have a host.' 62 | } 63 | 64 | return component 65 | } 66 | 67 | /** @type {SchemeFn} */ 68 | function httpSerialize (component) { 69 | const secure = String(component.scheme).toLowerCase() === 'https' 70 | 71 | // normalize the default port 72 | if (component.port === (secure ? 443 : 80) || component.port === '') { 73 | component.port = undefined 74 | } 75 | 76 | // normalize the empty path 77 | if (!component.path) { 78 | component.path = '/' 79 | } 80 | 81 | // NOTE: We do not parse query strings for HTTP URIs 82 | // as WWW Form Url Encoded query strings are part of the HTML4+ spec, 83 | // and not the HTTP spec. 84 | 85 | return component 86 | } 87 | 88 | /** @type {SchemeFn} */ 89 | function wsParse (wsComponent) { 90 | // indicate if the secure flag is set 91 | wsComponent.secure = wsIsSecure(wsComponent) 92 | 93 | // construct resouce name 94 | wsComponent.resourceName = (wsComponent.path || '/') + (wsComponent.query ? '?' + wsComponent.query : '') 95 | wsComponent.path = undefined 96 | wsComponent.query = undefined 97 | 98 | return wsComponent 99 | } 100 | 101 | /** @type {SchemeFn} */ 102 | function wsSerialize (wsComponent) { 103 | // normalize the default port 104 | if (wsComponent.port === (wsIsSecure(wsComponent) ? 443 : 80) || wsComponent.port === '') { 105 | wsComponent.port = undefined 106 | } 107 | 108 | // ensure scheme matches secure flag 109 | if (typeof wsComponent.secure === 'boolean') { 110 | wsComponent.scheme = (wsComponent.secure ? 'wss' : 'ws') 111 | wsComponent.secure = undefined 112 | } 113 | 114 | // reconstruct path from resource name 115 | if (wsComponent.resourceName) { 116 | const [path, query] = wsComponent.resourceName.split('?') 117 | wsComponent.path = (path && path !== '/' ? path : undefined) 118 | wsComponent.query = query 119 | wsComponent.resourceName = undefined 120 | } 121 | 122 | // forbid fragment component 123 | wsComponent.fragment = undefined 124 | 125 | return wsComponent 126 | } 127 | 128 | /** @type {SchemeFn} */ 129 | function urnParse (urnComponent, options) { 130 | if (!urnComponent.path) { 131 | urnComponent.error = 'URN can not be parsed' 132 | return urnComponent 133 | } 134 | const matches = urnComponent.path.match(URN_REG) 135 | if (matches) { 136 | const scheme = options.scheme || urnComponent.scheme || 'urn' 137 | urnComponent.nid = matches[1].toLowerCase() 138 | urnComponent.nss = matches[2] 139 | const urnScheme = `${scheme}:${options.nid || urnComponent.nid}` 140 | const schemeHandler = getSchemeHandler(urnScheme) 141 | urnComponent.path = undefined 142 | 143 | if (schemeHandler) { 144 | urnComponent = schemeHandler.parse(urnComponent, options) 145 | } 146 | } else { 147 | urnComponent.error = urnComponent.error || 'URN can not be parsed.' 148 | } 149 | 150 | return urnComponent 151 | } 152 | 153 | /** @type {SchemeFn} */ 154 | function urnSerialize (urnComponent, options) { 155 | if (urnComponent.nid === undefined) { 156 | throw new Error('URN without nid cannot be serialized') 157 | } 158 | const scheme = options.scheme || urnComponent.scheme || 'urn' 159 | const nid = urnComponent.nid.toLowerCase() 160 | const urnScheme = `${scheme}:${options.nid || nid}` 161 | const schemeHandler = getSchemeHandler(urnScheme) 162 | 163 | if (schemeHandler) { 164 | urnComponent = schemeHandler.serialize(urnComponent, options) 165 | } 166 | 167 | const uriComponent = urnComponent 168 | const nss = urnComponent.nss 169 | uriComponent.path = `${nid || options.nid}:${nss}` 170 | 171 | options.skipEscape = true 172 | return uriComponent 173 | } 174 | 175 | /** @type {SchemeFn} */ 176 | function urnuuidParse (urnComponent, options) { 177 | const uuidComponent = urnComponent 178 | uuidComponent.uuid = uuidComponent.nss 179 | uuidComponent.nss = undefined 180 | 181 | if (!options.tolerant && (!uuidComponent.uuid || !isUUID(uuidComponent.uuid))) { 182 | uuidComponent.error = uuidComponent.error || 'UUID is not valid.' 183 | } 184 | 185 | return uuidComponent 186 | } 187 | 188 | /** @type {SchemeFn} */ 189 | function urnuuidSerialize (uuidComponent) { 190 | const urnComponent = uuidComponent 191 | // normalize UUID 192 | urnComponent.nss = (uuidComponent.uuid || '').toLowerCase() 193 | return urnComponent 194 | } 195 | 196 | const http = /** @type {SchemeHandler} */ ({ 197 | scheme: 'http', 198 | domainHost: true, 199 | parse: httpParse, 200 | serialize: httpSerialize 201 | }) 202 | 203 | const https = /** @type {SchemeHandler} */ ({ 204 | scheme: 'https', 205 | domainHost: http.domainHost, 206 | parse: httpParse, 207 | serialize: httpSerialize 208 | }) 209 | 210 | const ws = /** @type {SchemeHandler} */ ({ 211 | scheme: 'ws', 212 | domainHost: true, 213 | parse: wsParse, 214 | serialize: wsSerialize 215 | }) 216 | 217 | const wss = /** @type {SchemeHandler} */ ({ 218 | scheme: 'wss', 219 | domainHost: ws.domainHost, 220 | parse: ws.parse, 221 | serialize: ws.serialize 222 | }) 223 | 224 | const urn = /** @type {SchemeHandler} */ ({ 225 | scheme: 'urn', 226 | parse: urnParse, 227 | serialize: urnSerialize, 228 | skipNormalize: true 229 | }) 230 | 231 | const urnuuid = /** @type {SchemeHandler} */ ({ 232 | scheme: 'urn:uuid', 233 | parse: urnuuidParse, 234 | serialize: urnuuidSerialize, 235 | skipNormalize: true 236 | }) 237 | 238 | const SCHEMES = /** @type {Record} */ ({ 239 | http, 240 | https, 241 | ws, 242 | wss, 243 | urn, 244 | 'urn:uuid': urnuuid 245 | }) 246 | 247 | Object.setPrototypeOf(SCHEMES, null) 248 | 249 | /** 250 | * @param {string|undefined} scheme 251 | * @returns {SchemeHandler|undefined} 252 | */ 253 | function getSchemeHandler (scheme) { 254 | return ( 255 | scheme && ( 256 | SCHEMES[/** @type {SchemeName} */ (scheme)] || 257 | SCHEMES[/** @type {SchemeName} */(scheme.toLowerCase())]) 258 | ) || 259 | undefined 260 | } 261 | 262 | module.exports = { 263 | wsIsSecure, 264 | SCHEMES, 265 | isValidSchemeName, 266 | getSchemeHandler, 267 | } 268 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fast-uri 2 | 3 |
4 | 5 | [![NPM version](https://img.shields.io/npm/v/fast-uri.svg?style=flat)](https://www.npmjs.com/package/fast-uri) 6 | [![CI](https://github.com/fastify/fast-uri/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fast-uri/actions/workflows/ci.yml) 7 | [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard) 8 | 9 |
10 | 11 | Dependency-free RFC 3986 URI toolbox. 12 | 13 | ## Usage 14 | 15 | ## Options 16 | 17 | All of the above functions can accept an additional options argument that is an object that can contain one or more of the following properties: 18 | 19 | * `scheme` (string) 20 | Indicates the scheme that the URI should be treated as, overriding the URI's normal scheme parsing behavior. 21 | 22 | * `reference` (string) 23 | If set to `"suffix"`, it indicates that the URI is in the suffix format and the parser will use the option's `scheme` property to determine the URI's scheme. 24 | 25 | * `tolerant` (boolean, false) 26 | If set to `true`, the parser will relax URI resolving rules. 27 | 28 | * `absolutePath` (boolean, false) 29 | If set to `true`, the serializer will not resolve a relative `path` component. 30 | 31 | * `unicodeSupport` (boolean, false) 32 | If set to `true`, the parser will unescape non-ASCII characters in the parsed output as per [RFC 3987](http://www.ietf.org/rfc/rfc3987.txt). 33 | 34 | * `domainHost` (boolean, false) 35 | If set to `true`, the library will treat the `host` component as a domain name, and convert IDNs (International Domain Names) as per [RFC 5891](http://www.ietf.org/rfc/rfc5891.txt). 36 | 37 | ### Parse 38 | 39 | ```js 40 | const uri = require('fast-uri') 41 | uri.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body') 42 | // Output 43 | { 44 | scheme: "uri", 45 | userinfo: "user:pass", 46 | host: "example.com", 47 | port: 123, 48 | path: "/one/two.three", 49 | query: "q1=a1&q2=a2", 50 | fragment: "body" 51 | } 52 | ``` 53 | 54 | ### Serialize 55 | 56 | ```js 57 | const uri = require('fast-uri') 58 | uri.serialize({scheme: "http", host: "example.com", fragment: "footer"}) 59 | // Output 60 | "http://example.com/#footer" 61 | 62 | ``` 63 | 64 | ### Resolve 65 | 66 | ```js 67 | const uri = require('fast-uri') 68 | uri.resolve("uri://a/b/c/d?q", "../../g") 69 | // Output 70 | "uri://a/g" 71 | ``` 72 | 73 | ### Equal 74 | 75 | ```js 76 | const uri = require('fast-uri') 77 | uri.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d") 78 | // Output 79 | true 80 | ``` 81 | 82 | ## Scheme supports 83 | 84 | fast-uri supports inserting custom [scheme](http://en.wikipedia.org/wiki/URI_scheme)-dependent processing rules. Currently, fast-uri has built-in support for the following schemes: 85 | 86 | * http \[[RFC 2616](http://www.ietf.org/rfc/rfc2616.txt)\] 87 | * https \[[RFC 2818](http://www.ietf.org/rfc/rfc2818.txt)\] 88 | * ws \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\] 89 | * wss \[[RFC 6455](http://www.ietf.org/rfc/rfc6455.txt)\] 90 | * urn \[[RFC 2141](http://www.ietf.org/rfc/rfc2141.txt)\] 91 | * urn:uuid \[[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt)\] 92 | 93 | 94 | ## Benchmarks 95 | 96 | ``` 97 | fast-uri benchmark 98 | ┌─────────┬──────────────────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐ 99 | │ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │ 100 | ├─────────┼──────────────────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤ 101 | │ 0 │ 'fast-uri: parse domain' │ '951.31 ± 0.75%' │ '875.00 ± 11.00' │ '1122538 ± 0.01%' │ '1142857 ± 14550' │ 1051187 │ 102 | │ 1 │ 'fast-uri: parse IPv4' │ '443.44 ± 0.22%' │ '406.00 ± 3.00' │ '2422762 ± 0.01%' │ '2463054 ± 18335' │ 2255105 │ 103 | │ 2 │ 'fast-uri: parse IPv6' │ '1241.6 ± 1.74%' │ '1131.0 ± 30.00' │ '875177 ± 0.02%' │ '884173 ± 24092' │ 805399 │ 104 | │ 3 │ 'fast-uri: parse URN' │ '689.19 ± 4.29%' │ '618.00 ± 9.00' │ '1598373 ± 0.01%' │ '1618123 ± 23913' │ 1450972 │ 105 | │ 4 │ 'fast-uri: parse URN uuid' │ '1025.4 ± 2.02%' │ '921.00 ± 19.00' │ '1072419 ± 0.02%' │ '1085776 ± 22871' │ 975236 │ 106 | │ 5 │ 'fast-uri: serialize uri' │ '1028.5 ± 0.53%' │ '933.00 ± 43.00' │ '1063310 ± 0.02%' │ '1071811 ± 50523' │ 972249 │ 107 | │ 6 │ 'fast-uri: serialize long uri with dots' │ '1805.1 ± 0.52%' │ '1627.0 ± 17.00' │ '602620 ± 0.02%' │ '614628 ± 6490' │ 553997 │ 108 | │ 7 │ 'fast-uri: serialize IPv6' │ '2569.4 ± 2.69%' │ '2302.0 ± 21.00' │ '426080 ± 0.03%' │ '434405 ± 3999' │ 389194 │ 109 | │ 8 │ 'fast-uri: serialize ws' │ '979.39 ± 0.43%' │ '882.00 ± 8.00' │ '1111665 ± 0.02%' │ '1133787 ± 10378' │ 1021045 │ 110 | │ 9 │ 'fast-uri: resolve' │ '2208.2 ± 1.08%' │ '1980.0 ± 24.00' │ '495001 ± 0.03%' │ '505051 ± 6049' │ 452848 │ 111 | └─────────┴──────────────────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘ 112 | uri-js benchmark 113 | ┌─────────┬───────────────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐ 114 | │ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │ 115 | ├─────────┼───────────────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤ 116 | │ 0 │ 'urijs: parse domain' │ '3618.3 ± 0.43%' │ '3314.0 ± 33.00' │ '294875 ± 0.04%' │ '301750 ± 2975' │ 276375 │ 117 | │ 1 │ 'urijs: parse IPv4' │ '4024.1 ± 0.41%' │ '3751.0 ± 25.00' │ '261981 ± 0.04%' │ '266596 ± 1789' │ 248506 │ 118 | │ 2 │ 'urijs: parse IPv6' │ '5417.2 ± 0.46%' │ '4968.0 ± 43.00' │ '196023 ± 0.05%' │ '201288 ± 1727' │ 184598 │ 119 | │ 3 │ 'urijs: parse URN' │ '1324.2 ± 0.23%' │ '1229.0 ± 17.00' │ '801535 ± 0.02%' │ '813670 ± 11413' │ 755185 │ 120 | │ 4 │ 'urijs: parse URN uuid' │ '1822.0 ± 3.08%' │ '1655.0 ± 15.00' │ '594433 ± 0.02%' │ '604230 ± 5427' │ 548843 │ 121 | │ 5 │ 'urijs: serialize uri' │ '4196.8 ± 0.36%' │ '3908.0 ± 27.00' │ '251146 ± 0.04%' │ '255885 ± 1756' │ 238276 │ 122 | │ 6 │ 'urijs: serialize long uri with dots' │ '8331.0 ± 1.30%' │ '7658.0 ± 72.00' │ '126440 ± 0.07%' │ '130582 ± 1239' │ 120034 │ 123 | │ 7 │ 'urijs: serialize IPv6' │ '5685.5 ± 0.30%' │ '5366.0 ± 33.00' │ '182632 ± 0.05%' │ '186359 ± 1153' │ 175886 │ 124 | │ 8 │ 'urijs: serialize ws' │ '4159.3 ± 0.20%' │ '3899.0 ± 28.00' │ '250459 ± 0.04%' │ '256476 ± 1855' │ 240423 │ 125 | │ 9 │ 'urijs: resolve' │ '6729.9 ± 0.39%' │ '6261.0 ± 37.00' │ '156361 ± 0.06%' │ '159719 ± 949' │ 148591 │ 126 | └─────────┴───────────────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘ 127 | WHATWG URL benchmark 128 | ┌─────────┬────────────────────────────┬──────────────────┬──────────────────┬────────────────────────┬────────────────────────┬─────────┐ 129 | │ (index) │ Task name │ Latency avg (ns) │ Latency med (ns) │ Throughput avg (ops/s) │ Throughput med (ops/s) │ Samples │ 130 | ├─────────┼────────────────────────────┼──────────────────┼──────────────────┼────────────────────────┼────────────────────────┼─────────┤ 131 | │ 0 │ 'WHATWG URL: parse domain' │ '475.22 ± 0.20%' │ '444.00 ± 5.00' │ '2217599 ± 0.01%' │ '2252252 ± 25652' │ 2104289 │ 132 | │ 1 │ 'WHATWG URL: parse URN' │ '384.78 ± 0.85%' │ '350.00 ± 5.00' │ '2809071 ± 0.01%' │ '2857143 ± 41408' │ 2598885 │ 133 | └─────────┴────────────────────────────┴──────────────────┴──────────────────┴────────────────────────┴────────────────────────┴─────────┘ 134 | ``` 135 | 136 | ## TODO 137 | 138 | - [ ] Support MailTo 139 | - [ ] Be 100% iso compatible with uri-js 140 | 141 | ## License 142 | 143 | Licensed under [BSD-3-Clause](./LICENSE). 144 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | /** @type {(value: string) => boolean} */ 4 | const isUUID = RegExp.prototype.test.bind(/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iu) 5 | 6 | /** @type {(value: string) => boolean} */ 7 | 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) 8 | 9 | /** 10 | * @param {Array} input 11 | * @returns {string} 12 | */ 13 | function stringArrayToHexStripped (input) { 14 | let acc = '' 15 | let code = 0 16 | let i = 0 17 | 18 | for (i = 0; i < input.length; i++) { 19 | code = input[i].charCodeAt(0) 20 | if (code === 48) { 21 | continue 22 | } 23 | if (!((code >= 48 && code <= 57) || (code >= 65 && code <= 70) || (code >= 97 && code <= 102))) { 24 | return '' 25 | } 26 | acc += input[i] 27 | break 28 | } 29 | 30 | for (i += 1; i < input.length; i++) { 31 | code = input[i].charCodeAt(0) 32 | if (!((code >= 48 && code <= 57) || (code >= 65 && code <= 70) || (code >= 97 && code <= 102))) { 33 | return '' 34 | } 35 | acc += input[i] 36 | } 37 | return acc 38 | } 39 | 40 | /** 41 | * @typedef {Object} GetIPV6Result 42 | * @property {boolean} error - Indicates if there was an error parsing the IPv6 address. 43 | * @property {string} address - The parsed IPv6 address. 44 | * @property {string} [zone] - The zone identifier, if present. 45 | */ 46 | 47 | /** 48 | * @param {string} value 49 | * @returns {boolean} 50 | */ 51 | const nonSimpleDomain = RegExp.prototype.test.bind(/[^!"$&'()*+,\-.;=_`a-z{}~]/u) 52 | 53 | /** 54 | * @param {Array} buffer 55 | * @returns {boolean} 56 | */ 57 | function consumeIsZone (buffer) { 58 | buffer.length = 0 59 | return true 60 | } 61 | 62 | /** 63 | * @param {Array} buffer 64 | * @param {Array} address 65 | * @param {GetIPV6Result} output 66 | * @returns {boolean} 67 | */ 68 | function consumeHextets (buffer, address, output) { 69 | if (buffer.length) { 70 | const hex = stringArrayToHexStripped(buffer) 71 | if (hex !== '') { 72 | address.push(hex) 73 | } else { 74 | output.error = true 75 | return false 76 | } 77 | buffer.length = 0 78 | } 79 | return true 80 | } 81 | 82 | /** 83 | * @param {string} input 84 | * @returns {GetIPV6Result} 85 | */ 86 | function getIPV6 (input) { 87 | let tokenCount = 0 88 | const output = { error: false, address: '', zone: '' } 89 | /** @type {Array} */ 90 | const address = [] 91 | /** @type {Array} */ 92 | const buffer = [] 93 | let endipv6Encountered = false 94 | let endIpv6 = false 95 | 96 | let consume = consumeHextets 97 | 98 | for (let i = 0; i < input.length; i++) { 99 | const cursor = input[i] 100 | if (cursor === '[' || cursor === ']') { continue } 101 | if (cursor === ':') { 102 | if (endipv6Encountered === true) { 103 | endIpv6 = true 104 | } 105 | if (!consume(buffer, address, output)) { break } 106 | if (++tokenCount > 7) { 107 | // not valid 108 | output.error = true 109 | break 110 | } 111 | if (i > 0 && input[i - 1] === ':') { 112 | endipv6Encountered = true 113 | } 114 | address.push(':') 115 | continue 116 | } else if (cursor === '%') { 117 | if (!consume(buffer, address, output)) { break } 118 | // switch to zone detection 119 | consume = consumeIsZone 120 | } else { 121 | buffer.push(cursor) 122 | continue 123 | } 124 | } 125 | if (buffer.length) { 126 | if (consume === consumeIsZone) { 127 | output.zone = buffer.join('') 128 | } else if (endIpv6) { 129 | address.push(buffer.join('')) 130 | } else { 131 | address.push(stringArrayToHexStripped(buffer)) 132 | } 133 | } 134 | output.address = address.join('') 135 | return output 136 | } 137 | 138 | /** 139 | * @typedef {Object} NormalizeIPv6Result 140 | * @property {string} host - The normalized host. 141 | * @property {string} [escapedHost] - The escaped host. 142 | * @property {boolean} isIPV6 - Indicates if the host is an IPv6 address. 143 | */ 144 | 145 | /** 146 | * @param {string} host 147 | * @returns {NormalizeIPv6Result} 148 | */ 149 | function normalizeIPv6 (host) { 150 | if (findToken(host, ':') < 2) { return { host, isIPV6: false } } 151 | const ipv6 = getIPV6(host) 152 | 153 | if (!ipv6.error) { 154 | let newHost = ipv6.address 155 | let escapedHost = ipv6.address 156 | if (ipv6.zone) { 157 | newHost += '%' + ipv6.zone 158 | escapedHost += '%25' + ipv6.zone 159 | } 160 | return { host: newHost, isIPV6: true, escapedHost } 161 | } else { 162 | return { host, isIPV6: false } 163 | } 164 | } 165 | 166 | /** 167 | * @param {string} str 168 | * @param {string} token 169 | * @returns {number} 170 | */ 171 | function findToken (str, token) { 172 | let ind = 0 173 | for (let i = 0; i < str.length; i++) { 174 | if (str[i] === token) ind++ 175 | } 176 | return ind 177 | } 178 | 179 | /** 180 | * @param {string} path 181 | * @returns {string} 182 | * 183 | * @see https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4 184 | */ 185 | function removeDotSegments (path) { 186 | let input = path 187 | const output = [] 188 | let nextSlash = -1 189 | let len = 0 190 | 191 | // eslint-disable-next-line no-cond-assign 192 | while (len = input.length) { 193 | if (len === 1) { 194 | if (input === '.') { 195 | break 196 | } else if (input === '/') { 197 | output.push('/') 198 | break 199 | } else { 200 | output.push(input) 201 | break 202 | } 203 | } else if (len === 2) { 204 | if (input[0] === '.') { 205 | if (input[1] === '.') { 206 | break 207 | } else if (input[1] === '/') { 208 | input = input.slice(2) 209 | continue 210 | } 211 | } else if (input[0] === '/') { 212 | if (input[1] === '.' || input[1] === '/') { 213 | output.push('/') 214 | break 215 | } 216 | } 217 | } else if (len === 3) { 218 | if (input === '/..') { 219 | if (output.length !== 0) { 220 | output.pop() 221 | } 222 | output.push('/') 223 | break 224 | } 225 | } 226 | if (input[0] === '.') { 227 | if (input[1] === '.') { 228 | if (input[2] === '/') { 229 | input = input.slice(3) 230 | continue 231 | } 232 | } else if (input[1] === '/') { 233 | input = input.slice(2) 234 | continue 235 | } 236 | } else if (input[0] === '/') { 237 | if (input[1] === '.') { 238 | if (input[2] === '/') { 239 | input = input.slice(2) 240 | continue 241 | } else if (input[2] === '.') { 242 | if (input[3] === '/') { 243 | input = input.slice(3) 244 | if (output.length !== 0) { 245 | output.pop() 246 | } 247 | continue 248 | } 249 | } 250 | } 251 | } 252 | 253 | // Rule 2E: Move normal path segment to output 254 | if ((nextSlash = input.indexOf('/', 1)) === -1) { 255 | output.push(input) 256 | break 257 | } else { 258 | output.push(input.slice(0, nextSlash)) 259 | input = input.slice(nextSlash) 260 | } 261 | } 262 | 263 | return output.join('') 264 | } 265 | 266 | /** 267 | * @param {import('../types/index').URIComponent} component 268 | * @param {boolean} esc 269 | * @returns {import('../types/index').URIComponent} 270 | */ 271 | function normalizeComponentEncoding (component, esc) { 272 | const func = esc !== true ? escape : unescape 273 | if (component.scheme !== undefined) { 274 | component.scheme = func(component.scheme) 275 | } 276 | if (component.userinfo !== undefined) { 277 | component.userinfo = func(component.userinfo) 278 | } 279 | if (component.host !== undefined) { 280 | component.host = func(component.host) 281 | } 282 | if (component.path !== undefined) { 283 | component.path = func(component.path) 284 | } 285 | if (component.query !== undefined) { 286 | component.query = func(component.query) 287 | } 288 | if (component.fragment !== undefined) { 289 | component.fragment = func(component.fragment) 290 | } 291 | return component 292 | } 293 | 294 | /** 295 | * @param {import('../types/index').URIComponent} component 296 | * @returns {string|undefined} 297 | */ 298 | function recomposeAuthority (component) { 299 | const uriTokens = [] 300 | 301 | if (component.userinfo !== undefined) { 302 | uriTokens.push(component.userinfo) 303 | uriTokens.push('@') 304 | } 305 | 306 | if (component.host !== undefined) { 307 | let host = unescape(component.host) 308 | if (!isIPv4(host)) { 309 | const ipV6res = normalizeIPv6(host) 310 | if (ipV6res.isIPV6 === true) { 311 | host = `[${ipV6res.escapedHost}]` 312 | } else { 313 | host = component.host 314 | } 315 | } 316 | uriTokens.push(host) 317 | } 318 | 319 | if (typeof component.port === 'number' || typeof component.port === 'string') { 320 | uriTokens.push(':') 321 | uriTokens.push(String(component.port)) 322 | } 323 | 324 | return uriTokens.length ? uriTokens.join('') : undefined 325 | }; 326 | 327 | module.exports = { 328 | nonSimpleDomain, 329 | recomposeAuthority, 330 | normalizeComponentEncoding, 331 | removeDotSegments, 332 | isIPv4, 333 | isUUID, 334 | normalizeIPv6, 335 | stringArrayToHexStripped 336 | } 337 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { normalizeIPv6, removeDotSegments, recomposeAuthority, normalizeComponentEncoding, isIPv4, nonSimpleDomain } = require('./lib/utils') 4 | const { SCHEMES, getSchemeHandler } = require('./lib/schemes') 5 | 6 | /** 7 | * @template {import('./types/index').URIComponent|string} T 8 | * @param {T} uri 9 | * @param {import('./types/index').Options} [options] 10 | * @returns {T} 11 | */ 12 | function normalize (uri, options) { 13 | if (typeof uri === 'string') { 14 | uri = /** @type {T} */ (serialize(parse(uri, options), options)) 15 | } else if (typeof uri === 'object') { 16 | uri = /** @type {T} */ (parse(serialize(uri, options), options)) 17 | } 18 | return uri 19 | } 20 | 21 | /** 22 | * @param {string} baseURI 23 | * @param {string} relativeURI 24 | * @param {import('./types/index').Options} [options] 25 | * @returns {string} 26 | */ 27 | function resolve (baseURI, relativeURI, options) { 28 | const schemelessOptions = options ? Object.assign({ scheme: 'null' }, options) : { scheme: 'null' } 29 | const resolved = resolveComponent(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true) 30 | schemelessOptions.skipEscape = true 31 | return serialize(resolved, schemelessOptions) 32 | } 33 | 34 | /** 35 | * @param {import ('./types/index').URIComponent} base 36 | * @param {import ('./types/index').URIComponent} relative 37 | * @param {import('./types/index').Options} [options] 38 | * @param {boolean} [skipNormalization=false] 39 | * @returns {import ('./types/index').URIComponent} 40 | */ 41 | function resolveComponent (base, relative, options, skipNormalization) { 42 | /** @type {import('./types/index').URIComponent} */ 43 | const target = {} 44 | if (!skipNormalization) { 45 | base = parse(serialize(base, options), options) // normalize base component 46 | relative = parse(serialize(relative, options), options) // normalize relative component 47 | } 48 | options = options || {} 49 | 50 | if (!options.tolerant && relative.scheme) { 51 | target.scheme = relative.scheme 52 | // target.authority = relative.authority; 53 | target.userinfo = relative.userinfo 54 | target.host = relative.host 55 | target.port = relative.port 56 | target.path = removeDotSegments(relative.path || '') 57 | target.query = relative.query 58 | } else { 59 | if (relative.userinfo !== undefined || relative.host !== undefined || relative.port !== undefined) { 60 | // target.authority = relative.authority; 61 | target.userinfo = relative.userinfo 62 | target.host = relative.host 63 | target.port = relative.port 64 | target.path = removeDotSegments(relative.path || '') 65 | target.query = relative.query 66 | } else { 67 | if (!relative.path) { 68 | target.path = base.path 69 | if (relative.query !== undefined) { 70 | target.query = relative.query 71 | } else { 72 | target.query = base.query 73 | } 74 | } else { 75 | if (relative.path[0] === '/') { 76 | target.path = removeDotSegments(relative.path) 77 | } else { 78 | if ((base.userinfo !== undefined || base.host !== undefined || base.port !== undefined) && !base.path) { 79 | target.path = '/' + relative.path 80 | } else if (!base.path) { 81 | target.path = relative.path 82 | } else { 83 | target.path = base.path.slice(0, base.path.lastIndexOf('/') + 1) + relative.path 84 | } 85 | target.path = removeDotSegments(target.path) 86 | } 87 | target.query = relative.query 88 | } 89 | // target.authority = base.authority; 90 | target.userinfo = base.userinfo 91 | target.host = base.host 92 | target.port = base.port 93 | } 94 | target.scheme = base.scheme 95 | } 96 | 97 | target.fragment = relative.fragment 98 | 99 | return target 100 | } 101 | 102 | /** 103 | * @param {import ('./types/index').URIComponent|string} uriA 104 | * @param {import ('./types/index').URIComponent|string} uriB 105 | * @param {import ('./types/index').Options} options 106 | * @returns {boolean} 107 | */ 108 | function equal (uriA, uriB, options) { 109 | if (typeof uriA === 'string') { 110 | uriA = unescape(uriA) 111 | uriA = serialize(normalizeComponentEncoding(parse(uriA, options), true), { ...options, skipEscape: true }) 112 | } else if (typeof uriA === 'object') { 113 | uriA = serialize(normalizeComponentEncoding(uriA, true), { ...options, skipEscape: true }) 114 | } 115 | 116 | if (typeof uriB === 'string') { 117 | uriB = unescape(uriB) 118 | uriB = serialize(normalizeComponentEncoding(parse(uriB, options), true), { ...options, skipEscape: true }) 119 | } else if (typeof uriB === 'object') { 120 | uriB = serialize(normalizeComponentEncoding(uriB, true), { ...options, skipEscape: true }) 121 | } 122 | 123 | return uriA.toLowerCase() === uriB.toLowerCase() 124 | } 125 | 126 | /** 127 | * @param {Readonly} cmpts 128 | * @param {import('./types/index').Options} [opts] 129 | * @returns {string} 130 | */ 131 | function serialize (cmpts, opts) { 132 | const component = { 133 | host: cmpts.host, 134 | scheme: cmpts.scheme, 135 | userinfo: cmpts.userinfo, 136 | port: cmpts.port, 137 | path: cmpts.path, 138 | query: cmpts.query, 139 | nid: cmpts.nid, 140 | nss: cmpts.nss, 141 | uuid: cmpts.uuid, 142 | fragment: cmpts.fragment, 143 | reference: cmpts.reference, 144 | resourceName: cmpts.resourceName, 145 | secure: cmpts.secure, 146 | error: '' 147 | } 148 | const options = Object.assign({}, opts) 149 | const uriTokens = [] 150 | 151 | // find scheme handler 152 | const schemeHandler = getSchemeHandler(options.scheme || component.scheme) 153 | 154 | // perform scheme specific serialization 155 | if (schemeHandler && schemeHandler.serialize) schemeHandler.serialize(component, options) 156 | 157 | if (component.path !== undefined) { 158 | if (!options.skipEscape) { 159 | component.path = escape(component.path) 160 | 161 | if (component.scheme !== undefined) { 162 | component.path = component.path.split('%3A').join(':') 163 | } 164 | } else { 165 | component.path = unescape(component.path) 166 | } 167 | } 168 | 169 | if (options.reference !== 'suffix' && component.scheme) { 170 | uriTokens.push(component.scheme, ':') 171 | } 172 | 173 | const authority = recomposeAuthority(component) 174 | if (authority !== undefined) { 175 | if (options.reference !== 'suffix') { 176 | uriTokens.push('//') 177 | } 178 | 179 | uriTokens.push(authority) 180 | 181 | if (component.path && component.path[0] !== '/') { 182 | uriTokens.push('/') 183 | } 184 | } 185 | if (component.path !== undefined) { 186 | let s = component.path 187 | 188 | if (!options.absolutePath && (!schemeHandler || !schemeHandler.absolutePath)) { 189 | s = removeDotSegments(s) 190 | } 191 | 192 | if ( 193 | authority === undefined && 194 | s[0] === '/' && 195 | s[1] === '/' 196 | ) { 197 | // don't allow the path to start with "//" 198 | s = '/%2F' + s.slice(2) 199 | } 200 | 201 | uriTokens.push(s) 202 | } 203 | 204 | if (component.query !== undefined) { 205 | uriTokens.push('?', component.query) 206 | } 207 | 208 | if (component.fragment !== undefined) { 209 | uriTokens.push('#', component.fragment) 210 | } 211 | return uriTokens.join('') 212 | } 213 | 214 | const URI_PARSE = /^(?:([^#/:?]+):)?(?:\/\/((?:([^#/?@]*)@)?(\[[^#/?\]]+\]|[^#/:?]*)(?::(\d*))?))?([^#?]*)(?:\?([^#]*))?(?:#((?:.|[\n\r])*))?/u 215 | 216 | /** 217 | * @param {string} uri 218 | * @param {import('./types/index').Options} [opts] 219 | * @returns 220 | */ 221 | function parse (uri, opts) { 222 | const options = Object.assign({}, opts) 223 | /** @type {import('./types/index').URIComponent} */ 224 | const parsed = { 225 | scheme: undefined, 226 | userinfo: undefined, 227 | host: '', 228 | port: undefined, 229 | path: '', 230 | query: undefined, 231 | fragment: undefined 232 | } 233 | 234 | let isIP = false 235 | if (options.reference === 'suffix') { 236 | if (options.scheme) { 237 | uri = options.scheme + ':' + uri 238 | } else { 239 | uri = '//' + uri 240 | } 241 | } 242 | 243 | const matches = uri.match(URI_PARSE) 244 | 245 | if (matches) { 246 | // store each component 247 | parsed.scheme = matches[1] 248 | parsed.userinfo = matches[3] 249 | parsed.host = matches[4] 250 | parsed.port = parseInt(matches[5], 10) 251 | parsed.path = matches[6] || '' 252 | parsed.query = matches[7] 253 | parsed.fragment = matches[8] 254 | 255 | // fix port number 256 | if (isNaN(parsed.port)) { 257 | parsed.port = matches[5] 258 | } 259 | if (parsed.host) { 260 | const ipv4result = isIPv4(parsed.host) 261 | if (ipv4result === false) { 262 | const ipv6result = normalizeIPv6(parsed.host) 263 | parsed.host = ipv6result.host.toLowerCase() 264 | isIP = ipv6result.isIPV6 265 | } else { 266 | isIP = true 267 | } 268 | } 269 | if (parsed.scheme === undefined && parsed.userinfo === undefined && parsed.host === undefined && parsed.port === undefined && parsed.query === undefined && !parsed.path) { 270 | parsed.reference = 'same-document' 271 | } else if (parsed.scheme === undefined) { 272 | parsed.reference = 'relative' 273 | } else if (parsed.fragment === undefined) { 274 | parsed.reference = 'absolute' 275 | } else { 276 | parsed.reference = 'uri' 277 | } 278 | 279 | // check for reference errors 280 | if (options.reference && options.reference !== 'suffix' && options.reference !== parsed.reference) { 281 | parsed.error = parsed.error || 'URI is not a ' + options.reference + ' reference.' 282 | } 283 | 284 | // find scheme handler 285 | const schemeHandler = getSchemeHandler(options.scheme || parsed.scheme) 286 | 287 | // check if scheme can't handle IRIs 288 | if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) { 289 | // if host component is a domain name 290 | if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && isIP === false && nonSimpleDomain(parsed.host)) { 291 | // convert Unicode IDN -> ASCII IDN 292 | try { 293 | parsed.host = URL.domainToASCII(parsed.host.toLowerCase()) 294 | } catch (e) { 295 | parsed.error = parsed.error || "Host's domain name can not be converted to ASCII: " + e 296 | } 297 | } 298 | // convert IRI -> URI 299 | } 300 | 301 | if (!schemeHandler || (schemeHandler && !schemeHandler.skipNormalize)) { 302 | if (uri.indexOf('%') !== -1) { 303 | if (parsed.scheme !== undefined) { 304 | parsed.scheme = unescape(parsed.scheme) 305 | } 306 | if (parsed.host !== undefined) { 307 | parsed.host = unescape(parsed.host) 308 | } 309 | } 310 | if (parsed.path) { 311 | parsed.path = escape(unescape(parsed.path)) 312 | } 313 | if (parsed.fragment) { 314 | parsed.fragment = encodeURI(decodeURIComponent(parsed.fragment)) 315 | } 316 | } 317 | 318 | // perform scheme specific parsing 319 | if (schemeHandler && schemeHandler.parse) { 320 | schemeHandler.parse(parsed, options) 321 | } 322 | } else { 323 | parsed.error = parsed.error || 'URI can not be parsed.' 324 | } 325 | return parsed 326 | } 327 | 328 | const fastUri = { 329 | SCHEMES, 330 | normalize, 331 | resolve, 332 | resolveComponent, 333 | equal, 334 | serialize, 335 | parse 336 | } 337 | 338 | module.exports = fastUri 339 | module.exports.default = fastUri 340 | module.exports.fastUri = fastUri 341 | -------------------------------------------------------------------------------- /test/fixtures/uri-js-parse.json: -------------------------------------------------------------------------------- 1 | [ 2 | [ 3 | "//www.g.com/error\n/bleh/bleh", 4 | { 5 | "host": "www.g.com", 6 | "path": "/error%0A/bleh/bleh", 7 | "reference": "relative" 8 | } 9 | ], 10 | [ 11 | "https://fastify.org", 12 | { 13 | "scheme": "https", 14 | "host": "fastify.org", 15 | "path": "", 16 | "reference": "absolute" 17 | } 18 | ], 19 | [ 20 | "/definitions/Record%3Cstring%2CPerson%3E", 21 | { 22 | "path": "/definitions/Record%3Cstring%2CPerson%3E", 23 | "reference": "relative" 24 | } 25 | ], 26 | [ 27 | "//10.10.10.10", 28 | { 29 | "host": "10.10.10.10", 30 | "path": "", 31 | "reference": "relative" 32 | } 33 | ], 34 | [ 35 | "//10.10.000.10", 36 | { 37 | "host": "10.10.0.10", 38 | "path": "", 39 | "reference": "relative" 40 | } 41 | ], 42 | [ 43 | "//[2001:db8::7%en0]", 44 | { 45 | "host": "2001:db8::7%en0", 46 | "path": "", 47 | "reference": "relative" 48 | } 49 | ], 50 | [ 51 | "//[2001:dbZ::1]:80", 52 | { 53 | "host": "[2001:dbz::1]", 54 | "port": 80, 55 | "path": "", 56 | "reference": "relative" 57 | } 58 | ], 59 | [ 60 | "//[2001:db8::1]:80", 61 | { 62 | "host": "2001:db8::1", 63 | "port": 80, 64 | "path": "", 65 | "reference": "relative" 66 | } 67 | ], 68 | [ 69 | "//[2001:db8::001]:80", 70 | { 71 | "host": "2001:db8::1", 72 | "port": 80, 73 | "path": "", 74 | "reference": "relative" 75 | } 76 | ], 77 | [ 78 | "uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body", 79 | { 80 | "scheme": "uri", 81 | "userinfo": "user:pass", 82 | "host": "example.com", 83 | "port": 123, 84 | "path": "/one/two.three", 85 | "query": "q1=a1&q2=a2", 86 | "fragment": "body", 87 | "reference": "uri" 88 | } 89 | ], 90 | [ 91 | "http://user:pass@example.com:123/one/space in.url?q1=a1&q2=a2#body", 92 | { 93 | "scheme": "http", 94 | "userinfo": "user:pass", 95 | "host": "example.com", 96 | "port": 123, 97 | "path": "/one/space%20in.url", 98 | "query": "q1=a1&q2=a2", 99 | "fragment": "body", 100 | "reference": "uri" 101 | } 102 | ], 103 | [ 104 | "http://User:Pass@example.com:123/one/space in.url?q1=a1&q2=a2#body", 105 | { 106 | "scheme": "http", 107 | "userinfo": "User:Pass", 108 | "host": "example.com", 109 | "port": 123, 110 | "path": "/one/space%20in.url", 111 | "query": "q1=a1&q2=a2", 112 | "fragment": "body", 113 | "reference": "uri" 114 | } 115 | ], 116 | [ 117 | "http://A%3AB@example.com:123/one/space", 118 | { 119 | "scheme": "http", 120 | "userinfo": "A%3AB", 121 | "host": "example.com", 122 | "port": 123, 123 | "path": "/one/space", 124 | "reference": "absolute" 125 | } 126 | ], 127 | [ 128 | "//[::ffff:129.144.52.38]", 129 | { 130 | "host": "::ffff:129.144.52.38", 131 | "path": "", 132 | "reference": "relative" 133 | } 134 | ], 135 | [ 136 | "uri://10.10.10.10.example.com/en/process", 137 | { 138 | "scheme": "uri", 139 | "host": "10.10.10.10.example.com", 140 | "path": "/en/process", 141 | "reference": "absolute" 142 | } 143 | ], 144 | [ 145 | "//[2606:2800:220:1:248:1893:25c8:1946]/test", 146 | { 147 | "host": "2606:2800:220:1:248:1893:25c8:1946", 148 | "path": "/test", 149 | "reference": "relative" 150 | } 151 | ], 152 | [ 153 | "ws://example.com/chat", 154 | { 155 | "scheme": "ws", 156 | "host": "example.com", 157 | "reference": "absolute", 158 | "secure": false, 159 | "resourceName": "/chat" 160 | } 161 | ], 162 | [ 163 | "ws://example.com/foo?bar=baz", 164 | { 165 | "scheme": "ws", 166 | "host": "example.com", 167 | "reference": "absolute", 168 | "secure": false, 169 | "resourceName": "/foo?bar=baz" 170 | } 171 | ], 172 | [ 173 | "wss://example.com/?bar=baz", 174 | { 175 | "scheme": "wss", 176 | "host": "example.com", 177 | "reference": "absolute", 178 | "secure": true, 179 | "resourceName": "/?bar=baz" 180 | } 181 | ], 182 | [ 183 | "wss://example.com/chat", 184 | { 185 | "scheme": "wss", 186 | "host": "example.com", 187 | "reference": "absolute", 188 | "secure": true, 189 | "resourceName": "/chat" 190 | } 191 | ], 192 | [ 193 | "wss://example.com/foo?bar=baz", 194 | { 195 | "scheme": "wss", 196 | "host": "example.com", 197 | "reference": "absolute", 198 | "secure": true, 199 | "resourceName": "/foo?bar=baz" 200 | } 201 | ], 202 | [ 203 | "wss://example.com/?bar=baz", 204 | { 205 | "scheme": "wss", 206 | "host": "example.com", 207 | "reference": "absolute", 208 | "secure": true, 209 | "resourceName": "/?bar=baz" 210 | } 211 | ], 212 | [ 213 | "urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6", 214 | { 215 | "scheme": "urn", 216 | "reference": "absolute", 217 | "nid": "uuid", 218 | "uuid": "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" 219 | } 220 | ], 221 | [ 222 | "urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6", 223 | { 224 | "scheme": "urn", 225 | "reference": "absolute", 226 | "nid": "uuid", 227 | "uuid": "notauuid-7dec-11d0-a765-00a0c91e6bf6", 228 | "error": "UUID is not valid." 229 | } 230 | ], 231 | [ 232 | "urn:example:%D0%B0123,z456", 233 | { 234 | "scheme": "urn", 235 | "reference": "absolute", 236 | "nid": "example", 237 | "nss": "%D0%B0123,z456" 238 | } 239 | ], 240 | [ 241 | "//[2606:2800:220:1:248:1893:25c8:1946:43209]", 242 | { 243 | "host": "[2606:2800:220:1:248:1893:25c8:1946:43209]", 244 | "path": "", 245 | "reference": "relative" 246 | } 247 | ], 248 | [ 249 | "http://foo.bar", 250 | { 251 | "scheme": "http", 252 | "host": "foo.bar", 253 | "path": "", 254 | "reference": "absolute" 255 | } 256 | ], 257 | [ 258 | "http://", 259 | { 260 | "scheme": "http", 261 | "host": "", 262 | "path": "", 263 | "reference": "absolute", 264 | "error": "HTTP URIs must have a host." 265 | } 266 | ], 267 | [ 268 | "#/$defs/stringMap", 269 | { 270 | "path": "", 271 | "fragment": "/$defs/stringMap", 272 | "reference": "same-document" 273 | } 274 | ], 275 | [ 276 | "#/$defs/string%20Map", 277 | { 278 | "path": "", 279 | "fragment": "/$defs/string%20Map", 280 | "reference": "same-document" 281 | } 282 | ], 283 | [ 284 | "#/$defs/string Map", 285 | { 286 | "path": "", 287 | "fragment": "/$defs/string%20Map", 288 | "reference": "same-document" 289 | } 290 | ], 291 | [ 292 | "//?json=%7B%22foo%22%3A%22bar%22%7D", 293 | { 294 | "host": "", 295 | "path": "", 296 | "query": "json=%7B%22foo%22%3A%22bar%22%7D", 297 | "reference": "relative" 298 | } 299 | ], 300 | [ 301 | "mailto:chris@example.com", 302 | { 303 | "scheme": "mailto", 304 | "reference": "absolute", 305 | "to": [ 306 | "chris@example.com" 307 | ] 308 | } 309 | ], 310 | [ 311 | "mailto:infobot@example.com?subject=current-issue", 312 | { 313 | "scheme": "mailto", 314 | "reference": "absolute", 315 | "to": [ 316 | "infobot@example.com" 317 | ], 318 | "subject": "current-issue" 319 | } 320 | ], 321 | [ 322 | "mailto:infobot@example.com?body=send%20current-issue", 323 | { 324 | "scheme": "mailto", 325 | "reference": "absolute", 326 | "to": [ 327 | "infobot@example.com" 328 | ], 329 | "body": "send current-issue" 330 | } 331 | ], 332 | [ 333 | "mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index", 334 | { 335 | "scheme": "mailto", 336 | "reference": "absolute", 337 | "to": [ 338 | "infobot@example.com" 339 | ], 340 | "body": "send current-issue\r\nsend index" 341 | } 342 | ], 343 | [ 344 | "mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E", 345 | { 346 | "scheme": "mailto", 347 | "reference": "absolute", 348 | "to": [ 349 | "list@example.org" 350 | ], 351 | "headers": { 352 | "In-Reply-To": "<3469A91.D10AF4C@example.com>" 353 | } 354 | } 355 | ], 356 | [ 357 | "mailto:majordomo@example.com?body=subscribe%20bamboo-l", 358 | { 359 | "scheme": "mailto", 360 | "reference": "absolute", 361 | "to": [ 362 | "majordomo@example.com" 363 | ], 364 | "body": "subscribe bamboo-l" 365 | } 366 | ], 367 | [ 368 | "mailto:joe@example.com?cc=bob@example.com&body=hello", 369 | { 370 | "scheme": "mailto", 371 | "reference": "absolute", 372 | "to": [ 373 | "joe@example.com" 374 | ], 375 | "body": "hello", 376 | "headers": { 377 | "cc": "bob@example.com" 378 | } 379 | } 380 | ], 381 | [ 382 | "mailto:gorby%25kremvax@example.com", 383 | { 384 | "scheme": "mailto", 385 | "reference": "absolute", 386 | "to": [ 387 | "gorby%kremvax@example.com" 388 | ] 389 | } 390 | ], 391 | [ 392 | "mailto:unlikely%3Faddress@example.com?blat=foop", 393 | { 394 | "scheme": "mailto", 395 | "reference": "absolute", 396 | "to": [ 397 | "unlikely?address@example.com" 398 | ], 399 | "headers": { 400 | "blat": "foop" 401 | } 402 | } 403 | ], 404 | [ 405 | "mailto:Mike%26family@example.org", 406 | { 407 | "scheme": "mailto", 408 | "reference": "absolute", 409 | "to": [ 410 | "Mike&family@example.org" 411 | ] 412 | } 413 | ], 414 | [ 415 | "mailto:%22not%40me%22@example.org", 416 | { 417 | "scheme": "mailto", 418 | "reference": "absolute", 419 | "to": [ 420 | "\"not@me\"@example.org" 421 | ] 422 | } 423 | ], 424 | [ 425 | "mailto:%22oh%5C%5Cno%22@example.org", 426 | { 427 | "scheme": "mailto", 428 | "reference": "absolute", 429 | "to": [ 430 | "\"oh\\\\no\"@example.org" 431 | ] 432 | } 433 | ], 434 | [ 435 | "mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org", 436 | { 437 | "scheme": "mailto", 438 | "reference": "absolute", 439 | "to": [ 440 | "\"\\\\\\\"it's\\ ugly\\\\\\\"\"@example.org" 441 | ] 442 | } 443 | ], 444 | [ 445 | "mailto:user@example.org?subject=caf%C3%A9", 446 | { 447 | "scheme": "mailto", 448 | "reference": "absolute", 449 | "to": [ 450 | "user@example.org" 451 | ], 452 | "subject": "café" 453 | } 454 | ], 455 | [ 456 | "mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D", 457 | { 458 | "scheme": "mailto", 459 | "reference": "absolute", 460 | "to": [ 461 | "user@example.org" 462 | ], 463 | "subject": "=?utf-8?Q?caf=C3=A9?=" 464 | } 465 | ], 466 | [ 467 | "mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D", 468 | { 469 | "scheme": "mailto", 470 | "reference": "absolute", 471 | "to": [ 472 | "user@example.org" 473 | ], 474 | "subject": "=?iso-8859-1?Q?caf=E9?=" 475 | } 476 | ], 477 | [ 478 | "mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9", 479 | { 480 | "scheme": "mailto", 481 | "reference": "absolute", 482 | "to": [ 483 | "user@example.org" 484 | ], 485 | "subject": "café", 486 | "body": "café" 487 | } 488 | ], 489 | [ 490 | "mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO", 491 | { 492 | "scheme": "mailto", 493 | "reference": "absolute", 494 | "to": [ 495 | "user@xn--99zt52a.example.org" 496 | ], 497 | "subject": "Test", 498 | "body": "NATTO" 499 | } 500 | ] 501 | ] -------------------------------------------------------------------------------- /test/parse.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | test('URI parse', (t) => { 7 | let components 8 | 9 | // scheme 10 | components = fastURI.parse('uri:') 11 | t.equal(components.error, undefined, 'scheme errors') 12 | t.equal(components.scheme, 'uri', 'scheme') 13 | // t.equal(components.authority, undefined, "authority"); 14 | t.equal(components.userinfo, undefined, 'userinfo') 15 | t.equal(components.host, undefined, 'host') 16 | t.equal(components.port, undefined, 'port') 17 | t.equal(components.path, '', 'path') 18 | t.equal(components.query, undefined, 'query') 19 | t.equal(components.fragment, undefined, 'fragment') 20 | 21 | // userinfo 22 | components = fastURI.parse('//@') 23 | t.equal(components.error, undefined, 'userinfo errors') 24 | t.equal(components.scheme, undefined, 'scheme') 25 | // t.equal(components.authority, "@", "authority"); 26 | t.equal(components.userinfo, '', 'userinfo') 27 | t.equal(components.host, '', 'host') 28 | t.equal(components.port, undefined, 'port') 29 | t.equal(components.path, '', 'path') 30 | t.equal(components.query, undefined, 'query') 31 | t.equal(components.fragment, undefined, 'fragment') 32 | 33 | // host 34 | components = fastURI.parse('//') 35 | t.equal(components.error, undefined, 'host errors') 36 | t.equal(components.scheme, undefined, 'scheme') 37 | // t.equal(components.authority, "", "authority"); 38 | t.equal(components.userinfo, undefined, 'userinfo') 39 | t.equal(components.host, '', 'host') 40 | t.equal(components.port, undefined, 'port') 41 | t.equal(components.path, '', 'path') 42 | t.equal(components.query, undefined, 'query') 43 | t.equal(components.fragment, undefined, 'fragment') 44 | 45 | // port 46 | components = fastURI.parse('//:') 47 | t.equal(components.error, undefined, 'port errors') 48 | t.equal(components.scheme, undefined, 'scheme') 49 | // t.equal(components.authority, ":", "authority"); 50 | t.equal(components.userinfo, undefined, 'userinfo') 51 | t.equal(components.host, '', 'host') 52 | t.equal(components.port, '', 'port') 53 | t.equal(components.path, '', 'path') 54 | t.equal(components.query, undefined, 'query') 55 | t.equal(components.fragment, undefined, 'fragment') 56 | 57 | // path 58 | components = fastURI.parse('') 59 | t.equal(components.error, undefined, 'path errors') 60 | t.equal(components.scheme, undefined, 'scheme') 61 | // t.equal(components.authority, undefined, "authority"); 62 | t.equal(components.userinfo, undefined, 'userinfo') 63 | t.equal(components.host, undefined, 'host') 64 | t.equal(components.port, undefined, 'port') 65 | t.equal(components.path, '', 'path') 66 | t.equal(components.query, undefined, 'query') 67 | t.equal(components.fragment, undefined, 'fragment') 68 | 69 | // query 70 | components = fastURI.parse('?') 71 | t.equal(components.error, undefined, 'query errors') 72 | t.equal(components.scheme, undefined, 'scheme') 73 | // t.equal(components.authority, undefined, "authority"); 74 | t.equal(components.userinfo, undefined, 'userinfo') 75 | t.equal(components.host, undefined, 'host') 76 | t.equal(components.port, undefined, 'port') 77 | t.equal(components.path, '', 'path') 78 | t.equal(components.query, '', 'query') 79 | t.equal(components.fragment, undefined, 'fragment') 80 | 81 | // fragment 82 | components = fastURI.parse('#') 83 | t.equal(components.error, undefined, 'fragment errors') 84 | t.equal(components.scheme, undefined, 'scheme') 85 | // t.equal(components.authority, undefined, "authority"); 86 | t.equal(components.userinfo, undefined, 'userinfo') 87 | t.equal(components.host, undefined, 'host') 88 | t.equal(components.port, undefined, 'port') 89 | t.equal(components.path, '', 'path') 90 | t.equal(components.query, undefined, 'query') 91 | t.equal(components.fragment, '', 'fragment') 92 | 93 | // fragment with character tabulation 94 | components = fastURI.parse('#\t') 95 | t.equal(components.error, undefined, 'path errors') 96 | t.equal(components.scheme, undefined, 'scheme') 97 | // t.equal(components.authority, undefined, "authority"); 98 | t.equal(components.userinfo, undefined, 'userinfo') 99 | t.equal(components.host, undefined, 'host') 100 | t.equal(components.port, undefined, 'port') 101 | t.equal(components.path, '', 'path') 102 | t.equal(components.query, undefined, 'query') 103 | t.equal(components.fragment, '%09', 'fragment') 104 | 105 | // fragment with line feed 106 | components = fastURI.parse('#\n') 107 | t.equal(components.error, undefined, 'path errors') 108 | t.equal(components.scheme, undefined, 'scheme') 109 | // t.equal(components.authority, undefined, "authority"); 110 | t.equal(components.userinfo, undefined, 'userinfo') 111 | t.equal(components.host, undefined, 'host') 112 | t.equal(components.port, undefined, 'port') 113 | t.equal(components.path, '', 'path') 114 | t.equal(components.query, undefined, 'query') 115 | t.equal(components.fragment, '%0A', 'fragment') 116 | 117 | // fragment with line tabulation 118 | components = fastURI.parse('#\v') 119 | t.equal(components.error, undefined, 'path errors') 120 | t.equal(components.scheme, undefined, 'scheme') 121 | // t.equal(components.authority, undefined, "authority"); 122 | t.equal(components.userinfo, undefined, 'userinfo') 123 | t.equal(components.host, undefined, 'host') 124 | t.equal(components.port, undefined, 'port') 125 | t.equal(components.path, '', 'path') 126 | t.equal(components.query, undefined, 'query') 127 | t.equal(components.fragment, '%0B', 'fragment') 128 | 129 | // fragment with form feed 130 | components = fastURI.parse('#\f') 131 | t.equal(components.error, undefined, 'path errors') 132 | t.equal(components.scheme, undefined, 'scheme') 133 | // t.equal(components.authority, undefined, "authority"); 134 | t.equal(components.userinfo, undefined, 'userinfo') 135 | t.equal(components.host, undefined, 'host') 136 | t.equal(components.port, undefined, 'port') 137 | t.equal(components.path, '', 'path') 138 | t.equal(components.query, undefined, 'query') 139 | t.equal(components.fragment, '%0C', 'fragment') 140 | 141 | // fragment with carriage return 142 | components = fastURI.parse('#\r') 143 | t.equal(components.error, undefined, 'path errors') 144 | t.equal(components.scheme, undefined, 'scheme') 145 | // t.equal(components.authority, undefined, "authority"); 146 | t.equal(components.userinfo, undefined, 'userinfo') 147 | t.equal(components.host, undefined, 'host') 148 | t.equal(components.port, undefined, 'port') 149 | t.equal(components.path, '', 'path') 150 | t.equal(components.query, undefined, 'query') 151 | t.equal(components.fragment, '%0D', 'fragment') 152 | 153 | // all 154 | components = fastURI.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body') 155 | t.equal(components.error, undefined, 'all errors') 156 | t.equal(components.scheme, 'uri', 'scheme') 157 | // t.equal(components.authority, "user:pass@example.com:123", "authority"); 158 | t.equal(components.userinfo, 'user:pass', 'userinfo') 159 | t.equal(components.host, 'example.com', 'host') 160 | t.equal(components.port, 123, 'port') 161 | t.equal(components.path, '/one/two.three', 'path') 162 | t.equal(components.query, 'q1=a1&q2=a2', 'query') 163 | t.equal(components.fragment, 'body', 'fragment') 164 | 165 | // IPv4address 166 | components = fastURI.parse('//10.10.10.10') 167 | t.equal(components.error, undefined, 'IPv4address errors') 168 | t.equal(components.scheme, undefined, 'scheme') 169 | t.equal(components.userinfo, undefined, 'userinfo') 170 | t.equal(components.host, '10.10.10.10', 'host') 171 | t.equal(components.port, undefined, 'port') 172 | t.equal(components.path, '', 'path') 173 | t.equal(components.query, undefined, 'query') 174 | t.equal(components.fragment, undefined, 'fragment') 175 | 176 | // IPv4address with unformated 0 stay as-is 177 | components = fastURI.parse('//10.10.000.10') // not valid as per https://datatracker.ietf.org/doc/html/rfc5954#section-4.1 178 | t.equal(components.error, undefined, 'IPv4address errors') 179 | t.equal(components.scheme, undefined, 'scheme') 180 | t.equal(components.userinfo, undefined, 'userinfo') 181 | t.equal(components.host, '10.10.000.10', 'host') 182 | t.equal(components.port, undefined, 'port') 183 | t.equal(components.path, '', 'path') 184 | t.equal(components.query, undefined, 'query') 185 | t.equal(components.fragment, undefined, 'fragment') 186 | components = fastURI.parse('//01.01.01.01') // not valid in URIs: https://datatracker.ietf.org/doc/html/rfc3986#section-7.4 187 | t.equal(components.error, undefined, 'IPv4address errors') 188 | t.equal(components.scheme, undefined, 'scheme') 189 | t.equal(components.userinfo, undefined, 'userinfo') 190 | t.equal(components.host, '01.01.01.01', 'host') 191 | t.equal(components.port, undefined, 'port') 192 | t.equal(components.path, '', 'path') 193 | t.equal(components.query, undefined, 'query') 194 | t.equal(components.fragment, undefined, 'fragment') 195 | 196 | // IPv6address 197 | components = fastURI.parse('//[2001:db8::7]') 198 | t.equal(components.error, undefined, 'IPv4address errors') 199 | t.equal(components.scheme, undefined, 'scheme') 200 | t.equal(components.userinfo, undefined, 'userinfo') 201 | t.equal(components.host, '2001:db8::7', 'host') 202 | t.equal(components.port, undefined, 'port') 203 | t.equal(components.path, '', 'path') 204 | t.equal(components.query, undefined, 'query') 205 | t.equal(components.fragment, undefined, 'fragment') 206 | 207 | // invalid IPv6 208 | components = fastURI.parse('//[2001:dbZ::7]') 209 | t.equal(components.host, '[2001:dbz::7]') 210 | 211 | // mixed IPv4address & IPv6address 212 | components = fastURI.parse('//[::ffff:129.144.52.38]') 213 | t.equal(components.error, undefined, 'IPv4address errors') 214 | t.equal(components.scheme, undefined, 'scheme') 215 | t.equal(components.userinfo, undefined, 'userinfo') 216 | t.equal(components.host, '::ffff:129.144.52.38', 'host') 217 | t.equal(components.port, undefined, 'port') 218 | t.equal(components.path, '', 'path') 219 | t.equal(components.query, undefined, 'query') 220 | t.equal(components.fragment, undefined, 'fragment') 221 | 222 | // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4) 223 | components = fastURI.parse('uri://10.10.10.10.example.com/en/process') 224 | t.equal(components.error, undefined, 'mixed errors') 225 | t.equal(components.scheme, 'uri', 'scheme') 226 | t.equal(components.userinfo, undefined, 'userinfo') 227 | t.equal(components.host, '10.10.10.10.example.com', 'host') 228 | t.equal(components.port, undefined, 'port') 229 | t.equal(components.path, '/en/process', 'path') 230 | t.equal(components.query, undefined, 'query') 231 | t.equal(components.fragment, undefined, 'fragment') 232 | 233 | // IPv6address, example from bkw (https://github.com/garycourt/uri-js/pull/16) 234 | components = fastURI.parse('//[2606:2800:220:1:248:1893:25c8:1946]/test') 235 | t.equal(components.error, undefined, 'IPv6address errors') 236 | t.equal(components.scheme, undefined, 'scheme') 237 | t.equal(components.userinfo, undefined, 'userinfo') 238 | t.equal(components.host, '2606:2800:220:1:248:1893:25c8:1946', 'host') 239 | t.equal(components.port, undefined, 'port') 240 | t.equal(components.path, '/test', 'path') 241 | t.equal(components.query, undefined, 'query') 242 | t.equal(components.fragment, undefined, 'fragment') 243 | 244 | // IPv6address, example from RFC 5952 245 | components = fastURI.parse('//[2001:db8::1]:80') 246 | t.equal(components.error, undefined, 'IPv6address errors') 247 | t.equal(components.scheme, undefined, 'scheme') 248 | t.equal(components.userinfo, undefined, 'userinfo') 249 | t.equal(components.host, '2001:db8::1', 'host') 250 | t.equal(components.port, 80, 'port') 251 | t.equal(components.path, '', 'path') 252 | t.equal(components.query, undefined, 'query') 253 | t.equal(components.fragment, undefined, 'fragment') 254 | 255 | // IPv6address with zone identifier, RFC 6874 256 | components = fastURI.parse('//[fe80::a%25en1]') 257 | t.equal(components.error, undefined, 'IPv4address errors') 258 | t.equal(components.scheme, undefined, 'scheme') 259 | t.equal(components.userinfo, undefined, 'userinfo') 260 | t.equal(components.host, 'fe80::a%en1', 'host') 261 | t.equal(components.port, undefined, 'port') 262 | t.equal(components.path, '', 'path') 263 | t.equal(components.query, undefined, 'query') 264 | t.equal(components.fragment, undefined, 'fragment') 265 | 266 | // IPv6address with an unescaped interface specifier, example from pekkanikander (https://github.com/garycourt/uri-js/pull/22) 267 | components = fastURI.parse('//[2001:db8::7%en0]') 268 | t.equal(components.error, undefined, 'IPv6address interface errors') 269 | t.equal(components.scheme, undefined, 'scheme') 270 | t.equal(components.userinfo, undefined, 'userinfo') 271 | t.equal(components.host, '2001:db8::7%en0', 'host') 272 | t.equal(components.port, undefined, 'port') 273 | t.equal(components.path, '', 'path') 274 | t.equal(components.query, undefined, 'query') 275 | t.equal(components.fragment, undefined, 'fragment') 276 | 277 | // UUID V1 278 | components = fastURI.parse('urn:uuid:b571b0bc-4713-11ec-81d3-0242ac130003') 279 | t.equal(components.error, undefined, 'errors') 280 | t.equal(components.scheme, 'urn', 'scheme') 281 | // t.equal(components.authority, undefined, "authority"); 282 | t.equal(components.userinfo, undefined, 'userinfo') 283 | t.equal(components.host, undefined, 'host') 284 | t.equal(components.port, undefined, 'port') 285 | t.equal(components.path, undefined, 'path') 286 | t.equal(components.query, undefined, 'query') 287 | t.equal(components.fragment, undefined, 'fragment') 288 | t.equal(components.nid, 'uuid', 'nid') 289 | t.equal(components.nss, undefined, 'nss') 290 | t.equal(components.uuid, 'b571b0bc-4713-11ec-81d3-0242ac130003', 'uuid') 291 | 292 | // UUID v4 293 | components = fastURI.parse('urn:uuid:97a32222-89b7-420e-8507-4360723e2c2a') 294 | t.equal(components.uuid, '97a32222-89b7-420e-8507-4360723e2c2a', 'uuid') 295 | 296 | components = fastURI.parse('urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6') 297 | t.notSame(components.error, undefined, 'errors') 298 | 299 | components = fastURI.parse('urn:foo:a123,456') 300 | t.equal(components.error, undefined, 'errors') 301 | t.equal(components.scheme, 'urn', 'scheme') 302 | // t.equal(components.authority, undefined, "authority"); 303 | t.equal(components.userinfo, undefined, 'userinfo') 304 | t.equal(components.host, undefined, 'host') 305 | t.equal(components.port, undefined, 'port') 306 | t.equal(components.path, undefined, 'path') 307 | t.equal(components.query, undefined, 'query') 308 | t.equal(components.fragment, undefined, 'fragment') 309 | t.equal(components.nid, 'foo', 'nid') 310 | t.equal(components.nss, 'a123,456', 'nss') 311 | 312 | components = fastURI.parse('//[2606:2800:220:1:248:1893:25c8:1946:43209]') 313 | t.equal(components.host, '[2606:2800:220:1:248:1893:25c8:1946:43209]') 314 | 315 | components = fastURI.parse('urn:foo:|\\24fpl') 316 | t.equal(components.error, 'URN can not be parsed.') 317 | t.end() 318 | }) 319 | -------------------------------------------------------------------------------- /test/uri-js.test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const fastURI = require('..') 5 | 6 | /** 7 | * URI.js 8 | * 9 | * @fileoverview An RFC 3986 compliant, scheme extendable URI parsing/normalizing/resolving/serializing library for JavaScript. 10 | * @author Gary Court 11 | * @see http://github.com/garycourt/uri-js 12 | */ 13 | 14 | /** 15 | * Copyright 2011 Gary Court. All rights reserved. 16 | * 17 | * Redistribution and use in source and binary forms, with or without modification, are 18 | * permitted provided that the following conditions are met: 19 | * 20 | * 1. Redistributions of source code must retain the above copyright notice, this list of 21 | * conditions and the following disclaimer. 22 | * 23 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list 24 | * of conditions and the following disclaimer in the documentation and/or other materials 25 | * provided with the distribution. 26 | * 27 | * THIS SOFTWARE IS PROVIDED BY GARY COURT ``AS IS'' AND ANY EXPRESS OR IMPLIED 28 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 29 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR 30 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 32 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 33 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 | * 37 | * The views and conclusions contained in the software and documentation are those of the 38 | * authors and should not be interpreted as representing official policies, either expressed 39 | * or implied, of Gary Court. 40 | */ 41 | 42 | test('Acquire URI', (t) => { 43 | t.ok(fastURI) 44 | t.end() 45 | }) 46 | 47 | test('URI Parsing', (t) => { 48 | let components 49 | 50 | // scheme 51 | components = fastURI.parse('uri:') 52 | t.equal(components.error, undefined, 'scheme errors') 53 | t.equal(components.scheme, 'uri', 'scheme') 54 | t.equal(components.userinfo, undefined, 'userinfo') 55 | t.equal(components.host, undefined, 'host') 56 | t.equal(components.port, undefined, 'port') 57 | t.equal(components.path, '', 'path') 58 | t.equal(components.query, undefined, 'query') 59 | t.equal(components.fragment, undefined, 'fragment') 60 | 61 | // userinfo 62 | components = fastURI.parse('//@') 63 | t.equal(components.error, undefined, 'userinfo errors') 64 | t.equal(components.scheme, undefined, 'scheme') 65 | t.equal(components.userinfo, '', 'userinfo') 66 | t.equal(components.host, '', 'host') 67 | t.equal(components.port, undefined, 'port') 68 | t.equal(components.path, '', 'path') 69 | t.equal(components.query, undefined, 'query') 70 | t.equal(components.fragment, undefined, 'fragment') 71 | 72 | // host 73 | components = fastURI.parse('//') 74 | t.equal(components.error, undefined, 'host errors') 75 | t.equal(components.scheme, undefined, 'scheme') 76 | t.equal(components.userinfo, undefined, 'userinfo') 77 | t.equal(components.host, '', 'host') 78 | t.equal(components.port, undefined, 'port') 79 | t.equal(components.path, '', 'path') 80 | t.equal(components.query, undefined, 'query') 81 | t.equal(components.fragment, undefined, 'fragment') 82 | 83 | // port 84 | components = fastURI.parse('//:') 85 | t.equal(components.error, undefined, 'port errors') 86 | t.equal(components.scheme, undefined, 'scheme') 87 | t.equal(components.userinfo, undefined, 'userinfo') 88 | t.equal(components.host, '', 'host') 89 | t.equal(components.port, '', 'port') 90 | t.equal(components.path, '', 'path') 91 | t.equal(components.query, undefined, 'query') 92 | t.equal(components.fragment, undefined, 'fragment') 93 | 94 | // path 95 | components = fastURI.parse('') 96 | t.equal(components.error, undefined, 'path errors') 97 | t.equal(components.scheme, undefined, 'scheme') 98 | t.equal(components.userinfo, undefined, 'userinfo') 99 | t.equal(components.host, undefined, 'host') 100 | t.equal(components.port, undefined, 'port') 101 | t.equal(components.path, '', 'path') 102 | t.equal(components.query, undefined, 'query') 103 | t.equal(components.fragment, undefined, 'fragment') 104 | 105 | // query 106 | components = fastURI.parse('?') 107 | t.equal(components.error, undefined, 'query errors') 108 | t.equal(components.scheme, undefined, 'scheme') 109 | t.equal(components.userinfo, undefined, 'userinfo') 110 | t.equal(components.host, undefined, 'host') 111 | t.equal(components.port, undefined, 'port') 112 | t.equal(components.path, '', 'path') 113 | t.equal(components.query, '', 'query') 114 | t.equal(components.fragment, undefined, 'fragment') 115 | 116 | // fragment 117 | components = fastURI.parse('#') 118 | t.equal(components.error, undefined, 'fragment errors') 119 | t.equal(components.scheme, undefined, 'scheme') 120 | t.equal(components.userinfo, undefined, 'userinfo') 121 | t.equal(components.host, undefined, 'host') 122 | t.equal(components.port, undefined, 'port') 123 | t.equal(components.path, '', 'path') 124 | t.equal(components.query, undefined, 'query') 125 | t.equal(components.fragment, '', 'fragment') 126 | 127 | // fragment with character tabulation 128 | components = fastURI.parse('#\t') 129 | t.equal(components.error, undefined, 'path errors') 130 | t.equal(components.scheme, undefined, 'scheme') 131 | t.equal(components.userinfo, undefined, 'userinfo') 132 | t.equal(components.host, undefined, 'host') 133 | t.equal(components.port, undefined, 'port') 134 | t.equal(components.path, '', 'path') 135 | t.equal(components.query, undefined, 'query') 136 | t.equal(components.fragment, '%09', 'fragment') 137 | 138 | // fragment with line feed 139 | components = fastURI.parse('#\n') 140 | t.equal(components.error, undefined, 'path errors') 141 | t.equal(components.scheme, undefined, 'scheme') 142 | t.equal(components.userinfo, undefined, 'userinfo') 143 | t.equal(components.host, undefined, 'host') 144 | t.equal(components.port, undefined, 'port') 145 | t.equal(components.path, '', 'path') 146 | t.equal(components.query, undefined, 'query') 147 | t.equal(components.fragment, '%0A', 'fragment') 148 | 149 | // fragment with line tabulation 150 | components = fastURI.parse('#\v') 151 | t.equal(components.error, undefined, 'path errors') 152 | t.equal(components.scheme, undefined, 'scheme') 153 | t.equal(components.userinfo, undefined, 'userinfo') 154 | t.equal(components.host, undefined, 'host') 155 | t.equal(components.port, undefined, 'port') 156 | t.equal(components.path, '', 'path') 157 | t.equal(components.query, undefined, 'query') 158 | t.equal(components.fragment, '%0B', 'fragment') 159 | 160 | // fragment with form feed 161 | components = fastURI.parse('#\f') 162 | t.equal(components.error, undefined, 'path errors') 163 | t.equal(components.scheme, undefined, 'scheme') 164 | t.equal(components.userinfo, undefined, 'userinfo') 165 | t.equal(components.host, undefined, 'host') 166 | t.equal(components.port, undefined, 'port') 167 | t.equal(components.path, '', 'path') 168 | t.equal(components.query, undefined, 'query') 169 | t.equal(components.fragment, '%0C', 'fragment') 170 | 171 | // fragment with carriage return 172 | components = fastURI.parse('#\r') 173 | t.equal(components.error, undefined, 'path errors') 174 | t.equal(components.scheme, undefined, 'scheme') 175 | t.equal(components.userinfo, undefined, 'userinfo') 176 | t.equal(components.host, undefined, 'host') 177 | t.equal(components.port, undefined, 'port') 178 | t.equal(components.path, '', 'path') 179 | t.equal(components.query, undefined, 'query') 180 | t.equal(components.fragment, '%0D', 'fragment') 181 | 182 | // all 183 | components = fastURI.parse('uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body') 184 | t.equal(components.error, undefined, 'all errors') 185 | t.equal(components.scheme, 'uri', 'scheme') 186 | t.equal(components.userinfo, 'user:pass', 'userinfo') 187 | t.equal(components.host, 'example.com', 'host') 188 | t.equal(components.port, 123, 'port') 189 | t.equal(components.path, '/one/two.three', 'path') 190 | t.equal(components.query, 'q1=a1&q2=a2', 'query') 191 | t.equal(components.fragment, 'body', 'fragment') 192 | 193 | // IPv4address 194 | components = fastURI.parse('//10.10.10.10') 195 | t.equal(components.error, undefined, 'IPv4address errors') 196 | t.equal(components.scheme, undefined, 'scheme') 197 | t.equal(components.userinfo, undefined, 'userinfo') 198 | t.equal(components.host, '10.10.10.10', 'host') 199 | t.equal(components.port, undefined, 'port') 200 | t.equal(components.path, '', 'path') 201 | t.equal(components.query, undefined, 'query') 202 | t.equal(components.fragment, undefined, 'fragment') 203 | 204 | // IPv6address 205 | components = fastURI.parse('//[2001:db8::7]') 206 | t.equal(components.error, undefined, 'IPv4address errors') 207 | t.equal(components.scheme, undefined, 'scheme') 208 | t.equal(components.userinfo, undefined, 'userinfo') 209 | t.equal(components.host, '2001:db8::7', 'host') 210 | t.equal(components.port, undefined, 'port') 211 | t.equal(components.path, '', 'path') 212 | t.equal(components.query, undefined, 'query') 213 | t.equal(components.fragment, undefined, 'fragment') 214 | 215 | // mixed IPv4address & IPv6address 216 | components = fastURI.parse('//[::ffff:129.144.52.38]') 217 | t.equal(components.error, undefined, 'IPv4address errors') 218 | t.equal(components.scheme, undefined, 'scheme') 219 | t.equal(components.userinfo, undefined, 'userinfo') 220 | t.equal(components.host, '::ffff:129.144.52.38', 'host') 221 | t.equal(components.port, undefined, 'port') 222 | t.equal(components.path, '', 'path') 223 | t.equal(components.query, undefined, 'query') 224 | t.equal(components.fragment, undefined, 'fragment') 225 | 226 | // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4) 227 | components = fastURI.parse('uri://10.10.10.10.example.com/en/process') 228 | t.equal(components.error, undefined, 'mixed errors') 229 | t.equal(components.scheme, 'uri', 'scheme') 230 | t.equal(components.userinfo, undefined, 'userinfo') 231 | t.equal(components.host, '10.10.10.10.example.com', 'host') 232 | t.equal(components.port, undefined, 'port') 233 | t.equal(components.path, '/en/process', 'path') 234 | t.equal(components.query, undefined, 'query') 235 | t.equal(components.fragment, undefined, 'fragment') 236 | 237 | // IPv6address, example from bkw (https://github.com/garycourt/uri-js/pull/16) 238 | components = fastURI.parse('//[2606:2800:220:1:248:1893:25c8:1946]/test') 239 | t.equal(components.error, undefined, 'IPv6address errors') 240 | t.equal(components.scheme, undefined, 'scheme') 241 | t.equal(components.userinfo, undefined, 'userinfo') 242 | t.equal(components.host, '2606:2800:220:1:248:1893:25c8:1946', 'host') 243 | t.equal(components.port, undefined, 'port') 244 | t.equal(components.path, '/test', 'path') 245 | t.equal(components.query, undefined, 'query') 246 | t.equal(components.fragment, undefined, 'fragment') 247 | 248 | // IPv6address, example from RFC 5952 249 | components = fastURI.parse('//[2001:db8::1]:80') 250 | t.equal(components.error, undefined, 'IPv6address errors') 251 | t.equal(components.scheme, undefined, 'scheme') 252 | t.equal(components.userinfo, undefined, 'userinfo') 253 | t.equal(components.host, '2001:db8::1', 'host') 254 | t.equal(components.port, 80, 'port') 255 | t.equal(components.path, '', 'path') 256 | t.equal(components.query, undefined, 'query') 257 | t.equal(components.fragment, undefined, 'fragment') 258 | 259 | // IPv6address with zone identifier, RFC 6874 260 | components = fastURI.parse('//[fe80::a%25en1]') 261 | t.equal(components.error, undefined, 'IPv4address errors') 262 | t.equal(components.scheme, undefined, 'scheme') 263 | t.equal(components.userinfo, undefined, 'userinfo') 264 | t.equal(components.host, 'fe80::a%en1', 'host') 265 | t.equal(components.port, undefined, 'port') 266 | t.equal(components.path, '', 'path') 267 | t.equal(components.query, undefined, 'query') 268 | t.equal(components.fragment, undefined, 'fragment') 269 | 270 | // IPv6address with an unescaped interface specifier, example from pekkanikander (https://github.com/garycourt/uri-js/pull/22) 271 | components = fastURI.parse('//[2001:db8::7%en0]') 272 | t.equal(components.error, undefined, 'IPv6address interface errors') 273 | t.equal(components.scheme, undefined, 'scheme') 274 | t.equal(components.userinfo, undefined, 'userinfo') 275 | t.equal(components.host, '2001:db8::7%en0', 'host') 276 | t.equal(components.port, undefined, 'port') 277 | t.equal(components.path, '', 'path') 278 | t.equal(components.query, undefined, 'query') 279 | t.equal(components.fragment, undefined, 'fragment') 280 | 281 | t.end() 282 | }) 283 | 284 | test('URI Serialization', (t) => { 285 | let components = { 286 | scheme: undefined, 287 | userinfo: undefined, 288 | host: undefined, 289 | port: undefined, 290 | path: undefined, 291 | query: undefined, 292 | fragment: undefined 293 | } 294 | t.equal(fastURI.serialize(components), '', 'Undefined Components') 295 | 296 | components = { 297 | scheme: '', 298 | userinfo: '', 299 | host: '', 300 | port: 0, 301 | path: '', 302 | query: '', 303 | fragment: '' 304 | } 305 | t.equal(fastURI.serialize(components), '//@:0?#', 'Empty Components') 306 | 307 | components = { 308 | scheme: 'uri', 309 | userinfo: 'foo:bar', 310 | host: 'example.com', 311 | port: 1, 312 | path: 'path', 313 | query: 'query', 314 | fragment: 'fragment' 315 | } 316 | t.equal(fastURI.serialize(components), 'uri://foo:bar@example.com:1/path?query#fragment', 'All Components') 317 | 318 | components = { 319 | scheme: 'uri', 320 | host: 'example.com', 321 | port: '9000' 322 | } 323 | t.equal(fastURI.serialize(components), 'uri://example.com:9000', 'String port') 324 | 325 | t.equal(fastURI.serialize({ path: '//path' }), '/%2Fpath', 'Double slash path') 326 | t.equal(fastURI.serialize({ path: 'foo:bar' }), 'foo%3Abar', 'Colon path') 327 | t.equal(fastURI.serialize({ path: '?query' }), '%3Fquery', 'Query path') 328 | 329 | // mixed IPv4address & reg-name, example from terion-name (https://github.com/garycourt/uri-js/issues/4) 330 | t.equal(fastURI.serialize({ host: '10.10.10.10.example.com' }), '//10.10.10.10.example.com', 'Mixed IPv4address & reg-name') 331 | 332 | // IPv6address 333 | t.equal(fastURI.serialize({ host: '2001:db8::7' }), '//[2001:db8::7]', 'IPv6 Host') 334 | t.equal(fastURI.serialize({ host: '::ffff:129.144.52.38' }), '//[::ffff:129.144.52.38]', 'IPv6 Mixed Host') 335 | t.equal(fastURI.serialize({ host: '2606:2800:220:1:248:1893:25c8:1946' }), '//[2606:2800:220:1:248:1893:25c8:1946]', 'IPv6 Full Host') 336 | 337 | // IPv6address with zone identifier, RFC 6874 338 | t.equal(fastURI.serialize({ host: 'fe80::a%en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Unescaped Host') 339 | t.equal(fastURI.serialize({ host: 'fe80::a%25en1' }), '//[fe80::a%25en1]', 'IPv6 Zone Escaped Host') 340 | 341 | t.end() 342 | }) 343 | 344 | test('URI Resolving', { skip: true }, (t) => { 345 | // normal examples from RFC 3986 346 | const base = 'uri://a/b/c/d;p?q' 347 | t.equal(fastURI.resolve(base, 'g:h'), 'g:h', 'g:h') 348 | t.equal(fastURI.resolve(base, 'g'), 'uri://a/b/c/g', 'g') 349 | t.equal(fastURI.resolve(base, './g'), 'uri://a/b/c/g', './g') 350 | t.equal(fastURI.resolve(base, 'g/'), 'uri://a/b/c/g/', 'g/') 351 | t.equal(fastURI.resolve(base, '/g'), 'uri://a/g', '/g') 352 | t.equal(fastURI.resolve(base, '//g'), 'uri://g', '//g') 353 | t.equal(fastURI.resolve(base, '?y'), 'uri://a/b/c/d;p?y', '?y') 354 | t.equal(fastURI.resolve(base, 'g?y'), 'uri://a/b/c/g?y', 'g?y') 355 | t.equal(fastURI.resolve(base, '#s'), 'uri://a/b/c/d;p?q#s', '#s') 356 | t.equal(fastURI.resolve(base, 'g#s'), 'uri://a/b/c/g#s', 'g#s') 357 | t.equal(fastURI.resolve(base, 'g?y#s'), 'uri://a/b/c/g?y#s', 'g?y#s') 358 | t.equal(fastURI.resolve(base, ';x'), 'uri://a/b/c/;x', ';x') 359 | t.equal(fastURI.resolve(base, 'g;x'), 'uri://a/b/c/g;x', 'g;x') 360 | t.equal(fastURI.resolve(base, 'g;x?y#s'), 'uri://a/b/c/g;x?y#s', 'g;x?y#s') 361 | t.equal(fastURI.resolve(base, ''), 'uri://a/b/c/d;p?q', '') 362 | t.equal(fastURI.resolve(base, '.'), 'uri://a/b/c/', '.') 363 | t.equal(fastURI.resolve(base, './'), 'uri://a/b/c/', './') 364 | t.equal(fastURI.resolve(base, '..'), 'uri://a/b/', '..') 365 | t.equal(fastURI.resolve(base, '../'), 'uri://a/b/', '../') 366 | t.equal(fastURI.resolve(base, '../g'), 'uri://a/b/g', '../g') 367 | t.equal(fastURI.resolve(base, '../..'), 'uri://a/', '../..') 368 | t.equal(fastURI.resolve(base, '../../'), 'uri://a/', '../../') 369 | t.equal(fastURI.resolve(base, '../../g'), 'uri://a/g', '../../g') 370 | 371 | // abnormal examples from RFC 3986 372 | t.equal(fastURI.resolve(base, '../../../g'), 'uri://a/g', '../../../g') 373 | t.equal(fastURI.resolve(base, '../../../../g'), 'uri://a/g', '../../../../g') 374 | 375 | t.equal(fastURI.resolve(base, '/./g'), 'uri://a/g', '/./g') 376 | t.equal(fastURI.resolve(base, '/../g'), 'uri://a/g', '/../g') 377 | t.equal(fastURI.resolve(base, 'g.'), 'uri://a/b/c/g.', 'g.') 378 | t.equal(fastURI.resolve(base, '.g'), 'uri://a/b/c/.g', '.g') 379 | t.equal(fastURI.resolve(base, 'g..'), 'uri://a/b/c/g..', 'g..') 380 | t.equal(fastURI.resolve(base, '..g'), 'uri://a/b/c/..g', '..g') 381 | 382 | t.equal(fastURI.resolve(base, './../g'), 'uri://a/b/g', './../g') 383 | t.equal(fastURI.resolve(base, './g/.'), 'uri://a/b/c/g/', './g/.') 384 | t.equal(fastURI.resolve(base, 'g/./h'), 'uri://a/b/c/g/h', 'g/./h') 385 | t.equal(fastURI.resolve(base, 'g/../h'), 'uri://a/b/c/h', 'g/../h') 386 | t.equal(fastURI.resolve(base, 'g;x=1/./y'), 'uri://a/b/c/g;x=1/y', 'g;x=1/./y') 387 | t.equal(fastURI.resolve(base, 'g;x=1/../y'), 'uri://a/b/c/y', 'g;x=1/../y') 388 | 389 | t.equal(fastURI.resolve(base, 'g?y/./x'), 'uri://a/b/c/g?y/./x', 'g?y/./x') 390 | t.equal(fastURI.resolve(base, 'g?y/../x'), 'uri://a/b/c/g?y/../x', 'g?y/../x') 391 | t.equal(fastURI.resolve(base, 'g#s/./x'), 'uri://a/b/c/g#s/./x', 'g#s/./x') 392 | t.equal(fastURI.resolve(base, 'g#s/../x'), 'uri://a/b/c/g#s/../x', 'g#s/../x') 393 | 394 | t.equal(fastURI.resolve(base, 'uri:g'), 'uri:g', 'uri:g') 395 | t.equal(fastURI.resolve(base, 'uri:g', { tolerant: true }), 'uri://a/b/c/g', 'uri:g') 396 | 397 | // examples by PAEz 398 | t.equal(fastURI.resolve('//www.g.com/', '/adf\ngf'), '//www.g.com/adf%0Agf', '/adf\\ngf') 399 | t.equal(fastURI.resolve('//www.g.com/error\n/bleh/bleh', '..'), '//www.g.com/error%0A/', '//www.g.com/error\\n/bleh/bleh') 400 | 401 | t.end() 402 | }) 403 | 404 | test('URI Normalizing', { skip: true }, (t) => { 405 | // test from RFC 3987 406 | t.equal(fastURI.normalize('uri://www.example.org/red%09ros\xE9#red'), 'uri://www.example.org/red%09ros%C3%A9#red') 407 | 408 | // IPv4address 409 | t.equal(fastURI.normalize('//192.068.001.000'), '//192.68.1.0') 410 | 411 | // IPv6address, example from RFC 3513 412 | t.equal(fastURI.normalize('http://[1080::8:800:200C:417A]/'), 'http://[1080::8:800:200c:417a]/') 413 | 414 | // IPv6address, examples from RFC 5952 415 | t.equal(fastURI.normalize('//[2001:0db8::0001]/'), '//[2001:db8::1]/') 416 | t.equal(fastURI.normalize('//[2001:db8::1:0000:1]/'), '//[2001:db8::1:0:1]/') 417 | t.equal(fastURI.normalize('//[2001:db8:0:0:0:0:2:1]/'), '//[2001:db8::2:1]/') 418 | t.equal(fastURI.normalize('//[2001:db8:0:1:1:1:1:1]/'), '//[2001:db8:0:1:1:1:1:1]/') 419 | t.equal(fastURI.normalize('//[2001:0:0:1:0:0:0:1]/'), '//[2001:0:0:1::1]/') 420 | t.equal(fastURI.normalize('//[2001:db8:0:0:1:0:0:1]/'), '//[2001:db8::1:0:0:1]/') 421 | t.equal(fastURI.normalize('//[2001:DB8::1]/'), '//[2001:db8::1]/') 422 | t.equal(fastURI.normalize('//[0:0:0:0:0:ffff:192.0.2.1]/'), '//[::ffff:192.0.2.1]/') 423 | 424 | // Mixed IPv4 and IPv6 address 425 | t.equal(fastURI.normalize('//[1:2:3:4:5:6:192.0.2.1]/'), '//[1:2:3:4:5:6:192.0.2.1]/') 426 | t.equal(fastURI.normalize('//[1:2:3:4:5:6:192.068.001.000]/'), '//[1:2:3:4:5:6:192.68.1.0]/') 427 | 428 | t.end() 429 | }) 430 | 431 | test('URI Equals', (t) => { 432 | // test from RFC 3986 433 | t.equal(fastURI.equal('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'), true) 434 | 435 | // test from RFC 3987 436 | t.equal(fastURI.equal('http://example.org/~user', 'http://example.org/%7euser'), true) 437 | 438 | t.end() 439 | }) 440 | 441 | test('Escape Component', { skip: true }, (t) => { 442 | let chr 443 | for (let d = 0; d <= 129; ++d) { 444 | chr = String.fromCharCode(d) 445 | if (!chr.match(/[$&+,;=]/)) { 446 | t.equal(fastURI.escapeComponent(chr), encodeURIComponent(chr)) 447 | } else { 448 | t.equal(fastURI.escapeComponent(chr), chr) 449 | } 450 | } 451 | t.equal(fastURI.escapeComponent('\u00c0'), encodeURIComponent('\u00c0')) 452 | t.equal(fastURI.escapeComponent('\u07ff'), encodeURIComponent('\u07ff')) 453 | t.equal(fastURI.escapeComponent('\u0800'), encodeURIComponent('\u0800')) 454 | t.equal(fastURI.escapeComponent('\u30a2'), encodeURIComponent('\u30a2')) 455 | t.end() 456 | }) 457 | 458 | test('Unescape Component', { skip: true }, (t) => { 459 | let chr 460 | for (let d = 0; d <= 129; ++d) { 461 | chr = String.fromCharCode(d) 462 | t.equal(fastURI.unescapeComponent(encodeURIComponent(chr)), chr) 463 | } 464 | t.equal(fastURI.unescapeComponent(encodeURIComponent('\u00c0')), '\u00c0') 465 | t.equal(fastURI.unescapeComponent(encodeURIComponent('\u07ff')), '\u07ff') 466 | t.equal(fastURI.unescapeComponent(encodeURIComponent('\u0800')), '\u0800') 467 | t.equal(fastURI.unescapeComponent(encodeURIComponent('\u30a2')), '\u30a2') 468 | t.end() 469 | }) 470 | 471 | const IRI_OPTION = { iri: true, unicodeSupport: true } 472 | 473 | test('IRI Parsing', { skip: true }, (t) => { 474 | const components = fastURI.parse('uri://us\xA0er:pa\uD7FFss@example.com:123/o\uF900ne/t\uFDCFwo.t\uFDF0hree?q1=a1\uF8FF\uE000&q2=a2#bo\uFFEFdy', IRI_OPTION) 475 | t.equal(components.error, undefined, 'all errors') 476 | t.equal(components.scheme, 'uri', 'scheme') 477 | t.equal(components.userinfo, 'us\xA0er:pa\uD7FFss', 'userinfo') 478 | t.equal(components.host, 'example.com', 'host') 479 | t.equal(components.port, 123, 'port') 480 | t.equal(components.path, '/o\uF900ne/t\uFDCFwo.t\uFDF0hree', 'path') 481 | t.equal(components.query, 'q1=a1\uF8FF\uE000&q2=a2', 'query') 482 | t.equal(components.fragment, 'bo\uFFEFdy', 'fragment') 483 | t.end() 484 | }) 485 | 486 | test('IRI Serialization', { skip: true }, (t) => { 487 | const components = { 488 | scheme: 'uri', 489 | userinfo: 'us\xA0er:pa\uD7FFss', 490 | host: 'example.com', 491 | port: 123, 492 | path: '/o\uF900ne/t\uFDCFwo.t\uFDF0hree', 493 | query: 'q1=a1\uF8FF\uE000&q2=a2', 494 | fragment: 'bo\uFFEFdy\uE001' 495 | } 496 | t.equal(fastURI.serialize(components, IRI_OPTION), 'uri://us\xA0er:pa\uD7FFss@example.com:123/o\uF900ne/t\uFDCFwo.t\uFDF0hree?q1=a1\uF8FF\uE000&q2=a2#bo\uFFEFdy%EE%80%81') 497 | t.end() 498 | }) 499 | 500 | test('IRI Normalizing', { skip: true }, (t) => { 501 | t.equal(fastURI.normalize('uri://www.example.org/red%09ros\xE9#red', IRI_OPTION), 'uri://www.example.org/red%09ros\xE9#red') 502 | t.end() 503 | }) 504 | 505 | test('IRI Equals', { skip: true }, (t) => { 506 | // example from RFC 3987 507 | t.equal(fastURI.equal('example://a/b/c/%7Bfoo%7D/ros\xE9', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9', IRI_OPTION), true) 508 | t.end() 509 | }) 510 | 511 | test('Convert IRI to URI', { skip: true }, (t) => { 512 | // example from RFC 3987 513 | t.equal(fastURI.serialize(fastURI.parse('uri://www.example.org/red%09ros\xE9#red', IRI_OPTION)), 'uri://www.example.org/red%09ros%C3%A9#red') 514 | 515 | // Internationalized Domain Name conversion via punycode example from RFC 3987 516 | t.equal(fastURI.serialize(fastURI.parse('uri://r\xE9sum\xE9.example.org', { iri: true, domainHost: true }), { domainHost: true }), 'uri://xn--rsum-bpad.example.org') 517 | t.end() 518 | }) 519 | 520 | test('Convert URI to IRI', { skip: true }, (t) => { 521 | // examples from RFC 3987 522 | t.equal(fastURI.serialize(fastURI.parse('uri://www.example.org/D%C3%BCrst'), IRI_OPTION), 'uri://www.example.org/D\xFCrst') 523 | t.equal(fastURI.serialize(fastURI.parse('uri://www.example.org/D%FCrst'), IRI_OPTION), 'uri://www.example.org/D%FCrst') 524 | t.equal(fastURI.serialize(fastURI.parse('uri://xn--99zt52a.example.org/%e2%80%ae'), IRI_OPTION), 'uri://xn--99zt52a.example.org/%E2%80%AE') // or uri://\u7D0D\u8C46.example.org/%E2%80%AE 525 | 526 | // Internationalized Domain Name conversion via punycode example from RFC 3987 527 | t.equal(fastURI.serialize(fastURI.parse('uri://xn--rsum-bpad.example.org', { domainHost: true }), { iri: true, domainHost: true }), 'uri://r\xE9sum\xE9.example.org') 528 | t.end() 529 | }) 530 | 531 | if (fastURI.SCHEMES.http) { 532 | test('HTTP Equals', (t) => { 533 | // test from RFC 2616 534 | t.equal(fastURI.equal('http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'), true) 535 | t.equal(fastURI.equal('http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'), true) 536 | t.equal(fastURI.equal('http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'), true) 537 | t.equal(fastURI.equal('HTTP://ABC.COM', 'http://abc.com/'), true) 538 | // test from RFC 3986 539 | t.equal(fastURI.equal('http://example.com:/', 'http://example.com:80/'), true) 540 | t.end() 541 | }) 542 | } 543 | 544 | if (fastURI.SCHEMES.https) { 545 | test('HTTPS Equals', (t) => { 546 | t.equal(fastURI.equal('https://example.com', 'https://example.com:443/'), true) 547 | t.equal(fastURI.equal('https://example.com:/', 'https://example.com:443/'), true) 548 | t.end() 549 | }) 550 | } 551 | 552 | if (fastURI.SCHEMES.urn) { 553 | test('URN Parsing', (t) => { 554 | // example from RFC 2141 555 | const components = fastURI.parse('urn:foo:a123,456') 556 | t.equal(components.error, undefined, 'errors') 557 | t.equal(components.scheme, 'urn', 'scheme') 558 | t.equal(components.userinfo, undefined, 'userinfo') 559 | t.equal(components.host, undefined, 'host') 560 | t.equal(components.port, undefined, 'port') 561 | t.equal(components.path, undefined, 'path') 562 | t.equal(components.query, undefined, 'query') 563 | t.equal(components.fragment, undefined, 'fragment') 564 | t.equal(components.nid, 'foo', 'nid') 565 | t.equal(components.nss, 'a123,456', 'nss') 566 | t.end() 567 | }) 568 | 569 | test('URN Serialization', (t) => { 570 | // example from RFC 2141 571 | const components = { 572 | scheme: 'urn', 573 | nid: 'foo', 574 | nss: 'a123,456' 575 | } 576 | t.equal(fastURI.serialize(components), 'urn:foo:a123,456') 577 | t.end() 578 | }) 579 | 580 | test('URN Equals', { skip: true }, (t) => { 581 | // test from RFC 2141 582 | t.equal(fastURI.equal('urn:foo:a123,456', 'urn:foo:a123,456'), true) 583 | t.equal(fastURI.equal('urn:foo:a123,456', 'URN:foo:a123,456'), true) 584 | t.equal(fastURI.equal('urn:foo:a123,456', 'urn:FOO:a123,456'), true) 585 | t.equal(fastURI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false) 586 | t.equal(fastURI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true) 587 | t.end() 588 | }) 589 | 590 | test('URN Resolving', (t) => { 591 | // example from epoberezkin 592 | t.equal(fastURI.resolve('', 'urn:some:ip:prop'), 'urn:some:ip:prop') 593 | t.equal(fastURI.resolve('#', 'urn:some:ip:prop'), 'urn:some:ip:prop') 594 | t.equal(fastURI.resolve('urn:some:ip:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop') 595 | t.equal(fastURI.resolve('urn:some:other:prop', 'urn:some:ip:prop'), 'urn:some:ip:prop') 596 | t.end() 597 | }) 598 | 599 | test('UUID Parsing', (t) => { 600 | // example from RFC 4122 601 | let components = fastURI.parse('urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6') 602 | t.equal(components.error, undefined, 'errors') 603 | t.equal(components.scheme, 'urn', 'scheme') 604 | t.equal(components.userinfo, undefined, 'userinfo') 605 | t.equal(components.host, undefined, 'host') 606 | t.equal(components.port, undefined, 'port') 607 | t.equal(components.path, undefined, 'path') 608 | t.equal(components.query, undefined, 'query') 609 | t.equal(components.fragment, undefined, 'fragment') 610 | t.equal(components.nid, 'uuid', 'nid') 611 | t.equal(components.nss, undefined, 'nss') 612 | t.equal(components.uuid, 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'uuid') 613 | 614 | components = fastURI.parse('urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6') 615 | t.notEqual(components.error, undefined, 'errors') 616 | t.end() 617 | }) 618 | 619 | test('UUID Serialization', (t) => { 620 | // example from RFC 4122 621 | let components = { 622 | scheme: 'urn', 623 | nid: 'uuid', 624 | uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 625 | } 626 | t.equal(fastURI.serialize(components), 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6') 627 | 628 | components = { 629 | scheme: 'urn', 630 | nid: 'uuid', 631 | uuid: 'notauuid-7dec-11d0-a765-00a0c91e6bf6' 632 | } 633 | t.equal(fastURI.serialize(components), 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6') 634 | t.end() 635 | }) 636 | 637 | test('UUID Equals', (t) => { 638 | t.equal(fastURI.equal('URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'), true) 639 | t.end() 640 | }) 641 | 642 | test('URN NID Override', (t) => { 643 | let components = fastURI.parse('urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', { nid: 'uuid' }) 644 | t.equal(components.error, undefined, 'errors') 645 | t.equal(components.scheme, 'urn', 'scheme') 646 | t.equal(components.path, undefined, 'path') 647 | t.equal(components.nid, 'foo', 'nid') 648 | t.equal(components.nss, undefined, 'nss') 649 | t.equal(components.uuid, 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'uuid') 650 | 651 | components = { 652 | scheme: 'urn', 653 | nid: 'foo', 654 | uuid: 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6' 655 | } 656 | t.equal(fastURI.serialize(components, { nid: 'uuid' }), 'urn:foo:f81d4fae-7dec-11d0-a765-00a0c91e6bf6') 657 | t.end() 658 | }) 659 | } 660 | 661 | if (fastURI.SCHEMES.mailto) { 662 | test('Mailto Parse', (t) => { 663 | let components 664 | 665 | // tests from RFC 6068 666 | 667 | components = fastURI.parse('mailto:chris@example.com') 668 | t.equal(components.error, undefined, 'error') 669 | t.equal(components.scheme, 'mailto', 'scheme') 670 | t.equal(components.userinfo, undefined, 'userinfo') 671 | t.equal(components.host, undefined, 'host') 672 | t.equal(components.port, undefined, 'port') 673 | t.equal(components.path, undefined, 'path') 674 | t.equal(components.query, undefined, 'query') 675 | t.equal(components.fragment, undefined, 'fragment') 676 | t.deepEqual(components.to, ['chris@example.com'], 'to') 677 | t.equal(components.subject, undefined, 'subject') 678 | t.equal(components.body, undefined, 'body') 679 | t.equal(components.headers, undefined, 'headers') 680 | 681 | components = fastURI.parse('mailto:infobot@example.com?subject=current-issue') 682 | t.deepEqual(components.to, ['infobot@example.com'], 'to') 683 | t.equal(components.subject, 'current-issue', 'subject') 684 | 685 | components = fastURI.parse('mailto:infobot@example.com?body=send%20current-issue') 686 | t.deepEqual(components.to, ['infobot@example.com'], 'to') 687 | t.equal(components.body, 'send current-issue', 'body') 688 | 689 | components = fastURI.parse('mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index') 690 | t.deepEqual(components.to, ['infobot@example.com'], 'to') 691 | t.equal(components.body, 'send current-issue\x0D\x0Asend index', 'body') 692 | 693 | components = fastURI.parse('mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E') 694 | t.deepEqual(components.to, ['list@example.org'], 'to') 695 | t.deepEqual(components.headers, { 'In-Reply-To': '<3469A91.D10AF4C@example.com>' }, 'headers') 696 | 697 | components = fastURI.parse('mailto:majordomo@example.com?body=subscribe%20bamboo-l') 698 | t.deepEqual(components.to, ['majordomo@example.com'], 'to') 699 | t.equal(components.body, 'subscribe bamboo-l', 'body') 700 | 701 | components = fastURI.parse('mailto:joe@example.com?cc=bob@example.com&body=hello') 702 | t.deepEqual(components.to, ['joe@example.com'], 'to') 703 | t.equal(components.body, 'hello', 'body') 704 | t.deepEqual(components.headers, { cc: 'bob@example.com' }, 'headers') 705 | 706 | components = fastURI.parse('mailto:joe@example.com?cc=bob@example.com?body=hello') 707 | if (fastURI.VALIDATE_SUPPORT) t.ok(components.error, 'invalid header fields') 708 | 709 | components = fastURI.parse('mailto:gorby%25kremvax@example.com') 710 | t.deepEqual(components.to, ['gorby%kremvax@example.com'], 'to gorby%kremvax@example.com') 711 | 712 | components = fastURI.parse('mailto:unlikely%3Faddress@example.com?blat=foop') 713 | t.deepEqual(components.to, ['unlikely?address@example.com'], 'to unlikely?address@example.com') 714 | t.deepEqual(components.headers, { blat: 'foop' }, 'headers') 715 | 716 | components = fastURI.parse('mailto:Mike%26family@example.org') 717 | t.deepEqual(components.to, ['Mike&family@example.org'], 'to Mike&family@example.org') 718 | 719 | components = fastURI.parse('mailto:%22not%40me%22@example.org') 720 | t.deepEqual(components.to, ['"not@me"@example.org'], 'to ' + '"not@me"@example.org') 721 | 722 | components = fastURI.parse('mailto:%22oh%5C%5Cno%22@example.org') 723 | t.deepEqual(components.to, ['"oh\\\\no"@example.org'], 'to ' + '"oh\\\\no"@example.org') 724 | 725 | components = fastURI.parse("mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org") 726 | t.deepEqual(components.to, ['"\\\\\\"it\'s\\ ugly\\\\\\""@example.org'], 'to ' + '"\\\\\\"it\'s\\ ugly\\\\\\""@example.org') 727 | 728 | components = fastURI.parse('mailto:user@example.org?subject=caf%C3%A9') 729 | t.deepEqual(components.to, ['user@example.org'], 'to') 730 | t.equal(components.subject, 'caf\xE9', 'subject') 731 | 732 | components = fastURI.parse('mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D') 733 | t.deepEqual(components.to, ['user@example.org'], 'to') 734 | t.equal(components.subject, '=?utf-8?Q?caf=C3=A9?=', 'subject') // TODO: Verify this 735 | 736 | components = fastURI.parse('mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D') 737 | t.deepEqual(components.to, ['user@example.org'], 'to') 738 | t.equal(components.subject, '=?iso-8859-1?Q?caf=E9?=', 'subject') // TODO: Verify this 739 | 740 | components = fastURI.parse('mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9') 741 | t.deepEqual(components.to, ['user@example.org'], 'to') 742 | t.equal(components.subject, 'caf\xE9', 'subject') 743 | t.equal(components.body, 'caf\xE9', 'body') 744 | 745 | if (fastURI.IRI_SUPPORT) { 746 | components = fastURI.parse('mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO') 747 | t.deepEqual(components.to, ['user@xn--99zt52a.example.org'], 'to') 748 | t.equal(components.subject, 'Test', 'subject') 749 | t.equal(components.body, 'NATTO', 'body') 750 | } 751 | 752 | t.end() 753 | }) 754 | 755 | test('Mailto Serialize', (t) => { 756 | // tests from RFC 6068 757 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['chris@example.com'] }), 'mailto:chris@example.com') 758 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['infobot@example.com'], body: 'current-issue' }), 'mailto:infobot@example.com?body=current-issue') 759 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['infobot@example.com'], body: 'send current-issue' }), 'mailto:infobot@example.com?body=send%20current-issue') 760 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['infobot@example.com'], body: 'send current-issue\x0D\x0Asend index' }), 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index') 761 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['list@example.org'], headers: { 'In-Reply-To': '<3469A91.D10AF4C@example.com>' } }), 'mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E') 762 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['majordomo@example.com'], body: 'subscribe bamboo-l' }), 'mailto:majordomo@example.com?body=subscribe%20bamboo-l') 763 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['joe@example.com'], headers: { cc: 'bob@example.com', body: 'hello' } }), 'mailto:joe@example.com?cc=bob@example.com&body=hello') 764 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['gorby%25kremvax@example.com'] }), 'mailto:gorby%25kremvax@example.com') 765 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['unlikely%3Faddress@example.com'], headers: { blat: 'foop' } }), 'mailto:unlikely%3Faddress@example.com?blat=foop') 766 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['Mike&family@example.org'] }), 'mailto:Mike%26family@example.org') 767 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['"not@me"@example.org'] }), 'mailto:%22not%40me%22@example.org') 768 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['"oh\\\\no"@example.org'] }), 'mailto:%22oh%5C%5Cno%22@example.org') 769 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['"\\\\\\"it\'s\\ ugly\\\\\\""@example.org'] }), "mailto:%22%5C%5C%5C%22it's%5C%20ugly%5C%5C%5C%22%22@example.org") 770 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: 'caf\xE9' }), 'mailto:user@example.org?subject=caf%C3%A9') 771 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: '=?utf-8?Q?caf=C3=A9?=' }), 'mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D') 772 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: '=?iso-8859-1?Q?caf=E9?=' }), 'mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D') 773 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['user@example.org'], subject: 'caf\xE9', body: 'caf\xE9' }), 'mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9') 774 | if (fastURI.IRI_SUPPORT) { 775 | t.equal(fastURI.serialize({ scheme: 'mailto', to: ['us\xE9r@\u7d0d\u8c46.example.org'], subject: 'Test', body: 'NATTO' }), 'mailto:us%C3%A9r@xn--99zt52a.example.org?subject=Test&body=NATTO') 776 | } 777 | t.end() 778 | }) 779 | 780 | test('Mailto Equals', (t) => { 781 | // tests from RFC 6068 782 | t.equal(fastURI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true) 783 | t.equal(fastURI.equal('mailto:?to=addr1@an.example,addr2@an.example', 'mailto:addr1@an.example?to=addr2@an.example'), true) 784 | t.end() 785 | }) 786 | } 787 | 788 | if (fastURI.SCHEMES.ws) { 789 | test('WS Parse', (t) => { 790 | let components 791 | 792 | // example from RFC 6455, Sec 4.1 793 | components = fastURI.parse('ws://example.com/chat') 794 | t.equal(components.error, undefined, 'error') 795 | t.equal(components.scheme, 'ws', 'scheme') 796 | t.equal(components.userinfo, undefined, 'userinfo') 797 | t.equal(components.host, 'example.com', 'host') 798 | t.equal(components.port, undefined, 'port') 799 | t.equal(components.path, undefined, 'path') 800 | t.equal(components.query, undefined, 'query') 801 | t.equal(components.fragment, undefined, 'fragment') 802 | t.equal(components.resourceName, '/chat', 'resourceName') 803 | t.equal(components.secure, false, 'secure') 804 | 805 | components = fastURI.parse('ws://example.com/foo?bar=baz') 806 | t.equal(components.error, undefined, 'error') 807 | t.equal(components.scheme, 'ws', 'scheme') 808 | t.equal(components.userinfo, undefined, 'userinfo') 809 | t.equal(components.host, 'example.com', 'host') 810 | t.equal(components.port, undefined, 'port') 811 | t.equal(components.path, undefined, 'path') 812 | t.equal(components.query, undefined, 'query') 813 | t.equal(components.fragment, undefined, 'fragment') 814 | t.equal(components.resourceName, '/foo?bar=baz', 'resourceName') 815 | t.equal(components.secure, false, 'secure') 816 | 817 | components = fastURI.parse('ws://example.com/?bar=baz') 818 | t.equal(components.resourceName, '/?bar=baz', 'resourceName') 819 | 820 | t.end() 821 | }) 822 | 823 | test('WS Serialize', (t) => { 824 | t.equal(fastURI.serialize({ scheme: 'ws' }), 'ws:') 825 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com' }), 'ws://example.com') 826 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/' }), 'ws:') 827 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/foo' }), 'ws:/foo') 828 | t.equal(fastURI.serialize({ scheme: 'ws', resourceName: '/foo?bar' }), 'ws:/foo?bar') 829 | t.equal(fastURI.serialize({ scheme: 'ws', secure: false }), 'ws:') 830 | t.equal(fastURI.serialize({ scheme: 'ws', secure: true }), 'wss:') 831 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo' }), 'ws://example.com/foo') 832 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar' }), 'ws://example.com/foo?bar') 833 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', secure: false }), 'ws://example.com') 834 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', secure: true }), 'wss://example.com') 835 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar') 836 | t.equal(fastURI.serialize({ scheme: 'ws', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar') 837 | t.end() 838 | }) 839 | 840 | test('WS Equal', (t) => { 841 | t.equal(fastURI.equal('WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'), true) 842 | t.end() 843 | }) 844 | 845 | test('WS Normalize', (t) => { 846 | t.equal(fastURI.normalize('ws://example.com:80/foo#hash'), 'ws://example.com/foo') 847 | t.end() 848 | }) 849 | } 850 | 851 | if (fastURI.SCHEMES.wss) { 852 | test('WSS Parse', (t) => { 853 | let components 854 | 855 | // example from RFC 6455, Sec 4.1 856 | components = fastURI.parse('wss://example.com/chat') 857 | t.equal(components.error, undefined, 'error') 858 | t.equal(components.scheme, 'wss', 'scheme') 859 | t.equal(components.userinfo, undefined, 'userinfo') 860 | t.equal(components.host, 'example.com', 'host') 861 | t.equal(components.port, undefined, 'port') 862 | t.equal(components.path, undefined, 'path') 863 | t.equal(components.query, undefined, 'query') 864 | t.equal(components.fragment, undefined, 'fragment') 865 | t.equal(components.resourceName, '/chat', 'resourceName') 866 | t.equal(components.secure, true, 'secure') 867 | 868 | components = fastURI.parse('wss://example.com/foo?bar=baz') 869 | t.equal(components.error, undefined, 'error') 870 | t.equal(components.scheme, 'wss', 'scheme') 871 | t.equal(components.userinfo, undefined, 'userinfo') 872 | t.equal(components.host, 'example.com', 'host') 873 | t.equal(components.port, undefined, 'port') 874 | t.equal(components.path, undefined, 'path') 875 | t.equal(components.query, undefined, 'query') 876 | t.equal(components.fragment, undefined, 'fragment') 877 | t.equal(components.resourceName, '/foo?bar=baz', 'resourceName') 878 | t.equal(components.secure, true, 'secure') 879 | 880 | components = fastURI.parse('wss://example.com/?bar=baz') 881 | t.equal(components.resourceName, '/?bar=baz', 'resourceName') 882 | 883 | t.end() 884 | }) 885 | 886 | test('WSS Serialize', (t) => { 887 | t.equal(fastURI.serialize({ scheme: 'wss' }), 'wss:') 888 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com' }), 'wss://example.com') 889 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/' }), 'wss:') 890 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/foo' }), 'wss:/foo') 891 | t.equal(fastURI.serialize({ scheme: 'wss', resourceName: '/foo?bar' }), 'wss:/foo?bar') 892 | t.equal(fastURI.serialize({ scheme: 'wss', secure: false }), 'ws:') 893 | t.equal(fastURI.serialize({ scheme: 'wss', secure: true }), 'wss:') 894 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo' }), 'wss://example.com/foo') 895 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar' }), 'wss://example.com/foo?bar') 896 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', secure: false }), 'ws://example.com') 897 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', secure: true }), 'wss://example.com') 898 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: false }), 'ws://example.com/foo?bar') 899 | t.equal(fastURI.serialize({ scheme: 'wss', host: 'example.com', resourceName: '/foo?bar', secure: true }), 'wss://example.com/foo?bar') 900 | t.end() 901 | }) 902 | 903 | test('WSS Equal', (t) => { 904 | t.equal(fastURI.equal('WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'), true) 905 | t.end() 906 | }) 907 | 908 | test('WSS Normalize', (t) => { 909 | t.equal(fastURI.normalize('wss://example.com:443/foo#hash'), 'wss://example.com/foo') 910 | t.end() 911 | }) 912 | } 913 | --------------------------------------------------------------------------------