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