Skip to content

Commit

Permalink
Merge pull request #63 from shgysk8zer0/bug/to-base64
Browse files Browse the repository at this point in the history
Fixed range error converting large `Uint8Array`s to base64
  • Loading branch information
shgysk8zer0 authored Jul 16, 2024
2 parents 1f2333c + a137457 commit 0c0fe79
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [v0.3.13] - 2024-07-16

### Fixed
- Fixed range error converting large `Uint8Array`s to base64

## [v0.3.12] - 2024-07-15

### Added
Expand Down
16 changes: 14 additions & 2 deletions array.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,27 @@ if (! (Array.isTemplateObject instanceof Function)) {
*/
if (! (Uint8Array.prototype.toHex instanceof Function)) {
Uint8Array.prototype.toHex = function toHex() {
return Array.from(this).map(n => n.toString(16).padStart('0', 2)).join('');
return Array.from(
this,
n => n.toString(16).padStart('0', 2)
).join('');
};
}

if (! (Uint8Array.prototype.toBase64 instanceof Function)) {
Uint8Array.prototype.toBase64 = function toBase64({ alphabet = 'base64' } = {}) {
if (alphabet === 'base64') {
// @todo Figure out encoding specifics
return btoa(String.fromCodePoint(...this));
//return btoa(String.fromCodePoint(...this));
const chunkSize = 0x8000; // 32,768 bytes per chunk
let str = '';

for (let i = 0; i < this.length; i += chunkSize) {
const chunk = this.subarray(i, i + chunkSize);
str += String.fromCharCode.apply(null, chunk);
}

return btoa(str);
// return btoa(new TextDecoder().decode(this));
} else if (alphabet === 'base64url') {
return this.toBase64({ alphabet: 'base64' }).replaceAll('+', '-').replaceAll('/', '_');
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shgysk8zer0/polyfills",
"version": "0.3.12",
"version": "0.3.13",
"private": false,
"type": "module",
"description": "A collection of JavaScript polyfills",
Expand Down

0 comments on commit 0c0fe79

Please sign in to comment.