File tree 1 file changed +8
-4
lines changed
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change 2
2
// that writes to the database.
3
3
// This could be either a "create" or an "update".
4
4
5
+ var crypto = require ( 'crypto' ) ;
5
6
var deepcopy = require ( 'deepcopy' ) ;
6
7
var rack = require ( 'hat' ) . rack ( ) ;
7
8
@@ -702,15 +703,18 @@ RestWrite.prototype.objectId = function() {
702
703
return this . data . objectId || this . query . objectId ;
703
704
} ;
704
705
705
- // Returns a string that's usable as an object id.
706
- // Probably unique. Good enough? Probably!
706
+ // Returns a unique string that's usable as an object id.
707
707
function newObjectId ( ) {
708
708
var chars = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
709
709
'abcdefghijklmnopqrstuvwxyz' +
710
710
'0123456789' ) ;
711
711
var objectId = '' ;
712
- for ( var i = 0 ; i < 10 ; ++ i ) {
713
- objectId += chars [ Math . floor ( Math . random ( ) * chars . length ) ] ;
712
+ var bytes = crypto . randomBytes ( 10 ) ;
713
+ for ( var i = 0 ; i < bytes . length ; ++ i ) {
714
+ // Note: there is a slight modulo bias, because chars length
715
+ // of 62 doesn't divide the number of all bytes (256) evenly.
716
+ // It is acceptable for our purposes.
717
+ objectId += chars [ bytes . readUInt8 ( i ) % chars . length ] ;
714
718
}
715
719
return objectId ;
716
720
}
You can’t perform that action at this time.
0 commit comments