forked from marijnh/UglifyJS-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
150 lines (139 loc) · 4.94 KB
/
serve.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
var sys = require("sys"),
http = require("http"),
https = require("https"),
fs = require("fs"),
path = require("path"),
qs = require("querystring"),
url_parse = require("url").parse,
ujs = require("uglify-js"),
config = require("./config"),
Mold = require("mold-template");
ujs.AST_Node.warn_function = null;
var ujsversion = JSON.parse(fs.readFileSync(path.normalize(__dirname +"/node_modules/uglify-js/package.json"), "utf8")).version;
function forEachIn(obj, f) {
var hop = Object.prototype.hasOwnProperty;
for (var n in obj) if (hop.call(obj, n)) f(n, obj[n]);
}
function parseQuery(query) {
var parsed = qs.parse(query);
forEachIn(parsed, function(prop, val) {
if (!Array.isArray(val)) parsed[prop] = [val];
});
return parsed;
}
function queryVal(query, name) {
return query.hasOwnProperty(name) ? query[name][0] : null;
}
var templates = {};
function template(name) {
if (templates.hasOwnProperty(name)) return templates[name];
return templates[name] = Mold.bake(fs.readFileSync(path.normalize(__dirname +"/templates/" + name), "utf8"));
}
function uglify(files, ascii_only, source_map) {
var parsed = null, allNamed = true, opts = {ascii_only: ascii_only};
files.forEach(function(file) {
parsed = ujs.parse(file.string, {filename: file.name, toplevel: parsed});
if (!file.name) allNamed = false;
});
if (!parsed) return "";
if (source_map) {
if (!allNamed) throw new Error("Source maps only work when all files are URLs");
opts.source_map = ujs.SourceMap({});
}
var compressor = ujs.Compressor({});
parsed.figure_out_scope();
parsed.transform(compressor);
parsed.mangle_names();
var output = ujs.OutputStream(opts);
parsed.print(output);
return source_map ? opts.source_map.toString() : output.get();
}
function readData(obj, c) {
var received = [];
obj.setEncoding("utf8");
obj.addListener("data", function(chunk) {received.push(chunk);});
obj.addListener("end", function() {c(received.join(""));});
}
http.createServer(function(req, resp) {
var question = req.url.indexOf("?");
var query = question == -1 ? {} : parseQuery(req.url.slice(question + 1));
if (req.method == "POST" && req.headers["content-type"] == "application/x-www-form-urlencoded") {
readData(req, function(data) {
forEachIn(parseQuery(data), function(name, val) {
var current = query.hasOwnProperty(name) && query[name];
query[name] = current ? current.concat(val) : val;
});
respond(query, resp);
});
} else {
respond(query, resp);
}
}).listen(config['port'], config['host']);
function gatherCode(direct, urls, c) {
var files = [], todo = urls.length + 1;
function done() {
if (--todo == 0) {
if (direct) files.push({string: direct});
c(files.filter(function(x){return x;}));
}
}
function handle(url, i, redir) {
var chunks = [];
var req = (/^https:/.test(url) ? https : http).request(url_parse(url), function(resp) {
if (resp.statusCode < 300) {
resp.on("data", function(chunk) {chunks.push(chunk);});
resp.on("end", function() {
files[i] = {string: chunks.join(""), name: url};
done();
});
resp.on("error", done);
} else if (resp.statusCode < 400 && redir < 10 && resp.headers.location) {
handle(resp.headers.location, i, redir + 1);
} else done();
});
req.on("error", function(e) { console.log(e); done(); });
req.end();
}
urls.forEach(function(url, i) {
if (/^https?:/.test(url)) handle(url, i, 0);
else done();
});
done();
}
function respond(query, resp) {
var direct = queryVal(query, "js_code"), urls = query.code_url || [],
ascii_only = typeof queryVal(query, "utf8") != "string",
source_map = typeof queryVal(query, "source_map") == "string";
gatherCode(direct, urls, function(files) {
try { var output = uglify(files, ascii_only, source_map); }
catch(e) { var error = e.message || e.msg; }
if (queryVal(query, "form") == "show" || !files.length) {
var totalLen = files.reduce(function(cur, f) {return cur + f.string.length;}, 0);
respondHTML(direct, urls, totalLen, ascii_only, output, error, resp);
} else {
respondDirect(queryVal(query, "download"), output, error, resp);
}
});
}
function respondHTML(direct, urls, totalLen, ascii, output, error, resp) {
resp.writeHead(200, {"Content-Type": "text/html"});
resp.write(template("body")({
code: direct,
urls: urls, old_size: totalLen, ascii_only: ascii,
mini: output, error: error,
version: ujsversion
}));
resp.end();
}
function respondDirect(download, output, error, resp) {
if (error) {
resp.writeHead(400, {"Content-Type": "text/html"});
resp.write(template("failed")(error));
} else {
var headers = {"Content-Type": "text/javascript"};
if (download) headers["Content-Disposition"] = "attachment; filename=" + download;
resp.writeHead(200, headers);
resp.write(output);
}
resp.end();
}