-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
qualifiers.js
87 lines (71 loc) · 2.46 KB
/
qualifiers.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
/**
* Module dependencies
*/
var STRINGFILE = require('sails-stringfile');
/**
* Mix in convenience flags about this request
*
* @param {Request} req
* @param {Response} res
* @api private
*/
module.exports = function _mixinReqQualifiers(req, res) {
var accept = req.get('Accept') || '';
// Flag indicating whether HTML was explicitly mentioned in the Accepts header
req.explicitlyAcceptsHTML = (accept.indexOf('html') !== -1);
// Flag indicating whether a request would like to receive a JSON response
//
// This qualification is determined based on a handful of heuristics, including:
// • if this looks like an AJAX request
// • if this is a virtual request from a socket
// • if this request DOESN'T explicitly want HTML
// • if this request has a "json" content-type AND ALSO has its "Accept" header set
// • if this request has the option "wantsJSON" set
req.wantsJSON = req.xhr;
req.wantsJSON = req.wantsJSON || req.isSocket;
req.wantsJSON = req.wantsJSON || !req.explicitlyAcceptsHTML;
req.wantsJSON = req.wantsJSON || (req.is('json') && req.get('Accept'));
req.wantsJSON = req.wantsJSON || req.options.wantsJSON;
// Deprecated properties
bindReqDeprecationNotice(req, 'isJson');
bindReqDeprecationNotice(req, 'isAjax');
bindResDeprecationNotice(res, 'viewExists');
};
/**
* Bind deprecation notices for `req.*` properties from 0.8.x,
* but only in development env, and only if the property
* doesn't already exist (i.e. in case a user-defined
* hook bound it on the `req` object.)
*
* @param {Request} req
* @param {String} key
*/
function bindReqDeprecationNotice(req, key) {
if (process.env.NODE_ENV === 'production' || req[key]) { return; }
// Attach a getter
Object.defineProperty(req, key, {
value: function showDeprecationNotice() {
var e = STRINGFILE.get('upgrade.req.' + key);
throw new Error(e);
}
});
}
/**
* Bind deprecation notices for `res.*` properties from 0.8.x,
* but only in development env, and only if the property
* doesn't already exist (i.e. in case a user-defined
* hook bound it on the `res` object.)
*
* @param {Response} res
* @param {String} key
*/
function bindResDeprecationNotice(res, key) {
if (process.env.NODE_ENV === 'production' || res[key]) { return; }
// Attach a getter
Object.defineProperty(res, key, {
value: function showDeprecationNotice() {
var e = STRINGFILE.get('upgrade.res.' + key);
throw new Error(e);
}
});
}