DEV Community

Cover image for Base64 problem solved
Zsolt Tövis
Zsolt Tövis

Posted on

Base64 problem solved

I just published an npm package that tackles the challenges associated with Base64 encoding and decoding in both browser and Node.js environments, while also offering support for handling binary files.

Problem 🤔

If you've ever encountered issues with the atob and btoa methods when working with utf-8 strings on the frontend, you know the kind of challenges they can pose. These built-in methods do not handle utf-8 characters correctly and can result in errors. The base64-transcode npm package aims to address this problem and provide a solution.

How It Solves the Problem 🛠️

The package enables seamless Base64 encoding and decoding on both the frontend and backend. This means you can use the same package and syntax in both the browser and Node.js environments. Furthermore, the package supports handling binary files, making it easy to work with them on the backend.

Compatibility 🌟

This package supports CommonJS, ES Modules, and IIFE (Immediately Invoked Function Expression) formats. You can safely use both require and import statements in any environment. TypeScript also supported.

A simple example of how to use it 🚀

It's a simple way to convert data to and from Base64 encoding in modern JavaScript.

// Don't forget to install the package...
// "yarn add base64-transcode" or "npm i base64-transcode"
import Base64 from 'base64-transcode';

// Decoding
const decodedData = Base64.decode('SGVsbG8gV29ybGQh');
console.log(decodedData); // "Hello World!"

// Encoding
const encodedData = Base64.encode('Hello World!');
console.log(encodedData); // "SGVsbG8gV29ybGQh"
Enter fullscreen mode Exit fullscreen mode

How you can use it with traditional html 💪

And the same example for raw html, in case you don't use any bundler.

<script src="https://unpkg.com/base64-transcode/dist/browser.js"></script>
<script>
  // Decoding
  const decodedData = Base64.decode('SGVsbG8gV29ybGQh');
  console.log(decodedData); // "Hello World!"

  // Encoding
  const encodedData = Base64.encode('Hello World!');
  console.log(encodedData); // "SGVsbG8gV29ybGQh"
</script>
Enter fullscreen mode Exit fullscreen mode

API documentation 📚

You can find documentation and detailed information

Final words 🌈

In a world where data encoding and decoding are vital tasks for web development, the base64-transcode package emerges as a savior. It seamlessly resolves the quirks and challenges of Base64 operations, making life easier for developers on both the frontend and backend.

With its support for binary files and compatibility across various module systems, this package simplifies the handling of data. Embrace base64-transcode and unlock the power of smooth, hassle-free Base64 operations in your projects.

Happy coding! 😊🚀

Top comments (7)

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix

My current 2 utility methods look like this

export function base64Decode(base64: string) {
  return Buffer.from(base64, "base64").toString("binary")
}
export function base64Encode(str: string) {
  return Buffer.from(str).toString("base64")
}
Enter fullscreen mode Exit fullscreen mode

Looking at your source code, wasn't btoa deprecated?
github.com/toviszsolt/base64-trans...

Collapse
 
toviszsolt profile image
Zsolt Tövis • Edited

Thank you for writing, finally someone! 🤗

Yes, Buffer is works in node environment only, but not in browser. 😉

That's how I used it in pure node, but now in a full stack application I can use the same syntax on both sides, which makes development and readability easier.

So now I get a data on the frontend and I decode it like this:

const { text, binary } = response;
const decodedText = Base64.decode(text);
const decodedBinary = Base64.decode(binary, true);
Enter fullscreen mode Exit fullscreen mode

And from node I send the encoded data like this:

const text = 'String data';
const binary = fs.readfileSync('file_name');
const send = {
  text: Base64.encode(text),
  binary: Base64.encode(binary, true),
};
Enter fullscreen mode Exit fullscreen mode

I've tried it of course, and I don't get a deprecated warning either in VSCode or when running the script in the console. 🤩💪

// index.js
const Base64 = require('base64-transcode');
const str = 'Hello, world!';
const testBtoa = btoa(str);
console.log(Base64.encode(str), testBtoa);

// output
D:\tmp\node_test>node index
SGVsbG8sIHdvcmxkIQ== SGVsbG8sIHdvcmxkIQ==
Enter fullscreen mode Exit fullscreen mode

If btoa becomes deprecated in a newer release of node, I'll add the some new lines quickly. 👌

I'm sorry, I deleted my previous comment and have now slightly edited it, because I misunderstood what you wrote.

Collapse
 
adaptive-shield-matrix profile image
Adaptive Shield Matrix

About the deprecated: I remember seeing it somewhere, but it does not show up for me now as well using bun-types 1.0.2.

Official MDN docs do not list it as deprecated as well
developer.mozilla.org/en-US/docs/W...

So I think you are good to go :)

Collapse
 
toviszsolt profile image
Zsolt Tövis

In the meantime I have forgotten the most important thing! Sorry.

Node version I use: v18.18.0

Collapse
 
toviszsolt profile image
Zsolt Tövis

I welcome your comments! 👏

Collapse
 
androaddict profile image
androaddict

Can I use in react native ?

Collapse
 
toviszsolt profile image
Zsolt Tövis • Edited

@androaddict Since React Native runs on Node.js, of course you can. If you encounter any error message, just copy it here and we'll fix it.