libbase64 0.1.0

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/.npmignore ADDED
@@ -0,0 +1,4 @@
1
+ .travis.yml
2
+ .jshintrc
3
+ Gruntfile.js
4
+ test
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Andris Reinman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # libbase64
2
+
3
+ Encode and decode base64 strings.
4
+
5
+ ## Usage
6
+
7
+ Install with npm
8
+
9
+ npm install libbase64
10
+
11
+ Require in your script
12
+
13
+ ```javascript
14
+ var libbase64 = require('libbase64');
15
+ ```
16
+
17
+ ### Encode values
18
+
19
+ Encode Buffer objects or unicode strings with
20
+
21
+ libbase64.encode(val) → String
22
+
23
+ Where
24
+
25
+ * **val** is a Buffer or an unicode string
26
+
27
+ **Example**
28
+
29
+ ```javascript
30
+ libbase64.encode('jõgeva');
31
+ // asO1Z2V2YQ==
32
+ ```
33
+
34
+ ### Wrap encoded values
35
+
36
+ To enforce soft line breaks on lines longer than selected amount of characters, use `wrap`
37
+
38
+ libbase64.wrap(str[, lineLength]) → String
39
+
40
+ Where
41
+
42
+ * **str** is a base64 encoded string
43
+ * **lineLength** (defaults to 76) is the maximum allowed line length
44
+
45
+ **Example**
46
+
47
+ ```javascript
48
+ libbase64.wrap('asO1Z2V2asO1Z2V2asO1Z2V2YQ==', 10)
49
+ // asO1Z2V2as\r\n
50
+ // O1Z2V2asO1\r\n
51
+ // Z2V2YQ==
52
+ ```
53
+
54
+ ### Transform Streams
55
+
56
+ `libbase64` makes it possible to encode and decode streams with `libbase64.Encoder` and `libbase64.Decoder` constructors.
57
+
58
+ ### Encoder Stream
59
+
60
+ Create new Encoder Stream with
61
+
62
+ var encoder = new libbase64.Encoder([options])
63
+
64
+ Where
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 characters (or set to `false` to turn the soft wrapping off completely)
67
+
68
+ **Example**
69
+
70
+ The following example script reads in a file, encodes it to base64 and saves the output to a file.
71
+
72
+ ```javascript
73
+ var libbase64 = require('libbase64');
74
+ var fs = require('fs');
75
+ var source = fs.createReadStream('source.txt');
76
+ var encoded = fs.createReadStream('encoded.txt');
77
+ var encoder = new libbase64.Encoder();
78
+
79
+ source.pipe(encoder).pipe(encoded);
80
+ ```
81
+
82
+ ### Decoder Stream
83
+
84
+ Create new Decoder Stream with
85
+
86
+ var decoder = new libbase64.Decoder([options])
87
+
88
+ Where
89
+
90
+ * **options** is the optional stream options object
91
+
92
+ **Example**
93
+
94
+ The following example script reads in a file in base64 encoding, decodes it and saves the output to a file.
95
+
96
+ ```javascript
97
+ var libbase64 = require('libbase64');
98
+ var fs = require('fs');
99
+ var encoded = fs.createReadStream('encoded.txt');
100
+ var dest = fs.createReadStream('dest.txt');
101
+ var decoder = new libbase64.Decoder();
102
+
103
+ encoded.pipe(decoder).pipe(dest);
104
+ ```
105
+
106
+ ## License
107
+
108
+ **MIT**
@@ -0,0 +1,201 @@
1
+ 'use strict';
2
+
3
+ var stream = require('stream');
4
+ var util = require('util');
5
+ var Transform = stream.Transform;
6
+
7
+ // expose to the world
8
+ module.exports = {
9
+ encode: encode,
10
+ decode: decode,
11
+ wrap: wrap,
12
+ Encoder: Encoder,
13
+ Decoder: Decoder
14
+ };
15
+
16
+ /**
17
+ * Encodes a Buffer into a base64 encoded string
18
+ *
19
+ * @param {Buffer} buffer Buffer to convert
20
+ * @returns {String} base64 encoded string
21
+ */
22
+ function encode(buffer) {
23
+ if (typeof buffer === 'string') {
24
+ buffer = new Buffer(buffer, 'utf-8');
25
+ }
26
+
27
+ return buffer.toString('base64');
28
+ }
29
+
30
+ /**
31
+ * Decodes a base64 encoded string to a Buffer object
32
+ *
33
+ * @param {String} str base64 encoded string
34
+ * @returns {Buffer} Decoded value
35
+ */
36
+ function decode(str) {
37
+ str = (str || '');
38
+ return new Buffer(str, 'base64');
39
+ }
40
+
41
+ /**
42
+ * Adds soft line breaks to a base64 string
43
+ *
44
+ * @param {String} str base64 encoded string that might need line wrapping
45
+ * @param {Number} [lineLength=76] Maximum allowed length for a line
46
+ * @returns {String} Soft-wrapped base64 encoded string
47
+ */
48
+ function wrap(str, lineLength) {
49
+ str = (str || '').toString();
50
+ lineLength = lineLength || 76;
51
+
52
+ if (str.length <= lineLength) {
53
+ return str;
54
+ }
55
+
56
+ return str.replace(new RegExp('.{' + lineLength + '}', 'g'), '$&\r\n').trim();
57
+ }
58
+
59
+ /**
60
+ * Creates a transform stream for encoding data to base64 encoding
61
+ *
62
+ * @constructor
63
+ * @param {Object} options Stream options
64
+ * @param {Number} [options.lineLength=76] Maximum lenght for lines, set to false to disable wrapping
65
+ */
66
+ function Encoder(options) {
67
+ // init Transform
68
+ this.options = options || {};
69
+
70
+ if (this.options.lineLength !== false) {
71
+ this.options.lineLength = this.options.lineLength || 76;
72
+ }
73
+
74
+ this._curLine = '';
75
+ this._remainingBytes = false;
76
+
77
+ this.inputBytes = 0;
78
+ this.outputBytes = 0;
79
+
80
+ Transform.call(this, this.options);
81
+ }
82
+ util.inherits(Encoder, Transform);
83
+
84
+ Encoder.prototype._transform = function(chunk, encoding, done) {
85
+ var b64, _self = this;
86
+
87
+ if (encoding !== 'buffer') {
88
+ chunk = new Buffer(chunk, encoding);
89
+ }
90
+
91
+ if (!chunk || !chunk.length) {
92
+ return done();
93
+ }
94
+
95
+ this.inputBytes += chunk.length;
96
+
97
+ if (this._remainingBytes && this._remainingBytes.length) {
98
+ chunk = Buffer.concat([this._remainingBytes, chunk]);
99
+ this._remainingBytes = false;
100
+ }
101
+
102
+ if (chunk.length % 3) {
103
+ this._remainingBytes = chunk.slice(chunk.length - chunk.length % 3);
104
+ chunk = chunk.slice(0, chunk.length - chunk.length % 3);
105
+ } else {
106
+ this._remainingBytes = false;
107
+ }
108
+
109
+ b64 = this._curLine + encode(chunk);
110
+
111
+ if (this.options.lineLength) {
112
+ b64 = wrap(b64, this.options.lineLength);
113
+ b64 = b64.replace(/(^|\n)([^\n]*)$/, function(match, lineBreak, lastLine) {
114
+ _self._curLine = lastLine;
115
+ return lineBreak;
116
+ });
117
+ }
118
+
119
+ if (b64) {
120
+ this.outputBytes += b64.length;
121
+ this.push(b64);
122
+ }
123
+
124
+ done();
125
+ };
126
+
127
+ Encoder.prototype._flush = function(done) {
128
+ if (this._remainingBytes && this._remainingBytes.length) {
129
+ this._curLine += encode(this._remainingBytes);
130
+ }
131
+ if (this._curLine) {
132
+ this._curLine = wrap(this._curLine, this.options.lineLength);
133
+ this.outputBytes += this._curLine.length;
134
+ this.push(this._curLine, 'ascii');
135
+ this._curLine = '';
136
+ }
137
+ done();
138
+ };
139
+
140
+ /**
141
+ * Creates a transform stream for decoding base64 encoded strings
142
+ *
143
+ * @constructor
144
+ * @param {Object} options Stream options
145
+ */
146
+ function Decoder(options) {
147
+ // init Transform
148
+ this.options = options || {};
149
+ this._curLine = '';
150
+
151
+ this.inputBytes = 0;
152
+ this.outputBytes = 0;
153
+
154
+ Transform.call(this, this.options);
155
+ }
156
+ util.inherits(Decoder, Transform);
157
+
158
+ Decoder.prototype._transform = function(chunk, encoding, done) {
159
+ var b64, buf;
160
+
161
+ chunk = chunk.toString('ascii');
162
+
163
+ if (!chunk || !chunk.length) {
164
+ return done();
165
+ }
166
+
167
+ this.inputBytes += chunk.length;
168
+
169
+ b64 = (this._curLine + chunk);
170
+ this._curLine = '';
171
+
172
+ b64 = b64.replace(/[^a-zA-Z0-9+\/=]/g, '');
173
+
174
+ if (b64.length % 4) {
175
+ this._curLine = b64.substr(-b64.length % 4);
176
+ if (this._curLine.length == b64.length) {
177
+ b64 = '';
178
+ } else {
179
+ b64 = b64.substr(0, this._curLine.length);
180
+ }
181
+ }
182
+
183
+ if (b64) {
184
+ buf = decode(b64);
185
+ this.outputBytes += buf.length;
186
+ this.push(buf);
187
+ }
188
+
189
+ done();
190
+ };
191
+
192
+ Decoder.prototype._flush = function(done) {
193
+ var b64, buf;
194
+ if (this._curLine) {
195
+ buf = decode(this._curLine);
196
+ this.outputBytes += buf.length;
197
+ this.push(buf);
198
+ this._curLine = '';
199
+ }
200
+ done();
201
+ };
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "libbase64",
3
+ "version": "0.1.0",
4
+ "description": "Encode and decode base64 encoded strings",
5
+ "main": "lib/libbase64.js",
6
+ "scripts": {
7
+ "test": "grunt"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git://github.com/andris9/libbase64.git"
12
+ },
13
+ "keywords": [
14
+ "base64",
15
+ "mime"
16
+ ],
17
+ "author": "Andris Reinman",
18
+ "license": "MIT",
19
+ "bugs": {
20
+ "url": "https://github.com/andris9/libbase64/issues"
21
+ },
22
+ "homepage": "https://github.com/andris9/libbase64",
23
+ "devDependencies": {
24
+ "chai": "~1.8.1",
25
+ "grunt": "~0.4.1",
26
+ "grunt-contrib-jshint": "~0.8.0",
27
+ "grunt-mocha-test": "~0.10.0"
28
+ }
29
+ }