unpkg/server/middleware/serveFile.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-08-11 05:29:44 +00:00
const fs = require('fs')
const path = require('path')
2017-08-19 00:53:56 +00:00
const babel = require('babel-core')
const unpkgRewrite = require('babel-plugin-unpkg-rewrite')
2017-08-18 22:49:12 +00:00
const getMetadata = require('./utils/getMetadata')
2017-08-18 19:57:42 +00:00
const getFileContentType = require('./utils/getFileContentType')
const getIndexHTML = require('./utils/getIndexHTML')
2017-08-11 05:29:44 +00:00
2017-08-12 17:38:50 +00:00
/**
* Automatically generate HTML pages that show package contents.
*/
const AutoIndex = !process.env.DISABLE_INDEX
2017-08-18 22:49:12 +00:00
/**
* Maximum recursion depth for meta listings.
*/
const MaximumDepth = 128
2017-08-19 00:53:56 +00:00
const FileTransforms = {
expand: function (file, callback) {
const options = {
plugins: [ unpkgRewrite ]
}
2017-08-11 05:29:44 +00:00
2017-08-19 00:53:56 +00:00
babel.transformFile(file, options, function (error, result) {
callback(error, result && result.code)
})
}
2017-08-11 05:29:44 +00:00
}
/**
* Send the file, JSON metadata, or HTML directory listing.
*/
2017-08-12 17:38:50 +00:00
function serveFile(req, res, next) {
2017-08-18 22:49:12 +00:00
if (req.query.meta != null) {
getMetadata(req.packageDir, req.file, req.stats, MaximumDepth, function (error, metadata) {
if (error) {
console.error(error)
res.status(500).type('text').send(`Cannot generate metadata for ${req.packageSpec}${req.filename}`)
} else {
// Cache metadata for 1 year.
res.set({
'Cache-Control': 'public, max-age=31536000',
'Cache-Tag': 'meta'
}).send(metadata)
}
})
2017-08-12 17:38:50 +00:00
} else if (req.stats.isFile()) {
2017-08-19 00:53:56 +00:00
const file = path.join(req.packageDir, req.file)
let contentType = getFileContentType(file)
if (contentType === 'text/html')
contentType = 'text/plain' // We can't serve HTML because bad people :(
2017-08-12 17:38:50 +00:00
// Cache files for 1 year.
2017-08-13 00:23:40 +00:00
res.set({
2017-08-19 00:53:56 +00:00
'Content-Type': contentType,
2017-08-13 00:23:40 +00:00
'Cache-Control': 'public, max-age=31536000',
'Cache-Tag': 'file'
})
2017-08-12 02:17:02 +00:00
2017-08-19 00:53:56 +00:00
if (contentType === 'application/javascript' && req.query.expand != null) {
FileTransforms.expand(file, function (error, code) {
if (error) {
console.error(error)
res.status(500).type('text').send(`Cannot generate index page for ${req.packageSpec}${req.filename}`)
} else {
res.send(code)
}
})
} else {
res.sendFile(file)
}
2017-08-12 17:38:50 +00:00
} else if (AutoIndex && req.stats.isDirectory()) {
2017-08-18 19:57:42 +00:00
getIndexHTML(req.packageInfo, req.packageVersion, req.packageDir, req.file, function (error, html) {
2017-08-12 17:38:50 +00:00
if (error) {
console.error(error)
2017-08-13 00:23:40 +00:00
res.status(500).type('text').send(`Cannot generate index page for ${req.packageSpec}${req.filename}`)
2017-08-12 17:38:50 +00:00
} else {
// Cache HTML directory listings for 1 minute.
2017-08-13 00:23:40 +00:00
res.set({
'Cache-Control': 'public, max-age=60',
'Cache-Tag': 'index'
}).send(html)
2017-08-12 17:38:50 +00:00
}
})
} else {
2017-08-13 00:23:40 +00:00
res.status(403).type('text').send(`Cannot serve ${req.packageSpec}${req.filename}; it's not a file`)
}
}
module.exports = serveFile