|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var ERR = require("async-stacktrace"); |
|
var settings = require('./Settings'); |
|
var async = require('async'); |
|
var fs = require('fs'); |
|
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('etherpad-require-kernel'); |
|
var urlutil = require('url'); |
|
|
|
var ROOT_DIR = path.normalize(__dirname + "/../../static/"); |
|
var TAR_PATH = path.join(__dirname, 'tar.json'); |
|
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); |
|
|
|
|
|
var LIBRARY_WHITELIST = [ |
|
'async' |
|
, 'security' |
|
, 'tinycon' |
|
, 'underscore' |
|
, 'unorm' |
|
]; |
|
|
|
|
|
var LIBRARY_PREFIX = 'ep_etherpad-lite/static/js'; |
|
exports.tar = {}; |
|
function prefixLocalLibraryPath(path) { |
|
if (path.charAt(0) == '$') { |
|
return path.slice(1); |
|
} else { |
|
return LIBRARY_PREFIX + '/' + path; |
|
} |
|
} |
|
|
|
for (var key in tar) { |
|
exports.tar[prefixLocalLibraryPath(key)] = |
|
tar[key].map(prefixLocalLibraryPath).concat( |
|
tar[key].map(prefixLocalLibraryPath).map(function (p) { |
|
return p.replace(/\.js$/, ''); |
|
}) |
|
).concat( |
|
tar[key].map(prefixLocalLibraryPath).map(function (p) { |
|
return p.replace(/\.js$/, '') + '/index.js'; |
|
}) |
|
); |
|
} |
|
|
|
|
|
|
|
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); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function minify(req, res, next) |
|
{ |
|
var filename = req.params['filename']; |
|
|
|
|
|
filename = path.normalize(path.join(ROOT_DIR, filename)); |
|
if (filename.indexOf(ROOT_DIR) == 0) { |
|
filename = filename.slice(ROOT_DIR.length); |
|
} else { |
|
res.writeHead(404, {}); |
|
res.end(); |
|
return; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
var match = filename.match(/^plugins\/([^\/]+)(\/(?:(static\/.*)|.*))?$/); |
|
if (match) { |
|
var library = match[1]; |
|
var libraryPath = match[2] || ''; |
|
|
|
if (plugins.plugins[library] && match[3]) { |
|
var plugin = plugins.plugins[library]; |
|
var pluginPath = plugin.package.realPath; |
|
filename = path.relative(ROOT_DIR, pluginPath + libraryPath); |
|
|
|
|
|
|
|
} else if (LIBRARY_WHITELIST.indexOf(library) != -1) { |
|
|
|
|
|
|
|
filename = '../node_modules/' + library + libraryPath; |
|
} |
|
} |
|
|
|
|
|
|
|
var contentType; |
|
if (filename.match(/\.js$/)) { |
|
contentType = "text/javascript"; |
|
} else if (filename.match(/\.css$/)) { |
|
contentType = "text/css"; |
|
} else if (filename.match(/\.html$/)) { |
|
contentType = "text/html"; |
|
} else if (filename.match(/\.txt$/)) { |
|
contentType = "text/plain"; |
|
} else if (filename.match(/\.png$/)) { |
|
contentType = "image/png"; |
|
} else if (filename.match(/\.gif$/)) { |
|
contentType = "image/gif"; |
|
} else if (filename.match(/\.ico$/)) { |
|
contentType = "image/x-icon"; |
|
} else { |
|
contentType = "application/octet-stream"; |
|
} |
|
|
|
statFile(filename, function (error, date, exists) { |
|
if (date) { |
|
date = new Date(date); |
|
res.setHeader('last-modified', date.toUTCString()); |
|
res.setHeader('date', (new Date()).toUTCString()); |
|
if (settings.maxAge !== undefined) { |
|
var expiresDate = new Date((new Date()).getTime()+settings.maxAge*1000); |
|
res.setHeader('expires', expiresDate.toUTCString()); |
|
res.setHeader('cache-control', 'max-age=' + settings.maxAge); |
|
} |
|
} |
|
|
|
if (error) { |
|
res.writeHead(500, {}); |
|
res.end(); |
|
} else if (!exists) { |
|
res.writeHead(404, {}); |
|
res.end(); |
|
} else if (new Date(req.headers['if-modified-since']) >= date) { |
|
res.writeHead(304, {}); |
|
res.end(); |
|
} else { |
|
if (req.method == 'HEAD') { |
|
res.header("Content-Type", contentType); |
|
res.writeHead(200, {}); |
|
res.end(); |
|
} else if (req.method == 'GET') { |
|
getFileCompressed(filename, contentType, function (error, content) { |
|
if(ERR(error, function(){ |
|
res.writeHead(500, {}); |
|
res.end(); |
|
})) return; |
|
res.header("Content-Type", contentType); |
|
res.writeHead(200, {}); |
|
res.write(content); |
|
res.end(); |
|
}); |
|
} else { |
|
res.writeHead(405, {'allow': 'HEAD, GET'}); |
|
res.end(); |
|
} |
|
} |
|
}); |
|
} |
|
|
|
|
|
function getAceFile(callback) { |
|
fs.readFile(ROOT_DIR + 'js/ace.js', "utf8", function(err, data) { |
|
if(ERR(err, callback)) return; |
|
|
|
|
|
var founds = data.match(/\$\$INCLUDE_[a-zA-Z_]+\("[^"]*"\)/gi); |
|
if (!settings.minify) { |
|
founds = []; |
|
} |
|
|
|
founds.push('$$INCLUDE_JS("../static/js/require-kernel.js")'); |
|
|
|
data += ';\n'; |
|
data += 'Ace2Editor.EMBEDED = Ace2Editor.EMBEDED || {};\n'; |
|
|
|
|
|
|
|
async.forEach(founds, function (item, callback) { |
|
var filename = item.match(/"([^"]*)"/)[1]; |
|
|
|
var baseURI = 'http://localhost:' + settings.port; |
|
var resourceURI = baseURI + path.normalize(path.join('/static/', filename)); |
|
resourceURI = resourceURI.replace(/\\/g, '/'); |
|
|
|
requestURI(resourceURI, 'GET', {}, function (status, headers, body) { |
|
var error = !(status == 200 || status == 404); |
|
if (!error) { |
|
data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = ' |
|
+ JSON.stringify(status == 200 ? body || '' : null) + ';\n'; |
|
} else { |
|
|
|
} |
|
callback(); |
|
}); |
|
}, function(error) { |
|
callback(error, data); |
|
}); |
|
}); |
|
} |
|
|
|
|
|
function statFile(filename, callback, dirStatLimit) { |
|
if (typeof dirStatLimit === 'undefined') { |
|
dirStatLimit = 3; |
|
} |
|
|
|
if (dirStatLimit < 1 || filename == '' || filename == '/') { |
|
callback(null, null, false); |
|
} else if (filename == 'js/ace.js') { |
|
|
|
|
|
lastModifiedDateOfEverything(function (error, date) { |
|
callback(error, date, !error); |
|
}); |
|
} else if (filename == 'js/require-kernel.js') { |
|
callback(null, requireLastModified(), true); |
|
} else { |
|
fs.stat(ROOT_DIR + filename, function (error, stats) { |
|
if (error) { |
|
if (error.code == "ENOENT") { |
|
|
|
statFile(path.dirname(filename), function (error, date, exists) { |
|
callback(error, date, false); |
|
}, dirStatLimit-1); |
|
} else { |
|
callback(error); |
|
} |
|
} else if (stats.isFile()) { |
|
callback(null, stats.mtime.getTime(), true); |
|
} else { |
|
callback(null, stats.mtime.getTime(), false); |
|
} |
|
}); |
|
} |
|
} |
|
function lastModifiedDateOfEverything(callback) { |
|
var folders2check = [ROOT_DIR + 'js/', ROOT_DIR + 'css/']; |
|
var latestModification = 0; |
|
|
|
async.forEach(folders2check, function(path, callback) |
|
{ |
|
|
|
fs.readdir(path, function(err, files) |
|
{ |
|
if(ERR(err, callback)) return; |
|
|
|
|
|
files.push("."); |
|
|
|
|
|
async.forEach(files, function(filename, callback) |
|
{ |
|
|
|
fs.stat(path + "/" + filename, function(err, stats) |
|
{ |
|
if(ERR(err, callback)) return; |
|
|
|
|
|
var modificationTime = stats.mtime.getTime(); |
|
|
|
|
|
if(modificationTime > latestModification) |
|
{ |
|
latestModification = modificationTime; |
|
} |
|
|
|
callback(); |
|
}); |
|
}, callback); |
|
}); |
|
}, function () { |
|
callback(null, latestModification); |
|
}); |
|
} |
|
|
|
|
|
|
|
var _requireLastModified = new Date(); |
|
function requireLastModified() { |
|
return _requireLastModified.toUTCString(); |
|
} |
|
function requireDefinition() { |
|
return 'var require = ' + RequireKernel.kernelSource + ';\n'; |
|
} |
|
|
|
function getFileCompressed(filename, contentType, callback) { |
|
getFile(filename, function (error, content) { |
|
if (error || !content) { |
|
callback(error, content); |
|
} else { |
|
if (settings.minify) { |
|
if (contentType == 'text/javascript') { |
|
try { |
|
content = compressJS([content]); |
|
} catch (error) { |
|
|
|
} |
|
} else if (contentType == 'text/css') { |
|
content = compressCSS([content]); |
|
} |
|
} |
|
callback(null, content); |
|
} |
|
}); |
|
} |
|
|
|
function getFile(filename, callback) { |
|
if (filename == 'js/ace.js') { |
|
getAceFile(callback); |
|
} else if (filename == 'js/require-kernel.js') { |
|
callback(undefined, requireDefinition()); |
|
} else { |
|
fs.readFile(ROOT_DIR + filename, callback); |
|
} |
|
} |
|
|
|
function compressJS(values) |
|
{ |
|
var complete = values.join("\n"); |
|
var ast = jsp.parse(complete); |
|
ast = pro.ast_mangle(ast); |
|
ast = pro.ast_squeeze(ast); |
|
return pro.gen_code(ast); |
|
} |
|
|
|
function compressCSS(values) |
|
{ |
|
var complete = values.join("\n"); |
|
var minimized = new CleanCSS().minify(complete).styles; |
|
return minimized; |
|
} |
|
|
|
exports.minify = minify; |
|
|
|
exports.requestURI = requestURI; |
|
exports.requestURIs = requestURIs; |
|
|