Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(encode/decodeURL): use legacy url.parse() to avoid try...catch() #128

Merged
merged 3 commits into from
Nov 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions lib/decode_url.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
'use strict';

const { URL } = require('url');
const { parse, URL } = require('url');
const { toUnicode } = require('./punycode');

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

const safeDecodeURI = (str) => {
try {
return decodeURI(str);
Expand All @@ -20,8 +12,10 @@ const safeDecodeURI = (str) => {
};

const decodeURL = (str) => {
const parsed = urlObj(str);
if (typeof parsed === 'object') {
if (parse(str).protocol) {
const parsed = new URL(str);

// Exit if input is a data url
if (parsed.origin === 'null') return str;

// TODO: refactor to `url.format()` once Node 8 is dropped
Expand Down
16 changes: 5 additions & 11 deletions lib/encode_url.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
'use strict';

const { toUnicode } = require('./punycode');
const { URL } = require('url');

const urlObj = (str) => {
try {
return new URL(str);
} catch (err) {
return str;
}
};
const { parse, URL } = require('url');

const safeDecodeURI = (str) => {
try {
Expand All @@ -20,8 +12,10 @@ const safeDecodeURI = (str) => {
};

const encodeURL = (str) => {
const parsed = urlObj(str);
if (typeof parsed === 'object') {
if (parse(str).protocol) {
const parsed = new URL(str);

// Exit if input is a data url
if (parsed.origin === 'null') return str;

parsed.search = encodeURI(safeDecodeURI(parsed.search));
Expand Down