Skip to content

Commit

Permalink
Merge pull request #227 from kanongil/no-new-buffer
Browse files Browse the repository at this point in the history
Don't use deprecated new Buffer() api
  • Loading branch information
nlf authored Jan 30, 2018
2 parents 1537365 + 8cc7c05 commit dfadda7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internals.escapeJavaScriptChar = function (charCode) {
return '\\u' + internals.padLeft('' + charCode, 4);
}

const hexValue = new Buffer(String.fromCharCode(charCode), 'ascii').toString('hex');
const hexValue = Buffer.from(String.fromCharCode(charCode), 'ascii').toString('hex');
return '\\x' + internals.padLeft(hexValue, 2);
};

Expand All @@ -109,7 +109,7 @@ internals.escapeHtmlChar = function (charCode) {
return '&#' + charCode + ';';
}

const hexValue = new Buffer(String.fromCharCode(charCode), 'ascii').toString('hex');
const hexValue = Buffer.from(String.fromCharCode(charCode), 'ascii').toString('hex');
return '&#x' + internals.padLeft(hexValue, 2) + ';';
};

Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.clone = function (obj, seen) {

if (!Array.isArray(obj)) {
if (Buffer.isBuffer(obj)) {
newObj = new Buffer(obj);
newObj = Buffer.from(obj);
}
else if (obj instanceof Date) {
newObj = new Date(obj.getTime());
Expand Down Expand Up @@ -779,7 +779,7 @@ exports.escapeRegex = function (string) {
exports.base64urlEncode = function (value, encoding) {

exports.assert(typeof value === 'string' || Buffer.isBuffer(value), 'value must be string or buffer');
const buf = (Buffer.isBuffer(value) ? value : new Buffer(value, encoding || 'binary'));
const buf = (Buffer.isBuffer(value) ? value : Buffer.from(value, encoding || 'binary'));
return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
};

Expand All @@ -798,7 +798,7 @@ exports.base64urlDecode = function (value, encoding) {
throw new Error('Invalid character');
}

const buf = new Buffer(value, 'base64');
const buf = Buffer.from(value, 'base64');
return (encoding === 'buffer' ? buf : buf.toString(encoding || 'binary'));
};

Expand Down
25 changes: 14 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,18 @@ describe('clone()', () => {
it('should copy a buffer', () => {

const tls = {
key: new Buffer([1, 2, 3, 4, 5]),
cert: new Buffer([1, 2, 3, 4, 5, 6, 10])
key: Buffer.from([1, 2, 3, 4, 5]),
cert: Buffer.from([1, 2, 3, 4, 5, 6, 10])
};

const copiedTls = Hoek.clone(tls);
expect(Buffer.isBuffer(copiedTls.key)).to.equal(true);
expect(JSON.stringify(copiedTls.key)).to.equal(JSON.stringify(tls.key));
expect(Buffer.isBuffer(copiedTls.cert)).to.equal(true);
expect(JSON.stringify(copiedTls.cert)).to.equal(JSON.stringify(tls.cert));

tls.key.write('hi');
expect(JSON.stringify(copiedTls.key)).to.not.equal(JSON.stringify(tls.key));
});

it('clones an object with a prototype', () => {
Expand Down Expand Up @@ -577,7 +580,7 @@ describe('merge()', () => {

it('overrides Buffer', () => {

const a = { x: new Buffer('abc') };
const a = { x: Buffer.from('abc') };

Hoek.merge({ x: {} }, a);
expect(a.x.toString()).to.equal('abc');
Expand Down Expand Up @@ -1061,11 +1064,11 @@ describe('deepEqual()', () => {

it('compares buffers', () => {

expect(Hoek.deepEqual(new Buffer([1, 2, 3]), new Buffer([1, 2, 3]))).to.be.true();
expect(Hoek.deepEqual(new Buffer([1, 2, 3]), new Buffer([1, 3, 2]))).to.be.false();
expect(Hoek.deepEqual(new Buffer([1, 2, 3]), new Buffer([1, 2]))).to.be.false();
expect(Hoek.deepEqual(new Buffer([1, 2, 3]), {})).to.be.false();
expect(Hoek.deepEqual(new Buffer([1, 2, 3]), [1, 2, 3])).to.be.false();
expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3]))).to.be.true();
expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 3, 2]))).to.be.false();
expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), Buffer.from([1, 2]))).to.be.false();
expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), {})).to.be.false();
expect(Hoek.deepEqual(Buffer.from([1, 2, 3]), [1, 2, 3])).to.be.false();
});

it('compares objects', () => {
Expand Down Expand Up @@ -1887,12 +1890,12 @@ describe('Base64Url', () => {

it('encodes a buffer', () => {

expect(Hoek.base64urlEncode(new Buffer(str, 'binary'))).to.equal(base64str);
expect(Hoek.base64urlEncode(Buffer.from(str, 'binary'))).to.equal(base64str);
});

it('should base64 URL-safe a hex string', () => {

const buffer = new Buffer(str, 'binary');
const buffer = Buffer.from(str, 'binary');
expect(Hoek.base64urlEncode(buffer.toString('hex'), 'hex')).to.equal(base64str);
});

Expand All @@ -1918,7 +1921,7 @@ describe('Base64Url', () => {

it('should un-base64 URL-safe a string into hex', () => {

expect(Hoek.base64urlDecode(base64str, 'hex')).to.equal(new Buffer(str, 'binary').toString('hex'));
expect(Hoek.base64urlDecode(base64str, 'hex')).to.equal(Buffer.from(str, 'binary').toString('hex'));
});

it('should un-base64 URL-safe a string and return a buffer', () => {
Expand Down

0 comments on commit dfadda7

Please sign in to comment.