sindresorhus / file-type

Detect the file type of a file, stream, or data
MIT License
3.72k stars 354 forks source link

fileTypeStream broken import #648

Closed tvthatsme closed 2 months ago

tvthatsme commented 3 months ago

We are updating from 19.0.0 to 19.0.3 and facing a seeming breaking change.

Had been using:

import { fileTypeStream } from "file-type/core";

But see that fileTypeStream has now been moved to the index file. So I expected that just changing the path would work.

import { fileTypeStream } from "file-type";

However, the module exports don't provide a path to import this function. The node imports work fine but the default is mapping to ./core.js so it's breaking the TypeScript compiler.

Is this something we could get reverted/adjusted to so the importing of fileTypeStream works as expected?

sindresorhus commented 3 months ago

But see that fileTypeStream has now been moved to the index file. So I expected that just changing the path would work.

That is indeed an unintended breaking change. I think we all had forgotten about the /core sub-export.

The node imports work fine but the default is mapping to ./core.js so it's breaking the TypeScript compiler.

It sounds like your bundler/compiler config is incorrect. You should be getting the node export, not default.


// @Borewit

tvthatsme commented 3 months ago

Thanks for the response! It was all working fine (including bundle/compiler config) prior to the update so I think it's probably just the breaking change that is the cause.

Borewit commented 3 months ago

I am sorry if I have broken something. I am currently traveling and cannot quickly fix something.

If you consider getting rid of it, I do not mind. Not sure if the /core sub export adds much value. I had trouble compiling Angular code using such sub export.

Borewit commented 3 months ago

Not a mistake at all.

In PR #635, introducing web stream support, the fileTypeStream has been moved to Node.js specific entry point as it is takes Node.js Readable stream as an argument, see discussion here: https://github.com/sindresorhus/file-type/pull/635#issuecomment-2211773896

We should add Web Stream flavour of fileTypeStream to the default entry point (which is the same as the /core sub export). I propose to keep this issue open, as request to add that function.

Update: Add web stream support for fileTypeStream(): #649

tvthatsme commented 3 months ago

Thanks for the info. I guess the problem I am seeing is that with that change (introduced in 19.1.0) TypeScript doesn't understand where to look for the type information anymore. It works when running the app but not for the tooling.

I was able to fix it on my side by adding a type definition file but this seems less than ideal. Maybe I am missing something?

import { Readable } from "node:stream";
import { FileTypeResult } from "file-type";

declare module "file-type" {
    export type ReadableStreamWithFileType = Readable & {
        readonly fileType?: FileTypeResult;
    };

    export async function fileTypeStream(readable: Readable): Promise<ReadableStreamWithFileType>;
}
sindresorhus commented 3 months ago

@tvthatsme Can you try this branch? https://github.com/sindresorhus/file-type/commit/7548a6222e21c2000265cc4e5052ca595f1b3b6b

npm install 'sindresorhus/file-type#7548a6222e21c2000265cc4e5052ca595f1b3b6b'
tvthatsme commented 3 months ago

Hey @sindresorhus thanks for looking at it a bit more! I tried your branch, together with deleting the type definition file I had created, but got the same error when running typecheck:

error TS2724: '"file-type"' has no exported member named 'fileTypeStream'. Did you mean 'fileTypeFromStream'?

3 import { fileTypeStream } from "file-type";

I think there is something going on where typechecking is looking at the default or core type definitions, but the app is really just using the node methods.

At this point, I have the type definitions working and am not blocked. So I'm fine to close this issue if it's a "just me" situation. I appreciate the support in looking at this issue!

Th3Vladimir commented 3 months ago

Same issue here

Borewit commented 3 months ago

Same issue here

That is very weird, as since v19.4.0, both entry points (Node & default) export fileTypeStream().

minicacristiancolleva commented 2 months ago

same issue

Borewit commented 2 months ago

The export of fileTypeStream() has been re-added in v19.4.0, therefor I conciser this issue as resolved.