-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (129 loc) · 3.48 KB
/
index.js
File metadata and controls
157 lines (129 loc) · 3.48 KB
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
/*!
* minify
* Copyright(c) 2014-2015 dmitriym09
* MIT Licensed
*/
const createBuffer = (req, res) => {
if(typeof res._createdMinifyBuffer != 'undefined') {
return res._createdMinifyBuffer
}
res._createdMinifyBuffer = false;
let contentType = res.getHeader('Content-Type');
if(typeof contentType == 'string') {
contentType = contentType.toLowerCase();
}
if(!!contentType) {
const find = options.find((option) => {
const ct = option.contentType;
if(typeof ct == 'string') {
return contentType.indexOf(ct.toLowerCase()) > -1;
}
if(ct instanceof RegExp) {
return ct.test(contentType);
}
throw Error(`Not support ${option}`);
});
if(!!find) {
res.minifyBuffer = new Buffer.from('');
res.minify = find.minify;
if(res.headersSent) {
throw Error('Headers sent')
}
res.removeHeader('Content-Length')
if(typeof res.minify != 'function') {
throw Error(`Error minify ${res.minify}`);
}
res._createdMinifyBuffer = true;
return true;
}
}
}
const addToBuffer = (res, chunk, encoding) => {
if(!(res.minifyBuffer instanceof Buffer)) {
throw Error('minifyBuffer not created');
}
if(!!chunk) {
if(typeof chunk == 'string') {
res.minifyBuffer = Buffer.concat([res.minifyBuffer, new Buffer.from(chunk, encoding)]);
}
else if(chunk instanceof Buffer) {
res.minifyBuffer = Buffer.concat([res.minifyBuffer, chunk]);
}
else {
throw Error(`Unsupport ${chunk}`);
}
}
}
const finish = (req, res, encoding, end, callback) => {
if(!(res.minifyBuffer instanceof Buffer)) {
throw Error('minifyBuffer not created');
}
if(res.finished) {
throw Error('Response finished');
}
if(res.headersSent) {
throw Error('Headers sent')
}
const _ = (minData)=>{
if(typeof minData != 'string') {
throw Error(`Error ${minData}`)
}
let tmpBuf = new Buffer.from(minData, encoding);
res.setHeader('Content-Length', tmpBuf.length)
end.call(res, tmpBuf, encoding, callback);
}
try {
let minify_ = res.minify(res.minifyBuffer.toString(encoding || 'utf-8'), req, res);
if(minify_ instanceof Promise) {
minify_
.then(_)
.catch((err) => {
console.warn(`Exception minify ${err}`);
res.statusCode = 500;
res.setHeader('Content-Length', 0);
res._end.call(res, null, encoding, callback);
});
}
else {
_(minify_);
}
}
catch(exc) {
console.warn(`Exception minify ${exc}`);
res.statusCode = 500;
res.setHeader('Content-Length', 0)
res._end.call(res, null, encoding, callback);
}
}
let options = [];
const middle = (req, res, next) => {
const _write = res.write;
res.write = (chunk, encoding, callback) => {
if(createBuffer(req, res)) {
addToBuffer(res, chunk, encoding)
if(typeof callback == 'function') callback();
}
else {
_write.call(res, chunk, encoding, callback);
}
};
const _end = res.end;
res._end = _end;
res.end = (data, encoding, callback) => {
if(createBuffer(req, res)) {
addToBuffer(res, data, encoding)
finish(req, res, encoding, _end, callback)
}
else {
_end.call(res, data, encoding, callback);
}
};
next();
}
/**
* Initialization middleware,
* hint write to client and store data
* @param {Array}
* @return {Function}
*/
module.exports = (options_) => {options = options_; return middle};