@@ -10,6 +10,13 @@ export function isBrowser() {
10
10
) ;
11
11
}
12
12
13
+ export function evalRequire ( lib : string ) {
14
+ if ( isBrowser ( ) ) {
15
+ throw new Error ( 'This method is not usable in the browser' ) ;
16
+ }
17
+ return require ( lib ) ;
18
+ }
19
+
13
20
export function getUuid ( ) {
14
21
return uuidv4 ( ) ;
15
22
}
@@ -102,7 +109,7 @@ export class Hash {
102
109
if ( isBrowser ( ) ) {
103
110
this . #hash = undefined ;
104
111
} else {
105
- this . #hash = eval ( 'require' ) ( 'crypto' ) . createHash ( algorithm ) ;
112
+ this . #hash = evalRequire ( 'crypto' ) . createHash ( algorithm ) ;
106
113
}
107
114
}
108
115
@@ -153,7 +160,7 @@ export function generateByteBuffer(size: number): Buffer {
153
160
window . crypto . getRandomValues ( buffer ) ;
154
161
return Buffer . from ( buffer ) ;
155
162
}
156
- const crypto = eval ( 'require' ) ( 'crypto' ) ;
163
+ const crypto = evalRequire ( 'crypto' ) ;
157
164
return crypto . randomBytes ( size ) ;
158
165
}
159
166
@@ -167,7 +174,7 @@ export function generateByteStreamFromBuffer(
167
174
controller . close ( ) ;
168
175
} ,
169
176
} )
170
- : eval ( 'require' ) ( 'stream' ) . Readable . from ( Buffer . from ( buffer ) ) ;
177
+ : evalRequire ( 'stream' ) . Readable . from ( Buffer . from ( buffer ) ) ;
171
178
}
172
179
173
180
export function generateByteStream ( size : number ) : Readable {
@@ -196,7 +203,7 @@ export function decodeBase64ByteStream(data: string): Readable {
196
203
controller . close ( ) ;
197
204
} ,
198
205
} )
199
- : eval ( 'require' ) ( 'stream' ) . Readable . from ( Buffer . from ( data , 'base64' ) ) ;
206
+ : evalRequire ( 'stream' ) . Readable . from ( Buffer . from ( data , 'base64' ) ) ;
200
207
}
201
208
202
209
export function stringToByteStream ( data : string ) : Readable {
@@ -212,7 +219,7 @@ export function stringToByteStream(data: string): Readable {
212
219
controller . close ( ) ;
213
220
} ,
214
221
} )
215
- : eval ( 'require' ) ( 'stream' ) . Readable . from ( Buffer . from ( data , 'ascii' ) ) ;
222
+ : evalRequire ( 'stream' ) . Readable . from ( Buffer . from ( data , 'ascii' ) ) ;
216
223
}
217
224
218
225
export async function readByteStream ( byteStream : Readable ) : Promise < Buffer > {
@@ -365,7 +372,7 @@ export async function createJwtAssertion(
365
372
key : JwtKey ,
366
373
options : JwtSignOptions ,
367
374
) : Promise < string > {
368
- const crypto = eval ( 'require' ) ( 'crypto' ) ;
375
+ const crypto = evalRequire ( 'crypto' ) ;
369
376
const privateKey = crypto . createPrivateKey ( {
370
377
key : key . key ,
371
378
format : 'pem' ,
@@ -394,7 +401,7 @@ export async function createJwtAssertion(
394
401
* Reads a text file and returns its content.
395
402
*/
396
403
export function readTextFromFile ( filepath : string ) : string {
397
- return eval ( 'require' ) ( 'fs' ) . readFileSync ( filepath , 'utf8' ) ;
404
+ return evalRequire ( 'fs' ) . readFileSync ( filepath , 'utf8' ) ;
398
405
}
399
406
400
407
/**
@@ -411,7 +418,7 @@ export function createAgent(options?: AgentOptions, proxyConfig?: any): Agent {
411
418
if ( isBrowser ( ) ) {
412
419
return undefined ;
413
420
}
414
- const ProxyAgent = eval ( 'require' ) ( 'proxy-agent' ) . ProxyAgent ;
421
+ const ProxyAgent = evalRequire ( 'proxy-agent' ) . ProxyAgent ;
415
422
let agentOptions = options ;
416
423
417
424
if ( proxyConfig && proxyConfig . url ) {
@@ -519,7 +526,7 @@ export async function computeWebhookSignature(
519
526
const result = await hmac . digest ( 'binary' ) ;
520
527
signature = Buffer . from ( result ) . toString ( 'base64' ) ;
521
528
} else {
522
- let crypto = eval ( 'require' ) ( 'crypto' ) ;
529
+ let crypto = evalRequire ( 'crypto' ) ;
523
530
let hmac = crypto . createHmac ( 'sha256' , signatureKey ) ;
524
531
hmac . update ( escapedBody ) ;
525
532
hmac . update ( headers [ 'box-delivery-timestamp' ] ) ;
0 commit comments