Files
scriptc/tests/fixtures/npm/node_modules/node24-url-fixture/mutation.js
Chris Tate 41b5eed650 Add Node 24 URL mutation support (#654)
* Add Node 24 URL mutation support

* Record preflight baselines for URL mutation fixtures
2026-10-04 01:57:10 -05:00

76 lines
3.6 KiB
JavaScript

import { URL as NodeURL } from "node:url";
export function reportMutations() {
for (const base of ["https://user:pw@h:81/a?old=1#before", "foo://h", "foo://", "foo:/a", "file:///C:/a", "data:text/plain,hello"]) {
const u = new NodeURL(base);
const alias = u;
const params = u.searchParams;
const show = (label) => console.log(label, u.href, u.pathname, u.search, u.hash, params.toString());
for (const value of ["", "///x", "a b\tc\r\n?#ü", "/a/%2e%2e/b", "/D|/x"]) {
u.pathname = value;
show("path");
}
for (const value of ["", "?", "??x", "\t?x", "\t", "a b\tc\r\n?#ü'", "x=%FF&x=second"]) {
u.search = value;
show("query");
console.log("same", params === u.searchParams, params.getAll("x").join("|"));
}
for (const value of ["", "#", "##x", "\t#x", "\n", "a b\tc\r\n?#ü`", "%2f"]) {
u.hash = value;
show("fragment");
}
params.set("next", "~ ü");
show("params");
u.href = "http://new.example:80/b/../c?fresh=two+words#end";
show("replace");
console.log("identity", alias === u, params === u.searchParams, params.get("fresh"), u.origin, u.protocol, u.host, u.username);
params.append("last", "value");
show("old-view");
const before = u.href;
try { u.href = "relative"; } catch (error) { console.log("invalid", error.name, error.code, error.message); }
console.log("retained", u.href === before, params === u.searchParams);
}
const u = new URL("https://h/a");
const params = u.searchParams;
for (const value of [12, false, null, undefined, 42n, ["one", "two"]]) {
const result = u.search = value;
console.log("value", typeof result, String(result), u.search, u.href);
}
const events = [];
const value = { toString() { events.push("convert"); u.hash = "during"; return "a b#c"; } };
console.log("object", (u.search = value) === value, events.join(","), u.href);
try { u.hash = Symbol("bad"); } catch (error) { console.log("symbol", error.name, error.message, u.hash); }
try { u.search = { toString() { throw new RangeError("conversion"); } }; } catch (error) { console.log("throw", error.name, error.message, u.search); }
let order = "";
const receiver = () => { order += "r"; return u; };
const rhs = () => { order += "v"; u.search = "during=yes"; return "after=done"; };
console.log("yield", receiver().search = rhs(), order, u.search);
order = "";
console.log("compound", receiver().search += rhs(), order, u.search);
console.log("computed", u["hash"] = "unescaped space", u.hash);
console.log("empty", u.search = "?", u.search, u.href);
u.href = "foo:/a";
u.pathname = "//host/path";
console.log("hostless", u.href, u.pathname, u.host);
u.href = "https://h/a?first=1&second=2";
const iterator = params.entries();
console.log("iteration", iterator.next().value.join("="));
u.search = "new=3&last=4";
console.log("iteration", iterator.next().value.join("="), iterator.next().done);
u.href = "data:hello%20?query#fragment";
u.search = "";
console.log("opaque", u.href);
u.hash = "";
console.log("opaque", u.href, u.pathname);
u.href = "https://h/a?old=1#before";
u.pathname = { toString() { u.search = "during=yes"; return "next path"; } };
console.log("path-convert", u.href, params.toString());
u.hash = { toString() { u.pathname = "during"; return "after"; } };
console.log("hash-convert", u.href);
u.href = { toString() { u.hash = "during"; return "https://final.example/end?final=yes"; } };
console.log("href-convert", u.href, params.toString());
u.href = "data:opaque#old";
u.pathname = { toString() { u.hash = "during"; return "ignored"; } };
console.log("opaque-convert", u.href);
}