christkv/node-mongodb-native — Issue #400: connections.js - maxBsonSize undefined
Author: tcoffin
Created: 2011-11-12 01:38:54+00:00
State: open

I ran across an issue where I'd get the following exception:
buffer.js:374
if (end > this.length) throw new Error('oob');
^
Error: oob
at Buffer.slice (buffer.js:374:32)
at Socket. (/home/tcoffin/node_modules/mongodb/lib/mongodb/connection/connection.js:252:41)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:672:14)
at IOWatcher.onReadable [as callback] (net.js:177:10)

Digging in to it I found that there was some scenario arising where maxBsonSize (connection.js) was ending up being undefined.

The temporary workaround that I applied was to change ConnectionPool.prototype.setMaxBsonSize (connect_pool.js) to add a check for an undefined maxBsonSize being set; and apply DEFAULT_MAX_BSON_SIZE to it instead (not the cleanest solution since I've duplicated the constant in _pool.js)

```javascript
ConnectionPool.prototype.setMaxBsonSize = function(maxBsonSize) {
var keys = Object.keys(this.openConnections);

if ( typeof maxBsonSize == 'undefined' ){
maxBsonSize = DEFAULT_MAX_BSON_SIZE;
}

for(var i = 0; i < keys.length; i++) {
this.openConnections[keys[i]].maxBsonSize = maxBsonSize;
}
}
```