Skip to content
sovereignbasePublic

About

Runtime-agnostic TypeScript byte codecs for Base45, Base64, UTF-8, gzip, derivation, generation, normalization, concatenation, and comparison.

Topics

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

npm version JSR version CI codecov license

bytecodec

Runtime-agnostic TypeScript byte codecs for Base45, Base64, UTF-8, gzip, HKDF derivation, secure random generation, normalization, concatenation, and comparison.

Contents

Compatibility

  • Runtimes: Node.js, Bun, Deno, modern browsers, Cloudflare Workers, and edge runtimes.
  • Formats: tree-shakeable ESM and CommonJS.
  • TypeScript: strict bundled declarations for the root and every subpath.
  • Dependencies: no runtime dependencies or bundler shims.
  • Gzip: node:zlib in Node-like runtimes; CompressionStream and DecompressionStream elsewhere.

Bytes API

Declarative Bytes API

import { Bytes } from '@sovereignbase/bytecodec'

Bytes.base45.encode(bytes)
Bytes.base45.decode(base45String)

Bytes.base64.encode(bytes)
Bytes.base64.decode(base64String)

Bytes.base64url.encode(bytes)
Bytes.base64url.decode(base64UrlString)

Bytes.utf8.encode(bytes)
Bytes.utf8.decode(text)

await Bytes.gzip.encode(bytes)
await Bytes.gzip.decode(compressedBytes)

Bytes.normalize(bytes)
Bytes.concat([first, second])
Bytes.equals(first, second)
await Bytes.derive(base, domain, byteLength)
Bytes.generate(byteLength)

Codec directions are consistent: encode converts bytes into the codec representation and decode converts that representation back into bytes.

The root also exports BytecodecError for instanceof checks and the ByteSource and BytecodecErrorCode types. Validation errors expose a stable code; unexpected wrapped failures use UNKNOWN_ERROR and retain the original cause.

Base45

Usage

import {
  bytesFromBase45String,
  bytesToBase45String,
} from '@sovereignbase/bytecodec/base45'

const encoded = bytesToBase45String(new Uint8Array([65, 66])) // "BB8"
const decoded = bytesFromBase45String(encoded)

Base45 follows RFC 9285 and is useful for QR-friendly payloads.

Errors

Invalid text throws BASE45_INPUT_EXPECTED, BASE45_INVALID_LENGTH, BASE45_INVALID_CHARACTER or BASE45_INVALID_CHUNK.

Benchmarks

2026-11-19T15:03:24.730Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

AMD EPYC 9V45 96-Core Processor · 1,000 measured calls per case after warmup

Operation Input bytes Ops/sec
encode 64 430,720
decode 64 918,324
encode 1024 112,595
decode 1024 318,757
encode 16384 9,029
decode 16384 19,355

Tests

2026-11-19T15:03:08.357Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

Suite Passed Failed Skipped
base45 (2 files) 35 0 0
Coverage statements branches functions lines
Percent 100% 100% 100% 100%

Compatibility

The shared compatibility suite exercises the whole public API.

Runtime / browser Version Status Passed Failed
node esm 24.22.3 passed 9 0
node cjs 24.22.3 passed 9 0
bun esm 1.4.5 passed 9 0
bun cjs 1.4.5 passed 9 0
deno esm 2.9.8 passed 9 0
edge-runtime esm 4.0.1 passed 9 0
cloudflare-workers esm 1.20261118.1 passed 9 0
chromium 159.0.8154.6 passed 1 0
firefox 159.0 passed 1 0
webkit 27.2 passed 1 0
mobile-chromium 159.0.8154.6 passed 1 0
mobile-firefox 159.0 passed 1 0
mobile-webkit 27.2 passed 1 0

Base64 and Base64URL

Usage

import {
  bytesFromBase64String,
  bytesFromBase64UrlString,
  bytesToBase64String,
  bytesToBase64UrlString,
} from '@sovereignbase/bytecodec/base64'

const bytes = new Uint8Array([104, 101, 108, 108, 111])
bytesToBase64String(bytes) // "aGVsbG8="
bytesToBase64UrlString(bytes) // "aGVsbG8" (unpadded)
bytesFromBase64String('aGVsbG8=')
bytesFromBase64UrlString('aGVsbG8')

Errors

Unavailable runtime APIs throw BASE64_ENCODER_UNAVAILABLE or BASE64_DECODER_UNAVAILABLE. Base64URL length modulo four must not equal one (BASE64URL_INVALID_LENGTH). Other parsing behavior follows the runtime Base64 decoder.

