libbase64
Advanced tools
Comparing version 0.2.0 to 1.0.0
'use strict'; | ||
var stream = require('stream'); | ||
var util = require('util'); | ||
var Transform = stream.Transform; | ||
const stream = require('stream'); | ||
const Transform = stream.Transform; | ||
// expose to the world | ||
module.exports = { | ||
encode: encode, | ||
decode: decode, | ||
wrap: wrap, | ||
Encoder: Encoder, | ||
Decoder: Decoder | ||
}; | ||
/** | ||
@@ -66,76 +56,82 @@ * Encodes a Buffer into a base64 encoded string | ||
*/ | ||
function Encoder(options) { | ||
// init Transform | ||
this.options = options || {}; | ||
class Encoder extends Transform { | ||
constructor(options) { | ||
super(); | ||
if (this.options.lineLength !== false) { | ||
this.options.lineLength = this.options.lineLength || 76; | ||
} | ||
// init Transform | ||
this.options = options || {}; | ||
this._curLine = ''; | ||
this._remainingBytes = false; | ||
if (this.options.lineLength !== false) { | ||
this.options.lineLength = this.options.lineLength || 76; | ||
} | ||
this.inputBytes = 0; | ||
this.outputBytes = 0; | ||
this._curLine = ''; | ||
this._remainingBytes = false; | ||
Transform.call(this, this.options); | ||
} | ||
util.inherits(Encoder, Transform); | ||
this.inputBytes = 0; | ||
this.outputBytes = 0; | ||
} | ||
Encoder.prototype._transform = function(chunk, encoding, done) { | ||
var b64, | ||
_self = this; | ||
_transform(chunk, encoding, done) { | ||
if (encoding !== 'buffer') { | ||
chunk = Buffer.from(chunk, encoding); | ||
} | ||
if (encoding !== 'buffer') { | ||
chunk = Buffer.from(chunk, encoding); | ||
} | ||
if (!chunk || !chunk.length) { | ||
return setImmediate(done); | ||
} | ||
if (!chunk || !chunk.length) { | ||
return done(); | ||
} | ||
this.inputBytes += chunk.length; | ||
this.inputBytes += chunk.length; | ||
if (this._remainingBytes && this._remainingBytes.length) { | ||
chunk = Buffer.concat([this._remainingBytes, chunk], this._remainingBytes.length + chunk.length); | ||
this._remainingBytes = false; | ||
} | ||
if (this._remainingBytes && this._remainingBytes.length) { | ||
chunk = Buffer.concat([this._remainingBytes, chunk]); | ||
this._remainingBytes = false; | ||
} | ||
if (chunk.length % 3) { | ||
this._remainingBytes = chunk.slice(chunk.length - chunk.length % 3); | ||
chunk = chunk.slice(0, chunk.length - chunk.length % 3); | ||
} else { | ||
this._remainingBytes = false; | ||
} | ||
if (chunk.length % 3) { | ||
this._remainingBytes = chunk.slice(chunk.length - chunk.length % 3); | ||
chunk = chunk.slice(0, chunk.length - chunk.length % 3); | ||
} else { | ||
this._remainingBytes = false; | ||
} | ||
let b64 = this._curLine + encode(chunk); | ||
b64 = this._curLine + encode(chunk); | ||
if (this.options.lineLength) { | ||
b64 = wrap(b64, this.options.lineLength); | ||
if (this.options.lineLength) { | ||
b64 = wrap(b64, this.options.lineLength); | ||
b64 = b64.replace(/(^|\n)([^\n]*)$/, function(match, lineBreak, lastLine) { | ||
_self._curLine = lastLine; | ||
return lineBreak; | ||
}); | ||
} | ||
// remove last line as it is still most probably incomplete | ||
let lastLF = b64.lastIndexOf('\n'); | ||
if (lastLF < 0) { | ||
this._curLine = b64; | ||
b64 = ''; | ||
} else if (lastLF === b64.length - 1) { | ||
this._curLine = ''; | ||
} else { | ||
this._curLine = b64.substr(lastLF + 1); | ||
b64 = b64.substr(0, lastLF + 1); | ||
} | ||
} | ||
if (b64) { | ||
this.outputBytes += b64.length; | ||
this.push(b64); | ||
if (b64) { | ||
this.outputBytes += b64.length; | ||
this.push(Buffer.from(b64, 'ascii')); | ||
} | ||
setImmediate(done); | ||
} | ||
done(); | ||
}; | ||
Encoder.prototype._flush = function(done) { | ||
if (this._remainingBytes && this._remainingBytes.length) { | ||
this._curLine += encode(this._remainingBytes); | ||
_flush(done) { | ||
if (this._remainingBytes && this._remainingBytes.length) { | ||
this._curLine += encode(this._remainingBytes); | ||
} | ||
if (this._curLine) { | ||
this._curLine = wrap(this._curLine, this.options.lineLength); | ||
this.outputBytes += this._curLine.length; | ||
this.push(Buffer.from(this._curLine, 'ascii')); | ||
this._curLine = ''; | ||
} | ||
setImmediate(done); | ||
} | ||
if (this._curLine) { | ||
this._curLine = wrap(this._curLine, this.options.lineLength); | ||
this.outputBytes += this._curLine.length; | ||
this.push(this._curLine, 'ascii'); | ||
this._curLine = ''; | ||
} | ||
done(); | ||
}; | ||
} | ||
@@ -148,58 +144,64 @@ /** | ||
*/ | ||
function Decoder(options) { | ||
// init Transform | ||
this.options = options || {}; | ||
this._curLine = ''; | ||
class Decoder extends Transform { | ||
constructor(options) { | ||
super(); | ||
// init Transform | ||
this.options = options || {}; | ||
this._curLine = ''; | ||
this.inputBytes = 0; | ||
this.outputBytes = 0; | ||
this.inputBytes = 0; | ||
this.outputBytes = 0; | ||
Transform.call(this, this.options); | ||
} | ||
util.inherits(Decoder, Transform); | ||
Transform.call(this, this.options); | ||
} | ||
Decoder.prototype._transform = function(chunk, encoding, done) { | ||
var b64, buf; | ||
_transform(chunk, encoding, done) { | ||
if (!chunk || !chunk.length) { | ||
return setImmediate(done); | ||
} | ||
chunk = chunk.toString('ascii'); | ||
this.inputBytes += chunk.length; | ||
if (!chunk || !chunk.length) { | ||
return done(); | ||
} | ||
let b64 = this._curLine + chunk.toString('ascii'); | ||
this._curLine = ''; | ||
this.inputBytes += chunk.length; | ||
if (/[^a-zA-Z0-9+/=]/.test(b64)) { | ||
b64 = b64.replace(/[^a-zA-Z0-9+/=]/g, ''); | ||
} | ||
b64 = this._curLine + chunk; | ||
this._curLine = ''; | ||
b64 = b64.replace(/[^a-zA-Z0-9+\/=]/g, ''); | ||
if (b64.length % 4) { | ||
this._curLine = b64.substr(-b64.length % 4); | ||
if (this._curLine.length == b64.length || this._curLine.length < 4) { | ||
if (b64.length < 4) { | ||
this._curLine = b64; | ||
b64 = ''; | ||
} else { | ||
b64 = b64.substr(0, this._curLine.length); | ||
} else if (b64.length % 4) { | ||
this._curLine = b64.substr(-b64.length % 4); | ||
b64 = b64.substr(0, b64.length - this._curLine.length); | ||
} | ||
if (b64) { | ||
let buf = decode(b64); | ||
this.outputBytes += buf.length; | ||
this.push(buf); | ||
} | ||
setImmediate(done); | ||
} | ||
if (b64) { | ||
buf = decode(b64); | ||
this.outputBytes += buf.length; | ||
this.push(buf); | ||
_flush(done) { | ||
if (this._curLine) { | ||
let buf = decode(this._curLine); | ||
this.outputBytes += buf.length; | ||
this.push(buf); | ||
this._curLine = ''; | ||
} | ||
setImmediate(done); | ||
} | ||
} | ||
done(); | ||
// expose to the world | ||
module.exports = { | ||
encode, | ||
decode, | ||
wrap, | ||
Encoder, | ||
Decoder | ||
}; | ||
Decoder.prototype._flush = function(done) { | ||
var b64, buf; | ||
if (this._curLine) { | ||
buf = decode(this._curLine); | ||
this.outputBytes += buf.length; | ||
this.push(buf); | ||
this._curLine = ''; | ||
} | ||
done(); | ||
}; |
{ | ||
"name": "libbase64", | ||
"version": "0.2.0", | ||
"version": "1.0.0", | ||
"description": "Encode and decode base64 encoded strings", | ||
@@ -11,20 +11,21 @@ "main": "lib/libbase64.js", | ||
"type": "git", | ||
"url": "git://github.com/andris9/libbase64.git" | ||
"url": "git://github.com/nodemailer/libbase64.git" | ||
}, | ||
"keywords": [ | ||
"base64", | ||
"mime" | ||
], | ||
"keywords": ["base64", "mime"], | ||
"author": "Andris Reinman", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/andris9/libbase64/issues" | ||
"url": "https://github.com/nodemailer/libbase64/issues" | ||
}, | ||
"homepage": "https://github.com/andris9/libbase64", | ||
"homepage": "https://github.com/nodemailer/libbase64", | ||
"devDependencies": { | ||
"chai": "~1.8.1", | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.8.0", | ||
"grunt-mocha-test": "~0.10.0" | ||
} | ||
"chai": "^4.1.2", | ||
"eslint-config-nodemailer": "^1.2.0", | ||
"grunt": "^1.0.1", | ||
"grunt-cli": "^1.2.0", | ||
"grunt-eslint": "^20.1.0", | ||
"grunt-mocha-test": "^0.13.3", | ||
"mocha": "^4.0.1" | ||
}, | ||
"dependencies": {} | ||
} |
@@ -14,3 +14,3 @@ # libbase64 | ||
```javascript | ||
var libbase64 = require('libbase64'); | ||
const libbase64 = require('libbase64'); | ||
``` | ||
@@ -26,3 +26,3 @@ | ||
* **val** is a Buffer or an unicode string | ||
* **val** is a Buffer or an unicode string | ||
@@ -44,4 +44,4 @@ **Example** | ||
* **str** is a base64 encoded string | ||
* **lineLength** (defaults to 76) is the maximum allowed line length | ||
* **str** is a base64 encoded string | ||
* **lineLength** (defaults to 76) is the maximum allowed line length | ||
@@ -51,3 +51,3 @@ **Example** | ||
```javascript | ||
libbase64.wrap('asO1Z2V2asO1Z2V2asO1Z2V2YQ==', 10) | ||
libbase64.wrap('asO1Z2V2asO1Z2V2asO1Z2V2YQ==', 10); | ||
// asO1Z2V2as\r\n | ||
@@ -66,7 +66,8 @@ // O1Z2V2asO1\r\n | ||
var encoder = new libbase64.Encoder([options]) | ||
const encoder = new libbase64.Encoder([options]) | ||
Where | ||
* **options** is the optional stream options object with an additional option `lineLength` if you want to use any other line length than the default 76 characters (or set to `false` to turn the soft wrapping off completely) | ||
* **options** is the optional stream options object with an additional option `lineLength` if you want to use any other line length than the default 76 | ||
characters (or set to `false` to turn the soft wrapping off completely) | ||
@@ -78,7 +79,7 @@ **Example** | ||
```javascript | ||
var libbase64 = require('libbase64'); | ||
var fs = require('fs'); | ||
var source = fs.createReadStream('source.txt'); | ||
var encoded = fs.createReadStream('encoded.txt'); | ||
var encoder = new libbase64.Encoder(); | ||
const libbase64 = require('libbase64'); | ||
const fs = require('fs'); | ||
const source = fs.createReadStream('source.txt'); | ||
const encoded = fs.createReadStream('encoded.txt'); | ||
const encoder = new libbase64.Encoder(); | ||
@@ -92,7 +93,7 @@ source.pipe(encoder).pipe(encoded); | ||
var decoder = new libbase64.Decoder([options]) | ||
const decoder = new libbase64.Decoder([options]) | ||
Where | ||
* **options** is the optional stream options object | ||
* **options** is the optional stream options object | ||
@@ -104,7 +105,7 @@ **Example** | ||
```javascript | ||
var libbase64 = require('libbase64'); | ||
var fs = require('fs'); | ||
var encoded = fs.createReadStream('encoded.txt'); | ||
var dest = fs.createReadStream('dest.txt'); | ||
var decoder = new libbase64.Decoder(); | ||
const libbase64 = require('libbase64'); | ||
const fs = require('fs'); | ||
const encoded = fs.createReadStream('encoded.txt'); | ||
const dest = fs.createReadStream('dest.txt'); | ||
const decoder = new libbase64.Decoder(); | ||
@@ -116,2 +117,2 @@ encoded.pipe(decoder).pipe(dest); | ||
**MIT** | ||
**MIT** |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9478
170
0
110
7