Skip to content

Commit 6953b7a

Browse files
committed
fix(common): used eval import
1 parent 1f20f52 commit 6953b7a

File tree

4 files changed

+45
-36
lines changed

4 files changed

+45
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"express": "4.21.2",
6767
"fast-json-stringify": "6.0.0",
6868
"fast-safe-stringify": "2.1.1",
69-
"file-type-checker": "1.1.4",
69+
"file-type": "20.4.1",
7070
"iterare": "1.2.1",
7171
"object-hash": "3.0.0",
7272
"path-to-regexp": "3.3.0",

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
},
1919
"license": "MIT",
2020
"dependencies": {
21-
"file-type-checker": "1.1.4",
21+
"file-type": "20.4.1",
2222
"iterare": "1.2.1",
2323
"tslib": "2.8.1",
2424
"uid": "2.0.2"

packages/common/pipes/file/file-type.validator.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import * as fileTypeChecker from 'file-type-checker';
21
import { FileValidator } from './file-validator.interface';
32
import { FileTypeValidatorOptions, IFile } from './interfaces';
43

4+
const importEsmPackage = async (
5+
packageName: string,
6+
): Promise<typeof import('file-type')> =>
7+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
8+
new Function(`return import('${packageName}')`)().then(
9+
(loadedModule: any) => loadedModule['default'] ?? loadedModule,
10+
);
11+
512
/**
613
* Defines the built-in FileTypeValidator. It validates incoming files by examining
714
* their magic numbers using the file-type package, providing more reliable file type validation
@@ -22,7 +29,7 @@ export class FileTypeValidator extends FileValidator<
2229
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
2330
}
2431

25-
isValid(file?: IFile): boolean {
32+
async isValid(file?: IFile): Promise<boolean> {
2633
if (!this.validationOptions) {
2734
return true;
2835
}
@@ -40,10 +47,12 @@ export class FileTypeValidator extends FileValidator<
4047
}
4148

4249
try {
43-
const fileType = fileTypeChecker.detectFile(file.buffer);
50+
const { fileTypeFromBuffer } = await importEsmPackage('file-type');
51+
52+
const fileType = await fileTypeFromBuffer(file.buffer);
4453

4554
return (
46-
!!fileType && !!fileType.mimeType.match(this.validationOptions.fileType)
55+
!!fileType && !!fileType.mime.match(this.validationOptions.fileType)
4756
);
4857
} catch {
4958
return false;

packages/common/test/pipes/file/file-type.validator.spec.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FileTypeValidator } from '../../../pipes';
44

55
describe('FileTypeValidator', () => {
66
describe('isValid', () => {
7-
it('should return true when the file buffer matches the specified type', () => {
7+
it('should return true when the file buffer matches the specified type', async () => {
88
const fileTypeValidator = new FileTypeValidator({
99
fileType: 'image/jpeg',
1010
});
@@ -17,10 +17,10 @@ describe('FileTypeValidator', () => {
1717
buffer: jpegBuffer,
1818
} as IFile;
1919

20-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
20+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
2121
});
2222

23-
it('should return true when the file buffer matches the specified file extension', () => {
23+
it('should return true when the file buffer matches the specified file extension', async () => {
2424
const fileTypeValidator = new FileTypeValidator({
2525
fileType: 'jpeg',
2626
});
@@ -32,10 +32,10 @@ describe('FileTypeValidator', () => {
3232
mimetype: 'image/jpeg',
3333
buffer: jpegBuffer,
3434
} as IFile;
35-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
35+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
3636
});
3737

38-
it('should return true when the file buffer matches the specified regexp', () => {
38+
it('should return true when the file buffer matches the specified regexp', async () => {
3939
const fileTypeValidator = new FileTypeValidator({
4040
fileType: /^image\//,
4141
});
@@ -48,10 +48,10 @@ describe('FileTypeValidator', () => {
4848
buffer: jpegBuffer,
4949
} as IFile;
5050

51-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
51+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
5252
});
5353

54-
it('should return false when the file buffer does not match the specified type', () => {
54+
it('should return false when the file buffer does not match the specified type', async () => {
5555
const fileTypeValidator = new FileTypeValidator({
5656
fileType: 'image/jpeg',
5757
});
@@ -64,10 +64,10 @@ describe('FileTypeValidator', () => {
6464
buffer: pngBuffer,
6565
} as IFile;
6666

67-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
67+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
6868
});
6969

70-
it('should return false when the file buffer does not match the specified file extension', () => {
70+
it('should return false when the file buffer does not match the specified file extension', async () => {
7171
const fileTypeValidator = new FileTypeValidator({
7272
fileType: 'jpeg',
7373
});
@@ -80,10 +80,10 @@ describe('FileTypeValidator', () => {
8080
buffer: pngBuffer,
8181
} as IFile;
8282

83-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
83+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
8484
});
8585

86-
it('should return false when no buffer is provided', () => {
86+
it('should return false when no buffer is provided', async () => {
8787
const fileTypeValidator = new FileTypeValidator({
8888
fileType: 'image/jpeg',
8989
});
@@ -92,18 +92,18 @@ describe('FileTypeValidator', () => {
9292
mimetype: 'image/jpeg',
9393
} as IFile;
9494

95-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
95+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
9696
});
9797

98-
it('should return false when no file is provided', () => {
98+
it('should return false when no file is provided', async () => {
9999
const fileTypeValidator = new FileTypeValidator({
100100
fileType: 'image/jpeg',
101101
});
102102

103-
expect(fileTypeValidator.isValid()).to.equal(false);
103+
expect(await fileTypeValidator.isValid()).to.equal(false);
104104
});
105105

106-
it('should return false when no buffer is provided', () => {
106+
it('should return false when no buffer is provided', async () => {
107107
const fileTypeValidator = new FileTypeValidator({
108108
fileType: 'image/jpeg',
109109
});
@@ -112,10 +112,10 @@ describe('FileTypeValidator', () => {
112112
mimetype: 'image/jpeg',
113113
} as IFile;
114114

115-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
115+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
116116
});
117117

118-
it('should return true when the file buffer matches the specified regexp', () => {
118+
it('should return true when the file buffer matches the specified regexp', async () => {
119119
const fileTypeValidator = new FileTypeValidator({
120120
fileType: /^image\//,
121121
});
@@ -128,10 +128,10 @@ describe('FileTypeValidator', () => {
128128
buffer: jpegBuffer,
129129
} as IFile;
130130

131-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
131+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
132132
});
133133

134-
it('should return true when no validation options are provided', () => {
134+
it('should return true when no validation options are provided', async () => {
135135
const fileTypeValidator = new FileTypeValidator({} as any);
136136
const jpegBuffer = Buffer.from([
137137
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
@@ -141,10 +141,10 @@ describe('FileTypeValidator', () => {
141141
buffer: jpegBuffer,
142142
} as IFile;
143143

144-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
144+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
145145
});
146146

147-
it('should skip magic numbers validation when the skipMagicNumbersValidation is true', () => {
147+
it('should skip magic numbers validation when the skipMagicNumbersValidation is true', async () => {
148148
const fileTypeValidator = new FileTypeValidator({
149149
fileType: 'image/jpeg',
150150
skipMagicNumbersValidation: true,
@@ -154,10 +154,10 @@ describe('FileTypeValidator', () => {
154154
mimetype: 'image/jpeg',
155155
} as IFile;
156156

157-
expect(fileTypeValidator.isValid(requestFile)).to.equal(true);
157+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(true);
158158
});
159159

160-
it('should return false when the file buffer does not match any known type', () => {
160+
it('should return false when the file buffer does not match any known type', async () => {
161161
const fileTypeValidator = new FileTypeValidator({
162162
fileType: 'unknown/type',
163163
});
@@ -170,10 +170,10 @@ describe('FileTypeValidator', () => {
170170
buffer: unknownBuffer,
171171
} as IFile;
172172

173-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
173+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
174174
});
175175

176-
it('should return false when the buffer is empty', () => {
176+
it('should return false when the buffer is empty', async () => {
177177
const fileTypeValidator = new FileTypeValidator({
178178
fileType: 'image/jpeg',
179179
});
@@ -184,12 +184,12 @@ describe('FileTypeValidator', () => {
184184
buffer: emptyBuffer,
185185
} as IFile;
186186

187-
expect(fileTypeValidator.isValid(requestFile)).to.equal(false);
187+
expect(await fileTypeValidator.isValid(requestFile)).to.equal(false);
188188
});
189189
});
190190

191191
describe('buildErrorMessage', () => {
192-
it('should return a string with the format "Validation failed (expected type is #fileType)"', () => {
192+
it('should return a string with the format "Validation failed (expected type is #fileType)"', async () => {
193193
const fileType = 'image/jpeg';
194194
const fileTypeValidator = new FileTypeValidator({
195195
fileType,
@@ -200,7 +200,7 @@ describe('FileTypeValidator', () => {
200200
);
201201
});
202202

203-
it('should include the file type in the error message when a file is provided', () => {
203+
it('should include the file type in the error message when a file is provided', async () => {
204204
const currentFileType = 'image/png';
205205
const fileType = 'image/jpeg';
206206
const fileTypeValidator = new FileTypeValidator({
@@ -214,7 +214,7 @@ describe('FileTypeValidator', () => {
214214
);
215215
});
216216

217-
it('should handle regexp file type in error message', () => {
217+
it('should handle regexp file type in error message', async () => {
218218
const fileTypeValidator = new FileTypeValidator({
219219
fileType: /^image\//,
220220
});
@@ -225,7 +225,7 @@ describe('FileTypeValidator', () => {
225225
);
226226
});
227227

228-
it('should handle file extension in error message', () => {
228+
it('should handle file extension in error message', async () => {
229229
const fileTypeValidator = new FileTypeValidator({
230230
fileType: 'jpeg',
231231
});

0 commit comments

Comments
 (0)