Skip to content

Commit

Permalink
Merge pull request #125 from curbengh/refactor-url-api
Browse files Browse the repository at this point in the history
perf: utilize legacy url.parse() in url_for() & full_url_for()
  • Loading branch information
curbengh committed Nov 10, 2019
2 parents 71bbcc4 + bd99335 commit 71fce3f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
20 changes: 6 additions & 14 deletions lib/full_url_for.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
'use strict';

const { URL } = require('url');
const { parse, URL } = require('url');
const encodeURL = require('./encode_url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};

function fullUrlForHelper(path = '/') {
if (path.startsWith('//')) return path;

const { config } = this;
const data = urlObj(path);
const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);

// Exit if this is an external path
if (typeof data === 'object') {
if (data.origin !== 'null') return path;
}
// Exit if this is an external link
if (data.origin !== 'null' && data.hostname !== sitehost) return path;

path = encodeURL(config.url + `/${path}`.replace(/\/{2,}/g, '/'));
return path;
Expand Down
23 changes: 7 additions & 16 deletions lib/url_for.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
'use strict';

const { URL } = require('url');
const { parse, URL } = require('url');
const encodeURL = require('./encode_url');
const relative_url = require('./relative_url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};

function urlForHelper(path = '/', options) {
if (path[0] === '#' || path.startsWith('//')) {
if (path.startsWith('#') || path.startsWith('//')) {
return path;
}

const { config } = this;
const { root } = config;
const data = urlObj(path);
const sitehost = parse(config.url).hostname || config.url;
const data = new URL(path, `http://${sitehost}`);

// Exit if this is an external link
if (data.origin !== 'null' && data.hostname !== sitehost) return path;

options = Object.assign({
relative: config.relative_link
}, options);

// Exit if this is an external path
if (typeof data === 'object') {
if (data.origin !== 'null') return path;
}

// Resolve relative url
if (options.relative) {
return relative_url(this.path, path);
Expand Down
4 changes: 3 additions & 1 deletion test/full_url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe('full_url_for', () => {
const ctx = {
config: {}
config: {
url: 'http://example.com'
}
};

const fullUrlFor = require('../lib/full_url_for').bind(ctx);
Expand Down
4 changes: 3 additions & 1 deletion test/url_for.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

describe('url_for', () => {
const ctx = {
config: {}
config: {
url: 'http://example.com'
}
};

const urlFor = require('../lib/url_for').bind(ctx);
Expand Down

0 comments on commit 71fce3f

Please sign in to comment.