file-type 16.5.2 → 17.0.2
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 +22 -28
- package/browser.js +21 -19
- package/core.d.ts +362 -356
- package/core.js +1214 -1155
- package/index.d.ts +4 -17
- package/index.js +5 -24
- package/package.json +28 -16
- package/readme.md +113 -97
- package/supported.js +275 -278
- package/util.js +10 -12
package/index.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import * as core from './core';
|
|
4
|
-
|
|
5
|
-
export type ReadableStreamWithFileType = core.ReadableStreamWithFileType;
|
|
6
|
-
export type FileTypeResult = core.FileTypeResult;
|
|
7
|
-
export type FileExtension = core.FileExtension;
|
|
8
|
-
export type MimeType = core.MimeType;
|
|
1
|
+
import {Readable as ReadableStream} from 'node:stream';
|
|
2
|
+
import {FileTypeResult} from './core.js';
|
|
9
3
|
|
|
10
4
|
/**
|
|
11
5
|
Detect the file type of a file path.
|
|
@@ -15,13 +9,6 @@ The file type is detected by checking the [magic number](https://en.wikipedia.or
|
|
|
15
9
|
@param path - The file path to parse.
|
|
16
10
|
@returns The detected file type and MIME type or `undefined` when there is no match.
|
|
17
11
|
*/
|
|
18
|
-
export function
|
|
12
|
+
export function fileTypeFromFile(path: string): Promise<FileTypeResult | undefined>;
|
|
19
13
|
|
|
20
|
-
export
|
|
21
|
-
fromBuffer,
|
|
22
|
-
fromStream,
|
|
23
|
-
fromTokenizer,
|
|
24
|
-
extensions,
|
|
25
|
-
mimeTypes,
|
|
26
|
-
stream
|
|
27
|
-
} from './core';
|
|
14
|
+
export * from './core.js';
|
package/index.js
CHANGED
|
@@ -1,32 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const core = require('./core');
|
|
1
|
+
import * as strtok3 from 'strtok3';
|
|
2
|
+
import {fileTypeFromTokenizer} from './core.js';
|
|
4
3
|
|
|
5
|
-
async function
|
|
4
|
+
export async function fileTypeFromFile(path) {
|
|
6
5
|
const tokenizer = await strtok3.fromFile(path);
|
|
7
6
|
try {
|
|
8
|
-
return await
|
|
7
|
+
return await fileTypeFromTokenizer(tokenizer);
|
|
9
8
|
} finally {
|
|
10
9
|
await tokenizer.close();
|
|
11
10
|
}
|
|
12
11
|
}
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
fromFile
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
Object.assign(fileType, core);
|
|
19
|
-
|
|
20
|
-
Object.defineProperty(fileType, 'extensions', {
|
|
21
|
-
get() {
|
|
22
|
-
return core.extensions;
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
Object.defineProperty(fileType, 'mimeTypes', {
|
|
27
|
-
get() {
|
|
28
|
-
return core.mimeTypes;
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
module.exports = fileType;
|
|
13
|
+
export * from './core.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-type",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "17.0.2",
|
|
4
4
|
"description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "sindresorhus/file-type",
|
|
@@ -10,11 +10,18 @@
|
|
|
10
10
|
"email": "sindresorhus@gmail.com",
|
|
11
11
|
"url": "https://sindresorhus.com"
|
|
12
12
|
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"node": "./index.js",
|
|
17
|
+
"default": "./browser.js"
|
|
18
|
+
},
|
|
19
|
+
"./core": "./core.js"
|
|
20
|
+
},
|
|
13
21
|
"engines": {
|
|
14
|
-
"node": ">=
|
|
22
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
15
23
|
},
|
|
16
24
|
"scripts": {
|
|
17
|
-
"ava": "ava --serial --verbose",
|
|
18
25
|
"test": "xo && ava && tsd"
|
|
19
26
|
},
|
|
20
27
|
"files": [
|
|
@@ -187,18 +194,19 @@
|
|
|
187
194
|
"jxl",
|
|
188
195
|
"vcf"
|
|
189
196
|
],
|
|
190
|
-
"devDependencies": {
|
|
191
|
-
"@types/node": "^13.1.4",
|
|
192
|
-
"ava": "^2.3.0",
|
|
193
|
-
"noop-stream": "^0.1.0",
|
|
194
|
-
"read-chunk": "^3.2.0",
|
|
195
|
-
"tsd": "^0.11.0",
|
|
196
|
-
"xo": "^0.25.3"
|
|
197
|
-
},
|
|
198
197
|
"dependencies": {
|
|
199
|
-
"readable-web-to-node-stream": "^3.0.
|
|
200
|
-
"strtok3": "
|
|
201
|
-
"token-types": "^
|
|
198
|
+
"readable-web-to-node-stream": "^3.0.2",
|
|
199
|
+
"strtok3": "^7.0.0-alpha.7",
|
|
200
|
+
"token-types": "^5.0.0-alpha.1"
|
|
201
|
+
},
|
|
202
|
+
"devDependencies": {
|
|
203
|
+
"@tokenizer/token": "^0.3.0",
|
|
204
|
+
"@types/node": "^16.11.10",
|
|
205
|
+
"ava": "^3.15.0",
|
|
206
|
+
"noop-stream": "^1.0.0",
|
|
207
|
+
"tsd": "^0.19.0",
|
|
208
|
+
"typescript": "^4.5.2",
|
|
209
|
+
"xo": "^0.46.4"
|
|
202
210
|
},
|
|
203
211
|
"xo": {
|
|
204
212
|
"envs": [
|
|
@@ -208,8 +216,12 @@
|
|
|
208
216
|
"rules": {
|
|
209
217
|
"no-inner-declarations": "warn",
|
|
210
218
|
"no-await-in-loop": "warn",
|
|
211
|
-
"
|
|
212
|
-
"
|
|
219
|
+
"no-bitwise": "off",
|
|
220
|
+
"@typescript-eslint/no-unsafe-assignment": "off"
|
|
213
221
|
}
|
|
222
|
+
},
|
|
223
|
+
"ava": {
|
|
224
|
+
"serial": true,
|
|
225
|
+
"verbose": true
|
|
214
226
|
}
|
|
215
227
|
}
|
package/readme.md
CHANGED
|
@@ -38,8 +38,8 @@ This package is for detecting binary-based file formats, not text-based formats
|
|
|
38
38
|
|
|
39
39
|
## Install
|
|
40
40
|
|
|
41
|
-
```
|
|
42
|
-
|
|
41
|
+
```sh
|
|
42
|
+
npm install file-type
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
## Usage
|
|
@@ -49,114 +49,99 @@ $ npm install file-type
|
|
|
49
49
|
Determine file type from a file:
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
|
|
52
|
+
import {fileTypeFromFile} from 'file-type';
|
|
53
53
|
|
|
54
|
-
(
|
|
55
|
-
|
|
56
|
-
//=> {ext: 'png', mime: 'image/png'}
|
|
57
|
-
})();
|
|
54
|
+
console.log(await fileTypeFromFile('Unicorn.png'));
|
|
55
|
+
//=> {ext: 'png', mime: 'image/png'}
|
|
58
56
|
```
|
|
59
57
|
|
|
60
58
|
Determine file type from a Buffer, which may be a portion of the beginning of a file:
|
|
61
59
|
|
|
62
60
|
```js
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
import {fileTypeFromBuffer} from 'file-type';
|
|
62
|
+
import {readChunk} from 'read-chunk';
|
|
65
63
|
|
|
66
|
-
|
|
67
|
-
const buffer = readChunk.sync('Unicorn.png', 0, 4100);
|
|
64
|
+
const buffer = await readChunk('Unicorn.png', {length: 4100});
|
|
68
65
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})();
|
|
66
|
+
console.log(await fileTypeFromBuffer(buffer));
|
|
67
|
+
//=> {ext: 'png', mime: 'image/png'}
|
|
72
68
|
```
|
|
73
69
|
|
|
74
70
|
Determine file type from a stream:
|
|
75
71
|
|
|
76
72
|
```js
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
import fs from 'node:fs';
|
|
74
|
+
import {fileTypeFromStream} from 'file-type';
|
|
79
75
|
|
|
80
|
-
|
|
81
|
-
const stream = fs.createReadStream('Unicorn.mp4');
|
|
76
|
+
const stream = fs.createReadStream('Unicorn.mp4');
|
|
82
77
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
)();
|
|
78
|
+
console.log(await fileTypeFromStream(stream));
|
|
79
|
+
//=> {ext: 'mp4', mime: 'video/mp4'}
|
|
87
80
|
```
|
|
88
81
|
|
|
89
82
|
The stream method can also be used to read from a remote location:
|
|
90
83
|
|
|
91
84
|
```js
|
|
92
|
-
|
|
93
|
-
|
|
85
|
+
import got from 'got';
|
|
86
|
+
import {fileTypeFromStream} from 'file-type';
|
|
94
87
|
|
|
95
88
|
const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
96
89
|
|
|
97
|
-
|
|
98
|
-
const stream = got.stream(url);
|
|
90
|
+
const stream = got.stream(url);
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
})();
|
|
92
|
+
console.log(await fileTypeFromStream(stream));
|
|
93
|
+
//=> {ext: 'jpg', mime: 'image/jpeg'}
|
|
103
94
|
```
|
|
104
95
|
|
|
105
96
|
Another stream example:
|
|
106
97
|
|
|
107
98
|
```js
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
99
|
+
import stream from 'node:stream';
|
|
100
|
+
import fs from 'node:fs';
|
|
101
|
+
import crypto from 'node:crypto';
|
|
102
|
+
import {fileTypeStream} from 'file-type';
|
|
112
103
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const decipher = crypto.createDecipheriv(alg, key, iv);
|
|
104
|
+
const read = fs.createReadStream('encrypted.enc');
|
|
105
|
+
const decipher = crypto.createDecipheriv(alg, key, iv);
|
|
116
106
|
|
|
117
|
-
|
|
107
|
+
const streamWithFileType = await fileTypeStream(stream.pipeline(read, decipher));
|
|
118
108
|
|
|
119
|
-
|
|
120
|
-
|
|
109
|
+
console.log(streamWithFileType.fileType);
|
|
110
|
+
//=> {ext: 'mov', mime: 'video/quicktime'}
|
|
121
111
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
})();
|
|
112
|
+
const write = fs.createWriteStream(`decrypted.${streamWithFileType.fileType.ext}`);
|
|
113
|
+
streamWithFileType.pipe(write);
|
|
125
114
|
```
|
|
126
115
|
|
|
127
116
|
#### Browser
|
|
128
117
|
|
|
129
118
|
```js
|
|
130
|
-
|
|
119
|
+
import {fileTypeFromStream} from 'file-type';
|
|
131
120
|
|
|
132
121
|
const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
133
122
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const fileType = await FileType.fromStream(response.body);
|
|
123
|
+
const response = await fetch(url);
|
|
124
|
+
const fileType = await fileTypeFromStream(response.body);
|
|
137
125
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
})();
|
|
126
|
+
console.log(fileType);
|
|
127
|
+
//=> {ext: 'jpg', mime: 'image/jpeg'}
|
|
141
128
|
```
|
|
142
129
|
|
|
143
130
|
```js
|
|
144
|
-
|
|
131
|
+
import {fileTypeFromBlob} from 'file-type';
|
|
145
132
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
});
|
|
133
|
+
const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
|
|
134
|
+
type: 'plain/text',
|
|
135
|
+
endings: 'native'
|
|
136
|
+
});
|
|
151
137
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
})();
|
|
138
|
+
console.log(await fileTypeFromBlob(blob));
|
|
139
|
+
//=> {ext: 'txt', mime: 'plain/text'}
|
|
155
140
|
```
|
|
156
141
|
|
|
157
142
|
## API
|
|
158
143
|
|
|
159
|
-
###
|
|
144
|
+
### fileTypeFromBuffer(buffer)
|
|
160
145
|
|
|
161
146
|
Detect the file type of a `Buffer`, `Uint8Array`, or `ArrayBuffer`.
|
|
162
147
|
|
|
@@ -177,7 +162,7 @@ Type: `Buffer | Uint8Array | ArrayBuffer`
|
|
|
177
162
|
|
|
178
163
|
A buffer representing file data. It works best if the buffer contains the entire file, it may work with a smaller portion as well.
|
|
179
164
|
|
|
180
|
-
###
|
|
165
|
+
### fileTypeFromFile(filePath)
|
|
181
166
|
|
|
182
167
|
Detect the file type of a file path.
|
|
183
168
|
|
|
@@ -196,7 +181,7 @@ Type: `string`
|
|
|
196
181
|
|
|
197
182
|
The file path to parse.
|
|
198
183
|
|
|
199
|
-
###
|
|
184
|
+
### fileTypeFromStream(stream)
|
|
200
185
|
|
|
201
186
|
Detect the file type of a Node.js [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
|
|
202
187
|
|
|
@@ -215,7 +200,7 @@ Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream
|
|
|
215
200
|
|
|
216
201
|
A readable stream representing file data.
|
|
217
202
|
|
|
218
|
-
###
|
|
203
|
+
### fileTypeFromTokenizer(tokenizer)
|
|
219
204
|
|
|
220
205
|
Detect the file type from an `ITokenizer` source.
|
|
221
206
|
|
|
@@ -233,41 +218,37 @@ Or `undefined` when there is no match.
|
|
|
233
218
|
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.
|
|
234
219
|
|
|
235
220
|
```js
|
|
236
|
-
|
|
237
|
-
|
|
221
|
+
import {makeTokenizer} from '@tokenizer/http';
|
|
222
|
+
import {fileTypeFromTokenizer} from 'file-type';
|
|
238
223
|
|
|
239
224
|
const audioTrackUrl = 'https://test-audio.netlify.com/Various%20Artists%20-%202009%20-%20netBloc%20Vol%2024_%20tiuqottigeloot%20%5BMP3-V2%5D/01%20-%20Diablo%20Swing%20Orchestra%20-%20Heroines.mp3';
|
|
240
225
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
const fileType = await FileType.fromTokenizer(httpTokenizer);
|
|
226
|
+
const httpTokenizer = await makeTokenizer(audioTrackUrl);
|
|
227
|
+
const fileType = await fileTypeFromTokenizer(httpTokenizer);
|
|
244
228
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
})();
|
|
229
|
+
console.log(fileType);
|
|
230
|
+
//=> {ext: 'mp3', mime: 'audio/mpeg'}
|
|
248
231
|
```
|
|
249
232
|
|
|
250
233
|
Or use [`@tokenizer/s3`](https://github.com/Borewit/tokenizer-s3) to determine the file type of a file stored on [Amazon S3](https://aws.amazon.com/s3):
|
|
251
234
|
|
|
252
235
|
```js
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
console.log(fileType);
|
|
270
|
-
})();
|
|
236
|
+
import S3 from 'aws-sdk/clients/s3';
|
|
237
|
+
import {makeTokenizer} from '@tokenizer/s3';
|
|
238
|
+
import {fileTypeFromTokenizer} from 'file-type';
|
|
239
|
+
|
|
240
|
+
// Initialize the S3 client
|
|
241
|
+
const s3 = new S3();
|
|
242
|
+
|
|
243
|
+
// Initialize the S3 tokenizer.
|
|
244
|
+
const s3Tokenizer = await makeTokenizer(s3, {
|
|
245
|
+
Bucket: 'affectlab',
|
|
246
|
+
Key: '1min_35sec.mp4'
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// Figure out what kind of file it is.
|
|
250
|
+
const fileType = await fileTypeFromTokenizer(s3Tokenizer);
|
|
251
|
+
console.log(fileType);
|
|
271
252
|
```
|
|
272
253
|
|
|
273
254
|
Note that only the minimum amount of data required to determine the file type is read (okay, just a bit extra to prevent too many fragmented reads).
|
|
@@ -278,13 +259,48 @@ Type: [`ITokenizer`](https://github.com/Borewit/strtok3#tokenizer)
|
|
|
278
259
|
|
|
279
260
|
A file source implementing the [tokenizer interface](https://github.com/Borewit/strtok3#tokenizer).
|
|
280
261
|
|
|
281
|
-
###
|
|
282
|
-
|
|
283
|
-
Detect the file type of a readable stream.
|
|
262
|
+
### fileTypeStream(readableStream, options?)
|
|
284
263
|
|
|
285
264
|
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 `FileType.fromFile()`.
|
|
286
265
|
|
|
287
|
-
|
|
266
|
+
This method can be handy to put in between a stream, but it comes with a price.
|
|
267
|
+
Internally `stream()` builds up a buffer of `sampleSize` bytes, used as a sample, to determine the file type.
|
|
268
|
+
The sample size impacts the file detection resolution.
|
|
269
|
+
A smaller sample size will result in lower probability of the best file type detection.
|
|
270
|
+
|
|
271
|
+
**Note:** This method is only available when using Node.js.
|
|
272
|
+
**Note:** Requires Node.js 14 or later.
|
|
273
|
+
|
|
274
|
+
#### readableStream
|
|
275
|
+
|
|
276
|
+
Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream_readable)
|
|
277
|
+
|
|
278
|
+
#### options
|
|
279
|
+
|
|
280
|
+
Type: `object`
|
|
281
|
+
|
|
282
|
+
##### sampleSize
|
|
283
|
+
|
|
284
|
+
Type: `number`\
|
|
285
|
+
Default: `4100`
|
|
286
|
+
|
|
287
|
+
The sample size in bytes.
|
|
288
|
+
|
|
289
|
+
#### Example
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
import got from 'got';
|
|
293
|
+
import {fileTypeStream} from 'file-type';
|
|
294
|
+
|
|
295
|
+
const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
|
|
296
|
+
|
|
297
|
+
const stream1 = got.stream(url);
|
|
298
|
+
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
|
|
299
|
+
|
|
300
|
+
if (stream2.fileType && stream2.fileType.mime === 'image/jpeg') {
|
|
301
|
+
// stream2 can be used to stream the JPEG image (from the very beginning of the stream)
|
|
302
|
+
}
|
|
303
|
+
```
|
|
288
304
|
|
|
289
305
|
#### readableStream
|
|
290
306
|
|
|
@@ -292,13 +308,13 @@ Type: [`stream.Readable`](https://nodejs.org/api/stream.html#stream_class_stream
|
|
|
292
308
|
|
|
293
309
|
The input stream.
|
|
294
310
|
|
|
295
|
-
###
|
|
311
|
+
### supportedExtensions
|
|
296
312
|
|
|
297
|
-
Returns a
|
|
313
|
+
Returns a `Set<string>` of supported file extensions.
|
|
298
314
|
|
|
299
|
-
###
|
|
315
|
+
### supportedMimeTypes
|
|
300
316
|
|
|
301
|
-
Returns a
|
|
317
|
+
Returns a `Set<string>` of supported MIME types.
|
|
302
318
|
|
|
303
319
|
## Supported file types
|
|
304
320
|
|
|
@@ -379,7 +395,7 @@ Returns a set of supported MIME types.
|
|
|
379
395
|
- [`lz`](https://en.wikipedia.org/wiki/Lzip) - Arhive file
|
|
380
396
|
- [`cfb`](https://en.wikipedia.org/wiki/Compound_File_Binary_Format) - Compount File Binary Format
|
|
381
397
|
- [`mxf`](https://en.wikipedia.org/wiki/Material_Exchange_Format) - Material Exchange Format
|
|
382
|
-
- [`mts`](https://en.wikipedia.org/wiki/.m2ts) - Blu-ray Disc Audio-Video
|
|
398
|
+
- [`mts`](https://en.wikipedia.org/wiki/.m2ts) - MPEG-2 Transport Stream, both raw and Blu-ray Disc Audio-Video (BDAV) versions
|
|
383
399
|
- [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - WebAssembly intermediate compiled format
|
|
384
400
|
- [`blend`](https://wiki.blender.org/index.php/Dev:Source/Architecture/File_Format) - Blender project
|
|
385
401
|
- [`bpg`](https://bellard.org/bpg/) - Better Portable Graphics file
|