Skip to content

Commit

Permalink
Use express 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroen committed Oct 19, 2024
1 parent 2fe3a1f commit ef98aba
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ app.use(function(req, res, next) {
next(createError(404));
});

// error handler
// general error handler.
// For example if the router does throw "error" then err below is "error"
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.type("text/plain").send(err.message || "CDN lookup error");
res.type("text/plain").send(err.message || err || "CDN lookup error");
});

export default app;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.1",
"express": "^5.0.1",
"gunzip-maybe": "^1.4.2",
"http-errors": "^2.0.0",
"mongodb": "^6.9.0",
Expand Down
10 changes: 6 additions & 4 deletions routes/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,18 @@ function error_cb(status, next) {
}
}

router.get("/cdn/:hash/:file?", function(req, res, next) {
router.get("/cdn/:hash{/:file}", function(req, res, next) {
let hash = req.params.hash || "";
let file = req.params.file || "send";
if(hash.length != 32 && hash.length != 64) //assume md5 for now
if(hash.length != 32 && hash.length != 64) //can be md5 or sha256
return next(createError(400, "Invalid hash length"));
send_from_bucket(hash, file, res).catch(error_cb(400, next));
return send_from_bucket(hash, file, res);
});

/* index all the files, we have nothing to hide */
router.get("/", function(req, res, next) {
next(createError(400, "Invalid CDN req: " + req.url));
var cursor = bucket.find({}, {sort: {_id: 1}, project: {_id: 1, filename: 1}});
return cursor.stream({transform: x => `${x._id} ${x.filename}\n`}).pipe(res.type('text/plain'));
});

export default router;

0 comments on commit ef98aba

Please sign in to comment.