Skip to content

Commit affc2e0

Browse files
committed
url: backport non-major changes from nodejs#46904
1 parent 17e3fd0 commit affc2e0

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

lib/internal/modules/cjs/loader.js

+2-2
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

+2-2
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,
@@ -789,7 +789,7 @@ class ESMLoader {
789789
if (
790790
!isMain &&
791791
typeof parentURL !== 'string' &&
792-
!isURLInstance(parentURL)
792+
!isURL(parentURL)
793793
) {
794794
throw new ERR_INVALID_ARG_TYPE(
795795
'parentURL',

lib/internal/url.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,15 @@ ObjectDefineProperties(URLSearchParams.prototype, {
557557
},
558558
});
559559

560+
/**
561+
* Checks if a value has the shape of a WHATWG URL object.
562+
*
563+
* Using a symbol or instanceof would not be able to recognize URL objects
564+
* coming from other implementations (e.g. in Electron), so instead we are
565+
* checking some well known properties for a lack of a better test.
566+
* @param {*} self
567+
* @returns {self is URL}
568+
*/
560569
function isURL(self) {
561570
return self != null && ObjectPrototypeHasOwnProperty(self, context);
562571
}
@@ -1235,7 +1244,7 @@ function getPathFromURLPosix(url) {
12351244
function fileURLToPath(path) {
12361245
if (typeof path === 'string')
12371246
path = new URL(path);
1238-
else if (!isURLInstance(path))
1247+
else if (!isURL(path))
12391248
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
12401249
if (path.protocol !== 'file:')
12411250
throw new ERR_INVALID_URL_SCHEME('file');
@@ -1311,12 +1320,8 @@ function pathToFileURL(filepath) {
13111320
return outURL;
13121321
}
13131322

1314-
function isURLInstance(fileURLOrPath) {
1315-
return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin;
1316-
}
1317-
13181323
function toPathIfFileURL(fileURLOrPath) {
1319-
if (!isURLInstance(fileURLOrPath))
1324+
if (!isURL(fileURLOrPath))
13201325
return fileURLOrPath;
13211326
return fileURLToPath(fileURLOrPath);
13221327
}
@@ -1326,7 +1331,6 @@ module.exports = {
13261331
fileURLToPath,
13271332
pathToFileURL,
13281333
toPathIfFileURL,
1329-
isURLInstance,
13301334
URL,
13311335
URLSearchParams,
13321336
domainToASCII,

lib/internal/worker.js

+3-3
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)