@moonbase.sh/electron 0.0.0 → 0.2.0
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 +40 -3
- package/dist/index.cjs +617 -0
- package/dist/index.d.cts +171 -0
- package/dist/index.d.ts +171 -0
- package/dist/index.js +588 -0
- package/dist/preload.cjs +89 -0
- package/dist/preload.d.cts +83 -0
- package/dist/preload.d.ts +83 -0
- package/dist/preload.js +63 -0
- package/dist/renderer.cjs +18 -0
- package/dist/renderer.d.cts +12 -0
- package/dist/renderer.d.ts +12 -0
- package/dist/renderer.js +0 -0
- package/dist/ui/index.html +18 -0
- package/dist/ui/overlay.html +13 -0
- package/dist/ui/overlay.js +34 -0
- package/dist/ui/ui.css +1241 -0
- package/dist/ui/ui.js +1053 -0
- package/dist/ui/window.js +29 -0
- package/dist/ui-preload.cjs +15 -0
- package/dist/vite.cjs +79 -0
- package/dist/vite.d.cts +24 -0
- package/dist/vite.d.ts +24 -0
- package/dist/vite.js +51 -0
- package/package.json +108 -4
- package/preload/package.json +5 -0
- package/renderer/package.json +5 -0
- package/vite/package.json +5 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/tsup@8.5.0_@microsoft+api-extractor@7.52.8_@types+node@18.19.64__jiti@2.7.0_postcss@8.5_fd9278b0af519e5ced4e697ff9c2675a/node_modules/tsup/assets/esm_shims.js
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { fileURLToPath } from "url";
|
|
4
|
+
var getFilename = () => fileURLToPath(import.meta.url);
|
|
5
|
+
var getDirname = () => path.dirname(getFilename());
|
|
6
|
+
var __dirname = /* @__PURE__ */ getDirname();
|
|
7
|
+
|
|
8
|
+
// src/index.ts
|
|
9
|
+
import { Buffer } from "buffer";
|
|
10
|
+
import fs from "fs";
|
|
11
|
+
import fsPromises from "fs/promises";
|
|
12
|
+
import path2 from "path";
|
|
13
|
+
import process from "process";
|
|
14
|
+
import { ActivationController, ActivationError, ReleaseNotCoveredError } from "@moonbase.sh/licensing";
|
|
15
|
+
import { app, BrowserWindow, clipboard, dialog, ipcMain, net, shell, WebContentsView } from "electron";
|
|
16
|
+
|
|
17
|
+
// package.json
|
|
18
|
+
var version = "0.2.0";
|
|
19
|
+
|
|
20
|
+
// src/download.ts
|
|
21
|
+
function downloadState(phase, rest = {}) {
|
|
22
|
+
return { phase, progress: null, path: null, error: null, releaseAccess: null, upgradeUrl: null, ...rest };
|
|
23
|
+
}
|
|
24
|
+
function fileSafe(name) {
|
|
25
|
+
const cleaned = name.replace(/[\u0000-\u001F\u007F\\/:*?"<>|]/g, "-").trim().replace(/^\.+|\.+$/g, "");
|
|
26
|
+
return cleaned || "license";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var WIDTH = 660;
|
|
31
|
+
var MOONBASE_SITE = "https://moonbase.sh";
|
|
32
|
+
var READ_ONLY = /* @__PURE__ */ new Set(["state", "device", "checkUpdate", "present"]);
|
|
33
|
+
function errorOutcome(err) {
|
|
34
|
+
if (err instanceof ActivationError)
|
|
35
|
+
return { ok: false, error: { kind: err.kind, message: err.message } };
|
|
36
|
+
const kind = err?.type;
|
|
37
|
+
return { ok: false, error: { kind: typeof kind === "string" ? kind : "internal", message: err instanceof Error ? err.message : String(err) } };
|
|
38
|
+
}
|
|
39
|
+
function uiFolder() {
|
|
40
|
+
return __dirname;
|
|
41
|
+
}
|
|
42
|
+
var UI_FILES = ["ui-preload.cjs", path2.join("ui", "index.html"), path2.join("ui", "overlay.html")];
|
|
43
|
+
function platformOs() {
|
|
44
|
+
switch (process.platform) {
|
|
45
|
+
case "darwin":
|
|
46
|
+
return "macos";
|
|
47
|
+
case "win32":
|
|
48
|
+
return "windows";
|
|
49
|
+
default:
|
|
50
|
+
return process.platform;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
var MoonbaseElectron = class extends ActivationController {
|
|
54
|
+
constructor(options) {
|
|
55
|
+
const ui = options.ui === false ? null : options.ui ?? {};
|
|
56
|
+
const productName = ui?.productName ?? app.getName();
|
|
57
|
+
super({
|
|
58
|
+
...options,
|
|
59
|
+
appVersion: options.appVersion ?? app.getVersion(),
|
|
60
|
+
licenseFile: options.licenseFile ?? path2.join(app.getPath("userData"), "license.mb"),
|
|
61
|
+
openUrl: options.openUrl ?? ((url) => shell.openExternal(url)),
|
|
62
|
+
// Ahead of the SDK's own token, so Moonbase knows the product uses this module.
|
|
63
|
+
clientInfo: [options.clientInfo, `@moonbase.sh/electron/${version}`].filter(Boolean).join(" "),
|
|
64
|
+
productName,
|
|
65
|
+
accountName: ui?.manufacturerName
|
|
66
|
+
});
|
|
67
|
+
this.subscribers = /* @__PURE__ */ new Set();
|
|
68
|
+
this.folder = uiFolder();
|
|
69
|
+
this.window = null;
|
|
70
|
+
this.overlay = null;
|
|
71
|
+
/** Waiting for a window to lay the overlay over, when the app has none. */
|
|
72
|
+
this.waitingForWindow = null;
|
|
73
|
+
this.pendingScreen = null;
|
|
74
|
+
this.lastLicensed = null;
|
|
75
|
+
this.download = downloadState("idle");
|
|
76
|
+
this.downloaded = null;
|
|
77
|
+
this.running = false;
|
|
78
|
+
this.onSubscribe = (event) => {
|
|
79
|
+
const contents = event.sender;
|
|
80
|
+
if (this.accessOf(contents) === "none" || this.subscribers.has(contents))
|
|
81
|
+
return;
|
|
82
|
+
this.subscribers.add(contents);
|
|
83
|
+
contents.once("destroyed", () => this.subscribers.delete(contents));
|
|
84
|
+
};
|
|
85
|
+
this.onWindowFocus = () => this.onFocusChanged(true);
|
|
86
|
+
this.onWindowBlur = () => this.onFocusChanged(false);
|
|
87
|
+
this.ui = ui;
|
|
88
|
+
this.rendererAccess = options.rendererAccess;
|
|
89
|
+
this.endpoint = options.endpoint;
|
|
90
|
+
this.subscribe((state) => this.afterStateChange(state));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Validate the stored license, start watching it, and answer the app's
|
|
94
|
+
* renderers. Call once, before or after the app is ready.
|
|
95
|
+
*/
|
|
96
|
+
start() {
|
|
97
|
+
if (this.running)
|
|
98
|
+
return;
|
|
99
|
+
if (this.ui)
|
|
100
|
+
this.checkUiFiles();
|
|
101
|
+
this.running = true;
|
|
102
|
+
ipcMain.handle("moonbase:invoke", (event, command, args) => this.handleRenderer(event, command, args ?? {}));
|
|
103
|
+
ipcMain.handle("moonbase:ui", (event, command, args) => this.handleUi(event, command, args ?? {}));
|
|
104
|
+
ipcMain.on("moonbase:subscribe", this.onSubscribe);
|
|
105
|
+
app.on("browser-window-focus", this.onWindowFocus);
|
|
106
|
+
app.on("browser-window-blur", this.onWindowBlur);
|
|
107
|
+
super.start();
|
|
108
|
+
}
|
|
109
|
+
/** Stop watching, close the UI and stop answering renderers. */
|
|
110
|
+
dispose() {
|
|
111
|
+
if (this.running) {
|
|
112
|
+
ipcMain.removeHandler("moonbase:invoke");
|
|
113
|
+
ipcMain.removeHandler("moonbase:ui");
|
|
114
|
+
ipcMain.off("moonbase:subscribe", this.onSubscribe);
|
|
115
|
+
app.off("browser-window-focus", this.onWindowFocus);
|
|
116
|
+
app.off("browser-window-blur", this.onWindowBlur);
|
|
117
|
+
this.running = false;
|
|
118
|
+
}
|
|
119
|
+
this.closeUi();
|
|
120
|
+
super.dispose();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Stop here, where the developer sees it, rather than open a blank window or
|
|
124
|
+
* lock the app behind an overlay that never loads. The files go missing when
|
|
125
|
+
* the module is bundled into the main process without them.
|
|
126
|
+
*/
|
|
127
|
+
checkUiFiles() {
|
|
128
|
+
const missing = UI_FILES.filter((file) => !fs.existsSync(path2.join(this.folder, file)));
|
|
129
|
+
if (missing.length === 0)
|
|
130
|
+
return;
|
|
131
|
+
throw new ActivationError("internal", `@moonbase.sh/electron can't find its activation UI in ${this.folder} (missing ${missing.join(", ")}). This happens when the module is bundled into the main process: add moonbase() from '@moonbase.sh/electron/vite' to the plugins of the main process's Vite config, which copies the UI beside the bundle, or leave the module out of the bundle. Pass ui: false to use a UI of your own instead.`);
|
|
132
|
+
}
|
|
133
|
+
isOwnUi(contents) {
|
|
134
|
+
return !!this.window && !this.window.isDestroyed() && this.window.webContents === contents || !!this.overlay && this.overlay.view.webContents === contents;
|
|
135
|
+
}
|
|
136
|
+
accessOf(contents) {
|
|
137
|
+
const access = this.rendererAccess ?? "full";
|
|
138
|
+
return typeof access === "function" ? access(contents) : access;
|
|
139
|
+
}
|
|
140
|
+
// -- Renderers ----------------------------------------------------------------
|
|
141
|
+
async handleRenderer(event, command, args) {
|
|
142
|
+
const access = this.isOwnUi(event.sender) ? "full" : this.accessOf(event.sender);
|
|
143
|
+
if (access === "none" || access === "read-only" && !READ_ONLY.has(command))
|
|
144
|
+
return { ok: false, error: { kind: "forbidden", message: `This window may not call ${command}` } };
|
|
145
|
+
try {
|
|
146
|
+
return { ok: true, value: await this.run(command, args) };
|
|
147
|
+
} catch (err) {
|
|
148
|
+
return errorOutcome(err);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async run(command, args) {
|
|
152
|
+
const id = String(args.id ?? "");
|
|
153
|
+
switch (command) {
|
|
154
|
+
case "state":
|
|
155
|
+
return this.state();
|
|
156
|
+
case "activate":
|
|
157
|
+
return await this.activate();
|
|
158
|
+
case "cancelActivation":
|
|
159
|
+
return this.cancelActivation();
|
|
160
|
+
case "refresh":
|
|
161
|
+
return await this.refresh({ force: args.force !== false });
|
|
162
|
+
case "deactivate":
|
|
163
|
+
return await this.deactivate();
|
|
164
|
+
case "forget":
|
|
165
|
+
return await this.forget();
|
|
166
|
+
case "deviceToken":
|
|
167
|
+
return await this.deviceToken();
|
|
168
|
+
case "activateOffline":
|
|
169
|
+
return await this.activateOffline(String(args.licenseFile ?? ""));
|
|
170
|
+
case "dismissError":
|
|
171
|
+
return this.dismissError();
|
|
172
|
+
case "device":
|
|
173
|
+
return await this.device();
|
|
174
|
+
case "checkUpdate":
|
|
175
|
+
return await this.checkUpdate();
|
|
176
|
+
case "updateDownloadUrl":
|
|
177
|
+
return await this.updateDownloadUrl();
|
|
178
|
+
case "present":
|
|
179
|
+
return await this.present(args.screen ?? void 0);
|
|
180
|
+
case "closeUi":
|
|
181
|
+
return this.closeUi();
|
|
182
|
+
case "upgradeLicense":
|
|
183
|
+
return await this.upgradeLicense();
|
|
184
|
+
case "loadAddOns":
|
|
185
|
+
return await this.loadAddOns();
|
|
186
|
+
case "refreshAddOns":
|
|
187
|
+
return await this.refreshAddOns();
|
|
188
|
+
case "activateSubProduct":
|
|
189
|
+
return await this.activateSubProduct(id);
|
|
190
|
+
case "requestSubProductTrial":
|
|
191
|
+
return await this.requestSubProductTrial(id);
|
|
192
|
+
case "buySubProduct":
|
|
193
|
+
return await this.buySubProduct(id);
|
|
194
|
+
default:
|
|
195
|
+
throw new ActivationError("internal", `Unknown command ${command}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** The built-in UI's own commands, which only its window or overlay may call. */
|
|
199
|
+
async handleUi(event, command, args) {
|
|
200
|
+
if (!this.isOwnUi(event.sender))
|
|
201
|
+
return { ok: false, error: { kind: "forbidden", message: "Only the activation UI may call this" } };
|
|
202
|
+
try {
|
|
203
|
+
return { ok: true, value: await this.runUi(event.sender, command, args) };
|
|
204
|
+
} catch (err) {
|
|
205
|
+
return errorOutcome(err);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async runUi(sender, command, args) {
|
|
209
|
+
switch (command) {
|
|
210
|
+
case "ui_config":
|
|
211
|
+
return this.pageConfig();
|
|
212
|
+
case "ui_ready":
|
|
213
|
+
return this.uiReady(Number(args.height) || 0);
|
|
214
|
+
case "ui_resize":
|
|
215
|
+
return this.uiResize(Number(args.height) || 0);
|
|
216
|
+
case "close_ui":
|
|
217
|
+
return this.closeUi();
|
|
218
|
+
case "open_link":
|
|
219
|
+
return await this.openLink(String(args.url ?? ""));
|
|
220
|
+
case "copy_text":
|
|
221
|
+
return clipboard.writeText(String(args.text ?? ""));
|
|
222
|
+
case "save_machine_file":
|
|
223
|
+
return await this.saveMachineFile(BrowserWindow.fromWebContents(sender) ?? this.overlay?.target ?? null);
|
|
224
|
+
case "download_update":
|
|
225
|
+
return await this.downloadUpdate();
|
|
226
|
+
case "reveal_update":
|
|
227
|
+
return this.revealUpdate();
|
|
228
|
+
case "skip_update":
|
|
229
|
+
return await this.skipUpdate(String(args.version ?? ""));
|
|
230
|
+
case "cancel_activation":
|
|
231
|
+
return this.cancelActivation();
|
|
232
|
+
case "activate_offline":
|
|
233
|
+
return await this.activateOffline(String(args.licenseFile ?? ""));
|
|
234
|
+
case "upgrade_license":
|
|
235
|
+
return await this.upgradeLicense();
|
|
236
|
+
case "check_update":
|
|
237
|
+
return await this.checkUpdate();
|
|
238
|
+
case "load_add_ons":
|
|
239
|
+
return await this.loadAddOns();
|
|
240
|
+
case "refresh_add_ons":
|
|
241
|
+
return await this.refreshAddOns();
|
|
242
|
+
case "activate_sub_product":
|
|
243
|
+
return await this.activateSubProduct(String(args.id ?? ""));
|
|
244
|
+
case "request_sub_product_trial":
|
|
245
|
+
return await this.requestSubProductTrial(String(args.id ?? ""));
|
|
246
|
+
case "buy_sub_product":
|
|
247
|
+
return await this.buySubProduct(String(args.id ?? ""));
|
|
248
|
+
default:
|
|
249
|
+
return await this.run(command, args);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
// -- The built-in UI ------------------------------------------------------------
|
|
253
|
+
pageConfig() {
|
|
254
|
+
const ui = this.ui ?? {};
|
|
255
|
+
const productName = ui.productName ?? app.getName();
|
|
256
|
+
let logo = null;
|
|
257
|
+
if (ui.logoPng) {
|
|
258
|
+
const png = typeof ui.logoPng === "string" ? fs.readFileSync(ui.logoPng) : ui.logoPng;
|
|
259
|
+
logo = `data:image/png;base64,${Buffer.from(png).toString("base64")}`;
|
|
260
|
+
}
|
|
261
|
+
let activationHost = this.endpoint;
|
|
262
|
+
try {
|
|
263
|
+
activationHost = new URL(this.endpoint).host;
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
productName,
|
|
268
|
+
manufacturerName: ui.manufacturerName ?? null,
|
|
269
|
+
accent: ui.accent ?? "#186cdc",
|
|
270
|
+
logo,
|
|
271
|
+
logoSvg: ui.logoSvg ?? null,
|
|
272
|
+
trialFeatures: ui.trialFeatures ?? [],
|
|
273
|
+
trialLengthDays: Math.max(1, ui.trialLengthDays ?? 14),
|
|
274
|
+
showBadge: ui.showMoonbaseBadge ?? true,
|
|
275
|
+
strings: ui.strings ?? {},
|
|
276
|
+
showAddOns: ui.showAddOns ?? false,
|
|
277
|
+
reduceMotion: ui.reduceMotion ?? false,
|
|
278
|
+
activationHost,
|
|
279
|
+
requestFileName: `${fileSafe(productName)}.dt`,
|
|
280
|
+
os: platformOs(),
|
|
281
|
+
windowTitle: ui.windowTitle ?? productName
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** The window `ui.window` names, if it is open. */
|
|
285
|
+
chosenWindow() {
|
|
286
|
+
const chosen = typeof this.ui?.window === "function" ? this.ui.window() : this.ui?.window;
|
|
287
|
+
return chosen && !chosen.isDestroyed() ? chosen : null;
|
|
288
|
+
}
|
|
289
|
+
/** The app's window the overlay locks, and the activation window's parent. */
|
|
290
|
+
targetWindow() {
|
|
291
|
+
return this.chosenWindow() ?? BrowserWindow.getAllWindows().filter((candidate) => !candidate.isDestroyed() && candidate !== this.window).sort((a, b) => a.id - b.id)[0] ?? null;
|
|
292
|
+
}
|
|
293
|
+
/** Hand the UI something new, wherever it is showing. */
|
|
294
|
+
push(name, payload) {
|
|
295
|
+
const contents = this.overlay?.view.webContents ?? (this.window && !this.window.isDestroyed() ? this.window.webContents : null);
|
|
296
|
+
if (contents && !contents.isDestroyed())
|
|
297
|
+
contents.send("moonbase:push", name, payload);
|
|
298
|
+
}
|
|
299
|
+
afterStateChange(state) {
|
|
300
|
+
for (const contents of [...this.subscribers]) {
|
|
301
|
+
if (contents.isDestroyed() || this.accessOf(contents) === "none")
|
|
302
|
+
this.subscribers.delete(contents);
|
|
303
|
+
else
|
|
304
|
+
contents.send("moonbase:state-changed", state);
|
|
305
|
+
}
|
|
306
|
+
if (!this.ui)
|
|
307
|
+
return;
|
|
308
|
+
this.push("update", state);
|
|
309
|
+
if (state.status === "loading")
|
|
310
|
+
return;
|
|
311
|
+
const previous = this.lastLicensed;
|
|
312
|
+
this.lastLicensed = state.licensed;
|
|
313
|
+
if (!state.licensed && previous !== false && (this.ui.autoPresentOnLock ?? true)) {
|
|
314
|
+
this.presentQuietly();
|
|
315
|
+
} else if (previous === null && state.licensed && (this.ui.autoPresentUpdate ?? true) && state.update) {
|
|
316
|
+
const latest = state.update.latestVersion;
|
|
317
|
+
void this.isUpdateSkipped(latest).then((skipped) => {
|
|
318
|
+
if (!skipped)
|
|
319
|
+
this.presentQuietly("update");
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
presentQuietly(screen) {
|
|
324
|
+
this.present(screen).catch((err) => console.warn(`moonbase: couldn't present the activation UI: ${err instanceof Error ? err.message : err}`));
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Show the activation UI and bring it to the front, creating it when needed:
|
|
328
|
+
* its own window, or the overlay over the app's window. `screen` opens it on a
|
|
329
|
+
* particular screen; without one it shows whatever the license state calls for.
|
|
330
|
+
*/
|
|
331
|
+
async present(screen) {
|
|
332
|
+
if (!this.ui)
|
|
333
|
+
throw new ActivationError("internal", "The built-in activation UI is turned off");
|
|
334
|
+
await app.whenReady();
|
|
335
|
+
if (this.ui.presentation === "overlay")
|
|
336
|
+
return this.mountOverlay(screen ?? null);
|
|
337
|
+
if (this.window && !this.window.isDestroyed()) {
|
|
338
|
+
if (screen)
|
|
339
|
+
this.push("show", screen);
|
|
340
|
+
if (this.window.isMinimized())
|
|
341
|
+
this.window.restore();
|
|
342
|
+
this.window.show();
|
|
343
|
+
this.window.focus();
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
this.pendingScreen = screen ?? null;
|
|
347
|
+
this.window = this.createWindow();
|
|
348
|
+
}
|
|
349
|
+
webPreferences() {
|
|
350
|
+
return {
|
|
351
|
+
preload: path2.join(this.folder, "ui-preload.cjs"),
|
|
352
|
+
contextIsolation: true,
|
|
353
|
+
sandbox: true,
|
|
354
|
+
nodeIntegration: false,
|
|
355
|
+
spellcheck: false
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
/** The UI's pages load nothing but their own files, and open nothing themselves. */
|
|
359
|
+
lockDown(contents) {
|
|
360
|
+
contents.on("will-navigate", (event) => event.preventDefault());
|
|
361
|
+
contents.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
362
|
+
}
|
|
363
|
+
createWindow() {
|
|
364
|
+
const ui = this.ui ?? {};
|
|
365
|
+
const parent = this.targetWindow();
|
|
366
|
+
const window = new BrowserWindow({
|
|
367
|
+
width: WIDTH,
|
|
368
|
+
height: 560,
|
|
369
|
+
useContentSize: true,
|
|
370
|
+
show: false,
|
|
371
|
+
resizable: false,
|
|
372
|
+
maximizable: false,
|
|
373
|
+
fullscreenable: false,
|
|
374
|
+
title: ui.windowTitle ?? ui.productName ?? app.getName(),
|
|
375
|
+
backgroundColor: "#080b13",
|
|
376
|
+
titleBarStyle: process.platform === "darwin" ? "hidden" : "default",
|
|
377
|
+
autoHideMenuBar: true,
|
|
378
|
+
...parent && { parent },
|
|
379
|
+
webPreferences: this.webPreferences()
|
|
380
|
+
});
|
|
381
|
+
window.setMenuBarVisibility(false);
|
|
382
|
+
this.lockDown(window.webContents);
|
|
383
|
+
window.on("closed", () => {
|
|
384
|
+
if (this.window === window)
|
|
385
|
+
this.window = null;
|
|
386
|
+
});
|
|
387
|
+
void window.loadFile(path2.join(this.folder, "ui", "index.html"));
|
|
388
|
+
setTimeout(() => {
|
|
389
|
+
if (!window.isDestroyed() && !window.isVisible())
|
|
390
|
+
window.show();
|
|
391
|
+
}, 1500).unref?.();
|
|
392
|
+
return window;
|
|
393
|
+
}
|
|
394
|
+
uiReady(height) {
|
|
395
|
+
if (this.window && !this.window.isDestroyed()) {
|
|
396
|
+
this.uiResize(height);
|
|
397
|
+
this.window.center();
|
|
398
|
+
this.window.show();
|
|
399
|
+
this.window.focus();
|
|
400
|
+
}
|
|
401
|
+
const screen = this.pendingScreen;
|
|
402
|
+
this.pendingScreen = null;
|
|
403
|
+
return { screen };
|
|
404
|
+
}
|
|
405
|
+
uiResize(height) {
|
|
406
|
+
if (this.window && !this.window.isDestroyed() && height > 0)
|
|
407
|
+
this.window.setContentSize(WIDTH, Math.round(Math.min(900, Math.max(240, height))));
|
|
408
|
+
}
|
|
409
|
+
/** Lay the overlay over the app's window, or bring it to `screen` if it is up. */
|
|
410
|
+
mountOverlay(screen, target = this.targetWindow()) {
|
|
411
|
+
if (this.overlay && !this.overlay.target.isDestroyed()) {
|
|
412
|
+
if (screen)
|
|
413
|
+
this.push("show", screen);
|
|
414
|
+
this.overlay.target.show();
|
|
415
|
+
this.overlay.view.webContents.focus();
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (!target) {
|
|
419
|
+
this.lockNextWindow(screen);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (typeof WebContentsView !== "function")
|
|
423
|
+
throw new ActivationError("internal", "The overlay needs Electron 30 or later");
|
|
424
|
+
this.pendingScreen = screen;
|
|
425
|
+
const view = new WebContentsView({ webPreferences: this.webPreferences() });
|
|
426
|
+
view.setBackgroundColor("#00000000");
|
|
427
|
+
this.lockDown(view.webContents);
|
|
428
|
+
target.contentView.addChildView(view);
|
|
429
|
+
const overlay = {
|
|
430
|
+
view,
|
|
431
|
+
target,
|
|
432
|
+
css: null,
|
|
433
|
+
fit: () => {
|
|
434
|
+
const [width = 0, height = 0] = target.getContentSize();
|
|
435
|
+
view.setBounds({ x: 0, y: 0, width, height });
|
|
436
|
+
},
|
|
437
|
+
reinsert: () => {
|
|
438
|
+
void target.webContents.insertCSS("html { filter: blur(10px) saturate(0.8) brightness(0.85); pointer-events: none; }").then((key) => {
|
|
439
|
+
if (this.overlay === overlay)
|
|
440
|
+
overlay.css = key;
|
|
441
|
+
else
|
|
442
|
+
void target.webContents.removeInsertedCSS(key);
|
|
443
|
+
}).catch(() => {
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
this.overlay = overlay;
|
|
448
|
+
overlay.fit();
|
|
449
|
+
overlay.reinsert();
|
|
450
|
+
target.on("resize", overlay.fit);
|
|
451
|
+
target.webContents.on("did-finish-load", overlay.reinsert);
|
|
452
|
+
target.once("closed", () => {
|
|
453
|
+
if (this.overlay !== overlay)
|
|
454
|
+
return;
|
|
455
|
+
this.overlay = null;
|
|
456
|
+
if (!view.webContents.isDestroyed())
|
|
457
|
+
view.webContents.close();
|
|
458
|
+
if (!this.state().licensed && (this.ui?.autoPresentOnLock ?? true))
|
|
459
|
+
this.lockNextWindow(null);
|
|
460
|
+
});
|
|
461
|
+
void view.webContents.loadFile(path2.join(this.folder, "ui", "overlay.html"));
|
|
462
|
+
view.webContents.once("did-finish-load", () => view.webContents.focus());
|
|
463
|
+
target.show();
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Lay the overlay over the next window the app opens, or over the one
|
|
467
|
+
* `ui.window` names by then. Waits for one window at a time.
|
|
468
|
+
*/
|
|
469
|
+
lockNextWindow(screen) {
|
|
470
|
+
this.pendingScreen = screen;
|
|
471
|
+
if (this.waitingForWindow)
|
|
472
|
+
return;
|
|
473
|
+
const listener = (_event, created) => {
|
|
474
|
+
this.waitingForWindow = null;
|
|
475
|
+
setImmediate(() => this.mountOverlay(this.pendingScreen, this.chosenWindow() ?? (created.isDestroyed() ? null : created)));
|
|
476
|
+
};
|
|
477
|
+
this.waitingForWindow = listener;
|
|
478
|
+
app.once("browser-window-created", listener);
|
|
479
|
+
}
|
|
480
|
+
/** Close the activation window, or take the overlay down, if it is showing. */
|
|
481
|
+
closeUi() {
|
|
482
|
+
if (this.waitingForWindow) {
|
|
483
|
+
app.off("browser-window-created", this.waitingForWindow);
|
|
484
|
+
this.waitingForWindow = null;
|
|
485
|
+
}
|
|
486
|
+
if (this.window && !this.window.isDestroyed())
|
|
487
|
+
this.window.close();
|
|
488
|
+
this.window = null;
|
|
489
|
+
const overlay = this.overlay;
|
|
490
|
+
if (!overlay)
|
|
491
|
+
return;
|
|
492
|
+
this.overlay = null;
|
|
493
|
+
if (overlay.target.isDestroyed())
|
|
494
|
+
return;
|
|
495
|
+
overlay.target.off("resize", overlay.fit);
|
|
496
|
+
overlay.target.webContents.off("did-finish-load", overlay.reinsert);
|
|
497
|
+
overlay.target.contentView.removeChildView(overlay.view);
|
|
498
|
+
overlay.view.webContents.close();
|
|
499
|
+
if (overlay.css)
|
|
500
|
+
void overlay.target.webContents.removeInsertedCSS(overlay.css);
|
|
501
|
+
overlay.target.webContents.focus();
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Open a link from the UI in the browser: the pending activation, the store, or
|
|
505
|
+
* Moonbase's site from the footer. Nothing else, so the UI cannot be used to
|
|
506
|
+
* open arbitrary links.
|
|
507
|
+
*/
|
|
508
|
+
async openLink(url) {
|
|
509
|
+
let parsed;
|
|
510
|
+
try {
|
|
511
|
+
parsed = new URL(url);
|
|
512
|
+
} catch {
|
|
513
|
+
throw new ActivationError("internal", "Not a link");
|
|
514
|
+
}
|
|
515
|
+
let sameOrigin = false;
|
|
516
|
+
try {
|
|
517
|
+
sameOrigin = new URL(this.endpoint).origin === parsed.origin;
|
|
518
|
+
} catch {
|
|
519
|
+
}
|
|
520
|
+
const pending = this.state().activation?.browserUrl === url;
|
|
521
|
+
const moonbase = url === MOONBASE_SITE && (this.ui?.showMoonbaseBadge ?? true);
|
|
522
|
+
if (!sameOrigin && !pending && !moonbase)
|
|
523
|
+
throw new ActivationError("internal", "That link can't be opened from here");
|
|
524
|
+
await shell.openExternal(url);
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Save the machine file for offline activation through a save dialog. Null when
|
|
528
|
+
* the customer cancels.
|
|
529
|
+
*/
|
|
530
|
+
async saveMachineFile(parent = null) {
|
|
531
|
+
const token = await this.deviceToken();
|
|
532
|
+
const name = `${fileSafe(this.ui?.productName ?? app.getName())}.dt`;
|
|
533
|
+
const options = {
|
|
534
|
+
title: "Save machine file",
|
|
535
|
+
defaultPath: path2.join(app.getPath("downloads"), name),
|
|
536
|
+
filters: [{ name: "Moonbase machine file", extensions: ["dt"] }]
|
|
537
|
+
};
|
|
538
|
+
const picked = parent ? await dialog.showSaveDialog(parent, options) : await dialog.showSaveDialog(options);
|
|
539
|
+
if (picked.canceled || !picked.filePath)
|
|
540
|
+
return null;
|
|
541
|
+
await fsPromises.writeFile(picked.filePath, token);
|
|
542
|
+
return picked.filePath;
|
|
543
|
+
}
|
|
544
|
+
setDownload(next) {
|
|
545
|
+
this.download = next;
|
|
546
|
+
this.push("download", next);
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Download the latest installer for this platform into Downloads, reporting
|
|
550
|
+
* progress to the UI, then show it in the file manager.
|
|
551
|
+
*/
|
|
552
|
+
async downloadUpdate() {
|
|
553
|
+
if (this.download.phase === "downloading")
|
|
554
|
+
return;
|
|
555
|
+
this.setDownload(downloadState("downloading"));
|
|
556
|
+
try {
|
|
557
|
+
const file = await this.downloadInstaller({
|
|
558
|
+
directory: app.getPath("downloads"),
|
|
559
|
+
onProgress: (progress) => this.setDownload(downloadState("downloading", { progress })),
|
|
560
|
+
// Through Chromium's network stack, which knows the system's proxy.
|
|
561
|
+
fetch: (url) => net.fetch(url)
|
|
562
|
+
});
|
|
563
|
+
this.downloaded = file;
|
|
564
|
+
shell.showItemInFolder(file);
|
|
565
|
+
this.setDownload(downloadState("done", { progress: 1, path: file }));
|
|
566
|
+
} catch (err) {
|
|
567
|
+
if (err instanceof ReleaseNotCoveredError) {
|
|
568
|
+
this.setDownload(downloadState("notCovered", { releaseAccess: err.releaseAccess ?? null, upgradeUrl: err.upgradeUrl ?? null }));
|
|
569
|
+
throw err;
|
|
570
|
+
}
|
|
571
|
+
this.setDownload(downloadState("failed", { error: `The download failed: ${err instanceof Error ? err.message : err}` }));
|
|
572
|
+
throw err;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
/** Show the downloaded installer in the file manager again. */
|
|
576
|
+
revealUpdate() {
|
|
577
|
+
if (!this.downloaded)
|
|
578
|
+
throw new ActivationError("internal", "Nothing has been downloaded");
|
|
579
|
+
shell.showItemInFolder(this.downloaded);
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
function createMoonbase(options) {
|
|
583
|
+
return new MoonbaseElectron(options);
|
|
584
|
+
}
|
|
585
|
+
export {
|
|
586
|
+
MoonbaseElectron,
|
|
587
|
+
createMoonbase
|
|
588
|
+
};
|
package/dist/preload.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/preload.ts
|
|
21
|
+
var preload_exports = {};
|
|
22
|
+
__export(preload_exports, {
|
|
23
|
+
createMoonbaseApi: () => createMoonbaseApi,
|
|
24
|
+
exposeMoonbase: () => exposeMoonbase
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(preload_exports);
|
|
27
|
+
var import_electron = require("electron");
|
|
28
|
+
async function call(command, args = {}) {
|
|
29
|
+
const outcome = await import_electron.ipcRenderer.invoke("moonbase:invoke", command, args);
|
|
30
|
+
if (!outcome.ok)
|
|
31
|
+
throw outcome.error;
|
|
32
|
+
return outcome.value;
|
|
33
|
+
}
|
|
34
|
+
function createMoonbaseApi() {
|
|
35
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
36
|
+
let subscribed = false;
|
|
37
|
+
return {
|
|
38
|
+
state: () => call("state"),
|
|
39
|
+
onStateChanged(listener) {
|
|
40
|
+
listeners.add(listener);
|
|
41
|
+
if (!subscribed) {
|
|
42
|
+
subscribed = true;
|
|
43
|
+
import_electron.ipcRenderer.on("moonbase:state-changed", (_event, state) => {
|
|
44
|
+
for (const each of [...listeners])
|
|
45
|
+
each(state);
|
|
46
|
+
});
|
|
47
|
+
import_electron.ipcRenderer.send("moonbase:subscribe");
|
|
48
|
+
}
|
|
49
|
+
return () => {
|
|
50
|
+
listeners.delete(listener);
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
activate: () => call("activate"),
|
|
54
|
+
cancelActivation: () => call("cancelActivation"),
|
|
55
|
+
refresh: (options) => call("refresh", { force: options?.force ?? true }),
|
|
56
|
+
deactivate: () => call("deactivate"),
|
|
57
|
+
forget: () => call("forget"),
|
|
58
|
+
deviceToken: () => call("deviceToken"),
|
|
59
|
+
activateOffline: (licenseFile) => call("activateOffline", { licenseFile }),
|
|
60
|
+
dismissError: () => call("dismissError"),
|
|
61
|
+
device: () => call("device"),
|
|
62
|
+
checkUpdate: () => call("checkUpdate"),
|
|
63
|
+
updateDownloadUrl: () => call("updateDownloadUrl"),
|
|
64
|
+
present: (options) => call("present", { screen: options?.screen ?? null }),
|
|
65
|
+
closeUi: () => call("closeUi"),
|
|
66
|
+
upgradeLicense: () => call("upgradeLicense"),
|
|
67
|
+
loadAddOns: () => call("loadAddOns"),
|
|
68
|
+
refreshAddOns: () => call("refreshAddOns"),
|
|
69
|
+
activateSubProduct: (id) => call("activateSubProduct", { id }),
|
|
70
|
+
requestSubProductTrial: (id) => call("requestSubProductTrial", { id }),
|
|
71
|
+
buySubProduct: (id) => call("buySubProduct", { id }),
|
|
72
|
+
hasSubProduct(state, id) {
|
|
73
|
+
const license = state.license;
|
|
74
|
+
if (!license)
|
|
75
|
+
return false;
|
|
76
|
+
const prefix = `${license.product.id}.`;
|
|
77
|
+
const own = id.startsWith(prefix) ? id.slice(prefix.length) : id;
|
|
78
|
+
return state.activeSubProducts.includes(own);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function exposeMoonbase(key = "moonbase") {
|
|
83
|
+
import_electron.contextBridge.exposeInMainWorld(key, createMoonbaseApi());
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
createMoonbaseApi,
|
|
88
|
+
exposeMoonbase
|
|
89
|
+
});
|