bson 1.0.0 → 1.0.4

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.
package/HISTORY.md CHANGED
@@ -1,3 +1,23 @@
1
+ 1.0.4 2016-01-11
2
+ ----------------
3
+ - #204 remove Buffer.from as it's partially broken in early 4.x.x. series of node releases.
4
+
5
+ 1.0.3 2016-01-03
6
+ ----------------
7
+ - Fixed toString for ObjectId so it will work with inspect.
8
+
9
+ 1.0.2 2016-01-02
10
+ ----------------
11
+ - Minor optimizations for ObjectID to use Buffer.from where available.
12
+
13
+ 1.0.1 2016-12-06
14
+ ----------------
15
+ - Reverse behavior for undefined to be serialized as NULL. MongoDB 3.4 does not allow for undefined comparisons.
16
+
17
+ 1.0.0 2016-12-06
18
+ ----------------
19
+ - Introduced new BSON API and documentation.
20
+
1
21
  0.5.7 2016-11-18
2
22
  -----------------
3
23
  - NODE-848 BSON Regex flags must be alphabetically ordered.
package/README.md CHANGED
@@ -57,7 +57,7 @@ console.log('doc_2:', doc_2)
57
57
 
58
58
  ## Installation
59
59
 
60
- `npm install bson-ext`
60
+ `npm install bson`
61
61
 
62
62
  ## API
63
63
 
@@ -10,6 +10,12 @@ var MACHINE_ID = parseInt(Math.random() * 0xFFFFFF, 10);
10
10
 
11
11
  // Regular expression that checks for hex value
12
12
  var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
13
+ var hasBufferType = false;
14
+
15
+ // Check if buffer exists
16
+ try {
17
+ if(Buffer && Buffer.from) hasBufferType = true;
18
+ } catch(err) {};
13
19
 
14
20
  /**
15
21
  * Create a new ObjectID instance
@@ -26,17 +32,26 @@ var ObjectID = function ObjectID(id) {
26
32
 
27
33
  this._bsontype = 'ObjectID';
28
34
 
29
- var __id = null;
35
+ // The most common usecase (blank id, new objectId instance)
36
+ if(id == null || typeof id == 'number') {
37
+ // Generate a new id
38
+ this.id = this.generate(id);
39
+ // If we are caching the hex string
40
+ if(ObjectID.cacheHexString) this.__id = this.toString('hex');
41
+ // Return the object
42
+ return;
43
+ }
44
+
45
+ // Check if the passed in id is valid
30
46
  var valid = ObjectID.isValid(id);
31
47
 
32
48
  // Throw an error if it's not a valid setup
33
49
  if(!valid && id != null){
34
50
  throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
51
+ } else if(valid && typeof id == 'string' && id.length == 24 && hasBufferType) {
52
+ return new ObjectID(new Buffer(id, 'hex'));
35
53
  } else if(valid && typeof id == 'string' && id.length == 24) {
36
54
  return ObjectID.createFromHexString(id);
37
- } else if(id == null || typeof id == 'number') {
38
- // convert to 12 byte binary string
39
- this.id = this.generate(id);
40
55
  } else if(id != null && id.length === 12) {
41
56
  // assume 12 byte string
42
57
  this.id = id;
@@ -47,7 +62,7 @@ var ObjectID = function ObjectID(id) {
47
62
  throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
48
63
  }
49
64
 
50
- if(ObjectID.cacheHexString) this.__id = this.toHexString();
65
+ if(ObjectID.cacheHexString) this.__id = this.toString('hex');
51
66
  };
52
67
 
53
68
  // Allow usage of ObjectId as well as ObjectID
@@ -149,10 +164,17 @@ ObjectID.prototype.generate = function(time) {
149
164
  /**
150
165
  * Converts the id into a 24 byte hex string for printing
151
166
  *
167
+ * @param {String} format The Buffer toString format parameter.
152
168
  * @return {String} return the 24 byte hex string representation.
153
169
  * @ignore
154
170
  */
