7

This is really infuriating. I can't find anywhere in my code where I'm doing anything illegal, but for some reason, calling fork blows up my program. Here's the code. The relevant portion is in svgToPNG where I call fork.

{fork} = require 'child_process'
{Coral} = require 'coral'

svgToPNG = (svg, reply, log) ->
  log "converting SVG to a PNG"
  # set up a child process to call convert svg: png:-
  convert = fork '/usr/bin/env', ['convert', 'svg:', 'png:-']
  log "Spawned child process running convert, pid " + convert.pid
  # set up behavior when an error occurs
  convert.stderr.on "data", ->
    log "Error occurred while executing convert"
    reply "error"
  # set up behavior for when we successfully convert
  convert.stdout.on "data", ->
    log "Successful conversion! :)"
    log "here's the data: " + data
    reply data
  # pipe the SVG into the stdin of the process (starting it)
  convert.stdin.end svg

If I take the fork line out and replace it with something else, everything is hunky dory, but if I leave it in, I get:

> coffee src/coral_client.coffee
finished doing conversion to svg!
converting SVG to a PNG
Spawned child process running convert, pid 60823

/usr/bin/grep:1
(function (exports, require, module, __filename, __dirname) { ����
                                                              ^
SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

It makes no sense. I don't have any weird illegal unicode character like in this question, I don't believe I have any kind of parse error like in this one... I really don't know what's going on.

Could it be that CoffeeScript is somehow breaking the code? That seems really unlikely, but I don't know.

Community
  • 1
  • 1
limp_chimp
  • 13,475
  • 17
  • 66
  • 105
  • What file are you converting? Shouldn't it be `convert svg:somefilename png:-`? – hpaulj Oct 03 '13 at 23:40
  • Have you tried to compile this script, and then run the `js` with `node`? If the `js` looks good then the problem isn't with coffeescript. – hpaulj Oct 03 '13 at 23:43
  • @hpaulj the svg file is piped into the standard input of the process. – limp_chimp Oct 03 '13 at 23:58
  • @hpaulj I get the same error when I compile to javascript and run it with `node`. – limp_chimp Oct 04 '13 at 00:00
  • What happens if you remove all the whitespace from the `{` to the next non-whitespace character? (In `(function (exports, require, module, __filename, __dirname) { ����`) – celwell Oct 04 '13 at 00:31

1 Answers1

2

The error is in your use of fork. fork is for spawning Node processes, i.e. foo.js files. Use spawn instead.

I figured this out by running a stripped down version of your code, reading an image file and then passing it to your svgToPNG. The error message starts:

/usr/bin/env:1
(function (exports, require, module, __filename, __dirname) { ELF

The characters rendered in this copy/paste as ELF are the head characters of my binary /usr/bin/env file. So node.js fork is trying to compile the /usr/bin/env file. Reviewing the child_process documentation confirms this. The examples running things like ls and grep use spawn.

hpaulj
  • 221,503
  • 14
  • 230
  • 353