@freestyle-sh/with-java 0.2.0
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 +19 -0
- package/dist/index.js +75 -0
- package/package.json +25 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { VmWithInstance, VmWith, CreateVmOptions } from 'freestyle-sandboxes';
|
|
2
|
+
import { VmRunCodeInstance, JSONValue, RunCodeResponse, VmRunCode } from '@freestyle-sh/with-type-run-code';
|
|
3
|
+
|
|
4
|
+
declare class VmJavaInstance extends VmWithInstance implements VmRunCodeInstance {
|
|
5
|
+
runCode<Result extends JSONValue = any>({ code, }: {
|
|
6
|
+
code: string;
|
|
7
|
+
}): Promise<RunCodeResponse<Result>>;
|
|
8
|
+
}
|
|
9
|
+
declare class VmJava extends VmWith<VmJavaInstance> implements VmRunCode<VmRunCodeInstance> {
|
|
10
|
+
private options;
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
version: string;
|
|
13
|
+
});
|
|
14
|
+
configure(existingConfig: CreateVmOptions): CreateVmOptions | Promise<CreateVmOptions>;
|
|
15
|
+
createInstance(): VmJavaInstance;
|
|
16
|
+
installServiceName(): string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { VmJava, VmJavaInstance };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { VmWithInstance, VmWith, VmTemplate } from 'freestyle-sandboxes';
|
|
2
|
+
|
|
3
|
+
class VmJavaInstance extends VmWithInstance {
|
|
4
|
+
async runCode({
|
|
5
|
+
code
|
|
6
|
+
}) {
|
|
7
|
+
const result = await this.vm.exec({
|
|
8
|
+
command: `java -cp /tmp -c "${code.replace(/"/g, '\\"')}"`
|
|
9
|
+
});
|
|
10
|
+
let parsedResult = void 0;
|
|
11
|
+
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
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
result: parsedResult,
|
|
22
|
+
stdout: result.stdout ?? void 0,
|
|
23
|
+
stderr: result.stderr ?? void 0,
|
|
24
|
+
statusCode: result.statusCode ?? -1
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
class VmJava extends VmWith {
|
|
29
|
+
constructor(options = { version: "21" }) {
|
|
30
|
+
super();
|
|
31
|
+
this.options = options;
|
|
32
|
+
}
|
|
33
|
+
configure(existingConfig) {
|
|
34
|
+
const installScript = `#!/bin/bash
|
|
35
|
+
set -e
|
|
36
|
+
sudo apt-get update
|
|
37
|
+
sudo apt-get install -y ca-certificates apt-transport-https gnupg wget
|
|
38
|
+
|
|
39
|
+
wget -O - https://apt.corretto.aws/corretto.key | sudo gpg --dearmor -o /usr/share/keyrings/corretto-keyring.gpg && \\
|
|
40
|
+
echo "deb [signed-by=/usr/share/keyrings/corretto-keyring.gpg] https://apt.corretto.aws stable main" | sudo tee /etc/apt/sources.list.d/corretto.list
|
|
41
|
+
|
|
42
|
+
sudo apt-get update
|
|
43
|
+
sudo apt-get install -y java-${this.options.version}-amazon-corretto-jdk libxi6 libxtst6 libxrender1
|
|
44
|
+
`;
|
|
45
|
+
const javaConfig = {
|
|
46
|
+
template: new VmTemplate({
|
|
47
|
+
additionalFiles: {
|
|
48
|
+
"/opt/install-java.sh": {
|
|
49
|
+
content: installScript
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
systemd: {
|
|
53
|
+
services: [
|
|
54
|
+
{
|
|
55
|
+
name: "install-java",
|
|
56
|
+
mode: "oneshot",
|
|
57
|
+
deleteAfterSuccess: true,
|
|
58
|
+
exec: ["bash /opt/install-java.sh"],
|
|
59
|
+
timeoutSec: 300
|
|
60
|
+
}
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
};
|
|
65
|
+
return this.compose(existingConfig, javaConfig);
|
|
66
|
+
}
|
|
67
|
+
createInstance() {
|
|
68
|
+
return new VmJavaInstance();
|
|
69
|
+
}
|
|
70
|
+
installServiceName() {
|
|
71
|
+
return "install-java.service";
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { VmJava, VmJavaInstance };
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@freestyle-sh/with-java",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"freestyle-sandboxes": "^0.1.2",
|
|
7
|
+
"@freestyle-sh/with-type-run-code": "^0.2.0"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"source": "./src/index.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "pkgroll"
|
|
24
|
+
}
|
|
25
|
+
}
|