155
- ObjectID.prototype.toString = function() {
171
+ ObjectID.prototype.toString = function(format) {
172
+ // Is the id a buffer then use the buffer toString method to return the format
173
+ if(this.id && this.id.copy) {
174
+ return this.id.toString(typeof format === 'string' ? format : 'hex');
175
+ }
176
+
177
+ // if(this.buffer )
156
178
  return this.toHexString();
157
179
  };
158
180
 
@@ -263,22 +285,19 @@ var convertToHex = function(bytes) {
263
285
  */
264
286
  ObjectID.createFromHexString = function createFromHexString (string) {
265
287
  // Throw an error if it's not a valid setup
266
- if(typeof string === 'undefined' || string != null && string.length != 24)
288
+ if(typeof string === 'undefined' || string != null && string.length != 24) {
267
289
  throw new Error("Argument passed in must be a single String of 12 bytes or a string of 24 hex characters");
268
-
269
- var length = string.length;
270
-
271
- if(length > 12*2) {
272
- throw new Error('Id cannot be longer than 12 bytes');
273
290
  }
274
291
 
292
+ // Use Buffer.from method if available
293
+ if(hasBufferType) return new ObjectID(new Buffer(string, 'hex'));
294
+
275
295
  // Calculate lengths
276
- var sizeof = length >> 1;
277
- var array = new _Buffer(sizeof);
296
+ var array = new _Buffer(12);
278
297
  var n = 0;
279
298
  var i = 0;
280
299
 
281
- while (i < length) {
300
+ while (i < 24) {
282
301
  array[n++] = decodeLookup[string.charCodeAt(i++)] << 4 | decodeLookup[string.charCodeAt(i++)]
283
302
  }
284
303
 
@@ -125,17 +125,6 @@ var serializeNumber = function(buffer, key, value, index, isArray) {
125
125
  return index;
126
126
  }
127
127
 
128
- var serializeUndefined = function(buffer, key, value, index, isArray) {
129
- // Set long type
130
- buffer[index++] = BSON.BSON_DATA_UNDEFINED;
131
- // Number of written bytes
132
- var numberOfWrittenBytes = !isArray ? buffer.write(key, index, 'utf8') : buffer.write(key, index, 'ascii');
133
- // Encode the name
134
- index = index + numberOfWrittenBytes;
135
- buffer[index++] = 0;
136
- return index;
137
- }
138
-
139
128
  var serializeNull = function(buffer, key, value, index, isArray) {
140
129
  // Set long type
141
130
  buffer[index++] = BSON.BSON_DATA_NULL;
@@ -695,9 +684,7 @@ var serializeInto = function serializeInto(buffer, object, checkKeys, startingIn
695
684
  } else if(value instanceof Date || isDate(value)) {
696
685
  index = serializeDate(buffer, key, value, index);
697
686
  } else if(value === undefined && ignoreUndefined == true) {
698
- } else if(value === undefined) {
699
- index = serializeUndefined(buffer, key, value, index);
700
- } else if(value === null) {
687
+ } else if(value === null || value === undefined) {
701
688
  index = serializeNull(buffer, key, value, index);
702
689
  } else if(value['_bsontype'] == 'ObjectID') {
703
690
  index = serializeObjectId(buffer, key, value, index);
@@ -777,9 +764,7 @@ var serializeInto = function serializeInto(buffer, object, checkKeys, startingIn
777
764
  } else if(value instanceof Date || isDate(value)) {
778
765
  index = serializeDate(buffer, key, value, index);
779
766
  } else if(value === undefined && ignoreUndefined == true) {
780
- } else if(value === undefined) {
781
- index = serializeUndefined(buffer, key, value, index);
782
- } else if(value === null) {
767
+ } else if(value === null || value === undefined) {
783
768
  index = serializeNull(buffer, key, value, index);
784
769
  } else if(value['_bsontype'] == 'ObjectID') {
785
770
  index = serializeObjectId(buffer, key, value, index);
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "browser_build",
13
13
  "bower.json"
14
14
  ],
15
- "version": "1.0.0",
15
+ "version": "1.0.4",
16
16
  "author": "Christian Amor Kvalheim <christkv@gmail.com>",
17
17
  "contributors": [],
18
18
  "repository": "mongodb/js-bson",