bolt.diy/electron/main/utils/serve.ts
Derek Wang 1ce6ad6b59
Some checks failed
Docker Publish / docker-build-publish (push) Has been cancelled
Update Stable Branch / prepare-release (push) Has been cancelled
feat: electron desktop app without express server (#1136)
* feat: add electron app

* refactor: using different approach

* chore: update commit hash to 02621e3545

* fix: working dev but prod showing not found and lint fix

* fix: add icon

* fix: resolve server file load issue

* fix: eslint and prettier wip

* fix: only load server build once

* fix: forward request for other ports

* fix: use cloudflare {} to avoid crash

* fix: no need for appLogger

* fix: forward cookie

* fix: update script and update preload loading path

* chore: minor update for appId

* fix: store and load all cookies

* refactor: split main/index.ts

* refactor: group electron main files into two folders

* fix: update electron build configs

* fix: update auto update feat

* fix: vite-plugin-node-polyfills need to be in dependencies for dmg version to work

* ci: trigger build for electron branch

* ci: mark draft if it's from branch commit

* ci: add icons for windows and linux

* fix: update icons for windows

* fix: add author in package.json

* ci: use softprops/action-gh-release@v2

* fix: use path to join

* refactor: refactor path logic for working in both mac and windows

* fix: still need vite-plugin-node-polyfills dependencies

* fix: update vite-electron.config.ts

* ci: sign mac app

* refactor: assets folder

* ci: notarization

* ci: add NODE_OPTIONS

* ci: window only nsis dist

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-20 00:22:06 +05:30

72 lines
2.2 KiB
TypeScript

import { createReadableStreamFromReadable } from '@remix-run/node';
import type { ServerBuild } from '@remix-run/node';
import mime from 'mime';
import { createReadStream, promises as fs } from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { app } from 'electron';
import { isDev } from './constants';
export async function loadServerBuild(): Promise<any> {
if (isDev) {
console.log('Dev mode: server build not loaded');
return;
}
const serverBuildPath = path.join(app.getAppPath(), 'build', 'server', 'index.js');
console.log(`Loading server build... path is ${serverBuildPath}`);
try {
const fileUrl = pathToFileURL(serverBuildPath).href;
const serverBuild: ServerBuild = /** @type {ServerBuild} */ await import(fileUrl);
console.log('Server build loaded successfully');
// eslint-disable-next-line consistent-return
return serverBuild;
} catch (buildError) {
console.log('Failed to load server build:', {
message: (buildError as Error)?.message,
stack: (buildError as Error)?.stack,
error: JSON.stringify(buildError, Object.getOwnPropertyNames(buildError as object)),
});
return;
}
}
// serve assets built by vite.
export async function serveAsset(req: Request, assetsPath: string): Promise<Response | undefined> {
const url = new URL(req.url);
const fullPath = path.join(assetsPath, decodeURIComponent(url.pathname));
console.log('Serving asset, path:', fullPath);
if (!fullPath.startsWith(assetsPath)) {
console.log('Path is outside assets directory:', fullPath);
return;
}
const stat = await fs.stat(fullPath).catch((err) => {
console.log('Failed to stat file:', fullPath, err);
return undefined;
});
if (!stat?.isFile()) {
console.log('Not a file:', fullPath);
return;
}
const headers = new Headers();
const mimeType = mime.getType(fullPath);
if (mimeType) {
headers.set('Content-Type', mimeType);
}
console.log('Serving file with mime type:', mimeType);
const body = createReadableStreamFromReadable(createReadStream(fullPath));
// eslint-disable-next-line consistent-return
return new Response(body, { headers });
}