What is @hapi/b64?
The @hapi/b64 package is designed for encoding and decoding data in base64 format. It is part of the hapi ecosystem, which is a rich framework for building applications and services. @hapi/b64 provides a straightforward and efficient way to work with base64 encoded data in Node.js applications.
What are @hapi/b64's main functionalities?
Encoding data to base64
This feature allows you to encode textual or binary data into base64 format. The provided code sample demonstrates how to encode the string 'Hello, World!' into base64.
"use strict"; const B64 = require('@hapi/b64'); const encoded = B64.encode('Hello, World!'); console.log(encoded);
Decoding base64 data
This feature enables the decoding of base64 encoded data back into its original format. The code sample shows how to decode a base64 encoded string back to 'Hello, World!'.
"use strict"; const B64 = require('@hapi/b64'); const decoded = B64.decode('SGVsbG8sIFdvcmxkIQ=='); console.log(decoded.toString());
Other packages similar to @hapi/b64
base64-js
base64-js is a package that provides similar base64 encoding and decoding functionalities. Compared to @hapi/b64, base64-js focuses solely on base64 operations and might offer more specialized functions for handling base64 data in various scenarios.
buffer
The buffer package includes methods for encoding and decoding base64 data as part of its broader functionality for handling binary data in Node.js. While @hapi/b64 is specifically tailored for base64 encoding and decoding, buffer offers a wider range of capabilities for binary data manipulation.
Base64 streaming encoder and decoder
Lead Maintainer - Eran Hammer
Installation
npm install b64 --save
API
encode(buffer)
Base64 encode the buffer and return it as a new Buffer.
decode(buffer)
Base64 decode the buffer and return the result as a new buffer.
Encoder
Transform stream that base64 encodes each chunk of the stream.
Example:
'use strict';
const Fs = require('fs');
const B64 = require('@hapi/b64');
const stream = Fs.createReadStream(`${__dirname}/package.json`);
const encoder = new B64.Encoder();
stream.pipe(encoder).pipe(process.stdout);
Decoder
Transform stream that base64 decodes each chunk of the stream.
Example:
'use strict';
const Fs = require('fs');
const B64 = require('@hapi/b64');
const stream = Fs.createReadStream(`${__dirname}/encodedfile.b64`);
const decoder = new B64.Decoder();
stream.pipe(decoder).pipe(process.stdout);
base64urlEncode(value)
Encodes value of string or buffer type in Base64 or URL encoding, function will assert input value is correct.
base64urlDecode(value)
Decodes string into Base64 or URL encoding, function throws an error on invalid input and returns a string or buffer depending on encoding provided. Default encoding is binary.