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
@@ -701,15 +702,18 @@ RestWrite.prototype.objectId = function() {
701
702
return this . data . objectId || this . query . objectId ;
702
703
} ;
703
704
704
- // Returns a string that's usable as an object id.
705
- // Probably unique. Good enough? Probably!
705
+ // Returns a unique string that's usable as an object id.
706
706
function newObjectId ( ) {
707
707
var chars = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
708
708
'abcdefghijklmnopqrstuvwxyz' +
709
709
'0123456789' ) ;
710
710
var objectId = '' ;
711
- for ( var i = 0 ; i < 10 ; ++ i ) {
712
- objectId += chars [ Math . floor ( Math . random ( ) * chars . length ) ] ;
711
+ var bytes = crypto . randomBytes ( 10 ) ;
712
+ for ( var i = 0 ; i < bytes . length ; ++ i ) {
713
+ // Note: there is a slight modulo bias, because chars length
714
+ // of 62 doesn't divide the number of all bytes (256) evenly.
715
+ // It is acceptable for our purposes.
716
+ objectId += chars [ bytes . readUInt8 ( i ) % chars . length ] ;
713
717
}
714
718
return objectId ;
715
719
}
You can’t perform that action at this time.
0 commit comments