Skip to content

Commit 66d149e

Browse files
committed
Ported utf8 chunking mechanism to base64 as well, fixes #800
1 parent e1f9d98 commit 66d149e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

lib/base64/index.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,47 @@ for (var i = 0; i < 64;)
4040
* @returns {string} Base64 encoded string
4141
*/
4242
base64.encode = function encode(buffer, start, end) {
43-
var string = []; // alt: new Array(Math.ceil((end - start) / 3) * 4);
43+
var parts = null,
44+
chunk = [];
4445
var i = 0, // output index
4546
j = 0, // goto index
4647
t; // temporary
4748
while (start < end) {
4849
var b = buffer[start++];
4950
switch (j) {
5051
case 0:
51-
string[i++] = b64[b >> 2];
52+
chunk[i++] = b64[b >> 2];
5253
t = (b & 3) << 4;
5354
j = 1;
5455
break;
5556
case 1:
56-
string[i++] = b64[t | b >> 4];
57+
chunk[i++] = b64[t | b >> 4];
5758
t = (b & 15) << 2;
5859
j = 2;
5960
break;
6061
case 2:
61-
string[i++] = b64[t | b >> 6];
62-
string[i++] = b64[b & 63];
62+
chunk[i++] = b64[t | b >> 6];
63+
chunk[i++] = b64[b & 63];
6364
j = 0;
6465
break;
6566
}
67+
if (i > 8191) {
68+
(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));
69+
i = 0;
70+
}
6671
}
6772
if (j) {
68-
string[i++] = b64[t];
69-
string[i ] = 61;
73+
chunk[i++] = b64[t];
74+
chunk[i++] = 61;
7075
if (j === 1)
71-
string[i + 1] = 61;
76+
chunk[i++] = 61;
77+
}
78+
if (parts) {
79+
if (i)
80+
parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
81+
return parts.join("");
7282
}
73-
return String.fromCharCode.apply(String, string);
83+
return String.fromCharCode.apply(String, chunk.slice(0, i));
7484
};
7585

7686
var invalidEncoding = "invalid encoding";

lib/base64/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@protobufjs/base64",
33
"description": "A minimal base64 implementation for number arrays.",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"author": "Daniel Wirtz <[email protected]>",
66
"repository": {
77
"type": "git",

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"dependencies": {
4747
"long": "^3.2.0",
4848
"@protobufjs/aspromise": "^1.1.2",
49-
"@protobufjs/base64": "^1.1.1",
49+
"@protobufjs/base64": "^1.1.2",
5050
"@protobufjs/codegen": "^2.0.3",
5151
"@protobufjs/eventemitter": "^1.1.0",
5252
"@protobufjs/fetch": "^1.1.0",

0 commit comments

Comments
 (0)