file-type 18.2.1 → 18.3.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,12 @@ 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
+ ; // eslint-disable-line semi-style
148
154
 
149
155
  export type MimeType =
150
156
  | 'image/jpeg'
@@ -231,6 +237,7 @@ export type MimeType =
231
237
  | 'video/mp2t'
232
238
  | 'application/x-blender'
233
239
  | 'image/bpg'
240
+ | 'image/j2c'
234
241
  | 'image/jp2'
235
242
  | 'image/jpx'
236
243
  | 'image/jpm'
@@ -284,7 +291,12 @@ export type MimeType =
284
291
  | 'image/jls'
285
292
  | 'application/vnd.ms-outlook'
286
293
  | 'image/vnd.dwg'
287
- | 'application/x-parquet';
294
+ | 'application/x-parquet'
295
+ | 'application/java-vm'
296
+ | 'application/x-arj'
297
+ | 'application/x-cpio'
298
+ | 'application/x-ace-compressed'
299
+ ; // eslint-disable-line semi-style
288
300
 
289
301
  export type FileTypeResult = {
290
302
  /**
@@ -401,3 +413,21 @@ if (stream2.fileType?.mime === 'image/jpeg') {
401
413
  ```
402
414
  */
403
415
  export function fileTypeStream(readableStream: ReadableStream, options?: StreamOptions): Promise<ReadableStreamWithFileType>;
416
+
417
+ /**
418
+ Detect the file type of a [`Blob`](https://nodejs.org/api/buffer.html#class-blob).
419
+
420
+ @example
421
+ ```
422
+ import {fileTypeFromBlob} from 'file-type';
423
+
424
+ const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
425
+ type: 'plain/text',
426
+ endings: 'native'
427
+ });
428
+
429
+ console.log(await fileTypeFromBlob(blob));
430
+ //=> {ext: 'txt', mime: 'plain/text'}
431
+ ```
432
+ */
433
+ 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
@@ -912,6 +931,13 @@ class FileTypeParser {
912
931
  };
913
932
  }
914
933
 
934
+ if (this.check([0xCA, 0xFE, 0xBA, 0xBE])) {
935
+ return {
936
+ ext: 'class',
937
+ mime: 'application/java-vm',
938
+ };
939
+ }
940
+
915
941
  // -- 6-byte signatures --
916
942
 
917
943
  if (this.check([0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])) {
@@ -962,6 +988,13 @@ class FileTypeParser {
962
988
  }
963
989
  }
964
990
 
991
+ if (this.checkString('070707')) {
992
+ return {
993
+ ext: 'cpio',
994
+ mime: 'application/x-cpio',
995
+ };
996
+ }
997
+
965
998
  // -- 7-byte signatures --
966
999
 
967
1000
  if (this.checkString('BLENDER')) {
@@ -987,6 +1020,16 @@ class FileTypeParser {
987
1020
  };
988
1021
  }
989
1022
 
1023
+ if (this.checkString('**ACE', {offset: 7})) {
1024
+ await tokenizer.peekBuffer(this.buffer, {length: 14, mayBeLess: true});
1025
+ if (this.checkString('**', {offset: 12})) {
1026
+ return {
1027
+ ext: 'ace',
1028
+ mime: 'application/x-ace-compressed',
1029
+ };
1030
+ }
1031
+ }
1032
+
990
1033
  // -- 8-byte signatures --
991
1034
 
992
1035
  if (this.check([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])) {
@@ -1158,6 +1201,13 @@ class FileTypeParser {
1158
1201
  };
1159
1202
  }
1160
1203
 
1204
+ if (this.check([0xFF, 0x4F, 0xFF, 0x51])) {
1205
+ return {
1206
+ ext: 'j2c',
1207
+ mime: 'image/j2c',
1208
+ };
1209
+ }
1210
+
1161
1211
  if (this.check([0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A])) {
1162
1212
  // JPEG-2000 family
1163
1213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "18.2.1",
3
+ "version": "18.3.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,11 @@
197
198
  "jls",
198
199
  "pst",
199
200
  "dwg",
200
- "parquet"
201
+ "parquet",
202
+ "class",
203
+ "arj",
204
+ "cpio",
205
+ "ace"
201
206
  ],
202
207
  "dependencies": {
203
208
  "readable-web-to-node-stream": "^3.0.2",
@@ -206,12 +211,12 @@
206
211
  },
207
212
  "devDependencies": {
208
213
  "@tokenizer/token": "^0.3.0",
209
- "@types/node": "^18.7.13",
210
- "ava": "^5.1.0",
214
+ "@types/node": "^18.16.0",
215
+ "ava": "^5.2.0",
211
216
  "commonmark": "^0.30.0",
212
217
  "noop-stream": "^1.0.0",
213
- "tsd": "^0.25.0",
214
- "xo": "^0.53.1"
218
+ "tsd": "^0.28.1",
219
+ "xo": "^0.54.1"
215
220
  },
