-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
241 lines (199 loc) · 7.07 KB
/
server.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
var opbeat = require('opbeat').start();
var root = require('root');
var pejs = require('pejs');
var send = require('send');
var cookie = require('cookie');
var param = require('param');
var fs = require('fs');
var req = require('request');
var qs = require('querystring');
var JSONStream = require('JSONStream');
var pump = require('pump');
var modules = require('./modules');
var users = require('./users');
var update = require('./update');
var COOKIE_MAX_AGE = 31 * 24 * 3600 * 1000; // 1 month
var FINGERPRINT_MAX_AGE = 365 * 24 * 3600;
var FINGERPRINT = param('fingerprint') && param('fingerprint').toString().trim();
var app = root();
var views = pejs({
compress: true
});
var string = function(str) {
return str ? str+'' : '';
};
var fingerprint = function(url) {
return FINGERPRINT ? 'http://dzdv0sfntaeum.cloudfront.net/'+FINGERPRINT+url : url;
};
app.use('response.render', function(filename, locals) {
var response = this;
locals = locals || {};
locals.anon = !this.request.username;
locals.username = locals.anon ? '' : this.request.username;
locals.fingerprint = fingerprint;
locals.query = string(this.request.query.q);
views.render(filename, locals, function(err, html) {
if (err) return response.error(err);
response.send(html);
});
});
app.use('request.search', function(callback) {
var query = string(this.query.q);
var marker = string(this.query.marker);
var limit = Math.min(parseInt(this.query.limit, 10) || 20, 50);
users.search(this.username, query, {marker:marker, limit:limit}, callback);
});
app.on('route', function(request, response) {
if (request.headers.host === 'blog.node-modules.com') return response.redirect('http://reddit.com/r/node_modules');
var setCookie = true;
var c = cookie.parse(request.headers.cookie || '');
var username = request.query.u || c.username || '';
if (request.query.u === '') username = '';
if (request.headers.host && request.headers.host.indexOf('.node-modules.com') > -1) {
setCookie = false;
username = request.headers.host.split('.')[0];
if (username === 'development' || username === 'www') username = '';
if (username) request.userPage = true;
}
request.username = username = username.toLowerCase();
if (setCookie) response.setHeader('Set-Cookie', cookie.serialize('username', username, {maxAge:COOKIE_MAX_AGE}));
response.setHeader('Access-Control-Allow-Origin', '*');
});
app.on('match', function (request, response, pattern) {
opbeat.setTransactionName(request.method + ' ' + pattern);
});
app.get('/package/{name}.json', function(request, response) {
modules.get(request.params.name, function(err, module) {
if (err) return response.error(err);
response.send(module);
});
});
app.get('/modules.json', function(request, response) {
var cached = new Date(string(request.query.cached) || '1970-01-01');
var query = {cached:{$gte:cached}};
pump(modules.createReadStream(query), JSONStream.stringify(), response);
});
app.get('/update.json', function(request, response) {
var progress = update();
var output = JSONStream.stringify();
response.setHeader('Content-Type', 'application/json; charset=utf-8');
output.pipe(response);
progress.on('module', function(module) {
output.write({type:'module', id:module._id, version:module.version, updated:module.updated});
});
progress.on('user', function(user) {
output.write({type:'user', id:user._id, updates:user.updates});
});
progress.on('end', function() {
output.end();
});
});
app.get('/me.json', function(request, response) {
users.get(request.username, function(err, user) {
if (err) return response.error(err);
response.send(user);
});
});
app.get('/search.json', function(request, response) {
request.search(function(err, results) {
if (err) return response.error(err);
response.send(results);
});
});
app.get('/.json', function(request, response) {
modules.info(function(err, info) {
if (err) return response.error(err);
info.now = new Date();
response.send(info);
});
});
app.get('/public/*', function(request, response) {
send(request, __dirname+'/public/'+request.params.glob).pipe(response);
});
app.get('/{version}/public/*', function(request, response) {
response.setHeader('Expires', new Date(Date.now() + FINGERPRINT_MAX_AGE * 1000).toGMTString());
response.setHeader('Cache-Control', 'public, max-age='+FINGERPRINT_MAX_AGE);
send(request, __dirname+'/public/'+request.params.glob).pipe(response);
});
app.get('/authorize', function(request, response) {
var q = encodeURIComponent(string(request.query.q));
req.post('https://github.com/login/oauth/access_token', {
headers: {
'User-Agent': 'node-modules.com'
},
form: {
client_id: param('github.client'),
client_secret: param('github.secret'),
code: request.query.code
}
}, function(err, res, body) {
if (err) return response.error(err);
req('https://api.github.com/user', {
json:true,
headers: {
'User-Agent': 'node-modules.com'
},
qs: {
access_token: qs.parse(body).access_token
}
}, function(err, res) {
if (err) return response.error(err);
if (request.query.f) return response.redirect('http://'+param('host')+'/?u='+string(res.body.login));
response.redirect('http://'+param('host')+'/search?q='+q+'&u='+string(res.body.login));
});
});
});
app.get('/personalize', function(request, response) {
if (!param('github.secret')) return response.error(new Error('github secret is not configured'));
var q = encodeURIComponent(string(request.query.q));
var f = encodeURIComponent(string(request.query.f));
var url = 'https://github.com/login/oauth/authorize?'+qs.stringify({
client_id: param('github.client'),
redirect_uri:'http://'+param('host')+'/authorize?q='+q+'&f='+f
});
response.redirect(url);
});
app.get('/~{username}', function(request, response) {
response.redirect('/search?q='+encodeURIComponent('@'+request.params.username));
});
app.get('/search', function(request, response) {
request.search(function(err, modules) {
if (err) return response.error(err);
response.render(request.query.partial ? 'partials/modules.html' : 'search.html', {modules:modules, userPage: !!request.userPage});
});
});
app.get('/favicon.ico', '/public/favicon.ico');
app.get('/', function(request, response) {
modules.info(function(err, info) {
if (err) return response.error(err);
info.userPage = !!request.userPage;
response.render('index.html', info);
});
});
app.get('/about', function(request, response) {
response.render('about.html');
});
app.get('/mission', function(request, response) {
response.render('mission.html');
});
app.error(404, function(request, response) {
response.render('error.html', {
title:'404 Not Found',
message:'We cannot find the page you are looking for'
});
});
app.error(function(request, response, opt) {
if (opt.error) console.error(opt.error.stack);
response.render('error.html', {
title:'Something bad happened',
message:opt.message || 'Unknown error'
});
});
app.listen(param('port'), function() {
console.log('app running on http://'+param('host'));
if (!param('autoupdate')) return;
var loop = function() {
setTimeout(update.bind(null, loop), 3600*1000);
};
loop() // update every hour
});