From 3bba0c002793900ec8ae3a3d3bfed5c5eb3b5612 Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Thu, 17 Aug 2023 22:35:21 +0800
Subject: [PATCH 1/6] Add files via upload

---
 src/tracers/JuliaTracer.ts | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 src/tracers/JuliaTracer.ts

diff --git a/src/tracers/JuliaTracer.ts b/src/tracers/JuliaTracer.ts
new file mode 100644
index 0000000..50f7025
--- /dev/null
+++ b/src/tracers/JuliaTracer.ts
@@ -0,0 +1,36 @@
+import path from 'path';
+import { Release, Tracer } from 'tracers/Tracer';
+import express from 'express';
+import { GitHubApi } from 'utils/apis';
+
+export class JuliaTracer extends Tracer {
+  readonly workerPath: string;
+  tagName?: string;
+
+  constructor() {
+    super('julia');
+    this.workerPath = path.resolve(__dirname, 'worker.js');
+  }
+
+  async build(release: Release) {
+    const {tag_name} = release;
+    this.tagName = tag_name;
+  }
+
+  route(router: express.Router) {
+    router.get(`/${this.lang}`, (req, res) => {
+      if (!this.tagName) throw new Error('JuliaTracer has not been built yet.');
+      const version = this.tagName.slice(1);
+      res.redirect(`https://unpkg.com/algorithm-visualizer@${version}/dist/algorithm-visualizer.umd.js`);
+    });
+    router.get(`/${this.lang}/worker`, (req, res) => res.sendFile(this.workerPath));
+  }
+
+  async update(release?: Release) {
+    if (release) {
+      return this.build(release);
+    }
+    const {data} = await GitHubApi.getLatestRelease('likewu', `tracers.${this.lang}`);
+    return this.build(data);
+  }
+}

From 3c53ac5cb40191a0073bc1c79d23cdd0e9698ac6 Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Thu, 17 Aug 2023 22:36:25 +0800
Subject: [PATCH 2/6] Update Server.ts

---
 src/Server.ts | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/Server.ts b/src/Server.ts
index 637b2df..2932cd5 100644
--- a/src/Server.ts
+++ b/src/Server.ts
@@ -14,6 +14,8 @@ import { errorHandlerMiddleware, frontendMiddleware, redirectMiddleware } from '
 import { execute, issueHttpsCertificate, pull } from 'utils/misc';
 import { frontendBuildDir, frontendBuiltDir, frontendDir, rootDir } from 'config/paths';
 
