-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
214 lines (180 loc) · 6.16 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
'use strict';
let mung = {};
let faux_fin = { end: () => null };
function isScalar(v) {
return typeof v !== 'object' && !Array.isArray(v);
}
mung.onError = (err, req, res, next) => {
res
.status(500)
.set('content-language', 'en')
.json({ message: err.message })
.end();
return res;
};
mung.json = function json (fn, options) {
return function (req, res, next) {
let original = res.json;
options = options || {};
let mungError = options.mungError;
function json_hook (json) {
let originalJson = json;
res.json = original;
if (res.headersSent)
return res;
if (!mungError && res.statusCode >= 400)
return original.call(this, json);
// Run the munger
try {
json = fn(json, req, res);
} catch (e) {
return mung.onError(e, req, res, next);
}
if (res.headersSent)
return res;
// If no returned value from fn, then assume json has been mucked with.
if (json === undefined)
json = originalJson;
// If null, then 204 No Content
if (json === null)
return res.status(204).end();
// If munged scalar value, then text/plain
if (originalJson !== json && isScalar(json)) {
res.set('content-type', 'text/plain');
return res.send(String(json));
}
return original.call(this, json);
}
res.json = json_hook;
next && next();
}
}
mung.jsonAsync = function json (fn, options) {
return function (req, res, next) {
let original = res.json;
options = options || {};
let mungError = options.mungError;
function json_async_hook (json) {
let originalJson = json;
res.json = original;
if (res.headersSent)
return;
if (!mungError && res.statusCode >= 400)
return original.call(this, json);
try {
fn(json, req, res)
.then(json => {
if (res.headersSent)
return;
// If null, then 204 No Content
if (json === null)
return res.status(204).end();
// If munged scalar value, then text/plain
if (json !== originalJson && isScalar(json)) {
res.set('content-type', 'text/plain');
return res.send(String(json));
}
return original.call(this, json);
})
.catch(e => mung.onError(e, req, res, next));
} catch (e) {
mung.onError(e, req, res, next);
}
return faux_fin;
}
res.json = json_async_hook;
next && next();
}
}
mung.headers = function headers (fn) {
return function (req, res, next) {
let original = res.end;
function headers_hook () {
res.end = original;
if (!res.headersSent) {
try {
fn(req, res);
} catch (e) {
return mung.onError(e, req, res, next);
}
if (res.headersSent) {
console.error('sending response while in mung.headers is undefined behaviour');
return;
}
}
return original.apply(this, arguments);
}
res.end = headers_hook;
next && next();
}
}
mung.headersAsync = function headersAsync (fn) {
return function (req, res, next) {
let original = res.end;
let onError = e => {
res.end = original;
return mung.onError(e, req, res, next);
};
function headers_async_hook () {
if (res.headersSent)
return original.apply(this, args);
let args = arguments;
res.end = () => null;
try {
fn(req, res)
.then(() => {
res.end = original;
if (res.headersSent)
return;
original.apply(this, args);
})
.catch(e => onError(e, req, res, next));
} catch (e) {
onError(e, req, res, next);
}
}
res.end = headers_async_hook;
next && next();
}
}
mung.write = function write (fn, options = {}) {
return function (req, res, next) {
const original = res.write;
const mungError = options.mungError;
function write_hook (chunk, encoding, callback) {
// If res.end has already been called, do nothing.
if (res.finished) {
return false;
}
// Do not mung on errors
if (!mungError && res.statusCode >= 400) {
return original.apply(res, arguments);
}
try {
let modifiedChunk = fn(
chunk,
// Since `encoding` is an optional argument to `res.write`,
// make sure it is a string and not actually the callback.
typeof encoding === 'string' ? encoding : null,
req,
res
);
// res.finished is set to `true` once res.end has been called.
// If it is called in the mung function, stop execution here.
if (res.finished) {
return false;
}
// If no returned value from fn, then set it back to the original value
if (modifiedChunk === undefined) {
modifiedChunk = chunk;
}
return original.call(res, modifiedChunk, encoding, callback)
} catch (err) {
return mung.onError(err, req, res, next);
}
}
res.write = write_hook;
next && next();
}
}
module.exports = mung;