Browse Source

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: https://github.com/nodejs/node/pull/12252
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Timothy Gu 8 years ago
committed by Anna Henningsen
parent
commit
aff5cc92b9
No known key found for this signature in database GPG Key ID: D8B9F5AEAE84E4CF
  1. 124
      lib/internal/url.js

124
lib/internal/url.js

@ -15,10 +15,6 @@ const os = require('os');
const isWindows = process.platform === 'win32';
const kScheme = Symbol('scheme');
const kHost = Symbol('host');
const kPort = Symbol('port');
const kDomain = Symbol('domain');
const kFormat = Symbol('format');
// https://tc39.github.io/ecma262/#sec-%iteratorprototype%-object
@ -38,62 +34,15 @@ function toUSVString(val) {
return binding.toUSVString(str, match.index);
}
class OpaqueOrigin {
toString() {
return 'null';
}
// Refs: https://html.spec.whatwg.org/multipage/browsers.html#concept-origin-opaque
const kOpaqueOrigin = 'null';
get effectiveDomain() {
return this;
}
}
class TupleOrigin {
constructor(scheme, host, port, domain) {
this[kScheme] = scheme;
this[kHost] = host;
this[kPort] = port;
this[kDomain] = domain;
}
get scheme() {
return this[kScheme];
}
get host() {
return this[kHost];
}
get port() {
return this[kPort];
}
get domain() {
return this[kDomain];
}
get effectiveDomain() {
return this[kDomain] || this[kHost];
}
// https://url.spec.whatwg.org/#dom-url-origin
toString(unicode = true) {
var result = this[kScheme];
result += '://';
result += unicode ? domainToUnicode(this[kHost]) : this[kHost];
if (this[kPort] !== undefined && this[kPort] !== null)
result += `:${this[kPort]}`;
return result;
}
[util.inspect.custom]() {
return `TupleOrigin {
scheme: ${this[kScheme]},
host: ${this[kHost]},
port: ${this[kPort]},
domain: ${this[kDomain]}
}`;
}
// Refs:
// - https://html.spec.whatwg.org/multipage/browsers.html#unicode-serialisation-of-an-origin
// - https://html.spec.whatwg.org/multipage/browsers.html#ascii-serialisation-of-an-origin
function serializeTupleOrigin(scheme, host, port, unicode = true) {
const unicodeHost = unicode ? domainToUnicode(host) : host;
return `${scheme}//${unicodeHost}${port == null ? '' : `:${port}`}`;
}
// This class provides the internal state of a URL object. An instance of this
@ -359,7 +308,27 @@ Object.defineProperties(URL.prototype, {
enumerable: true,
configurable: true,
get() {
return originFor(this).toString();
// Refs: https://url.spec.whatwg.org/#concept-url-origin
const ctx = this[context];
switch (ctx.scheme) {
case 'blob:':
if (ctx.path.length > 0) {
try {
return (new URL(ctx.path[0])).origin;
} catch (err) {
// fall through... do nothing
}
}
return kOpaqueOrigin;
case 'ftp:':
case 'gopher:':
case 'http:':
case 'https:':
case 'ws:':
case 'wss:':
return serializeTupleOrigin(ctx.scheme, ctx.host, ctx.port);
}
return kOpaqueOrigin;
}
},
protocol: {
@ -1274,41 +1243,6 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParamsIterator', {
}
});
function originFor(url, base) {
if (url != undefined &&
(!url[searchParams] || !url[searchParams][searchParams])) {
url = new URL(url, base);
}
var origin;
const protocol = url.protocol;
switch (protocol) {
case 'blob:':
if (url[context].path && url[context].path.length > 0) {
try {
return (new URL(url[context].path[0])).origin;
} catch (err) {
// fall through... do nothing
}
}
origin = new OpaqueOrigin();
break;
case 'ftp:':
case 'gopher:':
case 'http:':
case 'https:':
case 'ws:':
case 'wss:':
origin = new TupleOrigin(protocol.slice(0, -1),
url[context].host,
url[context].port,
null);
break;
default:
origin = new OpaqueOrigin();
}
return origin;
}
function domainToASCII(domain) {
if (arguments.length < 1)
throw new TypeError('"domain" argument must be specified');

Loading…
Cancel
Save