I have a Node app which uses Node Canvas to create a canvas then convert to a PNG. My code is:
// create the canvas and draw stuff on it up here
// convert to a base64 string
var buf = canvas.toBuffer();
var graph = buf.toString('base64');
var data = JSON.stringify({
graph: graph
});
var headers = {
'Content-Type': 'image/png'
};
res.writeHead(200, headers);
res.end(data);
This all works fine. Now I want to move this into a child process. So in my parent process:
// in parent process, start the child process
var self = this;
var spawn = require('child_process').spawn;
var child = spawn(process.execPath, ['/home/mark/Programming/myapp/app/child_process_scripts/createDotPlotGraph.js']);
child.stdout.on('data', function(buf){
console.log('buf is ', buff);
var graph = buf.toString('base64');
console.log('graph is ', graph);
var data = JSON.stringify({
graph: graph
});
var headers = {
'Content-Type': 'image/png'
};
res.writeHead(200, headers);
res.end(data);
child.kill();
});
And in my child process I do this:
// in child process, output the buffer
var buf = canvas.toBuffer();
console.log(buf);
However, it's not working, the base64 string sent back to the frontend seems to be corrupted.
The output of buf in the console.log in the parent is:
buf is <Buffer 3c 42 75 66 66 65 72 20 38 39 20 35 30 20 34 65 20 34 37 20 30 64 20 30 61 20 31 61 20 30 61 20 30 30 20 30 30 20 30 30 20 30 64 20 34 39 20 34 38 20 ... >
And the output of the graph in the console.log in the parent is:
graph is PEJ1ZmZlciA4OSA1MCA0ZSA0NyAwZCAwYSAxYSAwYSAwMCAwMCAwMCAwZCA0OSA0OCA0NCA1MiAwMCAwMCAwMSA2NCAwMCAwMCAwMSA2NCAwOCAwNiAwMCAwMCAwMCAxNyAxYiA0MSAyMCAwMCAwMCAwMCAwNiA2MiA0YiA0NyA0NCAwMCBmZiAwMCBmZiAwMCBmZiBhMCBiZCBhNyAuLi4gPgo=
It looks like when sending data to the arent process, it changes it to a buffer, because in the child even if I do:
console.log('test string');
The output of buf in the console.log in the parent is:
buf is <Buffer 74 65 73 74 20 73 74 72 69 6e 67 0a>
I would have expected that to be "test string". So it seems like the buffer is for the canvas is itself getting changed to a buffer....Any ideas?
via Mark
No comments:
Post a Comment