file-type 18.2.1 → 18.4.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 CHANGED
@@ -18,28 +18,9 @@ console.log(fileType);
18
18
  */
19
19
  export declare function fileTypeFromStream(stream: ReadableStream): Promise<FileTypeResult | undefined>;
20
20
 
21
- /**
22
- Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
23
-
24
- __Note:__ This method is only available in the browser.
25
-
26
- @example
27
- ```
28
- import {fileTypeFromBlob} from 'file-type';
29
-
30
- const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
31
- type: 'plain/text',
32
- endings: 'native'
33
- });
34
-
35
- console.log(await fileTypeFromBlob(blob));
36
- //=> {ext: 'txt', mime: 'plain/text'}
37
- ```
38
- */
39
- export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
40
-
41
21
  export {
42
22
  fileTypeFromBuffer,
23
+ fileTypeFromBlob,
43
24
  supportedExtensions,
44
25
  supportedMimeTypes,
45
26
  type FileTypeResult,
package/browser.js CHANGED
@@ -1,36 +1,5 @@
1
- import {Buffer} from 'node:buffer';
2
1
  import {ReadableWebToNodeStream} from 'readable-web-to-node-stream';
3
- import {fileTypeFromBuffer, fileTypeFromStream as coreFileTypeFromStream} from './core.js';
4
-
5
- /**
6
- Convert Blobs to ArrayBuffer.
7
-
8
- @param {Blob} blob - Web API Blob.
9
- @returns {Promise<ArrayBuffer>}
10
- */
11
- function blobToArrayBuffer(blob) {
12
- if (blob.arrayBuffer) {
13
- return blob.arrayBuffer();
14
- }
15
-
16
- // TODO: Remove when stop supporting older environments
17
- return new Promise((resolve, reject) => {
18
- const fileReader = new FileReader();
19
- fileReader.addEventListener('loadend', event => {
20
- resolve(event.target.result);
21
- });
22
-
23
- fileReader.addEventListener('error', event => {
24
- reject(new Error(event.message));
25
- });
26
-
27
- fileReader.addEventListener('abort', event => {
28
- reject(new Error(event.type));
29
- });
30
-
31
- fileReader.readAsArrayBuffer(blob);
32
- });
33
- }
2
+ import {fileTypeFromStream as coreFileTypeFromStream} from './core.js';
34
3
 
35
4
  export async function fileTypeFromStream(stream) {
36
5
  const readableWebToNodeStream = new ReadableWebToNodeStream(stream);
@@ -39,11 +8,6 @@ export async function fileTypeFromStream(stream) {
39
8
  return fileType;
40
9
  }
41
10
 
42
- export async function fileTypeFromBlob(blob) {
43
- const buffer = await blobToArrayBuffer(blob);
44
- return fileTypeFromBuffer(Buffer.from(buffer));
45
- }
46
-
47
11
  export {
48
12
  fileTypeFromTokenizer,
49
13
  fileTypeFromBuffer,
package/core.d.ts CHANGED
@@ -83,6 +83,7 @@ export type FileExtension =
83
83
  | 'xlsx'
84
84
  | '3gp'
85
85
  | '3g2'
86
+ | 'j2c'
86
87
  | 'jp2'
87
88
  | 'jpm'
88
89
  | 'jpx'
@@ -144,7 +145,13 @@ export type FileExtension =
144
145
  | 'jls'
145
146
  | 'pst'
146
147
  | 'dwg'
147
- | 'parquet';
148
+ | 'parquet'
149
+ | 'class'
150
+ | 'arj'
151
+ | 'cpio'
152
+ | 'ace'
153
+ | 'avro'
154
+ ; // eslint-disable-line semi-style
148
155
 
149
156
  export type MimeType =
150
157
  | 'image/jpeg'
@@ -231,6 +238,7 @@ export type MimeType =
231
238
  | 'video/mp2t'
232
239
  | 'application/x-blender'
233
240
  | 'image/bpg'
241
+ | 'image/j2c'
234
242
  | 'image/jp2'
235
243
  | 'image/jpx'
236
244
  | 'image/jpm'
@@ -284,7 +292,13 @@ export type MimeType =
284
292
  | 'image/jls'
285
293
  | 'application/vnd.ms-outlook'
286
294
  | 'image/vnd.dwg'
287
- | 'application/x-parquet';
295
+ | 'application/x-parquet'
296
+ | 'application/java-vm'
297
+ | 'application/x-arj'
298
+ | 'application/x-cpio'
299
+ | 'application/x-ace-compressed'
300
+ | 'application/avro'
301
+ ; // eslint-disable-line semi-style
288
302
 
289
303
  export type FileTypeResult = {
290
304
  /**
@@ -401,3 +415,21 @@ if (stream2.fileType?.mime === 'image/jpeg') {
401
415
  ```
402
416
  */
403
417
  export function fileTypeStream(readableStream: ReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>;
418
+
419
+ /**
420
+ Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob).
421
+
422
+ @example
423
+ ```
424
+ import {fileTypeFromBlob} from 'file-type';
425
+
426
+ const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
427
+ type: 'plain/text',
428
+ endings: 'native'
429
+ });
430
+
431
+ console.log(await fileTypeFromBlob(blob));
432
+ //=> {ext: 'txt', mime: 'plain/text'}
433
+ ```
434
+ */
435
+ export declare function fileTypeFromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
package/core.js CHANGED
@@ -33,6 +33,11 @@ export async function fileTypeFromBuffer(input) {
33
33
  return fileTypeFromTokenizer(strtok3.fromBuffer(buffer));
34
34
  }
35
35
 
36
+ export async function fileTypeFromBlob(blob) {
37
+ const buffer = await blob.arrayBuffer();
38
+ return fileTypeFromBuffer(new Uint8Array(buffer));
39
+ }
40
+
36
41
  function _check(buffer, headers, options) {
37
42
  options = {
38
43
  offset: 0,
@@ -144,6 +149,20 @@ class FileTypeParser {
144
149
  };
145
150
  }
146
151
 
152
+ if (this.check([0xC7, 0x71])) {
153
+ return {
154
+ ext: 'cpio',
155
+ mime: 'application/x-cpio',
156
+ };
157
+ }
158
+
159
+ if (this.check([0x60, 0xEA])) {
160
+ return {
161
+ ext: 'arj',
162
+ mime: 'application/x-arj',
163
+ };
164
+ }
165
+
147
166
  // -- 3-byte signatures --
148
167
 
149
168
  if (this.check([0xEF, 0xBB, 0xBF])) { // UTF-8-BOM
@@ -230,6 +249,13 @@ class FileTypeParser {
230
249
  };
231
250
  }
232
251
 
252
+ if (this.check([0x4F, 0x62, 0x6A, 0x01])) {
253
+ return {
254
+ ext: 'avro',
255
+ mime: 'application/avro',
256
+ };
257
+ }
258
+
233
259
  if (this.checkString('FLIF')) {
234
260
  return {
235
261
  ext: 'flif',
@@ -912,6 +938,13 @@ class FileTypeParser {
912
938
  };
913
939
  }
914
940
 
941
+ if (this.check([0xCA, 0xFE, 0xBA, 0xBE])) {
942
+ return {
943
+ ext: 'class',
944
+ mime: 'application/java-vm',
945
+ };
946
+ }
947
+
915
948
  // -- 6-byte signatures --
916
949
 
917
950
  if (this.check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
@@ -962,6 +995,13 @@ class FileTypeParser {
962
995
  }
963
996
  }
964
997
 
998
+ if (this.checkString('070707')) {
999
+ return {
1000
+ ext: 'cpio',
1001
+ mime: 'application/x-cpio',
1002
+ };
1003
+ }
1004
+
965
1005
  // -- 7-byte signatures --
966
1006
 
967
1007
  if (this.checkString('BLENDER')) {
@@ -987,6 +1027,16 @@ class FileTypeParser {
987
1027
  };
988
1028
  }
989
1029
 
1030
+ if (this.checkString('**ACE', {offset: 7})) {
1031
+ await tokenizer.peekBuffer(this.buffer, {length: 14, mayBeLess: true});
1032
+ if (this.checkString('**', {offset: 12})) {
1033
+ return {
1034
+ ext: 'ace',
1035
+ mime: 'application/x-ace-compressed',
1036
+ };
1037
+ }
1038
+ }
1039
+
990
1040
  // -- 8-byte signatures --
991
1041
 
992
1042
  if (this.check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
@@ -1158,6 +1208,13 @@ class FileTypeParser {
1158
1208
  };
1159
1209
  }
1160
1210
 
1211
+ if (this.check([0xFF, 0x4F, 0xFF, 0x51])) {
1212
+ return {
1213
+ ext: 'j2c',
1214
+ mime: 'image/j2c',
1215
+ };
1216
+ }
1217
+
1161
1218
  if (this.check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
1162
1219
  // JPEG-2000 family
1163
1220
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "18.2.1",
3
+ "version": "18.4.0",
4
4
  "description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/file-type",
@@ -132,6 +132,7 @@
132
132
  "pptx",
133
133
  "xlsx",
134
134
  "3gp",
135
+ "j2c",
135
136
  "jp2",
136
137
  "jpm",
137
138
  "jpx",
@@ -197,7 +198,12 @@
197
198
  "jls",
198
199
  "pst",
199
200
  "dwg",
200
- "parquet"
201
+ "parquet",
202
+ "class",
203
+ "arj",
204
+ "cpio",
205
+ "ace",
206
+ "avro"
201
207
  ],
202
208
  "dependencies": {
203
209
  "readable-web-to-node-stream": "^3.0.2",
@@ -206,12 +212,12 @@
206
212
  },
207
213
  "devDependencies": {
208
214
  "@tokenizer/token": "^0.3.0",
209
- "@types/node": "^18.7.13",
210
- "ava": "^5.1.0",
215
+ "@types/node": "^20.1.2",
216
+ "ava": "^5.2.0",
211
217
  "commonmark": "^0.30.0",
212
218
  "noop-stream": "^1.0.0",
213
- "tsd": "^0.25.0",
214
- "xo": "^0.53.1"
219
+ "tsd": "^0.28.1",
220
+ "xo": "^0.54.2"
215
221
  },
216
222
  "xo": {
217
223
  "envs": [
package/readme.md CHANGED
@@ -8,36 +8,6 @@ This package is for detecting binary-based file formats, not text-based formats
8
8
 
9
9
  We accept contributions for commonly used modern file formats, not historical or obscure ones. Open an issue first for discussion.
10
10
 
11
- <br>
12
-
13
- ---
14
-
15
- <div align="center">
16
- <p>
17
- <p>
18
- <sup>
19
- <a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
20
- </sup>
21
- </p>
22
- <sup>Special thanks to:</sup>
23
- <br>
24
- <br>
25
- <a href="https://bit.io/?utm_campaign=github_repo&utm_medium=referral&utm_content=file-type&utm_source=github">
26
- <div>
27
- <img src="https://sindresorhus.com/assets/thanks/bitio-logo.svg" width="190" alt="bit.io">
28
- </div>
29
- <b>Instant, shareable cloud PostgreSQL database</b>
30
- <div>
31
- <sup>Import any dataset in seconds, share with anyone with a click, try without signing up</sup>
32
- </div>
33
- </a>
34
- </p>
35
- </div>
36
-
37
- ---
38
-
39
- <br>
40
-
41
11
  ## Install
42
12
 
43
13
  ```sh
@@ -198,8 +168,6 @@ A readable stream representing file data.
198
168
 
199
169
  Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
200
170
 
201
- **Note:** This method is only available in the browser.
202
-
203
171
  The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer.
204
172
 
205
173
  Returns a `Promise` for an object with the detected file type and MIME type:
@@ -346,6 +314,7 @@ Returns a `Set<string>` of supported MIME types.
346
314
  - [`Z`](https://fileinfo.com/extension/z) - Unix Compressed File
347
315
  - [`aac`](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) - Advanced Audio Coding
348
316
  - [`ac3`](https://www.atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/) - ATSC A/52 Audio File
317
+ - [`ace`](https://en.wikipedia.org/wiki/ACE_(compressed_file_format)) - ACE archive
349
318
  - [`ai`](https://en.wikipedia.org/wiki/Adobe_Illustrator_Artwork) - Adobe Illustrator Artwork
350
319
  - [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format) - Audio Interchange file
351
320
  - [`alias`](https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) - macOS Alias file
@@ -353,12 +322,14 @@ Returns a `Set<string>` of supported MIME types.
353
322
  - [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio
354
323
  - [`apng`](https://en.wikipedia.org/wiki/APNG) - Animated Portable Network Graphics
355
324
  - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - Archive file
325
+ - [`arj`](https://en.wikipedia.org/wiki/ARJ) - Archive file
356
326
  - [`arrow`](https://arrow.apache.org) - Columnar format for tables of data
357
327
  - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw image file
358
328
  - [`asar`](https://github.com/electron/asar#format) - Archive format primarily used to enclose Electron applications
359
329
  - [`asf`](https://en.wikipedia.org/wiki/Advanced_Systems_Format) - Advanced Systems Format
360
330
  - [`avi`](https://en.wikipedia.org/wiki/Audio_Video_Interleave) - Audio Video Interleave file
361
331
  - [`avif`](https://en.wikipedia.org/wiki/AV1#AV1_Image_File_Format_(AVIF)) - AV1 Image File Format
332
+ - [`avro`](https://en.wikipedia.org/wiki/Apache_Avro#Avro_Object_Container_File) - Object container file developed by Apache Avro
362
333
  - [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format) - Blender project
363
334
  - [`bmp`](https://en.wikipedia.org/wiki/BMP_file_format) - Bitmap image file
364
335
  - [`bpg`](https://bellard.org/bpg/) - Better Portable Graphics file
@@ -366,6 +337,8 @@ Returns a `Set<string>` of supported MIME types.
366
337
  - [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format)) - Cabinet file
367
338
  - [`cfb`](https://en.wikipedia.org/wiki/Compound_File_Binary_Format) - Compount File Binary Format
368
339
  - [`chm`](https://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help) - Microsoft Compiled HTML Help
340
+ - [`class`](https://en.wikipedia.org/wiki/Java_class_file) - Java class file
341
+ - [`cpio`](https://en.wikipedia.org/wiki/Cpio) - Cpio archive
369
342
  - [`cr2`](https://fileinfo.com/extension/cr2) - Canon Raw image file (v2)
370
343
  - [`cr3`](https://fileinfo.com/extension/cr3) - Canon Raw image file (v3)
371
344
  - [`crx`](https://developer.chrome.com/extensions/crx) - Google Chrome extension
@@ -398,6 +371,7 @@ Returns a `Set<string>` of supported MIME types.
398
371
  - [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar
399
372
  - [`indd`](https://en.wikipedia.org/wiki/Adobe_InDesign#File_format) - Adobe InDesign document
400
373
  - [`it`](https://wiki.openmpt.org/Manual:_Module_formats#The_Impulse_Tracker_format_.28.it.29) - Audio module format: Impulse Tracker
374
+ - [`j2c`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
401
375
  - [`jls`](https://en.wikipedia.org/wiki/Lossless_JPEG#JPEG-LS) - Lossless/near-lossless compression standard for continuous-tone images
402
376
  - [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
403
377
  - [`jpg`](https://en.wikipedia.org/wiki/JPEG) - Joint Photographic Experts Group image
package/supported.js CHANGED
@@ -81,6 +81,7 @@ export const extensions = [
81
81
  'xlsx',
82
82
  '3gp',
83
83
  '3g2',
84
+ 'j2c',
84
85
  'jp2',
85
86
  'jpm',
86
87
  'jpx',
@@ -142,6 +143,11 @@ export const extensions = [
142
143
  'pst',
143
144
  'dwg',
144
145
  'parquet',
146
+ 'class',
147
+ 'arj',
148
+ 'cpio',
149
+ 'ace',
150
+ 'avro',
145
151
  ];
146
152
 
147
153
  export const mimeTypes = [
@@ -228,6 +234,7 @@ export const mimeTypes = [
228
234
  'video/mp2t',
229
235
  'application/x-blender',
230
236
  'image/bpg',
237
+ 'image/j2c',
231
238
  'image/jp2',
232
239
  'image/jpx',
233
240
  'image/jpm',
@@ -283,4 +290,9 @@ export const mimeTypes = [
283
290
  'application/vnd.ms-outlook',
284
291
  'image/vnd.dwg',
285
292
  'application/x-parquet',
293
+ 'application/java-vm',
294
+ 'application/x-arj',
295
+ 'application/x-cpio',
296
+ 'application/x-ace-compressed',
297
+ 'application/avro',
286
298
  ];