-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactored Code into Multiple Files (#14)
- Loading branch information
1 parent
f38658b
commit 7c4c550
Showing
14 changed files
with
831 additions
and
715 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.