From 327da9591021e01c087a891ee6481a9c4141a848 Mon Sep 17 00:00:00 2001 From: Fonger <5862369+Fonger@users.noreply.github.com> Date: Sat, 18 Aug 2018 15:04:45 +0800 Subject: [PATCH] fix(buffer): use safe-buffer polyfill to maintain compatibility node.js < 4.5 and node.js < 5.9 doesn't support Buffer.alloc and Buffer.from --- lib/gridfs-stream/upload.js | 1 + lib/gridfs/chunk.js | 2 ++ lib/gridfs/grid_store.js | 2 +- package.json | 3 ++- test/functional/cursor_tests.js | 1 + test/functional/cursorstream_tests.js | 1 + test/functional/find_tests.js | 1 + test/functional/gridfs_stream_tests.js | 3 ++- test/functional/gridfs_tests.js | 3 ++- test/functional/insert_tests.js | 3 ++- test/functional/operation_example_tests.js | 3 ++- test/functional/operation_generators_example_tests.js | 1 + test/functional/operation_promises_example_tests.js | 1 + test/functional/promote_buffers_tests.js | 2 +- 14 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/gridfs-stream/upload.js b/lib/gridfs-stream/upload.js index 45f0e474d6..da77dcd650 100644 --- a/lib/gridfs-stream/upload.js +++ b/lib/gridfs-stream/upload.js @@ -4,6 +4,7 @@ var core = require('mongodb-core'); var crypto = require('crypto'); var stream = require('stream'); var util = require('util'); +var Buffer = require('safe-buffer').Buffer; var ERROR_NAMESPACE_NOT_FOUND = 26; diff --git a/lib/gridfs/chunk.js b/lib/gridfs/chunk.js index fef81c14fc..c29bba02a7 100644 --- a/lib/gridfs/chunk.js +++ b/lib/gridfs/chunk.js @@ -3,6 +3,8 @@ var Binary = require('mongodb-core').BSON.Binary, ObjectID = require('mongodb-core').BSON.ObjectID; +var Buffer = require('safe-buffer').Buffer; + /** * Class for representing a single chunk in GridFS. * diff --git a/lib/gridfs/grid_store.js b/lib/gridfs/grid_store.js index bf72537b58..4b5eb5e4b8 100644 --- a/lib/gridfs/grid_store.js +++ b/lib/gridfs/grid_store.js @@ -38,7 +38,7 @@ const Chunk = require('./chunk'); const ObjectID = require('mongodb-core').BSON.ObjectID; const ReadPreference = require('mongodb-core').ReadPreference; -const Buffer = require('buffer').Buffer; +const Buffer = require('safe-buffer').Buffer; const fs = require('fs'); const f = require('util').format; const util = require('util'); diff --git a/package.json b/package.json index 41b645bab3..b6e95ad52f 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "official" ], "dependencies": { - "mongodb-core": "3.1.2" + "mongodb-core": "3.1.2", + "safe-buffer": "^5.1.2" }, "devDependencies": { "bluebird": "3.5.0", diff --git a/test/functional/cursor_tests.js b/test/functional/cursor_tests.js index 21146f6777..972cbaa4a9 100644 --- a/test/functional/cursor_tests.js +++ b/test/functional/cursor_tests.js @@ -6,6 +6,7 @@ const expect = require('chai').expect; const Long = require('bson').Long; const sinon = require('sinon'); const ReadPreference = require('mongodb-core').ReadPreference; +const Buffer = require('safe-buffer').Buffer; describe('Cursor', function() { before(function() { diff --git a/test/functional/cursorstream_tests.js b/test/functional/cursorstream_tests.js index 79621dd261..bee7f647f2 100644 --- a/test/functional/cursorstream_tests.js +++ b/test/functional/cursorstream_tests.js @@ -1,5 +1,6 @@ 'use strict'; var expect = require('chai').expect; +var Buffer = require('safe-buffer').Buffer; describe('Cursor Streams', function() { before(function() { diff --git a/test/functional/find_tests.js b/test/functional/find_tests.js index 9e9848c55d..8f00a81a2a 100644 --- a/test/functional/find_tests.js +++ b/test/functional/find_tests.js @@ -3,6 +3,7 @@ const test = require('./shared').assert; const setupDatabase = require('./shared').setupDatabase; const expect = require('chai').expect; const MongoClient = require('../../lib/mongo_client'); +const Buffer = require('safe-buffer').Buffer; describe('Find', function() { before(function() { diff --git a/test/functional/gridfs_stream_tests.js b/test/functional/gridfs_stream_tests.js index 6bb98d5fa7..db792c61fc 100644 --- a/test/functional/gridfs_stream_tests.js +++ b/test/functional/gridfs_stream_tests.js @@ -5,7 +5,8 @@ const crypto = require('crypto'), fs = require('fs'), test = require('./shared').assert, setupDatabase = require('./shared').setupDatabase, - expect = require('chai').expect; + expect = require('chai').expect, + Buffer = require('safe-buffer').Buffer; describe('GridFS Stream', function() { before(function() { diff --git a/test/functional/gridfs_tests.js b/test/functional/gridfs_tests.js index 6a801d09f8..ea063c98a1 100644 --- a/test/functional/gridfs_tests.js +++ b/test/functional/gridfs_tests.js @@ -5,7 +5,8 @@ const test = require('./shared').assert, fs = require('fs'), format = require('util').format, child_process = require('child_process'), - expect = require('chai').expect; + expect = require('chai').expect, + Buffer = require('safe-buffer').Buffer; describe('GridFS', function() { before(function() { diff --git a/test/functional/insert_tests.js b/test/functional/insert_tests.js index a2f008364e..eba7cf5040 100644 --- a/test/functional/insert_tests.js +++ b/test/functional/insert_tests.js @@ -3,7 +3,8 @@ const test = require('./shared').assert, setupDatabase = require('./shared').setupDatabase, Script = require('vm'), expect = require('chai').expect, - normalizedFunctionString = require('bson/lib/bson/parser/utils').normalizedFunctionString; + normalizedFunctionString = require('bson/lib/bson/parser/utils').normalizedFunctionString, + Buffer = require('safe-buffer').Buffer; /** * Module for parsing an ISO 8601 formatted string into a Date object. diff --git a/test/functional/operation_example_tests.js b/test/functional/operation_example_tests.js index b828036052..6addde4e8f 100644 --- a/test/functional/operation_example_tests.js +++ b/test/functional/operation_example_tests.js @@ -2,7 +2,8 @@ const test = require('./shared').assert, setupDatabase = require('./shared').setupDatabase, f = require('util').format, - expect = require('chai').expect; + expect = require('chai').expect, + Buffer = require('safe-buffer').Buffer; describe('Operation Examples', function() { before(function() { diff --git a/test/functional/operation_generators_example_tests.js b/test/functional/operation_generators_example_tests.js index 3edf376b0e..c6e74eebc5 100644 --- a/test/functional/operation_generators_example_tests.js +++ b/test/functional/operation_generators_example_tests.js @@ -1,6 +1,7 @@ 'use strict'; var test = require('./shared').assert; var setupDatabase = require('./shared').setupDatabase; +var Buffer = require('safe-buffer').Buffer; /************************************************************************** * diff --git a/test/functional/operation_promises_example_tests.js b/test/functional/operation_promises_example_tests.js index 725fc2866c..a0149293c0 100644 --- a/test/functional/operation_promises_example_tests.js +++ b/test/functional/operation_promises_example_tests.js @@ -3,6 +3,7 @@ var fs = require('fs'); var f = require('util').format; var test = require('./shared').assert; var setupDatabase = require('./shared').setupDatabase; +var Buffer = require('safe-buffer').Buffer; var delay = function(ms) { return new Promise(function(resolve) { diff --git a/test/functional/promote_buffers_tests.js b/test/functional/promote_buffers_tests.js index 68b8efedd1..033bd55abc 100644 --- a/test/functional/promote_buffers_tests.js +++ b/test/functional/promote_buffers_tests.js @@ -1,7 +1,7 @@ 'use strict'; var test = require('./shared').assert; var setupDatabase = require('./shared').setupDatabase; -var Buffer = require('buffer').Buffer; +var Buffer = require('safe-buffer').Buffer; describe('Promote Buffers', function() { before(function() {