Skip to content

Commit 42d9393

Browse files
committed
url: backport non-major changes from nodejs#46904
1 parent 476dff1 commit 42d9393

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const { BuiltinModule } = require('internal/bootstrap/loaders');
7979
const {
8080
maybeCacheSourceMap,
8181
} = require('internal/source_map/source_map_cache');
82-
const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url');
82+
const { pathToFileURL, fileURLToPath, isURL } = require('internal/url');
8383
const {
8484
deprecate,
8585
emitExperimentalWarning,
@@ -1363,7 +1363,7 @@ const createRequireError = 'must be a file URL object, file URL string, or ' +
13631363
function createRequire(filename) {
13641364
let filepath;
13651365

1366-
if (isURLInstance(filename) ||
1366+
if (isURL(filename) ||
13671367
(typeof filename === 'string' && !path.isAbsolute(filename))) {
13681368
try {
13691369
filepath = fileURLToPath(filename);

lib/internal/modules/esm/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const {
3131
ERR_INVALID_RETURN_VALUE,
3232
ERR_UNKNOWN_MODULE_FORMAT,
3333
} = require('internal/errors').codes;
34-
const { pathToFileURL, isURLInstance, URL } = require('internal/url');
34+
const { pathToFileURL, isURL, URL } = require('internal/url');
3535
const { emitExperimentalWarning } = require('internal/util');
3636
const {
3737
isAnyArrayBuffer,
@@ -792,7 +792,7 @@ class ESMLoader {
792792
if (
793793
!isMain &&
794794
typeof parentURL !== 'string' &&
795-
!isURLInstance(parentURL)
795+
!isURL(parentURL)
796796
) {
797797
throw new ERR_INVALID_ARG_TYPE(
798798
'parentURL',

lib/internal/url.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,16 @@ ObjectDefineProperties(URLSearchParams.prototype, {
536536
},
537537
});
538538

539+
/**
540+
* Checks if a value has the shape of a WHATWG URL object.
541+
*
542+
* Using a symbol or instanceof would not be able to recognize URL objects
543+
* coming from other implementations (e.g. in Electron), so instead we are
544+
* checking some well known properties for a lack of a better test.
545+
*
546+
* @param {*} self
547+
* @returns {self is URL}
548+
*/
539549
function isURL(self) {
540550
return self != null && ObjectPrototypeHasOwnProperty(self, context);
541551
}
@@ -1209,7 +1219,7 @@ function getPathFromURLPosix(url) {
12091219
function fileURLToPath(path) {
12101220
if (typeof path === 'string')
12111221
path = new URL(path);
1212-
else if (!isURLInstance(path))
1222+
else if (!isURL(path))
12131223
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
12141224
if (path.protocol !== 'file:')
12151225
throw new ERR_INVALID_URL_SCHEME('file');
@@ -1285,12 +1295,8 @@ function pathToFileURL(filepath) {
12851295
return outURL;
12861296
}
12871297

1288-
function isURLInstance(fileURLOrPath) {
1289-
return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin;
1290-
}
1291-
12921298
function toPathIfFileURL(fileURLOrPath) {
1293-
if (!isURLInstance(fileURLOrPath))
1299+
if (!isURL(fileURLOrPath))
12941300
return fileURLOrPath;
12951301
return fileURLToPath(fileURLOrPath);
12961302
}
@@ -1300,7 +1306,6 @@ module.exports = {
13001306
fileURLToPath,
13011307
pathToFileURL,
13021308
toPathIfFileURL,
1303-
isURLInstance,
13041309
URL,
13051310
URLSearchParams,
13061311
domainToASCII,

lib/internal/worker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const {
5555
WritableWorkerStdio,
5656
} = workerIo;
5757
const { deserializeError } = require('internal/error_serdes');
58-
const { fileURLToPath, isURLInstance, pathToFileURL } = require('internal/url');
58+
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
5959
const { kEmptyObject } = require('internal/util');
6060
const { validateArray, validateString } = require('internal/validators');
6161

@@ -145,13 +145,13 @@ class Worker extends EventEmitter {
145145
}
146146
url = null;
147147
doEval = 'classic';
148-
} else if (isURLInstance(filename) && filename.protocol === 'data:') {
148+
} else if (isURL(filename) && filename.protocol === 'data:') {
149149
url = null;
150150
doEval = 'module';
151151
filename = `import ${JSONStringify(`${filename}`)}`;
152152
} else {
153153
doEval = false;
154-
if (isURLInstance(filename)) {
154+
if (isURL(filename)) {
155155
url = filename;
156156
filename = fileURLToPath(filename);
157157
} else if (typeof filename !== 'string') {

0 commit comments

Comments
 (0)