-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import express from 'express'; | ||
import gunzip from 'gunzip-maybe'; | ||
import tar from 'tar-stream'; | ||
import createError from 'http-errors'; | ||
import {get_bucket_stream, bucket_find} from '../src/db.js'; | ||
import {index_files_from_stream} from '../src/tools.js'; | ||
|
||
const router = express.Router(); | ||
|
||
function send_from_bucket(hash, operation, res){ | ||
return get_bucket_stream(hash).then(function(pkg){ | ||
let name = pkg.filename; | ||
if(operation == 'send'){ | ||
let type = name.endsWith('.zip') ? 'application/zip' : 'application/x-gzip'; | ||
return pkg.stream.pipe( | ||
res.type(type).attachment(name).set({ | ||
'Content-Length': pkg.length, | ||
'Cache-Control': 'public, max-age=31557600, immutable', | ||
'Last-Modified' : pkg.uploadDate.toUTCString() | ||
}) | ||
); | ||
} | ||
|
||
if(operation == 'index'){ | ||
if(!name.endsWith('gz')){ | ||
pkg.stream.destroy(); | ||
throw createError(500, `Unable to index ${name} (only tar.gz files are supported)`); | ||
} | ||
return index_files_from_stream(pkg.stream).then(function(index){ | ||
index.files.forEach(function(entry){ | ||
entry.filename = entry.filename.match(/\/.*/)[0]; //strip pkg root dir | ||
}); | ||
index.gzip = true; | ||
res.send(index); | ||
}); | ||
} | ||
|
||
if(operation == 'decompress'){ | ||
if(!name.endsWith('gz')){ | ||
pkg.stream.destroy(); | ||
throw createError(`Unable to decompress ${name} (only tar.gz files are suppored)`); | ||
} | ||
var tarname = name.replace(/(tar.gz|tgz)/, 'tar'); | ||
return pkg.stream.pipe(gunzip()).pipe( | ||
res.type('application/tar').attachment(tarname).set({ | ||
'Cache-Control': 'public, max-age=31536000, immutable', | ||
'Last-Modified' : pkg.uploadDate.toUTCString() | ||
}) | ||
); | ||
} | ||
|
||
throw createError(400, `Unsuppored operation ${operation}`); | ||
}); | ||
} | ||
|
||
/* Reduce noise from crawlers in log files */ | ||
router.get("/robots.txt", function(req, res, next) { | ||
res.type('text/plain').send(`User-agent: *\nDisallow: /\n`); | ||
}); | ||
|
||
router.get("/:hash{/:postfix}", function(req, res, next) { | ||
let hash = req.params.hash || ""; | ||
let postfix = req.params.postfix || "send"; | ||
if(hash.length != 32 && hash.length != 64) //should be md5 or sha256 | ||
return next(createError(400, "Invalid hash length")); | ||
return send_from_bucket(hash, postfix, res); | ||
}); | ||
|
||
/* index all the files on the cdn */ | ||
router.get("/", function(req, res, next) { | ||
var cursor = bucket_find({}, {sort: {uploadDate: -1}, project: {_id: 1, filename: 1}}); | ||
cursor.stream({transform: x => `${x._id} ${x.uploadDate.toISOString()} ${x.filename}\n`}).on('error', function(err){ | ||
next(createError(500, err)); | ||
}).pipe(res.type('text/plain')); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export default function(req, res, next){ | ||
//remove trailing slashes except for root | ||
if (req.path.slice(-1) === '/' && req.path.length > 1) { | ||
const query = req.url.slice(req.path.length) | ||
const safepath = req.path.slice(0, -1).replace(/\/+/g, '/') | ||
return res.redirect(301, safepath + query); | ||
} | ||
|
||
//set universe globals | ||
if(process.env.UNIVERSE){ | ||
req.universe = process.env.UNIVERSE; | ||
} else if(req.app.get('env') === 'production'){ | ||
req.universe = req.hostname.replace('.r-universe.dev', ''); | ||
res.locals.vhost = req.headers['r-universe-vhost']; | ||
} | ||
res.locals.universe = req.universe || 'ropensci'; | ||
res.locals.node_env = req.app.get('env'); | ||
next(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters