Skip to content

Commit

Permalink
refactor(buffer): add a more robust workaround for feross/safe-buffer#23
Browse files Browse the repository at this point in the history


Fix #6826
  • Loading branch information
vkarpov15 committed Nov 5, 2018
1 parent 5869950 commit d3cfdcb
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/types/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const Binary = require('../driver').get().Binary;
const utils = require('../utils');
const Buffer = require('safe-buffer').Buffer;

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

/**
* Mongoose Buffer constructor.
Expand Down Expand Up @@ -134,7 +135,7 @@ MongooseBuffer.mixin = {
*/

write: function() {
const written = NodeBuffer.prototype.write.apply(this, arguments);
const written = proto.write.apply(this, arguments);

if (written > 0) {
this._markModified();
Expand All @@ -157,7 +158,7 @@ MongooseBuffer.mixin = {
*/

copy: function(target) {
const ret = NodeBuffer.prototype.copy.apply(this, arguments);
const ret = proto.copy.apply(this, arguments);

if (target && target.isMongooseBuffer) {
target._markModified();
Expand All @@ -181,11 +182,11 @@ MongooseBuffer.mixin = {
'writeUInt16LE writeUInt16BE writeUInt32LE writeUInt32BE ' +
'writeInt16LE writeInt16BE writeInt32LE writeInt32BE ' + 'writeFloatLE writeFloatBE writeDoubleLE writeDoubleBE')
).split(' ').forEach(function(method) {
if (!NodeBuffer.prototype[method]) {
if (!proto[method]) {
return;
}
MongooseBuffer.mixin[method] = function() {
const ret = NodeBuffer.prototype[method].apply(this, arguments);
const ret = proto[method].apply(this, arguments);
this._markModified();
return ret;
};
Expand Down

0 comments on commit d3cfdcb

Please sign in to comment.