Skip to content

Commit

Permalink
feat: Refactored Code into Multiple Files (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
viralgupta authored Aug 5, 2024
1 parent f38658b commit 7c4c550
Show file tree
Hide file tree
Showing 14 changed files with 831 additions and 715 deletions.
81 changes: 81 additions & 0 deletions src/NeutralinoProcess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { getBinaryName, normalize } = require("./utils.js");
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");

class NeutralinoProcess {
constructor({ url, windowOptions, WebSocketIPC }) {
this.WebSocketIPC = WebSocketIPC;
this.url = url;
this.windowOptions = windowOptions;
this.neuProcess = null;
}

init() {

if (this.WebSocketIPC.ws && this.WebSocketIPC.ws.readyState === this.WebSocketIPC.ws.OPEN) {
console.info("Already connected to the application.");
return;
}

this.WebSocketIPC.startWebsocket()

const EXEC_PERMISSION = 0o755;

let outputArgs = " --url=" + normalize(this.url);

for (let key in this.windowOptions) {
if (key == "processArgs") continue;

let cliKey = key.replace(/[A-Z]|^[a-z]/g, (token) => "-" + token.toLowerCase());

outputArgs += ` --window${cliKey}=${normalize(this.windowOptions[key])}`;
}

if (this.windowOptions && this.windowOptions.processArgs) {
outputArgs += " " + this.windowOptions.processArgs;
}

let arch = process.arch;

let binaryName = getBinaryName(arch);

if (!binaryName) {
return console.error(`Unsupported platform or CPU architecture: ${process.platform}_${arch}`);
}

let binaryPath = `bin${path.sep}${binaryName}`;

let args = " --load-dir-res --path=. --export-auth-info --neu-dev-extension";

if (outputArgs) args += " " + outputArgs;

if (process.platform == "linux" || process.platform == "darwin")
fs.chmodSync(binaryPath, EXEC_PERMISSION);

console.log(`Starting process: ${binaryName} ${args}`);

this.neuProcess = spawn(binaryPath, args.split(` `), { stdio: "inherit" });

this.neuProcess.on("exit", (code) => {
let statusCodeMsg = code ? `error code ${code}` : `success code 0`;
let runnerMsg = `${binaryName} was stopped with ${statusCodeMsg}`;
console.warn(runnerMsg);

this.WebSocketIPC.stopWebsocket()

if (this.windowOptions && this.windowOptions.exitProcessOnClose) {
process.exit(code);
}
});
}

close() {
this.WebSocketIPC.stopWebsocket();
if (this.neuProcess) {
this.neuProcess.kill();
}
}
}

module.exports = NeutralinoProcess;
48 changes: 48 additions & 0 deletions src/apis/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { base64ToBytesArray, arrayBufferToBase64 } = require("../utils.js");

class Clipboard {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

getFormat () {
return this.WebsocketIPC.sendMessage("clipboard.getFormat");
}

readText () {
return this.WebsocketIPC.sendMessage("clipboard.readText");
}

readImage () {
return new Promise((resolve, reject) => {
this.WebsocketIPC.sendMessage("clipboard.readImage")
.then((image) => {
if (image) {
image.data = base64ToBytesArray(image.data);
}
resolve(image);
})
.catch((error) => {
reject(error);
});
});
}

writeText (data) {
return this.WebsocketIPC.sendMessage("clipboard.writeText", { data });
}

writeImage (image) {
const props = { ...image };
if (image?.data) {
props.data = arrayBufferToBase64(image.data);
}
return this.WebsocketIPC.sendMessage("clipboard.writeImage", props);
}

clear () {
return this.WebsocketIPC.sendMessage("clipboard.clear");
}
}

module.exports = Clipboard;
36 changes: 36 additions & 0 deletions src/apis/computer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

class Computer {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

getMemoryInfo() {
return this.WebsocketIPC.sendMessage("computer.getMemoryInfo");
}

getArch() {
return this.WebsocketIPC.sendMessage("computer.getArch");
}

getKernelInfo() {
return this.WebsocketIPC.sendMessage("computer.getKernelInfo");
}

getOSInfo() {
return this.WebsocketIPC.sendMessage("computer.getOSInfo");
}

getCPUInfo() {
return this.WebsocketIPC.sendMessage("computer.getCPUInfo");
}

getDisplays() {
return this.WebsocketIPC.sendMessage("computer.getDisplays");
}

getMousePosition() {
return this.WebsocketIPC.sendMessage("computer.getMousePosition");
}
}

module.exports = Computer;
12 changes: 12 additions & 0 deletions src/apis/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Custom {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

getMethods() {
return this.WebsocketIPC.sendMessage('custom.getMethods');
}
}

module.exports = Custom;
12 changes: 12 additions & 0 deletions src/apis/debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

class Debug {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

log (message, type) {
return this.WebsocketIPC.sendMessage('debug.log', { message, type });
}
}

module.exports = Debug;
36 changes: 36 additions & 0 deletions src/apis/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

class Events {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

broadcast (event, data) {
return this.WebsocketIPC.sendMessage('events.broadcast', { event, data });
}

on (event, listener) {
this.WebsocketIPC.eventEmitter.on(event, listener);
return Promise.resolve({
success: true,
message: 'Event listener added'
});
}

off (event, listener) {
this.WebsocketIPC.eventEmitter.off(event, listener);
return Promise.resolve({
success: true,
message: 'Event listener removed'
});
}

dispatch (event, data) {
this.WebsocketIPC.eventEmitter.emit(event, data);
return Promise.resolve({
success: true,
message: 'Message dispatched'
});
}
}

module.exports = Events;
44 changes: 44 additions & 0 deletions src/apis/extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

class Extensions {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

dispatch (extensionId, event, data) {
return new Promise(async (resolve, reject) => {
const stats = await this.getStats();
if (!stats.loaded.includes(extensionId)) {
reject({
code: 'NE_EX_EXTNOTL',
message: `${extensionId} is not loaded`
});
}
else if (stats.connected.includes(extensionId)) {
try {
const result = await this.WebsocketIPC.sendMessage('extensions.dispatch', { extensionId, event, data });
resolve(result);
}
catch (err) {
reject(err);
}
}
else {
// loaded but not connected yet.
this.WebsocketIPC.sendWhenExtReady(extensionId, {
method: 'extensions.dispatch',
data: { extensionId, event, data }, resolve, reject
});
}
});
}

broadcast (event, data) {
return this.WebsocketIPC.sendMessage('extensions.broadcast', { event, data });
}

getStats () {
return this.WebsocketIPC.sendMessage('extensions.getStats');
}
}

module.exports = Extensions;
95 changes: 95 additions & 0 deletions src/apis/filesystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const { arrayBufferToBase64, base64ToBytesArray } = require("../utils.js");

class FileSystem {
constructor(WebSocketIPC) {
this.WebsocketIPC = WebSocketIPC;
}

createDirectory (path) {
return this.WebsocketIPC.sendMessage('filesystem.createDirectory', { path });
}

remove (path) {
return this.WebsocketIPC.sendMessage('filesystem.remove', { path });
}

writeFile (path, data) {
return this.WebsocketIPC.sendMessage('filesystem.writeFile', { path, data });
}

appendFile (path, data) {
return this.WebsocketIPC.sendMessage('filesystem.appendFile', { path, data });
}

writeBinaryFile (path, data) {
return this.WebsocketIPC.sendMessage('filesystem.writeBinaryFile', {
path,
data: arrayBufferToBase64(data)
});
}

appendBinaryFile (path, data) {
return this.WebsocketIPC.sendMessage('filesystem.appendBinaryFile', {
path,
data: arrayBufferToBase64(data)
});
}

readFile (path, options) {
return this.WebsocketIPC.sendMessage('filesystem.readFile', { path, ...options });
}

readBinaryFile (path, options) {
return new Promise((resolve, reject) => {
this.WebsocketIPC.sendMessage('filesystem.readBinaryFile', { path, ...options })
.then((base64Data) => {
resolve(base64ToBytesArray(base64Data));
})
.catch((error) => {
reject(error);
});
});
}

openFile (path) {
return this.WebsocketIPC.sendMessage('filesystem.openFile', { path });
}

createWatcher (path) {
return this.WebsocketIPC.sendMessage('filesystem.createWatcher', { path });
}

removeWatcher (id) {
return this.WebsocketIPC.sendMessage('filesystem.removeWatcher', { id });
}

getWatchers () {
return this.WebsocketIPC.sendMessage('filesystem.getWatchers');
}

updateOpenedFile (id, event, data) {
return this.WebsocketIPC.sendMessage('filesystem.updateOpenedFile', { id, event, data });
}

getOpenedFileInfo (id) {
return this.WebsocketIPC.sendMessage('filesystem.getOpenedFileInfo', { id });
}

readDirectory (path, options) {
return this.WebsocketIPC.sendMessage('filesystem.readDirectory', { path, ...options });
}

copy (source, destination) {
return this.WebsocketIPC.sendMessage('filesystem.copy', { source, destination });
}

move (source, destination) {
return this.WebsocketIPC.sendMessage('filesystem.move', { source, destination });
}

getStats (path) {
return this.WebsocketIPC.sendMessage('filesystem.getStats', { path });
}
}

module.exports = FileSystem;
Loading

0 comments on commit 7c4c550

Please sign in to comment.