Skip to content

Commit

Permalink
Use Buffer.(from|alloc) instead of deprecated Buffer API
Browse files Browse the repository at this point in the history
This also includes a dependnecy on a polyfill targeting older Node.js
versions where Buffer.alloc() and Buffer.from() API is not implemented
(Node.js < 4.5.0 and some 5.x versions).

Fixes: #102

Ref: nodejs/node#19079
Ref: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor
Ref: https://nodejs.org/api/buffer.html#buffer_class_buffer
Ref: https://github.com/ChALkeR/safer-buffer/blob/master/Porting-Buffer.md
  • Loading branch information
ChALkeR authored and indutny committed Jan 6, 2020
1 parent 94d6262 commit e186699
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 66 deletions.
4 changes: 2 additions & 2 deletions lib/asn1/base/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const inherits = require('inherits');
const Reporter = require('../base/reporter').Reporter;
const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

function DecoderBuffer(base, options) {
Reporter.call(this, options);
Expand Down Expand Up @@ -127,7 +127,7 @@ EncoderBuffer.isEncoderBuffer = function isEncoderBuffer(data) {

EncoderBuffer.prototype.join = function join(out, offset) {
if (!out)
out = new Buffer(this.length);
out = Buffer.alloc(this.length);
if (!offset)
offset = 0;

Expand Down
4 changes: 2 additions & 2 deletions lib/asn1/decoders/pem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const inherits = require('inherits');
const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

const DERDecoder = require('./der');

Expand Down Expand Up @@ -46,6 +46,6 @@ PEMDecoder.prototype.decode = function decode(data, options) {
// Remove excessive symbols
base64.replace(/[^a-z0-9+/=]+/gi, '');

const input = new Buffer(base64, 'base64');
const input = Buffer.from(base64, 'base64');
return DERDecoder.prototype.decode.call(this, input, options);
};
16 changes: 8 additions & 8 deletions lib/asn1/encoders/der.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const inherits = require('inherits');
const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;
const Node = require('../base/node');

// Import DER constants
Expand Down Expand Up @@ -37,7 +37,7 @@ DERNode.prototype._encodeComposite = function encodeComposite(tag,

// Short form
if (content.length < 0x80) {
const header = new Buffer(2);
const header = Buffer.alloc(2);
header[0] = encodedTag;
header[1] = content.length;
return this._createEncoderBuffer([ header, content ]);
Expand All @@ -49,7 +49,7 @@ DERNode.prototype._encodeComposite = function encodeComposite(tag,
for (let i = content.length; i >= 0x100; i >>= 8)
lenOctets++;

const header = new Buffer(1 + 1 + lenOctets);
const header = Buffer.alloc(1 + 1 + lenOctets);
header[0] = encodedTag;
header[1] = 0x80 | lenOctets;

Expand All @@ -63,7 +63,7 @@ DERNode.prototype._encodeStr = function encodeStr(str, tag) {
if (tag === 'bitstr') {
return this._createEncoderBuffer([ str.unused | 0, str.data ]);
} else if (tag === 'bmpstr') {
const buf = new Buffer(str.length * 2);
const buf = Buffer.alloc(str.length * 2);
for (let i = 0; i < str.length; i++) {
buf.writeUInt16BE(str.charCodeAt(i), i * 2);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ DERNode.prototype._encodeObjid = function encodeObjid(id, values, relative) {
size++;
}

const objid = new Buffer(size);
const objid = Buffer.alloc(size);
let offset = objid.length - 1;
for (let i = id.length - 1; i >= 0; i--) {
let ident = id[i];
Expand Down Expand Up @@ -199,15 +199,15 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) {
if (!num.sign && numArray[0] & 0x80) {
numArray.unshift(0);
}
num = new Buffer(numArray);
num = Buffer.from(numArray);
}

if (Buffer.isBuffer(num)) {
let size = num.length;
if (num.length === 0)
size++;

const out = new Buffer(size);
const out = Buffer.alloc(size);
num.copy(out);
if (num.length === 0)
out[0] = 0;
Expand All @@ -233,7 +233,7 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) {
out.unshift(0);
}

return this._createEncoderBuffer(new Buffer(out));
return this._createEncoderBuffer(Buffer.from(out));
};

DERNode.prototype._encodeBool = function encodeBool(value) {
Expand Down
21 changes: 10 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"dependencies": {
"bn.js": "^4.0.0",
"inherits": "^2.0.1",
"minimalistic-assert": "^1.0.0"
"minimalistic-assert": "^1.0.0",
"safer-buffer": "^2.1.0"
}
}
10 changes: 5 additions & 5 deletions rfc/2560/test/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
const assert = require('assert');
const rfc2560 = require('..');

const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

describe('asn1.js RFC2560', function() {
it('should decode OCSP response', function() {
const data = new Buffer(
const data = Buffer.from(
'308201d40a0100a08201cd308201c906092b0601050507300101048201ba308201b630' +
'819fa216041499e4405f6b145e3e05d9ddd36354fc62b8f700ac180f32303133313133' +
'303037343531305a30743072304a300906052b0e03021a050004140226ee2f5fa28108' +
Expand Down Expand Up @@ -58,8 +58,8 @@ describe('asn1.js RFC2560', function() {
{
reqCert: {
hashAlgorithm: { algorithm: [ 1, 3, 14, 3, 2, 26 ] },
issuerNameHash: new Buffer('01', 'hex'),
issuerKeyHash: new Buffer('02', 'hex'),
issuerNameHash: Buffer.from('01', 'hex'),
issuerKeyHash: Buffer.from('02', 'hex'),
serialNumber: 0x2b
}
}
Expand All @@ -68,7 +68,7 @@ describe('asn1.js RFC2560', function() {
{
extnID: [ 1, 3, 6, 1, 5, 5, 7, 48, 1, 2 ],
critical: false,
extnValue: new Buffer('03', 'hex')
extnValue: Buffer.from('03', 'hex')
}
]
};
Expand Down
8 changes: 4 additions & 4 deletions rfc/5280/test/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require('fs');
const asn1 = require('../../../');
const rfc5280 = require('..');

const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

describe('asn1.js RFC5280', function() {

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('asn1.js RFC5280', function() {
});

it('should decode AuthorityInfoAccess', function() {
const data = new Buffer('305a302b06082b06010505073002861f687474703a2f2f70' +
const data = Buffer.from('305a302b06082b06010505073002861f687474703a2f2f70' +
'6b692e676f6f676c652e636f6d2f47494147322e63727430' +
'2b06082b06010505073001861f687474703a2f2f636c6965' +
'6e7473312e676f6f676c652e636f6d2f6f637370',
Expand All @@ -60,7 +60,7 @@ describe('asn1.js RFC5280', function() {
});

it('should decode directoryName in GeneralName', function() {
const data = new Buffer('a411300f310d300b06022a03160568656c6c6f', 'hex');
const data = Buffer.from('a411300f310d300b06022a03160568656c6c6f', 'hex');

const name = rfc5280.GeneralName.decode(data, 'der');
assert.equal(name.type, 'directoryName');
Expand Down Expand Up @@ -124,7 +124,7 @@ describe('asn1.js RFC5280', function() {
onlyContainsCACerts: false,
indirectCRL: true,
onlyContainsAttributeCerts: false,
onlySomeReasons: { unused: 0, data: new Buffer('asdf') }
onlySomeReasons: { unused: 0, data: Buffer.from('asdf') }
};

data = rfc5280.IssuingDistributionPoint.encode(input);
Expand Down
22 changes: 11 additions & 11 deletions test/der-decode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const assert = require('assert');
const asn1 = require('..');

const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

describe('asn1.js DER decoder', function() {
it('should propagate implicit tag', function() {
Expand All @@ -20,7 +20,7 @@ describe('asn1.js DER decoder', function() {
);
});

const out = A.decode(new Buffer('300720050403313233', 'hex'), 'der');
const out = A.decode(Buffer.from('300720050403313233', 'hex'), 'der');
assert.equal(out.a.b.toString(), '123');
});

Expand All @@ -31,7 +31,7 @@ describe('asn1.js DER decoder', function() {
this.optional().key('opt').bool()
);
});
const out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
const out = A.decode(Buffer.from('30030101ff', 'hex'), 'der');
assert.deepEqual(out, { 'key': true });
});

Expand All @@ -42,14 +42,14 @@ describe('asn1.js DER decoder', function() {
this.optional().key('opt').octstr().def('default')
);
});
const out = A.decode(new Buffer('30030101ff', 'hex'), 'der');
const out = A.decode(Buffer.from('30030101ff', 'hex'), 'der');
assert.deepEqual(out, { 'key': true, 'opt': 'default' });
});

function test(name, model, inputHex, expected) {
it(name, function() {
const M = asn1.define('Model', model);
const decoded = M.decode(new Buffer(inputHex,'hex'), 'der');
const decoded = M.decode(Buffer.from(inputHex,'hex'), 'der');
assert.deepEqual(decoded, expected);
});
}
Expand All @@ -69,7 +69,7 @@ describe('asn1.js DER decoder', function() {
this.optional().use(B);
});

const out = A.decode(new Buffer('020101', 'hex'), 'der');
const out = A.decode(Buffer.from('020101', 'hex'), 'der');
assert.equal(out.toString(10), '1');
});

Expand All @@ -81,7 +81,7 @@ describe('asn1.js DER decoder', function() {

test('should decode objDesc', function() {
this.objDesc();
}, '0703323830', new Buffer('280'));
}, '0703323830', Buffer.from('280'));

test('should decode bmpstr', function() {
this.bmpstr();
Expand All @@ -108,7 +108,7 @@ describe('asn1.js DER decoder', function() {
this.octstr().contains(B);
});

const out = A.decode(new Buffer('04053003020105', 'hex'), 'der');
const out = A.decode(Buffer.from('04053003020105', 'hex'), 'der');
assert.equal(out.nested.toString(10), '5');
});

Expand Down Expand Up @@ -141,14 +141,14 @@ describe('asn1.js DER decoder', function() {
);
});

let out = A.decode(new Buffer(
let out = A.decode(Buffer.from(
'3018300A30030201013003020102300A30030201033003020104', 'hex'), 'der');
assert.equal(out.test1[0].num.toString(10), 1);
assert.equal(out.test1[1].num.toString(10), 2);
assert.equal(out.test2[0].num.toString(10), 3);
assert.equal(out.test2[1].num.toString(10), 4);

out = A.decode(new Buffer('300C300A30030201013003020102', 'hex'), 'der');
out = A.decode(Buffer.from('300C300A30030201013003020102', 'hex'), 'der');
assert.equal(out.test1[0].num.toString(10), 1);
assert.equal(out.test1[1].num.toString(10), 2);
assert.equal(out.test2, undefined);
Expand All @@ -161,7 +161,7 @@ describe('asn1.js DER decoder', function() {
});
});
// Note no decoder specified, defaults to 'der'
const decoded = M.decode(new Buffer('0101ff', 'hex'));
const decoded = M.decode(Buffer.from('0101ff', 'hex'));
assert.deepEqual(decoded, { 'type': 'apple', 'value': true });
});
});
8 changes: 4 additions & 4 deletions test/der-encode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert');
const asn1 = require('..');
const BN = require('bn.js');

const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

describe('asn1.js DER encoder', function() {
/*
Expand All @@ -29,13 +29,13 @@ describe('asn1.js DER encoder', function() {
let Model, derActual;
Model = asn1.define('Model', model_definition);
derActual = Model.encode(model_value, 'der');
assert.deepEqual(derActual, new Buffer(der_expected,'hex'));
assert.deepEqual(derActual, Buffer.from(der_expected,'hex'));
});
}

test('should encode objDesc', function() {
this.objDesc();
}, new Buffer('280'), '0703323830');
}, Buffer.from('280'), '0703323830');

test('should encode choice', function() {
this.choice({
Expand Down Expand Up @@ -146,6 +146,6 @@ describe('asn1.js DER encoder', function() {
});
// Note no encoder specified, defaults to 'der'
const encoded = M.encode({ 'type': 'apple', 'value': true });
assert.deepEqual(encoded, new Buffer('0101ff', 'hex'));
assert.deepEqual(encoded, Buffer.from('0101ff', 'hex'));
});
});
8 changes: 4 additions & 4 deletions test/error-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const bn = asn1.bignum;
const fixtures = require('./fixtures');
const jsonEqual = fixtures.jsonEqual;

const Buffer = require('buffer').Buffer;
const Buffer = require('safer-buffer').Buffer;

describe('asn1.js error', function() {
describe('encoder', function() {
Expand Down Expand Up @@ -104,13 +104,13 @@ describe('asn1.js error', function() {
let error;
assert.throws(function() {
try {
const decoded = M.decode(new Buffer(input, 'hex'), 'der');
const decoded = M.decode(Buffer.from(input, 'hex'), 'der');
} catch (e) {
error = e;
throw e;
}
});
const partial = M.decode(new Buffer(input, 'hex'), 'der', {
const partial = M.decode(Buffer.from(input, 'hex'), 'der', {
partial: true
});

Expand Down Expand Up @@ -181,7 +181,7 @@ describe('asn1.js error', function() {
it('should support ' + name, function() {
const M = asn1.define('TestModel', model);

const decoded = M.decode(new Buffer(input, 'hex'), 'der', {
const decoded = M.decode(Buffer.from(input, 'hex'), 'der', {
partial: true
});

Expand Down
Loading

0 comments on commit e186699

Please sign in to comment.