Skip to content

Commit 7c86f34

Browse files
fix: Remove eval usage (box/box-codegen#636) (#474)
1 parent 660dc6e commit 7c86f34

File tree

6 files changed

+33
-24
lines changed

6 files changed

+33
-24
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "ab51050", "specHash": "00c93fe", "version": "1.10.0" }
1+
{ "engineHash": "b2c3124", "specHash": "90b039e", "version": "1.10.0" }

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
"rollup": "^4.4.0"
5151
},
5252
"files": [
53-
"lib"
53+
"lib",
54+
"src",
55+
"README.md",
56+
"LICENSE"
5457
]
5558
}

src/internal/utils.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export function isBrowser() {
1010
);
1111
}
1212

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+
1320
export function getUuid() {
1421
return uuidv4();
1522
}
@@ -102,7 +109,7 @@ export class Hash {
102109
if (isBrowser()) {
103110
this.#hash = undefined;
104111
} else {
105-
this.#hash = eval('require')('crypto').createHash(algorithm);
112+
this.#hash = evalRequire('crypto').createHash(algorithm);
106113
}
107114
}
108115

@@ -153,7 +160,7 @@ export function generateByteBuffer(size: number): Buffer {
153160
window.crypto.getRandomValues(buffer);
154161
return Buffer.from(buffer);
155162
}
156-
const crypto = eval('require')('crypto');
163+
const crypto = evalRequire('crypto');
157164
return crypto.randomBytes(size);
158165
}
159166

@@ -167,7 +174,7 @@ export function generateByteStreamFromBuffer(
167174
controller.close();
168175
},
169176
})
170-
: eval('require')('stream').Readable.from(Buffer.from(buffer));
177+
: evalRequire('stream').Readable.from(Buffer.from(buffer));
171178
}
172179

173180
export function generateByteStream(size: number): Readable {
@@ -196,7 +203,7 @@ export function decodeBase64ByteStream(data: string): Readable {
196203
controller.close();
197204
},
198205
})
199-
: eval('require')('stream').Readable.from(Buffer.from(data, 'base64'));
206+
: evalRequire('stream').Readable.from(Buffer.from(data, 'base64'));
200207
}
201208

202209
export function stringToByteStream(data: string): Readable {
@@ -212,7 +219,7 @@ export function stringToByteStream(data: string): Readable {
212219
controller.close();
213220
},
214221
})
215-
: eval('require')('stream').Readable.from(Buffer.from(data, 'ascii'));
222+
: evalRequire('stream').Readable.from(Buffer.from(data, 'ascii'));
216223
}
217224

218225
export async function readByteStream(byteStream: Readable): Promise<Buffer> {
@@ -365,7 +372,7 @@ export async function createJwtAssertion(
365372
key: JwtKey,
366373
options: JwtSignOptions,
367374
): Promise<string> {
368-
const crypto = eval('require')('crypto');
375+
const crypto = evalRequire('crypto');
369376
const privateKey = crypto.createPrivateKey({
370377
key: key.key,
371378
format: 'pem',
@@ -394,7 +401,7 @@ export async function createJwtAssertion(
394401
* Reads a text file and returns its content.
395402
*/
396403
export function readTextFromFile(filepath: string): string {
397-
return eval('require')('fs').readFileSync(filepath, 'utf8');
404+
return evalRequire('fs').readFileSync(filepath, 'utf8');
398405
}
399406

400407
/**
@@ -411,7 +418,7 @@ export function createAgent(options?: AgentOptions, proxyConfig?: any): Agent {
411418
if (isBrowser()) {
412419
return undefined;
413420
}
414-
const ProxyAgent = eval('require')('proxy-agent').ProxyAgent;
421+
const ProxyAgent = evalRequire('proxy-agent').ProxyAgent;
415422
let agentOptions = options;
416423

417424
if (proxyConfig && proxyConfig.url) {
@@ -519,7 +526,7 @@ export async function computeWebhookSignature(
519526
const result = await hmac.digest('binary');
520527
signature = Buffer.from(result).toString('base64');
521528
} else {
522-
let crypto = eval('require')('crypto');
529+
let crypto = evalRequire('crypto');
523530
let hmac = crypto.createHmac('sha256', signatureKey);
524531
hmac.update(escapedBody);
525532
hmac.update(headers['box-delivery-timestamp']);

src/managers/chunkedUploads.generated.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { serializeFileFull } from '../schemas/fileFull.generated.js';
2-
import { deserializeFileFull } from '../schemas/fileFull.generated.js';
31
import { serializeUploadSession } from '../schemas/uploadSession.generated.js';
42
import { deserializeUploadSession } from '../schemas/uploadSession.generated.js';
53
import { serializeClientError } from '../schemas/clientError.generated.js';
@@ -12,10 +10,11 @@ import { serializeFiles } from '../schemas/files.generated.js';
1210
import { deserializeFiles } from '../schemas/files.generated.js';
1311
import { serializeUploadPart } from '../schemas/uploadPart.generated.js';
1412
import { deserializeUploadPart } from '../schemas/uploadPart.generated.js';
13+
import { serializeFileFull } from '../schemas/fileFull.generated.js';
14+
import { deserializeFileFull } from '../schemas/fileFull.generated.js';
1515
import { ResponseFormat } from '../networking/fetchOptions.generated.js';
1616
import { Buffer } from '../internal/utils.js';
1717
import { HashName } from '../internal/utils.js';
18-
import { FileFull } from '../schemas/fileFull.generated.js';
1918
import { Iterator } from '../internal/utils.js';
2019
import { UploadSession } from '../schemas/uploadSession.generated.js';
2120
import { ClientError } from '../schemas/clientError.generated.js';
@@ -41,6 +40,7 @@ import { readByteStream } from '../internal/utils.js';
4140
import { reduceIterator } from '../internal/utils.js';
4241
import { Hash } from '../internal/utils.js';
4342
import { bufferLength } from '../internal/utils.js';
43+
import { FileFull } from '../schemas/fileFull.generated.js';
4444
import { sdIsEmpty } from '../serialization/json.js';
4545
import { sdIsBoolean } from '../serialization/json.js';
4646
import { sdIsNumber } from '../serialization/json.js';

src/networking/boxNetworkClient.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { sha1 } from 'hash-wasm'; // Use hash-wasm to calculate SHA1 hash in bro
55
import { BoxApiError, BoxSdkError } from '../box/errors';
66
import {
77
ByteStream,
8+
evalRequire,
89
generateByteStreamFromBuffer,
910
isBrowser,
1011
readByteStream,
@@ -55,9 +56,7 @@ async function createRequestInit(options: FetchOptions): Promise<RequestInit> {
5556
}> => {
5657
const contentHeaders: { [key: string]: string } = {};
5758
if (options.multipartData) {
58-
const FormData = isBrowser()
59-
? window.FormData
60-
: eval('require')('form-data');
59+
const FormData = isBrowser() ? window.FormData : evalRequire('form-data');
6160
const formData = new FormData();
6261
for (const item of options.multipartData) {
6362
if (item.fileStream) {
@@ -290,7 +289,7 @@ async function calculateMD5Hash(data: string | Buffer): Promise<string> {
290289
}
291290

292291
// Node environment
293-
createHash = eval('require')('crypto').createHash;
292+
createHash = evalRequire('crypto').createHash;
294293
return createHash('sha1').update(data).digest('hex');
295294
}
296295

0 commit comments

Comments
 (0)