Replace Buffer with Uint8Array, drop buffer dependency - #1
Merged
Conversation
Public API: Frame is now `Uint8Array | string` (was `Buffer | string`),
and emitted frames are `Uint8Array` instead of `Buffer`. `Buffer extends
Uint8Array`, so existing callers that pass Buffer continue to work, but
consumers that called Buffer-only methods (`readUInt8`, `toString('hex')`,
`writeInt32BE`, etc.) on emitted frames will need to migrate.
Internals: replace Buffer.alloc/from/concat/isBuffer/readUInt8/writeUInt8/
writeInt32BE/slice/copy/toString('hex') with Uint8Array equivalents.
Hex encoding and byte concatenation move to a small `utils/bytes` helper.
Drops the `buffer` npm dependency entirely so browser bundles no longer
need to ship the ~50 kB Node Buffer polyfill.
Bumps version to 0.3.0 and adds `lib: ["es2018", "dom"]` plus
`skipLibCheck` to tsconfig to pick up TextEncoder typing.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Arechii
force-pushed
the
arechi/drop-buffer
branch
from
April 30, 2026 08:44
32ef6c0 to
304762f
Compare
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
Bufferto browser-nativeUint8Array.Frameis nowUint8Array | stringand emitted frames areUint8Array. SinceBuffer extends Uint8Array, code that passed Buffer values keeps working, but consumers calling Buffer-only methods (readUInt8,toString('hex'),writeInt32BE, etc.) on emitted frames will need to migrate.buffernpm dependency entirely so browser bundles no longer ship the ~50 kB Node Buffer polyfill.src/utils/bytes.tshelper (concatBytes,encodeUtf8,bytesToHex) used bywebSocketEndpoint,sub/xsub/xpub, androuter.0.3.0, addlib: ["es2018", "dom"]andskipLibChecktotsconfig.jsonto pick upTextEncodertyping.Why
Browser consumers of
@blueyerobotics/jszmq(and downstream@blueyerobotics/blueye-ts) currently ship the ~50 kBbufferpolyfill on Vite to satisfyBuffer.from/concat/isBuffer. With Vite 8 the popularvite-plugin-node-polyfillsplugin emits a deprecation warning on every build (it still uses the oldesbuildconfig rather thanoxc) and is unmaintained on the Vite-8 transition. StrippingBufferfrom this library eliminates the need for any Node compat shim.Tracking issue: BluEye-Robotics/blueye-ts#6.
Notable changes
src/types.ts:Frame = Uint8Array | string;IEndpoint.routingKey: Uint8Array.src/webSocketEndpoint.ts:send()runtime check is nowframe instanceof Uint8Array;Buffer.alloc→new Uint8Array;readUInt8(0)→[0];slice(1)→subarray(1);Buffer.from(arrayBuffer)→new Uint8Array(arrayBuffer).src/router.ts: routing-key generation now usesDataView.setInt32(..., false)for the BE 32-bit ID; map keys usebytesToHexinstead ofBuffer.toString('hex').src/sub.ts,src/xsub.ts,src/xpub.ts,src/utils/trie.ts,src/utils/multiTrie.ts: allBuffer.*calls replaced withUint8Arrayequivalents.src/index.ts: dropexport {Buffer} from 'buffer'.Test plan
pnpm buildpassespnpm test— 5/5 specs pass (pubsub subscribe/unsubscribe, dealer-router ping-pong, reqrep simple + 100x stress)grep -rn "Buffer\." libreturns no matchesgrep -rn "require(\"buffer\")\|require('buffer')" libreturns no matches@blueyerobotics/blueye-tsagainst this build (will follow up in that repo's PR)Breaking changes
Frame = Uint8Array | stringinstead ofBuffer | string. Source-compatible for callers passing Buffer.Uint8Array(still Buffer instances at runtime when used in Node, since Buffer extends Uint8Array). Consumers calling Buffer-specific methods on received frames must switch to:frame.toString('utf8')→new TextDecoder().decode(frame)frame.readUInt8(0)→frame[0]frame.toString('hex')→ use a hex helper (orbytesToHexexported from this lib if we want to expose it)export {Buffer} from 'buffer'removed fromindex.ts. Consumers re-importing Buffer from this package should import it from'buffer'(or useUint8Arraydirectly).🤖 Generated with Claude Code