Files
ignis/shims/url.js

22 lines
629 B
JavaScript
Raw Normal View History

2026-03-07 18:11:46 +01:00
export const urlShim = {
URL: globalThis.URL,
URLSearchParams: globalThis.URLSearchParams,
pathToFileURL(p) {
// Return an object with .href matching Node's url.pathToFileURL behavior
2026-03-11 22:08:30 +01:00
const encoded = encodeURI(p.replace(/\\/g, "/"));
const href = "file:///" + encoded.replace(/^\/+/, "");
2026-03-07 18:11:46 +01:00
return { href, toString: () => href };
},
fileURLToPath(url) {
2026-03-11 22:08:30 +01:00
let str = typeof url === "string" ? url : url.href || url.toString();
if (str.startsWith("file:///")) {
2026-03-07 18:11:46 +01:00
str = str.slice(8);
2026-03-11 22:08:30 +01:00
} else if (str.startsWith("file://")) {
2026-03-07 18:11:46 +01:00
str = str.slice(7);
}
return decodeURI(str);
},
};