Skip to content

Commit

Permalink
some minor changes to binary and bson for browser use
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Mar 4, 2012
1 parent 157528e commit cd460e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
3 changes: 3 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
NEEDS LOOKING AT
- ensureIndex on huge collection blocks when using safe (might need to take the connection out of the pool)

0.9.9.4 2012-02-26
------------------
* bugfix for findAndModify: Error: corrupt bson message < 5 bytes long (Issue #519)
Expand Down
14 changes: 8 additions & 6 deletions lib/mongodb/bson/binary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/**
* Module dependencies.
*/
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
var bson = require('./bson');
if(typeof window === 'undefined') {
var Buffer = require('buffer').Buffer; // TODO just use global Buffer
var bson = require('./bson');
}

/**
* A class representation of the BSON Binary type.
Expand All @@ -20,9 +22,7 @@ var bson = require('./bson');
* @param {Number} [subType] the option binary type.
* @return {Grid}
*/
function Binary(buffer, subType) {
if(!(this instanceof Binary)) return new Binary(buffer, subType);

function Binary(buffer, subType) {
this._bsontype = 'Binary';

if(buffer instanceof Number) {
Expand Down Expand Up @@ -184,5 +184,7 @@ Binary.SUBTYPE_USER_DEFINED = 128;
/**
* Expose.
*/
exports.Binary = Binary;
if(typeof window === 'undefined') {
exports.Binary = Binary;
}

27 changes: 14 additions & 13 deletions lib/mongodb/bson/bson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ if(typeof window === 'undefined') {
, MaxKey = require('./max_key').MaxKey
, DBRef = require('./db_ref').DBRef
, Binary = require('./binary').Binary
, ieee754 = require('./float_parser');
, writeIEEE754 = require('./float_parser').writeIEEE754
, readIEEE754 = require('./float_parser').readIEEE754;
}

/**
Expand Down Expand Up @@ -215,7 +216,7 @@ BSON.calculateObjectSize = function calculateObjectSize(object, serializeFunctio
* @api private
*/
function calculateElement(name, value, serializeFunctions, isBuffer) {
// isBuffer = isBuffer == null ? true : false;
isBuffer = typeof Buffer !== 'undefined';

switch(typeof value) {
case 'string':
Expand All @@ -241,7 +242,7 @@ function calculateElement(name, value, serializeFunctions, isBuffer) {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (12 + 1);
} else if(value instanceof Date) {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
} else if(isBuffer && Buffer.isBuffer(value)) {
} else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 4 + 1) + value.length;
} else if(value instanceof Long || value instanceof Double || value instanceof Timestamp
|| value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') {
Expand Down Expand Up @@ -270,15 +271,15 @@ function calculateElement(name, value, serializeFunctions, isBuffer) {
}

return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + BSON.calculateObjectSize(ordered_values, serializeFunctions, isBuffer);
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') {
} else if(value instanceof RegExp || String.call(value) === '[object RegExp]') {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
} else {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + BSON.calculateObjectSize(value, serializeFunctions, isBuffer) + 1;
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if(value instanceof RegExp || toString.call(value) === '[object RegExp]') {
if(value instanceof RegExp || String.call(value) === '[object RegExp]') {
return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
} else {
Expand Down Expand Up @@ -453,7 +454,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write float
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
} else {
Expand Down Expand Up @@ -487,7 +488,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write float
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
}
Expand Down Expand Up @@ -569,7 +570,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
return index;
} else if(Buffer.isBuffer(value)) {
} else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
// Write the type
buffer[index++] = BSON.BSON_DATA_BINARY;
// Number of written bytes
Expand Down Expand Up @@ -622,7 +623,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write float
ieee754.writeIEEE754(buffer, value, index, 'little', 52, 8);
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
return index;
Expand Down Expand Up @@ -785,7 +786,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
buffer[endIndex++] = 0x00;
// Return the end index
return endIndex;
} else if(value instanceof RegExp || toString.call(value) === '[object RegExp]') {
} else if(value instanceof RegExp || String.call(value) === '[object RegExp]') {
// Write the type
buffer[index++] = BSON.BSON_DATA_REGEXP;
// Number of written bytes
Expand Down Expand Up @@ -828,7 +829,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
}
case 'function':
// WTF for 0.4.X where typeof /someregexp/ === 'function'
if(value instanceof RegExp || toString.call(value) === '[object RegExp]') {
if(value instanceof RegExp || String.call(value) === '[object RegExp]') {
// Write the type
buffer[index++] = BSON.BSON_DATA_REGEXP;
// Number of written bytes
Expand Down Expand Up @@ -952,7 +953,7 @@ var packElement = function(name, value, checkKeys, buffer, index, serializeFunct
BSON.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
var size = BSON.calculateObjectSize(object, serializeFunctions, asBuffer);
// If asBuffer is false use typed arrays
var buffer = asBuffer ? new Buffer(size) : new Uint8Array(new ArrayBuffer(size));
var buffer = typeof Buffer !== 'undefined' ? new Buffer(size) : new Uint8Array(new ArrayBuffer(size));
BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, 0, serializeFunctions);
return buffer;
}
Expand Down Expand Up @@ -1169,7 +1170,7 @@ BSON.deserialize = function(buffer, options, isArray) {
break;
case BSON.BSON_DATA_NUMBER:
// Decode the double value
object[name] = ieee754.readIEEE754(buffer, index, 'little', 52, 8);
object[name] = readIEEE754(buffer, index, 'little', 52, 8);
// Update the index
index = index + 8;
break;
Expand Down
25 changes: 25 additions & 0 deletions test/gridstore/grid_store_file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('nodeunit'),
Step = require('step'),
gleak = require('../../dev/tools/gleak'),
ObjectID = require('../../lib/mongodb/bson/objectid').ObjectID,
Db = mongodb.Db,
Expand Down Expand Up @@ -925,6 +926,30 @@ exports.shouldCorrectlyRetrieveSingleCharacterUsingGetC = function(test) {
});
}

// /**
// * @ignore
// */
// exports.shouldNotThrowErrorOnClose = function(test) {
// var fieldId = new ObjectID();
// var gridStore = new GridStore(client, fieldId, "w", {root:'fs'});
// gridStore.chunkSize = 1024 * 256;
// gridStore.open(function(err, gridStore) {
// Step(
// function writeData() {
// var group = this.group();
// for(var i = 0; i < 1000000; i += 5000) {
// gridStore.write(new Buffer(5000), group());
// }
// },
//
// function doneWithWrite() {
// gridStore.close(function(err, result) {
// test.done();
// });
// }
// )
// });
// }

/**
* Retrieve the server information for the current
Expand Down

0 comments on commit cd460e3

Please sign in to comment.