From aaf24387f1310c8c59d30ab118cf9726085ed482 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Thu, 16 May 2024 15:50:37 +0200 Subject: [PATCH] Allow runner aliases --- src/runner.ts | 15 +++++++++++++-- src/server.ts | 8 ++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/runner.ts b/src/runner.ts index 8062a1b..2dc383b 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,5 +1,8 @@ -export const IMAGES = { - moshell: 'ghcr.io/moshell-lang/moshell:master', +export const RUNNERS = ['bash', 'moshell', 'bun', 'typescript'] as const; +const ALLOWED_LANGUAGES = new Set(RUNNERS); +const aliases: Record = { + 'JavaScript': 'bun', + 'TypeScript': 'typescript', }; /** @@ -20,3 +23,11 @@ export function allocateBuffer(jobId: string, code: string, image: string): Buff buffer.write(code, cur); return buffer; } + +export function getRunner(language: string): typeof RUNNERS[number] | null { + language = aliases[language] || language; + if (ALLOWED_LANGUAGES.has(language)) { + return language as typeof RUNNERS[number]; + } + return null; +} diff --git a/src/server.ts b/src/server.ts index 20a926b..7099321 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,7 +2,7 @@ import cors from '@fastify/cors'; import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; import Fastify, { FastifyReply } from 'fastify'; import { nanoid } from 'nanoid'; -import { allocateBuffer, IMAGES } from 'runner'; +import { allocateBuffer, getRunner } from 'runner'; import { Pull, Push } from 'zeromq'; const sender = new Push(); @@ -29,8 +29,12 @@ fastify.post('/run', { }, }, (req, reply) => { const { code, language } = req.body; + const runner = getRunner(language); + if (runner === null) { + return reply.status(422).send({ error: 'Invalid language' }); + } const jobId = generateId(); - const buffer = allocateBuffer(jobId, code, IMAGES.moshell); + const buffer = allocateBuffer(jobId, code, runner); reply.raw.writeHead(200, { 'Content-Type': 'text/event-stream', Connection: 'keep-alive',