Skip to content

Commit f96b0bf

Browse files
jridgewellMylesBorins
authored andcommitted
string_decoder: reset decoder on end
This resets the StringDecoder's state after calling `#end`. Further writes to the decoder will act as if it were a brand new instance, allowing simple reuse. PR-URL: #18494 Fixes: #16564 Refs: #16594 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent e5d5137 commit f96b0bf

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

lib/string_decoder.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ function utf8Text(buf, i) {
209209
// character.
210210
function utf8End(buf) {
211211
const r = (buf && buf.length ? this.write(buf) : '');
212-
if (this.lastNeed)
212+
if (this.lastNeed) {
213+
this.lastNeed = 0;
214+
this.lastTotal = 0;
213215
return r + '\ufffd';
216+
}
214217
return r;
215218
}
216219

@@ -245,6 +248,8 @@ function utf16End(buf) {
245248
const r = (buf && buf.length ? this.write(buf) : '');
246249
if (this.lastNeed) {
247250
const end = this.lastTotal - this.lastNeed;
251+
this.lastNeed = 0;
252+
this.lastTotal = 0;
248253
return r + this.lastChar.toString('utf16le', 0, end);
249254
}
250255
return r;
@@ -268,8 +273,12 @@ function base64Text(buf, i) {
268273

269274
function base64End(buf) {
270275
const r = (buf && buf.length ? this.write(buf) : '');
271-
if (this.lastNeed)
272-
return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
276+
if (this.lastNeed) {
277+
const end = 3 - this.lastNeed;
278+
this.lastNeed = 0;
279+
this.lastTotal = 0;
280+
return r + this.lastChar.toString('base64', 0, end);
281+
}
273282
return r;
274283
}
275284

test/parallel/test-string-decoder-end.js

+51
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,46 @@ for (let i = 1; i <= 16; i++) {
3939

4040
encodings.forEach(testEncoding);
4141

42+
testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x61), '\uFFFDa');
43+
testEnd('utf8', Buffer.of(0xE2), Buffer.of(0x82), '\uFFFD\uFFFD');
44+
testEnd('utf8', Buffer.of(0xE2), Buffer.of(0xE2), '\uFFFD\uFFFD');
45+
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0x61), '\uFFFDa');
46+
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xAC), '\uFFFD\uFFFD');
47+
testEnd('utf8', Buffer.of(0xE2, 0x82), Buffer.of(0xE2), '\uFFFD\uFFFD');
48+
testEnd('utf8', Buffer.of(0xE2, 0x82, 0xAC), Buffer.of(0x61), '€a');
49+
50+
testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0x61, 0x00), 'a');
51+
testEnd('utf16le', Buffer.of(0x3D), Buffer.of(0xD8, 0x4D, 0xDC), '\u4DD8');
52+
testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(), '\uD83D');
53+
testEnd('utf16le', Buffer.of(0x3D, 0xD8), Buffer.of(0x61, 0x00), '\uD83Da');
54+
testEnd(
55+
'utf16le',
56+
Buffer.of(0x3D, 0xD8),
57+
Buffer.of(0x4D, 0xDC),
58+
'\uD83D\uDC4D'
59+
);
60+
testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(), '\uD83D');
61+
testEnd(
62+
'utf16le',
63+
Buffer.of(0x3D, 0xD8, 0x4D),
64+
Buffer.of(0x61, 0x00),
65+
'\uD83Da'
66+
);
67+
testEnd('utf16le', Buffer.of(0x3D, 0xD8, 0x4D), Buffer.of(0xDC), '\uD83D');
68+
testEnd(
69+
'utf16le',
70+
Buffer.of(0x3D, 0xD8, 0x4D, 0xDC),
71+
Buffer.of(0x61, 0x00),
72+
'👍a'
73+
);
74+
75+
testEnd('base64', Buffer.of(0x61), Buffer.of(), 'YQ==');
76+
testEnd('base64', Buffer.of(0x61), Buffer.of(0x61), 'YQ==YQ==');
77+
testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(), 'YWE=');
78+
testEnd('base64', Buffer.of(0x61, 0x61), Buffer.of(0x61), 'YWE=YQ==');
79+
testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(), 'YWFh');
80+
testEnd('base64', Buffer.of(0x61, 0x61, 0x61), Buffer.of(0x61), 'YWFhYQ==');
81+
4282
function testEncoding(encoding) {
4383
bufs.forEach((buf) => {
4484
testBuf(encoding, buf);
@@ -66,3 +106,14 @@ function testBuf(encoding, buf) {
66106
assert.strictEqual(res1, res3, 'one byte at a time should match toString');
67107
assert.strictEqual(res2, res3, 'all bytes at once should match toString');
68108
}
109+
110+
function testEnd(encoding, incomplete, next, expected) {
111+
let res = '';
112+
const s = new SD(encoding);
113+
res += s.write(incomplete);
114+
res += s.end();
115+
res += s.write(next);
116+
res += s.end();
117+
118+
assert.strictEqual(res, expected);
119+
}

0 commit comments

Comments
 (0)