Skip to content

Commit

Permalink
Require Node.js 14
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 24, 2022
1 parent 8dff74d commit 6d457c5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 39 deletions.
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Expand Up @@ -13,7 +13,6 @@ jobs:
- 18
- 16
- 14
- 12
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion core.d.ts
Expand Up @@ -387,7 +387,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
const stream1 = got.stream(url);
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});
if (stream2.fileType && stream2.fileType.mime === 'image/jpeg') {
if (stream2.fileType?.mime === 'image/jpeg') {
// stream2 can be used to stream the JPEG image (from the very beginning of the stream)
}
```
Expand Down
10 changes: 5 additions & 5 deletions core.js
@@ -1,6 +1,6 @@
import {Buffer} from 'node:buffer';
import * as Token from 'token-types';
import * as strtok3 from 'strtok3/core';
import * as strtok3 from 'strtok3/core'; // eslint-disable-line n/file-extension-in-import
import {
stringToBytes,
tarHeaderChecksumMatches,
Expand All @@ -26,7 +26,7 @@ export async function fileTypeFromBuffer(input) {

const buffer = input instanceof Uint8Array ? input : new Uint8Array(input);

if (!(buffer && buffer.length > 1)) {
if (!(buffer?.length > 1)) {
return;
}

Expand Down Expand Up @@ -337,7 +337,8 @@ class FileTypeParser {
// - one entry indicating specific type of file.
// MS Office, OpenOffice and LibreOffice may put the parts in different order, so the check should not rely on it.
if (zipHeader.filename === 'mimetype' && zipHeader.compressedSize === zipHeader.uncompressedSize) {
const mimeType = (await tokenizer.readToken(new Token.StringType(zipHeader.compressedSize, 'utf-8'))).trim();
let mimeType = await tokenizer.readToken(new Token.StringType(zipHeader.compressedSize, 'utf-8'));
mimeType = mimeType.trim();

switch (mimeType) {
case 'application/epub+zip':
Expand Down Expand Up @@ -1499,7 +1500,6 @@ class FileTypeParser {
}

export async function fileTypeStream(readableStream, {sampleSize = minimumBytes} = {}) {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
const {default: stream} = await import('node:stream');

return new Promise((resolve, reject) => {
Expand All @@ -1513,7 +1513,7 @@ export async function fileTypeStream(readableStream, {sampleSize = minimumBytes}
const outputStream = stream.pipeline ? stream.pipeline(readableStream, pass, () => {}) : readableStream.pipe(pass);

// Read the input stream and detect the filetype
const chunk = readableStream.read(sampleSize) || readableStream.read() || Buffer.alloc(0);
const chunk = readableStream.read(sampleSize) ?? readableStream.read() ?? Buffer.alloc(0);
try {
const fileType = await fileTypeFromBuffer(chunk);
pass.fileType = fileType;
Expand Down
3 changes: 2 additions & 1 deletion index.test-d.ts
Expand Up @@ -60,7 +60,8 @@ const readableStream = fs.createReadStream('file.png');
const streamWithFileType = fileTypeStream(readableStream);
expectType<Promise<ReadableStreamWithFileType>>(streamWithFileType);
(async () => {
expectType<FileTypeResult | undefined>((await streamWithFileType).fileType);
const {fileType} = await streamWithFileType;
expectType<FileTypeResult | undefined>(fileType);
})();

// Browser
Expand Down
19 changes: 9 additions & 10 deletions package.json
Expand Up @@ -19,7 +19,7 @@
"./core": "./core.js"
},
"engines": {
"node": ">=14.16.0"
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -198,17 +198,16 @@
"dependencies": {
"readable-web-to-node-stream": "^3.0.2",
"strtok3": "^7.0.0",
"token-types": "^5.0.0"
"token-types": "^5.0.1"
},
"devDependencies": {
"@tokenizer/token": "^0.3.0",
"@types/node": "^16.11.10",
"ava": "^3.15.0",
"@types/node": "^18.7.13",
"ava": "^4.3.1",
"commonmark": "^0.30.0",
"noop-stream": "^1.0.0",
"tsd": "^0.19.0",
"typescript": "^4.5.2",
"xo": "^0.46.4"
"tsd": "^0.22.0",
"xo": "^0.51.0"
},
"xo": {
"envs": [
Expand All @@ -219,11 +218,11 @@
"no-inner-declarations": "warn",
"no-await-in-loop": "warn",
"no-bitwise": "off",
"@typescript-eslint/no-unsafe-assignment": "off"
"@typescript-eslint/no-unsafe-assignment": "off",
"unicorn/text-encoding-identifier-case": "off"
}
},
"ava": {
"serial": true,
"verbose": true
"serial": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -318,7 +318,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
const stream1 = got.stream(url);
const stream2 = await fileTypeStream(stream1, {sampleSize: 1024});

if (stream2.fileType && stream2.fileType.mime === 'image/jpeg') {
if (stream2.fileType?.mime === 'image/jpeg') {
// stream2 can be used to stream the JPEG image (from the very beginning of the stream)
}
```
Expand Down
12 changes: 6 additions & 6 deletions test.js
Expand Up @@ -239,24 +239,24 @@ const failingFixture = new Set([
]);

async function checkBufferLike(t, type, bufferLike) {
const {ext, mime} = await fileTypeFromBuffer(bufferLike) || {};
const {ext, mime} = await fileTypeFromBuffer(bufferLike) ?? {};
t.is(ext, type);
t.is(typeof mime, 'string');
}

async function checkFile(t, type, filePath) {
const {ext, mime} = await fileTypeFromFile(filePath) || {};
const {ext, mime} = await fileTypeFromFile(filePath) ?? {};
t.is(ext, type);
t.is(typeof mime, 'string');
}

async function testFromFile(t, ext, name) {
const file = path.join(__dirname, 'fixture', `${(name || 'fixture')}.${ext}`);
const file = path.join(__dirname, 'fixture', `${(name ?? 'fixture')}.${ext}`);
return checkFile(t, ext, file);
}

async function testFromBuffer(t, ext, name) {
const fixtureName = `${(name || 'fixture')}.${ext}`;
const fixtureName = `${(name ?? 'fixture')}.${ext}`;

const file = path.join(__dirname, 'fixture', fixtureName);
const chunk = fs.readFileSync(file);
Expand All @@ -277,7 +277,7 @@ async function testFalsePositive(t, ext, name) {
}

async function testFileFromStream(t, ext, name) {
const filename = `${(name || 'fixture')}.${ext}`;
const filename = `${(name ?? 'fixture')}.${ext}`;
const file = path.join(__dirname, 'fixture', filename);
const fileType = await fileTypeFromStream(fs.createReadStream(file));

Expand All @@ -296,7 +296,7 @@ async function loadEntireFile(readable) {
}

async function testStream(t, ext, name) {
const fixtureName = `${(name || 'fixture')}.${ext}`;
const fixtureName = `${(name ?? 'fixture')}.${ext}`;
const file = path.join(__dirname, 'fixture', fixtureName);

const readableStream = await fileTypeStream(fs.createReadStream(file));
Expand Down
16 changes: 7 additions & 9 deletions type.js
Expand Up @@ -9,13 +9,11 @@ if (!file) {
process.exit();
}

(async () => {
const fileType = await fileTypeFromFile(file);
const fileType = await fileTypeFromFile(file);

if (fileType) {
console.log(`MIME-type: ${fileType.mime}`);
console.log(`Extension: ${fileType.ext}`);
} else {
console.log('Could not determine file type');
}
})();
if (fileType) {
console.log(`MIME-type: ${fileType.mime}`);
console.log(`Extension: ${fileType.ext}`);
} else {
console.log('Could not determine file type');
}
10 changes: 5 additions & 5 deletions util.js
@@ -1,5 +1,5 @@
export function stringToBytes(string) {
return [...string].map(character => character.charCodeAt(0));
return [...string].map(character => character.charCodeAt(0)); // eslint-disable-line unicorn/prefer-code-point
}

/**
Expand All @@ -17,12 +17,12 @@ export function tarHeaderChecksumMatches(buffer, offset = 0) {

let sum = 8 * 0x20; // Initialize signed bit sum

for (let i = offset; i < offset + 148; i++) {
sum += buffer[i];
for (let index = offset; index < offset + 148; index++) {
sum += buffer[index];
}

for (let i = offset + 156; i < offset + 512; i++) {
sum += buffer[i];
for (let index = offset + 156; index < offset + 512; index++) {
sum += buffer[index];
}

return readSum === sum;
Expand Down

0 comments on commit 6d457c5

Please sign in to comment.