file-type 19.4.1 → 19.6.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 CHANGED
@@ -165,6 +165,8 @@ export type FileExtension =
165
165
  | 'icc'
166
166
  | 'fbx'
167
167
  | 'vsdx'
168
+ | 'vtt'
169
+ | 'apk'
168
170
  ; // eslint-disable-line semi-style
169
171
 
170
172
  export type MimeType =
@@ -212,9 +214,9 @@ export type MimeType =
212
214
  | 'video/3gpp'
213
215
  | 'audio/mpeg'
214
216
  | 'audio/mp4' // RFC 4337
215
- | 'audio/opus'
216
217
  | 'video/ogg'
217
218
  | 'audio/ogg'
219
+ | 'audio/ogg; codecs=opus'
218
220
  | 'application/ogg'
219
221
  | 'audio/x-flac'
220
222
  | 'audio/ape'
@@ -270,6 +272,7 @@ export type MimeType =
270
272
  | 'audio/x-musepack'
271
273
  | 'text/calendar'
272
274
  | 'text/vcard'
275
+ | 'text/vtt'
273
276
  | 'model/gltf-binary'
274
277
  | 'application/vnd.tcpdump.pcap'
275
278
  | 'audio/x-dsf' // Non-standard
@@ -316,6 +319,7 @@ export type MimeType =
316
319
  | 'application/vnd.iccprofile'
317
320
  | 'application/x.autodesk.fbx'
318
321
  | 'application/vnd.visio'
322
+ | 'application/vnd.android.package-archive'
319
323
  ; // eslint-disable-line semi-style
320
324
 
