This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
output-handler.server.js
144 lines (115 loc) · 4 KB
/
output-handler.server.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
/*
* Copyright (c) 2011-2013, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
/*jslint anon:true, sloppy:true, nomen:true*/
/**
* This is an object used as the single pathway for data to leave a mojit
* action execution. It is used as a component of the ActionContext object,
* which uses it to call <em>done</em> and <em>flush</em> in order to complete.
*
* There are two versions of this object, one for the client, and one for the
* server. This is the server version, which is more complex than the client
* version.
*
* @class OutputHandler
* @param {Object} req The Request object.
* @param {Object} res The Response object.
* @param {Function} next The next function, which should be invokable.
* @constructor
*/
var NAME = 'OutputHandler.server',
libutil = require('util'),
OutputHandler = function(req, res, next) {
this.req = req;
this.res = res;
this.next = next;
this.headers = {};
this.page = {};
};
OutputHandler.prototype = {
setLogger: function(logger) {
this.logger = logger;
},
flush: function(data, meta) {
this._readMeta(meta);
this._writeHeaders();
this.res.write(data);
},
done: function(data, meta) {
var name,
obj,
size,
memDebug = {};
this.logger.log('done', 'info', NAME);
this._readMeta(meta);
this._writeHeaders();
if (!data ||
(typeof data !== 'string' && Object.keys(data).length === 0)) {
data = '';
}
this.res.end(data);
this.logger.log('END', 'mojito', NAME);
},
error: function(err) {
err = err || new Error('Unknown error occurred');
if (!err.code) {
err.code = 500;
}
if (err.code === 404) {
// FUTURE: [Issue 96] default Mojito 404 page
this.logger.log(err, 'warn', NAME);
} else {
this.logger.log(err, 'error', NAME);
}
var out = '<html>' +
'<body><h1>Error: ' + err.code + '</h1>' +
// The following line that includes the error message has been
// removed because the Paranoids don't want this data to be
// revealed in production environments. Once the bug having
// to do with different development environments has been
// fixed, we will be able to conditionally display the error
// details.
// "<p>" + err.message + "</p>" +
'<p>Error details are not available.</p>' +
'</body>' +
'</html>';
// TODO: [Issue 96] If YUI._mojito.DEBUG, add stack.
this.done(out, {
http: {
code: err.code,
reasonPhrase: err.reasonPhrase,
headers: {
'content-type': 'text/html'
}
}
});
},
_readMeta: function(meta) {
if (!meta || !meta.http) { return; }
var header;
for (header in meta.http.headers) {
if (meta.http.headers.hasOwnProperty(header)) {
this.headers[header] = meta.http.headers[header];
}
}
this.statusCode = meta.http.code;
this.reasonPhrase = meta.http.reasonPhrase;
},
_writeHeaders: function() {
if (!this.headersSent) {
// passing a falsy reason phrase would break, because node uses every non-string arguments[1]
// as header object/array
if (this.reasonPhrase && typeof this.reasonPhrase === 'string') {
this.res.writeHead(this.statusCode || 200, this.reasonPhrase, this.headers);
} else {
this.res.writeHead(this.statusCode || 200, this.headers);
}
this.headersSent = true;
}
}
};
/**
*/
module.exports = OutputHandler;