-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments
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. |
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. |
Is |
No, we should follow the usual acronym rules for new features. |
The other thing we need to consider is what to return for |
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. |
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. |
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. |
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). |
* 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.
I've prototyped my take on this API in https://www.npmjs.com/package/whatwg-mimetype, FWIW. Including some experimental type testers. |
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. |
@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. |
Would be nice if browsers mime sniffing algoritm would somehow be exposed too. 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. |
(I realized today that the HTML standard exposes |
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
;
.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.
The text was updated successfully, but these errors were encountered: