diff --git a/lib/bson/bson.js b/lib/bson/bson.js index a85a0e8e..4a528471 100644 --- a/lib/bson/bson.js +++ b/lib/bson/bson.js @@ -24,9 +24,10 @@ var deserialize = require('./parser/deserializer'), * @ignore * @api private */ -// Max Size +// Default Max Size var MAXSIZE = 1024 * 1024 * 17; -// Max Document Buffer size + +// Current Internal Temporary Serialization Buffer var buffer = new Buffer(MAXSIZE); var BSON = function() {}; @@ -38,6 +39,7 @@ var BSON = function() {}; * @param {Boolean} [options.checkKeys] the serializer will check if keys are valid. * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**. * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**. + * @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**. * @return {Buffer} returns the Buffer object containing the serialized object. * @api public */ @@ -49,6 +51,13 @@ BSON.prototype.serialize = function serialize(object, options) { typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false; var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true; + var minInternalBufferSize = + typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE; + + // Resize the internal serialization buffer if needed + if (buffer.length < minInternalBufferSize) { + buffer = new Buffer(minInternalBufferSize); + } // Attempt to serialize var serializationIndex = serializer(