An RFC 3986/3987 compliant, scheme extendable URI/IRI parsing/normalizing/resolving/serializing library for JavaScript.
npx @tessl/cli install tessl/npm-uri-js@4.4.0URI.js is an RFC 3986/3987 compliant, scheme extendable URI parsing, normalizing, resolving, and serializing library for JavaScript environments. It provides comprehensive support for URI and IRI handling with built-in scheme support, IPv4/IPv6 address normalization, and internationalized domain name (IDN) processing.
npm install uri-jsimport * as URI from "uri-js";For named imports:
import {
parse,
serialize,
resolve,
normalize,
equal,
escapeComponent,
unescapeComponent,
removeDotSegments,
resolveComponents,
pctEncChar,
pctDecChars,
SCHEMES,
URIComponents,
URIOptions,
URISchemeHandler,
URIRegExps
} from "uri-js";For CommonJS:
const URI = require("uri-js");
// or
const {
parse,
serialize,
resolve,
normalize,
equal,
escapeComponent,
unescapeComponent,
removeDotSegments,
resolveComponents,
pctEncChar,
pctDecChars,
SCHEMES
} = require("uri-js");import { parse, serialize, resolve, normalize, equal } from "uri-js";
// Parse a URI into components
const components = parse("http://user:pass@example.com:8080/path?query=value#fragment");
console.log(components);
// {
// scheme: "http",
// userinfo: "user:pass",
// host: "example.com",
// port: 8080,
// path: "/path",
// query: "query=value",
// fragment: "fragment"
// }
// Serialize components back to URI string
const uri = serialize({
scheme: "https",
host: "example.com",
path: "/api/data",
query: "format=json"
});
console.log(uri); // "https://example.com/api/data?format=json"
// Resolve relative URIs
const resolved = resolve("http://example.com/a/b/c", "../d/e");
console.log(resolved); // "http://example.com/a/d/e"
// Normalize URIs
const normalized = normalize("HTTP://EXAMPLE.COM:80/%7Efoo");
console.log(normalized); // "http://example.com/~foo"
// Compare URIs for equality
const isEqual = equal("http://example.com/", "HTTP://EXAMPLE.COM:80/");
console.log(isEqual); // trueURI.js is built around several key components:
Essential URI parsing, serialization, and manipulation functions for standard URI processing workflows.
function parse(uriString: string, options?: URIOptions): URIComponents;
function serialize(components: URIComponents, options?: URIOptions): string;
function resolve(baseURI: string, relativeURI: string, options?: URIOptions): string;
function normalize(uri: string, options?: URIOptions): string;
function normalize(uri: URIComponents, options?: URIOptions): URIComponents;
function equal(uriA: string, uriB: string, options?: URIOptions): boolean;
function equal(uriA: URIComponents, uriB: URIComponents, options?: URIOptions): boolean;Advanced component-level URI manipulation including encoding, escaping, and path segment processing.
function escapeComponent(str: string, options?: URIOptions): string;
function unescapeComponent(str: string, options?: URIOptions): string;
function removeDotSegments(input: string): string;
function resolveComponents(base: URIComponents, relative: URIComponents, options?: URIOptions, skipNormalization?: boolean): URIComponents;
function pctEncChar(chr: string): string;
function pctDecChars(str: string): string;Built-in handlers for common URI schemes with protocol-specific parsing and serialization logic.
interface URISchemeHandler<Components extends URIComponents = URIComponents, Options extends URIOptions = URIOptions, ParentComponents extends URIComponents = URIComponents> {
scheme: string;
parse(components: ParentComponents, options: Options): Components;
serialize(components: Components, options: Options): ParentComponents;
unicodeSupport?: boolean;
domainHost?: boolean;
absolutePath?: boolean;
}
const SCHEMES: {[scheme: string]: URISchemeHandler};Built-in scheme support includes: HTTP, HTTPS, WebSocket (WS/WSS), mailto, URN, and URN UUID.
interface URIComponents {
scheme?: string;
userinfo?: string;
host?: string;
port?: number | string;
path?: string;
query?: string;
fragment?: string;
reference?: string;
error?: string;
}
interface URIOptions {
scheme?: string;
reference?: string;
tolerant?: boolean;
absolutePath?: boolean;
iri?: boolean;
unicodeSupport?: boolean;
domainHost?: boolean;
}
interface URIRegExps {
NOT_SCHEME: RegExp;
NOT_USERINFO: RegExp;
NOT_HOST: RegExp;
NOT_PATH: RegExp;
NOT_PATH_NOSCHEME: RegExp;
NOT_QUERY: RegExp;
NOT_FRAGMENT: RegExp;
ESCAPE: RegExp;
UNRESERVED: RegExp;
OTHER_CHARS: RegExp;
PCT_ENCODED: RegExp;
IPV4ADDRESS: RegExp;
IPV6ADDRESS: RegExp;
}These interfaces extend the base URIComponents for scheme-specific parsing and serialization. They are used internally by scheme handlers and returned by parsing functions when processing specific URI schemes.
interface WSComponents extends URIComponents {
resourceName?: string;
secure?: boolean;
}
interface MailtoHeaders {
[hfname: string]: string;
}
interface MailtoComponents extends URIComponents {
to: Array<string>;
headers?: MailtoHeaders;
subject?: string;
body?: string;
}
interface URNComponents extends URIComponents {
nid?: string;
nss?: string;
}
interface URNOptions extends URIOptions {
nid?: string;
}
interface UUIDComponents extends URNComponents {
uuid?: string;
}