@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 +112 -106
- package/docs/guide.md +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
import { execFile as e } from "node:child_process";
|
|
2
|
-
import { readFile as t,
|
|
3
|
-
import
|
|
4
|
-
import
|
|
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
|
|
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:
|
|
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
|
-
},
|
|
18
|
-
function
|
|
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
|
|
22
|
-
|
|
23
|
-
scheme:
|
|
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
|
-
}]),
|
|
31
|
+
}]), y = !0);
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
function
|
|
35
|
-
if (!process.versions.electron || process.type === "renderer" || !
|
|
33
|
+
C();
|
|
34
|
+
function w() {
|
|
35
|
+
if (!process.versions.electron || process.type === "renderer" || !s || !c) throw new o();
|
|
36
36
|
}
|
|
37
|
-
function
|
|
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 &&
|
|
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
|
-
|
|
61
|
-
let t = e.
|
|
62
|
-
|
|
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,
|
|
64
|
+
let n = !1, r = !1, i = !1, a = () => {
|
|
65
65
|
r = !0;
|
|
66
66
|
}, u = () => {
|
|
67
|
-
r || (r = !0, (e.onQuit ?? (() =>
|
|
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),
|
|
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
|
|
87
|
-
|
|
86
|
+
let o = s.getName();
|
|
87
|
+
l.showMessageBox(t, {
|
|
88
88
|
type: "question",
|
|
89
|
-
title:
|
|
90
|
-
message: `Quit ${
|
|
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),
|
|
104
|
+
return t.on("close", f), s.on("before-quit", a), d;
|
|
105
105
|
}
|
|
106
|
-
function
|
|
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
|
|
118
|
-
let n =
|
|
119
|
-
if (a.startsWith("..") ||
|
|
120
|
-
return
|
|
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
|
|
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
|
|
142
|
-
if (
|
|
143
|
-
let
|
|
144
|
-
if (!
|
|
145
|
-
let
|
|
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
|
|
148
|
-
if (
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
|
|
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(
|
|
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
|
|
161
|
+
function M(e) {
|
|
160
162
|
let t = new URL(e.url), n = new URL(`${t.pathname}${t.search}`, "https://app");
|
|
161
|
-
|
|
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
|
|
164
|
-
return e.protocol === `${
|
|
167
|
+
function N(e) {
|
|
168
|
+
return e.protocol === `${_}:` && e.hostname === "app" && e.username === "" && e.password === "" && e.port === "";
|
|
165
169
|
}
|
|
166
|
-
function
|
|
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
|
|
191
|
+
function F(e) {
|
|
188
192
|
e.webContents.on("will-navigate", (e, t) => {
|
|
189
193
|
try {
|
|
190
194
|
let n = new URL(t);
|
|
191
|
-
|
|
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:") &&
|
|
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
|
|
204
|
-
|
|
205
|
-
let t = e.window ??
|
|
206
|
-
if (!t) throw new
|
|
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:
|
|
212
|
+
app: s,
|
|
209
213
|
window: t,
|
|
210
|
-
Menu:
|
|
211
|
-
Tray:
|
|
212
|
-
dialog:
|
|
213
|
-
Notification:
|
|
214
|
-
nativeImage:
|
|
215
|
-
shell:
|
|
214
|
+
Menu: u,
|
|
215
|
+
Tray: h,
|
|
216
|
+
dialog: l,
|
|
217
|
+
Notification: f,
|
|
218
|
+
nativeImage: d,
|
|
219
|
+
shell: m,
|
|
216
220
|
get dock() {
|
|
217
|
-
return
|
|
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
|
-
|
|
233
|
+
s.quit();
|
|
230
234
|
},
|
|
231
235
|
setBadge(e) {
|
|
232
|
-
return
|
|
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
|
|
240
|
-
if (
|
|
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 =
|
|
243
|
-
|
|
244
|
-
let
|
|
245
|
-
if (
|
|
246
|
-
await
|
|
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 (!
|
|
250
|
-
let o = new
|
|
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
|
|
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
|
|
257
|
-
|
|
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), !
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
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"),
|
|
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
|
|
270
|
-
return e.fetch(
|
|
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
|
-
}),
|
|
276
|
-
let u =
|
|
277
|
-
app:
|
|
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
|
|
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
|
-
|
|
297
|
+
r = !0, s.quit();
|
|
292
298
|
}
|
|
293
299
|
};
|
|
294
|
-
return
|
|
295
|
-
|
|
296
|
-
}),
|
|
297
|
-
|
|
298
|
-
}),
|
|
299
|
-
if (e?.type === "cossack:renderer-reload") for (let e of
|
|
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
|
|
303
|
-
return
|
|
304
|
-
throw
|
|
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 {
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/desktop",
|
|
3
|
-
"version": "0.8.
|
|
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.
|
|
32
|
+
"@cossackframework/framework": "0.8.3"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
-
"electron": ">=
|
|
35
|
+
"electron": ">=43.5.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^22.19.0",
|
|
39
|
-
"electron": "^43.
|
|
39
|
+
"electron": "^43.5.0",
|
|
40
40
|
"typescript": "^7.0.2",
|
|
41
41
|
"vite": "^8.1.4",
|
|
42
|
-
"vitest": "^4.1.
|
|
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",
|