Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When calling Edge.js from C#, how do you hook stdout and stderr?

Background

I am working on a C# program which currently runs Node via Process.Start(). I am capturing the stdout and stderr from this child process and redirecting it for my own reasons. I am looking into replacing the invocation of Node.exe with a call to Edge.js instead. In order to be able to do this I must be able to reliably capture stdout and stderr from the Javascript running within Edge, and get the messages back into my C# application.

Approach 1

I'll describe this approach for completeness in case anybody recommends it :)

If the Edge process terminates, it is fairly easy to deal with this by simply declaring a msgs array and overwriting process.stdout.write and process.stderr.write with new functions that accumulate messages on that array, then at the end, simply return the msgs array. Example:

var msgs = [];
process.stdout.write = function (string) {
    msgs.push({ stream: 'o', message : string });
};
process.stderr.write = function (string) {
    msgs.push({ stream: 'e', message: string });
};

// Return to caller.
var result = { messages: msgs; ...other stuff... };
callback(null, result);

Obviously this only works if the Edge code terminates, and msgs may grow large in the worst case. However, it is likely to perform well because only one marshalling call is necessary to get all the messages back.

Approach 2

This is a little harder to explain. Instead of accumulating messages, we "hook" stdout and stderr using a delegate we send in from C#. In the C#, we create an object that we will pass into Edge, and that object has a property called stdoutHook:

dynamic payload = new ExpandoObject();
payload.stdoutHook = GetStdoutHook();

public Func<object, Task<object>> GetStdoutHook()
{
    Func<object, Task<object>> hook = (message) =>
    {
        TheLogger.LogMessage((message as string).Trim());
        return Task.FromResult<object>(null);
    };

    return hook;
}

I could really get away with an Action, but Edge appears to require the Func<object, Task<object>>, it won't proxy the function otherwise. Then, in the Javascript, we can detect that function and use it like this:

var func = Edge.Func(@"
    return function(payload, callback) {
        if (typeof (payload.stdoutHook) === 'function') {
            process.stdout.write = payload.stdoutHook;
        }

        // do lots of stuff while stdout and stderr are hooked...
        var what = require('whatever');
        what.futz();

        // terminate.
        callback(null, result);
}");

dynamic result = func(payload).Result;

Questions

Q1. Both of these techniques seem to work, but is there a better way of doing this, something built-in to Edge perhaps that I have missed? Both solutions are invasive - they require some shim code to wrap the actual work that is to be done in Edge. This is not the end of the world, but it would be better if there was a non-invasive method.

Q2. In approach 2, where I have to return a task here

return Task.FromResult<object>(null);

it feels wrong to be returning an already completed "null task". But is there another way of writing this?

Q3. Do I need to be more rigorous in the Javascript code when hooking stdout and stderr? I note in double-edge.js there is this code, frankly I am not sure what is happening here, but it is quite a bit more complex than my crude overwriting of process.stdout.write :-)

// Fix #176 for GUI applications on Windows
try {
    var stdout = process.stdout;
}
catch (e) {
    // This is a Windows GUI application without stdout and stderr defined.
    // Define process.stdout and process.stderr so that all output is discarded. 
    (function () {
        var stream = require('stream');
        var NullStream = function (o) {
            stream.Writable.call(this);
            this._write = function (c, e, cb) { cb && cb(); };
        }
        require('util').inherits(NullStream, stream.Writable);
        var nullStream = new NullStream();
        process.__defineGetter__('stdout', function () { return nullStream; });
        process.__defineGetter__('stderr', function () { return nullStream; });
    })();
}
like image 753
Philip Daniels Avatar asked Mar 30 '15 21:03

Philip Daniels


1 Answers

Q1: There isn't anything built into Edge that would make capturing stdout or stderr of Node.js code automatic when calling Node from CLR. At some point I thought of writing an extension of Edge that would make marshaling Streams across CLR/V8 boundary easy. Under the hood it would be very similar to your Approach 2. It could be done as a standalone module on top of Edge.

Q2: Returning a completed task is very appropriate in this case. Your function has captured the Node.js output, processed it, and has in fact "completed" in that sense. Returning a task completed with Null is really a moral equivalent of returning from an Action.

Q3: The code you are pointing to is only relevant in Windows GUI applications, not Console applications. If you are writing a Console application, simply overriding write should suffice at the level of the Node.js code you pass to Edge.js. Note that the signature of write in Node allows an optional encoding parameter to be passed in. You seem to ignore it both in Approach 1 and 2. In particular in Approach 2 I would suggest wrapping the JavaScript proxy to C# callback into a JavaScript function that normalizes the parameters before assigning it to process.stdout.write. Otherwise Edge.js code may assume that the encoding parameter passed to a write call is a callback function which would follow the Edge.js calling convention.

like image 165
Tomasz Janczuk Avatar answered Sep 18 '22 16:09

Tomasz Janczuk