Skip to content

Commit

Permalink
Deprecate NodeFileTypeParser
Browse files Browse the repository at this point in the history
In Node.js class `FileTypeParser` shall contain the Node.js specific functions (`fromFile()`) as well.
  • Loading branch information
Borewit committed Dec 18, 2024
1 parent 399b0f1 commit f526ce6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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} from './core.js';
import {FileTypeParser as DefaultFileTypeParser} from './core.js';

export type ReadableStreamWithFileType = NodeReadableStream & {
readonly fileType?: FileTypeResult;
Expand All @@ -14,7 +14,7 @@ export type ReadableStreamWithFileType = NodeReadableStream & {
/**
Extending `FileTypeParser` with Node.js engine specific functions.
*/
export declare class NodeFileTypeParser extends FileTypeParser {
export declare class FileTypeParser extends DefaultFileTypeParser {
/**
@param stream - Node.js `stream.Readable` or web `ReadableStream`.
*/
Expand All @@ -29,6 +29,12 @@ export declare class NodeFileTypeParser extends FileTypeParser {
toDetectionStream(webStream: AnyWebReadableStream<Uint8Array>, options?: StreamOptions): Promise<AnyWebReadableByteStreamWithFileType>;
}

/**
Backwards compatibility for `class NodeFileTypeParser`.
You can just use `FileTypeParser` in Node.js directly.
*/
export type NodeFileTypeParser = FileTypeParser;

/**
Detect the file type of a file path.
Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Node.js specific entry point.
import {ReadableStream as WebReadableStream} from 'node:stream/web';
import {pipeline, PassThrough, Readable} from 'node:stream';
import * as strtok3 from 'strtok3';
import {FileTypeParser, reasonableDetectionSizeInBytes} from './core.js';
import {FileTypeParser as DefaultFileTypeParser, reasonableDetectionSizeInBytes} from './core.js';

export class NodeFileTypeParser extends FileTypeParser {
export class FileTypeParser extends DefaultFileTypeParser {
async fromStream(stream) {
const tokenizer = await (stream instanceof WebReadableStream ? strtok3.fromWebStream(stream, this.tokenizerOptions) : strtok3.fromStream(stream, this.tokenizerOptions));
try {
Expand Down Expand Up @@ -66,15 +66,20 @@ export class NodeFileTypeParser extends FileTypeParser {
}

export async function fileTypeFromFile(path, fileTypeOptions) {
return (new NodeFileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions);
return (new FileTypeParser(fileTypeOptions)).fromFile(path, fileTypeOptions);
}

export async function fileTypeFromStream(stream, fileTypeOptions) {
return (new NodeFileTypeParser(fileTypeOptions)).fromStream(stream);
return (new FileTypeParser(fileTypeOptions)).fromStream(stream);
}

export async function fileTypeStream(readableStream, options = {}) {
return new NodeFileTypeParser(options).toDetectionStream(readableStream, options);
return new FileTypeParser(options).toDetectionStream(readableStream, options);
}

export {fileTypeFromTokenizer, fileTypeFromBuffer, fileTypeFromBlob, FileTypeParser, supportedMimeTypes, supportedExtensions} from './core.js';
export {fileTypeFromTokenizer, fileTypeFromBuffer, fileTypeFromBlob, supportedMimeTypes, supportedExtensions} from './core.js';

/**
* Backwards compatibility for `class NodeFileTypeParser`.
*/
export const NodeFileTypeParser = FileTypeParser;
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ Returns a `Set<string>` of supported MIME types.
A custom detector is a function that allows specifying custom detection mechanisms.
An iterable of detectors can be provided via the `fileTypeOptions` argument for the `FileTypeParser` constructor.
In Node.js, you should use `NodeFileTypeParser`, which extends `FileTypeParser` and provides access to Node.js specific functions.
The detectors are called before the default detections in the provided order.
Expand All @@ -359,7 +358,7 @@ If the detector returns `undefined`, there are 2 possible scenarios:
Example detector array which can be extended and provided to each public method via the `fileTypeOptions` argument:
```js
import {FileTypeParser} from 'file-type'; // or `NodeFileTypeParser` in Node.js
import {FileTypeParser} from 'file-type';

const customDetectors = [
async tokenizer => {
Expand All @@ -377,7 +376,7 @@ const customDetectors = [
];

const buffer = new Uint8Array(new TextEncoder().encode('UNICORN'));
const parser = new FileTypeParser({customDetectors}); // `NodeFileTypeParser({customDetectors})` in Node.js
const parser = new FileTypeParser({customDetectors});
const fileType = await parser.fromBuffer(buffer);
console.log(fileType);
```
Expand All @@ -387,7 +386,7 @@ console.log(fileType);
Some async operations can be aborted by passing an [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to the `FileTypeParser` constructor.
```js
import {FileTypeParser} from 'file-type'; // or `NodeFileTypeParser` in Node.js
import {FileTypeParser} from 'file-type';

const abortController = new AbortController()

Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
fileTypeStream,
supportedExtensions,
supportedMimeTypes,
NodeFileTypeParser,
FileTypeParser,
} from './index.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -487,7 +487,7 @@ test('.fileTypeFromStream() method - be able to abort operation', async t => {

const shortStream = new MyStream();
const abortController = new AbortController();
const parser = new NodeFileTypeParser({signal: abortController.signal});
const parser = new FileTypeParser({signal: abortController.signal});
const promiseFileType = parser.fromStream(shortStream);
abortController.abort(); // Abort asynchronous operation: reading from shortStream
const error = await t.throwsAsync(promiseFileType);
Expand Down Expand Up @@ -715,7 +715,7 @@ if (nodeMajorVersion >= nodeVersionSupportingByteBlobStream) {
const blob = new Blob([header]);

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBlob(blob);
t.deepEqual(result, {ext: 'unicorn', mime: 'application/unicorn'});
Expand All @@ -727,7 +727,7 @@ if (nodeMajorVersion >= nodeVersionSupportingByteBlobStream) {
const blob = new Blob([chunk]);

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBlob(blob);
t.deepEqual(result, {ext: 'png', mime: 'image/png'});
Expand All @@ -739,7 +739,7 @@ if (nodeMajorVersion >= nodeVersionSupportingByteBlobStream) {
const blob = new Blob([chunk]);

const customDetectors = [mockPngDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBlob(blob);
t.deepEqual(result, {ext: 'mockPng', mime: 'image/mockPng'});
Expand All @@ -751,7 +751,7 @@ test('fileTypeFromBuffer should detect custom file type "unicorn" using custom d
const uint8ArrayContent = new TextEncoder().encode(header);

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBuffer(uint8ArrayContent);
t.deepEqual(result, {ext: 'unicorn', mime: 'application/unicorn'});
Expand All @@ -762,7 +762,7 @@ test('fileTypeFromBuffer should keep detecting default file types when no custom
const uint8ArrayContent = fs.readFileSync(file);

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBuffer(uint8ArrayContent);
t.deepEqual(result, {ext: 'png', mime: 'image/png'});
Expand All @@ -773,7 +773,7 @@ test('fileTypeFromBuffer should allow overriding default file type detectors', a
const uint8ArrayContent = fs.readFileSync(file);

const customDetectors = [mockPngDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromBuffer(uint8ArrayContent);
t.deepEqual(result, {ext: 'mockPng', mime: 'image/mockPng'});
Expand All @@ -788,7 +788,7 @@ test('fileTypeFromStream should detect custom file type "unicorn" using custom d
const readableStream = new CustomReadableStream();

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromStream(readableStream);
t.deepEqual(result, {ext: 'unicorn', mime: 'application/unicorn'});
Expand All @@ -799,7 +799,7 @@ test('fileTypeFromStream should keep detecting default file types when no custom
const readableStream = fs.createReadStream(file);

const customDetectors = [unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromStream(readableStream);
t.deepEqual(result, {ext: 'png', mime: 'image/png'});
Expand All @@ -810,7 +810,7 @@ test('fileTypeFromStream should allow overriding default file type detectors', a
const readableStream = fs.createReadStream(file);

const customDetectors = [mockPngDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromStream(readableStream);
t.deepEqual(result, {ext: 'mockPng', mime: 'image/mockPng'});
Expand Down Expand Up @@ -849,7 +849,7 @@ test('fileTypeFromTokenizer should return undefined when a custom detector chang

// Include the unicornDetector here to verify it's not used after the tokenizer.position changed
const customDetectors = [tokenizerPositionChanger, unicornDetector];
const parser = new NodeFileTypeParser({customDetectors});
const parser = new FileTypeParser({customDetectors});

const result = await parser.fromTokenizer(strtok3.fromBuffer(uint8ArrayContent));
t.is(result, undefined);
Expand Down

0 comments on commit f526ce6

Please sign in to comment.