@cossackframework/desktop 0.8.2
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/LICENSE +21 -0
- package/README.md +86 -0
- package/dist/dev-signals.d.ts +8 -0
- package/dist/dev.d.ts +2 -0
- package/dist/dev.js +115 -0
- package/dist/error.d.ts +3 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +308 -0
- package/dist/platform.d.ts +2 -0
- package/docs/guide.md +79 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tan Nguyen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @cossackframework/desktop
|
|
2
|
+
|
|
3
|
+
The Electron main-process runtime for Cossack Desktop applications. It serves
|
|
4
|
+
the renderer and local Cossack SSR/RPC over the private `cossack://app` origin,
|
|
5
|
+
injecting `ASSETS` and a window-scoped `COSSACK_DESKTOP` shell into request
|
|
6
|
+
bindings.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { createDesktopApp } from '@cossackframework/desktop';
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
await createDesktopApp({
|
|
13
|
+
identifier: 'dev.cossack.my-app',
|
|
14
|
+
productName: 'My App',
|
|
15
|
+
assetsRoot: new URL('../../client/', import.meta.url).pathname,
|
|
16
|
+
fetch: (request, env) => app.fetch(request, env),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
void main();
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Do not top-level `await` `createDesktopApp()` in an ESM entry. Electron fires
|
|
24
|
+
`ready` after the main module's first event-loop tick completes, while Desktop
|
|
25
|
+
creation waits for `ready`; start an async function without awaiting it at the
|
|
26
|
+
module's top level as shown above.
|
|
27
|
+
|
|
28
|
+
`createDesktopApp()` forces context isolation and renderer sandboxing, with Node
|
|
29
|
+
integration and preload bridges disabled. Only same-origin navigation is
|
|
30
|
+
allowed; valid HTTP(S) popups are opened in the operating-system browser.
|
|
31
|
+
Desktop pages currently require `@Page({ transport: 'http' })`.
|
|
32
|
+
|
|
33
|
+
On Linux, `desktop:dev` checks Electron's local `chrome-sandbox` helper. pnpm's
|
|
34
|
+
download is normally user-owned rather than root-owned/setuid, so the runner
|
|
35
|
+
prints a warning and applies Electron's testing-only `--no-sandbox` flag to the
|
|
36
|
+
development process. Forge packages and installed DEB applications do not use
|
|
37
|
+
this fallback; the DEB installs its helper with the required permissions.
|
|
38
|
+
For Wayland sessions that expose XWayland, the development runner selects X11
|
|
39
|
+
to avoid Chromium's current Wayland/Vulkan incompatibility and hides nonfatal
|
|
40
|
+
GPU probe noise. Set `COSSACK_DESKTOP_OZONE_PLATFORM=wayland` to force native
|
|
41
|
+
Wayland, or `COSSACK_DESKTOP_DEBUG=1` to retain Chromium diagnostics and print
|
|
42
|
+
Desktop lifecycle tracing.
|
|
43
|
+
|
|
44
|
+
The generated Linux desktop entry also starts the installed DEB through
|
|
45
|
+
XWayland so Electron can register its tray with GNOME's StatusNotifier host.
|
|
46
|
+
An unpacked Forge executable bypasses that desktop entry; launch it with both
|
|
47
|
+
`--no-sandbox` and `--ozone-platform=x11` when testing directly from `out/`.
|
|
48
|
+
The sandbox flag is only appropriate for that uninstalled test package.
|
|
49
|
+
|
|
50
|
+
Node 22 or 24 LTS is recommended for Forge packaging. Node 26 is supported, but
|
|
51
|
+
the current Forge/Packager ZIP stack can extract Electron pathologically slowly
|
|
52
|
+
there; generated Desktop projects declare `node: ">=22"`.
|
|
53
|
+
|
|
54
|
+
Use Electron APIs in the bootstrap or through ordinary `@Server()` methods via
|
|
55
|
+
`this.env.COSSACK_DESKTOP`. There is deliberately no renderer client or generic
|
|
56
|
+
IPC bridge. See Electron's security checklist before adding any renderer-facing
|
|
57
|
+
capability: https://www.electronjs.org/docs/latest/tutorial/security
|
|
58
|
+
|
|
59
|
+
Use `createDesktopTray({ image, menu, toolTip })` when the application needs a
|
|
60
|
+
tray. It returns Electron's native `Tray` and, on Linux, performs the separate
|
|
61
|
+
StatusNotifier registration needed by Ubuntu's AppIndicator extension. The
|
|
62
|
+
compatibility registration uses `dbus-send`, supplied by the standard `dbus`
|
|
63
|
+
package on Debian/Ubuntu. Keep the returned tray strongly referenced.
|
|
64
|
+
|
|
65
|
+
Choose close behavior explicitly with `configureDesktopClose()`:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const behavior = process.platform === 'linux'
|
|
69
|
+
? 'confirm-quit'
|
|
70
|
+
: tray
|
|
71
|
+
? 'hide-to-tray'
|
|
72
|
+
: 'quit';
|
|
73
|
+
|
|
74
|
+
const close = configureDesktopClose({
|
|
75
|
+
window: desktop.mainWindow,
|
|
76
|
+
behavior,
|
|
77
|
+
tray,
|
|
78
|
+
onQuit: () => desktop.quit(),
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
`quit` closes the application, `hide-to-tray` hides only while its tray is
|
|
83
|
+
alive and otherwise quits, and `confirm-quit` uses an asynchronous native
|
|
84
|
+
dialog. Linux tray activation varies by desktop host, so prefer
|
|
85
|
+
`confirm-quit` unless the target distribution has been tested. Use
|
|
86
|
+
`close.quit()` from every explicit Quit menu action.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function isSuccessfulBuildOutput(value: string): boolean;
|
|
2
|
+
export interface SandboxHelperMetadata {
|
|
3
|
+
uid: number;
|
|
4
|
+
mode: number;
|
|
5
|
+
}
|
|
6
|
+
/** Use Electron's testing-only fallback only when the Linux SUID helper cannot work. */
|
|
7
|
+
export declare function developmentSandboxArgs(platform: NodeJS.Platform, helper: SandboxHelperMetadata | undefined): string[];
|
|
8
|
+
export declare function developmentGraphicsArgs(platform: NodeJS.Platform, environment: Record<string, string | undefined>): string[];
|
package/dist/dev.d.ts
ADDED
package/dist/dev.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn as e } from "node:child_process";
|
|
3
|
+
import t from "node:path";
|
|
4
|
+
import { statSync as n } from "node:fs";
|
|
5
|
+
import { createRequire as r } from "node:module";
|
|
6
|
+
//#region src/platform.ts
|
|
7
|
+
function i(e, t, n = "auto") {
|
|
8
|
+
if (e !== "linux") return;
|
|
9
|
+
if (n !== "auto") return n;
|
|
10
|
+
let r = t.COSSACK_DESKTOP_OZONE_PLATFORM;
|
|
11
|
+
if (r === "x11" || r === "wayland") return r;
|
|
12
|
+
if (t.XDG_SESSION_TYPE === "wayland" && t.DISPLAY) return "x11";
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/dev-signals.ts
|
|
16
|
+
function a(e) {
|
|
17
|
+
return /built in \d|built in [\d.]+s/i.test(e);
|
|
18
|
+
}
|
|
19
|
+
function o(e, t) {
|
|
20
|
+
return e === "linux" ? t?.uid === 0 && t.mode & 2048 ? [] : ["--no-sandbox"] : [];
|
|
21
|
+
}
|
|
22
|
+
function s(e, t) {
|
|
23
|
+
if (e !== "linux") return [];
|
|
24
|
+
let n = [], r = i(e, t);
|
|
25
|
+
return r && n.push(`--ozone-platform=${r}`), t.COSSACK_DESKTOP_DEBUG !== "1" && n.push("--log-level=3"), n;
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/dev.ts
|
|
29
|
+
var c = process.cwd(), l = r(t.join(c, "package.json")), u = t.join(t.dirname(l.resolve("vite/package.json")), "bin", "vite.js"), d = l("electron"), f = t.resolve(c, "dist/desktop/index.js"), p;
|
|
30
|
+
try {
|
|
31
|
+
let e = n(t.join(t.dirname(d), "chrome-sandbox"));
|
|
32
|
+
p = {
|
|
33
|
+
uid: e.uid,
|
|
34
|
+
mode: e.mode
|
|
35
|
+
};
|
|
36
|
+
} catch {}
|
|
37
|
+
var m = [...o(process.platform, p), ...s(process.platform, process.env)];
|
|
38
|
+
m.includes("--no-sandbox") && process.stderr.write("[desktop] Linux SUID sandbox helper is not root-owned with mode 4755; using Electron --no-sandbox for desktop:dev only. Packaged installers remain sandboxed.\n");
|
|
39
|
+
var h, g = !1, _ = !1, v = !1, y = !1;
|
|
40
|
+
function b(e, t, n) {
|
|
41
|
+
e.stdout?.on("data", (e) => {
|
|
42
|
+
let r = e.toString();
|
|
43
|
+
process.stdout.write(`[${t}] ${r}`), a(r) && n();
|
|
44
|
+
}), e.stderr?.on("data", (e) => {
|
|
45
|
+
let r = e.toString();
|
|
46
|
+
process.stderr.write(`[${t}] ${r}`), a(r) && n();
|
|
47
|
+
}), e.on("exit", (e) => {
|
|
48
|
+
!v && e !== 0 && w(e ?? 1);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function x() {
|
|
52
|
+
!g || !_ || v || (h = e(d, [...m, f], {
|
|
53
|
+
cwd: c,
|
|
54
|
+
stdio: [
|
|
55
|
+
"inherit",
|
|
56
|
+
"inherit",
|
|
57
|
+
"inherit",
|
|
58
|
+
"ipc"
|
|
59
|
+
]
|
|
60
|
+
}), h.on("exit", (e, t) => {
|
|
61
|
+
let n = y;
|
|
62
|
+
y = !1, h = void 0, !v && (n ? x() : w(e ?? +!!t));
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
function S() {
|
|
66
|
+
if (!h) return x();
|
|
67
|
+
y = !0, h.kill();
|
|
68
|
+
}
|
|
69
|
+
function C() {
|
|
70
|
+
h?.connected ? h.send?.({ type: "cossack:renderer-reload" }) : x();
|
|
71
|
+
}
|
|
72
|
+
function w(e = 0) {
|
|
73
|
+
v || (v = !0, T.kill(), E.kill(), h?.kill(), process.exitCode = e);
|
|
74
|
+
}
|
|
75
|
+
var T = e(process.execPath, [
|
|
76
|
+
u,
|
|
77
|
+
"build",
|
|
78
|
+
"--watch"
|
|
79
|
+
], {
|
|
80
|
+
cwd: c,
|
|
81
|
+
stdio: [
|
|
82
|
+
"ignore",
|
|
83
|
+
"pipe",
|
|
84
|
+
"pipe"
|
|
85
|
+
]
|
|
86
|
+
}), E = e(process.execPath, [
|
|
87
|
+
u,
|
|
88
|
+
"build",
|
|
89
|
+
"--mode",
|
|
90
|
+
"desktop",
|
|
91
|
+
"--ssr",
|
|
92
|
+
"src/desktop/index.ts",
|
|
93
|
+
"--outDir",
|
|
94
|
+
"dist/desktop",
|
|
95
|
+
"--watch",
|
|
96
|
+
"--minify",
|
|
97
|
+
"false"
|
|
98
|
+
], {
|
|
99
|
+
cwd: c,
|
|
100
|
+
stdio: [
|
|
101
|
+
"ignore",
|
|
102
|
+
"pipe",
|
|
103
|
+
"pipe"
|
|
104
|
+
]
|
|
105
|
+
});
|
|
106
|
+
b(T, "renderer", () => {
|
|
107
|
+
let e = !g;
|
|
108
|
+
g = !0, e ? x() : C();
|
|
109
|
+
}), b(E, "main", () => {
|
|
110
|
+
let e = !_;
|
|
111
|
+
_ = !0, e ? x() : S();
|
|
112
|
+
}), process.once("SIGINT", () => w(130)), process.once("SIGTERM", () => w(143)), process.once("exit", () => {
|
|
113
|
+
v = !0, T.kill(), E.kill(), h?.kill();
|
|
114
|
+
});
|
|
115
|
+
//#endregion
|
package/dist/error.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import electron from 'electron';
|
|
2
|
+
import type { BrowserWindowConstructorOptions, MessageBoxOptions, NativeImage, WebPreferences } from 'electron';
|
|
3
|
+
import type { CossackRuntimeAdapter } from '@cossackframework/framework/runtime-adapter';
|
|
4
|
+
export declare const app: Electron.App, BrowserWindow: typeof electron.BrowserWindow, dialog: Electron.Dialog, Menu: typeof electron.Menu, nativeImage: typeof Electron.NativeImage, Notification: typeof electron.Notification, protocol: Electron.Protocol, electronShell: Electron.Shell, Tray: typeof electron.Tray;
|
|
5
|
+
export { DesktopUnavailableError } from './error.js';
|
|
6
|
+
export { electronShell as shell };
|
|
7
|
+
export type { App, BrowserWindowConstructorOptions, Certificate, ContextMenuParams, Dialog, FileFilter, JumpListCategory, JumpListItem, MenuItemConstructorOptions, MessageBoxOptions, MessageBoxReturnValue, NativeImage, NotificationConstructorOptions, OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue, Shell, ThumbarButton, WebContents, WebPreferences, WillNavigateEvent, } from 'electron';
|
|
8
|
+
export type BrowserWindow = import('electron').BrowserWindow;
|
|
9
|
+
export type Menu = import('electron').Menu;
|
|
10
|
+
export type Notification = import('electron').Notification;
|
|
11
|
+
export type Tray = import('electron').Tray;
|
|
12
|
+
export type Dock = NonNullable<import('electron').App['dock']>;
|
|
13
|
+
export declare const electronRuntimeAdapter: CossackRuntimeAdapter & {
|
|
14
|
+
readonly name: 'electron';
|
|
15
|
+
readonly supportedTransports: readonly ['http'];
|
|
16
|
+
};
|
|
17
|
+
export type DesktopFetch = (request: Request, env?: Record<string, unknown>) => Response | Promise<Response>;
|
|
18
|
+
export type SafeBrowserWindowOptions = Omit<BrowserWindowConstructorOptions, 'webPreferences'> & {
|
|
19
|
+
webPreferences?: Pick<WebPreferences, 'spellcheck' | 'enableWebSQL' | 'navigateOnDragDrop'>;
|
|
20
|
+
};
|
|
21
|
+
export interface CreateDesktopAppOptions {
|
|
22
|
+
identifier: string;
|
|
23
|
+
productName: string;
|
|
24
|
+
assetsRoot: string;
|
|
25
|
+
fetch: DesktopFetch;
|
|
26
|
+
env?: Record<string, unknown>;
|
|
27
|
+
startPath?: string;
|
|
28
|
+
window?: SafeBrowserWindowOptions;
|
|
29
|
+
}
|
|
30
|
+
export interface CreateDesktopWindowOptions extends SafeBrowserWindowOptions {
|
|
31
|
+
path?: string;
|
|
32
|
+
}
|
|
33
|
+
export interface DesktopShell {
|
|
34
|
+
readonly app: typeof app;
|
|
35
|
+
readonly window: BrowserWindow;
|
|
36
|
+
readonly Menu: typeof Menu;
|
|
37
|
+
readonly Tray: typeof Tray;
|
|
38
|
+
readonly dialog: typeof dialog;
|
|
39
|
+
readonly Notification: typeof Notification;
|
|
40
|
+
readonly nativeImage: typeof nativeImage;
|
|
41
|
+
readonly shell: typeof electronShell;
|
|
42
|
+
readonly dock: typeof app.dock;
|
|
43
|
+
show(): void;
|
|
44
|
+
hide(): void;
|
|
45
|
+
focus(): void;
|
|
46
|
+
quit(): void;
|
|
47
|
+
setBadge(value: string | number | null): boolean;
|
|
48
|
+
setOverlayIcon(image: NativeImage | null, description?: string): void;
|
|
49
|
+
}
|
|
50
|
+
export interface DesktopApplication {
|
|
51
|
+
readonly app: typeof app;
|
|
52
|
+
readonly mainWindow: BrowserWindow;
|
|
53
|
+
readonly shell: DesktopShell;
|
|
54
|
+
readonly quitting: boolean;
|
|
55
|
+
createWindow(options?: CreateDesktopWindowOptions): BrowserWindow;
|
|
56
|
+
show(): void;
|
|
57
|
+
hide(): void;
|
|
58
|
+
quit(): void;
|
|
59
|
+
}
|
|
60
|
+
export interface CreateDesktopTrayOptions {
|
|
61
|
+
image: NativeImage | string;
|
|
62
|
+
menu?: Menu | null;
|
|
63
|
+
toolTip?: string;
|
|
64
|
+
guid?: string;
|
|
65
|
+
}
|
|
66
|
+
export type DesktopCloseBehavior = 'quit' | 'hide-to-tray' | 'confirm-quit';
|
|
67
|
+
export interface ConfigureDesktopCloseOptions {
|
|
68
|
+
window?: BrowserWindow;
|
|
69
|
+
behavior: DesktopCloseBehavior;
|
|
70
|
+
tray?: Tray;
|
|
71
|
+
confirmation?: Partial<MessageBoxOptions>;
|
|
72
|
+
confirmResponse?: number;
|
|
73
|
+
onQuit?: () => void;
|
|
74
|
+
}
|
|
75
|
+
export interface DesktopCloseController {
|
|
76
|
+
readonly behavior: DesktopCloseBehavior;
|
|
77
|
+
quit(): void;
|
|
78
|
+
dispose(): void;
|
|
79
|
+
}
|
|
80
|
+
/** Create a native Electron tray and apply Cossack's Linux host compatibility fix. */
|
|
81
|
+
export declare function createDesktopTray(options: CreateDesktopTrayOptions): Tray;
|
|
82
|
+
/** Apply one explicit, disposable policy to a native BrowserWindow close event. */
|
|
83
|
+
export declare function configureDesktopClose(options: ConfigureDesktopCloseOptions): DesktopCloseController;
|
|
84
|
+
export declare function createDesktopShell(options?: {
|
|
85
|
+
window?: BrowserWindow;
|
|
86
|
+
}): DesktopShell;
|
|
87
|
+
/** Initialize the process-wide Desktop application exactly once. */
|
|
88
|
+
export declare function createDesktopApp(options: CreateDesktopAppOptions): Promise<DesktopApplication>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { execFile as e } from "node:child_process";
|
|
2
|
+
import { readFile as t, stat as n } from "node:fs/promises";
|
|
3
|
+
import r from "node:path";
|
|
4
|
+
import i from "electron";
|
|
5
|
+
//#region src/error.ts
|
|
6
|
+
var a = class extends Error {
|
|
7
|
+
constructor() {
|
|
8
|
+
super("@cossackframework/desktop can only create applications and shells in the Electron main process."), this.name = "DesktopUnavailableError";
|
|
9
|
+
}
|
|
10
|
+
}, { app: o, BrowserWindow: s, dialog: c, Menu: l, nativeImage: u, Notification: d, protocol: f, shell: p, Tray: m } = typeof i == "object" && i ? i : {}, h = {
|
|
11
|
+
name: "electron",
|
|
12
|
+
supportedTransports: ["http"],
|
|
13
|
+
getClientMetadata: () => ({
|
|
14
|
+
platform: "desktop",
|
|
15
|
+
adapter: "electron"
|
|
16
|
+
})
|
|
17
|
+
}, g = "cossack", _ = "cossack://app", v = !1, y, b = 0;
|
|
18
|
+
function x(e) {
|
|
19
|
+
process.env.COSSACK_DESKTOP_DEBUG === "1" && console.error(`[Cossack Desktop] ${e}`);
|
|
20
|
+
}
|
|
21
|
+
function S() {
|
|
22
|
+
v || f?.registerSchemesAsPrivileged && (f.registerSchemesAsPrivileged([{
|
|
23
|
+
scheme: g,
|
|
24
|
+
privileges: {
|
|
25
|
+
standard: !0,
|
|
26
|
+
secure: !0,
|
|
27
|
+
supportFetchAPI: !0,
|
|
28
|
+
corsEnabled: !1,
|
|
29
|
+
stream: !0
|
|
30
|
+
}
|
|
31
|
+
}]), v = !0);
|
|
32
|
+
}
|
|
33
|
+
S();
|
|
34
|
+
function C() {
|
|
35
|
+
if (!process.versions.electron || process.type === "renderer" || !o || !s) throw new a();
|
|
36
|
+
}
|
|
37
|
+
function w(t, n) {
|
|
38
|
+
if (process.platform !== "linux") return;
|
|
39
|
+
let r = `org.freedesktop.StatusNotifierItem-${process.pid}-${n}`;
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
t.isDestroyed() || e("dbus-send", [
|
|
42
|
+
"--session",
|
|
43
|
+
"--print-reply",
|
|
44
|
+
"--type=method_call",
|
|
45
|
+
"--dest=org.kde.StatusNotifierWatcher",
|
|
46
|
+
"/StatusNotifierWatcher",
|
|
47
|
+
"org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem",
|
|
48
|
+
`string:${r}`
|
|
49
|
+
], { timeout: 2e3 }, (e) => {
|
|
50
|
+
e && x(`could not repair Linux tray registration: ${e.message}`);
|
|
51
|
+
});
|
|
52
|
+
}, 4e3).unref();
|
|
53
|
+
}
|
|
54
|
+
function T(e) {
|
|
55
|
+
C();
|
|
56
|
+
let t = ++b, n = e.guid ? new m(e.image, e.guid) : new m(e.image);
|
|
57
|
+
return e.toolTip && n.setToolTip(e.toolTip), e.menu !== void 0 && n.setContextMenu(e.menu), w(n, t), n;
|
|
58
|
+
}
|
|
59
|
+
function E(e) {
|
|
60
|
+
C();
|
|
61
|
+
let t = e.window ?? s.getFocusedWindow() ?? s.getAllWindows()[0];
|
|
62
|
+
if (!t) throw new a();
|
|
63
|
+
if (e.behavior === "hide-to-tray" && !e.tray) throw TypeError("Desktop hide-to-tray behavior requires a tray.");
|
|
64
|
+
let n = !1, r = !1, i = !1, l = () => {
|
|
65
|
+
r = !0;
|
|
66
|
+
}, u = () => {
|
|
67
|
+
r || (r = !0, (e.onQuit ?? (() => o.quit()))());
|
|
68
|
+
}, d = {
|
|
69
|
+
behavior: e.behavior,
|
|
70
|
+
quit: u,
|
|
71
|
+
dispose() {
|
|
72
|
+
n || (n = !0, t.removeListener("close", f), o.removeListener("before-quit", l));
|
|
73
|
+
}
|
|
74
|
+
}, f = (a) => {
|
|
75
|
+
if (n || r) return;
|
|
76
|
+
if (a.preventDefault(), e.behavior === "quit") {
|
|
77
|
+
u();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (e.behavior === "hide-to-tray") {
|
|
81
|
+
e.tray && !e.tray.isDestroyed() ? t.hide() : u();
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (i) return;
|
|
85
|
+
i = !0;
|
|
86
|
+
let s = o.getName();
|
|
87
|
+
c.showMessageBox(t, {
|
|
88
|
+
type: "question",
|
|
89
|
+
title: s,
|
|
90
|
+
message: `Quit ${s}?`,
|
|
91
|
+
buttons: ["Quit", "Cancel"],
|
|
92
|
+
defaultId: 1,
|
|
93
|
+
cancelId: 1,
|
|
94
|
+
noLink: !0,
|
|
95
|
+
...e.confirmation
|
|
96
|
+
}).then(({ response: t }) => {
|
|
97
|
+
t === (e.confirmResponse ?? 0) && u();
|
|
98
|
+
}).catch((e) => {
|
|
99
|
+
console.error("[Cossack Desktop] Could not show the close confirmation.", e);
|
|
100
|
+
}).finally(() => {
|
|
101
|
+
i = !1;
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
return t.on("close", f), o.on("before-quit", l), d;
|
|
105
|
+
}
|
|
106
|
+
function D(e) {
|
|
107
|
+
let t;
|
|
108
|
+
try {
|
|
109
|
+
t = decodeURIComponent(e);
|
|
110
|
+
} catch {
|
|
111
|
+
throw URIError("Malformed Desktop URL path.");
|
|
112
|
+
}
|
|
113
|
+
if (t.includes("\0") || t.includes("\\")) throw URIError("Unsafe Desktop URL path.");
|
|
114
|
+
if (t.split("/").includes("..")) throw URIError("Desktop URL traversal is not allowed.");
|
|
115
|
+
return t.replace(/^\/+/, "");
|
|
116
|
+
}
|
|
117
|
+
function O(e, t) {
|
|
118
|
+
let n = r.resolve(e), i = r.resolve(n, t || "__cossack_ssr__"), a = r.relative(n, i);
|
|
119
|
+
if (a.startsWith("..") || r.isAbsolute(a)) throw URIError("Desktop URL traversal is not allowed.");
|
|
120
|
+
return i;
|
|
121
|
+
}
|
|
122
|
+
var k = {
|
|
123
|
+
".css": "text/css; charset=utf-8",
|
|
124
|
+
".gif": "image/gif",
|
|
125
|
+
".html": "text/html; charset=utf-8",
|
|
126
|
+
".ico": "image/x-icon",
|
|
127
|
+
".jpeg": "image/jpeg",
|
|
128
|
+
".jpg": "image/jpeg",
|
|
129
|
+
".js": "text/javascript; charset=utf-8",
|
|
130
|
+
".json": "application/json; charset=utf-8",
|
|
131
|
+
".map": "application/json; charset=utf-8",
|
|
132
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
133
|
+
".png": "image/png",
|
|
134
|
+
".svg": "image/svg+xml; charset=utf-8",
|
|
135
|
+
".txt": "text/plain; charset=utf-8",
|
|
136
|
+
".wasm": "application/wasm",
|
|
137
|
+
".webp": "image/webp",
|
|
138
|
+
".woff": "font/woff",
|
|
139
|
+
".woff2": "font/woff2"
|
|
140
|
+
};
|
|
141
|
+
async function A(e, i) {
|
|
142
|
+
if (i.method !== "GET" && i.method !== "HEAD") return;
|
|
143
|
+
let a = D(new URL(i.url).pathname);
|
|
144
|
+
if (!a) return;
|
|
145
|
+
let o = O(e, a);
|
|
146
|
+
try {
|
|
147
|
+
let e = await n(o);
|
|
148
|
+
if (!e.isFile()) return;
|
|
149
|
+
let a = new Headers({
|
|
150
|
+
"content-type": k[r.extname(o).toLowerCase()] ?? "application/octet-stream",
|
|
151
|
+
"content-length": String(e.size)
|
|
152
|
+
});
|
|
153
|
+
return new Response(i.method === "HEAD" ? null : await t(o), { headers: a });
|
|
154
|
+
} catch (e) {
|
|
155
|
+
if (e.code === "ENOENT" || e.code === "ENOTDIR") return;
|
|
156
|
+
throw e;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function j(e) {
|
|
160
|
+
let t = new URL(e.url), n = new URL(`${t.pathname}${t.search}`, "https://app");
|
|
161
|
+
return n.searchParams.delete("__cossack_window"), new Request(n, e);
|
|
162
|
+
}
|
|
163
|
+
function M(e) {
|
|
164
|
+
return e.protocol === `${g}:` && e.hostname === "app" && e.username === "" && e.password === "" && e.port === "";
|
|
165
|
+
}
|
|
166
|
+
function N(e = {}) {
|
|
167
|
+
let { webPreferences: t, ...n } = e;
|
|
168
|
+
return {
|
|
169
|
+
width: 1e3,
|
|
170
|
+
height: 720,
|
|
171
|
+
show: !1,
|
|
172
|
+
...n,
|
|
173
|
+
webPreferences: {
|
|
174
|
+
...t,
|
|
175
|
+
contextIsolation: !0,
|
|
176
|
+
sandbox: !0,
|
|
177
|
+
nodeIntegration: !1,
|
|
178
|
+
nodeIntegrationInWorker: !1,
|
|
179
|
+
nodeIntegrationInSubFrames: !1,
|
|
180
|
+
webSecurity: !0,
|
|
181
|
+
allowRunningInsecureContent: !1,
|
|
182
|
+
webviewTag: !1,
|
|
183
|
+
preload: void 0
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
function P(e) {
|
|
188
|
+
e.webContents.on("will-navigate", (e, t) => {
|
|
189
|
+
try {
|
|
190
|
+
let n = new URL(t);
|
|
191
|
+
M(n) || (e.preventDefault(), (n.protocol === "https:" || n.protocol === "http:") && p.openExternal(n.href));
|
|
192
|
+
} catch {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
}
|
|
195
|
+
}), e.webContents.setWindowOpenHandler(({ url: e }) => {
|
|
196
|
+
try {
|
|
197
|
+
let t = new URL(e);
|
|
198
|
+
(t.protocol === "https:" || t.protocol === "http:") && p.openExternal(t.href);
|
|
199
|
+
} catch {}
|
|
200
|
+
return { action: "deny" };
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
function F(e = {}) {
|
|
204
|
+
C();
|
|
205
|
+
let t = e.window ?? s.getFocusedWindow() ?? s.getAllWindows()[0];
|
|
206
|
+
if (!t) throw new a();
|
|
207
|
+
return {
|
|
208
|
+
app: o,
|
|
209
|
+
window: t,
|
|
210
|
+
Menu: l,
|
|
211
|
+
Tray: m,
|
|
212
|
+
dialog: c,
|
|
213
|
+
Notification: d,
|
|
214
|
+
nativeImage: u,
|
|
215
|
+
shell: p,
|
|
216
|
+
get dock() {
|
|
217
|
+
return o.dock;
|
|
218
|
+
},
|
|
219
|
+
show() {
|
|
220
|
+
t.show();
|
|
221
|
+
},
|
|
222
|
+
hide() {
|
|
223
|
+
t.hide();
|
|
224
|
+
},
|
|
225
|
+
focus() {
|
|
226
|
+
t.focus();
|
|
227
|
+
},
|
|
228
|
+
quit() {
|
|
229
|
+
o.quit();
|
|
230
|
+
},
|
|
231
|
+
setBadge(e) {
|
|
232
|
+
return o.setBadgeCount(e === null ? 0 : typeof e == "number" ? e : Number(e) || 0);
|
|
233
|
+
},
|
|
234
|
+
setOverlayIcon(e, n = "") {
|
|
235
|
+
t.setOverlayIcon(e, n);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
async function I(e) {
|
|
240
|
+
if (C(), x("initializing Electron application"), !e.identifier.trim()) throw TypeError("Desktop identifier is required.");
|
|
241
|
+
if (!e.productName.trim()) throw TypeError("Desktop productName is required.");
|
|
242
|
+
let t = r.resolve(e.assetsRoot), n = `/${(e.startPath ?? "/").replace(/^\/+/, "")}`, i = !1, a;
|
|
243
|
+
o.setName(e.productName), process.platform === "linux" && o.commandLine.appendSwitch("class", e.identifier), process.platform === "win32" && o.setAppUserModelId(e.identifier), x("requesting single-instance lock");
|
|
244
|
+
let c = o.requestSingleInstanceLock();
|
|
245
|
+
if (x(`single-instance lock ${c ? "acquired" : "unavailable"}`), !c) throw o.quit(), Error(`[Cossack] Another ${e.productName} instance is already running.`);
|
|
246
|
+
await o.whenReady(), x("Electron app is ready");
|
|
247
|
+
let l = (t = {}) => {
|
|
248
|
+
let { path: r = n, ...i } = t, a = new URL(r, `${_}/`);
|
|
249
|
+
if (!M(a)) throw TypeError("Desktop window paths must stay within cossack://app.");
|
|
250
|
+
let o = new s(N({
|
|
251
|
+
...e.window,
|
|
252
|
+
...i
|
|
253
|
+
}));
|
|
254
|
+
return x(`created BrowserWindow ${o.id}`), P(o), a.searchParams.set("__cossack_window", String(o.id)), o.loadURL(a.href).then(() => x(`loaded ${a.href}`), (e) => console.error(`[Cossack Desktop] Failed to load ${a.href}`, e)), o.once("ready-to-show", () => o.show()), o;
|
|
255
|
+
};
|
|
256
|
+
await f.handle(g, async (n) => {
|
|
257
|
+
x(`handling ${n.method} ${n.url}`);
|
|
258
|
+
let r, i;
|
|
259
|
+
try {
|
|
260
|
+
if (r = new URL(n.url), !M(r)) return new Response("Forbidden Desktop authority", { status: 403 });
|
|
261
|
+
D(r.pathname);
|
|
262
|
+
let e = n.headers.get("referer");
|
|
263
|
+
if (i = new URL(e || n.url), !M(i)) return new Response("Forbidden Desktop authority", { status: 403 });
|
|
264
|
+
} catch {
|
|
265
|
+
return new Response("Invalid Desktop URL", { status: 400 });
|
|
266
|
+
}
|
|
267
|
+
let o = i.searchParams.get("__cossack_window"), c = o ? s.fromId(Number(o)) ?? a : a, l = c ? F({ window: c }) : void 0, u = await A(t, n);
|
|
268
|
+
if (u) return u;
|
|
269
|
+
let d = { fetch: async (e) => await A(t, e) ?? new Response("Not found", { status: 404 }) };
|
|
270
|
+
return e.fetch(j(n), {
|
|
271
|
+
...e.env ?? {},
|
|
272
|
+
ASSETS: d,
|
|
273
|
+
COSSACK_DESKTOP: l
|
|
274
|
+
});
|
|
275
|
+
}), x("registered cossack:// protocol handler"), a = l();
|
|
276
|
+
let u = F({ window: a }), d = {
|
|
277
|
+
app: o,
|
|
278
|
+
mainWindow: a,
|
|
279
|
+
shell: u,
|
|
280
|
+
get quitting() {
|
|
281
|
+
return i;
|
|
282
|
+
},
|
|
283
|
+
createWindow: l,
|
|
284
|
+
show() {
|
|
285
|
+
a.show(), a.focus();
|
|
286
|
+
},
|
|
287
|
+
hide() {
|
|
288
|
+
a.hide();
|
|
289
|
+
},
|
|
290
|
+
quit() {
|
|
291
|
+
i = !0, o.quit();
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
return o.on("before-quit", () => {
|
|
295
|
+
i = !0;
|
|
296
|
+
}), o.on("activate", () => {
|
|
297
|
+
s.getAllWindows().length === 0 ? a = l() : d.show();
|
|
298
|
+
}), o.on("second-instance", () => d.show()), process.on("message", (e) => {
|
|
299
|
+
if (e?.type === "cossack:renderer-reload") for (let e of s.getAllWindows()) e.webContents.reloadIgnoringCache();
|
|
300
|
+
}), d;
|
|
301
|
+
}
|
|
302
|
+
function L(e) {
|
|
303
|
+
return C(), y ??= I(e).catch((e) => {
|
|
304
|
+
throw y = void 0, e;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
//#endregion
|
|
308
|
+
export { s as BrowserWindow, a as DesktopUnavailableError, l as Menu, d as Notification, m as Tray, o as app, E as configureDesktopClose, L as createDesktopApp, F as createDesktopShell, T as createDesktopTray, c as dialog, h as electronRuntimeAdapter, p as electronShell, p as shell, u as nativeImage, f as protocol };
|
package/docs/guide.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Cossack Desktop guide
|
|
2
|
+
|
|
3
|
+
Add the Electron side target to a Cloudflare, Node, or Deno web application:
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
cossack add desktop
|
|
7
|
+
pnpm install
|
|
8
|
+
pnpm desktop:dev
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The scaffold creates `src/desktop/index.ts`, `forge.config.ts`, platform icons,
|
|
12
|
+
and build/package/make scripts. The Electron process always runs on Node even
|
|
13
|
+
when the web adapter is Deno or Cloudflare. Bindings such as D1 do not exist in
|
|
14
|
+
Electron unless the application supplies an Electron-compatible equivalent.
|
|
15
|
+
|
|
16
|
+
`createDesktopApp()` registers and serves `cossack://app`, injects `ASSETS` and
|
|
17
|
+
a window-scoped `COSSACK_DESKTOP`, and starts a sandboxed, context-isolated
|
|
18
|
+
`BrowserWindow`. Use only HTTP-transport pages. Native work belongs in the
|
|
19
|
+
bootstrap or ordinary `@Server()` methods:
|
|
20
|
+
|
|
21
|
+
Do not top-level `await` `createDesktopApp()` from an ESM entry. Invoke an async
|
|
22
|
+
`main()` without awaiting it at module scope so Electron can complete the first
|
|
23
|
+
event-loop tick and emit `ready`.
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
const shell = this.env.COSSACK_DESKTOP as DesktopShell;
|
|
27
|
+
const result = await shell.dialog.showMessageBox(shell.window, options);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Electron dialogs are asynchronous. Test `Notification.isSupported()` before
|
|
31
|
+
showing a native notification. Treat `app.dock`, Linux badges, Windows overlay
|
|
32
|
+
icons, taskbar buttons, menus, and trays as platform-native APIs rather than
|
|
33
|
+
cross-platform emulations. Keep a strong `Tray` reference, intercept close
|
|
34
|
+
only after tray creation succeeds, and maintain an explicit quitting flag.
|
|
35
|
+
Create trays with `createDesktopTray({ image, menu, toolTip })`; it returns the
|
|
36
|
+
native Electron object and repairs Ubuntu AppIndicator's StatusNotifier
|
|
37
|
+
registration through the standard Linux `dbus-send` utility.
|
|
38
|
+
|
|
39
|
+
Configure window close handling with `configureDesktopClose()`. Its
|
|
40
|
+
`behavior` is one of `quit`, `hide-to-tray`, or `confirm-quit`. The hide policy
|
|
41
|
+
requires a live tray and safely quits if that tray is later destroyed; the
|
|
42
|
+
confirmation policy uses an asynchronous native message box and ignores
|
|
43
|
+
duplicate close attempts while it is open. A conservative OS policy is
|
|
44
|
+
`confirm-quit` on Linux and `hide-to-tray` on Windows/macOS only after tray
|
|
45
|
+
construction succeeds. Generated projects start with `quit`.
|
|
46
|
+
|
|
47
|
+
On Linux, the development runner inspects Electron's local SUID sandbox helper.
|
|
48
|
+
If the pnpm copy is not root-owned with mode `4755`, it warns and uses
|
|
49
|
+
Electron's testing-only `--no-sandbox` flag for `desktop:dev`. Packaged
|
|
50
|
+
applications never receive this flag; DEB installation gives the helper its
|
|
51
|
+
required permissions.
|
|
52
|
+
|
|
53
|
+
On a Wayland session with XWayland available, `desktop:dev` selects X11 to
|
|
54
|
+
avoid Chromium's Wayland/Vulkan incompatibility and suppresses nonfatal GPU
|
|
55
|
+
probe logs. Set `COSSACK_DESKTOP_OZONE_PLATFORM=wayland` to test native Wayland,
|
|
56
|
+
or `COSSACK_DESKTOP_DEBUG=1` to retain Chromium diagnostics and enable Desktop
|
|
57
|
+
lifecycle tracing.
|
|
58
|
+
|
|
59
|
+
The generated DEB desktop entry includes `--ozone-platform=x11` for reliable
|
|
60
|
+
GNOME StatusNotifier tray integration. A binary launched directly from Forge's
|
|
61
|
+
unpacked `out/` directory does not use the desktop entry. Test that artifact as
|
|
62
|
+
`./your-app --no-sandbox --ozone-platform=x11`; do not add `--no-sandbox` to an
|
|
63
|
+
installed application's launcher.
|
|
64
|
+
|
|
65
|
+
Forge uses ASAR and the host Electron/Chromium runtime. Run `desktop:package`
|
|
66
|
+
for an unpacked app and `desktop:make` for the current host's `.deb`, `.msi`, or
|
|
67
|
+
`.dmg`; installers are not cross-built. Signing and notarization are optional
|
|
68
|
+
until credentials are supplied.
|
|
69
|
+
|
|
70
|
+
Node 22 or 24 LTS is recommended for packaging. Node 26 is supported, but
|
|
71
|
+
Electron Packager's current ZIP extraction path is extremely slow there;
|
|
72
|
+
generated Desktop manifests declare `node: ">=22"`.
|
|
73
|
+
|
|
74
|
+
References: [security checklist](https://www.electronjs.org/docs/latest/tutorial/security),
|
|
75
|
+
[protocol API](https://www.electronjs.org/docs/latest/api/protocol),
|
|
76
|
+
[Electron Forge](https://www.electronjs.org/docs/latest/tutorial/forge-overview),
|
|
77
|
+
[Menu](https://www.electronjs.org/docs/latest/api/menu),
|
|
78
|
+
[Tray](https://www.electronjs.org/docs/latest/api/tray), and
|
|
79
|
+
[Notification](https://www.electronjs.org/docs/latest/api/notification).
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cossackframework/desktop",
|
|
3
|
+
"version": "0.8.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Secure Electron desktop runtime for Cossack applications",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/cossackframework/cossack",
|
|
10
|
+
"directory": "packages/desktop"
|
|
11
|
+
},
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"cossack-desktop": "./dist/dev.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"docs",
|
|
26
|
+
"README.md"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@cossackframework/framework": "0.8.2"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"electron": ">=37.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^22.19.0",
|
|
39
|
+
"electron": "^43.3.0",
|
|
40
|
+
"typescript": "^7.0.2",
|
|
41
|
+
"vite": "^8.1.4",
|
|
42
|
+
"vitest": "^4.1.10"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "vite build && tsc -p tsconfig.declarations.json && chmod +x dist/dev.js",
|
|
46
|
+
"test": "vitest --run"
|
|
47
|
+
}
|
|
48
|
+
}
|