-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
162 lines (139 loc) · 4.43 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
'use strict';
let mung = {};
let faux_fin = { end: () => null };
function isScalar(v) {
return typeof v !== 'object' && !Array.isArray(v);
}
mung.onError = (err, req, res) => {
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);
}
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 scalar value, then text/plain
if (isScalar(json)) {
res.set('content-type', 'text/plain');
return res.send(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) {
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 scalar value, then text/plain
if (isScalar(json)) {
res.set('content-type', 'text/plain');
return res.send(json);
}
return original.call(this, json);
})
.catch(e => mung.onError(e, req, res));
} catch (e) {
mung.onError(e, req, res);
}
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);
}
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);
};
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));
} catch (e) {
onError(e, req, res);
}
}
res.end = headers_async_hook;
next && next();
}
}
module.exports = mung;