forked from cdnjs/new-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiServer.js
executable file
·160 lines (146 loc) · 5.81 KB
/
apiServer.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
#!/usr/bin/env node
var fs = require('fs');
var express = require('express');
var algoliasearch = require("algoliasearch/lite");
var _ = require('lodash');
var app = express();
var args = process.argv.slice(2);
var localMode = false;
var compress = require('compression');
var bodyParser = require('body-parser');
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
next();
};
if (process.env.LOCAL === 'true' || (args.length > 0 && (args[0] === '--local' || args[2] === '--local'))) {
console.log("local mode: on, gc() disabled!");
localMode = true;
} else {
console.log("local mode: off");
}
app.disable('x-powered-by');
app.use(bodyParser());
app.use(allowCrossDomain);
app.use(compress());
function humanOutput(res, json) {
res.header('Content-Type', 'text/html');
var htmlOutput = '<!doctype><html>' +
'<head><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/default.min.css" integrity="sha256-WfR+t4V9jUEtco303JmeVqRbf/++XunklhaJkoTp8u0=" crossorigin="anonymous"/></head><body>' +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js" integrity="sha256-fkOAs5tViC8MpG+5VCOqdlSpLL8htz4mdL2VZlWGoMA=" crossorigin="anonymous"></script>' +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/languages/json.min.js" integrity="sha256-D0YfZN5nP+2bF+7odQ7OaQcJXqMhax4a4sOYrZPf32k=" crossorigin="anonymous" defer></script>' +
'<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json2/20160511/json2.min.js" integrity="sha256-Fsw5X9ZUnlJb302irkG8pKCRwerGfxSArAw22uG/QkQ=" crossorigin="anonymous"></script>' +
'<script defer>hljs.initHighlightingOnLoad();</script>' +
'<script defer>var output=' + JSON.stringify(json) + '; ' +
'document.write("<pre><code class=\'json\'>" + JSON.stringify(output,null,2) + "</code></pre>");</script>' +
'<script defer>console.log("%cThanks for using CDNJS! 😊", "font: 5em roboto; color: #dd4814;");</script>' +
'</body></html>';
res.write(htmlOutput);
res.end();
htmlOutput = null;
}
var packages = JSON.parse(fs.readFileSync('public/packages.min.json', 'utf8')).packages;
// build an indexed version of the packages (speed up lookup)
var packagesByName = {};
_.each(packages, function(library) {
packagesByName[library.name] = library;
});
packages = null;
var algoliaIndex = algoliasearch('2QWLVLXZB6', '2663c73014d2e4d6d1778cc8ad9fd010').initIndex('libraries');
if (!localMode && (typeof global.gc !== 'undefined')) {
app.use(function(req, res, next) {
res.setHeader('Public-Key-Pins', 'pin-sha256="EULHwYvGhknyznoBvyvgbidiBH3JX3eFHHlIO3YK8Ek=";pin-sha256="x9SZw6TwIqfmvrLZ/kz1o0Ossjmn728BnBKpUFqGNVM=";max-age=3456000;report-uri="https://cdnjs.report-uri.io/r/default/hpkp/enforce"');
next();
});
global.gc();
}
app.get('/libraries', function(req, res) {
var results;
app.set('json spaces', 0);
// format the results including optional `fields`
function formatResults(fields, packagesByName) {
return _.map(packagesByName, function(library) {
var data = {
name: library.name,
latest: 'https://cdnjs.cloudflare.com/ajax/libs/' + library.name + '/' + library.version + '/' + library.filename
};
_.each(fields, function(field) {
data[field] = library[field] || null;
});
return data;
});
}
res.setHeader("Expires", new Date(Date.now() + 360 * 60 * 1000).toUTCString());
var fields = (req.query.fields && req.query.fields.split(',')) || [];
if (req.query.search) {
var searchParams = {
typoTolerance: 'min', // only keep the minimum typos
hitsPerPage: 1000 // maximum
};
algoliaIndex.search(req.query.search, searchParams, function(error, content) {
if (error) {
res.status(500).send(error.message);
return;
}
// fetch the orignal version of the package based on the search hit
results = _.map(content.hits, function(hit) {
return packagesByName[hit.originalName] || hit;
});
var json = {
results: formatResults(fields, results),
total: content.hits.length
};
if (req.query.output && req.query.output === 'human') {
humanOutput(res, json);
} else {
res.jsonp(json);
}
});
} else {
results = formatResults(fields, packagesByName);
var json = {
results: results,
total: results.length
};
if (req.query.output && req.query.output === 'human') {
humanOutput(res, json);
} else {
res.jsonp(json);
}
}
});
app.get('/libraries/:library', function(req, res) {
var results;
var fields = (req.query.fields && req.query.fields.split(',')) || false;
var ret = {};
app.set('json spaces', 0);
res.setHeader("Expires", new Date(Date.now() + 360 * 60 * 1000).toUTCString());
results = _.filter(packagesByName, function(library) {
if (library.name === req.params.library) {
return library;
}
return false;
});
if (fields && results.length > 0) {
_.each(fields, function(field) {
ret[field] = results[0][field] || null;
});
results[0] = ret;
}
if (results.length > 0) {
if (req.query.output && req.query.output === 'human') {
humanOutput(res, results[0]);
} else {
res.jsonp(results[0]);
}
} else {
res.jsonp({});
}
});
app.get('/', function(req, res) {
res.redirect('https://cdnjs.com/api');
});
var port = process.env.PORT || 5050;
app.listen(port);