From 4413d498d890c49192fc8b539a8b77f6e2141820 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Mon, 3 Sep 2012 14:35:36 -0700 Subject: [PATCH 1/4] Minify is a named function. --- src/node/utils/Minify.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index c5996565..731f2f83 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -51,7 +51,7 @@ for (var key in tar) { * @param req the Express request * @param res the Express response */ -exports.minify = function(req, res, next) +function minify(req, res, next) { var filename = req.params['filename']; @@ -319,3 +319,5 @@ function compressCSS(values) var complete = values.join("\n"); return cleanCSS.process(complete); } + +exports.minify = minify; From 024a26f2729890a1e7a2966dea74cd69a6cf1d59 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Mon, 3 Sep 2012 14:37:10 -0700 Subject: [PATCH 2/4] Minify publishes its own mock request thing. --- src/node/hooks/express/static.js | 68 +------------------------------- src/node/utils/Minify.js | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/node/hooks/express/static.js b/src/node/hooks/express/static.js index 0d78da2f..7d654c1b 100644 --- a/src/node/hooks/express/static.js +++ b/src/node/hooks/express/static.js @@ -7,74 +7,8 @@ var Yajsml = require('yajsml'); var fs = require("fs"); var ERR = require("async-stacktrace"); var _ = require("underscore"); -var urlutil = require('url'); exports.expressCreateServer = function (hook_name, args, cb) { - // What follows is a terrible hack to avoid loop-back within the server. - // TODO: Serve files from another service, or directly from the file system. - function requestURI(url, method, headers, callback, redirectCount) { - var parsedURL = urlutil.parse(url); - - var status = 500, headers = {}, content = []; - - var mockRequest = { - url: url - , method: method - , params: {filename: parsedURL.path.replace(/^\/static\//, '')} - , headers: headers - }; - var mockResponse = { - writeHead: function (_status, _headers) { - status = _status; - for (var header in _headers) { - if (Object.prototype.hasOwnProperty.call(_headers, header)) { - headers[header] = _headers[header]; - } - } - } - , setHeader: function (header, value) { - headers[header.toLowerCase()] = value.toString(); - } - , header: function (header, value) { - headers[header.toLowerCase()] = value.toString(); - } - , write: function (_content) { - _content && content.push(_content); - } - , end: function (_content) { - _content && content.push(_content); - callback(status, headers, content.join('')); - } - }; - - minify.minify(mockRequest, mockResponse); - } - function requestURIs(locations, method, headers, callback) { - var pendingRequests = locations.length; - var responses = []; - - function respondFor(i) { - return function (status, headers, content) { - responses[i] = [status, headers, content]; - if (--pendingRequests == 0) { - completed(); - } - }; - } - - for (var i = 0, ii = locations.length; i < ii; i++) { - requestURI(locations[i], method, headers, respondFor(i)); - } - - function completed() { - var statuss = responses.map(function (x) {return x[0]}); - var headerss = responses.map(function (x) {return x[1]}); - var contentss = responses.map(function (x) {return x[2]}); - callback(statuss, headerss, contentss); - }; - } - - // Cache both minified and static. var assetCache = new CachingMiddleware; @@ -91,7 +25,7 @@ exports.expressCreateServer = function (hook_name, args, cb) { , rootURI: 'http://localhost:' + settings.port + '/static/js/' , libraryPath: 'javascripts/lib/' , libraryURI: 'http://localhost:' + settings.port + '/static/plugins/' - , requestURIs: requestURIs // Loop-back is causing problems, this is a workaround. + , requestURIs: minify.requestURIs // Loop-back is causing problems, this is a workaround. }); var StaticAssociator = Yajsml.associators.StaticAssociator; diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index 731f2f83..e8b73606 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -29,6 +29,7 @@ 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 urlutil = require('url'); var ROOT_DIR = path.normalize(__dirname + "/../../static/"); var TAR_PATH = path.join(__dirname, 'tar.json'); @@ -46,6 +47,70 @@ for (var key in tar) { ); } +// What follows is a terrible hack to avoid loop-back within the server. +// TODO: Serve files from another service, or directly from the file system. +function requestURI(url, method, headers, callback, redirectCount) { + var parsedURL = urlutil.parse(url); + + var status = 500, headers = {}, content = []; + + var mockRequest = { + url: url + , method: method + , params: {filename: parsedURL.path.replace(/^\/static\//, '')} + , headers: headers + }; + var mockResponse = { + writeHead: function (_status, _headers) { + status = _status; + for (var header in _headers) { + if (Object.prototype.hasOwnProperty.call(_headers, header)) { + headers[header] = _headers[header]; + } + } + } + , setHeader: function (header, value) { + headers[header.toLowerCase()] = value.toString(); + } + , header: function (header, value) { + headers[header.toLowerCase()] = value.toString(); + } + , write: function (_content) { + _content && content.push(_content); + } + , end: function (_content) { + _content && content.push(_content); + callback(status, headers, content.join('')); + } + }; + + minify(mockRequest, mockResponse); +} +function requestURIs(locations, method, headers, callback) { + var pendingRequests = locations.length; + var responses = []; + + function respondFor(i) { + return function (status, headers, content) { + responses[i] = [status, headers, content]; + if (--pendingRequests == 0) { + completed(); + } + }; + } + + for (var i = 0, ii = locations.length; i < ii; i++) { + requestURI(locations[i], method, headers, respondFor(i)); + } + + function completed() { + var statuss = responses.map(function (x) {return x[0]}); + var headerss = responses.map(function (x) {return x[1]}); + var contentss = responses.map(function (x) {return x[2]}); + callback(statuss, headerss, contentss); + }; +} + /** * creates the minifed javascript for the given minified name * @param req the Express request @@ -321,3 +386,6 @@ function compressCSS(values) } exports.minify = minify; + +exports.requestURI = requestURI; +exports.requestURIs = requestURIs; From 02c22d7b891272cc9d36a5822a19addf4e1f6eee Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Mon, 3 Sep 2012 14:37:41 -0700 Subject: [PATCH 3/4] Remove loopback from Minify. --- src/node/utils/Minify.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js index e8b73606..21a663c0 100644 --- a/src/node/utils/Minify.js +++ b/src/node/utils/Minify.js @@ -239,10 +239,11 @@ function getAceFile(callback) { var resourceURI = baseURI + path.normalize(path.join('/static/', filename)); resourceURI = resourceURI.replace(/\\/g, '/'); // Windows (safe generally?) - request(resourceURI, function (error, response, body) { - if (!error && response.statusCode == 200) { + requestURI(resourceURI, 'GET', {}, function (status, headers, body) { + var error = !(status == 200 || status == 404); + if (!error) { data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = ' - + JSON.stringify(body || '') + ';\n'; + + JSON.stringify(status == 200 ? body || '' : null) + ';\n'; } else { // Silence? } From 6e796eb5fbaf7c842ecdecabd48f8065eaec1084 Mon Sep 17 00:00:00 2001 From: Chad Weider Date: Mon, 3 Sep 2012 14:41:26 -0700 Subject: [PATCH 4/4] Make exception case for require kernel obvious. --- src/static/js/ace.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/static/js/ace.js b/src/static/js/ace.js index 600b6136..d7414c17 100644 --- a/src/static/js/ace.js +++ b/src/static/js/ace.js @@ -168,11 +168,9 @@ require.setGlobalKeyPath("require");\n\ buffer.push(KERNEL_BOOT); buffer.push('<\/script>'); } else { - file = KERNEL_SOURCE; - buffer.push('