-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobjectid.js
86 lines (74 loc) · 2.49 KB
/
objectid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (C) 2014 Vaughn Iverson
//
// Adapted from:
// https://github.com/meteor/meteor/blob/5a5204e3a468b7b498baefb259c88a63b23699cf/packages/minimongo/objectid.js
// https://github.com/meteor/meteor/blob/cc667a487f0f32cf592df14aae80f45d0b3b6d84/packages/random/random.js
//
// Copyright (C) 2011-2014 Meteor Development Group
//
// All contributions licensed under the MIT license: see LICENSE file in this directory
nodeCrypto = require('crypto');
randomHexString = function (digits) {
var self = this;
var numBytes = Math.ceil(digits / 2);
var bytes;
// Try to get cryptographically strong randomness. Fall back to
// non-cryptographically strong if not available.
try {
bytes = nodeCrypto.randomBytes(numBytes);
} catch (e) {
// XXX should re-throw any error except insufficient entropy
bytes = nodeCrypto.pseudoRandomBytes(numBytes);
}
var result = bytes.toString("hex");
// If the number of digits is odd, we'll have generated an extra 4 bits
// of randomness, so we need to trim the last digit.
return result.substring(0, digits);
};
looksLikeObjectID = function (str) {
return str.length === 24 && str.match(/^[0-9a-f]*$/);
};
ObjectID = function (hexString) {
// Make new optional
if (!(this instanceof ObjectID))
return new ObjectID(hexString);
//random-based impl of Mongo ObjectID
var self = this;
if (hexString && (typeof hexString === 'string')) {
hexString = hexString.toLowerCase();
if (!looksLikeObjectID(hexString)) {
throw new Error("Invalid hexadecimal string for creating an ObjectID");
}
// meant to work with _.isEqual(), which relies on structural equality
self._str = hexString;
} else {
self._str = randomHexString(24);
}
};
ObjectID.prototype.toString = function () {
var self = this;
return "ObjectID(\"" + self._str + "\")";
};
ObjectID.prototype.equals = function (other) {
var self = this;
return other instanceof ObjectID &&
self.valueOf() === other.valueOf();
};
ObjectID.prototype.clone = function () {
var self = this;
return new ObjectID(self._str);
};
ObjectID.prototype.typeName = function() {
return "oid";
};
// Only use this if mongodb made this ID, otherwise this will be nonsense
ObjectID.prototype.getTimestamp = function() {
var self = this;
return parseInt(self._str.substr(0, 8), 16);
};
ObjectID.prototype.valueOf =
ObjectID.prototype.toJSONValue =
ObjectID.prototype.toHexString =
function () { return this._str; };
// Export ObjectID
module.exports = ObjectID