Skip to content

Commit 418e93e

Browse files
committed
chore: drop url.parse and url.format
BREAKING CHANGE: WHATWG API is used in favor of url.parse & url.format
1 parent 79a8cda commit 418e93e

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

lib/bundle/util/key-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Object.assign(KeyGenerator.prototype, {
134134

135135
generateKeyForUrl (schema, url, pathFromRoot) {
136136
if (!this.hasExistingGeneratedKey(schema, url)) {
137-
let { path } = parse(url, true);
137+
let { path } = parse(url);
138138
let key = path === "/" ? null : this.generateUniqueKey(schema, this.getPrettifiedKeyForFilepath(path), pathFromRoot);
139139

140140
this.registerNewGeneratedKey(schema, url, key, pathFromRoot);

lib/resolvers/http.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { ono } = require("@jsdevtools/ono");
55
const { AbortController } = require("node-abort-controller");
66
const url = require("../util/url");
77
const { ResolverError } = require("../util/errors");
8+
const { isBrowser } = require("../util/environment");
89

910
module.exports = {
1011
/**
@@ -73,12 +74,7 @@ module.exports = {
7374
* @returns {Promise<Uint8Array | string>}
7475
*/
7576
read (file) {
76-
let u = url.parse(file.url);
77-
78-
if (process.browser && !u.protocol) {
79-
// Use the protocol of the current page
80-
u.protocol = url.parse(location.href).protocol;
81-
}
77+
let u = process.browser ? new URL(file.url, location.origin) : new URL(file.url);
8278

8379
return download(u, this, []);
8480
},
@@ -87,7 +83,7 @@ module.exports = {
8783
/**
8884
* Downloads the given file.
8985
*
90-
* @param {Url|string} u - The url to download (can be a parsed {@link Url} object)
86+
* @param {URL|string} u - The url to download (can be a parsed {@link URL} object)
9187
* @param {object} httpOptions - The `options.resolve.http` object
9288
* @param {string[]} redirects - The redirect URLs that have already been followed
9389
*
@@ -106,7 +102,7 @@ async function download (u, httpOptions, redirects) {
106102
headers.set("Authorization", "Basic " + btoa(u.auth));
107103
}
108104

109-
u = url.parse(url.format(Object.assign(u, { auth: "" })));
105+
u.auth = "";
110106

111107
const controller = new AbortController();
112108

@@ -157,7 +153,7 @@ async function download (u, httpOptions, redirects) {
157153
);
158154
}
159155

160-
let redirectTo = url.resolve(u, location);
156+
let redirectTo = url.resolve(u.href, location);
161157
return await download(redirectTo, httpOptions, redirects);
162158
}
163159

lib/util/url.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,32 @@ let urlDecodePatterns = [
2626
"@",
2727
];
2828

29-
exports.format = require("url").format;
30-
exports.parse = require("url").parse;
29+
class ExtendedURL extends URL {
30+
get auth () {
31+
return this.username + (this.password ? ":" + this.password : "");
32+
}
33+
34+
set auth (auth) {
35+
const [username, password = ""] = auth.split(":");
36+
this.username = username;
37+
this.password = password;
38+
}
39+
40+
get path () {
41+
return this.pathname + this.search;
42+
}
43+
44+
get query () {
45+
return Object.fromEntries(this.searchParams);
46+
}
47+
}
48+
49+
/**
50+
*
51+
* @param value {string}
52+
* @returns {ExtendedURL}
53+
*/
54+
exports.parse = (value) => new ExtendedURL(value);
3155
exports.resolve = require("url").resolve;
3256

3357
/**

0 commit comments

Comments
 (0)