Skip to content

Replace Buffer with Uint8Array, drop buffer dependency - #1

Merged
Arechii merged 1 commit into
mainfrom
arechi/drop-buffer
Apr 30, 2026
Merged

Replace Buffer with Uint8Array, drop buffer dependency#1
Arechii merged 1 commit into
mainfrom
arechi/drop-buffer

Conversation

@Arechii

@Arechii Arechii commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Migrate the public API and internals from Node Buffer to browser-native Uint8Array. Frame is now Uint8Array | string and emitted frames are Uint8Array. Since Buffer 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.
  • Drop the buffer npm dependency entirely so browser bundles no longer ship the ~50 kB Node Buffer polyfill.
  • Add a small src/utils/bytes.ts helper (concatBytes, encodeUtf8, bytesToHex) used by webSocketEndpoint, sub/xsub/xpub, and router.
  • Bump version to 0.3.0, add lib: ["es2018", "dom"] and skipLibCheck to tsconfig.json to pick up TextEncoder typing.

Why

Browser consumers of @blueyerobotics/jszmq (and downstream @blueyerobotics/blueye-ts) currently ship the ~50 kB buffer polyfill on Vite to satisfy Buffer.from/concat/isBuffer. With Vite 8 the popular vite-plugin-node-polyfills plugin emits a deprecation warning on every build (it still uses the old esbuild config rather than oxc) and is unmaintained on the Vite-8 transition. Stripping Buffer from 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 now frame instanceof Uint8Array; Buffer.allocnew Uint8Array; readUInt8(0)[0]; slice(1)subarray(1); Buffer.from(arrayBuffer)new Uint8Array(arrayBuffer).
  • src/router.ts: routing-key generation now uses DataView.setInt32(..., false) for the BE 32-bit ID; map keys use bytesToHex instead of Buffer.toString('hex').
  • src/sub.ts, src/xsub.ts, src/xpub.ts, src/utils/trie.ts, src/utils/multiTrie.ts: all Buffer.* calls replaced with Uint8Array equivalents.
  • src/index.ts: drop export {Buffer} from 'buffer'.

Test plan

  • pnpm build passes
  • pnpm test — 5/5 specs pass (pubsub subscribe/unsubscribe, dealer-router ping-pong, reqrep simple + 100x stress)
  • grep -rn "Buffer\." lib returns no matches
  • grep -rn "require(\"buffer\")\|require('buffer')" lib returns no matches
  • Smoke-test downstream @blueyerobotics/blueye-ts against this build (will follow up in that repo's PR)

Breaking changes

  • Public type widening: Frame = Uint8Array | string instead of Buffer | string. Source-compatible for callers passing Buffer.
  • Emitted frames are typed as 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 (or bytesToHex exported from this lib if we want to expose it)
  • export {Buffer} from 'buffer' removed from index.ts. Consumers re-importing Buffer from this package should import it from 'buffer' (or use Uint8Array directly).

🤖 Generated with Claude Code

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
Arechii force-pushed the arechi/drop-buffer branch from 32ef6c0 to 304762f Compare April 30, 2026 08:44
@Arechii
Arechii merged commit 3439d08 into main Apr 30, 2026
1 check passed
@Arechii
Arechii deleted the arechi/drop-buffer branch April 30, 2026 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant