libbase64 1.0.2 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc CHANGED
@@ -1,3 +1,11 @@
1
1
  {
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
+ }
3
11
  }
package/.prettierrc.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ printWidth: 160,
3
+ tabWidth: 4,
4
+ singleQuote: true
5
+ };
package/README.md CHANGED
@@ -22,7 +22,7 @@ Encode Buffer objects or unicode strings with
22
22
 
23
23
  Where
24
24
 
25
- * **val** is a Buffer or an unicode string
25
+ - **val** is a Buffer or an unicode string
26
26
 
27
27
  **Example**
28
28
 
@@ -39,8 +39,8 @@ To enforce soft line breaks on lines longer than selected amount of characters,
39
39
 
40
40
  Where
41
41
 
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
44
44
 
45
45
  **Example**
46
46
 
@@ -63,8 +63,12 @@ Create new Encoder Stream with
63
63
 
64
64
  Where
65
65
 
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)
71
+ - **options.startPadding** (String) Optional. Fills first line with provided padding string. Usually goes together with skipStartBytes to get line folding correct.
68
72
 
69
73
  **Example**
70
74
 
@@ -88,7 +92,7 @@ Create new Decoder Stream with
88
92
 
89
93
  Where
90
94
 
91
- * **options** is the optional stream options object
95
+ - **options** is the optional stream options object
92
96
 
93
97
  **Example**
94
98
 
package/lib/libbase64.js CHANGED
@@ -68,21 +68,59 @@ function wrap(str, lineLength) {
68
68
  class Encoder extends Transform {
69
69
  constructor(options) {
70
70
  super();
71
-
72
71
  // init Transform
73
72
  this.options = options || {};
74
73
 
75
74
  if (this.options.lineLength !== false) {
76
- this.options.lineLength = this.options.lineLength || 76;
75
+ this.options.lineLength = Number(this.options.lineLength) || 76;
77
76
  }
78
77
 
79
- this._curLine = '';
78
+ this.skipStartBytes = Number(this.options.skipStartBytes) || 0;
79
+ this.limitOutbutBytes = Number(this.options.limitOutbutBytes) || 0;
80
+
81
+ // startPadding can be used together with skipStartBytes
82
+ this._curLine = this.options.startPadding || '';
80
83
  this._remainingBytes = false;
81
84
 
82
85
  this.inputBytes = 0;
83
86
  this.outputBytes = 0;
84
87
  }
85
88
 
89
+ _writeChunk(chunk /*, isFinal */) {
90
+ if (this.skipStartBytes) {
91
+ if (chunk.length <= this.skipStartBytes) {
92
+ this.skipStartBytes -= chunk.length;
93
+ return;
94
+ }
95
+
96
+ chunk = chunk.slice(this.skipStartBytes);
97
+ this.skipStartBytes = 0;
98
+ }
99
+
100
+ if (this.limitOutbutBytes) {
101
+ if (this.outputBytes + chunk.length <= this.limitOutbutBytes) {
102
+ // ignore, can use entire chunk
103
+ } else if (this.outputBytes >= this.limitOutbutBytes) {
104
+ // chunks already processed
105
+ return;
106
+ } else {
107
+ // use partial chunk
108
+ chunk = chunk.slice(0, this.limitOutbutBytes - this.outputBytes);
109
+ }
110
+ }
111
+
112
+ this.outputBytes += chunk.length;
113
+ this.push(chunk);
114
+ }
115
+
116
+ _getWrapped(str, isFinal) {
117
+ str = wrap(str, this.options.lineLength);
118
+ if (!isFinal && str.length === this.options.lineLength) {
119
+ str += '\r\n';
120
+ }
121
+ return str;
122
+ }
123
+
86
124
  _transform(chunk, encoding, done) {
87
125
  if (encoding !== 'buffer') {
88
126
  chunk = Buffer.from(chunk, encoding);
@@ -100,8 +138,8 @@ class Encoder extends Transform {
100
138
  }
101
139
 
102
140
  if (chunk.length % 3) {
103
- this._remainingBytes = chunk.slice(chunk.length - chunk.length % 3);
104
- chunk = chunk.slice(0, chunk.length - chunk.length % 3);
141
+ this._remainingBytes = chunk.slice(chunk.length - (chunk.length % 3));
142
+ chunk = chunk.slice(0, chunk.length - (chunk.length % 3));
105
143
  } else {
106
144
  this._remainingBytes = false;
107
145
  }
@@ -109,7 +147,7 @@ class Encoder extends Transform {
109
147
  let b64 = this._curLine + encode(chunk);
110
148
 
111
149
  if (this.options.lineLength) {
112
- b64 = wrap(b64, this.options.lineLength);
150
+ b64 = this._getWrapped(b64);
113
151
 
114
152
  // remove last line as it is still most probably incomplete
115
153
  let lastLF = b64.lastIndexOf('\n');
@@ -125,8 +163,7 @@ class Encoder extends Transform {
125
163
  }
126
164
 
127
165
  if (b64) {
128
- this.outputBytes += b64.length;
129
- this.push(Buffer.from(b64, 'ascii'));
166
+ this._writeChunk(Buffer.from(b64, 'ascii'), false);
130
167
  }
131
168
 
132
169
  setImmediate(done);
@@ -136,13 +173,13 @@ class Encoder extends Transform {
136
173
  if (this._remainingBytes && this._remainingBytes.length) {
137
174
  this._curLine += encode(this._remainingBytes);
138
175
  }
176
+
139
177
  if (this._curLine) {
140
- this._curLine = wrap(this._curLine, this.options.lineLength);
141
- this.outputBytes += this._curLine.length;
142
- this.push(Buffer.from(this._curLine, 'ascii'));
178
+ this._curLine = this._getWrapped(this._curLine, true);
179
+ this._writeChunk(Buffer.from(this._curLine, 'ascii'), true);
143
180
  this._curLine = '';
144
181
  }
145
- setImmediate(done);
182
+ done();
146
183
  }
147
184
  }
148
185
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libbase64",
3
- "version": "1.0.2",
3
+ "version": "1.2.1",
4
4
  "description": "Encode and decode base64 encoded strings",
5
5
  "main": "lib/libbase64.js",
6
6
  "scripts": {
@@ -21,13 +21,14 @@
21
21
  },
22
22
  "homepage": "https://github.com/nodemailer/libbase64",
23
23
  "devDependencies": {
24
- "chai": "^4.1.2",
25
- "eslint-config-nodemailer": "^1.2.0",
26
- "grunt": "^1.0.1",
27
- "grunt-cli": "^1.2.0",
28
- "grunt-eslint": "^20.1.0",
29
- "grunt-mocha-test": "^0.13.3",
30
- "mocha": "^4.0.1"
24
+ "chai": "4.2.0",
25
+ "eslint-config-nodemailer": "1.2.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",
30
+ "grunt-mocha-test": "0.13.3",
31
+ "mocha": "6.2.0"
31
32
  },
32
33
  "dependencies": {}
33
34
  }