isomorphic-base64 is a compact, zero-dependency utility that provides consistent Base64 encoding and decoding functionality across both Node.js and browser environments. It exports `atob` and `btoa` functions, mirroring the browser's global Web API for Base64 operations. Currently at version 1.0.2, the package was last published over a decade ago (May 2015), indicating a highly stable and mature codebase that has seen no significant breaking changes or feature additions, operating on a maintenance-only release cadence. Its key differentiator is providing a reliable polyfill or abstraction layer, ensuring that applications can perform Base64 transformations uniformly without needing to conditionally implement Node.js's `Buffer` API or rely on the global `window.atob`/`window.btoa` in browsers. It primarily handles ASCII/Latin-1 strings, a common limitation of the native `atob`/`btoa` functions.
npm install isomorphic-base64Verified import paths — ran on the pinned version, not inferred.
Demonstrates basic Base64 encoding/decoding for ASCII strings and provides crucial workarounds for correctly handling Unicode characters, a common limitation of `atob`/`btoa`.
Before encoding Unicode strings, convert them to a byte array (e.g., UTF-8) and then to a Latin-1 compatible string using `TextEncoder` and `String.fromCharCode` for `btoa`. For decoding, reverse the process using `atob`, `Uint8Array.from`, and `TextDecoder`. See quickstart for example. Older browsers might need `encodeURIComponent`/`escape`/`unescape`.
For new projects, consider native Node.js `Buffer` API (`Buffer.from(str).toString('base64')`), modern browser `btoa`/`atob` with careful Unicode handling, or newer isomorphic Base64 libraries that might leverage `TextEncoder`/`TextDecoder` more directly for robust Unicode support.For Node.js, directly using `Buffer.from(str).toString('base64')` and `Buffer.from(base64Str, 'base64').toString()` is generally preferred for byte-safe Base64 operations, especially with Unicode. This package still provides `atob`/`btoa` for browser compatibility, but be mindful of their limitations.Before calling `btoa`, encode the string to UTF-8 bytes and then convert those bytes into a 'binary string' where each character corresponds to a byte. This can be done using `TextEncoder` and `String.fromCharCode()` (or `encodeURIComponent`/`unescape` for older environments). See the quickstart example for details.
Ensure your build setup (e.g., webpack, Rollup, Parcel, Node.js with `type: 'module'`) is configured to handle CommonJS modules correctly. For pure ESM, use `import { atob, btoa } from 'isomorphic-base64';` or consider dynamically importing `import('isomorphic-base64')` if `require` is strictly unavailable and direct named CJS imports are not working.No dependency data recorded yet.