Base32 Encoding for JavaScript
This package provides implementations for various Base32 encoding schemes, including RFC 4648 and Crockford's Base32, tailored for JavaScript environments. Base32 offers distinct advantages over Base64, such as case-insensitivity, suitability for filenames (as it avoids the '/' character), and an alphabet carefully designed to minimize ambiguous characters (e.g., omitting 1, 8, 0 to prevent confusion with I, B, O). However, this comes at the cost of increased data size, with Base32 representations being approximately 20% larger than Base64. The package is currently at version 0.1.0, suggesting it is either an early-stage project or one that has not seen significant updates or active maintenance in a considerable amount of time, lacking a clear release cadence. It differentiates itself by supporting multiple Base32 variants and providing pre-built files for browser compatibility.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module (`.mjs` file or `"type": "module"` in `package.json`).fixUse `import * as base32 from 'base32.js';` and ensure your bundler or Node.js environment is configured to handle CommonJS interop. -
TypeError: base32.Encoder is not a constructor
cause Incorrect ES Module import of the CommonJS module. You might have tried `import base32 from 'base32.js';` which does not correctly capture the module's object export.fixUse `import * as base32 from 'base32.js';` to import the module object containing `Encoder` and `Decoder`. -
Error: Missing data at end of buffer
cause Mismatch in padding settings between the encoder and decoder, or providing an invalid Base32 string to the decoder.fixEnsure both `Encoder` and `Decoder` are initialized with the same `type` and `padding` options. If the encoded string was padded, the decoder must be initialized with `{ padding: true }`. Verify the input string is valid Base32 according to the specified type.
Warnings
- breaking This package is at version 0.1.0 and appears to be unmaintained. There is no active development, explicit support for modern JavaScript features like ES Modules, or official TypeScript types. Users should be aware of potential compatibility issues with newer Node.js versions, build tools, or browser environments, and the absence of security patches.
- gotcha The library's API uses a streaming encoder/decoder pattern (`.write().finalize()`), which might be unfamiliar to users expecting direct `encode(input)` and `decode(input)` functions. This pattern requires calling `.write()` with the input data, then `.finalize()` to get the result.
- gotcha The default Base32 variant is RFC 4648 *without* padding, which might differ from other Base32 implementations that commonly use padding by default. Incorrectly handling padding can lead to decoding errors or incompatible output with other systems.
Install
-
npm install base32.js -
yarn add base32.js -
pnpm add base32.js
Imports
- base32 (module object)
import base32 from "base32.js";
const base32 = require("base32.js"); - { Encoder, Decoder } (destructured CommonJS)
const Encoder = require("base32.js").Encoder;const { Encoder, Decoder } = require("base32.js"); - base32 (ESM interop for CJS module)
import base32 from 'base32.js';
import * as base32 from 'base32.js';
Quickstart
const base32 = require("base32.js");
console.log("Encoding a buffer using Crockford Base32 (lowercase)...");
const buf = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Extended buffer for a more realistic example
const encoder = new base32.Encoder({ type: "crockford", lc: true });
const encodedStr = encoder.write(buf).finalize();
console.log(`Original buffer: [${buf.join(', ')}]`);
console.log(`Encoded string: ${encodedStr}`);
console.log("\nDecoding the string back...");
const decoder = new base32.Decoder({ type: "crockford" });
const decodedBuf = decoder.write(encodedStr).finalize();
console.log(`Decoded buffer: [${decodedBuf.join(', ')}]`);
// Example with default RFC4648 without padding
console.log("\nEncoding with default RFC4648 (no padding)...");
const rfc4648Buf = Buffer.from("Hello World"); // Using Buffer for wider compatibility
const rfcEncoder = new base32.Encoder(); // Default is rfc4648, no padding
const rfcEncodedStr = rfcEncoder.write(rfc4648Buf).finalize();
console.log(`Original string: ${rfc4648Buf.toString()}`);
console.log(`Encoded RFC4648: ${rfcEncodedStr}`);
const rfcDecoder = new base32.Decoder();
const rfcDecodedBuf = rfcDecoder.write(rfcEncodedStr).finalize();
console.log(`Decoded RFC4648: ${rfcDecodedBuf.toString()});