Fix errors, bugs and known vulerabilities

This commit is contained in:
Benjamin Toby 2026-03-23 06:58:41 +01:00
parent 0308ea32ec
commit 567fb4f746
15 changed files with 84 additions and 52 deletions

View File

@ -19,7 +19,10 @@
"micromatch": "^4.0.8", "micromatch": "^4.0.8",
"ora": "^9.0.0", "ora": "^9.0.0",
"postcss": "^8.5.8", "postcss": "^8.5.8",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"tailwindcss": "^4.2.2", "tailwindcss": "^4.2.2",
"typescript": "^5.0.0",
}, },
"devDependencies": { "devDependencies": {
"@testing-library/dom": "^10.4.1", "@testing-library/dom": "^10.4.1",
@ -27,11 +30,6 @@
"@types/micromatch": "^4.0.10", "@types/micromatch": "^4.0.10",
"happy-dom": "^20.8.4", "happy-dom": "^20.8.4",
}, },
"peerDependencies": {
"react": "^18.0.0 || ^19.0.0",
"react-dom": "^18.0.0 || ^19.0.0",
"typescript": "^5.0.0",
},
}, },
}, },
"packages": { "packages": {

View File

@ -2,7 +2,7 @@
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"module": "index.ts", "module": "index.ts",
"type": "module", "type": "module",
"version": "1.0.17", "version": "1.0.18",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"exports": { "exports": {

View File

@ -1,11 +1,10 @@
import { Command } from "commander"; import { Command } from "commander";
import allPagesBundler from "../../functions/bundler/all-pages-bundler";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import init from "../../functions/init"; import init from "../../functions/init";
import rewritePagesModule from "../../utils/rewrite-pages-module"; import rewritePagesModule from "../../utils/rewrite-pages-module";
import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler"; import allPagesBunBundler from "../../functions/bundler/all-pages-bun-bundler";
import { execSync } from "child_process";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
@ -17,8 +16,8 @@ export default function () {
process.env.BUILD = "true"; process.env.BUILD = "true";
try { try {
execSync(`rm -rf ${HYDRATION_DST_DIR}`); rmSync(HYDRATION_DST_DIR, { recursive: true });
execSync(`rm -rf ${BUNX_CWD_PAGES_REWRITE_DIR}`); rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
} catch (error) {} } catch (error) {}
await rewritePagesModule(); await rewritePagesModule();

View File

@ -3,8 +3,8 @@ import startServer from "../../functions/server/start-server";
import { log } from "../../utils/log"; import { log } from "../../utils/log";
import bunextInit from "../../functions/bunext-init"; import bunextInit from "../../functions/bunext-init";
import rewritePagesModule from "../../utils/rewrite-pages-module"; import rewritePagesModule from "../../utils/rewrite-pages-module";
import { execSync } from "child_process";
import grabDirNames from "../../utils/grab-dir-names"; import grabDirNames from "../../utils/grab-dir-names";
import { rmSync } from "fs";
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
@ -12,13 +12,13 @@ export default function () {
return new Command("dev") return new Command("dev")
.description("Run development server") .description("Run development server")
.action(async () => { .action(async () => {
process.env.NODE_ENV == "development"; process.env.NODE_ENV = "development";
log.info("Running development server ..."); log.info("Running development server ...");
try { try {
execSync(`rm -rf ${HYDRATION_DST_DIR}`); rmSync(HYDRATION_DST_DIR, { recursive: true });
execSync(`rm -rf ${BUNX_CWD_PAGES_REWRITE_DIR}`); rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
} catch (error) {} } catch (error) {}
await rewritePagesModule(); await rewritePagesModule();

View File

@ -82,7 +82,7 @@ export default async function allPagesBunBundler(params?: Params) {
], ],
}); });
Bun.write( await Bun.write(
path.join(BUNX_TMP_DIR, "bundle.json"), path.join(BUNX_TMP_DIR, "bundle.json"),
JSON.stringify(result, null, 4), JSON.stringify(result, null, 4),
{ createPath: true }, { createPath: true },

View File

@ -1,6 +1,5 @@
import { existsSync, mkdirSync, writeFileSync } from "fs"; import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
import grabDirNames from "../utils/grab-dir-names"; import grabDirNames from "../utils/grab-dir-names";
import { execSync } from "child_process";
import path from "path"; import path from "path";
import grabConfig from "./grab-config"; import grabConfig from "./grab-config";
import type { BunextConfig } from "../types"; import type { BunextConfig } from "../types";
@ -9,8 +8,32 @@ export default async function () {
const dirNames = grabDirNames(); const dirNames = grabDirNames();
const is_dev = !Boolean(process.env.NODE_ENV == "production"); const is_dev = !Boolean(process.env.NODE_ENV == "production");
execSync(`rm -rf ${dirNames.BUNEXT_CACHE_DIR}`); rmSync(dirNames.BUNEXT_CACHE_DIR, {
execSync(`rm -rf ${dirNames.BUNX_CWD_MODULE_CACHE_DIR}`); recursive: true,
force: true,
});
rmSync(dirNames.BUNX_CWD_MODULE_CACHE_DIR, {
recursive: true,
force: true,
});
try {
const react_package_dir = path.join(
dirNames.ROOT_DIR,
"node_modules",
"react",
);
const react_dom_package_dir = path.join(
dirNames.ROOT_DIR,
"node_modules",
"react-dom",
);
if (dirNames.BUNX_ROOT_DIR !== dirNames.ROOT_DIR) {
rmSync(react_package_dir, { recursive: true });
rmSync(react_dom_package_dir, { recursive: true });
}
} catch (error) {}
try { try {
const package_json = await Bun.file( const package_json = await Bun.file(

View File

@ -18,6 +18,10 @@ export default async function ({ req }: Params): Promise<Response> {
url.pathname.replace(/\/\.bunext\/public\/pages\//, ""), url.pathname.replace(/\/\.bunext\/public\/pages\//, ""),
); );
if (!file_path.startsWith(HYDRATION_DST_DIR + path.sep)) {
return new Response("Forbidden", { status: 403 });
}
if (!existsSync(file_path)) { if (!existsSync(file_path)) {
return new Response(`File Doesn't Exist`, { return new Response(`File Doesn't Exist`, {
status: 404, status: 404,

View File

@ -15,6 +15,10 @@ export default async function ({ req }: Params): Promise<Response> {
const url = new URL(req.url); const url = new URL(req.url);
const file_path = path.join(PUBLIC_DIR, url.pathname); const file_path = path.join(PUBLIC_DIR, url.pathname);
if (!file_path.startsWith(PUBLIC_DIR + path.sep)) {
return new Response("Forbidden", { status: 403 });
}
if (!existsSync(file_path)) { if (!existsSync(file_path)) {
return new Response(`File Doesn't Exist`, { return new Response(`File Doesn't Exist`, {
status: 404, status: 404,

View File

@ -19,6 +19,10 @@ export default async function ({ req }: Params): Promise<Response> {
url.pathname.replace(/^\/public/, ""), url.pathname.replace(/^\/public/, ""),
); );
if (!file_path.startsWith(PUBLIC_DIR + path.sep)) {
return new Response("Forbidden", { status: 403 });
}
if (!existsSync(file_path)) { if (!existsSync(file_path)) {
return new Response(`Public File Doesn't Exist`, { return new Response(`Public File Doesn't Exist`, {
status: 404, status: 404,

View File

@ -28,7 +28,7 @@ export default async function ({ req }: Params): Promise<Response> {
msg: errMsg, msg: errMsg,
}, },
{ {
status: 401, status: 404,
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },

View File

@ -11,7 +11,7 @@ export default async function serverPostBuildFn() {
return; return;
} }
for (let i = 0; i < global.HMR_CONTROLLERS.length; i++) { for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
const controller = global.HMR_CONTROLLERS[i]; const controller = global.HMR_CONTROLLERS[i];
if (!controller.target_map?.local_path) { if (!controller.target_map?.local_path) {

View File

@ -61,9 +61,11 @@ export default async function genWebHTML({
html += ` <link rel="stylesheet" href="/${bundledMap.css_path}" />\n`; html += ` <link rel="stylesheet" href="/${bundledMap.css_path}" />\n`;
} }
html += ` <script>window.${ClientWindowPagePropsName} = ${ const serializedProps = (EJSON.stringify(pageProps || {}) || "{}").replace(
EJSON.stringify(pageProps || {}) || "{}" /<\//g,
}</script>\n`; "<\\/",
);
html += ` <script>window.${ClientWindowPagePropsName} = ${serializedProps}</script>\n`;
if (bundledMap?.path) { if (bundledMap?.path) {
const dev = isDevelopment(); const dev = isDevelopment();

View File

@ -1,7 +1,6 @@
import type { FC } from "react"; import type { FC } from "react";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import type { import type {
BundlerCTXMap,
BunextPageModule, BunextPageModule,
BunxRouteParams, BunxRouteParams,
GrabPageComponentRes, GrabPageComponentRes,
@ -34,7 +33,7 @@ export default async function grabPageErrorComponent({
const bundledMap = match?.filePath const bundledMap = match?.filePath
? global.BUNDLER_CTX_MAP?.[match.filePath] ? global.BUNDLER_CTX_MAP?.[match.filePath]
: ({} as BundlerCTXMap); : undefined;
const module: BunextPageModule = await import(filePath); const module: BunextPageModule = await import(filePath);
const Component = module.default as FC<any>; const Component = module.default as FC<any>;
@ -72,7 +71,7 @@ export default async function grabPageErrorComponent({
component: <DefaultNotFound />, component: <DefaultNotFound />,
routeParams, routeParams,
module: { default: DefaultNotFound }, module: { default: DefaultNotFound },
bundledMap: {} as BundlerCTXMap, bundledMap: undefined,
serverRes: { serverRes: {
responseOptions: { responseOptions: {
status: is404 ? 404 : 500, status: is404 ? 404 : 500,

View File

@ -1,3 +1,4 @@
import { escape } from "lodash";
import type { BunextPageModuleMeta } from "../../../types"; import type { BunextPageModuleMeta } from "../../../types";
type Params = { type Params = {
@ -8,68 +9,68 @@ export default function grabWebMetaHTML({ meta }: Params) {
let html = ``; let html = ``;
if (meta.title) { if (meta.title) {
html += ` <title>${meta.title}</title>\n`; html += ` <title>${escape(meta.title)}</title>\n`;
} }
if (meta.description) { if (meta.description) {
html += ` <meta name="description" content="${meta.description}" />\n`; html += ` <meta name="description" content="${escape(meta.description)}" />\n`;
} }
if (meta.keywords) { if (meta.keywords) {
const keywords = Array.isArray(meta.keywords) const keywords = Array.isArray(meta.keywords)
? meta.keywords.join(", ") ? meta.keywords.join(", ")
: meta.keywords; : meta.keywords;
html += ` <meta name="keywords" content="${keywords}" />\n`; html += ` <meta name="keywords" content="${escape(keywords)}" />\n`;
} }
if (meta.author) { if (meta.author) {
html += ` <meta name="author" content="${meta.author}" />\n`; html += ` <meta name="author" content="${escape(meta.author)}" />\n`;
} }
if (meta.robots) { if (meta.robots) {
html += ` <meta name="robots" content="${meta.robots}" />\n`; html += ` <meta name="robots" content="${escape(meta.robots)}" />\n`;
} }
if (meta.canonical) { if (meta.canonical) {
html += ` <link rel="canonical" href="${meta.canonical}" />\n`; html += ` <link rel="canonical" href="${escape(meta.canonical)}" />\n`;
} }
if (meta.themeColor) { if (meta.themeColor) {
html += ` <meta name="theme-color" content="${meta.themeColor}" />\n`; html += ` <meta name="theme-color" content="${escape(meta.themeColor)}" />\n`;
} }
if (meta.og) { if (meta.og) {
const { og } = meta; const { og } = meta;
if (og.title) if (og.title)
html += ` <meta property="og:title" content="${og.title}" />\n`; html += ` <meta property="og:title" content="${escape(og.title)}" />\n`;
if (og.description) if (og.description)
html += ` <meta property="og:description" content="${og.description}" />\n`; html += ` <meta property="og:description" content="${escape(og.description)}" />\n`;
if (og.image) if (og.image)
html += ` <meta property="og:image" content="${og.image}" />\n`; html += ` <meta property="og:image" content="${escape(og.image)}" />\n`;
if (og.url) if (og.url)
html += ` <meta property="og:url" content="${og.url}" />\n`; html += ` <meta property="og:url" content="${escape(og.url)}" />\n`;
if (og.type) if (og.type)
html += ` <meta property="og:type" content="${og.type}" />\n`; html += ` <meta property="og:type" content="${escape(og.type)}" />\n`;
if (og.siteName) if (og.siteName)
html += ` <meta property="og:site_name" content="${og.siteName}" />\n`; html += ` <meta property="og:site_name" content="${escape(og.siteName)}" />\n`;
if (og.locale) if (og.locale)
html += ` <meta property="og:locale" content="${og.locale}" />\n`; html += ` <meta property="og:locale" content="${escape(og.locale)}" />\n`;
} }
if (meta.twitter) { if (meta.twitter) {
const { twitter } = meta; const { twitter } = meta;
if (twitter.card) if (twitter.card)
html += ` <meta name="twitter:card" content="${twitter.card}" />\n`; html += ` <meta name="twitter:card" content="${escape(twitter.card)}" />\n`;
if (twitter.title) if (twitter.title)
html += ` <meta name="twitter:title" content="${twitter.title}" />\n`; html += ` <meta name="twitter:title" content="${escape(twitter.title)}" />\n`;
if (twitter.description) if (twitter.description)
html += ` <meta name="twitter:description" content="${twitter.description}" />\n`; html += ` <meta name="twitter:description" content="${escape(twitter.description)}" />\n`;
if (twitter.image) if (twitter.image)
html += ` <meta name="twitter:image" content="${twitter.image}" />\n`; html += ` <meta name="twitter:image" content="${escape(twitter.image)}" />\n`;
if (twitter.site) if (twitter.site)
html += ` <meta name="twitter:site" content="${twitter.site}" />\n`; html += ` <meta name="twitter:site" content="${escape(twitter.site)}" />\n`;
if (twitter.creator) if (twitter.creator)
html += ` <meta name="twitter:creator" content="${twitter.creator}" />\n`; html += ` <meta name="twitter:creator" content="${escape(twitter.creator)}" />\n`;
} }
return html; return html;

View File

@ -67,11 +67,9 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) {
} }
return pages_files.sort((a, b) => { return pages_files.sort((a, b) => {
if (a.url_path == "/index") { if (a.url_path === "/index") return -1;
return -1; if (b.url_path === "/index") return 1;
} return 0;
return 1;
}); });
} }