file-type 17.1.6 → 18.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser.d.ts +4 -4
- package/core.d.ts +15 -9
- package/core.js +39 -14
- package/index.d.ts +1 -2
- package/package.json +17 -13
- package/readme.md +9 -9
- package/supported.js +6 -0
- package/util.js +5 -5
package/browser.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {FileTypeResult} from './core.js';
|
|
1
|
+
import type {FileTypeResult} from './core.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Detect the file type of a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
|
|
@@ -42,7 +42,7 @@ export {
|
|
|
42
42
|
fileTypeFromBuffer,
|
|
43
43
|
supportedExtensions,
|
|
44
44
|
supportedMimeTypes,
|
|
45
|
-
FileTypeResult,
|
|
46
|
-
FileExtension,
|
|
47
|
-
MimeType,
|
|
45
|
+
type FileTypeResult,
|
|
46
|
+
type FileExtension,
|
|
47
|
+
type MimeType,
|
|
48
48
|
} from './core.js';
|
package/core.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Readable as ReadableStream} from 'node:stream';
|
|
2
|
-
import {ITokenizer} from 'strtok3';
|
|
1
|
+
import type {Readable as ReadableStream} from 'node:stream';
|
|
2
|
+
import type {ITokenizer} from 'strtok3';
|
|
3
3
|
|
|
4
4
|
export type FileExtension =
|
|
5
5
|
| 'jpg'
|
|
@@ -140,7 +140,10 @@ export type FileExtension =
|
|
|
140
140
|
| '3mf'
|
|
141
141
|
| 'zst'
|
|
142
142
|
| 'jxl'
|
|
143
|
-
| 'vcf'
|
|
143
|
+
| 'vcf'
|
|
144
|
+
| 'jls'
|
|
145
|
+
| 'pst'
|
|
146
|
+
| 'dwg';
|
|
144
147
|
|
|
145
148
|
export type MimeType =
|
|
146
149
|
| 'image/jpeg'
|
|
@@ -276,9 +279,12 @@ export type MimeType =
|
|
|
276
279
|
| 'application/vnd.ms-htmlhelp'
|
|
277
280
|
| 'model/3mf'
|
|
278
281
|
| 'image/jxl'
|
|
279
|
-
| 'application/zstd'
|
|
282
|
+
| 'application/zstd'
|
|
283
|
+
| 'image/jls'
|
|
284
|
+
| 'application/vnd.ms-outlook'
|
|
285
|
+
| 'image/vnd.dwg';
|
|
280
286
|
|
|
281
|
-
export
|
|
287
|
+
export type FileTypeResult = {
|
|
282
288
|
/**
|
|
283
289
|
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
|
|
284
290
|
*/
|
|
@@ -288,7 +294,7 @@ export interface FileTypeResult {
|
|
|
288
294
|
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
|
|
289
295
|
*/
|
|
290
296
|
readonly mime: MimeType;
|
|
291
|
-
}
|
|
297
|
+
};
|
|
292
298
|
|
|
293
299
|
export type ReadableStreamWithFileType = ReadableStream & {
|
|
294
300
|
readonly fileType?: FileTypeResult;
|
|
@@ -354,14 +360,14 @@ Supported MIME types.
|
|
|
354
360
|
*/
|
|
355
361
|
export const supportedMimeTypes: ReadonlySet<MimeType>;
|
|
356
362
|
|
|
357
|
-
export
|
|
363
|
+
export type StreamOptions = {
|
|
358
364
|
/**
|
|
359
365
|
The default sample size in bytes.
|
|
360
366
|
|
|
361
367
|
@default 4100
|
|
362
368
|
*/
|
|
363
369
|
readonly sampleSize?: number;
|
|
364
|
-
}
|
|
370
|
+
};
|
|
365
371
|
|
|
366
372
|
/**
|
|
367
373
|
Returns a `Promise` which resolves to the original readable stream argument, but with an added `fileType` property, which is an object like the one returned from `fileTypeFromFile()`.
|
|
@@ -387,7 +393,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
|
387
393
|
const stream1 = got.stream(url);
|
|
388
394
|
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
|
|
389
395
|
|
|
390
|
-
if (stream2.fileType
|
|
396
|
+
if (stream2.fileType?.mime === 'image/jpeg') {
|
|
391
397
|
// stream2 can be used to stream the JPEG image (from the very beginning of the stream)
|
|
392
398
|
}
|
|
393
399
|
```
|
package/core.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Buffer} from 'node:buffer';
|
|
2
2
|
import * as Token from 'token-types';
|
|
3
|
-
import * as strtok3 from 'strtok3/core';
|
|
3
|
+
import * as strtok3 from 'strtok3/core'; // eslint-disable-line n/file-extension-in-import
|
|
4
4
|
import {
|
|
5
5
|
stringToBytes,
|
|
6
6
|
tarHeaderChecksumMatches,
|
|
@@ -26,7 +26,7 @@ export async function fileTypeFromBuffer(input) {
|
|
|
26
26
|
|
|
27
27
|
const buffer = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
28
28
|
|
|
29
|
-
if (!(buffer
|
|
29
|
+
if (!(buffer?.length > 1)) {
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -159,13 +159,6 @@ class FileTypeParser {
|
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
if (this.check([0xFF, 0xD8, 0xFF])) {
|
|
163
|
-
return {
|
|
164
|
-
ext: 'jpg',
|
|
165
|
-
mime: 'image/jpeg',
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
162
|
if (this.check([0x49, 0x49, 0xBC])) {
|
|
170
163
|
return {
|
|
171
164
|
ext: 'jxr',
|
|
@@ -222,6 +215,21 @@ class FileTypeParser {
|
|
|
222
215
|
|
|
223
216
|
// -- 4-byte signatures --
|
|
224
217
|
|
|
218
|
+
// Requires a sample size of 4 bytes
|
|
219
|
+
if (this.check([0xFF, 0xD8, 0xFF])) {
|
|
220
|
+
if (this.check([0xF7], {offset: 3})) { // JPG7/SOF55, indicating a ISO/IEC 14495 / JPEG-LS file
|
|
221
|
+
return {
|
|
222
|
+
ext: 'jls',
|
|
223
|
+
mime: 'image/jls',
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
ext: 'jpg',
|
|
229
|
+
mime: 'image/jpeg',
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
225
233
|
if (this.checkString('FLIF')) {
|
|
226
234
|
return {
|
|
227
235
|
ext: 'flif',
|
|
@@ -337,7 +345,8 @@ class FileTypeParser {
|
|
|
337
345
|
// - one entry indicating specific type of file.
|
|
338
346
|
// MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
|
|
339
347
|
if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
|
|
340
|
-
|
|
348
|
+
let mimeType = await tokenizer.readToken(new Token.StringType(zipHeader.compressedSize, 'utf-8'));
|
|
349
|
+
mimeType = mimeType.trim();
|
|
341
350
|
|
|
342
351
|
switch (mimeType) {
|
|
343
352
|
case 'application/epub+zip':
|
|
@@ -799,6 +808,13 @@ class FileTypeParser {
|
|
|
799
808
|
};
|
|
800
809
|
}
|
|
801
810
|
|
|
811
|
+
if (this.check([0x21, 0x42, 0x44, 0x4E])) {
|
|
812
|
+
return {
|
|
813
|
+
ext: 'pst',
|
|
814
|
+
mime: 'application/vnd.ms-outlook',
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
802
818
|
// -- 5-byte signatures --
|
|
803
819
|
|
|
804
820
|
if (this.check([0x4F, 0x54, 0x54, 0x4F, 0x00])) {
|
|
@@ -922,6 +938,16 @@ class FileTypeParser {
|
|
|
922
938
|
};
|
|
923
939
|
}
|
|
924
940
|
|
|
941
|
+
if (this.checkString('AC')) {
|
|
942
|
+
const version = this.buffer.toString('binary', 2, 6);
|
|
943
|
+
if (version.match('^d*') && version >= 1000 && version <= 1050) {
|
|
944
|
+
return {
|
|
945
|
+
ext: 'dwg',
|
|
946
|
+
mime: 'image/vnd.dwg',
|
|
947
|
+
};
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
|
|
925
951
|
// -- 7-byte signatures --
|
|
926
952
|
|
|
927
953
|
if (this.checkString('BLENDER')) {
|
|
@@ -1482,8 +1508,8 @@ class FileTypeParser {
|
|
|
1482
1508
|
}
|
|
1483
1509
|
|
|
1484
1510
|
await this.tokenizer.ignore(ifdOffset);
|
|
1485
|
-
const fileType = await this.readTiffIFD(
|
|
1486
|
-
return fileType
|
|
1511
|
+
const fileType = await this.readTiffIFD(bigEndian);
|
|
1512
|
+
return fileType ?? {
|
|
1487
1513
|
ext: 'tif',
|
|
1488
1514
|
mime: 'image/tiff',
|
|
1489
1515
|
};
|
|
@@ -1499,7 +1525,6 @@ class FileTypeParser {
|
|
|
1499
1525
|
}
|
|
1500
1526
|
|
|
1501
1527
|
export async function fileTypeStream(readableStream, {sampleSize = minimumBytes} = {}) {
|
|
1502
|
-
// eslint-disable-next-line node/no-unsupported-features/es-syntax
|
|
1503
1528
|
const {default: stream} = await import('node:stream');
|
|
1504
1529
|
|
|
1505
1530
|
return new Promise((resolve, reject) => {
|
|
@@ -1513,7 +1538,7 @@ export async function fileTypeStream(readableStream, {sampleSize = minimumBytes}
|
|
|
1513
1538
|
const outputStream = stream.pipeline ? stream.pipeline(readableStream, pass, () => {}) : readableStream.pipe(pass);
|
|
1514
1539
|
|
|
1515
1540
|
// Read the input stream and detect the filetype
|
|
1516
|
-
const chunk = readableStream.read(sampleSize)
|
|
1541
|
+
const chunk = readableStream.read(sampleSize) ?? readableStream.read() ?? Buffer.alloc(0);
|
|
1517
1542
|
try {
|
|
1518
1543
|
const fileType = await fileTypeFromBuffer(chunk);
|
|
1519
1544
|
pass.fileType = fileType;
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-type",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "18.1.0",
|
|
4
4
|
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/file-type",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"./core": "./core.js"
|
|
20
20
|
},
|
|
21
21
|
"engines": {
|
|
22
|
-
"node": "
|
|
22
|
+
"node": ">=14.16"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
25
|
"test": "xo && ava && tsd"
|
|
@@ -193,22 +193,24 @@
|
|
|
193
193
|
"3mf",
|
|
194
194
|
"zst",
|
|
195
195
|
"jxl",
|
|
196
|
-
"vcf"
|
|
196
|
+
"vcf",
|
|
197
|
+
"jls",
|
|
198
|
+
"pst",
|
|
199
|
+
"dwg"
|
|
197
200
|
],
|
|
198
201
|
"dependencies": {
|
|
199
202
|
"readable-web-to-node-stream": "^3.0.2",
|
|
200
|
-
"strtok3": "^7.0.0
|
|
201
|
-
"token-types": "^5.0.
|
|
203
|
+
"strtok3": "^7.0.0",
|
|
204
|
+
"token-types": "^5.0.1"
|
|
202
205
|
},
|
|
203
206
|
"devDependencies": {
|
|
204
207
|
"@tokenizer/token": "^0.3.0",
|
|
205
|
-
"@types/node": "^
|
|
206
|
-
"ava": "^
|
|
208
|
+
"@types/node": "^18.7.13",
|
|
209
|
+
"ava": "^5.1.0",
|
|
207
210
|
"commonmark": "^0.30.0",
|
|
208
211
|
"noop-stream": "^1.0.0",
|
|
209
|
-
"tsd": "^0.
|
|
210
|
-
"
|
|
211
|
-
"xo": "^0.46.4"
|
|
212
|
+
"tsd": "^0.25.0",
|
|
213
|
+
"xo": "^0.53.1"
|
|
212
214
|
},
|
|
213
215
|
"xo": {
|
|
214
216
|
"envs": [
|
|
@@ -219,11 +221,13 @@
|
|
|
219
221
|
"no-inner-declarations": "warn",
|
|
220
222
|
"no-await-in-loop": "warn",
|
|
221
223
|
"no-bitwise": "off",
|
|
222
|
-
"@typescript-eslint/no-unsafe-assignment": "off"
|
|
224
|
+
"@typescript-eslint/no-unsafe-assignment": "off",
|
|
225
|
+
"unicorn/text-encoding-identifier-case": "off",
|
|
226
|
+
"unicorn/switch-case-braces": "off",
|
|
227
|
+
"unicorn/prefer-top-level-await": "off"
|
|
223
228
|
}
|
|
224
229
|
},
|
|
225
230
|
"ava": {
|
|
226
|
-
"serial": true
|
|
227
|
-
"verbose": true
|
|
231
|
+
"serial": true
|
|
228
232
|
}
|
|
229
233
|
}
|
package/readme.md
CHANGED
|
@@ -318,7 +318,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
|
318
318
|
const stream1 = got.stream(url);
|
|
319
319
|
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
|
|
320
320
|
|
|
321
|
-
if (stream2.fileType
|
|
321
|
+
if (stream2.fileType?.mime === 'image/jpeg') {
|
|
322
322
|
// stream2 can be used to stream the JPEG image (from the very beginning of the stream)
|
|
323
323
|
}
|
|
324
324
|
```
|
|
@@ -376,6 +376,7 @@ Returns a `Set<string>` of supported MIME types.
|
|
|
376
376
|
- [`dng`](https://en.wikipedia.org/wiki/Digital_Negative) - Adobe Digital Negative image file
|
|
377
377
|
- [`docx`](https://en.wikipedia.org/wiki/Office_Open_XML) - Microsoft Word
|
|
378
378
|
- [`dsf`](https://dsd-guide.com/sites/default/files/white-papers/DSFFileFormatSpec_E.pdf) - Sony DSD Stream File (DSF)
|
|
379
|
+
- [`dwg`](https://en.wikipedia.org/wiki/.dwg) - Autodesk CAD file
|
|
379
380
|
- [`elf`](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) - Unix Executable and Linkable Format
|
|
380
381
|
- [`eot`](https://en.wikipedia.org/wiki/Embedded_OpenType) - Embedded OpenType font
|
|
381
382
|
- [`eps`](https://en.wikipedia.org/wiki/Encapsulated_PostScript) - Encapsulated PostScript
|
|
@@ -397,6 +398,7 @@ Returns a `Set<string>` of supported MIME types.
|
|
|
397
398
|
- [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar
|
|
398
399
|
- [`indd`](https://en.wikipedia.org/wiki/Adobe_InDesign#File_format) - Adobe InDesign document
|
|
399
400
|
- [`it`](https://wiki.openmpt.org/Manual:_Module_formats#The_Impulse_Tracker_format_.28.it.29) - Audio module format: Impulse Tracker
|
|
401
|
+
- [`jls`](https://en.wikipedia.org/wiki/Lossless_JPEG#JPEG-LS) - Lossless/near-lossless compression standard for continuous-tone images
|
|
400
402
|
- [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
|
|
401
403
|
- [`jpg`](https://en.wikipedia.org/wiki/JPEG) - Joint Photographic Experts Group image
|
|
402
404
|
- [`jpm`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
|
|
@@ -410,7 +412,7 @@ Returns a `Set<string>` of supported MIME types.
|
|
|
410
412
|
- [`m4a`](https://en.wikipedia.org/wiki/M4A) - Audio-only MPEG-4 files
|
|
411
413
|
- [`m4b`](https://en.wikipedia.org/wiki/M4B) - Audiobook and podcast MPEG-4 files, which also contain metadata including chapter markers, images, and hyperlinks
|
|
412
414
|
- [`m4p`](https://en.wikipedia.org/wiki/MPEG-4_Part_14#Filename_extensions) - MPEG-4 files with audio streams encrypted by FairPlay Digital Rights Management as were sold through the iTunes Store
|
|
413
|
-
- [`m4v`](https://en.wikipedia.org/wiki/M4V) -
|
|
415
|
+
- [`m4v`](https://en.wikipedia.org/wiki/M4V) - Video container format developed by Apple, which is very similar to the MP4 format
|
|
414
416
|
- [`mid`](https://en.wikipedia.org/wiki/MIDI) - Musical Instrument Digital Interface file
|
|
415
417
|
- [`mie`](https://en.wikipedia.org/wiki/Sidecar_file) - Dedicated meta information format which supports storage of binary as well as textual meta information
|
|
416
418
|
- [`mj2`](https://en.wikipedia.org/wiki/Motion_JPEG_2000) - Motion JPEG 2000
|
|
@@ -445,6 +447,7 @@ Returns a `Set<string>` of supported MIME types.
|
|
|
445
447
|
- [`pptx`](https://en.wikipedia.org/wiki/Office_Open_XML) - Microsoft Powerpoint
|
|
446
448
|
- [`ps`](https://en.wikipedia.org/wiki/Postscript) - Postscript
|
|
447
449
|
- [`psd`](https://en.wikipedia.org/wiki/Adobe_Photoshop#File_format) - Adobe Photoshop document
|
|
450
|
+
- [`pst`](https://en.wikipedia.org/wiki/Personal_Storage_Table) - Personal Storage Table file
|
|
448
451
|
- [`qcp`](https://en.wikipedia.org/wiki/QCP) - Tagged and chunked data
|
|
449
452
|
- [`raf`](https://en.wikipedia.org/wiki/Raw_image_format) - Fujifilm RAW image file
|
|
450
453
|
- [`rar`](https://en.wikipedia.org/wiki/RAR_(file_format)) - Archive file
|
|
@@ -490,12 +493,6 @@ The following file types will not be accepted:
|
|
|
490
493
|
- `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196)
|
|
491
494
|
- `.svg` - Detecting it requires a full-blown parser. Check out [`is-svg`](https://github.com/sindresorhus/is-svg) for something that mostly works.
|
|
492
495
|
|
|
493
|
-
## file-type for enterprise
|
|
494
|
-
|
|
495
|
-
Available as part of the Tidelift Subscription.
|
|
496
|
-
|
|
497
|
-
The maintainers of file-type and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-file-type?utm_source=npm-file-type&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
|
|
498
|
-
|
|
499
496
|
## Related
|
|
500
497
|
|
|
501
498
|
- [file-type-cli](https://github.com/sindresorhus/file-type-cli) - CLI for this module
|
|
@@ -503,6 +500,9 @@ The maintainers of file-type and thousands of other packages are working with Ti
|
|
|
503
500
|
## Maintainers
|
|
504
501
|
|
|
505
502
|
- [Sindre Sorhus](https://github.com/sindresorhus)
|
|
503
|
+
- [Borewit](https://github.com/Borewit)
|
|
504
|
+
|
|
505
|
+
**Former**
|
|
506
|
+
|
|
506
507
|
- [Mikael Finstad](https://github.com/mifi)
|
|
507
508
|
- [Ben Brook](https://github.com/bencmbrook)
|
|
508
|
-
- [Borewit](https://github.com/Borewit)
|
package/supported.js
CHANGED
|
@@ -138,6 +138,9 @@ export const extensions = [
|
|
|
138
138
|
'zst',
|
|
139
139
|
'jxl',
|
|
140
140
|
'vcf',
|
|
141
|
+
'jls',
|
|
142
|
+
'pst',
|
|
143
|
+
'dwg',
|
|
141
144
|
];
|
|
142
145
|
|
|
143
146
|
export const mimeTypes = [
|
|
@@ -275,4 +278,7 @@ export const mimeTypes = [
|
|
|
275
278
|
'model/3mf',
|
|
276
279
|
'image/jxl',
|
|
277
280
|
'application/zstd',
|
|
281
|
+
'image/jls',
|
|
282
|
+
'application/vnd.ms-outlook',
|
|
283
|
+
'image/vnd.dwg',
|
|
278
284
|
];
|
package/util.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function stringToBytes(string) {
|
|
2
|
-
return [...string].map(character => character.charCodeAt(0));
|
|
2
|
+
return [...string].map(character => character.charCodeAt(0)); // eslint-disable-line unicorn/prefer-code-point
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -17,12 +17,12 @@ export function tarHeaderChecksumMatches(buffer, offset = 0) {
|
|
|
17
17
|
|
|
18
18
|
let sum = 8 * 0x20; // Initialize signed bit sum
|
|
19
19
|
|
|
20
|
-
for (let
|
|
21
|
-
sum += buffer[
|
|
20
|
+
for (let index = offset; index < offset + 148; index++) {
|
|
21
|
+
sum += buffer[index];
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
for (let
|
|
25
|
-
sum += buffer[
|
|
24
|
+
for (let index = offset + 156; index < offset + 512; index++) {
|
|
25
|
+
sum += buffer[index];
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
return readSum === sum;
|