-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
377 lines (337 loc) · 11 KB
/
application.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
'use strict';
const path = require('path');
const fs = require('fs');
const ms = require('ms');
const graceful = require('graceful');
const http = require('http');
const cluster = require('cluster-client');
const onFinished = require('on-finished');
const { assign } = require('utility');
const eggUtils = require('egg-core').utils;
const EggApplication = require('./egg');
const AppWorkerLoader = require('./loader').AppWorkerLoader;
const KEYS = Symbol('Application#keys');
const HELPER = Symbol('Application#Helper');
const LOCALS = Symbol('Application#locals');
const BIND_EVENTS = Symbol('Application#bindEvents');
const WARN_CONFUSED_CONFIG = Symbol('Application#warnConfusedConfig');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');
const CLUSTER_CLIENTS = Symbol.for('egg#clusterClients');
const RESPONSE_RAW = Symbol('Application#responseRaw');
// client error => 400 Bad Request
// Refs: https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_event_clienterror
const DEFAULT_BAD_REQUEST_HTML = `<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>❤</center>
</body>
</html>`;
const DEFAULT_BAD_REQUEST_HTML_LENGTH = Buffer.byteLength(DEFAULT_BAD_REQUEST_HTML);
const DEFAULT_BAD_REQUEST_RESPONSE =
`HTTP/1.1 400 Bad Request\r\nContent-Length: ${DEFAULT_BAD_REQUEST_HTML_LENGTH}` +
`\r\n\r\n${DEFAULT_BAD_REQUEST_HTML}`;
// Refs: https://github.com/nodejs/node/blob/b38c81/lib/_http_outgoing.js#L706-L710
function escapeHeaderValue(value) {
// Protect against response splitting. The regex test is there to
// minimize the performance impact in the common case.
return /[\r\n]/.test(value) ? value.replace(/[\r\n]+[ \t]*/g, '') : value;
}
// Refs: https://github.com/nodejs/node/blob/b38c81/lib/_http_outgoing.js#L706-L710
/**
* Singleton instance in App Worker, extend {@link EggApplication}
* @extends EggApplication
*/
class Application extends EggApplication {
/**
* @constructor
* @param {Object} options - see {@link EggApplication}
*/
constructor(options = {}) {
options.type = 'application';
super(options);
// will auto set after 'server' event emit
this.server = null;
try {
this.loader.load();
} catch (e) {
// close gracefully
this[CLUSTER_CLIENTS].forEach(cluster.close);
throw e;
}
// dump config after loaded, ensure all the dynamic modifications will be recorded
const dumpStartTime = Date.now();
this.dumpConfig();
this.coreLogger.info('[egg:core] dump config after load, %s', ms(Date.now() - dumpStartTime));
this[WARN_CONFUSED_CONFIG]();
this[BIND_EVENTS]();
}
get [EGG_LOADER]() {
return AppWorkerLoader;
}
get [EGG_PATH]() {
return path.join(__dirname, '..');
}
[RESPONSE_RAW](socket, raw) {
if (!socket.writable) return;
if (!raw) return socket.end(DEFAULT_BAD_REQUEST_RESPONSE);
let body = (raw.body == null) ? DEFAULT_BAD_REQUEST_HTML : raw.body;
const headers = raw.headers || {};
const status = raw.status || 400;
let responseHeaderLines = '';
let lengthOutputed = false;
const firstLine = `HTTP/1.1 ${status} ${http.STATUS_CODES[status] || 'Unknown'}`;
// Not that safe because no validation for header keys.
// Refs: https://github.com/nodejs/node/blob/b38c81/lib/_http_outgoing.js#L451
for (const key of Object.keys(headers)) {
if (key.toLowerCase() === 'content-length') lengthOutputed = true;
responseHeaderLines += `${key}: ${escapeHeaderValue(headers[key])}\r\n`;
}
if (!lengthOutputed) {
if (!Buffer.isBuffer(body)) body = Buffer.from(body);
responseHeaderLines += `Content-Length: ${body.byteLength}\r\n`;
}
socket.end(`${firstLine}\r\n${responseHeaderLines}\r\n${body.toString()}`);
}
onClientError(err, socket) {
this.logger.error('A client (%s:%d) error [%s] occurred: %s',
socket.remoteAddress,
socket.remotePort,
err.code,
err.message);
if (typeof this.config.onClientError === 'function') {
const p = eggUtils.callFn(this.config.onClientError, [ err, socket, this ]);
// the returned object should like:
//
// {
// body: '...',
// headers: {
// ...
// },
// status: 400
// }
//
// default values:
//
// + body: ''
// + headers: {}
// + status: 400
p.then(ret => {
this[RESPONSE_RAW](socket, ret || {});
}).catch(err => {
this.logger.error(err);
this[RESPONSE_RAW](socket);
});
} else {
// because it's a raw socket object, we should return the raw HTTP response
// packet.
this[RESPONSE_RAW](socket);
}
}
onServer(server) {
// expose app.server
this.server = server;
/* istanbul ignore next */
graceful({
server: [ server ],
error: (err, throwErrorCount) => {
if (err.message) {
err.message += ' (uncaughtException throw ' + throwErrorCount + ' times on pid:' + process.pid + ')';
}
this.coreLogger.error(err);
},
});
server.on('clientError', (err, socket) => this.onClientError(err, socket));
}
/**
* global locals for view
* @member {Object} Application#locals
* @see Context#locals
*/
get locals() {
if (!this[LOCALS]) {
this[LOCALS] = {};
}
return this[LOCALS];
}
set locals(val) {
if (!this[LOCALS]) {
this[LOCALS] = {};
}
assign(this[LOCALS], val);
}
/**
* Create egg context
* @method Application#createContext
* @param {Req} req - node native Request object
* @param {Res} res - node native Response object
* @return {Context} context object
*/
createContext(req, res) {
const app = this;
const context = Object.create(app.context);
const request = context.request = Object.create(app.request);
const response = context.response = Object.create(app.response);
context.app = request.app = response.app = app;
context.req = request.req = response.req = req;
context.res = request.res = response.res = res;
request.ctx = response.ctx = context;
request.response = response;
response.request = request;
context.onerror = context.onerror.bind(context);
context.originalUrl = request.originalUrl = req.url;
/**
* Request start time
* @member {Number} Context#starttime
*/
context.starttime = Date.now();
return context;
}
handleRequest(ctx, fnMiddleware) {
this.emit('request', ctx);
super.handleRequest(ctx, fnMiddleware);
onFinished(ctx.res, () => this.emit('response', ctx));
}
/**
* save routers to `run/router.json`
* @private
*/
dumpConfig() {
super.dumpConfig();
// dump routers to router.json
const rundir = this.config.rundir;
const FULLPATH = this.loader.FileLoader.FULLPATH;
try {
const dumpRouterFile = path.join(rundir, 'router.json');
const routers = [];
for (const layer of this.router.stack) {
routers.push({
name: layer.name,
methods: layer.methods,
paramNames: layer.paramNames,
path: layer.path,
regexp: layer.regexp.toString(),
stack: layer.stack.map(stack => stack[FULLPATH] || stack._name || stack.name || 'anonymous'),
});
}
fs.writeFileSync(dumpRouterFile, JSON.stringify(routers, null, 2));
} catch (err) {
this.coreLogger.warn(`dumpConfig router.json error: ${err.message}`);
}
}
/**
* Create an anonymous context, the context isn't request level, so the request is mocked.
* then you can use context level API like `ctx.service`
* @member {String} Application#createAnonymousContext
* @param {Request} req - if you want to mock request like querystring, you can pass an object to this function.
* @return {Context} context
*/
createAnonymousContext(req) {
const request = {
headers: {
'x-forwarded-for': '127.0.0.1',
},
query: {},
querystring: '',
host: '127.0.0.1',
hostname: '127.0.0.1',
protocol: 'http',
secure: 'false',
method: 'GET',
url: '/',
path: '/',
socket: {
remoteAddress: '127.0.0.1',
remotePort: 7001,
},
};
if (req) {
for (const key in req) {
if (key === 'headers' || key === 'query' || key === 'socket') {
Object.assign(request[key], req[key]);
} else {
request[key] = req[key];
}
}
}
const response = new http.ServerResponse(request);
return this.createContext(request, response);
}
/**
* Run async function in the background
* @see Context#runInBackground
* @param {Function} scope - the first args is an anonymous ctx
*/
runInBackground(scope) {
const ctx = this.createAnonymousContext();
if (!scope.name) scope._name = eggUtils.getCalleeFromStack(true);
ctx.runInBackground(scope);
}
/**
* secret key for Application
* @member {String} Application#keys
*/
get keys() {
if (!this[KEYS]) {
if (!this.config.keys) {
if (this.config.env === 'local' || this.config.env === 'unittest') {
const configPath = path.join(this.config.baseDir, 'config/config.default.js');
console.error('Cookie need secret key to sign and encrypt.');
console.error('Please add `config.keys` in %s', configPath);
}
throw new Error('Please set config.keys first');
}
this[KEYS] = this.config.keys.split(',').map(s => s.trim());
}
return this[KEYS];
}
/**
* reference to {@link Helper}
* @member {Helper} Application#Helper
*/
get Helper() {
if (!this[HELPER]) {
/**
* The Helper class which can be used as utility function.
* Files from `${baseDir}/app/helper` will be loaded to the prototype of Helper,
* then you can use all method on `ctx.helper` that is a instance of Helper.
*/
class Helper extends this.BaseContextClass {}
this[HELPER] = Helper;
}
return this[HELPER];
}
/**
* bind app's events
*
* @private
*/
[BIND_EVENTS]() {
// Browser Cookie Limits: http://browsercookielimits.squawky.net/
this.on('cookieLimitExceed', ({ name, value, ctx }) => {
const err = new Error(`cookie ${name}'s length(${value.length}) exceed the limit(4093)`);
err.name = 'CookieLimitExceedError';
err.key = name;
err.cookie = value;
ctx.coreLogger.error(err);
});
// expose server to support websocket
this.once('server', server => this.onServer(server));
}
/**
* warn when confused configurations are present
*
* @private
*/
[WARN_CONFUSED_CONFIG]() {
const confusedConfigurations = this.config.confusedConfigurations;
Object.keys(confusedConfigurations).forEach(key => {
if (this.config[key] !== undefined) {
this.logger.warn('Unexpected config key `%s` exists, Please use `%s` instead.',
key, confusedConfigurations[key]);
}
});
}
}
module.exports = Application;