Initial
This commit is contained in:
60
test/imaps/node_modules/base64-stream/README.md
generated
vendored
Normal file
60
test/imaps/node_modules/base64-stream/README.md
generated
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
# 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 Node.js [stream interfaces](http://nodejs.org/api/stream.html) 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.
|
||||
|
||||
```javascript
|
||||
var http = require('http');
|
||||
var {Base64Encode} = 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(new Base64Encode()).pipe(process.stdout);
|
||||
});
|
||||
```
|
||||
|
||||
This example takes in Base64 encoded data on stdin, decodes it, an pipes it to stdout.
|
||||
```javascript
|
||||
var {Base64Encode} = require('base64-stream');
|
||||
process.stdin.pipe(new Base64Encode()).pipe(process.stdout);
|
||||
```
|
||||
|
||||
## options:
|
||||
|
||||
`Base64Encode` can take an optional object `{lineLength: number, prefix: string}`
|
||||
The prefix is useful for prepending for example `data:image/png;base64,` to make a base64 URL.
|
||||
This example proxies an image url, and send the base64 string in response.
|
||||
|
||||
```
|
||||
app.get('/i/*', function(req, res){ // using express for example
|
||||
fetch(req.params[0]) // using node-fetch
|
||||
.then(r=>r.body.pipe(new Base64Encode({prefix:`data:${r.headers.get('content-type')};base64,`})).pipe(res))
|
||||
.catch(console.error);
|
||||
});
|
||||
```
|
||||
|
||||
# Requirements
|
||||
|
||||
This module currently requires Node 6.0.0 or higher.
|
||||
|
||||
# Testing
|
||||
|
||||
To run the unit tests
|
||||
|
||||
npm test
|
||||
|
||||
# License
|
||||
MIT
|
||||
7
test/imaps/node_modules/base64-stream/index.js
generated
vendored
Normal file
7
test/imaps/node_modules/base64-stream/index.js
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
const Base64Encode = require('./lib/encode');
|
||||
const Base64Decode = require('./lib/decode');
|
||||
|
||||
module.exports = {
|
||||
Base64Encode,
|
||||
Base64Decode
|
||||
};
|
||||
56
test/imaps/node_modules/base64-stream/lib/decode.js
generated
vendored
Normal file
56
test/imaps/node_modules/base64-stream/lib/decode.js
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
const { Transform } = require('stream');
|
||||
|
||||
/**
|
||||
* Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers.
|
||||
* @extends Transform
|
||||
*/
|
||||
module.exports = class Base64Decode extends Transform {
|
||||
/**
|
||||
* Create a Base64Decode
|
||||
*/
|
||||
constructor() {
|
||||
super({ decodeStrings: false });
|
||||
// Any extra chars from the last chunk
|
||||
this.extra = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes a Base64 data stream, coming in as a string or Buffer of UTF-8 text, into binary Buffers.
|
||||
* @param {Buffer|string} chunk
|
||||
* @param encoding
|
||||
* @param cb
|
||||
* @private
|
||||
*/
|
||||
_transform(chunk, encoding, cb) {
|
||||
// Convert chunk to a string
|
||||
chunk = '' + chunk;
|
||||
|
||||
// Add previous extra and remove any newline characters
|
||||
chunk = this.extra + chunk.replace(/(\r\n|\n|\r)/gm, '');
|
||||
|
||||
// 4 characters represent 3 bytes, so we can only decode in groups of 4 chars
|
||||
const remaining = chunk.length % 4;
|
||||
|
||||
// Store the extra chars for later
|
||||
this.extra = chunk.slice(chunk.length - remaining);
|
||||
chunk = chunk.slice(0, chunk.length - remaining);
|
||||
|
||||
// Create the new buffer and push
|
||||
const buf = Buffer.from(chunk, 'base64');
|
||||
this.push(buf);
|
||||
cb();
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits 1, 2, or 3 extra characters of base64 data.
|
||||
* @param cb
|
||||
* @private
|
||||
*/
|
||||
_flush(cb) {
|
||||
if (this.extra.length) {
|
||||
this.push(Buffer.from(this.extra, 'base64'));
|
||||
}
|
||||
|
||||
cb();
|
||||
}
|
||||
};
|
||||
116
test/imaps/node_modules/base64-stream/lib/encode.js
generated
vendored
Normal file
116
test/imaps/node_modules/base64-stream/lib/encode.js
generated
vendored
Normal file
@@ -0,0 +1,116 @@
|
||||
const { Transform } = require('stream');
|
||||
|
||||
/**
|
||||
* Transforms a Buffer stream of binary data to a stream of Base64 text. Note that this will
|
||||
* also work on a stream of pure strings, as the Writeable base class will automatically decode
|
||||
* text string chunks into Buffers.
|
||||
* You can pass optionally a line length or a prefix
|
||||
* @extends Transform
|
||||
*/
|
||||
module.exports = class Base64Encode extends Transform {
|
||||
/**
|
||||
* Creates a Base64Encode
|
||||
* @param {Object=} options - Options for stream creation. Passed to Transform constructor as-is.
|
||||
* @param {string=} options.inputEncoding - The input chunk format. Default is 'utf8'. No effect on Buffer input chunks.
|
||||
* @param {string=} options.outputEncoding - The output chunk format. Default is 'utf8'. Pass `null` for Buffer chunks.
|
||||
* @param {number=} options.lineLength - The max line-length of the output stream.
|
||||
* @param {string=} options.prefix - Prefix for output string.
|
||||
*/
|
||||
constructor(options) {
|
||||
super(options);
|
||||
|
||||
// Any extra chars from the last chunk
|
||||
this.extra = null;
|
||||
this.lineLength = options && options.lineLength;
|
||||
this.currLineLength = 0;
|
||||
if (options && options.prefix) {
|
||||
this.push(options.prefix);
|
||||
}
|
||||
|
||||
// Default string input to be treated as 'utf8'
|
||||
const encIn = options && options.inputEncoding;
|
||||
this.setDefaultEncoding(encIn || 'utf8');
|
||||
|
||||
// Default output to be strings
|
||||
const encOut = options && options.outputEncoding;
|
||||
if (encOut !== null) {
|
||||
this.setEncoding(encOut || 'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds \r\n as needed to the data chunk to ensure that the output Base64 string meets
|
||||
* the maximum line length requirement.
|
||||
* @param {string} chunk
|
||||
* @returns {string}
|
||||
* @private
|
||||
*/
|
||||
_fixLineLength(chunk) {
|
||||
// If we care about line length, add line breaks
|
||||
if (!this.lineLength) {
|
||||
return chunk;
|
||||
}
|
||||
|
||||
const size = chunk.length;
|
||||
const needed = this.lineLength - this.currLineLength;
|
||||
let start, end;
|
||||
|
||||
let _chunk = '';
|
||||
for (start = 0, end = needed; end < size; start = end, end += this.lineLength) {
|
||||
_chunk += chunk.slice(start, end);
|
||||
_chunk += '\r\n';
|
||||
}
|
||||
|
||||
const left = chunk.slice(start);
|
||||
this.currLineLength = left.length;
|
||||
|
||||
_chunk += left;
|
||||
|
||||
return _chunk;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a Buffer chunk of data to a Base64 string chunk.
|
||||
* @param {Buffer} chunk
|
||||
* @param {string} encoding - unused since chunk is always a Buffer
|
||||
* @param cb
|
||||
* @private
|
||||
*/
|
||||
_transform(chunk, encoding, cb) {
|
||||
// Add any previous extra bytes to the chunk
|
||||
if (this.extra) {
|
||||
chunk = Buffer.concat([this.extra, chunk]);
|
||||
this.extra = null;
|
||||
}
|
||||
|
||||
// 3 bytes are represented by 4 characters, so we can only encode in groups of 3 bytes
|
||||
const remaining = chunk.length % 3;
|
||||
|
||||
if (remaining !== 0) {
|
||||
// Store the extra bytes for later
|
||||
this.extra = chunk.slice(chunk.length - remaining);
|
||||
chunk = chunk.slice(0, chunk.length - remaining);
|
||||
}
|
||||
|
||||
// Convert chunk to a base 64 string
|
||||
chunk = chunk.toString('base64');
|
||||
|
||||
// Push the chunk
|
||||
this.push(Buffer.from(this._fixLineLength(chunk)));
|
||||
cb();
|
||||
}
|
||||
|
||||
/**
|
||||
* Emits 0 or 4 extra characters of Base64 data.
|
||||
* @param cb
|
||||
* @private
|
||||
*/
|
||||
_flush(cb) {
|
||||
if (this.extra) {
|
||||
this.push(Buffer.from(this._fixLineLength(this.extra.toString('base64'))));
|
||||
}
|
||||
|
||||
cb();
|
||||
}
|
||||
|
||||
};
|
||||
34
test/imaps/node_modules/base64-stream/package.json
generated
vendored
Normal file
34
test/imaps/node_modules/base64-stream/package.json
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "base64-stream",
|
||||
"description": "Contains new Node.js v0.10 style stream classes for encoding / decoding Base64 data",
|
||||
"keywords": [
|
||||
"Base64",
|
||||
"stream",
|
||||
"streaming",
|
||||
"piping",
|
||||
"node",
|
||||
"node.js",
|
||||
"encode",
|
||||
"decode"
|
||||
],
|
||||
"author": "Ross Johnson <ross@mazira.com>",
|
||||
"version": "1.0.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/mazira/base64-stream"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha --reporter spec"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"mocha": "*",
|
||||
"should": "*"
|
||||
},
|
||||
"license": "MIT",
|
||||
"files": [
|
||||
"lib",
|
||||
"index.js"
|
||||
],
|
||||
"engine": "node >= 0.8.0"
|
||||
}
|
||||
Reference in New Issue
Block a user