parallel-park 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -30,7 +30,7 @@ debug("reading input data...");
|
|
|
30
30
|
})
|
|
31
31
|
.catch(onError);
|
|
32
32
|
function onReady(inputs, fnString, callingFile) {
|
|
33
|
-
debug("in onReady", { inputs, fnString, callingFile });
|
|
33
|
+
debug("in onReady %o", { inputs, fnString, callingFile });
|
|
34
34
|
// Relevant when callingFile is eg. "REPL2" (from Node.js repl)
|
|
35
35
|
if (!path_1.default.isAbsolute(callingFile)) {
|
|
36
36
|
callingFile = path_1.default.join(process.cwd(), "fake-path.js");
|
|
@@ -50,11 +50,11 @@ function onReady(inputs, fnString, callingFile) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
function onSuccess(data) {
|
|
53
|
-
debug("in onSuccess", { data });
|
|
53
|
+
debug("in onSuccess %o", { data });
|
|
54
54
|
commsOut.end(JSON.stringify({ type: "success", data }));
|
|
55
55
|
}
|
|
56
56
|
function onError(error) {
|
|
57
|
-
debug("in onError", { error });
|
|
57
|
+
debug("in onError %o", { error });
|
|
58
58
|
commsOut.end(JSON.stringify({
|
|
59
59
|
type: "error",
|
|
60
60
|
error: {
|
package/dist/in-child-process.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.inChildProcess = void 0;
|
|
|
7
7
|
const child_process_1 = __importDefault(require("child_process"));
|
|
8
8
|
const error_utils_1 = require("@suchipi/error-utils");
|
|
9
9
|
const debug_1 = __importDefault(require("debug"));
|
|
10
|
+
const util_1 = __importDefault(require("util"));
|
|
10
11
|
const read_until_end_1 = require("./read-until-end");
|
|
11
12
|
const debug = (0, debug_1.default)("parallel-park:in-child-process");
|
|
12
13
|
const runnerPath = require.resolve("../dist/child-process-worker");
|
|
@@ -23,7 +24,7 @@ const inChildProcess = (...args) => {
|
|
|
23
24
|
const here = new error_utils_1.ParsedError(new Error("here"));
|
|
24
25
|
const callingFrame = here.stackFrames[1];
|
|
25
26
|
const callingFile = (_a = callingFrame === null || callingFrame === void 0 ? void 0 : callingFrame.fileName) !== null && _a !== void 0 ? _a : "unknown file";
|
|
26
|
-
debug("spawning child process:", [process.argv[0], runnerPath]);
|
|
27
|
+
debug("spawning child process: %o", [process.argv[0], runnerPath]);
|
|
27
28
|
const child = child_process_1.default.spawn(process.argv[0], [runnerPath], {
|
|
28
29
|
stdio: ["inherit", "inherit", "inherit", "pipe", "pipe"],
|
|
29
30
|
});
|
|
@@ -37,16 +38,16 @@ const inChildProcess = (...args) => {
|
|
|
37
38
|
functionToRun.toString(),
|
|
38
39
|
callingFile,
|
|
39
40
|
]);
|
|
40
|
-
debug("sending inputs to child process:", dataToSend);
|
|
41
|
+
debug("sending inputs to child process: %o", dataToSend);
|
|
41
42
|
commsOut.end(dataToSend, "utf-8");
|
|
42
43
|
});
|
|
43
44
|
let receivedData = "";
|
|
44
45
|
(0, read_until_end_1.readUntilEnd)(commsIn).then((data) => {
|
|
45
|
-
debug("received data from child process");
|
|
46
|
+
debug("received data from child process: %o", data);
|
|
46
47
|
receivedData = data;
|
|
47
48
|
});
|
|
48
49
|
child.on("close", (code, signal) => {
|
|
49
|
-
debug("child process closed:", { code, signal });
|
|
50
|
+
debug("child process closed: %o", { code, signal });
|
|
50
51
|
if (code !== 0) {
|
|
51
52
|
reject(new Error(`Child process exited with nonzero status code: ${JSON.stringify({
|
|
52
53
|
code,
|
|
@@ -55,13 +56,22 @@ const inChildProcess = (...args) => {
|
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
57
58
|
debug("parsing received data from child process...");
|
|
58
|
-
|
|
59
|
+
let result;
|
|
60
|
+
try {
|
|
61
|
+
result = JSON.parse(receivedData);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
reject(new Error(`parallel-park error: failed to parse received data as JSON. data was: ${util_1.default.inspect(receivedData, { colors: true, depth: Infinity })}`));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
59
67
|
switch (result.type) {
|
|
60
68
|
case "success": {
|
|
69
|
+
debug("child process finished successfully with result: %o", result.data);
|
|
61
70
|
resolve(result.data);
|
|
62
71
|
break;
|
|
63
72
|
}
|
|
64
73
|
case "error": {
|
|
74
|
+
debug("child process errored: %o", result.error);
|
|
65
75
|
const error = new Error(result.error.message);
|
|
66
76
|
Object.defineProperty(error, "name", { value: result.error.name });
|
|
67
77
|
Object.defineProperty(error, "stack", {
|
package/dist/read-until-end.js
CHANGED
|
@@ -8,16 +8,17 @@ const debug_1 = __importDefault(require("debug"));
|
|
|
8
8
|
const debug = (0, debug_1.default)("parallel-park:read-until-end");
|
|
9
9
|
let streamId = 0;
|
|
10
10
|
function readUntilEnd(stream) {
|
|
11
|
-
const id = streamId
|
|
11
|
+
const id = `${process.pid}-${streamId}`;
|
|
12
12
|
streamId++;
|
|
13
13
|
return new Promise((resolve) => {
|
|
14
14
|
let data = "";
|
|
15
15
|
stream.on("data", (chunk) => {
|
|
16
|
-
|
|
17
|
-
data
|
|
16
|
+
const chunkStr = chunk.toString("utf-8");
|
|
17
|
+
debug("received data chunk from stream %s: %o", id, chunkStr);
|
|
18
|
+
data += chunkStr;
|
|
18
19
|
});
|
|
19
20
|
stream.on("close", () => {
|
|
20
|
-
debug(
|
|
21
|
+
debug("stream %s closed; resolving with: %o", id, data);
|
|
21
22
|
resolve(data);
|
|
22
23
|
});
|
|
23
24
|
});
|
package/package.json
CHANGED
|
@@ -32,7 +32,7 @@ readUntilEnd(commsIn)
|
|
|
32
32
|
.catch(onError);
|
|
33
33
|
|
|
34
34
|
function onReady(inputs: any, fnString: string, callingFile: string) {
|
|
35
|
-
debug("in onReady", { inputs, fnString, callingFile });
|
|
35
|
+
debug("in onReady %o", { inputs, fnString, callingFile });
|
|
36
36
|
|
|
37
37
|
// Relevant when callingFile is eg. "REPL2" (from Node.js repl)
|
|
38
38
|
if (!path.isAbsolute(callingFile)) {
|
|
@@ -65,13 +65,13 @@ function onReady(inputs: any, fnString: string, callingFile: string) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function onSuccess(data: any) {
|
|
68
|
-
debug("in onSuccess", { data });
|
|
68
|
+
debug("in onSuccess %o", { data });
|
|
69
69
|
|
|
70
70
|
commsOut.end(JSON.stringify({ type: "success", data }));
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
function onError(error: Error) {
|
|
74
|
-
debug("in onError", { error });
|
|
74
|
+
debug("in onError %o", { error });
|
|
75
75
|
|
|
76
76
|
commsOut.end(
|
|
77
77
|
JSON.stringify({
|
package/src/in-child-process.ts
CHANGED
|
@@ -2,6 +2,7 @@ import child_process from "child_process";
|
|
|
2
2
|
import type * as stream from "stream";
|
|
3
3
|
import { ParsedError } from "@suchipi/error-utils";
|
|
4
4
|
import makeDebug from "debug";
|
|
5
|
+
import util from "util";
|
|
5
6
|
import { readUntilEnd } from "./read-until-end";
|
|
6
7
|
|
|
7
8
|
const debug = makeDebug("parallel-park:in-child-process");
|
|
@@ -38,7 +39,7 @@ export const inChildProcess: InChildProcess = (...args: Array<any>) => {
|
|
|
38
39
|
const callingFrame = here.stackFrames[1];
|
|
39
40
|
const callingFile = callingFrame?.fileName ?? "unknown file";
|
|
40
41
|
|
|
41
|
-
debug("spawning child process:", [process.argv[0], runnerPath]);
|
|
42
|
+
debug("spawning child process: %o", [process.argv[0], runnerPath]);
|
|
42
43
|
|
|
43
44
|
const child = child_process.spawn(process.argv[0], [runnerPath], {
|
|
44
45
|
stdio: ["inherit", "inherit", "inherit", "pipe", "pipe"],
|
|
@@ -56,18 +57,18 @@ export const inChildProcess: InChildProcess = (...args: Array<any>) => {
|
|
|
56
57
|
functionToRun.toString(),
|
|
57
58
|
callingFile,
|
|
58
59
|
]);
|
|
59
|
-
debug("sending inputs to child process:", dataToSend);
|
|
60
|
+
debug("sending inputs to child process: %o", dataToSend);
|
|
60
61
|
commsOut.end(dataToSend, "utf-8");
|
|
61
62
|
});
|
|
62
63
|
|
|
63
64
|
let receivedData = "";
|
|
64
65
|
readUntilEnd(commsIn).then((data) => {
|
|
65
|
-
debug("received data from child process");
|
|
66
|
+
debug("received data from child process: %o", data);
|
|
66
67
|
receivedData = data;
|
|
67
68
|
});
|
|
68
69
|
|
|
69
70
|
child.on("close", (code, signal) => {
|
|
70
|
-
debug("child process closed:", { code, signal });
|
|
71
|
+
debug("child process closed: %o", { code, signal });
|
|
71
72
|
|
|
72
73
|
if (code !== 0) {
|
|
73
74
|
reject(
|
|
@@ -80,13 +81,31 @@ export const inChildProcess: InChildProcess = (...args: Array<any>) => {
|
|
|
80
81
|
);
|
|
81
82
|
} else {
|
|
82
83
|
debug("parsing received data from child process...");
|
|
83
|
-
|
|
84
|
+
let result: any;
|
|
85
|
+
try {
|
|
86
|
+
result = JSON.parse(receivedData);
|
|
87
|
+
} catch (err) {
|
|
88
|
+
reject(
|
|
89
|
+
new Error(
|
|
90
|
+
`parallel-park error: failed to parse received data as JSON. data was: ${util.inspect(
|
|
91
|
+
receivedData,
|
|
92
|
+
{ colors: true, depth: Infinity }
|
|
93
|
+
)}`
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
84
98
|
switch (result.type) {
|
|
85
99
|
case "success": {
|
|
100
|
+
debug(
|
|
101
|
+
"child process finished successfully with result: %o",
|
|
102
|
+
result.data
|
|
103
|
+
);
|
|
86
104
|
resolve(result.data);
|
|
87
105
|
break;
|
|
88
106
|
}
|
|
89
107
|
case "error": {
|
|
108
|
+
debug("child process errored: %o", result.error);
|
|
90
109
|
const error = new Error(result.error.message);
|
|
91
110
|
Object.defineProperty(error, "name", { value: result.error.name });
|
|
92
111
|
Object.defineProperty(error, "stack", {
|
package/src/read-until-end.ts
CHANGED
|
@@ -5,19 +5,20 @@ const debug = makeDebug("parallel-park:read-until-end");
|
|
|
5
5
|
let streamId = 0;
|
|
6
6
|
|
|
7
7
|
export function readUntilEnd(stream: stream.Readable): Promise<string> {
|
|
8
|
-
const id = streamId
|
|
8
|
+
const id = `${process.pid}-${streamId}`;
|
|
9
9
|
streamId++;
|
|
10
10
|
|
|
11
11
|
return new Promise((resolve) => {
|
|
12
12
|
let data = "";
|
|
13
13
|
|
|
14
14
|
stream.on("data", (chunk) => {
|
|
15
|
-
|
|
16
|
-
data
|
|
15
|
+
const chunkStr = chunk.toString("utf-8");
|
|
16
|
+
debug("received data chunk from stream %s: %o", id, chunkStr);
|
|
17
|
+
data += chunkStr;
|
|
17
18
|
});
|
|
18
19
|
|
|
19
20
|
stream.on("close", () => {
|
|
20
|
-
debug(
|
|
21
|
+
debug("stream %s closed; resolving with: %o", id, data);
|
|
21
22
|
resolve(data);
|
|
22
23
|
});
|
|
23
24
|
});
|