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/index.d.ts CHANGED
@@ -1,11 +1,5 @@
1
- /// <reference types="node"/>
2
- import {Readable as ReadableStream} from 'stream';
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 fromFile(path: string): Promise<core.FileTypeResult | undefined>;
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
- 'use strict';
2
- const strtok3 = require('strtok3');
3
- const core = require('./core');
1
+ import * as strtok3 from 'strtok3';
2
+ import {fileTypeFromTokenizer} from './core.js';
4
3
 
5
- async function fromFile(path) {
4
+ export async function fileTypeFromFile(path) {
6
5
  const tokenizer = await strtok3.fromFile(path);
7
6
  try {
8
- return await core.fromTokenizer(tokenizer);
7
+ return await fileTypeFromTokenizer(tokenizer);
9
8
  } finally {
10
9
  await tokenizer.close();
11
10
  }
12
11
  }
13
12
 
14
- const fileType = {
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": "16.5.2",
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": ">=10"
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.0",
200
- "strtok3": "6.1.3",
201
- "token-types": "^3.0.0"
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
- "promise/prefer-await-to-then": "warn",
212
- "prefer-named-capture-group": "off"
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
- $ npm install file-type
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
- const FileType = require('file-type');
52
+ import {fileTypeFromFile} from 'file-type';
53
53
 
54
- (async () => {
55
- console.log(await FileType.fromFile('Unicorn.png'));
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
- const FileType = require('file-type');
64
- const readChunk = require('read-chunk');
61
+ import {fileTypeFromBuffer} from 'file-type';
62
+ import {readChunk} from 'read-chunk';
65
63
 
66
- (async () => {
67
- const buffer = readChunk.sync('Unicorn.png', 0, 4100);
64
+ const buffer = await readChunk('Unicorn.png', {length: 4100});
68
65
 
69
- console.log(await FileType.fromBuffer(buffer));
70
- //=> {ext: 'png', mime: 'image/png'}
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
- const fs = require('fs');
78
- const FileType = require('file-type');
73
+ import fs from 'node:fs';
74
+ import {fileTypeFromStream} from 'file-type';
79
75
 
80
- (async () => {
81
- const stream = fs.createReadStream('Unicorn.mp4');
76
+ const stream = fs.createReadStream('Unicorn.mp4');
82
77
 
83
- console.log(await FileType.fromStream(stream));
84
- //=> {ext: 'mp4', mime: 'video/mp4'}
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
- const got = require('got');
93
- const FileType = require('file-type');
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
- (async () => {
98
- const stream = got.stream(url);
90
+ const stream = got.stream(url);
99
91
 
100
- console.log(await FileType.fromStream(stream));
101
- //=> {ext: 'jpg', mime: 'image/jpeg'}
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
- const stream = require('stream');
109
- const fs = require('fs');
110
- const crypto = require('crypto');
111
- const FileType = require('file-type');
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
- (async () => {
114
- const read = fs.createReadStream('encrypted.enc');
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
- const fileTypeStream = await FileType.stream(stream.pipeline(read, decipher));
107
+ const streamWithFileType = await fileTypeStream(stream.pipeline(read, decipher));
118
108
 
119
- console.log(fileTypeStream.fileType);
120
- //=> {ext: 'mov', mime: 'video/quicktime'}
109
+ console.log(streamWithFileType.fileType);
110
+ //=> {ext: 'mov', mime: 'video/quicktime'}
121
111
 
122
- const write = fs.createWriteStream(`decrypted.${fileTypeStream.fileType.ext}`);
123
- fileTypeStream.pipe(write);
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
- const FileType = require('file-type/browser');
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
- (async () => {
135
- const response = await fetch(url);
136
- const fileType = await FileType.fromStream(response.body);
123
+ const response = await fetch(url);
124
+ const fileType = await fileTypeFromStream(response.body);
137
125
 
138
- console.log(fileType);
139
- //=> {ext: 'jpg', mime: 'image/jpeg'}
140
- })();
126
+ console.log(fileType);
127
+ //=> {ext: 'jpg', mime: 'image/jpeg'}
141
128
  ```
142
129
 
143
130
  ```js
144
- const FileType = require('file-type/browser');
131
+ import {fileTypeFromBlob} from 'file-type';
145
132
 
146
- (async () => {
147
- const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
148
- type: 'plain/text',
149
- endings: 'native'
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
- console.log(await FileType.fromBlob(blob));
153
- //=> {ext: 'txt', mime: 'plain/text'}
154
- })();
138
+ console.log(await fileTypeFromBlob(blob));
139
+ //=> {ext: 'txt', mime: 'plain/text'}
155
140
  ```
156
141
 
157
142
  ## API
158
143
 
159
- ### FileType.fromBuffer(buffer)
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
- ### FileType.fromFile(filePath)
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
- ### FileType.fromStream(stream)
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
- ### FileType.fromTokenizer(tokenizer)
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
- const {makeTokenizer} = require('@tokenizer/http');
237
- const FileType = require('file-type');
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
- (async () => {
242
- const httpTokenizer = await makeTokenizer(audioTrackUrl);
243
- const fileType = await FileType.fromTokenizer(httpTokenizer);
226
+ const httpTokenizer = await makeTokenizer(audioTrackUrl);
227
+ const fileType = await fileTypeFromTokenizer(httpTokenizer);
244
228
 
245
- console.log(fileType);
246
- //=> {ext: 'mp3', mime: 'audio/mpeg'}
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
- const FileType = require('file-type');
254
- const S3 = require('aws-sdk/clients/s3');
255
- const {makeTokenizer} = require('@tokenizer/s3');
256
-
257
- (async () => {
258
- // Initialize the S3 client
259
- const s3 = new S3();
260
-
261
- // Initialize the S3 tokenizer.
262
- const s3Tokenizer = await makeTokenizer(s3, {
263
- Bucket: 'affectlab',
264
- Key: '1min_35sec.mp4'
265
- });
266
-
267
- // Figure out what kind of file it is.
268
- const fileType = await FileType.fromTokenizer(s3Tokenizer);
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
- ### FileType.stream(readableStream)
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
- *Note:* This method is only available using Node.js.
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
- ### FileType.extensions
311
+ ### supportedExtensions
296
312
 
297
- Returns a set of supported file extensions.
313
+ Returns a `Set<string>` of supported file extensions.
298
314
 
299
- ### FileType.mimeTypes
315
+ ### supportedMimeTypes
300
316
 
301
- Returns a set of supported MIME types.
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 MPEG-2 Transport Stream
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