@@ -105,14 +105,25 @@ function shimIterator() {
105105}
106106
107107function shimCryptoUuid ( ) {
108+ let crypto = global . crypto ;
108109 if ( typeof global . crypto ?. randomUUID !== 'function' ) {
109- global . crypto . randomUUID = ( ) =>
110- 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
111- /* eslint-disable no-bitwise */
112- const r = ( Math . random ( ) * 16 ) | 0 ;
113- const v = c === 'x' ? r : ( r & 0x3 ) | 0x8 ;
114- return v . toString ( 16 ) ;
115- } ) as `${string } -${string } -${string } -${string } -${string } `;
110+ let createRandomUUID = ( ) => {
111+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace (
112+ / [ x y ] / g,
113+ function ( c ) {
114+ /* eslint-disable no-bitwise */
115+ const r = ( Math . random ( ) * 16 ) | 0 ;
116+ const v = c === 'x' ? r : ( r & 0x3 ) | 0x8 ;
117+ return v . toString ( 16 ) ;
118+ }
119+ ) as `${string } -${string } -${string } -${string } -${string } `;
120+ } ;
121+
122+ if ( ! crypto ) {
123+ crypto = { } as typeof global . crypto ;
124+ global . crypto = crypto ;
125+ }
126+ crypto . randomUUID = createRandomUUID ;
116127 }
117128}
118129
@@ -126,7 +137,7 @@ function shimWebstreams() {
126137 // @ts -expect-error: global.ReadableStream isn't typed here.
127138 if ( typeof global . ReadableStream === 'undefined' ) {
128139 // @ts -expect-error
129- global . WritableStream = ReadableStream ;
140+ global . ReadableStream = ReadableStream ;
130141 }
131142}
132143
0 commit comments