diff --git a/core.js b/core.js index a50f1e48..e349edea 100644 --- a/core.js +++ b/core.js @@ -535,16 +535,16 @@ export class FileTypeParser { // Use TextDecoder to decode the UTF-8 encoded data let xmlContent = new TextDecoder('utf-8').decode(fileData); const endPos = xmlContent.indexOf('.main+xml"'); - if (endPos >= 0) { - xmlContent = xmlContent.slice(0, Math.max(0, endPos)); - const firstPos = xmlContent.lastIndexOf('"'); - const mimeType = xmlContent.slice(Math.max(0, firstPos + 1)); - fileType = getFileTypeFromMimeType(mimeType); - } else { + if (endPos === -1) { const mimeType = 'application/vnd.ms-package.3dmanufacturing-3dmodel+xml'; if (xmlContent.includes(`ContentType="${mimeType}"`)) { fileType = getFileTypeFromMimeType(mimeType); } + } else { + xmlContent = xmlContent.slice(0, Math.max(0, endPos)); + const firstPos = xmlContent.lastIndexOf('"'); + const mimeType = xmlContent.slice(Math.max(0, firstPos + 1)); + fileType = getFileTypeFromMimeType(mimeType); } }, stop: true, @@ -821,9 +821,9 @@ export class FileTypeParser { } const re = await readElement(); - const docType = await readChildren(re.len); + const documentType = await readChildren(re.len); - switch (docType) { + switch (documentType) { case 'webm': return { ext: 'webm', @@ -1640,7 +1640,6 @@ export class FileTypeParser { }; } }; - // Detections with limited supporting data, resulting in a higher likelihood of false positives detectImprecise = async tokenizer => { this.buffer = new Uint8Array(reasonableDetectionSizeInBytes); diff --git a/index.d.ts b/index.d.ts index b5f8bc0e..c2757b3b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,8 +4,14 @@ Typings for Node.js specific entry point. import type {Readable as NodeReadableStream} from 'node:stream'; import type {AnyWebByteStream} from 'strtok3'; -import type {FileTypeResult, StreamOptions, AnyWebReadableStream, Detector, AnyWebReadableByteStreamWithFileType} from './core.js'; -import {FileTypeParser as DefaultFileTypeParser} from './core.js'; +import { + type FileTypeResult, + type StreamOptions, + type AnyWebReadableStream, + type Detector, + type AnyWebReadableByteStreamWithFileType, + FileTypeParser as DefaultFileTypeParser, +} from './core.js'; export type ReadableStreamWithFileType = NodeReadableStream & { readonly fileType?: FileTypeResult; diff --git a/index.js b/index.js index c4180ff2..07c4bc2e 100644 --- a/index.js +++ b/index.js @@ -77,4 +77,10 @@ export async function fileTypeStream(readableStream, options = {}) { return new FileTypeParser(options).toDetectionStream(readableStream, options); } -export {fileTypeFromTokenizer, fileTypeFromBuffer, fileTypeFromBlob, supportedMimeTypes, supportedExtensions} from './core.js'; +export { + fileTypeFromTokenizer, + fileTypeFromBuffer, + fileTypeFromBlob, + supportedMimeTypes, + supportedExtensions, +} from './core.js'; diff --git a/package.json b/package.json index bf61eab7..59101399 100644 --- a/package.json +++ b/package.json @@ -239,17 +239,17 @@ "@tokenizer/inflate": "^0.2.6", "strtok3": "^10.0.1", "token-types": "^6.0.0", - "uint8array-extras": "^1.3.0" + "uint8array-extras": "^1.4.0" }, "devDependencies": { "@tokenizer/token": "^0.3.0", - "@types/node": "^20.10.7", + "@types/node": "^22.10.5", "ava": "^6.0.1", - "commonmark": "^0.30.0", + "commonmark": "^0.31.2", "get-stream": "^9.0.1", "noop-stream": "^1.0.0", - "tsd": "^0.30.3", - "xo": "^0.56.0" + "tsd": "^0.31.2", + "xo": "^0.60.0" }, "xo": { "envs": [ diff --git a/test.js b/test.js index edd3c7aa..4eaab5dc 100644 --- a/test.js +++ b/test.js @@ -30,7 +30,7 @@ const missingTests = new Set([ const [nodeMajorVersion] = process.versions.node.split('.').map(Number); const nodeVersionSupportingByteBlobStream = 20; -const types = [...supportedExtensions].filter(ext => !missingTests.has(ext)); +const types = [...supportedExtensions].filter(extension => !missingTests.has(extension)); // Define an entry here only if the fixture has a different // name than `fixture` or if you want multiple fixtures @@ -307,31 +307,31 @@ async function checkFile(t, type, filePath) { t.is(typeof mime, 'string'); } -async function testFromFile(t, ext, name) { - const file = path.join(__dirname, 'fixture', `${(name ?? 'fixture')}.${ext}`); - return checkFile(t, ext, file); +async function testFromFile(t, extension, name) { + const file = path.join(__dirname, 'fixture', `${(name ?? 'fixture')}.${extension}`); + return checkFile(t, extension, file); } -async function testFromBuffer(t, ext, name) { - const fixtureName = `${(name ?? 'fixture')}.${ext}`; +async function testFromBuffer(t, extension, name) { + const fixtureName = `${(name ?? 'fixture')}.${extension}`; const file = path.join(__dirname, 'fixture', fixtureName); const chunk = fs.readFileSync(file); - await checkBufferLike(t, ext, chunk); - await checkBufferLike(t, ext, new Uint8Array(chunk)); - await checkBufferLike(t, ext, chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength)); + await checkBufferLike(t, extension, chunk); + await checkBufferLike(t, extension, new Uint8Array(chunk)); + await checkBufferLike(t, extension, chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength)); } -async function testFromBlob(t, ext, name) { - const fixtureName = `${(name ?? 'fixture')}.${ext}`; +async function testFromBlob(t, extension, name) { + const fixtureName = `${(name ?? 'fixture')}.${extension}`; const file = path.join(__dirname, 'fixture', fixtureName); const chunk = fs.readFileSync(file); - await checkBlobLike(t, ext, chunk); + await checkBlobLike(t, extension, chunk); } -async function testFalsePositive(t, ext, name) { - const file = path.join(__dirname, 'fixture', `${name}.${ext}`); +async function testFalsePositive(t, extension, name) { + const file = path.join(__dirname, 'fixture', `${name}.${extension}`); await t.is(await fileTypeFromFile(file), undefined); @@ -341,13 +341,13 @@ async function testFalsePositive(t, ext, name) { t.is(await fileTypeFromBuffer(chunk.buffer), undefined); } -async function testFileNodeFromStream(t, ext, name) { - const filename = `${(name ?? 'fixture')}.${ext}`; +async function testFileNodeFromStream(t, extension, name) { + const filename = `${(name ?? 'fixture')}.${extension}`; const file = path.join(__dirname, 'fixture', filename); const fileType = await fileTypeNodeFromStream(fs.createReadStream(file)); t.truthy(fileType, `identify ${filename}`); - t.is(fileType.ext, ext, 'fileType.ext'); + t.is(fileType.ext, extension, 'fileType.ext'); t.is(typeof fileType.mime, 'string', 'fileType.mime'); } @@ -355,8 +355,8 @@ async function getStreamAsUint8Array(stream) { return new Uint8Array(await getStreamAsArrayBuffer(stream)); } -async function testStreamWithNodeStream(t, ext, name) { - const fixtureName = `${(name ?? 'fixture')}.${ext}`; +async function testStreamWithNodeStream(t, extension, name) { + const fixtureName = `${(name ?? 'fixture')}.${extension}`; const file = path.join(__dirname, 'fixture', fixtureName); const readableStream = await fileTypeStream(fs.createReadStream(file)); @@ -367,8 +367,8 @@ async function testStreamWithNodeStream(t, ext, name) { t.true(areUint8ArraysEqual(bufferA, bufferB)); } -async function testStreamWithWebStream(t, ext, name) { - const fixtureName = `${(name ?? 'fixture')}.${ext}`; +async function testStreamWithWebStream(t, extension, name) { + const fixtureName = `${(name ?? 'fixture')}.${extension}`; const file = path.join(__dirname, 'fixture', fixtureName); // Read the file into a buffer const fileBuffer = await readFile(file); @@ -387,7 +387,7 @@ test('Test suite must be able to detect Node.js major version', t => { let i = 0; for (const type of types) { - if (Object.prototype.hasOwnProperty.call(names, type)) { + if (Object.hasOwn(names, type)) { for (const name of names[type]) { const fixtureName = `${name}.${type}`; const _test = failingFixture.has(fixtureName) ? test.failing : test; @@ -413,7 +413,7 @@ for (const type of types) { test(`${type} ${i++} .fileTypeStream() - identical streams`, testStreamWithNodeStream, type); } - if (Object.prototype.hasOwnProperty.call(falsePositives, type)) { + if (Object.hasOwn(falsePositives, type)) { for (const falsePositiveFile of falsePositives[type]) { test(`false positive - ${type} ${i++}`, testFalsePositive, type, falsePositiveFile); } @@ -518,13 +518,13 @@ test('validate the repo has all extensions and mimes in sync', t => { // File: core.js (base truth) function readIndexJS() { const core = fs.readFileSync('core.js', {encoding: 'utf8'}); - const extArray = core.match(/(?<=ext:\s')(.*)(?=',)/g); + const extensionArray = core.match(/(?<=ext:\s')(.*)(?=',)/g); const mimeArray = core.match(/(?<=mime:\s')(.*)(?=')/g); - const exts = new Set(extArray); + const extensions = new Set(extensionArray); const mimes = new Set(mimeArray); return { - exts, + exts: extensions, mimes, }; } @@ -557,15 +557,15 @@ test('validate the repo has all extensions and mimes in sync', t => { 'webassembly', ]); - const extArray = keywords.filter(keyword => !allowedExtras.has(keyword)); - return extArray; + const extensionArray = keywords.filter(keyword => !allowedExtras.has(keyword)); + return extensionArray; } // File: readme.md function readReadmeMD() { const index = fs.readFileSync('readme.md', {encoding: 'utf8'}); - const extArray = index.match(/(?<=-\s\[`)(.*)(?=`)/g); - return extArray; + const extensionArray = index.match(/(?<=-\s\[`)(.*)(?=`)/g); + return extensionArray; } // Helpers @@ -601,13 +601,13 @@ test('validate the repo has all extensions and mimes in sync', t => { } // Test runner - function validate(found, baseTruth, fileName, extOrMime) { + function validate(found, baseTruth, fileName, extensionOrMime) { const duplicates = findDuplicates(found); const extras = findExtras(found, baseTruth); const missing = findMissing(found, baseTruth); - t.is(duplicates.length, 0, `Found duplicate ${extOrMime}: ${duplicates} in ${fileName}.`); - t.is(extras.length, 0, `Extra ${extOrMime}: ${extras} in ${fileName}.`); - t.is(missing.length, 0, `Missing ${extOrMime}: ${missing} in ${fileName}.`); + t.is(duplicates.length, 0, `Found duplicate ${extensionOrMime}: ${duplicates} in ${fileName}.`); + t.is(extras.length, 0, `Extra ${extensionOrMime}: ${extras} in ${fileName}.`); + t.is(missing.length, 0, `Missing ${extensionOrMime}: ${missing} in ${fileName}.`); } // Get the base truth of extensions and mimes supported from core.js