Merge remote-tracking branch 'remotes/pita/develop' into pita-plugins

Conflicts:
	src/node/utils/caching_middleware.js
This commit is contained in:
Egil Moeller 2012-03-13 17:08:38 +01:00
commit 381e64a801
33 changed files with 165 additions and 164 deletions

View file

@ -1,6 +0,0 @@
var minify = require('../../utils/Minify');
exports.expressCreateServer = function (hook_name, args, cb) {
//serve minified files
args.app.get(/^\/minified\/(.*)/, minify.minifyJS);
}

View file

@ -8,34 +8,9 @@ var fs = require("fs");
var ERR = require("async-stacktrace");
exports.expressCreateServer = function (hook_name, args, cb) {
/* Handle static files for plugins:
paths like "/static/plugins/ep_myplugin/js/test.js"
are rewritten into ROOT_PATH_OF_MYPLUGIN/static/js/test.js,
commonly ETHERPAD_ROOT/node_modules/ep_myplugin/static/js/test.js
*/
args.app.get(/^\/minified\/plugins\/([^\/]+)\/static\/(.*)/, function(req, res, next) {
var plugin_name = req.params[0];
var modulePath = req.url.split("?")[0].substr("/minified/plugins/".length);
var fullPath = require.resolve(modulePath);
if (plugins.plugins[plugin_name] == undefined) {
return next();
}
fs.readFile(fullPath, "utf8", function(err, data){
if(ERR(err)) return;
res.send("require.define('" + modulePath + "', function (require, exports, module) {" + data + "})");
})
//require.define("/plugins.js", function (require, exports, module) {
//res.sendfile(fullPath);
});
// Cache both minified and static.
var assetCache = new CachingMiddleware;
args.app.all('/(minified|static)/*', assetCache.handle);
args.app.all('/(javascripts|static)/*', assetCache.handle);
// Minify will serve static files compressed (minify enabled). It also has
// file-specific hacks for ace/require-kernel/etc.
@ -44,8 +19,10 @@ exports.expressCreateServer = function (hook_name, args, cb) {
// Setup middleware that will package JavaScript files served by minify for
// CommonJS loader on the client-side.
var jsServer = new (Yajsml.Server)({
rootPath: 'minified/'
rootPath: 'javascripts/src/'
, rootURI: 'http://localhost:' + settings.port + '/static/js/'
, libraryPath: 'javascripts/lib/'
, libraryURI: 'http://localhost:' + settings.port + '/static/plugins/'
});
var StaticAssociator = Yajsml.associators.StaticAssociator;

View file

@ -27,6 +27,7 @@ var cleanCSS = require('clean-css');
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var path = require('path');
var plugins = require("ep_etherpad-lite/static/js/pluginfw/plugins");
var RequireKernel = require('require-kernel');
var server = require('../server');
@ -35,11 +36,14 @@ var TAR_PATH = path.join(__dirname, 'tar.json');
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
// Rewrite tar to include modules with no extensions and proper rooted paths.
var LIBRARY_PREFIX = 'ep_etherpad-lite/static/js';
exports.tar = {};
for (var key in tar) {
exports.tar['/' + key] =
tar[key].map(function (p) {return '/' + p}).concat(
tar[key].map(function (p) {return '/' + p.replace(/\.js$/, '')})
exports.tar[LIBRARY_PREFIX + '/' + key] =
tar[key].map(function (p) {return LIBRARY_PREFIX + '/' + p}).concat(
tar[key].map(function (p) {
return LIBRARY_PREFIX + '/' + p.replace(/\.js$/, '')
})
);
}
@ -63,6 +67,22 @@ exports.minify = function(req, res, next)
return;
}
/* Handle static files for plugins:
paths like "plugins/ep_myplugin/static/js/test.js"
are rewritten into ROOT_PATH_OF_MYPLUGIN/static/js/test.js,
commonly ETHERPAD_ROOT/node_modules/ep_myplugin/static/js/test.js
*/
var match = filename.match(/^plugins\/([^\/]+)\/static\/(.*)/);
if (match) {
var pluginName = match[1];
var resourcePath = match[2];
var plugin = plugins.plugins[pluginName];
if (plugin) {
var pluginPath = plugin.package.realPath;
filename = path.relative(ROOT_DIR, pluginPath + '/static/' + resourcePath);
}
}
// What content type should this be?
// TODO: This should use a MIME module.
var contentType;
@ -149,8 +169,10 @@ function getAceFile(callback) {
var request = require('request');
var baseURI = 'http://localhost:' + settings.port
var resourceURI = baseURI + path.normalize(path.join('/static/', filename));
resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?)
request(baseURI + path.normalize(path.join('/static/', filename)), function (error, response, body) {
request(resourceURI, function (error, response, body) {
if (!error && response.statusCode == 200) {
data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = '
+ JSON.stringify(body || '') + ';\n';

View file

@ -23,7 +23,8 @@ var zlib = require('zlib');
var util = require('util');
var settings = require('./Settings');
var CACHE_DIR = path.join(settings.root, 'var/');
var CACHE_DIR = path.normalize(path.join(settings.root, 'var/'));
CACHE_DIR = path.existsSync(CACHE_DIR) ? CACHE_DIR : undefined;
var responseCache = {};
@ -37,7 +38,7 @@ function CachingMiddleware() {
}
CachingMiddleware.prototype = new function () {
function handle(req, res, next) {
if (!(req.method == "GET" || req.method == "HEAD")) {
if (!(req.method == "GET" || req.method == "HEAD") || !CACHE_DIR) {
return next(undefined, req, res);
}
@ -54,7 +55,7 @@ CachingMiddleware.prototype = new function () {
var modifiedSince = (req.headers['if-modified-since']
&& new Date(req.headers['if-modified-since']));
var lastModifiedCache = !error && stats.mtime;
if (lastModifiedCache) {
if (lastModifiedCache && responseCache[cacheKey]) {
req.headers['if-modified-since'] = lastModifiedCache.toUTCString();
} else {
delete req.headers['if-modified-since'];
@ -83,7 +84,7 @@ CachingMiddleware.prototype = new function () {
&& new Date(res.getHeader('last-modified')));
res.writeHead = old_res.writeHead;
if (status == 200 || status == 404) {
if (status == 200) {
// Update cache
var buffer = '';