import express from "express"; import path from "path"; import { fileURLToPath } from "url"; import dotenv from "dotenv"; import cookieParser from "cookie-parser"; import { createRepo, uploadFiles, whoAmI } from "@huggingface/hub"; import { InferenceClient } from "@huggingface/inference"; import bodyParser from "body-parser"; import { diff_match_patch } from "diff-match-patch"; // Using a library for robustness import checkUser from "./middlewares/checkUser.js"; // Load environment variables from .env file dotenv.config(); const app = express(); const ipAddresses = new Map(); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const PORT = process.env.APP_PORT || 3000; const REDIRECT_URI = process.env.REDIRECT_URI || `http://localhost:${PORT}/auth/login`; const MODEL_ID = "deepseek-ai/DeepSeek-V3-0324"; const MAX_REQUESTS_PER_IP = 4; // Increased limit for testing diffs app.use(cookieParser()); app.use(bodyParser.json({ limit: "10mb" })); // Increase limit if HTML gets large app.use(express.static(path.join(__dirname, "dist"))); app.get("/api/login", (_req, res) => { res.redirect( 302, `https://huggingface.co/oauth/authorize?client_id=${process.env.OAUTH_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=openid%20profile%20write-repos%20manage-repos%20inference-api&prompt=consent&state=1234567890` ); }); app.get("/auth/login", async (req, res) => { const { code } = req.query; if (!code) { return res.redirect(302, "/"); } const Authorization = `Basic ${Buffer.from( `${process.env.OAUTH_CLIENT_ID}:${process.env.OAUTH_CLIENT_SECRET}` ).toString("base64")}`; const request_auth = await fetch("https://huggingface.co/oauth/token", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization, }, body: new URLSearchParams({ grant_type: "authorization_code", code: code, redirect_uri: REDIRECT_URI, }), }); const response = await request_auth.json(); if (!response.access_token) { return res.redirect(302, "/"); } res.cookie("hf_token", response.access_token, { httpOnly: false, secure: true, sameSite: "none", maxAge: 30 * 24 * 60 * 60 * 1000, }); return res.redirect(302, "/"); }); app.get("/api/@me", checkUser, async (req, res) => { const { hf_token } = req.cookies; try { const request_user = await fetch("https://huggingface.co/oauth/userinfo", { headers: { Authorization: `Bearer ${hf_token}`, }, }); const user = await request_user.json(); res.send(user); } catch (err) { res.clearCookie("hf_token"); res.status(401).send({ ok: false, message: err.message, }); } }); app.post("/api/deploy", checkUser, async (req, res) => { const { html, title, path } = req.body; if (!html || !title) { return res.status(400).send({ ok: false, message: "Missing required fields", }); } let newHtml = html; if (!path) { newHtml = html.replace( /<\/body>/, `
Made with DeepSite