What is base64-stream?
The base64-stream npm package provides a streaming interface for encoding and decoding data in base64 format. It is particularly useful for handling large amounts of data that need to be processed in chunks, such as files or network streams.
What are base64-stream's main functionalities?
Base64 Encoding
This feature allows you to encode a stream of data into base64 format. In this example, the contents of 'input.txt' are read, encoded in base64, and then written to 'output.txt'.
const fs = require('fs');
const base64 = require('base64-stream');
fs.createReadStream('input.txt')
.pipe(base64.encode())
.pipe(fs.createWriteStream('output.txt'));
Base64 Decoding
This feature allows you to decode a base64-encoded stream of data. In this example, the contents of 'input.txt' are read, decoded from base64, and then written to 'output.txt'.
const fs = require('fs');
const base64 = require('base64-stream');
fs.createReadStream('input.txt')
.pipe(base64.decode())
.pipe(fs.createWriteStream('output.txt'));
Other packages similar to base64-stream
base64-js
The base64-js package provides utilities for encoding and decoding base64 strings. Unlike base64-stream, it does not offer a streaming interface and is more suited for smaller data sizes that can be handled in memory.
base64url
The base64url package is designed for encoding and decoding base64 URL-safe strings. It is useful for web applications where URL-safe encoding is required. However, it does not provide a streaming interface like base64-stream.
node-base64-image
The node-base64-image package allows you to encode and decode images to and from base64 strings. It is specialized for image data and does not offer a general-purpose streaming interface like base64-stream.
Introduction
While Node.js has built-in support for Base64 data, it does not come with the ability to encode / decode data in a stream.
This library contains a streaming Base64 encoder and a streaming Base64 decoder for use with Node.js. These classes are written using the new Node.js v0.10 stream interfaces and are well covered with unit tests.
Installation
To install base64-stream
npm install base64-stream
Requirements
This module currently requires Node v0.8 or higher. Support for versions prior to v0.10 is made possible by using the readable-stream module.
Usage
To use base64-stream
var fs = require('fs');
var Base64Encode = require('base64-stream').Encode;
var Base64Decode = require('base64-stream').Decode;
var streamMailAttachment = function(req, res, next) {
var getAttachment(...);
attachmentStream.pipe(new Base64Encode()).pipe(res);
}
var uploadAttachment = function(req, res, next) {
var email = createEmail(...);
var stream = req.pipe(new Base64Encode());
email.streamAttachment(stream);
}
Testing
To run the unit tests
npm test