Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc15df6e

Browse files
committed
url: use private properties for brand check
1 parent928a2b0 commitc15df6e

File tree

6 files changed

+69
-117
lines changed

6 files changed

+69
-117
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
functioncreateRequire(filename){
13971397
letfilepath;
13981398

1399-
if(isURLInstance(filename)||
1399+
if(isURL(filename)||
14001400
(typeoffilename==='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
typeofparentURL!=='string'&&
266-
!isURLInstance(parentURL)
266+
!isURL(parentURL)
267267
){
268268
thrownewERR_INVALID_ARG_TYPE(
269269
'parentURL',

‎lib/internal/url.js

Lines changed: 57 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
ObjectGetOwnPropertySymbols,
1616
ObjectGetPrototypeOf,
1717
ObjectKeys,
18-
ObjectPrototypeHasOwnProperty,
1918
ReflectGetOwnPropertyDescriptor,
2019
ReflectOwnKeys,
2120
RegExpPrototypeSymbolReplace,
@@ -534,15 +533,27 @@ ObjectDefineProperties(URLSearchParams.prototype, {
534533
},
535534
});
536535

536+
/**
537+
* Checks if a value has the shape of a WHATWG URL object.
538+
*
539+
* Using a symbol or instanceof would not be able to recognize URL objects
540+
* coming from other implementations (e.g. in Electron), so instead we are
541+
* checking some well known properties for a lack of a better test.
542+
*
543+
*@param {*} self
544+
*@returns {asserts value is URL}
545+
*/
537546
functionisURL(self){
538-
returnself!=null&&ObjectPrototypeHasOwnProperty(self,context);
547+
returnself!=null&&self.href&&self.origin;
539548
}
540549

541550
classURL{
551+
#context=newURLContext();
552+
#searchParams;
553+
542554
constructor(input,base=undefined){
543555
// toUSVString is not needed.
544556
input=`${input}`;
545-
this[context]=newURLContext();
546557

547558
if(base!==undefined){
548559
base=`${base}`;
@@ -558,11 +569,6 @@ class URL {
558569
}
559570

560571
[inspect.custom](depth,opts){
561-
if(this==null||
562-
ObjectGetPrototypeOf(this[context])!==URLContext.prototype){
563-
thrownewERR_INVALID_THIS('URL');
564-
}
565-
566572
if(typeofdepth==='number'&&depth<0)
567573
returnthis;
568574

@@ -583,182 +589,133 @@ class URL {
583589
obj.hash=this.hash;
584590

585591
if(opts.showHidden){
586-
obj[context]=this[context];
592+
obj[context]=this.#context;
587593
}
588594

589595
return`${constructor.name}${inspect(obj,opts)}`;
590596
}
591597

592598
#onParseComplete=(href,origin,protocol,hostname,pathname,
593599
search,username,password,port,hash)=>{
594-
constctx=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);
600+
this.#context.href=href;
601+
this.#context.origin=origin;
602+
this.#context.protocol=protocol;
603+
this.#context.hostname=hostname;
604+
this.#context.pathname=pathname;
605+
this.#context.search=search;
606+
this.#context.username=username;
607+
this.#context.password=password;
608+
this.#context.port=port;
609+
this.#context.hash=hash;
610+
if(this.#searchParams){
611+
this.#searchParams[searchParams]=parseParams(search);
607612
}
608613
};
609614

610615
toString(){
611-
if(!isURL(this))
612-
thrownewERR_INVALID_THIS('URL');
613-
returnthis[context].href;
616+
returnthis.#context.href;
614617
}
615618

616619
gethref(){
617-
if(!isURL(this))
618-
thrownewERR_INVALID_THIS('URL');
619-
returnthis[context].href;
620+
returnthis.#context.href;
620621
}
621622

622623
sethref(value){
623-
if(!isURL(this))
624-
thrownewERR_INVALID_THIS('URL');
625-
constvalid=updateUrl(this[context].href,updateActions.kHref,`${value}`,this.#onParseComplete);
624+
constvalid=updateUrl(this.#context.href,updateActions.kHref,`${value}`,this.#onParseComplete);
626625
if(!valid){throwERR_INVALID_URL(`${value}`);}
627626
}
628627

629628
// readonly
630629
getorigin(){
631-
if(!isURL(this))
632-
thrownewERR_INVALID_THIS('URL');
633-
returnthis[context].origin;
630+
returnthis.#context.origin;
634631
}
635632

636633
getprotocol(){
637-
if(!isURL(this))
638-
thrownewERR_INVALID_THIS('URL');
639-
returnthis[context].protocol;
634+
returnthis.#context.protocol;
640635
}
641636

642637
setprotocol(value){
643-
if(!isURL(this))
644-
thrownewERR_INVALID_THIS('URL');
645-
updateUrl(this[context].href,updateActions.kProtocol,`${value}`,this.#onParseComplete);
638+
updateUrl(this.#context.href,updateActions.kProtocol,`${value}`,this.#onParseComplete);
646639
}
647640

648641
getusername(){
649-
if(!isURL(this))
650-
thrownewERR_INVALID_THIS('URL');
651-
returnthis[context].username;
642+
returnthis.#context.username;
652643
}
653644

654645
setusername(value){
655-
if(!isURL(this))
656-
thrownewERR_INVALID_THIS('URL');
657-
updateUrl(this[context].href,updateActions.kUsername,`${value}`,this.#onParseComplete);
646+
updateUrl(this.#context.href,updateActions.kUsername,`${value}`,this.#onParseComplete);
658647
}
659648

660649
getpassword(){
661-
if(!isURL(this))
662-
thrownewERR_INVALID_THIS('URL');
663-
returnthis[context].password;
650+
returnthis.#context.password;
664651
}
665652

666653
setpassword(value){
667-
if(!isURL(this))
668-
thrownewERR_INVALID_THIS('URL');
669-
updateUrl(this[context].href,updateActions.kPassword,`${value}`,this.#onParseComplete);
654+
updateUrl(this.#context.href,updateActions.kPassword,`${value}`,this.#onParseComplete);
670655
}
671656

672657
gethost(){
673-
if(!isURL(this))
674-
thrownewERR_INVALID_THIS('URL');
675-
constport=this[context].port;
658+
constport=this.#context.port;
676659
constsuffix=port.length>0 ?`:${port}` :'';
677-
returnthis[context].hostname+suffix;
660+
returnthis.#context.hostname+suffix;
678661
}
679662

680663
sethost(value){
681-
if(!isURL(this))
682-
thrownewERR_INVALID_THIS('URL');
683-
updateUrl(this[context].href,updateActions.kHost,`${value}`,this.#onParseComplete);
664+
updateUrl(this.#context.href,updateActions.kHost,`${value}`,this.#onParseComplete);
684665
}
685666

686667
gethostname(){
687-
if(!isURL(this))
688-
thrownewERR_INVALID_THIS('URL');
689-
returnthis[context].hostname;
668+
returnthis.#context.hostname;
690669
}
691670

692671
sethostname(value){
693-
if(!isURL(this))
694-
thrownewERR_INVALID_THIS('URL');
695-
updateUrl(this[context].href,updateActions.kHostname,`${value}`,this.#onParseComplete);
672+
updateUrl(this.#context.href,updateActions.kHostname,`${value}`,this.#onParseComplete);
696673
}
697674

698675
getport(){
699-
if(!isURL(this))
700-
thrownewERR_INVALID_THIS('URL');
701-
returnthis[context].port;
676+
returnthis.#context.port;
702677
}
703678

704679
setport(value){
705-
if(!isURL(this))
706-
thrownewERR_INVALID_THIS('URL');
707-
updateUrl(this[context].href,updateActions.kPort,`${value}`,this.#onParseComplete);
680+
updateUrl(this.#context.href,updateActions.kPort,`${value}`,this.#onParseComplete);
708681
}
709682

710683
getpathname(){
711-
if(!isURL(this))
712-
thrownewERR_INVALID_THIS('URL');
713-
returnthis[context].pathname;
684+
returnthis.#context.pathname;
714685
}
715686

716687
setpathname(value){
717-
if(!isURL(this))
718-
thrownewERR_INVALID_THIS('URL');
719-
updateUrl(this[context].href,updateActions.kPathname,`${value}`,this.#onParseComplete);
688+
updateUrl(this.#context.href,updateActions.kPathname,`${value}`,this.#onParseComplete);
720689
}
721690

722691
getsearch(){
723-
if(!isURL(this))
724-
thrownewERR_INVALID_THIS('URL');
725-
returnthis[context].search;
692+
returnthis.#context.search;
726693
}
727694

728695
setsearch(value){
729-
if(!isURL(this))
730-
thrownewERR_INVALID_THIS('URL');
731-
updateUrl(this[context].href,updateActions.kSearch,toUSVString(value),this.#onParseComplete);
696+
updateUrl(this.#context.href,updateActions.kSearch,toUSVString(value),this.#onParseComplete);
732697
}
733698

734699
// readonly
735700
getsearchParams(){
736-
if(!isURL(this))
737-
thrownewERR_INVALID_THIS('URL');
738701
// Create URLSearchParams on demand to greatly improve the URL performance.
739-
if(this[searchParams]==null){
740-
this[searchParams]=newURLSearchParams(this[context].search);
741-
this[searchParams][context]=this;
702+
if(this.#searchParams==null){
703+
this.#searchParams=newURLSearchParams(this.#context.search);
704+
this.#searchParams[context]=this;
742705
}
743-
returnthis[searchParams];
706+
returnthis.#searchParams;
744707
}
745708

746709
gethash(){
747-
if(!isURL(this))
748-
thrownewERR_INVALID_THIS('URL');
749-
returnthis[context].hash;
710+
returnthis.#context.hash;
750711
}
751712

752713
sethash(value){
753-
if(!isURL(this))
754-
thrownewERR_INVALID_THIS('URL');
755-
updateUrl(this[context].href,updateActions.kHash,`${value}`,this.#onParseComplete);
714+
updateUrl(this.#context.href,updateActions.kHash,`${value}`,this.#onParseComplete);
756715
}
757716

758717
toJSON(){
759-
if(!isURL(this))
760-
thrownewERR_INVALID_THIS('URL');
761-
returnthis[context].href;
718+
returnthis.#context.href;
762719
}
763720

764721
staticcreateObjectURL(obj){
@@ -1206,7 +1163,7 @@ function getPathFromURLPosix(url) {
12061163
functionfileURLToPath(path){
12071164
if(typeofpath==='string')
12081165
path=newURL(path);
1209-
elseif(!isURLInstance(path))
1166+
elseif(!isURL(path))
12101167
thrownewERR_INVALID_ARG_TYPE('path',['string','URL'],path);
12111168
if(path.protocol!=='file:')
12121169
thrownewERR_INVALID_URL_SCHEME('file');
@@ -1282,12 +1239,8 @@ function pathToFileURL(filepath) {
12821239
returnoutURL;
12831240
}
12841241

1285-
functionisURLInstance(fileURLOrPath){
1286-
returnfileURLOrPath!=null&&fileURLOrPath.href&&fileURLOrPath.origin;
1287-
}
1288-
12891242
functiontoPathIfFileURL(fileURLOrPath){
1290-
if(!isURLInstance(fileURLOrPath))
1243+
if(!isURL(fileURLOrPath))
12911244
returnfileURLOrPath;
12921245
returnfileURLToPath(fileURLOrPath);
12931246
}
@@ -1297,7 +1250,6 @@ module.exports = {
12971250
fileURLToPath,
12981251
pathToFileURL,
12991252
toPathIfFileURL,
1300-
isURLInstance,
13011253
URL,
13021254
URLSearchParams,
13031255
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-
}elseif(isURLInstance(filename)&&filename.protocol==='data:'){
151+
}elseif(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
}elseif(typeoffilename!=='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
classMyURLextendsURL{}
6767
assert(util.inspect(newMyURL(url.href)).startsWith('MyURL {'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp