file-type 17.1.6 → 18.0.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
@@ -387,7 +387,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
387
387
  const stream1 = got.stream(url);
388
388
  const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
389
389
 
390
- if (stream2.fileType && stream2.fileType.mime === 'image/jpeg') {
390
+ if (stream2.fileType?.mime === 'image/jpeg') {
391
391
  // stream2 can be used to stream the JPEG image (from the very beginning of the stream)
392
392
  }
393
393
  ```
package/core.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {Buffer} from 'node:buffer';
2
2
  import * as Token from 'token-types';
3
- import * as strtok3 from 'strtok3/core';
3
+ import * as strtok3 from 'strtok3/core'; // eslint-disable-line n/file-extension-in-import
4
4
  import {
5
5
  stringToBytes,
6
6
  tarHeaderChecksumMatches,
@@ -26,7 +26,7 @@ export async function fileTypeFromBuffer(input) {
26
26
 
27
27
  const buffer = input instanceof Uint8Array ? input : new Uint8Array(input);
28
28
 
29
- if (!(buffer && buffer.length > 1)) {
29
+ if (!(buffer?.length > 1)) {
30
30
  return;
31
31
  }
32
32
 
@@ -337,7 +337,8 @@ class FileTypeParser {
337
337
  // - one entry indicating specific type of file.
338
338
  // MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
339
339
  if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
340
- const mimeType = (await tokenizer.readToken(new Token.StringType(zipHeader.compressedSize, 'utf-8'))).trim();
340
+ let mimeType = await tokenizer.readToken(new Token.StringType(zipHeader.compressedSize, 'utf-8'));
341
+ mimeType = mimeType.trim();
341
342
 
342
343
  switch (mimeType) {
343
344
  case 'application/epub+zip':
@@ -1499,7 +1500,6 @@ class FileTypeParser {
1499
1500
  }
1500
1501
 
1501
1502
  export async function fileTypeStream(readableStream, {sampleSize = minimumBytes} = {}) {
1502
- // eslint-disable-next-line node/no-unsupported-features/es-syntax
1503
1503
  const {default: stream} = await import('node:stream');
1504
1504
 
1505
1505
  return new Promise((resolve, reject) => {
@@ -1513,7 +1513,7 @@ export async function fileTypeStream(readableStream, {sampleSize = minimumBytes}
1513
1513
  const outputStream = stream.pipeline ? stream.pipeline(readableStream, pass, () => {}) : readableStream.pipe(pass);
1514
1514
 
1515
1515
  // Read the input stream and detect the filetype
1516
- const chunk = readableStream.read(sampleSize) || readableStream.read() || Buffer.alloc(0);
1516
+ const chunk = readableStream.read(sampleSize) ?? readableStream.read() ?? Buffer.alloc(0);
1517
1517
  try {
1518
1518
  const fileType = await fileTypeFromBuffer(chunk);
1519
1519
  pass.fileType = fileType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "file-type",
3
- "version": "17.1.6",
3
+ "version": "18.0.0",
4
4
  "description": "Detect the file type of a Buffer/Uint8Array/ArrayBuffer",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/file-type",
@@ -19,7 +19,7 @@
19
19
  "./core": "./core.js"
20
20
  },
21
21
  "engines": {
22
- "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
22
+ "node": ">=14.16"
23
23
  },
24
24
  "scripts": {
25
25
  "test": "xo && ava && tsd"
@@ -197,18 +197,17 @@
197
197
  ],
198
198
  "dependencies": {
199
199
  "readable-web-to-node-stream": "^3.0.2",
200
- "strtok3": "^7.0.0-alpha.9",
201
- "token-types": "^5.0.0-alpha.2"
200
+ "strtok3": "^7.0.0",
201
+ "token-types": "^5.0.1"
202
202
  },
203
203
  "devDependencies": {
204
204
  "@tokenizer/token": "^0.3.0",
205
- "@types/node": "^16.11.10",
206
- "ava": "^3.15.0",
205
+ "@types/node": "^18.7.13",
206
+ "ava": "^4.3.1",
207
207
  "commonmark": "^0.30.0",
208
208
  "noop-stream": "^1.0.0",
209
- "tsd": "^0.19.0",
210
- "typescript": "^4.5.2",
211
- "xo": "^0.46.4"
209
+ "tsd": "^0.22.0",
210
+ "xo": "^0.51.0"
212
211
  },
213
212
  "xo": {
214
213
  "envs": [
@@ -219,11 +218,11 @@
219
218
  "no-inner-declarations": "warn",
220
219
  "no-await-in-loop": "warn",
221
220
  "no-bitwise": "off",
222
- "@typescript-eslint/no-unsafe-assignment": "off"
221
+ "@typescript-eslint/no-unsafe-assignment": "off",
222
+ "unicorn/text-encoding-identifier-case": "off"
223
223
  }
224
224
  },
225
225
  "ava": {
226
- "serial": true,
227
- "verbose": true
226
+ "serial": true
228
227
  }
229
228
  }
package/readme.md CHANGED
@@ -318,7 +318,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
318
318
  const stream1 = got.stream(url);
319
319
  const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
320
320
 
321
- if (stream2.fileType && stream2.fileType.mime === 'image/jpeg') {
321
+ if (stream2.fileType?.mime === 'image/jpeg') {
322
322
  // stream2 can be used to stream the JPEG image (from the very beginning of the stream)
323
323
  }
324
324
  ```
package/util.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export function stringToBytes(string) {
2
- return [...string].map(character => character.charCodeAt(0));
2
+ return [...string].map(character => character.charCodeAt(0)); // eslint-disable-line unicorn/prefer-code-point
3
3
  }
4
4
 
5
5
  /**
@@ -17,12 +17,12 @@ export function tarHeaderChecksumMatches(buffer, offset = 0) {
17
17
 
18
18
  let sum = 8 * 0x20; // Initialize signed bit sum
19
19
 
20
- for (let i = offset; i < offset + 148; i++) {
21
- sum += buffer[i];
20
+ for (let index = offset; index < offset + 148; index++) {
21
+ sum += buffer[index];
22
22
  }
23
23
 
24
- for (let i = offset + 156; i < offset + 512; i++) {
25
- sum += buffer[i];
24
+ for (let index = offset + 156; index < offset + 512; index++) {
25
+ sum += buffer[index];
26
26
  }
27
27
 
28
28
  return readSum === sum;