jshttp / media-typer Goto Github PK
View Code? Open in Web Editor NEWSimple RFC 6838 media type parser and formatter
License: MIT License
Simple RFC 6838 media type parser and formatter
License: MIT License
Currently, the parse
function can parse media types surrounded by whitespaces, but not tabs:
$ node
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> var typer = require('media-typer')
undefined
> typer.parse("text/html ")
MediaType { type: 'text', subtype: 'html', suffix: undefined }
> typer.parse("text/html\t")
Uncaught TypeError: invalid media type
at Object.parse (/mnt/wsl/tank/projects/type-is/node_modules/media-typer/index.js:117:11)
>
However, I believe HTAB
should be a valid whitespace character in the context of Content-Type
header parsing. The specification for the Content-Type header (https://www.rfc-editor.org/rfc/rfc9110#field.content-type) states that:
The type/subtype MAY be followed by semicolon-delimited parameters (Section 5.6.6) in the form of name/value pairs.
And section 5.6.6 defines the semicolon-delimited parameters as:
parameters = *( OWS ";" OWS [ parameter ] )
parameter = parameter-name "=" parameter-value
parameter-name = token
parameter-value = ( token / quoted-string )
Where OWS
is defined in section 5.6.3 as:
OWS = *( SP / HTAB )
; optional whitespace
So the media-type might be followed by HTAB
s in a valid Content-Type
header. If the parser function supports trailing spaces, it should as well support trailing tabs.
In version 0.3.0
, when you call the parse
function and hand over a req
object that has no content-type
header set, this module crashes.
The problem is first located here, where getcontenttype
is called. Then this calls this code, which simply returns undefined
if the header is not there - which in turn leads to this error message.
I'm not that sure how to handle this case, but if you decide for an option, I'm happy to provide a PR.
this is not working
multipart/form-data; boundary=b/QeEbFgqK9PCZo4T/eXv7f.T74SHd5MxCZ846AsTz-yNV0xrRR_Zks4fkNMCzJck9ZE8o
this ok
multipart/form-data; boundary=hQ9mmyU9285lg8QhbF7zuDmeuvc8flGel8sSgjHTBinO72dLucgB7oUOC3l6070sPpd1Es
This was more of an "info" for anyone who may come across this in the future.
A guy on the express gitter was trying to work out why body-parser.json()
was not parsing his data.
What looked suspicious to me was his content-type being a double application/json
.
Looked like the certain fetch library he was using was doing it.
Content-Type: application/json, application/json
After digging and digging, via body-parser
, type-is
and finally media-typer
I found it was a regular expression that was causing it to fail.
Read the rfc7231 from a link in another issue to find that two content types is not valid.
It makes sense, but until you actually KNOW it, you can never certain.
Just wanted to leave this hear in case someone else came along with something similar.
I didn't know if there was any sanity checking that could be done?
Does "application/json;"
has a invalid parameter format?
I don't know actually.
Content-Type of application/xml; name=123
should be valid according to RFC 2045, Content-Types should support parameters type "/" subtype *(";" parameter)
However if you run var obj = typer.parse('application/xml; name=123')
you get the following error invalid media type
Now that content-type
module exists, this module can be fixed to actually parse MIME types.
Version v1.0.1 breaks my unit tests, following mime types do no longer parse:
message/external-body; access-type=URL
, taken from RFC 2017, line 96Text/HTML;Charset="utf-8"
Following error is throws: TypeError: invalid media type
Blocks: Borewit/music-metadata#155
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.