Skip to content

Commit

Permalink
Fix array objects generated by NodeVM bug #19
Browse files Browse the repository at this point in the history
  • Loading branch information
typpo committed Aug 8, 2019
1 parent 80b4b28 commit 35816e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const winston = require('winston');
const { CanvasRenderService } = require('chartjs-node-canvas');
const { NodeVM } = require('vm2');

const { fixNodeVmObject } = require('./util');

const logger = new (winston.Logger)({
level: process.env.LOG_LEVEL || 'info',
transports: [
Expand Down Expand Up @@ -64,6 +66,8 @@ function renderChart(width, height, backgroundColor, untrustedChart) {
chart = untrustedChart;
}

fixNodeVmObject(chart);

chart.options = chart.options || {};

if (chart.type === 'donut') {
Expand Down
20 changes: 20 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function fixNodeVmObject(obj) {
// Fix for https://github.com/patriksimek/vm2/issues/198
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const val = obj[key];
if (Array.isArray(val)) {
obj[key] = Array.from(val);
obj[key].forEach((arrObj) => {
fixNodeVmObject(arrObj);
});
} else if (typeof val === 'object' && val !== null) {
fixNodeVmObject(val);
}
}
}
}

module.exports = {
fixNodeVmObject,
};

0 comments on commit 35816e9

Please sign in to comment.