Skip to content

Commit d3cfdcb

Browse files
committed
refactor(buffer): add a more robust workaround for feross/safe-buffer#23
Fix #6826
1 parent 5869950 commit d3cfdcb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/types/buffer.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const Binary = require('../driver').get().Binary;
88
const utils = require('../utils');
99
const Buffer = require('safe-buffer').Buffer;
1010

11-
const NodeBuffer = global.Buffer || Buffer;
11+
// Yes this is weird. See https://github.com/feross/safe-buffer/pull/23
12+
const proto = Buffer.from('').constructor.prototype;
1213

1314
/**
1415
* Mongoose Buffer constructor.
@@ -134,7 +135,7 @@ MongooseBuffer.mixin = {
134135
*/
135136

136137
write: function() {
137-
const written = NodeBuffer.prototype.write.apply(this, arguments);
138+
const written = proto.write.apply(this, arguments);
138139

139140
if (written > 0) {
140141
this._markModified();
@@ -157,7 +158,7 @@ MongooseBuffer.mixin = {
157158
*/
158159

159160
copy: function(target) {
160-
const ret = NodeBuffer.prototype.copy.apply(this, arguments);
161+
const ret = proto.copy.apply(this, arguments);
161162

162163
if (target && target.isMongooseBuffer) {
163164
target._markModified();
@@ -181,11 +182,11 @@ MongooseBuffer.mixin = {
181182
'writeUInt16LE writeUInt16BE writeUInt32LE writeUInt32BE ' +
182183
'writeInt16LE writeInt16BE writeInt32LE writeInt32BE ' + 'writeFloatLE writeFloatBE writeDoubleLE writeDoubleBE')
183184
).split(' ').forEach(function(method) {
184-
if (!NodeBuffer.prototype[method]) {
185+
if (!proto[method]) {
185186
return;
186187
}
187188
MongooseBuffer.mixin[method] = function() {
188-
const ret = NodeBuffer.prototype[method].apply(this, arguments);
189+
const ret = proto[method].apply(this, arguments);
189190
this._markModified();
190191
return ret;
191192
};

0 commit comments

Comments
 (0)