321
325
  export type FileTypeResult = {
@@ -489,7 +493,7 @@ export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, opti
489
493
  export declare class FileTypeParser {
490
494
  detectors: Iterable<Detector>;
491
495
 
492
- constructor(options?: {customDetectors?: Iterable<Detector>});
496
+ constructor(options?: {customDetectors?: Iterable<Detector>; signal: AbortSignal});
493
497
 
494
498
  /**
495
499
  Works the same way as {@link fileTypeFromBuffer}, additionally taking into account custom detectors (if any were provided to the constructor).
package/core.js CHANGED
@@ -58,7 +58,9 @@ export async function fileTypeStream(webStream, options) {
58
58
  export class FileTypeParser {
59
59
  constructor(options) {
60
60
  this.detectors = options?.customDetectors;
61
-
61
+ this.tokenizerOptions = {
62
+ abortSignal: options?.signal,
63
+ };
62
64
  this.fromTokenizer = this.fromTokenizer.bind(this);
63
65
  this.fromBuffer = this.fromBuffer.bind(this);
64
66
  this.parse = this.parse.bind(this);
@@ -92,7 +94,7 @@ export class FileTypeParser {
92
94
  return;
93
95
  }
94
96
 
95
- return this.fromTokenizer(strtok3.fromBuffer(buffer));
97
+ return this.fromTokenizer(strtok3.fromBuffer(buffer, this.tokenizerOptions));
96
98
  }
97
99
 
98
100
  async fromBlob(blob) {
@@ -100,7 +102,7 @@ export class FileTypeParser {
100
102
  }
101
103
 
102
104
  async fromStream(stream) {
103
- const tokenizer = await strtok3.fromWebStream(stream);
105
+ const tokenizer = await strtok3.fromWebStream(stream, this.tokenizerOptions);
104
106
  try {
105
107
  return await this.fromTokenizer(tokenizer);
106
108
  } finally {
@@ -402,6 +404,13 @@ export class FileTypeParser {
402
404
  zipHeader.filename = await tokenizer.readToken(new Token.StringType(zipHeader.filenameLength, 'utf-8'));
403
405
  await tokenizer.ignore(zipHeader.extraFieldLength);
404
406
 
407
+ if (/classes\d*\.dex/.test(zipHeader.filename)) {
408
+ return {
409
+ ext: 'apk',
410
+ mime: 'application/vnd.android.package-archive',
411
+ };
412
+ }
413
+
405
414
  // Assumes signed `.xpi` from addons.mozilla.org
406
415
  if (zipHeader.filename === 'META-INF/mozilla.rsa') {
407
416
  return {
@@ -527,7 +536,7 @@ export class FileTypeParser {
527
536
  if (_check(type, [0x4F, 0x70, 0x75, 0x73, 0x48, 0x65, 0x61, 0x64])) {
528
537
  return {
529
538
  ext: 'opus',
530
- mime: 'audio/opus',
539
+ mime: 'audio/ogg; codecs=opus',
531
540
  };
532
541
  }
533
542
 
@@ -1129,14 +1138,16 @@ export class FileTypeParser {
1129
1138
  };
1130
1139
  }
1131
1140
 
1132
- if (this.checkString('**ACE', {offset: 7})) {
1133
- await tokenizer.peekBuffer(this.buffer, {length: 14, mayBeLess: true});
1134
- if (this.checkString('**', {offset: 12})) {
1135
- return {
1136
- ext: 'ace',
1137
- mime: 'application/x-ace-compressed',
1138
- };
1139
- }
1141
+ if (
1142
+ this.checkString('WEBVTT')
1143
+ && (
1144
+ // One of LF, CR, tab, space, or end of file must follow "WEBVTT" per the spec (see `fixture/fixture-vtt-*.vtt` for examples). Note that `\0` is technically the null character (there is no such thing as an EOF character). However, checking for `\0` gives us the same result as checking for the end of the stream.
1145
+ (['\n', '\r', '\t', ' ', '\0'].some(char7 => this.checkString(char7, {offset: 6}))))
1146
+ ) {
1147
+ return {
1148
+ ext: 'vtt',
1149
+ mime: 'text/vtt',
1150
+ };
1140
1151
  }
1141
1152
 
1142
1153
  // -- 8-byte signatures --
@@ -1420,6 +1431,14 @@ export class FileTypeParser {
1420
1431
  };
1421
1432
  }
1422
1433
 
1434
+ // ACE: requires 14 bytes in the buffer
1435
+ if (this.checkString('**ACE', {offset: 7}) && this.checkString('**', {offset: 12})) {
1436
+ return {
1437
+ ext: 'ace',
1438
+ mime: 'application/x-ace-compressed',
1439
+ };
1440
+ }
1441
+
1423
1442
  // -- 15-byte signatures --
1424
1443
 
1425
1444
  if (this.checkString('BEGIN:')) {
package/index.js CHANGED
@@ -9,9 +9,9 @@ import {FileTypeParser, reasonableDetectionSizeInBytes} from './core.js';
9
9
 
10
10
  export class NodeFileTypeParser extends FileTypeParser {
11
11
  async fromStream(stream) {
12
- const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream) : strtok3.fromStream(stream));
12
+ const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.tokenizerOptions) : strtok3.fromStream(stream, this.tokenizerOptions));
13
13
  try {
14
- return super.fromTokenizer(tokenizer);
14
+ return await super.fromTokenizer(tokenizer);
15
15
  } finally {
16
16
  await tokenizer.close();
17
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "19.4.1",
3
+ "version": "19.6.0",
4
4
  "description": "Detect the file type of a file, stream, or data",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/file-type",
@@ -215,11 +215,13 @@
215
215
  "avro",
216
216
  "icc",
217
217
  "fbx",
218
- "vsdx"
218
+ "vsdx",
219
+ "vtt",
220
+ "apk"
219
221
  ],
220
222
  "dependencies": {
221
223
  "get-stream": "^9.0.1",
222
- "strtok3": "^8.1.0",
224
+ "strtok3": "^9.0.1",
223
225
  "token-types": "^6.0.0",
224
226
  "uint8array-extras": "^1.3.0"
225
227
  },
package/readme.md CHANGED
@@ -367,6 +367,22 @@ const fileType = await parser.fromBuffer(buffer);
367
367
  console.log(fileType);
368
368
  ```
369
369
 
370
+ ## Abort signal
371
+
372
+ Some async operations can be aborted by passing an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to the `FileTypeParser` constructor.
373
+
374
+ ```js
375
+ import {FileTypeParser} from 'file-type'; // or `NodeFileTypeParser` in Node.js
376
+
377
+ const abortController = new AbortController()
378
+
379
+ const parser = new FileTypeParser({abortSignal: abortController.signal});
380
+
381
+ const promise = parser.fromStream(blob.stream());
382
+
383
+ abortController.abort(); // Abort file-type reading from the Blob stream.
384
+ ```
385
+
370
386
  ## Supported file types
371
387
 
372
388
  - [`3g2`](https://en.wikipedia.org/wiki/3GP_and_3G2#3G2) - Multimedia container format defined by the 3GPP2 for 3G CDMA2000 multimedia services
@@ -382,6 +398,7 @@ console.log(fileType);
382
398
  - [`alias`](https://en.wikipedia.org/wiki/Alias_%28Mac_OS%29) - macOS Alias file
383
399
  - [`amr`](https://en.wikipedia.org/wiki/Adaptive_Multi-Rate_audio_codec) - Adaptive Multi-Rate audio codec
384
400
  - [`ape`](https://en.wikipedia.org/wiki/Monkey%27s_Audio) - Monkey's Audio
401
+ - [`apk`](https://en.wikipedia.org/wiki/Apk_(file_format)) - Android package format
385
402
  - [`apng`](https://en.wikipedia.org/wiki/APNG) - Animated Portable Network Graphics
386
403
  - [`ar`](https://en.wikipedia.org/wiki/Ar_(Unix)) - Archive file
387
404
  - [`arj`](https://en.wikipedia.org/wiki/ARJ) - Archive file
@@ -507,6 +524,7 @@ console.log(fileType);
507
524
  - [`vcf`](https://en.wikipedia.org/wiki/VCard) - vCard
508
525
  - [`voc`](https://wiki.multimedia.cx/index.php/Creative_Voice) - Creative Voice File
509
526
  - [`vsdx`](https://en.wikipedia.org/wiki/Microsoft_Visio) - Microsoft Visio File
527
+ - [`vtt`](https://en.wikipedia.org/wiki/WebVTT) - WebVTT File (for video captions)
510
528
  - [`wasm`](https://en.wikipedia.org/wiki/WebAssembly) - WebAssembly intermediate compiled format
511
529
  - [`wav`](https://en.wikipedia.org/wiki/WAV) - Waveform Audio file
512
530
  - [`webm`](https://en.wikipedia.org/wiki/WebM) - Web video file
package/supported.js CHANGED
@@ -152,6 +152,8 @@ export const extensions = [
152
152
  'icc',
153
153
  'fbx',
154
154
  'vsdx',
155
+ 'vtt',
156
+ 'apk',
155
157
  ];
156
158
 
157
159
  export const mimeTypes = [
@@ -199,9 +201,9 @@ export const mimeTypes = [
199
201
  'video/3gpp',
200
202
  'audio/mpeg',
201
203
  'audio/mp4', // RFC 4337
202
- 'audio/opus',
203
204
  'video/ogg',
204
205
  'audio/ogg',
206
+ 'audio/ogg; codecs=opus',
205
207
  'application/ogg',
206
208
  'audio/x-flac',
207
209
  'audio/ape',
@@ -257,6 +259,7 @@ export const mimeTypes = [
257
259
  'audio/x-musepack',
258
260
  'text/calendar',
259
261
  'text/vcard',
262
+ 'text/vtt',
260
263
  'model/gltf-binary',
261
264
  'application/vnd.tcpdump.pcap',
262
265
  'audio/x-dsf', // Non-standard
@@ -303,4 +306,5 @@ export const mimeTypes = [
303
306
  'application/vnd.iccprofile',
304
307
  'application/x.autodesk.fbx', // Invented by us
305
308
  'application/vnd.visio',
309
+ 'application/vnd.android.package-archive',
306
310
  ];