Skip to content

Commit

Permalink
Update project dependencies to their latest versions
Browse files Browse the repository at this point in the history
Update code style to latest `xo` requirements
  • Loading branch information
Borewit committed Jan 9, 2025
1 parent 7fe5667 commit d9fa223
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 51 deletions.
17 changes: 8 additions & 9 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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;
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ 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';
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
68 changes: 34 additions & 34 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -341,22 +341,22 @@ 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');
}

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));
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d9fa223

Please sign in to comment.