Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3d26822

Browse files
refactor(): use TextEncoder when available to have better browser support
1 parent a1c0b51 commit 3d26822

File tree

5 files changed

+176
-69
lines changed

5 files changed

+176
-69
lines changed

‎CHANGELOG.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
3.0.8:
2+
date: 2025年08月28日
3+
refactor:
4+
- Refactor `percent-encode.js` to not use Buffer.from(), which is not available in the browser
5+
16
3.0.7:
27
date: 2025年03月27日
38
fixed bugs:

‎encoder/percent-encode.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ function isPreEncodedCharacter (byte) {
4141
(byte >= 0x61 && byte <= 0x66); // a-f
4242
}
4343

44+
/**
45+
* Converts a string to a UTF-8 byte array.
46+
*
47+
* @private
48+
* @param {String} value The string to convert.
49+
* @returns {Array<Number>} The UTF-8 byte array.
50+
*/
51+
function stringToBytes (value) {
52+
// Use TextEncoder if available, which is the case in modern browsers and Node.js v11+.
53+
if (typeof TextEncoder === 'function') {
54+
return new TextEncoder().encode(value);
55+
}
56+
57+
// Fallback for Node.js v10 and older environments.
58+
return Buffer.from(value);
59+
}
60+
4461
/**
4562
* Checks if character at given index in the buffer is already percent encoded or not.
4663
*
@@ -91,7 +108,7 @@ function encode (value, encodeSet) {
91108
ii,
92109
charCode,
93110
encoded = E,
94-
buffer = Buffer.from(value);
111+
buffer = stringToBytes(value);
95112

96113
for (i = 0, ii = buffer.length; i < ii; ++i) {
97114
// avoid double encoding

‎package-lock.json‎

Lines changed: 30 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postman-url-encoder",
3-
"version": "3.0.7",
3+
"version": "3.0.8",
44
"description": "Implementation of the WHATWG URL Standard",
55
"author": "Postman Inc.",
66
"license": "Apache-2.0",

‎test/unit/encode.test.js‎

Lines changed: 122 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,140 @@
11
const expect = require('chai').expect,
2-
32
encode = require('../../').encode,
43
percentEncodeCharCode = require('../../encoder').percentEncodeCharCode;
54

65
describe('.encode', function () {
7-
it('should percent-encode C0 control codes', function () {
8-
var i,
9-
char;
6+
describe('with TextEncoder', function () {
7+
it('should percent-encode C0 control codes', function () {
8+
var i,
9+
char;
1010

11-
for (i = 0; i < 32; i++) {
12-
char = String.fromCharCode(i);
13-
expect(encode(char)).to.equal(percentEncodeCharCode(i));
14-
}
11+
for (i = 0; i < 32; i++) {
12+
char = String.fromCharCode(i);
13+
expect(encode(char)).to.equal(percentEncodeCharCode(i));
14+
}
1515

16-
char = String.fromCharCode(127);
17-
expect(encode(char)).to.equal(percentEncodeCharCode(127));
18-
});
16+
char = String.fromCharCode(127);
17+
expect(encode(char)).to.equal(percentEncodeCharCode(127));
18+
});
1919

20-
it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () {
21-
var i,
22-
char,
23-
encoded,
24-
chars = [],
25-
expected = [' ', '"', '#', '\'', '<', '>'];
20+
it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () {
21+
var i,
22+
char,
23+
encoded,
24+
chars = [],
25+
expected = [' ', '"', '#', '\'', '<', '>'];
2626

27-
for (i = 32; i < 127; i++) {
28-
char = String.fromCharCode(i);
29-
encoded = encode(char);
27+
for (i = 32; i < 127; i++) {
28+
char = String.fromCharCode(i);
29+
encoded = encode(char);
3030

31-
if (char !== encoded) {
32-
chars.push(char);
33-
expect(encoded).to.equal(percentEncodeCharCode(i));
31+
if (char !== encoded) {
32+
chars.push(char);
33+
expect(encoded).to.equal(percentEncodeCharCode(i));
34+
}
3435
}
35-
}
3636

37-
expect(chars).to.eql(expected);
38-
});
37+
expect(chars).to.eql(expected);
38+
});
3939

40-
it('should percent-encode unicode characters', function () {
41-
expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9');
42-
});
40+
it('should percent-encode unicode characters', function () {
41+
expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9');
42+
});
43+
44+
it('should percent-encode 4-byte unicode characters', function () {
45+
expect(encode('𝌆')).to.eql('%F0%9D%8C%86');
46+
});
4347

44-
it('should not double encode characters', function () {
45-
expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA');
48+
it('should handle unpaired surrogates', function () {
49+
expect(encode('\uD800')).to.eql('%EF%BF%BD');
50+
});
51+
52+
it('should not double encode characters', function () {
53+
expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA');
54+
});
55+
56+
it('should return empty string on invalid input types', function () {
57+
expect(encode()).to.equal('');
58+
expect(encode(null)).to.equal('');
59+
expect(encode(undefined)).to.equal('');
60+
expect(encode(NaN)).to.equal('');
61+
expect(encode(true)).to.equal('');
62+
expect(encode(1234)).to.equal('');
63+
expect(encode(Function)).to.equal('');
64+
expect(encode(['key', 'value'])).to.equal('');
65+
});
4666
});
4767

48-
it('should return empty string on invalid input types', function () {
49-
expect(encode()).to.equal('');
50-
expect(encode(null)).to.equal('');
51-
expect(encode(undefined)).to.equal('');
52-
expect(encode(NaN)).to.equal('');
53-
expect(encode(true)).to.equal('');
54-
expect(encode(1234)).to.equal('');
55-
expect(encode(Function)).to.equal('');
56-
expect(encode(['key', 'value'])).to.equal('');
68+
describe('without TextEncoder', function () {
69+
let oldTextEncoder;
70+
71+
before(function () {
72+
oldTextEncoder = global.TextEncoder;
73+
global.TextEncoder = undefined;
74+
});
75+
76+
after(function () {
77+
global.TextEncoder = oldTextEncoder;
78+
});
79+
80+
it('should percent-encode C0 control codes', function () {
81+
var i,
82+
char;
83+
84+
for (i = 0; i < 32; i++) {
85+
char = String.fromCharCode(i);
86+
expect(encode(char)).to.equal(percentEncodeCharCode(i));
87+
}
88+
89+
char = String.fromCharCode(127);
90+
expect(encode(char)).to.equal(percentEncodeCharCode(127));
91+
});
92+
93+
it('should percent-encode SPACE, ("), (#), (\'), (<), and (>)', function () {
94+
var i,
95+
char,
96+
encoded,
97+
chars = [],
98+
expected = [' ', '"', '#', '\'', '<', '>'];
99+
100+
for (i = 32; i < 127; i++) {
101+
char = String.fromCharCode(i);
102+
encoded = encode(char);
103+
104+
if (char !== encoded) {
105+
chars.push(char);
106+
expect(encoded).to.equal(percentEncodeCharCode(i));
107+
}
108+
}
109+
110+
expect(chars).to.eql(expected);
111+
});
112+
113+
it('should percent-encode unicode characters', function () {
114+
expect(encode('𝌆й你ス')).to.eql('%F0%9D%8C%86%D0%B9%E4%BD%A0%E3%82%B9');
115+
});
116+
117+
it('should percent-encode 4-byte unicode characters', function () {
118+
expect(encode('𝌆')).to.eql('%F0%9D%8C%86');
119+
});
120+
121+
it('should handle unpaired surrogates', function () {
122+
expect(encode('\uD800')).to.eql('%EF%BF%BD');
123+
});
124+
125+
it('should not double encode characters', function () {
126+
expect(encode('key:%F0%9F%8D%AA')).to.equal('key:%F0%9F%8D%AA');
127+
});
128+
129+
it('should return empty string on invalid input types', function () {
130+
expect(encode()).to.equal('');
131+
expect(encode(null)).to.equal('');
132+
expect(encode(undefined)).to.equal('');
133+
expect(encode(NaN)).to.equal('');
134+
expect(encode(true)).to.equal('');
135+
expect(encode(1234)).to.equal('');
136+
expect(encode(Function)).to.equal('');
137+
expect(encode(['key', 'value'])).to.equal('');
138+
});
57139
});
58140
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /