Skip to content

Commit

Permalink
url: clean up WHATWG URL origin generation
Browse files Browse the repository at this point in the history
- 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 <jasnell@gmail.com>
  • Loading branch information
TimothyGu authored and evanlucas committed May 1, 2017
1 parent 29531d2 commit b2a9e60
Showing 1 changed file with 29 additions and 95 deletions.
124 changes: 29 additions & 95 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit b2a9e60

Please sign in to comment.