Skip to content

Commit 246a519

Browse files
committed
url: use private properties for brand check
1 parent 928a2b0 commit 246a519

File tree

6 files changed

+59
-116
lines changed

6 files changed

+59
-116
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,
@@ -1396,7 +1396,7 @@ const createRequireError = 'must be a file URL object, file URL string, or ' +
13961396
function createRequire(filename) {
13971397
let filepath;
13981398

1399-
if (isURLInstance(filename) ||
1399+
if (isURL(filename) ||
14001400
(typeof filename === 'string' && !path.isAbsolute(filename))) {
14011401
try {
14021402
filepath = fileURLToPath(filename);

lib/internal/modules/esm/hooks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const {
2121
ERR_INVALID_RETURN_PROPERTY_VALUE,
2222
ERR_INVALID_RETURN_VALUE,
2323
} = require('internal/errors').codes;
24-
const { isURLInstance, URL } = require('internal/url');
24+
const { isURL, URL } = require('internal/url');
2525
const {
2626
isAnyArrayBuffer,
2727
isArrayBufferView,
@@ -263,7 +263,7 @@ class Hooks {
263263
if (
264264
!isMain &&
265265
typeof parentURL !== 'string' &&
266-
!isURLInstance(parentURL)
266+
!isURL(parentURL)
267267
) {
268268
throw new ERR_INVALID_ARG_TYPE(
269269
'parentURL',

lib/internal/url.js

Lines changed: 47 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,16 @@ ObjectDefineProperties(URLSearchParams.prototype, {
535535
});
536536

537537
function isURL(self) {
538-
return self != null && ObjectPrototypeHasOwnProperty(self, context);
538+
return self != null && self.href && self.origin;
539539
}
540540

541541
class URL {
542+
#context = new URLContext();
543+
#searchParams;
544+
542545
constructor(input, base = undefined) {
543546
// toUSVString is not needed.
544547
input = `${input}`;
545-
this[context] = new URLContext();
546548

547549
if (base !== undefined) {
548550
base = `${base}`;
@@ -558,11 +560,6 @@ class URL {
558560
}
559561

560562
[inspect.custom](depth, opts) {
561-
if (this == null ||
562-
ObjectGetPrototypeOf(this[context]) !== URLContext.prototype) {
563-
throw new ERR_INVALID_THIS('URL');
564-
}
565-
566563
if (typeof depth === 'number' && depth < 0)
567564
return this;
568565

@@ -583,182 +580,133 @@ class URL {
583580
obj.hash = this.hash;
584581

585582
if (opts.showHidden) {
586-
obj[context] = this[context];
583+
obj[context] = this.#context;
587584
}
588585

589586
return `${constructor.name} ${inspect(obj, opts)}`;
590587
}
591588

592589
#onParseComplete = (href, origin, protocol, hostname, pathname,
593590
search, username, password, port, hash) => {
594-
const ctx = this[context];
595-
ctx.href = href;
596-
ctx.origin = origin;
597-
ctx.protocol = protocol;
598-
ctx.hostname = hostname;
599-
ctx.pathname = pathname;
600-
ctx.search = search;
601-
ctx.username = username;
602-
ctx.password = password;
603-
ctx.port = port;
604-
ctx.hash = hash;
605-
if (this[searchParams]) {
606-
this[searchParams][searchParams] = parseParams(search);
591+
this.#context.href = href;
592+
this.#context.origin = origin;
593+
this.#context.protocol = protocol;
594+
this.#context.hostname = hostname;
595+
this.#context.pathname = pathname;
596+
this.#context.search = search;
597+
this.#context.username = username;
598+
this.#context.password = password;
599+
this.#context.port = port;
600+
this.#context.hash = hash;
601+
if (this.#searchParams) {
602+
this.#searchParams[searchParams] = parseParams(search);
607603
}
608604
};
609605

610606
toString() {
611-
if (!isURL(this))
612-
throw new ERR_INVALID_THIS('URL');
613-
return this[context].href;
607+
return this.#context.href;
614608
}
615609

616610
get href() {
617-
if (!isURL(this))
618-
throw new ERR_INVALID_THIS('URL');
619-
return this[context].href;
611+
return this.#context.href;
620612
}
621613

622614
set href(value) {
623-
if (!isURL(this))
624-
throw new ERR_INVALID_THIS('URL');
625-
const valid = updateUrl(this[context].href, updateActions.kHref, `${value}`, this.#onParseComplete);
615+
const valid = updateUrl(this.#context.href, updateActions.kHref, `${value}`, this.#onParseComplete);
626616
if (!valid) { throw ERR_INVALID_URL(`${value}`); }
627617
}
628618

629619
// readonly
630620
get origin() {
631-
if (!isURL(this))
632-
throw new ERR_INVALID_THIS('URL');
633-
return this[context].origin;
621+
return this.#context.origin;
634622
}
635623

636624
get protocol() {
637-
if (!isURL(this))
638-
throw new ERR_INVALID_THIS('URL');
639-
return this[context].protocol;
625+
return this.#context.protocol;
640626
}
641627

642628
set protocol(value) {
643-
if (!isURL(this))
644-
throw new ERR_INVALID_THIS('URL');
645-
updateUrl(this[context].href, updateActions.kProtocol, `${value}`, this.#onParseComplete);
629+
updateUrl(this.#context.href, updateActions.kProtocol, `${value}`, this.#onParseComplete);
646630
}
647631

648632
get username() {
649-
if (!isURL(this))
650-
throw new ERR_INVALID_THIS('URL');
651-
return this[context].username;
633+
return this.#context.username;
652634
}
653635

654636
set username(value) {
655-
if (!isURL(this))
656-
throw new ERR_INVALID_THIS('URL');
657-
updateUrl(this[context].href, updateActions.kUsername, `${value}`, this.#onParseComplete);
637+
updateUrl(this.#context.href, updateActions.kUsername, `${value}`, this.#onParseComplete);
658638
}
659639

660640
get password() {
661-
if (!isURL(this))
662-
throw new ERR_INVALID_THIS('URL');
663-
return this[context].password;
641+
return this.#context.password;
664642
}
665643

666644
set password(value) {
667-
if (!isURL(this))
668-
throw new ERR_INVALID_THIS('URL');
669-
updateUrl(this[context].href, updateActions.kPassword, `${value}`, this.#onParseComplete);
645+
updateUrl(this.#context.href, updateActions.kPassword, `${value}`, this.#onParseComplete);
670646
}
671647

672648
get host() {
673-
if (!isURL(this))
674-
throw new ERR_INVALID_THIS('URL');
675-
const port = this[context].port;
649+
const port = this.#context.port;
676650
const suffix = port.length > 0 ? `:${port}` : '';
677-
return this[context].hostname + suffix;
651+
return this.#context.hostname + suffix;
678652
}
679653

680654
set host(value) {
681-
if (!isURL(this))
682-
throw new ERR_INVALID_THIS('URL');
683-
updateUrl(this[context].href, updateActions.kHost, `${value}`, this.#onParseComplete);
655+
updateUrl(this.#context.href, updateActions.kHost, `${value}`, this.#onParseComplete);
684656
}
685657

686658
get hostname() {
687-
if (!isURL(this))
688-
throw new ERR_INVALID_THIS('URL');
689-
return this[context].hostname;
659+
return this.#context.hostname;
690660
}
691661

692662
set hostname(value) {
693-
if (!isURL(this))
694-
throw new ERR_INVALID_THIS('URL');
695-
updateUrl(this[context].href, updateActions.kHostname, `${value}`, this.#onParseComplete);
663+
updateUrl(this.#context.href, updateActions.kHostname, `${value}`, this.#onParseComplete);
696664
}
697665

698666
get port() {
699-
if (!isURL(this))
700-
throw new ERR_INVALID_THIS('URL');
701-
return this[context].port;
667+
return this.#context.port;
702668
}
703669

704670
set port(value) {
705-
if (!isURL(this))
706-
throw new ERR_INVALID_THIS('URL');
707-
updateUrl(this[context].href, updateActions.kPort, `${value}`, this.#onParseComplete);
671+
updateUrl(this.#context.href, updateActions.kPort, `${value}`, this.#onParseComplete);
708672
}
709673

710674
get pathname() {
711-
if (!isURL(this))
712-
throw new ERR_INVALID_THIS('URL');
713-
return this[context].pathname;
675+
return this.#context.pathname;
714676
}
715677

716678
set pathname(value) {
717-
if (!isURL(this))
718-
throw new ERR_INVALID_THIS('URL');
719-
updateUrl(this[context].href, updateActions.kPathname, `${value}`, this.#onParseComplete);
679+
updateUrl(this.#context.href, updateActions.kPathname, `${value}`, this.#onParseComplete);
720680
}
721681

722682
get search() {
723-
if (!isURL(this))
724-
throw new ERR_INVALID_THIS('URL');
725-
return this[context].search;
683+
return this.#context.search;
726684
}
727685

728686
set search(value) {
729-
if (!isURL(this))
730-
throw new ERR_INVALID_THIS('URL');
731-
updateUrl(this[context].href, updateActions.kSearch, toUSVString(value), this.#onParseComplete);
687+
updateUrl(this.#context.href, updateActions.kSearch, toUSVString(value), this.#onParseComplete);
732688
}
733689

734690
// readonly
735691
get searchParams() {
736-
if (!isURL(this))
737-
throw new ERR_INVALID_THIS('URL');
738692
// Create URLSearchParams on demand to greatly improve the URL performance.
739-
if (this[searchParams] == null) {
740-
this[searchParams] = new URLSearchParams(this[context].search);
741-
this[searchParams][context] = this;
693+
if (this.#searchParams == null) {
694+
this.#searchParams = new URLSearchParams(this.#context.search);
695+
this.#searchParams[context] = this;
742696
}
743-
return this[searchParams];
697+
return this.#searchParams;
744698
}
745699

746700
get hash() {
747-
if (!isURL(this))
748-
throw new ERR_INVALID_THIS('URL');
749-
return this[context].hash;
701+
return this.#context.hash;
750702
}
751703

752704
set hash(value) {
753-
if (!isURL(this))
754-
throw new ERR_INVALID_THIS('URL');
755-
updateUrl(this[context].href, updateActions.kHash, `${value}`, this.#onParseComplete);
705+
updateUrl(this.#context.href, updateActions.kHash, `${value}`, this.#onParseComplete);
756706
}
757707

758708
toJSON() {
759-
if (!isURL(this))
760-
throw new ERR_INVALID_THIS('URL');
761-
return this[context].href;
709+
return this.#context.href;
762710
}
763711

764712
static createObjectURL(obj) {
@@ -1206,7 +1154,7 @@ function getPathFromURLPosix(url) {
12061154
function fileURLToPath(path) {
12071155
if (typeof path === 'string')
12081156
path = new URL(path);
1209-
else if (!isURLInstance(path))
1157+
else if (!isURL(path))
12101158
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
12111159
if (path.protocol !== 'file:')
12121160
throw new ERR_INVALID_URL_SCHEME('file');
@@ -1282,12 +1230,8 @@ function pathToFileURL(filepath) {
12821230
return outURL;
12831231
}
12841232

1285-
function isURLInstance(fileURLOrPath) {
1286-
return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin;
1287-
}
1288-
12891233
function toPathIfFileURL(fileURLOrPath) {
1290-
if (!isURLInstance(fileURLOrPath))
1234+
if (!isURL(fileURLOrPath))
12911235
return fileURLOrPath;
12921236
return fileURLToPath(fileURLOrPath);
12931237
}
@@ -1297,7 +1241,6 @@ module.exports = {
12971241
fileURLToPath,
12981242
pathToFileURL,
12991243
toPathIfFileURL,
1300-
isURLInstance,
13011244
URL,
13021245
URLSearchParams,
13031246
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 } = require('internal/validators');
6161

@@ -148,13 +148,13 @@ class Worker extends EventEmitter {
148148
}
149149
url = null;
150150
doEval = 'classic';
151-
} else if (isURLInstance(filename) && filename.protocol === 'data:') {
151+
} else if (isURL(filename) && filename.protocol === 'data:') {
152152
url = null;
153153
doEval = 'module';
154154
filename = `import ${JSONStringify(`${filename}`)}`;
155155
} else {
156156
doEval = false;
157-
if (isURLInstance(filename)) {
157+
if (isURL(filename)) {
158158
url = filename;
159159
filename = fileURLToPath(filename);
160160
} else if (typeof filename !== 'string') {

test/parallel/test-whatwg-url-custom-inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ assert.strictEqual(
6161

6262
assert.strictEqual(
6363
util.inspect({ a: url }, { depth: 0 }),
64-
'{ a: [URL] }');
64+
'{ a: URL {} }');
6565

6666
class MyURL extends URL {}
6767
assert(util.inspect(new MyURL(url.href)).startsWith('MyURL {'));

test/parallel/test-whatwg-url-invalidthis.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const assert = require('assert');
1010
'toJSON',
1111
].forEach((i) => {
1212
assert.throws(() => Reflect.apply(URL.prototype[i], [], {}), {
13-
code: 'ERR_INVALID_THIS',
13+
name: 'TypeError',
1414
});
1515
});
1616

@@ -27,11 +27,11 @@ const assert = require('assert');
2727
'hash',
2828
].forEach((i) => {
2929
assert.throws(() => Reflect.get(URL.prototype, i, {}), {
30-
code: 'ERR_INVALID_THIS',
30+
name: 'TypeError',
3131
});
3232

3333
assert.throws(() => Reflect.set(URL.prototype, i, null, {}), {
34-
code: 'ERR_INVALID_THIS',
34+
name: 'TypeError',
3535
});
3636
});
3737

@@ -40,6 +40,6 @@ const assert = require('assert');
4040
'searchParams',
4141
].forEach((i) => {
4242
assert.throws(() => Reflect.get(URL.prototype, i, {}), {
43-
code: 'ERR_INVALID_THIS',
43+
name: 'TypeError',
4444
});
4545
});

0 commit comments

Comments
 (0)