216
221
  "xo": {
217
222
  "envs": [
package/readme.md CHANGED
@@ -198,8 +198,6 @@ A readable stream representing file data.
198
198
 
199
199
  Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
200
200
 
201
- **Note:** This method is only available in the browser.
202
-
203
201
  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
202
 
205
203
  Returns a `Promise` for an object with the detected file type and MIME type:
@@ -346,6 +344,7 @@ Returns a `Set<string>` of supported MIME types.
346
344
  - [`Z`](https://fileinfo.com/extension/z) - Unix Compressed File
347
345
  - [`aac`](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) - Advanced Audio Coding
348
346
  - [`ac3`](https://www.atsc.org/standard/a522012-digital-audio-compression-ac-3-e-ac-3-standard-12172012/) - ATSC A/52 Audio File
347
+ - [`ace`](https://en.wikipedia.org/wiki/ACE_(compressed_file_format)) - ACE archive
349
348
  - [`ai`](https://en.wikipedia.org/wiki/Adobe_Illustrator_Artwork) - Adobe Illustrator Artwork
350
349
  - [`aif`](https://en.wikipedia.org/wiki/Audio_Interchange_File_Format) - Audio Interchange file
351
350
  - [`alias`](https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) - macOS Alias file
@@ -353,6 +352,7 @@ Returns a `Set<string>` of supported MIME types.
353
352
  - [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio
354
353
  - [`apng`](https://en.wikipedia.org/wiki/APNG) - Animated Portable Network Graphics
355
354
  - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - Archive file
355
+ - [`arj`](https://en.wikipedia.org/wiki/ARJ) - Archive file
356
356
  - [`arrow`](https://arrow.apache.org) - Columnar format for tables of data
357
357
  - [`arw`](https://en.wikipedia.org/wiki/Raw_image_format#ARW) - Sony Alpha Raw image file
358
358
  - [`asar`](https://github.com/electron/asar#format) - Archive format primarily used to enclose Electron applications
@@ -366,6 +366,8 @@ Returns a `Set<string>` of supported MIME types.
366
366
  - [`cab`](https://en.wikipedia.org/wiki/Cabinet_(file_format)) - Cabinet file
367
367
  - [`cfb`](https://en.wikipedia.org/wiki/Compound_File_Binary_Format) - Compount File Binary Format
368
368
  - [`chm`](https://en.wikipedia.org/wiki/Microsoft_Compiled_HTML_Help) - Microsoft Compiled HTML Help
369
+ - [`class`](https://en.wikipedia.org/wiki/Java_class_file) - Java class file
370
+ - [`cpio`](https://en.wikipedia.org/wiki/Cpio) - Cpio archive
369
371
  - [`cr2`](https://fileinfo.com/extension/cr2) - Canon Raw image file (v2)
370
372
  - [`cr3`](https://fileinfo.com/extension/cr3) - Canon Raw image file (v3)
371
373
  - [`crx`](https://developer.chrome.com/extensions/crx) - Google Chrome extension
@@ -398,6 +400,7 @@ Returns a `Set<string>` of supported MIME types.
398
400
  - [`ics`](https://en.wikipedia.org/wiki/ICalendar#Data_format) - iCalendar
399
401
  - [`indd`](https://en.wikipedia.org/wiki/Adobe_InDesign#File_format) - Adobe InDesign document
400
402
  - [`it`](https://wiki.openmpt.org/Manual:_Module_formats#The_Impulse_Tracker_format_.28.it.29) - Audio module format: Impulse Tracker
403
+ - [`j2c`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
401
404
  - [`jls`](https://en.wikipedia.org/wiki/Lossless_JPEG#JPEG-LS) - Lossless/near-lossless compression standard for continuous-tone images
402
405
  - [`jp2`](https://en.wikipedia.org/wiki/JPEG_2000) - JPEG 2000
403
406
  - [`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,10 @@ export const extensions = [
142
143
  'pst',
143
144
  'dwg',
144
145
  'parquet',
146
+ 'class',
147
+ 'arj',
148
+ 'cpio',
149
+ 'ace',
145
150
  ];
146
151
 
147
152
  export const mimeTypes = [
@@ -228,6 +233,7 @@ export const mimeTypes = [
228
233
  'video/mp2t',
229
234
  'application/x-blender',
230
235
  'image/bpg',
236
+ 'image/j2c',
231
237
  'image/jp2',
232
238
  'image/jpx',
233
239
  'image/jpm',
@@ -283,4 +289,8 @@ export const mimeTypes = [
283
289
  'application/vnd.ms-outlook',
284
290
  'image/vnd.dwg',
285
291
  'application/x-parquet',
292
+ 'application/java-vm',
293
+ 'application/x-arj',
294
+ 'application/x-cpio',
295
+ 'application/x-ace-compressed',
286
296
  ];