@@ -3,6 +3,7 @@ import Help from './help';
3
3
import { BinaryType , bson as BSON } from '@mongosh/service-provider-core' ;
4
4
import { CommonErrors , MongoshInternalError , MongoshInvalidInputError } from '@mongosh/errors' ;
5
5
import { assertArgsDefined , assertArgsType } from './helpers' ;
6
+ import { randomBytes } from 'crypto' ;
6
7
7
8
function constructHelp ( className : string ) : Help {
8
9
const classHelpKeyPrefix = `shell-api.classes.${ className } .help` ;
@@ -145,12 +146,18 @@ export default function constructShellBson(bson: typeof BSON): any {
145
146
const buffer = Buffer . from ( hexstr , 'hex' ) ;
146
147
return new bson . Binary ( buffer , subtype ) ;
147
148
} ,
148
- UUID : function ( hexstr : string ) : BinaryType {
149
- assertArgsDefined ( hexstr ) ;
149
+ UUID : function ( hexstr ?: string ) : BinaryType {
150
+ if ( hexstr === undefined ) {
151
+ // Generate a version 4, variant 1 UUID, like the old shell did.
152
+ const uuid = randomBytes ( 16 ) ;
153
+ uuid [ 6 ] = ( uuid [ 6 ] & 0x0f ) | 0x40 ;
154
+ uuid [ 8 ] = ( uuid [ 8 ] & 0x3f ) | 0x80 ;
155
+ hexstr = uuid . toString ( 'hex' ) ;
156
+ }
150
157
assertArgsType ( [ hexstr ] , [ 'string' ] ) ;
151
158
// Strip any dashes, as they occur in the standard UUID formatting
152
159
// (e.g. 01234567-89ab-cdef-0123-456789abcdef).
153
- const buffer = Buffer . from ( hexstr . replace ( / - / g, '' ) , 'hex' ) ;
160
+ const buffer = Buffer . from ( ( hexstr as string ) . replace ( / - / g, '' ) , 'hex' ) ;
154
161
return new bson . Binary ( buffer , bson . Binary . SUBTYPE_UUID ) ;
155
162
} ,
156
163
MD5 : function ( hexstr : string ) : BinaryType {
0 commit comments