From 7bb9c57db1c5f862dee4762fd2211e2a722cb75d Mon Sep 17 00:00:00 2001 From: Karim Alibhai Date: Thu, 21 Feb 2019 15:25:02 -0500 Subject: [PATCH] fix(buffer): don't use deprecated Buffer constructors * Fix: deprecated inspection function * Add: throw deprecation to test running * Fix: buffer constructor deprecation * Fix: karma location in package.json --- lib/binary.js | 2 +- lib/objectid.js | 5 +++-- package.json | 4 ++-- test/node/bson_corpus_tests.js | 4 ++-- test/node/extended_json_tests.js | 2 +- test/node/object_id_tests.js | 6 +++--- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/binary.js b/lib/binary.js index b3d872c2..b12b6154 100644 --- a/lib/binary.js +++ b/lib/binary.js @@ -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); } } diff --git a/lib/objectid.js b/lib/objectid.js index 874adc3a..d3128eb1 100644 --- a/lib/objectid.js +++ b/lib/objectid.js @@ -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); @@ -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 diff --git a/package.json b/package.json index 8c113f94..53f360ae 100644 --- a/package.json +++ b/package.json @@ -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'", diff --git a/test/node/bson_corpus_tests.js b/test/node/bson_corpus_tests.js index 1e5dacfd..1a368f47 100644 --- a/test/node/bson_corpus_tests.js +++ b/test/node/bson_corpus_tests.js @@ -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); } diff --git a/test/node/extended_json_tests.js b/test/node/extended_json_tests.js index 1316ec9b..65c5efab 100644 --- a/test/node/extended_json_tests.js +++ b/test/node/extended_json_tests.js @@ -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); diff --git a/test/node/object_id_tests.js b/test/node/object_id_tests.js index ba2a8144..bab81ae6 100644 --- a/test/node/object_id_tests.js +++ b/test/node/object_id_tests.js @@ -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;