Skip to content

Commit

Permalink
WIP: fs adapter calling server API
Browse files Browse the repository at this point in the history
  • Loading branch information
linhtran174 committed Nov 29, 2023
1 parent ed5413a commit 64a58d1
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
143 changes: 127 additions & 16 deletions core/src/fs.ts
Original file line number Diff line number Diff line change
@@ -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<any>} A Promise that resolves when the file is written successfully.
*/
const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
window.coreAPI?.writeFile(path, data) ??
window.electronAPI?.writeFile(path, data);

const writeFile = (path: string, data: string): Promise<any> => {
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<boolean> =>
window.coreAPI?.isDirectory(path) ?? window.electronAPI?.isDirectory(path);
const isDirectory = (path: string): Promise<boolean> => {
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<any>} A Promise that resolves with the contents of the file.
*/
const readFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.readFile(path) ?? window.electronAPI?.readFile(path);
const readFile = (path: string): Promise<any> => {
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<any>} A Promise that resolves with the contents of the directory.
*/
const listFiles: (path: string) => Promise<any> = (path) =>
window.coreAPI?.listFiles(path) ?? window.electronAPI?.listFiles(path);
const listFiles = (path: string): Promise<any> => {
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<any>} A Promise that resolves when the directory is created successfully.
*/
const mkdir: (path: string) => Promise<any> = (path) =>
window.coreAPI?.mkdir(path) ?? window.electronAPI?.mkdir(path);
const mkdir = (path: string): Promise<any> => {
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<any>} A Promise that resolves when the directory is removed successfully.
*/
const rmdir: (path: string) => Promise<any> = (path) =>
window.coreAPI?.rmdir(path) ?? window.electronAPI?.rmdir(path);
const rmdir = (path: string): Promise<any> => {
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<any>} A Promise that resolves when the file is deleted.
*/
const deleteFile: (path: string) => Promise<any> = (path) =>
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
const deleteFile = (path: string): Promise<any> => {
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,
Expand Down
35 changes: 17 additions & 18 deletions server/main.ts
Original file line number Diff line number Diff line change
@@ -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()
}
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 64a58d1

Please sign in to comment.