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

Use Buffer.(from|alloc) instead of deprecated Buffer API #30

Merged
merged 1 commit into from
Jul 30, 2018
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following reads an ASN.1 sequence with a boolean.

var Ber = require('asn1').Ber;

var reader = new Ber.Reader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
var reader = new Ber.Reader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]));

reader.readSequence();
console.log('Sequence len: ' + reader.length);
Expand Down
3 changes: 2 additions & 1 deletion lib/ber/reader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.

var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;

var ASN1 = require('./types');
var errors = require('./errors');
Expand Down Expand Up @@ -179,7 +180,7 @@ Reader.prototype.readString = function(tag, retbuf) {
this._offset = o;

if (this.length === 0)
return retbuf ? new Buffer(0) : '';
return retbuf ? Buffer.alloc(0) : '';

var str = this._buf.slice(this._offset, this._offset + this.length);
this._offset += this.length;
Expand Down
5 changes: 3 additions & 2 deletions lib/ber/writer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.

var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;
var ASN1 = require('./types');
var errors = require('./errors');

Expand Down Expand Up @@ -42,7 +43,7 @@ function merge(from, to) {
function Writer(options) {
options = merge(DEFAULT_OPTS, options || {});

this._buf = new Buffer(options.size || 1024);
this._buf = Buffer.alloc(options.size || 1024);
this._size = this._buf.length;
this._offset = 0;
this._options = options;
Expand Down Expand Up @@ -301,7 +302,7 @@ Writer.prototype._ensure = function(len) {
if (sz - this._offset < len)
sz += len;

var buf = new Buffer(sz);
var buf = Buffer.alloc(sz);

this._buf.copy(buf, 0, 0, this._offset);
this._buf = buf;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"url": "git://github.com/mcavage/node-asn1.git"
},
"main": "lib/index.js",
"dependencies": {},
"dependencies": {
"safer-buffer": "~2.1.0"
Copy link
Contributor Author

@ChALkeR ChALkeR Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ^2.1.0 only because of npm bundled with Node.js 0.8 is too old and fails on ranges with ^.

The code itself works on 0.8. I suggest changing to ^2.1.0 and dropping Node.js 0.8 from test matrix, but that could be done in a non-patch release, so I'm not including that to this PR.

In fact, I suggest dropping safer-buffer in the 0.3.0 completely and bumping the minimum required Node.js version to 4.5.0 or to 6.0.0.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can drop 0.8.x from this now (pretty sure the last of our production systems on 0.8 at Joyent have moved over to 0.10 or 4.x now, but I'm still checking). We don't have the luxury of dropping 0.10 anytime soon from this library, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the only Joyent component on 0.8 still is https://github.com/joyent/sdc-amon/blob/master/Makefile#L34

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks @cburroughs. It looks like all of amon's deps that would pull in asn1 are pinned to a version that also pins asn1, so yeah, I think we can drop 0.8 compat in this library safely without breaking those images. Whenever we get around to updating it we'll probably have to update its node version with the other deps though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arekinath good to merge and release then? :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @arekinath, do you have an idea when we can expect the release to reach npm? It's one of the only two remaining dependencies we have to update for Yarn to stop printing the warning (yarnpkg/yarn#6208) :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just published asn1@0.2.4.

},
"devDependencies": {
"tap": "0.4.8"
},
Expand Down
33 changes: 17 additions & 16 deletions tst/ber/reader.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.

var test = require('tap').test;
var Buffer = require('safer-buffer').Buffer;



Expand All @@ -26,15 +27,15 @@ test('load library', function(t) {


test('read byte', function(t) {
var reader = new BerReader(new Buffer([0xde]));
var reader = new BerReader(Buffer.from([0xde]));
t.ok(reader);
t.equal(reader.readByte(), 0xde, 'wrong value');
t.end();
});


test('read 1 byte int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x01, 0x03]));
var reader = new BerReader(Buffer.from([0x02, 0x01, 0x03]));
t.ok(reader);
t.equal(reader.readInt(), 0x03, 'wrong value');
t.equal(reader.length, 0x01, 'wrong length');
Expand All @@ -43,7 +44,7 @@ test('read 1 byte int', function(t) {


test('read 2 byte int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x02, 0x7e, 0xde]));
var reader = new BerReader(Buffer.from([0x02, 0x02, 0x7e, 0xde]));
t.ok(reader);
t.equal(reader.readInt(), 0x7ede, 'wrong value');
t.equal(reader.length, 0x02, 'wrong length');
Expand All @@ -52,7 +53,7 @@ test('read 2 byte int', function(t) {


test('read 3 byte int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x03, 0x7e, 0xde, 0x03]));
var reader = new BerReader(Buffer.from([0x02, 0x03, 0x7e, 0xde, 0x03]));
t.ok(reader);
t.equal(reader.readInt(), 0x7ede03, 'wrong value');
t.equal(reader.length, 0x03, 'wrong length');
Expand All @@ -61,7 +62,7 @@ test('read 3 byte int', function(t) {


test('read 4 byte int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]));
var reader = new BerReader(Buffer.from([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]));
t.ok(reader);
t.equal(reader.readInt(), 0x7ede0301, 'wrong value');
t.equal(reader.length, 0x04, 'wrong length');
Expand All @@ -70,7 +71,7 @@ test('read 4 byte int', function(t) {


test('read 1 byte negative int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x01, 0xdc]));
var reader = new BerReader(Buffer.from([0x02, 0x01, 0xdc]));
t.ok(reader);
t.equal(reader.readInt(), -36, 'wrong value');
t.equal(reader.length, 0x01, 'wrong length');
Expand All @@ -79,7 +80,7 @@ test('read 1 byte negative int', function(t) {


test('read 2 byte negative int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x02, 0xc0, 0x4e]));
var reader = new BerReader(Buffer.from([0x02, 0x02, 0xc0, 0x4e]));
t.ok(reader);
t.equal(reader.readInt(), -16306, 'wrong value');
t.equal(reader.length, 0x02, 'wrong length');
Expand All @@ -88,7 +89,7 @@ test('read 2 byte negative int', function(t) {


test('read 3 byte negative int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x03, 0xff, 0x00, 0x19]));
var reader = new BerReader(Buffer.from([0x02, 0x03, 0xff, 0x00, 0x19]));
t.ok(reader);
t.equal(reader.readInt(), -65511, 'wrong value');
t.equal(reader.length, 0x03, 'wrong length');
Expand All @@ -97,7 +98,7 @@ test('read 3 byte negative int', function(t) {


test('read 4 byte negative int', function(t) {
var reader = new BerReader(new Buffer([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]));
var reader = new BerReader(Buffer.from([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]));
t.ok(reader);
t.equal(reader.readInt(), -1854135777, 'wrong value');
t.equal(reader.length, 0x04, 'wrong length');
Expand All @@ -106,7 +107,7 @@ test('read 4 byte negative int', function(t) {


test('read boolean true', function(t) {
var reader = new BerReader(new Buffer([0x01, 0x01, 0xff]));
var reader = new BerReader(Buffer.from([0x01, 0x01, 0xff]));
t.ok(reader);
t.equal(reader.readBoolean(), true, 'wrong value');
t.equal(reader.length, 0x01, 'wrong length');
Expand All @@ -115,7 +116,7 @@ test('read boolean true', function(t) {


test('read boolean false', function(t) {
var reader = new BerReader(new Buffer([0x01, 0x01, 0x00]));
var reader = new BerReader(Buffer.from([0x01, 0x01, 0x00]));
t.ok(reader);
t.equal(reader.readBoolean(), false, 'wrong value');
t.equal(reader.length, 0x01, 'wrong length');
Expand All @@ -124,7 +125,7 @@ test('read boolean false', function(t) {


test('read enumeration', function(t) {
var reader = new BerReader(new Buffer([0x0a, 0x01, 0x20]));
var reader = new BerReader(Buffer.from([0x0a, 0x01, 0x20]));
t.ok(reader);
t.equal(reader.readEnumeration(), 0x20, 'wrong value');
t.equal(reader.length, 0x01, 'wrong length');
Expand All @@ -134,7 +135,7 @@ test('read enumeration', function(t) {

test('read string', function(t) {
var dn = 'cn=foo,ou=unit,o=test';
var buf = new Buffer(dn.length + 2);
var buf = Buffer.alloc(dn.length + 2);
buf[0] = 0x04;
buf[1] = Buffer.byteLength(dn);
buf.write(dn, 2);
Expand All @@ -147,7 +148,7 @@ test('read string', function(t) {


test('read sequence', function(t) {
var reader = new BerReader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
var reader = new BerReader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]));
t.ok(reader);
t.equal(reader.readSequence(), 0x30, 'wrong value');
t.equal(reader.length, 0x03, 'wrong length');
Expand All @@ -158,7 +159,7 @@ test('read sequence', function(t) {


test('anonymous LDAPv3 bind', function(t) {
var BIND = new Buffer(14);
var BIND = Buffer.alloc(14);
BIND[0] = 0x30; // Sequence
BIND[1] = 12; // len
BIND[2] = 0x02; // ASN.1 Integer
Expand Down Expand Up @@ -192,7 +193,7 @@ test('anonymous LDAPv3 bind', function(t) {


test('long string', function(t) {
var buf = new Buffer(256);
var buf = Buffer.alloc(256);
var o;
var s =
'2;649;CN=Red Hat CS 71GA Demo,O=Red Hat CS 71GA Demo,C=US;' +
Expand Down
5 changes: 3 additions & 2 deletions tst/ber/writer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var test = require('tap').test;
var sys = require('sys');
var Buffer = require('safer-buffer').Buffer;

///--- Globals

Expand Down Expand Up @@ -218,7 +219,7 @@ test('write buffer', function(t) {
// write some stuff to start with
writer.writeString('hello world');
var ber = writer.buffer;
var buf = new Buffer([0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01,
var buf = Buffer.from([0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01, 0x01,
0xff, 0x01, 0x01, 0xff]);
writer.writeBuffer(buf.slice(2, buf.length), 0x04);
ber = writer.buffer;
Expand Down Expand Up @@ -362,7 +363,7 @@ test('Write OID', function(t) {
var ber = writer.buffer;
t.ok(ber);
console.log(require('util').inspect(ber));
console.log(require('util').inspect(new Buffer([0x06, 0x09, 0x2a, 0x86,
console.log(require('util').inspect(Buffer.from([0x06, 0x09, 0x2a, 0x86,
0x48, 0x86, 0xf7, 0x0d,
0x01, 0x01, 0x01])));

Expand Down