+import fs from 'fs-extra';
+
 const Webhook = require('express-github-webhook');
 
 export default class Server {
@@ -91,7 +93,23 @@ export default class Server {
   };
 
   async updateFrontend(commit?: string) {
-    await pull(frontendDir, 'algorithm-visualizer', commit);
+    if (fs.pathExistsSync(frontendDir)) {
+      await execute(`git fetch`, {
+        cwd: frontendDir,
+        stdout: process.stdout,
+        stderr: process.stderr,
+      });
+    } else {
+      await execute(`git clone https://github.com/likewu/algorithm-visualizer.git ${frontendDir}`, {
+        stdout: process.stdout,
+        stderr: process.stderr,
+      });
+    }
+    await execute(`git reset --hard origin/master`, {
+      cwd: frontendDir,
+      stdout: process.stdout,
+      stderr: process.stderr,
+    });
     await execute([
       'npm install',
       'npm run build',

From b275d366ead7643f045b5645cf817f64a3526227 Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Thu, 17 Aug 2023 22:38:39 +0800
Subject: [PATCH 3/6] Create worker.js

---
 src/tracers/julia/worker.js | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
 create mode 100644 src/tracers/julia/worker.js

diff --git a/src/tracers/julia/worker.js b/src/tracers/julia/worker.js
new file mode 100644
index 0000000..e91e3e5
--- /dev/null
+++ b/src/tracers/julia/worker.js
@@ -0,0 +1,14 @@
+const process = { env: { ALGORITHM_VISUALIZER: '1' } };
+importScripts('/api/tracers/julia');
+
+const sandbox = code => {
+  const require = name => ({ 'algorithm-visualizer': AlgorithmVisualizer }[name]); // fake require
+  eval(code);
+};
+
+onmessage = e => {
+  const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`));
+  const code = lines.join('\n');
+  sandbox(code);
+  postMessage(AlgorithmVisualizer.Commander.commands);
+};

From f4fda1eb3ad83b58baa40e566acb3e6e149d8c5c Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Thu, 17 Aug 2023 22:39:42 +0800
Subject: [PATCH 4/6] Rename src/tracers/JuliaTracer.ts to
 src/tracers/julia/JuliaTracer.ts

---
 src/tracers/{ => julia}/JuliaTracer.ts | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename src/tracers/{ => julia}/JuliaTracer.ts (100%)

diff --git a/src/tracers/JuliaTracer.ts b/src/tracers/julia/JuliaTracer.ts
similarity index 100%
rename from src/tracers/JuliaTracer.ts
rename to src/tracers/julia/JuliaTracer.ts

From cb2090d1050f51bb80c92e4e53d54ce4b45ceebc Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Fri, 18 Aug 2023 11:12:43 +0800
Subject: [PATCH 5/6] ok

---
 src/config/environments.ts | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/config/environments.ts b/src/config/environments.ts
index 912d9c8..d92f9d2 100644
--- a/src/config/environments.ts
+++ b/src/config/environments.ts
@@ -46,10 +46,10 @@ const missingVars = [
   ...(isEnabled(WEBHOOK_ENABLED) ? [
     'WEBHOOK_SECRET',
   ] : []),
-  'GITHUB_CLIENT_ID',
-  'GITHUB_CLIENT_SECRET',
-  'AWS_ACCESS_KEY_ID',
-  'AWS_SECRET_ACCESS_KEY',
+  //'GITHUB_CLIENT_ID',
+  //'GITHUB_CLIENT_SECRET',
+  //'AWS_ACCESS_KEY_ID',
+  //'AWS_SECRET_ACCESS_KEY',
 ].filter(variable => process.env[variable] === undefined);
 if (missingVars.length) throw new Error(`The following environment variables are missing: ${missingVars.join(', ')}`);
 

From cf827841603bc8734ddb81b24b7a789d2ce79a2d Mon Sep 17 00:00:00 2001
From: likewu <likewu@126.com>
Date: Fri, 18 Aug 2023 22:45:59 +0800
Subject: [PATCH 6/6] lsp

---
 src/config/constants.ts          |  4 +--
 src/tracers/LspTracer.ts         | 33 ++++++++++++++++++++++++
 src/tracers/RunTracer.ts         | 44 ++++++++++++++++++++++++++++++++
 src/tracers/julia/JuliaTracer.ts | 27 ++++----------------
 src/tracers/julia/worker.js      | 14 ----------
 5 files changed, 84 insertions(+), 38 deletions(-)
 create mode 100644 src/tracers/LspTracer.ts
 create mode 100644 src/tracers/RunTracer.ts
 delete mode 100644 src/tracers/julia/worker.js

diff --git a/src/config/constants.ts b/src/config/constants.ts
index 34f9787..97c5008 100644
--- a/src/config/constants.ts
+++ b/src/config/constants.ts
@@ -1,2 +1,2 @@
-export const memoryLimit = 256; // in megabytes
-export const timeLimit = 5000; // in milliseconds
+export const memoryLimit = 512; // in megabytes
+export const timeLimit = 50000; // in milliseconds
diff --git a/src/tracers/LspTracer.ts b/src/tracers/LspTracer.ts
new file mode 100644
index 0000000..cc415b6
--- /dev/null
+++ b/src/tracers/LspTracer.ts
@@ -0,0 +1,33 @@
+import AWS from 'aws-sdk';
+import express from 'express';
+import { Release, Tracer } from 'tracers/Tracer';
+import { awsAccessKeyId, awsSecretAccessKey } from 'config/environments';
+import { BadRequest } from 'ts-httpexceptions';
+
+export class LspTracer extends Tracer {
+  static lambda = new AWS.Lambda({
+    region: 'us-east-2',
+    accessKeyId: awsAccessKeyId,
+    secretAccessKey: awsSecretAccessKey,
+  });
+
+  async build(release: Release) {
+  }
+
+  route(router: express.Router) {
+    router.post(`/${this.lang}`, (req, res, next) => {
+      const {code} = req.body;
+      LspTracer.lambda.invoke({
+        FunctionName: `extractor-${this.lang}`,
+        InvocationType: 'RequestResponse',
+        Payload: JSON.stringify(code),
+      }, function (err, data) {
+        if (err) return next(err);
+        if (typeof data.Payload !== 'string') return next(new Error('Unexpected Payload Type'));
+        const payload = JSON.parse(data.Payload);
+        if (!payload.success) return next(new BadRequest(payload.errorMessage));
+        res.send(payload.commands);
+      });
+    });
+  }
+}
diff --git a/src/tracers/RunTracer.ts b/src/tracers/RunTracer.ts
new file mode 100644
index 0000000..d5ad01c
--- /dev/null
+++ b/src/tracers/RunTracer.ts
@@ -0,0 +1,44 @@
+import path from 'path';
+import { Release, Tracer } from 'tracers/Tracer';
+import express from 'express';
+import uuid from 'uuid';
+import fs from 'fs-extra';
+import { memoryLimit, timeLimit } from 'config/constants';
+import { codesDir } from 'config/paths';
+import { execute } from 'utils/misc';
+
+export class RunTracer extends Tracer {
+  private readonly directory: string;
+
+  constructor(lang: string) {
+    super(lang);
+    this.directory = path.resolve(__dirname, lang);
+  }
+
+  async build(release: Release) {
+    const {tag_name} = release;
+  }
+
+  route(router: express.Router) {
+    router.post(`/${this.lang}`, (req, res, next) => {
+      const {code} = req.body;
+      const tempPath = path.resolve(codesDir, uuid.v4());
+      fs.outputFile(path.resolve(tempPath, `Main.${this.lang}`), code)
+        .then(() => {
+          return execute([
+            'julia',
+            `${tempPath}`,
+          ].join(' ')).then((data) => {
+            //const payload = JSON.parse(data.Payload);
+            //if (!payload.success) return next(new BadRequest(payload.errorMessage));
+            //res.send(payload.commands);
+            res.send(data);
+          }).catch(error => {
+            throw error;
+          }).finally(() => {});
+        })
+        .catch(next)
+        .finally(() => fs.remove(tempPath));
+    });
+  }
+}
diff --git a/src/tracers/julia/JuliaTracer.ts b/src/tracers/julia/JuliaTracer.ts
index 50f7025..5247bd4 100644
--- a/src/tracers/julia/JuliaTracer.ts
+++ b/src/tracers/julia/JuliaTracer.ts
@@ -1,29 +1,12 @@
-import path from 'path';
-import { Release, Tracer } from 'tracers/Tracer';
-import express from 'express';
-import { GitHubApi } from 'utils/apis';
+//import { LspTracer } from 'tracers/LspTracer';
+import { RunTracer as LspTracer } from 'tracers/RunTracer';
 
-export class JuliaTracer extends Tracer {
-  readonly workerPath: string;
-  tagName?: string;
+import { Release } from 'tracers/Tracer';
+import { GitHubApi } from 'utils/apis';
 
+export class JuliaTracer extends LspTracer {
   constructor() {
     super('julia');
-    this.workerPath = path.resolve(__dirname, 'worker.js');
-  }
-
-  async build(release: Release) {
-    const {tag_name} = release;
-    this.tagName = tag_name;
-  }
-
-  route(router: express.Router) {
-    router.get(`/${this.lang}`, (req, res) => {
-      if (!this.tagName) throw new Error('JuliaTracer has not been built yet.');
-      const version = this.tagName.slice(1);
-      res.redirect(`https://unpkg.com/algorithm-visualizer@${version}/dist/algorithm-visualizer.umd.js`);
-    });
-    router.get(`/${this.lang}/worker`, (req, res) => res.sendFile(this.workerPath));
   }
 
   async update(release?: Release) {
diff --git a/src/tracers/julia/worker.js b/src/tracers/julia/worker.js
deleted file mode 100644
index e91e3e5..0000000
--- a/src/tracers/julia/worker.js
+++ /dev/null
@@ -1,14 +0,0 @@
-const process = { env: { ALGORITHM_VISUALIZER: '1' } };
-importScripts('/api/tracers/julia');
-
-const sandbox = code => {
-  const require = name => ({ 'algorithm-visualizer': AlgorithmVisualizer }[name]); // fake require
-  eval(code);
-};
-
-onmessage = e => {
-  const lines = e.data.split('\n').map((line, i) => line.replace(/(\.\s*delay\s*)\(\s*\)/g, `$1(${i})`));
-  const code = lines.join('\n');
-  sandbox(code);
-  postMessage(AlgorithmVisualizer.Commander.commands);
-};
