Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 121 additions & 124 deletions apps/server/bun.lock

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions apps/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nanoforge-loader-server",
"version": "1.0.0",
"name": "@nanoforge-dev/loader-server",
"version": "0.0.2",
"description": "NanoForge Loader - Server",
"homepage": "https://github.com/NanoForge-dev/Loader#readme",
"license": "MIT",
Expand All @@ -15,29 +15,36 @@
"url": ""
},
"type": "module",
"module": "src/server.ts",
"main": "dist/server.js",
"repository": {
"type": "git",
"url": "git+https://github.com/NanoForge-dev/Loader.git",
"directory": "apps/server"
},
"private": true,
"files": [
"dist",
"package.json",
"README.md",
"LICENSE"
],
"scripts": {
"setup": "bun i",
"build": "bun run clean && bun run build:raw",
"build:raw": "bun build src/index.ts --outdir dist --target browser",
"build:raw": "bun build src/server.ts --outdir dist --target bun",
"clean": "rm -rf dist",
"dev": "bun src/server.ts --watch",
"start": "bun dist/index.js",
"publish": "npm publish --access public",
"lint": "eslint . && prettier --check . '!.cloud/**'",
"fix": "eslint . --fix && prettier --write . '!.cloud/**'",
"taze": "taze major -w",
"lint-staged": "lint-staged"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.21.2",
"helmet": "^8.0.0"
"express": "^5.1.0",
"helmet": "^8.0.0",
"serve-static-bun": "^0.5.3"
},
"devDependencies": {
"@eslint/js": "^9.22.0",
Expand Down
27 changes: 16 additions & 11 deletions apps/server/src/files.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import * as fs from "node:fs";
import { readdir } from "node:fs/promises";

import { MANIFEST } from "./server";

export const updateFiles = () => {
addPath("public/game", "");
export const updateFiles = (dir: string) => {
return addPath(dir, "");
};

const addPath = (path: string, exportedPath: string) => {
if (fs.statSync(path).isDirectory()) {
fs.readdirSync(path).forEach((file) => {
addPath(`${path}/${file}`, `${exportedPath}/${file}`);
const addPath = async (path: string, exportedPath: string) => {
try {
if ((await Bun.file(path).stat()).isDirectory()) {
for (const file of await readdir(path)) {
await addPath(`${path}/${file}`, `${exportedPath}/${file}`);
}
return;
}
MANIFEST.files.push({
path: exportedPath,
});
return;
} catch (e) {
console.error(e);
/* empty */
}
MANIFEST.files.push({
path: exportedPath,
});
};
16 changes: 9 additions & 7 deletions apps/server/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as fs from "fs";

import { updateFiles } from "./files";
import { MANIFEST } from "./server";

export const updateManifest = () => {
MANIFEST.version = getVersion();
export const updateManifest = async (dir: string) => {
MANIFEST.version = await getVersion();
MANIFEST.files = [];
updateFiles();
await updateFiles(dir);
};

const getVersion = () => {
return fs.readFileSync("public/version", "utf8");
const getVersion = async () => {
try {
return await Bun.file("public/version").text();
} catch {
return "0.0.0";
}
};
52 changes: 27 additions & 25 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import cors from "cors";
import Express, { type Request, type Response } from "express";
import helmet from "helmet";
import { join } from "node:path";

import { updateManifest } from "./manifest";

Expand All @@ -9,28 +7,32 @@ export const MANIFEST: { version: string; files: { path: string }[] } = {
files: [],
};

export const app = Express();
const port = process.env.PORT || 8080;
const clientUrl = process.env.CLIENT_URL || "http://localhost:3000";
const gameDir = process.env.GAME_DIR || "public/game";

const boostrap = () => {
const port = process.env.PORT || 8080;

app.use(helmet());
app.use(
cors({
origin: process.env.CORS_ALLOWED_ORIGINS?.split(","),
}),
);

app.use("/game", Express.static("public/game"));

app.get("/manifest", (_req: Request, res: Response) => {
updateManifest();
res.send(MANIFEST);
});

app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
const headers = {
"Access-Control-Allow-Origin": clientUrl,
"Access-Control-Allow-Methods": "GET",
};

boostrap();
const server = Bun.serve({
port: port,
routes: {
"/manifest": async () => {
await updateManifest(gameDir);
return Response.json(MANIFEST, {
headers,
});
},
"/game/*": (req) => {
const path = new URL(req.url).pathname.replace("/game", "");
const file = Bun.file(join(gameDir, path));
return new Response(file, {
headers,
});
},
},
});

console.log(`Server started on port ${server.port}`);
22 changes: 14 additions & 8 deletions apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nanoforge-loader-web",
"version": "1.0.0",
"description": "NanoForge Loader - Web",
"name": "@nanoforge-dev/loader-client",
"version": "0.0.2",
"description": "NanoForge Loader - Client",
"homepage": "https://github.com/NanoForge-dev/Loader#readme",
"license": "MIT",
"contributors": [
Expand All @@ -15,22 +15,28 @@
"url": ""
},
"type": "module",
"module": "src/index.ts",
"main": "dist/index.html",
"browser": true,
"repository": {
"type": "git",
"url": "git+https://github.com/NanoForge-dev/Loader.git",
"directory": "apps/web"
},
"private": true,
"files": [
"src",
"package.json",
"README.md",
"LICENSE"
],
"scripts": {
"setup": "bun i",
"build": "bun run clean && bun run build:raw && bun run build:obfuscate",
"build:raw": "bun build src/index.html --outdir dist --target browser --env PUBLIC_*",
"build:obfuscate": "./scripts/obfuscate.sh dist",
"build": "bun run clean && bun run build:raw",
"build:silent": "bun --silent run clean && bun --silent run build:raw",
"build:raw": "bun build src/index.html --outdir dist --target browser",
"clean": "rm -rf dist",
"dev": "bun src/index.html",
"start": "bun dist/index.html",
"publish": "npm publish --access public",
"lint": "eslint . && prettier --check . '!.cloud/**'",
"fix": "eslint . --fix && prettier --write . '!.cloud/**'",
"taze": "taze major -w",
Expand Down
5 changes: 0 additions & 5 deletions apps/web/scripts/obfuscate.sh

This file was deleted.

4 changes: 2 additions & 2 deletions apps/web/src/cache/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { env } from "../env";
import { GAME_EXPOSURE_URL } from "../env.json";
import { FileSystemManager } from "../file-system";
import { isManifestUpToDate } from "../manifest";
import {
Expand Down Expand Up @@ -45,7 +45,7 @@ export class GameCache {
}

private async _updateCacheFile(fileManifest: IManifestFile): Promise<IExtendedManifestFile> {
const res = await fetch(`${env.PUBLIC_BASE_SERVER_URL}/game/${fileManifest.path}`);
const res = await fetch(`${GAME_EXPOSURE_URL}/game/${fileManifest.path}`);

const file = await this.fs.getFile(fileManifest.path);

Expand Down
3 changes: 3 additions & 0 deletions apps/web/src/env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"GAME_EXPOSURE_URL": "http://localhost:8080"
}
3 changes: 0 additions & 3 deletions apps/web/src/env.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/web/src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { env } from "./env";
import { GAME_EXPOSURE_URL } from "./env.json";
import { type IManifest } from "./types/manifest.type";
import { getVersion } from "./version";
import { setLoadingStatus } from "./window";

export const getManifest = async (): Promise<IManifest> => {
setLoadingStatus("Fetching manifest");
const res = await fetch(`${env.PUBLIC_BASE_SERVER_URL}/manifest`);
const res = await fetch(`${GAME_EXPOSURE_URL}/manifest`);
if (!res.ok) {
throw new Error();
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"strict": true,
"skipLibCheck": true
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint": "lerna run lint",
"dev": "lerna run dev",
"build": "lerna run build",
"publish": "lerna run publish",
"test": "lerna run test:unit,test:e2e",
"test:unit": "lerna run test:unit",
"test:e2e": "lerna run test:e2e",
Expand Down