Base32 Encoding/Decoding for Node.js
The `thirty-two` package provides an implementation of the Base32 encoding and decoding algorithms as specified in RFC 3548 for Node.js environments. Base32 is a binary-to-text encoding scheme that uses a 32-character alphabet, commonly employed for situations where human readability or case-insensitivity is desired, and URL-safety is important (as it avoids common punctuation characters). While RFC 3548 has been obsoleted by RFC 4648, the core Base32 encoding defined within it remains a standard and widely used method. This package, currently at version 1.0.2 and last updated approximately 10 years ago, is a mature and stable utility that fulfills its specific function without active feature development, operating effectively in maintenance mode. It's a foundational library for basic binary data transformation.
Common errors
-
TypeError: thirty-two.encode is not a function
cause Attempting to destructure `encode` or `decode` directly from the `require` statement (e.g., `const { encode } = require('thirty-two');`) or from an ESM named import (e.g., `import { encode } from 'thirty-two';`). The package exports a single object as its default.fixImport the entire object and access its methods: `const base32 = require('thirty-two'); base32.encode(...);` or `import base32 from 'thirty-two'; base32.encode(...);`. -
RangeError: Buffer.byteLength(string) is not a positive integer
cause Passing an invalid or non-string/non-Buffer input to `base32.encode()` or `base32.decode()`. These functions expect either a string or a Node.js Buffer.fixEnsure that the input argument to `encode` or `decode` is always a `string` or a `Buffer` instance. For example, `base32.encode(String(yourValue))` or `base32.encode(Buffer.from(yourBinaryData))`.
Warnings
- gotcha The `thirty-two` package is a CommonJS module and was last published 10 years ago. While Node.js provides interoperability for importing CommonJS modules into ESM projects, this package does not offer native ESM support. Projects built with modern ESM standards might experience unexpected behavior or require specific configuration for compatibility.
- gotcha By default, `thirty-two.encode()` accepts both strings and Buffers, but `thirty-two.decode()` always returns a Node.js `Buffer`. When decoding data that was originally a string, you must call `.toString()` on the returned Buffer to convert it back to a readable string, ensuring correct character encoding (e.g., UTF-8).
- gotcha RFC 3548, which `thirty-two` implements, has been obsoleted by RFC 4648. While the fundamental Base32 encoding it provides is largely compatible, RFC 4648 introduces additional considerations and explicit recommendations for padding and specific alphabets (like Base32hex). Ensure that your usage aligns with the specific Base32 variant required by your application.
Install
-
npm install thirty-two -
yarn add thirty-two -
pnpm add thirty-two
Imports
- base32
const { encode, decode } = require('thirty-two');const base32 = require('thirty-two'); - base32 (ESM)
import { encode, decode } from 'thirty-two';import base32 from 'thirty-two';
- encode
import { encode } from 'thirty-two'; encode('data');const base32 = require('thirty-two'); base32.encode('data');
Quickstart
import base32 from 'thirty-two';
import { Buffer } from 'buffer';
// Encode a string
const originalString = 'Hello, Base32!';
const encodedString = base32.encode(originalString);
console.log(`Original string: '${originalString}'`);
console.log(`Encoded string: '${encodedString}'`);
// Decode a Base32 string back to a string
const decodedString = base32.decode(encodedString).toString();
console.log(`Decoded string: '${decodedString}'`);
// Encode a Buffer for binary-safe handling
const originalBuffer = Buffer.from([0xDE, 0xAD, 0xBE, 0xEF]);
const encodedBufferString = base32.encode(originalBuffer);
console.log(`\nOriginal buffer: ${originalBuffer.toString('hex')}`);
console.log(`Encoded buffer: '${encodedBufferString}'`);
// Decode a Base32 string back to a Buffer
const decodedBuffer = base32.decode(encodedBufferString);
console.log(`Decoded buffer: ${decodedBuffer.toString('hex')}`);