“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
This article continues with the source code for the KOA dependent libraries, the last one looked at the accepts of Content-type, Content-Type and cache-Content-Type, and this one looks at some of the content-related libraries.
Type-is is also a content-type library. In KOA, the IS methods under request and response are implemented by calling the Type-is library. What this library does is determine whether the content-Type of the current request or response is of a certain type.
First look at the export section:
module.exports = typeofrequest
module.exports.is = typeis
module.exports.hasBody = hasbody
module.exports.normalize = normalize
module.exports.match = mimeMatch
Copy the code
The default export is Typeofrequest. This method supports passing reQ directly as a parameter. In non-REQ scenarios, you can export using IS, which accepts content-Type as a parameter. The default export is used in koA Request and the IS export is used in Response. The following arguments can be passed in multiple or one, or arrays can be used. Here is another typical argument handling logic:
var types = types_
if (arguments.length > 2) {
types = new Array(arguments.length - 1)
for (var i = 0; i < types.length; i++) {
types[i] = arguments[i + 1]}}Copy the code
Headers [‘content-type’] is also passed to the Typeis. The core logic is actually in the Typeis.
In TypeIs, a normalizeType procedure is first performed:
function normalizeType (value) {
// parse the type
var type = typer.parse(value)
// remove the parameters
type.parameters = undefined
// reformat it
return typer.format(type)
}
Copy the code
Here typer is the media-typer library used, with parameters removed.
The incoming type information is also normalize, again by calling the LOOKUP method of the MIME-types library:
function normalize (type) {
if (typeoftype ! = ='string') {
// invalid type
return false
}
switch (type) {
case 'urlencoded':
return 'application/x-www-form-urlencoded'
case 'multipart':
return 'multipart/*'
}
if (type[0= = ='+') {
// "+json" -> "*/*+json" expando
return '* / *' + type
}
return type.indexOf('/') = = = -1
? mime.lookup(type)
: type
}
Copy the code
Finally, the logic for traversing the match:
function mimeMatch (expected, actual) {
// invalid type
if (expected === false) {
return false
}
// split types
var actualParts = actual.split('/')
var expectedParts = expected.split('/')
// invalid format
if(actualParts.length ! = =2|| expectedParts.length ! = =2) {
return false
}
// validate type
if (expectedParts[0]! = =The '*' && expectedParts[0] !== actualParts[0]) {
return false
}
// validate suffix wildcard
if (expectedParts[1].substr(0.2) = = =* '+') {
return expectedParts[1].length <= actualParts[1].length + 1 &&
expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length)
}
// validate subtype
if (expectedParts[1]! = =The '*' && expectedParts[1] !== actualParts[1]) {
return false
}
return true
}
Copy the code
HasBody is also easy to use with transfer-encoding and Content-Length headers:
function hasbody (req) {
return req.headers['transfer-encoding']! = =undefined ||
!isNaN(req.headers['content-length'])}Copy the code
This is all the logic of the Type-IS library. It can be seen that all functions involving content-Type correlation are dependent on MIME-types judgment, so they all rely on the miME-types parsing library.
In addition to content-Type, there is also a content-disposition field in the HTTP response, which is used to handle file download scenarios. When content-disposition is not set or the value is inline, the file is in preview mode, where resources are viewed in the web page. Setting Content-Disposition to attachment browsers will consider this a file to be downloaded and will turn on the browser download function to download the content resource. Attachment can be set to filename to specify the download filename.
The Attachment method is provided in the KOA Response, where you can set the Content-Disposition header for the RES. The Content-Disposition library is used inside the Attachment method for header content assembly. Content-disposition also provides a default assembly method and a parse method for parsing, similar to other header parsing libraries, with mostly string matching logic inside, involving a series of regular expressions that are not expanded here.