|
|
@ -1,5 +1,5 @@
|
|
|
|
import cors from "@fastify/cors";
|
|
|
|
import cors from "@fastify/cors";
|
|
|
|
import websocket, { WebSocket } from '@fastify/websocket';
|
|
|
|
import websocket, { WebSocket } from "@fastify/websocket";
|
|
|
|
import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
|
|
|
|
import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
|
|
|
|
import Fastify, { FastifyReply } from "fastify";
|
|
|
|
import Fastify, { FastifyReply } from "fastify";
|
|
|
|
import { nanoid } from "nanoid";
|
|
|
|
import { nanoid } from "nanoid";
|
|
|
@ -18,13 +18,13 @@ const clients: Record<string, FastifyReply> = {};
|
|
|
|
const generateId = () => nanoid(32);
|
|
|
|
const generateId = () => nanoid(32);
|
|
|
|
|
|
|
|
|
|
|
|
let updates: Update[] = [];
|
|
|
|
let updates: Update[] = [];
|
|
|
|
let doc = Text.of(['']);
|
|
|
|
let doc = Text.of([""]);
|
|
|
|
const liveClients: WebSocket[] = [];
|
|
|
|
const liveClients: WebSocket[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
function send(socket: WebSocket, requestId: number, payload: unknown) {
|
|
|
|
function send(socket: WebSocket, requestId: number, payload: unknown) {
|
|
|
|
const response = {
|
|
|
|
const response = {
|
|
|
|
_request: requestId,
|
|
|
|
_request: requestId,
|
|
|
|
payload
|
|
|
|
payload,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
socket.send(JSON.stringify(response));
|
|
|
|
socket.send(JSON.stringify(response));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -33,46 +33,53 @@ const fastify = Fastify({
|
|
|
|
logger: true,
|
|
|
|
logger: true,
|
|
|
|
}).withTypeProvider<TypeBoxTypeProvider>();
|
|
|
|
}).withTypeProvider<TypeBoxTypeProvider>();
|
|
|
|
await fastify.register(cors, {
|
|
|
|
await fastify.register(cors, {
|
|
|
|
origin: process.env.ALLOW_ORIGIN || '*',
|
|
|
|
origin: process.env.ALLOW_ORIGIN || "*",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
fastify.register(websocket);
|
|
|
|
fastify.register(websocket);
|
|
|
|
fastify.get("/live", { websocket: true }, (socket, req) => {
|
|
|
|
fastify.get("/live", { websocket: true }, (socket, req) => {
|
|
|
|
liveClients.push(socket);
|
|
|
|
liveClients.push(socket);
|
|
|
|
socket.on("message", message => {
|
|
|
|
socket.on("message", (message) => {
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
const data = JSON.parse(message.toString());
|
|
|
|
const requestId = data._request;
|
|
|
|
const requestId = data._request;
|
|
|
|
if (data.type === "pullUpdates") {
|
|
|
|
if (data.type === "pullUpdates") {
|
|
|
|
send(socket, requestId, updates.slice(data.version))
|
|
|
|
send(socket, requestId, updates.slice(data.version));
|
|
|
|
} else if (data.type === "pushUpdates") {
|
|
|
|
} else if (data.type === "pushUpdates") {
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
let received = data.updates.map((json: any) => ({
|
|
|
|
clientID: json.clientID,
|
|
|
|
clientID: json.clientID,
|
|
|
|
changes: ChangeSet.fromJSON(json.changes)
|
|
|
|
changes: ChangeSet.fromJSON(json.changes),
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
if (data.version != updates.length) {
|
|
|
|
if (data.version != updates.length) {
|
|
|
|
received = rebaseUpdates(received, updates.slice(data.version))
|
|
|
|
received = rebaseUpdates(received, updates.slice(data.version));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let update of received) {
|
|
|
|
for (let update of received) {
|
|
|
|
updates.push(update)
|
|
|
|
updates.push(update);
|
|
|
|
doc = update.changes.apply(doc)
|
|
|
|
doc = update.changes.apply(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send(socket, requestId, received.map((update: any) => ({
|
|
|
|
send(
|
|
|
|
|
|
|
|
socket,
|
|
|
|
|
|
|
|
requestId,
|
|
|
|
|
|
|
|
received.map((update: any) => ({
|
|
|
|
clientID: update.clientID,
|
|
|
|
clientID: update.clientID,
|
|
|
|
changes: update.changes.toJSON()
|
|
|
|
changes: update.changes.toJSON(),
|
|
|
|
})));
|
|
|
|
}))
|
|
|
|
|
|
|
|
);
|
|
|
|
} else if (data.type == "getDocument") {
|
|
|
|
} else if (data.type == "getDocument") {
|
|
|
|
send(socket, requestId, {version: updates.length, doc: doc.toString()})
|
|
|
|
send(socket, requestId, { version: updates.length, doc: doc.toString() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
fastify.post("/run", {
|
|
|
|
fastify.post(
|
|
|
|
|
|
|
|
"/run",
|
|
|
|
|
|
|
|
{
|
|
|
|
schema: {
|
|
|
|
schema: {
|
|
|
|
body: Type.Object({
|
|
|
|
body: Type.Object({
|
|
|
|
code: Type.String(),
|
|
|
|
code: Type.String(),
|
|
|
|
language: Type.String(),
|
|
|
|
language: Type.String(),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, (req, reply) => {
|
|
|
|
},
|
|
|
|
|
|
|
|
(req, reply) => {
|
|
|
|
const { code, language } = req.body;
|
|
|
|
const { code, language } = req.body;
|
|
|
|
const runner = getRunner(language);
|
|
|
|
const runner = getRunner(language);
|
|
|
|
if (runner === null) {
|
|
|
|
if (runner === null) {
|
|
|
@ -95,7 +102,8 @@ fastify.post("/run", {
|
|
|
|
reply.raw.write("id: 0\n\n");
|
|
|
|
reply.raw.write("id: 0\n\n");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
clients[jobId] = reply;
|
|
|
|
clients[jobId] = reply;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Création du répertoire de la base de données s'il n'existe pas */
|
|
|
|
/* Création du répertoire de la base de données s'il n'existe pas */
|
|
|
|
db.createDbDirectory();
|
|
|
|
db.createDbDirectory();
|
|
|
@ -120,9 +128,12 @@ fastify.post(
|
|
|
|
},
|
|
|
|
},
|
|
|
|
async (request, reply) => {
|
|
|
|
async (request, reply) => {
|
|
|
|
const { login, password, permissions } = request.body;
|
|
|
|
const { login, password, permissions } = request.body;
|
|
|
|
db.insertUser(database, login, password, permissions);
|
|
|
|
if (!(await db.insertUser(database, login, password, permissions))) {
|
|
|
|
|
|
|
|
reply.send({ success: false });
|
|
|
|
|
|
|
|
} else {
|
|
|
|
reply.send({ success: true });
|
|
|
|
reply.send({ success: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/* Route pour mettre à jour le login d'un utilisateur */
|
|
|
|
/* Route pour mettre à jour le login d'un utilisateur */
|
|
|
|