-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
166 lines (135 loc) · 3.73 KB
/
utils.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
const proxyaddr = require('proxy-addr');
const Buffer = require('safe-buffer').Buffer
const contentType = require('content-type');
const { mime } = require('send');
const env = process.env.NODE_ENV || 'local'
const onFinished = require('on-finished');
exports.isAbsolute = function(path) {
if ('/' === path[0]) return true;
if (':' === path[1] && ('\\' === path[2] || '/' === path[2])) return true;
if ('\\\\' === path.substring(0, 2)) return true;
};
exports.normalizeType = function(type) {
return ~type.indexOf('/') ?
acceptParams(type) : {
value: mime.lookup(type),
params: {}
};
};
exports.normalizeTypes = function(types) {
var ret = [];
for (var i = 0; i < types.length; ++i) {
ret.push(exports.normalizeType(types[i]));
}
return ret;
};
exports.setCharset = function setCharset(type, charset) {
if (!type || !charset) {
return type;
}
var parsed = contentType.parse(type);
parsed.parameters.charset = charset;
return contentType.format(parsed);
};
exports.compileTrust = function(val) {
if (typeof val === 'function') return val;
if (val === true) {
return function() {
return true
};
}
if (typeof val === 'number') {
return function(a, i) {
return i < val
};
}
if (typeof val === 'string') {
val = val.split(/ *, */);
}
return proxyaddr.compile(val || []);
}
exports.stringify = function stringify(value, replacer, spaces, escape) {
var json = replacer || spaces ?
JSON.stringify(value, replacer, spaces) :
JSON.stringify(value);
if (escape) {
json = json.replace(/[<>&]/g, function(c) {
switch (c.charCodeAt(0)) {
case 0x3c:
return '\\u003c'
case 0x3e:
return '\\u003e'
case 0x26:
return '\\u0026'
default:
return c
}
})
}
return json
}
exports.sendfile = function sendfile(res, file, options, callback) {
var done = false;
var streaming;
function onaborted() {
if (done) return;
done = true;
var err = new Error('Request aborted');
err.code = 'ECONNABORTED';
callback(err);
}
function ondirectory() {
if (done) return;
done = true;
var err = new Error('EISDIR, read');
err.code = 'EISDIR';
callback(err);
}
function onerror(err) {
if (done) return;
done = true;
callback(err);
}
function onend() {
if (done) return;
done = true;
callback();
}
function onfile() {
streaming = false;
}
function onfinish(err) {
if (err && err.code === 'ECONNRESET') return onaborted();
if (err) return onerror(err);
if (done) return;
setImmediate(function() {
if (streaming !== false && !done) {
onaborted();
return;
}
if (done) return;
done = true;
callback();
});
}
function onstream() {
streaming = true;
}
file.on('directory', ondirectory);
file.on('end', onend);
file.on('error', onerror);
file.on('file', onfile);
file.on('stream', onstream);
onFinished(res, onfinish);
if (options.headers) {
file.on('headers', function headers(res) {
var obj = options.headers;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
res.setHeader(k, obj[k]);
}
});
}
file.pipe(res);
}