Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/decode_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { parse, format } from 'url';
import { unescape } from 'querystring';

const decodeURL = (str: string) => {
if (parse(str).protocol) {
const index = str.indexOf(':');
if (index < 0) {
return unescape(str);
}
if (parse(str.slice(0, index + 1)).protocol) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

url.parse is only to determine if the given input is "somewhat" a valid URL (because try { new URL() } catch {} is extremely slow, so we want to avoid new URL() to throw at all cost).

const parsed = new URL(str);

// Exit if input is a data url
Expand Down
6 changes: 5 additions & 1 deletion lib/encode_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { parse, format } from 'url';
import { unescape } from 'querystring';

const encodeURL = (str: string) => {
if (parse(str).protocol) {
const index = str.indexOf(':');
if (index < 0) {
return encodeURI(unescape(str));
}
if (parse(str.slice(0, index + 1)).protocol) {
const parsed = new URL(str);

// Exit if input is a data url
Expand Down