Fix base64 handling and also support Buffer.#145
Conversation
* base64 encoding of large arrays no longer throws on any platform. * When toBase64 is unavailable, but Buffer is, this uses Buffer. * Also, we now accept Buffer for serialization and treat it like Uint8Array. * Also, when decoding, if Buffer is available, we decode to it. This is convenient for Node users. * Also fixed the stripping of base64 padding (trailing `=`). Fixes #126 Opencode+Opus: https://share.opencode.cloudflare.dev/share/VwnZjojz
🦋 Changeset detectedLatest commit: 23dcf0b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
| } | ||
| b64 = btoa(binary); | ||
| } | ||
| return ["bytes", b64.replace(/=+$/, "")]; |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
There was a problem hiding this comment.
This is a false positive. This regex will never backtrack, and the input is in any case guaranteed never to have more than two trailing =.
|
@jasnell I'm curious if you have an opinion on this. With this change, Cap'n Web will automatically deserialize byte arrays as Any concerns? |
|
The are some quirky differences with methods like slice but otherwise should be fine |
| } else if (typeof Buffer !== "undefined") { | ||
| let buf = bytes instanceof Buffer ? bytes | ||
| : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength); | ||
| b64 = buf.toString("base64"); |
There was a problem hiding this comment.
Plain base64 is correct. This is for encoding in JSON strings, no need to worry about + or /.
| } | ||
| b64 = btoa(binary); | ||
| } | ||
| return ["bytes", b64.replace(/=+$/, "")]; |
There was a problem hiding this comment.
This stripping is redundant when we used toBase64() since we set omitPadding: true, I should skip it in that case.
Also add a test verifying padding-stripping.
=).Fixes #126
Opencode+Opus: https://share.opencode.cloudflare.dev/share/VwnZjojz