WIP: fs adapter calling server API
This commit is contained in:
parent
ed5413a1ee
commit
64a58d1e15
2
Makefile
2
Makefile
@ -17,7 +17,7 @@ install-and-build: build-uikit
|
|||||||
ifeq ($(OS),Windows_NT)
|
ifeq ($(OS),Windows_NT)
|
||||||
powershell -Command "yarn config set network-timeout 300000; \
|
powershell -Command "yarn config set network-timeout 300000; \
|
||||||
$$env:NITRO_VERSION = Get-Content .\\plugins\\inference-plugin\\nitro\\version.txt; \
|
$$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
|
else
|
||||||
yarn build:core
|
yarn build:core
|
||||||
yarn install
|
yarn install
|
||||||
|
|||||||
143
core/src/fs.ts
143
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.
|
* Writes data to a file at the specified path.
|
||||||
* @param {string} path - The path to the file.
|
* @param {string} path - The path to the file.
|
||||||
* @param {string} data - The data to write 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.
|
* @returns {Promise<any>} A Promise that resolves when the file is written successfully.
|
||||||
*/
|
*/
|
||||||
const writeFile: (path: string, data: string) => Promise<any> = (path, data) =>
|
const writeFile = (path: string, data: string): Promise<any> => {
|
||||||
window.coreAPI?.writeFile(path, data) ??
|
return fetchRetry(JAN_FS_API, {
|
||||||
window.electronAPI?.writeFile(path, data);
|
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.
|
* Checks whether the path is a directory.
|
||||||
* @param path - The path to check.
|
* @param path - The path to check.
|
||||||
* @returns {boolean} A boolean indicating whether the path is a directory.
|
* @returns {boolean} A boolean indicating whether the path is a directory.
|
||||||
*/
|
*/
|
||||||
const isDirectory = (path: string): Promise<boolean> =>
|
const isDirectory = (path: string): Promise<boolean> => {
|
||||||
window.coreAPI?.isDirectory(path) ?? window.electronAPI?.isDirectory(path);
|
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.
|
* Reads the contents of a file at the specified path.
|
||||||
* @param {string} path - The path of the file to read.
|
* @param {string} path - The path of the file to read.
|
||||||
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
|
* @returns {Promise<any>} A Promise that resolves with the contents of the file.
|
||||||
*/
|
*/
|
||||||
const readFile: (path: string) => Promise<any> = (path) =>
|
const readFile = (path: string): Promise<any> => {
|
||||||
window.coreAPI?.readFile(path) ?? window.electronAPI?.readFile(path);
|
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
|
* List the directory files
|
||||||
* @param {string} path - The path of the directory to list 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.
|
* @returns {Promise<any>} A Promise that resolves with the contents of the directory.
|
||||||
*/
|
*/
|
||||||
const listFiles: (path: string) => Promise<any> = (path) =>
|
const listFiles = (path: string): Promise<any> => {
|
||||||
window.coreAPI?.listFiles(path) ?? window.electronAPI?.listFiles(path);
|
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.
|
* Creates a directory at the specified path.
|
||||||
* @param {string} path - The path of the directory to create.
|
* @param {string} path - The path of the directory to create.
|
||||||
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
|
* @returns {Promise<any>} A Promise that resolves when the directory is created successfully.
|
||||||
*/
|
*/
|
||||||
const mkdir: (path: string) => Promise<any> = (path) =>
|
const mkdir = (path: string): Promise<any> => {
|
||||||
window.coreAPI?.mkdir(path) ?? window.electronAPI?.mkdir(path);
|
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.
|
* Removes a directory at the specified path.
|
||||||
* @param {string} path - The path of the directory to remove.
|
* @param {string} path - The path of the directory to remove.
|
||||||
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
|
* @returns {Promise<any>} A Promise that resolves when the directory is removed successfully.
|
||||||
*/
|
*/
|
||||||
const rmdir: (path: string) => Promise<any> = (path) =>
|
const rmdir = (path: string): Promise<any> => {
|
||||||
window.coreAPI?.rmdir(path) ?? window.electronAPI?.rmdir(path);
|
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.
|
* Deletes a file from the local file system.
|
||||||
* @param {string} path - The path of the file to delete.
|
* @param {string} path - The path of the file to delete.
|
||||||
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
|
* @returns {Promise<any>} A Promise that resolves when the file is deleted.
|
||||||
*/
|
*/
|
||||||
const deleteFile: (path: string) => Promise<any> = (path) =>
|
const deleteFile = (path: string): Promise<any> => {
|
||||||
window.coreAPI?.deleteFile(path) ?? window.electronAPI?.deleteFile(path);
|
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 = {
|
export const fs = {
|
||||||
isDirectory,
|
isDirectory,
|
||||||
|
|||||||
@ -1,32 +1,31 @@
|
|||||||
import { join } from 'path'
|
|
||||||
import { setupMenu } from './utils/menu'
|
import { setupMenu } from './utils/menu'
|
||||||
import { handleFsIPCs } from './handlers/fs'
|
|
||||||
import app from 'express'
|
import app from 'express'
|
||||||
|
import bodyParser from 'body-parser'
|
||||||
|
import fs from 'fs'
|
||||||
/**
|
/**
|
||||||
* Managers
|
* Managers
|
||||||
**/
|
**/
|
||||||
import { ModuleManager } from './managers/module'
|
import { ModuleManager } from './managers/module'
|
||||||
import { PluginManager } from './managers/plugin'
|
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.migratePlugins()
|
||||||
PluginManager.instance.setupPlugins()
|
PluginManager.instance.setupPlugins()
|
||||||
setupMenu()
|
setupMenu()
|
||||||
handleIPCs()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles various IPC messages from the renderer process.
|
|
||||||
*/
|
|
||||||
function handleIPCs() {
|
|
||||||
handleFsIPCs()
|
|
||||||
handleDownloaderIPCs()
|
|
||||||
handlePluginIPCs()
|
|
||||||
}
|
|
||||||
|
|||||||
@ -67,7 +67,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@npmcli/arborist": "^7.1.0",
|
"@npmcli/arborist": "^7.1.0",
|
||||||
"@types/express": "^4.17.21",
|
|
||||||
"@types/request": "^2.48.12",
|
"@types/request": "^2.48.12",
|
||||||
"@uiball/loaders": "^1.3.0",
|
"@uiball/loaders": "^1.3.0",
|
||||||
"electron-store": "^8.1.0",
|
"electron-store": "^8.1.0",
|
||||||
@ -81,6 +80,8 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@electron/notarize": "^2.1.0",
|
"@electron/notarize": "^2.1.0",
|
||||||
"@playwright/test": "^1.38.1",
|
"@playwright/test": "^1.38.1",
|
||||||
|
"@types/body-parser": "^1.19.5",
|
||||||
|
"@types/express": "^4.17.21",
|
||||||
"@types/npmcli__arborist": "^5.6.4",
|
"@types/npmcli__arborist": "^5.6.4",
|
||||||
"@types/pacote": "^11.1.7",
|
"@types/pacote": "^11.1.7",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.7.3",
|
"@typescript-eslint/eslint-plugin": "^6.7.3",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user