Skip to content

Commit 48b6d0e

Browse files
authored
Fix double-encoding of URLs (#182)
1 parent d542970 commit 48b6d0e

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ declare namespace open {
3333
readonly app?: string | readonly string[];
3434

3535
/**
36-
Uses `encodeURI` to encode the `target` before executing it.
36+
Uses `URL` to encode the `target` before executing it.
3737
3838
The use with targets that are not URLs is not recommended.
3939

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {promisify} = require('util');
33
const path = require('path');
44
const childProcess = require('child_process');
55
const fs = require('fs');
6+
const url = require('url');
67
const isWsl = require('is-wsl');
78
const isDocker = require('is-docker');
89

@@ -45,7 +46,7 @@ module.exports = async (target, options) => {
4546
// double-quotes through the “double-quotes on Windows caveat”, but it
4647
// can be used on any platform.
4748
if (options.url) {
48-
target = encodeURI(target);
49+
target = new url.URL(target).href;
4950

5051
if (isWsl) {
5152
target = target.replace(/&/g, '^&');

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ You may also pass in the app's full path. For example on WSL, this can be `/mnt/
9797
Type: `boolean`\
9898
Default: `false`
9999

100-
Uses `encodeURI` to encode the target before executing it.<br>
100+
Uses `URL` to encode the target before executing it.<br>
101101
We do not recommend using it on targets that are not URLs.
102102

103103
Especially useful when dealing with the [double-quotes on Windows](#double-quotes-on-windows) caveat.

test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ test('open URL with query strings, spaces, pipes and a fragment', async () => {
7878
await open('https://sindresorhus.com/?abc=123&def=456&ghi=w|i|t|h spaces#projects');
7979
});
8080

81+
test('open URL with query strings and URL reserved characters', async () => {
82+
await open('https://httpbin.org/get?amp=%26&colon=%3A&comma=%2C&commat=%40&dollar=%24&equals=%3D&plus=%2B&quest=%3F&semi=%3B&sol=%2F');
83+
});
84+
85+
test('open URL with query strings and URL reserved characters with `url` option', async () => {
86+
await open('https://httpbin.org/get?amp=%26&colon=%3A&comma=%2C&commat=%40&dollar=%24&equals=%3D&plus=%2B&quest=%3F&semi=%3B&sol=%2F', {url: true});
87+
});
88+
8189
if (isWsl) {
8290
test('open URL in specified Windows app given a WSL path to the app', async () => {
8391
await open('https://sindresorhus.com', {app: firefoxWslName});

0 commit comments

Comments
 (0)