A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(editor): serve the packaged renderer over loopback http (popout in packaging)
yhhc4a006a0f853
diff --git a/desktop/electron/exportPreview.ts b/desktop/electron/loopbackServer.tssimilarity index 51%rename from desktop/electron/exportPreview.tsrename to desktop/electron/loopbackServer.tsindex 307883824..ac6d82780 100644--- a/desktop/electron/exportPreview.ts+++ b/desktop/electron/loopbackServer.ts@@ -1,20 +1,25 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team/**- * @file exportPreview.ts — preview an http-servable export (web / playable) over a- * loopback http server, the way it actually deploys.+ * @file loopbackServer.ts — serve a static directory over a loopback http origin.*- * A single-file playable or a web build opened via `file://` hits the browser's- * opaque-origin rules — every `file:` URL is a unique origin, so subresource- * loads are treated cross-origin (the "unsafe attempt to load URL … unique- * security origin" notice) and wasm can't stream-compile. Served over `http`- * (a real origin — exactly the ad-network iframe / static host the build ships- * to) both concerns vanish. So the correct preview is http, not the OS opening- * the file.+ * Two callers, one need for a *real http origin* rather than `file://` or the+ * `app://` custom scheme:*- * One server per served root, reused across re-previews (files are read per- * request with no-store, so a re-export serves fresh bytes on the same URL).- * All are closed on app quit. Pure Node (http/fs) — IPC wiring is in main.ts.+ * • The packaged editor shell (dist/). dockview's "pop a panel into its own OS+ * window" opens a same-origin child via `window.open`, and it rejects any+ * opener that isn't http(s) same-origin — the `app://` custom scheme fails+ * that check. Serving dist/ over http gives popouts a valid origin. (Dev+ * already runs on http via Vite, so this only matters when packaged.)+ * • Export preview. A single-file playable or web build opened via `file://`+ * hits opaque-origin rules — every `file:` URL is a unique origin, so+ * subresource loads read cross-origin and wasm can't stream-compile. Served+ * over http (the ad-network iframe / static host it ships to) both vanish.+ *+ * One server per served root, reused across callers (files are read per request+ * with no-store, so a re-export serves fresh bytes on the same URL). Bound to+ * 127.0.0.1 on an ephemeral port; all are closed on app quit. Pure Node+ * (http/fs) — IPC wiring is in main.ts.*/import http from 'node:http';import { readFile } from 'node:fs/promises';@@ -26,14 +31,14 @@ import { httpContentType } from './mimeTypes';const servers = new Map<string, { server: http.Server; url: string }>();/**- * Start (or reuse) a loopback http server rooted at `rootDir` and return its base URL.- * Throws if the directory has no `index.html` (nothing to preview) — a clear failure- * beats silently serving 404s.+ * Start (or reuse) a loopback http server rooted at `rootDir` and return its base URL+ * (with a trailing slash). Throws if the directory has no `index.html` — a clear+ * failure beats silently serving 404s.*/-export async function previewServer(rootDir: string): Promise<string> {+export async function loopbackServer(rootDir: string): Promise<string> {const root = path.resolve(rootDir);if (!existsSync(path.join(root, 'index.html'))) {- throw new Error(`no index.html in ${root} — nothing to preview`);+ throw new Error(`no index.html in ${root} — nothing to serve`);}const existing = servers.get(root);if (existing) return existing.url;@@ -62,8 +67,8 @@ export async function previewServer(rootDir: string): Promise<string> {return url;}-/** Close every preview server (on app quit). */-export function closeAllPreviewServers(): void {+/** Close every loopback server (on app quit). */+export function closeAllLoopbackServers(): void {for (const { server } of servers.values()) {try { server.close(); } catch { /* already closing */ }}diff --git a/desktop/electron/main.ts b/desktop/electron/main.tsindex 5da7de12a..42f302897 100644--- a/desktop/electron/main.ts+++ b/desktop/electron/main.ts@@ -28,7 +28,7 @@ import { cookAssets } from './cookAssets';import { startProjectWatch, stopProjectWatch } from './projectWatcher';import { importAssets, createAsset, IMPORT_EXTENSIONS } from './importAssets';import { exportGame } from './exportGame';-import { previewServer, closeAllPreviewServers } from './exportPreview';+import { loopbackServer, closeAllLoopbackServers } from './loopbackServer';import { httpContentType } from './mimeTypes';import { buildPlayRealm } from './buildPlayRealm';import { ensureSdkTypes } from './syncSdkTypes';@@ -47,10 +47,12 @@ app.commandLine.appendSwitch('enable-unsafe-webgpu');// Two privileged custom schemes (must be declared before app ready):// • estella:// serves files from the open project root (sandboxed) — lets the// engine fetch project assets (textures via Assets.loadTexture → fetch).-// • app:// serves the built renderer (dist/) so the editor + play realm load-// over a STABLE origin instead of file://. Under file:// the engine glue path-// `${location.origin}/wasm/esengine.js` resolves to the filesystem root (404);-// app:// gives a real origin where `/wasm/...` + the play iframe resolve.+// • app:// serves the built renderer (dist/) — kept as a fallback origin. The+// packaged renderer normally loads over a loopback http origin instead (see+// loopbackServer + appBaseUrl) so it has a real http origin: the engine glue path+// `${location.origin}/wasm/esengine.js` resolves there, and dockview popouts get+// the same-origin http opener they require. app:// is a stable fallback if the+// loopback server can't start. (Under file:// the glue path 404s at the fs root.)// corsEnabled: the renderer reads texture pixels via `<img crossOrigin>` (TextureLoader)// + `fetch`, which are CORS requests. Without it Chromium rejects custom-scheme cross-// origin at the scheme level (even though the handler returns `access-control-allow-origin:@@ -67,11 +69,18 @@ protocol.registerSchemesAsPrivileged([},]);-// The app:// renderer origin (host is arbitrary; `local` keeps URLs readable).+// The app:// renderer origin (host is arbitrary; `local` keeps URLs readable). Kept+// as a fallback; the packaged renderer normally loads over a loopback http origin+// (see appBaseUrl) so dockview popouts get the same-origin http opener they require.const APP_ORIGIN = 'app://local';+// Where the renderer is served from: Vite dev server, a loopback http origin when+// packaged (set in whenReady), or app:// as a last resort. Trailing slash so+// `${appBaseUrl}index.html` composes cleanly.+let appBaseUrl = `${APP_ORIGIN}/`;+// dockview pops a dock panel into its own window by opening `popout.html` on our-// origin (http://localhost in dev, app://local when packaged). Match by pathname so+// origin (http://localhost in dev, http://127.0.0.1 when packaged). Match by pathname so// the window-open handler can tell a legit panel pop-out from an external link.const isPopoutUrl = (url: string): boolean => {try {@@ -412,9 +421,10 @@ function createWindow() {// SHOT_DEVTOOLS: measure the overhead DevTools itself adds to a run.if (!shotOut || process.env.ESTELLA_SHOT_DEVTOOLS) win.webContents.openDevTools({ mode: 'detach' });} else {- // Load over app:// (not file://) so the engine glue + play realm resolve from- // a real origin. handleApp serves dist/.- win.loadURL(`${APP_ORIGIN}/index.html${automation}`);+ // Load over the loopback http origin (set in whenReady) so the renderer has a real+ // http origin — the engine glue resolves from it and dockview popouts can open a+ // same-origin http child window. Falls back to app:// if the server didn't start.+ win.loadURL(`${appBaseUrl}index.html${automation}`);}if (shotOut) void runScreenshot(win, shotOut);@@ -692,7 +702,7 @@ ipcMain.handle(// http origin (static host / ad-network iframe) — so the file:// opaque-origin rules// (blocked subresource loads, no wasm streaming) never apply. Returns the URL.ipcMain.handle('export:preview', async (_e, absDir: string) => {- const url = await previewServer(absDir);+ const url = await loopbackServer(absDir);await shell.openExternal(url);return url;});@@ -856,9 +866,22 @@ async function handleApp(request: Request): Promise<Response> {installCrashCapture();-app.whenReady().then(() => {+app.whenReady().then(async () => {protocol.handle('estella', handleEstella);protocol.handle('app', handleApp);+ // Packaged: serve the built renderer (dist/) over a loopback http origin so it has a+ // real http origin, the way dev already runs on Vite's http. dockview's pop-a-panel-+ // out uses a same-origin `window.open`, which it rejects for the app:// custom scheme;+ // http satisfies it. estella:// (project assets + the play realm) is a separate scheme+ // and is unaffected. Fall back to app:// if the server can't start.+ if (!VITE_DEV_SERVER_URL) {+ try {+ appBaseUrl = await loopbackServer(RENDERER_DIST);+ } catch (err) {+ console.error('[loopback] renderer server failed; falling back to app://', err);+ appBaseUrl = `${APP_ORIGIN}/`;+ }+ }createWindow();// Startup update check: silent unless a newer release exists (offline = no-op).@@ -884,5 +907,5 @@ app.on('window-all-closed', () => {if (process.platform !== 'darwin') app.quit();});-// Tear down any loopback export-preview servers on quit.-app.on('before-quit', () => closeAllPreviewServers());+// Tear down the loopback servers (renderer shell + export previews) on quit.+app.on('before-quit', () => closeAllLoopbackServers());