@freestyle-sh/with-nodejs 0.2.1 → 0.2.3
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.
- package/dist/index.d.ts +3 -0
- package/dist/index.js +17 -7
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,9 @@ declare class NodeJsRuntimeInstance extends VmWithInstance implements VmJavaScri
|
|
|
21
21
|
constructor(builder: VmNodeJs);
|
|
22
22
|
runCode<Result extends JSONValue = any>(args: string | {
|
|
23
23
|
code: string;
|
|
24
|
+
argv?: string[];
|
|
25
|
+
env?: Record<string, string>;
|
|
26
|
+
workdir?: string;
|
|
24
27
|
}): Promise<RunCodeResponse<Result>>;
|
|
25
28
|
install(options?: InstallOptions): Promise<InstallResult>;
|
|
26
29
|
}
|
package/dist/index.js
CHANGED
|
@@ -64,17 +64,27 @@ class NodeJsRuntimeInstance extends VmWithInstance {
|
|
|
64
64
|
this.builder = builder;
|
|
65
65
|
}
|
|
66
66
|
async runCode(args) {
|
|
67
|
-
const
|
|
67
|
+
const options = typeof args === "string" ? { code: args } : args;
|
|
68
|
+
const { code, argv, env, workdir } = options;
|
|
69
|
+
const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
|
|
70
|
+
const argvArgs = argv?.map(shellEscape).join(" ");
|
|
71
|
+
const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
|
|
72
|
+
const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
|
|
73
|
+
const command = `${cdPrefix}${envPrefix}node -e "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
|
|
68
74
|
const result = await this.vm.exec({
|
|
69
|
-
command
|
|
75
|
+
command
|
|
70
76
|
});
|
|
71
77
|
let parsedResult = void 0;
|
|
72
78
|
if (result.stdout) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
79
|
+
const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
|
|
80
|
+
const lastLine = lines[lines.length - 1];
|
|
81
|
+
if (lastLine) {
|
|
82
|
+
try {
|
|
83
|
+
parsedResult = JSON.parse(lastLine);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
if (result.stderr) {
|
|
86
|
+
`Failed to parse JSON output. Stderr: ${result.stderr}`;
|
|
87
|
+
}
|
|
78
88
|
}
|
|
79
89
|
}
|
|
80
90
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@freestyle-sh/with-nodejs",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"freestyle-sandboxes": "^0.1.
|
|
7
|
-
"@freestyle-sh/with-type-js": "^0.2.
|
|
6
|
+
"freestyle-sandboxes": "^0.1.24",
|
|
7
|
+
"@freestyle-sh/with-type-js": "^0.2.3"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "./dist/index.js",
|