pax_global_header 0000666 0000000 0000000 00000000064 13604206437 0014517 g ustar 00root root 0000000 0000000 52 comment=68a87cd7b5e436671a6b251ff8a93becb0c9d466
b64-5.0.0/ 0000775 0000000 0000000 00000000000 13604206437 0012114 5 ustar 00root root 0000000 0000000 b64-5.0.0/.gitignore 0000664 0000000 0000000 00000000154 13604206437 0014104 0 ustar 00root root 0000000 0000000 **/node_modules
**/package-lock.json
coverage.*
**/.DS_Store
**/._*
**/*.pem
**/.vs
**/.vscode
**/.idea
b64-5.0.0/.travis.yml 0000775 0000000 0000000 00000000202 13604206437 0014222 0 ustar 00root root 0000000 0000000 language: node_js
node_js:
- "12"
- "node"
sudo: false
install:
- "npm install"
os:
- "linux"
- "osx"
- "windows"
b64-5.0.0/API.md 0000664 0000000 0000000 00000002177 13604206437 0013056 0 ustar 00root root 0000000 0000000
### encode(buffer)
Base64 encode the buffer and return it as a new Buffer.
### decode(buffer)
Base64 decode the buffer and return the result as a new buffer.
### Encoder
Transform stream that base64 encodes each chunk of the stream.
Example:
```js
'use strict';
const Fs = require('fs');
const B64 = require('@hapi/b64');
const stream = Fs.createReadStream(`${__dirname}/package.json`);
const encoder = new B64.Encoder();
stream.pipe(encoder).pipe(process.stdout);
```
### Decoder
Transform stream that base64 decodes each chunk of the stream.
Example:
```js
'use strict';
const Fs = require('fs');
const B64 = require('@hapi/b64');
const stream = Fs.createReadStream(`${__dirname}/encodedfile.b64`);
const decoder = new B64.Decoder();
stream.pipe(decoder).pipe(process.stdout);
```
### base64urlEncode(value)
Encodes value of string or buffer type in Base64 or URL encoding, function will assert input value is correct.
### base64urlDecode(value)
Decodes string into Base64 or URL encoding, function throws an error on invalid input and returns a string or buffer depending on encoding provided. Default encoding is binary.
b64-5.0.0/LICENSE.md 0000775 0000000 0000000 00000002734 13604206437 0013531 0 ustar 00root root 0000000 0000000 Copyright (c) 2014-2020, Sideway Inc, and project contributors
Copyright (c) 2014, Walmart.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The names of any contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS OFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
b64-5.0.0/README.md 0000664 0000000 0000000 00000001771 13604206437 0013401 0 ustar 00root root 0000000 0000000
# @hapi/b64
#### Base64 streaming encoder and decoder.
**b64** is part of the **hapi** ecosystem and was designed to work seamlessly with the [hapi web framework](https://hapi.dev) and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out [hapi](https://hapi.dev) – they work even better together.
### Visit the [hapi.dev](https://hapi.dev) Developer Portal for tutorials, documentation, and support
## Useful resources
- [Documentation and API](https://hapi.dev/family/b64/)
- [Versions status](https://hapi.dev/resources/status/#b64) (builds, dependencies, node versions, licenses, eol)
- [Changelog](https://hapi.dev/family/b64/changelog/)
- [Project policies](https://hapi.dev/policies/)
- [Free and commercial support options](https://hapi.dev/support/)
b64-5.0.0/lib/ 0000775 0000000 0000000 00000000000 13604206437 0012662 5 ustar 00root root 0000000 0000000 b64-5.0.0/lib/decoder.js 0000775 0000000 0000000 00000006007 13604206437 0014633 0 ustar 00root root 0000000 0000000 'use strict';
/*
Decode functions adapted from:
Version 1.0 12/25/99 Copyright (C) 1999 Masanao Izumo
http://www.onicos.com/staff/iz/amuse/javascript/expert/base64.txt
*/
const Stream = require('stream');
const internals = {
decodeChars: [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
]
};
exports.decode = function (buffer) {
const decodeChars = internals.decodeChars;
const len = buffer.length;
const allocated = Math.ceil(len / 4) * 3;
const result = Buffer.alloc(allocated);
let c1;
let c2;
let c3;
let c4;
let j = 0;
for (let i = 0; i < len; ) {
do {
c1 = decodeChars[buffer[i++] & 0xff];
}
while (i < len && c1 === -1);
if (c1 === -1) {
break;
}
do {
c2 = decodeChars[buffer[i++] & 0xff];
}
while (i < len && c2 === -1);
if (c2 === -1) {
break;
}
result[j++] = (c1 << 2) | ((c2 & 0x30) >> 4);
do {
c3 = buffer[i++] & 0xff;
if (c3 === 61) { // =
return result.slice(0, j);
}
c3 = decodeChars[c3];
}
while (i < len && c3 === -1);
if (c3 === -1) {
break;
}
result[j++] = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
do {
c4 = buffer[i++] & 0xff;
if (c4 === 61) { // =
return result.slice(0, j);
}
c4 = decodeChars[c4];
}
while (i < len && c4 === -1);
if (c4 !== -1) {
result[j++] = ((c3 & 0x03) << 6) | c4;
}
}
return (j === allocated ? result : result.slice(0, j));
};
exports.Decoder = class Decoder extends Stream.Transform {
constructor() {
super();
this._reminder = null;
}
_transform(chunk, encoding, callback) {
let part = this._reminder ? Buffer.concat([this._reminder, chunk]) : chunk;
const remaining = part.length % 4;
if (remaining) {
this._reminder = part.slice(part.length - remaining);
part = part.slice(0, part.length - remaining);
}
else {
this._reminder = null;
}
this.push(exports.decode(part));
return callback();
}
_flush(callback) {
if (this._reminder) {
this.push(exports.decode(this._reminder));
}
return callback();
}
};
b64-5.0.0/lib/encoder.js 0000775 0000000 0000000 00000002134 13604206437 0014642 0 ustar 00root root 0000000 0000000 'use strict';
/*
Encode functions adapted from:
Version 1.0 12/25/99 Copyright (C) 1999 Masanao Izumo
http://www.onicos.com/staff/iz/amuse/javascript/expert/base64.txt
*/
const Stream = require('stream');
const internals = {};
exports.encode = function (buffer) {
return Buffer.from(buffer.toString('base64'));
};
exports.Encoder = class Encoder extends Stream.Transform {
constructor() {
super();
this._reminder = null;
}
_transform(chunk, encoding, callback) {
let part = this._reminder ? Buffer.concat([this._reminder, chunk]) : chunk;
const remaining = part.length % 3;
if (remaining) {
this._reminder = part.slice(part.length - remaining);
part = part.slice(0, part.length - remaining);
}
else {
this._reminder = null;
}
this.push(exports.encode(part));
return callback();
}
_flush(callback) {
if (this._reminder) {
this.push(exports.encode(this._reminder));
}
return callback();
}
};
b64-5.0.0/lib/index.js 0000775 0000000 0000000 00000002035 13604206437 0014332 0 ustar 00root root 0000000 0000000 'use strict';
const Hoek = require('@hapi/hoek');
const Decoder = require('./decoder');
const Encoder = require('./encoder');
exports.decode = Decoder.decode;
exports.encode = Encoder.encode;
exports.Decoder = Decoder.Decoder;
exports.Encoder = Encoder.Encoder;
// Base64url (RFC 4648) encode
exports.base64urlEncode = function (value, encoding) {
Hoek.assert(typeof value === 'string' || Buffer.isBuffer(value), 'value must be string or buffer');
const buf = (Buffer.isBuffer(value) ? value : Buffer.from(value, encoding || 'binary'));
return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
};
// Base64url (RFC 4648) decode
exports.base64urlDecode = function (value, encoding) {
if (typeof value !== 'string') {
throw new Error('Value not a string');
}
if (!/^[\w\-]*$/.test(value)) {
throw new Error('Invalid character');
}
const buf = Buffer.from(value, 'base64');
return (encoding === 'buffer' ? buf : buf.toString(encoding || 'binary'));
};
b64-5.0.0/package.json 0000664 0000000 0000000 00000001147 13604206437 0014405 0 ustar 00root root 0000000 0000000 {
"name": "@hapi/b64",
"description": "Base64 streaming encoder and decoder",
"version": "5.0.0",
"repository": "git://github.com/hapijs/b64",
"main": "lib/index.js",
"files": [
"lib"
],
"keywords": [
"buffer",
"base64",
"decode",
"encode",
"stream"
],
"dependencies": {
"@hapi/hoek": "9.x.x"
},
"devDependencies": {
"@hapi/code": "8.x.x",
"@hapi/lab": "22.x.x",
"@hapi/wreck": "17.x.x"
},
"scripts": {
"test": "lab -a @hapi/code -t 100 -L",
"test-cov-html": "lab -a @hapi/code -r html -o coverage.html"
},
"license": "BSD-3-Clause"
}
b64-5.0.0/test/ 0000775 0000000 0000000 00000000000 13604206437 0013073 5 ustar 00root root 0000000 0000000 b64-5.0.0/test/index.js 0000775 0000000 0000000 00000016261 13604206437 0014551 0 ustar 00root root 0000000 0000000 'use strict';
const Crypto = require('crypto');
const Fs = require('fs');
const Path = require('path');
const Stream = require('stream');
const Util = require('util');
const B64 = require('..');
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const Wreck = require('@hapi/wreck');
const internals = {};
const { describe, it } = exports.lab = Lab.script();
const expect = Code.expect;
it('pipes buffer through encoder and decoder', async () => {
const buffer = Crypto.randomBytes(1024);
const payload = await internals.test(buffer);
expect(payload).to.equal(buffer.toString());
});
describe('decode()', () => {
it('decodes a short buffer (1)', () => {
const value = '0';
const encoded = B64.encode(Buffer.from(value));
expect(B64.decode(encoded).toString()).to.equal(value);
});
it('decodes an incomplete buffer', () => {
const value = '';
const encoded = Buffer.from('A');
expect(B64.decode(encoded).toString()).to.equal(value);
});
it('decodes an whitespace buffer', () => {
const value = '';
const encoded = Buffer.from(' ');
expect(B64.decode(encoded).toString()).to.equal(value);
});
it('decodes a buffer with whitespace', () => {
const value = '0123456789';
const encoded = Buffer.from('M D\nEy\tMz\r\nQ1Nj\rc4\r\nO Q ==');
expect(B64.decode(encoded).toString()).to.equal(value);
});
it('decodes a buffer with 4th invalid character', () => {
const value = '01';
const encoded = Buffer.from('MDE$');
expect(B64.decode(encoded).toString()).to.equal(value);
});
});
describe('Encoder', () => {
it('process remainder', async () => {
const buffer = [Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(5)];
const payload = await internals.test(buffer);
expect(payload).to.equal(Buffer.concat(buffer).toString());
});
it('flushes remainder', async () => {
const buffer = [Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(1)];
const payload = await internals.test(buffer);
expect(payload).to.equal(Buffer.concat(buffer).toString());
});
it('skips empty remainder', async () => {
const buffer = [Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(5), Crypto.randomBytes(3)];
const payload = await internals.test(buffer);
expect(payload).to.equal(Buffer.concat(buffer).toString());
});
});
describe('Decoder', () => {
it('process remainder', async () => {
const value = Crypto.randomBytes(100);
const encoded = B64.encode(value);
const stream = new internals.Payload([encoded.slice(0, 3), encoded.slice(3, 9), encoded.slice(9)]);
const source = stream.pipe(new B64.Decoder());
const payload = await Wreck.read(source);
expect(payload.toString()).to.equal(value.toString());
});
it('flushes remainder', async () => {
const value = '0123456789';
const encoded = B64.encode(Buffer.from(value)); // MDEyMzQ1Njc4OQ==
const stream = new internals.Payload([encoded.slice(0, 14)]);
const source = stream.pipe(new B64.Decoder());
const payload = await Wreck.read(source);
expect(payload.toString()).to.equal(value.toString());
});
});
describe('Base64Url', () => {
const base64str = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w';
const str = unescape('%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29*+%2C-./0123456789%3A%3B%3C%3D%3E%3F@ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF');
describe('base64urlEncode()', () => {
it('should assert function input is a string or buffer', () => {
const number = 1024;
const func = () => {
return B64.base64urlEncode(number);
};
expect(func).throws(Error);
});
it('should base64 URL-safe a string', () => {
expect(B64.base64urlEncode(str)).to.equal(base64str);
});
it('encodes a buffer', () => {
expect(B64.base64urlEncode(Buffer.from(str, 'binary'))).to.equal(base64str);
});
it('should base64 URL-safe a hex string', () => {
const buffer = Buffer.from(str, 'binary');
expect(B64.base64urlEncode(buffer.toString('hex'), 'hex')).to.equal(base64str);
});
it('works on larger input strings', () => {
const input = Fs.readFileSync(Path.join(__dirname, 'index.js')).toString();
const encoded = B64.base64urlEncode(input);
expect(encoded).to.not.contain('+');
expect(encoded).to.not.contain('/');
const decoded = B64.base64urlDecode(encoded);
expect(decoded).to.equal(input);
});
});
describe('base64urlDecode()', () => {
it('should un-base64 URL-safe a string', () => {
expect(B64.base64urlDecode(base64str)).to.equal(str);
});
it('should un-base64 URL-safe a string into hex', () => {
expect(B64.base64urlDecode(base64str, 'hex')).to.equal(Buffer.from(str, 'binary').toString('hex'));
});
it('should un-base64 URL-safe a string and return a buffer', () => {
const buf = B64.base64urlDecode(base64str, 'buffer');
expect(buf instanceof Buffer).to.equal(true);
expect(buf.toString('binary')).to.equal(str);
});
it('throws error on invalid input', () => {
expect(() => B64.base64urlDecode(1024)).to.throw('Value not a string');
});
it('throws error on invalid input', () => {
expect(() => B64.base64urlDecode('*')).to.throw('Invalid character');
});
});
});
internals.Payload = function (payload) {
Stream.Readable.call(this);
this._data = [].concat(payload);
this._position = 0;
};
Util.inherits(internals.Payload, Stream.Readable);
internals.Payload.prototype._read = function (size) {
const chunk = this._data[this._position++];
if (chunk) {
this.push(chunk);
}
else {
this.push(null);
}
};
internals.test = async function (buffer) {
const stream = new internals.Payload(buffer);
const source = stream.pipe(new B64.Encoder()).pipe(new B64.Decoder());
const payload = await Wreck.read(source);
return payload.toString();
};