Skip to content

Use nouse_stdio to avoid console.log pollution from destroying json response payload #12

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ best runtime available on the system.
Use the application environment (application key: `:execjs`, key: `:runtime`)
to set the runtime `Execjs` uses. Alternatively, the `EXECJS_RUNTIME`
environment variable can also be used to set the runtime.
### Additional Runtimes

It is possible to define a custom runtime to specify an exact path
to your runtime command:

```elixir
defmodule MyModule.Runtime do
import Execjs.Runtime

defruntime Node8,
command: "/Users/o_o/.nvm/versions/node/v8.9.3/bin/node",
runner: "node_runner.js.eex"
end

```



## Usage

Expand Down
2 changes: 1 addition & 1 deletion lib/execjs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule Execjs do

try do
port = Port.open({ :spawn_executable, command },
[:stream, :in, :binary, :eof, :hide, { :args, [tmpfile] }])
[:stream, :in, :binary,:nouse_stdio, :eof, :hide, { :args, [tmpfile] }])

extract_result(loop(port))
after
Expand Down
41 changes: 23 additions & 18 deletions priv/node_runner.js.eex
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
(function(program, runner) { runner(program); })(function() {
return <%= source %>;
}, function(program) {
var result;
try {
result = program();
try {
if (result === undefined) {
process.stdout.write('["ok"]');
} else {
process.stdout.write(JSON.stringify(['ok', result]));
}
} catch (err) {
process.stdout.write('["err"]');
const fs = require('fs');
(function(program, runner) {
runner(program);
})(
function() {
return <%= source %>;
}
} catch (err) {
process.stdout.write(JSON.stringify(['err', '' + err]));
}
});
, async function(program) {
let result;
try {
result = await program();
try {
if (result === undefined) {
fs.writeSync(4,'["ok"]');
} else {
fs.writeSync(4,JSON.stringify(['ok', result]));
}
} catch (err) {
fs.writeSync(4, '["err"]');
}
} catch (err) {
fs.writeSync(4,JSON.stringify(['err', '' + err]));
}
});