Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

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 ac41327
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 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
18 changes: 10 additions & 8 deletions routes/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function send_from_bucket(hash, operation, res){

if(operation == 'index'){
if(!name.endsWith('gz'))
throw `Unable to index ${name} (only tar.gz files are supported)`;
throw createError(500, `Unable to index ${name} (only tar.gz files are supported)`);
return tar_index_files(stream_file(pkg)).then(function(index){
index.files.forEach(function(entry){
entry.filename = entry.filename.match(/\/.*/)[0]; //strip pkg root dir
Expand All @@ -84,7 +84,7 @@ function send_from_bucket(hash, operation, res){

if(operation == 'decompress'){
if(!name.endsWith('gz'))
throw `Unable to decompress ${name} (only tar.gz files are suppored)`;
throw createError(`Unable to decompress ${name} (only tar.gz files are suppored)`);
var tarname = name.replace(/(tar.gz|tgz)/, 'tar');
return stream_file(pkg).pipe(gunzip()).pipe(
res.type('application/tar').attachment(tarname).set({
Expand All @@ -94,7 +94,7 @@ function send_from_bucket(hash, operation, res){
);
}

throw `Unsuppored operation ${operation}`;
throw createError(500, `Unsuppored operation ${operation}`);
});
}

Expand All @@ -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);
});

router.get("/", function(req, res, next) {
next(createError(400, "Invalid CDN req: " + req.url));
/* index all the files, we have nothing to hide */
router.get("/cdn", function(req, res, next) {
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 ac41327

Please sign in to comment.