Skip to content

Commit 0a71aa1

Browse files
committed
v1.1.0
1 parent eddbb21 commit 0a71aa1

File tree

7 files changed

+717
-23
lines changed

7 files changed

+717
-23
lines changed

.eslintrc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
{
2-
"extends": "nodemailer"
2+
"rules": {
3+
"indent": 0,
4+
"global-require": 0,
5+
"no-await-in-loop": 0
6+
},
7+
"extends": ["nodemailer", "prettier"],
8+
"parserOptions": {
9+
"ecmaVersion": 2017
10+
}
311
}

.prettierrc.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
printWidth: 160,
3+
tabWidth: 4,
4+
singleQuote: true
5+
};

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Encode Buffer objects or unicode strings with
2222

2323
Where
2424

25-
* **val** is a Buffer or an unicode string
25+
- **val** is a Buffer or an unicode string
2626

2727
**Example**
2828

@@ -39,8 +39,8 @@ To enforce soft line breaks on lines longer than selected amount of characters,
3939

4040
Where
4141

42-
* **str** is a base64 encoded string
43-
* **lineLength** (defaults to 76) is the maximum allowed line length
42+
- **str** is a base64 encoded string
43+
- **lineLength** (defaults to 76) is the maximum allowed line length
4444

4545
**Example**
4646

@@ -63,8 +63,11 @@ Create new Encoder Stream with
6363

6464
Where
6565

66-
* **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
67-
characters (or set to `false` to turn the soft wrapping off completely)
66+
- **options** is the optional stream options object
67+
- **options.lineLength** (Number) if you want to use any other line length than the default 76
68+
characters (or set to `false` to turn the soft wrapping off completely)
69+
- **options.skipStartBytes** (Number) Optional. How many bytes to skip from output (default to 0)
70+
- **options.limitOutbutBytes** (Number) Optional. How many bytes to return (defaults to all bytes)
6871

6972
**Example**
7073

@@ -88,7 +91,7 @@ Create new Decoder Stream with
8891

8992
Where
9093

91-
* **options** is the optional stream options object
94+
- **options** is the optional stream options object
9295

9396
**Example**
9497

lib/libbase64.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,50 @@ function wrap(str, lineLength) {
6868
class Encoder extends Transform {
6969
constructor(options) {
7070
super();
71-
7271
// init Transform
7372
this.options = options || {};
7473

7574
if (this.options.lineLength !== false) {
7675
this.options.lineLength = this.options.lineLength || 76;
7776
}
7877

78+
this.skipStartBytes = this.options.skipStartBytes || 0;
79+
this.limitOutbutBytes = this.options.limitOutbutBytes || 0;
80+
7981
this._curLine = '';
8082
this._remainingBytes = false;
8183

8284
this.inputBytes = 0;
8385
this.outputBytes = 0;
8486
}
8587

88+
_writeChunk(chunk /*, isFinal */) {
89+
if (this.skipStartBytes) {
90+
if (chunk.length <= this.skipStartBytes) {
91+
this.skipStartBytes -= chunk.length;
92+
return;
93+
}
94+
95+
chunk = chunk.slice(this.skipStartBytes);
96+
this.skipStartBytes = 0;
97+
}
98+
99+
if (this.limitOutbutBytes) {
100+
if (this.outputBytes + chunk.length <= this.limitOutbutBytes) {
101+
// ignore, can use entire chunk
102+
} else if (this.outputBytes >= this.limitOutbutBytes) {
103+
// chunks already processed
104+
return;
105+
} else {
106+
// use partial chunk
107+
chunk = chunk.slice(0, this.limitOutbutBytes - this.outputBytes);
108+
}
109+
}
110+
111+
this.outputBytes += chunk.length;
112+
this.push(chunk);
113+
}
114+
86115
_transform(chunk, encoding, done) {
87116
if (encoding !== 'buffer') {
88117
chunk = Buffer.from(chunk, encoding);
@@ -100,8 +129,8 @@ class Encoder extends Transform {
100129
}
101130

102131
if (chunk.length % 3) {
103-
this._remainingBytes = chunk.slice(chunk.length - chunk.length % 3);
104-
chunk = chunk.slice(0, chunk.length - chunk.length % 3);
132+
this._remainingBytes = chunk.slice(chunk.length - (chunk.length % 3));
133+
chunk = chunk.slice(0, chunk.length - (chunk.length % 3));
105134
} else {
106135
this._remainingBytes = false;
107136
}
@@ -125,8 +154,7 @@ class Encoder extends Transform {
125154
}
126155

127156
if (b64) {
128-
this.outputBytes += b64.length;
129-
this.push(Buffer.from(b64, 'ascii'));
157+
this._writeChunk(Buffer.from(b64, 'ascii'), false);
130158
}
131159

132160
setImmediate(done);
@@ -136,13 +164,13 @@ class Encoder extends Transform {
136164
if (this._remainingBytes && this._remainingBytes.length) {
137165
this._curLine += encode(this._remainingBytes);
138166
}
167+
139168
if (this._curLine) {
140169
this._curLine = wrap(this._curLine, this.options.lineLength);
141-
this.outputBytes += this._curLine.length;
142-
this.push(Buffer.from(this._curLine, 'ascii'));
170+
this._writeChunk(Buffer.from(this._curLine, 'ascii'), true);
143171
this._curLine = '';
144172
}
145-
setImmediate(done);
173+
done();
146174
}
147175
}
148176

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "libbase64",
3-
"version": "1.0.3",
3+
"version": "1.1.0",
44
"description": "Encode and decode base64 encoded strings",
55
"main": "lib/libbase64.js",
66
"scripts": {
@@ -21,13 +21,14 @@
2121
},
2222
"homepage": "https://github.com/nodemailer/libbase64",
2323
"devDependencies": {
24-
"chai": "^4.1.2",
24+
"chai": "^4.2.0",
2525
"eslint-config-nodemailer": "^1.2.0",
26-
"grunt": "^1.0.3",
27-
"grunt-cli": "^1.2.0",
28-
"grunt-eslint": "^21.0.0",
26+
"eslint-config-prettier": "^6.0.0",
27+
"grunt": "^1.0.4",
28+
"grunt-cli": "^1.3.2",
29+
"grunt-eslint": "^22.0.0",
2930
"grunt-mocha-test": "^0.13.3",
30-
"mocha": "^5.2.0"
31+
"mocha": "^6.2.0"
3132
},
3233
"dependencies": {}
3334
}

0 commit comments

Comments
 (0)