@cossackframework/desktop 0.8.2 → 0.8.3

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.js CHANGED
@@ -1,26 +1,26 @@
1
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";
2
+ import { readFile as t, realpath as n, stat as r } from "node:fs/promises";
3
+ import i from "node:path";
4
+ import a from "electron";
5
5
  //#region src/error.ts
6
- var a = class extends Error {
6
+ var o = class extends Error {
7
7
  constructor() {
8
8
  super("@cossackframework/desktop can only create applications and shells in the Electron main process."), this.name = "DesktopUnavailableError";
9
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 = {
10
+ }, { app: s, BrowserWindow: c, dialog: l, Menu: u, nativeImage: d, Notification: f, protocol: p, shell: m, Tray: h } = typeof a == "object" && a ? a : {}, g = {
11
11
  name: "electron",
12
12
  supportedTransports: ["http"],
13
13
  getClientMetadata: () => ({
14
14
  platform: "desktop",
15
15
  adapter: "electron"
16
16
  })
17
- }, g = "cossack", _ = "cossack://app", v = !1, y, b = 0;
18
- function x(e) {
17
+ }, _ = "cossack", v = "cossack://app", y = !1, b, x = 0;
18
+ function S(e) {
19
19
  process.env.COSSACK_DESKTOP_DEBUG === "1" && console.error(`[Cossack Desktop] ${e}`);
20
20
  }
21
- function S() {
22
- v || f?.registerSchemesAsPrivileged && (f.registerSchemesAsPrivileged([{
23
- scheme: g,
21
+ function C() {
22
+ y || p?.registerSchemesAsPrivileged && (p.registerSchemesAsPrivileged([{
23
+ scheme: _,
24
24
  privileges: {
25
25
  standard: !0,
26
26
  secure: !0,
@@ -28,13 +28,13 @@ function S() {
28
28
  corsEnabled: !1,
29
29
  stream: !0
30
30
  }
31
- }]), v = !0);
31
+ }]), y = !0);
32
32
  }
33
- S();
34
- function C() {
35
- if (!process.versions.electron || process.type === "renderer" || !o || !s) throw new a();
33
+ C();
34
+ function w() {
35
+ if (!process.versions.electron || process.type === "renderer" || !s || !c) throw new o();
36
36
  }
37
- function w(t, n) {
37
+ function T(t, n) {
38
38
  if (process.platform !== "linux") return;
39
39
  let r = `org.freedesktop.StatusNotifierItem-${process.pid}-${n}`;
40
40
  setTimeout(() => {
@@ -47,29 +47,29 @@ function w(t, n) {
47
47
  "org.kde.StatusNotifierWatcher.RegisterStatusNotifierItem",
48
48
  `string:${r}`
49
49
  ], { timeout: 2e3 }, (e) => {
50
- e && x(`could not repair Linux tray registration: ${e.message}`);
50
+ e && S(`could not repair Linux tray registration: ${e.message}`);
51
51
  });
52
52
  }, 4e3).unref();
53
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
54
  function E(e) {
60
- C();
61
- let t = e.window ?? s.getFocusedWindow() ?? s.getAllWindows()[0];
62
- if (!t) throw new a();
55
+ w();
56
+ let t = ++x, n = e.guid ? new h(e.image, e.guid) : new h(e.image);
57
+ return e.toolTip && n.setToolTip(e.toolTip), e.menu !== void 0 && n.setContextMenu(e.menu), T(n, t), n;
58
+ }
59
+ function D(e) {
60
+ w();
61
+ let t = e.window ?? c.getFocusedWindow() ?? c.getAllWindows()[0];
62
+ if (!t) throw new o();
63
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 = () => {
64
+ let n = !1, r = !1, i = !1, a = () => {
65
65
  r = !0;
66
66
  }, u = () => {
67
- r || (r = !0, (e.onQuit ?? (() => o.quit()))());
67
+ r || (r = !0, (e.onQuit ?? (() => s.quit()))());
68
68
  }, d = {
69
69
  behavior: e.behavior,
70
70
  quit: u,
71
71
  dispose() {
72
- n || (n = !0, t.removeListener("close", f), o.removeListener("before-quit", l));
72
+ n || (n = !0, t.removeListener("close", f), s.removeListener("before-quit", a));
73
73
  }
74
74
  }, f = (a) => {
75
75
  if (n || r) return;
@@ -83,11 +83,11 @@ function E(e) {
83
83
  }
84
84
  if (i) return;
85
85
  i = !0;
86
- let s = o.getName();
87
- c.showMessageBox(t, {
86
+ let o = s.getName();
87
+ l.showMessageBox(t, {
88
88
  type: "question",
89
- title: s,
90
- message: `Quit ${s}?`,
89
+ title: o,
90
+ message: `Quit ${o}?`,
91
91
  buttons: ["Quit", "Cancel"],
92
92
  defaultId: 1,
93
93
  cancelId: 1,
@@ -101,9 +101,9 @@ function E(e) {
101
101
  i = !1;
102
102
  });
103
103
  };
104
- return t.on("close", f), o.on("before-quit", l), d;
104
+ return t.on("close", f), s.on("before-quit", a), d;
105
105
  }
106
- function D(e) {
106
+ function O(e) {
107
107
  let t;
108
108
  try {
109
109
  t = decodeURIComponent(e);
@@ -114,12 +114,12 @@ function D(e) {
114
114
  if (t.split("/").includes("..")) throw URIError("Desktop URL traversal is not allowed.");
115
115
  return t.replace(/^\/+/, "");
116
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;
117
+ function k(e, t) {
118
+ let n = i.resolve(e), r = i.resolve(n, t || "__cossack_ssr__"), a = i.relative(n, r);
119
+ if (a.startsWith("..") || i.isAbsolute(a)) throw URIError("Desktop URL traversal is not allowed.");
120
+ return r;
121
121
  }
122
- var k = {
122
+ var A = {
123
123
  ".css": "text/css; charset=utf-8",
124
124
  ".gif": "image/gif",
125
125
  ".html": "text/html; charset=utf-8",
@@ -138,32 +138,36 @@ var k = {
138
138
  ".woff": "font/woff",
139
139
  ".woff2": "font/woff2"
140
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);
141
+ async function j(e, a) {
142
+ if (a.method !== "GET" && a.method !== "HEAD") return;
143
+ let o = O(new URL(a.url).pathname);
144
+ if (!o) return;
145
+ let s = k(e, o);
146
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)
147
+ let [o, c] = await Promise.all([n(e), n(s)]), l = i.relative(o, c);
148
+ if (l === ".." || l.startsWith(`..${i.sep}`) || i.isAbsolute(l)) return;
149
+ let u = await r(c);
150
+ if (!u.isFile()) return;
151
+ let d = new Headers({
152
+ "content-type": A[i.extname(s).toLowerCase()] ?? "application/octet-stream",
153
+ "content-length": String(u.size)
152
154
  });
153
- return new Response(i.method === "HEAD" ? null : await t(o), { headers: a });
155
+ return new Response(a.method === "HEAD" ? null : await t(c), { headers: d });
154
156
  } catch (e) {
155
157
  if (e.code === "ENOENT" || e.code === "ENOTDIR") return;
156
158
  throw e;
157
159
  }
158
160
  }
159
- function j(e) {
161
+ function M(e) {
160
162
  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);
163
+ n.searchParams.delete("__cossack_window");
164
+ let r = new Request(n, e);
165
+ return e.headers.get("origin") === v && r.headers.set("origin", n.origin), r;
162
166
  }
163
- function M(e) {
164
- return e.protocol === `${g}:` && e.hostname === "app" && e.username === "" && e.password === "" && e.port === "";
167
+ function N(e) {
168
+ return e.protocol === `${_}:` && e.hostname === "app" && e.username === "" && e.password === "" && e.port === "";
165
169
  }
166
- function N(e = {}) {
170
+ function P(e = {}) {
167
171
  let { webPreferences: t, ...n } = e;
168
172
  return {
169
173
  width: 1e3,
@@ -184,37 +188,37 @@ function N(e = {}) {
184
188
  }
185
189
  };
186
190
  }
187
- function P(e) {
191
+ function F(e) {
188
192
  e.webContents.on("will-navigate", (e, t) => {
189
193
  try {
190
194
  let n = new URL(t);
191
- M(n) || (e.preventDefault(), (n.protocol === "https:" || n.protocol === "http:") && p.openExternal(n.href));
195
+ N(n) || (e.preventDefault(), (n.protocol === "https:" || n.protocol === "http:") && m.openExternal(n.href));
192
196
  } catch {
193
197
  e.preventDefault();
194
198
  }
195
199
  }), e.webContents.setWindowOpenHandler(({ url: e }) => {
196
200
  try {
197
201
  let t = new URL(e);
198
- (t.protocol === "https:" || t.protocol === "http:") && p.openExternal(t.href);
202
+ (t.protocol === "https:" || t.protocol === "http:") && m.openExternal(t.href);
199
203
  } catch {}
200
204
  return { action: "deny" };
201
205
  });
202
206
  }
203
- function F(e = {}) {
204
- C();
205
- let t = e.window ?? s.getFocusedWindow() ?? s.getAllWindows()[0];
206
- if (!t) throw new a();
207
+ function I(e = {}) {
208
+ w();
209
+ let t = e.window ?? c.getFocusedWindow() ?? c.getAllWindows()[0];
210
+ if (!t) throw new o();
207
211
  return {
208
- app: o,
212
+ app: s,
209
213
  window: t,
210
- Menu: l,
211
- Tray: m,
212
- dialog: c,
213
- Notification: d,
214
- nativeImage: u,
215
- shell: p,
214
+ Menu: u,
215
+ Tray: h,
216
+ dialog: l,
217
+ Notification: f,
218
+ nativeImage: d,
219
+ shell: m,
216
220
  get dock() {
217
- return o.dock;
221
+ return s.dock;
218
222
  },
219
223
  show() {
220
224
  t.show();
@@ -226,59 +230,61 @@ function F(e = {}) {
226
230
  t.focus();
227
231
  },
228
232
  quit() {
229
- o.quit();
233
+ s.quit();
230
234
  },
231
235
  setBadge(e) {
232
- return o.setBadgeCount(e === null ? 0 : typeof e == "number" ? e : Number(e) || 0);
236
+ return s.setBadgeCount(e === null ? 0 : typeof e == "number" ? e : Number(e) || 0);
233
237
  },
234
238
  setOverlayIcon(e, n = "") {
235
239
  t.setOverlayIcon(e, n);
236
240
  }
237
241
  };
238
242
  }
239
- async function I(e) {
240
- if (C(), x("initializing Electron application"), !e.identifier.trim()) throw TypeError("Desktop identifier is required.");
243
+ async function L(e) {
244
+ if (w(), S("initializing Electron application"), !e.identifier.trim()) throw TypeError("Desktop identifier is required.");
241
245
  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");
246
+ let t = i.resolve(e.assetsRoot), n = `/${(e.startPath ?? "/").replace(/^\/+/, "")}`, r = !1, a;
247
+ s.setName(e.productName), process.platform === "linux" && s.commandLine.appendSwitch("class", e.identifier), process.platform === "win32" && s.setAppUserModelId(e.identifier), S("requesting single-instance lock");
248
+ let o = s.requestSingleInstanceLock();
249
+ if (S(`single-instance lock ${o ? "acquired" : "unavailable"}`), !o) throw s.quit(), Error(`[Cossack] Another ${e.productName} instance is already running.`);
250
+ await s.whenReady(), S("Electron app is ready");
247
251
  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({
252
+ let { path: r = n, ...i } = t, a = new URL(r, `${v}/`);
253
+ if (!N(a)) throw TypeError("Desktop window paths must stay within cossack://app.");
254
+ let o = new c(P({
251
255
  ...e.window,
252
256
  ...i
253
257
  }));
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;
258
+ return S(`created BrowserWindow ${o.id}`), F(o), a.searchParams.set("__cossack_window", String(o.id)), o.loadURL(a.href).then(() => S(`loaded ${a.href}`), (e) => console.error(`[Cossack Desktop] Failed to load ${a.href}`, e)), o.once("ready-to-show", () => o.show()), o;
255
259
  };
256
- await f.handle(g, async (n) => {
257
- x(`handling ${n.method} ${n.url}`);
260
+ await p.handle(_, async (n) => {
261
+ S(`handling ${n.method} ${n.url}`);
258
262
  let r, i;
259
263
  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
+ if (r = new URL(n.url), !N(r)) return new Response("Forbidden Desktop authority", { status: 403 });
265
+ let e = n.headers.get("origin");
266
+ if (e && e !== v) return new Response("Forbidden Desktop origin", { status: 403 });
267
+ O(r.pathname);
268
+ let t = n.headers.get("referer");
269
+ if (i = new URL(t || n.url), !N(i)) return new Response("Forbidden Desktop authority", { status: 403 });
264
270
  } catch {
265
271
  return new Response("Invalid Desktop URL", { status: 400 });
266
272
  }
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);
273
+ let o = i.searchParams.get("__cossack_window"), s = o ? c.fromId(Number(o)) ?? a : a, l = s ? I({ window: s }) : void 0, u = await j(t, n);
268
274
  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), {
275
+ let d = { fetch: async (e) => await j(t, e) ?? new Response("Not found", { status: 404 }) };
276
+ return e.fetch(M(n), {
271
277
  ...e.env ?? {},
272
278
  ASSETS: d,
273
279
  COSSACK_DESKTOP: l
274
280
  });
275
- }), x("registered cossack:// protocol handler"), a = l();
276
- let u = F({ window: a }), d = {
277
- app: o,
281
+ }), S("registered cossack:// protocol handler"), a = l();
282
+ let u = I({ window: a }), d = {
283
+ app: s,
278
284
  mainWindow: a,
279
285
  shell: u,
280
286
  get quitting() {
281
- return i;
287
+ return r;
282
288
  },
283
289
  createWindow: l,
284
290
  show() {
@@ -288,21 +294,21 @@ async function I(e) {
288
294
  a.hide();
289
295
  },
290
296
  quit() {
291
- i = !0, o.quit();
297
+ r = !0, s.quit();
292
298
  }
293
299
  };
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
+ return s.on("before-quit", () => {
301
+ r = !0;
302
+ }), s.on("activate", () => {
303
+ c.getAllWindows().length === 0 ? a = l() : d.show();
304
+ }), s.on("second-instance", () => d.show()), process.on("message", (e) => {
305
+ if (e?.type === "cossack:renderer-reload") for (let e of c.getAllWindows()) e.webContents.reloadIgnoringCache();
300
306
  }), d;
301
307
  }
302
- function L(e) {
303
- return C(), y ??= I(e).catch((e) => {
304
- throw y = void 0, e;
308
+ function R(e) {
309
+ return w(), b ??= L(e).catch((e) => {
310
+ throw b = void 0, e;
305
311
  });
306
312
  }
307
313
  //#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 };
314
+ export { c as BrowserWindow, o as DesktopUnavailableError, u as Menu, f as Notification, h as Tray, s as app, D as configureDesktopClose, R as createDesktopApp, I as createDesktopShell, E as createDesktopTray, l as dialog, g as electronRuntimeAdapter, m as electronShell, m as shell, d as nativeImage, p as protocol };
package/docs/guide.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Cossack Desktop guide
2
2
 
3
- Add the Electron side target to a Cloudflare, Node, or Deno web application:
3
+ Add an Electron target to a Cloudflare, Node.js, or Deno web application:
4
4
 
5
5
  ```sh
6
6
  cossack add desktop
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cossackframework/desktop",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "type": "module",
5
5
  "description": "Secure Electron desktop runtime for Cossack applications",
6
6
  "license": "MIT",
@@ -29,17 +29,17 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@cossackframework/framework": "0.8.2"
32
+ "@cossackframework/framework": "0.8.3"
33
33
  },
34
34
  "peerDependencies": {
35
- "electron": ">=37.0.0"
35
+ "electron": ">=43.5.0"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/node": "^22.19.0",
39
- "electron": "^43.3.0",
39
+ "electron": "^43.5.0",
40
40
  "typescript": "^7.0.2",
41
41
  "vite": "^8.1.4",
42
- "vitest": "^4.1.10"
42
+ "vitest": "^4.1.11"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "vite build && tsc -p tsconfig.declarations.json && chmod +x dist/dev.js",