-
Notifications
You must be signed in to change notification settings - Fork 8
/
static-here.js
175 lines (158 loc) · 4.61 KB
/
static-here.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
167
168
169
170
171
172
173
174
var fs = require('fs'),
http = require('http'),
path = require('path'),
url = require('url'),
EventEmitter = require('events').EventEmitter,
exec = require('child_process').exec,
spawn = require('child_process').spawn;
var args = {},
argv = process.argv.slice(2);
// Guess arguments.
for (var i = 0; i < argv.length; i++){
arg = argv[i];
if (arg.match(/^\d+$/)){
args.port = arg;
} else if (arg === 'coffee'){
args.coffee = true;
} else if (arg === 'fix'){
args.fix = true;
} else {
args.host = arg;
}
}
// Emulate mime if it didn't exist.
var mime;
try {
mime = require('mime');
} catch (e) {
mime = (function () {
var CONTENT_TYPES = {
'js': 'application/javascript; charset=utf-8',
'css': 'text/css; charset=utf-8',
'json': 'application/json; charset=utf-8',
'html': 'text/html; charset=utf-8',
'htm': 'text/html; charset=utf-8',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'ico': 'image/x-icon',
'gif': 'image/gif',
'txt': 'text/plain; charset=utf-8',
'svg': 'image/svg+xml',
'ttf': 'application/x-font-ttf',
'woff': 'application/font-woff',
'apk': 'application/vnd.android.package-archive'
};
return {
lookup: function (ext) {
ext = ext.trim();
if (ext[0] === '.') ext = ext.slice(1);
return CONTENT_TYPES[ext] || 'application/octet-stream';
}
}
})();
}
// Simple http response.
function httpRespond(res, code, txt, headers) {
headers = headers || {};
txt = txt || '';
headers['Content-Type'] = "text/plain";
res.writeHead(code, headers);
res.end(txt);
}
var httpCb = function (req, res) {
var uri = url.parse(req.url).pathname;
if (path.normalize(decodeURIComponent(uri)) !== decodeURIComponent(uri)) {
res.statusCode = 403;
res.end();
return;
}
var filename;
try {
filename = decodeURIComponent(path.join(process.cwd(), uri));
} catch (e) {
filename = path.join(process.cwd(), uri);
};
(path.exists || fs.exists)(filename, function (exists) {
if (!exists || /manifest.appcache/.test(filename)) {
httpRespond(res, 404, "Page Not Found!\n");
return;
}
if (fs.statSync(filename).isDirectory()) {
if (filename.slice(-1) !== '/') {
// Directory with out a trailing slash.
// redirect http://host/directory to http://host/directory/
httpRespond(res, 302, 'Location is a folder, redirecting..', {
'Location': uri + '/'
});
return;
} else {
filename = path.join(filename, 'index.html');
}
}
fs.readFile(filename, 'binary', function (err, file) {
if (err) {
httpRespond(res, 500, err + '\n');
return;
}
var ext = path.extname(filename).slice(1);
res.writeHead(200, {'Content-Type': mime.lookup(ext)});
res.write(file, 'binary');
res.end();
});
});
};
// Assign defaults and define the start server action.
args.port = args.port || 8888;
args.host = args.host || '0.0.0.0';
var startServer = function () {
http.createServer(httpCb).listen(args.port, args.host);
console.log('Serving files from %s at http://%s:%s/', process.cwd(), args.host, args.port);
};
// Check the coffee flag to start watching files.
var coffee;
if (args.coffee) {
try {
coffee = require('coffee-script');
} catch (e) {}
if (coffee) {
exec("find . -name '*.coffee'", function (err, files) {
if (err) throw err;
files = files.split(/\n/).filter(function (file) {
return !!(file.trim().length);
});
startWatching(files);
});
}
}
// Watch an array of coffee files.
var startWatching = function (files) {
if (!files.length) {
startServer();
return;
}
// Compile a single coffee file.
var compileCoffee = function (filename) {
console.log("Compiling %s", filename);
var coffee_src = fs.readFileSync(filename, 'utf8'),
js_src;
try{
js_src = coffee.compile(coffee_src);
} catch(e) {
console.log("Error compiling %s : %s", filename, e.message);
return;
}
fs.writeFileSync(filename.replace(/\.coffee$/, '.js'), js_src);
};
// initial compile pass and watch files.
files.forEach(function (filename) {
compileCoffee(filename);
console.log("Watching %s", filename);
fs.watchFile(filename, {interval:args.fix ? -1 : 0}, function (curr, old) {
if (+curr.mtime != +old.mtime) compileCoffee(filename);
});
startServer();
});
};
// The coffee feature will take care of starting the server.
if (!args.coffee) startServer();