2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-05-04 14:32:54 -04:00
|
|
|
import { build } from "../build.ts";
|
|
|
|
import { getRandomValues } from "../ops/get_random_values.ts";
|
2020-07-10 15:51:24 -04:00
|
|
|
import { domainToAscii } from "../ops/idna.ts";
|
2020-03-28 13:03:49 -04:00
|
|
|
import { customInspect } from "./console.ts";
|
2020-07-10 15:51:24 -04:00
|
|
|
import { TextEncoder } from "./text_encoding.ts";
|
2020-04-11 17:19:36 -04:00
|
|
|
import { urls } from "./url_search_params.ts";
|
2018-12-17 20:07:47 -05:00
|
|
|
|
|
|
|
interface URLParts {
|
|
|
|
protocol: string;
|
2020-07-10 15:51:24 -04:00
|
|
|
slashes: string;
|
2018-12-17 20:07:47 -05:00
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
hostname: string;
|
|
|
|
port: string;
|
2019-01-20 21:31:13 -05:00
|
|
|
path: string;
|
2020-05-18 09:47:45 -04:00
|
|
|
query: string;
|
2018-12-17 20:07:47 -05:00
|
|
|
hash: string;
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
const searchParamsMethods: Array<keyof URLSearchParams> = [
|
2018-12-17 20:07:47 -05:00
|
|
|
"append",
|
|
|
|
"delete",
|
2020-03-28 13:03:49 -04:00
|
|
|
"set",
|
2018-12-17 20:07:47 -05:00
|
|
|
];
|
|
|
|
|
2020-05-04 14:32:54 -04:00
|
|
|
const specialSchemes = ["ftp", "file", "http", "https", "ws", "wss"];
|
|
|
|
|
2020-04-28 01:23:06 -04:00
|
|
|
// https://url.spec.whatwg.org/#special-scheme
|
2020-07-06 21:45:39 -04:00
|
|
|
const schemePorts: Record<string, string> = {
|
2020-04-28 01:23:06 -04:00
|
|
|
ftp: "21",
|
|
|
|
file: "",
|
|
|
|
http: "80",
|
|
|
|
https: "443",
|
|
|
|
ws: "80",
|
|
|
|
wss: "443",
|
|
|
|
};
|
|
|
|
const MAX_PORT = 2 ** 16 - 1;
|
|
|
|
|
2020-05-04 14:32:54 -04:00
|
|
|
// Remove the part of the string that matches the pattern and return the
|
|
|
|
// remainder (RHS) as well as the first captured group of the matched substring
|
|
|
|
// (LHS). e.g.
|
|
|
|
// takePattern("https://deno.land:80", /^([a-z]+):[/]{2}/)
|
|
|
|
// = ["http", "deno.land:80"]
|
2020-06-10 14:05:10 -04:00
|
|
|
// takePattern("deno.land:80", /^(\[[0-9a-fA-F.:]{2,}\]|[^:]+)/)
|
2020-05-04 14:32:54 -04:00
|
|
|
// = ["deno.land", "80"]
|
|
|
|
function takePattern(string: string, pattern: RegExp): [string, string] {
|
|
|
|
let capture = "";
|
|
|
|
const rest = string.replace(pattern, (_, capture_) => {
|
|
|
|
capture = capture_;
|
|
|
|
return "";
|
|
|
|
});
|
|
|
|
return [capture, rest];
|
|
|
|
}
|
|
|
|
|
|
|
|
function parse(url: string, isBase = true): URLParts | undefined {
|
|
|
|
const parts: Partial<URLParts> = {};
|
|
|
|
let restUrl;
|
2020-05-18 09:47:45 -04:00
|
|
|
[parts.protocol, restUrl] = takePattern(url.trim(), /^([a-z]+):/);
|
2020-05-04 14:32:54 -04:00
|
|
|
if (isBase && parts.protocol == "") {
|
|
|
|
return undefined;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
2020-07-10 15:51:24 -04:00
|
|
|
const isSpecial = specialSchemes.includes(parts.protocol);
|
2020-05-04 14:32:54 -04:00
|
|
|
if (parts.protocol == "file") {
|
2020-07-10 15:51:24 -04:00
|
|
|
parts.slashes = "//";
|
2020-05-04 14:32:54 -04:00
|
|
|
parts.username = "";
|
|
|
|
parts.password = "";
|
|
|
|
[parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2}([^/\\?#]*)/);
|
|
|
|
parts.port = "";
|
2020-06-26 08:34:17 -04:00
|
|
|
if (build.os == "windows" && parts.hostname == "") {
|
|
|
|
// UNC paths. e.g. "\\\\localhost\\foo\\bar" on Windows should be
|
|
|
|
// representable as `new URL("file:////localhost/foo/bar")` which is
|
|
|
|
// equivalent to: `new URL("file://localhost/foo/bar")`.
|
|
|
|
[parts.hostname, restUrl] = takePattern(restUrl, /^[/\\]{2,}([^/\\?#]*)/);
|
|
|
|
}
|
2020-07-10 15:51:24 -04:00
|
|
|
} else if (isSpecial) {
|
|
|
|
parts.slashes = "//";
|
2020-05-04 14:32:54 -04:00
|
|
|
let restAuthority;
|
2020-06-26 08:34:17 -04:00
|
|
|
[restAuthority, restUrl] = takePattern(restUrl, /^[/\\]{2,}([^/\\?#]+)/);
|
2020-05-04 14:32:54 -04:00
|
|
|
if (isBase && restAuthority == "") {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
let restAuthentication;
|
|
|
|
[restAuthentication, restAuthority] = takePattern(restAuthority, /^(.*)@/);
|
|
|
|
[parts.username, restAuthentication] = takePattern(
|
|
|
|
restAuthentication,
|
|
|
|
/^([^:]*)/
|
|
|
|
);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.username = encodeUserinfo(parts.username);
|
2020-05-04 14:32:54 -04:00
|
|
|
[parts.password] = takePattern(restAuthentication, /^:(.*)/);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.password = encodeUserinfo(parts.password);
|
2020-06-10 14:05:10 -04:00
|
|
|
[parts.hostname, restAuthority] = takePattern(
|
|
|
|
restAuthority,
|
|
|
|
/^(\[[0-9a-fA-F.:]{2,}\]|[^:]+)/
|
|
|
|
);
|
2020-05-04 14:32:54 -04:00
|
|
|
[parts.port] = takePattern(restAuthority, /^:(.*)/);
|
|
|
|
if (!isValidPort(parts.port)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-10 15:51:24 -04:00
|
|
|
[parts.slashes, restUrl] = takePattern(restUrl, /^([/\\]{2})/);
|
2020-05-04 14:32:54 -04:00
|
|
|
parts.username = "";
|
|
|
|
parts.password = "";
|
2020-07-10 15:51:24 -04:00
|
|
|
if (parts.slashes) {
|
|
|
|
[parts.hostname, restUrl] = takePattern(restUrl, /^([^/\\?#]*)/);
|
|
|
|
} else {
|
|
|
|
parts.hostname = "";
|
|
|
|
}
|
2020-05-04 14:32:54 -04:00
|
|
|
parts.port = "";
|
|
|
|
}
|
2020-05-18 09:47:45 -04:00
|
|
|
try {
|
2020-07-10 15:51:24 -04:00
|
|
|
parts.hostname = encodeHostname(parts.hostname, isSpecial);
|
2020-05-18 09:47:45 -04:00
|
|
|
} catch {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-05-04 14:32:54 -04:00
|
|
|
[parts.path, restUrl] = takePattern(restUrl, /^([^?#]*)/);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.path = encodePathname(parts.path.replace(/\\/g, "/"));
|
2020-05-04 14:32:54 -04:00
|
|
|
[parts.query, restUrl] = takePattern(restUrl, /^(\?[^#]*)/);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.query = encodeSearch(parts.query);
|
2020-05-04 14:32:54 -04:00
|
|
|
[parts.hash] = takePattern(restUrl, /^(#.*)/);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.hash = encodeHash(parts.hash);
|
2020-05-04 14:32:54 -04:00
|
|
|
return parts as URLParts;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
2019-08-06 09:22:11 -04:00
|
|
|
// Based on https://github.com/kelektiv/node-uuid
|
|
|
|
// TODO(kevinkassimo): Use deno_std version once possible.
|
|
|
|
function generateUUID(): string {
|
2019-11-13 13:42:34 -05:00
|
|
|
return "00000000-0000-4000-8000-000000000000".replace(/[0]/g, (): string =>
|
|
|
|
// random integer from 0 to 15 as a hex digit.
|
|
|
|
(getRandomValues(new Uint8Array(1))[0] % 16).toString(16)
|
2019-08-06 09:22:11 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep it outside of URL to avoid any attempts of access.
|
2020-04-11 16:25:31 -04:00
|
|
|
export const blobURLMap = new Map<string, Blob>();
|
2019-08-06 09:22:11 -04:00
|
|
|
|
2019-09-05 20:01:27 -04:00
|
|
|
function isAbsolutePath(path: string): boolean {
|
|
|
|
return path.startsWith("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolves `.`s and `..`s where possible.
|
|
|
|
// Preserves repeating and trailing `/`s by design.
|
2020-05-04 14:32:54 -04:00
|
|
|
// On Windows, drive letter paths will be given a leading slash, and also a
|
|
|
|
// trailing slash if there are no other components e.g. "C:" -> "/C:/".
|
|
|
|
function normalizePath(path: string, isFilePath = false): string {
|
|
|
|
if (build.os == "windows" && isFilePath) {
|
|
|
|
path = path.replace(/^\/*([A-Za-z]:)(\/|$)/, "/$1/");
|
|
|
|
}
|
2019-09-05 20:01:27 -04:00
|
|
|
const isAbsolute = isAbsolutePath(path);
|
|
|
|
path = path.replace(/^\//, "");
|
|
|
|
const pathSegments = path.split("/");
|
|
|
|
|
|
|
|
const newPathSegments: string[] = [];
|
|
|
|
for (let i = 0; i < pathSegments.length; i++) {
|
|
|
|
const previous = newPathSegments[newPathSegments.length - 1];
|
|
|
|
if (
|
|
|
|
pathSegments[i] == ".." &&
|
|
|
|
previous != ".." &&
|
|
|
|
(previous != undefined || isAbsolute)
|
|
|
|
) {
|
|
|
|
newPathSegments.pop();
|
|
|
|
} else if (pathSegments[i] != ".") {
|
|
|
|
newPathSegments.push(pathSegments[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let newPath = newPathSegments.join("/");
|
|
|
|
if (!isAbsolute) {
|
|
|
|
if (newPathSegments.length == 0) {
|
|
|
|
newPath = ".";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newPath = `/${newPath}`;
|
|
|
|
}
|
|
|
|
return newPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standard URL basing logic, applied to paths.
|
2020-05-04 14:32:54 -04:00
|
|
|
function resolvePathFromBase(
|
|
|
|
path: string,
|
|
|
|
basePath: string,
|
|
|
|
isFilePath = false
|
|
|
|
): string {
|
|
|
|
let normalizedPath = normalizePath(path, isFilePath);
|
|
|
|
let normalizedBasePath = normalizePath(basePath, isFilePath);
|
|
|
|
|
|
|
|
let driveLetterPrefix = "";
|
|
|
|
if (build.os == "windows" && isFilePath) {
|
2020-07-06 21:45:39 -04:00
|
|
|
let driveLetter: string;
|
|
|
|
let baseDriveLetter: string;
|
2020-05-04 14:32:54 -04:00
|
|
|
[driveLetter, normalizedPath] = takePattern(
|
|
|
|
normalizedPath,
|
|
|
|
/^(\/[A-Za-z]:)(?=\/)/
|
|
|
|
);
|
|
|
|
[baseDriveLetter, normalizedBasePath] = takePattern(
|
|
|
|
normalizedBasePath,
|
|
|
|
/^(\/[A-Za-z]:)(?=\/)/
|
|
|
|
);
|
|
|
|
driveLetterPrefix = driveLetter || baseDriveLetter;
|
|
|
|
}
|
|
|
|
|
2019-09-05 20:01:27 -04:00
|
|
|
if (isAbsolutePath(normalizedPath)) {
|
2020-05-04 14:32:54 -04:00
|
|
|
return `${driveLetterPrefix}${normalizedPath}`;
|
2019-09-05 20:01:27 -04:00
|
|
|
}
|
|
|
|
if (!isAbsolutePath(normalizedBasePath)) {
|
|
|
|
throw new TypeError("Base path must be absolute.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case.
|
|
|
|
if (path == "") {
|
2020-05-04 14:32:54 -04:00
|
|
|
return `${driveLetterPrefix}${normalizedBasePath}`;
|
2019-09-05 20:01:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove everything after the last `/` in `normalizedBasePath`.
|
|
|
|
const prefix = normalizedBasePath.replace(/[^\/]*$/, "");
|
2020-05-04 14:32:54 -04:00
|
|
|
// If `normalizedPath` ends with `.` or `..`, add a trailing slash.
|
2019-09-05 20:01:27 -04:00
|
|
|
const suffix = normalizedPath.replace(/(?<=(^|\/)(\.|\.\.))$/, "/");
|
|
|
|
|
2020-05-04 14:32:54 -04:00
|
|
|
return `${driveLetterPrefix}${normalizePath(prefix + suffix)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isValidPort(value: string): boolean {
|
|
|
|
// https://url.spec.whatwg.org/#port-state
|
2020-07-06 21:45:39 -04:00
|
|
|
if (value === "") return true;
|
|
|
|
|
2020-05-04 14:32:54 -04:00
|
|
|
const port = Number(value);
|
|
|
|
return Number.isInteger(port) && port >= 0 && port <= MAX_PORT;
|
2019-09-05 20:01:27 -04:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
/** @internal */
|
|
|
|
export const parts = new WeakMap<URL, URLParts>();
|
|
|
|
|
2020-04-11 17:19:36 -04:00
|
|
|
export class URLImpl implements URL {
|
2020-03-28 13:03:49 -04:00
|
|
|
#searchParams!: URLSearchParams;
|
2018-12-17 20:07:47 -05:00
|
|
|
|
2019-10-31 14:55:54 -04:00
|
|
|
[customInspect](): string {
|
|
|
|
const keys = [
|
|
|
|
"href",
|
|
|
|
"origin",
|
|
|
|
"protocol",
|
|
|
|
"username",
|
|
|
|
"password",
|
|
|
|
"host",
|
|
|
|
"hostname",
|
|
|
|
"port",
|
|
|
|
"pathname",
|
|
|
|
"hash",
|
2020-03-28 13:03:49 -04:00
|
|
|
"search",
|
2019-10-31 14:55:54 -04:00
|
|
|
];
|
|
|
|
const objectString = keys
|
2019-11-14 15:05:36 -05:00
|
|
|
.map((key: string) => `${key}: "${this[key as keyof this] || ""}"`)
|
2019-10-31 14:55:54 -04:00
|
|
|
.join(", ");
|
|
|
|
return `URL { ${objectString} }`;
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
#updateSearchParams = (): void => {
|
|
|
|
const searchParams = new URLSearchParams(this.search);
|
2018-12-17 20:07:47 -05:00
|
|
|
|
|
|
|
for (const methodName of searchParamsMethods) {
|
2019-06-01 11:13:36 -04:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2018-12-17 20:07:47 -05:00
|
|
|
const method: (...args: any[]) => any = searchParams[methodName];
|
2019-06-01 11:13:36 -04:00
|
|
|
searchParams[methodName] = (...args: unknown[]): any => {
|
2018-12-17 20:07:47 -05:00
|
|
|
method.apply(searchParams, args);
|
|
|
|
this.search = searchParams.toString();
|
|
|
|
};
|
2019-06-01 11:13:36 -04:00
|
|
|
/* eslint-enable */
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#searchParams = searchParams;
|
2019-06-10 23:55:38 -04:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
urls.set(searchParams, this);
|
|
|
|
};
|
2018-12-17 20:07:47 -05:00
|
|
|
|
|
|
|
get hash(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return parts.get(this)!.hash;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set hash(value: string) {
|
|
|
|
value = unescape(String(value));
|
|
|
|
if (!value) {
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.get(this)!.hash = "";
|
2018-12-17 20:07:47 -05:00
|
|
|
} else {
|
|
|
|
if (value.charAt(0) !== "#") {
|
|
|
|
value = `#${value}`;
|
|
|
|
}
|
|
|
|
// hashes can contain % and # unescaped
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.get(this)!.hash = encodeHash(value);
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get host(): string {
|
|
|
|
return `${this.hostname}${this.port ? `:${this.port}` : ""}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
set host(value: string) {
|
|
|
|
value = String(value);
|
|
|
|
const url = new URL(`http://${value}`);
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.get(this)!.hostname = url.hostname;
|
|
|
|
parts.get(this)!.port = url.port;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get hostname(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return parts.get(this)!.hostname;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set hostname(value: string) {
|
|
|
|
value = String(value);
|
2020-05-18 09:47:45 -04:00
|
|
|
try {
|
2020-07-10 15:51:24 -04:00
|
|
|
const isSpecial = specialSchemes.includes(parts.get(this)!.protocol);
|
|
|
|
parts.get(this)!.hostname = encodeHostname(value, isSpecial);
|
2020-05-18 09:47:45 -04:00
|
|
|
} catch {}
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get href(): string {
|
|
|
|
const authentication =
|
|
|
|
this.username || this.password
|
|
|
|
? `${this.username}${this.password ? ":" + this.password : ""}@`
|
|
|
|
: "";
|
2020-07-10 15:51:24 -04:00
|
|
|
return `${this.protocol}${parts.get(this)!.slashes}${authentication}${
|
|
|
|
this.host
|
|
|
|
}${this.pathname}${this.search}${this.hash}`;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set href(value: string) {
|
|
|
|
value = String(value);
|
|
|
|
if (value !== this.href) {
|
|
|
|
const url = new URL(value);
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.set(this, { ...parts.get(url)! });
|
|
|
|
this.#updateSearchParams();
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get origin(): string {
|
2019-11-12 13:45:48 -05:00
|
|
|
if (this.host) {
|
|
|
|
return `${this.protocol}//${this.host}`;
|
|
|
|
}
|
|
|
|
return "null";
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get password(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return parts.get(this)!.password;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set password(value: string) {
|
|
|
|
value = String(value);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.get(this)!.password = encodeUserinfo(value);
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get pathname(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return parts.get(this)?.path || "/";
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set pathname(value: string) {
|
|
|
|
value = unescape(String(value));
|
|
|
|
if (!value || value.charAt(0) !== "/") {
|
|
|
|
value = `/${value}`;
|
|
|
|
}
|
2019-01-20 21:31:13 -05:00
|
|
|
// paths can contain % unescaped
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.get(this)!.path = encodePathname(value);
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get port(): string {
|
2020-04-28 01:23:06 -04:00
|
|
|
const port = parts.get(this)!.port;
|
|
|
|
if (schemePorts[parts.get(this)!.protocol] === port) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return port;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set port(value: string) {
|
2020-05-04 14:32:54 -04:00
|
|
|
if (!isValidPort(value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parts.get(this)!.port = value.toString();
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get protocol(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return `${parts.get(this)!.protocol}:`;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set protocol(value: string) {
|
|
|
|
value = String(value);
|
|
|
|
if (value) {
|
|
|
|
if (value.charAt(value.length - 1) === ":") {
|
|
|
|
value = value.slice(0, -1);
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.get(this)!.protocol = encodeURIComponent(value);
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get search(): string {
|
2020-05-18 09:47:45 -04:00
|
|
|
return parts.get(this)!.query;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set search(value: string) {
|
|
|
|
value = String(value);
|
2020-05-18 09:47:45 -04:00
|
|
|
const query = value == "" || value.charAt(0) == "?" ? value : `?${value}`;
|
|
|
|
parts.get(this)!.query = encodeSearch(query);
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#updateSearchParams();
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get username(): string {
|
2020-03-28 13:03:49 -04:00
|
|
|
return parts.get(this)!.username;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
set username(value: string) {
|
|
|
|
value = String(value);
|
2020-05-18 09:47:45 -04:00
|
|
|
parts.get(this)!.username = encodeUserinfo(value);
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
get searchParams(): URLSearchParams {
|
|
|
|
return this.#searchParams;
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
2020-04-29 16:20:55 -04:00
|
|
|
constructor(url: string | URL, base?: string | URL) {
|
2018-12-17 20:07:47 -05:00
|
|
|
let baseParts: URLParts | undefined;
|
|
|
|
if (base) {
|
2020-03-28 13:03:49 -04:00
|
|
|
baseParts = typeof base === "string" ? parse(base) : parts.get(base);
|
2020-07-06 21:45:39 -04:00
|
|
|
if (baseParts === undefined) {
|
2018-12-17 20:07:47 -05:00
|
|
|
throw new TypeError("Invalid base URL.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 14:32:54 -04:00
|
|
|
const urlParts =
|
|
|
|
typeof url === "string" ? parse(url, !baseParts) : parts.get(url);
|
|
|
|
if (urlParts == undefined) {
|
2020-04-28 01:23:06 -04:00
|
|
|
throw new TypeError("Invalid URL.");
|
|
|
|
}
|
|
|
|
|
2018-12-17 20:07:47 -05:00
|
|
|
if (urlParts.protocol) {
|
2020-05-04 14:32:54 -04:00
|
|
|
urlParts.path = normalizePath(urlParts.path, urlParts.protocol == "file");
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.set(this, urlParts);
|
2018-12-17 20:07:47 -05:00
|
|
|
} else if (baseParts) {
|
2020-03-28 13:03:49 -04:00
|
|
|
parts.set(this, {
|
2019-01-20 21:31:13 -05:00
|
|
|
protocol: baseParts.protocol,
|
2020-07-10 15:51:24 -04:00
|
|
|
slashes: baseParts.slashes,
|
2019-01-20 21:31:13 -05:00
|
|
|
username: baseParts.username,
|
|
|
|
password: baseParts.password,
|
|
|
|
hostname: baseParts.hostname,
|
|
|
|
port: baseParts.port,
|
2020-05-04 14:32:54 -04:00
|
|
|
path: resolvePathFromBase(
|
|
|
|
urlParts.path,
|
|
|
|
baseParts.path || "/",
|
|
|
|
baseParts.protocol == "file"
|
|
|
|
),
|
2019-09-05 20:01:27 -04:00
|
|
|
query: urlParts.query,
|
2020-03-28 13:03:49 -04:00
|
|
|
hash: urlParts.hash,
|
|
|
|
});
|
2018-12-17 20:07:47 -05:00
|
|
|
} else {
|
2020-05-04 10:06:47 -04:00
|
|
|
throw new TypeError("Invalid URL.");
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
2020-04-28 01:23:06 -04:00
|
|
|
|
2020-03-28 13:03:49 -04:00
|
|
|
this.#updateSearchParams();
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
return this.href;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON(): string {
|
|
|
|
return this.href;
|
|
|
|
}
|
2019-08-06 09:22:11 -04:00
|
|
|
|
|
|
|
// TODO(kevinkassimo): implement MediaSource version in the future.
|
2020-04-11 16:25:31 -04:00
|
|
|
static createObjectURL(b: Blob): string {
|
2020-05-01 14:15:28 -04:00
|
|
|
const origin = "http://deno-opaque-origin";
|
2019-08-06 09:22:11 -04:00
|
|
|
const key = `blob:${origin}/${generateUUID()}`;
|
|
|
|
blobURLMap.set(key, b);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
static revokeObjectURL(url: string): void {
|
|
|
|
let urlObject;
|
|
|
|
try {
|
|
|
|
urlObject = new URL(url);
|
|
|
|
} catch {
|
|
|
|
throw new TypeError("Provided URL string is not valid");
|
|
|
|
}
|
|
|
|
if (urlObject.protocol !== "blob:") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Origin match check seems irrelevant for now, unless we implement
|
2020-01-20 09:30:30 -05:00
|
|
|
// persisten storage for per globalThis.location.origin at some point.
|
2019-08-06 09:22:11 -04:00
|
|
|
blobURLMap.delete(url);
|
|
|
|
}
|
2018-12-17 20:07:47 -05:00
|
|
|
}
|
2020-05-18 09:47:45 -04:00
|
|
|
|
|
|
|
function charInC0ControlSet(c: string): boolean {
|
2020-07-10 15:51:24 -04:00
|
|
|
return (c >= "\u0000" && c <= "\u001F") || c > "\u007E";
|
2020-05-18 09:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function charInSearchSet(c: string): boolean {
|
|
|
|
// prettier-ignore
|
|
|
|
return charInC0ControlSet(c) || ["\u0020", "\u0022", "\u0023", "\u0027", "\u003C", "\u003E"].includes(c) || c > "\u007E";
|
|
|
|
}
|
|
|
|
|
|
|
|
function charInFragmentSet(c: string): boolean {
|
|
|
|
// prettier-ignore
|
|
|
|
return charInC0ControlSet(c) || ["\u0020", "\u0022", "\u003C", "\u003E", "\u0060"].includes(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
function charInPathSet(c: string): boolean {
|
|
|
|
// prettier-ignore
|
|
|
|
return charInFragmentSet(c) || ["\u0023", "\u003F", "\u007B", "\u007D"].includes(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
function charInUserinfoSet(c: string): boolean {
|
|
|
|
// "\u0027" ("'") seemingly isn't in the spec, but matches Chrome and Firefox.
|
|
|
|
// prettier-ignore
|
|
|
|
return charInPathSet(c) || ["\u0027", "\u002F", "\u003A", "\u003B", "\u003D", "\u0040", "\u005B", "\u005C", "\u005D", "\u005E", "\u007C"].includes(c);
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:51:24 -04:00
|
|
|
function charIsForbiddenInHost(c: string): boolean {
|
|
|
|
// prettier-ignore
|
|
|
|
return ["\u0000", "\u0009", "\u000A", "\u000D", "\u0020", "\u0023", "\u0025", "\u002F", "\u003A", "\u003C", "\u003E", "\u003F", "\u0040", "\u005B", "\u005C", "\u005D", "\u005E"].includes(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
2020-05-18 09:47:45 -04:00
|
|
|
function encodeChar(c: string): string {
|
2020-07-10 15:51:24 -04:00
|
|
|
return [...encoder.encode(c)]
|
|
|
|
.map((n) => `%${n.toString(16)}`)
|
|
|
|
.join("")
|
|
|
|
.toUpperCase();
|
2020-05-18 09:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function encodeUserinfo(s: string): string {
|
|
|
|
return [...s].map((c) => (charInUserinfoSet(c) ? encodeChar(c) : c)).join("");
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:51:24 -04:00
|
|
|
function encodeHostname(s: string, isSpecial = true): string {
|
|
|
|
// IPv6 parsing.
|
|
|
|
if (s.startsWith("[") && s.endsWith("]")) {
|
|
|
|
if (!s.match(/^\[[0-9A-Fa-f.:]{2,}\]$/)) {
|
|
|
|
throw new TypeError("Invalid hostname.");
|
|
|
|
}
|
|
|
|
return s.toLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = s;
|
|
|
|
|
|
|
|
if (!isSpecial) {
|
|
|
|
// Check against forbidden host code points except for "%".
|
|
|
|
for (const c of result) {
|
|
|
|
if (charIsForbiddenInHost(c) && c != "\u0025") {
|
|
|
|
throw new TypeError("Invalid hostname.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Percent-encode C0 control set.
|
|
|
|
result = [...result]
|
|
|
|
.map((c) => (charInC0ControlSet(c) ? encodeChar(c) : c))
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Percent-decode.
|
|
|
|
if (result.match(/%(?![0-9A-Fa-f]{2})/) != null) {
|
2020-05-18 09:47:45 -04:00
|
|
|
throw new TypeError("Invalid hostname.");
|
|
|
|
}
|
2020-07-10 15:51:24 -04:00
|
|
|
result = result.replace(/%(.{2})/g, (_, hex) =>
|
|
|
|
String.fromCodePoint(Number(`0x${hex}`))
|
|
|
|
);
|
|
|
|
|
|
|
|
// IDNA domain to ASCII.
|
|
|
|
result = domainToAscii(result);
|
|
|
|
|
|
|
|
// Check against forbidden host code points.
|
|
|
|
for (const c of result) {
|
|
|
|
if (charIsForbiddenInHost(c)) {
|
|
|
|
throw new TypeError("Invalid hostname.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(nayeemrmn): IPv4 parsing.
|
|
|
|
|
|
|
|
return result;
|
2020-05-18 09:47:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function encodePathname(s: string): string {
|
|
|
|
return [...s].map((c) => (charInPathSet(c) ? encodeChar(c) : c)).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeSearch(s: string): string {
|
|
|
|
return [...s].map((c) => (charInSearchSet(c) ? encodeChar(c) : c)).join("");
|
|
|
|
}
|
|
|
|
|
|
|
|
function encodeHash(s: string): string {
|
|
|
|
return [...s].map((c) => (charInFragmentSet(c) ? encodeChar(c) : c)).join("");
|
|
|
|
}
|