Benchmarks

2026-11-19T15:03:24.821Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

AMD EPYC 9V45 96-Core Processor · 1,000 measured calls per case after warmup

Operation Input bytes Ops/sec
base64 encode 64 760,873
base64 decode 64 1,290,061
base64url encode 64 317,789
base64url decode 64 1,855,350
base64 encode 1024 848,627
base64 decode 1024 870,762
base64url encode 1024 357,317
base64url decode 1024 418,324
base64 encode 16384 82,440
base64 decode 16384 148,189
base64url encode 16384 43,937
base64url decode 16384 91,285

Tests

2026-11-19T15:03:08.357Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

Suite Passed Failed Skipped
base64 (2 files) 34 0 0
Coverage statements branches functions lines
Percent 100% 100% 100% 100%

Compatibility

The shared compatibility suite exercises the whole public API.

Runtime / browser Version Status Passed Failed
node esm 24.22.3 passed 9 0
node cjs 24.22.3 passed 9 0
bun esm 1.4.5 passed 9 0
bun cjs 1.4.5 passed 9 0
deno esm 2.9.8 passed 9 0
edge-runtime esm 4.0.1 passed 9 0
cloudflare-workers esm 1.20261118.1 passed 9 0
chromium 159.0.8154.6 passed 1 0
firefox 159.0 passed 1 0
webkit 27.2 passed 1 0
mobile-chromium 159.0.8154.6 passed 1 0
mobile-firefox 159.0 passed 1 0
mobile-webkit 27.2 passed 1 0

UTF-8

Usage

import {
  bytesFromUTF8String,
  bytesToUTF8String,
} from '@sovereignbase/bytecodec/utf8'

const bytes = bytesFromUTF8String('café ✓ 🚀')
const text = bytesToUTF8String(bytes)

Errors

Non-string text input throws STRING_INPUT_EXPECTED. Missing codecs throw UTF8_ENCODER_UNAVAILABLE or UTF8_DECODER_UNAVAILABLE.

Benchmarks

2026-11-19T15:03:24.848Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

AMD EPYC 9V45 96-Core Processor · 1,000 measured calls per case after warmup

Operation Input bytes Ops/sec
encode (bytes → string) 64 1,546,927
decode (string → bytes) 64 2,618,521
encode (bytes → string) 1024 798,272
decode (string → bytes) 1024 1,206,290
encode (bytes → string) 16384 157,095
decode (string → bytes) 16384 125,942

Tests

2026-11-19T15:03:08.357Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

Suite Passed Failed Skipped
utf8 (2 files) 32 0 0
Coverage statements branches functions lines
Percent 100% 100% 100% 100%

Compatibility

The shared compatibility suite exercises the whole public API.

Runtime / browser Version Status Passed Failed
node esm 24.22.3 passed 9 0
node cjs 24.22.3 passed 9 0
bun esm 1.4.5 passed 9 0
bun cjs 1.4.5 passed 9 0
deno esm 2.9.8 passed 9 0
edge-runtime esm 4.0.1 passed 9 0
cloudflare-workers esm 1.20261118.1 passed 9 0
chromium 159.0.8154.6 passed 1 0
firefox 159.0 passed 1 0
webkit 27.2 passed 1 0
mobile-chromium 159.0.8154.6 passed 1 0
mobile-firefox 159.0 passed 1 0
mobile-webkit 27.2 passed 1 0

Gzip

Usage

import {
  bytesFromGzipBytes,
  bytesToGzipBytes,
} from '@sovereignbase/bytecodec/gzip'

const compressed = await bytesToGzipBytes(new Uint8Array([1, 2, 3]))
const restored = await bytesFromGzipBytes(compressed)

Errors

Missing compression APIs throw GZIP_COMPRESSION_UNAVAILABLE or GZIP_DECOMPRESSION_UNAVAILABLE. Node zlib failures throw UNKNOWN_ERROR with the original cause. Web stream processing errors can reject with the original runtime error.

Benchmarks

2026-11-19T15:03:25.390Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

AMD EPYC 9V45 96-Core Processor · 1,000 measured calls per case after warmup

Operation Input bytes Ops/sec
compress 64 11,970
decompress 64 15,505
compress 1024 14,038
decompress 1024 15,596
compress 16384 10,801
decompress 16384 10,648

Tests

