Skip to content

Commit cc37b16

Browse files
committed
Better error messages & child process now exposed
1 parent bdb387d commit cc37b16

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

forked.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ try {
99
process.on('message', async message => {
1010
try {
1111
if (message.prop) {
12+
if (!forkedModule[message.prop]) {
13+
return process.send({error: 'TypeError: ' + message.prop + ' is not a function', stack: ''})
14+
}
1215
let response = await forkedModule[message.prop](...message.args);
1316
process.send({response});
1417
} else {
@@ -21,4 +24,4 @@ try {
2124
});
2225
} catch (err) {
2326
process.send({error: err.message, stack: err.stack, failed: true});
24-
}
27+
}

index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const path = require('path');
21
const cp = require('child_process');
2+
const path = require('path');
33
const util = require('util');
44

55
function getProcessArgs() {
@@ -26,6 +26,12 @@ function handleResponse(process, resolve, reject, source) {
2626
}
2727

2828
function fixStack(stack, source) {
29+
if (!stack) {
30+
return;
31+
}
32+
if (!source) {
33+
return stack;
34+
}
2935
let myStack = source.stack;
3036
let tail = myStack.substr(myStack.indexOf('\n', myStack.indexOf('\n') + 1));
3137
let head = stack.substr(0, stack.substr(0, stack.indexOf('forked.js')).lastIndexOf('\n'));
@@ -48,7 +54,7 @@ module.exports = (file, options = {
4854
let filePath = path.isAbsolute(file) ? file : path.join(calleePath, file);
4955
options.args.unshift(filePath, options.title || 'fork-require.js file ' + filePath);
5056

51-
let child = cp.fork('./forked.js', options.args , {
57+
let child = cp.fork(path.join(__dirname, 'forked.js'), options.args , {
5258
env: options.env,
5359
cwd: options.cwd,
5460
execArgv: options.execArgv,
@@ -63,15 +69,18 @@ module.exports = (file, options = {
6369
}
6470
});
6571

66-
return new Proxy(() => {}, {
72+
let proxy = new Proxy(() => { this.process = process }, {
6773
apply: (_, __, args) => {
6874
let source = new Error();
6975
return new Promise((resolve, reject) => {
7076
handleResponse(child, resolve, reject, options.fixStack && source);
7177
child.send({args});
7278
});
7379
},
74-
get: (_, prop) => {
80+
get: (obj, prop) => {
81+
if (obj[prop]) {
82+
return obj[prop];
83+
}
7584
let source = new Error();
7685
return function (...args) {
7786
return new Promise((resolve, reject) => {
@@ -81,4 +90,6 @@ module.exports = (file, options = {
8190
}
8291
}
8392
});
84-
};
93+
proxy._childProcess = child;
94+
return proxy;
95+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-require",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Allows to \"require\" a file, while forking it into a child process.",
55
"author": "Ravi Gairola <mallox@pyxzl.net>",
66
"homepage": "https://github.com/mallocator/fork-require",

0 commit comments

Comments
 (0)