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.
Usage
Installation
To install base64-stream
npm install base64-stream
Examples
This example encodes an image and pipes it to stdout.
var http = require('http');
var base64 = require('base64-stream');
var img = 'http://farm3.staticflickr.com/2433/3973241798_86ddfa642b_o.jpg';
http.get(img, function(res) {
if (res.statusCode === 200)
res.pipe(base64.encode()).pipe(process.stdout);
});
This example takes in Base64 encoded data on stdin, decodes it, an pipes it to stdout.
var base64 = require('base64-stream');
process.stdin.pipe(base64.decode()).pipe(process.stdout);
You may also treat encode / decode as classes, so the following is also valid:
var Base64Decode = require('base64-stream').decode;
var stream = new Base64Decode();
...
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.
Testing
To run the unit tests
npm test
License
MIT
Source
| 1 | # Introduction |
| 2 | |
| 3 | While Node.js has built-in support for Base64 data, it does not come with the ability to encode / decode data in a stream. |
| 4 | |
| 5 | 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](http://nodejs.org/api/stream.html) and are well covered with unit tests. |
| 6 | |
| 7 | # Usage |
| 8 | |
| 9 | ## Installation |
| 10 | |
| 11 | To install base64-stream |
| 12 | |
| 13 | npm install base64-stream |
| 14 | |
| 15 | ## Examples |
| 16 | This example encodes an image and pipes it to stdout. |
| 17 | |
| 18 | ```javascript |
| 19 | var http = require('http'); |
| 20 | var base64 = require('base64-stream'); |
| 21 | |
| 22 | var img = 'http://farm3.staticflickr.com/2433/3973241798_86ddfa642b_o.jpg'; |
| 23 | http.get(img, function(res) { |
| 24 | if (res.statusCode === 200) |
| 25 | res.pipe(base64.encode()).pipe(process.stdout); |
| 26 | }); |
| 27 | ``` |
| 28 | |
| 29 | This example takes in Base64 encoded data on stdin, decodes it, an pipes it to stdout. |
| 30 | ```javascript |
| 31 | var base64 = require('base64-stream'); |
| 32 | process.stdin.pipe(base64.decode()).pipe(process.stdout); |
| 33 | ``` |
| 34 | |
| 35 | You may also treat `encode` / `decode` as classes, so the following is also valid: |
| 36 | ```javascript |
| 37 | var Base64Decode = require('base64-stream').decode; |
| 38 | var stream = new Base64Decode(); |
| 39 | ... |
| 40 | ``` |
| 41 | |
| 42 | # Requirements |
| 43 | |
| 44 | This module currently requires Node v0.8 or higher. Support for versions prior to v0.10 is made possible by using the [readable-stream](https://github.com/isaacs/readable-stream) module. |
| 45 | |
| 46 | # Testing |
| 47 | |
| 48 | To run the unit tests |
| 49 | |
| 50 | npm test |
| 51 | |
| 52 | # License |
| 53 | MIT |