forked from socketstream/socketstream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
127 lines (110 loc) · 3.08 KB
/
index.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
// Generated by CoffeeScript 1.3.3
var app, connect, eventMiddleware, existslib, fileUtils, fs, loadStaticDirs, pathlib, router, settings, staticDirs, staticFiles, transformURL, useAfterStack;
fs = require('fs');
pathlib = require('path');
existslib = process.version.split('.')[1] === '6' && require('path') || require('fs');
connect = require('connect');
fileUtils = require('../utils/file');
router = new (require('./router').Router);
staticDirs = [];
staticFiles = [];
settings = {
"static": {
maxAge: 30 * 24 * 60 * 60 * 1000
}
};
app = connect();
app.prepend = app.use;
useAfterStack = [];
app.append = function() {
var args;
args = Array.prototype.slice.call(arguments);
return useAfterStack.push(args);
};
module.exports = function(root) {
return {
connect: connect,
middleware: app,
router: router,
set: function(newSettings) {
var k, v, _results;
if (typeof newSettings !== 'object') {
throw new Error('ss.http.set() takes an object e.g. {static: {maxAge: 60000}}');
}
_results = [];
for (k in newSettings) {
v = newSettings[k];
_results.push(settings[k] = v);
}
return _results;
},
load: function(staticPath, sessionStore, sessionOptions) {
staticPath = pathlib.join(root, staticPath);
loadStaticDirs(staticPath);
app.use(connect.cookieParser('SocketStream')).use(connect.favicon(staticPath + '/favicon.ico')).use(connect.session({
cookie: {
path: '/',
httpOnly: false,
maxAge: sessionOptions.maxAge
},
store: sessionStore
}));
useAfterStack.forEach(function(m) {
return app.use.apply(app, m);
});
app.use(eventMiddleware).use(connect["static"](staticPath, settings["static"]));
return app;
},
route: function(url, fn) {
if (fn) {
return router.on(url, fn);
} else {
return {
serveClient: function(name) {
var cb;
cb = function(req, res) {
return res.serveClient(name);
};
return router.on(url, cb);
}
};
}
}
};
};
eventMiddleware = function(req, res, next) {
var initialDir;
initialDir = req.url.split('/')[1];
if (initialDir === '_serveDev') {
req.url = transformURL(req.url);
}
if (staticDirs.indexOf(initialDir) >= 0 || !router.route(req.url, req, res)) {
return next();
}
};
transformURL = function(url) {
var i, x, _i;
i = 0;
for (x = _i = 0; _i <= 1; x = ++_i) {
i = url.indexOf('/', i + 1);
}
if (url[i] === '/') {
url = url.replace('?', '&');
url = url.substr(0, i) + '?' + url.substr(i + 1);
}
return url;
};
loadStaticDirs = function(path) {
var pathLength;
if (existslib.existsSync(path)) {
staticDirs = fs.readdirSync(path);
if (!(staticDirs.indexOf('assets') >= 0)) {
staticDirs.push('assets');
}
pathLength = path.length;
staticFiles = fileUtils.readDirSync(path).files;
return staticFiles = staticFiles.map(function(file) {
return file.substr(pathLength);
});
}
};