From 670f8558b09178ac7af075f15191db2eef66b5dc Mon Sep 17 00:00:00 2001 From: Jeroen Ooms Date: Wed, 5 Jun 2024 22:26:40 +0200 Subject: [PATCH] Try to find packages in alternative locations --- app.js | 1 + package.json | 2 +- src/db.js | 22 +++++++++++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 4a13195..8c2087a 100644 --- a/app.js +++ b/app.js @@ -59,6 +59,7 @@ app.use(function(err, req, res, next) { // render the error page res.status(err.status || 500); + res.header(err.headers); res.render('error'); }); diff --git a/package.json b/package.json index 9336d98..b711547 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-frontend", - "version": "1.0.2", + "version": "1.0.3", "scripts": { "start": "node ./bin/www" }, diff --git a/src/db.js b/src/db.js index d704073..a77b8a3 100644 --- a/src/db.js +++ b/src/db.js @@ -107,9 +107,25 @@ function build_projection(fields){ function mongo_package_info(package, universe){ return mongo_find({_user: universe, Package: package}).toArray().then(function(docs){ - if(!docs.length) - throw createError(404, `Package ${package} not found in ${universe}`) - return group_package_data(docs); + if(docs.length){ + return group_package_data(docs); + } else { + // Try to find pkg elsewhere... + var altquery = { + _type: 'src', + Package : {$regex: `^${package}$`, $options: 'i'}, + '$or' : [{'_universes': universe}, {'_indexed': true}] + } + return mongo_find(altquery).next().then(function(alt){ + if(alt){ + throw createError(301, `Package has moved...`, {headers : { + location: `https://${alt._user}.r-universe.dev/${alt.Package}` + }}); + } else { + throw createError(404, `Package ${package} not found in ${universe}`) + } + }); + } }); }