Skip to content

Commit b2a9e60

Browse files
TimothyGuevanlucas
authored andcommittedMay 1, 2017
url: clean up WHATWG URL origin generation
- Use ordinary properties instead of symbols/getter redirection for internal object - Use template string literals - Remove unneeded custom inspection for internal objects - Remove unneeded OpaqueOrigin class - Remove unneeded type checks PR-URL: #12507 Reviewed-By: James M Snell <[email protected]>
1 parent 29531d2 commit b2a9e60

File tree

1 file changed

+29
-95
lines changed

1 file changed

+29
-95
lines changed
 

‎lib/internal/url.js

+29-95
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ const os = require('os');
1515

1616
const isWindows = process.platform === 'win32';
1717

18-
const kScheme = Symbol('scheme');
19-
const kHost = Symbol('host');
20-
const kPort = Symbol('port');
21-
const kDomain = Symbol('domain');
2218
const kFormat = Symbol('format');
2319

2420
// https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object
@@ -38,62 +34,15 @@ function toUSVString(val) {
3834
return binding.toUSVString(str, match.index);
3935
}
4036

41-
class OpaqueOrigin {
42-
toString() {
43-
return 'null';
44-
}
37+
// Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque
38+
const kOpaqueOrigin = 'null';
4539

46-
get effectiveDomain() {
47-
return this;
48-
}
49-
}
50-
51-
class TupleOrigin {
52-
constructor(scheme, host, port, domain) {
53-
this[kScheme] = scheme;
54-
this[kHost] = host;
55-
this[kPort] = port;
56-
this[kDomain] = domain;
57-
}
58-
59-
get scheme() {
60-
return this[kScheme];
61-
}
62-
63-
get host() {
64-
return this[kHost];
65-
}
66-
67-
get port() {
68-
return this[kPort];
69-
}
70-
71-
get domain() {
72-
return this[kDomain];
73-
}
74-
75-
get effectiveDomain() {
76-
return this[kDomain] || this[kHost];
77-
}
78-
79-
// https://url.spec.whatwg.org/#dom-url-origin
80-
toString(unicode = true) {
81-
var result = this[kScheme];
82-
result += '://';
83-
result += unicode ? domainToUnicode(this[kHost]) : this[kHost];
84-
if (this[kPort] !== undefined && this[kPort] !== null)
85-
result += `:${this[kPort]}`;
86-
return result;
87-
}
88-
89-
[util.inspect.custom]() {
90-
return `TupleOrigin {
91-
scheme: ${this[kScheme]},
92-
host: ${this[kHost]},
93-
port: ${this[kPort]},
94-
domain: ${this[kDomain]}
95-
}`;
96-
}
40+
// Refs:
41+
// - https://html.spec.whatwg.org/multipage/browsers.html#unicode-serialisation-of-an-origin
42+
// - https://html.spec.whatwg.org/multipage/browsers.html#ascii-serialisation-of-an-origin
43+
function serializeTupleOrigin(scheme, host, port, unicode = true) {
44+
const unicodeHost = unicode ? domainToUnicode(host) : host;
45+
return `${scheme}//${unicodeHost}${port == null ? '' : `:${port}`}`;
9746
}
9847

9948
// This class provides the internal state of a URL object. An instance of this
@@ -359,7 +308,27 @@ Object.defineProperties(URL.prototype, {
359308
enumerable: true,
360309
configurable: true,
361310
get() {
362-
return originFor(this).toString();
311+
// Refs: https://url.spec.whatwg.org/#concept-url-origin
312+
const ctx = this[context];
313+
switch (ctx.scheme) {
314+
case 'blob:':
315+
if (ctx.path.length > 0) {
316+
try {
317+
return (new URL(ctx.path[0])).origin;
318+
} catch (err) {
319+
// fall through... do nothing
320+
}
321+
}
322+
return kOpaqueOrigin;
323+
case 'ftp:':
324+
case 'gopher:':
325+
case 'http:':
326+
case 'https:':
327+
case 'ws:':
328+
case 'wss:':
329+
return serializeTupleOrigin(ctx.scheme, ctx.host, ctx.port);
330+
}
331+
return kOpaqueOrigin;
363332
}
364333
},
365334
protocol: {
@@ -1274,41 +1243,6 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', {
12741243
}
12751244
});
12761245

1277-
function originFor(url, base) {
1278-
if (url != undefined &&
1279-
(!url[searchParams] || !url[searchParams][searchParams])) {
1280-
url = new URL(url, base);
1281-
}
1282-
var origin;
1283-
const protocol = url.protocol;
1284-
switch (protocol) {
1285-
case 'blob:':
1286-
if (url[context].path && url[context].path.length > 0) {
1287-
try {
1288-
return (new URL(url[context].path[0])).origin;
1289-
} catch (err) {
1290-
// fall through... do nothing
1291-
}
1292-
}
1293-
origin = new OpaqueOrigin();
1294-
break;
1295-
case 'ftp:':
1296-
case 'gopher:':
1297-
case 'http:':
1298-
case 'https:':
1299-
case 'ws:':
1300-
case 'wss:':
1301-
origin = new TupleOrigin(protocol.slice(0, -1),
1302-
url[context].host,
1303-
url[context].port,
1304-
null);
1305-
break;
1306-
default:
1307-
origin = new OpaqueOrigin();
1308-
}
1309-
return origin;
1310-
}
1311-
13121246
function domainToASCII(domain) {
13131247
if (arguments.length < 1)
13141248
throw new TypeError('"domain" argument must be specified');

0 commit comments

Comments
 (0)