node icon indicating copy to clipboard operation
node copied to clipboard

add `path.mimeType()` and `path.charset()` to `node:path`

Open aralroca opened this issue 6 months ago • 2 comments

What is the problem this feature will solve?

Adding path.mimeType() and path.charset will solve the dependency on an external library, mime-types, to get the content type of a file based on its extension. This can be done with Node.js itself, just as Bun.js does with Bun.file(file).type.

What is the feature you are proposing to solve the problem?

I am proposing to add two new methods to the path module:

  • mimeType - returns the MIME type of a file based on its extension.
  • charset - returns the charset of a MIME type.
path.mimeType('file.js'); // returns 'application/javascript'
// or:
path.mimeType(path.extname('file.js')); // returns 'application/javascript'

And:

path.charset('file.js'); // returns 'utf-8'
// or:
path.charset(path.mimeType('file.js')); // returns 'utf-8'

What alternatives have you considered?

Alternatively, instead of being in node:path, it could be in fs.stat, but I think that in the end it can be extracted from the path without having to parse the file, besides it is better to use functions for this and the work is done only if they are executed individually.

CC: @vdeturckheim

aralroca avatar Aug 27 '24 18:08 aralroca