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

Add a MIME type API #43

Open
annevk opened this issue Oct 16, 2017 · 14 comments
Open

Add a MIME type API #43

annevk opened this issue Oct 16, 2017 · 14 comments

Comments

@annevk
Copy link
Member

annevk commented Oct 16, 2017

If you write a library and consume some kind of server-side code you might want to verify MIME types in the same way as browsers do. E.g., not reject MIME types that end with ;.

const mime = new MIMEType("text/javascript;charset=utf-8");
mime.type; // "text"
mime.subtype; // "javascript"
mime.essence; // "text/javascript"
mime.param("charset"); // "utf-8"

It also seems to make sense somewhat to make the object iterable from an IDL perspective so you can get all the parameters that way easily.

@domenic
Copy link
Member

domenic commented Oct 16, 2017

It's worth surveying the existing JS library APIs such as:

although I am sure we'll have our own concerns that will mean none of them are immediately translateable.

@annevk
Copy link
Member Author

annevk commented Nov 21, 2017

They all basically look the same. Main difference seems to be whether to group type and subtype (I suggest we offer both) and whether it's mutable (I'd suggest it's not).

@dougwilson seems responsible for some packages so might have thoughts. (For context, this is about a MIME type parser exposed to JavaScript in the browser.) @jasnell might as well given that this is something Node.js might want to pick up.

@annevk
Copy link
Member Author

annevk commented Nov 23, 2017

Is overrideMimeType() sufficient precedent to name it MimeType rather than MIMEType?

@domenic
Copy link
Member

domenic commented Nov 23, 2017

No, we should follow the usual acronym rules for new features.

@annevk
Copy link
Member Author

annevk commented Nov 27, 2017

The other thing we need to consider is what to return for x/x;charset="\"". I suspect we want get() that returns unquoted value (") and rawGet() or some such that returns the value as stored ("\""). (See #40 for more details.)

@domenic
Copy link
Member

domenic commented Nov 30, 2017

A big choice is whether this API is mutable (like URL) or immutable (as the OP outlines). For purposes like changing the charset, or adding params in general, mutable is necessary.

@annevk
Copy link
Member Author

annevk commented Dec 1, 2017

Yeah, maybe we should just stick to that and if you want immutable you use a string. Should have a section similar to the one URL has on usage elsewhere.

@jasnell
Copy link

jasnell commented Dec 1, 2017

mutable works for me.

Another question would be: will this provide any built in mechanism for wild cards and matching or is this purely a parsing function and everything else is left to user land to figure out?

For instance, would there be any provision for the following kinds of action:

const m = new MIMEType('text/javascript');
const n  = new MIMEType('text/*');
n.match(m);  // true
m.match(n);  // true

Would generally prefer that it not get in to that level.

@annevk
Copy link
Member Author

annevk commented Dec 2, 2017

The initial API would just be parsing, exposing the resulting (mutable) structure, and serializing. #48 has some ideas for constant-like operations (e.g., is this an XML MIME type), but I don't think we want to invent generic matching schemes (at least for now).

domenic added a commit to jsdom/whatwg-mimetype that referenced this issue Dec 10, 2017
* Renames the package from "content-type-parser" to "whatwg-mimetype", as MIME type is the more general concept, and this is now implementing part of the WHATWG MIME Sniffing standard
* Replaces the parser and serializer with the newly-specified one from  whatwg/mimesniff@cc81ec4. This closes #3 as regular expressions are no longer used.
* Overhauls the API to more or less match what is proposed in whatwg/mimesniff#43. Notably, the invariants of the MIME type model are now maintained more aggressively, and the parameters exist on a separate Map-like data structure. Also removes the isText() method, as it's much less interesting than the other two.
* Switches from Mocha to Jest, and brings in the appropriate web platform test data files.

All of this helps close #1, as it's now clear that this project has its own direction which is more standards-based and merging it with another project doesn't make much sense.
@domenic
Copy link
Member

domenic commented Jan 31, 2018

I've prototyped my take on this API in https://www.npmjs.com/package/whatwg-mimetype, FWIW. Including some experimental type testers.

@devsnek
Copy link

devsnek commented Jun 5, 2018

in reference to that node pr, could work be done at this point to normalise a spec which hopefully also includes sniffing? i'd be happy to help however i can.

@annevk
Copy link
Member Author

annevk commented Nov 13, 2018

@devsnek sorry for not responding sooner. Yes, this can be formalized at this point. An API for sniffing should very much be a separate effort I think. That might also be worthwhile, but let's not include that here as it would have a very different API.

@jimmywarting
Copy link

jimmywarting commented Nov 6, 2020

Would be nice if browsers mime sniffing algoritm would somehow be exposed too.
One usecase comes from unzipping a bunch of files but knows nothing about their types so creating a right mime type for use with createObjectURL is tuff without npm's mime

A api for looking up mimetype with extension & magic number would be nice too.

var mime = MIME.fromType("text/javascript;charset=utf-8")
var mime = MIME.fromExtension("txt")
var mime = await MIME.fromBlobOrFile(blob) // magic number lookup

Another solution would be if File, Blob constructor had an option for auto detecting the type according to mime sniffing standard. createObjectURL could have an auto detecting too, but i would like an api that works in service worker too.

@annevk
Copy link
Member Author

annevk commented Jan 7, 2021

(I realized today that the HTML standard exposes MimeType as an interface. I don't think we should reuse that here though and given the different capitalization it doesn't matter much, but wanted to note it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants