diff --git a/index.d.ts b/index.d.ts index 06405516..abdfc628 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,7 +3,7 @@ Typings for Node.js specific entry point. */ import type {Readable as NodeReadableStream} from 'node:stream'; -import type {FileTypeResult, StreamOptions, AnyWebReadableStream} from './core.js'; +import type {FileTypeResult, StreamOptions, AnyWebReadableStream, Detector} from './core.js'; import {FileTypeParser} from './core.js'; export type ReadableStreamWithFileType = NodeReadableStream & { @@ -16,6 +16,8 @@ export declare class NodeFileTypeParser extends FileTypeParser { */ fromStream(stream: AnyWebReadableStream | NodeReadableStream): Promise; + fromFile(filePath: string): Promise; + /** Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor). */ @@ -25,12 +27,11 @@ export declare class NodeFileTypeParser extends FileTypeParser { /** Detect the file type of a file path. -The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the buffer. +The file type is detected by checking the [magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) of the file. -@param path @returns The detected file type and MIME type or `undefined` when there is no match. */ -export function fileTypeFromFile(path: string): Promise; +export function fileTypeFromFile(filePath: string, options?: {customDetectors?: Iterable}): Promise; export function fileTypeFromStream(stream: AnyWebReadableStream | NodeReadableStream): Promise; diff --git a/index.js b/index.js index 0ca2ca06..ecebcaad 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,15 @@ export class NodeFileTypeParser extends FileTypeParser { } } + async fromFile(path) { + const tokenizer = await strtok3.fromFile(path); + try { + return await super.fromTokenizer(tokenizer); + } finally { + await tokenizer.close(); + } + } + async toDetectionStream(readableStream, options = {}) { const {default: stream} = await import('node:stream'); const {sampleSize = reasonableDetectionSizeInBytes} = options; @@ -53,13 +62,7 @@ export class NodeFileTypeParser extends FileTypeParser { } export async function fileTypeFromFile(path, fileTypeOptions) { - const tokenizer = await strtok3.fromFile(path); - try { - const parser = new FileTypeParser(fileTypeOptions); - return await parser.fromTokenizer(tokenizer); - } finally { - await tokenizer.close(); - } + return (new NodeFileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions); } export async function fileTypeFromStream(stream, fileTypeOptions) {