Convert to bearmetal router, updated sockpuppet

This commit is contained in:
2024-11-11 13:39:30 -07:00
parent 1f306485a8
commit 762baa37e7
5 changed files with 37 additions and 30 deletions

View File

@@ -15,6 +15,8 @@
},
"imports": {
"@babel/plugin-transform-react-jsx-development": "npm:@babel/plugin-transform-react-jsx-development@^7.25.7",
"@bearmetal/router": "jsr:@bearmetal/router@^0.1.1",
"@bearmetal/sockpuppet": "jsr:@bearmetal/sockpuppet@^1.0.0",
"@bearmetal/store": "jsr:@bearmetal/store@^0.0.5",
"@cgg/sockpuppet": "../sockpuppet.ts/server/mod.ts",
"@cgg/sockpuppet/client": "../sockpuppet.ts/client/mod.ts",

10
deno.lock generated
View File

@@ -1,6 +1,8 @@
{
"version": "4",
"specifiers": {
"jsr:@bearmetal/router@~0.1.1": "0.1.1",
"jsr:@bearmetal/sockpuppet@1": "1.0.0",
"jsr:@bearmetal/store@^0.0.5": "0.0.5",
"jsr:@denosaurs/plug@1.0.5": "1.0.5",
"jsr:@gfx/canvas@~0.5.8": "0.5.8",
@@ -43,6 +45,12 @@
"npm:vite@^5.4.8": "5.4.9"
},
"jsr": {
"@bearmetal/router@0.1.1": {
"integrity": "cd526ac6b4a6426ac171f3868f83ec1727ebf2de21aad4e1f128c2605345f19d"
},
"@bearmetal/sockpuppet@1.0.0": {
"integrity": "e6dc133b2a5c9e7f1fb99309f592c24b0b8ffd4fc43ee3d61313f5c517ddfb8c"
},
"@bearmetal/store@0.0.5": {
"integrity": "d17da24c91bcc05707deb8a55017ebdf5d8eebd2f6293dcb2bbfac57e4e3b395",
"dependencies": [
@@ -1488,6 +1496,8 @@
},
"workspace": {
"dependencies": [
"jsr:@bearmetal/router@~0.1.1",
"jsr:@bearmetal/sockpuppet@1",
"jsr:@bearmetal/store@^0.0.5",
"jsr:@gfx/canvas@~0.5.8",
"jsr:@std/encoding@^1.0.5",

View File

@@ -1,8 +1,8 @@
import { SockpuppetPlus } from "@cgg/sockpuppet";
import { Sockpuppet } from "@bearmetal/sockpuppet";
import { serveDir, serveFile } from "@std/http/file-server";
import { BearMetalStore } from "@bearmetal/store";
import { ensureDir, ensureFile, exists } from "@std/fs";
import { Router } from "./router.ts";
import { Router } from "@bearmetal/router";
import { getPackVersion } from "./util/packVersion.ts";
import { createTagRoutes } from "./tags/routes.ts";
import { createResourcesRoutes } from "./resources/routes.ts";
@@ -11,31 +11,21 @@ import { unzipResources } from "./resources/unzip.ts";
const installPath = Deno.env.get("BMP_INSTALL_DIR") || "./";
const sockpuppet = new SockpuppetPlus();
sockpuppet.addHandler((req: Request) => {
const url = new URL(req.url);
if (!url.pathname.startsWith("/images")) return;
return serveFile(req, url.searchParams.get("location") as string);
});
const sockpuppet = new Sockpuppet();
// sockpuppet.addHandler((req: Request) => {
// const method = req.method;
// if (method === "OPTIONS") {
// return new Response(null, {
// status: 200,
// headers: {
// "Access-Control-Allow-Origin": "*",
// "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
// },
// });
// }
// const url = new URL(req.url);
// if (!url.pathname.startsWith("/images")) return;
// return serveFile(req, url.searchParams.get("location") as string);
// });
const router = new Router();
router.route("/images").get((req,ctx) => serveFile(req,ctx.url.searchParams.get("location") as string));
router.route("/api/dir")
.get(async () => {
console.log("hitting dir");
using store = new BearMetalStore();
const mcPath = store.get("mcPath") as string;
if (mcPath) {
@@ -257,15 +247,19 @@ router.route("/api/stream/test")
});
})
sockpuppet.addHandler((req: Request) => {
if (new URL(req.url).pathname.startsWith("/api")) return;
// sockpuppet.addHandler((req: Request) => {
// if (new URL(req.url).pathname.startsWith("/api")) return;
return serveDir(req, {
fsRoot: installPath + "dist",
});
});
// return serveDir(req, {
// fsRoot: installPath + "dist",
// });
// });
sockpuppet.addHandler(router.handle);
router.route("/").get((req) => serveDir(req, {fsRoot:installPath + "dist"}));
router.route("/ws").get(sockpuppet.handler)
Deno.serve(router.handle);
async function createPack(store: BearMetalStore, packName: string) {
const realWorldPath = store.get("world");

View File

@@ -1,12 +1,12 @@
import { ensureDir } from "@std/fs/ensure-dir";
import type { Router } from "../router.ts";
import type { Router } from "@bearmetal/router";
import { readDirDirs } from "../util/readDir.ts";
import { versionCompat } from "../util/versionCompat.ts";
import { readBlocks, readItems } from "./readers.ts";
export const createResourcesRoutes = (router: Router) => {
router.route("/api/resources/:path*")
.get(async (req, ctx) => {
.get(async (_req, ctx) => {
const path = ctx.params.path;
if (!path) {
return new Response("no path provided", { status: 400 });
@@ -99,5 +99,6 @@ export const createResourcesRoutes = (router: Router) => {
// }
}
}
return new Response("no path provided", { status: 400 });
});
};

View File

@@ -1,6 +1,6 @@
import { BearMetalStore } from "@bearmetal/store";
import { getPackVersion } from "../util/packVersion.ts";
import type { Router } from "../router.ts";
import type { Router } from "@bearmetal/router";
import { getTagDir } from "./getTagDir.ts";
import { ensureDir } from "@std/fs/ensure-dir";
import { ensureFile } from "@std/fs/ensure-file";