Skip to content

Commit 392a0cb

Browse files
committed
Use punycode on encoder.encodeHost for browser
1 parent 6a32e19 commit 392a0cb

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

CHANGELOG.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
unreleased:
2+
fixed bugs:
3+
- Fixed a bug where `encoder.encodeHost` throws in the browser
4+
15
3.0.4:
26
date: 2021-08-09
37
fixed bugs:

encoder/index.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@
4444
* @see {@link https://tools.ietf.org/html/rfc3986#section-3}
4545
*/
4646

47-
const domainToASCII = require('url').domainToASCII,
48-
49-
encodeSet = require('./encode-set'),
47+
const encodeSet = require('./encode-set'),
5048

5149
_percentEncode = require('./percent-encode').encode,
5250
_percentEncodeCharCode = require('./percent-encode').encodeCharCode,
@@ -69,7 +67,30 @@ const domainToASCII = require('url').domainToASCII,
6967
OBJECT = 'object',
7068

7169
PATH_SEPARATOR = '/',
72-
DOMAIN_SEPARATOR = '.';
70+
DOMAIN_SEPARATOR = '.',
71+
72+
/**
73+
* Returns the Punycode ASCII serialization of the domain.
74+
*
75+
* @private
76+
* @function
77+
* @param {String} domain domain name
78+
* @returns {String} punycode encoded domain name
79+
*/
80+
domainToASCII = (function () {
81+
// @note `url.domainToASCII` returns empty string for invalid domain.
82+
const domainToASCII = require('url').domainToASCII;
83+
84+
// use faster native `url` method in Node.js
85+
/* istanbul ignore next */
86+
if (typeof domainToASCII === 'function') {
87+
return domainToASCII;
88+
}
89+
90+
// else, lazy load `punycode` dependency in browser
91+
/* istanbul ignore next */
92+
return require('punycode').toASCII;
93+
}());
7394

7495
/**
7596
* Returns the Punycode ASCII serialization of the domain.

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
"test-system": "node npm/test-system.js",
3131
"test-unit": "nyc --nycrc-path=.nycrc.js node npm/test-unit.js"
3232
},
33+
"dependencies": {
34+
"punycode": "^2.1.1"
35+
},
3336
"devDependencies": {
3437
"@postman/csv-parse": "^4.0.2",
3538
"@postman/shipit": "^0.3.0",

0 commit comments

Comments
 (0)