This commit is contained in:
Rizky 2024-02-10 13:18:38 +07:00
parent 010adfb473
commit 8e24d40a0f
4 changed files with 76 additions and 5 deletions

69
app/srv/api/prod-zip.ts Normal file
View File

@ -0,0 +1,69 @@
import { apiContext } from "service-srv";
import { code } from "../ws/sync/editor/code/util-code";
import fs from "fs";
import path from "path";
import { gzipAsync } from "../ws/sync/entity/zlib";
import { validate } from "uuid";
export const _ = {
url: "/prod-zip/:site_id",
async api(site_id: string) {
const { req, res } = apiContext(this);
if (validate(site_id)) {
const result = {
pages: await _db.page.findMany({
where: { id_site: site_id, is_deleted: false },
select: { id: true, name: true, url: true, content_tree: true },
}),
comps: await _db.component.findMany({
where: {
component_group: {
component_site: { some: { id_site: site_id } },
},
},
select: { id: true, content_tree: true },
}),
site: await _db.component.findFirst({ where: { id: site_id } }),
code: {
server: readDirectoryRecursively(
code.path(site_id, "server", "build")
),
site: readDirectoryRecursively(code.path(site_id, "site", "build")),
},
};
return await gzipAsync(JSON.stringify(result));
}
return new Response("NOT FOUND", { status: 403 });
},
};
export function readDirectoryRecursively(
dirPath: string,
baseDir?: string[]
): Record<string, string> {
const result: Record<string, string> = {};
const contents = fs.readdirSync(dirPath);
for (const item of contents) {
const itemPath = path.join(dirPath, item);
const stats = fs.statSync(itemPath);
if (stats.isFile()) {
const content = fs.readFileSync(itemPath, "utf-8");
result[[...(baseDir || []), item].join("/")] = content;
} else if (stats.isDirectory()) {
if (item !== "node_modules") {
const subdirResult = readDirectoryRecursively(itemPath, [
...(baseDir || []),
item,
]);
Object.assign(result, subdirResult);
}
}
}
return result;
}

View File

@ -3,9 +3,8 @@ import { apiContext } from "../../../pkgs/core/server/api/api-ctx";
import { dir } from "dir";
import fs from "fs";
import { exists } from "fs-jetpack";
import { gzipSync } from "zlib";
import path from "path";
import { g } from "utils/global";
import { gzipSync } from "zlib";
import { buildNpm } from "../util/build-npm";
export const _ = {

View File

@ -1,3 +1,4 @@
import { validate } from "uuid";
import { glb } from "./global";
import { server } from "./ws/sync/editor/code/server-main";
glb.npm = { page: {}, site: {} };
@ -25,11 +26,13 @@ glb.ws_hook = {
glb.server_hook = async (arg) => {
const url = arg.url;
if (url.pathname.startsWith("/prod")) {
if (url.pathname.startsWith("/prod/")) {
const arr = url.pathname.split("/");
const site_id = arr[2];
return await server.http(site_id, arg);
if (arr.length >= 3 && validate(site_id)) {
return await server.http(site_id, arg);
}
}
if (arg.handle) return await arg.handle(arg.req);

View File

@ -1,6 +1,6 @@
import { gzip, gunzip } from "zlib";
export const gzipAsync = (bin: Uint8Array) => {
export const gzipAsync = (bin: Uint8Array | string) => {
return new Promise<Buffer>((resolve, reject) => {
gzip(bin, (err, res) => {
if (err) {