[canvaskit] Fix conversion of large images to base64

Bug: skia:
Change-Id: I8ab40a382557ae6bd13884ddce027e51ce1fb21f
Reviewed-on: https://skia-review.googlesource.com/c/173701
Reviewed-by: Kevin Lubick <kjlubick@google.com>
This commit is contained in:
Kevin Lubick 2018-11-30 13:23:21 -05:00
parent ac7f23c807
commit dee08d8f65

View File

@ -17,7 +17,21 @@
if (isNode) {
return Buffer.from(bytes).toString('base64');
} else {
return btoa(String.fromCharCode.apply(null, bytes));
// From https://stackoverflow.com/a/25644409
// because the naive solution of
// btoa(String.fromCharCode.apply(null, bytes));
// would occasionally throw "Maximum call stack size exceeded"
var CHUNK_SIZE = 0x8000; //arbitrary number
var index = 0;
var length = bytes.length;
var result = '';
var slice;
while (index < length) {
slice = bytes.slice(index, Math.min(index + CHUNK_SIZE, length));
result += String.fromCharCode.apply(null, slice);
index += CHUNK_SIZE;
}
return btoa(result);
}
}