-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontroller.js
217 lines (195 loc) · 5.02 KB
/
controller.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
'use strict';
const delegate = require('delegates');
const requestLib = require('./lib/request');
const responseLib = require('./lib/response');
const Cookies = require('cookies');
const accepts = require('accepts');
const createError = require('http-errors');
const httpAssert = require('http-assert');
const Stream = require('stream');
const isJSON = require('koa-is-json');
const statuses = require('statuses');
function BayController(app, req, res) {
const request = this.request = Object.create(requestLib);
const response = this.response = Object.create(responseLib);
this.app = request.app = response.app = app;
this.req = request.req = response.req = req;
this.res = request.res = response.res = res;
request.ctx = response.ctx = this;
request.response = response;
response.request = request;
this.originalUrl = request.originalUrl = req.url;
this.cookies = new Cookies(req, res, this.keys);
this.accept = request.accept = accepts(req);
this.state = {};
}
/**
* Return JSON representation.
*
* Here we explicitly invoke .toJSON() on each
* object, as iteration will otherwise fail due
* to the getters and cause utilities such as
* clone() to fail.
*
* @return {Object}
* @api public
*/
BayController.prototype.toJSON = function () {
return {
request: this.request.toJSON(),
response: this.response.toJSON(),
app: this.app.toJSON(),
originalUrl: this.originalUrl,
req: '<original node req>',
res: '<original node res>',
socket: '<original node socket>'
};
};
/**
* Similar to .throw(), adds assertion.
*
* this.assert(this.user, 401, 'Please login!');
*
* See: https://github.com/jshttp/http-assert
*
* @param {Mixed} test
* @param {Number} [status=400]
* @param {String} message
* @api public
*/
BayController.prototype.assert = function (res, code, message) {
if (arguments.length === 1 || (arguments.length === 2 && typeof code === 'string')) {
return httpAssert(res, 400, code);
}
return httpAssert(res, code, message);
};
/**
* Throw an error with `msg` and optional `status`
* defaulting to 500. Note that these are user-level
* errors, and the message may be exposed to the client.
*
* this.throw(403)
* this.throw('name required', 400)
* this.throw(400, 'name required')
* this.throw('something exploded')
* this.throw(new Error('invalid'), 400);
* this.throw(400, new Error('invalid'));
*
* See: https://github.com/jshttp/http-errors
*
* @param {String|Number|Error} err, msg or status
* @param {String|Number|Error} [err, msg or status]
* @param {Object} [props]
* @api public
*/
BayController.prototype.throw = function (code, message, props) {
if (arguments.length === 1 && typeof code === 'string') {
throw createError(400, code, props);
}
throw createError(code, message, props);
};
/**
* Response helper.
*/
BayController.prototype.respond = function () {
const res = this.res;
if (res.headersSent || !this.writable) {
return;
}
let body = this.body;
const code = this.status;
// ignore body
if (statuses.empty[code]) {
// strip headers
this.body = null;
return res.end();
}
if (this.method === 'HEAD') {
if (isJSON(body)) {
this.length = Buffer.byteLength(JSON.stringify(body));
}
return res.end();
}
// status body
if (body === null || typeof body === 'undefined') {
this.type = 'text';
body = this.message || String(code);
this.length = Buffer.byteLength(body);
return res.end(body);
}
// responses
if (Buffer.isBuffer(body)) {
return res.end(body);
}
if (typeof body === 'string') {
return res.end(body);
}
if (body instanceof Stream) {
return body.pipe(res);
}
// body: json
body = JSON.stringify(body);
this.length = Buffer.byteLength(body);
res.end(body);
};
BayController.prototype.aroundAction = function (name, options) {
if (!options) {
options = {};
}
options.name = name;
if (!this._middleware) {
this._middleware = [];
}
this._middleware.push(options);
};
/**
* Response delegation.
*/
delegate(BayController.prototype, 'response')
.method('attachment')
.method('redirect')
.method('remove')
.method('vary')
.method('set')
.method('append')
.access('status')
.access('message')
.access('body')
.access('length')
.access('type')
.access('lastModified')
.access('etag')
.getter('headerSent')
.getter('writable');
/**
* Request delegation.
*/
delegate(BayController.prototype, 'request')
.method('acceptsLanguages')
.method('acceptsEncodings')
.method('acceptsCharsets')
.method('accepts')
.method('get')
.method('is')
.access('querystring')
.access('idempotent')
.access('socket')
.access('search')
.access('method')
.access('query')
.access('path')
.access('url')
.getter('origin')
.getter('href')
.getter('subdomains')
.getter('protocol')
.getter('host')
.getter('hostname')
.getter('header')
.getter('headers')
.getter('secure')
.getter('stale')
.getter('fresh')
.getter('ips')
.getter('ip');
module.exports = BayController;