Skip to content

Commit 7bb9c57

Browse files
karimsambroadst
authored andcommitted
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
1 parent a0820d5 commit 7bb9c57

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

Diff for: lib/binary.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class Binary {
297297
*/
298298
static fromExtendedJSON(doc) {
299299
const type = doc.$binary.subType ? parseInt(doc.$binary.subType, 16) : 0;
300-
const data = new Buffer(doc.$binary.base64, 'base64');
300+
const data = Buffer.from(doc.$binary.base64, 'base64');
301301
return new Binary(data, type);
302302
}
303303
}

Diff for: lib/objectid.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const Buffer = require('buffer').Buffer;
44
let randomBytes = require('./parser/utils').randomBytes;
5-
const deprecate = require('util').deprecate;
5+
const util = require('util');
6+
const deprecate = util.deprecate;
67

78
// constants
89
const PROCESS_UNIQUE = randomBytes(5);
@@ -403,7 +404,7 @@ Object.defineProperty(ObjectId.prototype, 'generationTime', {
403404
* @return {String} return the 24 byte hex string representation.
404405
* @ignore
405406
*/
406-
ObjectId.prototype.inspect = ObjectId.prototype.toString;
407+
ObjectId.prototype[util.inspect.custom || 'inspect'] = ObjectId.prototype.toString;
407408

408409
/**
409410
* @ignore

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
"scripts": {
6262
"docs": "jsdoc2md --heading-depth 3 --template tools/README.hbs --plugin dmd-clear --files lib/bson.js lib/extended_json.js > README.md",
6363
"test": "npm run-script lint && npm run-script test-node && npm run-script test-browser",
64-
"test-node": "mocha ./test/node",
65-
"test-browser": "npm run-script build && karma start",
64+
"test-node": "node --throw-deprecation node_modules/.bin/_mocha ./test/node",
65+
"test-browser": "npm run-script build && node --throw-deprecation node_modules/.bin/karma start",
6666
"build": "rollup -c",
6767
"lint": "eslint lib test",
6868
"format": "prettier --print-width 100 --tab-width 2 --single-quote --write 'test/**/*.js' 'lib/**/*.js'",

Diff for: test/node/bson_corpus_tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ describe('BSON Corpus', function() {
158158
// just use canonical.
159159
let cB, cEJ;
160160
if (deprecated) {
161-
cB = new Buffer(v.converted_bson, 'hex');
161+
cB = Buffer.from(v.converted_bson, 'hex');
162162
cEJ = normalize(v.converted_extjson);
163163
} else {
164-
cB = new Buffer(v.canonical_bson, 'hex');
164+
cB = Buffer.from(v.canonical_bson, 'hex');
165165
cEJ = normalize(v.canonical_extjson);
166166
}
167167

Diff for: test/node/extended_json_tests.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Extended JSON', function() {
4040
let doc = {};
4141

4242
before(function() {
43-
const buffer = new Buffer(64);
43+
const buffer = Buffer.alloc(64);
4444
for (var i = 0; i < buffer.length; i++) buffer[i] = i;
4545
const date = new Date();
4646
date.setTime(1488372056737);

Diff for: test/node/object_id_tests.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ describe('ObjectId', function() {
8585
* @ignore
8686
*/
8787
it('should isValid check input Buffer length', function(done) {
88-
var buffTooShort = new Buffer([]);
89-
var buffTooLong = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
90-
var buff12Bytes = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
88+
var buffTooShort = Buffer.from([]);
89+
var buffTooLong = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
90+
var buff12Bytes = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
9191

9292
expect(ObjectId.isValid(buffTooShort)).to.be.false;
9393
expect(ObjectId.isValid(buffTooLong)).to.be.false;

0 commit comments

Comments
 (0)