/usr/lib/nodejs/type-is/index.js is in node-type-is 1.5.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | var typer = require('media-typer')
var mime = require('mime-types')
module.exports = typeofrequest;
typeofrequest.is = typeis;
typeofrequest.hasBody = hasbody;
typeofrequest.normalize = normalize;
typeofrequest.match = mimeMatch;
/**
* Compare a `value` content-type with `types`.
* Each `type` can be an extension like `html`,
* a special shortcut like `multipart` or `urlencoded`,
* or a mime type.
*
* If no types match, `false` is returned.
* Otherwise, the first `type` that matches is returned.
*
* @param {String} value
* @param {Array} types
* @return String
*/
function typeis(value, types_) {
var i
var types = types_
// remove parameters and normalize
value = typenormalize(value)
// no type or invalid
if (!value) {
return false
}
// support flattened arguments
if (types && !Array.isArray(types)) {
types = new Array(arguments.length - 1)
for (i = 0; i < types.length; i++) {
types[i] = arguments[i + 1]
}
}
// no types, return the content type
if (!types || !types.length) return value;
var type
for (i = 0; i < types.length; i++) {
if (mimeMatch(normalize(type = types[i]), value)) {
return type[0] === '+' || ~type.indexOf('*')
? value
: type
}
}
// no matches
return false;
}
/**
* Check if a request has a request body.
* A request with a body __must__ either have `transfer-encoding`
* or `content-length` headers set.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3
*
* @param {Object} request
* @return {Boolean}
* @api public
*/
function hasbody(req) {
var headers = req.headers;
if ('transfer-encoding' in headers) return true;
return !isNaN(headers['content-length']);
}
/**
* Check if the incoming request contains the "Content-Type"
* header field, and it contains any of the give mime `type`s.
* If there is no request body, `null` is returned.
* If there is no content type, `false` is returned.
* Otherwise, it returns the first `type` that matches.
*
* Examples:
*
* // With Content-Type: text/html; charset=utf-8
* this.is('html'); // => 'html'
* this.is('text/html'); // => 'text/html'
* this.is('text/*', 'application/json'); // => 'text/html'
*
* // When Content-Type is application/json
* this.is('json', 'urlencoded'); // => 'json'
* this.is('application/json'); // => 'application/json'
* this.is('html', 'application/*'); // => 'application/json'
*
* this.is('html'); // => false
*
* @param {String|Array} types...
* @return {String|false|null}
* @api public
*/
function typeofrequest(req, types_) {
var types = types_
// no body
if (!hasbody(req)) {
return null
}
// support flattened arguments
if (arguments.length > 2) {
types = new Array(arguments.length - 1)
for (var i = 0; i < types.length; i++) {
types[i] = arguments[i + 1]
}
}
// request content type
var value = req.headers['content-type']
return typeis(value, types);
}
/**
* Normalize a mime type.
* If it's a shorthand, expand it to a valid mime type.
*
* In general, you probably want:
*
* var type = is(req, ['urlencoded', 'json', 'multipart']);
*
* Then use the appropriate body parsers.
* These three are the most common request body types
* and are thus ensured to work.
*
* @param {String} type
* @api private
*/
function normalize(type) {
switch (type) {
case 'urlencoded': return 'application/x-www-form-urlencoded';
case 'multipart':
type = 'multipart/*';
break;
}
return type[0] === '+' || ~type.indexOf('/')
? type
: mime.lookup(type)
}
/**
* Check if `exected` mime type
* matches `actual` mime type with
* wildcard and +suffix support.
*
* @param {String} expected
* @param {String} actual
* @return {Boolean}
* @api private
*/
function mimeMatch(expected, actual) {
// invalid type
if (expected === false) {
return false
}
// exact match
if (expected === actual) {
return true
}
actual = actual.split('/');
if (expected[0] === '+') {
// support +suffix
return Boolean(actual[1])
&& expected.length <= actual[1].length
&& expected === actual[1].substr(0 - expected.length)
}
if (!~expected.indexOf('*')) return false;
expected = expected.split('/');
if (expected[0] === '*') {
// support */yyy
return expected[1] === actual[1]
}
if (expected[1] === '*') {
// support xxx/*
return expected[0] === actual[0]
}
if (expected[1][0] === '*' && expected[1][1] === '+') {
// support xxx/*+zzz
return expected[0] === actual[0]
&& expected[1].length <= actual[1].length + 1
&& expected[1].substr(1) === actual[1].substr(1 - expected[1].length)
}
return false
}
/**
* Normalize a type and remove parameters.
*
* @param {string} value
* @return {string}
* @api private
*/
function typenormalize(value) {
try {
var type = typer.parse(value)
delete type.parameters
return typer.format(type)
} catch (err) {
return null
}
}
|