@torpor/adapter-electron 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/main.mjs +4 -2
- package/dist/runtime.d.mts +12 -6
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +16 -10
- package/dist/runtime.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -117,7 +117,7 @@ function devServerUrl(server) {
|
|
|
117
117
|
const RUNTIME_FILES = ["main.mjs", "preload.cjs"];
|
|
118
118
|
async function postbuild(site, options) {
|
|
119
119
|
const sourceFolder = path.dirname(fileURLToPath(import.meta.url));
|
|
120
|
-
const outputFolder = path.join(site.
|
|
120
|
+
const outputFolder = path.join(site.outputFolder, "electron");
|
|
121
121
|
await promises.mkdir(outputFolder, { recursive: true });
|
|
122
122
|
for (const file of RUNTIME_FILES) await promises.copyFile(path.join(sourceFolder, file), path.join(outputFolder, file));
|
|
123
123
|
const config = {
|
|
@@ -177,7 +177,7 @@ function electron(options = {}) {
|
|
|
177
177
|
};
|
|
178
178
|
}
|
|
179
179
|
function serve(site) {
|
|
180
|
-
const appFolder = path.join(site.
|
|
180
|
+
const appFolder = path.join(site.outputFolder, "electron");
|
|
181
181
|
if (!existsSync(path.join(appFolder, "main.mjs"))) throw new Error("Electron output not found. Run a build first (dist/electron/main.mjs is missing).");
|
|
182
182
|
const child = spawn(electronBinary(site.root), [appFolder], {
|
|
183
183
|
cwd: site.root,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["fs"],"sources":["../src/electronBinary.ts","../src/sourceMode.ts","../src/electronDev.ts","../src/postbuild.ts","../src/adapter.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\nimport path from \"node:path\";\n\n/**\n * Resolves the Electron executable to run, preferring the copy installed in\n * the site's node_modules (`require(\"electron\")` resolves to the binary path)\n * and falling back to `electron` on PATH. The package may not be installed --\n * e.g. a CI build that only builds the site -- in which case launching is\n * skipped and callers report how to install it.\n */\nexport default function electronBinary(siteRoot: string): string {\n\ttry {\n\t\tconst require = createRequire(path.join(siteRoot, \"package.json\"));\n\t\tconst resolved: unknown = require(\"electron\");\n\t\treturn typeof resolved === \"string\" ? resolved : \"electron\";\n\t} catch {\n\t\treturn \"electron\";\n\t}\n}\n","import { realpathSync } from \"node:fs\";\nimport path from \"node:path\";\n\n// Mirrors the entry-path logic in @torpor/build's `utils/entryPaths.ts` (kept\n// local so the adapter stays compatible with older @torpor/build installs).\n\n/**\n * Whether the framework is symlinked into the app from outside node_modules\n * (workspace links and `link:` overrides), meaning tb runs it from source via\n * the `development` export conditions. Registry installs always run dist.\n */\nexport default function detectSourceMode(siteRoot: string): boolean {\n\tif (process.env.TORPOR_SOURCE_DEV) return true;\n\tconst packages = [\"@torpor/build\", \"@torpor/view\"];\n\treturn packages.every((pkg) => {\n\t\ttry {\n\t\t\tconst dist = path.resolve(siteRoot, \"node_modules\", pkg, \"dist/index.mjs\");\n\t\t\tconst real = realpathSync(dist);\n\t\t\treturn !real.includes(\"node_modules\");\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t});\n}\n","import { type Site } from \"@torpor/build\";\nimport { nodeDev } from \"@torpor/build/node-dev\";\nimport { spawn, type ChildProcess } from \"node:child_process\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { Plugin, ViteDevServer } from \"vite\";\nimport electronBinary from \"./electronBinary\";\nimport detectSourceMode from \"./sourceMode\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * Dev support: the framework's default Node dev runtime (which serves SSR off\n * the Vite dev server) plus a launcher that opens an Electron window pointed at\n * it. Without `nodeDev`, providing `Adapter.dev` would replace the Node\n * runtime entirely, which Electron doesn't need -- it runs on Node.\n */\nexport default function electronDev(site: Site, options: ElectronOptions): Plugin[] {\n\treturn [nodeDev(site, detectSourceMode(site.root)), electronLauncher(site, options)];\n}\n\nfunction electronLauncher(site: Site, options: ElectronOptions): Plugin {\n\tlet child: ChildProcess | undefined;\n\tlet quitting = false;\n\n\tconst stopChild = (): void => {\n\t\tchild?.kill();\n\t\tchild = undefined;\n\t};\n\n\treturn {\n\t\tname: \"torpor-electron-dev\",\n\t\tconfigureServer(server: ViteDevServer) {\n\t\t\tif (options.dev === false || process.env.TORPOR_ELECTRON === \"0\") return;\n\n\t\t\tconst start = (): void => {\n\t\t\t\tconst url = devServerUrl(server);\n\t\t\t\tif (!url) return;\n\t\t\t\tstopChild();\n\t\t\t\tquitting = false;\n\n\t\t\t\tconst proc = launch(site, url, options);\n\t\t\t\tchild = proc;\n\n\t\t\t\t// When the user quits the app (Cmd+Q, or closing the window),\n\t\t\t\t// stop the dev server too, so `tb --dev` exits with the desktop\n\t\t\t\t// app. A failed launch emits \"error\", not \"exit\", so browser-only\n\t\t\t\t// dev is unaffected\n\t\t\t\tproc.once(\"exit\", () => {\n\t\t\t\t\tif (quitting) return;\n\t\t\t\t\tquitting = true;\n\t\t\t\t\tvoid server.close();\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tconst httpServer = server.httpServer;\n\t\t\tif (httpServer?.listening) {\n\t\t\t\tstart();\n\t\t\t} else {\n\t\t\t\thttpServer?.once(\"listening\", start);\n\t\t\t}\n\n\t\t\t// Vite is shutting down (Ctrl+C, a restart, or close() from the\n\t\t\t// handler above): kill the window without re-closing the server\n\t\t\thttpServer?.once(\"close\", () => {\n\t\t\t\tquitting = true;\n\t\t\t\tstopChild();\n\t\t\t});\n\n\t\t\tprocess.once(\"exit\", stopChild);\n\t\t},\n\t};\n}\n\nfunction launch(site: Site, url: string, options: ElectronOptions): ChildProcess {\n\tconst adapterFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst entry = path.join(adapterFolder, \"dev-main.mjs\");\n\n\tconst config = {\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\n\tconst child = spawn(electronBinary(site.root), [entry], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t\tenv: {\n\t\t\t...process.env,\n\t\t\tTORPOR_ELECTRON_URL: url,\n\t\t\tTORPOR_ELECTRON_CONFIG: JSON.stringify(config),\n\t\t},\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.warn(\n\t\t\t\t\"[torpor] Electron is not installed, so dev is running in the browser only. \" +\n\t\t\t\t\t\"Install it with `npm install -D electron` to open a desktop window.\",\n\t\t\t);\n\t\t} else {\n\t\t\tconsole.warn(`[torpor] Could not launch Electron: ${error.message}`);\n\t\t}\n\t});\n\n\treturn child;\n}\n\nfunction devServerUrl(server: ViteDevServer): string | undefined {\n\tconst local = server.resolvedUrls?.local?.[0];\n\tif (local) return local;\n\n\tconst address = server.httpServer?.address();\n\tif (address && typeof address === \"object\") {\n\t\tconst protocol = server.config.server.https ? \"https\" : \"http\";\n\t\tconst host =\n\t\t\taddress.address === \"::\" || address.address === \"0.0.0.0\" ? \"localhost\" : address.address;\n\t\treturn `${protocol}://${host}:${address.port}`;\n\t}\n\n\treturn undefined;\n}\n","import type { Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { ElectronOptions } from \"./types\";\n\n// The Electron main and preload scripts, copied verbatim from the adapter's\n// dist into the site's dist/electron output\nconst RUNTIME_FILES = [\"main.mjs\", \"preload.cjs\"];\n\nexport default async function postbuild(site: Site, options: ElectronOptions): Promise<void> {\n\tconst sourceFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst outputFolder = path.join(site.root, \"dist\", \"electron\");\n\n\tawait fs.mkdir(outputFolder, { recursive: true });\n\tfor (const file of RUNTIME_FILES) {\n\t\tawait fs.copyFile(path.join(sourceFolder, file), path.join(outputFolder, file));\n\t}\n\n\tconst config = {\n\t\thost: options.host ?? \"127.0.0.1\",\n\t\tport: options.port ?? 0,\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\tawait fs.writeFile(path.join(outputFolder, \"electron.json\"), JSON.stringify(config, null, \"\\t\"));\n\n\t// A minimal Electron app manifest. Run it with `electron dist/electron`\n\t// (see the adapter README for packaging with electron-builder/forge)\n\tconst { name, version } = await sitePackage(site.root);\n\tawait fs.writeFile(\n\t\tpath.join(outputFolder, \"package.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tname: `${name}-electron`,\n\t\t\t\tproductName: name,\n\t\t\t\tversion,\n\t\t\t\tprivate: true,\n\t\t\t\ttype: \"module\",\n\t\t\t\tmain: \"main.mjs\",\n\t\t\t\tscripts: { start: \"electron .\" },\n\t\t\t},\n\t\t\tnull,\n\t\t\t\"\\t\",\n\t\t),\n\t);\n}\n\nasync function sitePackage(root: string): Promise<{ name: string; version: string }> {\n\ttry {\n\t\tconst pkg = JSON.parse(await fs.readFile(path.join(root, \"package.json\"), \"utf-8\"));\n\t\tconst name = typeof pkg.name === \"string\" ? pkg.name.replace(/^@[^/]+\\//, \"\") : \"torpor-app\";\n\t\tconst version = typeof pkg.version === \"string\" ? pkg.version : \"1.0.0\";\n\t\treturn { name, version };\n\t} catch {\n\t\treturn { name: \"torpor-app\", version: \"1.0.0\" };\n\t}\n}\n","import { type Adapter, type Site } from \"@torpor/build\";\nimport { spawn } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport electronBinary from \"./electronBinary\";\nimport electronDev from \"./electronDev\";\nimport postbuild from \"./postbuild\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * A @torpor/build adapter for desktop apps.\n *\n * Builds emit an Electron app into `dist/electron` (a main process that serves\n * the site over a loopback HTTP server, a context-isolated preload, and a\n * manifest); preview launches it, and dev opens a window against the Vite dev\n * server. Requests run in the Electron main process on Node, so cookies,\n * sessions and redirects behave exactly as on a normal Node server.\n *\n * ```ts\n * import { electron } from \"@torpor/adapter-electron\";\n *\n * site.adapter = electron();\n * ```\n */\nexport default function electron(options: ElectronOptions = {}): Adapter {\n\treturn {\n\t\tdev: (site: Site) => electronDev(site, options),\n\t\tpostbuild: (site: Site) => postbuild(site, options),\n\t\tserve: (_server, site: Site) => serve(site),\n\t};\n}\n\nfunction serve(site: Site): void {\n\tconst appFolder = path.join(site.root, \"dist\", \"electron\");\n\tif (!existsSync(path.join(appFolder, \"main.mjs\"))) {\n\t\tthrow new Error(\n\t\t\t\"Electron output not found. Run a build first (dist/electron/main.mjs is missing).\",\n\t\t);\n\t}\n\n\tconst child = spawn(electronBinary(site.root), [appFolder], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t\"[torpor] Electron is not installed. Install it with `npm install -D electron`.\",\n\t\t\t);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tthrow error;\n\t});\n\n\tchild.on(\"exit\", (code) => process.exit(code ?? 0));\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,SAAwB,eAAe,UAA0B;CAChE,IAAI;EAEH,MAAM,WADU,cAAc,KAAK,KAAK,UAAU,cAAc,CAChC,CAAC,CAAC,UAAU;EAC5C,OAAO,OAAO,aAAa,WAAW,WAAW;CAClD,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;;ACPA,SAAwB,iBAAiB,UAA2B;CACnE,IAAI,QAAQ,IAAI,mBAAmB,OAAO;CAE1C,OAAO,CADW,iBAAiB,cACrB,CAAC,CAAC,OAAO,QAAQ;EAC9B,IAAI;GACH,MAAM,OAAO,KAAK,QAAQ,UAAU,gBAAgB,KAAK,gBAAgB;GAEzE,OAAO,CADM,aAAa,IACf,CAAC,CAAC,SAAS,cAAc;EACrC,QAAQ;GACP,OAAO;EACR;CACD,CAAC;AACF;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C,GACA,MACA,GACD,CACD;AACD;AAEA,eAAe,YAAY,MAA0D;CACpF,IAAI;EACH,MAAM,MAAM,KAAK,MAAM,MAAMA,SAAG,SAAS,KAAK,KAAK,MAAM,cAAc,GAAG,OAAO,CAAC;EAGlF,OAAO;GAAE,MAFI,OAAO,IAAI,SAAS,WAAW,IAAI,KAAK,QAAQ,aAAa,EAAE,IAAI;GAEjE,SADC,OAAO,IAAI,YAAY,WAAW,IAAI,UAAU;EACzC;CACxB,QAAQ;EACP,OAAO;GAAE,MAAM;GAAc,SAAS;EAAQ;CAC/C;AACD;;;;;;;;;;;;;;;;;;ACjCA,SAAwB,SAAS,UAA2B,CAAC,GAAY;CACxE,OAAO;EACN,MAAM,SAAe,YAAY,MAAM,OAAO;EAC9C,YAAY,SAAe,UAAU,MAAM,OAAO;EAClD,QAAQ,SAAS,SAAe,MAAM,IAAI;CAC3C;AACD;AAEA,SAAS,MAAM,MAAkB;CAChC,MAAM,YAAY,KAAK,KAAK,KAAK,MAAM,QAAQ,UAAU;CACzD,IAAI,CAAC,WAAW,KAAK,KAAK,WAAW,UAAU,CAAC,GAC/C,MAAM,IAAI,MACT,mFACD;CAGD,MAAM,QAAQ,MAAM,eAAe,KAAK,IAAI,GAAG,CAAC,SAAS,GAAG;EAC3D,KAAK,KAAK;EACV,OAAO;CACR,CAAC;CAED,MAAM,GAAG,UAAU,UAAiC;EACnD,IAAI,MAAM,SAAS,UAAU;GAC5B,QAAQ,MACP,gFACD;GACA,QAAQ,KAAK,CAAC;EACf;EACA,MAAM;CACP,CAAC;CAED,MAAM,GAAG,SAAS,SAAS,QAAQ,KAAK,QAAQ,CAAC,CAAC;AACnD"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["fs"],"sources":["../src/electronBinary.ts","../src/sourceMode.ts","../src/electronDev.ts","../src/postbuild.ts","../src/adapter.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\nimport path from \"node:path\";\n\n/**\n * Resolves the Electron executable to run, preferring the copy installed in\n * the site's node_modules (`require(\"electron\")` resolves to the binary path)\n * and falling back to `electron` on PATH. The package may not be installed --\n * e.g. a CI build that only builds the site -- in which case launching is\n * skipped and callers report how to install it.\n */\nexport default function electronBinary(siteRoot: string): string {\n\ttry {\n\t\tconst require = createRequire(path.join(siteRoot, \"package.json\"));\n\t\tconst resolved: unknown = require(\"electron\");\n\t\treturn typeof resolved === \"string\" ? resolved : \"electron\";\n\t} catch {\n\t\treturn \"electron\";\n\t}\n}\n","import { realpathSync } from \"node:fs\";\nimport path from \"node:path\";\n\n// Mirrors the entry-path logic in @torpor/build's `utils/entryPaths.ts` (kept\n// local so the adapter stays compatible with older @torpor/build installs).\n\n/**\n * Whether the framework is symlinked into the app from outside node_modules\n * (workspace links and `link:` overrides), meaning tb runs it from source via\n * the `development` export conditions. Registry installs always run dist.\n */\nexport default function detectSourceMode(siteRoot: string): boolean {\n\tif (process.env.TORPOR_SOURCE_DEV) return true;\n\tconst packages = [\"@torpor/build\", \"@torpor/view\"];\n\treturn packages.every((pkg) => {\n\t\ttry {\n\t\t\tconst dist = path.resolve(siteRoot, \"node_modules\", pkg, \"dist/index.mjs\");\n\t\t\tconst real = realpathSync(dist);\n\t\t\treturn !real.includes(\"node_modules\");\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t});\n}\n","import { type Site } from \"@torpor/build\";\nimport { nodeDev } from \"@torpor/build/node-dev\";\nimport { spawn, type ChildProcess } from \"node:child_process\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { Plugin, ViteDevServer } from \"vite\";\nimport electronBinary from \"./electronBinary\";\nimport detectSourceMode from \"./sourceMode\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * Dev support: the framework's default Node dev runtime (which serves SSR off\n * the Vite dev server) plus a launcher that opens an Electron window pointed at\n * it. Without `nodeDev`, providing `Adapter.dev` would replace the Node\n * runtime entirely, which Electron doesn't need -- it runs on Node.\n */\nexport default function electronDev(site: Site, options: ElectronOptions): Plugin[] {\n\treturn [nodeDev(site, detectSourceMode(site.root)), electronLauncher(site, options)];\n}\n\nfunction electronLauncher(site: Site, options: ElectronOptions): Plugin {\n\tlet child: ChildProcess | undefined;\n\tlet quitting = false;\n\n\tconst stopChild = (): void => {\n\t\tchild?.kill();\n\t\tchild = undefined;\n\t};\n\n\treturn {\n\t\tname: \"torpor-electron-dev\",\n\t\tconfigureServer(server: ViteDevServer) {\n\t\t\tif (options.dev === false || process.env.TORPOR_ELECTRON === \"0\") return;\n\n\t\t\tconst start = (): void => {\n\t\t\t\tconst url = devServerUrl(server);\n\t\t\t\tif (!url) return;\n\t\t\t\tstopChild();\n\t\t\t\tquitting = false;\n\n\t\t\t\tconst proc = launch(site, url, options);\n\t\t\t\tchild = proc;\n\n\t\t\t\t// When the user quits the app (Cmd+Q, or closing the window),\n\t\t\t\t// stop the dev server too, so `tb --dev` exits with the desktop\n\t\t\t\t// app. A failed launch emits \"error\", not \"exit\", so browser-only\n\t\t\t\t// dev is unaffected\n\t\t\t\tproc.once(\"exit\", () => {\n\t\t\t\t\tif (quitting) return;\n\t\t\t\t\tquitting = true;\n\t\t\t\t\tvoid server.close();\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tconst httpServer = server.httpServer;\n\t\t\tif (httpServer?.listening) {\n\t\t\t\tstart();\n\t\t\t} else {\n\t\t\t\thttpServer?.once(\"listening\", start);\n\t\t\t}\n\n\t\t\t// Vite is shutting down (Ctrl+C, a restart, or close() from the\n\t\t\t// handler above): kill the window without re-closing the server\n\t\t\thttpServer?.once(\"close\", () => {\n\t\t\t\tquitting = true;\n\t\t\t\tstopChild();\n\t\t\t});\n\n\t\t\tprocess.once(\"exit\", stopChild);\n\t\t},\n\t};\n}\n\nfunction launch(site: Site, url: string, options: ElectronOptions): ChildProcess {\n\tconst adapterFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst entry = path.join(adapterFolder, \"dev-main.mjs\");\n\n\tconst config = {\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\n\tconst child = spawn(electronBinary(site.root), [entry], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t\tenv: {\n\t\t\t...process.env,\n\t\t\tTORPOR_ELECTRON_URL: url,\n\t\t\tTORPOR_ELECTRON_CONFIG: JSON.stringify(config),\n\t\t},\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.warn(\n\t\t\t\t\"[torpor] Electron is not installed, so dev is running in the browser only. \" +\n\t\t\t\t\t\"Install it with `npm install -D electron` to open a desktop window.\",\n\t\t\t);\n\t\t} else {\n\t\t\tconsole.warn(`[torpor] Could not launch Electron: ${error.message}`);\n\t\t}\n\t});\n\n\treturn child;\n}\n\nfunction devServerUrl(server: ViteDevServer): string | undefined {\n\tconst local = server.resolvedUrls?.local?.[0];\n\tif (local) return local;\n\n\tconst address = server.httpServer?.address();\n\tif (address && typeof address === \"object\") {\n\t\tconst protocol = server.config.server.https ? \"https\" : \"http\";\n\t\tconst host =\n\t\t\taddress.address === \"::\" || address.address === \"0.0.0.0\" ? \"localhost\" : address.address;\n\t\treturn `${protocol}://${host}:${address.port}`;\n\t}\n\n\treturn undefined;\n}\n","import type { Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { ElectronOptions } from \"./types\";\n\n// The Electron main and preload scripts, copied verbatim from the adapter's\n// dist into the site's dist/electron output\nconst RUNTIME_FILES = [\"main.mjs\", \"preload.cjs\"];\n\nexport default async function postbuild(site: Site, options: ElectronOptions): Promise<void> {\n\tconst sourceFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst outputFolder = path.join(site.outputFolder, \"electron\");\n\n\tawait fs.mkdir(outputFolder, { recursive: true });\n\tfor (const file of RUNTIME_FILES) {\n\t\tawait fs.copyFile(path.join(sourceFolder, file), path.join(outputFolder, file));\n\t}\n\n\tconst config = {\n\t\thost: options.host ?? \"127.0.0.1\",\n\t\tport: options.port ?? 0,\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\tawait fs.writeFile(path.join(outputFolder, \"electron.json\"), JSON.stringify(config, null, \"\\t\"));\n\n\t// A minimal Electron app manifest. Run it with `electron dist/electron`\n\t// (see the adapter README for packaging with electron-builder/forge)\n\tconst { name, version } = await sitePackage(site.root);\n\tawait fs.writeFile(\n\t\tpath.join(outputFolder, \"package.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tname: `${name}-electron`,\n\t\t\t\tproductName: name,\n\t\t\t\tversion,\n\t\t\t\tprivate: true,\n\t\t\t\ttype: \"module\",\n\t\t\t\tmain: \"main.mjs\",\n\t\t\t\tscripts: { start: \"electron .\" },\n\t\t\t},\n\t\t\tnull,\n\t\t\t\"\\t\",\n\t\t),\n\t);\n}\n\nasync function sitePackage(root: string): Promise<{ name: string; version: string }> {\n\ttry {\n\t\tconst pkg = JSON.parse(await fs.readFile(path.join(root, \"package.json\"), \"utf-8\"));\n\t\tconst name = typeof pkg.name === \"string\" ? pkg.name.replace(/^@[^/]+\\//, \"\") : \"torpor-app\";\n\t\tconst version = typeof pkg.version === \"string\" ? pkg.version : \"1.0.0\";\n\t\treturn { name, version };\n\t} catch {\n\t\treturn { name: \"torpor-app\", version: \"1.0.0\" };\n\t}\n}\n","import { type Adapter, type Site } from \"@torpor/build\";\nimport { spawn } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport electronBinary from \"./electronBinary\";\nimport electronDev from \"./electronDev\";\nimport postbuild from \"./postbuild\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * A @torpor/build adapter for desktop apps.\n *\n * Builds emit an Electron app into `dist/electron` (a main process that serves\n * the site over a loopback HTTP server, a context-isolated preload, and a\n * manifest); preview launches it, and dev opens a window against the Vite dev\n * server. Requests run in the Electron main process on Node, so cookies,\n * sessions and redirects behave exactly as on a normal Node server.\n *\n * ```ts\n * import { electron } from \"@torpor/adapter-electron\";\n *\n * site.adapter = electron();\n * ```\n */\nexport default function electron(options: ElectronOptions = {}): Adapter {\n\treturn {\n\t\tdev: (site: Site) => electronDev(site, options),\n\t\tpostbuild: (site: Site) => postbuild(site, options),\n\t\tserve: (_server, site: Site) => serve(site),\n\t};\n}\n\nfunction serve(site: Site): void {\n\tconst appFolder = path.join(site.outputFolder, \"electron\");\n\tif (!existsSync(path.join(appFolder, \"main.mjs\"))) {\n\t\tthrow new Error(\n\t\t\t\"Electron output not found. Run a build first (dist/electron/main.mjs is missing).\",\n\t\t);\n\t}\n\n\tconst child = spawn(electronBinary(site.root), [appFolder], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t\"[torpor] Electron is not installed. Install it with `npm install -D electron`.\",\n\t\t\t);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tthrow error;\n\t});\n\n\tchild.on(\"exit\", (code) => process.exit(code ?? 0));\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,SAAwB,eAAe,UAA0B;CAChE,IAAI;EAEH,MAAM,WADU,cAAc,KAAK,KAAK,UAAU,cAAc,CAChC,CAAC,CAAC,UAAU;EAC5C,OAAO,OAAO,aAAa,WAAW,WAAW;CAClD,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;;ACPA,SAAwB,iBAAiB,UAA2B;CACnE,IAAI,QAAQ,IAAI,mBAAmB,OAAO;CAE1C,OAAO,CADW,iBAAiB,cACrB,CAAC,CAAC,OAAO,QAAQ;EAC9B,IAAI;GACH,MAAM,OAAO,KAAK,QAAQ,UAAU,gBAAgB,KAAK,gBAAgB;GAEzE,OAAO,CADM,aAAa,IACf,CAAC,CAAC,SAAS,cAAc;EACrC,QAAQ;GACP,OAAO;EACR;CACD,CAAC;AACF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,MACA,GACD,CACD;AACD;AAEA,eAAe,YAAY,MAA0D;CACpF,IAAI;EACH,MAAM,MAAM,KAAK,MAAM,MAAMA,SAAG,SAAS,KAAK,KAAK,MAAM,cAAc,GAAG,OAAO,CAAC;EAGlF,OAAO;GAAE,MAFI,OAAO,IAAI,SAAS,WAAW,IAAI,KAAK,QAAQ,aAAa,EAAE,IAAI;GAEjE,SADC,OAAO,IAAI,YAAY,WAAW,IAAI,UAAU;EACzC;CACxB,QAAQ;EACP,OAAO;GAAE,MAAM;GAAc,SAAS;EAAQ;CAC/C;AACD;;;;;;;;;;;;;;;;;;ACjCA,SAAwB,SAAS,UAA2B,CAAC,GAAY;CACxE,OAAO;EACN,MAAM,SAAe,YAAY,MAAM,OAAO;EAC9C,YAAY,SAAe,UAAU,MAAM,OAAO;EAClD,QAAQ,SAAS,SAAe,MAAM,IAAI;CAC3C;AACD;AAEA,SAAS,MAAM,MAAkB;CAChC,MAAM,YAAY,KAAK,KAAK,KAAK,cAAc,UAAU;CACzD,IAAI,CAAC,WAAW,KAAK,KAAK,WAAW,UAAU,CAAC,GAC/C,MAAM,IAAI,MACT,mFACD;CAGD,MAAM,QAAQ,MAAM,eAAe,KAAK,IAAI,GAAG,CAAC,SAAS,GAAG;EAC3D,KAAK,KAAK;EACV,OAAO;CACR,CAAC;CAED,MAAM,GAAG,UAAU,UAAiC;EACnD,IAAI,MAAM,SAAS,UAAU;GAC5B,QAAQ,MACP,gFACD;GACA,QAAQ,KAAK,CAAC;EACf;EACA,MAAM;CACP,CAAC;CAED,MAAM,GAAG,SAAS,SAAS,QAAQ,KAAK,QAAQ,CAAC,CAAC;AACnD"}
|
package/dist/main.mjs
CHANGED
|
@@ -9,7 +9,9 @@ import { fileURLToPath } from "node:url";
|
|
|
9
9
|
import { startServer } from "@torpor/adapter-electron/runtime";
|
|
10
10
|
|
|
11
11
|
const dir = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
-
|
|
12
|
+
// This file is copied to <output>/electron, so the build output folder is its
|
|
13
|
+
// parent (the layout no longer assumes the folder is named "dist")
|
|
14
|
+
const output = path.resolve(dir, "..");
|
|
13
15
|
const config = JSON.parse(readFileSync(path.join(dir, "electron.json"), "utf-8"));
|
|
14
16
|
const partition = config.partition ?? "persist:torpor";
|
|
15
17
|
|
|
@@ -45,7 +47,7 @@ async function createWindow() {
|
|
|
45
47
|
app
|
|
46
48
|
.whenReady()
|
|
47
49
|
.then(async () => {
|
|
48
|
-
server = await startServer(
|
|
50
|
+
server = await startServer(output, { host: config.host, port: config.port });
|
|
49
51
|
origin = server.url;
|
|
50
52
|
await createWindow();
|
|
51
53
|
|
package/dist/runtime.d.mts
CHANGED
|
@@ -7,18 +7,24 @@ import { i as StartServerOptions, r as RunningServer } from "./types-Ce9F9DiP.mj
|
|
|
7
7
|
*/
|
|
8
8
|
export declare function loadEnv(root: string): void;
|
|
9
9
|
/**
|
|
10
|
-
* Builds a Fetch API handler for a built site: it serves everything under
|
|
11
|
-
* `
|
|
12
|
-
* `serverEntry.load`. Replaces `runPreview`'s Server setup, which
|
|
13
|
-
* exported from @torpor/build.
|
|
10
|
+
* Builds a Fetch API handler for a built site: it serves everything under the
|
|
11
|
+
* output folder's `client/` statically and routes the rest through its
|
|
12
|
+
* `server/`'s `serverEntry.load`. Replaces `runPreview`'s Server setup, which
|
|
13
|
+
* isn't exported from @torpor/build.
|
|
14
|
+
*
|
|
15
|
+
* @param outputFolder The site's build output folder (the parent of
|
|
16
|
+
* `client/`, `server/` and `electron/`)
|
|
14
17
|
*/
|
|
15
|
-
export declare function createRequestHandler(
|
|
18
|
+
export declare function createRequestHandler(outputFolder: string): Promise<(request: Request) => Promise<Response>>;
|
|
16
19
|
/**
|
|
17
20
|
* Starts the loopback HTTP server for a built site. Requests are handled in
|
|
18
21
|
* the Electron main process (Node), so cookies, sessions and redirects behave
|
|
19
22
|
* exactly as they do on a normal Node server -- Chromium's session cookie
|
|
20
23
|
* store handles persistence across launches.
|
|
24
|
+
*
|
|
25
|
+
* @param outputFolder The site's build output folder (see
|
|
26
|
+
* `createRequestHandler`). `.env` is loaded from its parent.
|
|
21
27
|
*/
|
|
22
|
-
export declare function startServer(
|
|
28
|
+
export declare function startServer(outputFolder: string, options?: StartServerOptions): Promise<RunningServer>;
|
|
23
29
|
//#endregion
|
|
24
30
|
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;wBAcgB,QAAQ
|
|
1
|
+
{"version":3,"file":"runtime.d.mts","names":[],"sources":["../src/runtime.ts"],"mappings":";;;;;;;wBAcgB,QAAQ;;;;;;;;;;wBAqBF,qBACrB,uBACE,SAAS,SAAS,YAAY,QAAQ;;;;;;;;;;wBAqCnB,YACrB,sBACA,UAAS,qBACP,QAAQ"}
|
package/dist/runtime.mjs
CHANGED
|
@@ -84,14 +84,17 @@ function loadEnv(root) {
|
|
|
84
84
|
} catch {}
|
|
85
85
|
}
|
|
86
86
|
/**
|
|
87
|
-
* Builds a Fetch API handler for a built site: it serves everything under
|
|
88
|
-
* `
|
|
89
|
-
* `serverEntry.load`. Replaces `runPreview`'s Server setup, which
|
|
90
|
-
* exported from @torpor/build.
|
|
87
|
+
* Builds a Fetch API handler for a built site: it serves everything under the
|
|
88
|
+
* output folder's `client/` statically and routes the rest through its
|
|
89
|
+
* `server/`'s `serverEntry.load`. Replaces `runPreview`'s Server setup, which
|
|
90
|
+
* isn't exported from @torpor/build.
|
|
91
|
+
*
|
|
92
|
+
* @param outputFolder The site's build output folder (the parent of
|
|
93
|
+
* `client/`, `server/` and `electron/`)
|
|
91
94
|
*/
|
|
92
|
-
async function createRequestHandler(
|
|
93
|
-
const clientFolder = path.join(
|
|
94
|
-
const serverFolder = path.join(
|
|
95
|
+
async function createRequestHandler(outputFolder) {
|
|
96
|
+
const clientFolder = path.join(outputFolder, "client");
|
|
97
|
+
const serverFolder = path.join(outputFolder, "server");
|
|
95
98
|
const template = await loadTemplate(clientFolder);
|
|
96
99
|
const serverEntry = pathToFileURL(path.join(serverFolder, "serverEntry.js")).href;
|
|
97
100
|
const server = new Server();
|
|
@@ -112,11 +115,14 @@ async function createRequestHandler(root) {
|
|
|
112
115
|
* the Electron main process (Node), so cookies, sessions and redirects behave
|
|
113
116
|
* exactly as they do on a normal Node server -- Chromium's session cookie
|
|
114
117
|
* store handles persistence across launches.
|
|
118
|
+
*
|
|
119
|
+
* @param outputFolder The site's build output folder (see
|
|
120
|
+
* `createRequestHandler`). `.env` is loaded from its parent.
|
|
115
121
|
*/
|
|
116
|
-
async function startServer(
|
|
117
|
-
loadEnv(
|
|
122
|
+
async function startServer(outputFolder, options = {}) {
|
|
123
|
+
loadEnv(path.dirname(outputFolder));
|
|
118
124
|
globalThis.adapter = { env: process.env };
|
|
119
|
-
const handler = await createRequestHandler(
|
|
125
|
+
const handler = await createRequestHandler(outputFolder);
|
|
120
126
|
const server = createNodeServer(handler);
|
|
121
127
|
const host = options.host ?? "127.0.0.1";
|
|
122
128
|
const port = options.port ?? 0;
|
package/dist/runtime.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.mjs","names":["fs"],"sources":["../src/contentType.ts","../src/prepareTemplate.ts","../src/runtime.ts"],"sourcesContent":["// Kept local so the adapter doesn't depend on @torpor/build internals.\n// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types\n\nexport default function contentType(extension: string): string {\n\treturn types[extension] || \"application/octet-stream\";\n}\n\nconst types: Record<string, string> = {\n\t\".aac\": \"audio/aac\",\n\t\".apng\": \"image/apng\",\n\t\".avif\": \"image/avif\",\n\t\".bin\": \"application/octet-stream\",\n\t\".bmp\": \"image/bmp\",\n\t\".css\": \"text/css\",\n\t\".csv\": \"text/csv\",\n\t\".eot\": \"application/vnd.ms-fontobject\",\n\t\".gif\": \"image/gif\",\n\t\".htm\": \"text/html\",\n\t\".html\": \"text/html\",\n\t\".ico\": \"image/vnd.microsoft.icon\",\n\t\".jpeg\": \"image/jpeg\",\n\t\".jpg\": \"image/jpeg\",\n\t\".js\": \"text/javascript\",\n\t\".json\": \"application/json\",\n\t\".jsonld\": \"application/ld+json\",\n\t\".mjs\": \"text/javascript\",\n\t\".mp3\": \"audio/mpeg\",\n\t\".mp4\": \"video/mp4\",\n\t\".ogg\": \"audio/ogg\",\n\t\".ogv\": \"video/ogg\",\n\t\".opus\": \"audio/ogg\",\n\t\".otf\": \"font/otf\",\n\t\".pdf\": \"application/pdf\",\n\t\".png\": \"image/png\",\n\t\".svg\": \"image/svg+xml\",\n\t\".tar\": \"application/x-tar\",\n\t\".txt\": \"text/plain\",\n\t\".ttf\": \"font/ttf\",\n\t\".wasm\": \"application/wasm\",\n\t\".wav\": \"audio/wav\",\n\t\".webm\": \"video/webm\",\n\t\".webp\": \"image/webp\",\n\t\".woff\": \"font/woff\",\n\t\".woff2\": \"font/woff2\",\n\t\".xml\": \"application/xml\",\n\t\".zip\": \"application/zip\",\n};\n","// Mirrors @torpor/build's `run/prepareTemplate.ts` (kept local so the adapter\n// stays compatible with older @torpor/build installs).\n\nexport default function prepareTemplate(template: string, clientScript: string): string {\n\tlet result = template;\n\n\t// Put %COMPONENT_HEAD% before </head>\n\t// This is where styles will go\n\tconst headStart = result.indexOf(\"</head>\");\n\tif (headStart === -1) {\n\t\tthrow new Error(`Couldn't find <head> end tag`);\n\t}\n\tresult = result.substring(0, headStart) + \"%COMPONENT_HEAD%\" + result.substring(headStart);\n\n\t// Put %COMPONENT_BODY% inside <div id=\"app\"></div>\n\t// This is where the component's HTML will go\n\tconst divMatchStart = regexIndexOf(result, /<div\\s+id=(\"app\"|'app'|app)[\\s>]/);\n\tif (divMatchStart === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tconst bodyStart = result.indexOf(\">\", divMatchStart) + 1;\n\tconst bodyEnd = result.indexOf(\"</div>\", bodyStart);\n\tif (bodyEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tresult = result.substring(0, bodyStart) + \"%COMPONENT_BODY%\" + result.substring(bodyEnd);\n\n\t// Add the clientEntry script at the very end, for hydrating and navigating\n\tresult += `<script type=\"module\" src=\"${clientScript}\"></script>`;\n\n\treturn result;\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number): number {\n\tconst indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import { createNodeServer } from \"@torpor/adapter-node\";\nimport { Server } from \"@torpor/build/server\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport contentType from \"./contentType\";\nimport prepareTemplate from \"./prepareTemplate\";\nimport type { RunningServer, StartServerOptions } from \"./types\";\n\n/**\n * Loads a `.env` file from the site root into `process.env`, when the runtime's\n * Node version supports `process.loadEnvFile` (20.12+). Mirrors the CLI, which\n * loads `.env` for dev and preview.\n */\nexport function loadEnv(root: string): void {\n\tconst file = path.join(root, \".env\");\n\tif (!existsSync(file)) return;\n\tconst loader = (process as { loadEnvFile?: (file: string) => void }).loadEnvFile;\n\tif (typeof loader !== \"function\") return;\n\ttry {\n\t\tloader.call(process, file);\n\t} catch {\n\t\t// A malformed .env shouldn't take the app down; the CLI reports it in dev\n\t}\n}\n\n/**\n * Builds a Fetch API handler for a built site: it serves everything under\n * `dist/client` statically and routes the rest through `dist/server`'s\n * `serverEntry.load`. Replaces `runPreview`'s Server setup, which isn't\n * exported from @torpor/build.\n */\nexport async function createRequestHandler(\n\troot: string,\n): Promise<(request: Request) => Promise<Response>> {\n\tconst clientFolder = path.join(root, \"dist\", \"client\");\n\tconst serverFolder = path.join(root, \"dist\", \"server\");\n\tconst template = await loadTemplate(clientFolder);\n\tconst serverEntry = pathToFileURL(path.join(serverFolder, \"serverEntry.js\")).href;\n\n\tconst server = new Server();\n\n\t// Serve anything that exists under dist/client (assets, favicon.ico,\n\t// robots.txt, public files, ...) before falling through to the app\n\tserver.use({\n\t\tenter: async (ev) => serveStatic(clientFolder, ev.request),\n\t});\n\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Node caches the module, so this is only read once\n\t\t\tconst { load } = await import(serverEntry);\n\t\t\treturn await load(ev, template);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[torpor] Unhandled error while rendering:\", error);\n\t\t\treturn new Response(null, { status: 500 });\n\t\t}\n\t});\n\n\treturn (request) => server.fetch(request);\n}\n\n/**\n * Starts the loopback HTTP server for a built site. Requests are handled in\n * the Electron main process (Node), so cookies, sessions and redirects behave\n * exactly as they do on a normal Node server -- Chromium's session cookie\n * store handles persistence across launches.\n */\nexport async function startServer(\n\troot: string,\n\toptions: StartServerOptions = {},\n): Promise<RunningServer> {\n\tloadEnv(root);\n\n\t// Adapters may have added the `adapter` property to globalThis; torpor's\n\t// env() reads it (see @torpor/build/env)\n\t(globalThis as { adapter?: { env: NodeJS.ProcessEnv } }).adapter = { env: process.env };\n\n\tconst handler = await createRequestHandler(root);\n\tconst server = createNodeServer(handler);\n\n\tconst host = options.host ?? \"127.0.0.1\";\n\tconst port = options.port ?? 0;\n\n\tawait new Promise<void>((resolve, reject) => {\n\t\tserver.once(\"error\", reject);\n\t\tserver.listen(port, host, () => resolve());\n\t});\n\n\tconst address = server.address();\n\tconst boundPort = typeof address === \"object\" && address ? address.port : port;\n\n\treturn {\n\t\turl: `http://${host}:${boundPort}`,\n\t\thost,\n\t\tport: boundPort,\n\t\tclose: () =>\n\t\t\tnew Promise<void>((resolve, reject) => {\n\t\t\t\t// Force-destroy any remaining sockets (keep-alive, SSE from\n\t\t\t\t// `$stream`, etc). `close()` alone waits for connections to end,\n\t\t\t\t// so a long-lived one would keep it from calling back and hang\n\t\t\t\t// the app's shutdown\n\t\t\t\tserver.close((error) => (error ? reject(error) : resolve()));\n\t\t\t\tserver.closeAllConnections();\n\t\t\t}),\n\t};\n}\n\nasync function loadTemplate(clientFolder: string): Promise<string | undefined> {\n\t// An endpoints-only site has no site.html, and no template is needed\n\tconst siteHtml = path.join(clientFolder, \"site.html\");\n\tif (!existsSync(siteHtml)) return undefined;\n\n\tconst assets = path.join(clientFolder, \"assets\");\n\tconst clientScript = (await fs.readdir(assets)).find((file) => file.startsWith(\"clientEntry-\"));\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry not found in dist/client/assets\");\n\t}\n\n\treturn prepareTemplate(await fs.readFile(siteHtml, \"utf-8\"), `/assets/${clientScript}`);\n}\n\nasync function serveStatic(clientFolder: string, request: Request): Promise<Response | undefined> {\n\tif (request.method !== \"GET\" && request.method !== \"HEAD\") return undefined;\n\n\tlet pathname: string;\n\ttry {\n\t\tpathname = decodeURIComponent(new URL(request.url).pathname);\n\t} catch {\n\t\treturn undefined;\n\t}\n\n\t// Resolve within clientFolder, rejecting any path that escapes it\n\tconst root = path.resolve(clientFolder);\n\tconst file = path.resolve(root, \".\" + pathname);\n\tif (file !== root && !file.startsWith(root + path.sep)) return undefined;\n\n\tlet stats: Awaited<ReturnType<typeof fs.stat>>;\n\ttry {\n\t\tstats = await fs.stat(file);\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!stats.isFile()) return undefined;\n\n\tconst headers = { \"Content-Type\": contentType(path.extname(file)) };\n\tif (request.method === \"HEAD\") {\n\t\treturn new Response(null, { status: 200, headers });\n\t}\n\treturn new Response(await fs.readFile(file), { status: 200, headers });\n}\n"],"mappings":";;;;;;AAGA,SAAwB,YAAY,WAA2B;CAC9D,OAAO,MAAM,cAAc;AAC5B;AAEA,MAAM,QAAgC;CACrC,QAAQ;CACR,SAAS;CACT,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,SAAS;CACT,QAAQ;CACR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tE"}
|
|
1
|
+
{"version":3,"file":"runtime.mjs","names":["fs"],"sources":["../src/contentType.ts","../src/prepareTemplate.ts","../src/runtime.ts"],"sourcesContent":["// Kept local so the adapter doesn't depend on @torpor/build internals.\n// From https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/MIME_types/Common_types\n\nexport default function contentType(extension: string): string {\n\treturn types[extension] || \"application/octet-stream\";\n}\n\nconst types: Record<string, string> = {\n\t\".aac\": \"audio/aac\",\n\t\".apng\": \"image/apng\",\n\t\".avif\": \"image/avif\",\n\t\".bin\": \"application/octet-stream\",\n\t\".bmp\": \"image/bmp\",\n\t\".css\": \"text/css\",\n\t\".csv\": \"text/csv\",\n\t\".eot\": \"application/vnd.ms-fontobject\",\n\t\".gif\": \"image/gif\",\n\t\".htm\": \"text/html\",\n\t\".html\": \"text/html\",\n\t\".ico\": \"image/vnd.microsoft.icon\",\n\t\".jpeg\": \"image/jpeg\",\n\t\".jpg\": \"image/jpeg\",\n\t\".js\": \"text/javascript\",\n\t\".json\": \"application/json\",\n\t\".jsonld\": \"application/ld+json\",\n\t\".mjs\": \"text/javascript\",\n\t\".mp3\": \"audio/mpeg\",\n\t\".mp4\": \"video/mp4\",\n\t\".ogg\": \"audio/ogg\",\n\t\".ogv\": \"video/ogg\",\n\t\".opus\": \"audio/ogg\",\n\t\".otf\": \"font/otf\",\n\t\".pdf\": \"application/pdf\",\n\t\".png\": \"image/png\",\n\t\".svg\": \"image/svg+xml\",\n\t\".tar\": \"application/x-tar\",\n\t\".txt\": \"text/plain\",\n\t\".ttf\": \"font/ttf\",\n\t\".wasm\": \"application/wasm\",\n\t\".wav\": \"audio/wav\",\n\t\".webm\": \"video/webm\",\n\t\".webp\": \"image/webp\",\n\t\".woff\": \"font/woff\",\n\t\".woff2\": \"font/woff2\",\n\t\".xml\": \"application/xml\",\n\t\".zip\": \"application/zip\",\n};\n","// Mirrors @torpor/build's `run/prepareTemplate.ts` (kept local so the adapter\n// stays compatible with older @torpor/build installs).\n\nexport default function prepareTemplate(template: string, clientScript: string): string {\n\tlet result = template;\n\n\t// Put %COMPONENT_HEAD% before </head>\n\t// This is where styles will go\n\tconst headStart = result.indexOf(\"</head>\");\n\tif (headStart === -1) {\n\t\tthrow new Error(`Couldn't find <head> end tag`);\n\t}\n\tresult = result.substring(0, headStart) + \"%COMPONENT_HEAD%\" + result.substring(headStart);\n\n\t// Put %COMPONENT_BODY% inside <div id=\"app\"></div>\n\t// This is where the component's HTML will go\n\tconst divMatchStart = regexIndexOf(result, /<div\\s+id=(\"app\"|'app'|app)[\\s>]/);\n\tif (divMatchStart === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tconst bodyStart = result.indexOf(\">\", divMatchStart) + 1;\n\tconst bodyEnd = result.indexOf(\"</div>\", bodyStart);\n\tif (bodyEnd === -1) {\n\t\tthrow new Error(`Couldn't find <div id=\"app\"></div>`);\n\t}\n\tresult = result.substring(0, bodyStart) + \"%COMPONENT_BODY%\" + result.substring(bodyEnd);\n\n\t// Add the clientEntry script at the very end, for hydrating and navigating\n\tresult += `<script type=\"module\" src=\"${clientScript}\"></script>`;\n\n\treturn result;\n}\n\n// From https://stackoverflow.com/a/274094\nfunction regexIndexOf(string: string, regex: RegExp, position?: number): number {\n\tconst indexOf = string.substring(position || 0).search(regex);\n\treturn indexOf >= 0 ? indexOf + (position || 0) : indexOf;\n}\n","import { createNodeServer } from \"@torpor/adapter-node\";\nimport { Server } from \"@torpor/build/server\";\nimport { existsSync, promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { pathToFileURL } from \"node:url\";\nimport contentType from \"./contentType\";\nimport prepareTemplate from \"./prepareTemplate\";\nimport type { RunningServer, StartServerOptions } from \"./types\";\n\n/**\n * Loads a `.env` file from the site root into `process.env`, when the runtime's\n * Node version supports `process.loadEnvFile` (20.12+). Mirrors the CLI, which\n * loads `.env` for dev and preview.\n */\nexport function loadEnv(root: string): void {\n\tconst file = path.join(root, \".env\");\n\tif (!existsSync(file)) return;\n\tconst loader = (process as { loadEnvFile?: (file: string) => void }).loadEnvFile;\n\tif (typeof loader !== \"function\") return;\n\ttry {\n\t\tloader.call(process, file);\n\t} catch {\n\t\t// A malformed .env shouldn't take the app down; the CLI reports it in dev\n\t}\n}\n\n/**\n * Builds a Fetch API handler for a built site: it serves everything under the\n * output folder's `client/` statically and routes the rest through its\n * `server/`'s `serverEntry.load`. Replaces `runPreview`'s Server setup, which\n * isn't exported from @torpor/build.\n *\n * @param outputFolder The site's build output folder (the parent of\n * `client/`, `server/` and `electron/`)\n */\nexport async function createRequestHandler(\n\toutputFolder: string,\n): Promise<(request: Request) => Promise<Response>> {\n\tconst clientFolder = path.join(outputFolder, \"client\");\n\tconst serverFolder = path.join(outputFolder, \"server\");\n\tconst template = await loadTemplate(clientFolder);\n\tconst serverEntry = pathToFileURL(path.join(serverFolder, \"serverEntry.js\")).href;\n\n\tconst server = new Server();\n\n\t// Serve anything that exists under dist/client (assets, favicon.ico,\n\t// robots.txt, public files, ...) before falling through to the app\n\tserver.use({\n\t\tenter: async (ev) => serveStatic(clientFolder, ev.request),\n\t});\n\n\tserver.add(\"*\", async (ev) => {\n\t\ttry {\n\t\t\t// Node caches the module, so this is only read once\n\t\t\tconst { load } = await import(serverEntry);\n\t\t\treturn await load(ev, template);\n\t\t} catch (error) {\n\t\t\tconsole.error(\"[torpor] Unhandled error while rendering:\", error);\n\t\t\treturn new Response(null, { status: 500 });\n\t\t}\n\t});\n\n\treturn (request) => server.fetch(request);\n}\n\n/**\n * Starts the loopback HTTP server for a built site. Requests are handled in\n * the Electron main process (Node), so cookies, sessions and redirects behave\n * exactly as they do on a normal Node server -- Chromium's session cookie\n * store handles persistence across launches.\n *\n * @param outputFolder The site's build output folder (see\n * `createRequestHandler`). `.env` is loaded from its parent.\n */\nexport async function startServer(\n\toutputFolder: string,\n\toptions: StartServerOptions = {},\n): Promise<RunningServer> {\n\tloadEnv(path.dirname(outputFolder));\n\n\t// Adapters may have added the `adapter` property to globalThis; torpor's\n\t// env() reads it (see @torpor/build/env)\n\t(globalThis as { adapter?: { env: NodeJS.ProcessEnv } }).adapter = { env: process.env };\n\n\tconst handler = await createRequestHandler(outputFolder);\n\tconst server = createNodeServer(handler);\n\n\tconst host = options.host ?? \"127.0.0.1\";\n\tconst port = options.port ?? 0;\n\n\tawait new Promise<void>((resolve, reject) => {\n\t\tserver.once(\"error\", reject);\n\t\tserver.listen(port, host, () => resolve());\n\t});\n\n\tconst address = server.address();\n\tconst boundPort = typeof address === \"object\" && address ? address.port : port;\n\n\treturn {\n\t\turl: `http://${host}:${boundPort}`,\n\t\thost,\n\t\tport: boundPort,\n\t\tclose: () =>\n\t\t\tnew Promise<void>((resolve, reject) => {\n\t\t\t\t// Force-destroy any remaining sockets (keep-alive, SSE from\n\t\t\t\t// `$stream`, etc). `close()` alone waits for connections to end,\n\t\t\t\t// so a long-lived one would keep it from calling back and hang\n\t\t\t\t// the app's shutdown\n\t\t\t\tserver.close((error) => (error ? reject(error) : resolve()));\n\t\t\t\tserver.closeAllConnections();\n\t\t\t}),\n\t};\n}\n\nasync function loadTemplate(clientFolder: string): Promise<string | undefined> {\n\t// An endpoints-only site has no site.html, and no template is needed\n\tconst siteHtml = path.join(clientFolder, \"site.html\");\n\tif (!existsSync(siteHtml)) return undefined;\n\n\tconst assets = path.join(clientFolder, \"assets\");\n\tconst clientScript = (await fs.readdir(assets)).find((file) => file.startsWith(\"clientEntry-\"));\n\tif (!clientScript) {\n\t\tthrow new Error(\"clientEntry not found in dist/client/assets\");\n\t}\n\n\treturn prepareTemplate(await fs.readFile(siteHtml, \"utf-8\"), `/assets/${clientScript}`);\n}\n\nasync function serveStatic(clientFolder: string, request: Request): Promise<Response | undefined> {\n\tif (request.method !== \"GET\" && request.method !== \"HEAD\") return undefined;\n\n\tlet pathname: string;\n\ttry {\n\t\tpathname = decodeURIComponent(new URL(request.url).pathname);\n\t} catch {\n\t\treturn undefined;\n\t}\n\n\t// Resolve within clientFolder, rejecting any path that escapes it\n\tconst root = path.resolve(clientFolder);\n\tconst file = path.resolve(root, \".\" + pathname);\n\tif (file !== root && !file.startsWith(root + path.sep)) return undefined;\n\n\tlet stats: Awaited<ReturnType<typeof fs.stat>>;\n\ttry {\n\t\tstats = await fs.stat(file);\n\t} catch {\n\t\treturn undefined;\n\t}\n\tif (!stats.isFile()) return undefined;\n\n\tconst headers = { \"Content-Type\": contentType(path.extname(file)) };\n\tif (request.method === \"HEAD\") {\n\t\treturn new Response(null, { status: 200, headers });\n\t}\n\treturn new Response(await fs.readFile(file), { status: 200, headers });\n}\n"],"mappings":";;;;;;AAGA,SAAwB,YAAY,WAA2B;CAC9D,OAAO,MAAM,cAAc;AAC5B;AAEA,MAAM,QAAgC;CACrC,QAAQ;CACR,SAAS;CACT,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,OAAO;CACP,SAAS;CACT,WAAW;CACX,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,SAAS;CACT,SAAS;CACT,SAAS;CACT,UAAU;CACV,QAAQ;CACR,QAAQ;AACT;;;AC3CA,SAAwB,gBAAgB,UAAkB,cAA8B;CACvF,IAAI,SAAS;CAIb,MAAM,YAAY,OAAO,QAAQ,SAAS;CAC1C,IAAI,cAAc,IACjB,MAAM,IAAI,MAAM,8BAA8B;CAE/C,SAAS,OAAO,UAAU,GAAG,SAAS,IAAI,qBAAqB,OAAO,UAAU,SAAS;CAIzF,MAAM,gBAAgB,aAAa,QAAQ,kCAAkC;CAC7E,IAAI,kBAAkB,IACrB,MAAM,IAAI,MAAM,oCAAoC;CAErD,MAAM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ACH,WAAW,mBAAmB,IAAI,IAAI,QAAQ,GAAG,CAAC,CAAC,QAAQ;CAC5D,QAAQ;EACP;CACD;CAGA,MAAM,OAAO,KAAK,QAAQ,YAAY;CACtC,MAAM,OAAO,KAAK,QAAQ,MAAM,MAAM,QAAQ;CAC9C,IAAI,SAAS,QAAQ,CAAC,KAAK,WAAW,OAAO,KAAK,GAAG,GAAG,OAAO,KAAA;CAE/D,IAAI;CACJ,IAAI;EACH,QAAQ,MAAMA,SAAG,KAAK,IAAI;CAC3B,QAAQ;EACP;CACD;CACA,IAAI,CAAC,MAAM,OAAO,GAAG,OAAO,KAAA;CAE5B,MAAM,UAAU,EAAE,gBAAgB,YAAY,KAAK,QAAQ,IAAI,CAAC,EAAE;CAClE,IAAI,QAAQ,WAAW,QACtB,OAAO,IAAI,SAAS,MAAM;EAAE,QAAQ;EAAK;CAAQ,CAAC;CAEnD,OAAO,IAAI,SAAS,MAAMA,SAAG,SAAS,IAAI,GAAG;EAAE,QAAQ;EAAK;CAAQ,CAAC;AACtE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@torpor/adapter-electron",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "A @torpor/build adapter for building desktop apps with Electron",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.1.0",
|
|
27
|
-
"@torpor/adapter-node": "^1.1.
|
|
28
|
-
"@torpor/build": "^1.
|
|
27
|
+
"@torpor/adapter-node": "^1.1.4",
|
|
28
|
+
"@torpor/build": "^1.7.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^26.6.4",
|