-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
108 lines (86 loc) · 2.98 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
"use strict";
// Constants
var EXPANDED_URL_HEADER = "X-Expanded-Url";
// Modules
var mime = require( "mime" );
var express = require( "express" );
var compression = require( "compression" );
var onFinished = require( "on-finished" );
var uuid = require( "uuid" );
// Package modules
var plook = require( "./lib/plook" );
var utils = require( "./lib/utils" );
// -------------------------------------------------------------------------------------------------
var app = express();
// Set the default mime type to an object, so we know that it didn't found anything that could
// conflict with a valid mimetype.
mime.default_type = {};
app.use( compression({
threshold: 1024
}));
app.use(function( req, res, next ) {
var start = Date.now();
var id = uuid.v4();
req.plook = plook.branch({
id: id
});
res.set( "X-Request-Id", id );
onFinished( res, function() {
req.plook.logger.server(
"%d - %s %s - %d ms",
res.statusCode,
req.method,
req.url,
Date.now() - start
);
});
next();
});
app.route( "/:package/:version/*" ).get(function( req, res ) {
req.plook.get(
req.params.package,
req.params.version,
req.params[ 0 ],
req.get( "if-none-match" )
).then(function( result ) {
var etag = result.response.headers.etag;
var type = findMimetype( result.url, result.response );
res.statusCode = result.response.statusCode;
res.set( "Access-Control-Allow-Origin", "*" );
res.set( "Content-Type", type );
res.set( "ETag", etag );
res.set( EXPANDED_URL_HEADER, result.url );
result.response.pipe( res );
}, function( err ) {
send( res, err );
});
});
app.route( "/*" ).all(function( req, res ) {
send( res, utils.createHttpError( 404, "Not found" ) );
});
app.listen( process.env.PORT || 3000, function() {
plook.logger.server( "Listening on port " + this.address().port );
});
// -------------------------------------------------------------------------------------------------
function findMimetype( url, ghResp ) {
var ghContentType = ghResp.headers[ "content-type" ] || "";
var type = mime.lookup( url );
// If the defaule mime type was used, this means we should rely on GitHub and return their
// content-type (which is usually plain text or octet-stream).
// Otherwise, we simply use the mime module returned value.
if ( type === mime.default_type ) {
type = ghContentType;
} else {
// If a charset is available from the GitHub response,
// we'll use UTF-8 to the returned string as well. GitHub only sends UTF-8 responses.
type += ~ghContentType.indexOf( "charset" ) ? "; charset=UTF-8" : "";
}
return type;
}
function send( res, err ) {
res.set( "Content-Type", "text/plain" );
if ( err.url ) {
res.set( EXPANDED_URL_HEADER, err.url );
}
return res.status( err.status ).send( err.message );
}