mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
fix: typescript support fix, add some params to editor, etc
This commit is contained in:
+2
-1
@@ -3,11 +3,12 @@ import preview from "./preview";
|
||||
import trpc from "./trpc/handler";
|
||||
import { thumbnail } from "./thumbnail";
|
||||
import sandbox from "./sandbox";
|
||||
import { nocache } from "../middlewares/nocache";
|
||||
|
||||
const api = Router();
|
||||
|
||||
api.use("/trpc", trpc);
|
||||
api.use("/preview", preview);
|
||||
api.use("/preview", nocache, preview);
|
||||
api.use("/sandbox", sandbox);
|
||||
|
||||
api.get("/thumbnail/:slug", thumbnail);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getFileExt } from "~/lib/utils";
|
||||
import { FileSchema } from "~/server/db/schema/file";
|
||||
import { ProjectSettingsSchema } from "~/server/db/schema/project";
|
||||
import { transformJs } from "~/server/lib/transform-js";
|
||||
@@ -10,7 +11,9 @@ export const serveJs = async (
|
||||
|
||||
// transpile to es5
|
||||
if (cfg?.transpiler === "swc") {
|
||||
content = await transformJs(content);
|
||||
const ext = getFileExt(file.filename);
|
||||
const typescript = ["ts", "tsx"].includes(ext);
|
||||
content = await transformJs(content, typescript ? "ts" : "js");
|
||||
}
|
||||
|
||||
return content;
|
||||
|
||||
+9
-4
@@ -5,7 +5,6 @@ import { IS_DEV } from "./lib/consts";
|
||||
import cookieParser from "cookie-parser";
|
||||
import api from "./api";
|
||||
import { authMiddleware } from "./middlewares/auth";
|
||||
import fetch from "node-fetch";
|
||||
|
||||
async function createServer() {
|
||||
const app = express();
|
||||
@@ -24,8 +23,12 @@ async function createServer() {
|
||||
} else {
|
||||
// serve client assets
|
||||
app.use(express.static(root + "/dist/client"));
|
||||
|
||||
const { default: morgan } = await import("morgan");
|
||||
app.use(morgan("combined"));
|
||||
}
|
||||
|
||||
app.set("etag", false);
|
||||
app.use(cookieParser());
|
||||
app.use(authMiddleware);
|
||||
|
||||
@@ -42,9 +45,11 @@ async function createServer() {
|
||||
}
|
||||
|
||||
const { body, statusCode, headers, earlyHints } = httpResponse;
|
||||
if (res.writeEarlyHints) {
|
||||
res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) });
|
||||
}
|
||||
|
||||
// FIXME: SSL cloudflare gak bisa digunakan kalo ada ini..
|
||||
// if (res.writeEarlyHints) {
|
||||
// res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) });
|
||||
// }
|
||||
|
||||
headers.forEach(([name, value]) => res.setHeader(name, value));
|
||||
res.status(statusCode).send(body);
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ import standardTypes from "mime/types/standard.js";
|
||||
import otherTypes from "mime/types/other.js";
|
||||
|
||||
const mime = new Mime(standardTypes, otherTypes);
|
||||
mime.define({ "text/javascript": ["jsx", "tsx"] }, true);
|
||||
mime.define({ "text/javascript": ["jsx", "tsx", "ts"] }, true);
|
||||
|
||||
export const getMimeType = (
|
||||
ext: string,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import * as swc from "@swc/core";
|
||||
|
||||
export const transformJs = async (code: string) => {
|
||||
export const transformJs = async (code: string, type: "js" | "ts" = "js") => {
|
||||
try {
|
||||
const result = await swc.transform(code, {
|
||||
jsc: {
|
||||
parser: {
|
||||
syntax: type === "js" ? "ecmascript" : "typescript",
|
||||
jsx: true,
|
||||
syntax: "ecmascript",
|
||||
tsx: true,
|
||||
},
|
||||
target: "es5",
|
||||
},
|
||||
@@ -14,6 +15,7 @@ export const transformJs = async (code: string) => {
|
||||
|
||||
return result.code;
|
||||
} catch (err) {
|
||||
// console.log(err);
|
||||
return code;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@ export const unpackProject = async (
|
||||
) => {
|
||||
const files = await db.query.file.findMany({
|
||||
where: and(
|
||||
eq(file.projectId, projectData.id),
|
||||
eq(file.isDirectory, false),
|
||||
eq(file.isFile, false),
|
||||
isNull(file.deletedAt)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
|
||||
export const nocache = (_req: Request, res: Response, next: NextFunction) => {
|
||||
res.setHeader("Surrogate-Control", "no-store");
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
"no-store, no-cache, must-revalidate, proxy-revalidate"
|
||||
);
|
||||
res.setHeader("Expires", "0");
|
||||
|
||||
next();
|
||||
};
|
||||
@@ -116,6 +116,17 @@ const projectRouter = router({
|
||||
const projectId = projectData.id;
|
||||
|
||||
if (input.forkFromId) {
|
||||
const forkProject = await tx.query.project.findFirst({
|
||||
where: and(
|
||||
eq(project.id, input.forkFromId),
|
||||
isNull(project.deletedAt)
|
||||
),
|
||||
});
|
||||
|
||||
if (!forkProject) {
|
||||
throw new Error("Fork Project not found!");
|
||||
}
|
||||
|
||||
const forkFiles = await tx.query.file.findMany({
|
||||
where: and(
|
||||
eq(file.projectId, input.forkFromId),
|
||||
@@ -132,6 +143,11 @@ const projectRouter = router({
|
||||
await tx
|
||||
.insert(file)
|
||||
.values(forkFiles.map((file) => ({ ...file, projectId })));
|
||||
|
||||
await tx
|
||||
.update(project)
|
||||
.set({ settings: forkProject.settings })
|
||||
.where(eq(project.id, projectData.id));
|
||||
} else {
|
||||
await tx.insert(file).values([
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user