mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
121 lines
5.2 KiB
JavaScript
121 lines
5.2 KiB
JavaScript
import { URL as NodeURL } from "node:url";
|
|
|
|
export function reportAuthority() {
|
|
function show(label, u) {
|
|
console.log(label, u.href, u.protocol, u.username, u.password, u.host, u.hostname, u.port, u.origin);
|
|
}
|
|
|
|
for (const base of ["https://user:pw@old.example:81/a?old=1#before", "http://old.example:443/a", "foo://Mixed.example:81/a", "foo://", "foo:/a", "file://server/C:/a", "file:///C:/a", "data:text/plain,hello"]) {
|
|
const u = new NodeURL(base);
|
|
const params = u.searchParams;
|
|
for (const value of ["new user", "a:b@c/ü\t\r\n", "%2f", ""]) {
|
|
u.username = value;
|
|
show("username", u);
|
|
}
|
|
for (const value of ["new secret", "a:b@c/ü\t\r\n", "%2f", ""]) {
|
|
u.password = value;
|
|
show("password", u);
|
|
}
|
|
for (const value of ["NEW.example", "new.example:00443junk", "new.example:", "new.example:bad", "new.example:65536", "new.example/path?ignored#ignored", "[0:0:0:0:0:0:0:1]:0082", "bad host", "user@host", "[bad]", ""]) {
|
|
u.host = value;
|
|
show("host", u);
|
|
}
|
|
for (const value of ["OTHER.example", "[2001:0DB8:0:0::1]", "other.example:90", "other.example/path", "local\thost", "bad host", ""]) {
|
|
u.hostname = value;
|
|
show("hostname", u);
|
|
}
|
|
for (const value of ["", "00080", "443", "65535", "65536", "999999999999999999999999", "12.75", "4e21", "123suffix", "-1", " 90", "bad", "\t", "8\t0"]) {
|
|
u.port = value;
|
|
show("port", u);
|
|
}
|
|
for (const value of ["HTTP", "https:ignored", "ws:", "wss", "ftp", "file", "foo", "custom+v1:", "1bad", "bad scheme", "\thtt\nps\r:", ""]) {
|
|
u.protocol = value;
|
|
show("protocol", u);
|
|
}
|
|
console.log("view", params === u.searchParams, params.toString());
|
|
params.set("next", "value");
|
|
console.log("query", u.href);
|
|
}
|
|
|
|
const u = new NodeURL("https://old.example:81/a");
|
|
const alias = u;
|
|
console.log("assigned", u.port = "0082suffix", alias.port);
|
|
console.log("compound", u.port += "3", u.port);
|
|
console.log("user-compound", u.username += "a b", u.username);
|
|
const key = "hostname";
|
|
console.log("key", u[key] = "NEW.example", alias.href);
|
|
u.password = "secret";
|
|
u.username = "";
|
|
show("password-only", u);
|
|
u.password = "";
|
|
show("credentials-cleared", u);
|
|
u.href = "foo:/a";
|
|
u.pathname = "//path";
|
|
u.hostname = "new.example";
|
|
show("authority-added", u);
|
|
u.href = "foo:/a";
|
|
u.pathname = "//path";
|
|
u.host = "new.example:82";
|
|
show("host-port-added", u);
|
|
console.log("escaped-components", u.pathname, u.host);
|
|
u.port = "bad";
|
|
console.log("escaped-ignored", u.href, u.pathname, u.host);
|
|
u.port = "82";
|
|
console.log("escaped-next-write", u.href, u.pathname, u.host);
|
|
u.href = "foo:/a";
|
|
u.pathname = "//path";
|
|
u.hostname = "";
|
|
show("empty-authority", u);
|
|
}
|
|
export function reportCoercion() {
|
|
const u = new URL("https://old.example:81/a?old=1#before");
|
|
const params = u.searchParams;
|
|
for (const value of /** @type {any[]} */ ([0, 1234.567, 4.567e21, 1e10, false, null, undefined, 42n, ["12", "34"]])) {
|
|
console.log("port", (u.port = value) === value, u.port, u.href);
|
|
}
|
|
u.username = 12;
|
|
u.password = false;
|
|
u.hostname = { toString() { return "NEW.example"; } };
|
|
console.log("primitive", u.href);
|
|
u.protocol = { toString() { u.hash = "during"; return "http"; } };
|
|
console.log("protocol-convert", u.href);
|
|
u.protocol = { toString() { u.hash = "during"; return "bad scheme"; } };
|
|
console.log("protocol-invalid", u.href);
|
|
u.protocol = { toString() { u.hash = "during"; return "custom"; } };
|
|
console.log("protocol-boundary", u.href);
|
|
u.username = { toString() { u.hash = "during"; return "new user"; } };
|
|
console.log("user-convert", u.href);
|
|
u.password = { toString() { u.hash = "during"; return "new secret"; } };
|
|
console.log("password-convert", u.href);
|
|
u.host = { toString() { u.hash = "during"; return "other.example:bad"; } };
|
|
console.log("host-convert", u.href);
|
|
u.host = { toString() { u.hash = "during"; return "bad host"; } };
|
|
console.log("host-invalid", u.href);
|
|
u.hostname = { toString() { u.hash = "during"; return "final.example"; } };
|
|
console.log("hostname-convert", u.href);
|
|
u.port = { toString() { u.hash = "during"; return "82suffix"; } };
|
|
console.log("port-convert", u.href);
|
|
u.port = { toString() { u.hash = "during"; return "invalid"; } };
|
|
console.log("port-invalid", u.href);
|
|
try { u.username = Symbol("bad"); } catch (error) { console.log("symbol", error.name, error.message, u.href); }
|
|
try { u.port = { toString() { throw new RangeError("conversion"); } }; } catch (error) { console.log("throw", error.name, error.message, u.href); }
|
|
const box = { value: /** @type {unknown} */ (u) };
|
|
const checked = /** @type {any} */ (box.value);
|
|
checked.hostname = "checked.example";
|
|
checked.port = 8080;
|
|
checked.username = "checked user";
|
|
checked.password = "checked secret";
|
|
checked.protocol = "https";
|
|
checked.host = "last.example:443";
|
|
console.log("checked", u.href, params === u.searchParams, params.toString());
|
|
u.href = "file:///a?old=1#before";
|
|
u.username = { toString() { u.hash = "user-during"; return "ignored"; } };
|
|
u.password = { toString() { u.hash = "password-during"; return "ignored"; } };
|
|
u.port = { toString() { u.hash = "port-during"; return "82"; } };
|
|
console.log("file-ignored", u.href);
|
|
u.href = "data:opaque#before";
|
|
u.host = { toString() { u.hash = "host-during"; return "ignored"; } };
|
|
u.hostname = { toString() { u.hash = "hostname-during"; return "ignored"; } };
|
|
console.log("opaque-ignored", u.href);
|
|
}
|