diff options
Diffstat (limited to 'node_modules/mongodb')
82 files changed, 28666 insertions, 0 deletions
diff --git a/node_modules/mongodb/.travis.yml b/node_modules/mongodb/.travis.yml new file mode 100644 index 0000000..90b208a --- /dev/null +++ b/node_modules/mongodb/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.4 + - 0.6 + - 0.7 # development version of 0.8, may be unstable
\ No newline at end of file diff --git a/node_modules/mongodb/Makefile b/node_modules/mongodb/Makefile new file mode 100644 index 0000000..de11cbe --- /dev/null +++ b/node_modules/mongodb/Makefile @@ -0,0 +1,71 @@ +NODE = node +NPM = npm +NODEUNIT = node_modules/nodeunit/bin/nodeunit +DOX = node_modules/dox/bin/dox +name = all + +total: build_native + +build_native: + # $(MAKE) -C ./external-libs/bson all + +build_native_debug: + $(MAKE) -C ./external-libs/bson all_debug + +build_native_clang: + $(MAKE) -C ./external-libs/bson clang + +build_native_clang_debug: + $(MAKE) -C ./external-libs/bson clang_debug + +clean_native: + $(MAKE) -C ./external-libs/bson clean + +test: build_native + @echo "\n == Run All tests minus replicaset tests==" + $(NODE) dev/tools/test_all.js --noreplicaset --boot + +test_pure: build_native + @echo "\n == Run All tests minus replicaset tests==" + $(NODE) dev/tools/test_all.js --noreplicaset --boot --noactive + +test_junit: build_native + @echo "\n == Run All tests minus replicaset tests==" + $(NODE) dev/tools/test_all.js --junit --noreplicaset + +test_nodeunit_pure: + @echo "\n == Execute Test Suite using Pure JS BSON Parser == " + @$(NODEUNIT) test/ test/gridstore test/bson + +test_js: + @$(NODEUNIT) $(TESTS) + +test_nodeunit_replicaset_pure: + @echo "\n == Execute Test Suite using Pure JS BSON Parser == " + @$(NODEUNIT) test/replicaset + +test_nodeunit_native: + @echo "\n == Execute Test Suite using Native BSON Parser == " + @TEST_NATIVE=TRUE $(NODEUNIT) test/ test/gridstore test/bson + +test_nodeunit_replicaset_native: + @echo "\n == Execute Test Suite using Native BSON Parser == " + @TEST_NATIVE=TRUE $(NODEUNIT) test/replicaset + +test_all: build_native + @echo "\n == Run All tests ==" + $(NODE) dev/tools/test_all.js --boot + +test_all_junit: build_native + @echo "\n == Run All tests ==" + $(NODE) dev/tools/test_all.js --junit --boot + +clean: + rm ./external-libs/bson/bson.node + rm -r ./external-libs/bson/build + +generate_docs: + $(NODE) dev/tools/build-docs.js + make --directory=./docs/sphinx-docs --file=Makefile html + +.PHONY: total diff --git a/node_modules/mongodb/external-libs/bson/Makefile b/node_modules/mongodb/external-libs/bson/Makefile new file mode 100644 index 0000000..ad877d4 --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/Makefile @@ -0,0 +1,45 @@ +NODE = node +name = all +JOBS = 1 + +all: + rm -rf build .lock-wscript bson.node + node-waf configure build + cp -R ./build/Release/bson.node . || true + @$(NODE) --expose-gc test/test_bson.js + @$(NODE) --expose-gc test/test_full_bson.js + # @$(NODE) --expose-gc test/test_stackless_bson.js + +all_debug: + rm -rf build .lock-wscript bson.node + node-waf --debug configure build + cp -R ./build/Release/bson.node . || true + @$(NODE) --expose-gc test/test_bson.js + @$(NODE) --expose-gc test/test_full_bson.js + # @$(NODE) --expose-gc test/test_stackless_bson.js + +test: + @$(NODE) --expose-gc test/test_bson.js + @$(NODE) --expose-gc test/test_full_bson.js + # @$(NODE) --expose-gc test/test_stackless_bson.js + +clang: + rm -rf build .lock-wscript bson.node + CXX=clang node-waf configure build + cp -R ./build/Release/bson.node . || true + @$(NODE) --expose-gc test/test_bson.js + @$(NODE) --expose-gc test/test_full_bson.js + # @$(NODE) --expose-gc test/test_stackless_bson.js + +clang_debug: + rm -rf build .lock-wscript bson.node + CXX=clang node-waf --debug configure build + cp -R ./build/Release/bson.node . || true + @$(NODE) --expose-gc test/test_bson.js + @$(NODE) --expose-gc test/test_full_bson.js + # @$(NODE) --expose-gc test/test_stackless_bson.js + +clean: + rm -rf build .lock-wscript bson.node + +.PHONY: all
\ No newline at end of file diff --git a/node_modules/mongodb/external-libs/bson/bson.cc b/node_modules/mongodb/external-libs/bson/bson.cc new file mode 100644 index 0000000..8906eea --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/bson.cc @@ -0,0 +1,2165 @@ +#include <assert.h> +#include <string.h> +#include <stdlib.h> + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-parameter" +#endif + +#include <v8.h> + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#include <node.h> +#include <node_version.h> +#include <node_buffer.h> +#include <cstring> +#include <cmath> +#include <cstdlib> +#include <iostream> +#include <limits> +#include <vector> + +#include "bson.h" + +using namespace v8; +using namespace node; +using namespace std; + +// BSON DATA TYPES +const uint32_t BSON_DATA_NUMBER = 1; +const uint32_t BSON_DATA_STRING = 2; +const uint32_t BSON_DATA_OBJECT = 3; +const uint32_t BSON_DATA_ARRAY = 4; +const uint32_t BSON_DATA_BINARY = 5; +const uint32_t BSON_DATA_OID = 7; +const uint32_t BSON_DATA_BOOLEAN = 8; +const uint32_t BSON_DATA_DATE = 9; +const uint32_t BSON_DATA_NULL = 10; +const uint32_t BSON_DATA_REGEXP = 11; +const uint32_t BSON_DATA_CODE = 13; +const uint32_t BSON_DATA_SYMBOL = 14; +const uint32_t BSON_DATA_CODE_W_SCOPE = 15; +const uint32_t BSON_DATA_INT = 16; +const uint32_t BSON_DATA_TIMESTAMP = 17; +const uint32_t BSON_DATA_LONG = 18; +const uint32_t BSON_DATA_MIN_KEY = 0xff; +const uint32_t BSON_DATA_MAX_KEY = 0x7f; + +const int32_t BSON_INT32_MAX = (int32_t)2147483647L; +const int32_t BSON_INT32_MIN = (int32_t)(-1) * 2147483648L; + +const int64_t BSON_INT64_MAX = ((int64_t)1 << 63) - 1; +const int64_t BSON_INT64_MIN = (int64_t)-1 << 63; + +const int64_t JS_INT_MAX = (int64_t)1 << 53; +const int64_t JS_INT_MIN = (int64_t)-1 << 53; + +static Handle<Value> VException(const char *msg) { + HandleScope scope; + return ThrowException(Exception::Error(String::New(msg))); + }; + +Persistent<FunctionTemplate> BSON::constructor_template; + +void BSON::Initialize(v8::Handle<v8::Object> target) { + // Grab the scope of the call from Node + HandleScope scope; + // Define a new function template + Local<FunctionTemplate> t = FunctionTemplate::New(New); + constructor_template = Persistent<FunctionTemplate>::New(t); + constructor_template->InstanceTemplate()->SetInternalFieldCount(1); + constructor_template->SetClassName(String::NewSymbol("BSON")); + + // Instance methods + NODE_SET_PROTOTYPE_METHOD(constructor_template, "calculateObjectSize", CalculateObjectSize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "serialize", BSONSerialize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "serializeWithBufferAndIndex", SerializeWithBufferAndIndex); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "deserialize", BSONDeserialize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "deserializeStream", BSONDeserializeStream); + + // Experimental + // NODE_SET_PROTOTYPE_METHOD(constructor_template, "calculateObjectSize2", CalculateObjectSize2); + // NODE_SET_PROTOTYPE_METHOD(constructor_template, "serialize2", BSONSerialize2); + // NODE_SET_METHOD(constructor_template->GetFunction(), "serialize2", BSONSerialize2); + + target->ForceSet(String::NewSymbol("BSON"), constructor_template->GetFunction()); +} + +// Create a new instance of BSON and assing it the existing context +Handle<Value> BSON::New(const Arguments &args) { + HandleScope scope; + + // Check that we have an array + if(args.Length() == 1 && args[0]->IsArray()) { + // Cast the array to a local reference + Local<Array> array = Local<Array>::Cast(args[0]); + + if(array->Length() > 0) { + // Create a bson object instance and return it + BSON *bson = new BSON(); + + // Setup pre-allocated comparision objects + bson->_bsontypeString = Persistent<String>::New(String::New("_bsontype")); + bson->_longLowString = Persistent<String>::New(String::New("low_")); + bson->_longHighString = Persistent<String>::New(String::New("high_")); + bson->_objectIDidString = Persistent<String>::New(String::New("id")); + bson->_binaryPositionString = Persistent<String>::New(String::New("position")); + bson->_binarySubTypeString = Persistent<String>::New(String::New("sub_type")); + bson->_binaryBufferString = Persistent<String>::New(String::New("buffer")); + bson->_doubleValueString = Persistent<String>::New(String::New("value")); + bson->_symbolValueString = Persistent<String>::New(String::New("value")); + bson->_dbRefRefString = Persistent<String>::New(String::New("$ref")); + bson->_dbRefIdRefString = Persistent<String>::New(String::New("$id")); + bson->_dbRefDbRefString = Persistent<String>::New(String::New("$db")); + bson->_dbRefNamespaceString = Persistent<String>::New(String::New("namespace")); + bson->_dbRefDbString = Persistent<String>::New(String::New("db")); + bson->_dbRefOidString = Persistent<String>::New(String::New("oid")); + + // total number of found classes + uint32_t numberOfClasses = 0; + + // Iterate over all entries to save the instantiate funtions + for(uint32_t i = 0; i < array->Length(); i++) { + // Let's get a reference to the function + Local<Function> func = Local<Function>::Cast(array->Get(i)); + Local<String> functionName = func->GetName()->ToString(); + + // Save the functions making them persistant handles (they don't get collected) + if(functionName->StrictEquals(String::New("Long"))) { + bson->longConstructor = Persistent<Function>::New(func); + bson->longString = Persistent<String>::New(String::New("Long")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("ObjectID"))) { + bson->objectIDConstructor = Persistent<Function>::New(func); + bson->objectIDString = Persistent<String>::New(String::New("ObjectID")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Binary"))) { + bson->binaryConstructor = Persistent<Function>::New(func); + bson->binaryString = Persistent<String>::New(String::New("Binary")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Code"))) { + bson->codeConstructor = Persistent<Function>::New(func); + bson->codeString = Persistent<String>::New(String::New("Code")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("DBRef"))) { + bson->dbrefConstructor = Persistent<Function>::New(func); + bson->dbrefString = Persistent<String>::New(String::New("DBRef")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Symbol"))) { + bson->symbolConstructor = Persistent<Function>::New(func); + bson->symbolString = Persistent<String>::New(String::New("Symbol")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Double"))) { + bson->doubleConstructor = Persistent<Function>::New(func); + bson->doubleString = Persistent<String>::New(String::New("Double")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Timestamp"))) { + bson->timestampConstructor = Persistent<Function>::New(func); + bson->timestampString = Persistent<String>::New(String::New("Timestamp")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("MinKey"))) { + bson->minKeyConstructor = Persistent<Function>::New(func); + bson->minKeyString = Persistent<String>::New(String::New("MinKey")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("MaxKey"))) { + bson->maxKeyConstructor = Persistent<Function>::New(func); + bson->maxKeyString = Persistent<String>::New(String::New("MaxKey")); + numberOfClasses = numberOfClasses + 1; + } + } + + // Check if we have the right number of constructors otherwise throw an error + if(numberOfClasses != 10) { + // Destroy object + delete(bson); + // Fire exception + return VException("Missing function constructor for either [Long/ObjectID/Binary/Code/DbRef/Symbol/Double/Timestamp/MinKey/MaxKey]"); + } else { + bson->Wrap(args.This()); + return args.This(); + } + } else { + return VException("No types passed in"); + } + } else { + return VException("Argument passed in must be an array of types"); + } +} + +void BSON::write_int32(char *data, uint32_t value) { + // Write the int to the char* + memcpy(data, &value, 4); +} + +void BSON::write_double(char *data, double value) { + // Write the double to the char* + memcpy(data, &value, 8); +} + +void BSON::write_int64(char *data, int64_t value) { + // Write the int to the char* + memcpy(data, &value, 8); +} + +char *BSON::check_key(Local<String> key) { + // Allocate space for they key string + char *key_str = (char *)malloc(key->Utf8Length() * sizeof(char) + 1); + // Error string + char *error_str = (char *)malloc(256 * sizeof(char)); + // Decode the key + ssize_t len = DecodeBytes(key, BINARY); + DecodeWrite(key_str, len, key, BINARY); + *(key_str + key->Utf8Length()) = '\0'; + // Check if we have a valid key + if(key->Utf8Length() > 0 && *(key_str) == '$') { + // Create the string + sprintf(error_str, "key %s must not start with '$'", key_str); + // Free up memory + free(key_str); + // Throw exception with string + throw error_str; + } else if(key->Utf8Length() > 0 && strchr(key_str, '.') != NULL) { + // Create the string + sprintf(error_str, "key %s must not contain '.'", key_str); + // Free up memory + free(key_str); + // Throw exception with string + throw error_str; + } + // Free allocated space + free(key_str); + free(error_str); + // Return No check key error + return NULL; +} + +const char* BSON::ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : "<string conversion failed>"; +} + +Handle<Value> BSON::decodeDBref(BSON *bson, Local<Value> ref, Local<Value> oid, Local<Value> db) { + HandleScope scope; + Local<Value> argv[] = {ref, oid, db}; + Handle<Value> dbrefObj = bson->dbrefConstructor->NewInstance(3, argv); + return scope.Close(dbrefObj); +} + +Handle<Value> BSON::decodeCode(BSON *bson, char *code, Handle<Value> scope_object) { + HandleScope scope; + + Local<Value> argv[] = {String::New(code), scope_object->ToObject()}; + Handle<Value> codeObj = bson->codeConstructor->NewInstance(2, argv); + return scope.Close(codeObj); +} + +Handle<Value> BSON::decodeBinary(BSON *bson, uint32_t sub_type, uint32_t number_of_bytes, char *data) { + HandleScope scope; + + // Create a buffer object that wraps the raw stream + Buffer *bufferObj = Buffer::New(data, number_of_bytes); + // Arguments to be passed to create the binary + Handle<Value> argv[] = {bufferObj->handle_, Uint32::New(sub_type)}; + // Return the buffer handle + Local<Object> bufferObjHandle = bson->binaryConstructor->NewInstance(2, argv); + // Close the scope + return scope.Close(bufferObjHandle); +} + +Handle<Value> BSON::decodeOid(BSON *bson, char *oid) { + HandleScope scope; + + // Encode the string (string - null termiating character) + Local<Value> bin_value = Encode(oid, 12, BINARY)->ToString(); + + // Return the id object + Local<Value> argv[] = {bin_value}; + Local<Object> oidObj = bson->objectIDConstructor->NewInstance(1, argv); + return scope.Close(oidObj); +} + +Handle<Value> BSON::decodeLong(BSON *bson, char *data, uint32_t index) { + HandleScope scope; + + // Decode the integer value + int32_t lowBits = 0; + int32_t highBits = 0; + memcpy(&lowBits, (data + index), 4); + memcpy(&highBits, (data + index + 4), 4); + + // Decode 64bit value + int64_t value = 0; + memcpy(&value, (data + index), 8); + + // If value is < 2^53 and >-2^53 + if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) { + int64_t finalValue = 0; + memcpy(&finalValue, (data + index), 8); + return scope.Close(Number::New(finalValue)); + } + + // Instantiate the js object and pass it back + Local<Value> argv[] = {Int32::New(lowBits), Int32::New(highBits)}; + Local<Object> longObject = bson->longConstructor->NewInstance(2, argv); + return scope.Close(longObject); +} + +Handle<Value> BSON::decodeTimestamp(BSON *bson, char *data, uint32_t index) { + HandleScope scope; + + // Decode the integer value + int32_t lowBits = 0; + int32_t highBits = 0; + memcpy(&lowBits, (data + index), 4); + memcpy(&highBits, (data + index + 4), 4); + + // Build timestamp + Local<Value> argv[] = {Int32::New(lowBits), Int32::New(highBits)}; + Handle<Value> timestamp_obj = bson->timestampConstructor->NewInstance(2, argv); + return scope.Close(timestamp_obj); +} + +// Search for 0 terminated C string and return the string +char* BSON::extract_string(char *data, uint32_t offset) { + char *prt = strchr((data + offset), '\0'); + if(prt == NULL) return NULL; + // Figure out the length of the string + uint32_t length = (prt - data) - offset; + // Allocate memory for the new string + char *string_name = (char *)malloc((length * sizeof(char)) + 1); + // Copy the variable into the string_name + strncpy(string_name, (data + offset), length); + // Ensure the string is null terminated + *(string_name + length) = '\0'; + // Return the unpacked string + return string_name; +} + +// Decode a byte +uint16_t BSON::deserialize_int8(char *data, uint32_t offset) { + uint16_t value = 0; + value |= *(data + offset + 0); + return value; +} + +// Requires a 4 byte char array +uint32_t BSON::deserialize_int32(char* data, uint32_t offset) { + uint32_t value = 0; + memcpy(&value, (data + offset), 4); + return value; +} + +//------------------------------------------------------------------------------------------------ +// +// Experimental +// +//------------------------------------------------------------------------------------------------ +Handle<Value> BSON::CalculateObjectSize2(const Arguments &args) { + HandleScope scope; + // Ensure we have a valid object + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One argument required - [object]"); + if(args.Length() > 1) return VException("One argument required - [object]"); + // Calculate size of the object + uint32_t object_size = BSON::calculate_object_size2(args[0]); + // Return the object size + return scope.Close(Uint32::New(object_size)); +} + +uint32_t BSON::calculate_object_size2(Handle<Value> value) { + // Final object size + uint32_t object_size = (4 + 1); + uint32_t stackIndex = 0; + // Controls the flow + bool done = false; + bool finished = false; + + // Current object we are processing + Local<Object> currentObject = value->ToObject(); + + // Current list of object keys + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> keys = currentObject->GetPropertyNames(); + #else + Local<Array> keys = currentObject->GetOwnPropertyNames(); + #endif + + // Contains pointer to keysIndex + uint32_t keysIndex = 0; + uint32_t keysLength = keys->Length(); + + // printf("=================================================================================\n"); + // printf("Start serializing\n"); + + while(!done) { + // If the index is bigger than the number of keys for the object + // we finished up the previous object and are ready for the next one + if(keysIndex >= keysLength) { + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + keys = currentObject->GetPropertyNames(); + #else + keys = currentObject->GetOwnPropertyNames(); + #endif + keysLength = keys->Length(); + } + + // Iterate over all the keys + while(keysIndex < keysLength) { + // Fetch the key name + Local<String> name = keys->Get(keysIndex++)->ToString(); + // Fetch the object related to the key + Local<Value> value = currentObject->Get(name); + // Add size of the name, plus zero, plus type + object_size += name->Utf8Length() + 1 + 1; + + // If we have a string + if(value->IsString()) { + object_size += value->ToString()->Utf8Length() + 1 + 4; + } else if(value->IsNumber()) { + // Check if we have a float value or a long value + Local<Number> number = value->ToNumber(); + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + object_size = object_size + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + object_size = object_size + 4; + } else { + object_size = object_size + 8; + } + } else if(value->IsBoolean()) { + object_size = object_size + 1; + } else if(value->IsDate()) { + object_size = object_size + 8; + } else if(value->IsRegExp()) { + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + ssize_t len = DecodeBytes(regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + + // global + if((flags & (1 << 0)) != 0) len++; + // ignorecase + if((flags & (1 << 1)) != 0) len++; + //multiline + if((flags & (1 << 2)) != 0) len++; + // if((flags & (1 << 2)) != 0) len++; + // Calculate the space needed for the regexp: size of string - 2 for the /'ses +2 for null termiations + object_size = object_size + len + 2; + } else if(value->IsNull() || value->IsUndefined()) { + } + // } else if(value->IsNumber()) { + // // Check if we have a float value or a long value + // Local<Number> number = value->ToNumber(); + // double d_number = number->NumberValue(); + // int64_t l_number = number->IntegerValue(); + // // Check if we have a double value and not a int64 + // double d_result = d_number - l_number; + // // If we have a value after subtracting the integer value we have a float + // if(d_result > 0 || d_result < 0) { + // object_size = name->Utf8Length() + 1 + object_size + 8 + 1; + // } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + // object_size = name->Utf8Length() + 1 + object_size + 4 + 1; + // } else { + // object_size = name->Utf8Length() + 1 + object_size + 8 + 1; + // } + // } else if(value->IsObject()) { + // printf("------------- hello\n"); + // } + } + + // If we have finished all the keys + if(keysIndex == keysLength) { + finished = false; + } + + // Validate the stack + if(stackIndex == 0) { + // printf("======================================================================== 3\n"); + done = true; + } else if(finished || keysIndex == keysLength) { + // Pop off the stack + stackIndex = stackIndex - 1; + // Fetch the current object stack + // vector<Local<Value> > currentObjectStored = stack.back(); + // stack.pop_back(); + // // Unroll the current object + // currentObject = currentObjectStored.back()->ToObject(); + // currentObjectStored.pop_back(); + // // Unroll the keysIndex + // keys = Local<Array>::Cast(currentObjectStored.back()->ToObject()); + // currentObjectStored.pop_back(); + // // Unroll the keysIndex + // keysIndex = currentObjectStored.back()->ToUint32()->Value(); + // currentObjectStored.pop_back(); + // // Check if we finished up + // if(keysIndex == keys->Length()) { + // finished = true; + // } + } + } + + return object_size; +} + +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +Handle<Value> BSON::BSONDeserialize(const Arguments &args) { + HandleScope scope; + + // Ensure that we have an parameter + if(Buffer::HasInstance(args[0]) && args.Length() > 1) return VException("One argument required - buffer1."); + if(args[0]->IsString() && args.Length() > 1) return VException("One argument required - string1."); + // Throw an exception if the argument is not of type Buffer + if(!Buffer::HasInstance(args[0]) && !args[0]->IsString()) return VException("Argument must be a Buffer or String."); + + // Define pointer to data + char *data; + Local<Object> obj = args[0]->ToObject(); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // If we passed in a buffer, let's unpack it, otherwise let's unpack the string + if(Buffer::HasInstance(obj)) { + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + uint32_t length = buffer->length(); + #else + data = Buffer::Data(obj); + uint32_t length = Buffer::Length(obj); + #endif + + // Validate that we have at least 5 bytes + if(length < 5) { + return VException("corrupt bson message < 5 bytes long"); + } + + // Deserialize the data + return BSON::deserialize(bson, data, length, 0, NULL); + } else { + // The length of the data for this encoding + ssize_t len = DecodeBytes(args[0], BINARY); + + // Validate that we have at least 5 bytes + if(len < 5) { + return VException("corrupt bson message < 5 bytes long"); + } + + // Let's define the buffer size + data = (char *)malloc(len); + // Write the data to the buffer from the string object + ssize_t written = DecodeWrite(data, len, args[0], BINARY); + // Assert that we wrote the same number of bytes as we have length + assert(written == len); + // Get result + Handle<Value> result = BSON::deserialize(bson, data, len, 0, NULL); + // Free memory + free(data); + // Deserialize the content + return result; + } +} + +// Deserialize the stream +Handle<Value> BSON::deserialize(BSON *bson, char *data, uint32_t inDataLength, uint32_t startIndex, bool is_array_item) { + HandleScope scope; + // Holds references to the objects that are going to be returned + Local<Object> return_data = Object::New(); + Local<Array> return_array = Array::New(); + // The current index in the char data + uint32_t index = startIndex; + // Decode the size of the BSON data structure + uint32_t size = BSON::deserialize_int32(data, index); + + // If we have an illegal message size + if(size > inDataLength) return VException("corrupt bson message"); + + // Data length + uint32_t dataLength = index + size; + + // Adjust the index to point to next piece + index = index + 4; + + // While we have data left let's decode + while(index < dataLength) { + // Read the first to bytes to indicate the type of object we are decoding + uint8_t type = BSON::deserialize_int8(data, index); + // Adjust index to skip type byte + index = index + 1; + + if(type == BSON_DATA_STRING) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the length of the string (next 4 bytes) + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust index to point to start of string + index = index + 4; + // Decode the string and add zero terminating value at the end of the string + char *value = (char *)malloc((string_size * sizeof(char))); + strncpy(value, (data + index), string_size); + // Encode the string (string - null termiating character) + Local<Value> utf8_encoded_str = Encode(value, string_size - 1, UTF8)->ToString(); + // Add the value to the data + if(is_array_item) { + return_array->Set(Number::New(insert_index), utf8_encoded_str); + } else { + return_data->ForceSet(String::New(string_name), utf8_encoded_str); + } + + // Adjust index + index = index + string_size; + // Free up the memory + free(value); + free(string_name); + } else if(type == BSON_DATA_INT) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the integer value + uint32_t value = 0; + memcpy(&value, (data + index), 4); + + // Adjust the index for the size of the value + index = index + 4; + // Add the element to the object + if(is_array_item) { + return_array->Set(Integer::New(insert_index), Integer::New(value)); + } else { + return_data->ForceSet(String::New(string_name), Integer::New(value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_TIMESTAMP) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeTimestamp(bson, data, index)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeTimestamp(bson, data, index)); + } + + // Adjust the index for the size of the value + index = index + 8; + + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_LONG) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeLong(bson, data, index)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeLong(bson, data, index)); + } + + // Adjust the index for the size of the value + index = index + 8; + + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_NUMBER) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the integer value + double value = 0; + memcpy(&value, (data + index), 8); + // Adjust the index for the size of the value + index = index + 8; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Number::New(value)); + } else { + return_data->ForceSet(String::New(string_name), Number::New(value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_MIN_KEY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Create new MinKey + Local<Object> minKey = bson->minKeyConstructor->NewInstance(); + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), minKey); + } else { + return_data->ForceSet(String::New(string_name), minKey); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_MAX_KEY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Create new MinKey + Local<Object> maxKey = bson->maxKeyConstructor->NewInstance(); + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), maxKey); + } else { + return_data->ForceSet(String::New(string_name), maxKey); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_NULL) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Null()); + } else { + return_data->ForceSet(String::New(string_name), Null()); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_BOOLEAN) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the boolean value + char bool_value = *(data + index); + // Adjust the index for the size of the value + index = index + 1; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), bool_value == 1 ? Boolean::New(true) : Boolean::New(false)); + } else { + return_data->ForceSet(String::New(string_name), bool_value == 1 ? Boolean::New(true) : Boolean::New(false)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_DATE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the value 64 bit integer + int64_t value = 0; + memcpy(&value, (data + index), 8); + // Adjust the index for the size of the value + index = index + 8; + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Date::New((double)value)); + } else { + return_data->ForceSet(String::New(string_name), Date::New((double)value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_REGEXP) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Length variable + int32_t length_regexp = 0; + char chr; + + // Locate end of the regexp expression \0 + while((chr = *(data + index + length_regexp)) != '\0') { + length_regexp = length_regexp + 1; + } + + // Contains the reg exp + char *reg_exp = (char *)malloc(length_regexp * sizeof(char) + 2); + // Copy the regexp from the data to the char * + memcpy(reg_exp, (data + index), (length_regexp + 1)); + // Adjust the index to skip the first part of the regular expression + index = index + length_regexp + 1; + + // Reset the length + int32_t options_length = 0; + // Locate the end of the options for the regexp terminated with a '\0' + while((chr = *(data + index + options_length)) != '\0') { + options_length = options_length + 1; + } + + // Contains the reg exp + char *options = (char *)malloc(options_length * sizeof(char) + 1); + // Copy the options from the data to the char * + memcpy(options, (data + index), (options_length + 1)); + // Adjust the index to skip the option part of the regular expression + index = index + options_length + 1; + // ARRRRGH Google does not expose regular expressions through the v8 api + // Have to use Script to instantiate the object (slower) + + // Generate the string for execution in the string context + int flag = 0; + + for(int i = 0; i < options_length; i++) { + // Multiline + if(*(options + i) == 'm') { + flag = flag | 4; + } else if(*(options + i) == 'i') { + flag = flag | 2; + } + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), RegExp::New(String::New(reg_exp), (v8::RegExp::Flags)flag)); + } else { + return_data->ForceSet(String::New(string_name), RegExp::New(String::New(reg_exp), (v8::RegExp::Flags)flag)); + } + + // Free memory + free(reg_exp); + free(options); + free(string_name); + } else if(type == BSON_DATA_OID) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // The id string + char *oid_string = (char *)malloc(12 * sizeof(char)); + // Copy the options from the data to the char * + memcpy(oid_string, (data + index), 12); + + // Adjust the index + index = index + 12; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeOid(bson, oid_string)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeOid(bson, oid_string)); + } + + // Free memory + free(oid_string); + free(string_name); + } else if(type == BSON_DATA_BINARY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the binary data size + uint32_t number_of_bytes = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Decode the subtype, ensure it's positive + uint32_t sub_type = (int)*(data + index) & 0xff; + // Adjust the index + index = index + 1; + // Copy the binary data into a buffer + char *buffer = (char *)malloc(number_of_bytes * sizeof(char) + 1); + memcpy(buffer, (data + index), number_of_bytes); + *(buffer + number_of_bytes) = '\0'; + + // Adjust the index + index = index + number_of_bytes; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeBinary(bson, sub_type, number_of_bytes, buffer)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeBinary(bson, sub_type, number_of_bytes, buffer)); + } + // Free memory + free(buffer); + free(string_name); + } else if(type == BSON_DATA_SYMBOL) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the length of the string (next 4 bytes) + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust index to point to start of string + index = index + 4; + // Decode the string and add zero terminating value at the end of the string + char *value = (char *)malloc((string_size * sizeof(char))); + strncpy(value, (data + index), string_size); + // Encode the string (string - null termiating character) + Local<Value> utf8_encoded_str = Encode(value, string_size - 1, UTF8)->ToString(); + + // Wrap up the string in a Symbol Object + Local<Value> argv[] = {utf8_encoded_str}; + Handle<Value> symbolObj = bson->symbolConstructor->NewInstance(1, argv); + + // Add the value to the data + if(is_array_item) { + return_array->Set(Number::New(insert_index), symbolObj); + } else { + return_data->ForceSet(String::New(string_name), symbolObj); + } + + // Adjust index + index = index + string_size; + // Free up the memory + free(value); + free(string_name); + } else if(type == BSON_DATA_CODE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the string size + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string + char *code = (char *)malloc(string_size * sizeof(char) + 1); + // Copy string + terminating 0 + memcpy(code, (data + index), string_size); + + // Define empty scope object + Handle<Value> scope_object = Object::New(); + + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::decodeCode(bson, code, scope_object); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + free(string_name); + free(code); + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(code); + free(string_name); + } else if(type == BSON_DATA_CODE_W_SCOPE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Total number of bytes after array index + uint32_t total_code_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string size + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string + char *code = (char *)malloc(string_size * sizeof(char) + 1); + // Copy string + terminating 0 + memcpy(code, (data + index), string_size); + // Adjust the index + index = index + string_size; + // Get the scope object (bson object) + uint32_t bson_object_size = total_code_size - string_size - 8; + // Allocate bson object buffer and copy out the content + char *bson_buffer = (char *)malloc(bson_object_size * sizeof(char)); + memcpy(bson_buffer, (data + index), bson_object_size); + // Adjust the index + index = index + bson_object_size; + // Parse the bson object + Handle<Value> scope_object = BSON::deserialize(bson, bson_buffer, inDataLength, 0, false); + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::decodeCode(bson, code, scope_object); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Clean up memory allocation + free(string_name); + free(bson_buffer); + free(code); + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(code); + free(bson_buffer); + free(string_name); + } else if(type == BSON_DATA_OBJECT) { + // If this is the top level object we need to skip the undecoding + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Get the object size + uint32_t bson_object_size = BSON::deserialize_int32(data, index); + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::deserialize(bson, data + index, inDataLength, 0, false); + // Adjust the index + index = index + bson_object_size; + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(string_name); + } else if(type == BSON_DATA_ARRAY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Get the size + uint32_t array_size = BSON::deserialize_int32(data, index); + // Define the try catch block + TryCatch try_catch; + + // Decode the code object + Handle<Value> obj = BSON::deserialize(bson, data + index, inDataLength, 0, true); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Rethrow exception + return try_catch.ReThrow(); + } + // Adjust the index for the next value + index = index + array_size; + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + // Clean up memory allocation + free(string_name); + } + } + + // Check if we have a db reference + if(!is_array_item && return_data->Has(String::New("$ref")) && return_data->Has(String::New("$id"))) { + Handle<Value> dbrefValue = BSON::decodeDBref(bson, return_data->Get(String::New("$ref")), return_data->Get(String::New("$id")), return_data->Get(String::New("$db"))); + return scope.Close(dbrefValue); + } + + // Return the data object to javascript + if(is_array_item) { + return scope.Close(return_array); + } else { + return scope.Close(return_data); + } +} + +Handle<Value> BSON::BSONSerialize(const Arguments &args) { + HandleScope scope; + + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 3 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean() && !args[3]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]"); + if(args.Length() > 4) return VException("One, two, tree or four arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + uint32_t object_size = 0; + // Calculate the total size of the document in binary form to ensure we only allocate memory once + // With serialize function + if(args.Length() == 4) { + object_size = BSON::calculate_object_size(bson, args[0], args[3]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Allocate the memory needed for the serializtion + char *serialized_object = (char *)malloc(object_size * sizeof(char)); + // Catch any errors + try { + // Check if we have a boolean value + bool check_key = false; + if(args.Length() >= 3 && args[1]->IsBoolean()) { + check_key = args[1]->BooleanValue(); + } + + // Check if we have a boolean value + bool serializeFunctions = false; + if(args.Length() == 4 && args[1]->IsBoolean()) { + serializeFunctions = args[3]->BooleanValue(); + } + + // Serialize the object + BSON::serialize(bson, serialized_object, 0, Null(), args[0], check_key, serializeFunctions); + } catch(char *err_msg) { + // Free up serialized object space + free(serialized_object); + V8::AdjustAmountOfExternalAllocatedMemory(-object_size); + // Throw exception with the string + Handle<Value> error = VException(err_msg); + // free error message + free(err_msg); + // Return error + return error; + } + + // Write the object size + BSON::write_int32((serialized_object), object_size); + + // If we have 3 arguments + if(args.Length() == 3 || args.Length() == 4) { + // Local<Boolean> asBuffer = args[2]->ToBoolean(); + Buffer *buffer = Buffer::New(serialized_object, object_size); + // Release the serialized string + free(serialized_object); + return scope.Close(buffer->handle_); + } else { + // Encode the string (string - null termiating character) + Local<Value> bin_value = Encode(serialized_object, object_size, BINARY)->ToString(); + // Return the serialized content + return bin_value; + } +} + +Handle<Value> BSON::CalculateObjectSize(const Arguments &args) { + HandleScope scope; + // Ensure we have a valid object + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One argument required - [object]"); + if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return VException("Two arguments required - [object, boolean]"); + if(args.Length() > 3) return VException("One or two arguments required - [object] or [object, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Object size + uint32_t object_size = 0; + // Check if we have our argument, calculate size of the object + if(args.Length() >= 2) { + object_size = BSON::calculate_object_size(bson, args[0], args[1]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Return the object size + return scope.Close(Uint32::New(object_size)); +} + +uint32_t BSON::calculate_object_size(BSON *bson, Handle<Value> value, bool serializeFunctions) { + uint32_t object_size = 0; + + // If we have an object let's unwrap it and calculate the sub sections + if(value->IsString()) { + // Let's calculate the size the string adds, length + type(1 byte) + size(4 bytes) + object_size += value->ToString()->Utf8Length() + 1 + 4; + } else if(value->IsNumber()) { + // Check if we have a float value or a long value + Local<Number> number = value->ToNumber(); + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + object_size = object_size + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + object_size = object_size + 4; + } else { + object_size = object_size + 8; + } + } else if(value->IsBoolean()) { + object_size = object_size + 1; + } else if(value->IsDate()) { + object_size = object_size + 8; + } else if(value->IsRegExp()) { + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + ssize_t len = DecodeBytes(regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + + // global + if((flags & (1 << 0)) != 0) len++; + // ignorecase + if((flags & (1 << 1)) != 0) len++; + //multiline + if((flags & (1 << 2)) != 0) len++; + // if((flags & (1 << 2)) != 0) len++; + // Calculate the space needed for the regexp: size of string - 2 for the /'ses +2 for null termiations + object_size = object_size + len + 2; + } else if(value->IsNull() || value->IsUndefined()) { + } else if(value->IsArray()) { + // Cast to array + Local<Array> array = Local<Array>::Cast(value->ToObject()); + // Turn length into string to calculate the size of all the strings needed + char *length_str = (char *)malloc(256 * sizeof(char)); + // Calculate the size of each element + for(uint32_t i = 0; i < array->Length(); i++) { + // Add "index" string size for each element + sprintf(length_str, "%d", i); + // Add the size of the string length + uint32_t label_length = strlen(length_str) + 1; + // Add the type definition size for each item + object_size = object_size + label_length + 1; + // Add size of the object + uint32_t object_length = BSON::calculate_object_size(bson, array->Get(Integer::New(i)), serializeFunctions); + object_size = object_size + object_length; + } + // Add the object size + object_size = object_size + 4 + 1; + // Free up memory + free(length_str); + } else if(value->IsFunction()) { + if(serializeFunctions) { + object_size += value->ToString()->Utf8Length() + 4 + 1; + } + } else if(value->ToObject()->Has(bson->_bsontypeString)) { + // Handle holder + Local<String> constructorString = value->ToObject()->GetConstructorName(); + + // BSON type object, avoid non-needed checking unless we have a type + if(bson->longString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } else if(bson->timestampString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } else if(bson->objectIDString->StrictEquals(constructorString)) { + object_size = object_size + 12; + } else if(bson->binaryString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Uint32> positionObj = value->ToObject()->Get(String::New("position"))->ToUint32(); + // Adjust the object_size, binary content lengt + total size int32 + binary size int32 + subtype + object_size += positionObj->Value() + 4 + 1; + } else if(bson->codeString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Object> obj = value->ToObject(); + // Get the function + Local<String> function = obj->Get(String::New("code"))->ToString(); + // Get the scope object + Local<Object> scope = obj->Get(String::New("scope"))->ToObject(); + + // For Node < 0.6.X use the GetPropertyNames + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + uint32_t propertyNameLength = scope->GetPropertyNames()->Length(); + #else + uint32_t propertyNameLength = scope->GetOwnPropertyNames()->Length(); + #endif + + // Check if the scope has any parameters + // Let's calculate the size the code object adds adds + if(propertyNameLength > 0) { + object_size += function->Utf8Length() + 4 + BSON::calculate_object_size(bson, scope, serializeFunctions) + 4 + 1; + } else { + object_size += function->Utf8Length() + 4 + 1; + } + } else if(bson->dbrefString->StrictEquals(constructorString)) { + // Unpack the dbref + Local<Object> dbref = value->ToObject(); + // Create an object containing the right namespace variables + Local<Object> obj = Object::New(); + // Build the new object + obj->Set(bson->_dbRefRefString, dbref->Get(bson->_dbRefNamespaceString)); + obj->Set(bson->_dbRefIdRefString, dbref->Get(bson->_dbRefOidString)); + if(!dbref->Get(bson->_dbRefDbString)->IsNull() && !dbref->Get(bson->_dbRefDbString)->IsUndefined()) obj->Set(bson->_dbRefDbRefString, dbref->Get(bson->_dbRefDbString)); + // Calculate size + object_size += BSON::calculate_object_size(bson, obj, serializeFunctions); + } else if(bson->minKeyString->StrictEquals(constructorString) || bson->maxKeyString->Equals(constructorString)) { + } else if(bson->symbolString->StrictEquals(constructorString)) { + // Get string + Local<String> str = value->ToObject()->Get(String::New("value"))->ToString(); + // Get the utf8 length + int utf8_length = str->Utf8Length(); + // Check if we have a utf8 encoded string or not + if(utf8_length != str->Length()) { + // Let's calculate the size the string adds, length + type(1 byte) + size(4 bytes) + object_size += str->Utf8Length() + 1 + 4; + } else { + object_size += str->Length() + 1 + 4; + } + } else if(bson->doubleString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } + } else if(value->IsObject()) { + // Unwrap the object + Local<Object> object = value->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> property_names = object->GetPropertyNames(); + #else + Local<Array> property_names = object->GetOwnPropertyNames(); + #endif + + // Length of the property + uint32_t propertyLength = property_names->Length(); + + // Process all the properties on the object + for(uint32_t index = 0; index < propertyLength; index++) { + // Fetch the property name + Local<String> property_name = property_names->Get(index)->ToString(); + + // Fetch the object for the property + Local<Value> property = object->Get(property_name); + // Get size of property (property + property name length + 1 for terminating 0) + if(!property->IsFunction() || (property->IsFunction() && serializeFunctions)) { + // Convert name to char* + object_size += BSON::calculate_object_size(bson, property, serializeFunctions) + property_name->Utf8Length() + 1 + 1; + } + } + + object_size = object_size + 4 + 1; + } + + return object_size; +} + +uint32_t BSON::serialize(BSON *bson, char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, bool check_key, bool serializeFunctions) { + // Scope for method execution + HandleScope scope; + + // If we have a name check that key is valid + if(!name->IsNull() && check_key) { + if(BSON::check_key(name->ToString()) != NULL) return -1; + } + + // If we have an object let's serialize it + if(value->IsString()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_STRING; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Write the actual string into the char array + Local<String> str = value->ToString(); + // Let's fetch the int value + uint32_t utf8_length = str->Utf8Length(); + + // Write the integer to the char * + BSON::write_int32((serialized_object + index), utf8_length + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + str->WriteUtf8((serialized_object + index), utf8_length); + // Add the null termination + *(serialized_object + index + utf8_length) = '\0'; + // Adjust the index + index = index + utf8_length + 1; + } else if(value->IsNumber()) { + uint32_t first_pointer = index; + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_INT; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + Local<Number> number = value->ToNumber(); + // Get the values + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + // Write the double to the char array + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust index for double + index = index + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + // Smaller than 32 bit, write as 32 bit value + BSON::write_int32(serialized_object + index, value->ToInt32()->Value()); + // Adjust the size of the index + index = index + 4; + } else if(l_number <= JS_INT_MAX && l_number >= JS_INT_MIN) { + // Write the double to the char array + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust index for double + index = index + 8; + } else { + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust the size of the index + index = index + 8; + } + } else if(value->IsBoolean()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_BOOLEAN; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Save the boolean value + *(serialized_object + index) = value->BooleanValue() ? '\1' : '\0'; + // Adjust the index + index = index + 1; + } else if(value->IsDate()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_DATE; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Fetch the Integer value + int64_t integer_value = value->IntegerValue(); + BSON::write_int64((serialized_object + index), integer_value); + // Adjust the index + index = index + 8; + } else if(value->IsNull() || value->IsUndefined()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_NULL; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + } else if(value->IsArray()) { + // Cast to array + Local<Array> array = Local<Array>::Cast(value->ToObject()); + // Turn length into string to calculate the size of all the strings needed + char *length_str = (char *)malloc(256 * sizeof(char)); + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_ARRAY; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + // Object size + uint32_t object_size = BSON::calculate_object_size(bson, value, serializeFunctions); + // Write the size of the object + BSON::write_int32((serialized_object + index), object_size); + // Adjust the index + index = index + 4; + // Write out all the elements + for(uint32_t i = 0; i < array->Length(); i++) { + // Add "index" string size for each element + sprintf(length_str, "%d", i); + // Encode the values + index = BSON::serialize(bson, serialized_object, index, String::New(length_str), array->Get(Integer::New(i)), check_key, serializeFunctions); + // Write trailing '\0' for object + *(serialized_object + index) = '\0'; + } + + // Pad the last item + *(serialized_object + index) = '\0'; + index = index + 1; + // Free up memory + free(length_str); + } else if(value->IsRegExp()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_REGEXP; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + len = DecodeBytes(regExp->GetSource(), UTF8); + written = DecodeWrite((serialized_object + index), len, regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // global + if((flags & (1 << 0)) != 0) { + *(serialized_object + index) = 's'; + index = index + 1; + } + + // ignorecase + if((flags & (1 << 1)) != 0) { + *(serialized_object + index) = 'i'; + index = index + 1; + } + + //multiline + if((flags & (1 << 2)) != 0) { + *(serialized_object + index) = 'm'; + index = index + 1; + } + + // Add null termiation for the string + *(serialized_object + index) = '\0'; + // Adjust the index + index = index + 1; + } else if(value->IsFunction()) { + if(serializeFunctions) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_CODE; + + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Function String + Local<String> function = value->ToString(); + + // Decode the function + len = DecodeBytes(function, BINARY); + // Write the size of the code string + 0 byte end of cString + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + written = DecodeWrite((serialized_object + index), len, function, BINARY); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + } + } else if(value->ToObject()->Has(bson->_bsontypeString)) { + // Handle holder + Local<String> constructorString = value->ToObject()->GetConstructorName(); + uint32_t originalIndex = index; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + + // BSON type object, avoid non-needed checking unless we have a type + if(bson->longString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_LONG; + // Object reference + Local<Object> longObject = value->ToObject(); + + // Fetch the low and high bits + int32_t lowBits = longObject->Get(bson->_longLowString)->ToInt32()->Value(); + int32_t highBits = longObject->Get(bson->_longHighString)->ToInt32()->Value(); + + // Write the content to the char array + BSON::write_int32((serialized_object + index), lowBits); + BSON::write_int32((serialized_object + index + 4), highBits); + // Adjust the index + index = index + 8; + } else if(bson->timestampString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_TIMESTAMP; + // Object reference + Local<Object> timestampObject = value->ToObject(); + + // Fetch the low and high bits + int32_t lowBits = timestampObject->Get(bson->_longLowString)->ToInt32()->Value(); + int32_t highBits = timestampObject->Get(bson->_longHighString)->ToInt32()->Value(); + + // Write the content to the char array + BSON::write_int32((serialized_object + index), lowBits); + BSON::write_int32((serialized_object + index + 4), highBits); + // Adjust the index + index = index + 8; + } else if(bson->objectIDString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_OID; + // Convert to object + Local<Object> objectIDObject = value->ToObject(); + // Let's grab the id + Local<String> idString = objectIDObject->Get(bson->_objectIDidString)->ToString(); + // Let's decode the raw chars from the string + len = DecodeBytes(idString, BINARY); + written = DecodeWrite((serialized_object + index), len, idString, BINARY); + // Adjust the index + index = index + 12; + } else if(bson->binaryString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_BINARY; + + // Let's get the binary object + Local<Object> binaryObject = value->ToObject(); + + // Grab the size(position of the binary) + uint32_t position = value->ToObject()->Get(bson->_binaryPositionString)->ToUint32()->Value(); + // Grab the subtype + uint32_t subType = value->ToObject()->Get(bson->_binarySubTypeString)->ToUint32()->Value(); + // Grab the buffer object + Local<Object> bufferObj = value->ToObject()->Get(bson->_binaryBufferString)->ToObject(); + + // Buffer data pointers + char *data; + uint32_t length; + + // Unpack the buffer variable + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(bufferObj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(bufferObj); + length = Buffer::Length(bufferObj); + #endif + + // Write the size of the buffer out + BSON::write_int32((serialized_object + index), position); + // Adjust index + index = index + 4; + // Write subtype + *(serialized_object + index) = (char)subType; + // Adjust index + index = index + 1; + // Write binary content + memcpy((serialized_object + index), data, position); + // Adjust index.rar">_</a> + index = index + position; + } else if(bson->doubleString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_NUMBER; + + // Unpack the double + Local<Object> doubleObject = value->ToObject(); + + // Fetch the double value + Local<Number> doubleValue = doubleObject->Get(bson->_doubleValueString)->ToNumber(); + // Write the double to the char array + BSON::write_double((serialized_object + index), doubleValue->NumberValue()); + // Adjust index for double + index = index + 8; + } else if(bson->symbolString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_SYMBOL; + // Unpack symbol object + Local<Object> symbolObj = value->ToObject(); + + // Grab the actual string + Local<String> str = symbolObj->Get(bson->_symbolValueString)->ToString(); + // Let's fetch the int value + int utf8_length = str->Utf8Length(); + + // If the Utf8 length is different from the string length then we + // have a UTF8 encoded string, otherwise write it as ascii + if(utf8_length != str->Length()) { + // Write the integer to the char * + BSON::write_int32((serialized_object + index), utf8_length + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + str->WriteUtf8((serialized_object + index), utf8_length); + // Add the null termination + *(serialized_object + index + utf8_length) = '\0'; + // Adjust the index + index = index + utf8_length + 1; + } else { + // Write the integer to the char * + BSON::write_int32((serialized_object + index), str->Length() + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + written = DecodeWrite((serialized_object + index), str->Length(), str, BINARY); + // Add the null termination + *(serialized_object + index + str->Length()) = '\0'; + // Adjust the index + index = index + str->Length() + 1; + } + } else if(bson->codeString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Object> obj = value->ToObject(); + // Get the function + Local<String> function = obj->Get(String::New("code"))->ToString(); + // Get the scope object + Local<Object> scope = obj->Get(String::New("scope"))->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + uint32_t propertyNameLength = scope->GetPropertyNames()->Length(); + #else + uint32_t propertyNameLength = scope->GetOwnPropertyNames()->Length(); + #endif + + // Set the right type if we have a scope or not + if(propertyNameLength > 0) { + // Set basic data code object with scope object + *(serialized_object + originalIndex) = BSON_DATA_CODE_W_SCOPE; + + // Calculate the size of the whole object + uint32_t scopeSize = BSON::calculate_object_size(bson, scope, false); + // Decode the function length + ssize_t len = DecodeBytes(function, UTF8); + // Calculate total size + uint32_t size = 4 + len + 1 + 4 + scopeSize; + + // Write the total size + BSON::write_int32((serialized_object + index), size); + // Adjust the index + index = index + 4; + + // Write the function size + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + ssize_t written = DecodeWrite((serialized_object + index), len, function, UTF8); + assert(written == len); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index with the length of the function + index = index + len + 1; + // Write the scope object + BSON::serialize(bson, (serialized_object + index), 0, Null(), scope, check_key, serializeFunctions); + // Adjust the index + index = index + scopeSize; + } else { + // Set basic data code object + *(serialized_object + originalIndex) = BSON_DATA_CODE; + // Decode the function + ssize_t len = DecodeBytes(function, BINARY); + // Write the size of the code string + 0 byte end of cString + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + ssize_t written = DecodeWrite((serialized_object + index), len, function, BINARY); + assert(written == len); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + } + } else if(bson->dbrefString->StrictEquals(constructorString)) { + // Unpack the dbref + Local<Object> dbref = value->ToObject(); + // Create an object containing the right namespace variables + Local<Object> obj = Object::New(); + + // Build the new object + obj->Set(bson->_dbRefRefString, dbref->Get(bson->_dbRefNamespaceString)); + obj->Set(bson->_dbRefIdRefString, dbref->Get(bson->_dbRefOidString)); + if(!dbref->Get(bson->_dbRefDbString)->IsNull() && !dbref->Get(bson->_dbRefDbString)->IsUndefined()) obj->Set(bson->_dbRefDbRefString, dbref->Get(bson->_dbRefDbString)); + + // Encode the variable + index = BSON::serialize(bson, serialized_object, originalIndex, name, obj, false, serializeFunctions); + } else if(bson->minKeyString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_MIN_KEY; + } else if(bson->maxKeyString->StrictEquals(constructorString)) { + *(serialized_object + originalIndex) = BSON_DATA_MAX_KEY; + } + } else if(value->IsObject()) { + if(!name->IsNull()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_OBJECT; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + } + + // Unwrap the object + Local<Object> object = value->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> property_names = object->GetPropertyNames(); + #else + Local<Array> property_names = object->GetOwnPropertyNames(); + #endif + + // Calculate size of the total object + uint32_t object_size = BSON::calculate_object_size(bson, value, serializeFunctions); + // Write the size + BSON::write_int32((serialized_object + index), object_size); + // Adjust size + index = index + 4; + + // Process all the properties on the object + for(uint32_t i = 0; i < property_names->Length(); i++) { + // Fetch the property name + Local<String> property_name = property_names->Get(i)->ToString(); + // Fetch the object for the property + Local<Value> property = object->Get(property_name); + // Write the next serialized object + // printf("========== !property->IsFunction() || (property->IsFunction() && serializeFunctions) = %d\n", !property->IsFunction() || (property->IsFunction() && serializeFunctions) == true ? 1 : 0); + if(!property->IsFunction() || (property->IsFunction() && serializeFunctions)) { + // Convert name to char* + ssize_t len = DecodeBytes(property_name, UTF8); + // char *data = new char[len]; + char *data = (char *)malloc(len + 1); + *(data + len) = '\0'; + ssize_t written = DecodeWrite(data, len, property_name, UTF8); + assert(written == len); + // Serialize the content + index = BSON::serialize(bson, serialized_object, index, property_name, property, check_key, serializeFunctions); + // Free up memory of data + free(data); + } + } + // Pad the last item + *(serialized_object + index) = '\0'; + index = index + 1; + + // Null out reminding fields if we have a toplevel object and nested levels + if(name->IsNull()) { + for(uint32_t i = 0; i < (object_size - index); i++) { + *(serialized_object + index + i) = '\0'; + } + } + } + + return index; +} + +Handle<Value> BSON::SerializeWithBufferAndIndex(const Arguments &args) { + HandleScope scope; + + //BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index) { + // Ensure we have the correct values + if(args.Length() > 5) return VException("Four or five parameters required [object, boolean, Buffer, int] or [object, boolean, Buffer, int, boolean]"); + if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32()) return VException("Four parameters required [object, boolean, Buffer, int]"); + if(args.Length() == 5 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32() && !args[4]->IsBoolean()) return VException("Four parameters required [object, boolean, Buffer, int, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Define pointer to data + char *data; + uint32_t length; + // Unpack the object + Local<Object> obj = args[2]->ToObject(); + + // Unpack the buffer object and get pointers to structures + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(obj); + length = Buffer::Length(obj); + #endif + + uint32_t object_size = 0; + // Calculate the total size of the document in binary form to ensure we only allocate memory once + if(args.Length() == 5) { + object_size = BSON::calculate_object_size(bson, args[0], args[4]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Unpack the index variable + Local<Uint32> indexObject = args[3]->ToUint32(); + uint32_t index = indexObject->Value(); + + // Allocate the memory needed for the serializtion + char *serialized_object = (char *)malloc(object_size * sizeof(char)); + + // Catch any errors + try { + // Check if we have a boolean value + bool check_key = false; + if(args.Length() >= 4 && args[1]->IsBoolean()) { + check_key = args[1]->BooleanValue(); + } + + bool serializeFunctions = false; + if(args.Length() == 5) { + serializeFunctions = args[4]->BooleanValue(); + } + + // Serialize the object + BSON::serialize(bson, serialized_object, 0, Null(), args[0], check_key, serializeFunctions); + } catch(char *err_msg) { + // Free up serialized object space + free(serialized_object); + V8::AdjustAmountOfExternalAllocatedMemory(-object_size); + // Throw exception with the string + Handle<Value> error = VException(err_msg); + // free error message + free(err_msg); + // Return error + return error; + } + + for(uint32_t i = 0; i < object_size; i++) { + *(data + index + i) = *(serialized_object + i); + } + + return scope.Close(Uint32::New(index + object_size - 1)); +} + +Handle<Value> BSON::BSONDeserializeStream(const Arguments &args) { + HandleScope scope; + + // At least 3 arguments required + if(args.Length() < 5) VException("Arguments required (Buffer(data), Number(index in data), Number(number of documents to deserialize), Array(results), Number(index in the array), Object(optional))"); + + // If the number of argumets equals 3 + if(args.Length() >= 5) { + if(!Buffer::HasInstance(args[0])) return VException("First argument must be Buffer instance"); + if(!args[1]->IsUint32()) return VException("Second argument must be a positive index number"); + if(!args[2]->IsUint32()) return VException("Third argument must be a positive number of documents to deserialize"); + if(!args[3]->IsArray()) return VException("Fourth argument must be an array the size of documents to deserialize"); + if(!args[4]->IsUint32()) return VException("Sixth argument must be a positive index number"); + } + + // If we have 4 arguments + if(args.Length() == 6 && !args[5]->IsObject()) return VException("Fifth argument must be an object with options"); + + // Define pointer to data + char *data; + uint32_t length; + Local<Object> obj = args[0]->ToObject(); + uint32_t numberOfDocuments = args[2]->ToUint32()->Value(); + uint32_t index = args[1]->ToUint32()->Value(); + uint32_t resultIndex = args[4]->ToUint32()->Value(); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Unpack the buffer variable + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(obj); + length = Buffer::Length(obj); + #endif + + // Fetch the documents + Local<Object> documents = args[3]->ToObject(); + + for(uint32_t i = 0; i < numberOfDocuments; i++) { + // Decode the size of the BSON data structure + uint32_t size = BSON::deserialize_int32(data, index); + + // Get result + Handle<Value> result = BSON::deserialize(bson, data, size, index, NULL); + + // Add result to array + documents->Set(i + resultIndex, result); + + // Adjust the index for next pass + index = index + size; + } + + // Return new index of parsing + return scope.Close(Uint32::New(index)); +} + +// Exporting function +extern "C" void init(Handle<Object> target) { + HandleScope scope; + BSON::Initialize(target); +} + +// NODE_MODULE(bson, BSON::Initialize); +// NODE_MODULE(l, Long::Initialize); diff --git a/node_modules/mongodb/external-libs/bson/bson.h b/node_modules/mongodb/external-libs/bson/bson.h new file mode 100644 index 0000000..dcf21d1 --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/bson.h @@ -0,0 +1,105 @@ +#ifndef BSON_H_ +#define BSON_H_ + +#include <node.h> +#include <node_object_wrap.h> +#include <v8.h> + +using namespace v8; +using namespace node; + +class BSON : public ObjectWrap { + public: + BSON() : ObjectWrap() {} + ~BSON() {} + + static void Initialize(Handle<Object> target); + static Handle<Value> BSONDeserializeStream(const Arguments &args); + + // JS based objects + static Handle<Value> BSONSerialize(const Arguments &args); + static Handle<Value> BSONDeserialize(const Arguments &args); + + // Calculate size of function + static Handle<Value> CalculateObjectSize(const Arguments &args); + static Handle<Value> SerializeWithBufferAndIndex(const Arguments &args); + + // Experimental + static Handle<Value> CalculateObjectSize2(const Arguments &args); + static Handle<Value> BSONSerialize2(const Arguments &args); + + // Constructor used for creating new BSON objects from C++ + static Persistent<FunctionTemplate> constructor_template; + + private: + static Handle<Value> New(const Arguments &args); + static Handle<Value> deserialize(BSON *bson, char *data, uint32_t dataLength, uint32_t startIndex, bool is_array_item); + static uint32_t serialize(BSON *bson, char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, bool check_key, bool serializeFunctions); + + static char* extract_string(char *data, uint32_t offset); + static const char* ToCString(const v8::String::Utf8Value& value); + static uint32_t calculate_object_size(BSON *bson, Handle<Value> object, bool serializeFunctions); + + static void write_int32(char *data, uint32_t value); + static void write_int64(char *data, int64_t value); + static void write_double(char *data, double value); + static uint16_t deserialize_int8(char *data, uint32_t offset); + static uint32_t deserialize_int32(char* data, uint32_t offset); + static char *check_key(Local<String> key); + + // BSON type instantiate functions + Persistent<Function> longConstructor; + Persistent<Function> objectIDConstructor; + Persistent<Function> binaryConstructor; + Persistent<Function> codeConstructor; + Persistent<Function> dbrefConstructor; + Persistent<Function> symbolConstructor; + Persistent<Function> doubleConstructor; + Persistent<Function> timestampConstructor; + Persistent<Function> minKeyConstructor; + Persistent<Function> maxKeyConstructor; + + // Equality Objects + Persistent<String> longString; + Persistent<String> objectIDString; + Persistent<String> binaryString; + Persistent<String> codeString; + Persistent<String> dbrefString; + Persistent<String> symbolString; + Persistent<String> doubleString; + Persistent<String> timestampString; + Persistent<String> minKeyString; + Persistent<String> maxKeyString; + + // Equality speed up comparision objects + Persistent<String> _bsontypeString; + Persistent<String> _longLowString; + Persistent<String> _longHighString; + Persistent<String> _objectIDidString; + Persistent<String> _binaryPositionString; + Persistent<String> _binarySubTypeString; + Persistent<String> _binaryBufferString; + Persistent<String> _doubleValueString; + Persistent<String> _symbolValueString; + + Persistent<String> _dbRefRefString; + Persistent<String> _dbRefIdRefString; + Persistent<String> _dbRefDbRefString; + Persistent<String> _dbRefNamespaceString; + Persistent<String> _dbRefDbString; + Persistent<String> _dbRefOidString; + + // Decode JS function + static Handle<Value> decodeLong(BSON *bson, char *data, uint32_t index); + static Handle<Value> decodeTimestamp(BSON *bson, char *data, uint32_t index); + static Handle<Value> decodeOid(BSON *bson, char *oid); + static Handle<Value> decodeBinary(BSON *bson, uint32_t sub_type, uint32_t number_of_bytes, char *data); + static Handle<Value> decodeCode(BSON *bson, char *code, Handle<Value> scope); + static Handle<Value> decodeDBref(BSON *bson, Local<Value> ref, Local<Value> oid, Local<Value> db); + + // Experimental + static uint32_t calculate_object_size2(Handle<Value> object); + static uint32_t serialize2(char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, uint32_t object_size, bool check_key); +}; + +#endif // BSON_H_ diff --git a/node_modules/mongodb/external-libs/bson/index.js b/node_modules/mongodb/external-libs/bson/index.js new file mode 100644 index 0000000..2c66dee --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/index.js @@ -0,0 +1,20 @@ +var bson = require('./bson'); +exports.BSON = bson.BSON; +exports.Long = require('../../lib/mongodb/bson/long').Long; +exports.ObjectID = require('../../lib/mongodb/bson/objectid').ObjectID; +exports.DBRef = require('../../lib/mongodb/bson/db_ref').DBRef; +exports.Code = require('../../lib/mongodb/bson/code').Code; +exports.Timestamp = require('../../lib/mongodb/bson/timestamp').Timestamp; +exports.Binary = require('../../lib/mongodb/bson/binary').Binary; +exports.Double = require('../../lib/mongodb/bson/double').Double; +exports.MaxKey = require('../../lib/mongodb/bson/max_key').MaxKey; +exports.MinKey = require('../../lib/mongodb/bson/min_key').MinKey; +exports.Symbol = require('../../lib/mongodb/bson/symbol').Symbol; + +// Just add constants tot he Native BSON parser +exports.BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +exports.BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +exports.BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +exports.BSON.BSON_BINARY_SUBTYPE_UUID = 3; +exports.BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +exports.BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; diff --git a/node_modules/mongodb/external-libs/bson/test/test_bson.js b/node_modules/mongodb/external-libs/bson/test/test_bson.js new file mode 100644 index 0000000..706f1df --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/test/test_bson.js @@ -0,0 +1,349 @@ +var sys = require('util'), + debug = require('util').debug, + inspect = require('util').inspect, + Buffer = require('buffer').Buffer, + BSON = require('../bson').BSON, + Buffer = require('buffer').Buffer, + BSONJS = require('../../../lib/mongodb/bson/bson').BSON, + BinaryParser = require('../../../lib/mongodb/bson/binary_parser').BinaryParser, + Long = require('../../../lib/mongodb/bson/long').Long, + ObjectID = require('../../../lib/mongodb/bson/bson').ObjectID, + Binary = require('../../../lib/mongodb/bson/bson').Binary, + Code = require('../../../lib/mongodb/bson/bson').Code, + DBRef = require('../../../lib/mongodb/bson/bson').DBRef, + Symbol = require('../../../lib/mongodb/bson/bson').Symbol, + Double = require('../../../lib/mongodb/bson/bson').Double, + MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, + MinKey = require('../../../lib/mongodb/bson/bson').MinKey, + Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp, + assert = require('assert'); + +if(process.env['npm_package_config_native'] != null) return; + +sys.puts("=== EXECUTING TEST_BSON ==="); + +// Should fail due to illegal key +assert.throws(function() { new ObjectID('foo'); }) +assert.throws(function() { new ObjectID('foo'); }) + +// Parsers +var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); +var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + +// Simple serialization and deserialization of edge value +var doc = {doc:0x1ffffffffffffe}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +var doc = {doc:-0x1ffffffffffffe}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// +// Assert correct toJSON +// +var a = Long.fromNumber(10); +assert.equal(10, a); + +var a = Long.fromNumber(9223372036854775807); +assert.equal(9223372036854775807, a); + +// Simple serialization and deserialization test for a Single String value +var doc = {doc:'Serialize'}; +var simple_string_serialized = bsonC.serialize(doc, true, false); + +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Nested doc +var doc = {a:{b:{c:1}}}; +var simple_string_serialized = bsonC.serialize(doc, false, true); + +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple integer serialization/deserialization test, including testing boundary conditions +var doc = {doc:-1}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +var doc = {doc:2147483648}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +var doc = {doc:-2147483648}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization test for a Long value +var doc = {doc:Long.fromNumber(9223372036854775807)}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(9223372036854775807)}, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +var doc = {doc:Long.fromNumber(-9223372036854775807)}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(-9223372036854775807)}, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization for a Float value +var doc = {doc:2222.3333}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +var doc = {doc:-2222.3333}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization for a null value +var doc = {doc:null}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization for a boolean value +var doc = {doc:true}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization for a date value +var date = new Date(); +var doc = {doc:date}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + +// Simple serialization and deserialization for a boolean value +var doc = {doc:/abcd/mi}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + +var doc = {doc:/abcd/}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); +assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + +// Simple serialization and deserialization for a objectId value +var doc = {doc:new ObjectID()}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +var doc2 = {doc:ObjectID.createFromHexString(doc.doc.toHexString())}; + +assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc2, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + +// Simple serialization and deserialization for a Binary value +var binary = new Binary(); +var string = 'binstring' +for(var index = 0; index < string.length; index++) { binary.put(string.charAt(index)); } + +var Binary = new Binary(); +var string = 'binstring' +for(var index = 0; index < string.length; index++) { Binary.put(string.charAt(index)); } + +var simple_string_serialized = bsonC.serialize({doc:binary}, false, true); +assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Binary}, false, true)); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.value(), bsonC.deserialize(simple_string_serialized).doc.value()); + +// Simple serialization and deserialization for a Code value +var code = new Code('this.a > i', {'i': 1}); +var Code = new Code('this.a > i', {'i': 1}); +var simple_string_serialized_2 = bsonJS.serialize({doc:Code}, false, true); +var simple_string_serialized = bsonC.serialize({doc:code}, false, true); + +assert.deepEqual(simple_string_serialized, simple_string_serialized_2); +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc.scope, bsonC.deserialize(simple_string_serialized).doc.scope); + +// Simple serialization and deserialization for an Object +var simple_string_serialized = bsonC.serialize({doc:{a:1, b:{c:2}}}, false, true); +var simple_string_serialized_2 = bsonJS.serialize({doc:{a:1, b:{c:2}}}, false, true); +assert.deepEqual(simple_string_serialized, simple_string_serialized_2) +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); + +// Simple serialization and deserialization for an Array +var simple_string_serialized = bsonC.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); +var simple_string_serialized_2 = bsonJS.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); + +assert.deepEqual(simple_string_serialized, simple_string_serialized_2) +assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); + +// Simple serialization and deserialization for a DBRef +var oid = new ObjectID() +var oid2 = new ObjectID.createFromHexString(oid.toHexString()) +var simple_string_serialized = bsonJS.serialize({doc:new DBRef('namespace', oid2, 'integration_tests_')}, false, true); +var simple_string_serialized_2 = bsonC.serialize({doc:new DBRef('namespace', oid, 'integration_tests_')}, false, true); + +assert.deepEqual(simple_string_serialized, simple_string_serialized_2) +// Ensure we have the same values for the dbref +var object_js = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); +var object_c = bsonC.deserialize(simple_string_serialized); + +assert.equal(object_js.doc.namespace, object_c.doc.namespace); +assert.equal(object_js.doc.oid.toHexString(), object_c.doc.oid.toHexString()); +assert.equal(object_js.doc.db, object_c.doc.db); + +// Serialized document +var bytes = [47,0,0,0,2,110,97,109,101,0,6,0,0,0,80,97,116,116,121,0,16,97,103,101,0,34,0,0,0,7,95,105,100,0,76,100,12,23,11,30,39,8,89,0,0,1,0]; +var serialized_data = ''; +// Convert to chars +for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); +} +var object = bsonC.deserialize(new Buffer(serialized_data, 'binary')); +assert.equal('Patty', object.name) +assert.equal(34, object.age) +assert.equal('4c640c170b1e270859000001', object._id.toHexString()) + +// Serialize utf8 +var doc = { "name" : "本荘由利地域に洪水警報", "name1" : "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "name2" : "abcdedede"}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +var simple_string_serialized2 = bsonJS.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, simple_string_serialized2) + +var object = bsonC.deserialize(simple_string_serialized); +assert.equal(doc.name, object.name) +assert.equal(doc.name1, object.name1) +assert.equal(doc.name2, object.name2) + +// Serialize object with array +var doc = {b:[1, 2, 3]}; +var simple_string_serialized = bsonC.serialize(doc, false, true); +var simple_string_serialized_2 = bsonJS.serialize(doc, false, true); +assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + +var object = bsonC.deserialize(simple_string_serialized); +assert.deepEqual(doc, object) + +// Test equality of an object ID +var object_id = new ObjectID(); +var object_id_2 = new ObjectID(); +assert.ok(object_id.equals(object_id)); +assert.ok(!(object_id.equals(object_id_2))) + +// Test same serialization for Object ID +var object_id = new ObjectID(); +var object_id2 = ObjectID.createFromHexString(object_id.toString()) +var simple_string_serialized = bsonJS.serialize({doc:object_id}, false, true); +var simple_string_serialized_2 = bsonC.serialize({doc:object_id2}, false, true); + +assert.equal(simple_string_serialized_2.length, simple_string_serialized.length); +assert.deepEqual(simple_string_serialized, simple_string_serialized_2) +var object = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); +var object2 = bsonC.deserialize(simple_string_serialized); +assert.equal(object.doc.id, object2.doc.id) + +// JS Object +var c1 = { _id: new ObjectID, comments: [], title: 'number 1' }; +var c2 = { _id: new ObjectID, comments: [], title: 'number 2' }; +var doc = { + numbers: [] + , owners: [] + , comments: [c1, c2] + , _id: new ObjectID +}; + +var simple_string_serialized = bsonJS.serialize(doc, false, true); + +// C++ Object +var c1 = { _id: ObjectID.createFromHexString(c1._id.toHexString()), comments: [], title: 'number 1' }; +var c2 = { _id: ObjectID.createFromHexString(c2._id.toHexString()), comments: [], title: 'number 2' }; +var doc = { + numbers: [] + , owners: [] + , comments: [c1, c2] + , _id: ObjectID.createFromHexString(doc._id.toHexString()) +}; + +var simple_string_serialized_2 = bsonC.serialize(doc, false, true); + +for(var i = 0; i < simple_string_serialized_2.length; i++) { + // debug(i + "[" + simple_string_serialized_2[i] + "] = [" + simple_string_serialized[i] + "]") + assert.equal(simple_string_serialized_2[i], simple_string_serialized[i]); +} + +// Deserialize the string +var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); +var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); +assert.equal(doc._id.id, doc1._id.id) +assert.equal(doc._id.id, doc2._id.id) +assert.equal(doc1._id.id, doc2._id.id) + +var doc = { + _id: 'testid', + key1: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 }, + key2: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 } +}; + +var simple_string_serialized = bsonJS.serialize(doc, false, true); +var simple_string_serialized_2 = bsonC.serialize(doc, false, true); + +// Deserialize the string +var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); +var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); +assert.deepEqual(doc2, doc1) +assert.deepEqual(doc, doc2) +assert.deepEqual(doc, doc1) + +// Serialize function +var doc = { + _id: 'testid', + key1: function() {} +} + +var simple_string_serialized = bsonJS.serialize(doc, false, true, true); +var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); + +// Deserialize the string +var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); +var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); +assert.equal(doc1.key1.code.toString(), doc2.key1.code.toString()) + +var doc = {"user_id":"4e9fc8d55883d90100000003","lc_status":{"$ne":"deleted"},"owner_rating":{"$exists":false}}; +var simple_string_serialized = bsonJS.serialize(doc, false, true, true); +var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); + +// Should serialize to the same value +assert.equal(simple_string_serialized_2.toString('base64'), simple_string_serialized.toString('base64')) +var doc1 = bsonJS.deserialize(simple_string_serialized_2); +var doc2 = bsonC.deserialize(simple_string_serialized); +assert.deepEqual(doc1, doc2) + +// Hex Id +var hexId = new ObjectID().toString(); +var docJS = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; +var docC = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; +var docJSBin = bsonJS.serialize(docJS, false, true, true); +var docCBin = bsonC.serialize(docC, false, true, true); +assert.equal(docCBin.toString('base64'), docJSBin.toString('base64')); + +// // Complex document serialization +// doc = {"DateTime": "Tue Nov 40 2011 17:27:55 GMT+0000 (WEST)","isActive": true,"Media": {"URL": "http://videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb"},"Title": "Lisboa fecha a ganhar 0.19%","SetPosition": 60,"Type": "videos","Thumbnail": [{"URL": "http://rd3.videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb/pic/320x240","Dimensions": {"Height": 240,"Width": 320}}],"Source": {"URL": "http://videos.sapo.pt","SetID": "1288","SourceID": "http://videos.sapo.pt/tvnet/rss2","SetURL": "http://noticias.sapo.pt/videos/tv-net_1288/","ItemID": "Tc85NsjaKjj8o5aV7Ubb","Name": "SAPO VÃdeos"},"Category": "Tec_ciencia","Description": "Lisboa fecha a ganhar 0.19%","GalleryID": new ObjectID("4eea2a634ce8573200000000"),"InternalRefs": {"RegisterDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","ChangeDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","Hash": 332279244514},"_id": new ObjectID("4eea2a96e52778160000003a")} +// var docJSBin = bsonJS.serialize(docJS, false, true, true); +// var docCBin = bsonC.serialize(docC, false, true, true); +// +// + +// // Force garbage collect +// global.gc(); + + + + + + + + + + + + + + + diff --git a/node_modules/mongodb/external-libs/bson/test/test_full_bson.js b/node_modules/mongodb/external-libs/bson/test/test_full_bson.js new file mode 100644 index 0000000..2a6506c --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/test/test_full_bson.js @@ -0,0 +1,218 @@ +var sys = require('util'), + fs = require('fs'), + Buffer = require('buffer').Buffer, + BSON = require('../bson').BSON, + Buffer = require('buffer').Buffer, + assert = require('assert'), + BinaryParser = require('../../../lib/mongodb/bson/binary_parser').BinaryParser, + BSONJS = require('../../../lib/mongodb/bson/bson').BSON, + Long = require('../../../lib/mongodb/bson/long').Long, + ObjectID = require('../../../lib/mongodb/bson/bson').ObjectID, + Binary = require('../../../lib/mongodb/bson/bson').Binary, + Code = require('../../../lib/mongodb/bson/bson').Code, + DBRef = require('../../../lib/mongodb/bson/bson').DBRef, + Symbol = require('../../../lib/mongodb/bson/bson').Symbol, + Double = require('../../../lib/mongodb/bson/bson').Double, + MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, + MinKey = require('../../../lib/mongodb/bson/bson').MinKey, + Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp; + +if(process.env['npm_package_config_native'] != null) return; + +sys.puts("=== EXECUTING TEST_FULL_BSON ==="); + +// Parsers +var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); +var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + +// Should Correctly Deserialize object +var bytes = [95,0,0,0,2,110,115,0,42,0,0,0,105,110,116,101,103,114,97,116,105,111,110,95,116,101,115,116,115,95,46,116,101,115,116,95,105,110,100,101,120,95,105,110,102,111,114,109,97,116,105,111,110,0,8,117,110,105,113,117,101,0,0,3,107,101,121,0,12,0,0,0,16,97,0,1,0,0,0,0,2,110,97,109,101,0,4,0,0,0,97,95,49,0,0]; +var serialized_data = ''; +// Convert to chars +for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); +} +var object = bsonC.deserialize(serialized_data); +assert.equal("a_1", object.name); +assert.equal(false, object.unique); +assert.equal(1, object.key.a); + +// Should Correctly Deserialize object with all types +var bytes = [26,1,0,0,7,95,105,100,0,161,190,98,75,118,169,3,0,0,3,0,0,4,97,114,114,97,121,0,26,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,0,2,115,116,114,105,110,103,0,6,0,0,0,104,101,108,108,111,0,3,104,97,115,104,0,19,0,0,0,16,97,0,1,0,0,0,16,98,0,2,0,0,0,0,9,100,97,116,101,0,161,190,98,75,0,0,0,0,7,111,105,100,0,161,190,98,75,90,217,18,0,0,1,0,0,5,98,105,110,97,114,121,0,7,0,0,0,2,3,0,0,0,49,50,51,16,105,110,116,0,42,0,0,0,1,102,108,111,97,116,0,223,224,11,147,169,170,64,64,11,114,101,103,101,120,112,0,102,111,111,98,97,114,0,105,0,8,98,111,111,108,101,97,110,0,1,15,119,104,101,114,101,0,25,0,0,0,12,0,0,0,116,104,105,115,46,120,32,61,61,32,51,0,5,0,0,0,0,3,100,98,114,101,102,0,37,0,0,0,2,36,114,101,102,0,5,0,0,0,116,101,115,116,0,7,36,105,100,0,161,190,98,75,2,180,1,0,0,2,0,0,0,10,110,117,108,108,0,0]; +var serialized_data = ''; +// Convert to chars +for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); +} + +var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); +assert.equal("hello", object.string); +assert.deepEqual([1, 2, 3], object.array); +assert.equal(1, object.hash.a); +assert.equal(2, object.hash.b); +assert.ok(object.date != null); +assert.ok(object.oid != null); +assert.ok(object.binary != null); +assert.equal(42, object.int); +assert.equal(33.3333, object.float); +assert.ok(object.regexp != null); +assert.equal(true, object.boolean); +assert.ok(object.where != null); +assert.ok(object.dbref != null); +assert.ok(object['null'] == null); + +// Should Serialize and Deserialze String +var test_string = {hello: 'world'} +var serialized_data = bsonC.serialize(test_string) +assert.deepEqual(test_string, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Integer +var test_number = {doc: 5} +var serialized_data = bsonC.serialize(test_number) +assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize null value +var test_null = {doc:null} +var serialized_data = bsonC.serialize(test_null) +var object = bsonC.deserialize(serialized_data); +assert.deepEqual(test_null, object); + +// Should Correctly Serialize and Deserialize undefined value +var test_undefined = {doc:undefined} +var serialized_data = bsonC.serialize(test_undefined) +var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); +assert.equal(null, object.doc) + +// Should Correctly Serialize and Deserialize Number +var test_number = {doc: 5.5} +var serialized_data = bsonC.serialize(test_number) +assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Integer +var test_int = {doc: 42} +var serialized_data = bsonC.serialize(test_int) +assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + +test_int = {doc: -5600} +serialized_data = bsonC.serialize(test_int) +assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + +test_int = {doc: 2147483647} +serialized_data = bsonC.serialize(test_int) +assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + +test_int = {doc: -2147483648} +serialized_data = bsonC.serialize(test_int) +assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Object +var doc = {doc: {age: 42, name: 'Spongebob', shoe_size: 9.5}} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Array +var doc = {doc: [1, 2, 'a', 'b']} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Array with added on functions +var doc = {doc: [1, 2, 'a', 'b']} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize A Boolean +var doc = {doc: true} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize a Date +var date = new Date() +//(2009, 11, 12, 12, 00, 30) +date.setUTCDate(12) +date.setUTCFullYear(2009) +date.setUTCMonth(11 - 1) +date.setUTCHours(12) +date.setUTCMinutes(0) +date.setUTCSeconds(30) +var doc = {doc: date} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + +// // Should Correctly Serialize and Deserialize Oid +var doc = {doc: new ObjectID()} +var serialized_data = bsonC.serialize(doc) +assert.deepEqual(doc.doc.toHexString(), bsonC.deserialize(serialized_data).doc.toHexString()) + +// Should Correctly encode Empty Hash +var test_code = {} +var serialized_data = bsonC.serialize(test_code) +assert.deepEqual(test_code, bsonC.deserialize(serialized_data)); + +// Should Correctly Serialize and Deserialize Ordered Hash +var doc = {doc: {b:1, a:2, c:3, d:4}} +var serialized_data = bsonC.serialize(doc) +var decoded_hash = bsonC.deserialize(serialized_data).doc +var keys = [] +for(name in decoded_hash) keys.push(name) +assert.deepEqual(['b', 'a', 'c', 'd'], keys) + +// Should Correctly Serialize and Deserialize Regular Expression +// Serialize the regular expression +var doc = {doc: /foobar/mi} +var serialized_data = bsonC.serialize(doc) +var doc2 = bsonC.deserialize(serialized_data); +assert.equal(doc.doc.toString(), doc2.doc.toString()) + +// Should Correctly Serialize and Deserialize a Binary object +var bin = new Binary() +var string = 'binstring' +for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)) +} +var doc = {doc: bin} +var serialized_data = bsonC.serialize(doc) +var deserialized_data = bsonC.deserialize(serialized_data); +assert.equal(doc.doc.value(), deserialized_data.doc.value()) + +// Should Correctly Serialize and Deserialize a big Binary object +var data = fs.readFileSync("../../test/gridstore/test_gs_weird_bug.png", 'binary'); +var bin = new Binary() +bin.write(data) +var doc = {doc: bin} +var serialized_data = bsonC.serialize(doc) +var deserialized_data = bsonC.deserialize(serialized_data); +assert.equal(doc.doc.value(), deserialized_data.doc.value()) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/node_modules/mongodb/external-libs/bson/test/test_stackless_bson.js b/node_modules/mongodb/external-libs/bson/test/test_stackless_bson.js new file mode 100644 index 0000000..f271cac --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/test/test_stackless_bson.js @@ -0,0 +1,132 @@ +var Buffer = require('buffer').Buffer, + BSON = require('../bson').BSON, + Buffer = require('buffer').Buffer, + BSONJS = require('../../../lib/mongodb/bson/bson').BSON, + BinaryParser = require('../../../lib/mongodb/bson/binary_parser').BinaryParser, + Long = require('../../../lib/mongodb/bson/long').Long, + ObjectID = require('../../../lib/mongodb/bson/bson').ObjectID, + Binary = require('../../../lib/mongodb/bson/bson').Binary, + Code = require('../../../lib/mongodb/bson/bson').Code, + DBRef = require('../../../lib/mongodb/bson/bson').DBRef, + Symbol = require('../../../lib/mongodb/bson/bson').Symbol, + Double = require('../../../lib/mongodb/bson/bson').Double, + MaxKey = require('../../../lib/mongodb/bson/bson').MaxKey, + MinKey = require('../../../lib/mongodb/bson/bson').MinKey, + Timestamp = require('../../../lib/mongodb/bson/bson').Timestamp; + assert = require('assert'); + +if(process.env['npm_package_config_native'] != null) return; + +console.log("=== EXECUTING TEST_STACKLESS_BSON ==="); + +// Parsers +var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); +var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + +// Number of iterations for the benchmark +var COUNT = 10000; +// var COUNT = 1; +// Sample simple doc +var doc = {key:"Hello world", key2:"šđžčćŠĐŽČĆ", key3:'客家话', key4:'how are you doing dog!!'}; +// var doc = {}; +// for(var i = 0; i < 100; i++) { +// doc['string' + i] = "dumdyms fsdfdsfdsfdsfsdfdsfsdfsdfsdfsdfsdfsdfsdffsfsdfs"; +// } + +// // Calculate size +console.log(bsonC.calculateObjectSize2(doc)); +console.log(bsonJS.calculateObjectSize(doc)); +// assert.equal(bsonJS.calculateObjectSize(doc), bsonC.calculateObjectSize2(doc)); + +// ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- +// Benchmark calculateObjectSize +// ---------------------------------------------------------------------------- +// ---------------------------------------------------------------------------- + +// Benchmark 1 JS BSON +console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize(object))") +start = new Date + +for (j=COUNT; --j>=0; ) { + var objectBSON = bsonJS.calculateObjectSize(doc); +} + +end = new Date +var opsprsecond = COUNT / ((end - start)/1000); +console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); + +// Benchmark 2 C++ BSON calculateObjectSize +console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize(object))") +start = new Date + +for (j=COUNT; --j>=0; ) { + var objectBSON = bsonC.calculateObjectSize(doc); +} + +end = new Date +var opsprsecond = COUNT / ((end - start)/1000); +console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); + +// Benchmark 3 C++ BSON calculateObjectSize2 +console.log(COUNT + "x (objectBSON = bsonC.calculateObjectSize2(object))") +start = new Date + +for (j=COUNT; --j>=0; ) { + var objectBSON = bsonC.calculateObjectSize2(doc); +} + +end = new Date +var opsprsecond = COUNT / ((end - start)/1000); +console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); + +// // Serialize the content +// var _serializedDoc1 = bsonJS.serialize(doc, true, false); +// var _serializedDoc2 = bsonC.serialize2(doc, true, false); +// console.dir(_serializedDoc1); +// console.dir(_serializedDoc2); +// assert.equal(_serializedDoc1.toString('base64'), _serializedDoc2.toString('base64')) +// +// +// // Benchmark 1 +// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") +// start = new Date +// +// for (j=COUNT; --j>=0; ) { +// // var objectBSON = bsonC.serialize2(doc, true, false); +// var objectBSON = bsonJS.serialize(doc, true, false); +// } +// +// end = new Date +// var opsprsecond = COUNT / ((end - start)/1000); +// console.log("bson size (bytes): ", objectbsonC.length); +// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); +// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); +// +// // Benchmark 2 +// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") +// start = new Date +// +// for (j=COUNT; --j>=0; ) { +// var objectBSON = bsonC.serialize2(doc, true, false); +// } +// +// end = new Date +// var opsprsecond = COUNT / ((end - start)/1000); +// console.log("bson size (bytes): ", objectbsonC.length); +// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); +// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); +// +// // Benchmark 3 +// console.log(COUNT + "x (objectBSON = bsonC.serialize(object))") +// start = new Date +// +// for (j=COUNT; --j>=0; ) { +// var objectBSON = bsonC.serialize(doc, true, false); +// } +// +// end = new Date +// var opsprsecond = COUNT / ((end - start)/1000); +// console.log("bson size (bytes): ", objectbsonC.length); +// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec"); +// console.log("MB/s = " + ((opsprsecond*objectbsonC.length)/1024)); diff --git a/node_modules/mongodb/external-libs/bson/wscript b/node_modules/mongodb/external-libs/bson/wscript new file mode 100644 index 0000000..40f5317 --- /dev/null +++ b/node_modules/mongodb/external-libs/bson/wscript @@ -0,0 +1,39 @@ +import Options +from os import unlink, symlink, popen +from os.path import exists + +srcdir = "." +blddir = "build" +VERSION = "0.1.0" + +def set_options(opt): + opt.tool_options("compiler_cxx") + opt.add_option( '--debug' + , action='store_true' + , default=False + , help='Build debug variant [Default: False]' + , dest='debug' + ) + +def configure(conf): + conf.check_tool("compiler_cxx") + conf.check_tool("node_addon") + conf.env.append_value('CXXFLAGS', ['-O3', '-funroll-loops']) + + # conf.env.append_value('CXXFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra']) + # conf.check(lib='node', libpath=['/usr/lib', '/usr/local/lib'], uselib_store='NODE') + +def build(bld): + obj = bld.new_task_gen("cxx", "shlib", "node_addon") + obj.target = "bson" + obj.source = ["bson.cc"] + # obj.uselib = "NODE" + +def shutdown(): + # HACK to get compress.node out of build directory. + # better way to do this? + if Options.commands['clean']: + if exists('bson.node'): unlink('bson.node') + else: + if exists('build/default/bson.node') and not exists('bson.node'): + symlink('build/default/bson.node', 'bson.node') diff --git a/node_modules/mongodb/index.js b/node_modules/mongodb/index.js new file mode 100755 index 0000000..4f59e9d --- /dev/null +++ b/node_modules/mongodb/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/mongodb'); diff --git a/node_modules/mongodb/install.js b/node_modules/mongodb/install.js new file mode 100644 index 0000000..f9f2a57 --- /dev/null +++ b/node_modules/mongodb/install.js @@ -0,0 +1,40 @@ +var spawn = require('child_process').spawn, + exec = require('child_process').exec; + +process.stdout.write("================================================================================\n"); +process.stdout.write("= =\n"); +process.stdout.write("= To install with C++ bson parser do <npm install mongodb --mongodb:native> =\n"); +process.stdout.write("= =\n"); +process.stdout.write("================================================================================\n"); + +// Check if we want to build the native code +var build_native = process.env['npm_package_config_native'] != null ? process.env['npm_package_config_native'] : 'false'; +build_native = build_native == 'true' ? true : false; +// If we are building the native bson extension ensure we use gmake if available +if(build_native) { + // Check if we need to use gmake + exec('which gmake', function(err, stdout, stderr) { + // Set up spawn command + var make = null; + // No gmake build using make + if(err != null) { + make = spawn('make', ['total']); + } else { + make = spawn('gmake', ['total']); + } + + // Execute spawn + make.stdout.on('data', function(data) { + process.stdout.write(data); + }) + + make.stderr.on('data', function(data) { + process.stdout.write(data); + }) + + make.on('exit', function(code) { + process.stdout.write('child process exited with code ' + code + "\n"); + }) + }); +} + diff --git a/node_modules/mongodb/lib/mongodb/admin.js b/node_modules/mongodb/lib/mongodb/admin.js new file mode 100644 index 0000000..cb008a3 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/admin.js @@ -0,0 +1,390 @@ +/*! + * Module dependencies. + */ +var Collection = require('./collection').Collection, + Cursor = require('./cursor').Cursor, + DbCommand = require('./commands/db_command').DbCommand; + +/** + * Allows the user to access the admin functionality of MongoDB + * + * @class Represents the Admin methods of MongoDB. + * @param {Object} db Current db instance we wish to perform Admin operations on. + * @return {Function} Constructor for Admin type. + */ +function Admin(db) { + if(!(this instanceof Admin)) return new Admin(db); + + this.db = db; +}; + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.buildInfo = function(callback) { + this.serverInfo(callback); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api private + */ +Admin.prototype.serverInfo = function(callback) { + var self = this; + var command = {buildinfo:1}; + this.command(command, function(err, doc) { + if(err != null) return callback(err, null); + return callback(null, doc.documents[0]); + }); +} + +/** + * Retrieve this db's server status. + * + * @param {Function} callback returns the server status. + * @return {null} + * @api public + */ +Admin.prototype.serverStatus = function(callback) { + var self = this; + + this.command({serverStatus: 1}, function(err, result) { + if (err == null && result.documents[0].ok == 1) { + callback(null, result.documents[0]); + } else { + if (err) { + callback(err, false); + } else { + callback(self.wrap(result.documents[0]), false); + } + } + }); +}; + +/** + * Retrieve the current profiling Level for MongoDB + * + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.profilingLevel = function(callback) { + var self = this; + var command = {profile:-1}; + + this.command(command, function(err, doc) { + doc = doc.documents[0]; + + if(err == null && (doc.ok == 1 || typeof doc.was === 'number')) { + var was = doc.was; + if(was == 0) { + callback(null, "off"); + } else if(was == 1) { + callback(null, "slow_only"); + } else if(was == 2) { + callback(null, "all"); + } else { + callback(new Error("Error: illegal profiling level value " + was), null); + } + } else { + err != null ? callback(err, null) : callback(new Error("Error with profile command"), null); + } + }); +}; + +/** + * Ping the MongoDB server and retrieve results + * + * @param {Object} [options] Optional parameters to the command. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.ping = function(options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + options = args.length ? args.shift() : {}; + // Set self + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + this.db.executeDbCommand({ping:1}, options, function(err, result) { + self.db.databaseName = databaseName; + return callback(err, result); + }) +} + +/** + * Authenticate against MongoDB + * + * @param {String} username The user name for the authentication. + * @param {String} password The password for the authentication. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.authenticate = function(username, password, callback) { + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + this.db.authenticate(username, password, function(err, result) { + self.db.databaseName = databaseName; + return callback(err, result); + }) +} + +/** + * Logout current authenticated user + * + * @param {Object} [options] Optional parameters to the command. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.logout = function(callback) { + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + this.db.logout(function(err, result) { + return callback(err, result); + }) + + self.db.databaseName = databaseName; +} + +/** + * Add a user to the MongoDB server, if the user exists it will + * overwrite the current password + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {String} username The user name for the authentication. + * @param {String} password The password for the authentication. + * @param {Object} [options] additional options during update. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.addUser = function(username, password, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + this.db.addUser(username, password, options, function(err, result) { + self.db.databaseName = databaseName; + return callback(err, result); + }) +} + +/** + * Remove a user from the MongoDB server + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {String} username The user name for the authentication. + * @param {Object} [options] additional options during update. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.removeUser = function(username, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + this.db.removeUser(username, options, function(err, result) { + self.db.databaseName = databaseName; + return callback(err, result); + }) +} + +/** + * Set the current profiling level of MongoDB + * + * @param {String} level The new profiling level (off, slow_only, all) + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.setProfilingLevel = function(level, callback) { + var self = this; + var command = {}; + var profile = 0; + + if(level == "off") { + profile = 0; + } else if(level == "slow_only") { + profile = 1; + } else if(level == "all") { + profile = 2; + } else { + return callback(new Error("Error: illegal profiling level value " + level)); + } + + // Set up the profile number + command['profile'] = profile; + // Execute the command to set the profiling level + this.command(command, function(err, doc) { + doc = doc.documents[0]; + + if(err == null && (doc.ok == 1 || typeof doc.was === 'number')) { + return callback(null, level); + } else { + return err != null ? callback(err, null) : callback(new Error("Error with profile command"), null); + } + }); +}; + +/** + * Retrive the current profiling information for MongoDB + * + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.profilingInfo = function(callback) { + var self = this; + var databaseName = this.db.databaseName; + this.db.databaseName = 'admin'; + + try { + new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}).toArray(function(err, items) { + return callback(err, items); + }); + } catch (err) { + return callback(err, null); + } + + self.db.databaseName = databaseName; +}; + +/** + * Execute a db command against the Admin database + * + * @param {Object} command A command object `{ping:1}`. + * @param {Object} [options] Optional parameters to the command. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.command = function(command, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + // Execute a command + this.db.executeDbAdminCommand(command, options, function(err, result) { + // Ensure change before event loop executes + return callback != null ? callback(err, result) : null; + }); +} + +/** + * Validate an existing collection + * + * @param {String} collectionName The name of the collection to validate. + * @param {Object} [options] Optional parameters to the command. + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.validateCollection = function(collectionName, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + var self = this; + var command = {validate: collectionName}; + var keys = Object.keys(options); + + // Decorate command with extra options + for(var i = 0; i < keys.length; i++) { + if(options.hasOwnProperty(keys[i])) { + command[keys[i]] = options[keys[i]]; + } + } + + this.db.executeDbCommand(command, function(err, doc) { + if(err != null) return callback(err, null); + doc = doc.documents[0]; + + if(doc.ok == 0) { + return callback(new Error("Error with validate command"), null); + } else if(doc.result != null && doc.result.constructor != String) { + return callback(new Error("Error with validation data"), null); + } else if(doc.result != null && doc.result.match(/exception|corrupt/) != null) { + return callback(new Error("Error: invalid collection " + collectionName), null); + } else if(doc.valid != null && !doc.valid) { + return callback(new Error("Error: invalid collection " + collectionName), null); + } else { + return callback(null, doc); + } + }); +}; + +/** + * List the available databases + * + * @param {Function} callback Callback function of format `function(err, result) {}`. + * @return {null} Returns no result + * @api public + */ +Admin.prototype.listDatabases = function(callback) { + // Execute the listAllDatabases command + this.db.executeDbAdminCommand({listDatabases:1}, {}, function(err, result) { + if(err != null) { + callback(err, null); + } else { + callback(null, result.documents[0]); + } + }); +} + +/** + * Get ReplicaSet status + * + * @param {Function} callback returns the replica set status (if available). + * @return {null} + * @api public + */ +Admin.prototype.replSetGetStatus = function(callback) { + var self = this; + + this.command({replSetGetStatus:1}, function(err, result) { + if (err == null && result.documents[0].ok == 1) { + callback(null, result.documents[0]); + } else { + if (err) { + callback(err, false); + } else { + callback(self.db.wrap(result.documents[0]), false); + } + } + }); +}; + +/** + * @ignore + */ +exports.Admin = Admin; diff --git a/node_modules/mongodb/lib/mongodb/collection.js b/node_modules/mongodb/lib/mongodb/collection.js new file mode 100644 index 0000000..1c57c5b --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/collection.js @@ -0,0 +1,1504 @@ +/** + * Module dependencies. + * @ignore + */ +var InsertCommand = require('./commands/insert_command').InsertCommand + , QueryCommand = require('./commands/query_command').QueryCommand + , DeleteCommand = require('./commands/delete_command').DeleteCommand + , UpdateCommand = require('./commands/update_command').UpdateCommand + , DbCommand = require('./commands/db_command').DbCommand + , ObjectID = require('bson').ObjectID + , Code = require('bson').Code + , Cursor = require('./cursor').Cursor + , utils = require('./utils'); + +/** + * Precompiled regexes + * @ignore +**/ +const eErrorMessages = /No matching object found/; + +/** + * toString helper. + * @ignore + */ +var toString = Object.prototype.toString; + +/** + * Create a new Collection instance + * + * Options + * - **slaveOk** {Boolean, default:false}, Allow reads from secondaries. + * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document. + * - **raw** {Boolean, default:false}, perform all operations using raw bson objects. + * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation. + * + * @class Represents a Collection + * @param {Object} db db instance. + * @param {String} collectionName collection name. + * @param {Object} [pkFactory] alternative primary key factory. + * @param {Object} [options] additional options for the collection. + * @return {Object} a collection instance. + */ +function Collection (db, collectionName, pkFactory, options) { + if(!(this instanceof Collection)) return new Collection(db, collectionName, pkFactory, options); + + checkCollectionName(collectionName); + + this.db = db; + this.collectionName = collectionName; + this.internalHint; + this.opts = options != null && ('object' === typeof options) ? options : {}; + this.slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk; + this.serializeFunctions = options == null || options.serializeFunctions == null ? db.serializeFunctions : options.serializeFunctions; + this.raw = options == null || options.raw == null ? db.raw : options.raw; + this.pkFactory = pkFactory == null + ? ObjectID + : pkFactory; + + var self = this + Object.defineProperty(this, "hint", { + enumerable: true + , get: function () { + return this.internalHint; + } + , set: function (v) { + this.internalHint = normalizeHintField(v); + } + }); +}; + +/** + * Inserts a single document or a an array of documents into MongoDB. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **keepGoing** {Boolean, default:false}, keep inserting documents even if one document has an error, *mongodb 1.9.1 >*. + * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document. + * + * @param {Array|Object} docs + * @param {Object} [options] optional options for insert command + * @param {Function} [callback] optional callback for the function, must be provided when using `safe` or `strict` mode + * @return {null} + * @api public + */ +Collection.prototype.insert = function insert (docs, options, callback) { + if ('function' === typeof options) callback = options, options = {}; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + var self = this; + insertAll(self, Array.isArray(docs) ? docs : [docs], options, callback); + return this; +}; + +/** + * @ignore + */ +var checkCollectionName = function checkCollectionName (collectionName) { + if ('string' !== typeof collectionName) { + throw Error("collection name must be a String"); + } + + if (!collectionName || collectionName.indexOf('..') != -1) { + throw Error("collection names cannot be empty"); + } + + if (collectionName.indexOf('$') != -1 && + collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) { + throw Error("collection names must not contain '$'"); + } + + if (collectionName.match(/^\.|\.$/) != null) { + throw Error("collection names must not start or end with '.'"); + } +}; + +/** + * Removes documents specified by `selector` from the db. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {Object} [selector] optional select, no selector is equivalent to removing all documents. + * @param {Object} [options] additional options during remove. + * @param {Function} [callback] must be provided if you performing a safe remove + * @return {null} + * @api public + */ +Collection.prototype.remove = function remove(selector, options, callback) { + if ('function' === typeof selector) { + callback = selector; + selector = options = {}; + } else if ('function' === typeof options) { + callback = options; + options = {}; + } + + // Ensure options + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + // Ensure we have at least an empty selector + selector = selector == null ? {} : selector; + + var deleteCommand = new DeleteCommand( + this.db + , this.db.databaseName + "." + this.collectionName + , selector); + + var self = this; + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + errorOptions = errorOptions == null && this.db.strict != null ? this.db.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions && errorOptions['safe'] != false && typeof callback !== 'function') throw new Error("safe cannot be used without a callback"); + // Execute the command, do not add a callback as it's async + if (options && options.safe || this.opts.safe != null || this.db.strict) { + // Insert options + var commandOptions = {read:false}; + // If we have safe set set async to false + if(errorOptions == null) commandOptions['async'] = true; + // Set safe option + commandOptions['safe'] = true; + // If we have an error option + if(typeof errorOptions == 'object') { + var keys = Object.keys(errorOptions); + for(var i = 0; i < keys.length; i++) { + commandOptions[keys[i]] = errorOptions[keys[i]]; + } + } + + // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection) + this.db._executeRemoveCommand(deleteCommand, commandOptions, function (err, error) { + error = error && error.documents; + if(!callback) return; + + if(err) { + callback(err); + } else if(error[0].err || error[0].errmsg) { + callback(self.db.wrap(error[0])); + } else { + callback(null, error[0].n); + } + }); + } else { + var result = this.db._executeRemoveCommand(deleteCommand); + // If no callback just return + if (!callback) return; + // If error return error + if (result instanceof Error) { + return callback(result); + } + // Otherwise just return + return callback(); + } +}; + +/** + * Renames the collection. + * + * @param {String} newName the new name of the collection. + * @param {Function} callback the callback accepting the result + * @return {null} + * @api public + */ +Collection.prototype.rename = function rename (newName, callback) { + var self = this; + // Ensure the new name is valid + checkCollectionName(newName); + // Execute the command, return the new renamed collection if successful + self.db._executeQueryCommand(DbCommand.createRenameCollectionCommand(self.db, self.collectionName, newName), function(err, result) { + if(err == null && result.documents[0].ok == 1) { + if(callback != null) { + // Set current object to point to the new name + self.collectionName = newName; + // Return the current collection + callback(null, self); + } + } else if(result.documents[0].errmsg != null) { + if(callback != null) { + err != null ? callback(err, null) : callback(self.db.wrap(result.documents[0]), null); + } + } + }); +}; + +/** + * @ignore + */ +var insertAll = function insertAll (self, docs, options, callback) { + if('function' === typeof options) callback = options, options = {}; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + + // Insert options (flags for insert) + var insertFlags = {}; + // If we have a mongodb version >= 1.9.1 support keepGoing attribute + if(options['keepGoing'] != null) { + insertFlags['keepGoing'] = options['keepGoing']; + } + + // Either use override on the function, or go back to default on either the collection + // level or db + if(options['serializeFunctions'] != null) { + insertFlags['serializeFunctions'] = options['serializeFunctions']; + } else { + insertFlags['serializeFunctions'] = self.serializeFunctions; + } + + // Pass in options + var insertCommand = new InsertCommand( + self.db + , self.db.databaseName + "." + self.collectionName, true, insertFlags); + + // Add the documents and decorate them with id's if they have none + for (var index = 0, len = docs.length; index < len; ++index) { + var doc = docs[index]; + + // Add id to each document if it's not already defined + if (!(Buffer.isBuffer(doc)) && doc['_id'] == null && self.db.forceServerObjectId != true) { + doc['_id'] = self.pkFactory.createPk(); + } + + insertCommand.add(doc); + } + + // Collect errorOptions + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && self.opts.safe != null ? self.opts.safe : errorOptions; + errorOptions = errorOptions == null && self.db.strict != null ? self.db.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions && errorOptions['safe'] != false && typeof callback !== 'function') throw new Error("safe cannot be used without a callback"); + + // Default command options + var commandOptions = {}; + // If safe is defined check for error message + if(errorOptions && errorOptions != false) { + // Insert options + commandOptions['read'] = false; + // If we have safe set set async to false + if(errorOptions == null) commandOptions['async'] = true; + + // Set safe option + commandOptions['safe'] = errorOptions; + // If we have an error option + if(typeof errorOptions == 'object') { + var keys = Object.keys(errorOptions); + for(var i = 0; i < keys.length; i++) { + commandOptions[keys[i]] = errorOptions[keys[i]]; + } + } + + // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection) + self.db._executeInsertCommand(insertCommand, commandOptions, function (err, error) { + error = error && error.documents; + if(!callback) return; + + if (err) { + callback(err); + } else if(error[0].err || error[0].errmsg) { + callback(self.db.wrap(error[0])); + } else { + callback(null, docs); + } + }); + } else { + var result = self.db._executeInsertCommand(insertCommand, commandOptions); + // If no callback just return + if(!callback) return; + // If error return error + if(result instanceof Error) { + return callback(result); + } + // Otherwise just return + return callback(null, docs); + } +}; + +/** + * Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic + * operators and update instead for more efficient operations. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {Object} [doc] the document to save + * @param {Object} [options] additional options during remove. + * @param {Function} [callback] must be provided if you performing a safe save + * @return {null} + * @api public + */ +Collection.prototype.save = function save(doc, options, callback) { + if('function' === typeof options) callback = options, options = null; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + + var errorOptions = options.safe != null ? options.safe : false; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + // Extract the id, if we have one we need to do a update command + var id = doc['_id']; + + if(id) { + this.update({ _id: id }, doc, { upsert: true, safe: errorOptions }, callback); + } else { + this.insert(doc, { safe: errorOptions }, callback && function (err, docs) { + if (err) return callback(err, null); + + if (Array.isArray(docs)) { + callback(err, docs[0]); + } else { + callback(err, docs); + } + }); + } +}; + +/** + * Updates documents. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **upsert** {Boolean, default:false}, perform an upsert operation. + * - **multi** {Boolean, default:false}, update all documents matching the selector. + * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document. + * + * @param {Object} selector the query to select the document/documents to be updated + * @param {Object} document the fields/vals to be updated, or in the case of an upsert operation, inserted. + * @param {Object} [options] additional options during update. + * @param {Function} [callback] must be provided if you performing a safe update + * @return {null} + * @api public + */ +Collection.prototype.update = function update(selector, document, options, callback) { + if('function' === typeof options) callback = options, options = null; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + + // Either use override on the function, or go back to default on either the collection + // level or db + if(options['serializeFunctions'] != null) { + options['serializeFunctions'] = options['serializeFunctions']; + } else { + options['serializeFunctions'] = this.serializeFunctions; + } + + var updateCommand = new UpdateCommand( + this.db + , this.db.databaseName + "." + this.collectionName + , selector + , document + , options); + + var self = this; + // Unpack the error options if any + var errorOptions = (options && options.safe != null) ? options.safe : null; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + errorOptions = errorOptions == null && this.db.strict != null ? this.db.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions && errorOptions['safe'] != false && typeof callback !== 'function') throw new Error("safe cannot be used without a callback"); + + // If we are executing in strict mode or safe both the update and the safe command must happen on the same line + if(errorOptions && errorOptions != false) { + // Insert options + var commandOptions = {read:false}; + // If we have safe set set async to false + if(errorOptions == null) commandOptions['async'] = true; + // Set safe option + commandOptions['safe'] = true; + // If we have an error option + if(typeof errorOptions == 'object') { + var keys = Object.keys(errorOptions); + for(var i = 0; i < keys.length; i++) { + commandOptions[keys[i]] = errorOptions[keys[i]]; + } + } + + // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection) + this.db._executeUpdateCommand(updateCommand, commandOptions, function (err, error) { + error = error && error.documents; + if(!callback) return; + + if(err) { + callback(err); + } else if(error[0].err || error[0].errmsg) { + callback(self.db.wrap(error[0])); + } else { + callback(null, error[0].n); + } + }); + } else { + // Execute update + var result = this.db._executeUpdateCommand(updateCommand); + // If no callback just return + if (!callback) return; + // If error return error + if (result instanceof Error) { + return callback(result); + } + // Otherwise just return + return callback(); + } +}; + +/** + * The distinct command returns returns a list of distinct values for the given key across a collection. + * + * @param {String} key key to run distinct against. + * @param {Object} [query] option query to narrow the returned objects. + * @param {Function} callback must be provided. + * @return {null} + * @api public + */ +Collection.prototype.distinct = function distinct(key, query, callback) { + if ('function' === typeof query) callback = query, query = {}; + + var mapCommandHash = { + distinct: this.collectionName + , query: query + , key: key + }; + + var cmd = DbCommand.createDbSlaveOkCommand(this.db, mapCommandHash); + + this.db._executeQueryCommand(cmd, {read:true}, function (err, result) { + if (err) { + return callback(err); + } + + if (result.documents[0].ok != 1) { + return callback(new Error(result.documents[0].errmsg)); + } + + callback(null, result.documents[0].values); + }); +}; + +/** + * Count number of matching documents in the db to a query. + * + * @param {Object} [query] query to filter by before performing count. + * @param {Function} callback must be provided. + * @return {null} + * @api public + */ +Collection.prototype.count = function count (query, callback) { + if ('function' === typeof query) callback = query, query = {}; + + var final_query = { + count: this.collectionName + , query: query + , fields: null + }; + + var queryOptions = QueryCommand.OPTS_NO_CURSOR_TIMEOUT; + if (this.slaveOk || this.db.slaveOk) { + queryOptions |= QueryCommand.OPTS_SLAVE; + } + + var queryCommand = new QueryCommand( + this.db + , this.db.databaseName + ".$cmd" + , queryOptions + , 0 + , -1 + , final_query + , null + ); + + var self = this; + this.db._executeQueryCommand(queryCommand, {read:true}, function (err, result) { + result = result && result.documents; + if(!callback) return; + + if (err) { + callback(err); + } else if (result[0].ok != 1 || result[0].errmsg) { + callback(self.db.wrap(result[0])); + } else { + callback(null, result[0].n); + } + }); +}; + + +/** + * Drop the collection + * + * @param {Function} [callback] provide a callback to be notified when command finished executing + * @return {null} + * @api public + */ +Collection.prototype.drop = function drop(callback) { + this.db.dropCollection(this.collectionName, callback); +}; + +/** + * Find and update a document. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **remove** {Boolean, default:false}, set to true to remove the object before returning. + * - **upsert** {Boolean, default:false}, perform an upsert operation. + * - **new** {Boolean, default:false}, set to true if you want to return the modified object rather than the original. Ignored for remove. + * + * @param {Object} query query object to locate the object to modify + * @param {Array} sort - if multiple docs match, choose the first one in the specified sort order as the object to manipulate + * @param {Object} doc - the fields/vals to be updated + * @param {Object} [options] additional options during update. + * @param {Function} [callback] returns results. + * @return {null} + * @api public + */ +Collection.prototype.findAndModify = function findAndModify (query, sort, doc, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + sort = args.length ? args.shift() : []; + doc = args.length ? args.shift() : null; + options = args.length ? args.shift() : {}; + var self = this; + + var queryObject = { + 'findandmodify': this.collectionName + , 'query': query + , 'sort': utils.formattedOrderClause(sort) + }; + + queryObject.new = options.new ? 1 : 0; + queryObject.remove = options.remove ? 1 : 0; + queryObject.upsert = options.upsert ? 1 : 0; + + if (options.fields) { + queryObject.fields = options.fields; + } + + if (doc && !options.remove) { + queryObject.update = doc; + } + + // Either use override on the function, or go back to default on either the collection + // level or db + if(options['serializeFunctions'] != null) { + options['serializeFunctions'] = options['serializeFunctions']; + } else { + options['serializeFunctions'] = this.serializeFunctions; + } + + // Unpack the error options if any + var errorOptions = (options && options.safe != null) ? options.safe : null; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + errorOptions = errorOptions == null && this.db.strict != null ? this.db.strict : errorOptions; + + // Commands to send + var commands = []; + // Add the find and modify command + commands.push(DbCommand.createDbSlaveOkCommand(this.db, queryObject, options)); + // If we have safe defined we need to return both call results + var chainedCommands = errorOptions != null ? true : false; + // Add error command if we have one + if(chainedCommands) { + commands.push(DbCommand.createGetLastErrorCommand(errorOptions, this.db)); + } + + // Fire commands and + this.db._executeQueryCommand(commands, function(err, result) { + result = result && result.documents; + + if(err != null) { + callback(err); + } else if(result[0].err != null) { + callback(self.db.wrap(result[0]), null); + } else if(result[0].errmsg != null && !result[0].errmsg.match(eErrorMessages)) { + // Workaround due to 1.8.X returning an error on no matching object + // while 2.0.X does not not, making 2.0.X behaviour standard + callback(self.db.wrap(result[0]), null); + } else { + return callback(null, result[0].value); + } + }); +} + +/** + * Find and remove a document + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {Object} query query object to locate the object to modify + * @param {Array} sort - if multiple docs match, choose the first one in the specified sort order as the object to manipulate + * @param {Object} [options] additional options during update. + * @param {Function} [callback] returns results. + * @return {null} + * @api public + */ +Collection.prototype.findAndRemove = function(query, sort, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + sort = args.length ? args.shift() : []; + options = args.length ? args.shift() : {}; + // Add the remove option + options['remove'] = true; + // Execute the callback + this.findAndModify(query, sort, null, options, callback); +} + +var testForFields = {'limit' : 1, 'sort' : 1, 'fields' : 1, 'skip' : 1, 'hint' : 1, 'explain' : 1, 'snapshot' : 1 + , 'timeout' : 1, 'tailable' : 1, 'batchSize' : 1, 'raw' : 1, 'read' : 1 + , 'returnKey' : 1, 'maxScan' : 1, 'min' : 1, 'max' : 1, 'showDiskLoc' : 1, 'comment' : 1}; + +/** + * Creates a cursor for a query that can be used to iterate over results from MongoDB + * + * Various argument possibilities + * - callback? + * - selector, callback?, + * - selector, fields, callback? + * - selector, options, callback? + * - selector, fields, options, callback? + * - selector, fields, skip, limit, callback? + * - selector, fields, skip, limit, timeout, callback? + * + * Options + * - **limit** {Number, default:0}, sets the limit of documents returned in the query. + * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc. + * - **fields** {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {'a':1} + * - **skip** {Number, default:0}, set to skip N documents ahead in your query (useful for pagination). + * - **hint** {Object}, tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1} + * - **explain** {Boolean, default:false}, explain the query instead of returning the data. + * - **snapshot** {Boolean, default:false}, snapshot query. + * - **timeout** {Boolean, default:false}, specify if the cursor can timeout. + * - **tailable** {Boolean, default:false}, specify if the cursor is tailable. + * - **batchSize** {Number, default:0}, set the batchSize for the getMoreCommand when iterating over the query results. + * - **returnKey** {Boolean, default:false}, only return the index key. + * - **maxScan** {Number}, Limit the number of items to scan. + * - **min** {Number}, Set index bounds. + * - **max** {Number}, Set index bounds. + * - **showDiskLoc** {Boolean, default:false}, Show disk location of results. + * - **comment** {String}, You can put a $comment field on a query to make looking in the profiler logs simpler. + * - **raw** {Boolean, default:false}, Return all BSON documents as Raw Buffer documents. + * - **read** {Boolean, default:false}, Tell the query to read from a secondary server. + * + * @param {Object} query query object to locate the object to modify + * @param {Object} [options] additional options during update. + * @param {Function} [callback] optional callback for cursor. + * @return {Cursor} returns a cursor to the query + * @api public + */ +Collection.prototype.find = function find () { + var options + , args = Array.prototype.slice.call(arguments, 0) + , has_callback = typeof args[args.length - 1] === 'function' + , has_weird_callback = typeof args[0] === 'function' + , callback = has_callback ? args.pop() : (has_weird_callback ? args.shift() : null) + , len = args.length + , selector = len >= 1 ? args[0] : {} + , fields = len >= 2 ? args[1] : undefined; + + if(len === 1 && has_weird_callback) { + // backwards compat for callback?, options case + selector = {}; + options = args[0]; + } + + if(len === 2 && !Array.isArray(fields)) { + var fieldKeys = Object.getOwnPropertyNames(fields); + var is_option = false; + + for(var i = 0; i < fieldKeys.length; i++) { + if(testForFields[fieldKeys[i]] != null) { + is_option = true; + break; + } + } + + if(is_option) { + options = fields; + fields = undefined; + } else { + options = {}; + } + } else if(len === 2 && Array.isArray(fields) && !Array.isArray(fields[0])) { + var newFields = {}; + // Rewrite the array + for(var i = 0; i < fields.length; i++) { + newFields[fields[i]] = 1; + } + // Set the fields + fields = newFields; + } + + if(3 === len) { + options = args[2]; + } + + // Ensure selector is not null + selector = selector == null ? {} : selector; + // Validate correctness off the selector + var object = selector; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + // Validate correctness of the field selector + var object = fields; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + // Check special case where we are using an objectId + if(selector instanceof ObjectID) { + selector = {_id:selector}; + } + + // If it's a serialized fields field we need to just let it through + // user be warned it better be good + if(options && options.fields && !(Buffer.isBuffer(options.fields))) { + fields = {}; + + if(Array.isArray(options.fields)) { + if(!options.fields.length) { + fields['_id'] = 1; + } else { + for (var i = 0, l = options.fields.length; i < l; i++) { + fields[options.fields[i]] = 1; + } + } + } else { + fields = options.fields; + } + } + + if (!options) options = {}; + options.skip = len > 3 ? args[2] : options.skip ? options.skip : 0; + options.limit = len > 3 ? args[3] : options.limit ? options.limit : 0; + options.raw = options.raw != null && typeof options.raw === 'boolean' ? options.raw : this.raw; + options.hint = options.hint != null ? normalizeHintField(options.hint) : this.internalHint; + options.timeout = len == 5 ? args[4] : typeof options.timeout === 'undefined' ? undefined : options.timeout; + // If we have overridden slaveOk otherwise use the default db setting + options.slaveOk = options.slaveOk != null ? options.slaveOk : this.db.slaveOk; + var o = options; + + // callback for backward compatibility + if(callback) { + // TODO refactor Cursor args + callback(null, new Cursor(this.db, this, selector, fields, o.skip, o.limit + , o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize + , o.slaveOk, o.raw, o.read, o.returnKey, o.maxScan, o.min, o.max, o.showDiskLoc, o.comment)); + } else { + return new Cursor(this.db, this, selector, fields, o.skip, o.limit + , o.sort, o.hint, o.explain, o.snapshot, o.timeout, o.tailable, o.batchSize + , o.slaveOk, o.raw, o.read, o.returnKey, o.maxScan, o.min, o.max, o.showDiskLoc, o.comment); + } +}; + +/** + * Normalizes a `hint` argument. + * + * @param {String|Object|Array} hint + * @return {Object} + * @api private + */ +var normalizeHintField = function normalizeHintField(hint) { + var finalHint = null; + + if (null != hint) { + switch (hint.constructor) { + case String: + finalHint = {}; + finalHint[hint] = 1; + break; + case Object: + finalHint = {}; + for (var name in hint) { + finalHint[name] = hint[name]; + } + break; + case Array: + finalHint = {}; + hint.forEach(function(param) { + finalHint[param] = 1; + }); + break; + } + } + + return finalHint; +}; + +/** + * Finds a single document based on the query + * + * Various argument possibilities + * - callback? + * - selector, callback?, + * - selector, fields, callback? + * - selector, options, callback? + * - selector, fields, options, callback? + * - selector, fields, skip, limit, callback? + * - selector, fields, skip, limit, timeout, callback? + * + * Options + * - **limit** {Number, default:0}, sets the limit of documents returned in the query. + * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc. + * - **fields** {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {'a':1} + * - **skip** {Number, default:0}, set to skip N documents ahead in your query (useful for pagination). + * - **hint** {Object}, tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1} + * - **explain** {Boolean, default:false}, explain the query instead of returning the data. + * - **snapshot** {Boolean, default:false}, snapshot query. + * - **timeout** {Boolean, default:false}, specify if the cursor can timeout. + * - **tailable** {Boolean, default:false}, specify if the cursor is tailable. + * - **batchSize** {Number, default:0}, set the batchSize for the getMoreCommand when iterating over the query results. + * - **returnKey** {Boolean, default:false}, only return the index key. + * - **maxScan** {Number}, Limit the number of items to scan. + * - **min** {Number}, Set index bounds. + * - **max** {Number}, Set index bounds. + * - **showDiskLoc** {Boolean, default:false}, Show disk location of results. + * - **comment** {String}, You can put a $comment field on a query to make looking in the profiler logs simpler. + * - **raw** {Boolean, default:false}, Return all BSON documents as Raw Buffer documents. + * - **read** {Boolean, default:false}, Tell the query to read from a secondary server. + * + * @param {Object} query query object to locate the object to modify + * @param {Object} [options] additional options during update. + * @param {Function} [callback] optional callback for cursor. + * @return {Cursor} returns a cursor to the query + * @api public + */ +Collection.prototype.findOne = function findOne () { + var self = this; + var args = Array.prototype.slice.call(arguments, 0); + var callback = args.pop(); + var cursor = this.find.apply(this, args).limit(1).batchSize(1); + // Return the item + cursor.toArray(function(err, items) { + if(err != null) return callback(err instanceof Error ? err : self.db.wrap(new Error(err)), null); + if(items.length == 1) return callback(null, items[0]); + callback(null, null); + }); +}; + +/** + * Creates an index on the collection. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a + * - **unique** {Boolean, default:false}, creates an unique index. + * - **sparse** {Boolean, default:false}, creates a sparse index. + * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible. + * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value + * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates. + * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates. + * + * @param {Object} fieldOrSpec fieldOrSpec that defines the index. + * @param {Object} [options] additional options during update. + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Collection.prototype.createIndex = function createIndex (fieldOrSpec, options, callback) { + // Clean up call + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + options = typeof callback === 'function' ? options : callback; + options = options == null ? {} : options; + + // Collect errorOptions + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + errorOptions = errorOptions == null && this.db.strict != null ? this.db.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions != null && errorOptions != false && (typeof callback !== 'function' && typeof options !== 'function')) throw new Error("safe cannot be used without a callback"); + + // Execute create index + this.db.createIndex(this.collectionName, fieldOrSpec, options, callback); +}; + +/** + * Ensures that an index exists, if it does not it creates it + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a + * - **unique** {Boolean, default:false}, creates an unique index. + * - **sparse** {Boolean, default:false}, creates a sparse index. + * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible. + * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value + * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates. + * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates. + * - **v** {Number}, specify the format version of the indexes. + * + * @param {Object} fieldOrSpec fieldOrSpec that defines the index. + * @param {Object} [options] additional options during update. + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Collection.prototype.ensureIndex = function ensureIndex (fieldOrSpec, options, callback) { + // Clean up call + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + options = typeof callback === 'function' ? options : callback; + options = options == null ? {} : options; + + // Collect errorOptions + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && this.opts.safe != null ? this.opts.safe : errorOptions; + errorOptions = errorOptions == null && this.db.strict != null ? this.db.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions != null && errorOptions != false && (typeof callback !== 'function' && typeof options !== 'function')) throw new Error("safe cannot be used without a callback"); + + // Execute create index + this.db.ensureIndex(this.collectionName, fieldOrSpec, options, callback); +}; + +/** + * Retrieves this collections index info. + * + * Options + * - **full** {Boolean, default:false}, returns the full raw index information. + * + * @param {Object} [options] additional options during update. + * @param {Function} callback returns the index information. + * @return {null} + * @api public + */ +Collection.prototype.indexInformation = function indexInformation (options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + options = args.length ? args.shift() : {}; + // Call the index information + this.db.indexInformation(this.collectionName, options, callback); +}; + +/** + * Drops an index from this collection. + * + * @param {String} name + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Collection.prototype.dropIndex = function dropIndex (name, callback) { + this.db.dropIndex(this.collectionName, name, callback); +}; + +/** + * Drops all indexes from this collection. + * + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Collection.prototype.dropAllIndexes = function dropIndexes (callback) { + this.db.dropIndex(this.collectionName, '*', function (err, result) { + if(err != null) { + callback(err, false); + } else if(result.documents[0].errmsg == null) { + callback(null, true); + } else { + callback(new Error(result.documents[0].errmsg), false); + } + }); +}; + +/** + * Drops all indexes from this collection. + * + * @deprecated + * @param {Function} callback returns the results. + * @return {null} + * @api private + */ +Collection.prototype.dropIndexes = Collection.prototype.dropAllIndexes; + +/** + * Reindex all indexes on the collection + * Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections. + * + * @param {Function} callback returns the results. + * @return {null} + * @api public +**/ +Collection.prototype.reIndex = function(callback) { + this.db.reIndex(this.collectionName, callback); +} + +/** + * Run Map Reduce across a collection. + * + * Options + * - **out** {Object, default:*{inline:1}*}, sets the output target for the map reduce job. *{inline:1} | {replace:'collectionName'} | {merge:'collectionName'} | {reduce:'collectionName'}* + * - **query** {Object}, query filter object. + * - **sort** {Object}, sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces. + * - **limit** {Number}, number of objects to return from collection. + * - **keeptemp** {Boolean, default:false}, keep temporary data. + * - **finalize** {Function | String}, finalize function. + * - **scope** {Object}, can pass in variables that can be access from map/reduce/finalize. + * - **jsMode** {Boolean, default:false}, it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X. + * - **verbose** {Boolean, default:false}, provide statistics on job execution time. + * + * @param {Function|String} map the mapping function. + * @param {Function|String} reduce the reduce function. + * @param {Objects} [options] options for the map reduce job. + * @param {Function} callback returns the result of the map reduce job, (error, results, [stats]) + * @return {null} + * @api public + */ +Collection.prototype.mapReduce = function mapReduce (map, reduce, options, callback) { + if ('function' === typeof options) callback = options, options = {}; + // Out must allways be defined (make sure we don't break weirdly on pre 1.8+ servers) + if(null == options.out) { + throw new Error("the out option parameter must be defined, see mongodb docs for possible values"); + } + + if ('function' === typeof map) { + map = map.toString(); + } + + if ('function' === typeof reduce) { + reduce = reduce.toString(); + } + + if ('function' === typeof options.finalize) { + options.finalize = options.finalize.toString(); + } + + var mapCommandHash = { + mapreduce: this.collectionName + , map: map + , reduce: reduce + }; + + // Add any other options passed in + for (var name in options) { + mapCommandHash[name] = options[name]; + } + + var self = this; + var cmd = DbCommand.createDbSlaveOkCommand(this.db, mapCommandHash); + + this.db._executeQueryCommand(cmd, {read:true}, function (err, result) { + if (err) { + return callback(err); + } + + // + if (1 != result.documents[0].ok || result.documents[0].err || result.documents[0].errmsg) { + return callback(self.db.wrap(result.documents[0])); + } + + // Create statistics value + var stats = {}; + if(result.documents[0].timeMillis) stats['processtime'] = result.documents[0].timeMillis; + if(result.documents[0].counts) stats['counts'] = result.documents[0].counts; + if(result.documents[0].timing) stats['timing'] = result.documents[0].timing; + + // invoked with inline? + if (result.documents[0].results) { + return callback(null, result.documents[0].results, stats); + } + + // Create a collection object that wraps the result collection + self.db.collection(result.documents[0].result, function (err, collection) { + // If we wish for no verbosity + if(options['verbose'] == null || !options['verbose']) { + return callback(err, collection); + } + + // Create statistics value + var stats = {}; + if(result.documents[0].timeMillis) stats['processtime'] = result.documents[0].timeMillis; + if(result.documents[0].counts) stats['counts'] = result.documents[0].counts; + if(result.documents[0].timing) stats['timing'] = result.documents[0].timing; + // Return stats as third set of values + callback(err, collection, stats); + }); + }); +}; + +/** + * Group function helper + * @ignore + */ +var groupFunction = function () { + var c = db[ns].find(condition); + var map = new Map(); + var reduce_function = reduce; + + while (c.hasNext()) { + var obj = c.next(); + var key = {}; + + for (var i = 0, len = keys.length; i < len; ++i) { + var k = keys[i]; + key[k] = obj[k]; + } + + var aggObj = map.get(key); + + if (aggObj == null) { + var newObj = Object.extend({}, key); + aggObj = Object.extend(newObj, initial); + map.put(key, aggObj); + } + + reduce_function(obj, aggObj); + } + + return { "result": map.values() }; +}.toString(); + +/** + * Run a group command across a collection + * + * @param {Object|Array|Function|Code} keys an object, array or function expressing the keys to group by. + * @param {Object} condition an optional condition that must be true for a row to be considered. + * @param {Object} initial initial value of the aggregation counter object. + * @param {Function|Code} reduce the reduce function aggregates (reduces) the objects iterated + * @param {Function|Code} finalize an optional function to be run on each item in the result set just before the item is returned. + * @param {Boolean} command specify if you wish to run using the internal group command or using eval, default is true. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Collection.prototype.group = function group(keys, condition, initial, reduce, finalize, command, callback) { + var args = Array.prototype.slice.call(arguments, 3); + callback = args.pop(); + // Fetch all commands + reduce = args.length ? args.shift() : null; + finalize = args.length ? args.shift() : null; + command = args.length ? args.shift() : null; + + // Make sure we are backward compatible + if(!(typeof finalize == 'function')) { + command = finalize; + finalize = null; + } + + if (!Array.isArray(keys) && keys instanceof Object && typeof(keys) !== 'function') { + keys = Object.keys(keys); + } + + if(typeof reduce === 'function') { + reduce = reduce.toString(); + } + + if(typeof finalize === 'function') { + finalize = finalize.toString(); + } + + // Set up the command as default + command = command == null ? true : command; + + // Execute using the command + if(command) { + var reduceFunction = reduce instanceof Code + ? reduce + : new Code(reduce); + + var selector = { + group: { + 'ns': this.collectionName + , '$reduce': reduceFunction + , 'cond': condition + , 'initial': initial + , 'out': "inline" + } + }; + + // if finalize is defined + if(finalize != null) selector.group['finalize'] = finalize; + // Set up group selector + if ('function' === typeof keys) { + selector.group.$keyf = keys instanceof Code + ? keys + : new Code(keys); + } else { + var hash = {}; + keys.forEach(function (key) { + hash[key] = 1; + }); + selector.group.key = hash; + } + + var cmd = DbCommand.createDbSlaveOkCommand(this.db, selector); + + this.db._executeQueryCommand(cmd, {read:true}, function (err, result) { + if(err != null) return callback(err); + + var document = result.documents[0]; + if (null == document.retval) { + return callback(new Error("group command failed: " + document.errmsg)); + } + + callback(null, document.retval); + }); + + } else { + // Create execution scope + var scope = reduce != null && reduce instanceof Code + ? reduce.scope + : {}; + + scope.ns = this.collectionName; + scope.keys = keys; + scope.condition = condition; + scope.initial = initial; + + // Pass in the function text to execute within mongodb. + var groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';'); + + this.db.eval(new Code(groupfn, scope), function (err, results) { + if (err) return callback(err, null); + callback(null, results.result || results); + }); + } +}; + +/** + * Returns the options of the collection. + * + * @param {Function} callback returns option results. + * @return {null} + * @api public + */ +Collection.prototype.options = function options(callback) { + this.db.collectionsInfo(this.collectionName, function (err, cursor) { + if (err) return callback(err); + cursor.nextObject(function (err, document) { + callback(err, document && document.options || null); + }); + }); +}; + +/** + * Returns if the collection is a capped collection + * + * @param {Function} callback returns if collection is capped. + * @return {null} + * @api public + */ +Collection.prototype.isCapped = function isCapped(callback) { + this.options(function(err, document) { + if(err != null) { + callback(err); + } else { + callback(null, document.capped); + } + }); +}; + +/** + * Checks if one or more indexes exist on the collection + * + * @param {String|Array} indexNames check if one or more indexes exist on the collection. + * @param {Function} callback returns if the indexes exist. + * @return {null} + * @api public + */ +Collection.prototype.indexExists = function indexExists(indexes, callback) { + this.indexInformation(function(err, indexInformation) { + // If we have an error return + if(err != null) return callback(err, null); + // Let's check for the index names + if(Array.isArray(indexes)) { + for(var i = 0; i < indexes.length; i++) { + if(indexInformation[indexes[i]] == null) { + return callback(null, false); + } + } + + // All keys found return true + return callback(null, true); + } else { + return callback(null, indexInformation[indexes] != null); + } + }); +} + +/** + * Execute the geoNear command to search for items in the collection + * + * Options + * - **num** {Number}, max number of results to return. + * - **maxDistance** {Number}, include results up to maxDistance from the point. + * - **distanceMultiplier** {Number}, include a value to multiply the distances with allowing for range conversions. + * - **query** {Object}, filter the results by a query. + * - **spherical** {Boolean, default:false}, perform query using a spherical model. + * - **uniqueDocs** {Boolean, default:false}, the closest location in a document to the center of the search region will always be returned MongoDB > 2.X. + * - **includeLocs** {Boolean, default:false}, include the location data fields in the top level of the results MongoDB > 2.X. + * + * @param {Number} x point to search on the x axis, ensure the indexes are ordered in the same order. + * @param {Number} y point to search on the y axis, ensure the indexes are ordered in the same order. + * @param {Objects} [options] options for the map reduce job. + * @param {Function} callback returns matching documents. + * @return {null} + * @api public + */ +Collection.prototype.geoNear = function geoNear(x, y, options, callback) { + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + // Fetch all commands + options = args.length ? args.shift() : {}; + + // Build command object + var commandObject = { + geoNear:this.collectionName, + near: [x, y] + } + + // Decorate object if any with known properties + if(options['num'] != null) commandObject['num'] = options['num']; + if(options['maxDistance'] != null) commandObject['maxDistance'] = options['maxDistance']; + if(options['distanceMultiplier'] != null) commandObject['distanceMultiplier'] = options['distanceMultiplier']; + if(options['query'] != null) commandObject['query'] = options['query']; + if(options['spherical'] != null) commandObject['spherical'] = options['spherical']; + if(options['uniqueDocs'] != null) commandObject['uniqueDocs'] = options['uniqueDocs']; + if(options['includeLocs'] != null) commandObject['includeLocs'] = options['includeLocs']; + + // Execute the command + this.db.command(commandObject, callback); +} + +/** + * Execute a geo search using a geo haystack index on a collection. + * + * Options + * - **maxDistance** {Number}, include results up to maxDistance from the point. + * - **search** {Object}, filter the results by a query. + * - **limit** {Number}, max number of results to return. + * + * @param {Number} x point to search on the x axis, ensure the indexes are ordered in the same order. + * @param {Number} y point to search on the y axis, ensure the indexes are ordered in the same order. + * @param {Objects} [options] options for the map reduce job. + * @param {Function} callback returns matching documents. + * @return {null} + * @api public + */ +Collection.prototype.geoHaystackSearch = function geoHaystackSearch(x, y, options, callback) { + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + // Fetch all commands + options = args.length ? args.shift() : {}; + + // Build command object + var commandObject = { + geoSearch:this.collectionName, + near: [x, y] + } + + // Decorate object if any with known properties + if(options['maxDistance'] != null) commandObject['maxDistance'] = options['maxDistance']; + if(options['query'] != null) commandObject['search'] = options['query']; + if(options['search'] != null) commandObject['search'] = options['search']; + if(options['limit'] != null) commandObject['limit'] = options['limit']; + + // Execute the command + this.db.command(commandObject, callback); +} + +/** + * Retrieve all the indexes on the collection. + * + * @param {Function} callback returns index information. + * @return {null} + * @api public + */ +Collection.prototype.indexes = function indexes(callback) { + // Return all the index information + this.db.indexInformation(this.collectionName, {full:true}, callback); +} + +/** + * Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.1 + * + * @param {Array|Objects} pipline a pipleline containing all the object for the execution. + * @param {Function} callback returns matching documents. + * @return {null} + * @api public + */ +Collection.prototype.aggregate = function(pipeline, callback) { + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + var self = this; + + // Check if we have more than one argument then just make the pipeline + // the remaining arguments + if(args.length > 1) { + pipeline = args; + } + + // Build the command + var command = { aggregate : this.collectionName, pipeline : pipeline}; + // Execute the command + this.db.command(command, function(err, result) { + if(err) { + callback(err); + } else if(result['err'] || result['errmsg']) { + callback(self.db.wrap(result)); + } else { + callback(null, result.result); + } + }); +} + +/** + * Get all the collection statistics. + * + * Options + * - **scale** {Number}, divide the returned sizes by scale value. + * + * @param {Objects} [options] options for the map reduce job. + * @param {Function} callback returns statistical information for the collection. + * @return {null} + * @api public + */ +Collection.prototype.stats = function stats(options, callback) { + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + // Fetch all commands + options = args.length ? args.shift() : {}; + + // Build command object + var commandObject = { + collStats:this.collectionName, + } + + // Check if we have the scale value + if(options['scale'] != null) commandObject['scale'] = options['scale']; + + // Execute the command + this.db.command(commandObject, callback); +} + +/** + * Expose. + */ +exports.Collection = Collection; + + + + + + + + + + + + + diff --git a/node_modules/mongodb/lib/mongodb/commands/base_command.js b/node_modules/mongodb/lib/mongodb/commands/base_command.js new file mode 100644 index 0000000..6e531d3 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/base_command.js @@ -0,0 +1,27 @@ +/** + Base object used for common functionality +**/ +var BaseCommand = exports.BaseCommand = function() { +}; + +var id = 1; +BaseCommand.prototype.getRequestId = function() { + if (!this.requestId) this.requestId = id++; + return this.requestId; +}; + +BaseCommand.prototype.updateRequestId = function() { + this.requestId = id++; + return this.requestId; +}; + +// OpCodes +BaseCommand.OP_REPLY = 1; +BaseCommand.OP_MSG = 1000; +BaseCommand.OP_UPDATE = 2001; +BaseCommand.OP_INSERT = 2002; +BaseCommand.OP_GET_BY_OID = 2003; +BaseCommand.OP_QUERY = 2004; +BaseCommand.OP_GET_MORE = 2005; +BaseCommand.OP_DELETE = 2006; +BaseCommand.OP_KILL_CURSORS = 2007;
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/db_command.js b/node_modules/mongodb/lib/mongodb/commands/db_command.js new file mode 100644 index 0000000..7586886 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/db_command.js @@ -0,0 +1,205 @@ +var QueryCommand = require('./query_command').QueryCommand, + InsertCommand = require('./insert_command').InsertCommand, + inherits = require('util').inherits, + crypto = require('crypto'); + +/** + Db Command +**/ +var DbCommand = exports.DbCommand = function(dbInstance, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) { + QueryCommand.call(this); + this.collectionName = collectionName; + this.queryOptions = queryOptions; + this.numberToSkip = numberToSkip; + this.numberToReturn = numberToReturn; + this.query = query; + this.returnFieldSelector = returnFieldSelector; + this.db = dbInstance; + + // Make sure we don't get a null exception + options = options == null ? {} : options; + // Let us defined on a command basis if we want functions to be serialized or not + if(options['serializeFunctions'] != null && options['serializeFunctions']) { + this.serializeFunctions = true; + } +}; + +inherits(DbCommand, QueryCommand); + +// Constants +DbCommand.SYSTEM_NAMESPACE_COLLECTION = "system.namespaces"; +DbCommand.SYSTEM_INDEX_COLLECTION = "system.indexes"; +DbCommand.SYSTEM_PROFILE_COLLECTION = "system.profile"; +DbCommand.SYSTEM_USER_COLLECTION = "system.users"; +DbCommand.SYSTEM_COMMAND_COLLECTION = "$cmd"; + +// New commands +DbCommand.NcreateIsMasterCommand = function(db, databaseName) { + return new DbCommand(db, databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null); +}; + +// Provide constructors for different db commands +DbCommand.createIsMasterCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null); +}; + +DbCommand.createCollectionInfoCommand = function(db, selector) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_NAMESPACE_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, 0, selector, null); +}; + +DbCommand.createGetNonceCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getnonce':1}, null); +}; + +DbCommand.createAuthenticationCommand = function(db, username, password, nonce) { + // Use node md5 generator + var md5 = crypto.createHash('md5'); + // Generate keys used for authentication + md5.update(username + ":mongo:" + password); + var hash_password = md5.digest('hex'); + // Final key + md5 = crypto.createHash('md5'); + md5.update(nonce + username + hash_password); + var key = md5.digest('hex'); + // Creat selector + var selector = {'authenticate':1, 'user':username, 'nonce':nonce, 'key':key}; + // Create db command + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NONE, 0, -1, selector, null); +}; + +DbCommand.createLogoutCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'logout':1}, null); +}; + +DbCommand.createCreateCollectionCommand = function(db, collectionName, options) { + var selector = {'create':collectionName}; + // Modify the options to ensure correct behaviour + for(var name in options) { + if(options[name] != null && options[name].constructor != Function) selector[name] = options[name]; + } + // Execute the command + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, selector, null); +}; + +DbCommand.createDropCollectionCommand = function(db, collectionName) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'drop':collectionName}, null); +}; + +DbCommand.createRenameCollectionCommand = function(db, fromCollectionName, toCollectionName) { + var renameCollection = db.databaseName + "." + fromCollectionName; + var toCollection = db.databaseName + "." + toCollectionName; + return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'renameCollection':renameCollection, 'to':toCollection}, null); +}; + +DbCommand.createGetLastErrorCommand = function(options, db) { + var args = Array.prototype.slice.call(arguments, 0); + db = args.pop(); + options = args.length ? args.shift() : {}; + // Final command + var command = {'getlasterror':1}; + // If we have an options Object let's merge in the fields (fsync/wtimeout/w) + if('object' === typeof options) { + for(var name in options) { + command[name] = options[name] + } + } + + // Execute command + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command, null); +}; + +DbCommand.createGetLastStatusCommand = DbCommand.createGetLastErrorCommand; + +DbCommand.createGetPreviousErrorsCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getpreverror':1}, null); +}; + +DbCommand.createResetErrorHistoryCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reseterror':1}, null); +}; + +DbCommand.createCreateIndexCommand = function(db, collectionName, fieldOrSpec, options) { + var fieldHash = {}; + var indexes = []; + var keys; + + // Get all the fields accordingly + if (fieldOrSpec.constructor === String) { // 'type' + indexes.push(fieldOrSpec + '_' + 1); + fieldHash[fieldOrSpec] = 1; + } else if (fieldOrSpec.constructor === Array) { // [{location:'2d'}, ...] + fieldOrSpec.forEach(function(f) { + if (f.constructor === String) { // [{location:'2d'}, 'type'] + indexes.push(f + '_' + 1); + fieldHash[f] = 1; + } else if (f.constructor === Array) { // [['location', '2d'],['type', 1]] + indexes.push(f[0] + '_' + (f[1] || 1)); + fieldHash[f[0]] = f[1] || 1; + } else if (f.constructor === Object) { // [{location:'2d'}, {type:1}] + keys = Object.keys(f); + keys.forEach(function(k) { + indexes.push(k + '_' + f[k]); + fieldHash[k] = f[k]; + }); + } else { + // undefined + } + }); + } else if (fieldOrSpec.constructor === Object) { // {location:'2d', type:1} + keys = Object.keys(fieldOrSpec); + keys.forEach(function(key) { + indexes.push(key + '_' + fieldOrSpec[key]); + fieldHash[key] = fieldOrSpec[key]; + }); + } + + // Generate the index name + var indexName = indexes.join("_"); + // Build the selector + var selector = {'ns':(db.databaseName + "." + collectionName), 'key':fieldHash, 'name':indexName}; + + // Ensure we have a correct finalUnique + var finalUnique = options == null || 'object' === typeof options ? false : options; + // Set up options + options = options == null || typeof options == 'boolean' ? {} : options; + + // Add all the options + var keys = Object.keys(options); + // Add all the fields to the selector + for(var i = 0; i < keys.length; i++) { + selector[keys[i]] = options[keys[i]]; + } + + // If we don't have the unique property set on the selector + if(selector['unique'] == null) selector['unique'] = finalUnique; + // Create the insert command for the index and return the document + return new InsertCommand(db, db.databaseName + "." + DbCommand.SYSTEM_INDEX_COLLECTION, false).add(selector); +}; + +DbCommand.logoutCommand = function(db, command_hash) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null); +} + +DbCommand.createDropIndexCommand = function(db, collectionName, indexName) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'deleteIndexes':collectionName, 'index':indexName}, null); +}; + +DbCommand.createReIndexCommand = function(db, collectionName) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reIndex':collectionName}, null); +}; + +DbCommand.createDropDatabaseCommand = function(db) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'dropDatabase':1}, null); +}; + +DbCommand.createDbCommand = function(db, command_hash, options) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null, options); +}; + +DbCommand.createAdminDbCommand = function(db, command_hash) { + return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null); +}; + +DbCommand.createDbSlaveOkCommand = function(db, command_hash, options) { + return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 0, -1, command_hash, null, options); +};
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/delete_command.js b/node_modules/mongodb/lib/mongodb/commands/delete_command.js new file mode 100644 index 0000000..1adad07 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/delete_command.js @@ -0,0 +1,111 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits; + +/** + Insert Document Command +**/ +var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector) { + BaseCommand.call(this); + + // Validate correctness off the selector + var object = selector; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("delete raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + this.collectionName = collectionName; + this.selector = selector; + this.db = db; +}; + +inherits(DeleteCommand, BaseCommand); + +DeleteCommand.OP_DELETE = 2006; + +/* +struct { + MsgHeader header; // standard message header + int32 ZERO; // 0 - reserved for future use + cstring fullCollectionName; // "dbname.collectionname" + int32 ZERO; // 0 - reserved for future use + mongo.BSON selector; // query object. See below for details. +} +*/ +DeleteCommand.prototype.toBinary = function() { + // Calculate total length of the document + var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.selector, false, true) + (4 * 4); + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff; + _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index] = totalLengthOfCommand & 0xff; + // Adjust index + _index = _index + 4; + // Write the request ID + _command[_index + 3] = (this.requestId >> 24) & 0xff; + _command[_index + 2] = (this.requestId >> 16) & 0xff; + _command[_index + 1] = (this.requestId >> 8) & 0xff; + _command[_index] = this.requestId & 0xff; + // Adjust index + _index = _index + 4; + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + // Write the op_code for the command + _command[_index + 3] = (DeleteCommand.OP_DELETE >> 24) & 0xff; + _command[_index + 2] = (DeleteCommand.OP_DELETE >> 16) & 0xff; + _command[_index + 1] = (DeleteCommand.OP_DELETE >> 8) & 0xff; + _command[_index] = DeleteCommand.OP_DELETE & 0xff; + // Adjust index + _index = _index + 4; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Write the collection name to the command + _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1; + _command[_index - 1] = 0; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Document binary length + var documentLength = 0 + + // Serialize the selector + // If we are passing a raw buffer, do minimal validation + if(Buffer.isBuffer(this.selector)) { + documentLength = this.selector.length; + // Copy the data into the current buffer + this.selector.copy(_command, _index); + } else { + documentLength = this.db.bson.serializeWithBufferAndIndex(this.selector, this.checkKeys, _command, _index) - _index + 1; + } + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + return _command; +};
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/get_more_command.js b/node_modules/mongodb/lib/mongodb/commands/get_more_command.js new file mode 100644 index 0000000..d3aac02 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/get_more_command.js @@ -0,0 +1,83 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits, + binaryutils = require('../utils'); + +/** + Get More Document Command +**/ +var GetMoreCommand = exports.GetMoreCommand = function(db, collectionName, numberToReturn, cursorId) { + BaseCommand.call(this); + + this.collectionName = collectionName; + this.numberToReturn = numberToReturn; + this.cursorId = cursorId; + this.db = db; +}; + +inherits(GetMoreCommand, BaseCommand); + +GetMoreCommand.OP_GET_MORE = 2005; + +GetMoreCommand.prototype.toBinary = function() { + // Calculate total length of the document + var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 8 + (4 * 4); + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index++] = totalLengthOfCommand & 0xff; + _command[_index++] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index++] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index++] = (totalLengthOfCommand >> 24) & 0xff; + + // Write the request ID + _command[_index++] = this.requestId & 0xff; + _command[_index++] = (this.requestId >> 8) & 0xff; + _command[_index++] = (this.requestId >> 16) & 0xff; + _command[_index++] = (this.requestId >> 24) & 0xff; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Write the op_code for the command + _command[_index++] = GetMoreCommand.OP_GET_MORE & 0xff; + _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 8) & 0xff; + _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 16) & 0xff; + _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 24) & 0xff; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Write the collection name to the command + _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1; + _command[_index - 1] = 0; + + // Number of documents to return + _command[_index++] = this.numberToReturn & 0xff; + _command[_index++] = (this.numberToReturn >> 8) & 0xff; + _command[_index++] = (this.numberToReturn >> 16) & 0xff; + _command[_index++] = (this.numberToReturn >> 24) & 0xff; + + // Encode the cursor id + var low_bits = this.cursorId.getLowBits(); + // Encode low bits + _command[_index++] = low_bits & 0xff; + _command[_index++] = (low_bits >> 8) & 0xff; + _command[_index++] = (low_bits >> 16) & 0xff; + _command[_index++] = (low_bits >> 24) & 0xff; + + var high_bits = this.cursorId.getHighBits(); + // Encode high bits + _command[_index++] = high_bits & 0xff; + _command[_index++] = (high_bits >> 8) & 0xff; + _command[_index++] = (high_bits >> 16) & 0xff; + _command[_index++] = (high_bits >> 24) & 0xff; + // Return command + return _command; +};
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/insert_command.js b/node_modules/mongodb/lib/mongodb/commands/insert_command.js new file mode 100644 index 0000000..3036a02 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/insert_command.js @@ -0,0 +1,141 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits; + +/** + Insert Document Command +**/ +var InsertCommand = exports.InsertCommand = function(db, collectionName, checkKeys, options) { + BaseCommand.call(this); + + this.collectionName = collectionName; + this.documents = []; + this.checkKeys = checkKeys == null ? true : checkKeys; + this.db = db; + this.flags = 0; + this.serializeFunctions = false; + + // Ensure valid options hash + options = options == null ? {} : options; + + // Check if we have keepGoing set -> set flag if it's the case + if(options['keepGoing'] != null && options['keepGoing']) { + // This will finish inserting all non-index violating documents even if it returns an error + this.flags = 1; + } + + // Let us defined on a command basis if we want functions to be serialized or not + if(options['serializeFunctions'] != null && options['serializeFunctions']) { + this.serializeFunctions = true; + } +}; + +inherits(InsertCommand, BaseCommand); + +// OpCodes +InsertCommand.OP_INSERT = 2002; + +InsertCommand.prototype.add = function(document) { + if(Buffer.isBuffer(document)) { + var object_size = document[0] | document[1] << 8 | document[2] << 16 | document[3] << 24; + if(object_size != document.length) { + var error = new Error("insert raw message size does not match message header size [" + document.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + this.documents.push(document); + return this; +}; + +/* +struct { + MsgHeader header; // standard message header + int32 ZERO; // 0 - reserved for future use + cstring fullCollectionName; // "dbname.collectionname" + BSON[] documents; // one or more documents to insert into the collection +} +*/ +InsertCommand.prototype.toBinary = function() { + // Calculate total length of the document + var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + (4 * 4); + // var docLength = 0 + for(var i = 0; i < this.documents.length; i++) { + if(Buffer.isBuffer(this.documents[i])) { + totalLengthOfCommand += this.documents[i].length; + } else { + // Calculate size of document + totalLengthOfCommand += this.db.bson.calculateObjectSize(this.documents[i], this.serializeFunctions, true); + } + } + + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff; + _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index] = totalLengthOfCommand & 0xff; + // Adjust index + _index = _index + 4; + // Write the request ID + _command[_index + 3] = (this.requestId >> 24) & 0xff; + _command[_index + 2] = (this.requestId >> 16) & 0xff; + _command[_index + 1] = (this.requestId >> 8) & 0xff; + _command[_index] = this.requestId & 0xff; + // Adjust index + _index = _index + 4; + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + // Write the op_code for the command + _command[_index + 3] = (InsertCommand.OP_INSERT >> 24) & 0xff; + _command[_index + 2] = (InsertCommand.OP_INSERT >> 16) & 0xff; + _command[_index + 1] = (InsertCommand.OP_INSERT >> 8) & 0xff; + _command[_index] = InsertCommand.OP_INSERT & 0xff; + // Adjust index + _index = _index + 4; + // Write flags if any + _command[_index + 3] = (this.flags >> 24) & 0xff; + _command[_index + 2] = (this.flags >> 16) & 0xff; + _command[_index + 1] = (this.flags >> 8) & 0xff; + _command[_index] = this.flags & 0xff; + // Adjust index + _index = _index + 4; + // Write the collection name to the command + _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1; + _command[_index - 1] = 0; + + // Write all the bson documents to the buffer at the index offset + for(var i = 0; i < this.documents.length; i++) { + // Document binary length + var documentLength = 0 + var object = this.documents[i]; + + // Serialize the selector + // If we are passing a raw buffer, do minimal validation + if(Buffer.isBuffer(object)) { + documentLength = object.length; + // Copy the data into the current buffer + object.copy(_command, _index); + } else { + // Serialize the document straight to the buffer + documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1; + } + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + } + + return _command; +}; diff --git a/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js b/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js new file mode 100644 index 0000000..d8ccb0c --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js @@ -0,0 +1,98 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits, + binaryutils = require('../utils'); + +/** + Insert Document Command +**/ +var KillCursorCommand = exports.KillCursorCommand = function(db, cursorIds) { + BaseCommand.call(this); + + this.cursorIds = cursorIds; + this.db = db; +}; + +inherits(KillCursorCommand, BaseCommand); + +KillCursorCommand.OP_KILL_CURSORS = 2007; + +/* +struct { + MsgHeader header; // standard message header + int32 ZERO; // 0 - reserved for future use + int32 numberOfCursorIDs; // number of cursorIDs in message + int64[] cursorIDs; // array of cursorIDs to close +} +*/ +KillCursorCommand.prototype.toBinary = function() { + // Calculate total length of the document + var totalLengthOfCommand = 4 + 4 + (4 * 4) + (this.cursorIds.length * 8); + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff; + _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index] = totalLengthOfCommand & 0xff; + // Adjust index + _index = _index + 4; + // Write the request ID + _command[_index + 3] = (this.requestId >> 24) & 0xff; + _command[_index + 2] = (this.requestId >> 16) & 0xff; + _command[_index + 1] = (this.requestId >> 8) & 0xff; + _command[_index] = this.requestId & 0xff; + // Adjust index + _index = _index + 4; + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + // Write the op_code for the command + _command[_index + 3] = (KillCursorCommand.OP_KILL_CURSORS >> 24) & 0xff; + _command[_index + 2] = (KillCursorCommand.OP_KILL_CURSORS >> 16) & 0xff; + _command[_index + 1] = (KillCursorCommand.OP_KILL_CURSORS >> 8) & 0xff; + _command[_index] = KillCursorCommand.OP_KILL_CURSORS & 0xff; + // Adjust index + _index = _index + 4; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Number of cursors to kill + var numberOfCursors = this.cursorIds.length; + _command[_index + 3] = (numberOfCursors >> 24) & 0xff; + _command[_index + 2] = (numberOfCursors >> 16) & 0xff; + _command[_index + 1] = (numberOfCursors >> 8) & 0xff; + _command[_index] = numberOfCursors & 0xff; + // Adjust index + _index = _index + 4; + + // Encode all the cursors + for(var i = 0; i < this.cursorIds.length; i++) { + // Encode the cursor id + var low_bits = this.cursorIds[i].getLowBits(); + // Encode low bits + _command[_index + 3] = (low_bits >> 24) & 0xff; + _command[_index + 2] = (low_bits >> 16) & 0xff; + _command[_index + 1] = (low_bits >> 8) & 0xff; + _command[_index] = low_bits & 0xff; + // Adjust index + _index = _index + 4; + + var high_bits = this.cursorIds[i].getHighBits(); + // Encode high bits + _command[_index + 3] = (high_bits >> 24) & 0xff; + _command[_index + 2] = (high_bits >> 16) & 0xff; + _command[_index + 1] = (high_bits >> 8) & 0xff; + _command[_index] = high_bits & 0xff; + // Adjust index + _index = _index + 4; + } + + return _command; +};
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/query_command.js b/node_modules/mongodb/lib/mongodb/commands/query_command.js new file mode 100644 index 0000000..e07a8aa --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/query_command.js @@ -0,0 +1,209 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits; + +/** + Insert Document Command +**/ +var QueryCommand = exports.QueryCommand = function(db, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) { + BaseCommand.call(this); + + // Validate correctness off the selector + var object = query; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + var object = returnFieldSelector; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + // Make sure we don't get a null exception + options = options == null ? {} : options; + // Set up options + this.collectionName = collectionName; + this.queryOptions = queryOptions; + this.numberToSkip = numberToSkip; + this.numberToReturn = numberToReturn; + this.query = query; + this.returnFieldSelector = returnFieldSelector; + this.db = db; + + // Let us defined on a command basis if we want functions to be serialized or not + if(options['serializeFunctions'] != null && options['serializeFunctions']) { + this.serializeFunctions = true; + } +}; + +inherits(QueryCommand, BaseCommand); + +QueryCommand.OP_QUERY = 2004; + +/* +struct { + MsgHeader header; // standard message header + int32 opts; // query options. See below for details. + cstring fullCollectionName; // "dbname.collectionname" + int32 numberToSkip; // number of documents to skip when returning results + int32 numberToReturn; // number of documents to return in the first OP_REPLY + BSON query ; // query object. See below for details. + [ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details. +} +*/ +QueryCommand.prototype.toBinary = function() { + var totalLengthOfCommand = 0; + // Calculate total length of the document + if(Buffer.isBuffer(this.query)) { + totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.query.length + (4 * 4); + } else { + totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.db.bson.calculateObjectSize(this.query, this.serializeFunctions, true) + (4 * 4); + } + + // Calculate extra fields size + if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) { + if(Object.keys(this.returnFieldSelector).length > 0) { + totalLengthOfCommand += this.db.bson.calculateObjectSize(this.returnFieldSelector, this.serializeFunctions, true); + } + } else if(Buffer.isBuffer(this.returnFieldSelector)) { + totalLengthOfCommand += this.returnFieldSelector.length; + } + + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff; + _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index] = totalLengthOfCommand & 0xff; + // Adjust index + _index = _index + 4; + // Write the request ID + _command[_index + 3] = (this.requestId >> 24) & 0xff; + _command[_index + 2] = (this.requestId >> 16) & 0xff; + _command[_index + 1] = (this.requestId >> 8) & 0xff; + _command[_index] = this.requestId & 0xff; + // Adjust index + _index = _index + 4; + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + // Write the op_code for the command + _command[_index + 3] = (QueryCommand.OP_QUERY >> 24) & 0xff; + _command[_index + 2] = (QueryCommand.OP_QUERY >> 16) & 0xff; + _command[_index + 1] = (QueryCommand.OP_QUERY >> 8) & 0xff; + _command[_index] = QueryCommand.OP_QUERY & 0xff; + // Adjust index + _index = _index + 4; + + // Write the query options + _command[_index + 3] = (this.queryOptions >> 24) & 0xff; + _command[_index + 2] = (this.queryOptions >> 16) & 0xff; + _command[_index + 1] = (this.queryOptions >> 8) & 0xff; + _command[_index] = this.queryOptions & 0xff; + // Adjust index + _index = _index + 4; + + // Write the collection name to the command + _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1; + _command[_index - 1] = 0; + + // Write the number of documents to skip + _command[_index + 3] = (this.numberToSkip >> 24) & 0xff; + _command[_index + 2] = (this.numberToSkip >> 16) & 0xff; + _command[_index + 1] = (this.numberToSkip >> 8) & 0xff; + _command[_index] = this.numberToSkip & 0xff; + // Adjust index + _index = _index + 4; + + // Write the number of documents to return + _command[_index + 3] = (this.numberToReturn >> 24) & 0xff; + _command[_index + 2] = (this.numberToReturn >> 16) & 0xff; + _command[_index + 1] = (this.numberToReturn >> 8) & 0xff; + _command[_index] = this.numberToReturn & 0xff; + // Adjust index + _index = _index + 4; + + // Document binary length + var documentLength = 0 + var object = this.query; + + // Serialize the selector + if(Buffer.isBuffer(object)) { + documentLength = object.length; + // Copy the data into the current buffer + object.copy(_command, _index); + } else { + // Serialize the document straight to the buffer + documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1; + } + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + + // Push field selector if available + if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) { + if(Object.keys(this.returnFieldSelector).length > 0) { + var documentLength = this.db.bson.serializeWithBufferAndIndex(this.returnFieldSelector, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1; + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + } + } if(this.returnFieldSelector != null && Buffer.isBuffer(this.returnFieldSelector)) { + // Document binary length + var documentLength = 0 + var object = this.returnFieldSelector; + + // Serialize the selector + documentLength = object.length; + // Copy the data into the current buffer + object.copy(_command, _index); + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + } + + // Return finished command + return _command; +}; + +// Constants +QueryCommand.OPTS_NONE = 0; +QueryCommand.OPTS_TAILABLE_CURSOR = 2; +QueryCommand.OPTS_SLAVE = 4; +QueryCommand.OPTS_OPLOG_REPLY = 8; +QueryCommand.OPTS_NO_CURSOR_TIMEOUT = 16; +QueryCommand.OPTS_AWAIT_DATA = 32; +QueryCommand.OPTS_EXHAUST = 64;
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/commands/update_command.js b/node_modules/mongodb/lib/mongodb/commands/update_command.js new file mode 100644 index 0000000..9829dea --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/commands/update_command.js @@ -0,0 +1,174 @@ +var BaseCommand = require('./base_command').BaseCommand, + inherits = require('util').inherits; + +/** + Update Document Command +**/ +var UpdateCommand = exports.UpdateCommand = function(db, collectionName, spec, document, options) { + BaseCommand.call(this); + + var object = spec; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("update spec raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + var object = document; + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) { + var error = new Error("update document raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + error.name = 'MongoError'; + throw error; + } + } + + this.collectionName = collectionName; + this.spec = spec; + this.document = document; + this.db = db; + this.serializeFunctions = false; + + // Generate correct flags + var db_upsert = 0; + var db_multi_update = 0; + db_upsert = options != null && options['upsert'] != null ? (options['upsert'] == true ? 1 : 0) : db_upsert; + db_multi_update = options != null && options['multi'] != null ? (options['multi'] == true ? 1 : 0) : db_multi_update; + + // Flags + this.flags = parseInt(db_multi_update.toString() + db_upsert.toString(), 2); + // Let us defined on a command basis if we want functions to be serialized or not + if(options['serializeFunctions'] != null && options['serializeFunctions']) { + this.serializeFunctions = true; + } +}; + +inherits(UpdateCommand, BaseCommand); + +UpdateCommand.OP_UPDATE = 2001; + +/* +struct { + MsgHeader header; // standard message header + int32 ZERO; // 0 - reserved for future use + cstring fullCollectionName; // "dbname.collectionname" + int32 flags; // bit vector. see below + BSON spec; // the query to select the document + BSON document; // the document data to update with or insert +} +*/ +UpdateCommand.prototype.toBinary = function() { + // Calculate total length of the document + var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.spec, false, true) + + this.db.bson.calculateObjectSize(this.document, this.serializeFunctions, true) + (4 * 4); + + // Let's build the single pass buffer command + var _index = 0; + var _command = new Buffer(totalLengthOfCommand); + // Write the header information to the buffer + _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff; + _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff; + _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff; + _command[_index] = totalLengthOfCommand & 0xff; + // Adjust index + _index = _index + 4; + // Write the request ID + _command[_index + 3] = (this.requestId >> 24) & 0xff; + _command[_index + 2] = (this.requestId >> 16) & 0xff; + _command[_index + 1] = (this.requestId >> 8) & 0xff; + _command[_index] = this.requestId & 0xff; + // Adjust index + _index = _index + 4; + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + // Write the op_code for the command + _command[_index + 3] = (UpdateCommand.OP_UPDATE >> 24) & 0xff; + _command[_index + 2] = (UpdateCommand.OP_UPDATE >> 16) & 0xff; + _command[_index + 1] = (UpdateCommand.OP_UPDATE >> 8) & 0xff; + _command[_index] = UpdateCommand.OP_UPDATE & 0xff; + // Adjust index + _index = _index + 4; + + // Write zero + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + _command[_index++] = 0; + + // Write the collection name to the command + _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1; + _command[_index - 1] = 0; + + // Write the update flags + _command[_index + 3] = (this.flags >> 24) & 0xff; + _command[_index + 2] = (this.flags >> 16) & 0xff; + _command[_index + 1] = (this.flags >> 8) & 0xff; + _command[_index] = this.flags & 0xff; + // Adjust index + _index = _index + 4; + + // Document binary length + var documentLength = 0 + var object = this.spec; + + // Serialize the selector + // If we are passing a raw buffer, do minimal validation + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + documentLength = object.length; + // Copy the data into the current buffer + object.copy(_command, _index); + } else { + documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, false) - _index + 1; + } + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + + // Document binary length + var documentLength = 0 + var object = this.document; + + // Serialize the document + // If we are passing a raw buffer, do minimal validation + if(Buffer.isBuffer(object)) { + var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24; + if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]"); + documentLength = object.length; + // Copy the data into the current buffer + object.copy(_command, _index); + } else { + documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1; + } + + // Write the length to the document + _command[_index + 3] = (documentLength >> 24) & 0xff; + _command[_index + 2] = (documentLength >> 16) & 0xff; + _command[_index + 1] = (documentLength >> 8) & 0xff; + _command[_index] = documentLength & 0xff; + // Update index in buffer + _index = _index + documentLength; + // Add terminating 0 for the object + _command[_index - 1] = 0; + + return _command; +}; + +// Constants +UpdateCommand.DB_UPSERT = 0; +UpdateCommand.DB_MULTI_UPDATE = 1;
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/connection/connection.js b/node_modules/mongodb/lib/mongodb/connection/connection.js new file mode 100644 index 0000000..86818bb --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/connection.js @@ -0,0 +1,414 @@ +var utils = require('./connection_utils'), + inherits = require('util').inherits, + net = require('net'), + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits, + binaryutils = require('../utils'), + tls = require('tls'); + +var Connection = exports.Connection = function(id, socketOptions) { + // Set up event emitter + EventEmitter.call(this); + // Store all socket options + this.socketOptions = socketOptions ? socketOptions : {host:'localhost', port:27017}; + // Id for the connection + this.id = id; + // State of the connection + this.connected = false; + + // + // Connection parsing state + // + this.maxBsonSize = socketOptions.maxBsonSize ? socketOptions.maxBsonSize : Connection.DEFAULT_MAX_BSON_SIZE; + // Contains the current message bytes + this.buffer = null; + // Contains the current message size + this.sizeOfMessage = 0; + // Contains the readIndex for the messaage + this.bytesRead = 0; + // Contains spill over bytes from additional messages + this.stubBuffer = 0; + + // Just keeps list of events we allow + this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[], end:[]}; + + // Just keeps list of events we allow + resetHandlers(this, false); +} + +// Set max bson size +Connection.DEFAULT_MAX_BSON_SIZE = 4 * 1024 * 1024 * 4 * 3; + +// Inherit event emitter so we can emit stuff wohoo +inherits(Connection, EventEmitter); + +Connection.prototype.start = function() { + // If we have a normal connection + if(this.socketOptions.ssl) { + // Create a new stream + this.connection = new net.Socket(); + // Set options on the socket + this.connection.setTimeout(this.socketOptions.timeout); + // Work around for 0.4.X + if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay); + // Set keep alive if defined + if(process.version.indexOf("v0.4") == -1) { + if(this.socketOptions.keepAlive > 0) { + this.connection.setKeepAlive(true, this.socketOptions.keepAlive); + } else { + this.connection.setKeepAlive(false); + } + } + + // Set up pair for tls with server, accept self-signed certificates as well + var pair = this.pair = tls.createSecurePair(false); + // Set up encrypted streams + this.pair.encrypted.pipe(this.connection); + this.connection.pipe(this.pair.encrypted); + + // Setup clearText stream + this.writeSteam = this.pair.cleartext; + // Add all handlers to the socket to manage it + this.pair.on("secure", connectHandler(this)); + this.pair.cleartext.on("data", createDataHandler(this)); + // Add handlers + this.connection.on("error", errorHandler(this)); + // this.connection.on("connect", connectHandler(this)); + this.connection.on("end", endHandler(this)); + this.connection.on("timeout", timeoutHandler(this)); + this.connection.on("drain", drainHandler(this)); + this.writeSteam.on("close", closeHandler(this)); + // Start socket + this.connection.connect(this.socketOptions.port, this.socketOptions.host); + } else { + // // Create a new stream + // this.connection = new net.Stream(); + // // Create new connection instance + // this.connection = new net.Socket(); + this.connection = net.createConnection(this.socketOptions.port, this.socketOptions.host); + // Set options on the socket + this.connection.setTimeout(this.socketOptions.timeout); + // Work around for 0.4.X + if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay); + // Set keep alive if defined + if(process.version.indexOf("v0.4") == -1) { + if(this.socketOptions.keepAlive > 0) { + this.connection.setKeepAlive(true, this.socketOptions.keepAlive); + } else { + this.connection.setKeepAlive(false); + } + } + + // Set up write stream + this.writeSteam = this.connection; + // Add handlers + this.connection.on("error", errorHandler(this)); + // Add all handlers to the socket to manage it + this.connection.on("connect", connectHandler(this)); + // this.connection.on("end", endHandler(this)); + this.connection.on("data", createDataHandler(this)); + this.connection.on("timeout", timeoutHandler(this)); + this.connection.on("drain", drainHandler(this)); + this.connection.on("close", closeHandler(this)); + // // Start socket + // this.connection.connect(this.socketOptions.port, this.socketOptions.host); + } +} + +// Check if the sockets are live +Connection.prototype.isConnected = function() { + return this.connected && !this.connection.destroyed && this.connection.writable && this.connection.readable; +} + +// Write the data out to the socket +Connection.prototype.write = function(command, callback) { + try { + // If we have a list off commands to be executed on the same socket + if(Array.isArray(command)) { + for(var i = 0; i < command.length; i++) { + var binaryCommand = command[i].toBinary() + if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand); + var r = this.writeSteam.write(binaryCommand); + } + } else { + var binaryCommand = command.toBinary() + if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand); + var r = this.writeSteam.write(binaryCommand); + } + } catch (err) { + if(typeof callback === 'function') callback(err); + } +} + +// Force the closure of the connection +Connection.prototype.close = function() { + // clear out all the listeners + resetHandlers(this, true); + // Add a dummy error listener to catch any weird last moment errors (and ignore them) + this.connection.on("error", function() {}) + // destroy connection + this.connection.destroy(); +} + +// Reset all handlers +var resetHandlers = function(self, clearListeners) { + self.eventHandlers = {error:[], connect:[], close:[], end:[], timeout:[], parseError:[], message:[]}; + + // If we want to clear all the listeners + if(clearListeners && self.connection != null) { + var keys = Object.keys(self.eventHandlers); + // Remove all listeners + for(var i = 0; i < keys.length; i++) { + self.connection.removeAllListeners(keys[i]); + } + } +} + +// +// Handlers +// + +// Connect handler +var connectHandler = function(self) { + return function() { + // Set connected + self.connected = true; + // Emit the connect event with no error + self.emit("connect", null, self); + } +} + +var createDataHandler = exports.Connection.createDataHandler = function(self) { + // We need to handle the parsing of the data + // and emit the messages when there is a complete one + return function(data) { + // Parse until we are done with the data + while(data.length > 0) { + // If we still have bytes to read on the current message + if(self.bytesRead > 0 && self.sizeOfMessage > 0) { + // Calculate the amount of remaining bytes + var remainingBytesToRead = self.sizeOfMessage - self.bytesRead; + // Check if the current chunk contains the rest of the message + if(remainingBytesToRead > data.length) { + // Copy the new data into the exiting buffer (should have been allocated when we know the message size) + data.copy(self.buffer, self.bytesRead); + // Adjust the number of bytes read so it point to the correct index in the buffer + self.bytesRead = self.bytesRead + data.length; + + // Reset state of buffer + data = new Buffer(0); + } else { + // Copy the missing part of the data into our current buffer + data.copy(self.buffer, self.bytesRead, 0, remainingBytesToRead); + // Slice the overflow into a new buffer that we will then re-parse + data = data.slice(remainingBytesToRead); + + // Emit current complete message + try { + self.emit("message", self.buffer, self); + + } catch(err) { + var errorObject = {err:"socketHandler", trace:err, bin:buffer, parseState:{ + sizeOfMessage:self.sizeOfMessage, + bytesRead:self.bytesRead, + stubBuffer:self.stubBuffer}}; + if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); + // We got a parse Error fire it off then keep going + self.emit("parseError", errorObject, self); + } + + // Reset state of buffer + self.buffer = null; + self.sizeOfMessage = 0; + self.bytesRead = 0; + self.stubBuffer = null; + } + } else { + // Stub buffer is kept in case we don't get enough bytes to determine the + // size of the message (< 4 bytes) + if(self.stubBuffer != null && self.stubBuffer.length > 0) { + + // If we have enough bytes to determine the message size let's do it + if(self.stubBuffer.length + data.length > 4) { + // Prepad the data + var newData = new Buffer(self.stubBuffer.length + data.length); + self.stubBuffer.copy(newData, 0); + data.copy(newData, self.stubBuffer.length); + // Reassign for parsing + data = newData; + + // Reset state of buffer + self.buffer = null; + self.sizeOfMessage = 0; + self.bytesRead = 0; + self.stubBuffer = null; + + } else { + + // Add the the bytes to the stub buffer + var newStubBuffer = new Buffer(self.stubBuffer.length + data.length); + // Copy existing stub buffer + self.stubBuffer.copy(newStubBuffer, 0); + // Copy missing part of the data + data.copy(newStubBuffer, self.stubBuffer.length); + // Exit parsing loop + data = new Buffer(0); + } + } else { + if(data.length > 4) { + // Retrieve the message size + var sizeOfMessage = binaryutils.decodeUInt32(data, 0); + // If we have a negative sizeOfMessage emit error and return + if(sizeOfMessage < 0 || sizeOfMessage > self.maxBsonSize) { + var errorObject = {err:"socketHandler", trace:'', bin:self.buffer, parseState:{ + sizeOfMessage:sizeOfMessage, + bytesRead:self.bytesRead, + stubBuffer:self.stubBuffer}}; + if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); + // We got a parse Error fire it off then keep going + self.emit("parseError", errorObject, self); + return; + } + + // Ensure that the size of message is larger than 0 and less than the max allowed + if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage > data.length) { + self.buffer = new Buffer(sizeOfMessage); + // Copy all the data into the buffer + data.copy(self.buffer, 0); + // Update bytes read + self.bytesRead = data.length; + // Update sizeOfMessage + self.sizeOfMessage = sizeOfMessage; + // Ensure stub buffer is null + self.stubBuffer = null; + // Exit parsing loop + data = new Buffer(0); + + } else if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage == data.length) { + try { + self.emit("message", data, self); + // Reset state of buffer + self.buffer = null; + self.sizeOfMessage = 0; + self.bytesRead = 0; + self.stubBuffer = null; + // Exit parsing loop + data = new Buffer(0); + + } catch (err) { + var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{ + sizeOfMessage:self.sizeOfMessage, + bytesRead:self.bytesRead, + stubBuffer:self.stubBuffer}}; + if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); + // We got a parse Error fire it off then keep going + self.emit("parseError", errorObject, self); + } + } else if(sizeOfMessage <= 4 || sizeOfMessage > self.maxBsonSize) { + var errorObject = {err:"socketHandler", trace:null, bin:data, parseState:{ + sizeOfMessage:sizeOfMessage, + bytesRead:0, + buffer:null, + stubBuffer:null}}; + if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); + // We got a parse Error fire it off then keep going + self.emit("parseError", errorObject, self); + + // Clear out the state of the parser + self.buffer = null; + self.sizeOfMessage = 0; + self.bytesRead = 0; + self.stubBuffer = null; + // Exit parsing loop + data = new Buffer(0); + + } else { + try { + self.emit("message", data.slice(0, sizeOfMessage), self); + // Reset state of buffer + self.buffer = null; + self.sizeOfMessage = 0; + self.bytesRead = 0; + self.stubBuffer = null; + // Copy rest of message + data = data.slice(sizeOfMessage); + } catch (err) { + var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{ + sizeOfMessage:sizeOfMessage, + bytesRead:self.bytesRead, + stubBuffer:self.stubBuffer}}; + if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject); + // We got a parse Error fire it off then keep going + self.emit("parseError", errorObject, self); + } + + } + } else { + // Create a buffer that contains the space for the non-complete message + self.stubBuffer = new Buffer(data.length) + // Copy the data to the stub buffer + data.copy(self.stubBuffer, 0); + // Exit parsing loop + data = new Buffer(0); + } + } + } + } + } +} + +var endHandler = function(self) { + return function() { + // Set connected to false + self.connected = false; + // Emit end event + self.emit("end", {err: 'connection received Fin packet from [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self); + } +} + +var timeoutHandler = function(self) { + return function() { + self.emit("timeout", {err: 'connection to [' + self.socketOptions.host + ':' + self.socketOptions.port + '] timed out'}, self); + } +} + +var drainHandler = function(self) { + return function() { + } +} + +var errorHandler = function(self) { + return function(err) { + // Set connected to false + self.connected = false; + // Emit error + self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self); + } +} + +var closeHandler = function(self) { + return function(hadError) { + // If we have an error during the connection phase + if(hadError && !self.connected) { + // Set disconnected + self.connected = false; + // Emit error + self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self); + } else { + // Set disconnected + self.connected = false; + // Emit close + self.emit("close", {err: 'connection closed to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self); + } + } +} + +// Some basic defaults +Connection.DEFAULT_PORT = 27017; + + + + + + + diff --git a/node_modules/mongodb/lib/mongodb/connection/connection_pool.js b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js new file mode 100644 index 0000000..421d1bd --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js @@ -0,0 +1,259 @@ +var utils = require('./connection_utils'), + inherits = require('util').inherits, + net = require('net'), + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits, + MongoReply = require("../responses/mongo_reply").MongoReply, + Connection = require("./connection").Connection; + +var ConnectionPool = exports.ConnectionPool = function(host, port, poolSize, bson, socketOptions) { + if(typeof host !== 'string' || typeof port !== 'number') throw "host and port must be specified [" + host + ":" + port + "]"; + // Set up event emitter + EventEmitter.call(this); + // Keep all options for the socket in a specific collection allowing the user to specify the + // Wished upon socket connection parameters + this.socketOptions = typeof socketOptions === 'object' ? socketOptions : {}; + this.socketOptions.host = host; + this.socketOptions.port = port; + this.bson = bson; + // PoolSize is always + 1 for special reserved "measurment" socket (like ping, stats etc) + this.poolSize = poolSize; + + // Set default settings for the socket options + utils.setIntegerParameter(this.socketOptions, 'timeout', 0); + // Delay before writing out the data to the server + utils.setBooleanParameter(this.socketOptions, 'noDelay', true); + // Delay before writing out the data to the server + utils.setIntegerParameter(this.socketOptions, 'keepAlive', 0); + // Set the encoding of the data read, default is binary == null + utils.setStringParameter(this.socketOptions, 'encoding', null); + // Allows you to set a throttling bufferSize if you need to stop overflows + utils.setIntegerParameter(this.socketOptions, 'bufferSize', 0); + + // Internal structures + this.openConnections = []; + this.connections = []; + + // Assign connection id's + this.connectionId = 0; + + // Current index for selection of pool connection + this.currentConnectionIndex = 0; + // The pool state + this._poolState = 'disconnected'; + // timeout control + this._timeout = false; +} + +inherits(ConnectionPool, EventEmitter); + +ConnectionPool.prototype.setMaxBsonSize = function(maxBsonSize) { + if(maxBsonSize == null){ + maxBsonSize = Connection.DEFAULT_MAX_BSON_SIZE; + } + + for(var i = 0; i < this.openConnections.length; i++) { + this.openConnections[i].maxBsonSize = maxBsonSize; + } +} + +// Start a function +var _connect = function(_self) { + return new function() { + var connectionStatus = _self._poolState; + // Create a new connection instance + var connection = new Connection(_self.connectionId++, _self.socketOptions); + // Set logger on pool + connection.logger = _self.logger; + // Connect handler + connection.on("connect", function(err, connection) { + // Add connection to list of open connections + _self.openConnections.push(connection); + _self.connections.push(connection) + + // If the number of open connections is equal to the poolSize signal ready pool + if(_self.connections.length === _self.poolSize && _self._poolState !== 'disconnected') { + // Set connected + _self._poolState = 'connected'; + // Emit pool ready + _self.emit("poolReady"); + } else if(_self.connections.length < _self.poolSize) { + // We need to open another connection, make sure it's in the next + // tick so we don't get a cascade of errors + process.nextTick(function() { + _connect(_self); + }); + } + }); + + var numberOfErrors = 0 + + // Error handler + connection.on("error", function(err, connection) { + numberOfErrors++; + // If we are already disconnected ignore the event + if(connectionStatus != 'disconnected' && _self.listeners("error").length > 0) { + _self.emit("error", err); + } + + // Set disconnected + connectionStatus = 'disconnected'; + // Set disconnected + _self._poolState = 'disconnected'; + // Clean up + _self.openConnections = []; + _self.connections = []; + }); + + // Close handler + connection.on("close", function() { + // If we are already disconnected ignore the event + if(connectionStatus !== 'disconnected' && _self.listeners("close").length > 0) { + _self.emit("close"); + } + + // Set disconnected + connectionStatus = 'disconnected'; + // Set disconnected + _self._poolState = 'disconnected'; + // Clean up + _self.openConnections = []; + _self.connections = []; + }); + + // Timeout handler + connection.on("timeout", function(err, connection) { + // If we are already disconnected ignore the event + if(connectionStatus !== 'disconnected' && _self.listeners("timeout").length > 0) { + _self.emit("timeout", err); + } + + // Set disconnected + connectionStatus = 'disconnected'; + // Set disconnected + _self._poolState = 'disconnected'; + // Clean up + _self.openConnections = []; + _self.connections = []; + }); + + // Parse error, needs a complete shutdown of the pool + connection.on("parseError", function() { + // If we are already disconnected ignore the event + if(connectionStatus !== 'disconnected' && _self.listeners("parseError").length > 0) { + // if(connectionStatus == 'connected') { + _self.emit("parseError", new Error("parseError occured")); + } + + // Set disconnected + connectionStatus = 'disconnected'; + _self.stop(); + }); + + connection.on("message", function(message) { + _self.emit("message", message); + }); + + // Start connection in the next tick + connection.start(); + }(); +} + + +// Start method, will throw error if no listeners are available +// Pass in an instance of the listener that contains the api for +// finding callbacks for a given message etc. +ConnectionPool.prototype.start = function() { + var markerDate = new Date().getTime(); + var self = this; + + if(this.listeners("poolReady").length == 0) { + throw "pool must have at least one listener ready that responds to the [poolReady] event"; + } + + // Set pool state to connecting + this._poolState = 'connecting'; + this._timeout = false; + + _connect(self); +} + +// Restart a connection pool (on a close the pool might be in a wrong state) +ConnectionPool.prototype.restart = function() { + // Close all connections + this.stop(false); + // Now restart the pool + this.start(); +} + +// Stop the connections in the pool +ConnectionPool.prototype.stop = function(removeListeners) { + removeListeners = removeListeners == null ? true : removeListeners; + // Set disconnected + this._poolState = 'disconnected'; + + // Clear all listeners if specified + if(removeListeners) { + this.removeAllEventListeners(); + } + + // Close all connections + for(var i = 0; i < this.connections.length; i++) { + this.connections[i].close(); + } + + // Clean up + // this.connectionsWithErrors = []; + this.openConnections = []; + this.connections = []; +} + +// Check the status of the connection +ConnectionPool.prototype.isConnected = function() { + return this._poolState === 'connected'; +} + +// Checkout a connection from the pool for usage, or grab a specific pool instance +ConnectionPool.prototype.checkoutConnection = function(id) { + var index = (this.currentConnectionIndex++ % (this.openConnections.length)); + var connection = this.openConnections[index]; + return connection; +} + +ConnectionPool.prototype.getAllConnections = function() { + return this.connections; +} + +// Remove all non-needed event listeners +ConnectionPool.prototype.removeAllEventListeners = function() { + this.removeAllListeners("close"); + this.removeAllListeners("error"); + this.removeAllListeners("timeout"); + this.removeAllListeners("connect"); + this.removeAllListeners("end"); + this.removeAllListeners("parseError"); + this.removeAllListeners("message"); + this.removeAllListeners("poolReady"); +} + + + + + + + + + + + + + + + + + + + + + + diff --git a/node_modules/mongodb/lib/mongodb/connection/connection_utils.js b/node_modules/mongodb/lib/mongodb/connection/connection_utils.js new file mode 100644 index 0000000..5910924 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/connection_utils.js @@ -0,0 +1,23 @@ +exports.setIntegerParameter = function(object, field, defaultValue) { + if(object[field] == null) { + object[field] = defaultValue; + } else if(typeof object[field] !== "number" && object[field] !== parseInt(object[field], 10)) { + throw "object field [" + field + "] must be a numeric integer value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]"; + } +} + +exports.setBooleanParameter = function(object, field, defaultValue) { + if(object[field] == null) { + object[field] = defaultValue; + } else if(typeof object[field] !== "boolean") { + throw "object field [" + field + "] must be a boolean value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]"; + } +} + +exports.setStringParameter = function(object, field, defaultValue) { + if(object[field] == null) { + object[field] = defaultValue; + } else if(typeof object[field] !== "string") { + throw "object field [" + field + "] must be a string value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]"; + } +}
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/connection/repl_set_servers.js b/node_modules/mongodb/lib/mongodb/connection/repl_set_servers.js new file mode 100644 index 0000000..3e0e15a --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/repl_set_servers.js @@ -0,0 +1,972 @@ +var Connection = require('./connection').Connection, + DbCommand = require('../commands/db_command').DbCommand, + MongoReply = require('../responses/mongo_reply').MongoReply, + debug = require('util').debug, + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits, + inspect = require('util').inspect, + Server = require('./server').Server, + PingStrategy = require('./strategies/ping_strategy').PingStrategy, + StatisticsStrategy = require('./strategies/statistics_strategy').StatisticsStrategy; + +const STATE_STARTING_PHASE_1 = 0; +const STATE_PRIMARY = 1; +const STATE_SECONDARY = 2; +const STATE_RECOVERING = 3; +const STATE_FATAL_ERROR = 4; +const STATE_STARTING_PHASE_2 = 5; +const STATE_UNKNOWN = 6; +const STATE_ARBITER = 7; +const STATE_DOWN = 8; +const STATE_ROLLBACK = 9; + +/** +* ReplSetServers constructor provides master-slave functionality +* +* @param serverArr{Array of type Server} +* @return constructor of ServerCluster +* +*/ +var ReplSetServers = exports.ReplSetServers = function(servers, options) { + // Set up event emitter + EventEmitter.call(this); + // Set up basic + if(!(this instanceof ReplSetServers)) return new ReplSetServers(server, options); + + var self = this; + // Contains the master server entry + this.options = options == null ? {} : options; + this.reconnectWait = this.options["reconnectWait"] != null ? this.options["reconnectWait"] : 1000; + this.retries = this.options["retries"] != null ? this.options["retries"] : 30; + this.replicaSet = this.options["rs_name"]; + + // Are we allowing reads from secondaries ? + this.readSecondary = this.options["read_secondary"]; + this.slaveOk = true; + this.closedConnectionCount = 0; + this._used = false; + + // Default poolSize for new server instances + this.poolSize = this.options.poolSize == null ? 1 : this.options.poolSize; + + // Set up ssl connections + this.ssl = this.options.ssl == null ? false : this.options.ssl; + + // Just keeps list of events we allow + this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[]}; + // Internal state of server connection + this._serverState = 'disconnected'; + // Read preference + this._readPreference = null; + // Do we record server stats or not + this.recordQueryStats = false; + + // Get the readPreference + var readPreference = this.options['readPreference']; + // Read preference setting + if(readPreference != null) { + if(readPreference != Server.READ_PRIMARY && readPreference != Server.READ_SECONDARY_ONLY + && readPreference != Server.READ_SECONDARY) { + throw new Error("Illegal readPreference mode specified, " + readPreference); + } + + // Set read Preference + this._readPreference = readPreference; + } else { + this._readPreference = null; + } + + // Strategy for picking a secondary + this.strategy = this.options['strategy'] == null ? 'statistical' : this.options['strategy']; + // Make sure strategy is one of the two allowed + if(this.strategy != null && (this.strategy != 'ping' && this.strategy != 'statistical')) throw new Error("Only ping or statistical strategies allowed"); + // Let's set up our strategy object for picking secodaries + if(this.strategy == 'ping') { + // Create a new instance + this.strategyInstance = new PingStrategy(this); + } else if(this.strategy == 'statistical') { + // Set strategy as statistical + this.strategyInstance = new StatisticsStrategy(this); + // Add enable query information + this.enableRecordQueryStats(true); + } + + // Set default connection pool options + this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {}; + + // Set up logger if any set + this.logger = this.options.logger != null + && (typeof this.options.logger.debug == 'function') + && (typeof this.options.logger.error == 'function') + && (typeof this.options.logger.debug == 'function') + ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}}; + + // Ensure all the instances are of type server and auto_reconnect is false + if(!Array.isArray(servers) || servers.length == 0) { + throw Error("The parameter must be an array of servers and contain at least one server"); + } else if(Array.isArray(servers) || servers.length > 0) { + var count = 0; + servers.forEach(function(server) { + if(server instanceof Server) count = count + 1; + // Ensure no server has reconnect on + server.options.auto_reconnect = false; + }); + + if(count < servers.length) { + throw Error("All server entries must be of type Server"); + } else { + this.servers = servers; + } + } + + // Auto Reconnect property + Object.defineProperty(this, "autoReconnect", { enumerable: true + , get: function () { + return true; + } + }); + + // Get Read Preference method + Object.defineProperty(this, "readPreference", { enumerable: true + , get: function () { + if(this._readPreference == null && this.readSecondary) { + return Server.READ_SECONDARY; + } else if(this._readPreference == null && !this.readSecondary) { + return Server.READ_PRIMARY; + } else { + return this._readPreference; + } + } + }); + + // Db Instances + Object.defineProperty(this, "dbInstances", {enumerable:true + , get: function() { + var servers = this.allServerInstances(); + return servers[0].dbInstances; + } + }) + + // Auto Reconnect property + Object.defineProperty(this, "host", { enumerable: true + , get: function () { + if (this.primary != null) return this.primary.host; + } + }); + + Object.defineProperty(this, "port", { enumerable: true + , get: function () { + if (this.primary != null) return this.primary.port; + } + }); + + Object.defineProperty(this, "read", { enumerable: true + , get: function () { + return this.secondaries.length > 0 ? this.secondaries[0] : null; + } + }); + + // Get list of secondaries + Object.defineProperty(this, "secondaries", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.secondaries); + var array = new Array(keys.length); + // Convert secondaries to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.secondaries[keys[i]]; + } + return array; + } + }); + + // Get list of all secondaries including passives + Object.defineProperty(this, "allSecondaries", {enumerable: true + , get: function() { + return this.secondaries.concat(this.passives); + } + }); + + // Get list of arbiters + Object.defineProperty(this, "arbiters", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.arbiters); + var array = new Array(keys.length); + // Convert arbiters to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.arbiters[keys[i]]; + } + return array; + } + }); + + // Get list of passives + Object.defineProperty(this, "passives", {enumerable: true + , get: function() { + var keys = Object.keys(this._state.passives); + var array = new Array(keys.length); + // Convert arbiters to array + for(var i = 0; i < keys.length; i++) { + array[i] = this._state.passives[keys[i]]; + } + return array; + } + }); + + // Master connection property + Object.defineProperty(this, "primary", { enumerable: true + , get: function () { + return this._state != null ? this._state.master : null; + } + }); +}; + +inherits(ReplSetServers, EventEmitter); + +// Allow setting the read preference at the replicaset level +ReplSetServers.prototype.setReadPreference = function(preference) { + // Set read preference + this._readPreference = preference; + // Ensure slaveOk is correct for secodnaries read preference and tags + if((this._readPreference == Server.READ_SECONDARY || this._readPreference == Server.READ_SECONDARY_ONLY) + || (this._readPreference != null && typeof this._readPreference == 'object')) { + this.slaveOk = true; + } +} + +// Return the used state +ReplSetServers.prototype._isUsed = function() { + return this._used; +} + +ReplSetServers.prototype.setTarget = function(target) { + this.target = target; +}; + +ReplSetServers.prototype.isConnected = function() { + // Return the state of the replicaset server + return this.primary != null && this._state.master != null && this._state.master.isConnected(); +} + +Server.prototype.isSetMember = function() { + return false; +} + +ReplSetServers.prototype.isPrimary = function(config) { + return this.readSecondary && this.secondaries.length > 0 ? false : true; +} + +ReplSetServers.prototype.isReadPrimary = ReplSetServers.prototype.isPrimary; + +// Clean up dead connections +var cleanupConnections = ReplSetServers.cleanupConnections = function(connections, addresses, byTags) { + // Ensure we don't have entries in our set with dead connections + var keys = Object.keys(connections); + for(var i = 0; i < keys.length; i++) { + var server = connections[keys[i]]; + // If it's not connected remove it from the list + if(!server.isConnected()) { + // Remove from connections and addresses + delete connections[keys[i]]; + delete addresses[keys[i]]; + // Clean up tags if needed + if(server.tags != null && typeof server.tags === 'object') { + cleanupTags(server, byTags); + } + } + } +} + +var cleanupTags = ReplSetServers._cleanupTags = function(server, byTags) { + var serverTagKeys = Object.keys(server.tags); + // Iterate over all server tags and remove any instances for that tag that matches the current + // server + for(var i = 0; i < serverTagKeys.length; i++) { + // Fetch the value for the tag key + var value = server.tags[serverTagKeys[i]]; + + // If we got an instance of the server + if(byTags[serverTagKeys[i]] != null + && byTags[serverTagKeys[i]][value] != null + && Array.isArray(byTags[serverTagKeys[i]][value])) { + // List of clean servers + var cleanInstances = []; + // We got instances for the particular tag set + var instances = byTags[serverTagKeys[i]][value]; + for(var j = 0; j < instances.length; j++) { + var serverInstance = instances[j]; + // If we did not find an instance add it to the clean instances + if((serverInstance.host + ":" + serverInstance.port) !== (server.host + ":" + server.port)) { + cleanInstances.push(serverInstance); + } + } + + // Update the byTags list + byTags[serverTagKeys[i]][value] = cleanInstances; + } + } +} + +ReplSetServers.prototype.allServerInstances = function() { + var self = this; + // Close all the servers (concatenate entire list of servers first for ease) + var allServers = self._state.master != null ? [self._state.master] : []; + + // Secondary keys + var keys = Object.keys(self._state.secondaries); + // Add all secondaries + for(var i = 0; i < keys.length; i++) { + allServers.push(self._state.secondaries[keys[i]]); + } + + // Arbiter keys + var keys = Object.keys(self._state.arbiters); + // Add all arbiters + for(var i = 0; i < keys.length; i++) { + allServers.push(self._state.arbiters[keys[i]]); + } + + // Passive keys + var keys = Object.keys(self._state.passives); + // Add all arbiters + for(var i = 0; i < keys.length; i++) { + allServers.push(self._state.passives[keys[i]]); + } + + // Return complete list of all servers + return allServers; +} + +// Ensure no callback is left hanging when we have an error +var __executeAllCallbacksWithError = function(dbInstance, error) { + var keys = Object.keys(dbInstance._callBackStore._notReplied); + // Iterate over all callbacks + for(var i = 0; i < keys.length; i++) { + // Delete info object + delete dbInstance._callBackStore._notReplied[keys[i]]; + // Emit the error + dbInstance._callBackStore.emit(keys[i], error); + } +} + +ReplSetServers.prototype.connect = function(parent, options, callback) { + var self = this; + var dateStamp = new Date().getTime(); + if('function' === typeof options) callback = options, options = {}; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + + // Keep reference to parent + this.db = parent; + // Set server state to connecting + this._serverState = 'connecting'; + // Reference to the instance + var replSetSelf = this; + var serverConnections = this.servers; + // Ensure parent can do a slave query if it's set + parent.slaveOk = this.slaveOk ? this.slaveOk : parent.slaveOk; + // Number of total servers that need to initialized (known servers) + var numberOfServersLeftToInitialize = serverConnections.length; + + // Clean up state + replSetSelf._state = {'master':null, 'secondaries':{}, 'arbiters':{}, 'passives':{}, 'errors':{}, 'addresses':{}, 'byTags':{}, 'setName':null, 'errorMessages':[], 'members':[]}; + + // Create a connection handler + var connectionHandler = function(instanceServer) { + return function(err, result) { + // Don't attempt to connect if we are done + // if(replSetSelf._serverState === 'disconnected') return; + // Remove a server from the list of intialized servers we need to perform + numberOfServersLeftToInitialize = numberOfServersLeftToInitialize - 1; + // Add enable query information + instanceServer.enableRecordQueryStats(replSetSelf.recordQueryStats); + + if(err == null && result.documents[0].hosts != null) { + // Fetch the isMaster command result + var document = result.documents[0]; + // Break out the results + var setName = document.setName; + var isMaster = document.ismaster; + var secondary = document.secondary; + var passive = document.passive; + var arbiterOnly = document.arbiterOnly; + var hosts = Array.isArray(document.hosts) ? document.hosts : []; + var arbiters = Array.isArray(document.arbiters) ? document.arbiters : []; + var passives = Array.isArray(document.passives) ? document.passives : []; + var tags = document.tags ? document.tags : {}; + var primary = document.primary; + var me = document.me; + + // Only add server to our internal list if it's a master, secondary or arbiter + if(isMaster == true || secondary == true || arbiterOnly == true) { + // Handle a closed connection + var closeHandler = function(err, server) { + var closeServers = function() { + // Set the state to disconnected + parent._state = 'disconnected'; + // Shut down the replicaset for now and Fire off all the callbacks sitting with no reply + if(replSetSelf._serverState == 'connected') { + // Close the replicaset + replSetSelf.close(function() { + __executeAllCallbacksWithError(parent, err); + // Ensure single callback only + if(callback != null) { + // Single callback only + var internalCallback = callback; + callback = null; + // Return the error + internalCallback(err, null); + } else { + // If the parent has listeners trigger an event + if(parent.listeners("close").length > 0) { + parent.emit("close", err); + } + } + }); + } + } + + // Check if this is the primary server, then disconnect otherwise keep going + if(replSetSelf._state.master != null) { + var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port; + var errorServerAddress = server.host + ":" + server.port; + + // Only shut down the set if we have a primary server error + if(primaryAddress == errorServerAddress) { + closeServers(); + } else { + // Remove from the list of servers + delete replSetSelf._state.addresses[errorServerAddress]; + // Locate one of the lists and remove + if(replSetSelf._state.secondaries[errorServerAddress] != null) { + delete replSetSelf._state.secondaries[errorServerAddress]; + } else if(replSetSelf._state.arbiters[errorServerAddress] != null) { + delete replSetSelf._state.arbiters[errorServerAddress]; + } else if(replSetSelf._state.passives[errorServerAddress] != null) { + delete replSetSelf._state.passives[errorServerAddress]; + } + + // Check if we are reading from Secondary only + if(replSetSelf._readPreference == Server.READ_SECONDARY_ONLY && Object.keys(replSetSelf._state.secondaries).length == 0) { + closeServers(); + } + } + } else { + closeServers(); + } + } + + // Handle a connection timeout + var timeoutHandler = function(err, server) { + var closeServers = function() { + // Set the state to disconnected + parent._state = 'disconnected'; + // Shut down the replicaset for now and Fire off all the callbacks sitting with no reply + if(replSetSelf._serverState == 'connected') { + // Close the replicaset + replSetSelf.close(function() { + __executeAllCallbacksWithError(parent, err); + // Ensure single callback only + if(callback != null) { + // Single callback only + var internalCallback = callback; + callback = null; + // Return the error + internalCallback(new Error("connection timed out"), null); + } else { + // If the parent has listeners trigger an event + if(parent.listeners("error").length > 0) { + parent.emit("timeout", new Error("connection timed out")); + } + } + }); + } + } + + // Check if this is the primary server, then disconnect otherwise keep going + if(replSetSelf._state.master != null) { + var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port; + var errorServerAddress = server.host + ":" + server.port; + + // Only shut down the set if we have a primary server error + if(primaryAddress == errorServerAddress) { + closeServers(); + } else { + // Remove from the list of servers + delete replSetSelf._state.addresses[errorServerAddress]; + // Locate one of the lists and remove + if(replSetSelf._state.secondaries[errorServerAddress] != null) { + delete replSetSelf._state.secondaries[errorServerAddress]; + } else if(replSetSelf._state.arbiters[errorServerAddress] != null) { + delete replSetSelf._state.arbiters[errorServerAddress]; + } else if(replSetSelf._state.passives[errorServerAddress] != null) { + delete replSetSelf._state.passives[errorServerAddress]; + } + + // Check if we are reading from Secondary only + if(replSetSelf._readPreference == Server.READ_SECONDARY_ONLY && Object.keys(replSetSelf._state.secondaries).length == 0) { + closeServers(); + } + } + } else { + closeServers(); + } + } + + // Handle an error + var errorHandler = function(err, server) { + var closeServers = function() { + // Set the state to disconnected + parent._state = 'disconnected'; + // Shut down the replicaset for now and Fire off all the callbacks sitting with no reply + if(replSetSelf._serverState == 'connected') { + // Close the replicaset + replSetSelf.close(function() { + __executeAllCallbacksWithError(parent, err); + // Ensure single callback only + if(callback != null) { + // Single callback only + var internalCallback = callback; + callback = null; + // Return the error + internalCallback(err, null); + } else { + // If the parent has listeners trigger an event + if(parent.listeners("error").length > 0) { + parent.emit("error", err); + } + } + }); + } + } + + // Check if this is the primary server, then disconnect otherwise keep going + if(replSetSelf._state.master != null) { + var primaryAddress = replSetSelf._state.master.host + ":" + replSetSelf._state.master.port; + var errorServerAddress = server.host + ":" + server.port; + + // Only shut down the set if we have a primary server error + if(primaryAddress == errorServerAddress) { + closeServers(); + } else { + // Remove from the list of servers + delete replSetSelf._state.addresses[errorServerAddress]; + // Locate one of the lists and remove + if(replSetSelf._state.secondaries[errorServerAddress] != null) { + delete replSetSelf._state.secondaries[errorServerAddress]; + } else if(replSetSelf._state.arbiters[errorServerAddress] != null) { + delete replSetSelf._state.arbiters[errorServerAddress]; + } else if(replSetSelf._state.passives[errorServerAddress] != null) { + delete replSetSelf._state.passives[errorServerAddress]; + } + + // Check if we are reading from Secondary only + if(replSetSelf._readPreference == Server.READ_SECONDARY_ONLY && Object.keys(replSetSelf._state.secondaries).length == 0) { + closeServers(); + } + } + } else { + closeServers(); + } + } + + // Ensure we don't have duplicate handlers + instanceServer.removeAllListeners("close"); + instanceServer.removeAllListeners("error"); + instanceServer.removeAllListeners("timeout"); + + // Add error handler to the instance of the server + instanceServer.on("close", closeHandler); + // Add error handler to the instance of the server + instanceServer.on("error", errorHandler); + // instanceServer.on("timeout", errorHandler); + instanceServer.on("timeout", timeoutHandler); + // Add tag info + instanceServer.tags = tags; + + // For each tag in tags let's add the instance Server to the list for that tag + if(tags != null && typeof tags === 'object') { + var tagKeys = Object.keys(tags); + // For each tag file in the server add it to byTags + for(var i = 0; i < tagKeys.length; i++) { + var value = tags[tagKeys[i]]; + // Check if we have a top level tag object + if(replSetSelf._state.byTags[tagKeys[i]] == null) replSetSelf._state.byTags[tagKeys[i]] = {}; + // For the value check if we have an array of server instances + if(!Array.isArray(replSetSelf._state.byTags[tagKeys[i]][value])) replSetSelf._state.byTags[tagKeys[i]][value] = []; + // Check that the instance is not already registered there + var valueArray = replSetSelf._state.byTags[tagKeys[i]][value]; + var found = false; + + // Iterate over all values + for(var j = 0; j < valueArray.length; j++) { + if(valueArray[j].host == instanceServer.host && valueArray[j].port == instanceServer.port) { + found = true; + break; + } + } + + // If it was not found push the instance server to the list + if(!found) valueArray.push(instanceServer); + } + } + + // Remove from error list + delete replSetSelf._state.errors[me]; + + // Add our server to the list of finished servers + replSetSelf._state.addresses[me] = instanceServer; + + // Assign the set name + if(replSetSelf.replicaSet == null) { + replSetSelf._state.setName = setName; + } else if(replSetSelf.replicaSet != setName && replSetSelf._serverState != 'disconnected') { + replSetSelf._state.errorMessages.push(new Error("configured mongodb replicaset does not match provided replicaset [" + setName + "] != [" + replSetSelf.replicaSet + "]")); + // Set done + replSetSelf._serverState = 'disconnected'; + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Return error message ignoring rest of calls + return internalCallback(replSetSelf._state.errorMessages[0], parent); + } + + // Let's add the server to our list of server types + if(secondary == true && (passive == false || passive == null)) { + replSetSelf._state.secondaries[me] = instanceServer; + } else if(arbiterOnly == true) { + replSetSelf._state.arbiters[me] = instanceServer; + } else if(secondary == true && passive == true) { + replSetSelf._state.passives[me] = instanceServer; + } else if(isMaster == true) { + replSetSelf._state.master = instanceServer; + } else if(isMaster == false && primary != null && replSetSelf._state.addresses[primary]) { + replSetSelf._state.master = replSetSelf._state.addresses[primary]; + } + + // Let's go throught all the "possible" servers in the replicaset + var candidateServers = hosts.concat(arbiters).concat(passives); + + // If we have new servers let's add them + for(var i = 0; i < candidateServers.length; i++) { + // Fetch the server string + var candidateServerString = candidateServers[i]; + // Add the server if it's not defined + if(replSetSelf._state.addresses[candidateServerString] == null) { + // Split the server string + var parts = candidateServerString.split(/:/); + if(parts.length == 1) { + parts = [parts[0], Connection.DEFAULT_PORT]; + } + + // Default empty socket options object + var socketOptions = {}; + // If a socket option object exists clone it + if(replSetSelf.socketOptions != null) { + var keys = Object.keys(replSetSelf.socketOptions); + for(var i = 0; i < keys.length;i++) socketOptions[keys[i]] = replSetSelf.socketOptions[keys[i]]; + } + + // Add host information to socket options + socketOptions['host'] = parts[0]; + socketOptions['port'] = parseInt(parts[1]); + + // Create a new server instance + var newServer = new Server(parts[0], parseInt(parts[1]), {auto_reconnect:false, 'socketOptions':socketOptions + , logger:replSetSelf.logger, ssl:replSetSelf.ssl, poolSize:replSetSelf.poolSize}); + // Set the replicaset instance + newServer.replicasetInstance = replSetSelf; + + // Add handlers + newServer.on("close", closeHandler); + newServer.on("timeout", timeoutHandler); + newServer.on("error", errorHandler); + + // Add server to list, ensuring we don't get a cascade of request to the same server + replSetSelf._state.addresses[candidateServerString] = newServer; + + // Add a new server to the total number of servers that need to initialized before we are done + numberOfServersLeftToInitialize = numberOfServersLeftToInitialize + 1; + + // Let's set up a new server instance + newServer.connect(parent, {returnIsMasterResults: true, eventReceiver:newServer}, connectionHandler(newServer)); + } + } + } else { + // Remove the instance from out list of servers + delete replSetSelf._state.addresses[me]; + } + } + + // If done finish up + if((numberOfServersLeftToInitialize == 0) && replSetSelf._serverState === 'connecting' && replSetSelf._state.errorMessages.length == 0) { + // Set db as connected + replSetSelf._serverState = 'connected'; + // If we don't expect a master let's call back, otherwise we need a master before + // the connection is successful + if(replSetSelf.masterNotNeeded || replSetSelf._state.master != null) { + // If we have a read strategy boot it + if(replSetSelf.strategyInstance != null) { + // Ensure we have a proper replicaset defined + replSetSelf.strategyInstance.replicaset = replSetSelf; + // Start strategy + replSetSelf.strategyInstance.start(function(err) { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(null, parent); + }) + } else { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(null, parent); + } + } else if(replSetSelf.readSecondary == true && Object.keys(replSetSelf._state.secondaries).length > 0) { + // If we have a read strategy boot it + if(replSetSelf.strategyInstance != null) { + // Ensure we have a proper replicaset defined + replSetSelf.strategyInstance.replicaset = replSetSelf; + // Start strategy + replSetSelf.strategyInstance.start(function(err) { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(null, parent); + }) + } else { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(null, parent); + } + } else if(replSetSelf.readSecondary == true && Object.keys(replSetSelf._state.secondaries).length == 0) { + replSetSelf._serverState = 'disconnected'; + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Force close all server instances + replSetSelf.close(); + // Perform callback + internalCallback(new Error("no secondary server found"), null); + } else if(typeof callback === 'function'){ + replSetSelf._serverState = 'disconnected'; + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Force close all server instances + replSetSelf.close(); + // Perform callback + internalCallback(new Error("no primary server found"), null); + } + } else if((numberOfServersLeftToInitialize == 0) && replSetSelf._state.errorMessages.length > 0 && replSetSelf._serverState != 'disconnected') { + // Set done + replSetSelf._serverState = 'disconnected'; + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Force close all server instances + replSetSelf.close(); + // Callback to signal we are done + internalCallback(replSetSelf._state.errorMessages[0], null); + } + } + } + + // Ensure we have all registered servers in our set + for(var i = 0; i < serverConnections.length; i++) { + replSetSelf._state.addresses[serverConnections[i].host + ':' + serverConnections[i].port] = serverConnections[i]; + } + + // Initialize all the connections + for(var i = 0; i < serverConnections.length; i++) { + // Set up the logger for the server connection + serverConnections[i].logger = replSetSelf.logger; + // Default empty socket options object + var socketOptions = {}; + // If a socket option object exists clone it + if(this.socketOptions != null && typeof this.socketOptions === 'object') { + var keys = Object.keys(this.socketOptions); + for(var j = 0; j < keys.length;j++) socketOptions[keys[j]] = this.socketOptions[keys[j]]; + } + + // If ssl is specified + if(replSetSelf.ssl) serverConnections[i].ssl = true; + + // Add host information to socket options + socketOptions['host'] = serverConnections[i].host; + socketOptions['port'] = serverConnections[i].port; + + // Set the socket options + serverConnections[i].socketOptions = socketOptions; + // Set the replicaset instance + serverConnections[i].replicasetInstance = replSetSelf; + // Connect to server + serverConnections[i].connect(parent, {returnIsMasterResults: true, eventReceiver:serverConnections[i]}, connectionHandler(serverConnections[i])); + } + + // Check if we have an error in the inital set of servers and callback with error + if(replSetSelf._state.errorMessages.length > 0 && typeof callback === 'function') { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(replSetSelf._state.errorMessages[0], null); + } +} + +ReplSetServers.prototype.checkoutWriter = function() { + // Establish connection + var connection = this._state.master != null ? this._state.master.checkoutWriter() : null; + // Return the connection + return connection; +} + +ReplSetServers.prototype.checkoutReader = function() { + var connection = null; + // If we have specified to read from a secondary server grab a random one and read + // from it, otherwise just pass the primary connection + if((this.readSecondary == true || this._readPreference == Server.READ_SECONDARY || this._readPreference == Server.READ_SECONDARY_ONLY) && Object.keys(this._state.secondaries).length > 0) { + // Checkout a secondary server from the passed in set of servers + if(this.strategyInstance != null) { + connection = this.strategyInstance.checkoutSecondary(); + } else { + // Pick a random key + var keys = Object.keys(this._state.secondaries); + var key = keys[Math.floor(Math.random() * keys.length)]; + connection = this._state.secondaries[key].checkoutReader(); + } + } else if(this._readPreference == Server.READ_SECONDARY_ONLY && Object.keys(this._state.secondaries).length == 0) { + connection = null; + } else if(this._readPreference != null && typeof this._readPreference === 'object') { + // Get all tag keys (used to try to find a server that is valid) + var keys = Object.keys(this._readPreference); + // final instance server + var instanceServer = null; + // for each key look for an avilable instance + for(var i = 0; i < keys.length; i++) { + // Grab subkey value + var value = this._readPreference[keys[i]]; + + // Check if we have any servers for the tag, if we do pick a random one + if(this._state.byTags[keys[i]] != null + && this._state.byTags[keys[i]][value] != null + && Array.isArray(this._state.byTags[keys[i]][value]) + && this._state.byTags[keys[i]][value].length > 0) { + // Let's grab an available server from the list using a random pick + var serverInstances = this._state.byTags[keys[i]][value]; + // Set instance to return + instanceServer = serverInstances[Math.floor(Math.random() * serverInstances.length)]; + break; + } + } + + // Return the instance of the server + connection = instanceServer != null ? instanceServer.checkoutReader() : this.checkoutWriter(); + } else { + connection = this.checkoutWriter(); + } + // Return the connection + return connection; +} + +ReplSetServers.prototype.allRawConnections = function() { + // Neeed to build a complete list of all raw connections, start with master server + var allConnections = []; + // Get connection object + var allMasterConnections = this._state.master.connectionPool.getAllConnections(); + // Add all connections to list + allConnections = allConnections.concat(allMasterConnections); + + // If we have read secondary let's add all secondary servers + if(this.readSecondary && Object.keys(this._state.secondaries).length > 0) { + // Get all the keys + var keys = Object.keys(this._state.secondaries); + // For each of the secondaries grab the connections + for(var i = 0; i < keys.length; i++) { + // Get connection object + var secondaryPoolConnections = this._state.secondaries[keys[i]].connectionPool.getAllConnections(); + // Add all connections to list + allConnections = allConnections.concat(secondaryPoolConnections); + } + } + + // Return all the conections + return allConnections; +} + +ReplSetServers.prototype.enableRecordQueryStats = function(enable) { + // Set the global enable record query stats + this.recordQueryStats = enable; + // Ensure all existing servers already have the flag set, even if the + // connections are up already or we have not connected yet + if(this._state != null && this._state.addresses != null) { + var keys = Object.keys(this._state.addresses); + // Iterate over all server instances and set the enableRecordQueryStats flag + for(var i = 0; i < keys.length; i++) { + this._state.addresses[keys[i]].enableRecordQueryStats(enable); + } + } else if(Array.isArray(this.servers)) { + for(var i = 0; i < this.servers.length; i++) { + this.servers[i].enableRecordQueryStats(enable); + } + } +} + +ReplSetServers.prototype.disconnect = function(callback) { + this.close(callback); +} + +ReplSetServers.prototype.close = function(callback) { + var self = this; + // Set server status as disconnected + this._serverState = 'disconnected'; + // Get all the server instances and close them + var allServers = []; + // Make sure we have servers + if(this._state['addresses'] != null) { + var keys = Object.keys(this._state.addresses); + for(var i = 0; i < keys.length; i++) { + allServers.push(this._state.addresses[keys[i]]); + } + } + + // Let's process all the closing + var numberOfServersToClose = allServers.length; + + // Remove all the listeners + self.removeAllListeners(); + + // Special case where there are no servers + if(allServers.length == 0 && typeof callback === 'function') return callback(null, null); + + // Close the servers + for(var i = 0; i < allServers.length; i++) { + var server = allServers[i]; + if(server.isConnected()) { + // Close each server + server.close(function() { + numberOfServersToClose = numberOfServersToClose - 1; + // Clear out state if we are done + if(numberOfServersToClose == 0) { + // Clear out state + self._state = {'master':null, 'secondaries':{}, 'arbiters':{}, 'passives':{}, 'errors':{}, 'addresses':{}, 'byTags':{}, 'setName':null, 'errorMessages':[], 'members':[]}; + } + + // If we are finished perform the call back + if(numberOfServersToClose == 0 && typeof callback === 'function') { + callback(null); + } + }) + } else { + numberOfServersToClose = numberOfServersToClose - 1; + // If we have no more servers perform the callback + if(numberOfServersToClose == 0 && typeof callback === 'function') { + callback(null); + } + } + } +}
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/connection/server.js b/node_modules/mongodb/lib/mongodb/connection/server.js new file mode 100644 index 0000000..80c3b27 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/server.js @@ -0,0 +1,640 @@ +var Connection = require('./connection').Connection, + DbCommand = require('../commands/db_command').DbCommand, + MongoReply = require('../responses/mongo_reply').MongoReply, + ConnectionPool = require('./connection_pool').ConnectionPool, + EventEmitter = require('events').EventEmitter, + MongoReply = require("../responses/mongo_reply").MongoReply, + inherits = require('util').inherits; + +var Server = exports.Server = function(host, port, options) { + // Set up event emitter + EventEmitter.call(this); + // Set up Server instance + if(!(this instanceof Server)) return new Server(host, port, options); + + var self = this; + this.host = host; + this.port = port; + this.options = options == null ? {} : options; + this.internalConnection; + this.internalMaster = false; + this.connected = false; + this.poolSize = this.options.poolSize == null ? 1 : this.options.poolSize; + this.ssl = this.options.ssl == null ? false : this.options.ssl; + this.slaveOk = this.options["slave_ok"]; + this._used = false; + + // Get the readPreference + var readPreference = this.options['readPreference']; + // Read preference setting + if(readPreference != null) { + if(readPreference != Server.READ_PRIMARY && readPreference != Server.READ_SECONDARY_ONLY + && readPreference != Server.READ_SECONDARY) { + throw new Error("Illegal readPreference mode specified, " + readPreference); + } + + // Set read Preference + this._readPreference = readPreference; + } else { + this._readPreference = null; + } + + // Contains the isMaster information returned from the server + this.isMasterDoc; + + // Set default connection pool options + this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {}; + // Set ssl up if it's defined + if(this.ssl) { + this.socketOptions.ssl = true; + } + + // Set up logger if any set + this.logger = this.options.logger != null + && (typeof this.options.logger.debug == 'function') + && (typeof this.options.logger.error == 'function') + && (typeof this.options.logger.debug == 'function') + ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}}; + + // Just keeps list of events we allow + this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[]}; + // Internal state of server connection + this._serverState = 'disconnected'; + // this._timeout = false; + // Contains state information about server connection + this._state = {'runtimeStats': {'queryStats':new RunningStats()}}; + // Do we record server stats or not + this.recordQueryStats = false; + + // Setters and getters + Object.defineProperty(this, "autoReconnect", { enumerable: true + , get: function () { + return this.options['auto_reconnect'] == null ? false : this.options['auto_reconnect']; + } + }); + + Object.defineProperty(this, "connection", { enumerable: true + , get: function () { + return this.internalConnection; + } + , set: function(connection) { + this.internalConnection = connection; + } + }); + + Object.defineProperty(this, "master", { enumerable: true + , get: function () { + return this.internalMaster; + } + , set: function(value) { + this.internalMaster = value; + } + }); + + Object.defineProperty(this, "primary", { enumerable: true + , get: function () { + return this; + } + }); + + // Getter for query Stats + Object.defineProperty(this, "queryStats", { enumerable: true + , get: function () { + return this._state.runtimeStats.queryStats; + } + }); + + Object.defineProperty(this, "runtimeStats", { enumerable: true + , get: function () { + return this._state.runtimeStats; + } + }); + + // Get Read Preference method + Object.defineProperty(this, "readPreference", { enumerable: true + , get: function () { + if(this._readPreference == null && this.readSecondary) { + return Server.READ_SECONDARY; + } else if(this._readPreference == null && !this.readSecondary) { + return Server.READ_PRIMARY; + } else { + return this._readPreference; + } + } + }); +}; + +// Inherit simple event emitter +inherits(Server, EventEmitter); +// Read Preferences +Server.READ_PRIMARY = 'primary'; +Server.READ_SECONDARY = 'secondary'; +Server.READ_SECONDARY_ONLY = 'secondaryOnly'; + +// Always ourselves +Server.prototype.setReadPreference = function() {} + +// Return the used state +Server.prototype._isUsed = function() { + return this._used; +} + +// Server close function +Server.prototype.close = function(callback) { + // Remove all local listeners + this.removeAllListeners(); + + if(this.connectionPool != null) { + // Remove all the listeners on the pool so it does not fire messages all over the place + this.connectionPool.removeAllEventListeners(); + // Close the connection if it's open + this.connectionPool.stop(); + } + + // Set server status as disconnected + this._serverState = 'disconnected'; + // Peform callback if present + if(typeof callback === 'function') callback(); +}; + +Server.prototype.isConnected = function() { + return this.connectionPool != null && this.connectionPool.isConnected(); +} + +Server.prototype.allServerInstances = function() { + return [this]; +} + +Server.prototype.isSetMember = function() { + return this['replicasetInstance'] != null; +} + +Server.prototype.connect = function(dbInstance, options, callback) { + if('function' === typeof options) callback = options, options = {}; + if(options == null) options = {}; + if(!('function' === typeof callback)) callback = null; + + // Currently needed to work around problems with multiple connections in a pool with ssl + // TODO fix if possible + if(this.ssl == true) { + // Set up socket options for ssl + this.socketOptions.ssl = true; + } + + // Let's connect + var server = this; + // Let's us override the main receiver of events + var eventReceiver = options.eventReceiver != null ? options.eventReceiver : this; + // Creating dbInstance + this.dbInstance = dbInstance; + // Save reference to dbInstance + this.dbInstances = [dbInstance]; + + // Set server state to connecting + this._serverState = 'connecting'; + // Ensure dbInstance can do a slave query if it's set + dbInstance.slaveOk = this.slaveOk ? this.slaveOk : dbInstance.slaveOk; + // Create connection Pool instance with the current BSON serializer + var connectionPool = new ConnectionPool(this.host, this.port, this.poolSize, dbInstance.bson, this.socketOptions); + // Set logger on pool + connectionPool.logger = this.logger; + + // Set up a new pool using default settings + server.connectionPool = connectionPool; + + // Set basic parameters passed in + var returnIsMasterResults = options.returnIsMasterResults == null ? false : options.returnIsMasterResults; + + // Create a default connect handler, overriden when using replicasets + var connectCallback = function(err, reply) { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // If something close down the connection and removed the callback before + // proxy killed connection etc, ignore the erorr as close event was isssued + if(err != null && internalCallback == null) return; + // Internal callback + if(err != null) return internalCallback(err, null); + server.master = reply.documents[0].ismaster == 1 ? true : false; + server.connectionPool.setMaxBsonSize(reply.documents[0].maxBsonObjectSize); + // Set server as connected + server.connected = true; + // Save document returned so we can query it + server.isMasterDoc = reply.documents[0]; + + // If we have it set to returnIsMasterResults + if(returnIsMasterResults) { + internalCallback(null, reply); + } else { + internalCallback(null, dbInstance); + } + }; + + // Let's us override the main connect callback + var connectHandler = options.connectHandler == null ? connectCallback : options.connectHandler; + + // Set up on connect method + connectionPool.on("poolReady", function() { + // Create db command and Add the callback to the list of callbacks by the request id (mapping outgoing messages to correct callbacks) + var db_command = DbCommand.NcreateIsMasterCommand(dbInstance, dbInstance.databaseName); + // Check out a reader from the pool + var connection = connectionPool.checkoutConnection(); + // Set server state to connected + server._serverState = 'connected'; + + // Register handler for messages + dbInstance._registerHandler(db_command, false, connection, connectHandler); + + // Write the command out + connection.write(db_command); + }) + + // Set up item connection + connectionPool.on("message", function(message) { + // Attempt to parse the message + try { + // Create a new mongo reply + var mongoReply = new MongoReply() + // Parse the header + mongoReply.parseHeader(message, connectionPool.bson) + // If message size is not the same as the buffer size + // something went terribly wrong somewhere + if(mongoReply.messageLength != message.length) { + // Emit the error + eventReceiver.emit("error", new Error("bson length is different from message length"), server); + // Remove all listeners + server.removeAllListeners(); + } else { + var startDate = new Date().getTime(); + + // Callback instance + var callbackInfo = null; + var dbInstanceObject = null; + + // Locate a callback instance and remove any additional ones + for(var i = 0; i < server.dbInstances.length; i++) { + var dbInstanceObjectTemp = server.dbInstances[i]; + var hasHandler = dbInstanceObjectTemp._hasHandler(mongoReply.responseTo.toString()); + // Assign the first one we find and remove any duplicate ones + if(hasHandler && callbackInfo == null) { + callbackInfo = dbInstanceObjectTemp._findHandler(mongoReply.responseTo.toString()); + dbInstanceObject = dbInstanceObjectTemp; + } else if(hasHandler) { + dbInstanceObjectTemp._removeHandler(mongoReply.responseTo.toString()); + } + } + + // Only execute callback if we have a caller + if(callbackInfo.callback && Array.isArray(callbackInfo.info.chained)) { + // Check if callback has already been fired (missing chain command) + var chained = callbackInfo.info.chained; + var numberOfFoundCallbacks = 0; + for(var i = 0; i < chained.length; i++) { + if(dbInstanceObject._hasHandler(chained[i])) numberOfFoundCallbacks++; + } + + // If we have already fired then clean up rest of chain and move on + if(numberOfFoundCallbacks != chained.length) { + for(var i = 0; i < chained.length; i++) { + dbInstanceObject._removeHandler(chained[i]); + } + + // Just return from function + return; + } + + // Parse the body + mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) { + var callbackInfo = dbInstanceObject._findHandler(mongoReply.responseTo.toString()); + // If we have an error let's execute the callback and clean up all other + // chained commands + var firstResult = mongoReply && mongoReply.documents; + // Check for an error, if we have one let's trigger the callback and clean up + // The chained callbacks + if(firstResult[0].err != null || firstResult[0].errmsg != null) { + // Trigger the callback for the error + dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null); + } else { + var chainedIds = callbackInfo.info.chained; + + if(chainedIds.length > 0 && chainedIds[chainedIds.length - 1] == mongoReply.responseTo) { + // Cleanup all other chained calls + chainedIds.pop(); + // Remove listeners + for(var i = 0; i < chainedIds.length; i++) dbInstanceObject._removeHandler(chainedIds[i]); + // Call the handler + dbInstanceObject._callHandler(mongoReply.responseTo, callbackInfo.info.results.shift(), null); + } else{ + // Add the results to all the results + for(var i = 0; i < chainedIds.length; i++) { + var handler = dbInstanceObject._findHandler(chainedIds[i]); + // Check if we have an object, if it's the case take the current object commands and + // and add this one + if(handler.info != null) { + handler.info.results = Array.isArray(callbackInfo.info.results) ? callbackInfo.info.results : []; + handler.info.results.push(mongoReply); + } + } + } + } + }); + } else if(callbackInfo.callback) { + // Parse the body + mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) { + // Let's record the stats info if it's enabled + if(server.recordQueryStats == true && server._state['runtimeStats'] != null + && server._state.runtimeStats['queryStats'] instanceof RunningStats) { + // Add data point to the running statistics object + server._state.runtimeStats.queryStats.push(new Date().getTime() - callbackInfo.info.start); + } + + // Trigger the callback + dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null); + }); + } + } + } catch (err) { + // Throw error in next tick + process.nextTick(function() { + throw err; + }) + } + }); + + // Handle timeout + connectionPool.on("timeout", function(err) { + // If pool connection is already closed + if(server._serverState === 'disconnected') return; + // Set server state to disconnected + server._serverState = 'disconnected'; + // Close the pool + connectionPool.stop(); + // If we have a callback return the error + if(typeof callback === 'function') { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(err, null); + } else if(server.isSetMember()) { + server.emit("timeout", err, server); + } else { + eventReceiver.emit("timeout", err, server); + } + + // If we are a single server connection fire errors correctly + if(!server.isSetMember()) { + // Fire all callback errors + _fireCallbackErrors(server, err); + // Emit error + _emitAcrossAllDbInstances(server, eventReceiver, "timeout", err, server); + } + }); + + // Handle errors + connectionPool.on("error", function(message) { + // If pool connection is already closed + if(server._serverState === 'disconnected') return; + // Set server state to disconnected + server._serverState = 'disconnected'; + // Close the pool + connectionPool.stop(); + // If we have a callback return the error + if(typeof callback === 'function') {// && !server.isSetMember()) { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(new Error(message && message.err ? message.err : message), null); + } else if(server.isSetMember()) { + server.emit("error", new Error(message && message.err ? message.err : message), server); + } else { + eventReceiver.emit("error", new Error(message && message.err ? message.err : message), server); + } + + // If we are a single server connection fire errors correctly + if(!server.isSetMember()) { + // Fire all callback errors + _fireCallbackErrors(server, new Error(message && message.err ? message.err : message)); + // Emit error + _emitAcrossAllDbInstances(server, eventReceiver, "error", new Error(message && message.err ? message.err : message), server); + } + }); + + // Handle close events + connectionPool.on("close", function() { + // If pool connection is already closed + if(server._serverState === 'disconnected') return; + // Set server state to disconnected + server._serverState = 'disconnected'; + // Close the pool + connectionPool.stop(true); + // If we have a callback return the error + if(typeof callback == 'function') { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(new Error("connection closed"), null); + } else if(server.isSetMember()) { + server.emit("close", new Error("connection closed"), server); + } else { + eventReceiver.emit("close", new Error("connection closed"), server); + } + + // If we are a single server connection fire errors correctly + if(!server.isSetMember()) { + // Fire all callback errors + _fireCallbackErrors(server, new Error("connection closed")); + // Emit error + _emitAcrossAllDbInstances(server, eventReceiver, "close", server); + } + }); + + // If we have a parser error we are in an unknown state, close everything and emit + // error + connectionPool.on("parseError", function(message) { + // If pool connection is already closed + if(server._serverState === 'disconnected') return; + // Set server state to disconnected + server._serverState = 'disconnected'; + // Close the pool + connectionPool.stop(); + // If we have a callback return the error + if(typeof callback === 'function') { + // ensure no callbacks get called twice + var internalCallback = callback; + callback = null; + // Perform callback + internalCallback(new Error("connection closed due to parseError"), null); + } else if(server.isSetMember()) { + server.emit("parseError", new Error("connection closed due to parseError"), server); + } else { + eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server); + } + + // If we are a single server connection fire errors correctly + if(!server.isSetMember()) { + // Fire all callback errors + _fireCallbackErrors(server, new Error("connection closed due to parseError")); + // Emit error + _emitAcrossAllDbInstances(server, eventReceiver, "parseError", server); + } + }); + + // Boot up connection poole, pass in a locator of callbacks + connectionPool.start(); +} + +// Fire all the errors +var _fireCallbackErrors = function(server, err) { + // Locate all the possible callbacks that need to return + for(var i = 0; i < server.dbInstances.length; i++) { + // Fetch the db Instance + var dbInstance = server.dbInstances[i]; + // Check all callbacks + var keys = Object.keys(dbInstance._callBackStore._notReplied); + // For each key check if it's a callback that needs to be returned + for(var j = 0; j < keys.length; j++) { + var info = dbInstance._callBackStore._notReplied[keys[j]]; + if(info.connection.socketOptions.host === server.host && info.connection.socketOptions.port === server.port) { + dbInstance._callBackStore.emit(keys[j], err, null); + } + } + } +} + +var _emitAcrossAllDbInstances = function(server, filterDb, event, message, object) { + // Emit close event across all db instances sharing the sockets + var allServerInstances = server.allServerInstances(); + // Fetch the first server instance + var serverInstance = allServerInstances[0]; + // For all db instances signal all db instances + if(Array.isArray(serverInstance.dbInstances) && serverInstance.dbInstances.length > 1) { + for(var i = 0; i < serverInstance.dbInstances.length; i++) { + var dbInstance = serverInstance.dbInstances[i]; + // Check if it's our current db instance and skip if it is + if(filterDb == null || filterDb.databaseName !== dbInstance.databaseName || filterDb.tag !== dbInstance.tag) { + dbInstance.emit(event, message, object); + } + } + } +} + +Server.prototype.allRawConnections = function() { + return this.connectionPool.getAllConnections(); +} + +// Check if a writer can be provided +var canCheckoutWriter = function(self, read) { + // We cannot write to an arbiter or secondary server + if(self.isMasterDoc['arbiterOnly'] == true) { + return new Error("Cannot write to an arbiter"); + } if(self.isMasterDoc['secondary'] == true) { + return new Error("Cannot write to a secondary"); + } else if(read == true && self._readPreference == Server.READ_SECONDARY_ONLY && self.isMasterDoc['ismaster'] == true) { + return new Error("Cannot read from primary when secondary only specified"); + } + + // Return no error + return null; +} + +Server.prototype.checkoutWriter = function(read) { + if(read == true) return this.connectionPool.checkoutConnection(); + // Check if are allowed to do a checkout (if we try to use an arbiter f.ex) + var result = canCheckoutWriter(this, read); + // If the result is null check out a writer + if(result == null) { + return this.connectionPool.checkoutConnection(); + } else { + return result; + } +} + +// Check if a reader can be provided +var canCheckoutReader = function(self) { + // We cannot write to an arbiter or secondary server + if(self.isMasterDoc['arbiterOnly'] == true) { + return new Error("Cannot write to an arbiter"); + } else if(self._readPreference != null) { + // If the read preference is Primary and the instance is not a master return an error + if(self._readPreference == Server.READ_PRIMARY && self.isMasterDoc['ismaster'] != true) { + return new Error("Read preference is " + Server.READ_PRIMARY + " and server is not master"); + } else if(self._readPreference == Server.READ_SECONDARY_ONLY && self.isMasterDoc['ismaster'] == true) { + return new Error("Cannot read from primary when secondary only specified"); + } + } + + // Return no error + return null; +} + +Server.prototype.checkoutReader = function() { + // Check if are allowed to do a checkout (if we try to use an arbiter f.ex) + var result = canCheckoutReader(this); + // If the result is null check out a writer + if(result == null) { + return this.connectionPool.checkoutConnection(); + } else { + return result; + } +} + +Server.prototype.enableRecordQueryStats = function(enable) { + this.recordQueryStats = enable; +} + +// +// Internal statistics object used for calculating average and standard devitation on +// running queries +var RunningStats = function() { + var self = this; + this.m_n = 0; + this.m_oldM = 0.0; + this.m_oldS = 0.0; + this.m_newM = 0.0; + this.m_newS = 0.0; + + // Define getters + Object.defineProperty(this, "numDataValues", { enumerable: true + , get: function () { return this.m_n; } + }); + + Object.defineProperty(this, "mean", { enumerable: true + , get: function () { return (this.m_n > 0) ? this.m_newM : 0.0; } + }); + + Object.defineProperty(this, "variance", { enumerable: true + , get: function () { return ((this.m_n > 1) ? this.m_newS/(this.m_n - 1) : 0.0); } + }); + + Object.defineProperty(this, "standardDeviation", { enumerable: true + , get: function () { return Math.sqrt(this.variance); } + }); + + Object.defineProperty(this, "sScore", { enumerable: true + , get: function () { + var bottom = this.mean + this.standardDeviation; + if(bottom == 0) return 0; + return ((2 * this.mean * this.standardDeviation)/(bottom)); + } + }); +} + +RunningStats.prototype.push = function(x) { + // Update the number of samples + this.m_n = this.m_n + 1; + // See Knuth TAOCP vol 2, 3rd edition, page 232 + if(this.m_n == 1) { + this.m_oldM = this.m_newM = x; + this.m_oldS = 0.0; + } else { + this.m_newM = this.m_oldM + (x - this.m_oldM) / this.m_n; + this.m_newS = this.m_oldS + (x - this.m_oldM) * (x - this.m_newM); + + // set up for next iteration + this.m_oldM = this.m_newM; + this.m_oldS = this.m_newS; + } +} diff --git a/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js b/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js new file mode 100644 index 0000000..6bb36cf --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js @@ -0,0 +1,125 @@ +var Server = require("../server").Server; + +// The ping strategy uses pings each server and records the +// elapsed time for the server so it can pick a server based on lowest +// return time for the db command {ping:true} +var PingStrategy = exports.PingStrategy = function(replicaset) { + this.replicaset = replicaset; + this.state = 'disconnected'; + // Class instance + this.Db = require("../../db").Db; +} + +// Starts any needed code +PingStrategy.prototype.start = function(callback) { + this.state = 'connected'; + // Start ping server + this._pingServer(callback); +} + +// Stops and kills any processes running +PingStrategy.prototype.stop = function(callback) { + // Stop the ping process + this.state = 'disconnected'; + // Call the callback + callback(null, null); +} + +PingStrategy.prototype.checkoutSecondary = function() { + // Get all secondary server keys + var keys = Object.keys(this.replicaset._state.secondaries); + // Contains the picked instance + var minimumPingMs = null; + var selectedInstance = null; + // Pick server key by the lowest ping time + for(var i = 0; i < keys.length; i++) { + // Fetch a server + var server = this.replicaset._state.secondaries[keys[i]]; + // If we don't have a ping time use it + if(server.runtimeStats['pingMs'] == null) { + // Set to 0 ms for the start + server.runtimeStats['pingMs'] = 0; + // Pick server + selectedInstance = server; + break; + } else { + // If the next server's ping time is less than the current one choose than one + if(minimumPingMs == null || server.runtimeStats['pingMs'] < minimumPingMs) { + minimumPingMs = server.runtimeStats['pingMs']; + selectedInstance = server; + } + } + } + + // Return the selected instance + return selectedInstance != null ? selectedInstance.checkoutReader() : null; +} + +PingStrategy.prototype._pingServer = function(callback) { + var self = this; + + // Ping server function + var pingFunction = function() { + if(self.state == 'disconnected') return; + var addresses = self.replicaset._state != null && self.replicaset._state.addresses != null ? self.replicaset._state.addresses : null; + // Grab all servers + var serverKeys = Object.keys(addresses); + // Number of server entries + var numberOfEntries = serverKeys.length; + // We got keys + for(var i = 0; i < serverKeys.length; i++) { + // We got a server instance + var server = addresses[serverKeys[i]]; + // Create a new server object, avoid using internal connections as they might + // be in an illegal state + new function(serverInstance) { + var server = new Server(serverInstance.host, serverInstance.port, {poolSize:1, timeout:500}); + var db = new self.Db(self.replicaset.db.databaseName, server); + // Add error listener + db.on("error", function(err) { + // Adjust the number of checks + numberOfEntries = numberOfEntries - 1; + // Close connection + db.close(); + // If we are done with all results coming back trigger ping again + if(numberOfEntries == 0 && self.state == 'connected') { + setTimeout(pingFunction, 1000); + } + }) + + // Open the db instance + db.open(function(err, p_db) { + if(err != null) { + db.close(); + } else { + // Startup time of the command + var startTime = new Date().getTime(); + // Execute ping on this connection + p_db.executeDbCommand({ping:1}, function(err, result) { + // Adjust the number of checks + numberOfEntries = numberOfEntries - 1; + // Get end time of the command + var endTime = new Date().getTime(); + // Store the ping time in the server instance state variable, if there is one + if(serverInstance != null && serverInstance.runtimeStats != null && serverInstance.isConnected()) { + serverInstance.runtimeStats['pingMs'] = (endTime - startTime); + } + + // Close server + p_db.close(); + // If we are done with all results coming back trigger ping again + if(numberOfEntries == 0 && self.state == 'connected') { + setTimeout(pingFunction, 1000); + } + }) + } + }) + }(server); + } + } + + // Start pingFunction + setTimeout(pingFunction, 1000); + // Do the callback + callback(null); +} diff --git a/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js b/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js new file mode 100644 index 0000000..0c8b1c0 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js @@ -0,0 +1,40 @@ +// The Statistics strategy uses the measure of each end-start time for each +// query executed against the db to calculate the mean, variance and standard deviation +// and pick the server which the lowest mean and deviation +var StatisticsStrategy = exports.StatisticsStrategy = function(replicaset) { + this.replicaset = replicaset; +} + +// Starts any needed code +StatisticsStrategy.prototype.start = function(callback) { + callback(null, null); +} + +StatisticsStrategy.prototype.stop = function(callback) { + // Remove reference to replicaset + this.replicaset = null; + // Perform callback + callback(null, null); +} + +StatisticsStrategy.prototype.checkoutSecondary = function() { + // Get all secondary server keys + var keys = Object.keys(this.replicaset._state.secondaries); + // Contains the picked instance + var minimumSscore = null; + var selectedInstance = null; + + // Pick server key by the lowest ping time + for(var i = 0; i < keys.length; i++) { + // Fetch a server + var server = this.replicaset._state.secondaries[keys[i]]; + // Pick server by lowest Sscore + if(minimumSscore == null || (server.queryStats.sScore < minimumSscore)) { + minimumSscore = server.queryStats.sScore; + selectedInstance = server; + } + } + + // Return the selected instance + return selectedInstance != null ? selectedInstance.checkoutReader() : null; +} diff --git a/node_modules/mongodb/lib/mongodb/cursor.js b/node_modules/mongodb/lib/mongodb/cursor.js new file mode 100644 index 0000000..28c9da4 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/cursor.js @@ -0,0 +1,702 @@ +var QueryCommand = require('./commands/query_command').QueryCommand, + GetMoreCommand = require('./commands/get_more_command').GetMoreCommand, + KillCursorCommand = require('./commands/kill_cursor_command').KillCursorCommand, + Long = require('bson').Long, + CursorStream = require('./cursorstream'), + utils = require('./utils'); + +/** + * Constructor for a cursor object that handles all the operations on query result + * using find. This cursor object is unidirectional and cannot traverse backwards. Clients should not be creating a cursor directly, + * but use find to acquire a cursor. + * + * @class Represents a Cursor. + * @param {Db} db the database object to work with. + * @param {Collection} collection the collection to query. + * @param {Object} selector the query selector. + * @param {Object} fields an object containing what fields to include or exclude from objects returned. + * @param {Number} skip number of documents to skip. + * @param {Number} limit the number of results to return. -1 has a special meaning and is used by Db.eval. A value of 1 will also be treated as if it were -1. + * @param {String|Array|Object} sort the required sorting for the query. + * @param {Object} hint force the query to use a specific index. + * @param {Boolean} explain return the explaination of the query. + * @param {Boolean} snapshot Snapshot mode assures no duplicates are returned. + * @param {Boolean} timeout allow the query to timeout. + * @param {Boolean} tailable allow the cursor to be tailable. + * @param {Number} batchSize the number of the subset of results to request the database to return for every request. This should initially be greater than 1 otherwise the database will automatically close the cursor. The batch size can be set to 1 with cursorInstance.batchSize after performing the initial query to the database. + * @param {Boolean} raw return all query documents as raw buffers (default false). + * @param {Boolean} read specify override of read from source (primary/secondary). + * @param {Boolean} returnKey only return the index key. + * @param {Number} maxScan limit the number of items to scan. + * @param {Number} min set index bounds. + * @param {Number} max set index bounds. + * @param {Boolean} showDiskLoc show disk location of results. + * @param {String} comment you can put a $comment field on a query to make looking in the profiler logs simpler. + */ +function Cursor(db, collection, selector, fields, skip, limit + , sort, hint, explain, snapshot, timeout, tailable, batchSize, slaveOk, raw, read + , returnKey, maxScan, min, max, showDiskLoc, comment) { + this.db = db; + this.collection = collection; + this.selector = selector; + this.fields = fields; + this.skipValue = skip == null ? 0 : skip; + this.limitValue = limit == null ? 0 : limit; + this.sortValue = sort; + this.hint = hint; + this.explainValue = explain; + this.snapshot = snapshot; + this.timeout = timeout == null ? true : timeout; + this.tailable = tailable; + this.batchSizeValue = batchSize == null ? 0 : batchSize; + this.slaveOk = slaveOk == null ? collection.slaveOk : slaveOk; + this.raw = raw == null ? false : raw; + this.read = read == null ? true : read; + this.returnKey = returnKey; + this.maxScan = maxScan; + this.min = min; + this.max = max; + this.showDiskLoc = showDiskLoc; + this.comment = comment; + + this.totalNumberOfRecords = 0; + this.items = []; + this.cursorId = Long.fromInt(0); + + // State variables for the cursor + this.state = Cursor.INIT; + // Keep track of the current query run + this.queryRun = false; + this.getMoreTimer = false; + this.collectionName = (this.db.databaseName ? this.db.databaseName + "." : '') + this.collection.collectionName; +}; + +/** + * Resets this cursor to its initial state. All settings like the query string, + * tailable, batchSizeValue, skipValue and limits are preserved. + * + * @return {Cursor} returns itself with rewind applied. + * @api public + */ +Cursor.prototype.rewind = function() { + var self = this; + + if (self.state != Cursor.INIT) { + if (self.state != Cursor.CLOSED) { + self.close(function() {}); + } + + self.numberOfReturned = 0; + self.totalNumberOfRecords = 0; + self.items = []; + self.cursorId = Long.fromInt(0); + self.state = Cursor.INIT; + self.queryRun = false; + } + + return self; +}; + + +/** + * Returns an array of documents. The caller is responsible for making sure that there + * is enough memory to store the results. Note that the array only contain partial + * results when this cursor had been previouly accessed. In that case, + * cursor.rewind() can be used to reset the cursor. + * + * @param {Function} callback This will be called after executing this method successfully. The first paramter will contain the Error object if an error occured, or null otherwise. The second paramter will contain an array of BSON deserialized objects as a result of the query. + * @return {null} + * @api public + */ +Cursor.prototype.toArray = function(callback) { + var self = this; + + if(!callback) { + throw new Error('callback is mandatory'); + } + + if(this.tailable) { + callback(new Error("Tailable cursor cannot be converted to array"), null); + } else if(this.state != Cursor.CLOSED) { + var items = []; + + this.each(function(err, item) { + if(err != null) return callback(err, null); + + if (item != null) { + items.push(item); + } else { + callback(err, items); + items = null; + self.items = []; + } + }); + } else { + callback(new Error("Cursor is closed"), null); + } +}; + +/** + * Iterates over all the documents for this cursor. As with **{cursor.toArray}**, + * not all of the elements will be iterated if this cursor had been previouly accessed. + * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike + * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements + * at any given time if batch size is specified. Otherwise, the caller is responsible + * for making sure that the entire result can fit the memory. + * + * @param {Function} callback this will be called for while iterating every document of the query result. The first paramter will contain the Error object if an error occured, or null otherwise. While the second paramter will contain the document. + * @return {null} + * @api public + */ +Cursor.prototype.each = function(callback) { + var self = this; + + if (!callback) { + throw new Error('callback is mandatory'); + } + + if(this.state != Cursor.CLOSED) { + //FIX: stack overflow (on deep callback) (cred: https://github.com/limp/node-mongodb-native/commit/27da7e4b2af02035847f262b29837a94bbbf6ce2) + process.nextTick(function(){ + // Fetch the next object until there is no more objects + self.nextObject(function(err, item) { + if(err != null) return callback(err, null); + + if(item != null) { + callback(null, item); + self.each(callback); + } else { + // Close the cursor if done + self.state = Cursor.CLOSED; + callback(err, null); + } + + item = null; + }); + }); + } else { + callback(new Error("Cursor is closed"), null); + } +}; + +/** + * Determines how many result the query for this cursor will return + * + * @param {Function} callback this will be after executing this method. The first paramter will contain the Error object if an error occured, or null otherwise. While the second paramter will contain the number of results or null if an error occured. + * @return {null} + * @api public + */ +Cursor.prototype.count = function(callback) { + this.collection.count(this.selector, callback); +}; + +/** + * Sets the sort parameter of this cursor to the given value. + * + * This method has the following method signatures: + * (keyOrList, callback) + * (keyOrList, direction, callback) + * + * @param {String|Array|Object} keyOrList This can be a string or an array. If passed as a string, the string will be the field to sort. If passed an array, each element will represent a field to be sorted and should be an array that contains the format [string, direction]. + * @param {String|Number} direction this determines how the results are sorted. "asc", "ascending" or 1 for asceding order while "desc", "desceding or -1 for descending order. Note that the strings are case insensitive. + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution. + * @return {Cursor} an instance of this object. + * @api public + */ +Cursor.prototype.sort = function(keyOrList, direction, callback) { + callback = callback || function(){}; + if(typeof direction === "function") { callback = direction; direction = null; } + + if(this.tailable) { + callback(new Error("Tailable cursor doesn't support sorting"), null); + } else if(this.queryRun == true || this.state == Cursor.CLOSED) { + callback(new Error("Cursor is closed"), null); + } else { + var order = keyOrList; + + if(direction != null) { + order = [[keyOrList, direction]]; + } + + this.sortValue = order; + callback(null, this); + } + return this; +}; + +/** + * Sets the limit parameter of this cursor to the given value. + * + * @param {Number} limit the new limit. + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the limit given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution. + * @return {Cursor} an instance of this object. + * @api public + */ +Cursor.prototype.limit = function(limit, callback) { + callback = callback || function(){}; + + if(this.tailable) { + callback(new Error("Tailable cursor doesn't support limit"), null); + } else if(this.queryRun == true || this.state == Cursor.CLOSED) { + callback(new Error("Cursor is closed"), null); + } else { + if(limit != null && limit.constructor != Number) { + callback(new Error("limit requires an integer"), null); + } else { + this.limitValue = limit; + callback(null, this); + } + } + + return this; +}; + +/** + * Sets the skip parameter of this cursor to the given value. + * + * @param {Number} skip the new skip value. + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the skip value given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution. + * @return {Cursor} an instance of this object. + * @api public + */ +Cursor.prototype.skip = function(skip, callback) { + callback = callback || function(){}; + + if(this.tailable) { + callback(new Error("Tailable cursor doesn't support skip"), null); + } else if(this.queryRun == true || this.state == Cursor.CLOSED) { + callback(new Error("Cursor is closed"), null); + } else { + if(skip != null && skip.constructor != Number) { + callback(new Error("skip requires an integer"), null); + } else { + this.skipValue = skip; + callback(null, this); + } + } + + return this; +}; + +/** + * Sets the batch size parameter of this cursor to the given value. + * + * @param {Number} batchSize the new batch size. + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the batchSize given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution. + * @return {Cursor} an instance of this object. + * @api public + */ +Cursor.prototype.batchSize = function(batchSize, callback) { + if(this.state == Cursor.CLOSED) { + if(callback != null) { + return callback(new Error("Cursor is closed"), null); + } else { + throw new Error("Cursor is closed"); + } + } else if(batchSize != null && batchSize.constructor != Number) { + if(callback != null) { + return callback(new Error("batchSize requires an integer"), null); + } else { + throw new Error("batchSize requires an integer"); + } + } else { + this.batchSizeValue = batchSize; + if(callback != null) return callback(null, this); + } + + return this; +}; + +/** + * The limit used for the getMore command + * + * @return {Number} The number of records to request per batch. + * @ignore + * @api private + */ +var limitRequest = function(self) { + var requestedLimit = self.limitValue; + + if(self.limitValue > 0) { + if (self.batchSizeValue > 0) { + requestedLimit = self.limitValue < self.batchSizeValue ? + self.limitValue : self.batchSizeValue; + } + } else { + requestedLimit = self.batchSizeValue; + } + + return requestedLimit; +}; + + +/** + * Generates a QueryCommand object using the parameters of this cursor. + * + * @return {QueryCommand} The command object + * @ignore + * @api private + */ +var generateQueryCommand = function(self) { + // Unpack the options + var queryOptions = QueryCommand.OPTS_NONE; + if(!self.timeout) { + queryOptions |= QueryCommand.OPTS_NO_CURSOR_TIMEOUT; + } + + if(self.tailable != null) { + queryOptions |= QueryCommand.OPTS_TAILABLE_CURSOR; + self.skipValue = self.limitValue = 0; + } + + if(self.slaveOk) { + queryOptions |= QueryCommand.OPTS_SLAVE; + } + + // limitValue of -1 is a special case used by Db#eval + var numberToReturn = self.limitValue == -1 ? -1 : limitRequest(self); + + // Check if we need a special selector + if(self.sortValue != null || self.explainValue != null || self.hint != null || self.snapshot != null + || self.returnKey != null || self.maxScan != null || self.min != null || self.max != null + || self.showDiskLoc != null || self.comment != null) { + + // Build special selector + var specialSelector = {'query':self.selector}; + if(self.sortValue != null) specialSelector['orderby'] = utils.formattedOrderClause(self.sortValue); + if(self.hint != null && self.hint.constructor == Object) specialSelector['$hint'] = self.hint; + if(self.explainValue != null) specialSelector['$explain'] = true; + if(self.snapshot != null) specialSelector['$snapshot'] = true; + if(self.returnKey != null) specialSelector['$returnKey'] = self.returnKey; + if(self.maxScan != null) specialSelector['$maxScan'] = self.maxScan; + if(self.min != null) specialSelector['$min'] = self.min; + if(self.max != null) specialSelector['$max'] = self.max; + if(self.showDiskLoc != null) specialSelector['$showDiskLoc'] = self.showDiskLoc; + if(self.comment != null) specialSelector['$comment'] = self.comment; + + // Return the query + return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, specialSelector, self.fields); + } else { + return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, self.selector, self.fields); + } +}; + +/** + * @return {Object} Returns an object containing the sort value of this cursor with + * the proper formatting that can be used internally in this cursor. + * @ignore + * @api private + */ +Cursor.prototype.formattedOrderClause = function() { + return utils.formattedOrderClause(this.sortValue); +}; + +/** + * Converts the value of the sort direction into its equivalent numerical value. + * + * @param sortDirection {String|number} Range of acceptable values: + * 'ascending', 'descending', 'asc', 'desc', 1, -1 + * + * @return {number} The equivalent numerical value + * @throws Error if the given sortDirection is invalid + * @ignore + * @api private + */ +Cursor.prototype.formatSortValue = function(sortDirection) { + return utils.formatSortValue(sortDirection); +}; + +/** + * Gets the next document from the cursor. + * + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results. + * @api public + */ +Cursor.prototype.nextObject = function(callback) { + var self = this; + + if(self.state == Cursor.INIT) { + var cmd; + try { + cmd = generateQueryCommand(self); + } catch (err) { + return callback(err, null); + } + + var commandHandler = function(err, result) { + if(err != null && result == null) return callback(err, null); + + if(!err && result.documents[0] && result.documents[0]['$err']) { + return self.close(function() {callback(result.documents[0]['$err'], null);}); + } + + self.queryRun = true; + self.state = Cursor.OPEN; // Adjust the state of the cursor + self.cursorId = result.cursorId; + self.totalNumberOfRecords = result.numberReturned; + + // Add the new documents to the list of items + self.items = self.items.concat(result.documents); + self.nextObject(callback); + result = null; + }; + + self.db._executeQueryCommand(cmd, {read:self.read, raw:self.raw}, commandHandler); + commandHandler = null; + } else if(self.items.length) { + callback(null, self.items.shift()); + } else if(self.cursorId.greaterThan(Long.fromInt(0))) { + getMore(self, callback); + } else { + // Force cursor to stay open + return self.close(function() {callback(null, null);}); + } +} + +/** + * Gets more results from the database if any. + * + * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results. + * @ignore + * @api private + */ +var getMore = function(self, callback) { + var limit = 0; + + if (!self.tailable && self.limitValue > 0) { + limit = self.limitValue - self.totalNumberOfRecords; + if (limit < 1) { + self.close(function() {callback(null, null);}); + return; + } + } + try { + var getMoreCommand = new GetMoreCommand(self.db, self.collectionName, limitRequest(self), self.cursorId); + // Execute the command + self.db._executeQueryCommand(getMoreCommand, {read:self.read, raw:self.raw}, function(err, result) { + try { + if(err != null) callback(err, null); + + self.cursorId = result.cursorId; + self.totalNumberOfRecords += result.numberReturned; + // Determine if there's more documents to fetch + if(result.numberReturned > 0) { + if (self.limitValue > 0) { + var excessResult = self.totalNumberOfRecords - self.limitValue; + + if (excessResult > 0) { + result.documents.splice(-1 * excessResult, excessResult); + } + } + + self.items = self.items.concat(result.documents); + callback(null, self.items.shift()); + } else if(self.tailable) { + self.getMoreTimer = setTimeout(function() {getMore(self, callback);}, 500); + } else { + self.close(function() {callback(null, null);}); + } + + result = null; + } catch(err) { + callback(err, null); + } + }); + + getMoreCommand = null; + } catch(err) { + var handleClose = function() { + callback(err, null); + }; + + self.close(handleClose); + handleClose = null; + } +} + +/** + * Gets a detailed information about how the query is performed on this cursor and how + * long it took the database to process it. + * + * @param {Function} callback this will be called after executing this method. The first parameter will always be null while the second parameter will be an object containing the details. + * @api public + */ +Cursor.prototype.explain = function(callback) { + var limit = (-1)*Math.abs(this.limitValue); + // Create a new cursor and fetch the plan + var cursor = new Cursor(this.db, this.collection, this.selector, this.fields, this.skipValue, limit, + this.sortValue, this.hint, true, this.snapshot, this.timeout, this.tailable, this.batchSizeValue); + // Fetch the explaination document + cursor.nextObject(function(err, item) { + if(err != null) return callback(err, null); + // close the cursor + cursor.close(function(err, result) { + if(err != null) return callback(err, null); + callback(null, item); + }); + }); +}; + +/** + * Returns a stream object that can be used to listen to and stream records + * (**Use the CursorStream object instead as this is deprected**) + * + * Options + * - **fetchSize** {Number} the number of records to fetch in each batch (steam specific batchSize). + * + * Events + * - **data** {function(item) {}} the data event triggers when a document is ready. + * - **error** {function(err) {}} the error event triggers if an error happens. + * - **end** {function() {}} the end event triggers when there is no more documents available. + * + * @param {Object} [options] additional options for streamRecords. + * @return {EventEmitter} returns a stream object. + * @api public + */ +Cursor.prototype.streamRecords = function(options) { + var args = Array.prototype.slice.call(arguments, 0); + options = args.length ? args.shift() : {}; + + var + self = this, + stream = new process.EventEmitter(), + recordLimitValue = this.limitValue || 0, + emittedRecordCount = 0, + queryCommand = generateQueryCommand(self); + + // see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol + queryCommand.numberToReturn = options.fetchSize ? options.fetchSize : 500; + // Execute the query + execute(queryCommand); + + function execute(command) { + self.db._executeQueryCommand(command, {read:self.read, raw:self.raw}, function(err,result) { + if(err) { + stream.emit('error', err); + self.close(function(){}); + return; + } + + if (!self.queryRun && result) { + self.queryRun = true; + self.cursorId = result.cursorId; + self.state = Cursor.OPEN; + self.getMoreCommand = new GetMoreCommand(self.db, self.collectionName, queryCommand.numberToReturn, result.cursorId); + } + + var resflagsMap = { + CursorNotFound:1<<0, + QueryFailure:1<<1, + ShardConfigStale:1<<2, + AwaitCapable:1<<3 + }; + + if(result.documents && result.documents.length && !(result.responseFlag & resflagsMap.QueryFailure)) { + try { + result.documents.forEach(function(doc){ + if(recordLimitValue && emittedRecordCount>=recordLimitValue) { + throw("done"); + } + emittedRecordCount++; + stream.emit('data', doc); + }); + } catch(err) { + if (err != "done") { throw err; } + else { + self.close(function(){ + stream.emit('end', recordLimitValue); + }); + self.close(function(){}); + return; + } + } + // rinse & repeat + execute(self.getMoreCommand); + } else { + self.close(function(){ + stream.emit('end', recordLimitValue); + }); + } + }); + } + + return stream; +}; + +/** + * Returns a Node ReadStream interface for this cursor. + * + * @return {CursorStream} returns a stream object. + * @api public + */ +Cursor.prototype.stream = function stream () { + return new CursorStream(this); +} + +/** + * Close the cursor. + * + * @param {Function} callback this will be called after executing this method. The first parameter will always contain null while the second parameter will contain a reference to this cursor. + * @return {null} + * @api public + */ +Cursor.prototype.close = function(callback) { + var self = this + this.getMoreTimer && clearTimeout(this.getMoreTimer); + // Close the cursor if not needed + if(this.cursorId instanceof Long && this.cursorId.greaterThan(Long.fromInt(0))) { + try { + var command = new KillCursorCommand(this.db, [this.cursorId]); + this.db._executeQueryCommand(command, {read:self.read, raw:self.raw}, null); + } catch(err) {} + } + + // Reset cursor id + this.cursorId = Long.fromInt(0); + // Set to closed status + this.state = Cursor.CLOSED; + + if(callback) { + callback(null, self); + self.items = []; + } + + return this; +}; + +/** + * Check if the cursor is closed or open. + * + * @return {Boolean} returns the state of the cursor. + * @api public + */ +Cursor.prototype.isClosed = function() { + return this.state == Cursor.CLOSED ? true : false; +}; + +/** + * Init state + * + * @classconstant INIT + **/ +Cursor.INIT = 0; + +/** + * Cursor open + * + * @classconstant OPEN + **/ +Cursor.OPEN = 1; + +/** + * Cursor closed + * + * @classconstant CLOSED + **/ +Cursor.CLOSED = 2; + +/** + * @ignore + * @api private + */ +exports.Cursor = Cursor; diff --git a/node_modules/mongodb/lib/mongodb/cursorstream.js b/node_modules/mongodb/lib/mongodb/cursorstream.js new file mode 100644 index 0000000..fd2ff65 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/cursorstream.js @@ -0,0 +1,141 @@ +/** + * Module dependecies. + */ +var Stream = require('stream').Stream; + +/** + * CursorStream + * + * Returns a stream interface for the **cursor**. + * + * Events + * - **data** {function(item) {}} the data event triggers when a document is ready. + * - **error** {function(err) {}} the error event triggers if an error happens. + * - **end** {function() {}} the end event triggers when there is no more documents available. + * + * @class Represents a CursorStream. + * @param {Cursor} cursor a cursor object that the stream wraps. + * @return {Stream} + */ +function CursorStream(cursor) { + if(!(this instanceof CursorStream)) return new CursorStream(cursor); + + Stream.call(this); + + this.readable = true; + this.paused = false; + this._cursor = cursor; + this._destroyed = null; + + // give time to hook up events + var self = this; + process.nextTick(function () { + self._init(); + }); +} + +/** + * Inherit from Stream + * @ignore + * @api private + */ +CursorStream.prototype.__proto__ = Stream.prototype; + +/** + * Flag stating whether or not this stream is readable. + */ +CursorStream.prototype.readable; + +/** + * Flag stating whether or not this stream is paused. + */ +CursorStream.prototype.paused; + +/** + * Initialize the cursor. + * @ignore + * @api private + */ +CursorStream.prototype._init = function () { + if (this._destroyed) return; + this._next(); +} + +/** + * Pull the next document from the cursor. + * @ignore + * @api private + */ +CursorStream.prototype._next = function () { + if (this.paused || this._destroyed) return; + + var self = this; + + // nextTick is necessary to avoid stack overflows when + // dealing with large result sets. + process.nextTick(function () { + self._cursor.nextObject(function (err, doc) { + self._onNextObject(err, doc); + }); + }); +} + +/** + * Handle each document as its returned from the cursor. + * @ignore + * @api private + */ +CursorStream.prototype._onNextObject = function (err, doc) { + if (err) return this.destroy(err); + + // when doc is null we hit the end of the cursor + if (!doc) return this.destroy(); + + this.emit('data', doc); + this._next(); +} + +/** + * Pauses the stream. + * + * @api public + */ +CursorStream.prototype.pause = function () { + this.paused = true; +} + +/** + * Resumes the stream. + * + * @api public + */ +CursorStream.prototype.resume = function () { + this.paused = false; + this._next(); +} + +/** + * Destroys the stream, closing the underlying + * cursor. No more events will be emitted. + * + * @api public + */ +CursorStream.prototype.destroy = function (err) { + if (this._destroyed) return; + this._destroyed = true; + this.readable = false; + + this._cursor.close(); + + if (err) { + this.emit('error', err); + } + + this.emit('close'); +} + +// TODO - maybe implement the raw option to pass binary? +//CursorStream.prototype.setEncoding = function () { +//} + +module.exports = exports = CursorStream; diff --git a/node_modules/mongodb/lib/mongodb/db.js b/node_modules/mongodb/lib/mongodb/db.js new file mode 100644 index 0000000..b892fbd --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/db.js @@ -0,0 +1,1788 @@ +/** + * Module dependencies. + * @ignore + */ +var QueryCommand = require('./commands/query_command').QueryCommand, + DbCommand = require('./commands/db_command').DbCommand, + MongoReply = require('./responses/mongo_reply').MongoReply, + Admin = require('./admin').Admin, + Collection = require('./collection').Collection, + Server = require('./connection/server').Server, + ReplSetServers = require('./connection/repl_set_servers').ReplSetServers, + Cursor = require('./cursor').Cursor, + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits, + crypto = require('crypto'); + +/** + * Internal class for callback storage + * @ignore + */ +var CallbackStore = function() { + // Make class an event emitter + EventEmitter.call(this); + // Add a info about call variable + this._notReplied = {}; +} + +/** + * @ignore + */ +inherits(CallbackStore, EventEmitter); + +/** + * Create a new Db instance. + * + * Options + * - **strict** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, execute insert with a getLastError command returning the result of the insert command. + * - **native_parser** {Boolean, default:false}, use c++ bson parser. + * - **forceServerObjectId** {Boolean, default:false}, force server to create _id fields instead of client. + * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation. + * - **slaveOk** {Boolean, default:false}, allow reads from secondaries. + * - **serializeFunctions** {Boolean, default:false}, serialize functions. + * - **raw** {Boolean, default:false}, peform operations using raw bson buffers. + * - **recordQueryStats** {Boolean, default:false}, record query statistics during execution. + * - **reaper** {Boolean, default:false}, enables the reaper, timing out calls that never return. + * - **reaperInterval** {Number, default:10000}, number of miliseconds between reaper wakups. + * - **reaperTimeout** {Number, default:30000}, the amount of time before a callback times out. + * - **retryMiliSeconds** {Number, default:5000}, number of miliseconds between retries. + * - **numberOfRetries** {Number, default:5}, number of retries off connection. + * + * @class Represents a Collection + * @param {String} databaseName name of the database. + * @param {Object} serverConfig server config object. + * @param {Object} [options] additional options for the collection. + */ +function Db(databaseName, serverConfig, options) { + + if(!(this instanceof Db)) return new Db(databaseName, serverConfig, options); + + EventEmitter.call(this); + this.databaseName = databaseName; + this.serverConfig = serverConfig; + this.options = options == null ? {} : options; + // State to check against if the user force closed db + this._applicationClosed = false; + // Fetch the override flag if any + var overrideUsedFlag = this.options['override_used_flag'] == null ? false : this.options['override_used_flag']; + // Verify that nobody is using this config + if(!overrideUsedFlag && typeof this.serverConfig == 'object' && this.serverConfig._isUsed()) { + throw new Error("A Server or ReplSetServers instance cannot be shared across multiple Db instances"); + } else if(!overrideUsedFlag && typeof this.serverConfig == 'object'){ + // Set being used + this.serverConfig._used = true; + } + + // Ensure we have a valid db name + validateDatabaseName(databaseName); + + // Contains all the connections for the db + try { + this.native_parser = this.options.native_parser; + // The bson lib + var bsonLib = this.bsonLib = this.options.native_parser ? require('bson').BSONNative : new require('bson').BSONPure; + // Fetch the serializer object + var BSON = bsonLib.BSON; + // Create a new instance + this.bson = new BSON([bsonLib.Long, bsonLib.ObjectID, bsonLib.Binary, bsonLib.Code, bsonLib.DBRef, bsonLib.Symbol, bsonLib.Double, bsonLib.Timestamp, bsonLib.MaxKey, bsonLib.MinKey]); + // Backward compatibility to access types + this.bson_deserializer = bsonLib; + this.bson_serializer = bsonLib; + } catch (err) { + // If we tried to instantiate the native driver + throw "Native bson parser not compiled, please compile or avoid using native_parser=true"; + } + + // Internal state of the server + this._state = 'disconnected'; + + this.pkFactory = this.options.pk == null ? bsonLib.ObjectID : this.options.pk; + this.forceServerObjectId = this.options.forceServerObjectId != null ? this.options.forceServerObjectId : false; + // Added strict + this.strict = this.options.strict == null ? false : this.options.strict; + this.notReplied ={}; + this.isInitializing = true; + this.auths = []; + this.openCalled = false; + + // Command queue, keeps a list of incoming commands that need to be executed once the connection is up + this.commands = []; + + // Contains all the callbacks + this._callBackStore = new CallbackStore(); + + // Set up logger + this.logger = this.options.logger != null + && (typeof this.options.logger.debug == 'function') + && (typeof this.options.logger.error == 'function') + && (typeof this.options.logger.log == 'function') + ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}}; + // Allow slaveOk + this.slaveOk = this.options["slave_ok"] == null ? false : this.options["slave_ok"]; + + var self = this; + // Associate the logger with the server config + this.serverConfig.logger = this.logger; + this.tag = new Date().getTime(); + // Just keeps list of events we allow + this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[]}; + + // Controls serialization options + this.serializeFunctions = this.options.serializeFunctions != null ? this.options.serializeFunctions : false; + + // Raw mode + this.raw = this.options.raw != null ? this.options.raw : false; + + // Record query stats + this.recordQueryStats = this.options.recordQueryStats != null ? this.options.recordQueryStats : false; + + // If we have server stats let's make sure the driver objects have it enabled + if(this.recordQueryStats == true) { + this.serverConfig.enableRecordQueryStats(true); + } + + // Reaper enable setting + this.reaperEnabled = this.options.reaper != null ? this.options.reaper : false; + this._lastReaperTimestamp = new Date().getTime(); + + // Retry information + this.retryMiliSeconds = this.options.retryMiliSeconds != null ? this.options.retryMiliSeconds : 5000; + this.numberOfRetries = this.options.numberOfRetries != null ? this.options.numberOfRetries : 5; + + // Reaper information + this.reaperInterval = this.options.reaperInterval != null ? this.options.reaperInterval : 10000; + this.reaperTimeout = this.options.reaperTimeout != null ? this.options.reaperTimeout : 30000; + + // get self + var self = this; + // State of the db connection + Object.defineProperty(this, "state", { enumerable: true + , get: function () { + return this.serverConfig._serverState; + } + }); +}; + +/** + * The reaper cleans up any callbacks that have not returned inside the space set by + * the parameter reaperTimeout, it will only attempt to reap if the time since last reap + * is bigger or equal to the reaperInterval value + * @ignore + */ +var reaper = function(dbInstance, reaperInterval, reaperTimeout) { + // Get current time, compare to reaper interval + var currentTime = new Date().getTime(); + // Now calculate current time difference to check if it's time to reap + if((currentTime - dbInstance._lastReaperTimestamp) >= reaperInterval) { + // Save current timestamp for next reaper iteration + dbInstance._lastReaperTimestamp = currentTime; + // Get all non-replied to messages + var keys = Object.keys(dbInstance._callBackStore._notReplied); + // Iterate over all callbacks + for(var i = 0; i < keys.length; i++) { + // Fetch the current key + var key = keys[i]; + // Get info element + var info = dbInstance._callBackStore._notReplied[key]; + // If it's timed out let's remove the callback and return an error + if((currentTime - info.start) > reaperTimeout) { + // Cleanup + delete dbInstance._callBackStore._notReplied[key]; + // Perform callback in next Tick + process.nextTick(function() { + dbInstance._callBackStore.emit(key, new Error("operation timed out"), null); + }); + } + } + // Return reaping was done + return true; + } else { + // No reaping done + return false; + } +} + +/** + * @ignore + */ +function validateDatabaseName(databaseName) { + if(typeof databaseName !== 'string') throw new Error("database name must be a string"); + if(databaseName.length === 0) throw new Error("database name cannot be the empty string"); + + var invalidChars = [" ", ".", "$", "/", "\\"]; + for(var i = 0; i < invalidChars.length; i++) { + if(databaseName.indexOf(invalidChars[i]) != -1) throw new Error("database names cannot contain the character '" + invalidChars[i] + "'"); + } +} + +/** + * @ignore + */ +inherits(Db, EventEmitter); + +/** + * Initialize the database connection. + * + * @param {Function} callback returns index information. + * @return {null} + * @api public + */ +Db.prototype.open = function(callback) { + var self = this; + + // Check that the user has not called this twice + if(this.openCalled) { + // Close db + this.close(); + // Throw error + throw new Error("db object already connecting, open cannot be called multiple times"); + } + + // Set that db has been opened + this.openCalled = true; + + // Set the status of the server + self._state = 'connecting'; + // Set up connections + if(self.serverConfig instanceof Server || self.serverConfig instanceof ReplSetServers) { + self.serverConfig.connect(self, {firstCall: true}, function(err, result) { + if(err != null) { + // Return error from connection + return callback(err, null); + } + // Set the status of the server + self._state = 'connected'; + // Callback + return callback(null, self); + }); + } else { + return callback(Error("Server parameter must be of type Server or ReplSetServers"), null); + } +}; + +/** + * Create a new Db instance sharing the current socket connections. + * + * @param {String} dbName the name of the database we want to use. + * @return {Db} a db instance using the new database. + * @api public + */ +Db.prototype.db = function(dbName) { + // Copy the options and add out internal override of the not shared flag + var options = {}; + for(var key in this.options) { + options[key] = this.options[key]; + } + // Add override flag + options['override_used_flag'] = true; + // Create a new db instance + var newDbInstance = new Db(dbName, this.serverConfig, options); + // Add the instance to the list of approved db instances + var allServerInstances = this.serverConfig.allServerInstances(); + // Add ourselves to all server callback instances + for(var i = 0; i < allServerInstances.length; i++) { + var server = allServerInstances[i]; + server.dbInstances.push(newDbInstance); + } + // Return new db object + return newDbInstance; +} + +/** + * Close the current db connection, including all the child db instances. Emits close event if no callback is provided. + * + * @param {Boolean} [forceClose] connection can never be reused. + * @param {Function} [callback] returns the results. + * @return {null} + * @api public + */ +Db.prototype.close = function(forceClose, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + // Ensure we force close all connections + this._applicationClosed = args.length ? args.shift() : false; + // Remove all listeners and close the connection + this.serverConfig.close(callback); + // Emit the close event + if(typeof callback !== 'function') this.emit("close"); + + // Emit close event across all db instances sharing the sockets + var allServerInstances = this.serverConfig.allServerInstances(); + // Fetch the first server instance + if(Array.isArray(allServerInstances) && allServerInstances.length > 0) { + var server = allServerInstances[0]; + // For all db instances signal all db instances + if(Array.isArray(server.dbInstances) && server.dbInstances.length > 1) { + for(var i = 0; i < server.dbInstances.length; i++) { + var dbInstance = server.dbInstances[i]; + // Check if it's our current db instance and skip if it is + if(dbInstance.databaseName !== this.databaseName && dbInstance.tag !== this.tag) { + server.dbInstances[i].emit("close"); + } + } + } + } + + // Remove all listeners + this.removeAllEventListeners(); + // You can reuse the db as everything is shut down + this.openCalled = false; +}; + +/** + * Access the Admin database + * + * @param {Function} [callback] returns the results. + * @return {Admin} the admin db object. + * @api public + */ +Db.prototype.admin = function(callback) { + if(callback == null) return new Admin(this); + callback(null, new Admin(this)); +}; + +/** + * Returns a cursor to all the collection information. + * + * @param {String} [collectionName] the collection name we wish to retrieve the information from. + * @param {Function} callback returns option results. + * @return {null} + * @api public + */ +Db.prototype.collectionsInfo = function(collectionName, callback) { + if(callback == null && typeof collectionName == 'function') { callback = collectionName; collectionName = null; } + // Create selector + var selector = {}; + // If we are limiting the access to a specific collection name + if(collectionName != null) selector.name = this.databaseName + "." + collectionName; + + // Return Cursor + // callback for backward compatibility + if(callback) { + callback(null, new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector)); + } else { + return new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector); + } +}; + +/** + * Get the list of all collection names for the specified db + * + * @param {String} [collectionName] the collection name we wish to filter by. + * @param {Function} callback returns option results. + * @return {null} + * @api public + */ +Db.prototype.collectionNames = function(collectionName, callback) { + if(callback == null && typeof collectionName == 'function') { callback = collectionName; collectionName = null; } + var self = this; + // Let's make our own callback to reuse the existing collections info method + self.collectionsInfo(collectionName, function(err, cursor) { + if(err != null) return callback(err, null); + + cursor.toArray(function(err, documents) { + if(err != null) return callback(err, null); + + // List of result documents that have been filtered + var filtered_documents = []; + // Remove any collections that are not part of the db or a system db signed with $ + documents.forEach(function(document) { + if(!(document.name.indexOf(self.databaseName) == -1 || document.name.indexOf('$') != -1)) + filtered_documents.push(document); + }); + // Return filtered items + callback(null, filtered_documents); + }); + }); +}; + +/** + * Fetch a specific collection (containing the actual collection information) + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **slaveOk** {Boolean, default:false}, Allow reads from secondaries. + * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document. + * - **raw** {Boolean, default:false}, perform all operations using raw bson objects. + * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation. + * + * @param {String} collectionName the collection name we wish to access. + * @param {Object} [options] returns option results. + * @param {Function} [callback] returns the results. + * @return {null} + * @api public + */ +Db.prototype.collection = function(collectionName, options, callback) { + var self = this; + if(typeof options === "function") { callback = options; options = {}; } + // Execute safe + if(options && options.safe || this.strict) { + self.collectionNames(collectionName, function(err, collections) { + if(err != null) return callback(err, null); + + if(collections.length == 0) { + return callback(new Error("Collection " + collectionName + " does not exist. Currently in strict mode."), null); + } else { + try { + var collection = new Collection(self, collectionName, self.pkFactory, options); + } catch(err) { + return callback(err, null); + } + return callback(null, collection); + } + }); + } else { + try { + var collection = new Collection(self, collectionName, self.pkFactory, options); + } catch(err) { + if(callback == null) { + throw err; + } else { + return callback(err, null); + } + } + + // If we have no callback return collection object + return callback == null ? collection : callback(null, collection); + } +}; + +/** + * Fetch all collections for the current db. + * + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.collections = function(callback) { + var self = this; + // Let's get the collection names + self.collectionNames(function(err, documents) { + if(err != null) return callback(err, null); + var collections = []; + documents.forEach(function(document) { + collections.push(new Collection(self, document.name.replace(self.databaseName + ".", ''), self.pkFactory)); + }); + // Return the collection objects + callback(null, collections); + }); +}; + +/** + * Evaluate javascript on the server + * + * Options + * - **nolock** {Boolean, default:false}, Tell MongoDB not to block on the evaulation of the javascript. + * + * @param {Code} code javascript to execute on server. + * @param {Object|Array} [parameters] the parameters for the call. + * @param {Object} [options] the options + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.eval = function(code, parameters, options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + parameters = args.length ? args.shift() : parameters; + options = args.length ? args.shift() : {}; + + var finalCode = code; + var finalParameters = []; + // If not a code object translate to one + if(!(finalCode instanceof this.bsonLib.Code)) { + finalCode = new this.bsonLib.Code(finalCode); + } + + // Ensure the parameters are correct + if(parameters != null && parameters.constructor != Array && typeof parameters !== 'function') { + finalParameters = [parameters]; + } else if(parameters != null && parameters.constructor == Array && typeof parameters !== 'function') { + finalParameters = parameters; + } + // Create execution selector + var selector = {'$eval':finalCode, 'args':finalParameters}; + // Check if the nolock parameter is passed in + if(options['nolock']) { + selector['nolock'] = options['nolock']; + } + + // Iterate through all the fields of the index + new Cursor(this, new Collection(this, DbCommand.SYSTEM_COMMAND_COLLECTION), selector, options, 0, -1).nextObject(function(err, result) { + if(err != null) return callback(err, null); + + if(result.ok == 1) { + callback(null, result.retval); + } else { + callback(new Error("eval failed: " + result.errmsg), null); return; + } + }); +}; + +/** + * Dereference a dbref, against a db + * + * @param {DBRef} dbRef db reference object we wish to resolve. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.dereference = function(dbRef, callback) { + var db = this; + // If we have a db reference then let's get the db first + if(dbRef.db != null) db = this.db(dbRef.db); + // Fetch the collection and find the reference + db.collection(dbRef.namespace, function(err, collection) { + if(err != null) return callback(err, null); + + collection.findOne({'_id':dbRef.oid}, function(err, result) { + callback(err, result); + }); + }); +}; + +/** + * Logout user from server, fire off on all connections and remove all auth info + * + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.logout = function(callback) { + var self = this; + // Let's generate the logout command object + var logoutCommand = DbCommand.logoutCommand(self, {logout:1}); + self._executeQueryCommand(logoutCommand, {onAll:true}, function(err, result) { + // Reset auth + self.auths = []; + // Handle any errors + if(err == null && result.documents[0].ok == 1) { + callback(null, true); + } else { + err != null ? callback(err, false) : callback(new Error(result.documents[0].errmsg), false); + } + }); +} + +/** + * Authenticate a user against the server. + * + * @param {String} username username. + * @param {String} password password. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.authenticate = function(username, password, callback) { + var self = this; + + // Push the new auth if we have no previous record + self.auths = [{'username':username, 'password':password}]; + // Get the amount of connections in the pool to ensure we have authenticated all comments + var numberOfConnections = this.serverConfig.allRawConnections().length; + var errorObject = null; + + // Execute all four + this._executeQueryCommand(DbCommand.createGetNonceCommand(self), {onAll:true}, function(err, result, connection) { + // Execute on all the connections + if(err == null) { + // Nonce used to make authentication request with md5 hash + var nonce = result.documents[0].nonce; + // Execute command + self._executeQueryCommand(DbCommand.createAuthenticationCommand(self, username, password, nonce), {connection:connection}, function(err, result) { + // Ensure we save any error + if(err) { + errorObject = err; + } else if(result.documents[0].err != null || result.documents[0].errmsg != null){ + errorObject = self.wrap(result.documents[0]); + } + + // Count down + numberOfConnections = numberOfConnections - 1; + + // If we are done with the callbacks return + if(numberOfConnections <= 0) { + if(errorObject == null && result.documents[0].ok == 1) { + callback(errorObject, true); + } else { + callback(errorObject, false); + } + } + }); + } + }); +}; + +/** + * Add a user to the database. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {String} username username. + * @param {String} password password. + * @param {Object} [options] additional options during update. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.addUser = function(username, password, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + // Figure out the safe mode settings + var safe = self.strict != null && self.strict == false ? true : self.strict; + // Override with options passed in if applicable + safe = options != null && options['safe'] != null ? options['safe'] : safe; + // Ensure it's at least set to safe + safe = safe == null ? true : safe; + + // Use node md5 generator + var md5 = crypto.createHash('md5'); + // Generate keys used for authentication + md5.update(username + ":mongo:" + password); + var userPassword = md5.digest('hex'); + // Fetch a user collection + this.collection(DbCommand.SYSTEM_USER_COLLECTION, function(err, collection) { + collection.find({user: username}).toArray(function(err, documents) { + // We got an error (f.ex not authorized) + if(err != null) return callback(err, null); + // We have a user, let's update the password + if(documents.length > 0) { + collection.update({user: username},{user: username, pwd: userPassword}, {safe:safe}, function(err, results) { + callback(err, documents); + }); + } else { + collection.insert({user: username, pwd: userPassword}, {safe:safe}, function(err, documents) { + callback(err, documents); + }); + } + }); + }); +}; + +/** + * Remove a user from a database + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * + * @param {String} username username. + * @param {Object} [options] additional options during update. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.removeUser = function(username, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + // Figure out the safe mode settings + var safe = self.strict != null && self.strict == false ? true : self.strict; + // Override with options passed in if applicable + safe = options != null && options['safe'] != null ? options['safe'] : safe; + // Ensure it's at least set to safe + safe = safe == null ? true : safe; + + // Fetch a user collection + this.collection(DbCommand.SYSTEM_USER_COLLECTION, function(err, collection) { + collection.findOne({user: username}, function(err, user) { + if(user != null) { + collection.remove({user: username}, {safe:safe}, function(err, result) { + callback(err, true); + }); + } else { + callback(err, false); + } + }); + }); +}; + +/** + * Creates a collection on a server pre-allocating space, need to create f.ex capped collections. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **slaveOk** {Boolean, default:false}, Allow reads from secondaries. + * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document. + * - **raw** {Boolean, default:false}, perform all operations using raw bson objects. + * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation. + * - **capped** {Boolean, default:false}, create a capped collection. + * - **size** {Number}, the size of the capped collection in bytes. + * - **max** {Number}, the maximum number of documents in the capped collection. + * - **autoIndexId** {Boolean, default:false}, create an index on the _id field of the document, not created automatically on capped collections. + * + * @param {String} collectionName the collection name we wish to access. + * @param {Object} [options] returns option results. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.createCollection = function(collectionName, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : null; + var self = this; + + // Figure out the safe mode settings + var safe = self.strict != null && self.strict == false ? true : self.strict; + // Override with options passed in if applicable + safe = options != null && options['safe'] != null ? options['safe'] : safe; + // Ensure it's at least set to safe + safe = safe == null ? true : safe; + + // Check if we have the name + this.collectionNames(collectionName, function(err, collections) { + if(err != null) return callback(err, null); + + var found = false; + collections.forEach(function(collection) { + if(collection.name == self.databaseName + "." + collectionName) found = true; + }); + + // If the collection exists either throw an exception (if db in strict mode) or return the existing collection + if(found && ((options && options.safe) || self.strict)) { + return callback(new Error("Collection " + collectionName + " already exists. Currently in strict mode."), null); + } else if(found){ + try { + var collection = new Collection(self, collectionName, self.pkFactory, options); + } catch(err) { + return callback(err, null); + } + return callback(null, collection); + } + + // Create a new collection and return it + self._executeQueryCommand(DbCommand.createCreateCollectionCommand(self, collectionName, options), {read:false, safe:safe}, function(err, result) { + var document = result.documents[0]; + // If we have no error let's return the collection + if(err == null && document.ok == 1) { + try { + var collection = new Collection(self, collectionName, self.pkFactory, options); + } catch(err) { + return callback(err, null); + } + return callback(null, collection); + } else { + err != null ? callback(err, null) : callback(self.wrap(document), null); + } + }); + }); +}; + +/** + * Execute a command hash against MongoDB. This lets you acess any commands not available through the api on the server. + * + * @param {Object} selector the command hash to send to the server, ex: {ping:1}. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.command = function(selector, callback) { + var cursor = new Cursor(this, new Collection(this, DbCommand.SYSTEM_COMMAND_COLLECTION), selector, {}, 0, -1, null, null, null, null, QueryCommand.OPTS_NO_CURSOR_TIMEOUT); + cursor.nextObject(callback); +}; + +/** + * Drop a collection from the database, removing it permanently. New accesses will create a new collection. + * + * @param {String} collectionName the name of the collection we wish to drop. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.dropCollection = function(collectionName, callback) { + var self = this; + + // Drop the collection + this._executeQueryCommand(DbCommand.createDropCollectionCommand(this, collectionName), function(err, result) { + if(err == null && result.documents[0].ok == 1) { + if(callback != null) return callback(null, true); + } else { + if(callback != null) err != null ? callback(err, null) : callback(self.wrap(result.documents[0]), null); + } + }); +}; + +/** + * Rename a collection. + * + * @param {String} fromCollection the name of the current collection we wish to rename. + * @param {String} toCollection the new name of the collection. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.renameCollection = function(fromCollection, toCollection, callback) { + var self = this; + + // Execute the command, return the new renamed collection if successful + this._executeQueryCommand(DbCommand.createRenameCollectionCommand(this, fromCollection, toCollection), function(err, result) { + if(err == null && result.documents[0].ok == 1) { + if(callback != null) return callback(null, new Collection(self, toCollection, self.pkFactory)); + } else { + if(callback != null) err != null ? callback(err, null) : callback(self.wrap(result.documents[0]), null); + } + }); +}; + +/** + * Return last error message for the given connection, note options can be combined. + * + * Options + * - **fsync** {Boolean, default:false}, option forces the database to fsync all files before returning. + * - **j** {Boolean, default:false}, awaits the journal commit before returning, > MongoDB 2.0. + * - **w** {Number}, until a write operation has been replicated to N servers. + * - **wtimeout** {Number}, number of miliseconds to wait before timing out. + * + * Connection Options + * - **connection** {Connection}, fire the getLastError down a specific connection. + * + * @param {Object} [options] returns option results. + * @param {Object} [connectionOptions] returns option results. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.lastError = function(options, connectionOptions, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + options = args.length ? args.shift() : {}; + connectionOptions = args.length ? args.shift() : {}; + + this._executeQueryCommand(DbCommand.createGetLastErrorCommand(options, this), connectionOptions, function(err, error) { + callback(err, error && error.documents); + }); +}; + +/** + * Legacy method calls. + * + * @ignore + * @api private + */ +Db.prototype.error = Db.prototype.lastError; +Db.prototype.lastStatus = Db.prototype.lastError; + +/** + * Return all errors up to the last time db reset_error_history was called. + * + * Options + * - **connection** {Connection}, fire the getLastError down a specific connection. + * + * @param {Object} [options] returns option results. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.previousErrors = function(options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + this._executeQueryCommand(DbCommand.createGetPreviousErrorsCommand(this), options, function(err, error) { + callback(err, error.documents); + }); +}; + +/** + * Runs a command on the database. + * @ignore + * @api private + */ +Db.prototype.executeDbCommand = function(command_hash, options, callback) { + if(callback == null) { callback = options; options = {}; } + this._executeQueryCommand(DbCommand.createDbSlaveOkCommand(this, command_hash, options), options, callback); +}; + +/** + * Runs a command on the database as admin. + * @ignore + * @api private + */ +Db.prototype.executeDbAdminCommand = function(command_hash, options, callback) { + if(callback == null) { callback = options; options = {}; } + this._executeQueryCommand(DbCommand.createAdminDbCommand(this, command_hash, options), callback); +}; + +/** + * Resets the error history of the mongo instance. + * + * Options + * - **connection** {Connection}, fire the getLastError down a specific connection. + * + * @param {Object} [options] returns option results. + * @param {Function} callback returns the results. + * @return {null} + * @api public + */ +Db.prototype.resetErrorHistory = function(options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + this._executeQueryCommand(DbCommand.createResetErrorHistoryCommand(this), options, function(err, error) { + callback(err, error.documents); + }); +}; + +/** + * Creates an index on the collection. + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB. + * - **unique** {Boolean, default:false}, creates an unique index. + * - **sparse** {Boolean, default:false}, creates a sparse index. + * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible. + * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value + * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates. + * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates. + * + * @param {String} collectionName name of the collection to create the index on. + * @param {Object} fieldOrSpec fieldOrSpec that defines the index. + * @param {Object} [options] additional options during update. + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Db.prototype.createIndex = function(collectionName, fieldOrSpec, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + options = args.length ? args.shift() : {}; + options = typeof callback === 'function' ? options : callback; + options = options == null ? {} : options; + + // Collect errorOptions + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && self.strict != null ? self.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions != null && errorOptions != false && (typeof callback !== 'function' && typeof options !== 'function')) throw new Error("safe cannot be used without a callback"); + + // Create command + var command = DbCommand.createCreateIndexCommand(this, collectionName, fieldOrSpec, options); + // Default command options + var commandOptions = {}; + + // If we have error conditions set handle them + if(errorOptions && errorOptions != false) { + // Insert options + commandOptions['read'] = false; + // If we have safe set set async to false + if(errorOptions == null) commandOptions['async'] = true; + + // Set safe option + commandOptions['safe'] = errorOptions; + // If we have an error option + if(typeof errorOptions == 'object') { + var keys = Object.keys(errorOptions); + for(var i = 0; i < keys.length; i++) { + commandOptions[keys[i]] = errorOptions[keys[i]]; + } + } + + // Execute insert command + this._executeInsertCommand(command, commandOptions, function(err, result) { + if(err != null) return callback(err, null); + + result = result && result.documents; + if (result[0].err) { + callback(self.wrap(result[0])); + } else { + callback(null, command.documents[0].name); + } + }); + } else { + // Execute insert command + var result = this._executeInsertCommand(command, commandOptions); + // If no callback just return + if(!callback) return; + // If error return error + if(result instanceof Error) { + return callback(result); + } + // Otherwise just return + return callback(null, null); + } +}; + +/** + * Ensures that an index exists, if it does not it creates it + * + * Options + * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a + * - **unique** {Boolean, default:false}, creates an unique index. + * - **sparse** {Boolean, default:false}, creates a sparse index. + * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible. + * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value + * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates. + * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates. + * - **v** {Number}, specify the format version of the indexes. + * + * @param {String} collectionName name of the collection to create the index on. + * @param {Object} fieldOrSpec fieldOrSpec that defines the index. + * @param {Object} [options] additional options during update. + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Db.prototype.ensureIndex = function(collectionName, fieldOrSpec, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + options = args.length ? args.shift() : {}; + options = typeof callback === 'function' ? options : callback; + options = options == null ? {} : options; + + // Collect errorOptions + var errorOptions = options.safe != null ? options.safe : null; + errorOptions = errorOptions == null && self.strict != null ? self.strict : errorOptions; + + // If we have a write concern set and no callback throw error + if(errorOptions != null && errorOptions != false && (typeof callback !== 'function' && typeof options !== 'function')) throw new Error("safe cannot be used without a callback"); + + // Create command + var command = DbCommand.createCreateIndexCommand(this, collectionName, fieldOrSpec, options); + var index_name = command.documents[0].name; + + // Default command options + var commandOptions = {}; + // Check if the index allready exists + this.indexInformation(collectionName, function(err, collectionInfo) { + if(err != null) return callback(err, null); + + if(!collectionInfo[index_name]) { + // If we have error conditions set handle them + if(errorOptions && errorOptions != false) { + // Insert options + commandOptions['read'] = false; + // If we have safe set set async to false + if(errorOptions == null) commandOptions['async'] = true; + + // Set safe option + commandOptions['safe'] = errorOptions; + // If we have an error option + if(typeof errorOptions == 'object') { + var keys = Object.keys(errorOptions); + for(var i = 0; i < keys.length; i++) { + commandOptions[keys[i]] = errorOptions[keys[i]]; + } + } + + self._executeInsertCommand(command, commandOptions, function(err, result) { + // Only callback if we have one specified + if(typeof callback === 'function') { + if(err != null) return callback(err, null); + + result = result && result.documents; + if (result[0].err) { + callback(self.wrap(result[0])); + } else { + callback(null, command.documents[0].name); + } + } + }); + } else { + // Execute insert command + var result = self._executeInsertCommand(command, commandOptions); + // If no callback just return + if(!callback) return; + // If error return error + if(result instanceof Error) { + return callback(result); + } + // Otherwise just return + return callback(null, index_name); + } + } else { + if(typeof callback === 'function') return callback(null, index_name); + } + }); +}; + +/** + * Returns the information available on allocated cursors. + * + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Db.prototype.cursorInfo = function(callback) { + this._executeQueryCommand(DbCommand.createDbSlaveOkCommand(this, {'cursorInfo':1}), function(err, result) { + callback(err, result.documents[0]); + }); +}; + +/** + * Drop an index on a collection. + * + * @param {String} collectionName the name of the collection where the command will drop an index. + * @param {String} indexName name of the index to drop. + * @param {Function} callback for results. + * @return {null} + * @api public + */ +Db.prototype.dropIndex = function(collectionName, indexName, callback) { + this._executeQueryCommand(DbCommand.createDropIndexCommand(this, collectionName, indexName), callback); +}; + +/** + * Reindex all indexes on the collection + * Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections. + * + * @param {String} collectionName the name of the collection. + * @param {Function} callback returns the results. + * @api public +**/ +Db.prototype.reIndex = function(collectionName, callback) { + this._executeQueryCommand(DbCommand.createReIndexCommand(this, collectionName), function(err, result) { + if(err != null) { + callback(err, false); + } else if(result.documents[0].errmsg == null) { + callback(null, true); + } else { + callback(new Error(result.documents[0].errmsg), false); + } + }); +}; + +/** + * Retrieves this collections index info. + * + * Options + * - **full** {Boolean, default:false}, returns the full raw index information. + * + * @param {String} collectionName the name of the collection. + * @param {Object} [options] additional options during update. + * @param {Function} callback returns the index information. + * @return {null} + * @api public + */ +Db.prototype.indexInformation = function(collectionName, options, callback) { + // Unpack calls + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + collectionName = args.length ? args.shift() : null; + options = args.length ? args.shift() : {}; + + // If we specified full information + var full = options['full'] == null ? false : options['full']; + // Build selector for the indexes + var selector = collectionName != null ? {ns: (this.databaseName + "." + collectionName)} : {}; + // Iterate through all the fields of the index + new Cursor(this, new Collection(this, DbCommand.SYSTEM_INDEX_COLLECTION), selector).toArray(function(err, indexes) { + if(err != null) return callback(err, null); + // Contains all the information + var info = {}; + + // if full defined just return all the indexes directly + if(full) return callback(null, indexes); + + // Process all the indexes + for(var i = 0; i < indexes.length; i++) { + var index = indexes[i]; + // Let's unpack the object + info[index.name] = []; + for(var name in index.key) { + info[index.name].push([name, index.key[name]]); + } + } + + // Return all the indexes + callback(null, info); + }); +}; + +/** + * Drop a database. + * + * @param {Function} callback returns the index information. + * @return {null} + * @api public + */ +Db.prototype.dropDatabase = function(callback) { + var self = this; + + this._executeQueryCommand(DbCommand.createDropDatabaseCommand(this), function(err, result) { + if (err == null && result.documents[0].ok == 1) { + callback(null, true); + } else { + if (err) { + callback(err, false); + } else { + callback(self.wrap(result.documents[0]), false); + } + } + }); +}; + +/** + * Register a handler + * @ignore + * @api private + */ +Db.prototype._registerHandler = function(db_command, raw, connection, callback) { + // If we have an array of commands, chain them + var chained = Array.isArray(db_command); + + // If they are chained we need to add a special handler situation + if(chained) { + // List off chained id's + var chainedIds = []; + // Add all id's + for(var i = 0; i < db_command.length; i++) chainedIds.push(db_command[i].getRequestId().toString()); + + // Register all the commands together + for(var i = 0; i < db_command.length; i++) { + var command = db_command[i]; + // Add the callback to the store + this._callBackStore.once(command.getRequestId(), callback); + // Add the information about the reply + this._callBackStore._notReplied[command.getRequestId().toString()] = {start: new Date().getTime(), 'raw': raw, chained:chainedIds, connection:connection}; + } + } else { + // Add the callback to the list of handlers + this._callBackStore.once(db_command.getRequestId(), callback); + // Add the information about the reply + this._callBackStore._notReplied[db_command.getRequestId().toString()] = {start: new Date().getTime(), 'raw': raw, connection:connection}; + } +} + +/** + * + * @ignore + * @api private + */ +Db.prototype._callHandler = function(id, document, err) { + // If there is a callback peform it + if(this._callBackStore.listeners(id).length >= 1) { + // Get info object + var info = this._callBackStore._notReplied[id]; + // Delete the current object + delete this._callBackStore._notReplied[id]; + // Emit to the callback of the object + this._callBackStore.emit(id, err, document, info.connection); + } +} + +/** + * + * @ignore + * @api private + */ +Db.prototype._hasHandler = function(id) { + // If there is a callback peform it + return this._callBackStore.listeners(id).length >= 1; +} + +/** + * + * @ignore + * @api private + */ +Db.prototype._removeHandler = function(id) { + // Remove the information + if(this._callBackStore._notReplied[id] != null) delete this._callBackStore._notReplied[id]; + // Remove the callback if it's registered + this._callBackStore.removeAllListeners(id); + // Force cleanup _events, node.js seems to set it as a null value + if(this._callBackStore._events != null) delete this._callBackStore._events[id]; +} + +/** + * + * @ignore + * @api private + */ +Db.prototype._findHandler = function(id) { + var info = this._callBackStore._notReplied[id]; + // Return the callback + return {info:info, callback:(this._callBackStore.listeners(id).length >= 1)} +} + +/** + * @ignore + */ +var __executeQueryCommand = function(self, db_command, options, callback) { + // Options unpacking + var read = options['read'] != null ? options['read'] : false; + var raw = options['raw'] != null ? options['raw'] : self.raw; + var onAll = options['onAll'] != null ? options['onAll'] : false; + var specifiedConnection = options['connection'] != null ? options['connection'] : null; + + // If we got a callback object + if(typeof callback === 'function' && !onAll) { + // Fetch either a reader or writer dependent on the specified read option + var connection = read == true || read === 'secondary' ? self.serverConfig.checkoutReader() : self.serverConfig.checkoutWriter(true); + // Override connection if needed + connection = specifiedConnection != null ? specifiedConnection : connection; + // Ensure we have a valid connection + if(connection == null) { + return callback(new Error("no open connections")); + } else if(connection instanceof Error) { + return callback(connection); + } + + // Perform reaping of any dead connection + if(self.reaperEnabled) reaper(self, self.reaperInterval, self.reaperTimeout); + + // Register the handler in the data structure + self._registerHandler(db_command, raw, connection, callback); + + // Write the message out and handle any errors if there are any + connection.write(db_command, function(err) { + if(err != null) { + // Call the handler with an error + self._callHandler(db_command.getRequestId(), null, err); + } + }); + } else if(typeof callback === 'function' && onAll) { + var connections = self.serverConfig.allRawConnections(); + var numberOfEntries = connections.length; + // Go through all the connections + for(var i = 0; i < connections.length; i++) { + // Fetch a connection + var connection = connections[i]; + // Override connection if needed + connection = specifiedConnection != null ? specifiedConnection : connection; + // Ensure we have a valid connection + if(connection == null) { + return callback(new Error("no open connections")); + } else if(connection instanceof Error) { + return callback(connection); + } + + // Register the handler in the data structure + self._registerHandler(db_command, raw, connection, callback); + + // Write the message out + connection.write(db_command, function(err) { + // Adjust the number of entries we need to process + numberOfEntries = numberOfEntries - 1; + // Remove listener + if(err != null) { + // Clean up listener and return error + self._removeHandler(db_command.getRequestId()); + } + + // No more entries to process callback with the error + if(numberOfEntries <= 0) { + callback(err); + } + }); + + // Update the db_command request id + db_command.updateRequestId(); + } + } else { + // Fetch either a reader or writer dependent on the specified read option + var connection = read == true || read === 'secondary' ? self.serverConfig.checkoutReader() : self.serverConfig.checkoutWriter(); + // Override connection if needed + connection = specifiedConnection != null ? specifiedConnection : connection; + // Ensure we have a valid connection + if(connection == null || connection instanceof Error) return null; + // Write the message out + connection.write(db_command, function(err) { + if(err != null) { + // Emit the error + self.emit("error", err); + } + }); + } +} + +/** + * @ignore + */ +var __retryCommandOnFailure = function(self, retryInMilliseconds, numberOfTimes, command, db_command, options, callback) { + if(this._state == 'connected' || this._state == 'disconnected') this._state = 'connecting'; + // Number of retries done + var numberOfRetriesDone = numberOfTimes; + // Retry function, execute once + var retryFunction = function(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback) { + _self.serverConfig.connect(_self, {}, function(err, result) { + // Adjust the number of retries left + _numberOfRetriesDone = _numberOfRetriesDone - 1; + // Definitively restart + if(err != null && _numberOfRetriesDone > 0) { + _self._state = 'connecting'; + // Force close the current connections + _self.serverConfig.close(function(err) { + // Retry the connect + setTimeout(function() { + retryFunction(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback); + }, _retryInMilliseconds); + }); + } else if(err != null && _numberOfRetriesDone <= 0) { + _self._state = 'disconnected'; + // Force close the current connections + _self.serverConfig.close(function(_err) { + // Force close the current connections + _callback(err, null); + }); + } else if(err == null && _self.serverConfig.isConnected() == true && Array.isArray(_self.auths) && _self.auths.length > 0) { + _self._state = 'connected'; + // Get number of auths we need to execute + var numberOfAuths = _self.auths.length; + // Apply all auths + for(var i = 0; i < _self.auths.length; i++) { + _self.authenticate(_self.auths[i].username, _self.auths[i].password, function(err, authenticated) { + numberOfAuths = numberOfAuths - 1; + + // If we have no more authentications to replay + if(numberOfAuths == 0) { + if(err != null || !authenticated) { + return _callback(err, null); + } else { + // Execute command + command(_self, _db_command, _options, function(err, result) { + // Peform the command callback + _callback(err, result); + // Execute any backed up commands + while(_self.commands.length > 0) { + // Fetch the command + var command = _self.commands.shift(); + // Execute based on type + if(command['type'] == 'query') { + __executeQueryCommand(_self, command['db_command'], command['options'], command['callback']); + } else if(command['type'] == 'insert') { + __executeInsertCommand(_self, command['db_command'], command['options'], command['callback']); + } + } + }); + } + } + }); + } + } else if(err == null && _self.serverConfig.isConnected() == true) { + _self._state = 'connected'; + // Execute command + command(_self, _db_command, _options, function(err, result) { + // Peform the command callback + _callback(err, result); + // Execute any backed up commands + while(_self.commands.length > 0) { + // Fetch the command + var command = _self.commands.shift(); + // Execute based on type + if(command['type'] == 'query') { + __executeQueryCommand(_self, command['db_command'], command['options'], command['callback']); + } else if(command['type'] == 'insert') { + __executeInsertCommand(_self, command['db_command'], command['options'], command['callback']); + } + } + }); + } else { + _self._state = 'connecting'; + // Force close the current connections + _self.serverConfig.close(function(err) { + // Retry the connect + setTimeout(function() { + retryFunction(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback); + }, _retryInMilliseconds); + }); + } + }); + }; + + // Execute function first time + retryFunction(self, numberOfRetriesDone, retryInMilliseconds, numberOfTimes, command, db_command, options, callback); +} + +/** + * Execute db query command (not safe) + * @ignore + * @api private + */ +Db.prototype._executeQueryCommand = function(db_command, options, callback) { + var self = this; + // Unpack the parameters + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + // Check if the user force closed the command + if(this._applicationClosed) { + if(typeof callback == 'function') { + return callback(new Error("db closed by application"), null); + } else { + throw new Error("db closed by application"); + } + } + + // If the pool is not connected, attemp to reconnect to send the message + if(this._state == 'connecting' && this.serverConfig.autoReconnect) { + process.nextTick(function() { + self.commands.push({type:'query', 'db_command':db_command, 'options':options, 'callback':callback}); + }) + } else if(!this.serverConfig.isConnected() && this.serverConfig.autoReconnect) { + this._state = 'connecting'; + // Retry command + __retryCommandOnFailure(this, this.retryMiliSeconds, this.numberOfRetries, __executeQueryCommand, db_command, options, callback); + } else { + __executeQueryCommand(self, db_command, options, callback) + } +}; + +/** + * @ignore + */ +var __executeInsertCommand = function(self, db_command, options, callback) { + // Always checkout a writer for this kind of operations + var connection = self.serverConfig.checkoutWriter(); + // Get strict mode + var safe = options['safe'] != null ? options['safe'] : false; + var raw = options['raw'] != null ? options['raw'] : self.raw; + var specifiedConnection = options['connection'] != null ? options['connection'] : null; + // Override connection if needed + connection = specifiedConnection != null ? specifiedConnection : connection; + + // Ensure we have a valid connection + if(typeof callback === 'function') { + // Ensure we have a valid connection + if(connection == null) { + return callback(new Error("no open connections")); + } else if(connection instanceof Error) { + return callback(connection); + } + + // We are expecting a check right after the actual operation + if(safe != null && safe != false) { + // db command is now an array of commands (original command + lastError) + db_command = [db_command, DbCommand.createGetLastErrorCommand(safe, self)]; + + // Register the handler in the data structure + self._registerHandler(db_command[1], raw, connection, callback); + } + } + + // If we have no callback and there is no connection + if(connection == null) return null; + if(connection instanceof Error && typeof callback == 'function') return callback(connection, null); + if(connection instanceof Error) return null; + if(connection == null && typeof callback == 'function') return callback(new Error("no primary server found"), null); + + // Write the message out + connection.write(db_command, function(err) { + // Return the callback if it's not a safe operation and the callback is defined + if(typeof callback === 'function' && (safe == null || safe == false)) { + // Perform reaping + if(self.reaperEnabled) reaper(self, self.reaperInterval, self.reaperTimeout); + // Perform the callback + callback(err, null); + } else if(typeof callback === 'function'){ + // Call the handler with an error + self._callHandler(db_command[1].getRequestId(), null, err); + } else { + self.emit("error", err); + } + }); +} + +/** + * Execute an insert Command + * @ignore + * @api private + */ +Db.prototype._executeInsertCommand = function(db_command, options, callback) { + var self = this; + // Unpack the parameters + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + + // Check if the user force closed the command + if(this._applicationClosed) { + if(typeof callback == 'function') { + return callback(new Error("db closed by application"), null); + } else { + throw new Error("db closed by application"); + } + } + + // If the pool is not connected, attemp to reconnect to send the message + if(self._state == 'connecting' && this.serverConfig.autoReconnect) { + process.nextTick(function() { + self.commands.push({type:'insert', 'db_command':db_command, 'options':options, 'callback':callback}); + }) + } else if(!this.serverConfig.isConnected() && this.serverConfig.autoReconnect) { + this._state = 'connecting'; + // Retry command + __retryCommandOnFailure(this, this.retryMiliSeconds, this.numberOfRetries, __executeInsertCommand, db_command, options, callback); + } else { + __executeInsertCommand(self, db_command, options, callback) + } +} + +/** + * Update command is the same + * @ignore + * @api private + */ +Db.prototype._executeUpdateCommand = Db.prototype._executeInsertCommand; +/** + * Remove command is the same + * @ignore + * @api private + */ +Db.prototype._executeRemoveCommand = Db.prototype._executeInsertCommand; + +/** + * Wrap a Mongo error document into an Error instance + * @ignore + * @api private + */ +Db.prototype.wrap = function(error) { + var msg = error.err || error.errmsg || error; + var e = new Error(msg); + e.name = 'MongoError'; + + // Get all object keys + var keys = Object.keys(error); + // Populate error object with properties + for(var i = 0; i < keys.length; i++) { + e[keys[i]] = error[keys[i]]; + } + + return e; +} + +/** + * Default URL + * + * @classconstant DEFAULT_URL + **/ +Db.DEFAULT_URL = 'mongodb://localhost:27017/default'; + +/** + * Connect to MongoDB using a url as documented at + * + * www.mongodb.org/display/DOCS/Connections + * + * @param {String} url connection url for MongoDB. + * @param {Object} options additional options not covered by the url. + * @param {Function} callback callback returns the initialized db. + * @return {null} + * @api public + */ +Db.connect = function(url, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = typeof args[args.length - 1] == 'function' ? args.pop() : null; + options = args.length ? args.shift() : null; + options = options || {}; + var serverOptions = options.server || {}; + var replSetServersOptions = options.replSetServers || {}; + var dbOptions = options.db || {}; + + var urlRE = new RegExp('^mongo(?:db)?://(?:|([^@/]*)@)([^@/]*)(?:|/([^?]*)(?:|\\?([^?]*)))$'); + var match = (url || Db.DEFAULT_URL).match(urlRE); + if (!match) + throw Error("URL must be in the format mongodb://user:pass@host:port/dbname"); + + var authPart = match[1] || ''; + var auth = authPart.split(':', 2); + var hostPart = match[2]; + var dbname = match[3] || 'default'; + var urlOptions = (match[4] || '').split(/[&;]/); + + // Ugh, we have to figure out which options go to which constructor manually. + urlOptions.forEach(function(opt) { + if (!opt) return; + var splitOpt = opt.split('='), name = splitOpt[0], value = splitOpt[1]; + + // Server options: + if (name == 'slaveOk' || name == 'slave_ok') + serverOptions.slave_ok = (value == 'true'); + if (name == 'poolSize') + serverOptions.poolSize = Number(value); + if (name == 'autoReconnect' || name == 'auto_reconnect') + serverOptions.auto_reconnect = (value == 'true'); + if (name == 'ssl' || name == 'ssl') + serverOptions.ssl = (value == 'true'); + + // ReplSetServers options: + if (name == 'replicaSet' || name == 'rs_name') + replSetServersOptions.rs_name = value; + if (name == 'reconnectWait') + replSetServersOptions.reconnectWait = Number(value); + if (name == 'retries') + replSetServersOptions.retries = Number(value); + if (name == 'readSecondary' || name == 'read_secondary') + replSetServersOptions.read_secondary = (value == 'true'); + + // DB options: + if (name == 'safe') + dbOptions.safe = (value == 'true'); + // Not supported by Db: safe, w, wtimeoutMS, fsync, journal, connectTimeoutMS, socketTimeoutMS + if (name == 'nativeParser' || name == 'native_parser') + dbOptions.native_parser = (value == 'true'); + if (name == 'strict') + dbOptions.strict = (value == 'true'); + }); + + var servers = hostPart.split(',').map(function(h) { + var hostPort = h.split(':', 2); + return new Server(hostPort[0] || 'localhost', hostPort[1] != null ? parseInt(hostPort[1]) : 27017, serverOptions); + }); + + var server; + if (servers.length == 1) { + server = servers[0]; + } else { + server = new ReplSetServers(servers, replSetServersOptions); + } + + var db = new Db(dbname, server, dbOptions); + if(options.noOpen) + return db; + + // If callback is null throw an exception + if(callback == null) throw new Error("no callback function provided"); + + db.open(function(err, db){ + if(err == null && authPart){ + db.authenticate(auth[0], auth[1], function(err, success){ + if(success){ + callback(null, db); + } else { + callback(err ? err : new Error('Could not authenticate user ' + auth[0]), db); + } + }); + } else { + callback(err, db); + } + }); +} + +/** + * Legacy support + * + * @ignore + * @api private + */ +exports.connect = Db.connect; +exports.Db = Db; + +/** + * Remove all listeners to the db instance. + * @ignore + * @api private + */ +Db.prototype.removeAllEventListeners = function() { + this.removeAllListeners("close"); + this.removeAllListeners("error"); + this.removeAllListeners("timeout"); + this.removeAllListeners("parseError"); + this.removeAllListeners("poolReady"); + this.removeAllListeners("message"); +} diff --git a/node_modules/mongodb/lib/mongodb/gridfs/chunk.js b/node_modules/mongodb/lib/mongodb/gridfs/chunk.js new file mode 100644 index 0000000..cfb9009 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/gridfs/chunk.js @@ -0,0 +1,208 @@ +var Binary = require('bson').Binary, + ObjectID = require('bson').ObjectID; + +/** + * Class for representing a single chunk in GridFS. + * + * @class + * + * @param file {GridStore} The {@link GridStore} object holding this chunk. + * @param mongoObject {object} The mongo object representation of this chunk. + * + * @throws Error when the type of data field for {@link mongoObject} is not + * supported. Currently supported types for data field are instances of + * {@link String}, {@link Array}, {@link Binary} and {@link Binary} + * from the bson module + * + * @see Chunk#buildMongoObject + */ +var Chunk = exports.Chunk = function(file, mongoObject) { + if(!(this instanceof Chunk)) return new Chunk(file, mongoObject); + + this.file = file; + var self = this; + var mongoObjectFinal = mongoObject == null ? {} : mongoObject; + + this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id; + this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n; + this.data = new Binary(); + + if(mongoObjectFinal.data == null) { + } else if(typeof mongoObjectFinal.data == "string") { + var buffer = new Buffer(mongoObjectFinal.data.length); + buffer.write(mongoObjectFinal.data, 'binary', 0); + this.data = new Binary(buffer); + } else if(Array.isArray(mongoObjectFinal.data)) { + var buffer = new Buffer(mongoObjectFinal.data.length); + buffer.write(mongoObjectFinal.data.join(''), 'binary', 0); + this.data = new Binary(buffer); + } else if(mongoObjectFinal.data instanceof Binary || Object.prototype.toString.call(mongoObjectFinal.data) == "[object Binary]") { + this.data = mongoObjectFinal.data; + } else if(Buffer.isBuffer(mongoObjectFinal.data)) { + } else { + throw Error("Illegal chunk format"); + } + // Update position + this.internalPosition = 0; + + /** + * The position of the read/write head + * @name position + * @lends Chunk# + * @field + */ + Object.defineProperty(this, "position", { enumerable: true + , get: function () { + return this.internalPosition; + } + , set: function(value) { + this.internalPosition = value; + } + }); +}; + +/** + * Writes a data to this object and advance the read/write head. + * + * @param data {string} the data to write + * @param callback {function(*, GridStore)} This will be called after executing + * this method. The first parameter will contain null and the second one + * will contain a reference to this object. + */ +Chunk.prototype.write = function(data, callback) { + this.data.write(data, this.internalPosition); + this.internalPosition = this.data.length(); + callback(null, this); +}; + +/** + * Reads data and advances the read/write head. + * + * @param length {number} The length of data to read. + * + * @return {string} The data read if the given length will not exceed the end of + * the chunk. Returns an empty String otherwise. + */ +Chunk.prototype.read = function(length) { + // Default to full read if no index defined + length = length == null || length == 0 ? this.length() : length; + + if(this.length() - this.internalPosition + 1 >= length) { + var data = this.data.read(this.internalPosition, length); + this.internalPosition = this.internalPosition + length; + return data; + } else { + return ''; + } +}; + +Chunk.prototype.readSlice = function(length) { + if ((this.length() - this.internalPosition + 1) >= length) { + var data = null; + if (this.data.buffer != null) { //Pure BSON + data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length); + } else { //Native BSON + data = new Buffer(length); + length = this.data.readInto(data, this.internalPosition); + } + this.internalPosition = this.internalPosition + length; + return data; + } else { + return null; + } +}; + +/** + * Checks if the read/write head is at the end. + * + * @return {boolean} Whether the read/write head has reached the end of this + * chunk. + */ +Chunk.prototype.eof = function() { + return this.internalPosition == this.length() ? true : false; +}; + +/** + * Reads one character from the data of this chunk and advances the read/write + * head. + * + * @return {string} a single character data read if the the read/write head is + * not at the end of the chunk. Returns an empty String otherwise. + */ +Chunk.prototype.getc = function() { + return this.read(1); +}; + +/** + * Clears the contents of the data in this chunk and resets the read/write head + * to the initial position. + */ +Chunk.prototype.rewind = function() { + this.internalPosition = 0; + this.data = new Binary(); +}; + +/** + * Saves this chunk to the database. Also overwrites existing entries having the + * same id as this chunk. + * + * @param callback {function(*, GridStore)} This will be called after executing + * this method. The first parameter will contain null and the second one + * will contain a reference to this object. + */ +Chunk.prototype.save = function(callback) { + var self = this; + + self.file.chunkCollection(function(err, collection) { + collection.remove({'_id':self.objectId}, {safe:true}, function(err, result) { + if(self.data.length() > 0) { + self.buildMongoObject(function(mongoObject) { + collection.insert(mongoObject, {safe:true}, function(err, collection) { + callback(null, self); + }); + }); + } else { + callback(null, self); + } + }); + }); +}; + +/** + * Creates a mongoDB object representation of this chunk. + * + * @param callback {function(Object)} This will be called after executing this + * method. The object will be passed to the first parameter and will have + * the structure: + * + * <pre><code> + * { + * '_id' : , // {number} id for this chunk + * 'files_id' : , // {number} foreign key to the file collection + * 'n' : , // {number} chunk number + * 'data' : , // {bson#Binary} the chunk data itself + * } + * </code></pre> + * + * @see <a href="http://www.mongodb.org/display/DOCS/GridFS+Specification#GridFSSpecification-{{chunks}}">MongoDB GridFS Chunk Object Structure</a> + */ +Chunk.prototype.buildMongoObject = function(callback) { + var mongoObject = {'_id': this.objectId, + 'files_id': this.file.fileId, + 'n': this.chunkNumber, + 'data': this.data}; + callback(mongoObject); +}; + +/** + * @return {number} the length of the data + */ +Chunk.prototype.length = function() { + return this.data.length(); +}; + +/** + * The default chunk size + * @constant + */ +Chunk.DEFAULT_CHUNK_SIZE = 1024 * 256; diff --git a/node_modules/mongodb/lib/mongodb/gridfs/grid.js b/node_modules/mongodb/lib/mongodb/gridfs/grid.js new file mode 100644 index 0000000..d42c3d6 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/gridfs/grid.js @@ -0,0 +1,98 @@ +var GridStore = require('./gridstore').GridStore, + ObjectID = require('bson').ObjectID; + +/** + * A class representation of a simple Grid interface. + * + * @class Represents the Grid. + * @param {Db} db A database instance to interact with. + * @param {String} [fsName] optional different root collection for GridFS. + * @return {Grid} + */ +function Grid(db, fsName) { + + if(!(this instanceof Grid)) return new Grid(db, fsName); + + this.db = db; + this.fsName = fsName == null ? GridStore.DEFAULT_ROOT_COLLECTION : fsName; +} + +/** + * Puts binary data to the grid + * + * @param {Buffer} data buffer with Binary Data. + * @param {Object} [options] the options for the files. + * @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object. + * @return {null} + * @api public + */ +Grid.prototype.put = function(data, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + options = args.length ? args.shift() : {}; + // If root is not defined add our default one + options['root'] = options['root'] == null ? this.fsName : options['root']; + + // Return if we don't have a buffer object as data + if(!(Buffer.isBuffer(data))) return callback(new Error("Data object must be a buffer object"), null); + // Get filename if we are using it + var filename = options['filename']; + // Create gridstore + var gridStore = new GridStore(this.db, filename, "w", options); + gridStore.open(function(err, gridStore) { + if(err) return callback(err, null); + + gridStore.write(data, function(err, result) { + if(err) return callback(err, null); + + gridStore.close(function(err, result) { + if(err) return callback(err, null); + callback(null, result); + }) + }) + }) +} + +/** + * Get binary data to the grid + * + * @param {ObjectID} id ObjectID for file. + * @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object. + * @return {null} + * @api public + */ +Grid.prototype.get = function(id, callback) { + // Validate that we have a valid ObjectId + if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null)); + // Create gridstore + var gridStore = new GridStore(this.db, id, "r", {root:this.fsName}); + gridStore.open(function(err, gridStore) { + if(err) return callback(err, null); + + // Return the data + gridStore.read(function(err, data) { + return callback(err, data) + }); + }) +} + +/** + * Delete file from grid + * + * @param {ObjectID} id ObjectID for file. + * @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object. + * @return {null} + * @api public + */ +Grid.prototype.delete = function(id, callback) { + // Validate that we have a valid ObjectId + if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null)); + // Create gridstore + GridStore.unlink(this.db, id, {root:this.fsName}, function(err, result) { + if(err) return callback(err, false); + return callback(null, true); + }); +} + +exports.Grid = Grid; diff --git a/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js b/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js new file mode 100644 index 0000000..6b56cf1 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js @@ -0,0 +1,1109 @@ +/** + * @fileOverview GridFS is a tool for MongoDB to store files to the database. + * Because of the restrictions of the object size the database can hold, a + * facility to split a file into several chunks is needed. The {@link GridStore} + * class offers a simplified api to interact with files while managing the + * chunks of split files behind the scenes. More information about GridFS can be + * found <a href="http://www.mongodb.org/display/DOCS/GridFS">here</a>. + */ +var Chunk = require('./chunk').Chunk, + DbCommand = require('../commands/db_command').DbCommand, + ObjectID = require('bson').ObjectID, + Buffer = require('buffer').Buffer, + fs = require('fs'), + util = require('util'), + ReadStream = require('./readstream').ReadStream; + +var REFERENCE_BY_FILENAME = 0, + REFERENCE_BY_ID = 1; + +/** + * A class representation of a file stored in GridFS. + * + * Modes + * - **"r"** - read only. This is the default mode. + * - **"w"** - write in truncate mode. Existing data will be overwriten. + * - **w+"** - write in edit mode. + * + * Options + * - **root** {String}, root collection to use. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**. + * - **chunk_type** {String}, mime type of the file. Defaults to **{GridStore.DEFAULT_CONTENT_TYPE}**. + * - **chunk_size** {Number}, size for the chunk. Defaults to **{Chunk.DEFAULT_CHUNK_SIZE}**. + * - **metadata** {Object}, arbitrary data the user wants to store. + * + * @class Represents the GridStore. + * @param {Db} db A database instance to interact with. + * @param {ObjectID} id an unique ObjectID for this file + * @param {String} [filename] optional a filename for this file, no unique constrain on the field + * @param {String} mode set the mode for this file. + * @param {Object} options optional properties to specify. Recognized keys: + * @return {GridStore} + */ +function GridStore(db, id, filename, mode, options) { + if(!(this instanceof GridStore)) return new GridStore(db, id, filename, mode, options); + + var self = this; + this.db = db; + var _filename = filename; + + if(typeof filename == 'string' && typeof mode == 'string') { + _filename = filename; + } else if(typeof filename == 'string' && typeof mode == 'object' && mode != null) { + var _mode = mode; + mode = filename; + options = _mode; + _filename = id; + } else if(typeof filename == 'string' && mode == null) { + mode = filename; + _filename = id; + } + + // set grid referencetype + this.referenceBy = typeof id == 'string' ? 0 : 1; + this.filename = _filename; + this.fileId = id; + + // Set up the rest + this.mode = mode == null ? "r" : mode; + this.options = options == null ? {} : options; + this.root = this.options['root'] == null ? exports.GridStore.DEFAULT_ROOT_COLLECTION : this.options['root']; + this.position = 0; + // Set default chunk size + this.internalChunkSize = this.options['chunkSize'] == null ? Chunk.DEFAULT_CHUNK_SIZE : this.options['chunkSize']; + + /** + * Returns the current chunksize of the file. + * + * @field chunkSize + * @type {Number} + * @getter + * @setter + * @property return number of bytes in the current chunkSize. + */ + Object.defineProperty(this, "chunkSize", { enumerable: true + , get: function () { + return this.internalChunkSize; + } + , set: function(value) { + if(!(this.mode[0] == "w" && this.position == 0 && this.uploadDate == null)) { + this.internalChunkSize = this.internalChunkSize; + } else { + this.internalChunkSize = value; + } + } + }); + + /** + * The md5 checksum for this file. + * + * @field md5 + * @type {Number} + * @getter + * @setter + * @property return this files md5 checksum. + */ + Object.defineProperty(this, "md5", { enumerable: true + , get: function () { + return this.internalMd5; + } + }); +}; + +/** + * Opens the file from the database and initialize this object. Also creates a + * new one if file does not exist. + * + * @param {Function} callback this will be called after executing this method. The first parameter will contain an **{Error}** object and the second parameter will be null if an error occured. Otherwise, the first parameter will be null and the second will contain the reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.open = function(callback) { + if( this.mode != "w" && this.mode != "w+" && this.mode != "r"){ + callback(new Error("Illegal mode " + this.mode), null); + return; + } + + var self = this; + + if((self.mode == "w" || self.mode == "w+") && self.db.serverConfig.primary != null) { + // Get files collection + self.collection(function(err, collection) { + // Get chunk collection + self.chunkCollection(function(err, chunkCollection) { + // Ensure index on chunk collection + chunkCollection.ensureIndex([['files_id', 1], ['n', 1]], function(err, index) { + _open(self, callback); + }); + }); + }); + } else { + _open(self, callback); + } +} + +/** + * Hidding the _open function + * @ignore + * @api private + */ +var _open = function(self, callback) { + self.collection(function(err, collection) { + if(err!==null) { + callback(new Error("at collection: "+err), null); + return; + } + + // Create the query + var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename}; + query = self.fileId == null && this.filename == null ? null : query; + + // Fetch the chunks + if(query != null) { + collection.find(query, function(err, cursor) { + // Fetch the file + cursor.nextObject(function(err, doc) { + // Chek if the collection for the files exists otherwise prepare the new one + if(doc != null) { + self.fileId = doc._id; + self.contentType = doc.contentType; + self.internalChunkSize = doc.chunkSize; + self.uploadDate = doc.uploadDate; + self.aliases = doc.aliases; + self.length = doc.length; + self.metadata = doc.metadata; + self.internalMd5 = doc.md5; + } else { + self.fileId = self.fileId instanceof ObjectID ? self.fileId : new ObjectID(); + self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE; + self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize; + self.length = 0; + } + + // Process the mode of the object + if(self.mode == "r") { + nthChunk(self, 0, function(err, chunk) { + self.currentChunk = chunk; + self.position = 0; + callback(null, self); + }); + } else if(self.mode == "w") { + // Delete any existing chunks + deleteChunks(self, function(err, result) { + self.currentChunk = new Chunk(self, {'n':0}); + self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type']; + self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size']; + self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata']; + self.position = 0; + callback(null, self); + }); + } else if(self.mode == "w+") { + nthChunk(self, lastChunkNumber(self), function(err, chunk) { + // Set the current chunk + self.currentChunk = chunk == null ? new Chunk(self, {'n':0}) : chunk; + self.currentChunk.position = self.currentChunk.data.length(); + self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata']; + self.position = self.length; + callback(null, self); + }); + } + }); + }); + } else { + // Write only mode + self.fileId = new ObjectID(); + self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE; + self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize; + self.length = 0; + + self.chunkCollection(function(err, collection2) { + // No file exists set up write mode + if(self.mode == "w") { + // Delete any existing chunks + deleteChunks(self, function(err, result) { + self.currentChunk = new Chunk(self, {'n':0}); + self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type']; + self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size']; + self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata']; + self.position = 0; + callback(null, self); + }); + } else if(self.mode == "w+") { + nthChunk(self, lastChunkNumber(self), function(err, chunk) { + // Set the current chunk + self.currentChunk = chunk == null ? new Chunk(self, {'n':0}) : chunk; + self.currentChunk.position = self.currentChunk.data.length(); + self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata']; + self.position = self.length; + callback(null, self); + }); + } + }); + }; + }); +}; + +/** + * Stores a file from the file system to the GridFS database. + * + * @param {String|Buffer|FileHandle} file the file to store. + * @param {Function} callback this will be called after this method is executed. The first parameter will be null and the the second will contain the reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.writeFile = function (file, callback) { + var self = this; + if (typeof file === 'string') { + fs.open(file, 'r', 0666, function (err, fd) { + // TODO Handle err + self.writeFile(fd, callback); + }); + return; + } + + self.open(function (err, self) { + fs.fstat(file, function (err, stats) { + var offset = 0; + var index = 0; + var numberOfChunksLeft = Math.min(stats.size / self.chunkSize); + + // Write a chunk + var writeChunk = function() { + fs.read(file, self.chunkSize, offset, 'binary', function(err, data, bytesRead) { + offset = offset + bytesRead; + // Create a new chunk for the data + var chunk = new Chunk(self, {n:index++}); + chunk.write(data, function(err, chunk) { + chunk.save(function(err, result) { + // Point to current chunk + self.currentChunk = chunk; + + if(offset >= stats.size) { + fs.close(file); + self.close(function(err, result) { + return callback(null, result); + }) + } else { + return process.nextTick(writeChunk); + } + }); + }); + }); + } + + // Process the first write + process.nextTick(writeChunk); + }); + }); +}; + +/** + * Writes some data. This method will work properly only if initialized with mode "w" or "w+". + * + * @param {String|Buffer} data the data to write. + * @param {Boolean} [close] closes this file after writing if set to true. + * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.write = function(data, close, callback) { + // If we have a buffer write it using the writeBuffer method + if(Buffer.isBuffer(data)) return writeBuffer(this, data, close, callback); + // Otherwise check for the callback + if(typeof close === "function") { callback = close; close = null; } + var self = this; + var finalClose = close == null ? false : close; + // Otherwise let's write the data + if(self.mode[0] != "w") { + callback(new Error((self.referenceBy == REFERENCE_BY_ID ? self.toHexString() : self.filename) + " not opened for writing"), null); + } else { + if((self.currentChunk.position + data.length) > self.chunkSize) { + var previousChunkNumber = self.currentChunk.chunkNumber; + var leftOverDataSize = self.chunkSize - self.currentChunk.position; + var previousChunkData = data.slice(0, leftOverDataSize); + var leftOverData = data.slice(leftOverDataSize, (data.length - leftOverDataSize)); + // Save out current Chunk as another variable and assign a new Chunk for overflow data + var saveChunk = self.currentChunk; + // Create a new chunk at once (avoid wrong writing of chunks) + self.currentChunk = new Chunk(self, {'n': (previousChunkNumber + 1)}); + + // Let's finish the current chunk and then call write again for the remaining data + saveChunk.write(previousChunkData, function(err, chunk) { + chunk.save(function(err, result) { + self.position = self.position + leftOverDataSize; + // Write the remaining data + self.write(leftOverData, function(err, gridStore) { + if(finalClose) { + self.close(function(err, result) { + callback(null, gridStore); + }); + } else { + callback(null, gridStore); + } + }); + }); + }); + } else { + self.currentChunk.write(data, function(err, chunk) { + self.position = self.position + data.length; + if(finalClose) { + self.close(function(err, result) { + callback(null, self); + }); + } else { + callback(null, self); + } + }); + } + } +}; + +/** + * Writes some data. This method will work properly only if initialized with mode + * "w" or "w+". + * + * @param string {string} The data to write. + * @param close {boolean=false} opt_argument Closes this file after writing if + * true. + * @param callback {function(*, GridStore)} This will be called after executing + * this method. The first parameter will contain null and the second one + * will contain a reference to this object. + * + * @ignore + * @api private + */ +var writeBuffer = function(self, buffer, close, callback) { + if(typeof close === "function") { callback = close; close = null; } + var finalClose = (close == null) ? false : close; + + if(self.mode[0] != "w") { + callback(new Error((self.referenceBy == REFERENCE_BY_ID ? self.toHexString() : self.filename) + " not opened for writing"), null); + } else { + if((self.currentChunk.position + buffer.length) > self.chunkSize) { + // Data exceeds current chunk remaining free size; fill up current chunk and write the rest + // to a new chunk (recursively) + var previousChunkNumber = self.currentChunk.chunkNumber; + var leftOverDataSize = self.chunkSize - self.currentChunk.position; + var firstChunkData = buffer.slice(0, leftOverDataSize); + var leftOverData = buffer.slice(leftOverDataSize); + // Save out current Chunk as another variable and assign a new Chunk for overflow data + var saveChunk = self.currentChunk; + // Create a new chunk at once (avoid wrong writing of chunks) + self.currentChunk = new Chunk(self, {'n': (previousChunkNumber + 1)}); + + // Let's finish the current chunk and then call write again for the remaining data + saveChunk.write(firstChunkData, function(err, chunk) { + chunk.save(function(err, result) { + self.position = self.position + leftOverDataSize; + + // Write the remaining data + writeBuffer(self, leftOverData, function(err, gridStore) { + if(finalClose) { + self.close(function(err, result) { + callback(null, gridStore); + }); + } + else { + callback(null, gridStore); + } + }); + }); + }); + } + else { + // Write buffer to chunk all at once + self.currentChunk.write(buffer, function(err, chunk) { + self.position = self.position + buffer.length; + if(finalClose) { + self.close(function(err, result) { + callback(null, self); + }); + } + else { + callback(null, self); + } + }); + } + } +}; + +/** + * Creates a mongoDB object representation of this object. + * + * @param callback {function(object)} This will be called after executing this + * method. The object will be passed to the first parameter and will have + * the structure: + * + * <pre><code> + * { + * '_id' : , // {number} id for this file + * 'filename' : , // {string} name for this file + * 'contentType' : , // {string} mime type for this file + * 'length' : , // {number} size of this file? + * 'chunksize' : , // {number} chunk size used by this file + * 'uploadDate' : , // {Date} + * 'aliases' : , // {array of string} + * 'metadata' : , // {string} + * } + * </code></pre> + * + * @ignore + * @api private + */ +var buildMongoObject = function(self, callback) { + var length = self.currentChunk != null ? (self.currentChunk.chunkNumber * self.chunkSize + self.currentChunk.position) : 0; + var mongoObject = { + '_id': self.fileId, + 'filename': self.filename, + 'contentType': self.contentType, + 'length': length < 0 ? 0 : length, + 'chunkSize': self.chunkSize, + 'uploadDate': self.uploadDate, + 'aliases': self.aliases, + 'metadata': self.metadata + }; + + var md5Command = {filemd5:self.fileId, root:self.root}; + self.db.command(md5Command, function(err, results) { + mongoObject.md5 = results.md5; + callback(mongoObject); + }); +}; + +/** + * Saves this file to the database. This will overwrite the old entry if it + * already exists. This will work properly only if mode was initialized to + * "w" or "w+". + * + * @param {Function} callback this will be called after executing this method. Passes an **{Error}** object to the first parameter and null to the second if an error occured. Otherwise, passes null to the first and a reference to this object to the second. + * @return {null} + * @api public + */ +GridStore.prototype.close = function(callback) { + var self = this; + + if(self.mode[0] == "w") { + if(self.currentChunk != null && self.currentChunk.position > 0) { + self.currentChunk.save(function(err, chuck) { + self.collection(function(err, files) { + // Build the mongo object + if(self.uploadDate != null) { + files.remove({'_id':self.fileId}, {safe:true}, function(err, collection) { + buildMongoObject(self, function(mongoObject) { + files.save(mongoObject, {safe:true}, function(err, doc) { + callback(err, mongoObject); + }); + }); + }); + } else { + self.uploadDate = new Date(); + buildMongoObject(self, function(mongoObject) { + files.save(mongoObject, {safe:true}, function(err, doc) { + callback(err, mongoObject); + }); + }); + } + }); + }); + } else { + self.collection(function(err, files) { + self.uploadDate = new Date(); + buildMongoObject(self, function(mongoObject) { + files.save(mongoObject, {safe:true}, function(err, doc) { + callback(err, mongoObject); + }); + }); + }); + } + } else if(self.mode[0] == "r") { + callback(null, null); + } else { + callback(new Error("Illegal mode " + self.mode), null); + } +}; + +/** + * Gets the nth chunk of this file. + * + * @param chunkNumber {number} The nth chunk to retrieve. + * @param callback {function(*, Chunk|object)} This will be called after + * executing this method. null will be passed to the first parameter while + * a new {@link Chunk} instance will be passed to the second parameter if + * the chunk was found or an empty object {} if not. + * + * @ignore + * @api private + */ +var nthChunk = function(self, chunkNumber, callback) { + self.chunkCollection(function(err, collection) { + collection.find({'files_id':self.fileId, 'n':chunkNumber}, function(err, cursor) { + cursor.nextObject(function(err, chunk) { + var finalChunk = chunk == null ? {} : chunk; + callback(null, new Chunk(self, finalChunk)); + }); + }); + }); +}; + +/** + * + * @ignore + * @api private + */ +GridStore.prototype._nthChunk = function(chunkNumber, callback) { + nthChunk(this, chunkNumber, callback); +} + +/** + * @return {Number} The last chunk number of this file. + * + * @ignore + * @api private + */ +var lastChunkNumber = function(self) { + return Math.floor(self.length/self.chunkSize); +}; + +/** + * Retrieve this file's chunks collection. + * + * @param {Function} callback this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new **{Collection}** object will be passed to the second parameter if no error occured. + * @return {null} + * @api public + */ +GridStore.prototype.chunkCollection = function(callback) { + this.db.collection((this.root + ".chunks"), callback); +}; + +/** + * Deletes all the chunks of this file in the database. + * + * @param callback {function(*, boolean)} This will be called after this method + * executes. Passes null to the first and true to the second argument. + * + * @ignore + * @api private + */ +var deleteChunks = function(self, callback) { + if(self.fileId != null) { + self.chunkCollection(function(err, collection) { + if(err!==null) { + callback(err, false); + } + collection.remove({'files_id':self.fileId}, {safe:true}, function(err, result) { + callback(null, true); + }); + }); + } else { + callback(null, true); + } +}; + +/** + * Deletes all the chunks of this file in the database. + * + * @param {Function} callback this will be called after this method executes. Passes null to the first and true to the second argument. + * @return {null} + * @api public + */ +GridStore.prototype.unlink = function(callback) { + var self = this; + deleteChunks(this, function(err) { + if(err!==null) { + callback("at deleteChunks: "+err); + return; + } + + self.collection(function(err, collection) { + if(err!==null) { + callback("at collection: "+err); + return; + } + + collection.remove({'_id':self.fileId}, {safe:true}, function(err, collection) { + callback(err, self); + }); + }); + }); +}; + +/** + * Retrieves the file collection associated with this object. + * + * @param {Function} callback this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new **{Collection}** object will be passed to the second parameter if no error occured. + * @return {null} + * @api public + */ +GridStore.prototype.collection = function(callback) { + this.db.collection(this.root + ".files", function(err, collection) { + callback(err, collection); + }); +}; + +/** + * Reads the data of this file. + * + * @param {String} [separator] the character to be recognized as the newline separator. + * @param {Function} callback This will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character. + * @return {null} + * @api public + */ +GridStore.prototype.readlines = function(separator, callback) { + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + separator = args.length ? args.shift() : "\n"; + + this.read(function(err, data) { + var items = data.toString().split(separator); + items = items.length > 0 ? items.splice(0, items.length - 1) : []; + for(var i = 0; i < items.length; i++) { + items[i] = items[i] + separator; + } + + callback(null, items); + }); +}; + +/** + * Deletes all the chunks of this file in the database if mode was set to "w" or + * "w+" and resets the read/write head to the initial position. + * + * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.rewind = function(callback) { + var self = this; + + if(this.currentChunk.chunkNumber != 0) { + if(this.mode[0] == "w") { + deleteChunks(self, function(err, gridStore) { + self.currentChunk = new Chunk(self, {'n': 0}); + self.position = 0; + callback(null, self); + }); + } else { + self.currentChunk(0, function(err, chunk) { + self.currentChunk = chunk; + self.currentChunk.rewind(); + self.position = 0; + callback(null, self); + }); + } + } else { + self.currentChunk.rewind(); + self.position = 0; + callback(null, self); + } +}; + +/** + * Retrieves the contents of this file and advances the read/write head. Works with Buffers only. + * + * There are 3 signatures for this method: + * + * (callback) + * (length, callback) + * (length, buffer, callback) + * + * @param {Number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified. + * @param {String|Buffer} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method. + * @param {Function} callback this will be called after this method is executed. null will be passed to the first parameter and a string containing the contents of the buffer concatenated with the contents read from this file will be passed to the second. + * @return {null} + * @api public + */ +GridStore.prototype.read = function(length, buffer, callback) { + var self = this; + + var args = Array.prototype.slice.call(arguments, 0); + callback = args.pop(); + length = args.length ? args.shift() : null; + buffer = args.length ? args.shift() : null; + + // The data is a c-terminated string and thus the length - 1 + var finalLength = length == null ? self.length - self.position : length; + var finalBuffer = buffer == null ? new Buffer(finalLength) : buffer; + // Add a index to buffer to keep track of writing position or apply current index + finalBuffer._index = buffer != null && buffer._index != null ? buffer._index : 0; + + if((self.currentChunk.length() - self.currentChunk.position + 1 + finalBuffer._index) >= finalLength) { + var slice = self.currentChunk.readSlice(finalLength - finalBuffer._index); + // Copy content to final buffer + slice.copy(finalBuffer, finalBuffer._index); + // Update internal position + self.position = finalBuffer.length; + // Check if we don't have a file at all + if(finalLength == 0 && finalBuffer.length == 0) return callback(new Error("File does not exist"), null); + // Else return data + callback(null, finalBuffer); + } else { + var slice = self.currentChunk.readSlice(self.currentChunk.length()); + // Copy content to final buffer + slice.copy(finalBuffer, finalBuffer._index); + // Update index position + finalBuffer._index += slice.length; + + // Load next chunk and read more + nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) { + if(chunk.length() > 0) { + self.currentChunk = chunk; + self.read(length, finalBuffer, callback); + } else { + finalBuffer._index > 0 ? callback(null, finalBuffer) : callback(new Error("no chunks found for file, possibly corrupt"), null); + } + }); + } +} + +/** + * Retrieves the position of the read/write head of this file. + * + * @param {Function} callback This gets called after this method terminates. null is passed to the first parameter and the position is passed to the second. + * @return {null} + * @api public + */ +GridStore.prototype.tell = function(callback) { + callback(null, this.position); +}; + +/** + * Moves the read/write head to a new location. + * + * There are 3 signatures for this method + * + * Seek Location Modes + * - **GridStore.IO_SEEK_SET**, **(default)** set the position from the start of the file. + * - **GridStore.IO_SEEK_CUR**, set the position from the current position in the file. + * - **GridStore.IO_SEEK_END**, set the position from the end of the file. + * + * @param {Number} [position] the position to seek to + * @param {Number} [seekLocation] seek mode. Use one of the Seek Location modes. + * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.seek = function(position, seekLocation, callback) { + var self = this; + + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + seekLocation = args.length ? args.shift() : null; + + var seekLocationFinal = seekLocation == null ? exports.GridStore.IO_SEEK_SET : seekLocation; + var finalPosition = position; + var targetPosition = 0; + if(seekLocationFinal == exports.GridStore.IO_SEEK_CUR) { + targetPosition = self.position + finalPosition; + } else if(seekLocationFinal == exports.GridStore.IO_SEEK_END) { + targetPosition = self.length + finalPosition; + } else { + targetPosition = finalPosition; + } + + var newChunkNumber = Math.floor(targetPosition/self.chunkSize); + if(newChunkNumber != self.currentChunk.chunkNumber) { + var seekChunk = function() { + nthChunk(self, newChunkNumber, function(err, chunk) { + self.currentChunk = chunk; + self.position = targetPosition; + self.currentChunk.position = (self.position % self.chunkSize); + callback(null, self); + }); + }; + + if(self.mode[0] == 'w') { + self.currentChunk.save(function(err, chunk) { + seekChunk(); + }); + } else { + seekChunk(); + } + } else { + self.position = targetPosition; + self.currentChunk.position = (self.position % self.chunkSize); + callback(null, self); + } +}; + +/** + * Verify if the file is at EOF. + * + * @return {Boolean} true if the read/write head is at the end of this file. + * @api public + */ +GridStore.prototype.eof = function() { + return this.position == this.length ? true : false; +}; + +/** + * Retrieves a single character from this file. + * + * @param {Function} callback this gets called after this method is executed. Passes null to the first parameter and the character read to the second or null to the second if the read/write head is at the end of the file. + * @return {null} + * @api public + */ +GridStore.prototype.getc = function(callback) { + var self = this; + + if(self.eof()) { + callback(null, null); + } else if(self.currentChunk.eof()) { + nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) { + self.currentChunk = chunk; + self.position = self.position + 1; + callback(null, self.currentChunk.getc()); + }); + } else { + self.position = self.position + 1; + callback(null, self.currentChunk.getc()); + } +}; + +/** + * Writes a string to the file with a newline character appended at the end if + * the given string does not have one. + * + * @param {String} string the string to write. + * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object. + * @return {null} + * @api public + */ +GridStore.prototype.puts = function(string, callback) { + var finalString = string.match(/\n$/) == null ? string + "\n" : string; + this.write(finalString, callback); +}; + +/** + * Returns read stream based on this GridStore file + * + * Events + * - **data** {function(item) {}} the data event triggers when a document is ready. + * - **end** {function() {}} the end event triggers when there is no more documents available. + * - **close** {function() {}} the close event triggers when the stream is closed. + * - **error** {function(err) {}} the error event triggers if an error happens. + * + * @param {Boolean} autoclose if true current GridStore will be closed when EOF and 'close' event will be fired + * @return {null} + * @api public + */ +GridStore.prototype.stream = function(autoclose) { + return new ReadStream(autoclose, this); +}; + +/** +* The collection to be used for holding the files and chunks collection. +* +* @classconstant DEFAULT_ROOT_COLLECTION +**/ +GridStore.DEFAULT_ROOT_COLLECTION = 'fs'; + +/** +* Default file mime type +* +* @classconstant DEFAULT_CONTENT_TYPE +**/ +GridStore.DEFAULT_CONTENT_TYPE = 'binary/octet-stream'; + +/** +* Seek mode where the given length is absolute. +* +* @classconstant IO_SEEK_SET +**/ +GridStore.IO_SEEK_SET = 0; + +/** +* Seek mode where the given length is an offset to the current read/write head. +* +* @classconstant IO_SEEK_CUR +**/ +GridStore.IO_SEEK_CUR = 1; + +/** +* Seek mode where the given length is an offset to the end of the file. +* +* @classconstant IO_SEEK_END +**/ +GridStore.IO_SEEK_END = 2; + +/** + * Checks if a file exists in the database. + * + * @param {Db} db the database to query. + * @param {String} name the name of the file to look for. + * @param {String} [rootCollection] the root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**. + * @param {Function} callback this will be called after this method executes. Passes null to the first and passes true to the second if the file exists and false otherwise. + * @return {null} + * @api public + */ +GridStore.exist = function(db, fileIdObject, rootCollection, callback) { + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + rootCollection = args.length ? args.shift() : null; + + // Fetch collection + var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION; + db.collection(rootCollectionFinal + ".files", function(err, collection) { + // Build query + var query = (typeof fileIdObject == 'string' || Object.prototype.toString.call(fileIdObject) == '[object RegExp]' ) + ? {'filename':fileIdObject} : {'_id':fileIdObject}; // Attempt to locate file + collection.find(query, function(err, cursor) { + cursor.nextObject(function(err, item) { + callback(null, item == null ? false : true); + }); + }); + }); +}; + +/** + * Gets the list of files stored in the GridFS. + * + * @param {Db} db the database to query. + * @param {String} [rootCollection] the root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**. + * @param {Function} callback this will be called after this method executes. Passes null to the first and passes an array of strings containing the names of the files. + * @return {null} + * @api public + */ +GridStore.list = function(db, rootCollection, options, callback) { + var args = Array.prototype.slice.call(arguments, 1); + callback = args.pop(); + rootCollection = args.length ? args.shift() : null; + options = args.length ? args.shift() : {}; + + // Ensure we have correct values + if(rootCollection != null && typeof rootCollection == 'object') { + options = rootCollection; + rootCollection = null; + } + + // Check if we are returning by id not filename + var byId = options['id'] != null ? options['id'] : false; + // Fetch item + var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION; + var items = []; + db.collection((rootCollectionFinal + ".files"), function(err, collection) { + collection.find(function(err, cursor) { + cursor.each(function(err, item) { + if(item != null) { + items.push(byId ? item._id : item.filename); + } else { + callback(null, items); + } + }); + }); + }); +}; + +/** + * Reads the contents of a file. + * + * This method has the following signatures + * + * (db, name, callback) + * (db, name, length, callback) + * (db, name, length, offset, callback) + * (db, name, length, offset, options, callback) + * + * @param {Db} db the database to query. + * @param {String} name the name of the file. + * @param {Number} [length] the size of data to read. + * @param {Number} [offset] the offset from the head of the file of which to start reading from. + * @param {Object} [options] the options for the file. + * @param {Function} callback this will be called after this method executes. A string with an error message will be passed to the first parameter when the length and offset combination exceeds the length of the file while an Error object will be passed if other forms of error occured, otherwise, a string is passed. The second parameter will contain the data read if successful or null if an error occured. + * @return {null} + * @api public + */ +GridStore.read = function(db, name, length, offset, options, callback) { + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + length = args.length ? args.shift() : null; + offset = args.length ? args.shift() : null; + options = args.length ? args.shift() : null; + + new GridStore(db, name, "r", options).open(function(err, gridStore) { + // Make sure we are not reading out of bounds + if(offset && offset >= gridStore.length) return callback("offset larger than size of file", null); + if(length && length > gridStore.length) return callback("length is larger than the size of the file", null); + if(offset && length && (offset + length) > gridStore.length) return callback("offset and length is larger than the size of the file", null); + + if(offset != null) { + gridStore.seek(offset, function(err, gridStore) { + gridStore.read(length, function(err, data) { + callback(err, data); + }); + }); + } else { + gridStore.read(length, function(err, data) { + callback(err, data); + }); + } + }); +}; + +/** + * Reads the data of this file. + * + * @param {Db} db the database to query. + * @param {String} name the name of the file. + * @param {String} [separator] the character to be recognized as the newline separator. + * @param {Object} [options] file options. + * @param {Function} callback this will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character. + * @return {null} + * @api public + */ +GridStore.readlines = function(db, name, separator, options, callback) { + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + separator = args.length ? args.shift() : null; + options = args.length ? args.shift() : null; + + var finalSeperator = separator == null ? "\n" : separator; + new GridStore(db, name, "r", options).open(function(err, gridStore) { + gridStore.readlines(finalSeperator, function(err, lines) { + callback(err, lines); + }); + }); +}; + +/** + * Deletes the chunks and metadata information of a file from GridFS. + * + * @param {Db} db the database to interact with. + * @param {String|Array} names the name/names of the files to delete. + * @param {Object} [options] the options for the files. + * @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object. + * @return {null} + * @api public + */ +GridStore.unlink = function(db, names, options, callback) { + var self = this; + var args = Array.prototype.slice.call(arguments, 2); + callback = args.pop(); + options = args.length ? args.shift() : null; + + if(names.constructor == Array) { + var tc = 0; + for(var i = 0; i < names.length; i++) { + ++tc; + self.unlink(db, names[i], function(result) { + if(--tc == 0) { + callback(null, self); + } + }); + } + } else { + new GridStore(db, names, "w", options).open(function(err, gridStore) { + deleteChunks(gridStore, function(err, result) { + gridStore.collection(function(err, collection) { + collection.remove({'_id':gridStore.fileId}, {safe:true}, function(err, collection) { + callback(err, self); + }); + }); + }); + }); + } +}; + +/** + * @ignore + * @api private + */ +exports.GridStore = GridStore; diff --git a/node_modules/mongodb/lib/mongodb/gridfs/readstream.js b/node_modules/mongodb/lib/mongodb/gridfs/readstream.js new file mode 100644 index 0000000..2d3e574 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/gridfs/readstream.js @@ -0,0 +1,179 @@ +var Stream = require('stream').Stream, + util = require('util'); + +/** + * ReadStream + * + * Returns a stream interface for the **file**. + * + * Events + * - **data** {function(item) {}} the data event triggers when a document is ready. + * - **end** {function() {}} the end event triggers when there is no more documents available. + * - **close** {function() {}} the close event triggers when the stream is closed. + * - **error** {function(err) {}} the error event triggers if an error happens. + * + * @class Represents a GridFS File Stream. + * @param {Boolean} autoclose automatically close file when the stream reaches the end. + * @param {GridStore} cursor a cursor object that the stream wraps. + * @return {ReadStream} + */ +function ReadStream(autoclose, gstore) { + if (!(this instanceof ReadStream)) return new ReadStream(autoclose, gstore); + Stream.call(this); + + this.autoclose = !!autoclose; + this.gstore = gstore; + + this.finalLength = gstore.length - gstore.position; + this.completedLength = 0; + + this.paused = false; + this.readable = true; + this.pendingChunk = null; + this.executing = false; + + var self = this; + process.nextTick(function() { + self._execute(); + }); +}; + +/** + * Inherit from Stream + * @ignore + * @api private + */ +ReadStream.prototype.__proto__ = Stream.prototype; + +/** + * Flag stating whether or not this stream is readable. + */ +ReadStream.prototype.readable; + +/** + * Flag stating whether or not this stream is paused. + */ +ReadStream.prototype.paused; + +/** + * @ignore + * @api private + */ +ReadStream.prototype._execute = function() { + if(this.paused === true || this.readable === false) { + return; + } + + var gstore = this.gstore; + var self = this; + // Set that we are executing + this.executing = true; + + var last = false; + var toRead = 0; + + if ((gstore.currentChunk.length() - gstore.currentChunk.position + 1 + self.completedLength) >= self.finalLength) { + toRead = self.finalLength - self.completedLength; + self.executing = false; + last = true; + } else { + toRead = gstore.currentChunk.length(); + } + + var data = gstore.currentChunk.readSlice(toRead); + + if(data != null) { + self.completedLength += data.length; + self.pendingChunk = null; + self.emit("data", data); + } + + if(last === true) { + self.readable = false; + self.emit("end"); + + if(self.autoclose === true) { + if(gstore.mode[0] == "w") { + gstore.close(function(err, doc) { + if (err) { + self.emit("error", err); + return; + } + self.readable = false; + self.emit("close", doc); + }); + } else { + self.readable = false; + self.emit("close"); + } + } + } else { + gstore._nthChunk(gstore.currentChunk.chunkNumber + 1, function(err, chunk) { + if(err) { + self.readable = false; + self.emit("error", err); + self.executing = false; + return; + } + + self.pendingChunk = chunk; + if(self.paused === true) { + self.executing = false; + return; + } + + gstore.currentChunk = self.pendingChunk; + self._execute(); + }); + } +}; + +/** + * Pauses this stream, then no farther events will be fired. + * + * @ignore + * @api public + */ +ReadStream.prototype.pause = function() { + if(!this.executing) { + this.paused = true; + } +}; + +/** + * Destroys the stream, then no farther events will be fired. + * + * @ignore + * @api public + */ +ReadStream.prototype.destroy = function() { + this.readable = false; + // Emit close event + this.emit("close"); +}; + +/** + * Resumes this stream. + * + * @ignore + * @api public + */ +ReadStream.prototype.resume = function() { + if(this.paused === false || !this.readable) { + return; + } + + this.paused = false; + var self = this; + if(self.pendingChunk != null) { + self.currentChunk = self.pendingChunk; + process.nextTick(function() { + self._execute(); + }); + } else { + self.readable = false; + self.emit("close"); + } +}; + +exports.ReadStream = ReadStream; diff --git a/node_modules/mongodb/lib/mongodb/index.js b/node_modules/mongodb/lib/mongodb/index.js new file mode 100644 index 0000000..721e2f3 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/index.js @@ -0,0 +1,142 @@ +try { + exports.BSONPure = require('bson').BSONPure; + exports.BSONNative = require('bson').BSONNative; +} catch(err) { + // do nothing +} + +[ 'commands/base_command' + , 'commands/db_command' + , 'commands/delete_command' + , 'commands/get_more_command' + , 'commands/insert_command' + , 'commands/kill_cursor_command' + , 'commands/query_command' + , 'commands/update_command' + , 'responses/mongo_reply' + , 'admin' + , 'collection' + , 'connection/connection' + , 'connection/server' + , 'connection/repl_set_servers' + , 'cursor' + , 'db' + , 'gridfs/grid' + , 'gridfs/chunk' + , 'gridfs/gridstore'].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + exports[i] = module[i]; + } + + // Add BSON Classes + exports.Binary = require('bson').Binary; + exports.Code = require('bson').Code; + exports.DBRef = require('bson').DBRef; + exports.Double = require('bson').Double; + exports.Long = require('bson').Long; + exports.MinKey = require('bson').MinKey; + exports.MaxKey = require('bson').MaxKey; + exports.ObjectID = require('bson').ObjectID; + exports.Symbol = require('bson').Symbol; + exports.Timestamp = require('bson').Timestamp; + + // Add BSON Parser + exports.BSON = require('bson').BSONPure.BSON; +}); + +// Exports all the classes for the PURE JS BSON Parser +exports.pure = function() { + var classes = {}; + // Map all the classes + [ 'commands/base_command' + , 'commands/db_command' + , 'commands/delete_command' + , 'commands/get_more_command' + , 'commands/insert_command' + , 'commands/kill_cursor_command' + , 'commands/query_command' + , 'commands/update_command' + , 'responses/mongo_reply' + , 'admin' + , 'collection' + , 'connection/connection' + , 'connection/server' + , 'connection/repl_set_servers' + , 'cursor' + , 'db' + , 'gridfs/grid' + , 'gridfs/chunk' + , 'gridfs/gridstore'].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + classes[i] = module[i]; + } + }); + + // Add BSON Classes + classes.Binary = require('bson').Binary; + classes.Code = require('bson').Code; + classes.DBRef = require('bson').DBRef; + classes.Double = require('bson').Double; + classes.Long = require('bson').Long; + classes.MinKey = require('bson').MinKey; + classes.MaxKey = require('bson').MaxKey; + classes.ObjectID = require('bson').ObjectID; + classes.Symbol = require('bson').Symbol; + classes.Timestamp = require('bson').Timestamp; + + // Add BSON Parser + classes.BSON = require('bson').BSONPure.BSON; + + // Return classes list + return classes; +} + +// Exports all the classes for the PURE JS BSON Parser +exports.native = function() { + var classes = {}; + // Map all the classes + [ 'commands/base_command' + , 'commands/db_command' + , 'commands/delete_command' + , 'commands/get_more_command' + , 'commands/insert_command' + , 'commands/kill_cursor_command' + , 'commands/query_command' + , 'commands/update_command' + , 'responses/mongo_reply' + , 'admin' + , 'collection' + , 'connection/connection' + , 'connection/server' + , 'connection/repl_set_servers' + , 'cursor' + , 'db' + , 'gridfs/grid' + , 'gridfs/chunk' + , 'gridfs/gridstore'].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + classes[i] = module[i]; + } + }); + + // Add BSON Classes + classes.Binary = require('bson').Binary; + classes.Code = require('bson').Code; + classes.DBRef = require('bson').DBRef; + classes.Double = require('bson').Double; + classes.Long = require('bson').Long; + classes.MinKey = require('bson').MinKey; + classes.MaxKey = require('bson').MaxKey; + classes.ObjectID = require('bson').ObjectID; + classes.Symbol = require('bson').Symbol; + classes.Timestamp = require('bson').Timestamp; + + // Add BSON Parser + classes.BSON = require('bson').BSONNative.BSON; + + // Return classes list + return classes; +} diff --git a/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js b/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js new file mode 100644 index 0000000..74396fa --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js @@ -0,0 +1,131 @@ +var Long = require('bson').Long; + +/** + Reply message from mongo db +**/ +var MongoReply = exports.MongoReply = function() { + this.documents = []; + this.index = 0; +}; + +MongoReply.prototype.parseHeader = function(binary_reply, bson) { + // Unpack the standard header first + this.messageLength = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + // Fetch the request id for this reply + this.requestId = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + // Fetch the id of the request that triggered the response + this.responseTo = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + // Skip op-code field + this.index = this.index + 4 + 4; + // Unpack the reply message + this.responseFlag = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + // Unpack the cursor id (a 64 bit long integer) + var low_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + var high_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + this.cursorId = new Long(low_bits, high_bits); + // Unpack the starting from + this.startingFrom = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; + // Unpack the number of objects returned + this.numberReturned = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + this.index = this.index + 4; +} + +MongoReply.prototype.parseBody = function(binary_reply, bson, raw, callback) { + raw = raw == null ? false : raw; + // Just set a doc limit for deserializing + var docLimitSize = 1024*20; + + // If our message length is very long, let's switch to process.nextTick for messages + if(this.messageLength > docLimitSize) { + var batchSize = this.numberReturned; + this.documents = new Array(this.numberReturned); + + // Just walk down until we get a positive number >= 1 + for(var i = 50; i > 0; i--) { + if((this.numberReturned/i) >= 1) { + batchSize = i; + break; + } + } + + // Actual main creator of the processFunction setting internal state to control the flow + var parseFunction = function(_self, _binary_reply, _batchSize, _numberReturned) { + var object_index = 0; + // Internal loop process that will use nextTick to ensure we yield some time + var processFunction = function() { + // Adjust batchSize if we have less results left than batchsize + if((_numberReturned - object_index) < _batchSize) { + _batchSize = _numberReturned - object_index; + } + + // If raw just process the entries + if(raw) { + // Iterate over the batch + for(var i = 0; i < _batchSize; i++) { + // Are we done ? + if(object_index <= _numberReturned) { + // Read the size of the bson object + var bsonObjectSize = _binary_reply[_self.index] | _binary_reply[_self.index + 1] << 8 | _binary_reply[_self.index + 2] << 16 | _binary_reply[_self.index + 3] << 24; + // If we are storing the raw responses to pipe straight through + _self.documents[object_index] = binary_reply.slice(_self.index, _self.index + bsonObjectSize); + // Adjust binary index to point to next block of binary bson data + _self.index = _self.index + bsonObjectSize; + // Update number of docs parsed + object_index = object_index + 1; + } + } + } else { + // Parse documents + _self.index = bson.deserializeStream(binary_reply, _self.index, _batchSize, _self.documents, object_index); + // Adjust index + object_index = object_index + _batchSize; + } + + // If we hav more documents process NextTick + if(object_index < _numberReturned) { + process.nextTick(processFunction); + } else { + callback(null); + } + } + + // Return the process function + return processFunction; + }(this, binary_reply, batchSize, this.numberReturned)(); + } else { + // Let's unpack all the bson documents, deserialize them and store them + for(var object_index = 0; object_index < this.numberReturned; object_index++) { + // Read the size of the bson object + var bsonObjectSize = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24; + // If we are storing the raw responses to pipe straight through + if(raw) { + // Deserialize the object and add to the documents array + this.documents.push(binary_reply.slice(this.index, this.index + bsonObjectSize)); + } else { + // Deserialize the object and add to the documents array + this.documents.push(bson.deserialize(binary_reply.slice(this.index, this.index + bsonObjectSize))); + } + // Adjust binary index to point to next block of binary bson data + this.index = this.index + bsonObjectSize; + } + + callback(null); + } +} + +MongoReply.prototype.is_error = function(){ + if(this.documents.length == 1) { + return this.documents[0].ok == 1 ? false : true; + } + return false; +}; + +MongoReply.prototype.error_message = function() { + return this.documents.length == 1 && this.documents[0].ok == 1 ? '' : this.documents[0].errmsg; +};
\ No newline at end of file diff --git a/node_modules/mongodb/lib/mongodb/utils.js b/node_modules/mongodb/lib/mongodb/utils.js new file mode 100644 index 0000000..07a0ab0 --- /dev/null +++ b/node_modules/mongodb/lib/mongodb/utils.js @@ -0,0 +1,74 @@ +/** + * Sort functions, Normalize and prepare sort parameters + */ +var formatSortValue = exports.formatSortValue = function(sortDirection) { + var value = ("" + sortDirection).toLowerCase(); + + switch (value) { + case 'ascending': + case 'asc': + case '1': + return 1; + case 'descending': + case 'desc': + case '-1': + return -1; + default: + throw new Error("Illegal sort clause, must be of the form " + + "[['field1', '(ascending|descending)'], " + + "['field2', '(ascending|descending)']]"); + } +}; + +var formattedOrderClause = exports.formattedOrderClause = function(sortValue) { + var orderBy = {}; + + if (Array.isArray(sortValue)) { + for(var i = 0; i < sortValue.length; i++) { + if(sortValue[i].constructor == String) { + orderBy[sortValue[i]] = 1; + } else { + orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]); + } + } + } else if(Object.prototype.toString.call(sortValue) === '[object Object]') { + orderBy = sortValue; + } else if (sortValue.constructor == String) { + orderBy[sortValue] = 1; + } else { + throw new Error("Illegal sort clause, must be of the form " + + "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"); + } + + return orderBy; +}; + +exports.encodeInt = function(value) { + var buffer = new Buffer(4); + buffer[3] = (value >> 24) & 0xff; + buffer[2] = (value >> 16) & 0xff; + buffer[1] = (value >> 8) & 0xff; + buffer[0] = value & 0xff; + return buffer; +} + +exports.encodeIntInPlace = function(value, buffer, index) { + buffer[index + 3] = (value >> 24) & 0xff; + buffer[index + 2] = (value >> 16) & 0xff; + buffer[index + 1] = (value >> 8) & 0xff; + buffer[index] = value & 0xff; +} + +exports.encodeCString = function(string) { + var buf = new Buffer(string, 'utf8'); + return [buf, new Buffer([0])]; +} + +exports.decodeUInt32 = function(array, index) { + return array[index] | array[index + 1] << 8 | array[index + 2] << 16 | array[index + 3] << 24; +} + +// Decode the int +exports.decodeUInt8 = function(array, index) { + return array[index]; +} diff --git a/node_modules/mongodb/node_modules/bson/.travis.yml b/node_modules/mongodb/node_modules/bson/.travis.yml new file mode 100644 index 0000000..90b208a --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 0.4 + - 0.6 + - 0.7 # development version of 0.8, may be unstable
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/Makefile b/node_modules/mongodb/node_modules/bson/Makefile new file mode 100644 index 0000000..88b1ddc --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/Makefile @@ -0,0 +1,31 @@ +NODE = node +NPM = npm +NODEUNIT = node_modules/nodeunit/bin/nodeunit +name = all + +total: build_native + +test: build_native + $(NODEUNIT) ./test/node + TEST_NATIVE=TRUE $(NODEUNIT) ./test/node + +build_native: + $(MAKE) -C ./ext all + +build_native_debug: + $(MAKE) -C ./ext all_debug + +build_native_clang: + $(MAKE) -C ./ext clang + +build_native_clang_debug: + $(MAKE) -C ./ext clang_debug + +clean_native: + $(MAKE) -C ./ext clean + +clean: + rm ./ext/bson.node + rm -r ./ext/build + +.PHONY: total diff --git a/node_modules/mongodb/node_modules/bson/README b/node_modules/mongodb/node_modules/bson/README new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/README diff --git a/node_modules/mongodb/node_modules/bson/ext/Makefile b/node_modules/mongodb/node_modules/bson/ext/Makefile new file mode 100644 index 0000000..435999e --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/ext/Makefile @@ -0,0 +1,28 @@ +NODE = node +name = all +JOBS = 1 + +all: + rm -rf build .lock-wscript bson.node + node-waf configure build + cp -R ./build/Release/bson.node . || true + +all_debug: + rm -rf build .lock-wscript bson.node + node-waf --debug configure build + cp -R ./build/Release/bson.node . || true + +clang: + rm -rf build .lock-wscript bson.node + CXX=clang node-waf configure build + cp -R ./build/Release/bson.node . || true + +clang_debug: + rm -rf build .lock-wscript bson.node + CXX=clang node-waf --debug configure build + cp -R ./build/Release/bson.node . || true + +clean: + rm -rf build .lock-wscript bson.node + +.PHONY: all
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/ext/bson.cc b/node_modules/mongodb/node_modules/bson/ext/bson.cc new file mode 100644 index 0000000..8906eea --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/ext/bson.cc @@ -0,0 +1,2165 @@ +#include <assert.h> +#include <string.h> +#include <stdlib.h> + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-parameter" +#endif + +#include <v8.h> + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#include <node.h> +#include <node_version.h> +#include <node_buffer.h> +#include <cstring> +#include <cmath> +#include <cstdlib> +#include <iostream> +#include <limits> +#include <vector> + +#include "bson.h" + +using namespace v8; +using namespace node; +using namespace std; + +// BSON DATA TYPES +const uint32_t BSON_DATA_NUMBER = 1; +const uint32_t BSON_DATA_STRING = 2; +const uint32_t BSON_DATA_OBJECT = 3; +const uint32_t BSON_DATA_ARRAY = 4; +const uint32_t BSON_DATA_BINARY = 5; +const uint32_t BSON_DATA_OID = 7; +const uint32_t BSON_DATA_BOOLEAN = 8; +const uint32_t BSON_DATA_DATE = 9; +const uint32_t BSON_DATA_NULL = 10; +const uint32_t BSON_DATA_REGEXP = 11; +const uint32_t BSON_DATA_CODE = 13; +const uint32_t BSON_DATA_SYMBOL = 14; +const uint32_t BSON_DATA_CODE_W_SCOPE = 15; +const uint32_t BSON_DATA_INT = 16; +const uint32_t BSON_DATA_TIMESTAMP = 17; +const uint32_t BSON_DATA_LONG = 18; +const uint32_t BSON_DATA_MIN_KEY = 0xff; +const uint32_t BSON_DATA_MAX_KEY = 0x7f; + +const int32_t BSON_INT32_MAX = (int32_t)2147483647L; +const int32_t BSON_INT32_MIN = (int32_t)(-1) * 2147483648L; + +const int64_t BSON_INT64_MAX = ((int64_t)1 << 63) - 1; +const int64_t BSON_INT64_MIN = (int64_t)-1 << 63; + +const int64_t JS_INT_MAX = (int64_t)1 << 53; +const int64_t JS_INT_MIN = (int64_t)-1 << 53; + +static Handle<Value> VException(const char *msg) { + HandleScope scope; + return ThrowException(Exception::Error(String::New(msg))); + }; + +Persistent<FunctionTemplate> BSON::constructor_template; + +void BSON::Initialize(v8::Handle<v8::Object> target) { + // Grab the scope of the call from Node + HandleScope scope; + // Define a new function template + Local<FunctionTemplate> t = FunctionTemplate::New(New); + constructor_template = Persistent<FunctionTemplate>::New(t); + constructor_template->InstanceTemplate()->SetInternalFieldCount(1); + constructor_template->SetClassName(String::NewSymbol("BSON")); + + // Instance methods + NODE_SET_PROTOTYPE_METHOD(constructor_template, "calculateObjectSize", CalculateObjectSize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "serialize", BSONSerialize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "serializeWithBufferAndIndex", SerializeWithBufferAndIndex); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "deserialize", BSONDeserialize); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "deserializeStream", BSONDeserializeStream); + + // Experimental + // NODE_SET_PROTOTYPE_METHOD(constructor_template, "calculateObjectSize2", CalculateObjectSize2); + // NODE_SET_PROTOTYPE_METHOD(constructor_template, "serialize2", BSONSerialize2); + // NODE_SET_METHOD(constructor_template->GetFunction(), "serialize2", BSONSerialize2); + + target->ForceSet(String::NewSymbol("BSON"), constructor_template->GetFunction()); +} + +// Create a new instance of BSON and assing it the existing context +Handle<Value> BSON::New(const Arguments &args) { + HandleScope scope; + + // Check that we have an array + if(args.Length() == 1 && args[0]->IsArray()) { + // Cast the array to a local reference + Local<Array> array = Local<Array>::Cast(args[0]); + + if(array->Length() > 0) { + // Create a bson object instance and return it + BSON *bson = new BSON(); + + // Setup pre-allocated comparision objects + bson->_bsontypeString = Persistent<String>::New(String::New("_bsontype")); + bson->_longLowString = Persistent<String>::New(String::New("low_")); + bson->_longHighString = Persistent<String>::New(String::New("high_")); + bson->_objectIDidString = Persistent<String>::New(String::New("id")); + bson->_binaryPositionString = Persistent<String>::New(String::New("position")); + bson->_binarySubTypeString = Persistent<String>::New(String::New("sub_type")); + bson->_binaryBufferString = Persistent<String>::New(String::New("buffer")); + bson->_doubleValueString = Persistent<String>::New(String::New("value")); + bson->_symbolValueString = Persistent<String>::New(String::New("value")); + bson->_dbRefRefString = Persistent<String>::New(String::New("$ref")); + bson->_dbRefIdRefString = Persistent<String>::New(String::New("$id")); + bson->_dbRefDbRefString = Persistent<String>::New(String::New("$db")); + bson->_dbRefNamespaceString = Persistent<String>::New(String::New("namespace")); + bson->_dbRefDbString = Persistent<String>::New(String::New("db")); + bson->_dbRefOidString = Persistent<String>::New(String::New("oid")); + + // total number of found classes + uint32_t numberOfClasses = 0; + + // Iterate over all entries to save the instantiate funtions + for(uint32_t i = 0; i < array->Length(); i++) { + // Let's get a reference to the function + Local<Function> func = Local<Function>::Cast(array->Get(i)); + Local<String> functionName = func->GetName()->ToString(); + + // Save the functions making them persistant handles (they don't get collected) + if(functionName->StrictEquals(String::New("Long"))) { + bson->longConstructor = Persistent<Function>::New(func); + bson->longString = Persistent<String>::New(String::New("Long")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("ObjectID"))) { + bson->objectIDConstructor = Persistent<Function>::New(func); + bson->objectIDString = Persistent<String>::New(String::New("ObjectID")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Binary"))) { + bson->binaryConstructor = Persistent<Function>::New(func); + bson->binaryString = Persistent<String>::New(String::New("Binary")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Code"))) { + bson->codeConstructor = Persistent<Function>::New(func); + bson->codeString = Persistent<String>::New(String::New("Code")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("DBRef"))) { + bson->dbrefConstructor = Persistent<Function>::New(func); + bson->dbrefString = Persistent<String>::New(String::New("DBRef")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Symbol"))) { + bson->symbolConstructor = Persistent<Function>::New(func); + bson->symbolString = Persistent<String>::New(String::New("Symbol")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Double"))) { + bson->doubleConstructor = Persistent<Function>::New(func); + bson->doubleString = Persistent<String>::New(String::New("Double")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("Timestamp"))) { + bson->timestampConstructor = Persistent<Function>::New(func); + bson->timestampString = Persistent<String>::New(String::New("Timestamp")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("MinKey"))) { + bson->minKeyConstructor = Persistent<Function>::New(func); + bson->minKeyString = Persistent<String>::New(String::New("MinKey")); + numberOfClasses = numberOfClasses + 1; + } else if(functionName->StrictEquals(String::New("MaxKey"))) { + bson->maxKeyConstructor = Persistent<Function>::New(func); + bson->maxKeyString = Persistent<String>::New(String::New("MaxKey")); + numberOfClasses = numberOfClasses + 1; + } + } + + // Check if we have the right number of constructors otherwise throw an error + if(numberOfClasses != 10) { + // Destroy object + delete(bson); + // Fire exception + return VException("Missing function constructor for either [Long/ObjectID/Binary/Code/DbRef/Symbol/Double/Timestamp/MinKey/MaxKey]"); + } else { + bson->Wrap(args.This()); + return args.This(); + } + } else { + return VException("No types passed in"); + } + } else { + return VException("Argument passed in must be an array of types"); + } +} + +void BSON::write_int32(char *data, uint32_t value) { + // Write the int to the char* + memcpy(data, &value, 4); +} + +void BSON::write_double(char *data, double value) { + // Write the double to the char* + memcpy(data, &value, 8); +} + +void BSON::write_int64(char *data, int64_t value) { + // Write the int to the char* + memcpy(data, &value, 8); +} + +char *BSON::check_key(Local<String> key) { + // Allocate space for they key string + char *key_str = (char *)malloc(key->Utf8Length() * sizeof(char) + 1); + // Error string + char *error_str = (char *)malloc(256 * sizeof(char)); + // Decode the key + ssize_t len = DecodeBytes(key, BINARY); + DecodeWrite(key_str, len, key, BINARY); + *(key_str + key->Utf8Length()) = '\0'; + // Check if we have a valid key + if(key->Utf8Length() > 0 && *(key_str) == '$') { + // Create the string + sprintf(error_str, "key %s must not start with '$'", key_str); + // Free up memory + free(key_str); + // Throw exception with string + throw error_str; + } else if(key->Utf8Length() > 0 && strchr(key_str, '.') != NULL) { + // Create the string + sprintf(error_str, "key %s must not contain '.'", key_str); + // Free up memory + free(key_str); + // Throw exception with string + throw error_str; + } + // Free allocated space + free(key_str); + free(error_str); + // Return No check key error + return NULL; +} + +const char* BSON::ToCString(const v8::String::Utf8Value& value) { + return *value ? *value : "<string conversion failed>"; +} + +Handle<Value> BSON::decodeDBref(BSON *bson, Local<Value> ref, Local<Value> oid, Local<Value> db) { + HandleScope scope; + Local<Value> argv[] = {ref, oid, db}; + Handle<Value> dbrefObj = bson->dbrefConstructor->NewInstance(3, argv); + return scope.Close(dbrefObj); +} + +Handle<Value> BSON::decodeCode(BSON *bson, char *code, Handle<Value> scope_object) { + HandleScope scope; + + Local<Value> argv[] = {String::New(code), scope_object->ToObject()}; + Handle<Value> codeObj = bson->codeConstructor->NewInstance(2, argv); + return scope.Close(codeObj); +} + +Handle<Value> BSON::decodeBinary(BSON *bson, uint32_t sub_type, uint32_t number_of_bytes, char *data) { + HandleScope scope; + + // Create a buffer object that wraps the raw stream + Buffer *bufferObj = Buffer::New(data, number_of_bytes); + // Arguments to be passed to create the binary + Handle<Value> argv[] = {bufferObj->handle_, Uint32::New(sub_type)}; + // Return the buffer handle + Local<Object> bufferObjHandle = bson->binaryConstructor->NewInstance(2, argv); + // Close the scope + return scope.Close(bufferObjHandle); +} + +Handle<Value> BSON::decodeOid(BSON *bson, char *oid) { + HandleScope scope; + + // Encode the string (string - null termiating character) + Local<Value> bin_value = Encode(oid, 12, BINARY)->ToString(); + + // Return the id object + Local<Value> argv[] = {bin_value}; + Local<Object> oidObj = bson->objectIDConstructor->NewInstance(1, argv); + return scope.Close(oidObj); +} + +Handle<Value> BSON::decodeLong(BSON *bson, char *data, uint32_t index) { + HandleScope scope; + + // Decode the integer value + int32_t lowBits = 0; + int32_t highBits = 0; + memcpy(&lowBits, (data + index), 4); + memcpy(&highBits, (data + index + 4), 4); + + // Decode 64bit value + int64_t value = 0; + memcpy(&value, (data + index), 8); + + // If value is < 2^53 and >-2^53 + if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) { + int64_t finalValue = 0; + memcpy(&finalValue, (data + index), 8); + return scope.Close(Number::New(finalValue)); + } + + // Instantiate the js object and pass it back + Local<Value> argv[] = {Int32::New(lowBits), Int32::New(highBits)}; + Local<Object> longObject = bson->longConstructor->NewInstance(2, argv); + return scope.Close(longObject); +} + +Handle<Value> BSON::decodeTimestamp(BSON *bson, char *data, uint32_t index) { + HandleScope scope; + + // Decode the integer value + int32_t lowBits = 0; + int32_t highBits = 0; + memcpy(&lowBits, (data + index), 4); + memcpy(&highBits, (data + index + 4), 4); + + // Build timestamp + Local<Value> argv[] = {Int32::New(lowBits), Int32::New(highBits)}; + Handle<Value> timestamp_obj = bson->timestampConstructor->NewInstance(2, argv); + return scope.Close(timestamp_obj); +} + +// Search for 0 terminated C string and return the string +char* BSON::extract_string(char *data, uint32_t offset) { + char *prt = strchr((data + offset), '\0'); + if(prt == NULL) return NULL; + // Figure out the length of the string + uint32_t length = (prt - data) - offset; + // Allocate memory for the new string + char *string_name = (char *)malloc((length * sizeof(char)) + 1); + // Copy the variable into the string_name + strncpy(string_name, (data + offset), length); + // Ensure the string is null terminated + *(string_name + length) = '\0'; + // Return the unpacked string + return string_name; +} + +// Decode a byte +uint16_t BSON::deserialize_int8(char *data, uint32_t offset) { + uint16_t value = 0; + value |= *(data + offset + 0); + return value; +} + +// Requires a 4 byte char array +uint32_t BSON::deserialize_int32(char* data, uint32_t offset) { + uint32_t value = 0; + memcpy(&value, (data + offset), 4); + return value; +} + +//------------------------------------------------------------------------------------------------ +// +// Experimental +// +//------------------------------------------------------------------------------------------------ +Handle<Value> BSON::CalculateObjectSize2(const Arguments &args) { + HandleScope scope; + // Ensure we have a valid object + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One argument required - [object]"); + if(args.Length() > 1) return VException("One argument required - [object]"); + // Calculate size of the object + uint32_t object_size = BSON::calculate_object_size2(args[0]); + // Return the object size + return scope.Close(Uint32::New(object_size)); +} + +uint32_t BSON::calculate_object_size2(Handle<Value> value) { + // Final object size + uint32_t object_size = (4 + 1); + uint32_t stackIndex = 0; + // Controls the flow + bool done = false; + bool finished = false; + + // Current object we are processing + Local<Object> currentObject = value->ToObject(); + + // Current list of object keys + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> keys = currentObject->GetPropertyNames(); + #else + Local<Array> keys = currentObject->GetOwnPropertyNames(); + #endif + + // Contains pointer to keysIndex + uint32_t keysIndex = 0; + uint32_t keysLength = keys->Length(); + + // printf("=================================================================================\n"); + // printf("Start serializing\n"); + + while(!done) { + // If the index is bigger than the number of keys for the object + // we finished up the previous object and are ready for the next one + if(keysIndex >= keysLength) { + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + keys = currentObject->GetPropertyNames(); + #else + keys = currentObject->GetOwnPropertyNames(); + #endif + keysLength = keys->Length(); + } + + // Iterate over all the keys + while(keysIndex < keysLength) { + // Fetch the key name + Local<String> name = keys->Get(keysIndex++)->ToString(); + // Fetch the object related to the key + Local<Value> value = currentObject->Get(name); + // Add size of the name, plus zero, plus type + object_size += name->Utf8Length() + 1 + 1; + + // If we have a string + if(value->IsString()) { + object_size += value->ToString()->Utf8Length() + 1 + 4; + } else if(value->IsNumber()) { + // Check if we have a float value or a long value + Local<Number> number = value->ToNumber(); + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + object_size = object_size + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + object_size = object_size + 4; + } else { + object_size = object_size + 8; + } + } else if(value->IsBoolean()) { + object_size = object_size + 1; + } else if(value->IsDate()) { + object_size = object_size + 8; + } else if(value->IsRegExp()) { + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + ssize_t len = DecodeBytes(regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + + // global + if((flags & (1 << 0)) != 0) len++; + // ignorecase + if((flags & (1 << 1)) != 0) len++; + //multiline + if((flags & (1 << 2)) != 0) len++; + // if((flags & (1 << 2)) != 0) len++; + // Calculate the space needed for the regexp: size of string - 2 for the /'ses +2 for null termiations + object_size = object_size + len + 2; + } else if(value->IsNull() || value->IsUndefined()) { + } + // } else if(value->IsNumber()) { + // // Check if we have a float value or a long value + // Local<Number> number = value->ToNumber(); + // double d_number = number->NumberValue(); + // int64_t l_number = number->IntegerValue(); + // // Check if we have a double value and not a int64 + // double d_result = d_number - l_number; + // // If we have a value after subtracting the integer value we have a float + // if(d_result > 0 || d_result < 0) { + // object_size = name->Utf8Length() + 1 + object_size + 8 + 1; + // } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + // object_size = name->Utf8Length() + 1 + object_size + 4 + 1; + // } else { + // object_size = name->Utf8Length() + 1 + object_size + 8 + 1; + // } + // } else if(value->IsObject()) { + // printf("------------- hello\n"); + // } + } + + // If we have finished all the keys + if(keysIndex == keysLength) { + finished = false; + } + + // Validate the stack + if(stackIndex == 0) { + // printf("======================================================================== 3\n"); + done = true; + } else if(finished || keysIndex == keysLength) { + // Pop off the stack + stackIndex = stackIndex - 1; + // Fetch the current object stack + // vector<Local<Value> > currentObjectStored = stack.back(); + // stack.pop_back(); + // // Unroll the current object + // currentObject = currentObjectStored.back()->ToObject(); + // currentObjectStored.pop_back(); + // // Unroll the keysIndex + // keys = Local<Array>::Cast(currentObjectStored.back()->ToObject()); + // currentObjectStored.pop_back(); + // // Unroll the keysIndex + // keysIndex = currentObjectStored.back()->ToUint32()->Value(); + // currentObjectStored.pop_back(); + // // Check if we finished up + // if(keysIndex == keys->Length()) { + // finished = true; + // } + } + } + + return object_size; +} + +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------------------ +Handle<Value> BSON::BSONDeserialize(const Arguments &args) { + HandleScope scope; + + // Ensure that we have an parameter + if(Buffer::HasInstance(args[0]) && args.Length() > 1) return VException("One argument required - buffer1."); + if(args[0]->IsString() && args.Length() > 1) return VException("One argument required - string1."); + // Throw an exception if the argument is not of type Buffer + if(!Buffer::HasInstance(args[0]) && !args[0]->IsString()) return VException("Argument must be a Buffer or String."); + + // Define pointer to data + char *data; + Local<Object> obj = args[0]->ToObject(); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // If we passed in a buffer, let's unpack it, otherwise let's unpack the string + if(Buffer::HasInstance(obj)) { + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + uint32_t length = buffer->length(); + #else + data = Buffer::Data(obj); + uint32_t length = Buffer::Length(obj); + #endif + + // Validate that we have at least 5 bytes + if(length < 5) { + return VException("corrupt bson message < 5 bytes long"); + } + + // Deserialize the data + return BSON::deserialize(bson, data, length, 0, NULL); + } else { + // The length of the data for this encoding + ssize_t len = DecodeBytes(args[0], BINARY); + + // Validate that we have at least 5 bytes + if(len < 5) { + return VException("corrupt bson message < 5 bytes long"); + } + + // Let's define the buffer size + data = (char *)malloc(len); + // Write the data to the buffer from the string object + ssize_t written = DecodeWrite(data, len, args[0], BINARY); + // Assert that we wrote the same number of bytes as we have length + assert(written == len); + // Get result + Handle<Value> result = BSON::deserialize(bson, data, len, 0, NULL); + // Free memory + free(data); + // Deserialize the content + return result; + } +} + +// Deserialize the stream +Handle<Value> BSON::deserialize(BSON *bson, char *data, uint32_t inDataLength, uint32_t startIndex, bool is_array_item) { + HandleScope scope; + // Holds references to the objects that are going to be returned + Local<Object> return_data = Object::New(); + Local<Array> return_array = Array::New(); + // The current index in the char data + uint32_t index = startIndex; + // Decode the size of the BSON data structure + uint32_t size = BSON::deserialize_int32(data, index); + + // If we have an illegal message size + if(size > inDataLength) return VException("corrupt bson message"); + + // Data length + uint32_t dataLength = index + size; + + // Adjust the index to point to next piece + index = index + 4; + + // While we have data left let's decode + while(index < dataLength) { + // Read the first to bytes to indicate the type of object we are decoding + uint8_t type = BSON::deserialize_int8(data, index); + // Adjust index to skip type byte + index = index + 1; + + if(type == BSON_DATA_STRING) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the length of the string (next 4 bytes) + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust index to point to start of string + index = index + 4; + // Decode the string and add zero terminating value at the end of the string + char *value = (char *)malloc((string_size * sizeof(char))); + strncpy(value, (data + index), string_size); + // Encode the string (string - null termiating character) + Local<Value> utf8_encoded_str = Encode(value, string_size - 1, UTF8)->ToString(); + // Add the value to the data + if(is_array_item) { + return_array->Set(Number::New(insert_index), utf8_encoded_str); + } else { + return_data->ForceSet(String::New(string_name), utf8_encoded_str); + } + + // Adjust index + index = index + string_size; + // Free up the memory + free(value); + free(string_name); + } else if(type == BSON_DATA_INT) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the integer value + uint32_t value = 0; + memcpy(&value, (data + index), 4); + + // Adjust the index for the size of the value + index = index + 4; + // Add the element to the object + if(is_array_item) { + return_array->Set(Integer::New(insert_index), Integer::New(value)); + } else { + return_data->ForceSet(String::New(string_name), Integer::New(value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_TIMESTAMP) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeTimestamp(bson, data, index)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeTimestamp(bson, data, index)); + } + + // Adjust the index for the size of the value + index = index + 8; + + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_LONG) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeLong(bson, data, index)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeLong(bson, data, index)); + } + + // Adjust the index for the size of the value + index = index + 8; + + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_NUMBER) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the integer value + double value = 0; + memcpy(&value, (data + index), 8); + // Adjust the index for the size of the value + index = index + 8; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Number::New(value)); + } else { + return_data->ForceSet(String::New(string_name), Number::New(value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_MIN_KEY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Create new MinKey + Local<Object> minKey = bson->minKeyConstructor->NewInstance(); + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), minKey); + } else { + return_data->ForceSet(String::New(string_name), minKey); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_MAX_KEY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Create new MinKey + Local<Object> maxKey = bson->maxKeyConstructor->NewInstance(); + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), maxKey); + } else { + return_data->ForceSet(String::New(string_name), maxKey); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_NULL) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Null()); + } else { + return_data->ForceSet(String::New(string_name), Null()); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_BOOLEAN) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the boolean value + char bool_value = *(data + index); + // Adjust the index for the size of the value + index = index + 1; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), bool_value == 1 ? Boolean::New(true) : Boolean::New(false)); + } else { + return_data->ForceSet(String::New(string_name), bool_value == 1 ? Boolean::New(true) : Boolean::New(false)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_DATE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Decode the value 64 bit integer + int64_t value = 0; + memcpy(&value, (data + index), 8); + // Adjust the index for the size of the value + index = index + 8; + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), Date::New((double)value)); + } else { + return_data->ForceSet(String::New(string_name), Date::New((double)value)); + } + // Free up the memory + free(string_name); + } else if(type == BSON_DATA_REGEXP) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Length variable + int32_t length_regexp = 0; + char chr; + + // Locate end of the regexp expression \0 + while((chr = *(data + index + length_regexp)) != '\0') { + length_regexp = length_regexp + 1; + } + + // Contains the reg exp + char *reg_exp = (char *)malloc(length_regexp * sizeof(char) + 2); + // Copy the regexp from the data to the char * + memcpy(reg_exp, (data + index), (length_regexp + 1)); + // Adjust the index to skip the first part of the regular expression + index = index + length_regexp + 1; + + // Reset the length + int32_t options_length = 0; + // Locate the end of the options for the regexp terminated with a '\0' + while((chr = *(data + index + options_length)) != '\0') { + options_length = options_length + 1; + } + + // Contains the reg exp + char *options = (char *)malloc(options_length * sizeof(char) + 1); + // Copy the options from the data to the char * + memcpy(options, (data + index), (options_length + 1)); + // Adjust the index to skip the option part of the regular expression + index = index + options_length + 1; + // ARRRRGH Google does not expose regular expressions through the v8 api + // Have to use Script to instantiate the object (slower) + + // Generate the string for execution in the string context + int flag = 0; + + for(int i = 0; i < options_length; i++) { + // Multiline + if(*(options + i) == 'm') { + flag = flag | 4; + } else if(*(options + i) == 'i') { + flag = flag | 2; + } + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), RegExp::New(String::New(reg_exp), (v8::RegExp::Flags)flag)); + } else { + return_data->ForceSet(String::New(string_name), RegExp::New(String::New(reg_exp), (v8::RegExp::Flags)flag)); + } + + // Free memory + free(reg_exp); + free(options); + free(string_name); + } else if(type == BSON_DATA_OID) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // The id string + char *oid_string = (char *)malloc(12 * sizeof(char)); + // Copy the options from the data to the char * + memcpy(oid_string, (data + index), 12); + + // Adjust the index + index = index + 12; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeOid(bson, oid_string)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeOid(bson, oid_string)); + } + + // Free memory + free(oid_string); + free(string_name); + } else if(type == BSON_DATA_BINARY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the binary data size + uint32_t number_of_bytes = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Decode the subtype, ensure it's positive + uint32_t sub_type = (int)*(data + index) & 0xff; + // Adjust the index + index = index + 1; + // Copy the binary data into a buffer + char *buffer = (char *)malloc(number_of_bytes * sizeof(char) + 1); + memcpy(buffer, (data + index), number_of_bytes); + *(buffer + number_of_bytes) = '\0'; + + // Adjust the index + index = index + number_of_bytes; + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), BSON::decodeBinary(bson, sub_type, number_of_bytes, buffer)); + } else { + return_data->ForceSet(String::New(string_name), BSON::decodeBinary(bson, sub_type, number_of_bytes, buffer)); + } + // Free memory + free(buffer); + free(string_name); + } else if(type == BSON_DATA_SYMBOL) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the length of the string (next 4 bytes) + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust index to point to start of string + index = index + 4; + // Decode the string and add zero terminating value at the end of the string + char *value = (char *)malloc((string_size * sizeof(char))); + strncpy(value, (data + index), string_size); + // Encode the string (string - null termiating character) + Local<Value> utf8_encoded_str = Encode(value, string_size - 1, UTF8)->ToString(); + + // Wrap up the string in a Symbol Object + Local<Value> argv[] = {utf8_encoded_str}; + Handle<Value> symbolObj = bson->symbolConstructor->NewInstance(1, argv); + + // Add the value to the data + if(is_array_item) { + return_array->Set(Number::New(insert_index), symbolObj); + } else { + return_data->ForceSet(String::New(string_name), symbolObj); + } + + // Adjust index + index = index + string_size; + // Free up the memory + free(value); + free(string_name); + } else if(type == BSON_DATA_CODE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Read the string size + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string + char *code = (char *)malloc(string_size * sizeof(char) + 1); + // Copy string + terminating 0 + memcpy(code, (data + index), string_size); + + // Define empty scope object + Handle<Value> scope_object = Object::New(); + + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::decodeCode(bson, code, scope_object); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + free(string_name); + free(code); + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(code); + free(string_name); + } else if(type == BSON_DATA_CODE_W_SCOPE) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Total number of bytes after array index + uint32_t total_code_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string size + uint32_t string_size = BSON::deserialize_int32(data, index); + // Adjust the index + index = index + 4; + // Read the string + char *code = (char *)malloc(string_size * sizeof(char) + 1); + // Copy string + terminating 0 + memcpy(code, (data + index), string_size); + // Adjust the index + index = index + string_size; + // Get the scope object (bson object) + uint32_t bson_object_size = total_code_size - string_size - 8; + // Allocate bson object buffer and copy out the content + char *bson_buffer = (char *)malloc(bson_object_size * sizeof(char)); + memcpy(bson_buffer, (data + index), bson_object_size); + // Adjust the index + index = index + bson_object_size; + // Parse the bson object + Handle<Value> scope_object = BSON::deserialize(bson, bson_buffer, inDataLength, 0, false); + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::decodeCode(bson, code, scope_object); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Clean up memory allocation + free(string_name); + free(bson_buffer); + free(code); + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(code); + free(bson_buffer); + free(string_name); + } else if(type == BSON_DATA_OBJECT) { + // If this is the top level object we need to skip the undecoding + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Get the object size + uint32_t bson_object_size = BSON::deserialize_int32(data, index); + // Define the try catch block + TryCatch try_catch; + // Decode the code object + Handle<Value> obj = BSON::deserialize(bson, data + index, inDataLength, 0, false); + // Adjust the index + index = index + bson_object_size; + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Rethrow exception + return try_catch.ReThrow(); + } + + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + + // Clean up memory allocation + free(string_name); + } else if(type == BSON_DATA_ARRAY) { + // Read the null terminated index String + char *string_name = BSON::extract_string(data, index); + if(string_name == NULL) return VException("Invalid C String found."); + // Let's create a new string + index = index + strlen(string_name) + 1; + // Handle array value if applicable + uint32_t insert_index = 0; + if(is_array_item) { + insert_index = atoi(string_name); + } + + // Get the size + uint32_t array_size = BSON::deserialize_int32(data, index); + // Define the try catch block + TryCatch try_catch; + + // Decode the code object + Handle<Value> obj = BSON::deserialize(bson, data + index, inDataLength, 0, true); + // If an error was thrown push it up the chain + if(try_catch.HasCaught()) { + // Rethrow exception + return try_catch.ReThrow(); + } + // Adjust the index for the next value + index = index + array_size; + // Add the element to the object + if(is_array_item) { + return_array->Set(Number::New(insert_index), obj); + } else { + return_data->ForceSet(String::New(string_name), obj); + } + // Clean up memory allocation + free(string_name); + } + } + + // Check if we have a db reference + if(!is_array_item && return_data->Has(String::New("$ref")) && return_data->Has(String::New("$id"))) { + Handle<Value> dbrefValue = BSON::decodeDBref(bson, return_data->Get(String::New("$ref")), return_data->Get(String::New("$id")), return_data->Get(String::New("$db"))); + return scope.Close(dbrefValue); + } + + // Return the data object to javascript + if(is_array_item) { + return scope.Close(return_array); + } else { + return scope.Close(return_data); + } +} + +Handle<Value> BSON::BSONSerialize(const Arguments &args) { + HandleScope scope; + + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 3 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean]"); + if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !args[2]->IsBoolean() && !args[3]->IsBoolean()) return VException("One, two or tree arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]"); + if(args.Length() > 4) return VException("One, two, tree or four arguments required - [object] or [object, boolean] or [object, boolean, boolean] or [object, boolean, boolean, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + uint32_t object_size = 0; + // Calculate the total size of the document in binary form to ensure we only allocate memory once + // With serialize function + if(args.Length() == 4) { + object_size = BSON::calculate_object_size(bson, args[0], args[3]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Allocate the memory needed for the serializtion + char *serialized_object = (char *)malloc(object_size * sizeof(char)); + // Catch any errors + try { + // Check if we have a boolean value + bool check_key = false; + if(args.Length() >= 3 && args[1]->IsBoolean()) { + check_key = args[1]->BooleanValue(); + } + + // Check if we have a boolean value + bool serializeFunctions = false; + if(args.Length() == 4 && args[1]->IsBoolean()) { + serializeFunctions = args[3]->BooleanValue(); + } + + // Serialize the object + BSON::serialize(bson, serialized_object, 0, Null(), args[0], check_key, serializeFunctions); + } catch(char *err_msg) { + // Free up serialized object space + free(serialized_object); + V8::AdjustAmountOfExternalAllocatedMemory(-object_size); + // Throw exception with the string + Handle<Value> error = VException(err_msg); + // free error message + free(err_msg); + // Return error + return error; + } + + // Write the object size + BSON::write_int32((serialized_object), object_size); + + // If we have 3 arguments + if(args.Length() == 3 || args.Length() == 4) { + // Local<Boolean> asBuffer = args[2]->ToBoolean(); + Buffer *buffer = Buffer::New(serialized_object, object_size); + // Release the serialized string + free(serialized_object); + return scope.Close(buffer->handle_); + } else { + // Encode the string (string - null termiating character) + Local<Value> bin_value = Encode(serialized_object, object_size, BINARY)->ToString(); + // Return the serialized content + return bin_value; + } +} + +Handle<Value> BSON::CalculateObjectSize(const Arguments &args) { + HandleScope scope; + // Ensure we have a valid object + if(args.Length() == 1 && !args[0]->IsObject()) return VException("One argument required - [object]"); + if(args.Length() == 2 && !args[0]->IsObject() && !args[1]->IsBoolean()) return VException("Two arguments required - [object, boolean]"); + if(args.Length() > 3) return VException("One or two arguments required - [object] or [object, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Object size + uint32_t object_size = 0; + // Check if we have our argument, calculate size of the object + if(args.Length() >= 2) { + object_size = BSON::calculate_object_size(bson, args[0], args[1]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Return the object size + return scope.Close(Uint32::New(object_size)); +} + +uint32_t BSON::calculate_object_size(BSON *bson, Handle<Value> value, bool serializeFunctions) { + uint32_t object_size = 0; + + // If we have an object let's unwrap it and calculate the sub sections + if(value->IsString()) { + // Let's calculate the size the string adds, length + type(1 byte) + size(4 bytes) + object_size += value->ToString()->Utf8Length() + 1 + 4; + } else if(value->IsNumber()) { + // Check if we have a float value or a long value + Local<Number> number = value->ToNumber(); + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + object_size = object_size + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + object_size = object_size + 4; + } else { + object_size = object_size + 8; + } + } else if(value->IsBoolean()) { + object_size = object_size + 1; + } else if(value->IsDate()) { + object_size = object_size + 8; + } else if(value->IsRegExp()) { + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + ssize_t len = DecodeBytes(regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + + // global + if((flags & (1 << 0)) != 0) len++; + // ignorecase + if((flags & (1 << 1)) != 0) len++; + //multiline + if((flags & (1 << 2)) != 0) len++; + // if((flags & (1 << 2)) != 0) len++; + // Calculate the space needed for the regexp: size of string - 2 for the /'ses +2 for null termiations + object_size = object_size + len + 2; + } else if(value->IsNull() || value->IsUndefined()) { + } else if(value->IsArray()) { + // Cast to array + Local<Array> array = Local<Array>::Cast(value->ToObject()); + // Turn length into string to calculate the size of all the strings needed + char *length_str = (char *)malloc(256 * sizeof(char)); + // Calculate the size of each element + for(uint32_t i = 0; i < array->Length(); i++) { + // Add "index" string size for each element + sprintf(length_str, "%d", i); + // Add the size of the string length + uint32_t label_length = strlen(length_str) + 1; + // Add the type definition size for each item + object_size = object_size + label_length + 1; + // Add size of the object + uint32_t object_length = BSON::calculate_object_size(bson, array->Get(Integer::New(i)), serializeFunctions); + object_size = object_size + object_length; + } + // Add the object size + object_size = object_size + 4 + 1; + // Free up memory + free(length_str); + } else if(value->IsFunction()) { + if(serializeFunctions) { + object_size += value->ToString()->Utf8Length() + 4 + 1; + } + } else if(value->ToObject()->Has(bson->_bsontypeString)) { + // Handle holder + Local<String> constructorString = value->ToObject()->GetConstructorName(); + + // BSON type object, avoid non-needed checking unless we have a type + if(bson->longString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } else if(bson->timestampString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } else if(bson->objectIDString->StrictEquals(constructorString)) { + object_size = object_size + 12; + } else if(bson->binaryString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Uint32> positionObj = value->ToObject()->Get(String::New("position"))->ToUint32(); + // Adjust the object_size, binary content lengt + total size int32 + binary size int32 + subtype + object_size += positionObj->Value() + 4 + 1; + } else if(bson->codeString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Object> obj = value->ToObject(); + // Get the function + Local<String> function = obj->Get(String::New("code"))->ToString(); + // Get the scope object + Local<Object> scope = obj->Get(String::New("scope"))->ToObject(); + + // For Node < 0.6.X use the GetPropertyNames + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + uint32_t propertyNameLength = scope->GetPropertyNames()->Length(); + #else + uint32_t propertyNameLength = scope->GetOwnPropertyNames()->Length(); + #endif + + // Check if the scope has any parameters + // Let's calculate the size the code object adds adds + if(propertyNameLength > 0) { + object_size += function->Utf8Length() + 4 + BSON::calculate_object_size(bson, scope, serializeFunctions) + 4 + 1; + } else { + object_size += function->Utf8Length() + 4 + 1; + } + } else if(bson->dbrefString->StrictEquals(constructorString)) { + // Unpack the dbref + Local<Object> dbref = value->ToObject(); + // Create an object containing the right namespace variables + Local<Object> obj = Object::New(); + // Build the new object + obj->Set(bson->_dbRefRefString, dbref->Get(bson->_dbRefNamespaceString)); + obj->Set(bson->_dbRefIdRefString, dbref->Get(bson->_dbRefOidString)); + if(!dbref->Get(bson->_dbRefDbString)->IsNull() && !dbref->Get(bson->_dbRefDbString)->IsUndefined()) obj->Set(bson->_dbRefDbRefString, dbref->Get(bson->_dbRefDbString)); + // Calculate size + object_size += BSON::calculate_object_size(bson, obj, serializeFunctions); + } else if(bson->minKeyString->StrictEquals(constructorString) || bson->maxKeyString->Equals(constructorString)) { + } else if(bson->symbolString->StrictEquals(constructorString)) { + // Get string + Local<String> str = value->ToObject()->Get(String::New("value"))->ToString(); + // Get the utf8 length + int utf8_length = str->Utf8Length(); + // Check if we have a utf8 encoded string or not + if(utf8_length != str->Length()) { + // Let's calculate the size the string adds, length + type(1 byte) + size(4 bytes) + object_size += str->Utf8Length() + 1 + 4; + } else { + object_size += str->Length() + 1 + 4; + } + } else if(bson->doubleString->StrictEquals(constructorString)) { + object_size = object_size + 8; + } + } else if(value->IsObject()) { + // Unwrap the object + Local<Object> object = value->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> property_names = object->GetPropertyNames(); + #else + Local<Array> property_names = object->GetOwnPropertyNames(); + #endif + + // Length of the property + uint32_t propertyLength = property_names->Length(); + + // Process all the properties on the object + for(uint32_t index = 0; index < propertyLength; index++) { + // Fetch the property name + Local<String> property_name = property_names->Get(index)->ToString(); + + // Fetch the object for the property + Local<Value> property = object->Get(property_name); + // Get size of property (property + property name length + 1 for terminating 0) + if(!property->IsFunction() || (property->IsFunction() && serializeFunctions)) { + // Convert name to char* + object_size += BSON::calculate_object_size(bson, property, serializeFunctions) + property_name->Utf8Length() + 1 + 1; + } + } + + object_size = object_size + 4 + 1; + } + + return object_size; +} + +uint32_t BSON::serialize(BSON *bson, char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, bool check_key, bool serializeFunctions) { + // Scope for method execution + HandleScope scope; + + // If we have a name check that key is valid + if(!name->IsNull() && check_key) { + if(BSON::check_key(name->ToString()) != NULL) return -1; + } + + // If we have an object let's serialize it + if(value->IsString()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_STRING; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Write the actual string into the char array + Local<String> str = value->ToString(); + // Let's fetch the int value + uint32_t utf8_length = str->Utf8Length(); + + // Write the integer to the char * + BSON::write_int32((serialized_object + index), utf8_length + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + str->WriteUtf8((serialized_object + index), utf8_length); + // Add the null termination + *(serialized_object + index + utf8_length) = '\0'; + // Adjust the index + index = index + utf8_length + 1; + } else if(value->IsNumber()) { + uint32_t first_pointer = index; + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_INT; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + Local<Number> number = value->ToNumber(); + // Get the values + double d_number = number->NumberValue(); + int64_t l_number = number->IntegerValue(); + + // Check if we have a double value and not a int64 + double d_result = d_number - l_number; + // If we have a value after subtracting the integer value we have a float + if(d_result > 0 || d_result < 0) { + // Write the double to the char array + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust index for double + index = index + 8; + } else if(l_number <= BSON_INT32_MAX && l_number >= BSON_INT32_MIN) { + // Smaller than 32 bit, write as 32 bit value + BSON::write_int32(serialized_object + index, value->ToInt32()->Value()); + // Adjust the size of the index + index = index + 4; + } else if(l_number <= JS_INT_MAX && l_number >= JS_INT_MIN) { + // Write the double to the char array + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust index for double + index = index + 8; + } else { + BSON::write_double((serialized_object + index), d_number); + // Adjust type to be double + *(serialized_object + first_pointer) = BSON_DATA_NUMBER; + // Adjust the size of the index + index = index + 8; + } + } else if(value->IsBoolean()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_BOOLEAN; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Save the boolean value + *(serialized_object + index) = value->BooleanValue() ? '\1' : '\0'; + // Adjust the index + index = index + 1; + } else if(value->IsDate()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_DATE; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Fetch the Integer value + int64_t integer_value = value->IntegerValue(); + BSON::write_int64((serialized_object + index), integer_value); + // Adjust the index + index = index + 8; + } else if(value->IsNull() || value->IsUndefined()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_NULL; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + } else if(value->IsArray()) { + // Cast to array + Local<Array> array = Local<Array>::Cast(value->ToObject()); + // Turn length into string to calculate the size of all the strings needed + char *length_str = (char *)malloc(256 * sizeof(char)); + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_ARRAY; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + // Object size + uint32_t object_size = BSON::calculate_object_size(bson, value, serializeFunctions); + // Write the size of the object + BSON::write_int32((serialized_object + index), object_size); + // Adjust the index + index = index + 4; + // Write out all the elements + for(uint32_t i = 0; i < array->Length(); i++) { + // Add "index" string size for each element + sprintf(length_str, "%d", i); + // Encode the values + index = BSON::serialize(bson, serialized_object, index, String::New(length_str), array->Get(Integer::New(i)), check_key, serializeFunctions); + // Write trailing '\0' for object + *(serialized_object + index) = '\0'; + } + + // Pad the last item + *(serialized_object + index) = '\0'; + index = index + 1; + // Free up memory + free(length_str); + } else if(value->IsRegExp()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_REGEXP; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Fetch the string for the regexp + Handle<RegExp> regExp = Handle<RegExp>::Cast(value); + len = DecodeBytes(regExp->GetSource(), UTF8); + written = DecodeWrite((serialized_object + index), len, regExp->GetSource(), UTF8); + int flags = regExp->GetFlags(); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // global + if((flags & (1 << 0)) != 0) { + *(serialized_object + index) = 's'; + index = index + 1; + } + + // ignorecase + if((flags & (1 << 1)) != 0) { + *(serialized_object + index) = 'i'; + index = index + 1; + } + + //multiline + if((flags & (1 << 2)) != 0) { + *(serialized_object + index) = 'm'; + index = index + 1; + } + + // Add null termiation for the string + *(serialized_object + index) = '\0'; + // Adjust the index + index = index + 1; + } else if(value->IsFunction()) { + if(serializeFunctions) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_CODE; + + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + + // Function String + Local<String> function = value->ToString(); + + // Decode the function + len = DecodeBytes(function, BINARY); + // Write the size of the code string + 0 byte end of cString + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + written = DecodeWrite((serialized_object + index), len, function, BINARY); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + } + } else if(value->ToObject()->Has(bson->_bsontypeString)) { + // Handle holder + Local<String> constructorString = value->ToObject()->GetConstructorName(); + uint32_t originalIndex = index; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + // Add null termiation for the string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + + // BSON type object, avoid non-needed checking unless we have a type + if(bson->longString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_LONG; + // Object reference + Local<Object> longObject = value->ToObject(); + + // Fetch the low and high bits + int32_t lowBits = longObject->Get(bson->_longLowString)->ToInt32()->Value(); + int32_t highBits = longObject->Get(bson->_longHighString)->ToInt32()->Value(); + + // Write the content to the char array + BSON::write_int32((serialized_object + index), lowBits); + BSON::write_int32((serialized_object + index + 4), highBits); + // Adjust the index + index = index + 8; + } else if(bson->timestampString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_TIMESTAMP; + // Object reference + Local<Object> timestampObject = value->ToObject(); + + // Fetch the low and high bits + int32_t lowBits = timestampObject->Get(bson->_longLowString)->ToInt32()->Value(); + int32_t highBits = timestampObject->Get(bson->_longHighString)->ToInt32()->Value(); + + // Write the content to the char array + BSON::write_int32((serialized_object + index), lowBits); + BSON::write_int32((serialized_object + index + 4), highBits); + // Adjust the index + index = index + 8; + } else if(bson->objectIDString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_OID; + // Convert to object + Local<Object> objectIDObject = value->ToObject(); + // Let's grab the id + Local<String> idString = objectIDObject->Get(bson->_objectIDidString)->ToString(); + // Let's decode the raw chars from the string + len = DecodeBytes(idString, BINARY); + written = DecodeWrite((serialized_object + index), len, idString, BINARY); + // Adjust the index + index = index + 12; + } else if(bson->binaryString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_BINARY; + + // Let's get the binary object + Local<Object> binaryObject = value->ToObject(); + + // Grab the size(position of the binary) + uint32_t position = value->ToObject()->Get(bson->_binaryPositionString)->ToUint32()->Value(); + // Grab the subtype + uint32_t subType = value->ToObject()->Get(bson->_binarySubTypeString)->ToUint32()->Value(); + // Grab the buffer object + Local<Object> bufferObj = value->ToObject()->Get(bson->_binaryBufferString)->ToObject(); + + // Buffer data pointers + char *data; + uint32_t length; + + // Unpack the buffer variable + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(bufferObj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(bufferObj); + length = Buffer::Length(bufferObj); + #endif + + // Write the size of the buffer out + BSON::write_int32((serialized_object + index), position); + // Adjust index + index = index + 4; + // Write subtype + *(serialized_object + index) = (char)subType; + // Adjust index + index = index + 1; + // Write binary content + memcpy((serialized_object + index), data, position); + // Adjust index.rar">_</a> + index = index + position; + } else if(bson->doubleString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_NUMBER; + + // Unpack the double + Local<Object> doubleObject = value->ToObject(); + + // Fetch the double value + Local<Number> doubleValue = doubleObject->Get(bson->_doubleValueString)->ToNumber(); + // Write the double to the char array + BSON::write_double((serialized_object + index), doubleValue->NumberValue()); + // Adjust index for double + index = index + 8; + } else if(bson->symbolString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_SYMBOL; + // Unpack symbol object + Local<Object> symbolObj = value->ToObject(); + + // Grab the actual string + Local<String> str = symbolObj->Get(bson->_symbolValueString)->ToString(); + // Let's fetch the int value + int utf8_length = str->Utf8Length(); + + // If the Utf8 length is different from the string length then we + // have a UTF8 encoded string, otherwise write it as ascii + if(utf8_length != str->Length()) { + // Write the integer to the char * + BSON::write_int32((serialized_object + index), utf8_length + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + str->WriteUtf8((serialized_object + index), utf8_length); + // Add the null termination + *(serialized_object + index + utf8_length) = '\0'; + // Adjust the index + index = index + utf8_length + 1; + } else { + // Write the integer to the char * + BSON::write_int32((serialized_object + index), str->Length() + 1); + // Adjust the index + index = index + 4; + // Write string to char in utf8 format + written = DecodeWrite((serialized_object + index), str->Length(), str, BINARY); + // Add the null termination + *(serialized_object + index + str->Length()) = '\0'; + // Adjust the index + index = index + str->Length() + 1; + } + } else if(bson->codeString->StrictEquals(constructorString)) { + // Unpack the object and encode + Local<Object> obj = value->ToObject(); + // Get the function + Local<String> function = obj->Get(String::New("code"))->ToString(); + // Get the scope object + Local<Object> scope = obj->Get(String::New("scope"))->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + uint32_t propertyNameLength = scope->GetPropertyNames()->Length(); + #else + uint32_t propertyNameLength = scope->GetOwnPropertyNames()->Length(); + #endif + + // Set the right type if we have a scope or not + if(propertyNameLength > 0) { + // Set basic data code object with scope object + *(serialized_object + originalIndex) = BSON_DATA_CODE_W_SCOPE; + + // Calculate the size of the whole object + uint32_t scopeSize = BSON::calculate_object_size(bson, scope, false); + // Decode the function length + ssize_t len = DecodeBytes(function, UTF8); + // Calculate total size + uint32_t size = 4 + len + 1 + 4 + scopeSize; + + // Write the total size + BSON::write_int32((serialized_object + index), size); + // Adjust the index + index = index + 4; + + // Write the function size + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + ssize_t written = DecodeWrite((serialized_object + index), len, function, UTF8); + assert(written == len); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index with the length of the function + index = index + len + 1; + // Write the scope object + BSON::serialize(bson, (serialized_object + index), 0, Null(), scope, check_key, serializeFunctions); + // Adjust the index + index = index + scopeSize; + } else { + // Set basic data code object + *(serialized_object + originalIndex) = BSON_DATA_CODE; + // Decode the function + ssize_t len = DecodeBytes(function, BINARY); + // Write the size of the code string + 0 byte end of cString + BSON::write_int32((serialized_object + index), len + 1); + // Adjust the index + index = index + 4; + + // Write the data into the serialization stream + ssize_t written = DecodeWrite((serialized_object + index), len, function, BINARY); + assert(written == len); + // Write \0 for string + *(serialized_object + index + len) = 0x00; + // Adjust the index + index = index + len + 1; + } + } else if(bson->dbrefString->StrictEquals(constructorString)) { + // Unpack the dbref + Local<Object> dbref = value->ToObject(); + // Create an object containing the right namespace variables + Local<Object> obj = Object::New(); + + // Build the new object + obj->Set(bson->_dbRefRefString, dbref->Get(bson->_dbRefNamespaceString)); + obj->Set(bson->_dbRefIdRefString, dbref->Get(bson->_dbRefOidString)); + if(!dbref->Get(bson->_dbRefDbString)->IsNull() && !dbref->Get(bson->_dbRefDbString)->IsUndefined()) obj->Set(bson->_dbRefDbRefString, dbref->Get(bson->_dbRefDbString)); + + // Encode the variable + index = BSON::serialize(bson, serialized_object, originalIndex, name, obj, false, serializeFunctions); + } else if(bson->minKeyString->StrictEquals(constructorString)) { + // Save the string at the offset provided + *(serialized_object + originalIndex) = BSON_DATA_MIN_KEY; + } else if(bson->maxKeyString->StrictEquals(constructorString)) { + *(serialized_object + originalIndex) = BSON_DATA_MAX_KEY; + } + } else if(value->IsObject()) { + if(!name->IsNull()) { + // Save the string at the offset provided + *(serialized_object + index) = BSON_DATA_OBJECT; + // Adjust writing position for the first byte + index = index + 1; + // Convert name to char* + ssize_t len = DecodeBytes(name, UTF8); + ssize_t written = DecodeWrite((serialized_object + index), len, name, UTF8); + assert(written == len); + // Add null termiation for the string + *(serialized_object + index + len) = '\0'; + // Adjust the index + index = index + len + 1; + } + + // Unwrap the object + Local<Object> object = value->ToObject(); + + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 6 + Local<Array> property_names = object->GetPropertyNames(); + #else + Local<Array> property_names = object->GetOwnPropertyNames(); + #endif + + // Calculate size of the total object + uint32_t object_size = BSON::calculate_object_size(bson, value, serializeFunctions); + // Write the size + BSON::write_int32((serialized_object + index), object_size); + // Adjust size + index = index + 4; + + // Process all the properties on the object + for(uint32_t i = 0; i < property_names->Length(); i++) { + // Fetch the property name + Local<String> property_name = property_names->Get(i)->ToString(); + // Fetch the object for the property + Local<Value> property = object->Get(property_name); + // Write the next serialized object + // printf("========== !property->IsFunction() || (property->IsFunction() && serializeFunctions) = %d\n", !property->IsFunction() || (property->IsFunction() && serializeFunctions) == true ? 1 : 0); + if(!property->IsFunction() || (property->IsFunction() && serializeFunctions)) { + // Convert name to char* + ssize_t len = DecodeBytes(property_name, UTF8); + // char *data = new char[len]; + char *data = (char *)malloc(len + 1); + *(data + len) = '\0'; + ssize_t written = DecodeWrite(data, len, property_name, UTF8); + assert(written == len); + // Serialize the content + index = BSON::serialize(bson, serialized_object, index, property_name, property, check_key, serializeFunctions); + // Free up memory of data + free(data); + } + } + // Pad the last item + *(serialized_object + index) = '\0'; + index = index + 1; + + // Null out reminding fields if we have a toplevel object and nested levels + if(name->IsNull()) { + for(uint32_t i = 0; i < (object_size - index); i++) { + *(serialized_object + index + i) = '\0'; + } + } + } + + return index; +} + +Handle<Value> BSON::SerializeWithBufferAndIndex(const Arguments &args) { + HandleScope scope; + + //BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index) { + // Ensure we have the correct values + if(args.Length() > 5) return VException("Four or five parameters required [object, boolean, Buffer, int] or [object, boolean, Buffer, int, boolean]"); + if(args.Length() == 4 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32()) return VException("Four parameters required [object, boolean, Buffer, int]"); + if(args.Length() == 5 && !args[0]->IsObject() && !args[1]->IsBoolean() && !Buffer::HasInstance(args[2]) && !args[3]->IsUint32() && !args[4]->IsBoolean()) return VException("Four parameters required [object, boolean, Buffer, int, boolean]"); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Define pointer to data + char *data; + uint32_t length; + // Unpack the object + Local<Object> obj = args[2]->ToObject(); + + // Unpack the buffer object and get pointers to structures + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(obj); + length = Buffer::Length(obj); + #endif + + uint32_t object_size = 0; + // Calculate the total size of the document in binary form to ensure we only allocate memory once + if(args.Length() == 5) { + object_size = BSON::calculate_object_size(bson, args[0], args[4]->BooleanValue()); + } else { + object_size = BSON::calculate_object_size(bson, args[0], false); + } + + // Unpack the index variable + Local<Uint32> indexObject = args[3]->ToUint32(); + uint32_t index = indexObject->Value(); + + // Allocate the memory needed for the serializtion + char *serialized_object = (char *)malloc(object_size * sizeof(char)); + + // Catch any errors + try { + // Check if we have a boolean value + bool check_key = false; + if(args.Length() >= 4 && args[1]->IsBoolean()) { + check_key = args[1]->BooleanValue(); + } + + bool serializeFunctions = false; + if(args.Length() == 5) { + serializeFunctions = args[4]->BooleanValue(); + } + + // Serialize the object + BSON::serialize(bson, serialized_object, 0, Null(), args[0], check_key, serializeFunctions); + } catch(char *err_msg) { + // Free up serialized object space + free(serialized_object); + V8::AdjustAmountOfExternalAllocatedMemory(-object_size); + // Throw exception with the string + Handle<Value> error = VException(err_msg); + // free error message + free(err_msg); + // Return error + return error; + } + + for(uint32_t i = 0; i < object_size; i++) { + *(data + index + i) = *(serialized_object + i); + } + + return scope.Close(Uint32::New(index + object_size - 1)); +} + +Handle<Value> BSON::BSONDeserializeStream(const Arguments &args) { + HandleScope scope; + + // At least 3 arguments required + if(args.Length() < 5) VException("Arguments required (Buffer(data), Number(index in data), Number(number of documents to deserialize), Array(results), Number(index in the array), Object(optional))"); + + // If the number of argumets equals 3 + if(args.Length() >= 5) { + if(!Buffer::HasInstance(args[0])) return VException("First argument must be Buffer instance"); + if(!args[1]->IsUint32()) return VException("Second argument must be a positive index number"); + if(!args[2]->IsUint32()) return VException("Third argument must be a positive number of documents to deserialize"); + if(!args[3]->IsArray()) return VException("Fourth argument must be an array the size of documents to deserialize"); + if(!args[4]->IsUint32()) return VException("Sixth argument must be a positive index number"); + } + + // If we have 4 arguments + if(args.Length() == 6 && !args[5]->IsObject()) return VException("Fifth argument must be an object with options"); + + // Define pointer to data + char *data; + uint32_t length; + Local<Object> obj = args[0]->ToObject(); + uint32_t numberOfDocuments = args[2]->ToUint32()->Value(); + uint32_t index = args[1]->ToUint32()->Value(); + uint32_t resultIndex = args[4]->ToUint32()->Value(); + + // Unpack the BSON parser instance + BSON *bson = ObjectWrap::Unwrap<BSON>(args.This()); + + // Unpack the buffer variable + #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 3 + Buffer *buffer = ObjectWrap::Unwrap<Buffer>(obj); + data = buffer->data(); + length = buffer->length(); + #else + data = Buffer::Data(obj); + length = Buffer::Length(obj); + #endif + + // Fetch the documents + Local<Object> documents = args[3]->ToObject(); + + for(uint32_t i = 0; i < numberOfDocuments; i++) { + // Decode the size of the BSON data structure + uint32_t size = BSON::deserialize_int32(data, index); + + // Get result + Handle<Value> result = BSON::deserialize(bson, data, size, index, NULL); + + // Add result to array + documents->Set(i + resultIndex, result); + + // Adjust the index for next pass + index = index + size; + } + + // Return new index of parsing + return scope.Close(Uint32::New(index)); +} + +// Exporting function +extern "C" void init(Handle<Object> target) { + HandleScope scope; + BSON::Initialize(target); +} + +// NODE_MODULE(bson, BSON::Initialize); +// NODE_MODULE(l, Long::Initialize); diff --git a/node_modules/mongodb/node_modules/bson/ext/bson.h b/node_modules/mongodb/node_modules/bson/ext/bson.h new file mode 100644 index 0000000..dcf21d1 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/ext/bson.h @@ -0,0 +1,105 @@ +#ifndef BSON_H_ +#define BSON_H_ + +#include <node.h> +#include <node_object_wrap.h> +#include <v8.h> + +using namespace v8; +using namespace node; + +class BSON : public ObjectWrap { + public: + BSON() : ObjectWrap() {} + ~BSON() {} + + static void Initialize(Handle<Object> target); + static Handle<Value> BSONDeserializeStream(const Arguments &args); + + // JS based objects + static Handle<Value> BSONSerialize(const Arguments &args); + static Handle<Value> BSONDeserialize(const Arguments &args); + + // Calculate size of function + static Handle<Value> CalculateObjectSize(const Arguments &args); + static Handle<Value> SerializeWithBufferAndIndex(const Arguments &args); + + // Experimental + static Handle<Value> CalculateObjectSize2(const Arguments &args); + static Handle<Value> BSONSerialize2(const Arguments &args); + + // Constructor used for creating new BSON objects from C++ + static Persistent<FunctionTemplate> constructor_template; + + private: + static Handle<Value> New(const Arguments &args); + static Handle<Value> deserialize(BSON *bson, char *data, uint32_t dataLength, uint32_t startIndex, bool is_array_item); + static uint32_t serialize(BSON *bson, char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, bool check_key, bool serializeFunctions); + + static char* extract_string(char *data, uint32_t offset); + static const char* ToCString(const v8::String::Utf8Value& value); + static uint32_t calculate_object_size(BSON *bson, Handle<Value> object, bool serializeFunctions); + + static void write_int32(char *data, uint32_t value); + static void write_int64(char *data, int64_t value); + static void write_double(char *data, double value); + static uint16_t deserialize_int8(char *data, uint32_t offset); + static uint32_t deserialize_int32(char* data, uint32_t offset); + static char *check_key(Local<String> key); + + // BSON type instantiate functions + Persistent<Function> longConstructor; + Persistent<Function> objectIDConstructor; + Persistent<Function> binaryConstructor; + Persistent<Function> codeConstructor; + Persistent<Function> dbrefConstructor; + Persistent<Function> symbolConstructor; + Persistent<Function> doubleConstructor; + Persistent<Function> timestampConstructor; + Persistent<Function> minKeyConstructor; + Persistent<Function> maxKeyConstructor; + + // Equality Objects + Persistent<String> longString; + Persistent<String> objectIDString; + Persistent<String> binaryString; + Persistent<String> codeString; + Persistent<String> dbrefString; + Persistent<String> symbolString; + Persistent<String> doubleString; + Persistent<String> timestampString; + Persistent<String> minKeyString; + Persistent<String> maxKeyString; + + // Equality speed up comparision objects + Persistent<String> _bsontypeString; + Persistent<String> _longLowString; + Persistent<String> _longHighString; + Persistent<String> _objectIDidString; + Persistent<String> _binaryPositionString; + Persistent<String> _binarySubTypeString; + Persistent<String> _binaryBufferString; + Persistent<String> _doubleValueString; + Persistent<String> _symbolValueString; + + Persistent<String> _dbRefRefString; + Persistent<String> _dbRefIdRefString; + Persistent<String> _dbRefDbRefString; + Persistent<String> _dbRefNamespaceString; + Persistent<String> _dbRefDbString; + Persistent<String> _dbRefOidString; + + // Decode JS function + static Handle<Value> decodeLong(BSON *bson, char *data, uint32_t index); + static Handle<Value> decodeTimestamp(BSON *bson, char *data, uint32_t index); + static Handle<Value> decodeOid(BSON *bson, char *oid); + static Handle<Value> decodeBinary(BSON *bson, uint32_t sub_type, uint32_t number_of_bytes, char *data); + static Handle<Value> decodeCode(BSON *bson, char *code, Handle<Value> scope); + static Handle<Value> decodeDBref(BSON *bson, Local<Value> ref, Local<Value> oid, Local<Value> db); + + // Experimental + static uint32_t calculate_object_size2(Handle<Value> object); + static uint32_t serialize2(char *serialized_object, uint32_t index, Handle<Value> name, Handle<Value> value, uint32_t object_size, bool check_key); +}; + +#endif // BSON_H_ diff --git a/node_modules/mongodb/node_modules/bson/ext/index.js b/node_modules/mongodb/node_modules/bson/ext/index.js new file mode 100644 index 0000000..9c45d53 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/ext/index.js @@ -0,0 +1,20 @@ +var bson = require('./bson'); +exports.BSON = bson.BSON; +exports.Long = require('../lib/bson/long').Long; +exports.ObjectID = require('../lib/bson/objectid').ObjectID; +exports.DBRef = require('../lib/bson/db_ref').DBRef; +exports.Code = require('../lib/bson/code').Code; +exports.Timestamp = require('../lib/bson/timestamp').Timestamp; +exports.Binary = require('../lib/bson/binary').Binary; +exports.Double = require('../lib/bson/double').Double; +exports.MaxKey = require('../lib/bson/max_key').MaxKey; +exports.MinKey = require('../lib/bson/min_key').MinKey; +exports.Symbol = require('../lib/bson/symbol').Symbol; + +// Just add constants tot he Native BSON parser +exports.BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +exports.BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +exports.BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +exports.BSON.BSON_BINARY_SUBTYPE_UUID = 3; +exports.BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +exports.BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; diff --git a/node_modules/mongodb/node_modules/bson/ext/wscript b/node_modules/mongodb/node_modules/bson/ext/wscript new file mode 100644 index 0000000..40f5317 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/ext/wscript @@ -0,0 +1,39 @@ +import Options +from os import unlink, symlink, popen +from os.path import exists + +srcdir = "." +blddir = "build" +VERSION = "0.1.0" + +def set_options(opt): + opt.tool_options("compiler_cxx") + opt.add_option( '--debug' + , action='store_true' + , default=False + , help='Build debug variant [Default: False]' + , dest='debug' + ) + +def configure(conf): + conf.check_tool("compiler_cxx") + conf.check_tool("node_addon") + conf.env.append_value('CXXFLAGS', ['-O3', '-funroll-loops']) + + # conf.env.append_value('CXXFLAGS', ['-DDEBUG', '-g', '-O0', '-Wall', '-Wextra']) + # conf.check(lib='node', libpath=['/usr/lib', '/usr/local/lib'], uselib_store='NODE') + +def build(bld): + obj = bld.new_task_gen("cxx", "shlib", "node_addon") + obj.target = "bson" + obj.source = ["bson.cc"] + # obj.uselib = "NODE" + +def shutdown(): + # HACK to get compress.node out of build directory. + # better way to do this? + if Options.commands['clean']: + if exists('bson.node'): unlink('bson.node') + else: + if exists('build/default/bson.node') and not exists('bson.node'): + symlink('build/default/bson.node', 'bson.node') diff --git a/node_modules/mongodb/node_modules/bson/install.js b/node_modules/mongodb/node_modules/bson/install.js new file mode 100644 index 0000000..c9cc91d --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/install.js @@ -0,0 +1,41 @@ +var spawn = require('child_process').spawn, + exec = require('child_process').exec; + +process.stdout.write("================================================================================\n"); +process.stdout.write("= =\n"); +process.stdout.write("= To install with C++ bson parser do <npm install mongodb --mongodb:native> =\n"); +process.stdout.write("= =\n"); +process.stdout.write("================================================================================\n"); + +// Check if we want to build the native code +var build_native = process.env['npm_config_mongodb_native'] != null ? process.env['npm_config_mongodb_native'] : 'false'; +build_native = build_native == 'true' ? true : false; + +// If we are building the native bson extension ensure we use gmake if available +if(build_native) { + // Check if we need to use gmake + exec('which gmake', function(err, stdout, stderr) { + // Set up spawn command + var make = null; + // No gmake build using make + if(err != null) { + make = spawn('make', ['total'], {cwd:process.env['PWD']}); + } else { + make = spawn('gmake', ['total'], {cwd:process.env['PWD']}); + } + + // Execute spawn + make.stdout.on('data', function(data) { + process.stdout.write(data); + }) + + make.stderr.on('data', function(data) { + process.stdout.write(data); + }) + + make.on('exit', function(code) { + process.stdout.write('child process exited with code ' + code + "\n"); + }) + }); +} + diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/binary.js b/node_modules/mongodb/node_modules/bson/lib/bson/binary.js new file mode 100644 index 0000000..eaa0dad --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/binary.js @@ -0,0 +1,336 @@ +/** + * Module dependencies. + */ +if(typeof window === 'undefined') { + var Buffer = require('buffer').Buffer; // TODO just use global Buffer + var bson = require('./bson'); +} + +// Binary default subtype +var BSON_BINARY_SUBTYPE_DEFAULT = 0; + +/** + * @ignore + * @api private + */ +var writeStringToArray = function(data) { + // Create a buffer + var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(data.length)) : new Array(data.length); + // Write the content to the buffer + for(var i = 0; i < data.length; i++) { + buffer[i] = data.charCodeAt(i); + } + // Write the string to the buffer + return buffer; +} + +/** + * Convert Array ot Uint8Array to Binary String + * + * @ignore + * @api private + */ +var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) { + var result = ""; + for(var i = startIndex; i < endIndex; i++) { + result = result + String.fromCharCode(byteArray[i]); + } + return result; +}; + +/** + * A class representation of the BSON Binary type. + * + * Sub types + * - **BSON.BSON_BINARY_SUBTYPE_DEFAULT**, default BSON type. + * - **BSON.BSON_BINARY_SUBTYPE_FUNCTION**, BSON function type. + * - **BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY**, BSON byte array type. + * - **BSON.BSON_BINARY_SUBTYPE_UUID**, BSON uuid type. + * - **BSON.BSON_BINARY_SUBTYPE_MD5**, BSON md5 type. + * - **BSON.BSON_BINARY_SUBTYPE_USER_DEFINED**, BSON user defined type. + * + * @class Represents the Binary BSON type. + * @param {Buffer} buffer a buffer object containing the binary data. + * @param {Number} [subType] the option binary type. + * @return {Grid} + */ +function Binary(buffer, subType) { + if(!(this instanceof Binary)) return new Binary(buffer, subType); + + this._bsontype = 'Binary'; + + if(buffer instanceof Number) { + this.sub_type = buffer; + this.position = 0; + } else { + this.sub_type = subType == null ? BSON_BINARY_SUBTYPE_DEFAULT : subType; + this.position = 0; + } + + if(buffer != null && !(buffer instanceof Number)) { + // Only accept Buffer, Uint8Array or Arrays + if(typeof buffer == 'string') { + // Different ways of writing the length of the string for the different types + if(typeof Buffer != 'undefined') { + this.buffer = new Buffer(buffer); + } else if(typeof Uint8Array != 'undefined' || (Object.prototype.toString.call(buffer) == '[object Array]')) { + this.buffer = writeStringToArray(buffer); + } else { + throw new Error("only String, Buffer, Uint8Array or Array accepted"); + } + } else { + this.buffer = buffer; + } + this.position = buffer.length; + } else { + if(typeof Buffer != 'undefined') { + this.buffer = new Buffer(Binary.BUFFER_SIZE); + } else if(typeof Uint8Array != 'undefined'){ + this.buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE)); + } else { + this.buffer = new Array(Binary.BUFFER_SIZE); + } + // Set position to start of buffer + this.position = 0; + } +}; + +/** + * Updates this binary with byte_value. + * + * @param {Character} byte_value a single byte we wish to write. + * @api public + */ +Binary.prototype.put = function put(byte_value) { + // If it's a string and a has more than one character throw an error + if(byte_value['length'] != null && typeof byte_value != 'number' && byte_value.length != 1) throw new Error("only accepts single character String, Uint8Array or Array"); + if(typeof byte_value != 'number' && byte_value < 0 || byte_value > 255) throw new Error("only accepts number in a valid unsigned byte range 0-255"); + + // Decode the byte value once + var decoded_byte = null; + if(typeof byte_value == 'string') { + decoded_byte = byte_value.charCodeAt(0); + } else if(byte_value['length'] != null) { + decoded_byte = byte_value[0]; + } else { + decoded_byte = byte_value; + } + + if(this.buffer.length > this.position) { + this.buffer[this.position++] = decoded_byte; + } else { + if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) { + // Create additional overflow buffer + var buffer = new Buffer(Binary.BUFFER_SIZE + this.buffer.length); + // Combine the two buffers together + this.buffer.copy(buffer, 0, 0, this.buffer.length); + this.buffer = buffer; + this.buffer[this.position++] = decoded_byte; + } else { + var buffer = null; + // Create a new buffer (typed or normal array) + if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') { + buffer = new Uint8Array(new ArrayBuffer(Binary.BUFFER_SIZE + this.buffer.length)); + } else { + buffer = new Array(Binary.BUFFER_SIZE + this.buffer.length); + } + + // We need to copy all the content to the new array + for(var i = 0; i < this.buffer.length; i++) { + buffer[i] = this.buffer[i]; + } + + // Reassign the buffer + this.buffer = buffer; + // Write the byte + this.buffer[this.position++] = decoded_byte; + } + } +}; + +/** + * Writes a buffer or string to the binary. + * + * @param {Buffer|String} string a string or buffer to be written to the Binary BSON object. + * @param {Number} offset specify the binary of where to write the content. + * @api public + */ +Binary.prototype.write = function write(string, offset) { + offset = typeof offset == 'number' ? offset : this.position; + + // If the buffer is to small let's extend the buffer + if(this.buffer.length < offset + string.length) { + var buffer = null; + // If we are in node.js + if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) { + buffer = new Buffer(this.buffer.length + string.length); + this.buffer.copy(buffer, 0, 0, this.buffer.length); + } else if(Object.prototype.toString.call(this.buffer) == '[object Uint8Array]') { + // Create a new buffer + buffer = new Uint8Array(new ArrayBuffer(this.buffer.length + string.length)) + // Copy the content + for(var i = 0; i < this.position; i++) { + buffer[i] = this.buffer[i]; + } + } + + // Assign the new buffer + this.buffer = buffer; + } + + if(typeof Buffer != 'undefined' && Buffer.isBuffer(string) && Buffer.isBuffer(this.buffer)) { + string.copy(this.buffer, offset, 0, string.length); + this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position; + // offset = string.length + } else if(typeof Buffer != 'undefined' && typeof string == 'string' && Buffer.isBuffer(this.buffer)) { + this.buffer.write(string, 'binary', offset); + this.position = (offset + string.length) > this.position ? (offset + string.length) : this.position; + // offset = string.length; + } else if(Object.prototype.toString.call(string) == '[object Uint8Array]' + || Object.prototype.toString.call(string) == '[object Array]' && typeof string != 'string') { + for(var i = 0; i < string.length; i++) { + this.buffer[offset++] = string[i]; + } + + this.position = offset > this.position ? offset : this.position; + } else if(typeof string == 'string') { + for(var i = 0; i < string.length; i++) { + this.buffer[offset++] = string.charCodeAt(i); + } + + this.position = offset > this.position ? offset : this.position; + } +}; + +/** + * Reads **length** bytes starting at **position**. + * + * @param {Number} position read from the given position in the Binary. + * @param {Number} length the number of bytes to read. + * @return {Buffer} + * @api public + */ +Binary.prototype.read = function read(position, length) { + length = length && length > 0 + ? length + : this.position; + + // Let's return the data based on the type we have + if(this.buffer['slice']) { + return this.buffer.slice(position, position + length); + } else { + // Create a buffer to keep the result + var buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(length)) : new Array(length); + for(var i = 0; i < length; i++) { + buffer[i] = this.buffer[position++]; + } + } + // Return the buffer + return buffer; +}; + +/** + * Returns the value of this binary as a string. + * + * @return {String} + * @api public + */ +Binary.prototype.value = function value(asRaw) { + asRaw = asRaw == null ? false : asRaw; + + // If it's a node.js buffer object + if(typeof Buffer != 'undefined' && Buffer.isBuffer(this.buffer)) { + return asRaw ? this.buffer.slice(0, this.position) : this.buffer.toString('binary', 0, this.position); + } else { + if(asRaw) { + // we support the slice command use it + if(this.buffer['slice'] != null) { + return this.buffer.slice(0, this.position); + } else { + // Create a new buffer to copy content to + var newBuffer = Object.prototype.toString.call(this.buffer) == '[object Uint8Array]' ? new Uint8Array(new ArrayBuffer(this.position)) : new Array(this.position); + // Copy content + for(var i = 0; i < this.position; i++) { + newBuffer[i] = this.buffer[i]; + } + // Return the buffer + return newBuffer; + } + } else { + return convertArraytoUtf8BinaryString(this.buffer, 0, this.position); + } + } +}; + +/** + * Length. + * + * @return {Number} the length of the binary. + * @api public + */ +Binary.prototype.length = function length() { + return this.position; +}; + +/** + * @ignore + * @api private + */ +Binary.prototype.toJSON = function() { + return this.buffer != null ? this.buffer.toString('base64') : ''; +} + +/** + * @ignore + * @api private + */ +Binary.prototype.toString = function(format) { + return this.buffer != null ? this.buffer.slice(0, this.position).toString(format) : ''; +} + +Binary.BUFFER_SIZE = 256; + +/** + * Default BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_DEFAULT = 0; +/** + * Function BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_FUNCTION = 1; +/** + * Byte Array BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_BYTE_ARRAY = 2; +/** + * UUID BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_UUID = 3; +/** + * MD5 BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_MD5 = 4; +/** + * User BSON type + * + * @classconstant SUBTYPE_DEFAULT + **/ +Binary.SUBTYPE_USER_DEFINED = 128; + +/** + * Expose. + */ +if(typeof window === 'undefined') { + exports.Binary = Binary; +} + diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/binary_parser.js b/node_modules/mongodb/node_modules/bson/lib/bson/binary_parser.js new file mode 100644 index 0000000..29951e9 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/binary_parser.js @@ -0,0 +1,387 @@ +/** + * Binary Parser. + * Jonas Raoni Soares Silva + * http://jsfromhell.com/classes/binary-parser [v1.0] + */ +var chr = String.fromCharCode; + +var maxBits = []; +for (var i = 0; i < 64; i++) { + maxBits[i] = Math.pow(2, i); +} + +function BinaryParser (bigEndian, allowExceptions) { + if(!(this instanceof BinaryParser)) return new BinaryParser(bigEndian, allowExceptions); + + this.bigEndian = bigEndian; + this.allowExceptions = allowExceptions; +}; + +BinaryParser.warn = function warn (msg) { + if (this.allowExceptions) { + throw new Error(msg); + } + + return 1; +}; + +BinaryParser.decodeFloat = function decodeFloat (data, precisionBits, exponentBits) { + var b = new this.Buffer(this.bigEndian, data); + + b.checkBuffer(precisionBits + exponentBits + 1); + + var bias = maxBits[exponentBits - 1] - 1 + , signal = b.readBits(precisionBits + exponentBits, 1) + , exponent = b.readBits(precisionBits, exponentBits) + , significand = 0 + , divisor = 2 + , curByte = b.buffer.length + (-precisionBits >> 3) - 1; + + do { + for (var byteValue = b.buffer[ ++curByte ], startBit = precisionBits % 8 || 8, mask = 1 << startBit; mask >>= 1; ( byteValue & mask ) && ( significand += 1 / divisor ), divisor *= 2 ); + } while (precisionBits -= startBit); + + return exponent == ( bias << 1 ) + 1 ? significand ? NaN : signal ? -Infinity : +Infinity : ( 1 + signal * -2 ) * ( exponent || significand ? !exponent ? Math.pow( 2, -bias + 1 ) * significand : Math.pow( 2, exponent - bias ) * ( 1 + significand ) : 0 ); +}; + +BinaryParser.decodeInt = function decodeInt (data, bits, signed, forceBigEndian) { + var b = new this.Buffer(this.bigEndian || forceBigEndian, data) + , x = b.readBits(0, bits) + , max = maxBits[bits]; //max = Math.pow( 2, bits ); + + return signed && x >= max / 2 + ? x - max + : x; +}; + +BinaryParser.encodeFloat = function encodeFloat (data, precisionBits, exponentBits) { + var bias = maxBits[exponentBits - 1] - 1 + , minExp = -bias + 1 + , maxExp = bias + , minUnnormExp = minExp - precisionBits + , n = parseFloat(data) + , status = isNaN(n) || n == -Infinity || n == +Infinity ? n : 0 + , exp = 0 + , len = 2 * bias + 1 + precisionBits + 3 + , bin = new Array(len) + , signal = (n = status !== 0 ? 0 : n) < 0 + , intPart = Math.floor(n = Math.abs(n)) + , floatPart = n - intPart + , lastBit + , rounded + , result + , i + , j; + + for (i = len; i; bin[--i] = 0); + + for (i = bias + 2; intPart && i; bin[--i] = intPart % 2, intPart = Math.floor(intPart / 2)); + + for (i = bias + 1; floatPart > 0 && i; (bin[++i] = ((floatPart *= 2) >= 1) - 0 ) && --floatPart); + + for (i = -1; ++i < len && !bin[i];); + + if (bin[(lastBit = precisionBits - 1 + (i = (exp = bias + 1 - i) >= minExp && exp <= maxExp ? i + 1 : bias + 1 - (exp = minExp - 1))) + 1]) { + if (!(rounded = bin[lastBit])) { + for (j = lastBit + 2; !rounded && j < len; rounded = bin[j++]); + } + + for (j = lastBit + 1; rounded && --j >= 0; (bin[j] = !bin[j] - 0) && (rounded = 0)); + } + + for (i = i - 2 < 0 ? -1 : i - 3; ++i < len && !bin[i];); + + if ((exp = bias + 1 - i) >= minExp && exp <= maxExp) { + ++i; + } else if (exp < minExp) { + exp != bias + 1 - len && exp < minUnnormExp && this.warn("encodeFloat::float underflow"); + i = bias + 1 - (exp = minExp - 1); + } + + if (intPart || status !== 0) { + this.warn(intPart ? "encodeFloat::float overflow" : "encodeFloat::" + status); + exp = maxExp + 1; + i = bias + 2; + + if (status == -Infinity) { + signal = 1; + } else if (isNaN(status)) { + bin[i] = 1; + } + } + + for (n = Math.abs(exp + bias), j = exponentBits + 1, result = ""; --j; result = (n % 2) + result, n = n >>= 1); + + for (n = 0, j = 0, i = (result = (signal ? "1" : "0") + result + bin.slice(i, i + precisionBits).join("")).length, r = []; i; j = (j + 1) % 8) { + n += (1 << j) * result.charAt(--i); + if (j == 7) { + r[r.length] = String.fromCharCode(n); + n = 0; + } + } + + r[r.length] = n + ? String.fromCharCode(n) + : ""; + + return (this.bigEndian ? r.reverse() : r).join(""); +}; + +BinaryParser.encodeInt = function encodeInt (data, bits, signed, forceBigEndian) { + var max = maxBits[bits]; + + if (data >= max || data < -(max / 2)) { + this.warn("encodeInt::overflow"); + data = 0; + } + + if (data < 0) { + data += max; + } + + for (var r = []; data; r[r.length] = String.fromCharCode(data % 256), data = Math.floor(data / 256)); + + for (bits = -(-bits >> 3) - r.length; bits--; r[r.length] = "\0"); + + return ((this.bigEndian || forceBigEndian) ? r.reverse() : r).join(""); +}; + +BinaryParser.toSmall = function( data ){ return this.decodeInt( data, 8, true ); }; +BinaryParser.fromSmall = function( data ){ return this.encodeInt( data, 8, true ); }; +BinaryParser.toByte = function( data ){ return this.decodeInt( data, 8, false ); }; +BinaryParser.fromByte = function( data ){ return this.encodeInt( data, 8, false ); }; +BinaryParser.toShort = function( data ){ return this.decodeInt( data, 16, true ); }; +BinaryParser.fromShort = function( data ){ return this.encodeInt( data, 16, true ); }; +BinaryParser.toWord = function( data ){ return this.decodeInt( data, 16, false ); }; +BinaryParser.fromWord = function( data ){ return this.encodeInt( data, 16, false ); }; +BinaryParser.toInt = function( data ){ return this.decodeInt( data, 32, true ); }; +BinaryParser.fromInt = function( data ){ return this.encodeInt( data, 32, true ); }; +BinaryParser.toLong = function( data ){ return this.decodeInt( data, 64, true ); }; +BinaryParser.fromLong = function( data ){ return this.encodeInt( data, 64, true ); }; +BinaryParser.toDWord = function( data ){ return this.decodeInt( data, 32, false ); }; +BinaryParser.fromDWord = function( data ){ return this.encodeInt( data, 32, false ); }; +BinaryParser.toQWord = function( data ){ return this.decodeInt( data, 64, true ); }; +BinaryParser.fromQWord = function( data ){ return this.encodeInt( data, 64, true ); }; +BinaryParser.toFloat = function( data ){ return this.decodeFloat( data, 23, 8 ); }; +BinaryParser.fromFloat = function( data ){ return this.encodeFloat( data, 23, 8 ); }; +BinaryParser.toDouble = function( data ){ return this.decodeFloat( data, 52, 11 ); }; +BinaryParser.fromDouble = function( data ){ return this.encodeFloat( data, 52, 11 ); }; + +// Factor out the encode so it can be shared by add_header and push_int32 +BinaryParser.encode_int32 = function encode_int32 (number, asArray) { + var a, b, c, d, unsigned; + unsigned = (number < 0) ? (number + 0x100000000) : number; + a = Math.floor(unsigned / 0xffffff); + unsigned &= 0xffffff; + b = Math.floor(unsigned / 0xffff); + unsigned &= 0xffff; + c = Math.floor(unsigned / 0xff); + unsigned &= 0xff; + d = Math.floor(unsigned); + return asArray ? [chr(a), chr(b), chr(c), chr(d)] : chr(a) + chr(b) + chr(c) + chr(d); +}; + +BinaryParser.encode_int64 = function encode_int64 (number) { + var a, b, c, d, e, f, g, h, unsigned; + unsigned = (number < 0) ? (number + 0x10000000000000000) : number; + a = Math.floor(unsigned / 0xffffffffffffff); + unsigned &= 0xffffffffffffff; + b = Math.floor(unsigned / 0xffffffffffff); + unsigned &= 0xffffffffffff; + c = Math.floor(unsigned / 0xffffffffff); + unsigned &= 0xffffffffff; + d = Math.floor(unsigned / 0xffffffff); + unsigned &= 0xffffffff; + e = Math.floor(unsigned / 0xffffff); + unsigned &= 0xffffff; + f = Math.floor(unsigned / 0xffff); + unsigned &= 0xffff; + g = Math.floor(unsigned / 0xff); + unsigned &= 0xff; + h = Math.floor(unsigned); + return chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f) + chr(g) + chr(h); +}; + +/** + * UTF8 methods + */ + +// Take a raw binary string and return a utf8 string +BinaryParser.decode_utf8 = function decode_utf8 (binaryStr) { + var len = binaryStr.length + , decoded = '' + , i = 0 + , c = 0 + , c1 = 0 + , c2 = 0 + , c3; + + while (i < len) { + c = binaryStr.charCodeAt(i); + if (c < 128) { + decoded += String.fromCharCode(c); + i++; + } else if ((c > 191) && (c < 224)) { + c2 = binaryStr.charCodeAt(i+1); + decoded += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } else { + c2 = binaryStr.charCodeAt(i+1); + c3 = binaryStr.charCodeAt(i+2); + decoded += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + + return decoded; +}; + +// Encode a cstring +BinaryParser.encode_cstring = function encode_cstring (s) { + return unescape(encodeURIComponent(s)) + BinaryParser.fromByte(0); +}; + +// Take a utf8 string and return a binary string +BinaryParser.encode_utf8 = function encode_utf8 (s) { + var a = "" + , c; + + for (var n = 0, len = s.length; n < len; n++) { + c = s.charCodeAt(n); + + if (c < 128) { + a += String.fromCharCode(c); + } else if ((c > 127) && (c < 2048)) { + a += String.fromCharCode((c>>6) | 192) ; + a += String.fromCharCode((c&63) | 128); + } else { + a += String.fromCharCode((c>>12) | 224); + a += String.fromCharCode(((c>>6) & 63) | 128); + a += String.fromCharCode((c&63) | 128); + } + } + + return a; +}; + +BinaryParser.hprint = function hprint (s) { + var number; + + for (var i = 0, len = s.length; i < len; i++) { + if (s.charCodeAt(i) < 32) { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(16) + : s.charCodeAt(i).toString(16); + process.stdout.write(number + " ") + } else { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(16) + : s.charCodeAt(i).toString(16); + process.stdout.write(number + " ") + } + } + + process.stdout.write("\n\n"); +}; + +BinaryParser.ilprint = function hprint (s) { + var number; + + for (var i = 0, len = s.length; i < len; i++) { + if (s.charCodeAt(i) < 32) { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(10) + : s.charCodeAt(i).toString(10); + + require('util').debug(number+' : '); + } else { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(10) + : s.charCodeAt(i).toString(10); + require('util').debug(number+' : '+ s.charAt(i)); + } + } +}; + +BinaryParser.hlprint = function hprint (s) { + var number; + + for (var i = 0, len = s.length; i < len; i++) { + if (s.charCodeAt(i) < 32) { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(16) + : s.charCodeAt(i).toString(16); + require('util').debug(number+' : '); + } else { + number = s.charCodeAt(i) <= 15 + ? "0" + s.charCodeAt(i).toString(16) + : s.charCodeAt(i).toString(16); + require('util').debug(number+' : '+ s.charAt(i)); + } + } +}; + +/** + * BinaryParser buffer constructor. + */ +function BinaryParserBuffer (bigEndian, buffer) { + this.bigEndian = bigEndian || 0; + this.buffer = []; + this.setBuffer(buffer); +}; + +BinaryParserBuffer.prototype.setBuffer = function setBuffer (data) { + var l, i, b; + + if (data) { + i = l = data.length; + b = this.buffer = new Array(l); + for (; i; b[l - i] = data.charCodeAt(--i)); + this.bigEndian && b.reverse(); + } +}; + +BinaryParserBuffer.prototype.hasNeededBits = function hasNeededBits (neededBits) { + return this.buffer.length >= -(-neededBits >> 3); +}; + +BinaryParserBuffer.prototype.checkBuffer = function checkBuffer (neededBits) { + if (!this.hasNeededBits(neededBits)) { + throw new Error("checkBuffer::missing bytes"); + } +}; + +BinaryParserBuffer.prototype.readBits = function readBits (start, length) { + //shl fix: Henri Torgemane ~1996 (compressed by Jonas Raoni) + + function shl (a, b) { + for (; b--; a = ((a %= 0x7fffffff + 1) & 0x40000000) == 0x40000000 ? a * 2 : (a - 0x40000000) * 2 + 0x7fffffff + 1); + return a; + } + + if (start < 0 || length <= 0) { + return 0; + } + + this.checkBuffer(start + length); + + var offsetLeft + , offsetRight = start % 8 + , curByte = this.buffer.length - ( start >> 3 ) - 1 + , lastByte = this.buffer.length + ( -( start + length ) >> 3 ) + , diff = curByte - lastByte + , sum = ((this.buffer[ curByte ] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1)) + (diff && (offsetLeft = (start + length) % 8) ? (this.buffer[lastByte++] & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight : 0); + + for(; diff; sum += shl(this.buffer[lastByte++], (diff-- << 3) - offsetRight)); + + return sum; +}; + +/** + * Expose. + */ +BinaryParser.Buffer = BinaryParserBuffer; + +if(typeof window === 'undefined') { + exports.BinaryParser = BinaryParser; +} diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/bson.js b/node_modules/mongodb/node_modules/bson/lib/bson/bson.js new file mode 100644 index 0000000..d24de4b --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/bson.js @@ -0,0 +1,1483 @@ +if(typeof window === 'undefined') { + var Long = require('./long').Long + , Double = require('./double').Double + , Timestamp = require('./timestamp').Timestamp + , ObjectID = require('./objectid').ObjectID + , Symbol = require('./symbol').Symbol + , Code = require('./code').Code + , MinKey = require('./min_key').MinKey + , MaxKey = require('./max_key').MaxKey + , DBRef = require('./db_ref').DBRef + , Binary = require('./binary').Binary + , BinaryParser = require('./binary_parser').BinaryParser + , writeIEEE754 = require('./float_parser').writeIEEE754 + , readIEEE754 = require('./float_parser').readIEEE754; +} + +/** + * Create a new BSON instance + * + * @class Represents the BSON Parser + * @return {BSON} instance of BSON Parser. + */ +function BSON () {}; + +/** + * @ignore + * @api private + */ +// BSON MAX VALUES +BSON.BSON_INT32_MAX = 0x7FFFFFFF; +BSON.BSON_INT32_MIN = -0x80000000; + +BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1; +BSON.BSON_INT64_MIN = -Math.pow(2, 63); + +// JS MAX PRECISE VALUES +BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double. +BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double. + +// Internal long versions +var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double. +var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double. + +/** + * Number BSON Type + * + * @classconstant BSON_DATA_NUMBER + **/ +BSON.BSON_DATA_NUMBER = 1; +/** + * String BSON Type + * + * @classconstant BSON_DATA_STRING + **/ +BSON.BSON_DATA_STRING = 2; +/** + * Object BSON Type + * + * @classconstant BSON_DATA_OBJECT + **/ +BSON.BSON_DATA_OBJECT = 3; +/** + * Array BSON Type + * + * @classconstant BSON_DATA_ARRAY + **/ +BSON.BSON_DATA_ARRAY = 4; +/** + * Binary BSON Type + * + * @classconstant BSON_DATA_BINARY + **/ +BSON.BSON_DATA_BINARY = 5; +/** + * ObjectID BSON Type + * + * @classconstant BSON_DATA_OID + **/ +BSON.BSON_DATA_OID = 7; +/** + * Boolean BSON Type + * + * @classconstant BSON_DATA_BOOLEAN + **/ +BSON.BSON_DATA_BOOLEAN = 8; +/** + * Date BSON Type + * + * @classconstant BSON_DATA_DATE + **/ +BSON.BSON_DATA_DATE = 9; +/** + * null BSON Type + * + * @classconstant BSON_DATA_NULL + **/ +BSON.BSON_DATA_NULL = 10; +/** + * RegExp BSON Type + * + * @classconstant BSON_DATA_REGEXP + **/ +BSON.BSON_DATA_REGEXP = 11; +/** + * Code BSON Type + * + * @classconstant BSON_DATA_CODE + **/ +BSON.BSON_DATA_CODE = 13; +/** + * Symbol BSON Type + * + * @classconstant BSON_DATA_SYMBOL + **/ +BSON.BSON_DATA_SYMBOL = 14; +/** + * Code with Scope BSON Type + * + * @classconstant BSON_DATA_CODE_W_SCOPE + **/ +BSON.BSON_DATA_CODE_W_SCOPE = 15; +/** + * 32 bit Integer BSON Type + * + * @classconstant BSON_DATA_INT + **/ +BSON.BSON_DATA_INT = 16; +/** + * Timestamp BSON Type + * + * @classconstant BSON_DATA_TIMESTAMP + **/ +BSON.BSON_DATA_TIMESTAMP = 17; +/** + * Long BSON Type + * + * @classconstant BSON_DATA_LONG + **/ +BSON.BSON_DATA_LONG = 18; +/** + * MinKey BSON Type + * + * @classconstant BSON_DATA_MIN_KEY + **/ +BSON.BSON_DATA_MIN_KEY = 0xff; +/** + * MaxKey BSON Type + * + * @classconstant BSON_DATA_MAX_KEY + **/ +BSON.BSON_DATA_MAX_KEY = 0x7f; + +/** + * Binary Default Type + * + * @classconstant BSON_BINARY_SUBTYPE_DEFAULT + **/ +BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; +/** + * Binary Function Type + * + * @classconstant BSON_BINARY_SUBTYPE_FUNCTION + **/ +BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1; +/** + * Binary Byte Array Type + * + * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY + **/ +BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +/** + * Binary UUID Type + * + * @classconstant BSON_BINARY_SUBTYPE_UUID + **/ +BSON.BSON_BINARY_SUBTYPE_UUID = 3; +/** + * Binary MD5 Type + * + * @classconstant BSON_BINARY_SUBTYPE_MD5 + **/ +BSON.BSON_BINARY_SUBTYPE_MD5 = 4; +/** + * Binary User Defined Type + * + * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED + **/ +BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +/** + * Calculate the bson size for a passed in Javascript object. + * + * @param {Object} object the Javascript object to calculate the BSON byte size for. + * @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**. + * @return {Number} returns the number of bytes the BSON object will take up. + * @api public + */ +BSON.calculateObjectSize = function calculateObjectSize(object, serializeFunctions) { + var totalLength = (4 + 1); + + if(Array.isArray(object)) { + for(var i = 0; i < object.length; i++) { + totalLength += calculateElement(i.toString(), object[i], serializeFunctions) + } + } else { + for(var key in object) { + totalLength += calculateElement(key, object[key], serializeFunctions) + } + } + + return totalLength; +} + +/** + * @ignore + * @api private + */ +function calculateElement(name, value, serializeFunctions) { + var isBuffer = typeof Buffer !== 'undefined'; + + switch(typeof value) { + case 'string': + return 1 + (!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1 + 4 + (!isBuffer ? numberOfBytes(value) : Buffer.byteLength(value, 'utf8')) + 1; + case 'number': + if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { // 32 bit + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (4 + 1); + } else { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1); + } + } else { // 64 bit + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1); + } + case 'undefined': + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1); + case 'boolean': + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 1); + case 'object': + if(value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1); + } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') { + 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(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') { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1); + } else if(value instanceof Code || value['_bsontype'] == 'Code') { + // Calculate size depending on the availability of a scope + if(value.scope != null && Object.keys(value.scope).length > 0) { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions); + } else { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1; + } + } else if(value instanceof Binary || value['_bsontype'] == 'Binary') { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1); + } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + ((!isBuffer ? numberOfBytes(value.value) : Buffer.byteLength(value.value, 'utf8')) + 4 + 1 + 1); + } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') { + // Set up correct object for serialization + var ordered_values = { + '$ref': value.namespace + , '$id' : value.oid + }; + + // Add db reference if it exists + if(null != value.db) { + ordered_values['$db'] = value.db; + } + + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + BSON.calculateObjectSize(ordered_values, serializeFunctions); + } else if(value instanceof RegExp || Object.prototype.toString.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) + 1; + } + case 'function': + // WTF for 0.4.X where typeof /someregexp/ === 'function' + if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object 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 { + if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions); + } else if(serializeFunctions) { + return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1; + } + } + } + + return 0; +} + +/** + * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization. + * + * @param {Object} object the Javascript object to serialize. + * @param {Boolean} checkKeys the serializer will check if keys are valid. + * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object. + * @param {Number} index the index in the buffer where we wish to start serializing into. + * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**. + * @return {Number} returns the new write index in the Buffer. + * @api public + */ +BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index, serializeFunctions) { + // Default setting false + serializeFunctions = serializeFunctions == null ? false : serializeFunctions; + // Write end information (length of the object) + var size = buffer.length; + // Write the size of the object + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + return serializeObject(object, checkKeys, buffer, index, serializeFunctions) - 1; +} + +/** + * @ignore + * @api private + */ +var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) { + // Process the object + if(Array.isArray(object)) { + for(var i = 0; i < object.length; i++) { + index = packElement(i.toString(), object[i], checkKeys, buffer, index, serializeFunctions); + } + } else { + for(var key in object) { + // Check the key and throw error if it's illegal + if(checkKeys == true && (key != '$db' && key != '$ref' && key != '$id')) { + BSON.checkKey(key); + } + + // Pack the element + index = packElement(key, object[key], checkKeys, buffer, index, serializeFunctions); + } + } + + // Write zero + buffer[index++] = 0; + return index; +} + +var stringToBytes = function(str) { + var ch, st, re = []; + for (var i = 0; i < str.length; i++ ) { + ch = str.charCodeAt(i); // get char + st = []; // set up "stack" + do { + st.push( ch & 0xFF ); // push byte to stack + ch = ch >> 8; // shift value down by 1 byte + } + while ( ch ); + // add stack contents to result + // done because chars have "wrong" endianness + re = re.concat( st.reverse() ); + } + // return an array of bytes + return re; +} + +var numberOfBytes = function(str) { + var ch, st, re = 0; + for (var i = 0; i < str.length; i++ ) { + ch = str.charCodeAt(i); // get char + st = []; // set up "stack" + do { + st.push( ch & 0xFF ); // push byte to stack + ch = ch >> 8; // shift value down by 1 byte + } + while ( ch ); + // add stack contents to result + // done because chars have "wrong" endianness + re = re + st.length; + } + // return an array of bytes + return re; +} + +/** + * @ignore + * @api private + */ +var writeToTypedArray = function(buffer, string, index) { + var bytes = stringToBytes(string); + for(var i = 0; i < bytes.length; i++) { + buffer[index + i] = bytes[i]; + } + return bytes.length; +} + +/** + * @ignore + * @api private + */ +var supportsBuffer = typeof Buffer != 'undefined'; + +/** + * @ignore + * @api private + */ +var packElement = function(name, value, checkKeys, buffer, index, serializeFunctions) { + var startIndex = index; + + switch(typeof value) { + case 'string': + // Encode String type + buffer[index++] = BSON.BSON_DATA_STRING; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + + // Calculate size + var size = supportsBuffer ? Buffer.byteLength(value) + 1 : numberOfBytes(value) + 1; + // Write the size of the string to buffer + buffer[index + 3] = (size >> 24) & 0xff; + buffer[index + 2] = (size >> 16) & 0xff; + buffer[index + 1] = (size >> 8) & 0xff; + buffer[index] = size & 0xff; + // Ajust the index + index = index + 4; + // Write the string + supportsBuffer ? buffer.write(value, index, 'utf8') : writeToTypedArray(buffer, value, index); + // Update index + index = index + size - 1; + // Write zero + buffer[index++] = 0; + // Return index + return index; + case 'number': + // We have an integer value + if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + // If the value fits in 32 bits encode as int, if it fits in a double + // encode it as a double, otherwise long + if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { + // Set int type 32 bits or less + buffer[index++] = BSON.BSON_DATA_INT; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write the int value + buffer[index++] = value & 0xff; + buffer[index++] = (value >> 8) & 0xff; + buffer[index++] = (value >> 16) & 0xff; + buffer[index++] = (value >> 24) & 0xff; + } else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + } else { + // Set long type + buffer[index++] = BSON.BSON_DATA_LONG; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + var longVal = Long.fromNumber(value); + var lowBits = longVal.getLowBits(); + var highBits = longVal.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + } + } else { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + } + + return index; + case 'undefined': + // Set long type + buffer[index++] = BSON.BSON_DATA_NULL; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + return index; + case 'boolean': + // Write the type + buffer[index++] = BSON.BSON_DATA_BOOLEAN; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Encode the boolean value + buffer[index++] = value ? 1 : 0; + return index; + case 'object': + if(value === null || value instanceof MinKey || value instanceof MaxKey + || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { + // Write the type of either min or max key + if(value === null) { + buffer[index++] = BSON.BSON_DATA_NULL; + } else if(value instanceof MinKey) { + buffer[index++] = BSON.BSON_DATA_MIN_KEY; + } else { + buffer[index++] = BSON.BSON_DATA_MAX_KEY; + } + + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + return index; + } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') { + // Write the type + buffer[index++] = BSON.BSON_DATA_OID; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + + // Write objectid + supportsBuffer ? buffer.write(value.id, index, 'binary') : writeToTypedArray(buffer, value.id, index); + // Ajust index + index = index + 12; + return index; + } else if(value instanceof Date) { + // Write the type + buffer[index++] = BSON.BSON_DATA_DATE; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + + // Write the date + var dateInMilis = Long.fromNumber(value.getTime()); + var lowBits = dateInMilis.getLowBits(); + var highBits = dateInMilis.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + return index; + } else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) { + // Write the type + buffer[index++] = BSON.BSON_DATA_BINARY; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Get size of the buffer (current write point) + var size = value.length; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the default subtype + buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT; + // Copy the content form the binary field to the buffer + value.copy(buffer, index, 0, size); + // Adjust the index + index = index + size; + return index; + } else if(value instanceof Long || value instanceof Timestamp || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') { + // Write the type + buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write the date + var lowBits = value.getLowBits(); + var highBits = value.getHighBits(); + // Encode low bits + buffer[index++] = lowBits & 0xff; + buffer[index++] = (lowBits >> 8) & 0xff; + buffer[index++] = (lowBits >> 16) & 0xff; + buffer[index++] = (lowBits >> 24) & 0xff; + // Encode high bits + buffer[index++] = highBits & 0xff; + buffer[index++] = (highBits >> 8) & 0xff; + buffer[index++] = (highBits >> 16) & 0xff; + buffer[index++] = (highBits >> 24) & 0xff; + return index; + } else if(value instanceof Double || value['_bsontype'] == 'Double') { + // Encode as double + buffer[index++] = BSON.BSON_DATA_NUMBER; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Write float + writeIEEE754(buffer, value, index, 'little', 52, 8); + // Ajust index + index = index + 8; + return index; + } else if(value instanceof Code || value['_bsontype'] == 'Code') { + if(value.scope != null && Object.keys(value.scope).length > 0) { + // Write the type + buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Calculate the scope size + var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions); + // Function string + var functionString = value.code.toString(); + // Function Size + var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1; + + // Calculate full size of the object + var totalSize = 4 + codeSize + scopeSize + 4; + + // Write the total size of the object + buffer[index++] = totalSize & 0xff; + buffer[index++] = (totalSize >> 8) & 0xff; + buffer[index++] = (totalSize >> 16) & 0xff; + buffer[index++] = (totalSize >> 24) & 0xff; + + // Write the size of the string to buffer + buffer[index++] = codeSize & 0xff; + buffer[index++] = (codeSize >> 8) & 0xff; + buffer[index++] = (codeSize >> 16) & 0xff; + buffer[index++] = (codeSize >> 24) & 0xff; + + // Write the string + supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index); + // Update index + index = index + codeSize - 1; + // Write zero + buffer[index++] = 0; + // Serialize the scope object + var scopeObjectBuffer = supportsBuffer ? new Buffer(scopeSize) : new Uint8Array(new ArrayBuffer(scopeSize)); + // Execute the serialization into a seperate buffer + serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions); + + // Adjusted scope Size (removing the header) + var scopeDocSize = scopeSize; + // Write scope object size + buffer[index++] = scopeDocSize & 0xff; + buffer[index++] = (scopeDocSize >> 8) & 0xff; + buffer[index++] = (scopeDocSize >> 16) & 0xff; + buffer[index++] = (scopeDocSize >> 24) & 0xff; + + // Write the scopeObject into the buffer + supportsBuffer ? scopeObjectBuffer.copy(buffer, index, 0, scopeSize) : buffer.set(scopeObjectBuffer, index); + // Adjust index, removing the empty size of the doc (5 bytes 0000000005) + index = index + scopeDocSize - 5; + // Write trailing zero + buffer[index++] = 0; + return index + } else { + buffer[index++] = BSON.BSON_DATA_CODE; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Function string + var functionString = value.code.toString(); + // Function Size + var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the string + buffer.write(functionString, index, 'utf8'); + // Update index + index = index + size - 1; + // Write zero + buffer[index++] = 0; + return index; + } + } else if(value instanceof Binary || value['_bsontype'] == 'Binary') { + // Write the type + buffer[index++] = BSON.BSON_DATA_BINARY; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Extract the buffer + var data = value.value(true); + // Calculate size + var size = value.position; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the subtype to the buffer + buffer[index++] = value.sub_type; + // Write the data to the object + supportsBuffer ? data.copy(buffer, index, 0, value.position) : buffer.set(data, index); + // Ajust index + index = index + value.position; + return index; + } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') { + // Write the type + buffer[index++] = BSON.BSON_DATA_SYMBOL; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Calculate size + var size = supportsBuffer ? Buffer.byteLength(value.value) + 1 : numberOfBytes(value.value) + 1; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the string + buffer.write(value.value, index, 'utf8'); + // Update index + index = index + size - 1; + // Write zero + buffer[index++] = 0x00; + return index; + } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') { + // Write the type + buffer[index++] = BSON.BSON_DATA_OBJECT; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Set up correct object for serialization + var ordered_values = { + '$ref': value.namespace + , '$id' : value.oid + }; + + // Add db reference if it exists + if(null != value.db) { + ordered_values['$db'] = value.db; + } + + // Message size + var size = BSON.calculateObjectSize(ordered_values, serializeFunctions); + // Serialize the object + var endIndex = BSON.serializeWithBufferAndIndex(ordered_values, checkKeys, buffer, index, serializeFunctions); + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write zero for object + buffer[endIndex++] = 0x00; + // Return the end index + return endIndex; + } else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') { + // Write the type + buffer[index++] = BSON.BSON_DATA_REGEXP; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + + // Write the regular expression string + supportsBuffer ? buffer.write(value.source, index, 'utf8') : writeToTypedArray(buffer, value.source, index); + // Adjust the index + index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source)); + // Write zero + buffer[index++] = 0x00; + // Write the parameters + if(value.global) buffer[index++] = 0x73; // s + if(value.ignoreCase) buffer[index++] = 0x69; // i + if(value.multiline) buffer[index++] = 0x6d; // m + // Add ending zero + buffer[index++] = 0x00; + return index; + } else { + // Write the type + buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Adjust the index + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Serialize the object + var endIndex = serializeObject(value, checkKeys, buffer, index + 4, serializeFunctions); + // Write size + var size = endIndex - index; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + return endIndex; + } + case 'function': + // WTF for 0.4.X where typeof /someregexp/ === 'function' + if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') { + // Write the type + buffer[index++] = BSON.BSON_DATA_REGEXP; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + + // Write the regular expression string + buffer.write(value.source, index, 'utf8'); + // Adjust the index + index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source)); + // Write zero + buffer[index++] = 0x00; + // Write the parameters + if(value.global) buffer[index++] = 0x73; // s + if(value.ignoreCase) buffer[index++] = 0x69; // i + if(value.multiline) buffer[index++] = 0x6d; // m + // Add ending zero + buffer[index++] = 0x00; + return index; + } else { + if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) { + // Write the type + buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Calculate the scope size + var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions); + // Function string + var functionString = value.toString(); + // Function Size + var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1; + + // Calculate full size of the object + var totalSize = 4 + codeSize + scopeSize; + + // Write the total size of the object + buffer[index++] = totalSize & 0xff; + buffer[index++] = (totalSize >> 8) & 0xff; + buffer[index++] = (totalSize >> 16) & 0xff; + buffer[index++] = (totalSize >> 24) & 0xff; + + // Write the size of the string to buffer + buffer[index++] = codeSize & 0xff; + buffer[index++] = (codeSize >> 8) & 0xff; + buffer[index++] = (codeSize >> 16) & 0xff; + buffer[index++] = (codeSize >> 24) & 0xff; + + // Write the string + buffer.write(functionString, index, 'utf8'); + // Update index + index = index + codeSize - 1; + // Write zero + buffer[index++] = 0; + // Serialize the scope object + var scopeObjectBuffer = new Buffer(scopeSize); + // Execute the serialization into a seperate buffer + serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions); + + // Adjusted scope Size (removing the header) + var scopeDocSize = scopeSize - 4; + // Write scope object size + buffer[index++] = scopeDocSize & 0xff; + buffer[index++] = (scopeDocSize >> 8) & 0xff; + buffer[index++] = (scopeDocSize >> 16) & 0xff; + buffer[index++] = (scopeDocSize >> 24) & 0xff; + + // Write the scopeObject into the buffer + scopeObjectBuffer.copy(buffer, index, 0, scopeSize); + + // Adjust index, removing the empty size of the doc (5 bytes 0000000005) + index = index + scopeDocSize - 5; + // Write trailing zero + buffer[index++] = 0; + return index + } else if(serializeFunctions) { + buffer[index++] = BSON.BSON_DATA_CODE; + // Number of written bytes + var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index); + // Encode the name + index = index + numberOfWrittenBytes + 1; + buffer[index - 1] = 0; + // Function string + var functionString = value.toString(); + // Function Size + var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1; + // Write the size of the string to buffer + buffer[index++] = size & 0xff; + buffer[index++] = (size >> 8) & 0xff; + buffer[index++] = (size >> 16) & 0xff; + buffer[index++] = (size >> 24) & 0xff; + // Write the string + buffer.write(functionString, index, 'utf8'); + // Update index + index = index + size - 1; + // Write zero + buffer[index++] = 0; + return index; + } + } + } + + // If no value to serialize + return index; +} + +/** + * Serialize a Javascript object. + * + * @param {Object} object the Javascript object to serialize. + * @param {Boolean} checkKeys the serializer will check if keys are valid. + * @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**. + * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**. + * @return {Buffer} returns the Buffer object containing the serialized object. + * @api public + */ +BSON.serialize = function(object, checkKeys, asBuffer, serializeFunctions) { + var buffer = null; + // Calculate the size of the object + var size = BSON.calculateObjectSize(object, serializeFunctions); + // Fetch the best available type for storing the binary data + if(buffer = typeof Buffer != 'undefined') { + buffer = new Buffer(size); + asBuffer = true; + } else if(typeof Uint8Array != 'undefined') { + buffer = new Uint8Array(new ArrayBuffer(size)); + } else { + buffer = new Array(size); + } + + // If asBuffer is false use typed arrays + BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, 0, serializeFunctions); + return buffer; +} + +/** + * Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5 + * + * @ignore + * @api private + */ +var functionCache = BSON.functionCache = {}; + +/** + * Crc state variables shared by function + * + * @ignore + * @api private + */ +var table = [0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D]; + +/** + * CRC32 hash method, Fast and enough versitility for our usage + * + * @ignore + * @api private + */ +var crc32 = function(string, start, end) { + var crc = 0 + var x = 0; + var y = 0; + crc = crc ^ (-1); + + for(var i = start, iTop = end; i < iTop;i++) { + y = (crc ^ string[i]) & 0xFF; + x = table[y]; + crc = (crc >>> 8) ^ x; + } + + return crc ^ (-1); +} + +/** + * Deserialize stream data as BSON documents. + * + * Options + * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized. + * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse. + * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function. + * + * @param {Buffer} data the buffer containing the serialized set of BSON documents. + * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start. + * @param {Number} numberOfDocuments number of documents to deserialize. + * @param {Array} documents an array where to store the deserialized documents. + * @param {Number} docStartIndex the index in the documents array from where to start inserting documents. + * @param {Object} [options] additional options used for the deserialization. + * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents. + * @api public + */ +BSON.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) { + // if(numberOfDocuments !== documents.length) throw new Error("Number of expected results back is less than the number of documents"); + options = options != null ? options : {}; + var index = startIndex; + // Loop over all documents + for(var i = 0; i < numberOfDocuments; i++) { + // Find size of the document + var size = data[index] | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24; + // Update options with index + options['index'] = index; + // Parse the document at this point + documents[docStartIndex + i] = BSON.deserialize(data, options); + // Adjust index by the document size + index = index + size; + } + + // Return object containing end index of parsing and list of documents + return index; +} + +/** + * Ensure eval is isolated. + * + * @ignore + * @api private + */ +var isolateEvalWithHash = function(functionCache, hash, functionString, object) { + // Contains the value we are going to set + var value = null; + + // Check for cache hit, eval if missing and return cached function + if(functionCache[hash] == null) { + eval("value = " + functionString); + functionCache[hash] = value; + } + // Set the object + return functionCache[hash].bind(object); +} + +/** + * Ensure eval is isolated. + * + * @ignore + * @api private + */ +var isolateEval = function(functionString) { + // Contains the value we are going to set + var value = null; + // Eval the function + eval("value = " + functionString); + return value; +} + +/** + * Convert Uint8Array to String + * + * @ignore + * @api private + */ +var convertUint8ArrayToUtf8String = function(byteArray, startIndex, endIndex) { + return BinaryParser.decode_utf8(convertArraytoUtf8BinaryString(byteArray, startIndex, endIndex)); +} + +var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) { + var result = ""; + for(var i = startIndex; i < endIndex; i++) { + result = result + String.fromCharCode(byteArray[i]); + } + + return result; +}; + +/** + * Deserialize data as BSON. + * + * Options + * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized. + * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse. + * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function. + * + * @param {Buffer} buffer the buffer containing the serialized set of BSON documents. + * @param {Object} [options] additional options used for the deserialization. + * @param {Boolean} [isArray] ignore used for recursive parsing. + * @return {Object} returns the deserialized Javascript Object. + * @api public + */ +BSON.deserialize = function(buffer, options, isArray) { + // Options + options = options == null ? {} : options; + var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions']; + var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions']; + var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32']; + + // Validate that we have at least 4 bytes of buffer + if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long"); + + // Set up index + var index = typeof options['index'] == 'number' ? options['index'] : 0; + // Reads in a C style string + var readCStyleString = function() { + // Get the start search index + var i = index; + // Locate the end of the c string + while(buffer[i] !== 0x00) { i++ } + // Grab utf8 encoded string + var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, i) : convertUint8ArrayToUtf8String(buffer, index, i); + // Update index position + index = i + 1; + // Return string + return string; + } + + // Create holding object + var object = isArray ? [] : {}; + + // Read the document size + var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + + // Ensure buffer is valid size + if(size < 5 || size > buffer.length) throw new Error("corrupt bson message"); + + // While we have more left data left keep parsing + while(true) { + // Read the type + var elementType = buffer[index++]; + // If we get a zero it's the last byte, exit + if(elementType == 0) break; + // Read the name of the field + var name = readCStyleString(); + // Switch on the type + switch(elementType) { + case BSON.BSON_DATA_OID: + var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('binary', index, index + 12) : convertArraytoUtf8BinaryString(buffer, index, index + 12); + // Decode the oid + object[name] = new ObjectID(string); + // Update index + index = index + 12; + break; + case BSON.BSON_DATA_STRING: + // Read the content of the field + var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Add string to object + object[name] = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1); + // Update parse index position + index = index + stringSize; + break; + case BSON.BSON_DATA_INT: + // Decode the 32bit value + object[name] = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + break; + case BSON.BSON_DATA_NUMBER: + // Decode the double value + object[name] = readIEEE754(buffer, index, 'little', 52, 8); + // Update the index + index = index + 8; + break; + case BSON.BSON_DATA_DATE: + // Unpack the low and high bits + var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Set date object + object[name] = new Date(new Long(lowBits, highBits).toNumber()); + break; + case BSON.BSON_DATA_BOOLEAN: + // Parse the boolean value + object[name] = buffer[index++] == 1; + break; + case BSON.BSON_DATA_NULL: + // Parse the boolean value + object[name] = null; + break; + case BSON.BSON_DATA_BINARY: + // Decode the size of the binary blob + var binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Decode the subtype + var subType = buffer[index++]; + // Decode as raw Buffer object if options specifies it + if(buffer['slice'] != null) { + object[name] = new Binary(buffer.slice(index, index + binarySize), subType); + } else { + var _buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(binarySize)) : new Array(binarySize); + for(var i = 0; i < binarySize; i++) { + _buffer[i] = buffer[index + i]; + } + // Create the binary object + object[name] = new Binary(_buffer, subType); + } + // Update the index + index = index + binarySize; + break; + case BSON.BSON_DATA_ARRAY: + options['index'] = index; + // Decode the size of the array document + var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24; + // Set the array to the object + object[name] = BSON.deserialize(buffer, options, true); + // Adjust the index + index = index + objectSize; + break; + case BSON.BSON_DATA_OBJECT: + options['index'] = index; + // Decode the size of the object document + var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24; + // Set the array to the object + object[name] = BSON.deserialize(buffer, options, false); + // Adjust the index + index = index + objectSize; + break; + case BSON.BSON_DATA_REGEXP: + // Create the regexp + var source = readCStyleString(); + var regExpOptions = readCStyleString(); + // For each option add the corresponding one for javascript + var optionsArray = new Array(regExpOptions.length); + + // Parse options + for(var i = 0; i < regExpOptions.length; i++) { + switch(regExpOptions[i]) { + case 'm': + optionsArray[i] = 'm'; + break; + case 's': + optionsArray[i] = 'g'; + break; + case 'i': + optionsArray[i] = 'i'; + break; + } + } + + object[name] = new RegExp(source, optionsArray.join('')); + break; + case BSON.BSON_DATA_LONG: + // Unpack the low and high bits + var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Create long object + var long = new Long(lowBits, highBits); + // Set the object + object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long; + break; + case BSON.BSON_DATA_SYMBOL: + // Read the content of the field + var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Add string to object + object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1)); + // Update parse index position + index = index + stringSize; + break; + case BSON.BSON_DATA_TIMESTAMP: + // Unpack the low and high bits + var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Set the object + object[name] = new Timestamp(lowBits, highBits); + break; + case BSON.BSON_DATA_MIN_KEY: + // Parse the object + object[name] = new MinKey(); + break; + case BSON.BSON_DATA_MAX_KEY: + // Parse the object + object[name] = new MaxKey(); + break; + case BSON.BSON_DATA_CODE: + // Read the content of the field + var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Function string + var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1); + + // If we are evaluating the functions + if(evalFunctions) { + // Contains the value we are going to set + var value = null; + // If we have cache enabled let's look for the md5 of the function in the cache + if(cacheFunctions) { + var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString; + // Got to do this to avoid V8 deoptimizing the call due to finding eval + object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + } else { + // Set directly + object[name] = isolateEval(functionString); + } + } else { + object[name] = new Code(functionString, {}); + } + + // Update parse index position + index = index + stringSize; + break; + case BSON.BSON_DATA_CODE_W_SCOPE: + // Read the content of the field + var totalSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; + // Javascript function + var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1); + // Update parse index position + index = index + stringSize; + // Parse the element + options['index'] = index; + // Decode the size of the object document + var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24; + // Decode the scope object + var scopeObject = BSON.deserialize(buffer, options, false); + // Adjust the index + index = index + objectSize; + + // If we are evaluating the functions + if(evalFunctions) { + // Contains the value we are going to set + var value = null; + // If we have cache enabled let's look for the md5 of the function in the cache + if(cacheFunctions) { + var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString; + // Got to do this to avoid V8 deoptimizing the call due to finding eval + object[name] = isolateEvalWithHash(functionCache, hash, functionString, object); + } else { + // Set directly + object[name] = isolateEval(functionString); + } + + // Set the scope on the object + object[name].scope = scopeObject; + } else { + object[name] = new Code(functionString, scopeObject); + } + + // Add string to object + break; + } + } + + // Check if we have a db ref object + if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']); + + // Return the final objects + return object; +} + +/** + * Check if key name is valid. + * + * @ignore + * @api private + */ +BSON.checkKey = function checkKey (key) { + if (!key.length) return; + // Check if we have a legal key for the object + if('$' == key[0]) { + throw Error("key " + key + " must not start with '$'"); + } else if (!!~key.indexOf('.')) { + throw Error("key " + key + " must not contain '.'"); + } +}; + +/** + * Deserialize data as BSON. + * + * Options + * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized. + * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse. + * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function. + * + * @param {Buffer} buffer the buffer containing the serialized set of BSON documents. + * @param {Object} [options] additional options used for the deserialization. + * @param {Boolean} [isArray] ignore used for recursive parsing. + * @return {Object} returns the deserialized Javascript Object. + * @api public + */ +BSON.prototype.deserialize = function(data, options) { + return BSON.deserialize(data, options); +} + +/** + * Deserialize stream data as BSON documents. + * + * Options + * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized. + * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse. + * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function. + * + * @param {Buffer} data the buffer containing the serialized set of BSON documents. + * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start. + * @param {Number} numberOfDocuments number of documents to deserialize. + * @param {Array} documents an array where to store the deserialized documents. + * @param {Number} docStartIndex the index in the documents array from where to start inserting documents. + * @param {Object} [options] additional options used for the deserialization. + * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents. + * @api public + */ +BSON.prototype.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) { + return BSON.deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options); +} + +/** + * Serialize a Javascript object. + * + * @param {Object} object the Javascript object to serialize. + * @param {Boolean} checkKeys the serializer will check if keys are valid. + * @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**. + * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**. + * @return {Buffer} returns the Buffer object containing the serialized object. + * @api public + */ +BSON.prototype.serialize = function(object, checkKeys, asBuffer, serializeFunctions) { + return BSON.serialize(object, checkKeys, asBuffer, serializeFunctions); +} + +/** + * Calculate the bson size for a passed in Javascript object. + * + * @param {Object} object the Javascript object to calculate the BSON byte size for. + * @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**. + * @return {Number} returns the number of bytes the BSON object will take up. + * @api public + */ +BSON.prototype.calculateObjectSize = function(object, serializeFunctions) { + return BSON.calculateObjectSize(object, serializeFunctions); +} + +/** + * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization. + * + * @param {Object} object the Javascript object to serialize. + * @param {Boolean} checkKeys the serializer will check if keys are valid. + * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object. + * @param {Number} index the index in the buffer where we wish to start serializing into. + * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**. + * @return {Number} returns the new write index in the Buffer. + * @api public + */ +BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, buffer, startIndex, serializeFunctions) { + return BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, startIndex, serializeFunctions); +} + +/** + * @ignore + * @api private + */ +if(typeof window === 'undefined') { + exports.Code = Code; + exports.Symbol = Symbol; + exports.BSON = BSON; + exports.DBRef = DBRef; + exports.Binary = Binary; + exports.ObjectID = ObjectID; + exports.Long = Long; + exports.Timestamp = Timestamp; + exports.Double = Double; + exports.MinKey = MinKey; + exports.MaxKey = MaxKey; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/code.js b/node_modules/mongodb/node_modules/bson/lib/bson/code.js new file mode 100644 index 0000000..c15c776 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/code.js @@ -0,0 +1,27 @@ +/** + * A class representation of the BSON Code type. + * + * @class Represents the BSON Code type. + * @param {String|Function} code a string or function. + * @param {Object} [scope] an optional scope for the function. + * @return {Code} + */ +function Code(code, scope) { + if(!(this instanceof Code)) return new Code(code, scope); + + this._bsontype = 'Code'; + this.code = code; + this.scope = scope == null ? {} : scope; +}; + +/** + * @ignore + * @api private + */ +Code.prototype.toJSON = function() { + return {scope:this.scope, code:this.code}; +} + +if(typeof window === 'undefined') { + exports.Code = Code; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/db_ref.js b/node_modules/mongodb/node_modules/bson/lib/bson/db_ref.js new file mode 100644 index 0000000..5e5e33b --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/db_ref.js @@ -0,0 +1,33 @@ +/** + * A class representation of the BSON DBRef type. + * + * @class Represents the BSON DBRef type. + * @param {String} namespace the collection name. + * @param {ObjectID} oid the reference ObjectID. + * @param {String} [db] optional db name, if omitted the reference is local to the current db. + * @return {DBRef} + */ +function DBRef(namespace, oid, db) { + if(!(this instanceof DBRef)) return new DBRef(namespace, oid, db); + + this._bsontype = 'DBRef'; + this.namespace = namespace; + this.oid = oid; + this.db = db; +}; + +/** + * @ignore + * @api private + */ +DBRef.prototype.toJSON = function() { + return { + '$ref':this.namespace, + '$id':this.oid, + '$db':this.db == null ? '' : this.db + }; +} + +if(typeof window === 'undefined') { + exports.DBRef = DBRef; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/double.js b/node_modules/mongodb/node_modules/bson/lib/bson/double.js new file mode 100644 index 0000000..b1e12df --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/double.js @@ -0,0 +1,35 @@ +/** + * A class representation of the BSON Double type. + * + * @class Represents the BSON Double type. + * @param {Number} value the number we want to represent as a double. + * @return {Double} + */ +function Double(value) { + if(!(this instanceof Double)) return new Double(value); + + this._bsontype = 'Double'; + this.value = value; +} + +/** + * Access the number value. + * + * @return {Number} returns the wrapped double number. + * @api public + */ +Double.prototype.valueOf = function() { + return this.value; +}; + +/** + * @ignore + * @api private + */ +Double.prototype.toJSON = function() { + return this.value; +} + +if(typeof window === 'undefined') { + exports.Double = Double; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/float_parser.js b/node_modules/mongodb/node_modules/bson/lib/bson/float_parser.js new file mode 100644 index 0000000..7c12fe5 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/float_parser.js @@ -0,0 +1,123 @@ +// Copyright (c) 2008, Fair Oaks Labs, Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * Neither the name of Fair Oaks Labs, Inc. nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// +// Modifications to writeIEEE754 to support negative zeroes made by Brian White + +var readIEEE754 = function(buffer, offset, endian, mLen, nBytes) { + var e, m, + bBE = (endian === 'big'), + eLen = nBytes * 8 - mLen - 1, + eMax = (1 << eLen) - 1, + eBias = eMax >> 1, + nBits = -7, + i = bBE ? 0 : (nBytes - 1), + d = bBE ? 1 : -1, + s = buffer[offset + i]; + + i += d; + + e = s & ((1 << (-nBits)) - 1); + s >>= (-nBits); + nBits += eLen; + for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8); + + m = e & ((1 << (-nBits)) - 1); + e >>= (-nBits); + nBits += mLen; + for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8); + + if (e === 0) { + e = 1 - eBias; + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity); + } else { + m = m + Math.pow(2, mLen); + e = e - eBias; + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen); +}; + +var writeIEEE754 = function(buffer, value, offset, endian, mLen, nBytes) { + var e, m, c, + bBE = (endian === 'big'), + eLen = nBytes * 8 - mLen - 1, + eMax = (1 << eLen) - 1, + eBias = eMax >> 1, + rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0), + i = bBE ? (nBytes-1) : 0, + d = bBE ? -1 : 1, + s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; + + value = Math.abs(value); + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0; + e = eMax; + } else { + e = Math.floor(Math.log(value) / Math.LN2); + if (value * (c = Math.pow(2, -e)) < 1) { + e--; + c *= 2; + } + if (e+eBias >= 1) { + value += rt / c; + } else { + value += rt * Math.pow(2, 1 - eBias); + } + if (value * c >= 2) { + e++; + c /= 2; + } + + if (e + eBias >= eMax) { + m = 0; + e = eMax; + } else if (e + eBias >= 1) { + m = (value * c - 1) * Math.pow(2, mLen); + e = e + eBias; + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); + e = 0; + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8); + + e = (e << mLen) | m; + eLen += mLen; + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8); + + buffer[offset + i - d] |= s * 128; +}; + +if(typeof window === 'undefined') { + exports.readIEEE754 = readIEEE754; + exports.writeIEEE754 = writeIEEE754; +} diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/index.js b/node_modules/mongodb/node_modules/bson/lib/bson/index.js new file mode 100644 index 0000000..950fcad --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/index.js @@ -0,0 +1,74 @@ +try { + exports.BSONPure = require('./bson'); + exports.BSONNative = require('../../ext'); +} catch(err) { + // do nothing +} + +[ './binary_parser' + , './binary' + , './code' + , './db_ref' + , './double' + , './max_key' + , './min_key' + , './objectid' + , './symbol' + , './timestamp' + , './long'].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + exports[i] = module[i]; + } +}); + +// Exports all the classes for the NATIVE JS BSON Parser +exports.native = function() { + var classes = {}; + // Map all the classes + [ './binary_parser' + , './binary' + , './code' + , './db_ref' + , './double' + , './max_key' + , './min_key' + , './objectid' + , './symbol' + , './timestamp' + , './long' + , '../../ext' +].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + classes[i] = module[i]; + } + }); + // Return classes list + return classes; +} + +// Exports all the classes for the PURE JS BSON Parser +exports.pure = function() { + var classes = {}; + // Map all the classes + [ './binary_parser' + , './binary' + , './code' + , './db_ref' + , './double' + , './max_key' + , './min_key' + , './objectid' + , './symbol' + , './timestamp' + , './long' + , '././bson'].forEach(function (path) { + var module = require('./' + path); + for (var i in module) { + classes[i] = module[i]; + } + }); + // Return classes list + return classes; +} diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/long.js b/node_modules/mongodb/node_modules/bson/lib/bson/long.js new file mode 100644 index 0000000..6aa9749 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/long.js @@ -0,0 +1,856 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright 2009 Google Inc. All Rights Reserved + +/** + * Defines a Long class for representing a 64-bit two's-complement + * integer value, which faithfully simulates the behavior of a Java "Long". This + * implementation is derived from LongLib in GWT. + * + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Longs. + * + * The internal representation of a Long is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @class Represents the BSON Long type. + * @param {Number} low the low (signed) 32 bits of the Long. + * @param {Number} high the high (signed) 32 bits of the Long. + */ +function Long(low, high) { + if(!(this instanceof Long)) return new Long(low, high); + + this._bsontype = 'Long'; + /** + * @type {number} + * @api private + */ + this.low_ = low | 0; // force into 32 signed bits. + + /** + * @type {number} + * @api private + */ + this.high_ = high | 0; // force into 32 signed bits. +}; + +/** + * Return the int value. + * + * @return {Number} the value, assuming it is a 32-bit integer. + * @api public + */ +Long.prototype.toInt = function() { + return this.low_; +}; + +/** + * Return the Number value. + * + * @return {Number} the closest floating-point representation to this value. + * @api public + */ +Long.prototype.toNumber = function() { + return this.high_ * Long.TWO_PWR_32_DBL_ + + this.getLowBitsUnsigned(); +}; + +/** + * Return the JSON value. + * + * @return {String} the JSON representation. + * @api public + */ +Long.prototype.toJSON = function() { + return this.toString(); +} + +/** + * Return the String value. + * + * @param {Number} [opt_radix] the radix in which the text should be written. + * @return {String} the textual representation of this value. + * @api public + */ +Long.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } + + if (this.isNegative()) { + if (this.equals(Long.MIN_VALUE)) { + // We need to change the Long value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixLong = Long.fromNumber(radix); + var div = this.div(radixLong); + var rem = div.multiply(radixLong).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Long.fromNumber(Math.pow(radix, 6)); + + var rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower); + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt(); + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + +/** + * Return the high 32-bits value. + * + * @return {Number} the high 32-bits as a signed value. + * @api public + */ +Long.prototype.getHighBits = function() { + return this.high_; +}; + +/** + * Return the low 32-bits value. + * + * @return {Number} the low 32-bits as a signed value. + * @api public + */ +Long.prototype.getLowBits = function() { + return this.low_; +}; + +/** + * Return the low unsigned 32-bits value. + * + * @return {Number} the low 32-bits as an unsigned value. + * @api public + */ +Long.prototype.getLowBitsUnsigned = function() { + return (this.low_ >= 0) ? + this.low_ : Long.TWO_PWR_32_DBL_ + this.low_; +}; + +/** + * Returns the number of bits needed to represent the absolute value of this Long. + * + * @return {Number} Returns the number of bits needed to represent the absolute value of this Long. + * @api public + */ +Long.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { + if (this.equals(Long.MIN_VALUE)) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high_ != 0 ? this.high_ : this.low_; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) != 0) { + break; + } + } + return this.high_ != 0 ? bit + 33 : bit + 1; + } +}; + +/** + * Return whether this value is zero. + * + * @return {Boolean} whether this value is zero. + * @api public + */ +Long.prototype.isZero = function() { + return this.high_ == 0 && this.low_ == 0; +}; + +/** + * Return whether this value is negative. + * + * @return {Boolean} whether this value is negative. + * @api public + */ +Long.prototype.isNegative = function() { + return this.high_ < 0; +}; + +/** + * Return whether this value is odd. + * + * @return {Boolean} whether this value is odd. + * @api public + */ +Long.prototype.isOdd = function() { + return (this.low_ & 1) == 1; +}; + +/** + * Return whether this Long equals the other + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long equals the other + * @api public + */ +Long.prototype.equals = function(other) { + return (this.high_ == other.high_) && (this.low_ == other.low_); +}; + +/** + * Return whether this Long does not equal the other. + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long does not equal the other. + * @api public + */ +Long.prototype.notEquals = function(other) { + return (this.high_ != other.high_) || (this.low_ != other.low_); +}; + +/** + * Return whether this Long is less than the other. + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long is less than the other. + * @api public + */ +Long.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + +/** + * Return whether this Long is less than or equal to the other. + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long is less than or equal to the other. + * @api public + */ +Long.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + +/** + * Return whether this Long is greater than the other. + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long is greater than the other. + * @api public + */ +Long.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + +/** + * Return whether this Long is greater than or equal to the other. + * + * @param {Long} other Long to compare against. + * @return {Boolean} whether this Long is greater than or equal to the other. + * @api public + */ +Long.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + +/** + * Compares this Long with the given one. + * + * @param {Long} other Long to compare against. + * @return {Boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater. + * @api public + */ +Long.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + + // at this point, the signs are the same, so subtraction will not overflow + if (this.subtract(other).isNegative()) { + return -1; + } else { + return 1; + } +}; + +/** + * The negation of this value. + * + * @return {Long} the negation of this value. + * @api public + */ +Long.prototype.negate = function() { + if (this.equals(Long.MIN_VALUE)) { + return Long.MIN_VALUE; + } else { + return this.not().add(Long.ONE); + } +}; + +/** + * Returns the sum of this and the given Long. + * + * @param {Long} other Long to add to this one. + * @return {Long} the sum of this and the given Long. + * @api public + */ +Long.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns the difference of this and the given Long. + * + * @param {Long} other Long to subtract from this. + * @return {Long} the difference of this and the given Long. + * @api public + */ +Long.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + +/** + * Returns the product of this and the given Long. + * + * @param {Long} other Long to multiply with this. + * @return {Long} the product of this and the other. + * @api public + */ +Long.prototype.multiply = function(other) { + if (this.isZero()) { + return Long.ZERO; + } else if (other.isZero()) { + return Long.ZERO; + } + + if (this.equals(Long.MIN_VALUE)) { + return other.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } else if (other.equals(Long.MIN_VALUE)) { + return this.isOdd() ? Long.MIN_VALUE : Long.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate().multiply(other).negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both Longs are small, use float multiplication + if (this.lessThan(Long.TWO_PWR_24_) && + other.lessThan(Long.TWO_PWR_24_)) { + return Long.fromNumber(this.toNumber() * other.toNumber()); + } + + // Divide each Long into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns this Long divided by the given one. + * + * @param {Long} other Long by which to divide. + * @return {Long} this Long divided by the given one. + * @api public + */ +Long.prototype.div = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return Long.ZERO; + } + + if (this.equals(Long.MIN_VALUE)) { + if (other.equals(Long.ONE) || + other.equals(Long.NEG_ONE)) { + return Long.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + } else if (other.equals(Long.MIN_VALUE)) { + return Long.ONE; + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + var approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(Long.ZERO)) { + return other.isNegative() ? Long.ONE : Long.NEG_ONE; + } else { + var rem = this.subtract(other.multiply(approx)); + var result = approx.add(rem.div(other)); + return result; + } + } + } else if (other.equals(Long.MIN_VALUE)) { + return Long.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate().div(other).negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = Long.ZERO; + var rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = Long.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = Long.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = Long.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + +/** + * Returns this Long modulo the given one. + * + * @param {Long} other Long by which to mod. + * @return {Long} this Long modulo the given one. + * @api public + */ +Long.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); +}; + +/** + * The bitwise-NOT of this value. + * + * @return {Long} the bitwise-NOT of this value. + * @api public + */ +Long.prototype.not = function() { + return Long.fromBits(~this.low_, ~this.high_); +}; + +/** + * Returns the bitwise-AND of this Long and the given one. + * + * @param {Long} other the Long with which to AND. + * @return {Long} the bitwise-AND of this and the other. + * @api public + */ +Long.prototype.and = function(other) { + return Long.fromBits(this.low_ & other.low_, this.high_ & other.high_); +}; + +/** + * Returns the bitwise-OR of this Long and the given one. + * + * @param {Long} other the Long with which to OR. + * @return {Long} the bitwise-OR of this and the other. + * @api public + */ +Long.prototype.or = function(other) { + return Long.fromBits(this.low_ | other.low_, this.high_ | other.high_); +}; + +/** + * Returns the bitwise-XOR of this Long and the given one. + * + * @param {Long} other the Long with which to XOR. + * @return {Long} the bitwise-XOR of this and the other. + * @api public + */ +Long.prototype.xor = function(other) { + return Long.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_); +}; + +/** + * Returns this Long with bits shifted to the left by the given amount. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the left by the given amount. + * @api public + */ +Long.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var low = this.low_; + if (numBits < 32) { + var high = this.high_; + return Long.fromBits( + low << numBits, + (high << numBits) | (low >>> (32 - numBits))); + } else { + return Long.fromBits(0, low << (numBits - 32)); + } + } +}; + +/** + * Returns this Long with bits shifted to the right by the given amount. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the right by the given amount. + * @api public + */ +Long.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Long.fromBits( + (low >>> numBits) | (high << (32 - numBits)), + high >> numBits); + } else { + return Long.fromBits( + high >> (numBits - 32), + high >= 0 ? 0 : -1); + } + } +}; + +/** + * Returns this Long with bits shifted to the right by the given amount, with the new top bits matching the current sign bit. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Long} this shifted to the right by the given amount, with zeros placed into the new leading bits. + * @api public + */ +Long.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Long.fromBits( + (low >>> numBits) | (high << (32 - numBits)), + high >>> numBits); + } else if (numBits == 32) { + return Long.fromBits(high, 0); + } else { + return Long.fromBits(high >>> (numBits - 32), 0); + } + } +}; + +/** + * Returns a Long representing the given (32-bit) integer value. + * + * @param {Number} value the 32-bit integer in question. + * @return {Long} the corresponding Long value. + * @api public + */ +Long.fromInt = function(value) { + if (-128 <= value && value < 128) { + var cachedObj = Long.INT_CACHE_[value]; + if (cachedObj) { + return cachedObj; + } + } + + var obj = new Long(value | 0, value < 0 ? -1 : 0); + if (-128 <= value && value < 128) { + Long.INT_CACHE_[value] = obj; + } + return obj; +}; + +/** + * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * + * @param {Number} value the number in question. + * @return {Long} the corresponding Long value. + * @api public + */ +Long.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return Long.ZERO; + } else if (value <= -Long.TWO_PWR_63_DBL_) { + return Long.MIN_VALUE; + } else if (value + 1 >= Long.TWO_PWR_63_DBL_) { + return Long.MAX_VALUE; + } else if (value < 0) { + return Long.fromNumber(-value).negate(); + } else { + return new Long( + (value % Long.TWO_PWR_32_DBL_) | 0, + (value / Long.TWO_PWR_32_DBL_) | 0); + } +}; + +/** + * Returns a Long representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits. + * + * @param {Number} lowBits the low 32-bits. + * @param {Number} highBits the high 32-bits. + * @return {Long} the corresponding Long value. + * @api public + */ +Long.fromBits = function(lowBits, highBits) { + return new Long(lowBits, highBits); +}; + +/** + * Returns a Long representation of the given string, written using the given radix. + * + * @param {String} str the textual representation of the Long. + * @param {Number} opt_radix the radix in which the text is written. + * @return {Long} the corresponding Long value. + * @api public + */ +Long.fromString = function(str, opt_radix) { + if (str.length == 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) == '-') { + return Long.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character: ' + str); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Long.fromNumber(Math.pow(radix, 8)); + + var result = Long.ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = Long.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(Long.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(Long.fromNumber(value)); + } + } + return result; +}; + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + + +/** + * A cache of the Long representations of small integer values. + * @type {Object} + * @api private + */ +Long.INT_CACHE_ = {}; + +// NOTE: the compiler should inline these constant values below and then remove +// these variables, so there should be no runtime penalty for these. + +/** + * Number used repeated below in calculations. This must appear before the + * first call to any from* function below. + * @type {number} + * @api private + */ +Long.TWO_PWR_16_DBL_ = 1 << 16; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_24_DBL_ = 1 << 24; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_32_DBL_ = Long.TWO_PWR_16_DBL_ * Long.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_31_DBL_ = Long.TWO_PWR_32_DBL_ / 2; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_48_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_64_DBL_ = Long.TWO_PWR_32_DBL_ * Long.TWO_PWR_32_DBL_; + +/** + * @type {number} + * @api private + */ +Long.TWO_PWR_63_DBL_ = Long.TWO_PWR_64_DBL_ / 2; + +/** @type {Long} */ +Long.ZERO = Long.fromInt(0); + +/** @type {Long} */ +Long.ONE = Long.fromInt(1); + +/** @type {Long} */ +Long.NEG_ONE = Long.fromInt(-1); + +/** @type {Long} */ +Long.MAX_VALUE = + Long.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0); + +/** @type {Long} */ +Long.MIN_VALUE = Long.fromBits(0, 0x80000000 | 0); + +/** + * @type {Long} + * @api private + */ +Long.TWO_PWR_24_ = Long.fromInt(1 << 24); + +/** + * Expose. + */ +if(typeof window === 'undefined') { + exports.Long = Long; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/max_key.js b/node_modules/mongodb/node_modules/bson/lib/bson/max_key.js new file mode 100644 index 0000000..29d558f --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/max_key.js @@ -0,0 +1,15 @@ +/** + * A class representation of the BSON MaxKey type. + * + * @class Represents the BSON MaxKey type. + * @return {MaxKey} + */ +function MaxKey() { + if(!(this instanceof MaxKey)) return new MaxKey(); + + this._bsontype = 'MaxKey'; +} + +if(typeof window === 'undefined') { + exports.MaxKey = MaxKey; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/min_key.js b/node_modules/mongodb/node_modules/bson/lib/bson/min_key.js new file mode 100644 index 0000000..489fbaf --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/min_key.js @@ -0,0 +1,15 @@ +/** + * A class representation of the BSON MinKey type. + * + * @class Represents the BSON MinKey type. + * @return {MinKey} + */ +function MinKey() { + if(!(this instanceof MinKey)) return new MinKey(); + + this._bsontype = 'MinKey'; +} + +if(typeof window === 'undefined') { + exports.MinKey = MinKey; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js b/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js new file mode 100644 index 0000000..547e219 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js @@ -0,0 +1,251 @@ +/** + * Module dependencies. + */ +if(typeof window === 'undefined') { + var BinaryParser = require('./binary_parser').BinaryParser; +} + +/** + * Machine id. + * + * Create a random 3-byte value (i.e. unique for this + * process). Other drivers use a md5 of the machine id here, but + * that would mean an asyc call to gethostname, so we don't bother. + */ +var MACHINE_ID = parseInt(Math.random() * 0xFFFFFF, 10); + +// Regular expression that checks for hex value +var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$"); + +/** +* Create a new ObjectID instance +* +* @class Represents the BSON ObjectID type +* @param {String|Number} id Can be a 24 byte hex string, 12 byte binary string or a Number. +* @return {Object} instance of ObjectID. +*/ +function ObjectID(id) { + if(!(this instanceof ObjectID)) return new ObjectID(id); + + this._bsontype = 'ObjectID'; + + var self = this; + // Throw an error if it's not a valid setup + if(id != null && 'number' != typeof id && (id.length != 12 && id.length != 24)) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format"); + // Generate id based on the input + if(id == null || typeof id == 'number') { + this.id = this.generate(id); + } else if(id != null && id.length === 12) { + this.id = id; + } else if(checkForHexRegExp.test(id)) { + return ObjectID.createFromHexString(id); + } else { + this.id = id; + } + + /** + * Returns the generation time in seconds that this ID was generated. + * + * @field generationTime + * @type {Number} + * @getter + * @setter + * @property return number of seconds in the timestamp part of the 12 byte id. + */ + Object.defineProperty(this, "generationTime", { + enumerable: true + , get: function () { + return Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)); + } + , set: function (value) { + var value = BinaryParser.encodeInt(value, 32, true, true); + this.id = value + this.id.substr(4); + } + }); +}; + +/** +* Update the ObjectID index used in generating new ObjectID's on the driver +* +* @return {Number} returns next index value. +* @api private +*/ +ObjectID.prototype.get_inc = function() { + return ObjectID.index = (ObjectID.index + 1) % 0xFFFFFF; +}; + +/** +* Update the ObjectID index used in generating new ObjectID's on the driver +* +* @return {Number} returns next index value. +* @api private +*/ +ObjectID.prototype.getInc = function() { + return this.get_inc(); +}; + +/** +* Generate a 12 byte id string used in ObjectID's +* +* @param {Number} [time] optional parameter allowing to pass in a second based timestamp. +* @return {String} return the 12 byte id binary string. +* @api private +*/ +ObjectID.prototype.generate = function(time) { + if ('number' == typeof time) { + var time4Bytes = BinaryParser.encodeInt(time, 32, true, true); + /* for time-based ObjectID the bytes following the time will be zeroed */ + var machine3Bytes = BinaryParser.encodeInt(MACHINE_ID, 24, false); + var pid2Bytes = BinaryParser.fromShort(typeof process === 'undefined' ? Math.floor(Math.random() * 100000) : process.pid); + var index3Bytes = BinaryParser.encodeInt(this.get_inc(), 24, false, true); + } else { + var unixTime = parseInt(Date.now()/1000,10); + var time4Bytes = BinaryParser.encodeInt(unixTime, 32, true, true); + var machine3Bytes = BinaryParser.encodeInt(MACHINE_ID, 24, false); + var pid2Bytes = BinaryParser.fromShort(typeof process === 'undefined' ? Math.floor(Math.random() * 100000) : process.pid); + var index3Bytes = BinaryParser.encodeInt(this.get_inc(), 24, false, true); + } + + return time4Bytes + machine3Bytes + pid2Bytes + index3Bytes; +}; + +/** +* Return the ObjectID id as a 24 byte hex string representation +* +* @return {String} return the 24 byte hex string representation. +* @api public +*/ +ObjectID.prototype.toHexString = function() { + if(this.__id) return this.__id; + + var hexString = '' + , number + , value; + + for (var index = 0, len = this.id.length; index < len; index++) { + value = BinaryParser.toByte(this.id[index]); + number = value <= 15 + ? '0' + value.toString(16) + : value.toString(16); + hexString = hexString + number; + } + + return this.__id = hexString; +}; + +/** +* Converts the id into a 24 byte hex string for printing +* +* @return {String} return the 24 byte hex string representation. +* @api private +*/ +ObjectID.prototype.toString = function() { + return this.toHexString(); +}; + +/** +* Converts to a string representation of this Id. +* +* @return {String} return the 24 byte hex string representation. +* @api private +*/ +ObjectID.prototype.inspect = ObjectID.prototype.toString; + +/** +* Converts to its JSON representation. +* +* @return {String} return the 24 byte hex string representation. +* @api private +*/ +ObjectID.prototype.toJSON = function() { + return this.toHexString(); +}; + +/** +* Compares the equality of this ObjectID with `otherID`. +* +* @param {Object} otherID ObjectID instance to compare against. +* @return {Bool} the result of comparing two ObjectID's +* @api public +*/ +ObjectID.prototype.equals = function equals (otherID) { + var id = (otherID instanceof ObjectID || otherID.toHexString) + ? otherID.id + : ObjectID.createFromHexString(otherID).id; + + return this.id === id; +} + +/** +* Returns the generation time in seconds that this ID was generated. +* +* @return {Number} return number of seconds in the timestamp part of the 12 byte id. +* @api public +*/ +ObjectID.prototype.getTimestamp = function() { + var timestamp = new Date(); + timestamp.setTime(Math.floor(BinaryParser.decodeInt(this.id.substring(0,4), 32, true, true)) * 1000); + return timestamp; +} + +/** +* @ignore +* @api private +*/ +ObjectID.index = 0; + +ObjectID.createPk = function createPk () { + return new ObjectID(); +}; + +/** +* Creates an ObjectID from a second based number, with the rest of the ObjectID zeroed out. Used for comparisons or sorting the ObjectID. +* +* @param {Number} time an integer number representing a number of seconds. +* @return {ObjectID} return the created ObjectID +* @api public +*/ +ObjectID.createFromTime = function createFromHexString(time) { + var time4Bytes = BinaryParser.encodeInt(time, 32, true, true); + var objectID = new ObjectID(); + objectID.id = BinaryParser.encodeInt(time, 32, true, true) + BinaryParser.encodeInt(0, 64, true, true) + return objectID; +}; + +/** +* Creates an ObjectID from a hex string representation of an ObjectID. +* +* @param {String} hexString create a ObjectID from a passed in 24 byte hexstring. +* @return {ObjectID} return the created ObjectID +* @api public +*/ +ObjectID.createFromHexString = function createFromHexString (hexString) { + // Throw an error if it's not a valid setup + if(typeof hexString === 'undefined' || hexString != null && hexString.length != 24) throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters in hex format"); + + var len = hexString.length; + + if(len > 12*2) { + throw new Error('Id cannot be longer than 12 bytes'); + } + + var result = '' + , string + , number; + + for (var index = 0; index < len; index += 2) { + string = hexString.substr(index, 2); + number = parseInt(string, 16); + result += BinaryParser.fromByte(number); + } + + return new ObjectID(result); +}; + +/** + * Expose. + */ +if(typeof window === 'undefined') { + exports.ObjectID = ObjectID; +} + diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/symbol.js b/node_modules/mongodb/node_modules/bson/lib/bson/symbol.js new file mode 100644 index 0000000..e88d083 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/symbol.js @@ -0,0 +1,50 @@ +/** + * A class representation of the BSON Symbol type. + * + * @class Represents the BSON Symbol type. + * @param {String} value the string representing the symbol. + * @return {Symbol} + */ +function Symbol(value) { + if(!(this instanceof Symbol)) return new Symbol(value); + this._bsontype = 'Symbol'; + this.value = value; +} + +/** + * Access the wrapped string value. + * + * @return {String} returns the wrapped string. + * @api public + */ +Symbol.prototype.valueOf = function() { + return this.value; +}; + +/** + * @ignore + * @api private + */ +Symbol.prototype.toString = function() { + return this.value; +} + +/** + * @ignore + * @api private + */ +Symbol.prototype.inspect = function() { + return this.value; +} + +/** + * @ignore + * @api private + */ +Symbol.prototype.toJSON = function() { + return this.value; +} + +if(typeof window === 'undefined') { + exports.Symbol = Symbol; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/lib/bson/timestamp.js b/node_modules/mongodb/node_modules/bson/lib/bson/timestamp.js new file mode 100644 index 0000000..d570074 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/lib/bson/timestamp.js @@ -0,0 +1,855 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright 2009 Google Inc. All Rights Reserved + +/** + * Defines a Timestamp class for representing a 64-bit two's-complement + * integer value, which faithfully simulates the behavior of a Java "Timestamp". This + * implementation is derived from TimestampLib in GWT. + * + * Constructs a 64-bit two's-complement integer, given its low and high 32-bit + * values as *signed* integers. See the from* functions below for more + * convenient ways of constructing Timestamps. + * + * The internal representation of a Timestamp is the two given signed, 32-bit values. + * We use 32-bit pieces because these are the size of integers on which + * Javascript performs bit-operations. For operations like addition and + * multiplication, we split each number into 16-bit pieces, which can easily be + * multiplied within Javascript's floating-point representation without overflow + * or change in sign. + * + * In the algorithms below, we frequently reduce the negative case to the + * positive case by negating the input(s) and then post-processing the result. + * Note that we must ALWAYS check specially whether those values are MIN_VALUE + * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as + * a positive number, it overflows back into a negative). Not handling this + * case would often result in infinite recursion. + * + * @class Represents the BSON Timestamp type. + * @param {Number} low the low (signed) 32 bits of the Timestamp. + * @param {Number} high the high (signed) 32 bits of the Timestamp. + */ +function Timestamp(low, high) { + if(!(this instanceof Timestamp)) return new Timestamp(low, high); + this._bsontype = 'Timestamp'; + /** + * @type {number} + * @api private + */ + this.low_ = low | 0; // force into 32 signed bits. + + /** + * @type {number} + * @api private + */ + this.high_ = high | 0; // force into 32 signed bits. +}; + +/** + * Return the int value. + * + * @return {Number} the value, assuming it is a 32-bit integer. + * @api public + */ +Timestamp.prototype.toInt = function() { + return this.low_; +}; + +/** + * Return the Number value. + * + * @return {Number} the closest floating-point representation to this value. + * @api public + */ +Timestamp.prototype.toNumber = function() { + return this.high_ * Timestamp.TWO_PWR_32_DBL_ + + this.getLowBitsUnsigned(); +}; + +/** + * Return the JSON value. + * + * @return {String} the JSON representation. + * @api public + */ +Timestamp.prototype.toJSON = function() { + return this.toString(); +} + +/** + * Return the String value. + * + * @param {Number} [opt_radix] the radix in which the text should be written. + * @return {String} the textual representation of this value. + * @api public + */ +Timestamp.prototype.toString = function(opt_radix) { + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (this.isZero()) { + return '0'; + } + + if (this.isNegative()) { + if (this.equals(Timestamp.MIN_VALUE)) { + // We need to change the Timestamp value before it can be negated, so we remove + // the bottom-most digit in this base and then recurse to do the rest. + var radixTimestamp = Timestamp.fromNumber(radix); + var div = this.div(radixTimestamp); + var rem = div.multiply(radixTimestamp).subtract(this); + return div.toString(radix) + rem.toInt().toString(radix); + } else { + return '-' + this.negate().toString(radix); + } + } + + // Do several (6) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Timestamp.fromNumber(Math.pow(radix, 6)); + + var rem = this; + var result = ''; + while (true) { + var remDiv = rem.div(radixToPower); + var intval = rem.subtract(remDiv.multiply(radixToPower)).toInt(); + var digits = intval.toString(radix); + + rem = remDiv; + if (rem.isZero()) { + return digits + result; + } else { + while (digits.length < 6) { + digits = '0' + digits; + } + result = '' + digits + result; + } + } +}; + +/** + * Return the high 32-bits value. + * + * @return {Number} the high 32-bits as a signed value. + * @api public + */ +Timestamp.prototype.getHighBits = function() { + return this.high_; +}; + +/** + * Return the low 32-bits value. + * + * @return {Number} the low 32-bits as a signed value. + * @api public + */ +Timestamp.prototype.getLowBits = function() { + return this.low_; +}; + +/** + * Return the low unsigned 32-bits value. + * + * @return {Number} the low 32-bits as an unsigned value. + * @api public + */ +Timestamp.prototype.getLowBitsUnsigned = function() { + return (this.low_ >= 0) ? + this.low_ : Timestamp.TWO_PWR_32_DBL_ + this.low_; +}; + +/** + * Returns the number of bits needed to represent the absolute value of this Timestamp. + * + * @return {Number} Returns the number of bits needed to represent the absolute value of this Timestamp. + * @api public + */ +Timestamp.prototype.getNumBitsAbs = function() { + if (this.isNegative()) { + if (this.equals(Timestamp.MIN_VALUE)) { + return 64; + } else { + return this.negate().getNumBitsAbs(); + } + } else { + var val = this.high_ != 0 ? this.high_ : this.low_; + for (var bit = 31; bit > 0; bit--) { + if ((val & (1 << bit)) != 0) { + break; + } + } + return this.high_ != 0 ? bit + 33 : bit + 1; + } +}; + +/** + * Return whether this value is zero. + * + * @return {Boolean} whether this value is zero. + * @api public + */ +Timestamp.prototype.isZero = function() { + return this.high_ == 0 && this.low_ == 0; +}; + +/** + * Return whether this value is negative. + * + * @return {Boolean} whether this value is negative. + * @api public + */ +Timestamp.prototype.isNegative = function() { + return this.high_ < 0; +}; + +/** + * Return whether this value is odd. + * + * @return {Boolean} whether this value is odd. + * @api public + */ +Timestamp.prototype.isOdd = function() { + return (this.low_ & 1) == 1; +}; + +/** + * Return whether this Timestamp equals the other + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp equals the other + * @api public + */ +Timestamp.prototype.equals = function(other) { + return (this.high_ == other.high_) && (this.low_ == other.low_); +}; + +/** + * Return whether this Timestamp does not equal the other. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp does not equal the other. + * @api public + */ +Timestamp.prototype.notEquals = function(other) { + return (this.high_ != other.high_) || (this.low_ != other.low_); +}; + +/** + * Return whether this Timestamp is less than the other. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp is less than the other. + * @api public + */ +Timestamp.prototype.lessThan = function(other) { + return this.compare(other) < 0; +}; + +/** + * Return whether this Timestamp is less than or equal to the other. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp is less than or equal to the other. + * @api public + */ +Timestamp.prototype.lessThanOrEqual = function(other) { + return this.compare(other) <= 0; +}; + +/** + * Return whether this Timestamp is greater than the other. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp is greater than the other. + * @api public + */ +Timestamp.prototype.greaterThan = function(other) { + return this.compare(other) > 0; +}; + +/** + * Return whether this Timestamp is greater than or equal to the other. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} whether this Timestamp is greater than or equal to the other. + * @api public + */ +Timestamp.prototype.greaterThanOrEqual = function(other) { + return this.compare(other) >= 0; +}; + +/** + * Compares this Timestamp with the given one. + * + * @param {Timestamp} other Timestamp to compare against. + * @return {Boolean} 0 if they are the same, 1 if the this is greater, and -1 if the given one is greater. + * @api public + */ +Timestamp.prototype.compare = function(other) { + if (this.equals(other)) { + return 0; + } + + var thisNeg = this.isNegative(); + var otherNeg = other.isNegative(); + if (thisNeg && !otherNeg) { + return -1; + } + if (!thisNeg && otherNeg) { + return 1; + } + + // at this point, the signs are the same, so subtraction will not overflow + if (this.subtract(other).isNegative()) { + return -1; + } else { + return 1; + } +}; + +/** + * The negation of this value. + * + * @return {Timestamp} the negation of this value. + * @api public + */ +Timestamp.prototype.negate = function() { + if (this.equals(Timestamp.MIN_VALUE)) { + return Timestamp.MIN_VALUE; + } else { + return this.not().add(Timestamp.ONE); + } +}; + +/** + * Returns the sum of this and the given Timestamp. + * + * @param {Timestamp} other Timestamp to add to this one. + * @return {Timestamp} the sum of this and the given Timestamp. + * @api public + */ +Timestamp.prototype.add = function(other) { + // Divide each number into 4 chunks of 16 bits, and then sum the chunks. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 + b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 + b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 + b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 + b48; + c48 &= 0xFFFF; + return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns the difference of this and the given Timestamp. + * + * @param {Timestamp} other Timestamp to subtract from this. + * @return {Timestamp} the difference of this and the given Timestamp. + * @api public + */ +Timestamp.prototype.subtract = function(other) { + return this.add(other.negate()); +}; + +/** + * Returns the product of this and the given Timestamp. + * + * @param {Timestamp} other Timestamp to multiply with this. + * @return {Timestamp} the product of this and the other. + * @api public + */ +Timestamp.prototype.multiply = function(other) { + if (this.isZero()) { + return Timestamp.ZERO; + } else if (other.isZero()) { + return Timestamp.ZERO; + } + + if (this.equals(Timestamp.MIN_VALUE)) { + return other.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO; + } else if (other.equals(Timestamp.MIN_VALUE)) { + return this.isOdd() ? Timestamp.MIN_VALUE : Timestamp.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().multiply(other.negate()); + } else { + return this.negate().multiply(other).negate(); + } + } else if (other.isNegative()) { + return this.multiply(other.negate()).negate(); + } + + // If both Timestamps are small, use float multiplication + if (this.lessThan(Timestamp.TWO_PWR_24_) && + other.lessThan(Timestamp.TWO_PWR_24_)) { + return Timestamp.fromNumber(this.toNumber() * other.toNumber()); + } + + // Divide each Timestamp into 4 chunks of 16 bits, and then add up 4x4 products. + // We can skip products that would overflow. + + var a48 = this.high_ >>> 16; + var a32 = this.high_ & 0xFFFF; + var a16 = this.low_ >>> 16; + var a00 = this.low_ & 0xFFFF; + + var b48 = other.high_ >>> 16; + var b32 = other.high_ & 0xFFFF; + var b16 = other.low_ >>> 16; + var b00 = other.low_ & 0xFFFF; + + var c48 = 0, c32 = 0, c16 = 0, c00 = 0; + c00 += a00 * b00; + c16 += c00 >>> 16; + c00 &= 0xFFFF; + c16 += a16 * b00; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c16 += a00 * b16; + c32 += c16 >>> 16; + c16 &= 0xFFFF; + c32 += a32 * b00; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a16 * b16; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c32 += a00 * b32; + c48 += c32 >>> 16; + c32 &= 0xFFFF; + c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48; + c48 &= 0xFFFF; + return Timestamp.fromBits((c16 << 16) | c00, (c48 << 16) | c32); +}; + +/** + * Returns this Timestamp divided by the given one. + * + * @param {Timestamp} other Timestamp by which to divide. + * @return {Timestamp} this Timestamp divided by the given one. + * @api public + */ +Timestamp.prototype.div = function(other) { + if (other.isZero()) { + throw Error('division by zero'); + } else if (this.isZero()) { + return Timestamp.ZERO; + } + + if (this.equals(Timestamp.MIN_VALUE)) { + if (other.equals(Timestamp.ONE) || + other.equals(Timestamp.NEG_ONE)) { + return Timestamp.MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE + } else if (other.equals(Timestamp.MIN_VALUE)) { + return Timestamp.ONE; + } else { + // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|. + var halfThis = this.shiftRight(1); + var approx = halfThis.div(other).shiftLeft(1); + if (approx.equals(Timestamp.ZERO)) { + return other.isNegative() ? Timestamp.ONE : Timestamp.NEG_ONE; + } else { + var rem = this.subtract(other.multiply(approx)); + var result = approx.add(rem.div(other)); + return result; + } + } + } else if (other.equals(Timestamp.MIN_VALUE)) { + return Timestamp.ZERO; + } + + if (this.isNegative()) { + if (other.isNegative()) { + return this.negate().div(other.negate()); + } else { + return this.negate().div(other).negate(); + } + } else if (other.isNegative()) { + return this.div(other.negate()).negate(); + } + + // Repeat the following until the remainder is less than other: find a + // floating-point that approximates remainder / other *from below*, add this + // into the result, and subtract it from the remainder. It is critical that + // the approximate value is less than or equal to the real value so that the + // remainder never becomes negative. + var res = Timestamp.ZERO; + var rem = this; + while (rem.greaterThanOrEqual(other)) { + // Approximate the result of division. This may be a little greater or + // smaller than the actual value. + var approx = Math.max(1, Math.floor(rem.toNumber() / other.toNumber())); + + // We will tweak the approximate result by changing it in the 48-th digit or + // the smallest non-fractional digit, whichever is larger. + var log2 = Math.ceil(Math.log(approx) / Math.LN2); + var delta = (log2 <= 48) ? 1 : Math.pow(2, log2 - 48); + + // Decrease the approximation until it is smaller than the remainder. Note + // that if it is too large, the product overflows and is negative. + var approxRes = Timestamp.fromNumber(approx); + var approxRem = approxRes.multiply(other); + while (approxRem.isNegative() || approxRem.greaterThan(rem)) { + approx -= delta; + approxRes = Timestamp.fromNumber(approx); + approxRem = approxRes.multiply(other); + } + + // We know the answer can't be zero... and actually, zero would cause + // infinite recursion since we would make no progress. + if (approxRes.isZero()) { + approxRes = Timestamp.ONE; + } + + res = res.add(approxRes); + rem = rem.subtract(approxRem); + } + return res; +}; + +/** + * Returns this Timestamp modulo the given one. + * + * @param {Timestamp} other Timestamp by which to mod. + * @return {Timestamp} this Timestamp modulo the given one. + * @api public + */ +Timestamp.prototype.modulo = function(other) { + return this.subtract(this.div(other).multiply(other)); +}; + +/** + * The bitwise-NOT of this value. + * + * @return {Timestamp} the bitwise-NOT of this value. + * @api public + */ +Timestamp.prototype.not = function() { + return Timestamp.fromBits(~this.low_, ~this.high_); +}; + +/** + * Returns the bitwise-AND of this Timestamp and the given one. + * + * @param {Timestamp} other the Timestamp with which to AND. + * @return {Timestamp} the bitwise-AND of this and the other. + * @api public + */ +Timestamp.prototype.and = function(other) { + return Timestamp.fromBits(this.low_ & other.low_, this.high_ & other.high_); +}; + +/** + * Returns the bitwise-OR of this Timestamp and the given one. + * + * @param {Timestamp} other the Timestamp with which to OR. + * @return {Timestamp} the bitwise-OR of this and the other. + * @api public + */ +Timestamp.prototype.or = function(other) { + return Timestamp.fromBits(this.low_ | other.low_, this.high_ | other.high_); +}; + +/** + * Returns the bitwise-XOR of this Timestamp and the given one. + * + * @param {Timestamp} other the Timestamp with which to XOR. + * @return {Timestamp} the bitwise-XOR of this and the other. + * @api public + */ +Timestamp.prototype.xor = function(other) { + return Timestamp.fromBits(this.low_ ^ other.low_, this.high_ ^ other.high_); +}; + +/** + * Returns this Timestamp with bits shifted to the left by the given amount. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the left by the given amount. + * @api public + */ +Timestamp.prototype.shiftLeft = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var low = this.low_; + if (numBits < 32) { + var high = this.high_; + return Timestamp.fromBits( + low << numBits, + (high << numBits) | (low >>> (32 - numBits))); + } else { + return Timestamp.fromBits(0, low << (numBits - 32)); + } + } +}; + +/** + * Returns this Timestamp with bits shifted to the right by the given amount. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the right by the given amount. + * @api public + */ +Timestamp.prototype.shiftRight = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Timestamp.fromBits( + (low >>> numBits) | (high << (32 - numBits)), + high >> numBits); + } else { + return Timestamp.fromBits( + high >> (numBits - 32), + high >= 0 ? 0 : -1); + } + } +}; + +/** + * Returns this Timestamp with bits shifted to the right by the given amount, with the new top bits matching the current sign bit. + * + * @param {Number} numBits the number of bits by which to shift. + * @return {Timestamp} this shifted to the right by the given amount, with zeros placed into the new leading bits. + * @api public + */ +Timestamp.prototype.shiftRightUnsigned = function(numBits) { + numBits &= 63; + if (numBits == 0) { + return this; + } else { + var high = this.high_; + if (numBits < 32) { + var low = this.low_; + return Timestamp.fromBits( + (low >>> numBits) | (high << (32 - numBits)), + high >>> numBits); + } else if (numBits == 32) { + return Timestamp.fromBits(high, 0); + } else { + return Timestamp.fromBits(high >>> (numBits - 32), 0); + } + } +}; + +/** + * Returns a Timestamp representing the given (32-bit) integer value. + * + * @param {Number} value the 32-bit integer in question. + * @return {Timestamp} the corresponding Timestamp value. + * @api public + */ +Timestamp.fromInt = function(value) { + if (-128 <= value && value < 128) { + var cachedObj = Timestamp.INT_CACHE_[value]; + if (cachedObj) { + return cachedObj; + } + } + + var obj = new Timestamp(value | 0, value < 0 ? -1 : 0); + if (-128 <= value && value < 128) { + Timestamp.INT_CACHE_[value] = obj; + } + return obj; +}; + +/** + * Returns a Timestamp representing the given value, provided that it is a finite number. Otherwise, zero is returned. + * + * @param {Number} value the number in question. + * @return {Timestamp} the corresponding Timestamp value. + * @api public + */ +Timestamp.fromNumber = function(value) { + if (isNaN(value) || !isFinite(value)) { + return Timestamp.ZERO; + } else if (value <= -Timestamp.TWO_PWR_63_DBL_) { + return Timestamp.MIN_VALUE; + } else if (value + 1 >= Timestamp.TWO_PWR_63_DBL_) { + return Timestamp.MAX_VALUE; + } else if (value < 0) { + return Timestamp.fromNumber(-value).negate(); + } else { + return new Timestamp( + (value % Timestamp.TWO_PWR_32_DBL_) | 0, + (value / Timestamp.TWO_PWR_32_DBL_) | 0); + } +}; + +/** + * Returns a Timestamp representing the 64-bit integer that comes by concatenating the given high and low bits. Each is assumed to use 32 bits. + * + * @param {Number} lowBits the low 32-bits. + * @param {Number} highBits the high 32-bits. + * @return {Timestamp} the corresponding Timestamp value. + * @api public + */ +Timestamp.fromBits = function(lowBits, highBits) { + return new Timestamp(lowBits, highBits); +}; + +/** + * Returns a Timestamp representation of the given string, written using the given radix. + * + * @param {String} str the textual representation of the Timestamp. + * @param {Number} opt_radix the radix in which the text is written. + * @return {Timestamp} the corresponding Timestamp value. + * @api public + */ +Timestamp.fromString = function(str, opt_radix) { + if (str.length == 0) { + throw Error('number format error: empty string'); + } + + var radix = opt_radix || 10; + if (radix < 2 || 36 < radix) { + throw Error('radix out of range: ' + radix); + } + + if (str.charAt(0) == '-') { + return Timestamp.fromString(str.substring(1), radix).negate(); + } else if (str.indexOf('-') >= 0) { + throw Error('number format error: interior "-" character: ' + str); + } + + // Do several (8) digits each time through the loop, so as to + // minimize the calls to the very expensive emulated div. + var radixToPower = Timestamp.fromNumber(Math.pow(radix, 8)); + + var result = Timestamp.ZERO; + for (var i = 0; i < str.length; i += 8) { + var size = Math.min(8, str.length - i); + var value = parseInt(str.substring(i, i + size), radix); + if (size < 8) { + var power = Timestamp.fromNumber(Math.pow(radix, size)); + result = result.multiply(power).add(Timestamp.fromNumber(value)); + } else { + result = result.multiply(radixToPower); + result = result.add(Timestamp.fromNumber(value)); + } + } + return result; +}; + +// NOTE: Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the +// from* methods on which they depend. + + +/** + * A cache of the Timestamp representations of small integer values. + * @type {Object} + * @api private + */ +Timestamp.INT_CACHE_ = {}; + +// NOTE: the compiler should inline these constant values below and then remove +// these variables, so there should be no runtime penalty for these. + +/** + * Number used repeated below in calculations. This must appear before the + * first call to any from* function below. + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_16_DBL_ = 1 << 16; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_24_DBL_ = 1 << 24; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_32_DBL_ = Timestamp.TWO_PWR_16_DBL_ * Timestamp.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_31_DBL_ = Timestamp.TWO_PWR_32_DBL_ / 2; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_48_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_16_DBL_; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_64_DBL_ = Timestamp.TWO_PWR_32_DBL_ * Timestamp.TWO_PWR_32_DBL_; + +/** + * @type {number} + * @api private + */ +Timestamp.TWO_PWR_63_DBL_ = Timestamp.TWO_PWR_64_DBL_ / 2; + +/** @type {Timestamp} */ +Timestamp.ZERO = Timestamp.fromInt(0); + +/** @type {Timestamp} */ +Timestamp.ONE = Timestamp.fromInt(1); + +/** @type {Timestamp} */ +Timestamp.NEG_ONE = Timestamp.fromInt(-1); + +/** @type {Timestamp} */ +Timestamp.MAX_VALUE = + Timestamp.fromBits(0xFFFFFFFF | 0, 0x7FFFFFFF | 0); + +/** @type {Timestamp} */ +Timestamp.MIN_VALUE = Timestamp.fromBits(0, 0x80000000 | 0); + +/** + * @type {Timestamp} + * @api private + */ +Timestamp.TWO_PWR_24_ = Timestamp.fromInt(1 << 24); + +/** + * Expose. + */ +if(typeof window === 'undefined') { + exports.Timestamp = Timestamp; +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/package.json b/node_modules/mongodb/node_modules/bson/package.json new file mode 100755 index 0000000..18f539d --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/package.json @@ -0,0 +1,58 @@ +{ + "name": "bson", + "description": "A bson parser for node.js and the browser", + "keywords": [ + "mongodb", + "bson", + "parser" + ], + "version": "0.0.4", + "author": { + "name": "Christian Amor Kvalheim", + "email": "christkv@gmail.com" + }, + "contributors": [], + "repository": { + "type": "git", + "url": "git@github.com:christkv/bson.git" + }, + "bugs": { + "email": "node-mongodb-native@googlegroups.com", + "url": "https://github.com/christkv/bson/issues" + }, + "devDependencies": { + "nodeunit": "0.7.3", + "gleak": "0.2.3" + }, + "config": { + "native": false + }, + "main": "./lib/bson/index", + "directories": { + "lib": "./lib/bson" + }, + "engines": { + "node": ">=0.4.12" + }, + "scripts": { + "install": "node install.js", + "test": "make test" + }, + "licenses": [ + { + "type": "Apache License, Version 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + } + ], + "_id": "bson@0.0.4", + "dependencies": {}, + "optionalDependencies": {}, + "_engineSupported": true, + "_npmVersion": "1.1.12", + "_nodeVersion": "v0.6.14", + "_defaultsLoaded": true, + "dist": { + "shasum": "e05fa2e643436084acc8ad960ada487d8988ddd5" + }, + "_from": "bson@0.0.4" +} diff --git a/node_modules/mongodb/node_modules/bson/test/browser/bson_test.js b/node_modules/mongodb/node_modules/bson/test/browser/bson_test.js new file mode 100644 index 0000000..c7d9a67 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/browser/bson_test.js @@ -0,0 +1,242 @@ +this.bson_test = { + 'Full document serialization and deserialization': function (test) { + var motherOfAllDocuments = { + 'string': "客家话", + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': new Date(), + 'oid': new ObjectID(), + 'binary': new Binary('hello world'), + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': Long.fromNumber(100), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', new ObjectID(), 'integration_tests_'), + 'minkey': new MinKey(), + 'maxkey': new MaxKey() + } + + // Let's serialize it + var data = BSON.serialize(motherOfAllDocuments, true, true, false); + // Deserialize the object + var object = BSON.deserialize(data); + + // Asserts + test.equal(Utf8.decode(motherOfAllDocuments.string), object.string); + test.deepEqual(motherOfAllDocuments.array, object.array); + test.deepEqual(motherOfAllDocuments.date, object.date); + test.deepEqual(motherOfAllDocuments.oid.toHexString(), object.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.binary.length(), object.binary.length()); + test.ok(assertArrayEqual(motherOfAllDocuments.binary.value(true), object.binary.value(true))); + test.deepEqual(motherOfAllDocuments.int, object.int); + test.deepEqual(motherOfAllDocuments.float, object.float); + test.deepEqual(motherOfAllDocuments.regexp, object.regexp); + test.deepEqual(motherOfAllDocuments.boolean, object.boolean); + test.deepEqual(motherOfAllDocuments.long.toNumber(), object.long); + test.deepEqual(motherOfAllDocuments.where, object.where); + test.deepEqual(motherOfAllDocuments.dbref.oid.toHexString(), object.dbref.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.dbref.namespace, object.dbref.namespace); + test.deepEqual(motherOfAllDocuments.dbref.db, object.dbref.db); + test.deepEqual(motherOfAllDocuments.minkey, object.minkey); + test.deepEqual(motherOfAllDocuments.maxkey, object.maxkey); + test.done(); + }, + + 'exercise all the binary object constructor methods': function (test) { + // Construct using array + var string = 'hello world'; + // String to array + var array = stringToArrayBuffer(string); + + // Binary from array buffer + var binary = new Binary(stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + test.ok(assertArrayEqual(array, binary.buffer)); + + // Construct using number of chars + binary = new Binary(5); + test.ok(5, binary.buffer.length); + + // Construct using an Array + var binary = new Binary(stringToArray(string)); + test.ok(string.length, binary.buffer.length); + test.ok(assertArrayEqual(array, binary.buffer)); + + // Construct using a string + var binary = new Binary(string); + test.ok(string.length, binary.buffer.length); + test.ok(assertArrayEqual(array, binary.buffer)); + test.done(); + }, + + 'exercise the put binary object method for an instance when using Uint8Array': function (test) { + // Construct using array + var string = 'hello world'; + // String to array + var array = stringToArrayBuffer(string + 'a'); + + // Binary from array buffer + var binary = new Binary(stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Write a byte to the array + binary.put('a') + + // Verify that the data was writtencorrectly + test.equal(string.length + 1, binary.position); + test.ok(assertArrayEqual(array, binary.value(true))); + test.equal('hello worlda', binary.value()); + + // Exercise a binary with lots of space in the buffer + var binary = new Binary(); + test.ok(Binary.BUFFER_SIZE, binary.buffer.length); + + // Write a byte to the array + binary.put('a') + + // Verify that the data was writtencorrectly + test.equal(1, binary.position); + test.ok(assertArrayEqual(['a'.charCodeAt(0)], binary.value(true))); + test.equal('a', binary.value()); + test.done(); + }, + + 'exercise the write binary object method for an instance when using Uint8Array': function (test) { + // Construct using array + var string = 'hello world'; + // Array + var writeArrayBuffer = new Uint8Array(new ArrayBuffer(1)); + writeArrayBuffer[0] = 'a'.charCodeAt(0); + var arrayBuffer = ['a'.charCodeAt(0)]; + + // Binary from array buffer + var binary = new Binary(stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Write a string starting at end of buffer + binary.write('a'); + test.equal('hello worlda', binary.value()); + // Write a string starting at index 0 + binary.write('a', 0); + test.equal('aello worlda', binary.value()); + // Write a arraybuffer starting at end of buffer + binary.write(writeArrayBuffer); + test.equal('aello worldaa', binary.value()); + // Write a arraybuffer starting at position 5 + binary.write(writeArrayBuffer, 5); + test.equal('aelloaworldaa', binary.value()); + // Write a array starting at end of buffer + binary.write(arrayBuffer); + test.equal('aelloaworldaaa', binary.value()); + // Write a array starting at position 6 + binary.write(arrayBuffer, 6); + test.equal('aelloaaorldaaa', binary.value()); + test.done(); + }, + + 'exercise the read binary object method for an instance when using Uint8Array': function (test) { + // Construct using array + var string = 'hello world'; + var array = stringToArrayBuffer(string); + + // Binary from array buffer + var binary = new Binary(stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Read the first 2 bytes + var data = binary.read(0, 2); + test.ok(assertArrayEqual(stringToArrayBuffer('he'), data)); + + // Read the entire field + var data = binary.read(0); + test.ok(assertArrayEqual(stringToArrayBuffer(string), data)); + + // Read 3 bytes + var data = binary.read(6, 5); + test.ok(assertArrayEqual(stringToArrayBuffer('world'), data)); + test.done(); + } +}; + +var assertArrayEqual = function(array1, array2) { + if(array1.length != array2.length) return false; + for(var i = 0; i < array1.length; i++) { + if(array1[i] != array2[i]) return false; + } + + return true; +} + +// String to arraybuffer +var stringToArrayBuffer = function(string) { + var dataBuffer = new Uint8Array(new ArrayBuffer(string.length)); + // Return the strings + for(var i = 0; i < string.length; i++) { + dataBuffer[i] = string.charCodeAt(i); + } + // Return the data buffer + return dataBuffer; +} + +// String to arraybuffer +var stringToArray = function(string) { + var dataBuffer = new Array(string.length); + // Return the strings + for(var i = 0; i < string.length; i++) { + dataBuffer[i] = string.charCodeAt(i); + } + // Return the data buffer + return dataBuffer; +} + +var Utf8 = { + // public method for url encoding + encode : function (string) { + string = string.replace(/\r\n/g,"\n"); + var utftext = ""; + + for (var n = 0; n < string.length; n++) { + var c = string.charCodeAt(n); + if (c < 128) { + utftext += String.fromCharCode(c); + } else if((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); + } else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + + } + + return utftext; + }, + + // public method for url decoding + decode : function (utftext) { + var string = ""; + var i = 0; + var c = c1 = c2 = 0; + + while ( i < utftext.length ) { + c = utftext.charCodeAt(i); + if(c < 128) { + string += String.fromCharCode(c); + i++; + } else if((c > 191) && (c < 224)) { + c2 = utftext.charCodeAt(i+1); + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } else { + c2 = utftext.charCodeAt(i+1); + c3 = utftext.charCodeAt(i+2); + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + return string; + } +} diff --git a/node_modules/mongodb/node_modules/bson/test/browser/nodeunit.js b/node_modules/mongodb/node_modules/bson/test/browser/nodeunit.js new file mode 100644 index 0000000..af7fd0b --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/browser/nodeunit.js @@ -0,0 +1,2034 @@ +/*! + * Nodeunit + * https://github.com/caolan/nodeunit + * Copyright (c) 2010 Caolan McMahon + * MIT Licensed + * + * json2.js + * http://www.JSON.org/json2.js + * Public Domain. + * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. + */ +nodeunit = (function(){ +/* + http://www.JSON.org/json2.js + 2010-11-17 + + Public Domain. + + NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. + + See http://www.JSON.org/js.html + + + This code should be minified before deployment. + See http://javascript.crockford.com/jsmin.html + + USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO + NOT CONTROL. + + + This file creates a global JSON object containing two methods: stringify + and parse. + + JSON.stringify(value, replacer, space) + value any JavaScript value, usually an object or array. + + replacer an optional parameter that determines how object + values are stringified for objects. It can be a + function or an array of strings. + + space an optional parameter that specifies the indentation + of nested structures. If it is omitted, the text will + be packed without extra whitespace. If it is a number, + it will specify the number of spaces to indent at each + level. If it is a string (such as '\t' or ' '), + it contains the characters used to indent at each level. + + This method produces a JSON text from a JavaScript value. + + When an object value is found, if the object contains a toJSON + method, its toJSON method will be called and the result will be + stringified. A toJSON method does not serialize: it returns the + value represented by the name/value pair that should be serialized, + or undefined if nothing should be serialized. The toJSON method + will be passed the key associated with the value, and this will be + bound to the value + + For example, this would serialize Dates as ISO strings. + + Date.prototype.toJSON = function (key) { + function f(n) { + // Format integers to have at least two digits. + return n < 10 ? '0' + n : n; + } + + return this.getUTCFullYear() + '-' + + f(this.getUTCMonth() + 1) + '-' + + f(this.getUTCDate()) + 'T' + + f(this.getUTCHours()) + ':' + + f(this.getUTCMinutes()) + ':' + + f(this.getUTCSeconds()) + 'Z'; + }; + + You can provide an optional replacer method. It will be passed the + key and value of each member, with this bound to the containing + object. The value that is returned from your method will be + serialized. If your method returns undefined, then the member will + be excluded from the serialization. + + If the replacer parameter is an array of strings, then it will be + used to select the members to be serialized. It filters the results + such that only members with keys listed in the replacer array are + stringified. + + Values that do not have JSON representations, such as undefined or + functions, will not be serialized. Such values in objects will be + dropped; in arrays they will be replaced with null. You can use + a replacer function to replace those with JSON values. + JSON.stringify(undefined) returns undefined. + + The optional space parameter produces a stringification of the + value that is filled with line breaks and indentation to make it + easier to read. + + If the space parameter is a non-empty string, then that string will + be used for indentation. If the space parameter is a number, then + the indentation will be that many spaces. + + Example: + + text = JSON.stringify(['e', {pluribus: 'unum'}]); + // text is '["e",{"pluribus":"unum"}]' + + + text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t'); + // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' + + text = JSON.stringify([new Date()], function (key, value) { + return this[key] instanceof Date ? + 'Date(' + this[key] + ')' : value; + }); + // text is '["Date(---current time---)"]' + + + JSON.parse(text, reviver) + This method parses a JSON text to produce an object or array. + It can throw a SyntaxError exception. + + The optional reviver parameter is a function that can filter and + transform the results. It receives each of the keys and values, + and its return value is used instead of the original value. + If it returns what it received, then the structure is not modified. + If it returns undefined then the member is deleted. + + Example: + + // Parse the text. Values that look like ISO date strings will + // be converted to Date objects. + + myData = JSON.parse(text, function (key, value) { + var a; + if (typeof value === 'string') { + a = +/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); + if (a) { + return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], + +a[5], +a[6])); + } + } + return value; + }); + + myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { + var d; + if (typeof value === 'string' && + value.slice(0, 5) === 'Date(' && + value.slice(-1) === ')') { + d = new Date(value.slice(5, -1)); + if (d) { + return d; + } + } + return value; + }); + + + This is a reference implementation. You are free to copy, modify, or + redistribute. +*/ + +/*jslint evil: true, strict: false, regexp: false */ + +/*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, + call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, + getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, + lastIndex, length, parse, prototype, push, replace, slice, stringify, + test, toJSON, toString, valueOf +*/ + + +// Create a JSON object only if one does not already exist. We create the +// methods in a closure to avoid creating global variables. + +var JSON = {}; + +(function () { + "use strict"; + + function f(n) { + // Format integers to have at least two digits. + return n < 10 ? '0' + n : n; + } + + if (typeof Date.prototype.toJSON !== 'function') { + + Date.prototype.toJSON = function (key) { + + return isFinite(this.valueOf()) ? + this.getUTCFullYear() + '-' + + f(this.getUTCMonth() + 1) + '-' + + f(this.getUTCDate()) + 'T' + + f(this.getUTCHours()) + ':' + + f(this.getUTCMinutes()) + ':' + + f(this.getUTCSeconds()) + 'Z' : null; + }; + + String.prototype.toJSON = + Number.prototype.toJSON = + Boolean.prototype.toJSON = function (key) { + return this.valueOf(); + }; + } + + var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, + escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, + gap, + indent, + meta = { // table of character substitutions + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '"' : '\\"', + '\\': '\\\\' + }, + rep; + + + function quote(string) { + +// If the string contains no control characters, no quote characters, and no +// backslash characters, then we can safely slap some quotes around it. +// Otherwise we must also replace the offending characters with safe escape +// sequences. + + escapable.lastIndex = 0; + return escapable.test(string) ? + '"' + string.replace(escapable, function (a) { + var c = meta[a]; + return typeof c === 'string' ? c : + '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + }) + '"' : + '"' + string + '"'; + } + + + function str(key, holder) { + +// Produce a string from holder[key]. + + var i, // The loop counter. + k, // The member key. + v, // The member value. + length, + mind = gap, + partial, + value = holder[key]; + +// If the value has a toJSON method, call it to obtain a replacement value. + + if (value && typeof value === 'object' && + typeof value.toJSON === 'function') { + value = value.toJSON(key); + } + +// If we were called with a replacer function, then call the replacer to +// obtain a replacement value. + + if (typeof rep === 'function') { + value = rep.call(holder, key, value); + } + +// What happens next depends on the value's type. + + switch (typeof value) { + case 'string': + return quote(value); + + case 'number': + +// JSON numbers must be finite. Encode non-finite numbers as null. + + return isFinite(value) ? String(value) : 'null'; + + case 'boolean': + case 'null': + +// If the value is a boolean or null, convert it to a string. Note: +// typeof null does not produce 'null'. The case is included here in +// the remote chance that this gets fixed someday. + + return String(value); + +// If the type is 'object', we might be dealing with an object or an array or +// null. + + case 'object': + +// Due to a specification blunder in ECMAScript, typeof null is 'object', +// so watch out for that case. + + if (!value) { + return 'null'; + } + +// Make an array to hold the partial results of stringifying this object value. + + gap += indent; + partial = []; + +// Is the value an array? + + if (Object.prototype.toString.apply(value) === '[object Array]') { + +// The value is an array. Stringify every element. Use null as a placeholder +// for non-JSON values. + + length = value.length; + for (i = 0; i < length; i += 1) { + partial[i] = str(i, value) || 'null'; + } + +// Join all of the elements together, separated with commas, and wrap them in +// brackets. + + v = partial.length === 0 ? '[]' : + gap ? '[\n' + gap + + partial.join(',\n' + gap) + '\n' + + mind + ']' : + '[' + partial.join(',') + ']'; + gap = mind; + return v; + } + +// If the replacer is an array, use it to select the members to be stringified. + + if (rep && typeof rep === 'object') { + length = rep.length; + for (i = 0; i < length; i += 1) { + k = rep[i]; + if (typeof k === 'string') { + v = str(k, value); + if (v) { + partial.push(quote(k) + (gap ? ': ' : ':') + v); + } + } + } + } else { + +// Otherwise, iterate through all of the keys in the object. + + for (k in value) { + if (Object.hasOwnProperty.call(value, k)) { + v = str(k, value); + if (v) { + partial.push(quote(k) + (gap ? ': ' : ':') + v); + } + } + } + } + +// Join all of the member texts together, separated with commas, +// and wrap them in braces. + + v = partial.length === 0 ? '{}' : + gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' + + mind + '}' : '{' + partial.join(',') + '}'; + gap = mind; + return v; + } + } + +// If the JSON object does not yet have a stringify method, give it one. + + if (typeof JSON.stringify !== 'function') { + JSON.stringify = function (value, replacer, space) { + +// The stringify method takes a value and an optional replacer, and an optional +// space parameter, and returns a JSON text. The replacer can be a function +// that can replace values, or an array of strings that will select the keys. +// A default replacer method can be provided. Use of the space parameter can +// produce text that is more easily readable. + + var i; + gap = ''; + indent = ''; + +// If the space parameter is a number, make an indent string containing that +// many spaces. + + if (typeof space === 'number') { + for (i = 0; i < space; i += 1) { + indent += ' '; + } + +// If the space parameter is a string, it will be used as the indent string. + + } else if (typeof space === 'string') { + indent = space; + } + +// If there is a replacer, it must be a function or an array. +// Otherwise, throw an error. + + rep = replacer; + if (replacer && typeof replacer !== 'function' && + (typeof replacer !== 'object' || + typeof replacer.length !== 'number')) { + throw new Error('JSON.stringify'); + } + +// Make a fake root object containing our value under the key of ''. +// Return the result of stringifying the value. + + return str('', {'': value}); + }; + } + + +// If the JSON object does not yet have a parse method, give it one. + + if (typeof JSON.parse !== 'function') { + JSON.parse = function (text, reviver) { + +// The parse method takes a text and an optional reviver function, and returns +// a JavaScript value if the text is a valid JSON text. + + var j; + + function walk(holder, key) { + +// The walk method is used to recursively walk the resulting structure so +// that modifications can be made. + + var k, v, value = holder[key]; + if (value && typeof value === 'object') { + for (k in value) { + if (Object.hasOwnProperty.call(value, k)) { + v = walk(value, k); + if (v !== undefined) { + value[k] = v; + } else { + delete value[k]; + } + } + } + } + return reviver.call(holder, key, value); + } + + +// Parsing happens in four stages. In the first stage, we replace certain +// Unicode characters with escape sequences. JavaScript handles many characters +// incorrectly, either silently deleting them, or treating them as line endings. + + text = String(text); + cx.lastIndex = 0; + if (cx.test(text)) { + text = text.replace(cx, function (a) { + return '\\u' + + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + }); + } + +// In the second stage, we run the text against regular expressions that look +// for non-JSON patterns. We are especially concerned with '()' and 'new' +// because they can cause invocation, and '=' because it can cause mutation. +// But just to be safe, we want to reject all unexpected forms. + +// We split the second stage into 4 regexp operations in order to work around +// crippling inefficiencies in IE's and Safari's regexp engines. First we +// replace the JSON backslash pairs with '@' (a non-JSON character). Second, we +// replace all simple value tokens with ']' characters. Third, we delete all +// open brackets that follow a colon or comma or that begin the text. Finally, +// we look to see that the remaining characters are only whitespace or ']' or +// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval. + + if (/^[\],:{}\s]*$/ +.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@') +.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']') +.replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { + +// In the third stage we use the eval function to compile the text into a +// JavaScript structure. The '{' operator is subject to a syntactic ambiguity +// in JavaScript: it can begin a block or an object literal. We wrap the text +// in parens to eliminate the ambiguity. + + j = eval('(' + text + ')'); + +// In the optional fourth stage, we recursively walk the new structure, passing +// each name/value pair to a reviver function for possible transformation. + + return typeof reviver === 'function' ? + walk({'': j}, '') : j; + } + +// If the text is not JSON parseable, then a SyntaxError is thrown. + + throw new SyntaxError('JSON.parse'); + }; + } +}()); +var assert = this.assert = {}; +var types = {}; +var core = {}; +var nodeunit = {}; +var reporter = {}; +/*global setTimeout: false, console: false */ +(function () { + + var async = {}; + + // global on the server, window in the browser + var root = this, + previous_async = root.async; + + if (typeof module !== 'undefined' && module.exports) { + module.exports = async; + } + else { + root.async = async; + } + + async.noConflict = function () { + root.async = previous_async; + return async; + }; + + //// cross-browser compatiblity functions //// + + var _forEach = function (arr, iterator) { + if (arr.forEach) { + return arr.forEach(iterator); + } + for (var i = 0; i < arr.length; i += 1) { + iterator(arr[i], i, arr); + } + }; + + var _map = function (arr, iterator) { + if (arr.map) { + return arr.map(iterator); + } + var results = []; + _forEach(arr, function (x, i, a) { + results.push(iterator(x, i, a)); + }); + return results; + }; + + var _reduce = function (arr, iterator, memo) { + if (arr.reduce) { + return arr.reduce(iterator, memo); + } + _forEach(arr, function (x, i, a) { + memo = iterator(memo, x, i, a); + }); + return memo; + }; + + var _keys = function (obj) { + if (Object.keys) { + return Object.keys(obj); + } + var keys = []; + for (var k in obj) { + if (obj.hasOwnProperty(k)) { + keys.push(k); + } + } + return keys; + }; + + var _indexOf = function (arr, item) { + if (arr.indexOf) { + return arr.indexOf(item); + } + for (var i = 0; i < arr.length; i += 1) { + if (arr[i] === item) { + return i; + } + } + return -1; + }; + + //// exported async module functions //// + + //// nextTick implementation with browser-compatible fallback //// + if (typeof process === 'undefined' || !(process.nextTick)) { + async.nextTick = function (fn) { + setTimeout(fn, 0); + }; + } + else { + async.nextTick = process.nextTick; + } + + async.forEach = function (arr, iterator, callback) { + if (!arr.length) { + return callback(); + } + var completed = 0; + _forEach(arr, function (x) { + iterator(x, function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + if (completed === arr.length) { + callback(); + } + } + }); + }); + }; + + async.forEachSeries = function (arr, iterator, callback) { + if (!arr.length) { + return callback(); + } + var completed = 0; + var iterate = function () { + iterator(arr[completed], function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + if (completed === arr.length) { + callback(); + } + else { + iterate(); + } + } + }); + }; + iterate(); + }; + + + var doParallel = function (fn) { + return function () { + var args = Array.prototype.slice.call(arguments); + return fn.apply(null, [async.forEach].concat(args)); + }; + }; + var doSeries = function (fn) { + return function () { + var args = Array.prototype.slice.call(arguments); + return fn.apply(null, [async.forEachSeries].concat(args)); + }; + }; + + + var _asyncMap = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (err, v) { + results[x.index] = v; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + }; + async.map = doParallel(_asyncMap); + async.mapSeries = doSeries(_asyncMap); + + + // reduce only has a series version, as doing reduce in parallel won't + // work in many situations. + async.reduce = function (arr, memo, iterator, callback) { + async.forEachSeries(arr, function (x, callback) { + iterator(memo, x, function (err, v) { + memo = v; + callback(err); + }); + }, function (err) { + callback(err, memo); + }); + }; + // inject alias + async.inject = async.reduce; + // foldl alias + async.foldl = async.reduce; + + async.reduceRight = function (arr, memo, iterator, callback) { + var reversed = _map(arr, function (x) { + return x; + }).reverse(); + async.reduce(reversed, memo, iterator, callback); + }; + // foldr alias + async.foldr = async.reduceRight; + + var _filter = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (v) { + if (v) { + results.push(x); + } + callback(); + }); + }, function (err) { + callback(_map(results.sort(function (a, b) { + return a.index - b.index; + }), function (x) { + return x.value; + })); + }); + }; + async.filter = doParallel(_filter); + async.filterSeries = doSeries(_filter); + // select alias + async.select = async.filter; + async.selectSeries = async.filterSeries; + + var _reject = function (eachfn, arr, iterator, callback) { + var results = []; + arr = _map(arr, function (x, i) { + return {index: i, value: x}; + }); + eachfn(arr, function (x, callback) { + iterator(x.value, function (v) { + if (!v) { + results.push(x); + } + callback(); + }); + }, function (err) { + callback(_map(results.sort(function (a, b) { + return a.index - b.index; + }), function (x) { + return x.value; + })); + }); + }; + async.reject = doParallel(_reject); + async.rejectSeries = doSeries(_reject); + + var _detect = function (eachfn, arr, iterator, main_callback) { + eachfn(arr, function (x, callback) { + iterator(x, function (result) { + if (result) { + main_callback(x); + } + else { + callback(); + } + }); + }, function (err) { + main_callback(); + }); + }; + async.detect = doParallel(_detect); + async.detectSeries = doSeries(_detect); + + async.some = function (arr, iterator, main_callback) { + async.forEach(arr, function (x, callback) { + iterator(x, function (v) { + if (v) { + main_callback(true); + main_callback = function () {}; + } + callback(); + }); + }, function (err) { + main_callback(false); + }); + }; + // any alias + async.any = async.some; + + async.every = function (arr, iterator, main_callback) { + async.forEach(arr, function (x, callback) { + iterator(x, function (v) { + if (!v) { + main_callback(false); + main_callback = function () {}; + } + callback(); + }); + }, function (err) { + main_callback(true); + }); + }; + // all alias + async.all = async.every; + + async.sortBy = function (arr, iterator, callback) { + async.map(arr, function (x, callback) { + iterator(x, function (err, criteria) { + if (err) { + callback(err); + } + else { + callback(null, {value: x, criteria: criteria}); + } + }); + }, function (err, results) { + if (err) { + return callback(err); + } + else { + var fn = function (left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }; + callback(null, _map(results.sort(fn), function (x) { + return x.value; + })); + } + }); + }; + + async.auto = function (tasks, callback) { + callback = callback || function () {}; + var keys = _keys(tasks); + if (!keys.length) { + return callback(null); + } + + var completed = []; + + var listeners = []; + var addListener = function (fn) { + listeners.unshift(fn); + }; + var removeListener = function (fn) { + for (var i = 0; i < listeners.length; i += 1) { + if (listeners[i] === fn) { + listeners.splice(i, 1); + return; + } + } + }; + var taskComplete = function () { + _forEach(listeners, function (fn) { + fn(); + }); + }; + + addListener(function () { + if (completed.length === keys.length) { + callback(null); + } + }); + + _forEach(keys, function (k) { + var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k]; + var taskCallback = function (err) { + if (err) { + callback(err); + // stop subsequent errors hitting callback multiple times + callback = function () {}; + } + else { + completed.push(k); + taskComplete(); + } + }; + var requires = task.slice(0, Math.abs(task.length - 1)) || []; + var ready = function () { + return _reduce(requires, function (a, x) { + return (a && _indexOf(completed, x) !== -1); + }, true); + }; + if (ready()) { + task[task.length - 1](taskCallback); + } + else { + var listener = function () { + if (ready()) { + removeListener(listener); + task[task.length - 1](taskCallback); + } + }; + addListener(listener); + } + }); + }; + + async.waterfall = function (tasks, callback) { + if (!tasks.length) { + return callback(); + } + callback = callback || function () {}; + var wrapIterator = function (iterator) { + return function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + var args = Array.prototype.slice.call(arguments, 1); + var next = iterator.next(); + if (next) { + args.push(wrapIterator(next)); + } + else { + args.push(callback); + } + async.nextTick(function () { + iterator.apply(null, args); + }); + } + }; + }; + wrapIterator(async.iterator(tasks))(); + }; + + async.parallel = function (tasks, callback) { + callback = callback || function () {}; + if (tasks.constructor === Array) { + async.map(tasks, function (fn, callback) { + if (fn) { + fn(function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + callback.call(null, err, args || null); + }); + } + }, callback); + } + else { + var results = {}; + async.forEach(_keys(tasks), function (k, callback) { + tasks[k](function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + results[k] = args; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + } + }; + + async.series = function (tasks, callback) { + callback = callback || function () {}; + if (tasks.constructor === Array) { + async.mapSeries(tasks, function (fn, callback) { + if (fn) { + fn(function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + callback.call(null, err, args || null); + }); + } + }, callback); + } + else { + var results = {}; + async.forEachSeries(_keys(tasks), function (k, callback) { + tasks[k](function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length <= 1) { + args = args[0]; + } + results[k] = args; + callback(err); + }); + }, function (err) { + callback(err, results); + }); + } + }; + + async.iterator = function (tasks) { + var makeCallback = function (index) { + var fn = function () { + if (tasks.length) { + tasks[index].apply(null, arguments); + } + return fn.next(); + }; + fn.next = function () { + return (index < tasks.length - 1) ? makeCallback(index + 1): null; + }; + return fn; + }; + return makeCallback(0); + }; + + async.apply = function (fn) { + var args = Array.prototype.slice.call(arguments, 1); + return function () { + return fn.apply( + null, args.concat(Array.prototype.slice.call(arguments)) + ); + }; + }; + + var _concat = function (eachfn, arr, fn, callback) { + var r = []; + eachfn(arr, function (x, cb) { + fn(x, function (err, y) { + r = r.concat(y || []); + cb(err); + }); + }, function (err) { + callback(err, r); + }); + }; + async.concat = doParallel(_concat); + async.concatSeries = doSeries(_concat); + + async.whilst = function (test, iterator, callback) { + if (test()) { + iterator(function (err) { + if (err) { + return callback(err); + } + async.whilst(test, iterator, callback); + }); + } + else { + callback(); + } + }; + + async.until = function (test, iterator, callback) { + if (!test()) { + iterator(function (err) { + if (err) { + return callback(err); + } + async.until(test, iterator, callback); + }); + } + else { + callback(); + } + }; + + async.queue = function (worker, concurrency) { + var workers = 0; + var tasks = []; + var q = { + concurrency: concurrency, + push: function (data, callback) { + tasks.push({data: data, callback: callback}); + async.nextTick(q.process); + }, + process: function () { + if (workers < q.concurrency && tasks.length) { + var task = tasks.splice(0, 1)[0]; + workers += 1; + worker(task.data, function () { + workers -= 1; + if (task.callback) { + task.callback.apply(task, arguments); + } + q.process(); + }); + } + }, + length: function () { + return tasks.length; + } + }; + return q; + }; + + var _console_fn = function (name) { + return function (fn) { + var args = Array.prototype.slice.call(arguments, 1); + fn.apply(null, args.concat([function (err) { + var args = Array.prototype.slice.call(arguments, 1); + if (typeof console !== 'undefined') { + if (err) { + if (console.error) { + console.error(err); + } + } + else if (console[name]) { + _forEach(args, function (x) { + console[name](x); + }); + } + } + }])); + }; + }; + async.log = _console_fn('log'); + async.dir = _console_fn('dir'); + /*async.info = _console_fn('info'); + async.warn = _console_fn('warn'); + async.error = _console_fn('error');*/ + + async.memoize = function (fn, hasher) { + var memo = {}; + hasher = hasher || function (x) { + return x; + }; + return function () { + var args = Array.prototype.slice.call(arguments); + var callback = args.pop(); + var key = hasher.apply(null, args); + if (key in memo) { + callback.apply(null, memo[key]); + } + else { + fn.apply(null, args.concat([function () { + memo[key] = arguments; + callback.apply(null, arguments); + }])); + } + }; + }; + +}()); +(function(exports){ +/** + * This file is based on the node.js assert module, but with some small + * changes for browser-compatibility + * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! + */ + + +/** + * Added for browser compatibility + */ + +var _keys = function(obj){ + if(Object.keys) return Object.keys(obj); + if (typeof obj != 'object' && typeof obj != 'function') { + throw new TypeError('-'); + } + var keys = []; + for(var k in obj){ + if(obj.hasOwnProperty(k)) keys.push(k); + } + return keys; +}; + + + +// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 +// +// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! +// +// Originally from narwhal.js (http://narwhaljs.org) +// Copyright (c) 2009 Thomas Robinson <280north.com> +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the 'Software'), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +var pSlice = Array.prototype.slice; + +// 1. The assert module provides functions that throw +// AssertionError's when particular conditions are not met. The +// assert module must conform to the following interface. + +var assert = exports; + +// 2. The AssertionError is defined in assert. +// new assert.AssertionError({message: message, actual: actual, expected: expected}) + +assert.AssertionError = function AssertionError (options) { + this.name = "AssertionError"; + this.message = options.message; + this.actual = options.actual; + this.expected = options.expected; + this.operator = options.operator; + var stackStartFunction = options.stackStartFunction || fail; + + if (Error.captureStackTrace) { + Error.captureStackTrace(this, stackStartFunction); + } +}; +// code from util.inherits in node +assert.AssertionError.super_ = Error; + + +// EDITED FOR BROWSER COMPATIBILITY: replaced Object.create call +// TODO: test what effect this may have +var ctor = function () { this.constructor = assert.AssertionError; }; +ctor.prototype = Error.prototype; +assert.AssertionError.prototype = new ctor(); + + +assert.AssertionError.prototype.toString = function() { + if (this.message) { + return [this.name+":", this.message].join(' '); + } else { + return [ this.name+":" + , JSON.stringify(this.expected ) + , this.operator + , JSON.stringify(this.actual) + ].join(" "); + } +}; + +// assert.AssertionError instanceof Error + +assert.AssertionError.__proto__ = Error.prototype; + +// At present only the three keys mentioned above are used and +// understood by the spec. Implementations or sub modules can pass +// other keys to the AssertionError's constructor - they will be +// ignored. + +// 3. All of the following functions must throw an AssertionError +// when a corresponding condition is not met, with a message that +// may be undefined if not provided. All assertion methods provide +// both the actual and expected values to the assertion error for +// display purposes. + +function fail(actual, expected, message, operator, stackStartFunction) { + throw new assert.AssertionError({ + message: message, + actual: actual, + expected: expected, + operator: operator, + stackStartFunction: stackStartFunction + }); +} + +// EXTENSION! allows for well behaved errors defined elsewhere. +assert.fail = fail; + +// 4. Pure assertion tests whether a value is truthy, as determined +// by !!guard. +// assert.ok(guard, message_opt); +// This statement is equivalent to assert.equal(true, guard, +// message_opt);. To test strictly for the value true, use +// assert.strictEqual(true, guard, message_opt);. + +assert.ok = function ok(value, message) { + if (!!!value) fail(value, true, message, "==", assert.ok); +}; + +// 5. The equality assertion tests shallow, coercive equality with +// ==. +// assert.equal(actual, expected, message_opt); + +assert.equal = function equal(actual, expected, message) { + if (actual != expected) fail(actual, expected, message, "==", assert.equal); +}; + +// 6. The non-equality assertion tests for whether two objects are not equal +// with != assert.notEqual(actual, expected, message_opt); + +assert.notEqual = function notEqual(actual, expected, message) { + if (actual == expected) { + fail(actual, expected, message, "!=", assert.notEqual); + } +}; + +// 7. The equivalence assertion tests a deep equality relation. +// assert.deepEqual(actual, expected, message_opt); + +assert.deepEqual = function deepEqual(actual, expected, message) { + if (!_deepEqual(actual, expected)) { + fail(actual, expected, message, "deepEqual", assert.deepEqual); + } +}; + +function _deepEqual(actual, expected) { + // 7.1. All identical values are equivalent, as determined by ===. + if (actual === expected) { + return true; + // 7.2. If the expected value is a Date object, the actual value is + // equivalent if it is also a Date object that refers to the same time. + } else if (actual instanceof Date && expected instanceof Date) { + return actual.getTime() === expected.getTime(); + + // 7.3. Other pairs that do not both pass typeof value == "object", + // equivalence is determined by ==. + } else if (typeof actual != 'object' && typeof expected != 'object') { + return actual == expected; + + // 7.4. For all other Object pairs, including Array objects, equivalence is + // determined by having the same number of owned properties (as verified + // with Object.prototype.hasOwnProperty.call), the same set of keys + // (although not necessarily the same order), equivalent values for every + // corresponding key, and an identical "prototype" property. Note: this + // accounts for both named and indexed properties on Arrays. + } else { + return objEquiv(actual, expected); + } +} + +function isUndefinedOrNull (value) { + return value === null || value === undefined; +} + +function isArguments (object) { + return Object.prototype.toString.call(object) == '[object Arguments]'; +} + +function objEquiv (a, b) { + if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) + return false; + // an identical "prototype" property. + if (a.prototype !== b.prototype) return false; + //~~~I've managed to break Object.keys through screwy arguments passing. + // Converting to array solves the problem. + if (isArguments(a)) { + if (!isArguments(b)) { + return false; + } + a = pSlice.call(a); + b = pSlice.call(b); + return _deepEqual(a, b); + } + try{ + var ka = _keys(a), + kb = _keys(b), + key, i; + } catch (e) {//happens when one is a string literal and the other isn't + return false; + } + // having the same number of owned properties (keys incorporates hasOwnProperty) + if (ka.length != kb.length) + return false; + //the same set of keys (although not necessarily the same order), + ka.sort(); + kb.sort(); + //~~~cheap key test + for (i = ka.length - 1; i >= 0; i--) { + if (ka[i] != kb[i]) + return false; + } + //equivalent values for every corresponding key, and + //~~~possibly expensive deep test + for (i = ka.length - 1; i >= 0; i--) { + key = ka[i]; + if (!_deepEqual(a[key], b[key] )) + return false; + } + return true; +} + +// 8. The non-equivalence assertion tests for any deep inequality. +// assert.notDeepEqual(actual, expected, message_opt); + +assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (_deepEqual(actual, expected)) { + fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual); + } +}; + +// 9. The strict equality assertion tests strict equality, as determined by ===. +// assert.strictEqual(actual, expected, message_opt); + +assert.strictEqual = function strictEqual(actual, expected, message) { + if (actual !== expected) { + fail(actual, expected, message, "===", assert.strictEqual); + } +}; + +// 10. The strict non-equality assertion tests for strict inequality, as determined by !==. +// assert.notStrictEqual(actual, expected, message_opt); + +assert.notStrictEqual = function notStrictEqual(actual, expected, message) { + if (actual === expected) { + fail(actual, expected, message, "!==", assert.notStrictEqual); + } +}; + +function _throws (shouldThrow, block, err, message) { + var exception = null, + threw = false, + typematters = true; + + message = message || ""; + + //handle optional arguments + if (arguments.length == 3) { + if (typeof(err) == "string") { + message = err; + typematters = false; + } + } else if (arguments.length == 2) { + typematters = false; + } + + try { + block(); + } catch (e) { + threw = true; + exception = e; + } + + if (shouldThrow && !threw) { + fail( "Missing expected exception" + + (err && err.name ? " ("+err.name+")." : '.') + + (message ? " " + message : "") + ); + } + if (!shouldThrow && threw && typematters && exception instanceof err) { + fail( "Got unwanted exception" + + (err && err.name ? " ("+err.name+")." : '.') + + (message ? " " + message : "") + ); + } + if ((shouldThrow && threw && typematters && !(exception instanceof err)) || + (!shouldThrow && threw)) { + throw exception; + } +}; + +// 11. Expected to throw an error: +// assert.throws(block, Error_opt, message_opt); + +assert.throws = function(block, /*optional*/error, /*optional*/message) { + _throws.apply(this, [true].concat(pSlice.call(arguments))); +}; + +// EXTENSION! This is annoying to write outside this module. +assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { + _throws.apply(this, [false].concat(pSlice.call(arguments))); +}; + +assert.ifError = function (err) { if (err) {throw err;}}; +})(assert); +(function(exports){ +/*! + * Nodeunit + * Copyright (c) 2010 Caolan McMahon + * MIT Licensed + * + * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! + * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build. + * Only code on that line will be removed, its mostly to avoid requiring code + * that is node specific + */ + +/** + * Module dependencies + */ + +//var assert = require('./assert'), //@REMOVE_LINE_FOR_BROWSER +// async = require('../deps/async'); //@REMOVE_LINE_FOR_BROWSER + + +/** + * Creates assertion objects representing the result of an assert call. + * Accepts an object or AssertionError as its argument. + * + * @param {object} obj + * @api public + */ + +exports.assertion = function (obj) { + return { + method: obj.method || '', + message: obj.message || (obj.error && obj.error.message) || '', + error: obj.error, + passed: function () { + return !this.error; + }, + failed: function () { + return Boolean(this.error); + } + }; +}; + +/** + * Creates an assertion list object representing a group of assertions. + * Accepts an array of assertion objects. + * + * @param {Array} arr + * @param {Number} duration + * @api public + */ + +exports.assertionList = function (arr, duration) { + var that = arr || []; + that.failures = function () { + var failures = 0; + for (var i = 0; i < this.length; i += 1) { + if (this[i].failed()) { + failures += 1; + } + } + return failures; + }; + that.passes = function () { + return that.length - that.failures(); + }; + that.duration = duration || 0; + return that; +}; + +/** + * Create a wrapper function for assert module methods. Executes a callback + * after the it's complete with an assertion object representing the result. + * + * @param {Function} callback + * @api private + */ + +var assertWrapper = function (callback) { + return function (new_method, assert_method, arity) { + return function () { + var message = arguments[arity - 1]; + var a = exports.assertion({method: new_method, message: message}); + try { + assert[assert_method].apply(null, arguments); + } + catch (e) { + a.error = e; + } + callback(a); + }; + }; +}; + +/** + * Creates the 'test' object that gets passed to every test function. + * Accepts the name of the test function as its first argument, followed by + * the start time in ms, the options object and a callback function. + * + * @param {String} name + * @param {Number} start + * @param {Object} options + * @param {Function} callback + * @api public + */ + +exports.test = function (name, start, options, callback) { + var expecting; + var a_list = []; + + var wrapAssert = assertWrapper(function (a) { + a_list.push(a); + if (options.log) { + async.nextTick(function () { + options.log(a); + }); + } + }); + + var test = { + done: function (err) { + if (expecting !== undefined && expecting !== a_list.length) { + var e = new Error( + 'Expected ' + expecting + ' assertions, ' + + a_list.length + ' ran' + ); + var a1 = exports.assertion({method: 'expect', error: e}); + a_list.push(a1); + if (options.log) { + async.nextTick(function () { + options.log(a1); + }); + } + } + if (err) { + var a2 = exports.assertion({error: err}); + a_list.push(a2); + if (options.log) { + async.nextTick(function () { + options.log(a2); + }); + } + } + var end = new Date().getTime(); + async.nextTick(function () { + var assertion_list = exports.assertionList(a_list, end - start); + options.testDone(name, assertion_list); + callback(null, a_list); + }); + }, + ok: wrapAssert('ok', 'ok', 2), + same: wrapAssert('same', 'deepEqual', 3), + equals: wrapAssert('equals', 'equal', 3), + expect: function (num) { + expecting = num; + }, + _assertion_list: a_list + }; + // add all functions from the assert module + for (var k in assert) { + if (assert.hasOwnProperty(k)) { + test[k] = wrapAssert(k, k, assert[k].length); + } + } + return test; +}; + +/** + * Ensures an options object has all callbacks, adding empty callback functions + * if any are missing. + * + * @param {Object} opt + * @return {Object} + * @api public + */ + +exports.options = function (opt) { + var optionalCallback = function (name) { + opt[name] = opt[name] || function () {}; + }; + + optionalCallback('moduleStart'); + optionalCallback('moduleDone'); + optionalCallback('testStart'); + optionalCallback('testDone'); + //optionalCallback('log'); + + // 'done' callback is not optional. + + return opt; +}; +})(types); +(function(exports){ +/*! + * Nodeunit + * Copyright (c) 2010 Caolan McMahon + * MIT Licensed + * + * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! + * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build. + * Only code on that line will be removed, its mostly to avoid requiring code + * that is node specific + */ + +/** + * Module dependencies + */ + +//var async = require('../deps/async'), //@REMOVE_LINE_FOR_BROWSER +// types = require('./types'); //@REMOVE_LINE_FOR_BROWSER + + +/** + * Added for browser compatibility + */ + +var _keys = function (obj) { + if (Object.keys) { + return Object.keys(obj); + } + var keys = []; + for (var k in obj) { + if (obj.hasOwnProperty(k)) { + keys.push(k); + } + } + return keys; +}; + + +var _copy = function (obj) { + var nobj = {}; + var keys = _keys(obj); + for (var i = 0; i < keys.length; i += 1) { + nobj[keys[i]] = obj[keys[i]]; + } + return nobj; +}; + + +/** + * Runs a test function (fn) from a loaded module. After the test function + * calls test.done(), the callback is executed with an assertionList as its + * second argument. + * + * @param {String} name + * @param {Function} fn + * @param {Object} opt + * @param {Function} callback + * @api public + */ + +exports.runTest = function (name, fn, opt, callback) { + var options = types.options(opt); + + options.testStart(name); + var start = new Date().getTime(); + var test = types.test(name, start, options, callback); + + try { + fn(test); + } + catch (e) { + test.done(e); + } +}; + +/** + * Takes an object containing test functions or other test suites as properties + * and runs each in series. After all tests have completed, the callback is + * called with a list of all assertions as the second argument. + * + * If a name is passed to this function it is prepended to all test and suite + * names that run within it. + * + * @param {String} name + * @param {Object} suite + * @param {Object} opt + * @param {Function} callback + * @api public + */ + +exports.runSuite = function (name, suite, opt, callback) { + var keys = _keys(suite); + + async.concatSeries(keys, function (k, cb) { + var prop = suite[k], _name; + + _name = name ? [].concat(name, k) : [k]; + + _name.toString = function () { + // fallback for old one + return this.join(' - '); + }; + + if (typeof prop === 'function') { + var in_name = false; + for (var i = 0; i < _name.length; i += 1) { + if (_name[i] === opt.testspec) { + in_name = true; + } + } + if (!opt.testspec || in_name) { + if (opt.moduleStart) { + opt.moduleStart(); + } + exports.runTest(_name, suite[k], opt, cb); + } + else { + return cb(); + } + } + else { + exports.runSuite(_name, suite[k], opt, cb); + } + }, callback); +}; + +/** + * Run each exported test function or test suite from a loaded module. + * + * @param {String} name + * @param {Object} mod + * @param {Object} opt + * @param {Function} callback + * @api public + */ + +exports.runModule = function (name, mod, opt, callback) { + var options = _copy(types.options(opt)); + + var _run = false; + var _moduleStart = options.moduleStart; + function run_once() { + if (!_run) { + _run = true; + _moduleStart(name); + } + } + options.moduleStart = run_once; + + var start = new Date().getTime(); + + exports.runSuite(null, mod, options, function (err, a_list) { + var end = new Date().getTime(); + var assertion_list = types.assertionList(a_list, end - start); + options.moduleDone(name, assertion_list); + callback(null, a_list); + }); +}; + +/** + * Treats an object literal as a list of modules keyed by name. Runs each + * module and finished with calling 'done'. You can think of this as a browser + * safe alternative to runFiles in the nodeunit module. + * + * @param {Object} modules + * @param {Object} opt + * @api public + */ + +// TODO: add proper unit tests for this function +exports.runModules = function (modules, opt) { + var all_assertions = []; + var options = types.options(opt); + var start = new Date().getTime(); + + async.concatSeries(_keys(modules), function (k, cb) { + exports.runModule(k, modules[k], options, cb); + }, + function (err, all_assertions) { + var end = new Date().getTime(); + options.done(types.assertionList(all_assertions, end - start)); + }); +}; + + +/** + * Wraps a test function with setUp and tearDown functions. + * Used by testCase. + * + * @param {Function} setUp + * @param {Function} tearDown + * @param {Function} fn + * @api private + */ + +var wrapTest = function (setUp, tearDown, fn) { + return function (test) { + var context = {}; + if (tearDown) { + var done = test.done; + test.done = function (err) { + try { + tearDown.call(context, function (err2) { + if (err && err2) { + test._assertion_list.push( + types.assertion({error: err}) + ); + return done(err2); + } + done(err || err2); + }); + } + catch (e) { + done(e); + } + }; + } + if (setUp) { + setUp.call(context, function (err) { + if (err) { + return test.done(err); + } + fn.call(context, test); + }); + } + else { + fn.call(context, test); + } + }; +}; + + +/** + * Wraps a group of tests with setUp and tearDown functions. + * Used by testCase. + * + * @param {Function} setUp + * @param {Function} tearDown + * @param {Object} group + * @api private + */ + +var wrapGroup = function (setUp, tearDown, group) { + var tests = {}; + var keys = _keys(group); + for (var i = 0; i < keys.length; i += 1) { + var k = keys[i]; + if (typeof group[k] === 'function') { + tests[k] = wrapTest(setUp, tearDown, group[k]); + } + else if (typeof group[k] === 'object') { + tests[k] = wrapGroup(setUp, tearDown, group[k]); + } + } + return tests; +}; + + +/** + * Utility for wrapping a suite of test functions with setUp and tearDown + * functions. + * + * @param {Object} suite + * @return {Object} + * @api public + */ + +exports.testCase = function (suite) { + var setUp = suite.setUp; + var tearDown = suite.tearDown; + delete suite.setUp; + delete suite.tearDown; + return wrapGroup(setUp, tearDown, suite); +}; +})(core); +(function(exports){ +/*! + * Nodeunit + * Copyright (c) 2010 Caolan McMahon + * MIT Licensed + * + * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS! + * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build. + * Only code on that line will be removed, its mostly to avoid requiring code + * that is node specific + */ + + +/** + * NOTE: this test runner is not listed in index.js because it cannot be + * used with the command-line tool, only inside the browser. + */ + + +/** + * Reporter info string + */ + +exports.info = "Browser-based test reporter"; + + +/** + * Run all tests within each module, reporting the results + * + * @param {Array} files + * @api public + */ + +exports.run = function (modules, options) { + var start = new Date().getTime(); + + function setText(el, txt) { + if ('innerText' in el) { + el.innerText = txt; + } + else if ('textContent' in el){ + el.textContent = txt; + } + } + + function getOrCreate(tag, id) { + var el = document.getElementById(id); + if (!el) { + el = document.createElement(tag); + el.id = id; + document.body.appendChild(el); + } + return el; + }; + + var header = getOrCreate('h1', 'nodeunit-header'); + var banner = getOrCreate('h2', 'nodeunit-banner'); + var userAgent = getOrCreate('h2', 'nodeunit-userAgent'); + var tests = getOrCreate('ol', 'nodeunit-tests'); + var result = getOrCreate('p', 'nodeunit-testresult'); + + setText(userAgent, navigator.userAgent); + + nodeunit.runModules(modules, { + moduleStart: function (name) { + /*var mheading = document.createElement('h2'); + mheading.innerText = name; + results.appendChild(mheading); + module = document.createElement('ol'); + results.appendChild(module);*/ + }, + testDone: function (name, assertions) { + var test = document.createElement('li'); + var strong = document.createElement('strong'); + strong.innerHTML = name + ' <b style="color: black;">(' + + '<b class="fail">' + assertions.failures() + '</b>, ' + + '<b class="pass">' + assertions.passes() + '</b>, ' + + assertions.length + + ')</b>'; + test.className = assertions.failures() ? 'fail': 'pass'; + test.appendChild(strong); + + var aList = document.createElement('ol'); + aList.style.display = 'none'; + test.onclick = function () { + var d = aList.style.display; + aList.style.display = (d == 'none') ? 'block': 'none'; + }; + for (var i=0; i<assertions.length; i++) { + var li = document.createElement('li'); + var a = assertions[i]; + if (a.failed()) { + li.innerHTML = (a.message || a.method || 'no message') + + '<pre>' + (a.error.stack || a.error) + '</pre>'; + li.className = 'fail'; + } + else { + li.innerHTML = a.message || a.method || 'no message'; + li.className = 'pass'; + } + aList.appendChild(li); + } + test.appendChild(aList); + tests.appendChild(test); + }, + done: function (assertions) { + var end = new Date().getTime(); + var duration = end - start; + + var failures = assertions.failures(); + banner.className = failures ? 'fail': 'pass'; + + result.innerHTML = 'Tests completed in ' + duration + + ' milliseconds.<br/><span class="passed">' + + assertions.passes() + '</span> assertions of ' + + '<span class="all">' + assertions.length + '<span> passed, ' + + assertions.failures() + ' failed.'; + } + }); +}; +})(reporter); +nodeunit = core; +nodeunit.assert = assert; +nodeunit.reporter = reporter; +nodeunit.run = reporter.run; +return nodeunit; })(); diff --git a/node_modules/mongodb/node_modules/bson/test/browser/suite2.js b/node_modules/mongodb/node_modules/bson/test/browser/suite2.js new file mode 100644 index 0000000..c7288e8 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/browser/suite2.js @@ -0,0 +1,13 @@ +this.suite2 = { + 'another test': function (test) { + setTimeout(function () { + // lots of assertions + test.ok(true, 'everythings ok'); + test.ok(true, 'everythings ok'); + test.ok(true, 'everythings ok'); + test.ok(true, 'everythings ok'); + test.ok(true, 'everythings ok'); + test.done(); + }, 10); + } +}; diff --git a/node_modules/mongodb/node_modules/bson/test/browser/suite3.js b/node_modules/mongodb/node_modules/bson/test/browser/suite3.js new file mode 100644 index 0000000..8929741 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/browser/suite3.js @@ -0,0 +1,7 @@ +this.suite3 = { + 'test for ie6,7,8': function (test) { + test.deepEqual(["test"], ["test"]); + test.notDeepEqual(["a"], ["b"]); + test.done(); + } +}; diff --git a/node_modules/mongodb/node_modules/bson/test/browser/test.html b/node_modules/mongodb/node_modules/bson/test/browser/test.html new file mode 100644 index 0000000..56d4d96 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/browser/test.html @@ -0,0 +1,30 @@ +<html> + <head> + <title>Example tests</title> + <!-- Actual BSON Code --> + <script src="../../lib/bson/binary_parser.js"></script> + <script src="../../lib/bson/code.js"></script> + <script src="../../lib/bson/db_ref.js"></script> + <script src="../../lib/bson/double.js"></script> + <script src="../../lib/bson/float_parser.js"></script> + <script src="../../lib/bson/long.js"></script> + <script src="../../lib/bson/max_key.js"></script> + <script src="../../lib/bson/min_key.js"></script> + <script src="../../lib/bson/objectid.js"></script> + <script src="../../lib/bson/symbol.js"></script> + <script src="../../lib/bson/timestamp.js"></script> + <script src="../../lib/bson/bson.js"></script> + <script src="../../lib/bson/binary.js"></script> + + <!-- Unit tests --> + <script src="nodeunit.js"></script> + <script src="bson_test.js"></script> + </head> + <body> + <script> + nodeunit.run({ + 'bson_test': bson_test, + }); + </script> + </body> +</html> diff --git a/node_modules/mongodb/node_modules/bson/test/node/bson_array_test.js b/node_modules/mongodb/node_modules/bson/test/node/bson_array_test.js new file mode 100644 index 0000000..5304bef --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/bson_array_test.js @@ -0,0 +1,240 @@ +var mongodb = require('../../lib/bson').pure(); + +var testCase = require('nodeunit').testCase, + mongoO = require('../../lib/bson').pure(), + debug = require('util').debug, + inspect = require('util').inspect, + Buffer = require('buffer').Buffer, + gleak = require('../../tools/gleak'), + fs = require('fs'), + BSON = mongoO.BSON, + Code = mongoO.Code, + Binary = mongoO.Binary, + Timestamp = mongoO.Timestamp, + Long = mongoO.Long, + MongoReply = mongoO.MongoReply, + ObjectID = mongoO.ObjectID, + Symbol = mongoO.Symbol, + DBRef = mongoO.DBRef, + Double = mongoO.Double, + MinKey = mongoO.MinKey, + MaxKey = mongoO.MaxKey, + BinaryParser = mongoO.BinaryParser, + utils = require('./tools/utils'); + +var BSONSE = mongodb, + BSONDE = mongodb; + +// for tests +BSONDE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONDE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONDE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONDE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONDE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONDE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +BSONSE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONSE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONSE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONSE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONSE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONSE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +var hexStringToBinary = function(string) { + var numberofValues = string.length / 2; + var array = ""; + + for(var i = 0; i < numberofValues; i++) { + array += String.fromCharCode(parseInt(string[i*2] + string[i*2 + 1], 16)); + } + return array; +} + +var assertBuffersEqual = function(test, buffer1, buffer2) { + if(buffer1.length != buffer2.length) test.fail("Buffers do not have the same length", buffer1, buffer2); + + for(var i = 0; i < buffer1.length; i++) { + test.equal(buffer1[i], buffer2[i]); + } +} + +/** + * Module for parsing an ISO 8601 formatted string into a Date object. + */ +var ISODate = function (string) { + var match; + + if (typeof string.getTime === "function") + return string; + else if (match = string.match(/^(\d{4})(-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|((\+|-)(\d{2}):(\d{2}))))?)?)?$/)) { + var date = new Date(); + date.setUTCFullYear(Number(match[1])); + date.setUTCMonth(Number(match[3]) - 1 || 0); + date.setUTCDate(Number(match[5]) || 0); + date.setUTCHours(Number(match[7]) || 0); + date.setUTCMinutes(Number(match[8]) || 0); + date.setUTCSeconds(Number(match[10]) || 0); + date.setUTCMilliseconds(Number("." + match[12]) * 1000 || 0); + + if (match[13] && match[13] !== "Z") { + var h = Number(match[16]) || 0, + m = Number(match[17]) || 0; + + h *= 3600000; + m *= 60000; + + var offset = h + m; + if (match[15] == "+") + offset = -offset; + + date = new Date(date.valueOf() + offset); + } + + return date; + } else + throw new Error("Invalid ISO 8601 date given.", __filename); +}; + +var _Uint8Array = null; + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.setUp = function(callback) { + _Uint8Array = global.Uint8Array; + delete global['Uint8Array']; + callback(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.tearDown = function(callback) { + global['Uint8Array'] = _Uint8Array; + callback(); +} + +// /** +// * @ignore +// */ +// exports.shouldCorrectlyDeserializeUsingTypedArray = function(test) { +// var motherOfAllDocuments = { +// 'string': '客家话', +// 'array': [1,2,3], +// 'hash': {'a':1, 'b':2}, +// 'date': new Date(), +// 'oid': new ObjectID(), +// 'binary': new Binary(new Buffer("hello")), +// 'int': 42, +// 'float': 33.3333, +// 'regexp': /regexp/, +// 'boolean': true, +// 'long': Long.fromNumber(100), +// 'where': new Code('this.a > i', {i:1}), +// 'dbref': new DBRef('namespace', new ObjectID(), 'integration_tests_'), +// 'minkey': new MinKey(), +// 'maxkey': new MaxKey() +// } +// +// // Let's serialize it +// var data = BSONSE.BSON.serialize(motherOfAllDocuments, true, true, false); +// // Build a typed array +// var arr = new Uint8Array(new ArrayBuffer(data.length)); +// // Iterate over all the fields and copy +// for(var i = 0; i < data.length; i++) { +// arr[i] = data[i] +// } +// +// // Deserialize the object +// var object = BSONDE.BSON.deserialize(arr); +// // Asserts +// test.equal(motherOfAllDocuments.string, object.string); +// test.deepEqual(motherOfAllDocuments.array, object.array); +// test.deepEqual(motherOfAllDocuments.date, object.date); +// test.deepEqual(motherOfAllDocuments.oid.toHexString(), object.oid.toHexString()); +// test.deepEqual(motherOfAllDocuments.binary.length(), object.binary.length()); +// // Assert the values of the binary +// for(var i = 0; i < motherOfAllDocuments.binary.length(); i++) { +// test.equal(motherOfAllDocuments.binary.value[i], object.binary[i]); +// } +// test.deepEqual(motherOfAllDocuments.int, object.int); +// test.deepEqual(motherOfAllDocuments.float, object.float); +// test.deepEqual(motherOfAllDocuments.regexp, object.regexp); +// test.deepEqual(motherOfAllDocuments.boolean, object.boolean); +// test.deepEqual(motherOfAllDocuments.long.toNumber(), object.long); +// test.deepEqual(motherOfAllDocuments.where, object.where); +// test.deepEqual(motherOfAllDocuments.dbref.oid.toHexString(), object.dbref.oid.toHexString()); +// test.deepEqual(motherOfAllDocuments.dbref.namespace, object.dbref.namespace); +// test.deepEqual(motherOfAllDocuments.dbref.db, object.dbref.db); +// test.deepEqual(motherOfAllDocuments.minkey, object.minkey); +// test.deepEqual(motherOfAllDocuments.maxkey, object.maxkey); +// test.done(); +// } + +/** + * @ignore + */ +exports.shouldCorrectlySerializeUsingTypedArray = function(test) { + var motherOfAllDocuments = { + 'string': 'hello', + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': new Date(), + 'oid': new ObjectID(), + 'binary': new Binary(new Buffer("hello")), + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': Long.fromNumber(100), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', new ObjectID(), 'integration_tests_'), + 'minkey': new MinKey(), + 'maxkey': new MaxKey() + } + + // Let's serialize it + var data = BSONSE.BSON.serialize(motherOfAllDocuments, true, false, false); + // And deserialize it again + var object = BSONSE.BSON.deserialize(data); + // Asserts + test.equal(motherOfAllDocuments.string, object.string); + test.deepEqual(motherOfAllDocuments.array, object.array); + test.deepEqual(motherOfAllDocuments.date, object.date); + test.deepEqual(motherOfAllDocuments.oid.toHexString(), object.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.binary.length(), object.binary.length()); + // Assert the values of the binary + for(var i = 0; i < motherOfAllDocuments.binary.length(); i++) { + test.equal(motherOfAllDocuments.binary.value[i], object.binary[i]); + } + test.deepEqual(motherOfAllDocuments.int, object.int); + test.deepEqual(motherOfAllDocuments.float, object.float); + test.deepEqual(motherOfAllDocuments.regexp, object.regexp); + test.deepEqual(motherOfAllDocuments.boolean, object.boolean); + test.deepEqual(motherOfAllDocuments.long.toNumber(), object.long); + test.deepEqual(motherOfAllDocuments.where, object.where); + test.deepEqual(motherOfAllDocuments.dbref.oid.toHexString(), object.dbref.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.dbref.namespace, object.dbref.namespace); + test.deepEqual(motherOfAllDocuments.dbref.db, object.dbref.db); + test.deepEqual(motherOfAllDocuments.minkey, object.minkey); + test.deepEqual(motherOfAllDocuments.maxkey, object.maxkey); + test.done(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.noGlobalsLeaked = function(test) { + var leaks = gleak.detectNew(); + test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', ')); + test.done(); +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/test/node/bson_parser_comparision_test.js b/node_modules/mongodb/node_modules/bson/test/node/bson_parser_comparision_test.js new file mode 100644 index 0000000..e217be9 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/bson_parser_comparision_test.js @@ -0,0 +1,459 @@ +var sys = require('util'), + debug = require('util').debug, + inspect = require('util').inspect, + Buffer = require('buffer').Buffer, + BSON = require('../../ext/bson').BSON, + Buffer = require('buffer').Buffer, + BSONJS = require('../../lib/bson/bson').BSON, + BinaryParser = require('../../lib/bson/binary_parser').BinaryParser, + Long = require('../../lib/bson/long').Long, + ObjectID = require('../../lib/bson/bson').ObjectID, + Binary = require('../../lib/bson/bson').Binary, + Code = require('../../lib/bson/bson').Code, + DBRef = require('../../lib/bson/bson').DBRef, + Symbol = require('../../lib/bson/bson').Symbol, + Double = require('../../lib/bson/bson').Double, + MaxKey = require('../../lib/bson/bson').MaxKey, + MinKey = require('../../lib/bson/bson').MinKey, + Timestamp = require('../../lib/bson/bson').Timestamp, + assert = require('assert'); + +// Parsers +var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); +var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.setUp = function(callback) { + callback(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.tearDown = function(callback) { + callback(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize simple edge value'] = function(test) { + // Simple serialization and deserialization of edge value + var doc = {doc:0x1ffffffffffffe}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + + var doc = {doc:-0x1ffffffffffffe}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly execute toJSON'] = function(test) { + var a = Long.fromNumber(10); + assert.equal(10, a); + + var a = Long.fromNumber(9223372036854775807); + assert.equal(9223372036854775807, a); + + // Simple serialization and deserialization test for a Single String value + var doc = {doc:'Serialize'}; + var simple_string_serialized = bsonC.serialize(doc, true, false); + + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Serialize and Deserialize nested document'] = function(test) { + // Nested doc + var doc = {a:{b:{c:1}}}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple integer serialization/deserialization test, including testing boundary conditions'] = function(test) { + var doc = {doc:-1}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + + var doc = {doc:2147483648}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + + var doc = {doc:-2147483648}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization test for a Long value'] = function(test) { + var doc = {doc:Long.fromNumber(9223372036854775807)}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(9223372036854775807)}, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + + var doc = {doc:Long.fromNumber(-9223372036854775807)}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:Long.fromNumber(-9223372036854775807)}, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a Float value'] = function(test) { + var doc = {doc:2222.3333}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + + var doc = {doc:-2222.3333}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a null value'] = function(test) { + var doc = {doc:null}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a boolean value'] = function(test) { + var doc = {doc:true}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a date value'] = function(test) { + var date = new Date(); + var doc = {doc:date}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')), bsonC.deserialize(simple_string_serialized)); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a boolean value'] = function(test) { + var doc = {doc:/abcd/mi}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + + var doc = {doc:/abcd/}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc, false, true)); + assert.equal(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a objectId value'] = function(test) { + var doc = {doc:new ObjectID()}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + var doc2 = {doc:ObjectID.createFromHexString(doc.doc.toHexString())}; + + assert.deepEqual(simple_string_serialized, bsonJS.serialize(doc2, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.toString(), bsonC.deserialize(simple_string_serialized).doc.toString()); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a Binary value'] = function(test) { + var binary = new Binary(); + var string = 'binstring' + for(var index = 0; index < string.length; index++) { binary.put(string.charAt(index)); } + + var simple_string_serialized = bsonC.serialize({doc:binary}, false, true); + assert.deepEqual(simple_string_serialized, bsonJS.serialize({doc:binary}, false, true)); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized, 'binary')).doc.value(), bsonC.deserialize(simple_string_serialized).doc.value()); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a Code value'] = function(test) { + var code = new Code('this.a > i', {'i': 1}); + var simple_string_serialized_2 = bsonJS.serialize({doc:code}, false, true); + var simple_string_serialized = bsonC.serialize({doc:code}, false, true); + + assert.deepEqual(simple_string_serialized, simple_string_serialized_2); + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc.scope, bsonC.deserialize(simple_string_serialized).doc.scope); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for an Object'] = function(test) { + var simple_string_serialized = bsonC.serialize({doc:{a:1, b:{c:2}}}, false, true); + var simple_string_serialized_2 = bsonJS.serialize({doc:{a:1, b:{c:2}}}, false, true); + assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); + + // Simple serialization and deserialization for an Array + var simple_string_serialized = bsonC.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); + var simple_string_serialized_2 = bsonJS.serialize({doc:[9, 9, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1]}, false, true); + + assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + assert.deepEqual(bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')).doc, bsonC.deserialize(simple_string_serialized).doc); + test.done(); +} + +/** + * @ignore + */ +exports['Simple serialization and deserialization for a DBRef'] = function(test) { + var oid = new ObjectID() + var oid2 = new ObjectID.createFromHexString(oid.toHexString()) + var simple_string_serialized = bsonJS.serialize({doc:new DBRef('namespace', oid2, 'integration_tests_')}, false, true); + var simple_string_serialized_2 = bsonC.serialize({doc:new DBRef('namespace', oid, 'integration_tests_')}, false, true); + + assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + // Ensure we have the same values for the dbref + var object_js = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); + var object_c = bsonC.deserialize(simple_string_serialized); + + assert.equal(object_js.doc.namespace, object_c.doc.namespace); + assert.equal(object_js.doc.oid.toHexString(), object_c.doc.oid.toHexString()); + assert.equal(object_js.doc.db, object_c.doc.db); + test.done(); +} + +/** + * @ignore + */ +exports['Should correctly deserialize bytes array'] = function(test) { + // Serialized document + var bytes = [47,0,0,0,2,110,97,109,101,0,6,0,0,0,80,97,116,116,121,0,16,97,103,101,0,34,0,0,0,7,95,105,100,0,76,100,12,23,11,30,39,8,89,0,0,1,0]; + var serialized_data = ''; + // Convert to chars + for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); + } + var object = bsonC.deserialize(new Buffer(serialized_data, 'binary')); + assert.equal('Patty', object.name) + assert.equal(34, object.age) + assert.equal('4c640c170b1e270859000001', object._id.toHexString()) + test.done(); +} + +/** + * @ignore + */ +exports['Serialize utf8'] = function(test) { + var doc = { "name" : "本荘由利地域に洪水警報", "name1" : "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "name2" : "abcdedede"}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + var simple_string_serialized2 = bsonJS.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, simple_string_serialized2) + + var object = bsonC.deserialize(simple_string_serialized); + assert.equal(doc.name, object.name) + assert.equal(doc.name1, object.name1) + assert.equal(doc.name2, object.name2) + test.done(); +} + +/** + * @ignore + */ +exports['Serialize object with array'] = function(test) { + var doc = {b:[1, 2, 3]}; + var simple_string_serialized = bsonC.serialize(doc, false, true); + var simple_string_serialized_2 = bsonJS.serialize(doc, false, true); + assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + + var object = bsonC.deserialize(simple_string_serialized); + assert.deepEqual(doc, object) + test.done(); +} + +/** + * @ignore + */ +exports['Test equality of an object ID'] = function(test) { + var object_id = new ObjectID(); + var object_id_2 = new ObjectID(); + assert.ok(object_id.equals(object_id)); + assert.ok(!(object_id.equals(object_id_2))) + test.done(); +} + +/** + * @ignore + */ +exports['Test same serialization for Object ID'] = function(test) { + var object_id = new ObjectID(); + var object_id2 = ObjectID.createFromHexString(object_id.toString()) + var simple_string_serialized = bsonJS.serialize({doc:object_id}, false, true); + var simple_string_serialized_2 = bsonC.serialize({doc:object_id2}, false, true); + + assert.equal(simple_string_serialized_2.length, simple_string_serialized.length); + assert.deepEqual(simple_string_serialized, simple_string_serialized_2) + var object = bsonJS.deserialize(new Buffer(simple_string_serialized_2, 'binary')); + var object2 = bsonC.deserialize(simple_string_serialized); + assert.equal(object.doc.id, object2.doc.id) + test.done(); +} + +/** + * @ignore + */ +exports['Complex object serialization'] = function(test) { + // JS Object + var c1 = { _id: new ObjectID, comments: [], title: 'number 1' }; + var c2 = { _id: new ObjectID, comments: [], title: 'number 2' }; + var doc = { + numbers: [] + , owners: [] + , comments: [c1, c2] + , _id: new ObjectID + }; + + var simple_string_serialized = bsonJS.serialize(doc, false, true); + + // C++ Object + var c1 = { _id: ObjectID.createFromHexString(c1._id.toHexString()), comments: [], title: 'number 1' }; + var c2 = { _id: ObjectID.createFromHexString(c2._id.toHexString()), comments: [], title: 'number 2' }; + var doc = { + numbers: [] + , owners: [] + , comments: [c1, c2] + , _id: ObjectID.createFromHexString(doc._id.toHexString()) + }; + + var simple_string_serialized_2 = bsonC.serialize(doc, false, true); + + for(var i = 0; i < simple_string_serialized_2.length; i++) { + // debug(i + "[" + simple_string_serialized_2[i] + "] = [" + simple_string_serialized[i] + "]") + assert.equal(simple_string_serialized_2[i], simple_string_serialized[i]); + } + + var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); + var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); + assert.equal(doc._id.id, doc1._id.id) + assert.equal(doc._id.id, doc2._id.id) + assert.equal(doc1._id.id, doc2._id.id) + + var doc = { + _id: 'testid', + key1: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 }, + key2: { code: 'test1', time: {start:1309323402727,end:1309323402727}, x:10, y:5 } + }; + + var simple_string_serialized = bsonJS.serialize(doc, false, true); + var simple_string_serialized_2 = bsonC.serialize(doc, false, true); + test.done(); +} + +/** + * @ignore + */ +exports['Serialize function'] = function(test) { + var doc = { + _id: 'testid', + key1: function() {} + } + + var simple_string_serialized = bsonJS.serialize(doc, false, true, true); + var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); + + // Deserialize the string + var doc1 = bsonJS.deserialize(new Buffer(simple_string_serialized_2)); + var doc2 = bsonC.deserialize(new Buffer(simple_string_serialized_2)); + assert.equal(doc1.key1.code.toString(), doc2.key1.code.toString()) + test.done(); +} + +/** + * @ignore + */ +exports['Serialize document with special operators'] = function(test) { + var doc = {"user_id":"4e9fc8d55883d90100000003","lc_status":{"$ne":"deleted"},"owner_rating":{"$exists":false}}; + var simple_string_serialized = bsonJS.serialize(doc, false, true, true); + var simple_string_serialized_2 = bsonC.serialize(doc, false, true, true); + + // Should serialize to the same value + assert.equal(simple_string_serialized_2.toString('base64'), simple_string_serialized.toString('base64')) + var doc1 = bsonJS.deserialize(simple_string_serialized_2); + var doc2 = bsonC.deserialize(simple_string_serialized); + assert.deepEqual(doc1, doc2) + test.done(); +} + +/** + * @ignore + */ +exports['Create ObjectID from hex string'] = function(test) { + // Hex Id + var hexId = new ObjectID().toString(); + var docJS = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; + var docC = {_id: ObjectID.createFromHexString(hexId), 'funds.remaining': {$gte: 1.222}, 'transactions.id': {$ne: ObjectID.createFromHexString(hexId)}}; + var docJSBin = bsonJS.serialize(docJS, false, true, true); + var docCBin = bsonC.serialize(docC, false, true, true); + assert.equal(docCBin.toString('base64'), docJSBin.toString('base64')); + test.done(); +} + +/** + * @ignore + */ +exports['Serialize big complex document'] = function(test) { + // Complex document serialization + var doc = {"DateTime": "Tue Nov 40 2011 17:27:55 GMT+0000 (WEST)","isActive": true,"Media": {"URL": "http://videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb"},"Title": "Lisboa fecha a ganhar 0.19%","SetPosition": 60,"Type": "videos","Thumbnail": [{"URL": "http://rd3.videos.sapo.pt/Tc85NsjaKjj8o5aV7Ubb/pic/320x240","Dimensions": {"Height": 240,"Width": 320}}],"Source": {"URL": "http://videos.sapo.pt","SetID": "1288","SourceID": "http://videos.sapo.pt/tvnet/rss2","SetURL": "http://noticias.sapo.pt/videos/tv-net_1288/","ItemID": "Tc85NsjaKjj8o5aV7Ubb","Name": "SAPO VÃdeos"},"Category": "Tec_ciencia","Description": "Lisboa fecha a ganhar 0.19%","GalleryID": new ObjectID("4eea2a634ce8573200000000"),"InternalRefs": {"RegisterDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","ChangeDate": "Thu Dec 15 2011 17:12:51 GMT+0000 (WEST)","Hash": 332279244514},"_id": new ObjectID("4eea2a96e52778160000003a")} + var docJSBin = bsonJS.serialize(doc, false, true, true); + var docCBin = bsonC.serialize(doc, false, true, true); + assert.equal(docCBin.toString('base64'), docJSBin.toString('base64')); + test.done(); +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/test/node/bson_test.js b/node_modules/mongodb/node_modules/bson/test/node/bson_test.js new file mode 100644 index 0000000..d21f3dd --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/bson_test.js @@ -0,0 +1,1591 @@ +var mongodb = process.env['TEST_NATIVE'] != null ? require('../../lib/bson').native() : require('../../lib/bson').pure(); + +var testCase = require('nodeunit').testCase, + mongoO = require('../../lib/bson').pure(), + Buffer = require('buffer').Buffer, + gleak = require('../../tools/gleak'), + fs = require('fs'), + BSON = mongoO.BSON, + Code = mongoO.Code, + Binary = mongoO.Binary, + Timestamp = mongoO.Timestamp, + Long = mongoO.Long, + MongoReply = mongoO.MongoReply, + ObjectID = mongoO.ObjectID, + Symbol = mongoO.Symbol, + DBRef = mongoO.DBRef, + Double = mongoO.Double, + MinKey = mongoO.MinKey, + MaxKey = mongoO.MaxKey, + BinaryParser = mongoO.BinaryParser; + +var BSONSE = mongodb, + BSONDE = mongodb; + +// for tests +BSONDE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONDE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONDE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONDE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONDE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONDE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +BSONSE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONSE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONSE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONSE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONSE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONSE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +var hexStringToBinary = function(string) { + var numberofValues = string.length / 2; + var array = ""; + + for(var i = 0; i < numberofValues; i++) { + array += String.fromCharCode(parseInt(string[i*2] + string[i*2 + 1], 16)); + } + return array; +} + +var assertBuffersEqual = function(test, buffer1, buffer2) { + if(buffer1.length != buffer2.length) test.fail("Buffers do not have the same length", buffer1, buffer2); + + for(var i = 0; i < buffer1.length; i++) { + test.equal(buffer1[i], buffer2[i]); + } +} + +/** + * Module for parsing an ISO 8601 formatted string into a Date object. + */ +var ISODate = function (string) { + var match; + + if (typeof string.getTime === "function") + return string; + else if (match = string.match(/^(\d{4})(-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|((\+|-)(\d{2}):(\d{2}))))?)?)?$/)) { + var date = new Date(); + date.setUTCFullYear(Number(match[1])); + date.setUTCMonth(Number(match[3]) - 1 || 0); + date.setUTCDate(Number(match[5]) || 0); + date.setUTCHours(Number(match[7]) || 0); + date.setUTCMinutes(Number(match[8]) || 0); + date.setUTCSeconds(Number(match[10]) || 0); + date.setUTCMilliseconds(Number("." + match[12]) * 1000 || 0); + + if (match[13] && match[13] !== "Z") { + var h = Number(match[16]) || 0, + m = Number(match[17]) || 0; + + h *= 3600000; + m *= 60000; + + var offset = h + m; + if (match[15] == "+") + offset = -offset; + + date = new Date(date.valueOf() + offset); + } + + return date; + } else + throw new Error("Invalid ISO 8601 date given.", __filename); +}; + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.setUp = function(callback) { + callback(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.tearDown = function(callback) { + callback(); +} + +/** + * @ignore + */ +exports['Should Correctly get BSON types from require'] = function(test) { + var _mongodb = require('../../lib/bson'); + test.ok(_mongodb.ObjectID === ObjectID); + test.ok(_mongodb.Binary === Binary); + test.ok(_mongodb.Long === Long); + test.ok(_mongodb.Timestamp === Timestamp); + test.ok(_mongodb.Code === Code); + test.ok(_mongodb.DBRef === DBRef); + test.ok(_mongodb.Symbol === Symbol); + test.ok(_mongodb.MinKey === MinKey); + test.ok(_mongodb.MaxKey === MaxKey); + test.ok(_mongodb.Double === Double); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Deserialize object'] = function(test) { + var bytes = [95,0,0,0,2,110,115,0,42,0,0,0,105,110,116,101,103,114,97,116,105,111,110,95,116,101,115,116,115,95,46,116,101,115,116,95,105,110,100,101,120,95,105,110,102,111,114,109,97,116,105,111,110,0,8,117,110,105,113,117,101,0,0,3,107,101,121,0,12,0,0,0,16,97,0,1,0,0,0,0,2,110,97,109,101,0,4,0,0,0,97,95,49,0,0]; + var serialized_data = ''; + // Convert to chars + for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); + } + + var object = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(new Buffer(serialized_data, 'binary')); + test.equal("a_1", object.name); + test.equal(false, object.unique); + test.equal(1, object.key.a); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Deserialize object with all types'] = function(test) { + var bytes = [26,1,0,0,7,95,105,100,0,161,190,98,75,118,169,3,0,0,3,0,0,4,97,114,114,97,121,0,26,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,0,2,115,116,114,105,110,103,0,6,0,0,0,104,101,108,108,111,0,3,104,97,115,104,0,19,0,0,0,16,97,0,1,0,0,0,16,98,0,2,0,0,0,0,9,100,97,116,101,0,161,190,98,75,0,0,0,0,7,111,105,100,0,161,190,98,75,90,217,18,0,0,1,0,0,5,98,105,110,97,114,121,0,7,0,0,0,2,3,0,0,0,49,50,51,16,105,110,116,0,42,0,0,0,1,102,108,111,97,116,0,223,224,11,147,169,170,64,64,11,114,101,103,101,120,112,0,102,111,111,98,97,114,0,105,0,8,98,111,111,108,101,97,110,0,1,15,119,104,101,114,101,0,25,0,0,0,12,0,0,0,116,104,105,115,46,120,32,61,61,32,51,0,5,0,0,0,0,3,100,98,114,101,102,0,37,0,0,0,2,36,114,101,102,0,5,0,0,0,116,101,115,116,0,7,36,105,100,0,161,190,98,75,2,180,1,0,0,2,0,0,0,10,110,117,108,108,0,0]; + var serialized_data = ''; + // Convert to chars + for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); + } + + var object = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(new Buffer(serialized_data, 'binary'));//, false, true); + // Perform tests + test.equal("hello", object.string); + test.deepEqual([1,2,3], object.array); + test.equal(1, object.hash.a); + test.equal(2, object.hash.b); + test.ok(object.date != null); + test.ok(object.oid != null); + test.ok(object.binary != null); + test.equal(42, object.int); + test.equal(33.3333, object.float); + test.ok(object.regexp != null); + test.equal(true, object.boolean); + test.ok(object.where != null); + test.ok(object.dbref != null); + test.ok(object[null] == null); + test.done(); +} + +/** + * @ignore + */ +exports['Should Serialize and Deserialize String'] = function(test) { + var test_string = {hello: 'world'}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_string, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_string)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_string, false, serialized_data2, 0); + + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_string, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Serialize and Deserialize Empty String'] = function(test) { + var test_string = {hello: ''}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_string, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_string)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_string, false, serialized_data2, 0); + + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_string, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Integer'] = function(test) { + var test_number = {doc: 5}; + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_number, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_number)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_number, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_number, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.deepEqual(test_number, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data2)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize null value'] = function(test) { + var test_null = {doc:null}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_null, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_null)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_null, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var object = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal(null, object.doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Number'] = function(test) { + var test_number = {doc: 5.5}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_number, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_number)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_number, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.deepEqual(test_number, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Integer'] = function(test) { + var test_int = {doc: 42}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_int, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_int)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_int, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_int.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc); + + test_int = {doc: -5600}; + serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_int, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_int)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_int, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_int.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc); + + test_int = {doc: 2147483647}; + serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_int, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_int)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_int, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_int.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc); + + test_int = {doc: -2147483648}; + serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_int, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_int)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_int, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + test.deepEqual(test_int.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Object'] = function(test) { + var doc = {doc: {age: 42, name: 'Spongebob', shoe_size: 9.5}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.deepEqual(doc.doc.age, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc.age); + test.deepEqual(doc.doc.name, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc.name); + test.deepEqual(doc.doc.shoe_size, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc.shoe_size); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Array'] = function(test) { + var doc = {doc: [1, 2, 'a', 'b']}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal(doc.doc[0], deserialized.doc[0]) + test.equal(doc.doc[1], deserialized.doc[1]) + test.equal(doc.doc[2], deserialized.doc[2]) + test.equal(doc.doc[3], deserialized.doc[3]) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Array with added on functions'] = function(test) { + Array.prototype.toXml = function() {}; + var doc = {doc: [1, 2, 'a', 'b']}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal(doc.doc[0], deserialized.doc[0]) + test.equal(doc.doc[1], deserialized.doc[1]) + test.equal(doc.doc[2], deserialized.doc[2]) + test.equal(doc.doc[3], deserialized.doc[3]) + test.done(); +} + +/** + * @ignore + */ +exports['Should correctly deserialize a nested object'] = function(test) { + var doc = {doc: {doc:1}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.deepEqual(doc.doc.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc.doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize A Boolean'] = function(test) { + var doc = {doc: true}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.equal(doc.doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a Date'] = function(test) { + var date = new Date(); + //(2009, 11, 12, 12, 00, 30) + date.setUTCDate(12); + date.setUTCFullYear(2009); + date.setUTCMonth(11 - 1); + date.setUTCHours(12); + date.setUTCMinutes(0); + date.setUTCSeconds(30); + var doc = {doc: date}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.equal(doc.date, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc.date); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize nested doc'] = function(test) { + var doc = { + string: "Strings are great", + decimal: 3.14159265, + bool: true, + integer: 5, + + subObject: { + moreText: "Bacon ipsum dolor.", + longKeylongKeylongKeylongKeylongKeylongKey: "Pork belly." + }, + + subArray: [1,2,3,4,5,6,7,8,9,10], + anotherString: "another string" + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Oid'] = function(test) { + var doc = {doc: new ObjectID()}; + var doc2 = {doc: ObjectID.createFromHexString(doc.doc.toHexString())}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + delete doc.doc.__id; + test.deepEqual(doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly encode Empty Hash'] = function(test) { + var doc = {}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + test.deepEqual(doc, new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Ordered Hash'] = function(test) { + var doc = {doc: {b:1, a:2, c:3, d:4}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var decoded_hash = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data).doc; + var keys = []; + + for(var name in decoded_hash) keys.push(name); + test.deepEqual(['b', 'a', 'c', 'd'], keys); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Regular Expression'] = function(test) { + // Serialize the regular expression + var doc = {doc: /foobar/mi}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc.doc.toString(), doc2.doc.toString()); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a Binary object'] = function(test) { + var bin = new Binary(); + var string = 'binstring'; + for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)); + } + + var doc = {doc: bin}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc.doc.value(), deserialized_data.doc.value()); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a big Binary object'] = function(test) { + var data = fs.readFileSync("test/node/data/test_gs_weird_bug.png", 'binary'); + var bin = new Binary(); + bin.write(data); + var doc = {doc: bin}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc.value(), deserialized_data.doc.value()); + test.done(); +} + +/** + * @ignore + */ +exports["Should Correctly Serialize and Deserialize DBRef"] = function(test) { + var oid = new ObjectID(); + var doc = {dbref: new DBRef('namespace', oid, null)}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal("namespace", doc2.dbref.namespace); + test.deepEqual(doc2.dbref.oid.toHexString(), oid.toHexString()); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize partial DBRef'] = function(test) { + var id = new ObjectID(); + var doc = {'name':'something', 'user':{'$ref':'username', '$id': id}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal('something', doc2.name); + test.equal('username', doc2.user.namespace); + test.equal(id.toString(), doc2.user.oid.toString()); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize simple Int'] = function(test) { + var doc = {doc:2147483648}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc, doc2.doc) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Long Integer'] = function(test) { + var doc = {doc: Long.fromNumber(9223372036854775807)}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc, deserialized_data.doc); + + doc = {doc: Long.fromNumber(-9223372036854775)}; + serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc, deserialized_data.doc); + + doc = {doc: Long.fromNumber(-9223372036854775809)}; + serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc, deserialized_data.doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Deserialize Large Integers as Number not Long'] = function(test) { + function roundTrip(val) { + var doc = {doc: val}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc, deserialized_data.doc); + }; + + roundTrip(Math.pow(2,52)); + roundTrip(Math.pow(2,53) - 1); + roundTrip(Math.pow(2,53)); + roundTrip(-Math.pow(2,52)); + roundTrip(-Math.pow(2,53) + 1); + roundTrip(-Math.pow(2,53)); + roundTrip(Math.pow(2,65)); // Too big for Long. + roundTrip(-Math.pow(2,65)); + roundTrip(9223372036854775807); + roundTrip(1234567890123456800); // Bigger than 2^53, stays a double. + roundTrip(-1234567890123456800); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Long Integer and Timestamp as different types'] = function(test) { + var long = Long.fromNumber(9223372036854775807); + var timestamp = Timestamp.fromNumber(9223372036854775807); + test.ok(long instanceof Long); + test.ok(!(long instanceof Timestamp)); + test.ok(timestamp instanceof Timestamp); + test.ok(!(timestamp instanceof Long)); + + var test_int = {doc: long, doc2: timestamp}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(test_int, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(test_int)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(test_int, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(test_int.doc, deserialized_data.doc); + test.done(); +} + +/** + * @ignore + */ +exports['Should Always put the id as the first item in a hash'] = function(test) { + var hash = {doc: {not_id:1, '_id':2}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(hash, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(hash)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(hash, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + var keys = []; + + for(var name in deserialized_data.doc) { + keys.push(name); + } + + test.deepEqual(['not_id', '_id'], keys); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a User defined Binary object'] = function(test) { + var bin = new Binary(); + bin.sub_type = BSON.BSON_BINARY_SUBTYPE_USER_DEFINED; + var string = 'binstring'; + for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)); + } + + var doc = {doc: bin}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(deserialized_data.doc.sub_type, BSON.BSON_BINARY_SUBTYPE_USER_DEFINED); + test.deepEqual(doc.doc.value(), deserialized_data.doc.value()); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correclty Serialize and Deserialize a Code object'] = function(test) { + var doc = {'doc': {'doc2': new Code('this.a > i', {i:1})}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.doc.doc2.code, deserialized_data.doc.doc2.code); + test.deepEqual(doc.doc.doc2.scope.i, deserialized_data.doc.doc2.scope.i); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly serialize and deserialize and embedded array'] = function(test) { + var doc = {'a':0, + 'b':['tmp1', 'tmp2', 'tmp3', 'tmp4', 'tmp5', 'tmp6', 'tmp7', 'tmp8', 'tmp9', 'tmp10', 'tmp11', 'tmp12', 'tmp13', 'tmp14', 'tmp15', 'tmp16'] + }; + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.a, deserialized_data.a); + test.deepEqual(doc.b, deserialized_data.b); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize UTF8'] = function(test) { + // Serialize utf8 + var doc = { "name" : "本荘由利地域に洪水警報", "name1" : "öüóőúéáűíÖÜÓŐÚÉÁŰÍ", "name2" : "abcdedede"}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc, deserialized_data); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize query object'] = function(test) { + var doc = { count: 'remove_with_no_callback_bug_test', query: {}, fields: null}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc, deserialized_data); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize empty query object'] = function(test) { + var doc = {}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc, deserialized_data); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize array based doc'] = function(test) { + var doc = { b: [ 1, 2, 3 ], _id: new ObjectID() }; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.b, deserialized_data.b) + test.deepEqual(doc, deserialized_data); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Symbol'] = function(test) { + if(Symbol != null) { + var doc = { b: [ new Symbol('test') ]}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc.b, deserialized_data.b) + test.deepEqual(doc, deserialized_data); + test.ok(deserialized_data.b[0] instanceof Symbol); + } + + test.done(); +} + +/** + * @ignore + */ +exports['Should handle Deeply nested document'] = function(test) { + var doc = {a:{b:{c:{d:2}}}}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var deserialized_data = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc, deserialized_data); + test.done(); +} + +/** + * @ignore + */ +exports['Should handle complicated all typed object'] = function(test) { + // First doc + var date = new Date(); + var oid = new ObjectID(); + var string = 'binstring' + var bin = new Binary() + for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)) + } + + var doc = { + 'string': 'hello', + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': date, + 'oid': oid, + 'binary': bin, + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': date.getTime(), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', oid, 'integration_tests_') + } + + // Second doc + var oid = new ObjectID.createFromHexString(oid.toHexString()); + var string = 'binstring' + var bin = new Binary() + for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)) + } + + var doc2 = { + 'string': 'hello', + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': date, + 'oid': oid, + 'binary': bin, + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': date.getTime(), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', oid, 'integration_tests_') + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var serialized_data2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc2, false, true); + + for(var i = 0; i < serialized_data2.length; i++) { + require('assert').equal(serialized_data2[i], serialized_data[i]) + } + + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize Complex Nested Object'] = function(test) { + var doc = { email: 'email@email.com', + encrypted_password: 'password', + friends: [ '4db96b973d01205364000006', + '4dc77b24c5ba38be14000002' ], + location: [ 72.4930088, 23.0431957 ], + name: 'Amit Kumar', + password_salt: 'salty', + profile_fields: [], + username: 'amit', + _id: new ObjectID() } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = doc; + doc2._id = ObjectID.createFromHexString(doc2._id.toHexString()); + var serialized_data2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc2, false, true); + + for(var i = 0; i < serialized_data2.length; i++) { + require('assert').equal(serialized_data2[i], serialized_data[i]) + } + + test.done(); +} + +/** + * @ignore + */ +exports['Should correctly massive doc'] = function(test) { + var oid1 = new ObjectID(); + var oid2 = new ObjectID(); + + // JS doc + var doc = { dbref2: new DBRef('namespace', oid1, 'integration_tests_'), + _id: oid2 }; + + var doc2 = { dbref2: new DBRef('namespace', ObjectID.createFromHexString(oid1.toHexString()), 'integration_tests_'), + _id: new ObjectID.createFromHexString(oid2.toHexString()) }; + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var serialized_data2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc2, false, true); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize/Deserialize regexp object'] = function(test) { + var doc = {'b':/foobaré/}; + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var serialized_data2 = new BSONDE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + for(var i = 0; i < serialized_data2.length; i++) { + require('assert').equal(serialized_data2[i], serialized_data[i]) + } + + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize/Deserialize complicated object'] = function(test) { + var doc = {a:{b:{c:[new ObjectID(), new ObjectID()]}}, d:{f:1332.3323}}; + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc, doc2) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize/Deserialize nested object'] = function(test) { + var doc = { "_id" : { "date" : new Date(), "gid" : "6f35f74d2bea814e21000000" }, + "value" : { + "b" : { "countries" : { "--" : 386 }, "total" : 1599 }, + "bc" : { "countries" : { "--" : 3 }, "total" : 10 }, + "gp" : { "countries" : { "--" : 2 }, "total" : 13 }, + "mgc" : { "countries" : { "--" : 2 }, "total" : 14 } + } + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc, doc2) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize/Deserialize nested object with even more nesting'] = function(test) { + var doc = { "_id" : { "date" : {a:1, b:2, c:new Date()}, "gid" : "6f35f74d2bea814e21000000" }, + "value" : { + "b" : { "countries" : { "--" : 386 }, "total" : 1599 }, + "bc" : { "countries" : { "--" : 3 }, "total" : 10 }, + "gp" : { "countries" : { "--" : 2 }, "total" : 13 }, + "mgc" : { "countries" : { "--" : 2 }, "total" : 14 } + } + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual(doc, doc2) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize empty name object'] = function(test) { + var doc = {'':'test', + 'bbbb':1}; + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.equal(doc2[''], 'test'); + test.equal(doc2['bbbb'], 1); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly handle Forced Doubles to ensure we allocate enough space for cap collections'] = function(test) { + if(Double != null) { + var doubleValue = new Double(100); + var doc = {value:doubleValue}; + + // Serialize + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + test.deepEqual({value:100}, doc2); + } + + test.done(); +} + +/** + * @ignore + */ +exports['Should deserialize correctly'] = function(test) { + var doc = { + "_id" : new ObjectID("4e886e687ff7ef5e00000162"), + "str" : "foreign", + "type" : 2, + "timestamp" : ISODate("2011-10-02T14:00:08.383Z"), + "links" : [ + "http://www.reddit.com/r/worldnews/comments/kybm0/uk_home_secretary_calls_for_the_scrapping_of_the/" + ] + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc, doc2) + test.done(); +} + +/** + * @ignore + */ +exports['Should correctly serialize and deserialize MinKey and MaxKey values'] = function(test) { + var doc = { + _id : new ObjectID("4e886e687ff7ef5e00000162"), + minKey : new MinKey(), + maxKey : new MaxKey() + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.deepEqual(doc, doc2) + test.ok(doc2.minKey instanceof MinKey); + test.ok(doc2.maxKey instanceof MaxKey); + test.done(); +} + +/** + * @ignore + */ +exports['Should correctly serialize Double value'] = function(test) { + var doc = { + value : new Double(34343.2222) + } + + var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); + var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); + new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); + assertBuffersEqual(test, serialized_data, serialized_data2, 0); + var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data); + + test.ok(doc.value.valueOf(), doc2.value); + test.ok(doc.value.value, doc2.value); + test.done(); +} + +/** + * @ignore + */ +exports['ObjectID should correctly create objects'] = function(test) { + try { + var object1 = ObjectID.createFromHexString('000000000000000000000001') + var object2 = ObjectID.createFromHexString('00000000000000000000001') + test.ok(false); + } catch(err) { + test.ok(err != null); + } + + test.done(); +} + +/** + * @ignore + */ +exports['ObjectID should correctly retrieve timestamp'] = function(test) { + var testDate = new Date(); + var object1 = new ObjectID(); + test.equal(Math.floor(testDate.getTime()/1000), Math.floor(object1.getTimestamp().getTime()/1000)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly throw error on bsonparser errors'] = function(test) { + var data = new Buffer(3); + var parser = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + + // Catch to small buffer error + try { + parser.deserialize(data); + test.ok(false); + } catch(err) {} + + data = new Buffer(5); + data[0] = 0xff; + data[1] = 0xff; + // Catch illegal size + try { + parser.deserialize(data); + test.ok(false); + } catch(err) {} + + // Finish up + test.done(); +} + +/** + * A simple example showing the usage of BSON.calculateObjectSize function returning the number of BSON bytes a javascript object needs. + * + * @_class bson + * @_function BSON.calculateObjectSize + * @ignore + */ +exports['Should correctly calculate the size of a given javascript object'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Calculate the size of the object without serializing the function + var size = BSON.calculateObjectSize(doc, false); + test.equal(12, size); + // Calculate the size of the object serializing the function + size = BSON.calculateObjectSize(doc, true); + // Validate the correctness + test.equal(36, size); + test.done(); +} + +/** + * A simple example showing the usage of BSON.calculateObjectSize function returning the number of BSON bytes a javascript object needs. + * + * @_class bson + * @_function calculateObjectSize + * @ignore + */ +exports['Should correctly calculate the size of a given javascript object using instance method'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Create a BSON parser instance + var bson = new BSON(); + // Calculate the size of the object without serializing the function + var size = bson.calculateObjectSize(doc, false); + test.equal(12, size); + // Calculate the size of the object serializing the function + size = bson.calculateObjectSize(doc, true); + // Validate the correctness + test.equal(36, size); + test.done(); +} + +/** + * A simple example showing the usage of BSON.serializeWithBufferAndIndex function. + * + * @_class bson + * @_function BSON.serializeWithBufferAndIndex + * @ignore + */ +exports['Should correctly serializeWithBufferAndIndex a given javascript object'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Calculate the size of the document, no function serialization + var size = BSON.calculateObjectSize(doc, false); + // Allocate a buffer + var buffer = new Buffer(size); + // Serialize the object to the buffer, checking keys and not serializing functions + var index = BSON.serializeWithBufferAndIndex(doc, true, buffer, 0, false); + // Validate the correctness + test.equal(12, size); + test.equal(11, index); + + // Serialize with functions + // Calculate the size of the document, no function serialization + var size = BSON.calculateObjectSize(doc, true); + // Allocate a buffer + var buffer = new Buffer(size); + // Serialize the object to the buffer, checking keys and not serializing functions + var index = BSON.serializeWithBufferAndIndex(doc, true, buffer, 0, true); + // Validate the correctness + test.equal(36, size); + test.equal(35, index); + test.done(); +} + +/** + * A simple example showing the usage of BSON.serializeWithBufferAndIndex function. + * + * @_class bson + * @_function serializeWithBufferAndIndex + * @ignore + */ +exports['Should correctly serializeWithBufferAndIndex a given javascript object using a BSON instance'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Create a BSON parser instance + var bson = new BSON(); + // Calculate the size of the document, no function serialization + var size = bson.calculateObjectSize(doc, false); + // Allocate a buffer + var buffer = new Buffer(size); + // Serialize the object to the buffer, checking keys and not serializing functions + var index = bson.serializeWithBufferAndIndex(doc, true, buffer, 0, false); + // Validate the correctness + test.equal(12, size); + test.equal(11, index); + + // Serialize with functions + // Calculate the size of the document, no function serialization + var size = bson.calculateObjectSize(doc, true); + // Allocate a buffer + var buffer = new Buffer(size); + // Serialize the object to the buffer, checking keys and not serializing functions + var index = bson.serializeWithBufferAndIndex(doc, true, buffer, 0, true); + // Validate the correctness + test.equal(36, size); + test.equal(35, index); + test.done(); +} + +/** + * A simple example showing the usage of BSON.serialize function returning serialized BSON Buffer object. + * + * @_class bson + * @_function BSON.serialize + * @ignore + */ +exports['Should correctly serialize a given javascript object'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Serialize the object to a buffer, checking keys and not serializing functions + var buffer = BSON.serialize(doc, true, true, false); + // Validate the correctness + test.equal(12, buffer.length); + + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = BSON.serialize(doc, true, true, true); + // Validate the correctness + test.equal(36, buffer.length); + test.done(); +} + +/** + * A simple example showing the usage of BSON.serialize function returning serialized BSON Buffer object. + * + * @_class bson + * @_function serialize + * @ignore + */ +exports['Should correctly serialize a given javascript object using a bson instance'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){}} + // Create a BSON parser instance + var bson = new BSON(); + // Serialize the object to a buffer, checking keys and not serializing functions + var buffer = bson.serialize(doc, true, true, false); + // Validate the correctness + test.equal(12, buffer.length); + + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = bson.serialize(doc, true, true, true); + // Validate the correctness + test.equal(36, buffer.length); + test.done(); +} + +/** + * A simple example showing the usage of BSON.deserialize function returning a deserialized Javascript function. + * + * @_class bson + * @_function BSON.deserialize + * @ignore + */ + exports['Should correctly deserialize a buffer using the BSON class level parser'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){ console.log('hello world'); }} + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = BSON.serialize(doc, true, true, true); + // Validate the correctness + test.equal(65, buffer.length); + + // Deserialize the object with no eval for the functions + var deserializedDoc = BSON.deserialize(buffer); + // Validate the correctness + test.equal('object', typeof deserializedDoc.func); + test.equal(1, deserializedDoc.a); + + // Deserialize the object with eval for the functions caching the functions + deserializedDoc = BSON.deserialize(buffer, {evalFunctions:true, cacheFunctions:true}); + // Validate the correctness + test.equal('function', typeof deserializedDoc.func); + test.equal(1, deserializedDoc.a); + test.done(); +} + +/** + * A simple example showing the usage of BSON instance deserialize function returning a deserialized Javascript function. + * + * @_class bson + * @_function deserialize + * @ignore + */ +exports['Should correctly deserialize a buffer using the BSON instance parser'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){ console.log('hello world'); }} + // Create a BSON parser instance + var bson = new BSON(); + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = bson.serialize(doc, true, true, true); + // Validate the correctness + test.equal(65, buffer.length); + + // Deserialize the object with no eval for the functions + var deserializedDoc = bson.deserialize(buffer); + // Validate the correctness + test.equal('object', typeof deserializedDoc.func); + test.equal(1, deserializedDoc.a); + + // Deserialize the object with eval for the functions caching the functions + deserializedDoc = bson.deserialize(buffer, {evalFunctions:true, cacheFunctions:true}); + // Validate the correctness + test.equal('function', typeof deserializedDoc.func); + test.equal(1, deserializedDoc.a); + test.done(); +} + +/** + * A simple example showing the usage of BSON.deserializeStream function returning deserialized Javascript objects. + * + * @_class bson + * @_function BSON.deserializeStream + * @ignore + */ +exports['Should correctly deserializeStream a buffer object'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){ console.log('hello world'); }} + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = BSON.serialize(doc, true, true, true); + // Validate the correctness + test.equal(65, buffer.length); + + // The array holding the number of retuned documents + var documents = new Array(1); + // Deserialize the object with no eval for the functions + var index = BSON.deserializeStream(buffer, 0, 1, documents, 0); + // Validate the correctness + test.equal(65, index); + test.equal(1, documents.length); + test.equal(1, documents[0].a); + test.equal('object', typeof documents[0].func); + + // Deserialize the object with eval for the functions caching the functions + // The array holding the number of retuned documents + var documents = new Array(1); + // Deserialize the object with no eval for the functions + var index = BSON.deserializeStream(buffer, 0, 1, documents, 0, {evalFunctions:true, cacheFunctions:true}); + // Validate the correctness + test.equal(65, index); + test.equal(1, documents.length); + test.equal(1, documents[0].a); + test.equal('function', typeof documents[0].func); + test.done(); +} + +/** + * A simple example showing the usage of BSON instance deserializeStream function returning deserialized Javascript objects. + * + * @_class bson + * @_function deserializeStream + * @ignore + */ +exports['Should correctly deserializeStream a buffer object'] = function(test) { + // Create a simple object + var doc = {a: 1, func:function(){ console.log('hello world'); }} + // Create a BSON parser instance + var bson = new BSON(); + // Serialize the object to a buffer, checking keys and serializing functions + var buffer = bson.serialize(doc, true, true, true); + // Validate the correctness + test.equal(65, buffer.length); + + // The array holding the number of retuned documents + var documents = new Array(1); + // Deserialize the object with no eval for the functions + var index = bson.deserializeStream(buffer, 0, 1, documents, 0); + // Validate the correctness + test.equal(65, index); + test.equal(1, documents.length); + test.equal(1, documents[0].a); + test.equal('object', typeof documents[0].func); + + // Deserialize the object with eval for the functions caching the functions + // The array holding the number of retuned documents + var documents = new Array(1); + // Deserialize the object with no eval for the functions + var index = bson.deserializeStream(buffer, 0, 1, documents, 0, {evalFunctions:true, cacheFunctions:true}); + // Validate the correctness + test.equal(65, index); + test.equal(1, documents.length); + test.equal(1, documents[0].a); + test.equal('function', typeof documents[0].func); + test.done(); +} + +/** + * @ignore + */ +// 'Should Correctly Function' = function(test) { +// var doc = {b:1, func:function() { +// this.b = 2; +// }}; +// +// var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc, false, true); +// +// debug("----------------------------------------------------------------------") +// debug(inspect(serialized_data)) +// +// // var serialized_data2 = new Buffer(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).calculateObjectSize(doc, false, true)); +// // new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serializeWithBufferAndIndex(doc, false, serialized_data2, 0); +// // assertBuffersEqual(test, serialized_data, serialized_data2, 0); +// var COUNT = 100000; +// +// // var b = null; +// // eval("b = function(x) { return x+x; }"); +// // var b = new Function("x", "return x+x;"); +// +// console.log(COUNT + "x (objectBSON = BSON.serialize(object))") +// start = new Date +// +// for (i=COUNT; --i>=0; ) { +// var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data, {evalFunctions: true, cacheFunctions:true}); +// } +// +// end = new Date +// console.log("time = ", end - start, "ms -", COUNT * 1000 / (end - start), " ops/sec") +// +// // debug(inspect(new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).functionCache)) +// // +// // var doc2 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data, {evalFunctions: true, cacheFunctions:true}); +// // // test.deepEqual(doc, doc2) +// // // +// // debug(inspect(doc2)) +// // doc2.func() +// // debug(inspect(doc2)) +// // +// // var serialized_data = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).serialize(doc2, false, true); +// // var doc3 = new BSONSE.BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]).deserialize(serialized_data, {evalFunctions: true, cacheFunctions:true}); +// // +// // debug("-----------------------------------------------") +// // debug(inspect(doc3)) +// +// // var key = "0" +// // for(var i = 1; i < 10000; i++) { +// // key = key + " " + i +// // } +// +// test.done(); +// +// +// // var car = { +// // model : "Volvo", +// // country : "Sweden", +// // +// // isSwedish : function() { +// // return this.country == "Sweden"; +// // } +// // } +// +// }, + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.noGlobalsLeaked = function(test) { + var leaks = gleak.detectNew(); + test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', ')); + test.done(); +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/test/node/bson_typed_array_test.js b/node_modules/mongodb/node_modules/bson/test/node/bson_typed_array_test.js new file mode 100644 index 0000000..cde83f8 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/bson_typed_array_test.js @@ -0,0 +1,392 @@ +var mongodb = require('../../lib/bson').pure(); + +var testCase = require('nodeunit').testCase, + mongoO = require('../../lib/bson').pure(), + debug = require('util').debug, + inspect = require('util').inspect, + Buffer = require('buffer').Buffer, + gleak = require('../../tools/gleak'), + fs = require('fs'), + BSON = mongoO.BSON, + Code = mongoO.Code, + Binary = mongoO.Binary, + Timestamp = mongoO.Timestamp, + Long = mongoO.Long, + MongoReply = mongoO.MongoReply, + ObjectID = mongoO.ObjectID, + Symbol = mongoO.Symbol, + DBRef = mongoO.DBRef, + Double = mongoO.Double, + MinKey = mongoO.MinKey, + MaxKey = mongoO.MaxKey, + BinaryParser = mongoO.BinaryParser, + utils = require('./tools/utils'); + +var BSONSE = mongodb, + BSONDE = mongodb; + +// for tests +BSONDE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONDE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONDE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONDE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONDE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONDE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +BSONSE.BSON_BINARY_SUBTYPE_DEFAULT = 0; +BSONSE.BSON_BINARY_SUBTYPE_FUNCTION = 1; +BSONSE.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2; +BSONSE.BSON_BINARY_SUBTYPE_UUID = 3; +BSONSE.BSON_BINARY_SUBTYPE_MD5 = 4; +BSONSE.BSON_BINARY_SUBTYPE_USER_DEFINED = 128; + +var hexStringToBinary = function(string) { + var numberofValues = string.length / 2; + var array = ""; + + for(var i = 0; i < numberofValues; i++) { + array += String.fromCharCode(parseInt(string[i*2] + string[i*2 + 1], 16)); + } + return array; +} + +var assertBuffersEqual = function(test, buffer1, buffer2) { + if(buffer1.length != buffer2.length) test.fail("Buffers do not have the same length", buffer1, buffer2); + + for(var i = 0; i < buffer1.length; i++) { + test.equal(buffer1[i], buffer2[i]); + } +} + +/** + * Module for parsing an ISO 8601 formatted string into a Date object. + */ +var ISODate = function (string) { + var match; + + if (typeof string.getTime === "function") + return string; + else if (match = string.match(/^(\d{4})(-(\d{2})(-(\d{2})(T(\d{2}):(\d{2})(:(\d{2})(\.(\d+))?)?(Z|((\+|-)(\d{2}):(\d{2}))))?)?)?$/)) { + var date = new Date(); + date.setUTCFullYear(Number(match[1])); + date.setUTCMonth(Number(match[3]) - 1 || 0); + date.setUTCDate(Number(match[5]) || 0); + date.setUTCHours(Number(match[7]) || 0); + date.setUTCMinutes(Number(match[8]) || 0); + date.setUTCSeconds(Number(match[10]) || 0); + date.setUTCMilliseconds(Number("." + match[12]) * 1000 || 0); + + if (match[13] && match[13] !== "Z") { + var h = Number(match[16]) || 0, + m = Number(match[17]) || 0; + + h *= 3600000; + m *= 60000; + + var offset = h + m; + if (match[15] == "+") + offset = -offset; + + date = new Date(date.valueOf() + offset); + } + + return date; + } else + throw new Error("Invalid ISO 8601 date given.", __filename); +}; + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.setUp = function(callback) { + callback(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.tearDown = function(callback) { + callback(); +} + +/** + * @ignore + */ +exports.shouldCorrectlyDeserializeUsingTypedArray = function(test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + var motherOfAllDocuments = { + 'string': '客家话', + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': new Date(), + 'oid': new ObjectID(), + 'binary': new Binary(new Buffer("hello")), + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': Long.fromNumber(100), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', new ObjectID(), 'integration_tests_'), + 'minkey': new MinKey(), + 'maxkey': new MaxKey() + } + + // Let's serialize it + var data = BSONSE.BSON.serialize(motherOfAllDocuments, true, true, false); + // Build a typed array + var arr = new Uint8Array(new ArrayBuffer(data.length)); + // Iterate over all the fields and copy + for(var i = 0; i < data.length; i++) { + arr[i] = data[i] + } + + // Deserialize the object + var object = BSONDE.BSON.deserialize(arr); + // Asserts + test.equal(motherOfAllDocuments.string, object.string); + test.deepEqual(motherOfAllDocuments.array, object.array); + test.deepEqual(motherOfAllDocuments.date, object.date); + test.deepEqual(motherOfAllDocuments.oid.toHexString(), object.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.binary.length(), object.binary.length()); + // Assert the values of the binary + for(var i = 0; i < motherOfAllDocuments.binary.length(); i++) { + test.equal(motherOfAllDocuments.binary.value[i], object.binary[i]); + } + test.deepEqual(motherOfAllDocuments.int, object.int); + test.deepEqual(motherOfAllDocuments.float, object.float); + test.deepEqual(motherOfAllDocuments.regexp, object.regexp); + test.deepEqual(motherOfAllDocuments.boolean, object.boolean); + test.deepEqual(motherOfAllDocuments.long.toNumber(), object.long); + test.deepEqual(motherOfAllDocuments.where, object.where); + test.deepEqual(motherOfAllDocuments.dbref.oid.toHexString(), object.dbref.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.dbref.namespace, object.dbref.namespace); + test.deepEqual(motherOfAllDocuments.dbref.db, object.dbref.db); + test.deepEqual(motherOfAllDocuments.minkey, object.minkey); + test.deepEqual(motherOfAllDocuments.maxkey, object.maxkey); + test.done(); +} + +/** + * @ignore + */ +exports.shouldCorrectlySerializeUsingTypedArray = function(test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + var motherOfAllDocuments = { + 'string': 'hello', + 'array': [1,2,3], + 'hash': {'a':1, 'b':2}, + 'date': new Date(), + 'oid': new ObjectID(), + 'binary': new Binary(new Buffer("hello")), + 'int': 42, + 'float': 33.3333, + 'regexp': /regexp/, + 'boolean': true, + 'long': Long.fromNumber(100), + 'where': new Code('this.a > i', {i:1}), + 'dbref': new DBRef('namespace', new ObjectID(), 'integration_tests_'), + 'minkey': new MinKey(), + 'maxkey': new MaxKey() + } + + // Let's serialize it + var data = BSONSE.BSON.serialize(motherOfAllDocuments, true, false, false); + // And deserialize it again + var object = BSONSE.BSON.deserialize(data); + // Asserts + test.equal(motherOfAllDocuments.string, object.string); + test.deepEqual(motherOfAllDocuments.array, object.array); + test.deepEqual(motherOfAllDocuments.date, object.date); + test.deepEqual(motherOfAllDocuments.oid.toHexString(), object.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.binary.length(), object.binary.length()); + // Assert the values of the binary + for(var i = 0; i < motherOfAllDocuments.binary.length(); i++) { + test.equal(motherOfAllDocuments.binary.value[i], object.binary[i]); + } + test.deepEqual(motherOfAllDocuments.int, object.int); + test.deepEqual(motherOfAllDocuments.float, object.float); + test.deepEqual(motherOfAllDocuments.regexp, object.regexp); + test.deepEqual(motherOfAllDocuments.boolean, object.boolean); + test.deepEqual(motherOfAllDocuments.long.toNumber(), object.long); + test.deepEqual(motherOfAllDocuments.where, object.where); + test.deepEqual(motherOfAllDocuments.dbref.oid.toHexString(), object.dbref.oid.toHexString()); + test.deepEqual(motherOfAllDocuments.dbref.namespace, object.dbref.namespace); + test.deepEqual(motherOfAllDocuments.dbref.db, object.dbref.db); + test.deepEqual(motherOfAllDocuments.minkey, object.minkey); + test.deepEqual(motherOfAllDocuments.maxkey, object.maxkey); + test.done(); +} + +/** + * @ignore + */ +exports['exercise all the binary object constructor methods'] = function (test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + // Construct using array + var string = 'hello world'; + // String to array + var array = utils.stringToArrayBuffer(string); + + // Binary from array buffer + var binary = new Binary(utils.stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + test.ok(utils.assertArrayEqual(array, binary.buffer)); + + // Construct using number of chars + binary = new Binary(5); + test.ok(5, binary.buffer.length); + + // Construct using an Array + var binary = new Binary(utils.stringToArray(string)); + test.ok(string.length, binary.buffer.length); + test.ok(utils.assertArrayEqual(array, binary.buffer)); + + // Construct using a string + var binary = new Binary(string); + test.ok(string.length, binary.buffer.length); + test.ok(utils.assertArrayEqual(array, binary.buffer)); + test.done(); +}; + +/** + * @ignore + */ +exports['exercise the put binary object method for an instance when using Uint8Array'] = function (test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + // Construct using array + var string = 'hello world'; + // String to array + var array = utils.stringToArrayBuffer(string + 'a'); + + // Binary from array buffer + var binary = new Binary(utils.stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Write a byte to the array + binary.put('a') + + // Verify that the data was writtencorrectly + test.equal(string.length + 1, binary.position); + test.ok(utils.assertArrayEqual(array, binary.value(true))); + test.equal('hello worlda', binary.value()); + + // Exercise a binary with lots of space in the buffer + var binary = new Binary(); + test.ok(Binary.BUFFER_SIZE, binary.buffer.length); + + // Write a byte to the array + binary.put('a') + + // Verify that the data was writtencorrectly + test.equal(1, binary.position); + test.ok(utils.assertArrayEqual(['a'.charCodeAt(0)], binary.value(true))); + test.equal('a', binary.value()); + test.done(); +}, + +/** + * @ignore + */ +exports['exercise the write binary object method for an instance when using Uint8Array'] = function (test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + // Construct using array + var string = 'hello world'; + // Array + var writeArrayBuffer = new Uint8Array(new ArrayBuffer(1)); + writeArrayBuffer[0] = 'a'.charCodeAt(0); + var arrayBuffer = ['a'.charCodeAt(0)]; + + // Binary from array buffer + var binary = new Binary(utils.stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Write a string starting at end of buffer + binary.write('a'); + test.equal('hello worlda', binary.value()); + // Write a string starting at index 0 + binary.write('a', 0); + test.equal('aello worlda', binary.value()); + // Write a arraybuffer starting at end of buffer + binary.write(writeArrayBuffer); + test.equal('aello worldaa', binary.value()); + // Write a arraybuffer starting at position 5 + binary.write(writeArrayBuffer, 5); + test.equal('aelloaworldaa', binary.value()); + // Write a array starting at end of buffer + binary.write(arrayBuffer); + test.equal('aelloaworldaaa', binary.value()); + // Write a array starting at position 6 + binary.write(arrayBuffer, 6); + test.equal('aelloaaorldaaa', binary.value()); + test.done(); +}, + +/** + * @ignore + */ +exports['exercise the read binary object method for an instance when using Uint8Array'] = function (test) { + if(typeof ArrayBuffer == 'undefined') { + test.done(); + return; + } + + // Construct using array + var string = 'hello world'; + var array = utils.stringToArrayBuffer(string); + + // Binary from array buffer + var binary = new Binary(utils.stringToArrayBuffer(string)); + test.ok(string.length, binary.buffer.length); + + // Read the first 2 bytes + var data = binary.read(0, 2); + test.ok(utils.assertArrayEqual(utils.stringToArrayBuffer('he'), data)); + + // Read the entire field + var data = binary.read(0); + test.ok(utils.assertArrayEqual(utils.stringToArrayBuffer(string), data)); + + // Read 3 bytes + var data = binary.read(6, 5); + test.ok(utils.assertArrayEqual(utils.stringToArrayBuffer('world'), data)); + test.done(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.noGlobalsLeaked = function(test) { + var leaks = gleak.detectNew(); + test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', ')); + test.done(); +}
\ No newline at end of file diff --git a/node_modules/mongodb/node_modules/bson/test/node/data/test_gs_weird_bug.png b/node_modules/mongodb/node_modules/bson/test/node/data/test_gs_weird_bug.png Binary files differnew file mode 100644 index 0000000..1554dc3 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/data/test_gs_weird_bug.png diff --git a/node_modules/mongodb/node_modules/bson/test/node/test_full_bson.js b/node_modules/mongodb/node_modules/bson/test/node/test_full_bson.js new file mode 100644 index 0000000..fbeeb66 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/test_full_bson.js @@ -0,0 +1,295 @@ +var sys = require('util'), + fs = require('fs'), + Buffer = require('buffer').Buffer, + BSON = require('../../ext/bson').BSON, + Buffer = require('buffer').Buffer, + BSONJS = require('../../lib/bson/bson').BSON, + BinaryParser = require('../../lib/bson/binary_parser').BinaryParser, + Long = require('../../lib/bson/long').Long, + ObjectID = require('../../lib/bson/bson').ObjectID, + Binary = require('../../lib/bson/bson').Binary, + Code = require('../../lib/bson/bson').Code, + DBRef = require('../../lib/bson/bson').DBRef, + Symbol = require('../../lib/bson/bson').Symbol, + Double = require('../../lib/bson/bson').Double, + MaxKey = require('../../lib/bson/bson').MaxKey, + MinKey = require('../../lib/bson/bson').MinKey, + Timestamp = require('../../lib/bson/bson').Timestamp, + assert = require('assert'); + +// Parsers +var bsonC = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); +var bsonJS = new BSONJS([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]); + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.setUp = function(callback) { + callback(); +} + +/** + * Retrieve the server information for the current + * instance of the db client + * + * @ignore + */ +exports.tearDown = function(callback) { + callback(); +} + +/** + * @ignore + */ +exports['Should Correctly Deserialize object'] = function(test) { + var bytes = [95,0,0,0,2,110,115,0,42,0,0,0,105,110,116,101,103,114,97,116,105,111,110,95,116,101,115,116,115,95,46,116,101,115,116,95,105,110,100,101,120,95,105,110,102,111,114,109,97,116,105,111,110,0,8,117,110,105,113,117,101,0,0,3,107,101,121,0,12,0,0,0,16,97,0,1,0,0,0,0,2,110,97,109,101,0,4,0,0,0,97,95,49,0,0]; + var serialized_data = ''; + // Convert to chars + for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); + } + + var object = bsonC.deserialize(serialized_data); + assert.equal("a_1", object.name); + assert.equal(false, object.unique); + assert.equal(1, object.key.a); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Deserialize object with all types'] = function(test) { + var bytes = [26,1,0,0,7,95,105,100,0,161,190,98,75,118,169,3,0,0,3,0,0,4,97,114,114,97,121,0,26,0,0,0,16,48,0,1,0,0,0,16,49,0,2,0,0,0,16,50,0,3,0,0,0,0,2,115,116,114,105,110,103,0,6,0,0,0,104,101,108,108,111,0,3,104,97,115,104,0,19,0,0,0,16,97,0,1,0,0,0,16,98,0,2,0,0,0,0,9,100,97,116,101,0,161,190,98,75,0,0,0,0,7,111,105,100,0,161,190,98,75,90,217,18,0,0,1,0,0,5,98,105,110,97,114,121,0,7,0,0,0,2,3,0,0,0,49,50,51,16,105,110,116,0,42,0,0,0,1,102,108,111,97,116,0,223,224,11,147,169,170,64,64,11,114,101,103,101,120,112,0,102,111,111,98,97,114,0,105,0,8,98,111,111,108,101,97,110,0,1,15,119,104,101,114,101,0,25,0,0,0,12,0,0,0,116,104,105,115,46,120,32,61,61,32,51,0,5,0,0,0,0,3,100,98,114,101,102,0,37,0,0,0,2,36,114,101,102,0,5,0,0,0,116,101,115,116,0,7,36,105,100,0,161,190,98,75,2,180,1,0,0,2,0,0,0,10,110,117,108,108,0,0]; + var serialized_data = ''; + // Convert to chars + for(var i = 0; i < bytes.length; i++) { + serialized_data = serialized_data + BinaryParser.fromByte(bytes[i]); + } + + var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); + assert.equal("hello", object.string); + assert.deepEqual([1, 2, 3], object.array); + assert.equal(1, object.hash.a); + assert.equal(2, object.hash.b); + assert.ok(object.date != null); + assert.ok(object.oid != null); + assert.ok(object.binary != null); + assert.equal(42, object.int); + assert.equal(33.3333, object.float); + assert.ok(object.regexp != null); + assert.equal(true, object.boolean); + assert.ok(object.where != null); + assert.ok(object.dbref != null); + assert.ok(object['null'] == null); + test.done(); +} + +/** + * @ignore + */ +exports['Should Serialize and Deserialize String'] = function(test) { + var test_string = {hello: 'world'} + var serialized_data = bsonC.serialize(test_string) + assert.deepEqual(test_string, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Integer'] = function(test) { + var test_number = {doc: 5} + var serialized_data = bsonC.serialize(test_number) + assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize null value'] = function(test) { + var test_null = {doc:null} + var serialized_data = bsonC.serialize(test_null) + var object = bsonC.deserialize(serialized_data); + assert.deepEqual(test_null, object); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize undefined value'] = function(test) { + var test_undefined = {doc:undefined} + var serialized_data = bsonC.serialize(test_undefined) + var object = bsonJS.deserialize(new Buffer(serialized_data, 'binary')); + assert.equal(null, object.doc) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Number'] = function(test) { + var test_number = {doc: 5.5} + var serialized_data = bsonC.serialize(test_number) + assert.deepEqual(test_number, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Integer'] = function(test) { + var test_int = {doc: 42} + var serialized_data = bsonC.serialize(test_int) + assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + + test_int = {doc: -5600} + serialized_data = bsonC.serialize(test_int) + assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + + test_int = {doc: 2147483647} + serialized_data = bsonC.serialize(test_int) + assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + + test_int = {doc: -2147483648} + serialized_data = bsonC.serialize(test_int) + assert.deepEqual(test_int, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Object'] = function(test) { + var doc = {doc: {age: 42, name: 'Spongebob', shoe_size: 9.5}} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Array'] = function(test) { + var doc = {doc: [1, 2, 'a', 'b']} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Array with added on functions'] = function(test) { + var doc = {doc: [1, 2, 'a', 'b']} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize A Boolean'] = function(test) { + var doc = {doc: true} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a Date'] = function(test) { + var date = new Date() + //(2009, 11, 12, 12, 00, 30) + date.setUTCDate(12) + date.setUTCFullYear(2009) + date.setUTCMonth(11 - 1) + date.setUTCHours(12) + date.setUTCMinutes(0) + date.setUTCSeconds(30) + var doc = {doc: date} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Oid'] = function(test) { + var doc = {doc: new ObjectID()} + var serialized_data = bsonC.serialize(doc) + assert.deepEqual(doc.doc.toHexString(), bsonC.deserialize(serialized_data).doc.toHexString()) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly encode Empty Hash'] = function(test) { + var test_code = {} + var serialized_data = bsonC.serialize(test_code) + assert.deepEqual(test_code, bsonC.deserialize(serialized_data)); + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Ordered Hash'] = function(test) { + var doc = {doc: {b:1, a:2, c:3, d:4}} + var serialized_data = bsonC.serialize(doc) + var decoded_hash = bsonC.deserialize(serialized_data).doc + var keys = [] + for(name in decoded_hash) keys.push(name) + assert.deepEqual(['b', 'a', 'c', 'd'], keys) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize Regular Expression'] = function(test) { + var doc = {doc: /foobar/mi} + var serialized_data = bsonC.serialize(doc) + var doc2 = bsonC.deserialize(serialized_data); + assert.equal(doc.doc.toString(), doc2.doc.toString()) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a Binary object'] = function(test) { + var bin = new Binary() + var string = 'binstring' + for(var index = 0; index < string.length; index++) { + bin.put(string.charAt(index)) + } + var doc = {doc: bin} + var serialized_data = bsonC.serialize(doc) + var deserialized_data = bsonC.deserialize(serialized_data); + assert.equal(doc.doc.value(), deserialized_data.doc.value()) + test.done(); +} + +/** + * @ignore + */ +exports['Should Correctly Serialize and Deserialize a big Binary object'] = function(test) { + var data = fs.readFileSync("test/node/data/test_gs_weird_bug.png", 'binary'); + var bin = new Binary() + bin.write(data) + var doc = {doc: bin} + var serialized_data = bsonC.serialize(doc) + var deserialized_data = bsonC.deserialize(serialized_data); + assert.equal(doc.doc.value(), deserialized_data.doc.value()) + test.done(); +} diff --git a/node_modules/mongodb/node_modules/bson/test/node/tools/utils.js b/node_modules/mongodb/node_modules/bson/test/node/tools/utils.js new file mode 100644 index 0000000..9d7cbe7 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/test/node/tools/utils.js @@ -0,0 +1,80 @@ +exports.assertArrayEqual = function(array1, array2) { + if(array1.length != array2.length) return false; + for(var i = 0; i < array1.length; i++) { + if(array1[i] != array2[i]) return false; + } + + return true; +} + +// String to arraybuffer +exports.stringToArrayBuffer = function(string) { + var dataBuffer = new Uint8Array(new ArrayBuffer(string.length)); + // Return the strings + for(var i = 0; i < string.length; i++) { + dataBuffer[i] = string.charCodeAt(i); + } + // Return the data buffer + return dataBuffer; +} + +// String to arraybuffer +exports.stringToArray = function(string) { + var dataBuffer = new Array(string.length); + // Return the strings + for(var i = 0; i < string.length; i++) { + dataBuffer[i] = string.charCodeAt(i); + } + // Return the data buffer + return dataBuffer; +} + +exports.Utf8 = { + // public method for url encoding + encode : function (string) { + string = string.replace(/\r\n/g,"\n"); + var utftext = ""; + + for (var n = 0; n < string.length; n++) { + var c = string.charCodeAt(n); + if (c < 128) { + utftext += String.fromCharCode(c); + } else if((c > 127) && (c < 2048)) { + utftext += String.fromCharCode((c >> 6) | 192); + utftext += String.fromCharCode((c & 63) | 128); + } else { + utftext += String.fromCharCode((c >> 12) | 224); + utftext += String.fromCharCode(((c >> 6) & 63) | 128); + utftext += String.fromCharCode((c & 63) | 128); + } + + } + + return utftext; + }, + + // public method for url decoding + decode : function (utftext) { + var string = ""; + var i = 0; + var c = c1 = c2 = 0; + + while ( i < utftext.length ) { + c = utftext.charCodeAt(i); + if(c < 128) { + string += String.fromCharCode(c); + i++; + } else if((c > 191) && (c < 224)) { + c2 = utftext.charCodeAt(i+1); + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } else { + c2 = utftext.charCodeAt(i+1); + c3 = utftext.charCodeAt(i+2); + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + } + return string; + } +} diff --git a/node_modules/mongodb/node_modules/bson/tools/gleak.js b/node_modules/mongodb/node_modules/bson/tools/gleak.js new file mode 100644 index 0000000..1737a6d --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/gleak.js @@ -0,0 +1,8 @@ + +var gleak = require('gleak')(); +gleak.ignore('AssertionError'); +gleak.ignore('testFullSpec_param_found'); +gleak.ignore('events'); +gleak.ignore('Uint8Array'); + +module.exports = gleak; diff --git a/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/MIT.LICENSE b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/MIT.LICENSE new file mode 100644 index 0000000..7c435ba --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/MIT.LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2008-2011 Pivotal Labs + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine-html.js b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine-html.js new file mode 100644 index 0000000..7383401 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine-html.js @@ -0,0 +1,190 @@ +jasmine.TrivialReporter = function(doc) { + this.document = doc || document; + this.suiteDivs = {}; + this.logRunningSpecs = false; +}; + +jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) { + var el = document.createElement(type); + + for (var i = 2; i < arguments.length; i++) { + var child = arguments[i]; + + if (typeof child === 'string') { + el.appendChild(document.createTextNode(child)); + } else { + if (child) { el.appendChild(child); } + } + } + + for (var attr in attrs) { + if (attr == "className") { + el[attr] = attrs[attr]; + } else { + el.setAttribute(attr, attrs[attr]); + } + } + + return el; +}; + +jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) { + var showPassed, showSkipped; + + this.outerDiv = this.createDom('div', { className: 'jasmine_reporter' }, + this.createDom('div', { className: 'banner' }, + this.createDom('div', { className: 'logo' }, + this.createDom('span', { className: 'title' }, "Jasmine"), + this.createDom('span', { className: 'version' }, runner.env.versionString())), + this.createDom('div', { className: 'options' }, + "Show ", + showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }), + this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "), + showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }), + this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped") + ) + ), + + this.runnerDiv = this.createDom('div', { className: 'runner running' }, + this.createDom('a', { className: 'run_spec', href: '?' }, "run all"), + this.runnerMessageSpan = this.createDom('span', {}, "Running..."), + this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, "")) + ); + + this.document.body.appendChild(this.outerDiv); + + var suites = runner.suites(); + for (var i = 0; i < suites.length; i++) { + var suite = suites[i]; + var suiteDiv = this.createDom('div', { className: 'suite' }, + this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"), + this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description)); + this.suiteDivs[suite.id] = suiteDiv; + var parentDiv = this.outerDiv; + if (suite.parentSuite) { + parentDiv = this.suiteDivs[suite.parentSuite.id]; + } + parentDiv.appendChild(suiteDiv); + } + + this.startedAt = new Date(); + + var self = this; + showPassed.onclick = function(evt) { + if (showPassed.checked) { + self.outerDiv.className += ' show-passed'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, ''); + } + }; + + showSkipped.onclick = function(evt) { + if (showSkipped.checked) { + self.outerDiv.className += ' show-skipped'; + } else { + self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, ''); + } + }; +}; + +jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) { + var results = runner.results(); + var className = (results.failedCount > 0) ? "runner failed" : "runner passed"; + this.runnerDiv.setAttribute("class", className); + //do it twice for IE + this.runnerDiv.setAttribute("className", className); + var specs = runner.specs(); + var specCount = 0; + for (var i = 0; i < specs.length; i++) { + if (this.specFilter(specs[i])) { + specCount++; + } + } + var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s"); + message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s"; + this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild); + + this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString())); +}; + +jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) { + var results = suite.results(); + var status = results.passed() ? 'passed' : 'failed'; + if (results.totalCount === 0) { // todo: change this to check results.skipped + status = 'skipped'; + } + this.suiteDivs[suite.id].className += " " + status; +}; + +jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) { + if (this.logRunningSpecs) { + this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...'); + } +}; + +jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) { + var results = spec.results(); + var status = results.passed() ? 'passed' : 'failed'; + if (results.skipped) { + status = 'skipped'; + } + var specDiv = this.createDom('div', { className: 'spec ' + status }, + this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"), + this.createDom('a', { + className: 'description', + href: '?spec=' + encodeURIComponent(spec.getFullName()), + title: spec.getFullName() + }, spec.description)); + + + var resultItems = results.getItems(); + var messagesDiv = this.createDom('div', { className: 'messages' }); + for (var i = 0; i < resultItems.length; i++) { + var result = resultItems[i]; + + if (result.type == 'log') { + messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString())); + } else if (result.type == 'expect' && result.passed && !result.passed()) { + messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message)); + + if (result.trace.stack) { + messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); + } + } + } + + if (messagesDiv.childNodes.length > 0) { + specDiv.appendChild(messagesDiv); + } + + this.suiteDivs[spec.suite.id].appendChild(specDiv); +}; + +jasmine.TrivialReporter.prototype.log = function() { + var console = jasmine.getGlobal().console; + if (console && console.log) { + if (console.log.apply) { + console.log.apply(console, arguments); + } else { + console.log(arguments); // ie fix: console.log.apply doesn't exist on ie + } + } +}; + +jasmine.TrivialReporter.prototype.getLocation = function() { + return this.document.location; +}; + +jasmine.TrivialReporter.prototype.specFilter = function(spec) { + var paramMap = {}; + var params = this.getLocation().search.substring(1).split('&'); + for (var i = 0; i < params.length; i++) { + var p = params[i].split('='); + paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); + } + + if (!paramMap.spec) { + return true; + } + return spec.getFullName().indexOf(paramMap.spec) === 0; +}; diff --git a/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.css b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.css new file mode 100644 index 0000000..6583fe7 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.css @@ -0,0 +1,166 @@ +body { + font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; +} + + +.jasmine_reporter a:visited, .jasmine_reporter a { + color: #303; +} + +.jasmine_reporter a:hover, .jasmine_reporter a:active { + color: blue; +} + +.run_spec { + float:right; + padding-right: 5px; + font-size: .8em; + text-decoration: none; +} + +.jasmine_reporter { + margin: 0 5px; +} + +.banner { + color: #303; + background-color: #fef; + padding: 5px; +} + +.logo { + float: left; + font-size: 1.1em; + padding-left: 5px; +} + +.logo .version { + font-size: .6em; + padding-left: 1em; +} + +.runner.running { + background-color: yellow; +} + + +.options { + text-align: right; + font-size: .8em; +} + + + + +.suite { + border: 1px outset gray; + margin: 5px 0; + padding-left: 1em; +} + +.suite .suite { + margin: 5px; +} + +.suite.passed { + background-color: #dfd; +} + +.suite.failed { + background-color: #fdd; +} + +.spec { + margin: 5px; + padding-left: 1em; + clear: both; +} + +.spec.failed, .spec.passed, .spec.skipped { + padding-bottom: 5px; + border: 1px solid gray; +} + +.spec.failed { + background-color: #fbb; + border-color: red; +} + +.spec.passed { + background-color: #bfb; + border-color: green; +} + +.spec.skipped { + background-color: #bbb; +} + +.messages { + border-left: 1px dashed gray; + padding-left: 1em; + padding-right: 1em; +} + +.passed { + background-color: #cfc; + display: none; +} + +.failed { + background-color: #fbb; +} + +.skipped { + color: #777; + background-color: #eee; + display: none; +} + + +/*.resultMessage {*/ + /*white-space: pre;*/ +/*}*/ + +.resultMessage span.result { + display: block; + line-height: 2em; + color: black; +} + +.resultMessage .mismatch { + color: black; +} + +.stackTrace { + white-space: pre; + font-size: .8em; + margin-left: 10px; + max-height: 5em; + overflow: auto; + border: 1px inset red; + padding: 1em; + background: #eef; +} + +.finished-at { + padding-left: 1em; + font-size: .6em; +} + +.show-passed .passed, +.show-skipped .skipped { + display: block; +} + + +#jasmine_content { + position:fixed; + right: 100%; +} + +.runner { + border: 1px solid gray; + display: block; + margin: 5px 0; + padding: 2px 0 2px 10px; +} diff --git a/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.js b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.js new file mode 100644 index 0000000..c3d2dc7 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine.js @@ -0,0 +1,2476 @@ +var isCommonJS = typeof window == "undefined"; + +/** + * Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework. + * + * @namespace + */ +var jasmine = {}; +if (isCommonJS) exports.jasmine = jasmine; +/** + * @private + */ +jasmine.unimplementedMethod_ = function() { + throw new Error("unimplemented method"); +}; + +/** + * Use <code>jasmine.undefined</code> instead of <code>undefined</code>, since <code>undefined</code> is just + * a plain old variable and may be redefined by somebody else. + * + * @private + */ +jasmine.undefined = jasmine.___undefined___; + +/** + * Show diagnostic messages in the console if set to true + * + */ +jasmine.VERBOSE = false; + +/** + * Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. + * + */ +jasmine.DEFAULT_UPDATE_INTERVAL = 250; + +/** + * Default timeout interval in milliseconds for waitsFor() blocks. + */ +jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; + +jasmine.getGlobal = function() { + function getGlobal() { + return this; + } + + return getGlobal(); +}; + +/** + * Allows for bound functions to be compared. Internal use only. + * + * @ignore + * @private + * @param base {Object} bound 'this' for the function + * @param name {Function} function to find + */ +jasmine.bindOriginal_ = function(base, name) { + var original = base[name]; + if (original.apply) { + return function() { + return original.apply(base, arguments); + }; + } else { + // IE support + return jasmine.getGlobal()[name]; + } +}; + +jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); +jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); +jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); +jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); + +jasmine.MessageResult = function(values) { + this.type = 'log'; + this.values = values; + this.trace = new Error(); // todo: test better +}; + +jasmine.MessageResult.prototype.toString = function() { + var text = ""; + for (var i = 0; i < this.values.length; i++) { + if (i > 0) text += " "; + if (jasmine.isString_(this.values[i])) { + text += this.values[i]; + } else { + text += jasmine.pp(this.values[i]); + } + } + return text; +}; + +jasmine.ExpectationResult = function(params) { + this.type = 'expect'; + this.matcherName = params.matcherName; + this.passed_ = params.passed; + this.expected = params.expected; + this.actual = params.actual; + this.message = this.passed_ ? 'Passed.' : params.message; + + var trace = (params.trace || new Error(this.message)); + this.trace = this.passed_ ? '' : trace; +}; + +jasmine.ExpectationResult.prototype.toString = function () { + return this.message; +}; + +jasmine.ExpectationResult.prototype.passed = function () { + return this.passed_; +}; + +/** + * Getter for the Jasmine environment. Ensures one gets created + */ +jasmine.getEnv = function() { + var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(); + return env; +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isArray_ = function(value) { + return jasmine.isA_("Array", value); +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isString_ = function(value) { + return jasmine.isA_("String", value); +}; + +/** + * @ignore + * @private + * @param value + * @returns {Boolean} + */ +jasmine.isNumber_ = function(value) { + return jasmine.isA_("Number", value); +}; + +/** + * @ignore + * @private + * @param {String} typeName + * @param value + * @returns {Boolean} + */ +jasmine.isA_ = function(typeName, value) { + return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; +}; + +/** + * Pretty printer for expecations. Takes any object and turns it into a human-readable string. + * + * @param value {Object} an object to be outputted + * @returns {String} + */ +jasmine.pp = function(value) { + var stringPrettyPrinter = new jasmine.StringPrettyPrinter(); + stringPrettyPrinter.format(value); + return stringPrettyPrinter.string; +}; + +/** + * Returns true if the object is a DOM Node. + * + * @param {Object} obj object to check + * @returns {Boolean} + */ +jasmine.isDomNode = function(obj) { + return obj.nodeType > 0; +}; + +/** + * Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter. + * + * @example + * // don't care about which function is passed in, as long as it's a function + * expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function)); + * + * @param {Class} clazz + * @returns matchable object of the type clazz + */ +jasmine.any = function(clazz) { + return new jasmine.Matchers.Any(clazz); +}; + +/** + * Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. + * + * Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine + * expectation syntax. Spies can be checked if they were called or not and what the calling params were. + * + * A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). + * + * Spies are torn down at the end of every spec. + * + * Note: Do <b>not</b> call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj. + * + * @example + * // a stub + * var myStub = jasmine.createSpy('myStub'); // can be used anywhere + * + * // spy example + * var foo = { + * not: function(bool) { return !bool; } + * } + * + * // actual foo.not will not be called, execution stops + * spyOn(foo, 'not'); + + // foo.not spied upon, execution will continue to implementation + * spyOn(foo, 'not').andCallThrough(); + * + * // fake example + * var foo = { + * not: function(bool) { return !bool; } + * } + * + * // foo.not(val) will return val + * spyOn(foo, 'not').andCallFake(function(value) {return value;}); + * + * // mock example + * foo.not(7 == 7); + * expect(foo.not).toHaveBeenCalled(); + * expect(foo.not).toHaveBeenCalledWith(true); + * + * @constructor + * @see spyOn, jasmine.createSpy, jasmine.createSpyObj + * @param {String} name + */ +jasmine.Spy = function(name) { + /** + * The name of the spy, if provided. + */ + this.identity = name || 'unknown'; + /** + * Is this Object a spy? + */ + this.isSpy = true; + /** + * The actual function this spy stubs. + */ + this.plan = function() { + }; + /** + * Tracking of the most recent call to the spy. + * @example + * var mySpy = jasmine.createSpy('foo'); + * mySpy(1, 2); + * mySpy.mostRecentCall.args = [1, 2]; + */ + this.mostRecentCall = {}; + + /** + * Holds arguments for each call to the spy, indexed by call count + * @example + * var mySpy = jasmine.createSpy('foo'); + * mySpy(1, 2); + * mySpy(7, 8); + * mySpy.mostRecentCall.args = [7, 8]; + * mySpy.argsForCall[0] = [1, 2]; + * mySpy.argsForCall[1] = [7, 8]; + */ + this.argsForCall = []; + this.calls = []; +}; + +/** + * Tells a spy to call through to the actual implemenatation. + * + * @example + * var foo = { + * bar: function() { // do some stuff } + * } + * + * // defining a spy on an existing property: foo.bar + * spyOn(foo, 'bar').andCallThrough(); + */ +jasmine.Spy.prototype.andCallThrough = function() { + this.plan = this.originalValue; + return this; +}; + +/** + * For setting the return value of a spy. + * + * @example + * // defining a spy from scratch: foo() returns 'baz' + * var foo = jasmine.createSpy('spy on foo').andReturn('baz'); + * + * // defining a spy on an existing property: foo.bar() returns 'baz' + * spyOn(foo, 'bar').andReturn('baz'); + * + * @param {Object} value + */ +jasmine.Spy.prototype.andReturn = function(value) { + this.plan = function() { + return value; + }; + return this; +}; + +/** + * For throwing an exception when a spy is called. + * + * @example + * // defining a spy from scratch: foo() throws an exception w/ message 'ouch' + * var foo = jasmine.createSpy('spy on foo').andThrow('baz'); + * + * // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch' + * spyOn(foo, 'bar').andThrow('baz'); + * + * @param {String} exceptionMsg + */ +jasmine.Spy.prototype.andThrow = function(exceptionMsg) { + this.plan = function() { + throw exceptionMsg; + }; + return this; +}; + +/** + * Calls an alternate implementation when a spy is called. + * + * @example + * var baz = function() { + * // do some stuff, return something + * } + * // defining a spy from scratch: foo() calls the function baz + * var foo = jasmine.createSpy('spy on foo').andCall(baz); + * + * // defining a spy on an existing property: foo.bar() calls an anonymnous function + * spyOn(foo, 'bar').andCall(function() { return 'baz';} ); + * + * @param {Function} fakeFunc + */ +jasmine.Spy.prototype.andCallFake = function(fakeFunc) { + this.plan = fakeFunc; + return this; +}; + +/** + * Resets all of a spy's the tracking variables so that it can be used again. + * + * @example + * spyOn(foo, 'bar'); + * + * foo.bar(); + * + * expect(foo.bar.callCount).toEqual(1); + * + * foo.bar.reset(); + * + * expect(foo.bar.callCount).toEqual(0); + */ +jasmine.Spy.prototype.reset = function() { + this.wasCalled = false; + this.callCount = 0; + this.argsForCall = []; + this.calls = []; + this.mostRecentCall = {}; +}; + +jasmine.createSpy = function(name) { + + var spyObj = function() { + spyObj.wasCalled = true; + spyObj.callCount++; + var args = jasmine.util.argsToArray(arguments); + spyObj.mostRecentCall.object = this; + spyObj.mostRecentCall.args = args; + spyObj.argsForCall.push(args); + spyObj.calls.push({object: this, args: args}); + return spyObj.plan.apply(this, arguments); + }; + + var spy = new jasmine.Spy(name); + + for (var prop in spy) { + spyObj[prop] = spy[prop]; + } + + spyObj.reset(); + + return spyObj; +}; + +/** + * Determines whether an object is a spy. + * + * @param {jasmine.Spy|Object} putativeSpy + * @returns {Boolean} + */ +jasmine.isSpy = function(putativeSpy) { + return putativeSpy && putativeSpy.isSpy; +}; + +/** + * Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something + * large in one call. + * + * @param {String} baseName name of spy class + * @param {Array} methodNames array of names of methods to make spies + */ +jasmine.createSpyObj = function(baseName, methodNames) { + if (!jasmine.isArray_(methodNames) || methodNames.length === 0) { + throw new Error('createSpyObj requires a non-empty array of method names to create spies for'); + } + var obj = {}; + for (var i = 0; i < methodNames.length; i++) { + obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]); + } + return obj; +}; + +/** + * All parameters are pretty-printed and concatenated together, then written to the current spec's output. + * + * Be careful not to leave calls to <code>jasmine.log</code> in production code. + */ +jasmine.log = function() { + var spec = jasmine.getEnv().currentSpec; + spec.log.apply(spec, arguments); +}; + +/** + * Function that installs a spy on an existing object's method name. Used within a Spec to create a spy. + * + * @example + * // spy example + * var foo = { + * not: function(bool) { return !bool; } + * } + * spyOn(foo, 'not'); // actual foo.not will not be called, execution stops + * + * @see jasmine.createSpy + * @param obj + * @param methodName + * @returns a Jasmine spy that can be chained with all spy methods + */ +var spyOn = function(obj, methodName) { + return jasmine.getEnv().currentSpec.spyOn(obj, methodName); +}; +if (isCommonJS) exports.spyOn = spyOn; + +/** + * Creates a Jasmine spec that will be added to the current suite. + * + * // TODO: pending tests + * + * @example + * it('should be true', function() { + * expect(true).toEqual(true); + * }); + * + * @param {String} desc description of this specification + * @param {Function} func defines the preconditions and expectations of the spec + */ +var it = function(desc, func) { + return jasmine.getEnv().it(desc, func); +}; +if (isCommonJS) exports.it = it; + +/** + * Creates a <em>disabled</em> Jasmine spec. + * + * A convenience method that allows existing specs to be disabled temporarily during development. + * + * @param {String} desc description of this specification + * @param {Function} func defines the preconditions and expectations of the spec + */ +var xit = function(desc, func) { + return jasmine.getEnv().xit(desc, func); +}; +if (isCommonJS) exports.xit = xit; + +/** + * Starts a chain for a Jasmine expectation. + * + * It is passed an Object that is the actual value and should chain to one of the many + * jasmine.Matchers functions. + * + * @param {Object} actual Actual value to test against and expected value + */ +var expect = function(actual) { + return jasmine.getEnv().currentSpec.expect(actual); +}; +if (isCommonJS) exports.expect = expect; + +/** + * Defines part of a jasmine spec. Used in cominbination with waits or waitsFor in asynchrnous specs. + * + * @param {Function} func Function that defines part of a jasmine spec. + */ +var runs = function(func) { + jasmine.getEnv().currentSpec.runs(func); +}; +if (isCommonJS) exports.runs = runs; + +/** + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait + */ +var waits = function(timeout) { + jasmine.getEnv().currentSpec.waits(timeout); +}; +if (isCommonJS) exports.waits = waits; + +/** + * Waits for the latchFunction to return true before proceeding to the next block. + * + * @param {Function} latchFunction + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout + */ +var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments); +}; +if (isCommonJS) exports.waitsFor = waitsFor; + +/** + * A function that is called before each spec in a suite. + * + * Used for spec setup, including validating assumptions. + * + * @param {Function} beforeEachFunction + */ +var beforeEach = function(beforeEachFunction) { + jasmine.getEnv().beforeEach(beforeEachFunction); +}; +if (isCommonJS) exports.beforeEach = beforeEach; + +/** + * A function that is called after each spec in a suite. + * + * Used for restoring any state that is hijacked during spec execution. + * + * @param {Function} afterEachFunction + */ +var afterEach = function(afterEachFunction) { + jasmine.getEnv().afterEach(afterEachFunction); +}; +if (isCommonJS) exports.afterEach = afterEach; + +/** + * Defines a suite of specifications. + * + * Stores the description and all defined specs in the Jasmine environment as one suite of specs. Variables declared + * are accessible by calls to beforeEach, it, and afterEach. Describe blocks can be nested, allowing for specialization + * of setup in some tests. + * + * @example + * // TODO: a simple suite + * + * // TODO: a simple suite with a nested describe block + * + * @param {String} description A string, usually the class under test. + * @param {Function} specDefinitions function that defines several specs. + */ +var describe = function(description, specDefinitions) { + return jasmine.getEnv().describe(description, specDefinitions); +}; +if (isCommonJS) exports.describe = describe; + +/** + * Disables a suite of specifications. Used to disable some suites in a file, or files, temporarily during development. + * + * @param {String} description A string, usually the class under test. + * @param {Function} specDefinitions function that defines several specs. + */ +var xdescribe = function(description, specDefinitions) { + return jasmine.getEnv().xdescribe(description, specDefinitions); +}; +if (isCommonJS) exports.xdescribe = xdescribe; + + +// Provide the XMLHttpRequest class for IE 5.x-6.x: +jasmine.XmlHttpRequest = (typeof XMLHttpRequest == "undefined") ? function() { + function tryIt(f) { + try { + return f(); + } catch(e) { + } + return null; + } + + var xhr = tryIt(function() { + return new ActiveXObject("Msxml2.XMLHTTP.6.0"); + }) || + tryIt(function() { + return new ActiveXObject("Msxml2.XMLHTTP.3.0"); + }) || + tryIt(function() { + return new ActiveXObject("Msxml2.XMLHTTP"); + }) || + tryIt(function() { + return new ActiveXObject("Microsoft.XMLHTTP"); + }); + + if (!xhr) throw new Error("This browser does not support XMLHttpRequest."); + + return xhr; +} : XMLHttpRequest; +/** + * @namespace + */ +jasmine.util = {}; + +/** + * Declare that a child class inherit it's prototype from the parent class. + * + * @private + * @param {Function} childClass + * @param {Function} parentClass + */ +jasmine.util.inherit = function(childClass, parentClass) { + /** + * @private + */ + var subclass = function() { + }; + subclass.prototype = parentClass.prototype; + childClass.prototype = new subclass(); +}; + +jasmine.util.formatException = function(e) { + var lineNumber; + if (e.line) { + lineNumber = e.line; + } + else if (e.lineNumber) { + lineNumber = e.lineNumber; + } + + var file; + + if (e.sourceURL) { + file = e.sourceURL; + } + else if (e.fileName) { + file = e.fileName; + } + + var message = (e.name && e.message) ? (e.name + ': ' + e.message) : e.toString(); + + if (file && lineNumber) { + message += ' in ' + file + ' (line ' + lineNumber + ')'; + } + + return message; +}; + +jasmine.util.htmlEscape = function(str) { + if (!str) return str; + return str.replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>'); +}; + +jasmine.util.argsToArray = function(args) { + var arrayOfArgs = []; + for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]); + return arrayOfArgs; +}; + +jasmine.util.extend = function(destination, source) { + for (var property in source) destination[property] = source[property]; + return destination; +}; + +/** + * Environment for Jasmine + * + * @constructor + */ +jasmine.Env = function() { + this.currentSpec = null; + this.currentSuite = null; + this.currentRunner_ = new jasmine.Runner(this); + + this.reporter = new jasmine.MultiReporter(); + + this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; + this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; + this.lastUpdate = 0; + this.specFilter = function() { + return true; + }; + + this.nextSpecId_ = 0; + this.nextSuiteId_ = 0; + this.equalityTesters_ = []; + + // wrap matchers + this.matchersClass = function() { + jasmine.Matchers.apply(this, arguments); + }; + jasmine.util.inherit(this.matchersClass, jasmine.Matchers); + + jasmine.Matchers.wrapInto_(jasmine.Matchers.prototype, this.matchersClass); +}; + + +jasmine.Env.prototype.setTimeout = jasmine.setTimeout; +jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout; +jasmine.Env.prototype.setInterval = jasmine.setInterval; +jasmine.Env.prototype.clearInterval = jasmine.clearInterval; + +/** + * @returns an object containing jasmine version build info, if set. + */ +jasmine.Env.prototype.version = function () { + if (jasmine.version_) { + return jasmine.version_; + } else { + throw new Error('Version not set'); + } +}; + +/** + * @returns string containing jasmine version build info, if set. + */ +jasmine.Env.prototype.versionString = function() { + if (!jasmine.version_) { + return "version unknown"; + } + + var version = this.version(); + var versionString = version.major + "." + version.minor + "." + version.build; + if (version.release_candidate) { + versionString += ".rc" + version.release_candidate; + } + versionString += " revision " + version.revision; + return versionString; +}; + +/** + * @returns a sequential integer starting at 0 + */ +jasmine.Env.prototype.nextSpecId = function () { + return this.nextSpecId_++; +}; + +/** + * @returns a sequential integer starting at 0 + */ +jasmine.Env.prototype.nextSuiteId = function () { + return this.nextSuiteId_++; +}; + +/** + * Register a reporter to receive status updates from Jasmine. + * @param {jasmine.Reporter} reporter An object which will receive status updates. + */ +jasmine.Env.prototype.addReporter = function(reporter) { + this.reporter.addReporter(reporter); +}; + +jasmine.Env.prototype.execute = function() { + this.currentRunner_.execute(); +}; + +jasmine.Env.prototype.describe = function(description, specDefinitions) { + var suite = new jasmine.Suite(this, description, specDefinitions, this.currentSuite); + + var parentSuite = this.currentSuite; + if (parentSuite) { + parentSuite.add(suite); + } else { + this.currentRunner_.add(suite); + } + + this.currentSuite = suite; + + var declarationError = null; + try { + specDefinitions.call(suite); + } catch(e) { + declarationError = e; + } + + if (declarationError) { + this.it("encountered a declaration exception", function() { + throw declarationError; + }); + } + + this.currentSuite = parentSuite; + + return suite; +}; + +jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { + if (this.currentSuite) { + this.currentSuite.beforeEach(beforeEachFunction); + } else { + this.currentRunner_.beforeEach(beforeEachFunction); + } +}; + +jasmine.Env.prototype.currentRunner = function () { + return this.currentRunner_; +}; + +jasmine.Env.prototype.afterEach = function(afterEachFunction) { + if (this.currentSuite) { + this.currentSuite.afterEach(afterEachFunction); + } else { + this.currentRunner_.afterEach(afterEachFunction); + } + +}; + +jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { + return { + execute: function() { + } + }; +}; + +jasmine.Env.prototype.it = function(description, func) { + var spec = new jasmine.Spec(this, this.currentSuite, description); + this.currentSuite.add(spec); + this.currentSpec = spec; + + if (func) { + spec.runs(func); + } + + return spec; +}; + +jasmine.Env.prototype.xit = function(desc, func) { + return { + id: this.nextSpecId(), + runs: function() { + } + }; +}; + +jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { + if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { + return true; + } + + a.__Jasmine_been_here_before__ = b; + b.__Jasmine_been_here_before__ = a; + + var hasKey = function(obj, keyName) { + return obj !== null && obj[keyName] !== jasmine.undefined; + }; + + for (var property in b) { + if (!hasKey(a, property) && hasKey(b, property)) { + mismatchKeys.push("expected has key '" + property + "', but missing from actual."); + } + } + for (property in a) { + if (!hasKey(b, property) && hasKey(a, property)) { + mismatchKeys.push("expected missing key '" + property + "', but present in actual."); + } + } + for (property in b) { + if (property == '__Jasmine_been_here_before__') continue; + if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { + mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "' in actual."); + } + } + + if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) { + mismatchValues.push("arrays were not the same length"); + } + + delete a.__Jasmine_been_here_before__; + delete b.__Jasmine_been_here_before__; + return (mismatchKeys.length === 0 && mismatchValues.length === 0); +}; + +jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { + mismatchKeys = mismatchKeys || []; + mismatchValues = mismatchValues || []; + + for (var i = 0; i < this.equalityTesters_.length; i++) { + var equalityTester = this.equalityTesters_[i]; + var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); + if (result !== jasmine.undefined) return result; + } + + if (a === b) return true; + + if (a === jasmine.undefined || a === null || b === jasmine.undefined || b === null) { + return (a == jasmine.undefined && b == jasmine.undefined); + } + + if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) { + return a === b; + } + + if (a instanceof Date && b instanceof Date) { + return a.getTime() == b.getTime(); + } + + if (a instanceof jasmine.Matchers.Any) { + return a.matches(b); + } + + if (b instanceof jasmine.Matchers.Any) { + return b.matches(a); + } + + if (jasmine.isString_(a) && jasmine.isString_(b)) { + return (a == b); + } + + if (jasmine.isNumber_(a) && jasmine.isNumber_(b)) { + return (a == b); + } + + if (typeof a === "object" && typeof b === "object") { + return this.compareObjects_(a, b, mismatchKeys, mismatchValues); + } + + //Straight check + return (a === b); +}; + +jasmine.Env.prototype.contains_ = function(haystack, needle) { + if (jasmine.isArray_(haystack)) { + for (var i = 0; i < haystack.length; i++) { + if (this.equals_(haystack[i], needle)) return true; + } + return false; + } + return haystack.indexOf(needle) >= 0; +}; + +jasmine.Env.prototype.addEqualityTester = function(equalityTester) { + this.equalityTesters_.push(equalityTester); +}; +/** No-op base class for Jasmine reporters. + * + * @constructor + */ +jasmine.Reporter = function() { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportRunnerStarting = function(runner) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportRunnerResults = function(runner) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSuiteResults = function(suite) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSpecStarting = function(spec) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.reportSpecResults = function(spec) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.Reporter.prototype.log = function(str) { +}; + +/** + * Blocks are functions with executable code that make up a spec. + * + * @constructor + * @param {jasmine.Env} env + * @param {Function} func + * @param {jasmine.Spec} spec + */ +jasmine.Block = function(env, func, spec) { + this.env = env; + this.func = func; + this.spec = spec; +}; + +jasmine.Block.prototype.execute = function(onComplete) { + try { + this.func.apply(this.spec); + } catch (e) { + this.spec.fail(e); + } + onComplete(); +}; +/** JavaScript API reporter. + * + * @constructor + */ +jasmine.JsApiReporter = function() { + this.started = false; + this.finished = false; + this.suites_ = []; + this.results_ = {}; +}; + +jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) { + this.started = true; + var suites = runner.topLevelSuites(); + for (var i = 0; i < suites.length; i++) { + var suite = suites[i]; + this.suites_.push(this.summarize_(suite)); + } +}; + +jasmine.JsApiReporter.prototype.suites = function() { + return this.suites_; +}; + +jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) { + var isSuite = suiteOrSpec instanceof jasmine.Suite; + var summary = { + id: suiteOrSpec.id, + name: suiteOrSpec.description, + type: isSuite ? 'suite' : 'spec', + children: [] + }; + + if (isSuite) { + var children = suiteOrSpec.children(); + for (var i = 0; i < children.length; i++) { + summary.children.push(this.summarize_(children[i])); + } + } + return summary; +}; + +jasmine.JsApiReporter.prototype.results = function() { + return this.results_; +}; + +jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) { + return this.results_[specId]; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) { + this.finished = true; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) { +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) { + this.results_[spec.id] = { + messages: spec.results().getItems(), + result: spec.results().failedCount > 0 ? "failed" : "passed" + }; +}; + +//noinspection JSUnusedLocalSymbols +jasmine.JsApiReporter.prototype.log = function(str) { +}; + +jasmine.JsApiReporter.prototype.resultsForSpecs = function(specIds){ + var results = {}; + for (var i = 0; i < specIds.length; i++) { + var specId = specIds[i]; + results[specId] = this.summarizeResult_(this.results_[specId]); + } + return results; +}; + +jasmine.JsApiReporter.prototype.summarizeResult_ = function(result){ + var summaryMessages = []; + var messagesLength = result.messages.length; + for (var messageIndex = 0; messageIndex < messagesLength; messageIndex++) { + var resultMessage = result.messages[messageIndex]; + summaryMessages.push({ + text: resultMessage.type == 'log' ? resultMessage.toString() : jasmine.undefined, + passed: resultMessage.passed ? resultMessage.passed() : true, + type: resultMessage.type, + message: resultMessage.message, + trace: { + stack: resultMessage.passed && !resultMessage.passed() ? resultMessage.trace.stack : jasmine.undefined + } + }); + } + + return { + result : result.result, + messages : summaryMessages + }; +}; + +/** + * @constructor + * @param {jasmine.Env} env + * @param actual + * @param {jasmine.Spec} spec + */ +jasmine.Matchers = function(env, actual, spec, opt_isNot) { + this.env = env; + this.actual = actual; + this.spec = spec; + this.isNot = opt_isNot || false; + this.reportWasCalled_ = false; +}; + +// todo: @deprecated as of Jasmine 0.11, remove soon [xw] +jasmine.Matchers.pp = function(str) { + throw new Error("jasmine.Matchers.pp() is no longer supported, please use jasmine.pp() instead!"); +}; + +// todo: @deprecated Deprecated as of Jasmine 0.10. Rewrite your custom matchers to return true or false. [xw] +jasmine.Matchers.prototype.report = function(result, failing_message, details) { + throw new Error("As of jasmine 0.11, custom matchers must be implemented differently -- please see jasmine docs"); +}; + +jasmine.Matchers.wrapInto_ = function(prototype, matchersClass) { + for (var methodName in prototype) { + if (methodName == 'report') continue; + var orig = prototype[methodName]; + matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig); + } +}; + +jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) { + return function() { + var matcherArgs = jasmine.util.argsToArray(arguments); + var result = matcherFunction.apply(this, arguments); + + if (this.isNot) { + result = !result; + } + + if (this.reportWasCalled_) return result; + + var message; + if (!result) { + if (this.message) { + message = this.message.apply(this, arguments); + if (jasmine.isArray_(message)) { + message = message[this.isNot ? 1 : 0]; + } + } else { + var englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); }); + message = "Expected " + jasmine.pp(this.actual) + (this.isNot ? " not " : " ") + englishyPredicate; + if (matcherArgs.length > 0) { + for (var i = 0; i < matcherArgs.length; i++) { + if (i > 0) message += ","; + message += " " + jasmine.pp(matcherArgs[i]); + } + } + message += "."; + } + } + var expectationResult = new jasmine.ExpectationResult({ + matcherName: matcherName, + passed: result, + expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0], + actual: this.actual, + message: message + }); + this.spec.addMatcherResult(expectationResult); + return jasmine.undefined; + }; +}; + + + + +/** + * toBe: compares the actual to the expected using === + * @param expected + */ +jasmine.Matchers.prototype.toBe = function(expected) { + return this.actual === expected; +}; + +/** + * toNotBe: compares the actual to the expected using !== + * @param expected + * @deprecated as of 1.0. Use not.toBe() instead. + */ +jasmine.Matchers.prototype.toNotBe = function(expected) { + return this.actual !== expected; +}; + +/** + * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. + * + * @param expected + */ +jasmine.Matchers.prototype.toEqual = function(expected) { + return this.env.equals_(this.actual, expected); +}; + +/** + * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual + * @param expected + * @deprecated as of 1.0. Use not.toNotEqual() instead. + */ +jasmine.Matchers.prototype.toNotEqual = function(expected) { + return !this.env.equals_(this.actual, expected); +}; + +/** + * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes + * a pattern or a String. + * + * @param expected + */ +jasmine.Matchers.prototype.toMatch = function(expected) { + return new RegExp(expected).test(this.actual); +}; + +/** + * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch + * @param expected + * @deprecated as of 1.0. Use not.toMatch() instead. + */ +jasmine.Matchers.prototype.toNotMatch = function(expected) { + return !(new RegExp(expected).test(this.actual)); +}; + +/** + * Matcher that compares the actual to jasmine.undefined. + */ +jasmine.Matchers.prototype.toBeDefined = function() { + return (this.actual !== jasmine.undefined); +}; + +/** + * Matcher that compares the actual to jasmine.undefined. + */ +jasmine.Matchers.prototype.toBeUndefined = function() { + return (this.actual === jasmine.undefined); +}; + +/** + * Matcher that compares the actual to null. + */ +jasmine.Matchers.prototype.toBeNull = function() { + return (this.actual === null); +}; + +/** + * Matcher that boolean not-nots the actual. + */ +jasmine.Matchers.prototype.toBeTruthy = function() { + return !!this.actual; +}; + + +/** + * Matcher that boolean nots the actual. + */ +jasmine.Matchers.prototype.toBeFalsy = function() { + return !this.actual; +}; + + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was called. + */ +jasmine.Matchers.prototype.toHaveBeenCalled = function() { + if (arguments.length > 0) { + throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith'); + } + + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy " + this.actual.identity + " to have been called.", + "Expected spy " + this.actual.identity + " not to have been called." + ]; + }; + + return this.actual.wasCalled; +}; + +/** @deprecated Use expect(xxx).toHaveBeenCalled() instead */ +jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled; + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was not called. + * + * @deprecated Use expect(xxx).not.toHaveBeenCalled() instead + */ +jasmine.Matchers.prototype.wasNotCalled = function() { + if (arguments.length > 0) { + throw new Error('wasNotCalled does not take arguments'); + } + + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy " + this.actual.identity + " to not have been called.", + "Expected spy " + this.actual.identity + " to have been called." + ]; + }; + + return !this.actual.wasCalled; +}; + +/** + * Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters. + * + * @example + * + */ +jasmine.Matchers.prototype.toHaveBeenCalledWith = function() { + var expectedArgs = jasmine.util.argsToArray(arguments); + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + this.message = function() { + if (this.actual.callCount === 0) { + // todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw] + return [ + "Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.", + "Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but it was." + ]; + } else { + return [ + "Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall), + "Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall) + ]; + } + }; + + return this.env.contains_(this.actual.argsForCall, expectedArgs); +}; + +/** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */ +jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith; + +/** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */ +jasmine.Matchers.prototype.wasNotCalledWith = function() { + var expectedArgs = jasmine.util.argsToArray(arguments); + if (!jasmine.isSpy(this.actual)) { + throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); + } + + this.message = function() { + return [ + "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was", + "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was" + ]; + }; + + return !this.env.contains_(this.actual.argsForCall, expectedArgs); +}; + +/** + * Matcher that checks that the expected item is an element in the actual Array. + * + * @param {Object} expected + */ +jasmine.Matchers.prototype.toContain = function(expected) { + return this.env.contains_(this.actual, expected); +}; + +/** + * Matcher that checks that the expected item is NOT an element in the actual Array. + * + * @param {Object} expected + * @deprecated as of 1.0. Use not.toNotContain() instead. + */ +jasmine.Matchers.prototype.toNotContain = function(expected) { + return !this.env.contains_(this.actual, expected); +}; + +jasmine.Matchers.prototype.toBeLessThan = function(expected) { + return this.actual < expected; +}; + +jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { + return this.actual > expected; +}; + +/** + * Matcher that checks that the expected item is equal to the actual item + * up to a given level of decimal precision (default 2). + * + * @param {Number} expected + * @param {Number} precision + */ +jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) { + if (!(precision === 0)) { + precision = precision || 2; + } + var multiplier = Math.pow(10, precision); + var actual = Math.round(this.actual * multiplier); + expected = Math.round(expected * multiplier); + return expected == actual; +}; + +/** + * Matcher that checks that the expected exception was thrown by the actual. + * + * @param {String} expected + */ +jasmine.Matchers.prototype.toThrow = function(expected) { + var result = false; + var exception; + if (typeof this.actual != 'function') { + throw new Error('Actual is not a function'); + } + try { + this.actual(); + } catch (e) { + exception = e; + } + if (exception) { + result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected)); + } + + var not = this.isNot ? "not " : ""; + + this.message = function() { + if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { + return ["Expected function " + not + "to throw", expected ? expected.message || expected : "an exception", ", but it threw", exception.message || exception].join(' '); + } else { + return "Expected function to throw an exception."; + } + }; + + return result; +}; + +jasmine.Matchers.Any = function(expectedClass) { + this.expectedClass = expectedClass; +}; + +jasmine.Matchers.Any.prototype.matches = function(other) { + if (this.expectedClass == String) { + return typeof other == 'string' || other instanceof String; + } + + if (this.expectedClass == Number) { + return typeof other == 'number' || other instanceof Number; + } + + if (this.expectedClass == Function) { + return typeof other == 'function' || other instanceof Function; + } + + if (this.expectedClass == Object) { + return typeof other == 'object'; + } + + return other instanceof this.expectedClass; +}; + +jasmine.Matchers.Any.prototype.toString = function() { + return '<jasmine.any(' + this.expectedClass + ')>'; +}; + +/** + * @constructor + */ +jasmine.MultiReporter = function() { + this.subReporters_ = []; +}; +jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter); + +jasmine.MultiReporter.prototype.addReporter = function(reporter) { + this.subReporters_.push(reporter); +}; + +(function() { + var functionNames = [ + "reportRunnerStarting", + "reportRunnerResults", + "reportSuiteResults", + "reportSpecStarting", + "reportSpecResults", + "log" + ]; + for (var i = 0; i < functionNames.length; i++) { + var functionName = functionNames[i]; + jasmine.MultiReporter.prototype[functionName] = (function(functionName) { + return function() { + for (var j = 0; j < this.subReporters_.length; j++) { + var subReporter = this.subReporters_[j]; + if (subReporter[functionName]) { + subReporter[functionName].apply(subReporter, arguments); + } + } + }; + })(functionName); + } +})(); +/** + * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults + * + * @constructor + */ +jasmine.NestedResults = function() { + /** + * The total count of results + */ + this.totalCount = 0; + /** + * Number of passed results + */ + this.passedCount = 0; + /** + * Number of failed results + */ + this.failedCount = 0; + /** + * Was this suite/spec skipped? + */ + this.skipped = false; + /** + * @ignore + */ + this.items_ = []; +}; + +/** + * Roll up the result counts. + * + * @param result + */ +jasmine.NestedResults.prototype.rollupCounts = function(result) { + this.totalCount += result.totalCount; + this.passedCount += result.passedCount; + this.failedCount += result.failedCount; +}; + +/** + * Adds a log message. + * @param values Array of message parts which will be concatenated later. + */ +jasmine.NestedResults.prototype.log = function(values) { + this.items_.push(new jasmine.MessageResult(values)); +}; + +/** + * Getter for the results: message & results. + */ +jasmine.NestedResults.prototype.getItems = function() { + return this.items_; +}; + +/** + * Adds a result, tracking counts (total, passed, & failed) + * @param {jasmine.ExpectationResult|jasmine.NestedResults} result + */ +jasmine.NestedResults.prototype.addResult = function(result) { + if (result.type != 'log') { + if (result.items_) { + this.rollupCounts(result); + } else { + this.totalCount++; + if (result.passed()) { + this.passedCount++; + } else { + this.failedCount++; + } + } + } + this.items_.push(result); +}; + +/** + * @returns {Boolean} True if <b>everything</b> below passed + */ +jasmine.NestedResults.prototype.passed = function() { + return this.passedCount === this.totalCount; +}; +/** + * Base class for pretty printing for expectation results. + */ +jasmine.PrettyPrinter = function() { + this.ppNestLevel_ = 0; +}; + +/** + * Formats a value in a nice, human-readable string. + * + * @param value + */ +jasmine.PrettyPrinter.prototype.format = function(value) { + if (this.ppNestLevel_ > 40) { + throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); + } + + this.ppNestLevel_++; + try { + if (value === jasmine.undefined) { + this.emitScalar('undefined'); + } else if (value === null) { + this.emitScalar('null'); + } else if (value === jasmine.getGlobal()) { + this.emitScalar('<global>'); + } else if (value instanceof jasmine.Matchers.Any) { + this.emitScalar(value.toString()); + } else if (typeof value === 'string') { + this.emitString(value); + } else if (jasmine.isSpy(value)) { + this.emitScalar("spy on " + value.identity); + } else if (value instanceof RegExp) { + this.emitScalar(value.toString()); + } else if (typeof value === 'function') { + this.emitScalar('Function'); + } else if (typeof value.nodeType === 'number') { + this.emitScalar('HTMLNode'); + } else if (value instanceof Date) { + this.emitScalar('Date(' + value + ')'); + } else if (value.__Jasmine_been_here_before__) { + this.emitScalar('<circular reference: ' + (jasmine.isArray_(value) ? 'Array' : 'Object') + '>'); + } else if (jasmine.isArray_(value) || typeof value == 'object') { + value.__Jasmine_been_here_before__ = true; + if (jasmine.isArray_(value)) { + this.emitArray(value); + } else { + this.emitObject(value); + } + delete value.__Jasmine_been_here_before__; + } else { + this.emitScalar(value.toString()); + } + } finally { + this.ppNestLevel_--; + } +}; + +jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { + for (var property in obj) { + if (property == '__Jasmine_been_here_before__') continue; + fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined && + obj.__lookupGetter__(property) !== null) : false); + } +}; + +jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_; +jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_; + +jasmine.StringPrettyPrinter = function() { + jasmine.PrettyPrinter.call(this); + + this.string = ''; +}; +jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter); + +jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) { + this.append(value); +}; + +jasmine.StringPrettyPrinter.prototype.emitString = function(value) { + this.append("'" + value + "'"); +}; + +jasmine.StringPrettyPrinter.prototype.emitArray = function(array) { + this.append('[ '); + for (var i = 0; i < array.length; i++) { + if (i > 0) { + this.append(', '); + } + this.format(array[i]); + } + this.append(' ]'); +}; + +jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { + var self = this; + this.append('{ '); + var first = true; + + this.iterateObject(obj, function(property, isGetter) { + if (first) { + first = false; + } else { + self.append(', '); + } + + self.append(property); + self.append(' : '); + if (isGetter) { + self.append('<getter>'); + } else { + self.format(obj[property]); + } + }); + + this.append(' }'); +}; + +jasmine.StringPrettyPrinter.prototype.append = function(value) { + this.string += value; +}; +jasmine.Queue = function(env) { + this.env = env; + this.blocks = []; + this.running = false; + this.index = 0; + this.offset = 0; + this.abort = false; +}; + +jasmine.Queue.prototype.addBefore = function(block) { + this.blocks.unshift(block); +}; + +jasmine.Queue.prototype.add = function(block) { + this.blocks.push(block); +}; + +jasmine.Queue.prototype.insertNext = function(block) { + this.blocks.splice((this.index + this.offset + 1), 0, block); + this.offset++; +}; + +jasmine.Queue.prototype.start = function(onComplete) { + this.running = true; + this.onComplete = onComplete; + this.next_(); +}; + +jasmine.Queue.prototype.isRunning = function() { + return this.running; +}; + +jasmine.Queue.LOOP_DONT_RECURSE = true; + +jasmine.Queue.prototype.next_ = function() { + var self = this; + var goAgain = true; + + while (goAgain) { + goAgain = false; + + if (self.index < self.blocks.length && !this.abort) { + var calledSynchronously = true; + var completedSynchronously = false; + + var onComplete = function () { + if (jasmine.Queue.LOOP_DONT_RECURSE && calledSynchronously) { + completedSynchronously = true; + return; + } + + if (self.blocks[self.index].abort) { + self.abort = true; + } + + self.offset = 0; + self.index++; + + var now = new Date().getTime(); + if (self.env.updateInterval && now - self.env.lastUpdate > self.env.updateInterval) { + self.env.lastUpdate = now; + self.env.setTimeout(function() { + self.next_(); + }, 0); + } else { + if (jasmine.Queue.LOOP_DONT_RECURSE && completedSynchronously) { + goAgain = true; + } else { + self.next_(); + } + } + }; + self.blocks[self.index].execute(onComplete); + + calledSynchronously = false; + if (completedSynchronously) { + onComplete(); + } + + } else { + self.running = false; + if (self.onComplete) { + self.onComplete(); + } + } + } +}; + +jasmine.Queue.prototype.results = function() { + var results = new jasmine.NestedResults(); + for (var i = 0; i < this.blocks.length; i++) { + if (this.blocks[i].results) { + results.addResult(this.blocks[i].results()); + } + } + return results; +}; + + +/** + * Runner + * + * @constructor + * @param {jasmine.Env} env + */ +jasmine.Runner = function(env) { + var self = this; + self.env = env; + self.queue = new jasmine.Queue(env); + self.before_ = []; + self.after_ = []; + self.suites_ = []; +}; + +jasmine.Runner.prototype.execute = function() { + var self = this; + if (self.env.reporter.reportRunnerStarting) { + self.env.reporter.reportRunnerStarting(this); + } + self.queue.start(function () { + self.finishCallback(); + }); +}; + +jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.splice(0,0,beforeEachFunction); +}; + +jasmine.Runner.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.splice(0,0,afterEachFunction); +}; + + +jasmine.Runner.prototype.finishCallback = function() { + this.env.reporter.reportRunnerResults(this); +}; + +jasmine.Runner.prototype.addSuite = function(suite) { + this.suites_.push(suite); +}; + +jasmine.Runner.prototype.add = function(block) { + if (block instanceof jasmine.Suite) { + this.addSuite(block); + } + this.queue.add(block); +}; + +jasmine.Runner.prototype.specs = function () { + var suites = this.suites(); + var specs = []; + for (var i = 0; i < suites.length; i++) { + specs = specs.concat(suites[i].specs()); + } + return specs; +}; + +jasmine.Runner.prototype.suites = function() { + return this.suites_; +}; + +jasmine.Runner.prototype.topLevelSuites = function() { + var topLevelSuites = []; + for (var i = 0; i < this.suites_.length; i++) { + if (!this.suites_[i].parentSuite) { + topLevelSuites.push(this.suites_[i]); + } + } + return topLevelSuites; +}; + +jasmine.Runner.prototype.results = function() { + return this.queue.results(); +}; +/** + * Internal representation of a Jasmine specification, or test. + * + * @constructor + * @param {jasmine.Env} env + * @param {jasmine.Suite} suite + * @param {String} description + */ +jasmine.Spec = function(env, suite, description) { + if (!env) { + throw new Error('jasmine.Env() required'); + } + if (!suite) { + throw new Error('jasmine.Suite() required'); + } + var spec = this; + spec.id = env.nextSpecId ? env.nextSpecId() : null; + spec.env = env; + spec.suite = suite; + spec.description = description; + spec.queue = new jasmine.Queue(env); + + spec.afterCallbacks = []; + spec.spies_ = []; + + spec.results_ = new jasmine.NestedResults(); + spec.results_.description = description; + spec.matchersClass = null; +}; + +jasmine.Spec.prototype.getFullName = function() { + return this.suite.getFullName() + ' ' + this.description + '.'; +}; + + +jasmine.Spec.prototype.results = function() { + return this.results_; +}; + +/** + * All parameters are pretty-printed and concatenated together, then written to the spec's output. + * + * Be careful not to leave calls to <code>jasmine.log</code> in production code. + */ +jasmine.Spec.prototype.log = function() { + return this.results_.log(arguments); +}; + +jasmine.Spec.prototype.runs = function (func) { + var block = new jasmine.Block(this.env, func, this); + this.addToQueue(block); + return this; +}; + +jasmine.Spec.prototype.addToQueue = function (block) { + if (this.queue.isRunning()) { + this.queue.insertNext(block); + } else { + this.queue.add(block); + } +}; + +/** + * @param {jasmine.ExpectationResult} result + */ +jasmine.Spec.prototype.addMatcherResult = function(result) { + this.results_.addResult(result); +}; + +jasmine.Spec.prototype.expect = function(actual) { + var positive = new (this.getMatchersClass_())(this.env, actual, this); + positive.not = new (this.getMatchersClass_())(this.env, actual, this, true); + return positive; +}; + +/** + * Waits a fixed time period before moving to the next block. + * + * @deprecated Use waitsFor() instead + * @param {Number} timeout milliseconds to wait + */ +jasmine.Spec.prototype.waits = function(timeout) { + var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); + this.addToQueue(waitsFunc); + return this; +}; + +/** + * Waits for the latchFunction to return true before proceeding to the next block. + * + * @param {Function} latchFunction + * @param {String} optional_timeoutMessage + * @param {Number} optional_timeout + */ +jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { + var latchFunction_ = null; + var optional_timeoutMessage_ = null; + var optional_timeout_ = null; + + for (var i = 0; i < arguments.length; i++) { + var arg = arguments[i]; + switch (typeof arg) { + case 'function': + latchFunction_ = arg; + break; + case 'string': + optional_timeoutMessage_ = arg; + break; + case 'number': + optional_timeout_ = arg; + break; + } + } + + var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this); + this.addToQueue(waitsForFunc); + return this; +}; + +jasmine.Spec.prototype.fail = function (e) { + var expectationResult = new jasmine.ExpectationResult({ + passed: false, + message: e ? jasmine.util.formatException(e) : 'Exception', + trace: { stack: e.stack } + }); + this.results_.addResult(expectationResult); +}; + +jasmine.Spec.prototype.getMatchersClass_ = function() { + return this.matchersClass || this.env.matchersClass; +}; + +jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { + var parent = this.getMatchersClass_(); + var newMatchersClass = function() { + parent.apply(this, arguments); + }; + jasmine.util.inherit(newMatchersClass, parent); + jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass); + this.matchersClass = newMatchersClass; +}; + +jasmine.Spec.prototype.finishCallback = function() { + this.env.reporter.reportSpecResults(this); +}; + +jasmine.Spec.prototype.finish = function(onComplete) { + this.removeAllSpies(); + this.finishCallback(); + if (onComplete) { + onComplete(); + } +}; + +jasmine.Spec.prototype.after = function(doAfter) { + if (this.queue.isRunning()) { + this.queue.add(new jasmine.Block(this.env, doAfter, this)); + } else { + this.afterCallbacks.unshift(doAfter); + } +}; + +jasmine.Spec.prototype.execute = function(onComplete) { + var spec = this; + if (!spec.env.specFilter(spec)) { + spec.results_.skipped = true; + spec.finish(onComplete); + return; + } + + this.env.reporter.reportSpecStarting(this); + + spec.env.currentSpec = spec; + + spec.addBeforesAndAftersToQueue(); + + spec.queue.start(function () { + spec.finish(onComplete); + }); +}; + +jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { + var runner = this.env.currentRunner(); + var i; + + for (var suite = this.suite; suite; suite = suite.parentSuite) { + for (i = 0; i < suite.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); + } + } + for (i = 0; i < runner.before_.length; i++) { + this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); + } + for (i = 0; i < this.afterCallbacks.length; i++) { + this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); + } + for (suite = this.suite; suite; suite = suite.parentSuite) { + for (i = 0; i < suite.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); + } + } + for (i = 0; i < runner.after_.length; i++) { + this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); + } +}; + +jasmine.Spec.prototype.explodes = function() { + throw 'explodes function should not have been called'; +}; + +jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { + if (obj == jasmine.undefined) { + throw "spyOn could not find an object to spy upon for " + methodName + "()"; + } + + if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) { + throw methodName + '() method does not exist'; + } + + if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { + throw new Error(methodName + ' has already been spied upon'); + } + + var spyObj = jasmine.createSpy(methodName); + + this.spies_.push(spyObj); + spyObj.baseObj = obj; + spyObj.methodName = methodName; + spyObj.originalValue = obj[methodName]; + + obj[methodName] = spyObj; + + return spyObj; +}; + +jasmine.Spec.prototype.removeAllSpies = function() { + for (var i = 0; i < this.spies_.length; i++) { + var spy = this.spies_[i]; + spy.baseObj[spy.methodName] = spy.originalValue; + } + this.spies_ = []; +}; + +/** + * Internal representation of a Jasmine suite. + * + * @constructor + * @param {jasmine.Env} env + * @param {String} description + * @param {Function} specDefinitions + * @param {jasmine.Suite} parentSuite + */ +jasmine.Suite = function(env, description, specDefinitions, parentSuite) { + var self = this; + self.id = env.nextSuiteId ? env.nextSuiteId() : null; + self.description = description; + self.queue = new jasmine.Queue(env); + self.parentSuite = parentSuite; + self.env = env; + self.before_ = []; + self.after_ = []; + self.children_ = []; + self.suites_ = []; + self.specs_ = []; +}; + +jasmine.Suite.prototype.getFullName = function() { + var fullName = this.description; + for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) { + fullName = parentSuite.description + ' ' + fullName; + } + return fullName; +}; + +jasmine.Suite.prototype.finish = function(onComplete) { + this.env.reporter.reportSuiteResults(this); + this.finished = true; + if (typeof(onComplete) == 'function') { + onComplete(); + } +}; + +jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { + beforeEachFunction.typeName = 'beforeEach'; + this.before_.unshift(beforeEachFunction); +}; + +jasmine.Suite.prototype.afterEach = function(afterEachFunction) { + afterEachFunction.typeName = 'afterEach'; + this.after_.unshift(afterEachFunction); +}; + +jasmine.Suite.prototype.results = function() { + return this.queue.results(); +}; + +jasmine.Suite.prototype.add = function(suiteOrSpec) { + this.children_.push(suiteOrSpec); + if (suiteOrSpec instanceof jasmine.Suite) { + this.suites_.push(suiteOrSpec); + this.env.currentRunner().addSuite(suiteOrSpec); + } else { + this.specs_.push(suiteOrSpec); + } + this.queue.add(suiteOrSpec); +}; + +jasmine.Suite.prototype.specs = function() { + return this.specs_; +}; + +jasmine.Suite.prototype.suites = function() { + return this.suites_; +}; + +jasmine.Suite.prototype.children = function() { + return this.children_; +}; + +jasmine.Suite.prototype.execute = function(onComplete) { + var self = this; + this.queue.start(function () { + self.finish(onComplete); + }); +}; +jasmine.WaitsBlock = function(env, timeout, spec) { + this.timeout = timeout; + jasmine.Block.call(this, env, null, spec); +}; + +jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block); + +jasmine.WaitsBlock.prototype.execute = function (onComplete) { + if (jasmine.VERBOSE) { + this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...'); + } + this.env.setTimeout(function () { + onComplete(); + }, this.timeout); +}; +/** + * A block which waits for some condition to become true, with timeout. + * + * @constructor + * @extends jasmine.Block + * @param {jasmine.Env} env The Jasmine environment. + * @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true. + * @param {Function} latchFunction A function which returns true when the desired condition has been met. + * @param {String} message The message to display if the desired condition hasn't been met within the given time period. + * @param {jasmine.Spec} spec The Jasmine spec. + */ +jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { + this.timeout = timeout || env.defaultTimeoutInterval; + this.latchFunction = latchFunction; + this.message = message; + this.totalTimeSpentWaitingForLatch = 0; + jasmine.Block.call(this, env, null, spec); +}; +jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); + +jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10; + +jasmine.WaitsForBlock.prototype.execute = function(onComplete) { + if (jasmine.VERBOSE) { + this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen')); + } + var latchFunctionResult; + try { + latchFunctionResult = this.latchFunction.apply(this.spec); + } catch (e) { + this.spec.fail(e); + onComplete(); + return; + } + + if (latchFunctionResult) { + onComplete(); + } else if (this.totalTimeSpentWaitingForLatch >= this.timeout) { + var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen'); + this.spec.fail({ + name: 'timeout', + message: message + }); + + this.abort = true; + onComplete(); + } else { + this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; + var self = this; + this.env.setTimeout(function() { + self.execute(onComplete); + }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); + } +}; +// Mock setTimeout, clearTimeout +// Contributed by Pivotal Computer Systems, www.pivotalsf.com + +jasmine.FakeTimer = function() { + this.reset(); + + var self = this; + self.setTimeout = function(funcToCall, millis) { + self.timeoutsMade++; + self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false); + return self.timeoutsMade; + }; + + self.setInterval = function(funcToCall, millis) { + self.timeoutsMade++; + self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true); + return self.timeoutsMade; + }; + + self.clearTimeout = function(timeoutKey) { + self.scheduledFunctions[timeoutKey] = jasmine.undefined; + }; + + self.clearInterval = function(timeoutKey) { + self.scheduledFunctions[timeoutKey] = jasmine.undefined; + }; + +}; + +jasmine.FakeTimer.prototype.reset = function() { + this.timeoutsMade = 0; + this.scheduledFunctions = {}; + this.nowMillis = 0; +}; + +jasmine.FakeTimer.prototype.tick = function(millis) { + var oldMillis = this.nowMillis; + var newMillis = oldMillis + millis; + this.runFunctionsWithinRange(oldMillis, newMillis); + this.nowMillis = newMillis; +}; + +jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) { + var scheduledFunc; + var funcsToRun = []; + for (var timeoutKey in this.scheduledFunctions) { + scheduledFunc = this.scheduledFunctions[timeoutKey]; + if (scheduledFunc != jasmine.undefined && + scheduledFunc.runAtMillis >= oldMillis && + scheduledFunc.runAtMillis <= nowMillis) { + funcsToRun.push(scheduledFunc); + this.scheduledFunctions[timeoutKey] = jasmine.undefined; + } + } + + if (funcsToRun.length > 0) { + funcsToRun.sort(function(a, b) { + return a.runAtMillis - b.runAtMillis; + }); + for (var i = 0; i < funcsToRun.length; ++i) { + try { + var funcToRun = funcsToRun[i]; + this.nowMillis = funcToRun.runAtMillis; + funcToRun.funcToCall(); + if (funcToRun.recurring) { + this.scheduleFunction(funcToRun.timeoutKey, + funcToRun.funcToCall, + funcToRun.millis, + true); + } + } catch(e) { + } + } + this.runFunctionsWithinRange(oldMillis, nowMillis); + } +}; + +jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) { + this.scheduledFunctions[timeoutKey] = { + runAtMillis: this.nowMillis + millis, + funcToCall: funcToCall, + recurring: recurring, + timeoutKey: timeoutKey, + millis: millis + }; +}; + +/** + * @namespace + */ +jasmine.Clock = { + defaultFakeTimer: new jasmine.FakeTimer(), + + reset: function() { + jasmine.Clock.assertInstalled(); + jasmine.Clock.defaultFakeTimer.reset(); + }, + + tick: function(millis) { + jasmine.Clock.assertInstalled(); + jasmine.Clock.defaultFakeTimer.tick(millis); + }, + + runFunctionsWithinRange: function(oldMillis, nowMillis) { + jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis); + }, + + scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) { + jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring); + }, + + useMock: function() { + if (!jasmine.Clock.isInstalled()) { + var spec = jasmine.getEnv().currentSpec; + spec.after(jasmine.Clock.uninstallMock); + + jasmine.Clock.installMock(); + } + }, + + installMock: function() { + jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer; + }, + + uninstallMock: function() { + jasmine.Clock.assertInstalled(); + jasmine.Clock.installed = jasmine.Clock.real; + }, + + real: { + setTimeout: jasmine.getGlobal().setTimeout, + clearTimeout: jasmine.getGlobal().clearTimeout, + setInterval: jasmine.getGlobal().setInterval, + clearInterval: jasmine.getGlobal().clearInterval + }, + + assertInstalled: function() { + if (!jasmine.Clock.isInstalled()) { + throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); + } + }, + + isInstalled: function() { + return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer; + }, + + installed: null +}; +jasmine.Clock.installed = jasmine.Clock.real; + +//else for IE support +jasmine.getGlobal().setTimeout = function(funcToCall, millis) { + if (jasmine.Clock.installed.setTimeout.apply) { + return jasmine.Clock.installed.setTimeout.apply(this, arguments); + } else { + return jasmine.Clock.installed.setTimeout(funcToCall, millis); + } +}; + +jasmine.getGlobal().setInterval = function(funcToCall, millis) { + if (jasmine.Clock.installed.setInterval.apply) { + return jasmine.Clock.installed.setInterval.apply(this, arguments); + } else { + return jasmine.Clock.installed.setInterval(funcToCall, millis); + } +}; + +jasmine.getGlobal().clearTimeout = function(timeoutKey) { + if (jasmine.Clock.installed.clearTimeout.apply) { + return jasmine.Clock.installed.clearTimeout.apply(this, arguments); + } else { + return jasmine.Clock.installed.clearTimeout(timeoutKey); + } +}; + +jasmine.getGlobal().clearInterval = function(timeoutKey) { + if (jasmine.Clock.installed.clearTimeout.apply) { + return jasmine.Clock.installed.clearInterval.apply(this, arguments); + } else { + return jasmine.Clock.installed.clearInterval(timeoutKey); + } +}; + +jasmine.version_= { + "major": 1, + "minor": 1, + "build": 0, + "revision": 1315677058 +}; diff --git a/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine_favicon.png b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine_favicon.png Binary files differnew file mode 100644 index 0000000..218f3b4 --- /dev/null +++ b/node_modules/mongodb/node_modules/bson/tools/jasmine-1.1.0/jasmine_favicon.png diff --git a/node_modules/mongodb/package.json b/node_modules/mongodb/package.json new file mode 100755 index 0000000..94843a3 --- /dev/null +++ b/node_modules/mongodb/package.json @@ -0,0 +1,214 @@ +{ + "name": "mongodb", + "description": "A node.js driver for MongoDB", + "keywords": [ + "mongodb", + "mongo", + "driver", + "db" + ], + "version": "0.9.9-7", + "author": { + "name": "Christian Amor Kvalheim", + "email": "christkv@gmail.com" + }, + "contributors": [ + { + "name": "Aaron Heckmann" + }, + { + "name": "Christoph Pojer" + }, + { + "name": "Pau Ramon Revilla" + }, + { + "name": "Nathan White" + }, + { + "name": "Emmerman" + }, + { + "name": "Seth LaForge" + }, + { + "name": "Boris Filipov" + }, + { + "name": "Stefan Schärmeli" + }, + { + "name": "Tedde Lundgren" + }, + { + "name": "renctan" + }, + { + "name": "Sergey Ukustov" + }, + { + "name": "Ciaran Jessup" + }, + { + "name": "kuno" + }, + { + "name": "srimonti" + }, + { + "name": "Erik Abele" + }, + { + "name": "Pratik Daga" + }, + { + "name": "Slobodan Utvic" + }, + { + "name": "Kristina Chodorow" + }, + { + "name": "Yonathan Randolph" + }, + { + "name": "Brian Noguchi" + }, + { + "name": "Sam Epstein" + }, + { + "name": "James Harrison Fisher" + }, + { + "name": "Vladimir Dronnikov" + }, + { + "name": "Ben Hockey" + }, + { + "name": "Henrik Johansson" + }, + { + "name": "Simon Weare" + }, + { + "name": "Alex Gorbatchev" + }, + { + "name": "Shimon Doodkin" + }, + { + "name": "Kyle Mueller" + }, + { + "name": "Eran Hammer-Lahav" + }, + { + "name": "Marcin Ciszak" + }, + { + "name": "François de Metz" + }, + { + "name": "Vinay Pulim" + }, + { + "name": "nstielau" + }, + { + "name": "Adam Wiggins" + }, + { + "name": "entrinzikyl" + }, + { + "name": "Jeremy Selier" + }, + { + "name": "Ian Millington" + }, + { + "name": "Public Keating" + }, + { + "name": "andrewjstone" + }, + { + "name": "Christopher Stott" + }, + { + "name": "Corey Jewett" + }, + { + "name": "brettkiefer" + }, + { + "name": "Rob Holland" + }, + { + "name": "Senmiao Liu" + }, + { + "name": "heroic" + }, + { + "name": "gitfy" + }, + { + "name": "Andrew Stone" + }, + { + "name": "John Le Drew" + } + ], + "repository": { + "type": "git", + "url": "git://github.com/christkv/node-mongodb-native.git" + }, + "bugs": { + "email": "node-mongodb-native@googlegroups.com", + "url": "http://github.com/christkv/node-mongodb-native/issues" + }, + "dependencies": { + "bson": "0.0.4" + }, + "devDependencies": { + "dox": "0.2.0", + "uglify-js": "1.2.5", + "ejs": "0.6.1", + "nodeunit": "0.7.3", + "github3": ">=0.3.0", + "markdown": "0.3.1", + "gleak": "0.2.3", + "step": "0.0.5" + }, + "config": { + "native": false + }, + "main": "./lib/mongodb/index", + "directories": { + "lib": "./lib/mongodb" + }, + "engines": { + "node": ">=0.4.0" + }, + "scripts": { + "test": "make test_pure" + }, + "licenses": [ + { + "type": "Apache License, Version 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0" + } + ], + "_id": "mongodb@0.9.9-7", + "optionalDependencies": {}, + "_engineSupported": true, + "_npmVersion": "1.1.12", + "_nodeVersion": "v0.6.14", + "_defaultsLoaded": true, + "dist": { + "shasum": "6c4aff5b6c0c66d37dd736585426c43c73b04308" + }, + "_from": "mongodb@" +} |
