Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for (old-style) non-module visualizations and clean up dependencies #58

Merged
merged 12 commits into from
Dec 6, 2015
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: node_js
node_js:
- "4.0"
- "0.12"
- "0.10"
- "iojs"
2 changes: 1 addition & 1 deletion app/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ exports.misses = function() {

exports.keys = function() {
return Object.keys(cache);
};
};
136 changes: 15 additions & 121 deletions app/controllers/static.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
var models = require('../models');
var _ = require('lodash');
var resumer = require('resumer');
var browserify = require('browserify');
var path = require('path');
var cache = require('../cache');
var sass = require('node-sass');
var uuid = require('node-uuid');
var Q = require('q');
var debug = require('debug')('lightning:server:controllers:static');
var config = require('../../config/config');



exports.getDynamicVizBundle = function (req, res, next) {

res.set('Content-Type', 'application/javascript');
Expand Down Expand Up @@ -41,133 +38,30 @@ exports.getDynamicVizBundle = function (req, res, next) {

models.VisualizationType
.findAll().then(function(vizTypes) {
debug('Found ' + vizTypes.length + ' visualization types');
var funcs = [];
_.each(vizTypes, function(vizType) {
if(!vizType.isModule) {
funcs.push(vizType.exportToFS(tmpPath));
}
});

Q.all(funcs).spread(function() {

_.each(_.filter(vizTypes, function(vizType) { return (visualizationTypes.indexOf(vizType.name) > -1); }), function(vizType) {
if(vizType.isModule) {
debug(vizType.moduleName);
b.require(vizType.moduleName, {
expose: vizType.moduleName
});
} else {
var stream = resumer().queue(vizType.javascript).end();
b.require(stream, {
basedir: tmpPath,
expose: vizType.name
});
}
});

b.bundle(function(err, buf) {
if(err) {
return next(err);
}
debug('Found ' + vizTypes.length + ' visualization types');

var out = buf.toString('utf8');
cache.put('js/' + visualizationTypes.toString(), out, 1000 * 60 * 10);
if(!cacheHit) {
res.send(out);
}
_.each(_.filter(vizTypes, function(vizType) { return (visualizationTypes.indexOf(vizType.name) > -1); }), function(vizType) {
debug(vizType.moduleName);
b.require(vizType.moduleName, {
expose: vizType.moduleName
});
});
}).error(function(err) {
return res.status(500).send(err.message).end();
});

};


exports.bundleJSForExecution = function(req, res, next) {

res.set('Content-Type', 'application/javascript');

var b = browserify({
paths: [ config.root + '/node_modules']
});
var js = req.body.javascript;
var stream = resumer().queue(js).end();
b.require(stream, {
basedir: req.session.lastBundlePath || '',
expose: 'dynamicallyBundledJavascript'
});

var bundle = b.bundle();

bundle.on('error', function(err) {
debug(err);
return res.status(500).send();
});

bundle.pipe(res);


};



exports.buildSCSS = function(req, res) {
res.set('Content-Type', 'text/css');

var scssData = '#lightning-body {\n';
scssData += req.body.styles + '\n';
scssData += '\n}';

sass.render({
data: scssData,
success: function(sassResultss) {
res.send(sassResultss.css);
}
});
};


exports.getDynamicVizStyles = function (req, res, next) {

res.set('Content-Type', 'text/css');


// Get all vizTypes in array
var visualizationTypes = _.uniq(req.query.visualizations).sort();
var styles = cache.get('css/' + visualizationTypes.toString());

if(styles) {
return res.send(styles);
}

debug('building viz styles with ' + visualizationTypes);

models.VisualizationType
.findAll({
where: {
name: {
in: visualizationTypes
b.bundle(function(err, buf) {
if(err) {
return next(err);
}
}
}).then(function(vizTypes) {

var scssData = '#lightning-body {\n';
_.each(vizTypes, function(vizType) {
if(vizType.styles) {
scssData += vizType.styles + '\n';
var out = buf.toString('utf8');
cache.put('js/' + visualizationTypes.toString(), out, 1000 * 60 * 10);
if(!cacheHit) {
res.send(out);
}
});

scssData += '\n}';
}).error(function(err) {
return res.status(500).send(err.message).end();
});

sass.render({
data: scssData,
success: function(sassResults) {
cache.put('css/' + visualizationTypes.toString(), sassResults.css, 1000 * 60 * 60);
res.send(sassResults.css);
}
});
}).error(next);
};
14 changes: 7 additions & 7 deletions app/controllers/visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ exports.getDataWithKeys = function (req, res, next) {
};

exports.getSettingsWithKeys = function (req, res, next) {

res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Headers", "X-Requested-With");
res.set("Access-Control-Allow-Headers", "Content-Type");
Expand Down Expand Up @@ -124,7 +124,7 @@ exports.updateSettings = function (req, res, next) {
.findById(vid)
.then(function(viz) {
if(_.isString(viz.settings)) {
viz.settings = _.extend(JSON.parse(viz.settings || '{}'), req.body);
viz.settings = _.extend(JSON.parse(viz.settings || '{}'), req.body);
} else {
viz.settings = _.extend(viz.settings || {}, req.body);
}
Expand All @@ -146,7 +146,7 @@ exports.updateData = function (req, res, next) {
models.Visualization
.findById(vizId)
.then(function(viz) {

if(fieldName) {
viz.data[fieldName] = req.body.data;
} else {
Expand All @@ -168,7 +168,7 @@ var readWithTemplate = function(template, req, res, next) {
Visualization.find({
where: {
id: vizId
},
},
include: [VisualizationType]
}).then(function(viz) {
res.render(template, {
Expand Down Expand Up @@ -205,13 +205,13 @@ exports.delete = function (req, res, next) {
.then(function(viz) {
if(!viz) {
return res.status(404).send();
}
}

var sessionId = viz.SessionId;
viz.destroy({where: {}}).then(function() {
req.io.of(viz.getSessionSocketNamespace())
.emit('viz:delete', vizId);
return res.json(viz);
return res.json(viz);
}).catch(next);
}).catch(next);
};
Expand Down Expand Up @@ -256,7 +256,7 @@ exports.screenshot = function(req, res, next) {
console.warn(err);
return res.status(500).send();
}

res.setHeader('Content-Type', 'image/png');
renderStream.pipe(res);
});
Expand Down
Loading