@cossackframework/desktop 0.8.3 → 0.9.1
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/README.md +85 -17
- package/dist/dev-signals.d.ts +0 -1
- package/dist/dev.js +34 -79
- package/dist/development-assets.d.ts +7 -0
- package/dist/development-error.d.ts +2 -0
- package/dist/development-response.d.ts +2 -0
- package/dist/development-runtime.d.ts +1 -0
- package/dist/development-runtime.js +196 -0
- package/dist/index.d.ts +36 -1
- package/dist/index.js +2 -314
- package/dist/request-security.d.ts +9 -0
- package/dist/src-LxY0vJPo.js +390 -0
- package/docs/guide.md +37 -23
- package/package.json +13 -6
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { randomUUID as e } from "node:crypto";
|
|
2
|
+
import { execFile as t } from "node:child_process";
|
|
3
|
+
import { readFile as n, realpath as r, stat as i } from "node:fs/promises";
|
|
4
|
+
import a from "node:path";
|
|
5
|
+
import o from "electron";
|
|
6
|
+
var s = "x-cossack-desktop-owner", c = "x-cossack-desktop-token";
|
|
7
|
+
function l(e) {
|
|
8
|
+
return e.protocol === "cossack:" && e.hostname === "app" && e.username === "" && e.password === "" && e.port === "";
|
|
9
|
+
}
|
|
10
|
+
function u(e, t, n) {
|
|
11
|
+
if (!t || e.webContentsId === void 0) return;
|
|
12
|
+
try {
|
|
13
|
+
if (!l(new URL(e.url))) return;
|
|
14
|
+
} catch {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (!(e.resourceType === "mainFrame" && e.method === "GET" && e.initiatorOrigin === void 0) && e.initiatorOrigin !== "cossack://app") return;
|
|
18
|
+
let r = new Headers(e.requestHeaders), i = r.get("origin");
|
|
19
|
+
if (!(i !== null && i !== "cossack://app")) return r.set(s, String(e.webContentsId)), r.set(c, n), Object.fromEntries(r);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region ../core/dist/shared/static-assets.js
|
|
23
|
+
var d = "assets";
|
|
24
|
+
Object.freeze(["/*", `!/${d}/*`]);
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/error.ts
|
|
27
|
+
var f = class extends Error {
|
|
28
|
+
constructor() {
|
|
29
|
+
super("@cossackframework/desktop can only create applications and shells in the Electron main process."), this.name = "DesktopUnavailableError";
|
|
30
|
+
}
|
|
31
|
+
}, p = typeof o == "object" && o ? o : {}, { app: m, BrowserWindow: h, dialog: g, Menu: _, nativeImage: v, Notification: y, protocol: b, shell: x, Tray: S } = p, C = {
|
|
32
|
+
name: "electron",
|
|
33
|
+
supportedTransports: ["http"],
|
|
34
|
+
getClientMetadata: () => ({
|
|
35
|
+
platform: "desktop",
|
|
36
|
+
adapter: "electron"
|
|
37
|
+
})
|
|
38
|
+
}, w = "cossack", T = "cossack://app", E = !1, D, O = 0;
|
|
39
|
+
function k(e) {
|
|
40
|
+
process.env.COSSACK_DESKTOP_DEBUG === "1" && console.error(`[Cossack Desktop] ${e}`);
|
|
41
|
+
}
|
|
42
|
+
function A() {
|
|
43
|
+
E || b?.registerSchemesAsPrivileged && (b.registerSchemesAsPrivileged([{
|
|
44
|
+
scheme: w,
|
|
45
|
+
privileges: {
|
|
46
|
+
standard: !0,
|
|
47
|
+
secure: !0,
|
|
48
|
+
supportFetchAPI: !0,
|
|
49
|
+
corsEnabled: !0,
|
|
50
|
+
stream: !0
|
|
51
|
+
}
|
|
52
|
+
}]), E = !0);
|
|
53
|
+
}
|
|
54
|
+
A();
|
|
55
|
+
function j() {
|
|
56
|
+
if (!process.versions.electron || process.type === "renderer" || !m || !h) throw new f();
|
|
57
|
+
let [e, t] = process.versions.electron.split(".").map(Number);
|
|
58
|
+
if (e < 43 || e === 43 && t < 7 || e === 44 && t < 4) throw Error("Cossack Desktop requires Electron 43.7+ or 44.4+ for native request initiator verification.");
|
|
59
|
+
}
|
|
60
|
+
function M(e, n) {
|
|
61
|
+
if (process.platform !== "linux") return;
|
|
62
|
+
let r = `org.freedesktop.StatusNotifierItem-${process.pid}-${n}`;
|
|
63
|
+
setTimeout(() => {
|
|
64
|
+
e.isDestroyed() || t("dbus-send", [
|
|
65
|
+
"--session",
|
|
66
|
+
"--print-reply",
|
|
67
|
+
"--type=method_call",
|
|
68
|
+
"--dest=org.kde.StatusNotifierWatcher",
|
|
69
|
+
"/StatusNotifierWatcher",
|
|
70
|
+
"org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem",
|
|
71
|
+
`string:${r}`
|
|
72
|
+
], { timeout: 2e3 }, (e) => {
|
|
73
|
+
e && k(`could not repair Linux tray registration: ${e.message}`);
|
|
74
|
+
});
|
|
75
|
+
}, 4e3).unref();
|
|
76
|
+
}
|
|
77
|
+
function N(e) {
|
|
78
|
+
j();
|
|
79
|
+
let t = ++O, n = e.guid ? new S(e.image, e.guid) : new S(e.image);
|
|
80
|
+
return e.toolTip && n.setToolTip(e.toolTip), e.menu !== void 0 && n.setContextMenu(e.menu), M(n, t), n;
|
|
81
|
+
}
|
|
82
|
+
function P(e) {
|
|
83
|
+
j();
|
|
84
|
+
let t = e.window ?? h.getFocusedWindow() ?? h.getAllWindows()[0];
|
|
85
|
+
if (!t) throw new f();
|
|
86
|
+
if (e.behavior === "hide-to-tray" && !e.tray) throw TypeError("Desktop hide-to-tray behavior requires a tray.");
|
|
87
|
+
let n = !1, r = !1, i = !1, a = () => {
|
|
88
|
+
r = !0;
|
|
89
|
+
}, o = () => {
|
|
90
|
+
r || (r = !0, (e.onQuit ?? (() => m.quit()))());
|
|
91
|
+
}, s = {
|
|
92
|
+
behavior: e.behavior,
|
|
93
|
+
quit: o,
|
|
94
|
+
dispose() {
|
|
95
|
+
n || (n = !0, t.removeListener("close", c), m.removeListener("before-quit", a));
|
|
96
|
+
}
|
|
97
|
+
}, c = (a) => {
|
|
98
|
+
if (n || r) return;
|
|
99
|
+
if (a.preventDefault(), e.behavior === "quit") {
|
|
100
|
+
o();
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (e.behavior === "hide-to-tray") {
|
|
104
|
+
e.tray && !e.tray.isDestroyed() ? t.hide() : o();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (i) return;
|
|
108
|
+
i = !0;
|
|
109
|
+
let s = m.getName();
|
|
110
|
+
g.showMessageBox(t, {
|
|
111
|
+
type: "question",
|
|
112
|
+
title: s,
|
|
113
|
+
message: `Quit ${s}?`,
|
|
114
|
+
buttons: ["Quit", "Cancel"],
|
|
115
|
+
defaultId: 1,
|
|
116
|
+
cancelId: 1,
|
|
117
|
+
noLink: !0,
|
|
118
|
+
...e.confirmation
|
|
119
|
+
}).then(({ response: t }) => {
|
|
120
|
+
t === (e.confirmResponse ?? 0) && o();
|
|
121
|
+
}).catch((e) => {
|
|
122
|
+
console.error("[Cossack Desktop] Could not show the close confirmation.", e);
|
|
123
|
+
}).finally(() => {
|
|
124
|
+
i = !1;
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
return t.on("close", c), m.on("before-quit", a), s;
|
|
128
|
+
}
|
|
129
|
+
function F(e) {
|
|
130
|
+
let t;
|
|
131
|
+
try {
|
|
132
|
+
t = decodeURIComponent(e);
|
|
133
|
+
} catch {
|
|
134
|
+
throw URIError("Malformed Desktop URL path.");
|
|
135
|
+
}
|
|
136
|
+
if (t.includes("\0") || t.includes("\\")) throw URIError("Unsafe Desktop URL path.");
|
|
137
|
+
if (t.split("/").includes("..")) throw URIError("Desktop URL traversal is not allowed.");
|
|
138
|
+
return t.replace(/^\/+/, "");
|
|
139
|
+
}
|
|
140
|
+
function I(e, t) {
|
|
141
|
+
let n = a.resolve(e), r = a.resolve(n, t || "__cossack_ssr__"), i = a.relative(n, r);
|
|
142
|
+
if (i.startsWith("..") || a.isAbsolute(i)) throw URIError("Desktop URL traversal is not allowed.");
|
|
143
|
+
return r;
|
|
144
|
+
}
|
|
145
|
+
var L = {
|
|
146
|
+
".css": "text/css; charset=utf-8",
|
|
147
|
+
".gif": "image/gif",
|
|
148
|
+
".html": "text/html; charset=utf-8",
|
|
149
|
+
".ico": "image/x-icon",
|
|
150
|
+
".jpeg": "image/jpeg",
|
|
151
|
+
".jpg": "image/jpeg",
|
|
152
|
+
".js": "text/javascript; charset=utf-8",
|
|
153
|
+
".json": "application/json; charset=utf-8",
|
|
154
|
+
".map": "application/json; charset=utf-8",
|
|
155
|
+
".mjs": "text/javascript; charset=utf-8",
|
|
156
|
+
".png": "image/png",
|
|
157
|
+
".svg": "image/svg+xml; charset=utf-8",
|
|
158
|
+
".txt": "text/plain; charset=utf-8",
|
|
159
|
+
".wasm": "application/wasm",
|
|
160
|
+
".webp": "image/webp",
|
|
161
|
+
".woff": "font/woff",
|
|
162
|
+
".woff2": "font/woff2"
|
|
163
|
+
}, R = `${d}/`, z = /\.[a-zA-Z0-9_-]{8,}\.[^/]+$/;
|
|
164
|
+
function B(e) {
|
|
165
|
+
return e.method !== "GET" && e.method !== "HEAD" ? !1 : F(new URL(e.url).pathname).startsWith(R);
|
|
166
|
+
}
|
|
167
|
+
function V(e) {
|
|
168
|
+
return e.startsWith(R) && z.test(e) ? "public, max-age=31536000, immutable" : "public, max-age=0, must-revalidate";
|
|
169
|
+
}
|
|
170
|
+
async function H(e, t) {
|
|
171
|
+
if (t.method !== "GET" && t.method !== "HEAD") return;
|
|
172
|
+
let o = F(new URL(t.url).pathname);
|
|
173
|
+
if (!o) return;
|
|
174
|
+
let s = I(e, o);
|
|
175
|
+
try {
|
|
176
|
+
let [c, l] = await Promise.all([r(e), r(s)]), u = a.relative(c, l);
|
|
177
|
+
if (u === ".." || u.startsWith(`..${a.sep}`) || a.isAbsolute(u)) return;
|
|
178
|
+
let d = await i(l);
|
|
179
|
+
if (!d.isFile()) return;
|
|
180
|
+
let f = new Headers({
|
|
181
|
+
"content-type": L[a.extname(s).toLowerCase()] ?? "application/octet-stream",
|
|
182
|
+
"content-length": String(d.size),
|
|
183
|
+
"cache-control": V(o)
|
|
184
|
+
});
|
|
185
|
+
return new Response(t.method === "HEAD" ? null : await n(l), { headers: f });
|
|
186
|
+
} catch (e) {
|
|
187
|
+
if (e.code === "ENOENT" || e.code === "ENOTDIR") return;
|
|
188
|
+
throw e;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function U(e) {
|
|
192
|
+
let t = new URL(e.url), n = new URL(`${t.pathname}${t.search}`, "https://app");
|
|
193
|
+
n.searchParams.delete("__cossack_window");
|
|
194
|
+
let r = new Request(n, e);
|
|
195
|
+
return r.headers.delete(s), r.headers.delete(c), r.headers.set("origin", n.origin), r;
|
|
196
|
+
}
|
|
197
|
+
var W = /* @__PURE__ */ new Set([s, c]);
|
|
198
|
+
function G(e) {
|
|
199
|
+
let t = Object.entries(e), n = t.filter(([e]) => !W.has(e.toLowerCase()));
|
|
200
|
+
return n.length === t.length ? {} : { requestHeaders: Object.fromEntries(n) };
|
|
201
|
+
}
|
|
202
|
+
function K(e = {}) {
|
|
203
|
+
let { webPreferences: t, ...n } = e;
|
|
204
|
+
return {
|
|
205
|
+
width: 1e3,
|
|
206
|
+
height: 720,
|
|
207
|
+
show: !1,
|
|
208
|
+
...n,
|
|
209
|
+
webPreferences: {
|
|
210
|
+
...t,
|
|
211
|
+
contextIsolation: !0,
|
|
212
|
+
sandbox: !0,
|
|
213
|
+
nodeIntegration: !1,
|
|
214
|
+
nodeIntegrationInWorker: !1,
|
|
215
|
+
nodeIntegrationInSubFrames: !1,
|
|
216
|
+
webSecurity: !0,
|
|
217
|
+
allowRunningInsecureContent: !1,
|
|
218
|
+
webviewTag: !1,
|
|
219
|
+
preload: void 0
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function q(e) {
|
|
224
|
+
e.webContents.on("will-navigate", (e, t) => {
|
|
225
|
+
try {
|
|
226
|
+
let n = new URL(t);
|
|
227
|
+
l(n) || (e.preventDefault(), (n.protocol === "https:" || n.protocol === "http:") && x.openExternal(n.href));
|
|
228
|
+
} catch {
|
|
229
|
+
e.preventDefault();
|
|
230
|
+
}
|
|
231
|
+
}), e.webContents.setWindowOpenHandler(({ url: e }) => {
|
|
232
|
+
try {
|
|
233
|
+
let t = new URL(e);
|
|
234
|
+
(t.protocol === "https:" || t.protocol === "http:") && x.openExternal(t.href);
|
|
235
|
+
} catch {}
|
|
236
|
+
return { action: "deny" };
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function J(e = {}) {
|
|
240
|
+
j();
|
|
241
|
+
let t = e.window ?? h.getFocusedWindow() ?? h.getAllWindows()[0];
|
|
242
|
+
if (!t) throw new f();
|
|
243
|
+
return Y(() => t);
|
|
244
|
+
}
|
|
245
|
+
function Y(e) {
|
|
246
|
+
return {
|
|
247
|
+
app: m,
|
|
248
|
+
get window() {
|
|
249
|
+
return e();
|
|
250
|
+
},
|
|
251
|
+
Menu: _,
|
|
252
|
+
Tray: S,
|
|
253
|
+
dialog: g,
|
|
254
|
+
Notification: y,
|
|
255
|
+
nativeImage: v,
|
|
256
|
+
shell: x,
|
|
257
|
+
get dock() {
|
|
258
|
+
return m.dock;
|
|
259
|
+
},
|
|
260
|
+
show() {
|
|
261
|
+
e().show();
|
|
262
|
+
},
|
|
263
|
+
hide() {
|
|
264
|
+
e().hide();
|
|
265
|
+
},
|
|
266
|
+
focus() {
|
|
267
|
+
e().focus();
|
|
268
|
+
},
|
|
269
|
+
quit() {
|
|
270
|
+
m.quit();
|
|
271
|
+
},
|
|
272
|
+
setBadge(e) {
|
|
273
|
+
return m.setBadgeCount(e === null ? 0 : typeof e == "number" ? e : Number(e) || 0);
|
|
274
|
+
},
|
|
275
|
+
setOverlayIcon(t, n = "") {
|
|
276
|
+
e().setOverlayIcon(t, n);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
async function X(t) {
|
|
281
|
+
if (j(), k("initializing Electron application"), !t.identifier.trim()) throw TypeError("Desktop identifier is required.");
|
|
282
|
+
if (!t.productName.trim()) throw TypeError("Desktop productName is required.");
|
|
283
|
+
let n = a.resolve(t.assetsRoot), r = `/${(t.startPath ?? "/").replace(/^\/+/, "")}`, i = !1, o, c = !1;
|
|
284
|
+
m.setName(t.productName), process.platform === "linux" && m.commandLine.appendSwitch("class", t.identifier), process.platform === "win32" && m.setAppUserModelId(t.identifier), k("requesting single-instance lock");
|
|
285
|
+
let d = m.requestSingleInstanceLock();
|
|
286
|
+
if (k(`single-instance lock ${d ? "acquired" : "unavailable"}`), !d) throw m.quit(), Error(`[Cossack] Another ${t.productName} instance is already running.`);
|
|
287
|
+
await m.whenReady(), k("Electron app is ready");
|
|
288
|
+
let f = p.session.fromPartition(`persist:cossack.${t.identifier}`), g = /* @__PURE__ */ new Map(), _ = e(), v = /* @__PURE__ */ new Set([...Z, ...t.permissions ?? []]), y = (e) => {
|
|
289
|
+
try {
|
|
290
|
+
return e !== void 0 && l(new URL(e));
|
|
291
|
+
} catch {
|
|
292
|
+
return !1;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
f.setPermissionRequestHandler((e, t, n, r) => {
|
|
296
|
+
let i = v.has(t) && g.has(e?.id ?? -1) && y(r?.requestingUrl ?? e?.getURL());
|
|
297
|
+
i || k(`denied permission ${t}`), n(i);
|
|
298
|
+
}), f.setPermissionCheckHandler((e, t, n) => v.has(t) && g.has(e?.id ?? -1) && y(n)), f.webRequest.onBeforeSendHeaders((e, t) => {
|
|
299
|
+
if (!e.url.startsWith(`${w}:`)) {
|
|
300
|
+
t(G(e.requestHeaders));
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
let n = u(e, g.has(e.webContentsId ?? -1), _);
|
|
304
|
+
n || k(`rejected ${e.resourceType} from ${e.initiatorOrigin ?? "browser"} (owned: ${g.has(e.webContentsId ?? -1)})`), t(n ? { requestHeaders: n } : { cancel: !0 });
|
|
305
|
+
});
|
|
306
|
+
let b = (e = {}) => {
|
|
307
|
+
let { path: n = r, ...i } = e, a = new URL(n, `${T}/`);
|
|
308
|
+
if (!l(a)) throw TypeError("Desktop window paths must stay within cossack://app.");
|
|
309
|
+
let o = K({
|
|
310
|
+
...t.window,
|
|
311
|
+
...i
|
|
312
|
+
}), s = new h({
|
|
313
|
+
...o,
|
|
314
|
+
webPreferences: {
|
|
315
|
+
...o.webPreferences,
|
|
316
|
+
session: f
|
|
317
|
+
}
|
|
318
|
+
}), c = s.webContents.id;
|
|
319
|
+
return g.set(c, s), s.once("closed", () => g.delete(c)), k(`created BrowserWindow ${s.id}`), q(s), s.loadURL(a.href).then(() => k(`loaded ${a.href}`), (e) => console.error(`[Cossack Desktop] Failed to load ${a.href}`, e)), s.once("ready-to-show", () => s.show()), s;
|
|
320
|
+
}, x = { fetch: async (e) => await H(n, e) ?? new Response("Not found", { status: 404 }) };
|
|
321
|
+
await f.protocol.handle(w, async (e) => {
|
|
322
|
+
k(`handling ${e.method} ${e.url}`);
|
|
323
|
+
let n;
|
|
324
|
+
try {
|
|
325
|
+
if (n = new URL(e.url), !l(n)) return new Response("Forbidden Desktop authority", { status: 403 });
|
|
326
|
+
let t = e.headers.get("origin");
|
|
327
|
+
if (t && t !== T) return new Response("Forbidden Desktop origin", { status: 403 });
|
|
328
|
+
F(n.pathname);
|
|
329
|
+
let r = e.headers.get("referer");
|
|
330
|
+
if (!l(new URL(r || e.url))) return new Response("Forbidden Desktop authority", { status: 403 });
|
|
331
|
+
} catch {
|
|
332
|
+
return new Response("Invalid Desktop URL", { status: 400 });
|
|
333
|
+
}
|
|
334
|
+
let r = e.headers.get(s), i = r === null ? void 0 : g.get(Number(r));
|
|
335
|
+
if (e.headers.get("x-cossack-desktop-token") !== _ || !i) return new Response("Unverified Desktop request", { status: 403 });
|
|
336
|
+
let a = J({ window: i }), o = U(e), c = t.assets ?? x;
|
|
337
|
+
if (B(o)) {
|
|
338
|
+
let e = await c.fetch(o);
|
|
339
|
+
if (e.status !== 404) return e;
|
|
340
|
+
await e.body?.cancel();
|
|
341
|
+
}
|
|
342
|
+
return t.fetch(o, {
|
|
343
|
+
...t.env ?? {},
|
|
344
|
+
ASSETS: c,
|
|
345
|
+
COSSACK_DESKTOP: a
|
|
346
|
+
});
|
|
347
|
+
}), k("registered cossack:// protocol handler");
|
|
348
|
+
let S = (e) => (o = e, c = !0, e.once("closed", () => {
|
|
349
|
+
if (o !== e) return;
|
|
350
|
+
let t = g.values().next().value;
|
|
351
|
+
t ? S(t) : c = !1;
|
|
352
|
+
}), e), C = () => c ? o : S(b());
|
|
353
|
+
S(b());
|
|
354
|
+
let E = {
|
|
355
|
+
app: m,
|
|
356
|
+
get mainWindow() {
|
|
357
|
+
return o;
|
|
358
|
+
},
|
|
359
|
+
shell: Y(() => o),
|
|
360
|
+
get quitting() {
|
|
361
|
+
return i;
|
|
362
|
+
},
|
|
363
|
+
createWindow: b,
|
|
364
|
+
show() {
|
|
365
|
+
let e = C();
|
|
366
|
+
e.show(), e.focus();
|
|
367
|
+
},
|
|
368
|
+
hide() {
|
|
369
|
+
c && o.hide();
|
|
370
|
+
},
|
|
371
|
+
quit() {
|
|
372
|
+
i = !0, m.quit();
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
return m.on("before-quit", () => {
|
|
376
|
+
i = !0;
|
|
377
|
+
}), m.on("activate", () => {
|
|
378
|
+
h.getAllWindows().length === 0 ? S(b()) : E.show();
|
|
379
|
+
}), m.on("second-instance", () => E.show()), process.on("message", (e) => {
|
|
380
|
+
if (e?.type === "cossack:renderer-reload") for (let e of h.getAllWindows()) e.webContents.reloadIgnoringCache();
|
|
381
|
+
}), E;
|
|
382
|
+
}
|
|
383
|
+
var Z = ["clipboard-sanitized-write", "fullscreen"];
|
|
384
|
+
function Q(e) {
|
|
385
|
+
return j(), D ??= X(e).catch((e) => {
|
|
386
|
+
throw D = void 0, e;
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
//#endregion
|
|
390
|
+
export { S as a, Q as c, g as d, C as f, f as g, b as h, y as i, J as l, v as m, Z as n, m as o, x as p, _ as r, P as s, h as t, N as u };
|
package/docs/guide.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Cossack Desktop guide
|
|
2
2
|
|
|
3
|
-
Add an Electron target to a Cloudflare
|
|
3
|
+
Add an Electron target to a Cloudflare or Node.js web application:
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
cossack add desktop
|
|
@@ -10,17 +10,31 @@ pnpm desktop:dev
|
|
|
10
10
|
|
|
11
11
|
The scaffold creates `src/desktop/index.ts`, `forge.config.ts`, platform icons,
|
|
12
12
|
and build/package/make scripts. The Electron process always runs on Node even
|
|
13
|
-
when the web adapter is
|
|
13
|
+
when the web adapter is Cloudflare. Bindings such as D1 do not exist in
|
|
14
14
|
Electron unless the application supplies an Electron-compatible equivalent.
|
|
15
15
|
|
|
16
16
|
`createDesktopApp()` registers and serves `cossack://app`, injects `ASSETS` and
|
|
17
17
|
a window-scoped `COSSACK_DESKTOP`, and starts a sandboxed, context-isolated
|
|
18
|
-
`BrowserWindow`.
|
|
19
|
-
|
|
18
|
+
`BrowserWindow`. It serves hashed `/assets/*` build output directly. Every
|
|
19
|
+
other file, including `public/`, reaches the application, so global and page
|
|
20
|
+
policies run first. `desktop:dev` applies those policies to `public/` files too,
|
|
21
|
+
while Vite answers its client and modules first. Use only HTTP-transport pages.
|
|
22
|
+
Native work belongs in the bootstrap or ordinary `@Server()` methods:
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
Keep the generated `frameworkApp`, `env` and `startDesktop(factory)` exports.
|
|
25
|
+
Use the supplied factory to create the application and return its Desktop result.
|
|
26
|
+
Put native menus, tray and close handlers inside that bootstrap. The development
|
|
27
|
+
runner calls it once. Only production calls it at module scope, guarded by
|
|
28
|
+
`if (!import.meta.env.DEV)`. Do not top-level `await` Desktop creation: Electron
|
|
29
|
+
must complete the first event-loop tick before it emits `ready`.
|
|
30
|
+
|
|
31
|
+
`desktop:dev` runs Vite inside Electron and keeps the renderer on `cossack://app`.
|
|
32
|
+
Page edits reload managed windows. Native bootstrap, Vite configuration and root `.env*`
|
|
33
|
+
changes restart Electron. Page syntax repair recovers the development window. Reloads
|
|
34
|
+
reset in-memory modules, so store durable application data explicitly. The Vite
|
|
35
|
+
asset listener never dispatches native application actions over loopback HTTP.
|
|
36
|
+
Keep `ssr.noExternal: true` limited to `mode === 'desktop' && command === 'build'`.
|
|
37
|
+
See the [bootstrap example](../README.md) when migrating an existing entry.
|
|
24
38
|
|
|
25
39
|
```ts
|
|
26
40
|
const shell = this.env.COSSACK_DESKTOP as DesktopShell;
|
|
@@ -30,46 +44,46 @@ const result = await shell.dialog.showMessageBox(shell.window, options);
|
|
|
30
44
|
Electron dialogs are asynchronous. Test `Notification.isSupported()` before
|
|
31
45
|
showing a native notification. Treat `app.dock`, Linux badges, Windows overlay
|
|
32
46
|
icons, taskbar buttons, menus, and trays as platform-native APIs rather than
|
|
33
|
-
cross-platform emulations. Keep a strong `Tray` reference
|
|
34
|
-
only after tray creation succeeds
|
|
35
|
-
Create trays with `createDesktopTray({ image, menu, toolTip })
|
|
47
|
+
cross-platform emulations. Keep a strong `Tray` reference. Intercept close
|
|
48
|
+
only after tray creation succeeds. Maintain an explicit quitting flag.
|
|
49
|
+
Create trays with `createDesktopTray({ image, menu, toolTip })`. It returns the
|
|
36
50
|
native Electron object and repairs Ubuntu AppIndicator's StatusNotifier
|
|
37
51
|
registration through the standard Linux `dbus-send` utility.
|
|
38
52
|
|
|
39
53
|
Configure window close handling with `configureDesktopClose()`. Its
|
|
40
54
|
`behavior` is one of `quit`, `hide-to-tray`, or `confirm-quit`. The hide policy
|
|
41
|
-
requires a live tray
|
|
42
|
-
confirmation policy uses an asynchronous native message box
|
|
43
|
-
duplicate close attempts while
|
|
55
|
+
requires a live tray. It quits safely if that tray is later destroyed. The
|
|
56
|
+
confirmation policy uses an asynchronous native message box. It ignores
|
|
57
|
+
duplicate close attempts while the message box is open. A safe OS policy is
|
|
44
58
|
`confirm-quit` on Linux and `hide-to-tray` on Windows/macOS only after tray
|
|
45
59
|
construction succeeds. Generated projects start with `quit`.
|
|
46
60
|
|
|
47
61
|
On Linux, the development runner inspects Electron's local SUID sandbox helper.
|
|
48
62
|
If the pnpm copy is not root-owned with mode `4755`, it warns and uses
|
|
49
63
|
Electron's testing-only `--no-sandbox` flag for `desktop:dev`. Packaged
|
|
50
|
-
applications never receive this flag
|
|
64
|
+
applications never receive this flag. DEB installation gives the helper its
|
|
51
65
|
required permissions.
|
|
52
66
|
|
|
53
|
-
On a Wayland session with XWayland available, `desktop:dev` selects X11
|
|
54
|
-
|
|
55
|
-
probe logs. Set `COSSACK_DESKTOP_OZONE_PLATFORM=wayland` to test native Wayland,
|
|
67
|
+
On a Wayland session with XWayland available, `desktop:dev` selects X11.
|
|
68
|
+
This avoids Chromium's Wayland/Vulkan incompatibility. The runner suppresses
|
|
69
|
+
nonfatal GPU probe logs. Set `COSSACK_DESKTOP_OZONE_PLATFORM=wayland` to test native Wayland,
|
|
56
70
|
or `COSSACK_DESKTOP_DEBUG=1` to retain Chromium diagnostics and enable Desktop
|
|
57
71
|
lifecycle tracing.
|
|
58
72
|
|
|
59
|
-
The generated DEB desktop entry includes `--ozone-platform=x11` for
|
|
73
|
+
The generated DEB desktop entry includes `--ozone-platform=x11` for
|
|
60
74
|
GNOME StatusNotifier tray integration. A binary launched directly from Forge's
|
|
61
75
|
unpacked `out/` directory does not use the desktop entry. Test that artifact as
|
|
62
|
-
`./your-app --no-sandbox --ozone-platform=x11
|
|
76
|
+
`./your-app --no-sandbox --ozone-platform=x11`. Do not add `--no-sandbox` to an
|
|
63
77
|
installed application's launcher.
|
|
64
78
|
|
|
65
79
|
Forge uses ASAR and the host Electron/Chromium runtime. Run `desktop:package`
|
|
66
80
|
for an unpacked app and `desktop:make` for the current host's `.deb`, `.msi`, or
|
|
67
|
-
`.dmg
|
|
81
|
+
`.dmg`. Installers are not cross-built. Signing and notarization are optional
|
|
68
82
|
until credentials are supplied.
|
|
69
83
|
|
|
70
|
-
Node 22 or 24 LTS
|
|
71
|
-
Electron Packager's current ZIP extraction path is
|
|
72
|
-
|
|
84
|
+
Use Node 22 or 24 LTS for packaging. Node 26 is supported, but
|
|
85
|
+
Electron Packager's current ZIP extraction path is slow there.
|
|
86
|
+
Generated Desktop manifests declare `node: ">=22"`.
|
|
73
87
|
|
|
74
88
|
References: [security checklist](https://www.electronjs.org/docs/latest/tutorial/security),
|
|
75
89
|
[protocol API](https://www.electronjs.org/docs/latest/api/protocol),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/desktop",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Secure Electron desktop runtime for Cossack applications",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,17 +29,24 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@cossackframework/
|
|
32
|
+
"@cossackframework/core": "0.9.1"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"electron": "
|
|
35
|
+
"electron": "^43.8.1 || >=44.4.0",
|
|
36
|
+
"vite": "^8.1.6"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@types/node": "^22.19.0",
|
|
39
|
-
"electron": "^43.
|
|
40
|
+
"electron": "^43.8.4",
|
|
41
|
+
"image-size": "^2.0.4",
|
|
40
42
|
"typescript": "^7.0.2",
|
|
41
|
-
"vite": "^8.1.
|
|
42
|
-
"vitest": "^4.1.
|
|
43
|
+
"vite": "^8.1.6",
|
|
44
|
+
"vitest": "^4.1.12"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"vite": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
43
50
|
},
|
|
44
51
|
"scripts": {
|
|
45
52
|
"build": "vite build && tsc -p tsconfig.declarations.json && chmod +x dist/dev.js",
|