Add features: custom server and websocket. Minor refactoring

This commit is contained in:
2026-03-21 07:42:38 +01:00
parent 1611c845b9
commit 98d3cf7da7
29 changed files with 321 additions and 286 deletions
+1 -5
View File
@@ -1,6 +1,2 @@
import type { ServeOptions } from "bun";
type Params = {
dev?: boolean;
};
export default function (params?: Params): Promise<ServeOptions>;
export {};
export default function (): Promise<ServeOptions>;
+9 -6
View File
@@ -1,17 +1,20 @@
import grabAppPort from "../../utils/grab-app-port";
import isDevelopment from "../../utils/is-development";
import bunextRequestHandler from "./bunext-req-handler";
export default async function (params) {
import grabConfig from "../grab-config";
import _ from "lodash";
export default async function () {
const port = grabAppPort();
const is_dev = isDevelopment();
const development = isDevelopment();
const config = await grabConfig();
return {
async fetch(req, server) {
return await bunextRequestHandler({ req });
},
port,
// idleTimeout: 0,
development: {
hmr: true,
},
idleTimeout: development ? 0 : undefined,
development,
websocket: config?.websocket,
..._.omit(config?.serverOptions || {}, ["fetch"]),
};
}
+1 -5
View File
@@ -1,5 +1 @@
type Params = {
dev?: boolean;
};
export default function startServer(params?: Params): Promise<import("bun").Server>;
export {};
export default function startServer(): Promise<import("bun").Server>;
+1 -44
View File
@@ -1,53 +1,10 @@
import _ from "lodash";
import AppNames from "../../utils/grab-app-names";
import { log } from "../../utils/log";
import allPagesBundler from "../bundler/all-pages-bundler";
import serverParamsGen from "./server-params-gen";
import watcher from "./watcher";
import serverPostBuildFn from "./server-post-build-fn";
import grabDirNames from "../../utils/grab-dir-names";
import EJSON from "../../utils/ejson";
import { readFileSync } from "fs";
import cron from "./cron";
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
export default async function startServer(params) {
const { name } = AppNames;
export default async function startServer() {
const serverParams = await serverParamsGen();
if (params?.dev) {
await allPagesBundler({
watch: true,
post_build_fn: serverPostBuildFn,
});
watcher();
}
else {
const artifacts = EJSON.parse(readFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, "utf-8"));
if (!artifacts?.[0]) {
log.error("Please build first.");
process.exit(1);
}
global.BUNDLER_CTX_MAP = artifacts;
global.IS_FIRST_BUNDLE_READY = true;
cron();
}
let bundle_ready_retries = 0;
const MAX_BUNDLE_READY_RETRIES = 10;
while (!global.IS_FIRST_BUNDLE_READY) {
if (bundle_ready_retries > MAX_BUNDLE_READY_RETRIES) {
log.error("Couldn't grab first bundle for dev environment");
process.exit(1);
}
bundle_ready_retries++;
await Bun.sleep(500);
}
const server = Bun.serve(serverParams);
global.SERVER = server;
log.server(`http://localhost:${server.port}`);
/**
* First Rebuild to Avoid errors
*/
if (params?.dev && global.BUNDLER_CTX) {
await global.BUNDLER_CTX.rebuild();
}
return server;
}
+1 -1
View File
@@ -3,5 +3,5 @@ type Params = {
tsx: string;
out_file: string;
};
export default function writeHMRTsxModule({ tsx, out_file }: Params): Promise<Pick<BundlerCTXMap, "css_path" | "path" | "hash" | "type"> | undefined>;
export default function writeHMRTsxModule({ tsx, out_file }: Params): Promise<Pick<BundlerCTXMap, "path" | "css_path" | "hash" | "type"> | undefined>;
export {};