Allow runner aliases
continuous-integration/drone/push Build is passing Details

pull/5/head
Clément FRÉVILLE 11 months ago
parent 0801e3005e
commit aaf24387f1

@ -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<string>(RUNNERS);
const aliases: Record<string, typeof RUNNERS[number]> = {
'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;
}

@ -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',

Loading…
Cancel
Save