feat: eliminate Buffer polyfill for browser builds#384
Open
MathiasWP wants to merge 1 commit intopillarjs:masterfrom
Open
feat: eliminate Buffer polyfill for browser builds#384MathiasWP wants to merge 1 commit intopillarjs:masterfrom
MathiasWP wants to merge 1 commit intopillarjs:masterfrom
Conversation
Add minimal browser-specific shims for `safer-buffer` and `string_decoder` that use native web APIs (TextEncoder/TextDecoder, Uint8Array, atob/btoa), reducing browser bundle size by ~180KB gzipped. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
lib/browser-buffer.jsandlib/browser-string-decoder.js) that replacesafer-bufferandstring_decoderin browser bundlespackage.json"browser"field so bundlers (webpack, browserify, etc.) automatically substitute them — zero changes to any existing codec fileBufferpolyfillHow it works
lib/browser-buffer.jsis a minimalBufferShimclass that extendsUint8Arrayand implements only the Buffer methods iconv-lite actually uses, built on native browser APIs:TextEncoder/TextDecoderfor UTF-8atob()/btoa()for base64String.fromCharCode()for ascii/binary/ucs2readUInt16LE,writeUInt32LE/BElib/browser-string-decoder.jsis a minimalStringDecoderreplacement usingTextDecoderwith{stream: true}for proper chunk boundary handling.Since
BufferShim extends Uint8Array, the return value ofencode()is a standardUint8Arrayin browsers — no polyfills needed, fully compatible with all web APIs (fetch, WebSocket, FileReader, etc.).What didn't change
sbcs-codec.js,dbcs-codec.js,utf16.js,utf32.js,utf7.js,internal.js)lib/index.js,lib/bom-handling.js, or encoding data filesBrowser compatibility
Requires ES2015+ (
class extends Uint8Array), which covers all modern browsers: Chrome 58+, Firefox 52+, Safari 10.1+, Edge 15+.Test plan
npm test)npm run test:webpack) — could not run locally due to Puppeteer/arm64 incompatibility, needs CI verification🤖 Generated with Claude Code