10BC0
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 25a7bde commit 9af1e4bCopy full SHA for 9af1e4b
lib/string_decoder.js
@@ -210,8 +210,11 @@ function utf8Text(buf, i) {
210
// character.
211
function utf8End(buf) {
212
const r = (buf && buf.length ? this.write(buf) : '');
213
- if (this.lastNeed)
+ if (this.lastNeed) {
214
+ this.lastNeed = 0;
215
+ this.lastTotal = 0;
216
return r + '\ufffd';
217
+ }
218
return r;
219
}
220
@@ -246,6 +249,8 @@ function utf16End(buf) {
246
249
247
250
if (this.lastNeed) {
248
251
const end = this.lastTotal - this.lastNeed;
252
253
254
return r + this.lastChar.toString('utf16le', 0, end);
255
256
@@ -269,8 +274,12 @@ function base64Text(buf, i) {
269
274
270
275
function base64End(buf) {
271
276
272
273
- return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed);
277
278
+ const end = 3 - this.lastNeed;
279
280
281
+ return r + this.lastChar.toString('base64', 0, end);
282
283
284
285
test/parallel/test-string-decoder-end.js
@@ -39,6 +39,46 @@ for (let i = 1; i <= 16; i++) {
39
40
encodings.forEach(testEncoding);
41
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
62
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
69
70
+ Buffer.of(0x3D, 0xD8, 0x4D, 0xDC),
71
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
82
function testEncoding(encoding) {
83
bufs.forEach((buf) => {
84
testBuf(encoding, buf);
@@ -66,3 +106,14 @@ function testBuf(encoding, buf) {
106
assert.strictEqual(res1, res3, 'one byte at a time should match toString');
107
assert.strictEqual(res2, res3, 'all bytes at once should match toString');
108
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
117
118
+ assert.strictEqual(res, expected);
119
+}