file-type 21.3.0 → 21.3.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.
Files changed (4) hide show
  1. package/core.js +925 -148
  2. package/index.js +27 -3
  3. package/package.json +4 -4
  4. package/readme.md +9 -7
package/index.js CHANGED
@@ -5,13 +5,37 @@ Node.js specific entry point.
5
5
  import {ReadableStream as WebReadableStream} from 'node:stream/web';
6
6
  import {pipeline, PassThrough, Readable} from 'node:stream';
7
7
  import * as strtok3 from 'strtok3';
8
- import {FileTypeParser as DefaultFileTypeParser, reasonableDetectionSizeInBytes} from './core.js';
8
+ import {
9
+ FileTypeParser as DefaultFileTypeParser,
10
+ reasonableDetectionSizeInBytes,
11
+ normalizeSampleSize,
12
+ } from './core.js';
13
+
14
+ function isTokenizerStreamBoundsError(error) {
15
+ if (
16
+ !(error instanceof RangeError)
17
+ || error.message !== 'offset is out of bounds'
18
+ || typeof error.stack !== 'string'
19
+ ) {
20
+ return false;
21
+ }
22
+
23
+ // Some malformed or non-byte Node.js streams can surface this tokenizer-internal range error.
24
+ // Note: This stack-trace check is fragile and may break if strtok3 restructures its internals.
25
+ return /strtok3[/\\]lib[/\\]stream[/\\]/.test(error.stack);
26
+ }
9
27
 
10
28
  export class FileTypeParser extends DefaultFileTypeParser {
11
29
  async fromStream(stream) {
12
- const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.tokenizerOptions) : strtok3.fromStream(stream, this.tokenizerOptions));
30
+ const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.getTokenizerOptions()) : strtok3.fromStream(stream, this.getTokenizerOptions()));
13
31
  try {
14
32
  return await super.fromTokenizer(tokenizer);
33
+ } catch (error) {
34
+ if (isTokenizerStreamBoundsError(error)) {
35
+ return;
36
+ }
37
+
38
+ throw error;
15
39
  } finally {
16
40
  await tokenizer.close();
17
41
  }
@@ -31,7 +55,7 @@ export class FileTypeParser extends DefaultFileTypeParser {
31
55
  return super.toDetectionStream(readableStream, options);
32
56
  }
33
57
 
34
- const {sampleSize = reasonableDetectionSizeInBytes} = options;
58
+ const sampleSize = normalizeSampleSize(options.sampleSize ?? reasonableDetectionSizeInBytes);
35
59
 
36
60
  return new Promise((resolve, reject) => {
37
61
  readableStream.on('error', reject);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "21.3.0",
3
+ "version": "21.3.2",
4
4
  "description": "Detect the file type of a file, stream, or data",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/file-type",
@@ -258,12 +258,12 @@
258
258
  },
259
259
  "devDependencies": {
260
260
  "@tokenizer/token": "^0.3.0",
261
- "@types/node": "^22.15.21",
262
- "ava": "^6.3.0",
261
+ "@types/node": "^25.3.3",
262
+ "ava": "^7.0.0",
263
263
  "commonmark": "^0.31.2",
264
264
  "get-stream": "^9.0.1",
265
265
  "noop-stream": "^1.0.0",
266
- "tsd": "^0.32.0",
266
+ "tsd": "^0.33.0",
267
267
  "xo": "^0.60.0"
268
268
  },
269
269
  "xo": {
package/readme.md CHANGED
@@ -16,11 +16,12 @@ We accept contributions for commonly used modern file formats, not historical or
16
16
  npm install file-type
17
17
  ```
18
18
 
19
- **This package is an ESM package. Your project needs to be ESM too. [Read more](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). For TypeScript + CommonJS, see [`load-esm`](https://github.com/Borewit/load-esm).**
19
+ **This package is an ESM package. Your project needs to be ESM too. [Read more](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). For TypeScript + CommonJS, see [`load-esm`](https://github.com/Borewit/load-esm).** If you use it with Webpack, you need the latest Webpack version and ensure you configure it correctly for ESM.
20
20
 
21
- If you use it with Webpack, you need the latest Webpack version and ensure you configure it correctly for ESM.
22
-
23
- File type detection is based on binary signatures (magic numbers) and should be treated as a best-effort hint, not a guarantee.
21
+ > [!IMPORTANT]
22
+ > File type detection is based on binary signatures (magic numbers) and is a best-effort hint. It does not guarantee the file is actually of that type or that the file is valid/not malformed.
23
+ >
24
+ > Robustness against malformed input is best-effort. When processing untrusted files on a server, enforce a reasonable file size limit and use a worker thread with a timeout (e.g., [`make-asynchronous`](https://github.com/sindresorhus/make-asynchronous)). These are not considered security issues in this package.
24
25
 
25
26
  ## Usage
26
27
 
@@ -380,6 +381,7 @@ console.log(fileType);
380
381
  ### Available third-party file-type detectors
381
382
 
382
383
  - [@file-type/av](https://github.com/Borewit/file-type-av): Improves detection of audio and video file formats, with accurate differentiation between the two
384
+ - [@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`.
383
385
  - [@file-type/pdf](https://github.com/Borewit/file-type-pdf): Detects PDF based file types, such as Adobe Illustrator
384
386
  - [@file-type/xml](https://github.com/Borewit/file-type-xml): Detects common XML file types, such as GLM, KML, MusicXML, RSS, SVG, and XHTML
385
387
 
@@ -628,14 +630,14 @@ abortController.abort(); // Abort file-type reading from the Blob stream.
628
630
 
629
631
  *[Pull requests](.github/pull_request_template.md) are welcome for additional commonly used file types.*
630
632
 
631
- The following file types will not be accepted:
632
- - [MS-CFB: Microsoft Compound File Binary File Format based formats](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b), too old and difficult to parse:
633
+ The following file types will not be accepted, but most of them are supported by [third-party detector](#available-third-party-file-type-detectors)
634
+ - [MS-CFB: Microsoft Compound File Binary File Format based formats](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b)
633
635
  - `.doc` - Microsoft Word 97-2003 Document
634
636
  - `.xls` - Microsoft Excel 97-2003 Document
635
637
  - `.ppt` - Microsoft PowerPoint97-2003 Document
636
638
  - `.msi` - Microsoft Windows Installer
637
639
  - `.csv` - [Reason.](https://github.com/sindresorhus/file-type/issues/264#issuecomment-568439196)
638
- - `.svg` - Supported by [third-party detector](#available-third-party-file-type-detectors).
640
+ - `.svg`
639
641
 
640
642
  #### tokenizer
641
643