Skip to content

Commit 24def19

Browse files
jasnellMylesBorins
authored andcommitted
url: adding WHATWG URL support
Implements WHATWG URL support. Example: ``` var u = new url.URL('http://example.org'); ``` Many, many other commits improving the implementation have been squashed into this backport PR. They are not listed separately here for brevity. Backport-PR-URL: #17365 PR-URL: #7448 Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
1 parent 60b10f0 commit 24def19

57 files changed

Lines changed: 16345 additions & 75 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
consturl=require('url');
4+
constURL=url.URL;
5+
constassert=require('assert');
6+
constinputs=require('../fixtures/url-inputs.js').urls;
7+
8+
constbench=common.createBenchmark(main,{
9+
type: Object.keys(inputs),
10+
method: ['legacy','whatwg'],
11+
n: [1e5]
12+
});
13+
14+
// At the time of writing, when using a passed property name to index
15+
// the object, Crankshaft would generate a LoadKeyedGeneric even when it
16+
// remains a constant in the function, so here we must use the literal
17+
// instead to get a LoadNamedField.
18+
functionuseLegacy(n,input){
19+
constobj=url.parse(input);
20+
constnoDead={
21+
protocol: obj.protocol,
22+
auth: obj.auth,
23+
host: obj.host,
24+
hostname: obj.hostname,
25+
port: obj.port,
26+
pathname: obj.pathname,
27+
search: obj.search,
28+
hash: obj.hash
29+
};
30+
// It's necessary to assign the values to an object
31+
// to avoid loop invariant code motion.
32+
bench.start();
33+
for(vari=0;i<n;i+=1){
34+
noDead.protocol=obj.protocol;
35+
noDead.auth=obj.auth;
36+
noDead.host=obj.host;
37+
noDead.hostname=obj.hostname;
38+
noDead.port=obj.port;
39+
noDead.pathname=obj.pathname;
40+
noDead.search=obj.search;
41+
noDead.hash=obj.hash;
42+
}
43+
bench.end(n);
44+
returnnoDead;
45+
}
46+
47+
functionuseWHATWG(n,input){
48+
constobj=newURL(input);
49+
constnoDead={
50+
protocol: obj.protocol,
51+
auth: `${obj.username}:${obj.password}`,
52+
host: obj.host,
53+
hostname: obj.hostname,
54+
port: obj.port,
55+
pathname: obj.pathname,
56+
search: obj.search,
57+
hash: obj.hash
58+
};
59+
bench.start();
60+
for(vari=0;i<n;i+=1){
61+
noDead.protocol=obj.protocol;
62+
noDead.auth=`${obj.username}:${obj.password}`;
63+
noDead.host=obj.host;
64+
noDead.hostname=obj.hostname;
65+
noDead.port=obj.port;
66+
noDead.pathname=obj.pathname;
67+
noDead.search=obj.search;
68+
noDead.hash=obj.hash;
69+
}
70+
bench.end(n);
71+
returnnoDead;
72+
}
73+
74+
functionmain(conf){
75+
consttype=conf.type;
76+
constn=conf.n|0;
77+
constmethod=conf.method;
78+
79+
constinput=inputs[type];
80+
if(!input){
81+
thrownewError('Unknown input type');
82+
}
83+
84+
varnoDead;// Avoid dead code elimination.
85+
switch(method){
86+
case'legacy':
87+
noDead=useLegacy(n,input);
88+
break;
89+
case'whatwg':
90+
noDead=useWHATWG(n,input);
91+
break;
92+
default:
93+
thrownewError('Unknown method');
94+
}
95+
96+
assert.ok(noDead);
97+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
consturl=require('url');
4+
constURL=url.URL;
5+
constassert=require('assert');
6+
constinputs=require('../fixtures/url-inputs.js').urls;
7+
8+
constbench=common.createBenchmark(main,{
9+
type: Object.keys(inputs),
10+
method: ['legacy','whatwg'],
11+
n: [1e5]
12+
});
13+
14+
functionuseLegacy(n,input){
15+
varnoDead=url.parse(input);
16+
bench.start();
17+
for(vari=0;i<n;i+=1){
18+
noDead=url.parse(input);
19+
}
20+
bench.end(n);
21+
returnnoDead;
22+
}
23+
24+
functionuseWHATWG(n,input){
25+
varnoDead=newURL(input);
26+
bench.start();
27+
for(vari=0;i<n;i+=1){
28+
noDead=newURL(input);
29+
}
30+
bench.end(n);
31+
returnnoDead;
32+
}
33+
34+
functionmain(conf){
35+
consttype=conf.type;
36+
constn=conf.n|0;
37+
constmethod=conf.method;
38+
39+
constinput=inputs[type];
40+
if(!input){
41+
thrownewError('Unknown input type');
42+
}
43+
44+
varnoDead;// Avoid dead code elimination.
45+
switch(method){
46+
case'legacy':
47+
noDead=useLegacy(n,input);
48+
break;
49+
case'whatwg':
50+
noDead=useWHATWG(n,input);
51+
break;
52+
default:
53+
thrownewError('Unknown method');
54+
}
55+
56+
assert.ok(noDead);
57+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
const{ URLSearchParams }=require('url');
4+
constquerystring=require('querystring');
5+
constinputs=require('../fixtures/url-inputs.js').searchParams;
6+
7+
constbench=common.createBenchmark(main,{
8+
type: Object.keys(inputs),
9+
method: ['legacy','whatwg'],
10+
n: [1e6]
11+
});
12+
13+
functionuseLegacy(n,input){
14+
querystring.parse(input);
15+
bench.start();
16+
for(vari=0;i<n;i+=1){
17+
querystring.parse(input);
18+
}
19+
bench.end(n);
20+
}
21+
22+
functionuseWHATWG(n,input){
23+
newURLSearchParams(input);
24+
bench.start();
25+
for(vari=0;i<n;i+=1){
26+
newURLSearchParams(input);
27+
}
28+
bench.end(n);
29+
}
30+
31+
functionmain(conf){
32+
consttype=conf.type;
33+
constn=conf.n|0;
34+
constmethod=conf.method;
35+
36+
constinput=inputs[type];
37+
if(!input){
38+
thrownewError('Unknown input type');
39+
}
40+
41+
switch(method){
42+
case'legacy':
43+
useLegacy(n,input);
44+
break;
45+
case'whatwg':
46+
useWHATWG(n,input);
47+
break;
48+
default:
49+
thrownewError('Unknown method');
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
const{ URLSearchParams }=require('url');
4+
constquerystring=require('querystring');
5+
constinputs=require('../fixtures/url-inputs.js').searchParams;
6+
7+
constbench=common.createBenchmark(main,{
8+
type: Object.keys(inputs),
9+
method: ['legacy','whatwg'],
10+
n: [1e6]
11+
});
12+
13+
functionuseLegacy(n,input,prop){
14+
constobj=querystring.parse(input);
15+
querystring.stringify(obj);
16+
bench.start();
17+
for(vari=0;i<n;i+=1){
18+
querystring.stringify(obj);
19+
}
20+
bench.end(n);
21+
}
22+
23+
functionuseWHATWG(n,input,prop){
24+
constobj=newURLSearchParams(input);
25+
obj.toString();
26+
bench.start();
27+
for(vari=0;i<n;i+=1){
28+
obj.toString();
29+
}
30+
bench.end(n);
31+
}
32+
33+
functionmain(conf){
34+
consttype=conf.type;
35+
constn=conf.n|0;
36+
constmethod=conf.method;
37+
38+
constinput=inputs[type];
39+
if(!input){
40+
thrownewError('Unknown input type');
41+
}
42+
43+
switch(method){
44+
case'legacy':
45+
useLegacy(n,input);
46+
break;
47+
case'whatwg':
48+
useWHATWG(n,input);
49+
break;
50+
default:
51+
thrownewError('Unknown method');
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
consturl=require('url');
4+
constURL=url.URL;
5+
constassert=require('assert');
6+
constinputs=require('../fixtures/url-inputs.js').urls;
7+
8+
constbench=common.createBenchmark(main,{
9+
type: Object.keys(inputs),
10+
method: ['legacy','whatwg'],
11+
n: [1e5]
12+
});
13+
14+
functionuseLegacy(n,input,prop){
15+
constobj=url.parse(input);
16+
varnoDead=url.format(obj);
17+
bench.start();
18+
for(vari=0;i<n;i+=1){
19+
noDead=url.format(obj);
20+
}
21+
bench.end(n);
22+
returnnoDead;
23+
}
24+
25+
functionuseWHATWG(n,input,prop){
26+
constobj=newURL(input);
27+
varnoDead=obj.toString();
28+
bench.start();
29+
for(vari=0;i<n;i+=1){
30+
noDead=obj.toString();
31+
}
32+
bench.end(n);
33+
returnnoDead;
34+
}
35+
36+
functionmain(conf){
37+
consttype=conf.type;
38+
constn=conf.n|0;
39+
constmethod=conf.method;
40+
41+
constinput=inputs[type];
42+
if(!input){
43+
thrownewError('Unknown input type');
44+
}
45+
46+
varnoDead;// Avoid dead code elimination.
47+
switch(method){
48+
case'legacy':
49+
noDead=useLegacy(n,input);
50+
break;
51+
case'whatwg':
52+
noDead=useWHATWG(n,input);
53+
break;
54+
default:
55+
thrownewError('Unknown method');
56+
}
57+
58+
assert.ok(noDead);
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
constcommon=require('../common.js');
3+
constassert=require('assert');
4+
const{ URLSearchParams }=require('url');
5+
6+
constbench=common.createBenchmark(main,{
7+
method: ['forEach','iterator'],
8+
n: [1e6]
9+
});
10+
11+
conststr='one=single&two=first&three=first&two=2nd&three=2nd&three=3rd';
12+
13+
functionforEach(n){
14+
constparams=newURLSearchParams(str);
15+
constnoDead=[];
16+
constcb=(val,key)=>{
17+
noDead[0]=key;
18+
noDead[1]=val;
19+
};
20+
21+
bench.start();
22+
for(vari=0;i<n;i+=1)
23+
params.forEach(cb);
24+
bench.end(n);
25+
26+
assert.strictEqual(noDead[0],'three');
27+
assert.strictEqual(noDead[1],'3rd');
28+
}
29+
30+
functioniterator(n){
31+
constparams=newURLSearchParams(str);
32+
constnoDead=[];
33+
34+
bench.start();
35+
for(vari=0;i<n;i+=1){
36+
for(constpairofparams){
37+
noDead[0]=pair[0];
38+
noDead[1]=pair[1];
39+
}
40+
}
41+
bench.end(n);
42+
43+
assert.strictEqual(noDead[0],'three');
44+
assert.strictEqual(noDead[1],'3rd');
45+
}
46+
47+
functionmain(conf){
48+
constmethod=conf.method;
49+
constn=conf.n|0;
50+
51+
switch(method){
52+
case'forEach':
53+
forEach(n);
54+
break;
55+
case'iterator':
56+
iterator(n);
57+
break;
58+
default:
59+
thrownewError('Unknown method');
60+
}
61+
}

0 commit comments

Comments
 (0)