bson 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1965,6 +1965,7 @@ var crc32 = function(string, start, end) {
1965
1965
  * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1966
1966
  * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1967
1967
  * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1968
+ * - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
1968
1969
  *
1969
1970
  * @param {Buffer} data the buffer containing the serialized set of BSON documents.
1970
1971
  * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
@@ -2054,6 +2055,7 @@ var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
2054
2055
  * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
2055
2056
  * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
2056
2057
  * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
2058
+ * - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
2057
2059
  *
2058
2060
  * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
2059
2061
  * @param {Object} [options] additional options used for the deserialization.
@@ -2067,6 +2069,7 @@ BSON.deserialize = function(buffer, options, isArray) {
2067
2069
  var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
2068
2070
  var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
2069
2071
  var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
2072
+ var promoteLongs = options['promoteLongs'] || true;
2070
2073
 
2071
2074
  // Validate that we have at least 4 bytes of buffer
2072
2075
  if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
@@ -2221,8 +2224,14 @@ BSON.deserialize = function(buffer, options, isArray) {
2221
2224
  // Unpack the low and high bits
2222
2225
  var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
2223
2226
  var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
2224
- // Set the object
2225
- object[name] = new Long(lowBits, highBits);
2227
+ // Create long object
2228
+ var long = new Long(lowBits, highBits);
2229
+ // Promote the long if possible
2230
+ if(promoteLongs) {
2231
+ object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
2232
+ } else {
2233
+ object[name] = long;
2234
+ }
2226
2235
  break;
2227
2236
  case BSON.BSON_DATA_SYMBOL:
2228
2237
  // Read the content of the field
Binary file
Binary file
Binary file
Binary file
@@ -1,2 +1,2 @@
1
- #v4.0:v100
2
- Release|Win32|Z:\coding\projects\js-bson\build\|
1
+ #v4.0:Windows7.1SDK
2
+ Release|x64|Z:\coding\projects\js-bson\build\|
Binary file
package/build/binding.sln CHANGED
@@ -4,14 +4,14 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bson", "bson.vcxproj", "{C4
4
4
  EndProject
5
5
  Global
6
6
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
7
- Debug|Win32 = Debug|Win32
8
- Release|Win32 = Release|Win32
7
+ Debug|x64 = Debug|x64
8
+ Release|x64 = Release|x64
9
9
  EndGlobalSection
10
10
  GlobalSection(ProjectConfigurationPlatforms) = postSolution
11
- {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Debug|Win32.ActiveCfg = Debug|Win32
12
- {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Debug|Win32.Build.0 = Debug|Win32
13
- {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Release|Win32.ActiveCfg = Release|Win32
14
- {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Release|Win32.Build.0 = Release|Win32
11
+ {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Debug|x64.ActiveCfg = Debug|x64
12
+ {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Debug|x64.Build.0 = Debug|x64
13
+ {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Release|x64.ActiveCfg = Release|x64
14
+ {C480D2C0-7E29-C7A7-FD22-F87AD7E5A4C6}.Release|x64.Build.0 = Release|x64
15
15
  EndGlobalSection
16
16
  GlobalSection(SolutionProperties) = preSolution
17
17
  HideSolutionNode = FALSE
@@ -1,13 +1,13 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
3
  <ItemGroup Label="ProjectConfigurations">
4
- <ProjectConfiguration Include="Debug|Win32">
4
+ <ProjectConfiguration Include="Debug|x64">
5
5
  <Configuration>Debug</Configuration>
6
- <Platform>Win32</Platform>
6
+ <Platform>x64</Platform>
7
7
  </ProjectConfiguration>
8
- <ProjectConfiguration Include="Release|Win32">
8
+ <ProjectConfiguration Include="Release|x64">
9
9
  <Configuration>Release</Configuration>
10
- <Platform>Win32</Platform>
10
+ <Platform>x64</Platform>
11
11
  </ProjectConfiguration>
12
12
  </ItemGroup>
13
13
  <PropertyGroup Label="Globals">
@@ -19,6 +19,9 @@
19
19
  <PropertyGroup Label="Configuration">
20
20
  <ConfigurationType>DynamicLibrary</ConfigurationType>
21
21
  </PropertyGroup>
22
+ <PropertyGroup Label="Locals">
23
+ <PlatformToolset>Windows7.1SDK</PlatformToolset>
24
+ </PropertyGroup>
22
25
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props"/>
23
26
  <ImportGroup Label="ExtensionSettings"/>
24
27
  <ImportGroup Label="PropertySheets">
@@ -29,14 +32,14 @@
29
32
  <ExecutablePath>$(ExecutablePath);$(MSBuildProjectDirectory)\..\bin\;$(MSBuildProjectDirectory)\..\bin\</ExecutablePath>
30
33
  <IgnoreImportLibrary>true</IgnoreImportLibrary>
31
34
  <IntDir>$(Configuration)\obj\$(ProjectName)\</IntDir>
32
- <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">false</LinkIncremental>
33
- <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</LinkIncremental>
35
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</LinkIncremental>
36
+ <LinkIncremental Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</LinkIncremental>
34
37
  <OutDir>$(SolutionDir)$(Configuration)\</OutDir>
35
38
  <TargetExt>.node</TargetExt>
36
39
  <TargetName>$(ProjectName)</TargetName>
37
40
  <TargetPath>$(OutDir)\$(ProjectName).node</TargetPath>
38
41
  </PropertyGroup>
39
- <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
42
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
40
43
  <ClCompile>
41
44
  <AdditionalIncludeDirectories>C:\Users\ck\.node-gyp\0.10.13\src;C:\Users\ck\.node-gyp\0.10.13\deps\uv\include;C:\Users\ck\.node-gyp\0.10.13\deps\v8\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
42
45
  <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
@@ -62,13 +65,14 @@
62
65
  <OutputFile>$(OutDir)$(ProjectName).node</OutputFile>
63
66
  <RandomizedBaseAddress>true</RandomizedBaseAddress>
64
67
  <SuppressStartupBanner>true</SuppressStartupBanner>
68
+ <TargetMachine>MachineX64</TargetMachine>
65
69
  </Link>
66
70
  <ResourceCompile>
67
71
  <AdditionalIncludeDirectories>C:\Users\ck\.node-gyp\0.10.13\src;C:\Users\ck\.node-gyp\0.10.13\deps\uv\include;C:\Users\ck\.node-gyp\0.10.13\deps\v8\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
68
72
  <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BUILDING_V8_SHARED=1;BUILDING_UV_SHARED=1;BUILDING_NODE_EXTENSION;DEBUG;_DEBUG;%(PreprocessorDefinitions);%(PreprocessorDefinitions)</PreprocessorDefinitions>
69
73
  </ResourceCompile>
70
74
  </ItemDefinitionGroup>
71
- <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
75
+ <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
72
76
  <ClCompile>
73
77
  <AdditionalIncludeDirectories>C:\Users\ck\.node-gyp\0.10.13\src;C:\Users\ck\.node-gyp\0.10.13\deps\uv\include;C:\Users\ck\.node-gyp\0.10.13\deps\v8\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
74
78
  <AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions>
@@ -105,6 +109,7 @@
105
109
  <OutputFile>$(OutDir)$(ProjectName).node</OutputFile>
106
110
  <RandomizedBaseAddress>true</RandomizedBaseAddress>
107
111
  <SuppressStartupBanner>true</SuppressStartupBanner>
112
+ <TargetMachine>MachineX64</TargetMachine>
108
113
  </Link>
109
114
  <ResourceCompile>
110
115
  <AdditionalIncludeDirectories>C:\Users\ck\.node-gyp\0.10.13\src;C:\Users\ck\.node-gyp\0.10.13\deps\uv\include;C:\Users\ck\.node-gyp\0.10.13\deps\v8\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
package/build/config.gypi CHANGED
@@ -5,7 +5,8 @@
5
5
  "default_configuration": "Release",
6
6
  "defines": [],
7
7
  "include_dirs": [],
8
- "libraries": []
8
+ "libraries": [],
9
+ "msbuild_toolset": "Windows7.1SDK"
9
10
  },
10
11
  "variables": {
11
12
  "clang": 0,
@@ -26,7 +27,7 @@
26
27
  "node_use_perfctr": "true",
27
28
  "node_use_systemtap": "false",
28
29
  "python": "C:\\Python27\\python.exe",
29
- "target_arch": "ia32",
30
+ "target_arch": "x64",
30
31
  "v8_enable_gdbjit": 0,
31
32
  "v8_no_strict_aliasing": 1,
32
33
  "v8_use_snapshot": "false",
package/ext/bson.cc CHANGED
@@ -65,10 +65,8 @@ void DataStream::CheckKey(const Local<String>& keyName)
65
65
  size_t keyLength = keyName->Utf8Length();
66
66
  if(keyLength == 0) return;
67
67
 
68
- // Allocate space for the key
68
+ // Allocate space for the key, do not need to zero terminate as WriteUtf8 does it
69
69
  char* keyStringBuffer = (char*) alloca(keyLength + 1);
70
- // Zero terminate the string
71
- keyStringBuffer[keyLength - 1] = 0x00;
72
70
  // Write the key to the allocated buffer
73
71
  keyName->WriteUtf8(keyStringBuffer);
74
72
  // Check for the zero terminator
@@ -408,16 +406,16 @@ Local<String> BSONDeserializer::ReadObjectId()
408
406
  return String::New(objectId, 12);
409
407
  }
410
408
 
411
- Handle<Value> BSONDeserializer::DeserializeDocument()
409
+ Handle<Value> BSONDeserializer::DeserializeDocument(bool promoteLongs)
412
410
  {
413
411
  uint32_t length = ReadUInt32();
414
412
  if(length < 5) ThrowAllocatedStringException(64, "Bad BSON: Document is less than 5 bytes");
415
413
 
416
414
  BSONDeserializer documentDeserializer(*this, length-4);
417
- return documentDeserializer.DeserializeDocumentInternal();
415
+ return documentDeserializer.DeserializeDocumentInternal(promoteLongs);
418
416
  }
419
417
 
420
- Handle<Value> BSONDeserializer::DeserializeDocumentInternal()
418
+ Handle<Value> BSONDeserializer::DeserializeDocumentInternal(bool promoteLongs)
421
419
  {
422
420
  Local<Object> returnObject = Object::New();
423
421
 
@@ -425,7 +423,7 @@ Handle<Value> BSONDeserializer::DeserializeDocumentInternal()
425
423
  {
426
424
  BsonType type = (BsonType) ReadByte();
427
425
  const Local<String>& name = ReadCString();
428
- const Handle<Value>& value = DeserializeValue(type);
426
+ const Handle<Value>& value = DeserializeValue(type, promoteLongs);
429
427
  returnObject->ForceSet(name, value);
430
428
  }
431
429
  if(p != pEnd) ThrowAllocatedStringException(64, "Bad BSON Document: Serialize consumed unexpected number of bytes");
@@ -443,16 +441,16 @@ Handle<Value> BSONDeserializer::DeserializeDocumentInternal()
443
441
  }
444
442
  }
445
443
 
446
- Handle<Value> BSONDeserializer::DeserializeArray()
444
+ Handle<Value> BSONDeserializer::DeserializeArray(bool promoteLongs)
447
445
  {
448
446
  uint32_t length = ReadUInt32();
449
447
  if(length < 5) ThrowAllocatedStringException(64, "Bad BSON: Array Document is less than 5 bytes");
450
448
 
451
449
  BSONDeserializer documentDeserializer(*this, length-4);
452
- return documentDeserializer.DeserializeArrayInternal();
450
+ return documentDeserializer.DeserializeArrayInternal(promoteLongs);
453
451
  }
454
452
 
455
- Handle<Value> BSONDeserializer::DeserializeArrayInternal()
453
+ Handle<Value> BSONDeserializer::DeserializeArrayInternal(bool promoteLongs)
456
454
  {
457
455
  Local<Array> returnArray = Array::New();
458
456
 
@@ -460,7 +458,7 @@ Handle<Value> BSONDeserializer::DeserializeArrayInternal()
460
458
  {
461
459
  BsonType type = (BsonType) ReadByte();
462
460
  uint32_t index = ReadIntegerString();
463
- const Handle<Value>& value = DeserializeValue(type);
461
+ const Handle<Value>& value = DeserializeValue(type, promoteLongs);
464
462
  returnArray->Set(index, value);
465
463
  }
466
464
  if(p != pEnd) ThrowAllocatedStringException(64, "Bad BSON Array: Serialize consumed unexpected number of bytes");
@@ -468,7 +466,7 @@ Handle<Value> BSONDeserializer::DeserializeArrayInternal()
468
466
  return returnArray;
469
467
  }
470
468
 
471
- Handle<Value> BSONDeserializer::DeserializeValue(BsonType type)
469
+ Handle<Value> BSONDeserializer::DeserializeValue(BsonType type, bool promoteLongs)
472
470
  {
473
471
  switch(type)
474
472
  {
@@ -517,7 +515,7 @@ Handle<Value> BSONDeserializer::DeserializeValue(BsonType type)
517
515
  {
518
516
  ReadUInt32();
519
517
  const Local<Value>& code = ReadString();
520
- const Handle<Value>& scope = DeserializeDocument();
518
+ const Handle<Value>& scope = DeserializeDocument(promoteLongs);
521
519
  Local<Value> argv[] = { code, scope->ToObject() };
522
520
  return bson->codeConstructor->NewInstance(2, argv);
523
521
  }
@@ -549,6 +547,18 @@ Handle<Value> BSONDeserializer::DeserializeValue(BsonType type)
549
547
  int32_t lowBits = (int32_t) ReadInt32();
550
548
  int32_t highBits = (int32_t) ReadInt32();
551
549
 
550
+ // Promote long is enabled
551
+ if(promoteLongs) {
552
+ // If value is < 2^53 and >-2^53
553
+ if((highBits < 0x200000 || (highBits == 0x200000 && lowBits == 0)) && highBits >= -0x200000) {
554
+ // Adjust the pointer and read as 64 bit value
555
+ p -= 8;
556
+ // Read the 64 bit value
557
+ int64_t finalValue = (int64_t) ReadInt64();
558
+ return Number::New(finalValue);
559
+ }
560
+ }
561
+
552
562
  // Decode the Long value
553
563
  Local<Value> argv[] = { Int32::New(lowBits), Int32::New(highBits) };
554
564
  return bson->longConstructor->NewInstance(2, argv);
@@ -558,10 +568,10 @@ Handle<Value> BSONDeserializer::DeserializeValue(BsonType type)
558
568
  return Date::New((double) ReadInt64());
559
569
 
560
570
  case BSON_TYPE_ARRAY:
561
- return DeserializeArray();
571
+ return DeserializeArray(promoteLongs);
562
572
 
563
573
  case BSON_TYPE_OBJECT:
564
- return DeserializeDocument();
574
+ return DeserializeDocument(promoteLongs);
565
575
 
566
576
  case BSON_TYPE_SYMBOL:
567
577
  {
@@ -733,11 +743,21 @@ Handle<Value> BSON::BSONDeserialize(const Arguments &args)
733
743
  {
734
744
  HandleScope scope;
735
745
 
736
- // Ensure that we have an parameter
737
- if(Buffer::HasInstance(args[0]) && args.Length() > 1) return VException("One argument required - buffer1.");
738
- if(args[0]->IsString() && args.Length() > 1) return VException("One argument required - string1.");
739
- // Throw an exception if the argument is not of type Buffer
740
- if(!Buffer::HasInstance(args[0]) && !args[0]->IsString()) return VException("Argument must be a Buffer or String.");
746
+ // Fail if the first argument is not a string or a buffer
747
+ if(args.Length() > 1 && !args[0]->IsString() && !Buffer::HasInstance(args[0]))
748
+ return VException("First Argument must be a Buffer or String.");
749
+
750
+ // Promote longs
751
+ bool promoteLongs = true;
752
+
753
+ // If we have an options object
754
+ if(args.Length() == 2 && args[1]->IsObject()) {
755
+ Local<Object> options = args[1]->ToObject();
756
+
757
+ if(options->Has(String::New("promoteLongs"))) {
758
+ promoteLongs = options->Get(String::New("promoteLongs"))->ToBoolean()->Value();
759
+ }
760
+ }
741
761
 
742
762
  // Define pointer to data
743
763
  Local<Object> obj = args[0]->ToObject();
@@ -763,7 +783,8 @@ Handle<Value> BSON::BSONDeserialize(const Arguments &args)
763
783
  try
764
784
  {
765
785
  BSONDeserializer deserializer(bson, data, length);
766
- return deserializer.DeserializeDocument();
786
+ // deserializer.promoteLongs = promoteLongs;
787
+ return deserializer.DeserializeDocument(promoteLongs);
767
788
  }
768
789
  catch(char* exception)
769
790
  {
@@ -788,7 +809,8 @@ Handle<Value> BSON::BSONDeserialize(const Arguments &args)
788
809
  try
789
810
  {
790
811
  BSONDeserializer deserializer(bson, data, len);
791
- Handle<Value> result = deserializer.DeserializeDocument();
812
+ // deserializer.promoteLongs = promoteLongs;
813
+ Handle<Value> result = deserializer.DeserializeDocument(promoteLongs);
792
814
  free(data);
793
815
  return result;
794
816
 
@@ -966,6 +988,17 @@ Handle<Value> BSON::BSONDeserializeStream(const Arguments &args)
966
988
  uint32_t numberOfDocuments = args[2]->Uint32Value();
967
989
  uint32_t index = args[1]->Uint32Value();
968
990
  uint32_t resultIndex = args[4]->Uint32Value();
991
+ bool promoteLongs = true;
992
+
993
+ // Check for the value promoteLongs in the options object
994
+ if(args.Length() == 6) {
995
+ Local<Object> options = args[5]->ToObject();
996
+
997
+ // Check if we have the promoteLong variable
998
+ if(options->Has(String::New("promoteLongs"))) {
999
+ promoteLongs = options->Get(String::New("promoteLongs"))->ToBoolean()->Value();
1000
+ }
1001
+ }
969
1002
 
970
1003
  // Unpack the BSON parser instance
971
1004
  BSON *bson = ObjectWrap::Unwrap<BSON>(args.This());
@@ -988,7 +1021,7 @@ Handle<Value> BSON::BSONDeserializeStream(const Arguments &args)
988
1021
  {
989
1022
  try
990
1023
  {
991
- documents->Set(i + resultIndex, deserializer.DeserializeDocument());
1024
+ documents->Set(i + resultIndex, deserializer.DeserializeDocument(promoteLongs));
992
1025
  }
993
1026
  catch (char* exception)
994
1027
  {
package/ext/bson.h CHANGED
@@ -234,7 +234,7 @@ public:
234
234
  BSONDeserializer(BSON* aBson, char* data, size_t length);
235
235
  BSONDeserializer(BSONDeserializer& parentSerializer, size_t length);
236
236
 
237
- Handle<Value> DeserializeDocument();
237
+ Handle<Value> DeserializeDocument(bool promoteLongs);
238
238
 
239
239
  bool HasMoreData() const { return p < pEnd; }
240
240
  Local<String> ReadCString();
@@ -259,10 +259,10 @@ public:
259
259
  size_t GetSerializeSize() const { return p - pStart; }
260
260
 
261
261
  private:
262
- Handle<Value> DeserializeArray();
263
- Handle<Value> DeserializeValue(BsonType type);
264
- Handle<Value> DeserializeDocumentInternal();
265
- Handle<Value> DeserializeArrayInternal();
262
+ Handle<Value> DeserializeArray(bool promoteLongs);
263
+ Handle<Value> DeserializeValue(BsonType type, bool promoteLongs);
264
+ Handle<Value> DeserializeDocumentInternal(bool promoteLongs);
265
+ Handle<Value> DeserializeArrayInternal(bool promoteLongs);
266
266
 
267
267
  BSON* bson;
268
268
  char* const pStart;
Binary file
Binary file
package/lib/bson/bson.js CHANGED
@@ -1053,6 +1053,7 @@ var crc32 = function(string, start, end) {
1053
1053
  * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1054
1054
  * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1055
1055
  * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1056
+ * - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
1056
1057
  *
1057
1058
  * @param {Buffer} data the buffer containing the serialized set of BSON documents.
1058
1059
  * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
@@ -1142,6 +1143,7 @@ var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
1142
1143
  * - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1143
1144
  * - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1144
1145
  * - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1146
+ * - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
1145
1147
  *
1146
1148
  * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
1147
1149
  * @param {Object} [options] additional options used for the deserialization.
@@ -1155,6 +1157,7 @@ BSON.deserialize = function(buffer, options, isArray) {
1155
1157
  var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
1156
1158
  var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
1157
1159
  var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
1160
+ var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
1158
1161
 
1159
1162
  // Validate that we have at least 4 bytes of buffer
1160
1163
  if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
@@ -1309,8 +1312,14 @@ BSON.deserialize = function(buffer, options, isArray) {
1309
1312
  // Unpack the low and high bits
1310
1313
  var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1311
1314
  var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1312
- // Set the object
1313
- object[name] = new Long(lowBits, highBits);
1315
+ // Create long object
1316
+ var long = new Long(lowBits, highBits);
1317
+ // Promote the long if possible
1318
+ if(promoteLongs) {
1319
+ object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
1320
+ } else {
1321
+ object[name] = long;
1322
+ }
1314
1323
  break;
1315
1324
  case BSON.BSON_DATA_SYMBOL:
1316
1325
  // Read the content of the field
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  { "name" : "bson"
2
2
  , "description" : "A bson parser for node.js and the browser"
3
3
  , "keywords" : ["mongodb", "bson", "parser"]
4
- , "version" : "0.2.1"
4
+ , "version" : "0.2.2"
5
5
  , "author" : "Christian Amor Kvalheim <christkv@gmail.com>"
6
6
  , "contributors" : []
7
7
 
Binary file