Created
October 4, 2010 23:23
-
-
Save rgrove/610644 to your computer and use it in GitHub Desktop.
Simple NodeJS combo handler for YUI 3 (or anything, really).
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
#!/usr/bin/env node | |
/** | |
* Simple YUI combo handler using NodeJS and Express. Stick a caching and | |
* compressing proxy in front of this and you're ready to rock in production. | |
* | |
* Copyright (c) 2010 Ryan Grove <[email protected]> | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
var express = require('express'), | |
fs = require('fs'), | |
path = require('path'), | |
// Port to listen on. | |
PORT = 8000, | |
// Absolute path to the public root directory. | |
// ROOT_PATH = '/home/node/public/yui', | |
ROOT_PATH = '/Users/rgrove/src/yui', | |
// Absolute path to the public YUI3 directory. | |
YUI3_PATH = ROOT_PATH + '/yui3', | |
// -- Express app and middleware ----------------------------------------------- | |
app = express.createServer( | |
// express.logger(), | |
express.staticProvider(ROOT_PATH), | |
express.conditionalGet() | |
); | |
// -- Routes ------------------------------------------------------------------- | |
app.get('/yui3', function (req, res) { | |
comboHandler(YUI3_PATH, req, res); | |
}); | |
// -- Functions ---------------------------------------------------------------- | |
var defaultMimeType = 'application/octet-stream', | |
mimeTypes = { | |
'.css' : 'text/css', | |
'.js' : 'application/javascript', | |
'.json': 'application/json', | |
'.txt' : 'text/plain', | |
'.xml' : 'application/xml' | |
}; | |
function comboHandler(rootPath, req, res) { | |
var body = [], | |
query = parseQuery(req.url), | |
pending = query.length, | |
type = pending && getMimeType(path.extname(query[0])), | |
lastModified; | |
if (!pending) { | |
res.send(400); | |
} | |
function finish() { | |
if (lastModified) { | |
res.header('Last-Modified', lastModified.toUTCString()); | |
} | |
res.send(body.join("\n"), { | |
'Content-Type': type + ';charset=utf-8' | |
}); | |
} | |
function notFound() { | |
res.send(400); | |
} | |
query.forEach(function (relativePath, i) { | |
var absolutePath; | |
// Skip empty params. | |
if (!relativePath) { | |
pending -= 1; | |
return; | |
} | |
absolutePath = path.normalize(path.join(rootPath, relativePath)); | |
// Don't allow traversal above the root path. | |
if (absolutePath.indexOf(rootPath) !== 0) { | |
res.send(403); // TODO: nicer error handling | |
return; | |
} | |
fs.readFile(absolutePath, 'utf8', function (err, data) { | |
if (err) { return notFound(); } | |
body[i] = data; | |
fs.stat(absolutePath, function (err, stats) { | |
var mtime; | |
if (!err) { | |
mtime = new Date(stats.mtime); | |
if (!lastModified || mtime > lastModified) { | |
lastModified = mtime; | |
} | |
} | |
pending -= 1; | |
if (pending === 0) { | |
finish(); | |
} | |
}); | |
}); | |
}); | |
} | |
function decode(string) { | |
return decodeURIComponent(string).replace(/\+/g, ' '); | |
} | |
function getMimeType(extension) { | |
extension = (extension.charAt(0) === '.' ? extension : '.' + extension).toLowerCase(); | |
return mimeTypes[extension] || defaultMimeType; | |
} | |
// Because querystring.parse() is stupid. | |
function parseQuery(url) { | |
var parsed = [], | |
query = url.split('?')[1]; | |
if (query) { | |
query.split('&').forEach(function (item) { | |
parsed.push(decode(item.split('=')[0])); | |
}); | |
} | |
return parsed; | |
} | |
app.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment