-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ef9ae8
commit e5bdd31
Showing
3 changed files
with
119 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export type Langs = "en" | "ca" | "es"; | ||
type Translations = { | ||
placeholder: string; | ||
compiling: string; | ||
error: string; | ||
}; | ||
|
||
const translations: Record<Langs, Translations> = { | ||
en: { | ||
placeholder: "Enter your code here...", | ||
compiling: "Compiling...", | ||
error: | ||
'Woops, something went wrong and the code does not compile!\nIf you\'ve mistakenly messed up the code, click the "Reset" button to return it back to its original state.\n\nRemember to replace ? with your answer.', | ||
}, | ||
ca: { | ||
placeholder: "Escriu el teu codi aqui...", | ||
compiling: "Compilant...", | ||
error: | ||
'Ups, alguna cosa ha fallat i el codi no compila!\nSi t\'has equivocat modificant el codi, fes clic al botó de "Reset" per tornar-lo al seu estat original.\n\nRecorda substituïr ? amb la teva resposta.', | ||
}, | ||
es: { | ||
placeholder: "Escribe tu código aquí...", | ||
compiling: "Compilando...", | ||
error: | ||
'Vaya, ¡algo ha ido mal y el código no compila!\nSi has estropeado el código por error, haz clic en el botón "Reset" para devolverlo a su estado original.\n\nRecuerda sustituir ? con tu respuesta.', | ||
}, | ||
}; | ||
|
||
export function translation(lang: Langs): Translations { | ||
return translations[lang]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { translation, type Langs } from "@i18n/CodeBlock"; | ||
|
||
export async function evaluate(value: string, setup: string, lang: Langs, errorMsg?: string): Promise<string> { | ||
const error = errorMsg || translation(lang).error; | ||
const code = value.replaceAll("__VALUE__", setup); | ||
|
||
return Promise.race([ | ||
server(code, error), | ||
playground(code, error), | ||
new Promise((_, reject) => setTimeout(() => reject("TIMEOUT"), 2000)) as Promise<string> | ||
]); | ||
} | ||
|
||
async function server(code: string, error: string): Promise<string> { | ||
const params = { | ||
code | ||
}; | ||
|
||
// TODO: Does not work | ||
const auth = process.env.AUTH; | ||
console.log({auth}); | ||
|
||
return fetch("https://rust-quest.garriga.dev/evaluate.json", { | ||
headers: { | ||
"Content-Type": "application/json", | ||
"authorization": auth || "", | ||
}, | ||
method: "POST", | ||
mode: "cors", | ||
body: JSON.stringify(params), | ||
}).then((response) => response.json()) | ||
.then((response) => { | ||
console.log({ params, response }); | ||
return response; | ||
}) | ||
.then((response) => { | ||
if (response.ok) { | ||
return response.ok.stdout | ||
} else { | ||
return error || response.err.stderr | ||
} | ||
}) | ||
.catch( | ||
(error) => error || error.message | ||
) | ||
} | ||
|
||
async function playground(code: string, error: string): Promise<string> { | ||
const params = { | ||
version: "stable", | ||
optimize: "0", | ||
code, | ||
edition: "2021", | ||
}; | ||
|
||
return fetch("https://play.rust-lang.org/evaluate.json", { | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
method: "POST", | ||
mode: "cors", | ||
body: JSON.stringify(params), | ||
}) | ||
.then((response) => response.json()) | ||
.then((response) => { | ||
console.log({ params, response }); | ||
return response; | ||
}) | ||
.then((response) => { | ||
if (response.error === null) { | ||
return response.result | ||
} else { | ||
return error || response.error | ||
} | ||
}) | ||
.catch( | ||
(error) => error || error.message | ||
) | ||
} |