Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is detecting file type from buffer async? #410

Closed
zadam opened this issue Oct 28, 2020 · 4 comments
Closed

Why is detecting file type from buffer async? #410

zadam opened this issue Oct 28, 2020 · 4 comments
Labels

Comments

@zadam
Copy link

zadam commented Oct 28, 2020

Hi, I'm wondering why is FileType.fromBuffer(buffer) async - i.e. returns Promise? 

Reading buffer seems naturally synchronous since there's no I/O involved.

@sindresorhus
Copy link
Owner

Because the core parser is made with a stream which is async. It would indeed be nice to have synchronous support too.

@Borewit
Copy link
Collaborator

Borewit commented Oct 28, 2020

Indeed, due to the common functions and code which, depending under the underlying media, require asynchronous behavior. it not feasible to split these into asynchronous and synchronous. Unfortunately there is no way, or at least as far as I know, to convert asynchronous into synchronous behavior. Is the asynchronous an issue, knowing it resolves immediate?

@zadam
Copy link
Author

zadam commented Oct 29, 2020

Thank you for the explanation.

Is the asynchronous an issue, knowing it resolves immediate?

In some cases yes. As an example transaction function from better-sqlite3 needs a synchronous callback, I believe it won't work for async callbacks even if it resolves immediately. Of course there are workarounds possible, but they often result in more complex code.

But I understand the engineering challenges in making this call synchronous.

@Borewit
Copy link
Collaborator

Borewit commented Nov 2, 2020

I didn't directly thought about such a case. In case of a transaction, you don''t want to introduce any unnecessary delay. Even if there an expensive synchronous function is probably better to execute outside the transaction. Nevertheless, I understand it maybe bit inconvenient some cases.

@Borewit Borewit closed this as completed Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants