Skip to content

Commit 9d2ee02

Browse files
Add NodeFileTypeParser#fromFile() (#644)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent b02dd24 commit 9d2ee02

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

index.d.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Typings for Node.js specific entry point.
33
*/
44

55
import type {Readable as NodeReadableStream} from 'node:stream';
6-
import type {FileTypeResult, StreamOptions, AnyWebReadableStream} from './core.js';
6+
import type {FileTypeResult, StreamOptions, AnyWebReadableStream, Detector} from './core.js';
77
import {FileTypeParser} from './core.js';
88

99
export type ReadableStreamWithFileType = NodeReadableStream & {
@@ -16,6 +16,8 @@ export declare class NodeFileTypeParser extends FileTypeParser {
1616
*/
1717
fromStream(stream: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>;
1818

19+
fromFile(filePath: string): Promise<FileTypeResult | undefined>;
20+
1921
/**
2022
Works the same way as {@link fileTypeStream}, additionally taking into account custom detectors (if any were provided to the constructor).
2123
*/
@@ -25,12 +27,11 @@ export declare class NodeFileTypeParser extends FileTypeParser {
2527
/**
2628
Detect the file type of a file path.
2729
28-
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.
30+
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.
2931
30-
@param path
3132
@returns The detected file type and MIME type or `undefined` when there is no match.
3233
*/
33-
export function fileTypeFromFile(path: string): Promise<FileTypeResult | undefined>;
34+
export function fileTypeFromFile(filePath: string, options?: {customDetectors?: Iterable<Detector>}): Promise<FileTypeResult | undefined>;
3435

3536
export function fileTypeFromStream(stream: AnyWebReadableStream<Uint8Array> | NodeReadableStream): Promise<FileTypeResult | undefined>;
3637

index.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ export class NodeFileTypeParser extends FileTypeParser {
1616
}
1717
}
1818

19+
async fromFile(path) {
20+
const tokenizer = await strtok3.fromFile(path);
21+
try {
22+
return await super.fromTokenizer(tokenizer);
23+
} finally {
24+
await tokenizer.close();
25+
}
26+
}
27+
1928
async toDetectionStream(readableStream, options = {}) {
2029
const {default: stream} = await import('node:stream');
2130
const {sampleSize = reasonableDetectionSizeInBytes} = options;
@@ -53,13 +62,7 @@ export class NodeFileTypeParser extends FileTypeParser {
5362
}
5463

5564
export async function fileTypeFromFile(path, fileTypeOptions) {
56-
const tokenizer = await strtok3.fromFile(path);
57-
try {
58-
const parser = new FileTypeParser(fileTypeOptions);
59-
return await parser.fromTokenizer(tokenizer);
60-
} finally {
61-
await tokenizer.close();
62-
}
65+
return (new NodeFileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions);
6366
}
6467

6568
export async function fileTypeFromStream(stream, fileTypeOptions) {

0 commit comments

Comments
 (0)