file-type 19.1.1 → 19.2.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 +3 -3
- package/index.d.ts +26 -4
- package/index.js +10 -7
- package/package.json +2 -2
- package/readme.md +18 -6
package/core.d.ts
CHANGED
|
@@ -341,12 +341,12 @@ If file access is available, it is recommended to use `.fromFile()` instead.
|
|
|
341
341
|
export function fileTypeFromBuffer(buffer: Uint8Array | ArrayBuffer): Promise<FileTypeResult | undefined>;
|
|
342
342
|
|
|
343
343
|
/**
|
|
344
|
-
Detect the file type of a
|
|
344
|
+
Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream).
|
|
345
345
|
|
|
346
346
|
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
347
|
|
|
348
|
-
@param stream - A
|
|
349
|
-
@returns
|
|
348
|
+
@param stream - A [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) streaming a file to examine.
|
|
349
|
+
@returns A `Promise` for an object with the detected file type, or `undefined` when there is no match.
|
|
350
350
|
*/
|
|
351
351
|
export function fileTypeFromStream(stream: AnyWebReadableStream<Uint8Array>): Promise<FileTypeResult | undefined>;
|
|
352
352
|
|
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
|
|
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(
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "19.2.0",
|
|
4
4
|
"description": "Detect the file type of a Uint8Array/ArrayBuffer",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/file-type",
|
|
@@ -208,7 +208,7 @@
|
|
|
208
208
|
"fbx"
|
|
209
209
|
],
|
|
210
210
|
"dependencies": {
|
|
211
|
-
"strtok3": "^
|
|
211
|
+
"strtok3": "^8.0.0",
|
|
212
212
|
"token-types": "^6.0.0",
|
|
213
213
|
"uint8array-extras": "^1.3.0"
|
|
214
214
|
},
|
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
|
|
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 [
|
|
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/
|
|
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
|
|
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
|
```
|