-
Notifications
You must be signed in to change notification settings - Fork 142
/
index.js
136 lines (124 loc) · 3.71 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
128
129
130
131
132
133
134
135
136
var t0 = Date.now();
var bibref = require('./lib/bibref');
var app = module.exports = require("express")();
var errorhandlerOptions = { log: true };
if (process.env.NODE_ENV == "dev" || process.env.NODE_ENV == "development") {
errorhandlerOptions.dumpExceptions = true;
errorhandlerOptions.showStack = true;
}
app.enable("etag");
var bannedIPs = [
// Palo Alto Networks bot
"34.96.130.0/24", "34.77.162.0/24", "34.86.35.0/24"
];
app.use(require('express-ipfilter').IpFilter(bannedIPs, { logLevel: "deny" }));
app.use(require("compression")());
app.use(require("cors")());
app.use(require("body-parser").urlencoded({ extended: true }));
app.use(require("errorhandler")(errorhandlerOptions));
// bibrefs
app.get('/bibrefs', function (req, res, next) {
var refs = req.query["refs"];
res.setHeader("Expires", new Date(Date.now() + 86400000).toUTCString());
res.setHeader("Cache-Control", "public, max-age=86400");
if (refs) {
refs = bibref.getRefs(refs.split(","));
res.status(200).jsonp(refs);
} else {
res.status(200).jsonp(bibref.all);
}
});
// search
app.get('/search-refs', function (req, res, next) {
var q = (req.query["q"] || "").toLowerCase();
if (q) {
var obj = {};
var current, shortname;
var FALSE_POSITIVES = /https?:\/\/|\.html|\.shtml|\.xhtml|\/html/g;
function match(str) {
str = str.toLowerCase() || "";
str = str.replace(FALSE_POSITIVES, "");
return str.indexOf(q) > -1;
}
function add() {
obj[shortname] = current;
}
var all = bibref.all;
for (shortname in all) {
current = all[shortname];
if (match(shortname)) {
add();
if (current.aliasOf) {
var r = bibref.get(current.aliasOf);
var k = current.aliasOf;
while (k) {
obj[k] = r[k];
k = obj[k].aliasOf;
}
}
} else if (typeof current == "string") { // legacy
if (match(current)) { add(); }
} else if (!("aliasOf" in current)) {
for (var key in current) {
var value = current[key];
if (typeof value == "string") {
if (match(value)) { add(); }
} else if (Array.isArray(value)) {
value.forEach(function(item) {
if (typeof item == "string") {
if (match(item)) { add(); }
} else {
for (var prop in item) {
if (match(item[prop])) { add(); }
}
}
});
}
}
}
}
res.status(200).jsonp(obj);
} else {
res.status(400).jsonp({ message: "Missing q parameter" });
}
});
// search by url
app.get('/reverse-lookup', function (req, res, next) {
var refs,
urls = req.query["urls"];
if (urls) {
refs = bibref.reverseLookup(urls.split(","));
res.status(200).jsonp(refs);
} else {
res.status(400).jsonp({ message: "Missing urls parameter" });
}
});
var metadata = (function(pkg) {
var all = bibref.all;
var ids = Object.keys(all);
var refCount = 0;
ids.forEach(function(id) {
var ref = all[id];
if (typeof ref == "string" || !("aliasOf" in ref)) refCount++;
});
return {
name: pkg.name,
version: pkg.version,
refCount: refCount,
aliasCount: ids.length - refCount,
startupTime: new Date()
};
})(require("./package.json"));
app.get('/metadata', function (req, res, next) {
metadata.runningFor = new Date() - metadata.startupTime;
res.status(200).jsonp(metadata);
});
// xrefs
app.get('/xrefs', function (req, res, next) {
res.status(410).jsonp({ message: "xrefs are no longer supported." });
});
var port = process.env.PORT || 5000;
app.listen(port, function () {
console.log("Express server listening on port %d in %s mode", port, app.settings.env);
console.log("App started in", (Date.now() - t0) + "ms.");
});