-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (130 loc) · 3.31 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
'use strict';
const Stream = require('stream');
const Response = require('./response');
const { STATUS_CODES } = require('http');
/**
* [short-hands]
* @type {Object}
*/
const CONTENT_TYPES = {
text : 'text/plain' ,
html : 'text/html' ,
xml : 'application/xml' ,
xhtml: 'application/xhtml+xml',
json : 'application/json' ,
png : 'image/png' ,
mpeg : 'video/mpeg' ,
webp : 'image/webp' ,
};
/**
* [function description]
* @param {[type]} req [description]
* @param {[type]} res [description]
* @param {Function} next [description]
* @return {[type]} [description]
*/
function send(req, res, next){
/**
* [type description]
* @param {[type]} type [description]
* @param {[type]} options [description]
* @return {[type]} [description]
*/
res.type = function(type, options){
type = CONTENT_TYPES[ type ] || type;
options = Object.keys(options || {}).map(function(name){
return [ name, options[ name ] ].join('=');
}).join('; ');
res.setHeader('Content-Type', type + (options ? ('; ' + options) : ''));
return res;
};
/**
* [header description]
* @param {[type]} name [description]
* @param {[type]} value [description]
* @return {[type]} [description]
*/
res.header = function(name, value){
res.setHeader(name, value);
return res;
};
/**
* [status description]
* @param {[type]} code [description]
* @return {[type]} [description]
*/
res.status = function(code){
this.statusCode = code;
return res;
};
/**
* [redirect description]
* @param {[type]} url [description]
* @param {[type]} code [description]
* @param {[type]} headers [description]
* @return {[type]} [description]
*/
res.redirect = function(url, code){
this.status(code || 302);
this.header('Location', url);
return res.end();
};
/**
* [send description]
* @param {[type]} body [description]
* @return {[type]} [description]
*/
res.send = body => {
var type = ({})
.toString
.call(body)
.match(/^\[object (\w+)\]$/)[1]
.toLowerCase();
if(type === 'object' && body.then)
type = 'promise';
if(type === 'object' && (body instanceof Stream))
type = 'stream';
if(type === 'object' && (body instanceof Buffer))
type = 'buffer';
if(type === 'object' && (body instanceof Response))
type = 'response';
switch(type){
case 'number':
res.status(body);
res.send(STATUS_CODES[body]);
break;
case 'function':
body = body(req, res);
body && res.send(body);
break;
case 'error':
res.send(body.stack);
break;
case 'promise':
body.then(res.send, next);
break;
case 'stream':
body.pipe(res);
break;
case 'array':
case 'object':
res.type('json');
res.send(JSON.stringify(body));
break;
case 'string':
case 'buffer':
case 'uint8array':
res.end(body);
break;
case 'response':
body.respond(req, res);
break;
default:
res.end();
break;
}
return res;
};
return next();
};
module.exports = send;