@freestyle-sh/with-java 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.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,9 @@ import { VmRunCodeInstance, JSONValue, RunCodeResponse, VmRunCode } from '@frees
4
4
  declare class VmJavaInstance extends VmWithInstance implements VmRunCodeInstance {
5
5
  runCode<Result extends JSONValue = any>(args: string | {
6
6
  code: string;
7
+ argv?: string[];
8
+ env?: Record<string, string>;
9
+ workdir?: string;
7
10
  }): Promise<RunCodeResponse<Result>>;
8
11
  }
9
12
  declare class VmJava extends VmWith<VmJavaInstance> implements VmRunCode<VmRunCodeInstance> {
package/dist/index.js CHANGED
@@ -2,17 +2,27 @@ import { VmWithInstance, VmWith, VmSpec } from 'freestyle-sandboxes';
2
2
 
3
3
  class VmJavaInstance extends VmWithInstance {
4
4
  async runCode(args) {
5
- const code = typeof args === "string" ? args : args.code;
5
+ const options = typeof args === "string" ? { code: args } : args;
6
+ const { code, argv, env, workdir } = options;
7
+ const shellEscape = (value) => `'${value.replace(/'/g, "'\\''")}'`;
8
+ const argvArgs = argv?.map(shellEscape).join(" ");
9
+ const envPrefix = env ? `${Object.entries(env).map(([key, value]) => `${key}=${shellEscape(value)}`).join(" ")} ` : "";
10
+ const cdPrefix = workdir ? `cd ${shellEscape(workdir)} && ` : "";
11
+ const command = `${cdPrefix}${envPrefix}java -cp /tmp -c "${code.replace(/"/g, '\\"')}"${argvArgs ? ` -- ${argvArgs}` : ""}`;
6
12
  const result = await this.vm.exec({
7
- command: `java -cp /tmp -c "${code.replace(/"/g, '\\"')}"`
13
+ command
8
14
  });
9
15
  let parsedResult = void 0;
10
16
  if (result.stdout) {
11
- try {
12
- parsedResult = JSON.parse(result.stdout);
13
- } catch (e) {
14
- if (result.stderr) {
15
- `Failed to parse JSON output. Stderr: ${result.stderr}`;
17
+ const lines = result.stdout.split(/\r?\n/).map((line) => line.trim()).filter((line) => line.length > 0);
18
+ const lastLine = lines[lines.length - 1];
19
+ if (lastLine) {
20
+ try {
21
+ parsedResult = JSON.parse(lastLine);
22
+ } catch (e) {
23
+ if (result.stderr) {
24
+ `Failed to parse JSON output. Stderr: ${result.stderr}`;
25
+ }
16
26
  }
17
27
  }
18
28
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-java",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "freestyle-sandboxes": "^0.1.8",
7
- "@freestyle-sh/with-type-run-code": "^0.2.1"
7
+ "@freestyle-sh/with-type-run-code": "^0.2.2"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",