forked from jshttp/type-is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
278 lines (238 loc) · 6.78 KB
/
index.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*!
* request-type-is
* Copyright(c) 2014 Jonathan Ong
* Copyright(c) 2014-2015 Douglas Christopher Wilson
* Copyright(c) 2022 Josep Galearez
* MIT Licensed
*/
/**
* Module dependencies.
* @private
*/
const typer = require('media-typer');
const mime = require('mime-types');
/**
* Normalize a type and remove parameters.
*
* @param {string | Request} value - A `string` or `Request`
* with a type to normalize
* @return A `string` with the normalized value or `Error`
* if the type cannot be normalized
* @private
*/
function normalizeType(value) {
// gets type from request when is a Request object
// prettier-ignore
const val = typeof value === 'object' ? value.headers.get('Content-Type') : value;
// parse the type
const type = typer.parse(val);
// remove the parameters
type.parameters = undefined;
// reformat it
return typer.format(type);
// flatten arguments of string[]
}
/**
* This function is a wrapper of the normalizeType function,
* meant to handle the errors comming form the media-typer instances.
*
* @param {string | Request} value - A `string` or `Request` with
* a type to normalize
* @return A `string` with the normalized value or `null` if the
* type cannot be normalized
* @private
*/
function tryNormalizeType(value) {
if (!value) {
return null;
}
try {
return normalizeType(value);
} catch (err) {
return null;
}
}
/**
* Normalize a mime type.
* If it's a shorthand, expand it to a valid mime type.
*
* In general, you probably want:
*
* const 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 A `string` containing the type to normalize
* @return A `string` with the normalized value, `null` if
* cannot be normalized
* @public
*/
function normalize(type) {
if (typeof type !== 'string') {
// invalid type
return false;
}
switch (type) {
case 'urlencoded':
return 'application/x-www-form-urlencoded';
case 'multipart':
return 'multipart/*';
default:
}
if (type[0] === '+') {
// "+json" -> "*/*+json" expando
return `*/*${type}`;
}
return type.indexOf('/') === -1 ? mime.lookup(type) : type;
}
/**
* Check if `actual` mime type
* matches `expected` mime type with
* wildcard and +suffix support.
*
* @param {string | false} actual The result of normalize (i.e.
* a `string` or `false`)
* @param {string} expected What you expect the type to be
* @return A `boolean` indicating if actual and expected types
* match
* @public
*/
function mimeMatch(expected, actual) {
// invalid type
if (expected === false) {
return false;
}
// split types
const actualParts = actual.split('/');
const expectedParts = expected.split('/');
// invalid format
if (actualParts.length !== 2 || expectedParts.length !== 2) {
return false;
}
// validate type
if (expectedParts[0] !== '*' && expectedParts[0] !== actualParts[0]) {
return false;
}
// validate suffix wildcard
if (expectedParts[1].substr(0, 2) === '*+') {
return (
// prettier-ignore
expectedParts[1].length <= actualParts[1].length + 1
&& expectedParts[1].substr(1)
=== actualParts[1].substr(1 - expectedParts[1].length)
);
}
// validate subtype
if (expectedParts[1] !== '*' && expectedParts[1] !== actualParts[1]) {
return false;
}
return true;
}
/**
* 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 | Request} value A `string` or `Request` to
* compare with one or multiple types
* @param {string[] | string[][]} types_ An array of `string` | `string[]`
* to check if the type from **value** matches with one of these types
* @return `false` if there is not match or a `string` with the
* type if a match was found
* @public
*/
function typeIs(value, ...types_) {
let i;
// remove parameters and normalize
const val = tryNormalizeType(value);
// no type or invalid
if (!val) {
return false;
}
// flatten arguments of string[], make a 2d array into a 1d array
const types = [].concat(...types_);
// no types, return the content type
if (!types || !types.length) {
return val;
}
let type;
for (i = 0; i < types.length; i += 1) {
type = types[i];
if (mimeMatch(normalize(type), val)) {
return type[0] === '+' || type.indexOf('*') !== -1 ? val : 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 {Request} req A `Request` object
* @return `true` if the `Request` has a body, `false` if not
* @public
*/
function hasBody(req) {
return (
// prettier-ignore
req.headers.get('Transfer-Encoding') !== undefined
|| !Number.isNaN(Number(req.headers.get('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 {Request} req A `Request` object
* @param {string[] | string[][]} types_ An array of `string` | `string[]`
* to check if the type of the **Request**, matches with one of these types
* @return A `string` with the match type, `null` if the request has
* no body or `false` if the types doesn't match
* @public
*/
function requestTypeIs(req, ...types_) {
// no body
if (!hasBody(req)) {
return null;
}
// flatten arguments of string[], make a 2d array into a 1d array
const types = [].concat(...types_);
// request content type
const value = req.headers.get('Content-Type');
return typeIs(value, types);
}
/**
* Module exports.
* @public
*/
module.exports = requestTypeIs;
module.exports.typeIs = typeIs;
module.exports.hasBody = hasBody;
module.exports.normalize = normalize;
module.exports.mimeMatch = mimeMatch;