@n3rdw1z4rd/core 0.24.0 → 0.26.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 +45 -0
- package/dist/astar.js +3 -3
- package/dist/desktop.d.ts +37 -0
- package/dist/desktop.js +14 -0
- package/dist/dungeon-generator.js +4 -4
- package/dist/electron/after-pack.cjs +17 -0
- package/dist/electron/index.d.ts +110 -0
- package/dist/electron/index.js +257 -0
- package/dist/electron/preload.cjs +12 -0
- package/dist/electron/updater.d.ts +81 -0
- package/dist/electron/updater.js +133 -0
- package/dist/electron.cjs +416 -0
- package/dist/electron.d.cts +189 -0
- package/dist/graph/index.d.ts +21 -0
- package/dist/graph/subgraph.js +1 -1
- package/dist/graph.cjs +2 -2
- package/dist/graph.d.cts +21 -0
- package/dist/index.cjs +25 -11
- package/dist/index.d.cts +48 -0
- package/dist/index.d.ts +13 -1
- package/dist/index.js +2 -1
- package/dist/map2d.js +2 -2
- package/dist/math.js +2 -2
- package/dist/{rng-edL6atS3.cjs → rng-BtvhTJmM.cjs} +2 -2
- package/dist/{spatial-partition-3d-D1uyJVhM.cjs → spatial-partition-3d-qhjqIpcy.cjs} +1 -1
- package/dist/three/adaptive-grid.d.ts +1 -1
- package/dist/three/adaptive-grid.js +1 -1
- package/dist/three/asteroid-mesh.d.ts +1 -1
- package/dist/three/camera-rig.js +1 -1
- package/dist/three/cube-sphere-geometry.d.ts +1 -1
- package/dist/three/index.d.ts +13 -2
- package/dist/three/particle-system-3d.js +1 -1
- package/dist/three/picking.d.ts +1 -1
- package/dist/three/picking.js +1 -1
- package/dist/three/starfield.js +4 -4
- package/dist/three/texture-atlas.d.ts +1 -1
- package/dist/three/texture-atlas.js +1 -1
- package/dist/three/texture-utils.js +2 -4
- package/dist/three/voxel-geometry.d.ts +44 -10
- package/dist/three/voxel-geometry.js +282 -77
- package/dist/three/voxel-mesh.d.ts +1 -1
- package/dist/three/voxel-mesh.js +1 -1
- package/dist/three/world.d.ts +1 -1
- package/dist/three/world.js +1 -1
- package/dist/three.cjs +290 -87
- package/dist/three.d.cts +55 -10
- package/dist/vite.cjs +124 -0
- package/dist/vite.d.cts +53 -2
- package/dist/vite.d.ts +53 -2
- package/dist/vite.js +122 -1
- package/dist/webgl.js +2 -2
- package/package.json +23 -3
- package/dist/input/_input.d.ts +0 -23
- package/dist/input/_input.js +0 -96
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
// src/electron/updater.ts
|
|
5
|
+
const VERSION_ID = /^[\w.-]+$/;
|
|
6
|
+
/** Whether `site` is one builds may come from: https, or http on localhost (for tests). */
|
|
7
|
+
function isUpdateSite(site) {
|
|
8
|
+
return /^https:\/\/[\w.-]+(:\d+)?$/.test(site) || /^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(site);
|
|
9
|
+
}
|
|
10
|
+
function readJson(file) {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
13
|
+
} catch {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** A build folder's manifest, if the build is complete (version.json is written last). */
|
|
18
|
+
function readBuild(dir) {
|
|
19
|
+
const info = readJson(path.join(dir, "version.json"));
|
|
20
|
+
return info && typeof info.version === "string" && info.version ? info : void 0;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The build to run: the downloaded one `store/current.json` names, if it's complete, works with
|
|
24
|
+
* this app's `api` and is no older than the bundled one (a rebuilt app brings a newer build);
|
|
25
|
+
* otherwise the bundled one.
|
|
26
|
+
*/
|
|
27
|
+
function pickBuild(options) {
|
|
28
|
+
const bundled = {
|
|
29
|
+
dir: options.bundledDir,
|
|
30
|
+
info: readBuild(options.bundledDir) ?? {
|
|
31
|
+
version: "dev",
|
|
32
|
+
builtAt: ""
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
if (options.bundledOnly) return bundled;
|
|
36
|
+
const current = readJson(path.join(options.store, "current.json"));
|
|
37
|
+
if (current && typeof current.version === "string" && VERSION_ID.test(current.version)) {
|
|
38
|
+
const dir = path.join(options.store, current.version);
|
|
39
|
+
const info = readBuild(dir);
|
|
40
|
+
if (info && (info.needs ?? 1) <= options.api && !(new Date(bundled.info.builtAt) > new Date(info.builtAt))) return {
|
|
41
|
+
dir,
|
|
42
|
+
info
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return bundled;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Looks for a newer build on `site`. If there is one (built later than the one running, and not
|
|
49
|
+
* needing a newer app), downloads all of it into a folder of its own, checks every file's size
|
|
50
|
+
* and SHA-256, and only then makes it current (current.json), keeping the build that was running
|
|
51
|
+
* and removing older ones. Anything wrong (offline, a bad file) and nothing changes.
|
|
52
|
+
*/
|
|
53
|
+
async function downloadUpdate(options) {
|
|
54
|
+
const { site, store, current, api } = options;
|
|
55
|
+
const log = options.log ?? (() => void 0);
|
|
56
|
+
const fail = (status, reason) => {
|
|
57
|
+
log(reason);
|
|
58
|
+
return {
|
|
59
|
+
status,
|
|
60
|
+
reason
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
if (!isUpdateSite(site)) return fail("unavailable", `not updating from "${site}": https only`);
|
|
64
|
+
const get = async (file, ms) => {
|
|
65
|
+
const controller = new AbortController();
|
|
66
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
67
|
+
try {
|
|
68
|
+
const url = `${site}/${file}${file === "version.json" ? `?t=${Date.now()}` : ""}`;
|
|
69
|
+
const response = await options.fetch(url, {
|
|
70
|
+
signal: controller.signal,
|
|
71
|
+
cache: "no-store"
|
|
72
|
+
});
|
|
73
|
+
if (!response.ok) throw new Error(`${file}: ${response.status}`);
|
|
74
|
+
return Buffer.from(await response.arrayBuffer());
|
|
75
|
+
} finally {
|
|
76
|
+
clearTimeout(timer);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
let remote;
|
|
80
|
+
try {
|
|
81
|
+
remote = JSON.parse((await get("version.json", options.timeout ?? 15e3)).toString("utf8"));
|
|
82
|
+
} catch (e) {
|
|
83
|
+
return fail("unavailable", `no update check: ${e.message}`);
|
|
84
|
+
}
|
|
85
|
+
if (!remote || typeof remote.version !== "string" || !VERSION_ID.test(remote.version) || !Array.isArray(remote.files)) return fail("failed", "no update: the site's version.json is not a build manifest");
|
|
86
|
+
if (remote.version === current.info.version && remote.builtAt === current.info.builtAt) return fail("up-to-date", `up to date: ${remote.version}`);
|
|
87
|
+
if (current.info.builtAt && !(new Date(remote.builtAt) > new Date(current.info.builtAt))) return fail("not-newer", `not updating: ${remote.version} is no newer than ${current.info.version}`);
|
|
88
|
+
if ((remote.needs ?? 1) > api) return fail("needs-newer-app", `not updating to ${remote.version}: it needs a newer desktop app (${remote.needs}, this is ${api})`);
|
|
89
|
+
const dir = path.join(store, remote.version);
|
|
90
|
+
const partial = `${dir}.partial`;
|
|
91
|
+
try {
|
|
92
|
+
fs.rmSync(partial, {
|
|
93
|
+
recursive: true,
|
|
94
|
+
force: true
|
|
95
|
+
});
|
|
96
|
+
for (const file of remote.files) {
|
|
97
|
+
const out = path.normalize(path.join(partial, String(file.path)));
|
|
98
|
+
if (!out.startsWith(partial + path.sep) || typeof file.sha256 !== "string" || path.basename(out) === "version.json") throw new Error(`bad file ${file.path}`);
|
|
99
|
+
const data = await get(file.path, 6e4);
|
|
100
|
+
if (data.length !== file.size || createHash("sha256").update(data).digest("hex") !== file.sha256) throw new Error(`${file.path} doesn't match its checksum`);
|
|
101
|
+
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
102
|
+
fs.writeFileSync(out, data);
|
|
103
|
+
}
|
|
104
|
+
fs.writeFileSync(path.join(partial, "version.json"), JSON.stringify(remote));
|
|
105
|
+
fs.rmSync(dir, {
|
|
106
|
+
recursive: true,
|
|
107
|
+
force: true
|
|
108
|
+
});
|
|
109
|
+
fs.renameSync(partial, dir);
|
|
110
|
+
fs.writeFileSync(path.join(store, "current.json"), JSON.stringify({ version: remote.version }));
|
|
111
|
+
} catch (e) {
|
|
112
|
+
fs.rmSync(partial, {
|
|
113
|
+
recursive: true,
|
|
114
|
+
force: true
|
|
115
|
+
});
|
|
116
|
+
return fail("failed", `update to ${remote.version} failed: ${e.message}`);
|
|
117
|
+
}
|
|
118
|
+
const keep = /* @__PURE__ */ new Set([
|
|
119
|
+
remote.version,
|
|
120
|
+
current.dir === options.bundledDir ? "" : path.basename(current.dir),
|
|
121
|
+
"current.json"
|
|
122
|
+
]);
|
|
123
|
+
for (const name of fs.readdirSync(store)) if (!keep.has(name)) fs.rmSync(path.join(store, name), {
|
|
124
|
+
recursive: true,
|
|
125
|
+
force: true
|
|
126
|
+
});
|
|
127
|
+
log(`updated to ${remote.version}`);
|
|
128
|
+
return {
|
|
129
|
+
status: "updated",
|
|
130
|
+
version: remote.version
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
export { downloadUpdate, isUpdateSite, pickBuild, readBuild };
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
// \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
12
|
+
desc = __getOwnPropDesc(from, key);
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: desc && desc.get && !desc.configurable ? desc.get : () => from[key],
|
|
15
|
+
enumerable: !desc || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
22
|
+
value: mod,
|
|
23
|
+
enumerable: true
|
|
24
|
+
}) : target, mod));
|
|
25
|
+
let electron = require("electron");
|
|
26
|
+
let node_fs = require("node:fs");
|
|
27
|
+
let node_path = require("node:path");
|
|
28
|
+
let node_url = require("node:url");
|
|
29
|
+
let node_crypto = require("node:crypto");
|
|
30
|
+
node_fs = __toESM(node_fs, 1);
|
|
31
|
+
node_path = __toESM(node_path, 1);
|
|
32
|
+
// src/electron/updater.ts
|
|
33
|
+
const VERSION_ID = /^[\w.-]+$/;
|
|
34
|
+
/** Whether `site` is one builds may come from: https, or http on localhost (for tests). */
|
|
35
|
+
function isUpdateSite(site) {
|
|
36
|
+
return /^https:\/\/[\w.-]+(:\d+)?$/.test(site) || /^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(site);
|
|
37
|
+
}
|
|
38
|
+
function readJson(file) {
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(node_fs.readFileSync(file, "utf8"));
|
|
41
|
+
} catch {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** A build folder's manifest, if the build is complete (version.json is written last). */
|
|
46
|
+
function readBuild(dir) {
|
|
47
|
+
const info = readJson(node_path.join(dir, "version.json"));
|
|
48
|
+
return info && typeof info.version === "string" && info.version ? info : void 0;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The build to run: the downloaded one `store/current.json` names, if it's complete, works with
|
|
52
|
+
* this app's `api` and is no older than the bundled one (a rebuilt app brings a newer build);
|
|
53
|
+
* otherwise the bundled one.
|
|
54
|
+
*/
|
|
55
|
+
function pickBuild(options) {
|
|
56
|
+
const bundled = {
|
|
57
|
+
dir: options.bundledDir,
|
|
58
|
+
info: readBuild(options.bundledDir) ?? {
|
|
59
|
+
version: "dev",
|
|
60
|
+
builtAt: ""
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
if (options.bundledOnly) return bundled;
|
|
64
|
+
const current = readJson(node_path.join(options.store, "current.json"));
|
|
65
|
+
if (current && typeof current.version === "string" && VERSION_ID.test(current.version)) {
|
|
66
|
+
const dir = node_path.join(options.store, current.version);
|
|
67
|
+
const info = readBuild(dir);
|
|
68
|
+
if (info && (info.needs ?? 1) <= options.api && !(new Date(bundled.info.builtAt) > new Date(info.builtAt))) return {
|
|
69
|
+
dir,
|
|
70
|
+
info
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return bundled;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Looks for a newer build on `site`. If there is one (built later than the one running, and not
|
|
77
|
+
* needing a newer app), downloads all of it into a folder of its own, checks every file's size
|
|
78
|
+
* and SHA-256, and only then makes it current (current.json), keeping the build that was running
|
|
79
|
+
* and removing older ones. Anything wrong (offline, a bad file) and nothing changes.
|
|
80
|
+
*/
|
|
81
|
+
async function downloadUpdate(options) {
|
|
82
|
+
const { site, store, current, api } = options;
|
|
83
|
+
const log = options.log ?? (() => void 0);
|
|
84
|
+
const fail = (status, reason) => {
|
|
85
|
+
log(reason);
|
|
86
|
+
return {
|
|
87
|
+
status,
|
|
88
|
+
reason
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
if (!isUpdateSite(site)) return fail("unavailable", `not updating from "${site}": https only`);
|
|
92
|
+
const get = async (file, ms) => {
|
|
93
|
+
const controller = new AbortController();
|
|
94
|
+
const timer = setTimeout(() => controller.abort(), ms);
|
|
95
|
+
try {
|
|
96
|
+
const url = `${site}/${file}${file === "version.json" ? `?t=${Date.now()}` : ""}`;
|
|
97
|
+
const response = await options.fetch(url, {
|
|
98
|
+
signal: controller.signal,
|
|
99
|
+
cache: "no-store"
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) throw new Error(`${file}: ${response.status}`);
|
|
102
|
+
return Buffer.from(await response.arrayBuffer());
|
|
103
|
+
} finally {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
let remote;
|
|
108
|
+
try {
|
|
109
|
+
remote = JSON.parse((await get("version.json", options.timeout ?? 15e3)).toString("utf8"));
|
|
110
|
+
} catch (e) {
|
|
111
|
+
return fail("unavailable", `no update check: ${e.message}`);
|
|
112
|
+
}
|
|
113
|
+
if (!remote || typeof remote.version !== "string" || !VERSION_ID.test(remote.version) || !Array.isArray(remote.files)) return fail("failed", "no update: the site's version.json is not a build manifest");
|
|
114
|
+
if (remote.version === current.info.version && remote.builtAt === current.info.builtAt) return fail("up-to-date", `up to date: ${remote.version}`);
|
|
115
|
+
if (current.info.builtAt && !(new Date(remote.builtAt) > new Date(current.info.builtAt))) return fail("not-newer", `not updating: ${remote.version} is no newer than ${current.info.version}`);
|
|
116
|
+
if ((remote.needs ?? 1) > api) return fail("needs-newer-app", `not updating to ${remote.version}: it needs a newer desktop app (${remote.needs}, this is ${api})`);
|
|
117
|
+
const dir = node_path.join(store, remote.version);
|
|
118
|
+
const partial = `${dir}.partial`;
|
|
119
|
+
try {
|
|
120
|
+
node_fs.rmSync(partial, {
|
|
121
|
+
recursive: true,
|
|
122
|
+
force: true
|
|
123
|
+
});
|
|
124
|
+
for (const file of remote.files) {
|
|
125
|
+
const out = node_path.normalize(node_path.join(partial, String(file.path)));
|
|
126
|
+
if (!out.startsWith(partial + node_path.sep) || typeof file.sha256 !== "string" || node_path.basename(out) === "version.json") throw new Error(`bad file ${file.path}`);
|
|
127
|
+
const data = await get(file.path, 6e4);
|
|
128
|
+
if (data.length !== file.size || (0, node_crypto.createHash)("sha256").update(data).digest("hex") !== file.sha256) throw new Error(`${file.path} doesn't match its checksum`);
|
|
129
|
+
node_fs.mkdirSync(node_path.dirname(out), { recursive: true });
|
|
130
|
+
node_fs.writeFileSync(out, data);
|
|
131
|
+
}
|
|
132
|
+
node_fs.writeFileSync(node_path.join(partial, "version.json"), JSON.stringify(remote));
|
|
133
|
+
node_fs.rmSync(dir, {
|
|
134
|
+
recursive: true,
|
|
135
|
+
force: true
|
|
136
|
+
});
|
|
137
|
+
node_fs.renameSync(partial, dir);
|
|
138
|
+
node_fs.writeFileSync(node_path.join(store, "current.json"), JSON.stringify({ version: remote.version }));
|
|
139
|
+
} catch (e) {
|
|
140
|
+
node_fs.rmSync(partial, {
|
|
141
|
+
recursive: true,
|
|
142
|
+
force: true
|
|
143
|
+
});
|
|
144
|
+
return fail("failed", `update to ${remote.version} failed: ${e.message}`);
|
|
145
|
+
}
|
|
146
|
+
const keep = /* @__PURE__ */ new Set([
|
|
147
|
+
remote.version,
|
|
148
|
+
current.dir === options.bundledDir ? "" : node_path.basename(current.dir),
|
|
149
|
+
"current.json"
|
|
150
|
+
]);
|
|
151
|
+
for (const name of node_fs.readdirSync(store)) if (!keep.has(name)) node_fs.rmSync(node_path.join(store, name), {
|
|
152
|
+
recursive: true,
|
|
153
|
+
force: true
|
|
154
|
+
});
|
|
155
|
+
log(`updated to ${remote.version}`);
|
|
156
|
+
return {
|
|
157
|
+
status: "updated",
|
|
158
|
+
version: remote.version
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
// src/electron/index.ts
|
|
162
|
+
/**
|
|
163
|
+
* The Electron main process for a web build as a desktop app: the build served over its own
|
|
164
|
+
* `app://` origin (so workers, fetch and localStorage behave as on the web), one window, one copy
|
|
165
|
+
* of the app at a time, links kept out of it, localStorage written to disk before quitting, and
|
|
166
|
+
* optionally updating itself from the site the web build is on.
|
|
167
|
+
*
|
|
168
|
+
* ```js
|
|
169
|
+
* // electron/main.mjs (package.json: "main": "electron/main.mjs")
|
|
170
|
+
* import { createDesktopApp } from '@n3rdw1z4rd/core/electron';
|
|
171
|
+
*
|
|
172
|
+
* createDesktopApp({
|
|
173
|
+
* title: 'My Game',
|
|
174
|
+
* root: new URL('../dist/', import.meta.url),
|
|
175
|
+
* update: { site: 'https://my-game.example.com' },
|
|
176
|
+
* });
|
|
177
|
+
* ```
|
|
178
|
+
*
|
|
179
|
+
* The page gets {@link Desktop} as `window.desktop` (see `desktop` in the main entry point). The
|
|
180
|
+
* packaged app needs `@n3rdw1z4rd/core` at run time: list it in `dependencies`. Node-only: not
|
|
181
|
+
* exported from the package root.
|
|
182
|
+
*
|
|
183
|
+
* @packageDocumentation
|
|
184
|
+
* @module @n3rdw1z4rd/core/electron
|
|
185
|
+
*/
|
|
186
|
+
const isFile = (file) => {
|
|
187
|
+
try {
|
|
188
|
+
return node_fs.statSync(file).isFile();
|
|
189
|
+
} catch {
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const toPath = (p) => p instanceof URL ? (0, node_url.fileURLToPath)(p) : node_path.resolve(p);
|
|
194
|
+
/** this module's own folder, in the ESM build (dist/electron/) and the CJS one (dist/) alike */
|
|
195
|
+
function defaultPreload() {
|
|
196
|
+
const here = typeof __dirname === "string" ? __dirname : node_path.dirname((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
197
|
+
const beside = node_path.join(here, "preload.cjs");
|
|
198
|
+
return node_fs.existsSync(beside) ? beside : node_path.join(here, "electron", "preload.cjs");
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Starts the desktop app. Call it once, straight away at start-up (it registers the `app://`
|
|
202
|
+
* scheme, which Electron only allows before the app is ready).
|
|
203
|
+
*
|
|
204
|
+
* @category Desktop
|
|
205
|
+
*/
|
|
206
|
+
function createDesktopApp(options) {
|
|
207
|
+
const title = options.title;
|
|
208
|
+
const bundledDir = toPath(options.root).replace(/[\\/]+$/, "");
|
|
209
|
+
const host = options.host ?? "app";
|
|
210
|
+
const origin = `app://${host}`;
|
|
211
|
+
const game = (options.profile ?? "game") === "game";
|
|
212
|
+
const argv = process.argv;
|
|
213
|
+
const siteOverride = options.siteEnv ? process.env[options.siteEnv] ?? "" : "";
|
|
214
|
+
const update = options.update || void 0;
|
|
215
|
+
const site = (siteOverride || update?.site || options.site || "").replace(/\/+$/, "");
|
|
216
|
+
const api = update?.api ?? 1;
|
|
217
|
+
const noUpdate = !update || argv.includes("--no-update");
|
|
218
|
+
const bundledOnly = !update || argv.includes("--bundled");
|
|
219
|
+
const forceWindowed = argv.includes("--windowed") || !!options.windowedEnv && process.env[options.windowedEnv] === "1";
|
|
220
|
+
const store = () => node_path.join(electron.app.getPath("userData"), update?.folder ?? "builds");
|
|
221
|
+
const windowFile = () => node_path.join(electron.app.getPath("userData"), "window.json");
|
|
222
|
+
const log = (message) => {
|
|
223
|
+
console.log(`[${title}] ${message}`);
|
|
224
|
+
try {
|
|
225
|
+
node_fs.mkdirSync(electron.app.getPath("userData"), { recursive: true });
|
|
226
|
+
node_fs.appendFileSync(node_path.join(electron.app.getPath("userData"), "desktop.log"), `${(/* @__PURE__ */ new Date()).toISOString()} ${message}\n`);
|
|
227
|
+
} catch {}
|
|
228
|
+
};
|
|
229
|
+
electron.protocol.registerSchemesAsPrivileged([{
|
|
230
|
+
scheme: "app",
|
|
231
|
+
privileges: {
|
|
232
|
+
standard: true,
|
|
233
|
+
secure: true,
|
|
234
|
+
supportFetchAPI: true,
|
|
235
|
+
corsEnabled: true,
|
|
236
|
+
stream: true
|
|
237
|
+
}
|
|
238
|
+
}]);
|
|
239
|
+
if (game) electron.app.commandLine.appendSwitch("autoplay-policy", "no-user-gesture-required");
|
|
240
|
+
if (options.softwareWebGL ?? true) electron.app.commandLine.appendSwitch("enable-unsafe-swiftshader");
|
|
241
|
+
if (!electron.app.requestSingleInstanceLock()) electron.app.quit();
|
|
242
|
+
let build = pickBuild({
|
|
243
|
+
bundledDir,
|
|
244
|
+
store: store(),
|
|
245
|
+
api,
|
|
246
|
+
bundledOnly
|
|
247
|
+
});
|
|
248
|
+
let win;
|
|
249
|
+
/** what to tell the page after an update's reloaded it (once) */
|
|
250
|
+
let note = null;
|
|
251
|
+
let needsNewerApp = false;
|
|
252
|
+
const fullscreen = options.window?.fullscreen ?? (game ? {
|
|
253
|
+
remember: true,
|
|
254
|
+
first: true
|
|
255
|
+
} : false);
|
|
256
|
+
const remembering = typeof fullscreen === "object" && !forceWindowed;
|
|
257
|
+
const startFullscreen = () => {
|
|
258
|
+
if (forceWindowed) return false;
|
|
259
|
+
if (typeof fullscreen === "boolean") return fullscreen;
|
|
260
|
+
try {
|
|
261
|
+
return JSON.parse(node_fs.readFileSync(windowFile(), "utf8")).fullscreen ?? fullscreen.first;
|
|
262
|
+
} catch {
|
|
263
|
+
return fullscreen.first;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
const rememberFullscreen = (on) => {
|
|
267
|
+
if (!remembering) return;
|
|
268
|
+
try {
|
|
269
|
+
node_fs.writeFileSync(windowFile(), JSON.stringify({ fullscreen: on }));
|
|
270
|
+
} catch {}
|
|
271
|
+
};
|
|
272
|
+
let checking;
|
|
273
|
+
const checkForUpdate = () => {
|
|
274
|
+
if (noUpdate || bundledOnly) return Promise.resolve(false);
|
|
275
|
+
if (!site) {
|
|
276
|
+
log("not updating: no site");
|
|
277
|
+
return Promise.resolve(false);
|
|
278
|
+
}
|
|
279
|
+
checking ??= (async () => {
|
|
280
|
+
try {
|
|
281
|
+
node_fs.mkdirSync(store(), { recursive: true });
|
|
282
|
+
const result = await downloadUpdate({
|
|
283
|
+
site,
|
|
284
|
+
store: store(),
|
|
285
|
+
current: build,
|
|
286
|
+
bundledDir,
|
|
287
|
+
api,
|
|
288
|
+
log,
|
|
289
|
+
fetch: (url, init) => electron.net.fetch(url, init)
|
|
290
|
+
});
|
|
291
|
+
if (result.status === "needs-newer-app") needsNewerApp = true;
|
|
292
|
+
if (result.status !== "updated") return false;
|
|
293
|
+
build = pickBuild({
|
|
294
|
+
bundledDir,
|
|
295
|
+
store: store(),
|
|
296
|
+
api
|
|
297
|
+
});
|
|
298
|
+
note = `Updated to ${result.version}`;
|
|
299
|
+
win?.webContents.reloadIgnoringCache();
|
|
300
|
+
return true;
|
|
301
|
+
} finally {
|
|
302
|
+
checking = void 0;
|
|
303
|
+
}
|
|
304
|
+
})();
|
|
305
|
+
return checking;
|
|
306
|
+
};
|
|
307
|
+
let resolveReady;
|
|
308
|
+
const ready = new Promise((resolve) => {
|
|
309
|
+
resolveReady = resolve;
|
|
310
|
+
});
|
|
311
|
+
function createWindow() {
|
|
312
|
+
const iconFile = node_path.join(bundledDir, options.window?.icon ?? "icon-512.png");
|
|
313
|
+
const w = new electron.BrowserWindow({
|
|
314
|
+
width: options.window?.width ?? 1280,
|
|
315
|
+
height: options.window?.height ?? 800,
|
|
316
|
+
minWidth: options.window?.minWidth,
|
|
317
|
+
minHeight: options.window?.minHeight,
|
|
318
|
+
fullscreen: startFullscreen(),
|
|
319
|
+
backgroundColor: options.window?.backgroundColor ?? "#000000",
|
|
320
|
+
title,
|
|
321
|
+
icon: node_fs.existsSync(iconFile) ? iconFile : void 0,
|
|
322
|
+
autoHideMenuBar: !options.menu,
|
|
323
|
+
webPreferences: {
|
|
324
|
+
preload: options.preload ?? defaultPreload(),
|
|
325
|
+
backgroundThrottling: !game,
|
|
326
|
+
contextIsolation: true,
|
|
327
|
+
sandbox: true
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
w.setMenu(options.menu ? electron.Menu.buildFromTemplate(options.menu) : null);
|
|
331
|
+
w.on("enter-full-screen", () => rememberFullscreen(true));
|
|
332
|
+
w.on("leave-full-screen", () => rememberFullscreen(false));
|
|
333
|
+
w.webContents.on("before-input-event", (e, input) => {
|
|
334
|
+
if (input.type === "keyDown" && input.key === "F11") {
|
|
335
|
+
w.setFullScreen(!w.isFullScreen());
|
|
336
|
+
e.preventDefault();
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
w.webContents.setWindowOpenHandler(({ url }) => {
|
|
340
|
+
if ((options.links ?? "browser") === "browser" && /^https:\/\//.test(url)) electron.shell.openExternal(url);
|
|
341
|
+
return { action: "deny" };
|
|
342
|
+
});
|
|
343
|
+
w.webContents.on("will-navigate", (e, url) => {
|
|
344
|
+
if (!url.startsWith(`${origin}/`)) e.preventDefault();
|
|
345
|
+
});
|
|
346
|
+
w.loadURL(`${origin}/index.html`);
|
|
347
|
+
return w;
|
|
348
|
+
}
|
|
349
|
+
electron.app.on("second-instance", () => {
|
|
350
|
+
if (!win) return;
|
|
351
|
+
if (win.isMinimized()) win.restore();
|
|
352
|
+
win.focus();
|
|
353
|
+
});
|
|
354
|
+
electron.app.whenReady().then(() => {
|
|
355
|
+
log(`running ${build.info.version} (${build.dir === bundledDir ? "bundled" : "downloaded"})`);
|
|
356
|
+
electron.protocol.handle("app", (request) => {
|
|
357
|
+
const url = new URL(request.url);
|
|
358
|
+
if (url.host !== host) return new Response("not found", { status: 404 });
|
|
359
|
+
const file = node_path.normalize(node_path.join(build.dir, decodeURIComponent(url.pathname)));
|
|
360
|
+
if (!file.startsWith(build.dir + node_path.sep) || !isFile(file)) return new Response("not found", { status: 404 });
|
|
361
|
+
return electron.net.fetch((0, node_url.pathToFileURL)(file).toString());
|
|
362
|
+
});
|
|
363
|
+
electron.ipcMain.on("desktop:quit", () => electron.app.quit());
|
|
364
|
+
electron.ipcMain.on("desktop:fullscreen", () => win?.setFullScreen(!win.isFullScreen()));
|
|
365
|
+
electron.ipcMain.on("desktop:site", (e) => {
|
|
366
|
+
e.returnValue = isUpdateSite(site) ? site : "";
|
|
367
|
+
});
|
|
368
|
+
electron.ipcMain.handle("desktop:version", () => ({
|
|
369
|
+
build: build.info.version,
|
|
370
|
+
builtAt: build.info.builtAt,
|
|
371
|
+
app: electron.app.getVersion(),
|
|
372
|
+
api,
|
|
373
|
+
needsNewerApp
|
|
374
|
+
}));
|
|
375
|
+
electron.ipcMain.handle("desktop:update-note", () => {
|
|
376
|
+
const n = note;
|
|
377
|
+
note = null;
|
|
378
|
+
return n;
|
|
379
|
+
});
|
|
380
|
+
electron.ipcMain.handle("desktop:update", () => checkForUpdate());
|
|
381
|
+
win = createWindow();
|
|
382
|
+
resolveReady(win);
|
|
383
|
+
if (update && (update.onStart ?? true)) checkForUpdate();
|
|
384
|
+
});
|
|
385
|
+
electron.app.on("window-all-closed", () => electron.app.quit());
|
|
386
|
+
const flush = () => {
|
|
387
|
+
try {
|
|
388
|
+
electron.session.defaultSession.flushStorageData();
|
|
389
|
+
} catch {}
|
|
390
|
+
};
|
|
391
|
+
let flushed = false;
|
|
392
|
+
electron.app.on("before-quit", (e) => {
|
|
393
|
+
if (flushed) return;
|
|
394
|
+
e.preventDefault();
|
|
395
|
+
flushed = true;
|
|
396
|
+
flush();
|
|
397
|
+
setTimeout(() => electron.app.quit(), 300);
|
|
398
|
+
});
|
|
399
|
+
electron.app.whenReady().then(() => setInterval(flush, 5e3));
|
|
400
|
+
return {
|
|
401
|
+
ready,
|
|
402
|
+
get window() {
|
|
403
|
+
return win;
|
|
404
|
+
},
|
|
405
|
+
get build() {
|
|
406
|
+
return build;
|
|
407
|
+
},
|
|
408
|
+
checkForUpdate,
|
|
409
|
+
log
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
exports.createDesktopApp = createDesktopApp;
|
|
413
|
+
exports.downloadUpdate = downloadUpdate;
|
|
414
|
+
exports.isUpdateSite = isUpdateSite;
|
|
415
|
+
exports.pickBuild = pickBuild;
|
|
416
|
+
exports.readBuild = readBuild;
|