file-type 21.3.3 → 22.0.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/package.json +36 -53
- package/readme.md +35 -105
- package/source/detectors/asf.js +127 -0
- package/source/detectors/ebml.js +120 -0
- package/source/detectors/png.js +123 -0
- package/source/detectors/zip.js +643 -0
- package/{core.d.ts → source/index.d.ts} +49 -22
- package/{core.js → source/index.js} +242 -1122
- package/source/index.test-d.ts +53 -0
- package/source/parser.js +65 -0
- package/{supported.js → source/supported.js} +14 -6
- package/{util.js → source/tokens.js} +2 -2
- package/index.d.ts +0 -98
- package/index.js +0 -126
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
Typings for primary entry point
|
|
2
|
+
Typings for primary entry point.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type {ReadableStream as WebReadableStream} from 'node:stream/web';
|
|
6
|
-
import type {ITokenizer
|
|
6
|
+
import type {ITokenizer} from 'strtok3';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
Either the Node.js ReadableStream or the `lib.dom.d.ts` ReadableStream.
|
|
@@ -28,9 +28,9 @@ Detect the file type of a `Uint8Array` or `ArrayBuffer`.
|
|
|
28
28
|
|
|
29
29
|
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.
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
In Node.js, it is recommended to use `fileTypeFromFile()` instead when reading from a file path.
|
|
32
32
|
|
|
33
|
-
@param buffer -
|
|
33
|
+
@param buffer - A Uint8Array or ArrayBuffer representing file data. It works best if the buffer contains the entire file. It may work with a smaller portion as well.
|
|
34
34
|
@param options - Options to override default behavior.
|
|
35
35
|
@returns The detected file type, or `undefined` when there is no match.
|
|
36
36
|
*/
|
|
@@ -41,11 +41,13 @@ Detect the file type of a [web `ReadableStream`](https://developer.mozilla.org/e
|
|
|
41
41
|
|
|
42
42
|
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.
|
|
43
43
|
|
|
44
|
+
If you have a Node.js `stream.Readable`, convert it with [`Readable.toWeb()`](https://nodejs.org/api/stream.html#streamreadabletowebstreamreadable-options).
|
|
45
|
+
|
|
44
46
|
@param stream - A [web `ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) streaming a file to examine.
|
|
45
47
|
@param options - Options to override default behavior.
|
|
46
48
|
@returns A `Promise` for an object with the detected file type, or `undefined` when there is no match.
|
|
47
49
|
*/
|
|
48
|
-
export function fileTypeFromStream(stream:
|
|
50
|
+
export function fileTypeFromStream(stream: AnyWebReadableStream<Uint8Array>, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
49
51
|
|
|
50
52
|
/**
|
|
51
53
|
Detect the file type from an [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer) source.
|
|
@@ -58,7 +60,7 @@ A tokenizer propagates the internal read functions, allowing alternative transpo
|
|
|
58
60
|
@param options - Options to override default behavior.
|
|
59
61
|
@returns The detected file type, or `undefined` when there is no match.
|
|
60
62
|
|
|
61
|
-
An example is [`@tokenizer/http`](https://github.com/Borewit/tokenizer-http), which requests data using [HTTP-range-requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests). A difference with a conventional stream and the [*tokenizer*](https://github.com/Borewit/strtok3#tokenizer), is that it
|
|
63
|
+
An example is [`@tokenizer/http`](https://github.com/Borewit/tokenizer-http), which requests data using [HTTP-range-requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests). A difference with a conventional stream and the [*tokenizer*](https://github.com/Borewit/strtok3#tokenizer), is that it can *ignore* (seek, fast-forward) in the stream. For example, you may only need and read the first 6 bytes, and the last 128 bytes, which may be an advantage in case reading the entire file would take longer.
|
|
62
64
|
|
|
63
65
|
@example
|
|
64
66
|
```
|
|
@@ -123,10 +125,8 @@ A custom file type detector.
|
|
|
123
125
|
Custom file type detectors are plugins designed to extend the default detection capabilities.
|
|
124
126
|
They allow support for uncommon file types, non-binary formats, or customized detection behavior.
|
|
125
127
|
|
|
126
|
-
Detectors can be added via the constructor options or by modifying `FileTypeParser#detectors` directly.
|
|
127
|
-
Detectors provided through the constructor are executed before the default ones.
|
|
128
|
-
|
|
129
128
|
Detectors can be added via the constructor options or by directly modifying `FileTypeParser#detectors`.
|
|
129
|
+
Detectors provided through the constructor are executed before the default ones.
|
|
130
130
|
|
|
131
131
|
### Example adding a detector
|
|
132
132
|
|
|
@@ -139,8 +139,11 @@ const fileType = await parser.fromFile('sample.kml');
|
|
|
139
139
|
console.log(fileType);
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
### Available-
|
|
142
|
+
### Available third-party file-type detectors
|
|
143
143
|
|
|
144
|
+
- [@file-type/av](https://github.com/Borewit/file-type-av): Improves detection of audio and video file formats, with accurate differentiation between the two
|
|
145
|
+
- [@file-type/cfbf](https://github.com/Borewit/file-type-cfbf): Detects Compound File Binary Format (CFBF) based formats, such as Office 97–2003 documents and `.msi`.
|
|
146
|
+
- [@file-type/pdf](https://github.com/Borewit/file-type-pdf): Detects PDF based file types, such as Adobe Illustrator
|
|
144
147
|
- [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
|
|
145
148
|
|
|
146
149
|
### Detector execution flow
|
|
@@ -152,13 +155,14 @@ If a detector returns `undefined`, the following rules apply:
|
|
|
152
155
|
|
|
153
156
|
### Example writing a custom detector
|
|
154
157
|
|
|
155
|
-
Below is an example of a custom detector
|
|
158
|
+
Below is an example of a custom detector. This can be passed to the `FileTypeParser` via the `customDetectors` option.
|
|
156
159
|
|
|
157
160
|
```
|
|
158
161
|
import {FileTypeParser} from 'file-type';
|
|
159
162
|
|
|
160
|
-
const
|
|
161
|
-
|
|
163
|
+
const unicornDetector = {
|
|
164
|
+
id: 'unicorn',
|
|
165
|
+
async detect(tokenizer) {
|
|
162
166
|
const unicornHeader = [85, 78, 73, 67, 79, 82, 78]; // "UNICORN" in ASCII decimal
|
|
163
167
|
|
|
164
168
|
const buffer = new Uint8Array(unicornHeader.length);
|
|
@@ -168,11 +172,11 @@ const customDetectors = [
|
|
|
168
172
|
}
|
|
169
173
|
|
|
170
174
|
return undefined;
|
|
171
|
-
}
|
|
172
|
-
|
|
175
|
+
}
|
|
176
|
+
};
|
|
173
177
|
|
|
174
178
|
const buffer = new Uint8Array([85, 78, 73, 67, 79, 82, 78]);
|
|
175
|
-
const parser = new FileTypeParser({customDetectors});
|
|
179
|
+
const parser = new FileTypeParser({customDetectors: [unicornDetector]});
|
|
176
180
|
const fileType = await parser.fromBuffer(buffer);
|
|
177
181
|
console.log(fileType); // {ext: 'unicorn', mime: 'application/unicorn'}
|
|
178
182
|
```
|
|
@@ -189,6 +193,11 @@ export type Detector = {
|
|
|
189
193
|
export type FileTypeOptions = {
|
|
190
194
|
customDetectors?: Iterable<Detector>;
|
|
191
195
|
|
|
196
|
+
/**
|
|
197
|
+
An `AbortSignal` to cancel the detection.
|
|
198
|
+
*/
|
|
199
|
+
signal?: AbortSignal;
|
|
200
|
+
|
|
192
201
|
/**
|
|
193
202
|
Specifies the byte tolerance for locating the first MPEG audio frame (e.g. `.mp1`, `.mp2`, `.mp3`, `.aac`).
|
|
194
203
|
|
|
@@ -196,7 +205,7 @@ export type FileTypeOptions = {
|
|
|
196
205
|
|
|
197
206
|
A tolerance of 10 bytes covers most cases.
|
|
198
207
|
|
|
199
|
-
|
|
208
|
+
@default 0
|
|
200
209
|
*/
|
|
201
210
|
mpegOffsetTolerance?: number;
|
|
202
211
|
};
|
|
@@ -210,16 +219,24 @@ export type AnyWebReadableByteStreamWithFileType = AnyWebReadableStream<Uint8Arr
|
|
|
210
219
|
};
|
|
211
220
|
|
|
212
221
|
/**
|
|
213
|
-
|
|
222
|
+
Detect the file type of a file path.
|
|
223
|
+
|
|
224
|
+
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.
|
|
225
|
+
|
|
226
|
+
Only available in environments where `node:fs` is available, such as Node.js. To read from a [`File`](https://developer.mozilla.org/docs/Web/API/File), see `fileTypeFromBlob()`.
|
|
227
|
+
|
|
228
|
+
@param filePath - The file path to examine.
|
|
229
|
+
@param options - Options to override default behavior.
|
|
230
|
+
@returns The detected file type and MIME type or `undefined` when there is no match.
|
|
214
231
|
*/
|
|
215
|
-
export function fileTypeFromFile(filePath: string, options?:
|
|
232
|
+
export function fileTypeFromFile(filePath: string, options?: FileTypeOptions): Promise<FileTypeResult | undefined>;
|
|
216
233
|
|
|
217
234
|
/**
|
|
218
235
|
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()`.
|
|
219
236
|
|
|
220
237
|
This method can be handy to put in a stream pipeline, but it comes with a price. Internally `stream()` builds up a buffer of `sampleSize` bytes, used as a sample, to determine the file type. The sample size impacts the file detection resolution. A smaller sample size will result in lower probability of the best file type detection.
|
|
221
238
|
*/
|
|
222
|
-
export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
239
|
+
export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions & FileTypeOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
223
240
|
|
|
224
241
|
export declare class FileTypeParser {
|
|
225
242
|
/**
|
|
@@ -229,7 +246,7 @@ export declare class FileTypeParser {
|
|
|
229
246
|
*/
|
|
230
247
|
detectors: Detector[];
|
|
231
248
|
|
|
232
|
-
constructor(options?:
|
|
249
|
+
constructor(options?: FileTypeOptions);
|
|
233
250
|
|
|
234
251
|
/**
|
|
235
252
|
Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
@@ -246,8 +263,18 @@ export declare class FileTypeParser {
|
|
|
246
263
|
*/
|
|
247
264
|
fromBlob(blob: Blob): Promise<FileTypeResult | undefined>;
|
|
248
265
|
|
|
266
|
+
/**
|
|
267
|
+
Works the same way as {@link fileTypeFromStream}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
268
|
+
*/
|
|
269
|
+
fromStream(stream: AnyWebReadableStream<Uint8Array>): Promise<FileTypeResult | undefined>;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
Works the same way as {@link fileTypeFromFile}, additionally taking into account custom detectors (if any were provided to the constructor). Only available where `node:fs` is available.
|
|
273
|
+
*/
|
|
274
|
+
fromFile(filePath: string): Promise<FileTypeResult | undefined>;
|
|
275
|
+
|
|
249
276
|
/**
|
|
250
277
|
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
|
|
251
278
|
*/
|
|
252
|
-
toDetectionStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
279
|
+
toDetectionStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions & FileTypeOptions): Promise<AnyWebReadableByteStreamWithFileType>;
|
|
253
280
|
}
|