file-type 19.5.0 → 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
@@ -166,6 +166,7 @@ export type FileExtension =
166
166
  | 'fbx'
167
167
  | 'vsdx'
168
168
  | 'vtt'
169
+ | 'apk'
169
170
  ; // eslint-disable-line semi-style
170
171
 
171
172
  export type MimeType =
@@ -213,9 +214,9 @@ export type MimeType =
213
214
  | 'video/3gpp'
214
215
  | 'audio/mpeg'
215
216
  | 'audio/mp4' // RFC 4337
216
- | 'audio/opus'
217
217
  | 'video/ogg'
218
218
  | 'audio/ogg'
219
+ | 'audio/ogg; codecs=opus'
219
220
  | 'application/ogg'
220
221
  | 'audio/x-flac'
221
222
  | 'audio/ape'
@@ -318,6 +319,7 @@ export type MimeType =
318
319
  | 'application/vnd.iccprofile'
319
320
  | 'application/x.autodesk.fbx'
320
321
  | 'application/vnd.visio'
322
+ | 'application/vnd.android.package-archive'
321
323
  ; // eslint-disable-line semi-style
322
324
 
323
325
  export type FileTypeResult = {
@@ -491,7 +493,7 @@ export function fileTypeStream(webStream: AnyWebReadableStream<Uint8Array>, opti
491
493
  export declare class FileTypeParser {
492
494
  detectors: Iterable<Detector>;
493
495
 
494
- constructor(options?: {customDetectors?: Iterable<Detector>});
496
+ constructor(options?: {customDetectors?: Iterable<Detector>; signal: AbortSignal});
495
497
 
496
498
  /**
497
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,16 +1138,6 @@ 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
- }
1140
- }
1141
-
1142
1141
  if (
1143
1142
  this.checkString('WEBVTT')
1144
1143
  && (
@@ -1432,6 +1431,14 @@ export class FileTypeParser {
1432
1431
  };
1433
1432
  }
1434
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
+
1435
1442
  // -- 15-byte signatures --
1436
1443
 
1437
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.5.0",
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",
@@ -216,11 +216,12 @@
216
216
  "icc",
217
217
  "fbx",
218
218
  "vsdx",
219
- "vtt"
219
+ "vtt",
220
+ "apk"
220
221
  ],
221
222
  "dependencies": {
222
223
  "get-stream": "^9.0.1",
223
- "strtok3": "^8.1.0",
224
+ "strtok3": "^9.0.1",
224
225
  "token-types": "^6.0.0",
225
226
  "uint8array-extras": "^1.3.0"
226
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
package/supported.js CHANGED
@@ -153,6 +153,7 @@ export const extensions = [
153
153
  'fbx',
154
154
  'vsdx',
155
155
  'vtt',
156
+ 'apk',
156
157
  ];
157
158
 
158
159
  export const mimeTypes = [
@@ -200,9 +201,9 @@ export const mimeTypes = [
200
201
  'video/3gpp',
201
202
  'audio/mpeg',
202
203
  'audio/mp4', // RFC 4337
203
- 'audio/opus',
204
204
  'video/ogg',
205
205
  'audio/ogg',
206
+ 'audio/ogg; codecs=opus',
206
207
  'application/ogg',
207
208
  'audio/x-flac',
208
209
  'audio/ape',
@@ -305,4 +306,5 @@ export const mimeTypes = [
305
306
  'application/vnd.iccprofile',
306
307
  'application/x.autodesk.fbx', // Invented by us
307
308
  'application/vnd.visio',
309
+ 'application/vnd.android.package-archive',
308
310
  ];