Skip to content

Commit 31614b1

Browse files
Sam GotoSam Goto
authored andcommitted
Process WIP
1 parent 1bb41f5 commit 31614b1

File tree

9 files changed

+477
-3
lines changed

9 files changed

+477
-3
lines changed

docker/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM python:3.8-slim-buster
22
WORKDIR /src
3-
COPY ./test.py /src
4-
CMD ["python3", "test.py"]
3+
# COPY ./test.py /src
4+
# CMD ["python3", "test.py"]

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
"build": "rimraf ./build && tsc"
55
},
66
"devDependencies": {
7+
"@types/watch": "^1.0.3",
78
"nodemon": "^2.0.21",
89
"rimraf": "^4.4.0",
910
"typescript": "^5.0.2"
11+
},
12+
"dependencies": {
13+
"child_process": "^1.0.2",
14+
"crypto": "^1.0.1"
1015
}
1116
}

src/config.ts

Whitespace-only changes.

src/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
console.log("fit")
1+
import Process from "./process";
2+
3+
const process = new Process();
4+
process
5+
.prepTransfer("print('Hi')", "python3")
6+
.then((containerID) => console.log(containerID))
7+
.catch((e) => console.log(e));
8+
// process
9+
// .initContainer("python3")
10+
// .then((containerID) => console.log(containerID))
11+
// .catch((e) => console.log(e));

src/process.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as child from "child_process";
2+
import crypto from "crypto";
3+
import fs from "fs";
4+
import path from "path";
5+
import {
6+
ContainerInitialization,
7+
container,
8+
fileFormat,
9+
} from "./types/process";
10+
11+
class Process {
12+
constructor() {}
13+
14+
private _fileFormats: Record<container, fileFormat> = {
15+
python3: "py",
16+
javascript: "js",
17+
};
18+
/**
19+
* Creates an appropriate docker container
20+
* to execute the target code
21+
*/
22+
async initContainer(container: container): Promise<ContainerInitialization> {
23+
return new Promise((reject, resolve) => {
24+
const initCommand = `docker create ${container}`;
25+
26+
child.exec(initCommand, (error, containerID, stderr) => {
27+
if (error) {
28+
reject(error.message);
29+
} else if (stderr) {
30+
reject(stderr);
31+
} else {
32+
resolve(containerID);
33+
}
34+
});
35+
});
36+
}
37+
38+
/**
39+
* writes a code into a temp file
40+
*/
41+
async prepTransfer(context: string, container: container): Promise<string> {
42+
return new Promise((reject, resolve) => {
43+
const fileName = crypto.randomBytes(32).toString("hex");
44+
const filePath = path.join(
45+
__dirname,
46+
"..",
47+
"temp",
48+
`${fileName}.${this._fileFormats[container]}`
49+
);
50+
51+
fs.writeFile(filePath, context, (error) => {
52+
if (error) {
53+
reject(error.message);
54+
} else {
55+
resolve(filePath);
56+
}
57+
});
58+
});
59+
}
60+
61+
/**
62+
* copies the target code into the newly created
63+
* isolated container
64+
*/
65+
async copyContext(containerID: string): Promise<void> {
66+
return new Promise(async (reject, resolve) => {
67+
// const initCommand = `docker cp ${} ${containerID}/src`;
68+
// child.exec(initCommand, (error, containerID, stderr) => {
69+
// if (error) {
70+
// reject(error.message);
71+
// } else if (stderr) {
72+
// reject(stderr);
73+
// } else {
74+
// resolve(containerID);
75+
// }
76+
// });
77+
});
78+
}
79+
}
80+
81+
export default Process;

src/types/process.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type container = "python3" | "javascript";
2+
export type fileFormat = "py" | "js";
3+
export type ContainerInitialization = string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('Hi')

0 commit comments

Comments
 (0)