file-type 19.1.1 → 19.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/core.d.ts CHANGED
@@ -164,6 +164,7 @@ export type FileExtension =
164
164
  | 'avro'
165
165
  | 'icc'
166
166
  | 'fbx'
167
+ | 'vsdx'
167
168
  ; // eslint-disable-line semi-style
168
169
 
169
170
  export type MimeType =
@@ -314,6 +315,7 @@ export type MimeType =
314
315
  | 'application/avro'
315
316
  | 'application/vnd.iccprofile'
316
317
  | 'application/x.autodesk.fbx'
318
+ | 'application/vnd.visio'
317
319
  ; // eslint-disable-line semi-style
318
320
 
319
321
  export type FileTypeResult = {
@@ -341,12 +343,12 @@ If file access is available, it is recommended to use `.fromFile()` instead.
341
343
  export function fileTypeFromBuffer(buffer: Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
342
344
 
343
345
  /**
344
- Detect the file type of a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
346
+ Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
345
347
 
346
348
  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.
347
349
 
348
- @param stream - A Node.js Readable stream or Web API Readable Stream representing file data. The Web Readable stream **must be a byte stream**.
349
- @returns The detected file type, or `undefined` when there is no match.
350
+ @param stream - A [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) streaming a file to examine.
351
+ @returns A `Promise` for an object with the detected file type, or `undefined` when there is no match.
350
352
  */
351
353
  export function fileTypeFromStream(stream: AnyWebReadableStream<Uint8Array>): Promise<FileTypeResult | undefined>;
352
354
 
package/core.js CHANGED
@@ -381,6 +381,11 @@ export class FileTypeParser {
381
381
  ext: 'xlsx',
382
382
  mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
383
383
  };
384
+ case 'visio':
385
+ return {
386
+ ext: 'vsdx',
387
+ mime: 'application/vnd.visio',
388
+ };
384
389
  default:
385
390
  break;
386
391
  }
package/index.d.ts CHANGED
@@ -3,19 +3,24 @@ Typings for Node.js specific entry point.
3
3
  */
4
4
 
5
5
  import type {Readable as NodeReadableStream} from 'node:stream';
6
- import type {FileTypeResult, StreamOptions, AnyWebReadableStream} from './core.js';
6
+ import type {FileTypeResult, StreamOptions, AnyWebReadableStream, Detector} from './core.js';
7
7
  import {FileTypeParser} from './core.js';
8
8
 
9
9
  export type ReadableStreamWithFileType = NodeReadableStream & {
10
10
  readonly fileType?: FileTypeResult;
11
11
  };
12
12
 
13
+ /**
14
+ Extending `FileTypeParser` with Node.js engine specific functions.
15
+ */
13
16
  export declare class NodeFileTypeParser extends FileTypeParser {
14
17
  /**
15
- @param stream - Node.js `stream.Readable` or Web API `ReadableStream`.
18
+ @param stream - Node.js `stream.Readable` or web `ReadableStream`.
16
19
  */
17
20
  fromStream(stream: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>;
18
21
 
22
+ fromFile(filePath: string): Promise<FileTypeResult | undefined>;
23
+
19
24
  /**
20
25
  Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
21
26
  */
@@ -25,13 +30,30 @@ export declare class NodeFileTypeParser extends FileTypeParser {
25
30
  /**
26
31
  Detect the file type of a file path.
27
32
 
33
+ The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the file.
34
+
35
+ This is for Node.js only.
36
+
37
+ To read from a [`File`](https://developer.mozilla.org/docs/Web/API/File), see `fileTypeFromBlob()`.
38
+
28
39
  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.
29
40
 
30
- @param path
31
41
  @returns The detected file type and MIME type or `undefined` when there is no match.
32
42
  */
33
- export function fileTypeFromFile(path: string): Promise<FileTypeResult | undefined>;
43
+ export function fileTypeFromFile(filePath: string, options?: {customDetectors?: Iterable<Detector>}): Promise<FileTypeResult | undefined>;
34
44
 
45
+ /**
46
+ Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
47
+
48
+ If the engine is Node.js, this may also be a [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
49
+
50
+ Direct support for Node.js streams will be dropped in the future, when Node.js streams can be converted to Web streams (see [`toWeb()`](https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options)).
51
+
52
+ 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.
53
+
54
+ @param stream - A [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) or [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable) streaming a file to examine.
55
+ @returns A `Promise` for an object with the detected file type, or `undefined` when there is no match.
56
+ */
35
57
  export function fileTypeFromStream(stream: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>;
36
58
 
37
59
  /**
package/index.js CHANGED
@@ -16,6 +16,15 @@ export class NodeFileTypeParser extends FileTypeParser {
16
16
  }
17
17
  }
18
18
 
19
+ async fromFile(path) {
20
+ const tokenizer = await strtok3.fromFile(path);
21
+ try {
22
+ return await super.fromTokenizer(tokenizer);
23
+ } finally {
24
+ await tokenizer.close();
25
+ }
26
+ }
27
+
19
28
  async toDetectionStream(readableStream, options = {}) {
20
29
  const {default: stream} = await import('node:stream');
21
30
  const {sampleSize = reasonableDetectionSizeInBytes} = options;
@@ -53,13 +62,7 @@ export class NodeFileTypeParser extends FileTypeParser {
53
62
  }
54
63
 
55
64
  export async function fileTypeFromFile(path, fileTypeOptions) {
56
- const tokenizer = await strtok3.fromFile(path);
57
- try {
58
- const parser = new FileTypeParser(fileTypeOptions);
59
- return await parser.fromTokenizer(tokenizer);
60
- } finally {
61
- await tokenizer.close();
62
- }
65
+ return (new NodeFileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions);
63
66
  }
64
67
 
65
68
  export async function fileTypeFromStream(stream, fileTypeOptions) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "19.1.1",
3
+ "version": "19.3.0",
4
4
  "description": "Detect the file type of a Uint8Array/ArrayBuffer",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/file-type",
@@ -205,10 +205,11 @@
205
205
  "ace",
206
206
  "avro",
207
207
  "icc",
208
- "fbx"
208
+ "fbx",
209
+ "vsdx"
209
210
  ],
210
211
  "dependencies": {
211
- "strtok3": "^7.1.0",
212
+ "strtok3": "^8.0.0",
212
213
  "token-types": "^6.0.0",
213
214
  "uint8array-extras": "^1.3.0"
214
215
  },
package/readme.md CHANGED
@@ -14,7 +14,7 @@ We accept contributions for commonly used modern file formats, not historical or
14
14
  npm install file-type
15
15
  ```
16
16
 
17
- **This package is a ESM package. Your project needs to be ESM too. [Read more](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).**
17
+ **This package is an ESM package. Your project needs to be ESM too. [Read more](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).**
18
18
 
19
19
  If you use it with Webpack, you need the latest Webpack version and ensure you configure it correctly for ESM.
20
20
 
@@ -130,6 +130,10 @@ A buffer representing file data. It works best if the buffer contains the entire
130
130
 
131
131
  Detect the file type of a file path.
132
132
 
133
+ This is for Node.js only.
134
+
135
+ To read from a [`File`](https://developer.mozilla.org/docs/Web/API/File), see [`fileTypeFromBlob()`](#filetypefromblobblob).
136
+
133
137
  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.
134
138
 
135
139
  Returns a `Promise` for an object with the detected file type:
@@ -147,7 +151,11 @@ The file path to parse.
147
151
 
148
152
  ### fileTypeFromStream(stream)
149
153
 
150
- Detect the file type of a [Node.js readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) or a [Web API ReadableStream](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
154
+ Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
155
+
156
+ If the engine is Node.js, this may also be a [Node.js `stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable).
157
+
158
+ Direct support for Node.js streams will be dropped in the future, when Node.js streams can be converted to Web streams (see [`toWeb()`](https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options)).
151
159
 
152
160
  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.
153
161
 
@@ -166,11 +174,14 @@ A readable stream representing file data.
166
174
 
167
175
  ### fileTypeFromBlob(blob)
168
176
 
169
- Detect the file type of a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
177
+ Detect the file type of a [`Blob`](https://developer.mozilla.org/docs/Web/API/Blob),
178
+
179
+ [!TIP]
180
+ > A [`File` object](https://developer.mozilla.org/docs/Web/API/File) is a `Blob` and can be passed in here.
170
181
 
171
182
  It will **stream** the underlying Blob, and required a [ReadableStreamBYOBReader](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamBYOBReader) which **require Node.js ≥ 20**.
172
183
 
173
- 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.
184
+ The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the blob.
174
185
 
175
186
  Returns a `Promise` for an object with the detected file type:
176
187
 
@@ -316,6 +327,7 @@ Returns a `Set<string>` of supported MIME types.
316
327
  A custom detector is a function that allows specifying custom detection mechanisms.
317
328
 
318
329
  An iterable of detectors can be provided via the `fileTypeOptions` argument for the `FileTypeParser` constructor.
330
+ In Node.js, you should use `NodeFileTypeParser`, which extends `FileTypeParser` and provides access to Node.js specific functions.
319
331
 
320
332
  The detectors are called before the default detections in the provided order.
321
333
 
@@ -331,7 +343,7 @@ If the detector returns `undefined`, there are 2 possible scenarios:
331
343
  Example detector array which can be extended and provided to each public method via the `fileTypeOptions` argument:
332
344
 
333
345
  ```js
334
- import {FileTypeParser} from 'file-type';
346
+ import {FileTypeParser} from 'file-type'; // or `NodeFileTypeParser` in Node.js
335
347
 
336
348
  const customDetectors = [
337
349
  async tokenizer => {
@@ -349,7 +361,7 @@ const customDetectors = [
349
361
  ];
350
362
 
351
363
  const buffer = new Uint8Array(new TextEncoder().encode('UNICORN'));
352
- const parser = new FileTypeParser({customDetectors});
364
+ const parser = new FileTypeParser({customDetectors}); // `NodeFileTypeParser({customDetectors})` in Node.js
353
365
  const fileType = await parser.fromBuffer(buffer);
354
366
  console.log(fileType);
355
367
  ```
@@ -493,6 +505,7 @@ console.log(fileType);
493
505
  - [`ttf`](https://en.wikipedia.org/wiki/TrueType) - TrueType font
494
506
  - [`vcf`](https://en.wikipedia.org/wiki/VCard) - vCard
495
507
  - [`voc`](https://wiki.multimedia.cx/index.php/Creative_Voice) - Creative Voice File
508
+ - [`vsdx`](https://en.wikipedia.org/wiki/Microsoft_Visio) - Microsoft Visio File
496
509
  - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - WebAssembly intermediate compiled format
497
510
  - [`wav`](https://en.wikipedia.org/wiki/WAV) - Waveform Audio file
498
511
  - [`webm`](https://en.wikipedia.org/wiki/WebM) - Web video file
package/supported.js CHANGED
@@ -151,6 +151,7 @@ export const extensions = [
151
151
  'avro',
152
152
  'icc',
153
153
  'fbx',
154
+ 'vsdx',
154
155
  ];
155
156
 
156
157
  export const mimeTypes = [
@@ -301,4 +302,5 @@ export const mimeTypes = [
301
302
  'application/avro',
302
303
  'application/vnd.iccprofile',
303
304
  'application/x.autodesk.fbx', // Invented by us
305
+ 'application/vnd.visio',
304
306
  ];