Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into feature/laravel-reverb
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhamp authored Sep 11, 2024
2 parents 605589e + 861dfab commit 0eb6eb9
Show file tree
Hide file tree
Showing 24 changed files with 11,493 additions and 8,768 deletions.
1 change: 1 addition & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: simonhamp
5 changes: 5 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Security contact information

To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.
228 changes: 135 additions & 93 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,137 +11,179 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const electron_1 = require("electron");
const electron_updater_1 = require("electron-updater");
const state_1 = __importDefault(require("./server/state"));
const utils_1 = require("@electron-toolkit/utils");
const server_1 = require("./server");
const utils_2 = require("./server/utils");
const electron_1 = require("electron");
const path_1 = require("path");
const ps_node_1 = __importDefault(require("ps-node"));
let phpProcesses = [];
let websocketProcess;
let schedulerInterval;
const killChildProcesses = () => {
let processes = [
...phpProcesses,
websocketProcess,
].filter((p) => p !== undefined);
processes.forEach((process) => {
try {
ps_node_1.default.kill(process.pid);
}
catch (err) {
console.error(err);
}
});
};
class NativePHP {
constructor() {
this.processes = [];
this.schedulerInterval = undefined;
}
bootstrap(app, icon, phpBinary, cert) {
require('@electron/remote/main').initialize();
require("@electron/remote/main").initialize();
state_1.default.icon = icon;
state_1.default.php = phpBinary;
state_1.default.caCert = cert;
this.bootstrapApp(app);
this.addEventListeners(app);
this.addTerminateListeners(app);
}
addEventListeners(app) {
app.on('open-url', (event, url) => {
(0, utils_2.notifyLaravel)('events', {
event: '\\Native\\Laravel\\Events\\App\\OpenedFromURL',
payload: [url]
app.on("open-url", (event, url) => {
(0, utils_2.notifyLaravel)("events", {
event: "\\Native\\Laravel\\Events\\App\\OpenedFromURL",
payload: [url],
});
});
app.on('open-file', (event, path) => {
(0, utils_2.notifyLaravel)('events', {
event: '\\Native\\Laravel\\Events\\App\\OpenFile',
payload: [path]
app.on("open-file", (event, path) => {
(0, utils_2.notifyLaravel)("events", {
event: "\\Native\\Laravel\\Events\\App\\OpenFile",
payload: [path],
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
}
addTerminateListeners(app) {
app.on('before-quit', (e) => {
if (schedulerInterval) {
clearInterval(schedulerInterval);
app.on("before-quit", () => {
if (this.schedulerInterval) {
clearInterval(this.schedulerInterval);
}
this.killChildProcesses();
});
app.on("browser-window-created", (_, window) => {
utils_1.optimizer.watchWindowShortcuts(window);
});
app.on("activate", function (event, hasVisibleWindows) {
if (!hasVisibleWindows) {
(0, utils_2.notifyLaravel)("booted");
}
killChildProcesses();
event.preventDefault();
});
}
bootstrapApp(app) {
let nativePHPConfig = {};
(0, server_1.retrieveNativePHPConfig)().then((result) => {
return __awaiter(this, void 0, void 0, function* () {
yield app.whenReady();
const config = yield this.loadConfig();
this.setDockIcon();
this.setAppUserModelId(config);
this.setDeepLinkHandler(config);
this.startAutoUpdater(config);
yield this.startElectronApi();
state_1.default.phpIni = yield this.loadPhpIni();
yield this.startPhpApp();
yield this.startQueueWorker();
yield this.startWebsockets();
this.startScheduler();
yield (0, utils_2.notifyLaravel)("booted");
});
}
loadConfig() {
return __awaiter(this, void 0, void 0, function* () {
let config = {};
try {
nativePHPConfig = JSON.parse(result.stdout);
const result = yield (0, server_1.retrieveNativePHPConfig)();
config = JSON.parse(result.stdout);
}
catch (e) {
console.error(e);
catch (error) {
console.error(error);
}
}).catch((err) => {
console.error(err);
}).finally(() => {
this.setupApp(nativePHPConfig);
return config;
});
}
setupApp(nativePHPConfig) {
electron_1.app.whenReady().then(() => __awaiter(this, void 0, void 0, function* () {
var _a;
if (process.platform === 'darwin' && process.env.NODE_ENV === 'development') {
electron_1.app.dock.setIcon(state_1.default.icon);
setDockIcon() {
if (process.platform === "darwin" &&
process.env.NODE_ENV === "development") {
electron_1.app.dock.setIcon(state_1.default.icon);
}
}
setAppUserModelId(config) {
utils_1.electronApp.setAppUserModelId(config === null || config === void 0 ? void 0 : config.app_id);
}
setDeepLinkHandler(config) {
const deepLinkProtocol = config === null || config === void 0 ? void 0 : config.deeplink_scheme;
if (deepLinkProtocol) {
if (process.defaultApp) {
if (process.argv.length >= 2) {
electron_1.app.setAsDefaultProtocolClient(deepLinkProtocol, process.execPath, [
(0, path_1.resolve)(process.argv[1]),
]);
}
}
electron_1.app.on('browser-window-created', (_, window) => {
utils_1.optimizer.watchWindowShortcuts(window);
});
let phpIniSettings = {};
else {
electron_1.app.setAsDefaultProtocolClient(deepLinkProtocol);
}
}
}
startAutoUpdater(config) {
var _a;
if (((_a = config === null || config === void 0 ? void 0 : config.updater) === null || _a === void 0 ? void 0 : _a.enabled) === true) {
electron_updater_1.autoUpdater.checkForUpdatesAndNotify();
}
}
startElectronApi() {
return __awaiter(this, void 0, void 0, function* () {
const electronApi = yield (0, server_1.startAPI)();
state_1.default.electronApiPort = electronApi.port;
console.log("Electron API server started on port", electronApi.port);
});
}
loadPhpIni() {
return __awaiter(this, void 0, void 0, function* () {
let config = {};
try {
let { stdout } = yield (0, server_1.retrievePhpIniSettings)();
phpIniSettings = JSON.parse(stdout);
const result = yield (0, server_1.retrievePhpIniSettings)();
config = JSON.parse(result.stdout);
}
catch (e) {
console.error(e);
catch (error) {
console.error(error);
}
utils_1.electronApp.setAppUserModelId(nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig.app_id);
const deepLinkProtocol = nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig.deeplink_scheme;
if (deepLinkProtocol) {
if (process.defaultApp) {
if (process.argv.length >= 2) {
electron_1.app.setAsDefaultProtocolClient(deepLinkProtocol, process.execPath, [(0, path_1.resolve)(process.argv[1])]);
}
}
else {
electron_1.app.setAsDefaultProtocolClient(deepLinkProtocol);
}
return config;
});
}
startPhpApp() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield (0, server_1.startPhpApp)());
});
}
startQueueWorker() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield (0, server_1.startQueue)());
});
}
startWebsockets() {
return __awaiter(this, void 0, void 0, function* () {
this.processes.push(yield (0, server_1.startWebsockets)());
});
}
startScheduler() {
const now = new Date();
const delay = (60 - now.getSeconds()) * 1000 + (1000 - now.getMilliseconds());
setTimeout(() => {
console.log("Running scheduler...");
(0, server_1.runScheduler)();
this.schedulerInterval = setInterval(() => {
console.log("Running scheduler...");
(0, server_1.runScheduler)();
}, 60 * 1000);
}, delay);
}
killChildProcesses() {
this.processes
.filter((p) => p !== undefined)
.forEach((process) => {
try {
ps_node_1.default.kill(process.pid);
}
const apiPort = yield (0, server_1.startAPI)();
console.log('API server started on port', apiPort.port);
phpProcesses = yield (0, server_1.servePhpApp)(apiPort.port, phpIniSettings);
websocketProcess = (0, server_1.serveWebsockets)();
yield (0, utils_2.notifyLaravel)('booted');
if (((_a = nativePHPConfig === null || nativePHPConfig === void 0 ? void 0 : nativePHPConfig.updater) === null || _a === void 0 ? void 0 : _a.enabled) === true) {
electron_updater_1.autoUpdater.checkForUpdatesAndNotify();
catch (err) {
console.error(err);
}
let now = new Date();
let delay = (60 - now.getSeconds()) * 1000 + (1000 - now.getMilliseconds());
setTimeout(() => {
console.log("Running scheduler...");
(0, server_1.runScheduler)(apiPort.port, phpIniSettings);
schedulerInterval = setInterval(() => {
console.log("Running scheduler...");
(0, server_1.runScheduler)(apiPort.port, phpIniSettings);
}, 60 * 1000);
}, delay);
electron_1.app.on('activate', function (event, hasVisibleWindows) {
if (!hasVisibleWindows) {
(0, utils_2.notifyLaravel)('booted');
}
event.preventDefault();
});
}));
});
}
}
module.exports = new NativePHP();
2 changes: 2 additions & 0 deletions dist/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const contextMenu_1 = __importDefault(require("./api/contextMenu"));
const settings_1 = __importDefault(require("./api/settings"));
const shell_1 = __importDefault(require("./api/shell"));
const progressBar_1 = __importDefault(require("./api/progressBar"));
const powerMonitor_1 = __importDefault(require("./api/powerMonitor"));
function startAPIServer(randomSecret) {
return __awaiter(this, void 0, void 0, function* () {
const port = yield (0, get_port_1.default)({
Expand All @@ -58,6 +59,7 @@ function startAPIServer(randomSecret) {
httpServer.use("/api/context", contextMenu_1.default);
httpServer.use("/api/menu-bar", menuBar_1.default);
httpServer.use("/api/progress-bar", progressBar_1.default);
httpServer.use("/api/power-monitor", powerMonitor_1.default);
if (process.env.NODE_ENV === "development") {
httpServer.use("/api/debug", debug_1.default);
}
Expand Down
4 changes: 4 additions & 0 deletions dist/server/api/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ const express_1 = __importDefault(require("express"));
const state_1 = __importDefault(require("../state"));
const router = express_1.default.Router();
router.post('/log', (req, res) => {
var _a;
const { level, message, context } = req.body;
Object.values(state_1.default.windows).forEach(window => {
window.webContents.send('log', { level, message, context });
});
if ((_a = state_1.default.activeMenuBar) === null || _a === void 0 ? void 0 : _a.window) {
state_1.default.activeMenuBar.window.webContents.send('log', { level, message, context });
}
res.sendStatus(200);
});
exports.default = router;
62 changes: 62 additions & 0 deletions dist/server/api/powerMonitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = __importDefault(require("express"));
const electron_1 = require("electron");
const utils_1 = require("../utils");
const router = express_1.default.Router();
router.get('/get-system-idle-state', (req, res) => {
res.json({
result: electron_1.powerMonitor.getSystemIdleState(req.body.threshold),
});
});
router.get('/get-system-idle-time', (req, res) => {
res.json({
result: electron_1.powerMonitor.getSystemIdleTime(),
});
});
router.get('/get-current-thermal-state', (req, res) => {
res.json({
result: electron_1.powerMonitor.getCurrentThermalState(),
});
});
router.get('/is-on-battery-power', (req, res) => {
res.json({
result: electron_1.powerMonitor.isOnBatteryPower(),
});
});
electron_1.powerMonitor.addListener('on-ac', () => {
(0, utils_1.notifyLaravel)("events", {
event: `\\Native\\Laravel\\Events\\PowerMonitor\\PowerStateChanged`,
payload: {
state: 'on-ac'
}
});
});
electron_1.powerMonitor.addListener('on-battery', () => {
(0, utils_1.notifyLaravel)("events", {
event: `\\Native\\Laravel\\Events\\PowerMonitor\\PowerStateChanged`,
payload: {
state: 'on-battery'
}
});
});
electron_1.powerMonitor.addListener('thermal-state-change', (state) => {
(0, utils_1.notifyLaravel)("events", {
event: `\\Native\\Laravel\\Events\\PowerMonitor\\ThermalStateChanged`,
payload: {
state
}
});
});
electron_1.powerMonitor.addListener('speed-limit-change', (limit) => {
(0, utils_1.notifyLaravel)("events", {
event: `\\Native\\Laravel\\Events\\PowerMonitor\\SpeedLimitChanged`,
payload: {
limit
}
});
});
exports.default = router;
Loading

0 comments on commit 0eb6eb9

Please sign in to comment.