diff --git a/Makefile b/Makefile index af416304c..e0f6cb274 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ install-and-build: build-uikit ifeq ($(OS),Windows_NT) powershell -Command "yarn config set network-timeout 300000; \ $$env:NITRO_VERSION = Get-Content .\\plugins\\inference-plugin\\nitro\\version.txt; \ - Write-Output \"Nitro version: $$env:NITRO_VERSION\"; yarn build:core; yarn install; yarn build:plugins" + Write-Output \"Nitro version: $$env:NITRO_VERSION\"; yarn build:core; yarn install; yarn build:plugins" else yarn build:core yarn install diff --git a/core/src/fs.ts b/core/src/fs.ts index 2c94a2ce8..b8722e1a6 100644 --- a/core/src/fs.ts +++ b/core/src/fs.ts @@ -1,59 +1,170 @@ +const fetchRetry = require("fetch-retry")(global.fetch); + +const PORT = 1337; +const LOCAL_HOST = "127.0.0.1"; +const JAN_HTTP_SERVER_URL = `http://${LOCAL_HOST}:${PORT}`; +const JAN_FS_API = `${JAN_HTTP_SERVER_URL}/fs`; /** * Writes data to a file at the specified path. * @param {string} path - The path to the file. * @param {string} data - The data to write to the file. * @returns {Promise} A Promise that resolves when the file is written successfully. */ -const writeFile: (path: string, data: string) => Promise = (path, data) => - window.coreAPI?.writeFile(path, data) ?? - window.electronAPI?.writeFile(path, data); - +const writeFile = (path: string, data: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'writeFile', + path, + data + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`writeFile: ${path} failed`); + }) +} + /** * Checks whether the path is a directory. * @param path - The path to check. * @returns {boolean} A boolean indicating whether the path is a directory. */ -const isDirectory = (path: string): Promise => - window.coreAPI?.isDirectory(path) ?? window.electronAPI?.isDirectory(path); +const isDirectory = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'isDirectory', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`isDirectory: ${path} failed`); + }) +} /** * Reads the contents of a file at the specified path. * @param {string} path - The path of the file to read. * @returns {Promise} A Promise that resolves with the contents of the file. */ -const readFile: (path: string) => Promise = (path) => - window.coreAPI?.readFile(path) ?? window.electronAPI?.readFile(path); +const readFile = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'readFile', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`readFile: ${path} failed`); + }) +} /** * List the directory files * @param {string} path - The path of the directory to list files. * @returns {Promise} A Promise that resolves with the contents of the directory. */ -const listFiles: (path: string) => Promise = (path) => - window.coreAPI?.listFiles(path) ?? window.electronAPI?.listFiles(path); +const listFiles = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'listFiles', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`listFiles: ${path} failed`); + }) +} /** * Creates a directory at the specified path. * @param {string} path - The path of the directory to create. * @returns {Promise} A Promise that resolves when the directory is created successfully. */ -const mkdir: (path: string) => Promise = (path) => - window.coreAPI?.mkdir(path) ?? window.electronAPI?.mkdir(path); +const mkdir = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'mkdir', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`mkdir: ${path} failed`); + }) +} /** * Removes a directory at the specified path. * @param {string} path - The path of the directory to remove. * @returns {Promise} A Promise that resolves when the directory is removed successfully. */ -const rmdir: (path: string) => Promise = (path) => - window.coreAPI?.rmdir(path) ?? window.electronAPI?.rmdir(path); +const rmdir = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'rmdir', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`rmdir: ${path} failed`); + }) +} /** * Deletes a file from the local file system. * @param {string} path - The path of the file to delete. * @returns {Promise} A Promise that resolves when the file is deleted. */ -const deleteFile: (path: string) => Promise = (path) => - window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path); +const deleteFile = (path: string): Promise => { + return fetchRetry(JAN_FS_API, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + op: 'deleteFile', + path, + }), + retries: 3, + retryDelay: 500, + }).catch((err: any) => { + console.error(err); + throw new Error(`deleteFile: ${path} failed`); + }) +} export const fs = { isDirectory, diff --git a/server/main.ts b/server/main.ts index a4f4df1bb..5ba2045c1 100644 --- a/server/main.ts +++ b/server/main.ts @@ -1,32 +1,31 @@ -import { join } from 'path' import { setupMenu } from './utils/menu' -import { handleFsIPCs } from './handlers/fs' import app from 'express' - +import bodyParser from 'body-parser' +import fs from 'fs' /** * Managers **/ import { ModuleManager } from './managers/module' import { PluginManager } from './managers/plugin' -/** - * IPC Handlers - **/ -import { handleDownloaderIPCs } from './handlers/download' -import { handlePluginIPCs } from './handlers/plugin' -app().listen(6969, ()=>{ +const server = app() +server.use(bodyParser) + +const USER_ROOT_DIR = '.data' +server.post("fs", (req, res) => { + let op = req.body.op; + switch(op){ + case 'readFile': + fs.readFile(req.body.path, ()=>{}) + case 'writeFile': + fs.writeFile(req.body.path, Buffer.from(req.body.data, "base64"), ()=>{}) + } +}) + +server.listen(1337, ()=>{ PluginManager.instance.migratePlugins() PluginManager.instance.setupPlugins() setupMenu() - handleIPCs() }) -/** - * Handles various IPC messages from the renderer process. - */ -function handleIPCs() { - handleFsIPCs() - handleDownloaderIPCs() - handlePluginIPCs() -} diff --git a/server/package.json b/server/package.json index 8185f37d9..9523fa000 100644 --- a/server/package.json +++ b/server/package.json @@ -67,7 +67,6 @@ }, "dependencies": { "@npmcli/arborist": "^7.1.0", - "@types/express": "^4.17.21", "@types/request": "^2.48.12", "@uiball/loaders": "^1.3.0", "electron-store": "^8.1.0", @@ -81,6 +80,8 @@ "devDependencies": { "@electron/notarize": "^2.1.0", "@playwright/test": "^1.38.1", + "@types/body-parser": "^1.19.5", + "@types/express": "^4.17.21", "@types/npmcli__arborist": "^5.6.4", "@types/pacote": "^11.1.7", "@typescript-eslint/eslint-plugin": "^6.7.3",