Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address buffer constructor deprecation #116

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/asn1/base/buffer.js
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion lib/asn1/decoders/pem.js
Original file line number Diff line number Diff line change
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);
};
14 changes: 7 additions & 7 deletions lib/asn1/encoders/der.js
Original file line number Diff line number Diff line change
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
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"mocha": "^2.3.4"
},
"dependencies": {
"bn.js": "^4.0.0",
"bn.js": "^5.0.0",
"inherits": "^2.0.1",
"minimalistic-assert": "^1.0.0"
}
Expand Down
8 changes: 4 additions & 4 deletions rfc/2560/test/basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Buffer = require('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
6 changes: 3 additions & 3 deletions rfc/5280/test/basic-test.js
Original file line number Diff line number Diff line change
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
16 changes: 8 additions & 8 deletions test/der-decode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,7 +42,7 @@ 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' });
});

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 @@ -148,7 +148,7 @@ describe('asn1.js DER decoder', function() {
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 });
});
});
4 changes: 2 additions & 2 deletions test/der-encode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('asn1.js DER encoder', function() {

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'));
});
});
6 changes: 3 additions & 3 deletions test/error-test.js
Original file line number Diff line number Diff line change
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
10 changes: 5 additions & 5 deletions test/ping-pong-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ describe('asn1.js ping/pong', function() {

test('octstr', function() {
this.octstr();
}, new Buffer('hello'));
}, Buffer.from('hello'));

test('objDesc', function() {
this.objDesc()
}, new Buffer('hello'));
}, Buffer.from('hello'));

test('bitstr', function() {
this.bitstr();
}, { unused: 4, data: new Buffer('hello!') });
}, { unused: 4, data: Buffer.from('hello!') });

test('ia5str', function() {
this.ia5str();
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('asn1.js ping/pong', function() {

test('any', function() {
this.any();
}, new Buffer('02210081347a0d3d674aeeb563061d94a3aea5f6a7' +
}, Buffer.from('02210081347a0d3d674aeeb563061d94a3aea5f6a7' +
'c6dc153ea90a42c1ca41929ac1b9', 'hex'));

test('default explicit', function() {
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('asn1.js ping/pong', function() {
this.seq().obj(
this.key('content').optional().any()
);
}, { content: new Buffer('0500', 'hex') });
}, { content: Buffer.from('0500', 'hex') });

test('seqof', function() {
const S = asn1.define('S', function() {
Expand Down
18 changes: 9 additions & 9 deletions test/use-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('asn1.js models', function() {
);
});

const data = {a: new bn(1), sub: {b: new Buffer("XXX")}};
const data = {a: new bn(1), sub: {b: Buffer.from('XXX')}};
const wire = Model.encode(data, 'der');
assert.equal(wire.toString('hex'), '300a02010130050403585858');
const back = Model.decode(wire, 'der');
Expand All @@ -35,7 +35,7 @@ describe('asn1.js models', function() {
const SubModel = asn1.define('SubModel', function() {
this.seq().obj(
this.key('x').octstr()
)
);
});
const Model = asn1.define('Model', function() {
this.seq().obj(
Expand All @@ -44,7 +44,7 @@ describe('asn1.js models', function() {
);
});

const data = {a: new bn(1), sub: {x: new Buffer("123")}};
const data = {a: new bn(1), sub: {x: Buffer.from('123')}};
const wire = Model.encode(data, 'der');
assert.equal(wire.toString('hex'), '300a020101a0050403313233');
const back = Model.decode(wire, 'der');
Expand All @@ -55,7 +55,7 @@ describe('asn1.js models', function() {
const SubModel = asn1.define('SubModel', function() {
this.seq().obj(
this.key('x').octstr()
)
);
});
const Model = asn1.define('Model', function() {
this.seq().obj(
Expand All @@ -64,7 +64,7 @@ describe('asn1.js models', function() {
);
});

const data = {a: new bn(1), sub: {x: new Buffer("123")}};
const data = {a: new bn(1), sub: {x: Buffer.from('123')}};
const wire = Model.encode(data, 'der');
assert.equal(wire.toString('hex'), '300c020101a00730050403313233');
const back = Model.decode(wire, 'der');
Expand All @@ -76,19 +76,19 @@ describe('asn1.js models', function() {
const SubModel = asn1.define('SubModel', function() {
this.seq().obj(
this.key('x').octstr()
)
);
});
const Model = asn1.define('Model', function() {
this.seq().obj(
this.key('a').int(),
this.key('sub').use(function(obj) {
assert.equal(obj.a, 1);
return SubModel;
assert.equal(obj.a, 1);
return SubModel;
})
);
});

const data = {a: new bn(1), sub: {x: new Buffer("123")}};
const data = {a: new bn(1), sub: {x: Buffer.from('123')}};
const wire = Model.encode(data, 'der');
assert.equal(wire.toString('hex'), '300a02010130050403313233');
const back = Model.decode(wire, 'der');
Expand Down