Skip to content

Commit

Permalink
fix(buffer): don't use deprecated Buffer constructors
Browse files Browse the repository at this point in the history
* Fix: deprecated inspection function

* Add: throw deprecation to test running

* Fix: buffer constructor deprecation

* Fix: karma location in package.json
  • Loading branch information
karimsa authored and mbroadst committed Feb 21, 2019
1 parent a0820d5 commit 7bb9c57
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class Binary {
*/
static fromExtendedJSON(doc) {
const type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
const data = new Buffer(doc.$binary.base64, 'base64');
const data = Buffer.from(doc.$binary.base64, 'base64');
return new Binary(data, type);
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const Buffer = require('buffer').Buffer;
let randomBytes = require('./parser/utils').randomBytes;
const deprecate = require('util').deprecate;
const util = require('util');
const deprecate = util.deprecate;

// constants
const PROCESS_UNIQUE = randomBytes(5);
Expand Down Expand Up @@ -403,7 +404,7 @@ Object.defineProperty(ObjectId.prototype, 'generationTime', {
* @return {String} return the 24 byte hex string representation.
* @ignore
*/
ObjectId.prototype.inspect = ObjectId.prototype.toString;
ObjectId.prototype[util.inspect.custom || 'inspect'] = ObjectId.prototype.toString;

/**
* @ignore
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"scripts": {
"docs": "jsdoc2md --heading-depth 3 --template tools/README.hbs --plugin dmd-clear --files lib/bson.js lib/extended_json.js > README.md",
"test": "npm run-script lint && npm run-script test-node && npm run-script test-browser",
"test-node": "mocha ./test/node",
"test-browser": "npm run-script build && karma start",
"test-node": "node --throw-deprecation node_modules/.bin/_mocha ./test/node",
"test-browser": "npm run-script build && node --throw-deprecation node_modules/.bin/karma start",
"build": "rollup -c",
"lint": "eslint lib test",
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'",
Expand Down
4 changes: 2 additions & 2 deletions test/node/bson_corpus_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ describe('BSON Corpus', function() {
// just use canonical.
let cB, cEJ;
if (deprecated) {
cB = new Buffer(v.converted_bson, 'hex');
cB = Buffer.from(v.converted_bson, 'hex');
cEJ = normalize(v.converted_extjson);
} else {
cB = new Buffer(v.canonical_bson, 'hex');
cB = Buffer.from(v.canonical_bson, 'hex');
cEJ = normalize(v.canonical_extjson);
}

Expand Down
2 changes: 1 addition & 1 deletion test/node/extended_json_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Extended JSON', function() {
let doc = {};

before(function() {
const buffer = new Buffer(64);
const buffer = Buffer.alloc(64);
for (var i = 0; i < buffer.length; i++) buffer[i] = i;
const date = new Date();
date.setTime(1488372056737);
Expand Down
6 changes: 3 additions & 3 deletions test/node/object_id_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ describe('ObjectId', function() {
* @ignore
*/
it('should isValid check input Buffer length', function(done) {
var buffTooShort = new Buffer([]);
var buffTooLong = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
var buff12Bytes = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
var buffTooShort = Buffer.from([]);
var buffTooLong = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
var buff12Bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);

expect(ObjectId.isValid(buffTooShort)).to.be.false;
expect(ObjectId.isValid(buffTooLong)).to.be.false;
Expand Down

0 comments on commit 7bb9c57

Please sign in to comment.