2026-11-19T15:03:08.357Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

Suite Passed Failed Skipped
gzip (1 files) 5 0 0
Coverage statements branches functions lines
Percent 100% 100% 100% 100%

Compatibility

The shared compatibility suite exercises the whole public API.

Runtime / browser Version Status Passed Failed
node esm 24.22.3 passed 9 0
node cjs 24.22.3 passed 9 0
bun esm 1.4.5 passed 9 0
bun cjs 1.4.5 passed 9 0
deno esm 2.9.8 passed 9 0
edge-runtime esm 4.0.1 passed 9 0
cloudflare-workers esm 1.20261118.1 passed 9 0
chromium 159.0.8154.6 passed 1 0
firefox 159.0 passed 1 0
webkit 27.2 passed 1 0
mobile-chromium 159.0.8154.6 passed 1 0
mobile-firefox 159.0 passed 1 0
mobile-webkit 27.2 passed 1 0

Byte utilities

Usage

import {
  concatBytes,
  deriveBytes,
  equalBytes,
  generateBytes,
  normalizeBytes,
} from '@sovereignbase/bytecodec/util'

const normalized = normalizeBytes(new DataView(new Uint8Array([1, 2]).buffer))
const joined = concatBytes([normalized, [3, 4]])
const equal = equalBytes(joined, new Uint8Array([1, 2, 3, 4]))
const derived = await deriveBytes(joined, new Uint8Array([5]), 32)
const random = generateBytes(32)

deriveBytes() uses HKDF-SHA-256 and treats base as the salt and domain as the context information. The same inputs and byte length produce the same result. Use a distinct domain for every purpose.

All functions accepting ByteSource support:

  • ArrayBuffer
  • SharedArrayBuffer
  • any ArrayBufferView, including Uint8Array and DataView
  • number[]

normalizeBytes() always returns an independent Uint8Array<ArrayBuffer> copy, including for shared buffers and offset views. All byte-returning APIs explicitly return Uint8Array<ArrayBuffer> (or a promise of it). concatBytes() also normalizes every input before joining it.

Errors

Invalid sources throw BYTE_SOURCE_EXPECTED. Invalid concatenation throws CONCAT_INVALID_INPUT or CONCAT_NORMALIZE_FAILED with the input index. Web Crypto failures throw UNKNOWN_ERROR with the original cause. Random generation is limited to 65,536 bytes per call by Web Crypto.

Benchmarks

2026-11-19T15:03:25.649Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

AMD EPYC 9V45 96-Core Processor · 1,000 measured calls per case after warmup

Operation Input bytes Ops/sec
normalize view 64 3,714,076
concat three 64 969,129
equals same 64 1,901,061
equals different 64 1,985,707
generate 64 398,313
derive 32 bytes 64 33,764
normalize view 1024 1,198,443
concat three 1024 266,317
equals same 1024 612,881
equals different 1024 620,823
generate 1024 418,057
derive 32 bytes 1024 34,182
normalize view 16384 179,659
concat three 16384 23,440
equals same 16384 40,926
equals different 16384 92,154
generate 16384 185,185
derive 32 bytes 16384 25,152

Tests

2026-11-19T15:03:08.357Z · CI run · commit 6be3f67

Node v24.22.3 · linux x64

Suite Passed Failed Skipped
util (3 files) 68 0 0
Coverage statements branches functions lines
Percent 100% 100% 100% 100%

Compatibility

The shared compatibility suite exercises the whole public API.

Runtime / browser Version Status Passed Failed
node esm 24.22.3 passed 9 0
node cjs 24.22.3 passed 9 0
bun esm 1.4.5 passed 9 0
bun cjs 1.4.5 passed 9 0
deno esm 2.9.8 passed 9 0
edge-runtime esm 4.0.1 passed 9 0
cloudflare-workers esm 1.20261118.1 passed 9 0
chromium 159.0.8154.6 passed 1 0
firefox 159.0 passed 1 0
webkit 27.2 passed 1 0
mobile-chromium 159.0.8154.6 passed 1 0
mobile-firefox 159.0 passed 1 0
mobile-webkit 27.2 passed 1 0

License

Licensed under the Apache License, Version 2.0.

Copyright © 2026 Sovereignbase.

About

Runtime-agnostic TypeScript byte codecs for Base45, Base64, UTF-8, gzip, derivation, generation, normalization, concatenation, and comparison.

Topics

Resources

Code of conduct

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages