File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change 22// that writes to the database.
33// This could be either a "create" or an "update".
44
5+ var crypto = require ( 'crypto' ) ;
56var deepcopy = require ( 'deepcopy' ) ;
67var rack = require ( 'hat' ) . rack ( ) ;
78
@@ -701,15 +702,18 @@ RestWrite.prototype.objectId = function() {
701702 return this . data . objectId || this . query . objectId ;
702703} ;
703704
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.
706706function newObjectId ( ) {
707707 var chars = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
708708 'abcdefghijklmnopqrstuvwxyz' +
709709 '0123456789' ) ;
710710 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 ] ;
713717 }
714718 return objectId ;
715719}
You can’t perform that action at this time.
0 commit comments