capacitor-filesharer
capacitor-filesharer copied to clipboard
Bug: RangeError: Invalid array length (and fix)
Affected Platform(s):
- Web (Chrome)
Current Behavior
When sharing a 200MB ZIP-File Chrome fails with RangeError: Invalid array length at WebUtils.toByteArray (web-utils.js:7:27)
. This seems to work on iOS and macOS Safari without problems.
I don't know if this is a memory issue as the same code is working with smaller files without problems even on Chrome, but I found a simple fix:
Fix
If I replace the following code
static toByteArray(base64Data) {
const byteCharacters = atob(base64Data);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
return new Uint8Array(byteNumbers);
}
with
static toByteArray(base64Data) {
const byteCharacters = atob(base64Data);
const byteNumbers = new Uint8Array(new ArrayBuffer(byteCharacters.length));
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
return byteNumbers;
}
sharing just works fine.
Note: The following one-liner (which seems even more straight forward) makes Chrome crash due to memory issues:
return Uint8Array.from(atob(base64Data), (v) => v.charCodeAt(0));