Repository navigation
Expand file tree
/
Copy pathconnect-inject.js
More file actions
175 lines (147 loc) · 4.89 KB
/
Copy pathconnect-inject.js
File metadata and controls
175 lines (147 loc) · 4.89 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// https://github.com/danielhq/connect-inject
// Copyright (c) 2013 danielhq
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
const { StringDecoder } = require("string_decoder");
module.exports = function inject(opt) {
// options
var opt = opt || {};
var include = opt.include || "";
var html = opt.html || _html;
var rules = opt.rules || [{
match: /<\/body>/,
fn: prepend
}, {
match: /<\/html>/,
fn: prepend
}, {
match: /<\!DOCTYPE.+>/,
fn: append
}];
var snippetBuilder = function(snippet) {
if (snippet) {
if (snippet instanceof Array) {
return snippet.join("");
} else {
return snippet;
}
} else {
return "";
}
};
var snippet = snippetBuilder(opt.snippet);
var runAll = opt.runAll || false;
// helper functions
var regex = (function() {
var matches = rules.map(function(item) {
return item.match.source;
}).join('|');
return new RegExp(matches);
})();
function prepend(w, s) {
return s + w;
}
function append(w, s) {
return w + s;
}
function _html(str) {
if (!str) return false;
return /<[:_-\w\s\!\/\=\"\']+>/i.test(str);
}
function exists(body) {
if (!body) return false;
return regex.test(body);
}
function snip(body) {
if (!body) return false;
return (~body.lastIndexOf("/livereload.js"));
}
function snap(body) {
var _body = body;
rules.some(function(rule) {
if (rule.match.test(_body)) {
_body = _body.replace(rule.match, function(w) {
return rule.fn(w, rule.snippet|| snippet);
});
if (runAll === false) {
return true;
} else {
return false;
}
}
return false;
});
return _body;
}
function accept(req) {
var ha = req.headers["accept"];
if (!ha) return false;
return (~ha.indexOf("html"));
}
function leave(req) {
var url = req.url.toLowerCase();
var ignored = false;
if (!url) return true;
if(url.toLowerCase().indexOf(include) === -1){
ignored = true;
}
return ignored;
}
// middleware
return function inject(req, res, next) {
if (res.inject) return next();
var writeHead = res.writeHead;
var write = res.write;
var end = res.end;
var decoder = new StringDecoder("utf8");
if (!accept(req) || leave(req)) {
return next();
}
function restore() {
res.writeHead = writeHead;
res.write = write;
res.end = end;
}
res.push = function(chunk) {
res.data = (res.data || '') + chunk;
};
res.inject = res.write = function(string, encoding) {
if (string !== undefined) {
var body = string instanceof Buffer ? decoder.write(string) : string;
if (exists(body) && !snip(res.data)) {
res.push(snap(body));
return true;
} else if (html(body) || html(res.data)) {
res.push(body);
return true;
} else {
restore();
return write.call(res, string, encoding);
}
}
return true;
};
res.writeHead = function() {};
res.end = function(string, encoding) {
restore();
var result = res.inject(string, encoding);
if (!result) return end.call(res, string, encoding);
if (res.data !== undefined && !res._header) res.setHeader('content-length', Buffer.byteLength(res.data, encoding));
res.end(res.data, encoding);
};
next();
};
}