@freestyle-sh/with-java 0.2.0 → 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
@@ -1,9 +1,12 @@
1
- import { VmWithInstance, VmWith, CreateVmOptions } from 'freestyle-sandboxes';
1
+ import { VmWithInstance, VmWith, VmSpec } from 'freestyle-sandboxes';
2
2
  import { VmRunCodeInstance, JSONValue, RunCodeResponse, VmRunCode } from '@freestyle-sh/with-type-run-code';
3
3
 
4
4
  declare class VmJavaInstance extends VmWithInstance implements VmRunCodeInstance {
5
- runCode<Result extends JSONValue = any>({ code, }: {
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> {
@@ -11,7 +14,7 @@ declare class VmJava extends VmWith<VmJavaInstance> implements VmRunCode<VmRunCo
11
14
  constructor(options?: {
12
15
  version: string;
13
16
  });
14
- configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
17
+ configureSnapshotSpec(spec: VmSpec): VmSpec;
15
18
  createInstance(): VmJavaInstance;
16
19
  installServiceName(): string;
17
20
  }
package/dist/index.js CHANGED
@@ -1,19 +1,28 @@
1
- import { VmWithInstance, VmWith, VmTemplate } from 'freestyle-sandboxes';
1
+ import { VmWithInstance, VmWith, VmSpec } from 'freestyle-sandboxes';
2
2
 
3
3
  class VmJavaInstance extends VmWithInstance {
4
- async runCode({
5
- code
6
- }) {
4
+ async runCode(args) {
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}` : ""}`;
7
12
  const result = await this.vm.exec({
8
- command: `java -cp /tmp -c "${code.replace(/"/g, '\\"')}"`
13
+ command
9
14
  });
10
15
  let parsedResult = void 0;
11
16
  if (result.stdout) {
12
- try {
13
- parsedResult = JSON.parse(result.stdout);
14
- } catch (e) {
15
- if (result.stderr) {
16
- `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
+ }
17
26
  }
18
27
  }
19
28
  }
@@ -30,7 +39,7 @@ class VmJava extends VmWith {
30
39
  super();
31
40
  this.options = options;
32
41
  }
33
- configure(existingConfig) {
42
+ configureSnapshotSpec(spec) {
34
43
  const installScript = `#!/bin/bash
35
44
  set -e
36
45
  sudo apt-get update
@@ -42,8 +51,9 @@ echo "deb [signed-by=/usr/share/keyrings/corretto-keyring.gpg] https://apt.corre
42
51
  sudo apt-get update
43
52
  sudo apt-get install -y java-${this.options.version}-amazon-corretto-jdk libxi6 libxtst6 libxrender1
44
53
  `;
45
- const javaConfig = {
46
- template: new VmTemplate({
54
+ return this.composeSpecs(
55
+ spec,
56
+ new VmSpec({
47
57
  additionalFiles: {
48
58
  "/opt/install-java.sh": {
49
59
  content: installScript
@@ -61,8 +71,7 @@ sudo apt-get install -y java-${this.options.version}-amazon-corretto-jdk libxi6
61
71
  ]
62
72
  }
63
73
  })
64
- };
65
- return this.compose(existingConfig, javaConfig);
74
+ );
66
75
  }
67
76
  createInstance() {
68
77
  return new VmJavaInstance();
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@freestyle-sh/with-java",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "private": false,
5
5
  "dependencies": {
6
- "freestyle-sandboxes": "^0.1.2",
7
- "@freestyle-sh/with-type-run-code": "^0.2.0"
6
+ "freestyle-sandboxes": "^0.1.8",
7
+ "@freestyle-sh/with-type-run-code": "^0.2.2"
8
8
  },
9
9
  "type": "module",
10
10
  "main": "./dist/index.js",