basic version of forms
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
master.forms
|
||||||
5
app.js
5
app.js
@@ -1,6 +1,6 @@
|
|||||||
import { app, BrowserWindow, globalShortcut } from 'electron';
|
import { app, BrowserWindow, globalShortcut } from 'electron';
|
||||||
import paths from 'path'
|
import paths from 'path'
|
||||||
import "./server/index.js"
|
import Server from "./server/index.js"
|
||||||
|
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
@@ -68,7 +68,8 @@ function toggleDevTools(win) {
|
|||||||
|
|
||||||
app.on("ready", async () => {
|
app.on("ready", async () => {
|
||||||
// await Forms.init();
|
// await Forms.init();
|
||||||
createWindow({onTop: false});
|
// createWindow({onTop: false});
|
||||||
|
new Server()
|
||||||
|
|
||||||
globalShortcut.register('CommandOrControl+Shift+Space', () => {
|
globalShortcut.register('CommandOrControl+Shift+Space', () => {
|
||||||
createWindow({onTop: true});
|
createWindow({onTop: true});
|
||||||
|
|||||||
@@ -7,28 +7,37 @@ import { fileURLToPath } from "url"
|
|||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||||
const KERNEL = path.join(__dirname, "../kernel/kernel.js")
|
const KERNEL = path.join(__dirname, "../kernel/kernel.js")
|
||||||
const PID_FILE = "/tmp/forms.pid"
|
const PID_FILE = "/tmp/forms.pid"
|
||||||
|
const LOG_FILE = path.join(__dirname, "forms.log") // Define your log file
|
||||||
|
|
||||||
const cmd = process.argv[2]
|
const cmd = process.argv[2]
|
||||||
|
|
||||||
|
// Function to append logs
|
||||||
|
function logToFile(message) {
|
||||||
|
fs.appendFileSync(LOG_FILE, `${new Date().toISOString()} - ${message}\n`)
|
||||||
|
}
|
||||||
|
|
||||||
if (cmd === "start") {
|
if (cmd === "start") {
|
||||||
if (fs.existsSync(PID_FILE)) {
|
if (fs.existsSync(PID_FILE)) {
|
||||||
console.log("forms kernel already running")
|
console.log("forms kernel already running")
|
||||||
|
logToFile("forms kernel already running")
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
const proc = spawn("node", [KERNEL], {
|
const proc = spawn("node", [KERNEL], {
|
||||||
detached: true,
|
detached: true,
|
||||||
stdio: "ignore"
|
stdio: ["ignore", fs.openSync(LOG_FILE, "a"), fs.openSync(LOG_FILE, "a")] // Redirect stdout and stderr to the log file
|
||||||
})
|
})
|
||||||
|
|
||||||
proc.unref()
|
proc.unref()
|
||||||
fs.writeFileSync(PID_FILE, String(proc.pid))
|
fs.writeFileSync(PID_FILE, String(proc.pid))
|
||||||
console.log("forms kernel started")
|
console.log("forms kernel started")
|
||||||
|
logToFile("forms kernel started")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cmd === "stop") {
|
if (cmd === "stop") {
|
||||||
if (!fs.existsSync(PID_FILE)) {
|
if (!fs.existsSync(PID_FILE)) {
|
||||||
console.log("forms kernel not running")
|
console.log("forms kernel not running")
|
||||||
|
logToFile("forms kernel not running")
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,8 +45,10 @@ if (cmd === "stop") {
|
|||||||
process.kill(pid)
|
process.kill(pid)
|
||||||
fs.unlinkSync(PID_FILE)
|
fs.unlinkSync(PID_FILE)
|
||||||
console.log("forms kernel stopped")
|
console.log("forms kernel stopped")
|
||||||
|
logToFile("forms kernel stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cmd) {
|
if (!cmd) {
|
||||||
console.log("usage: forms start | stop")
|
console.log("usage: forms start | stop")
|
||||||
|
logToFile("usage: forms start | stop")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,47 @@
|
|||||||
import WebSocket from "ws"
|
import WebSocket from "ws"
|
||||||
|
|
||||||
export default class FormsClient {
|
class client {
|
||||||
constructor(url = "ws://localhost:4001") {
|
constructor() {
|
||||||
this.url = url
|
this.url = `ws://localhost:${10000}`
|
||||||
this.ws = null
|
this.ws = null
|
||||||
this.seq = 0
|
this.seq = 0
|
||||||
this.handlers = new Map()
|
this.handlers = new Map()
|
||||||
|
console.log(process.cwd())
|
||||||
}
|
}
|
||||||
|
|
||||||
connect() {
|
connect() {
|
||||||
this.ws = new WebSocket(this.url)
|
return new Promise((resolve, reject) => {
|
||||||
|
this.ws = new WebSocket(this.url)
|
||||||
|
|
||||||
this.ws.on("message", msg => this._onMessage(msg))
|
this.ws.on("message", msg => this.onMessage(msg))
|
||||||
this.ws.on("open", () => console.log("[forms] connected"))
|
|
||||||
this.ws.on("close", () => console.log("[forms] disconnected"))
|
this.ws.on("open", () => {
|
||||||
|
console.log("[forms] connected")
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
|
||||||
|
this.ws.on("error", err => {
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
|
||||||
|
this.ws.on("close", () => {
|
||||||
|
console.log("[forms] disconnected")
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
append(form, data) {
|
register(type, schema) {
|
||||||
this.ws.send(JSON.stringify({
|
this.ws.send(JSON.stringify({
|
||||||
op: "append",
|
op: "register",
|
||||||
form,
|
type,
|
||||||
|
schema
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
add(type, data) {
|
||||||
|
this.ws.send(JSON.stringify({
|
||||||
|
op: "add",
|
||||||
|
type,
|
||||||
data
|
data
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -38,15 +60,16 @@ export default class FormsClient {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessage(raw) {
|
onMessage(raw) {
|
||||||
const msg = JSON.parse(raw.toString())
|
const msg = JSON.parse(raw.toString())
|
||||||
|
console.log("msg received: ", msg)
|
||||||
|
|
||||||
if (msg.type === "hello") {
|
if (msg.type === "hello") {
|
||||||
this.seq = msg.seq
|
this.seq = msg.seq
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.type === "form:update") {
|
if (msg.type === "add") {
|
||||||
this.seq = msg.seq
|
this.seq = msg.seq
|
||||||
|
|
||||||
const set = this.handlers.get(msg.form)
|
const set = this.handlers.get(msg.form)
|
||||||
@@ -58,3 +81,7 @@ export default class FormsClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
client: client
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
|
import * as z from "zod"
|
||||||
import { WebSocketServer } from "ws"
|
import { WebSocketServer } from "ws"
|
||||||
|
import { fileURLToPath } from "url"
|
||||||
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||||
|
|
||||||
const DATA_DIR = path.resolve("./")
|
const DATA_DIR = path.resolve(__dirname)
|
||||||
const DATA_FILE = path.join(DATA_DIR, "master.forms")
|
const DATA_FILE = path.join(DATA_DIR, "master.forms")
|
||||||
|
|
||||||
if (!fs.existsSync(DATA_DIR)) {
|
if (!fs.existsSync(DATA_DIR)) {
|
||||||
@@ -17,19 +20,20 @@ class Kernel {
|
|||||||
port
|
port
|
||||||
seq = 0
|
seq = 0
|
||||||
clients = new Set()
|
clients = new Set()
|
||||||
|
schemas = new Map()
|
||||||
|
|
||||||
constructor(port = 10000) {
|
constructor(port = 10000) {
|
||||||
this.port = port
|
this.port = port
|
||||||
|
|
||||||
this._loadSeq()
|
this.loadData()
|
||||||
|
|
||||||
this.wss = new WebSocketServer({ port: this.port })
|
this.wss = new WebSocketServer({ port: this.port })
|
||||||
this.wss.on("connection", ws => this._onConnection(ws))
|
this.wss.on("connection", ws => this.onConnection(ws))
|
||||||
|
|
||||||
console.log(`[kernel] running on ws://localhost:${port}`)
|
console.log(`[kernel] running on ws://localhost:${port}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
_loadSeq() {
|
loadData() {
|
||||||
const data = fs.readFileSync(DATA_FILE, "utf8")
|
const data = fs.readFileSync(DATA_FILE, "utf8")
|
||||||
if (!data) return
|
if (!data) return
|
||||||
|
|
||||||
@@ -41,10 +45,10 @@ class Kernel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_onConnection(ws) {
|
onConnection(ws) {
|
||||||
this.clients.add(ws)
|
this.clients.add(ws)
|
||||||
|
|
||||||
ws.on("message", msg => this._onMessage(ws, msg))
|
ws.on("message", msg => this.onMessage(ws, msg))
|
||||||
ws.on("close", () => this.clients.delete(ws))
|
ws.on("close", () => this.clients.delete(ws))
|
||||||
|
|
||||||
ws.send(JSON.stringify({
|
ws.send(JSON.stringify({
|
||||||
@@ -53,7 +57,7 @@ class Kernel {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessage(ws, raw) {
|
onMessage(ws, raw) {
|
||||||
let msg
|
let msg
|
||||||
try {
|
try {
|
||||||
msg = JSON.parse(raw.toString())
|
msg = JSON.parse(raw.toString())
|
||||||
@@ -61,27 +65,60 @@ class Kernel {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (msg.op === "append") {
|
switch(msg.op) {
|
||||||
this._append(msg.form, msg.data)
|
case "add":
|
||||||
}
|
this.add(msg.type, msg.data)
|
||||||
|
break;
|
||||||
|
case "register":
|
||||||
|
this.register(msg.type, msg.schema)
|
||||||
|
break;
|
||||||
|
case "replay":
|
||||||
|
this.replay(ws, msg.type, msg.fromSeq)
|
||||||
|
break;
|
||||||
|
|
||||||
if (msg.op === "replay") {
|
default:
|
||||||
this._replay(ws, msg.form, msg.fromSeq)
|
console.error("error: unknown message operation: ", msg.op)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_append(form, data) {
|
register(name, schema) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
schema = z.fromJSONSchema(schema)
|
||||||
|
} catch(e) {
|
||||||
|
console.error("register() error: bad schema ", schema, e)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.schemas.set(name, schema)
|
||||||
|
|
||||||
|
console.log(`[kernel] registered form "${name}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
add(name, data) {
|
||||||
|
let schema = this.schemas.get(name)
|
||||||
|
if(!schema) {
|
||||||
|
console.error("No schema for this form: ", name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
schema.parse(data)
|
||||||
|
} catch(e) {
|
||||||
|
console.error("error parsing data : ", data, " for schema ", name, e)
|
||||||
|
}
|
||||||
|
|
||||||
const entry = {
|
const entry = {
|
||||||
seq: ++this.seq,
|
seq: ++this.seq,
|
||||||
time: Date.now(),
|
time: Date.now(),
|
||||||
form,
|
name,
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.appendFileSync(DATA_FILE, JSON.stringify(entry) + "\n")
|
fs.appendFileSync(DATA_FILE, JSON.stringify(entry) + "\n")
|
||||||
|
|
||||||
const payload = JSON.stringify({
|
const payload = JSON.stringify({
|
||||||
type: "form:update",
|
type: "add",
|
||||||
...entry
|
...entry
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -92,7 +129,7 @@ class Kernel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_replay(ws, form, fromSeq = 0) {
|
replay(ws, form, fromSeq = 0) {
|
||||||
const stream = fs.createReadStream(DATA_FILE, { encoding: "utf8" })
|
const stream = fs.createReadStream(DATA_FILE, { encoding: "utf8" })
|
||||||
let buffer = ""
|
let buffer = ""
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
".": "./client/index.js"
|
".": "./client/index.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ws": "^8.16.0"
|
"ws": "^8.16.0",
|
||||||
|
"zod": "^4.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
cd forms
|
|
||||||
npm link
|
|
||||||
|
|
||||||
In app:
|
To Link Globally
|
||||||
npm link forms
|
```cd forms```
|
||||||
|
```npm link```
|
||||||
|
|
||||||
Start:
|
Running Forms:
|
||||||
forms start
|
```forms start```
|
||||||
|
```forms stop```
|
||||||
|
|
||||||
|
In app that is using forms:
|
||||||
|
```npm link forms```
|
||||||
|
```import FormsClient from "forms"```
|
||||||
1615
package-lock.json
generated
1615
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -12,8 +12,9 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^5.6.2",
|
"chalk": "^5.6.2",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"moment": "^2.30.1",
|
"moment": "^2.30.1",
|
||||||
"ws": "^8.18.3"
|
"ws": "^8.18.3",
|
||||||
|
"zod": "^4.2.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
import chalk from 'chalk'
|
|
||||||
import path from 'path';
|
|
||||||
import fs from 'fs/promises';
|
|
||||||
import { pathToFileURL } from 'url';
|
|
||||||
|
|
||||||
export default class Database {
|
|
||||||
#nodes;
|
|
||||||
#edges;
|
|
||||||
#labels = {}
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.getData()
|
|
||||||
}
|
|
||||||
|
|
||||||
async getData() {
|
|
||||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
|
||||||
let dbJson;
|
|
||||||
try {
|
|
||||||
dbJson = JSON.parse(dbData);
|
|
||||||
} catch {
|
|
||||||
dbJson = []
|
|
||||||
}
|
|
||||||
this.#nodes = dbJson["nodes"];
|
|
||||||
this.#edges = dbJson["edges"];
|
|
||||||
|
|
||||||
console.log(chalk.yellow("DB established."))
|
|
||||||
Object.preventExtensions(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
// superKey = "nodes" || "edges"
|
|
||||||
async writeData(superKey, key, value) {
|
|
||||||
const dbData = await fs.readFile(path.join(process.cwd(), 'db/db.json'), 'utf8');
|
|
||||||
let dbJson;
|
|
||||||
try {
|
|
||||||
dbJson = JSON.parse(dbData);
|
|
||||||
} catch {
|
|
||||||
dbJson = []
|
|
||||||
}
|
|
||||||
|
|
||||||
dbJson[superKey][key] = value;
|
|
||||||
|
|
||||||
await fs.writeFile(path.join(process.cwd(), 'db/db.json'), JSON.stringify(dbJson, null, 2), 'utf8')
|
|
||||||
}
|
|
||||||
|
|
||||||
async getLabelModels() {
|
|
||||||
const labelHandlers = {};
|
|
||||||
const labelDir = path.join(process.cwd(), 'src/model/labels');
|
|
||||||
const files = await fs.readdir(labelDir);
|
|
||||||
|
|
||||||
for (const file of files) {
|
|
||||||
if (!file.endsWith('.js')) continue;
|
|
||||||
|
|
||||||
const label = path.basename(file, '.js');
|
|
||||||
const modulePath = path.join(labelDir, file);
|
|
||||||
const module = await import(pathToFileURL(modulePath).href);
|
|
||||||
labelHandlers[label] = module.default;
|
|
||||||
|
|
||||||
if (!this.#labels[label]) {
|
|
||||||
this.#labels[label] = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return labelHandlers
|
|
||||||
}
|
|
||||||
|
|
||||||
generateUserID() {
|
|
||||||
let id = this.#labels["User"].length + 1;
|
|
||||||
while (this.get.user(`user-${id}`)) {
|
|
||||||
id++;
|
|
||||||
}
|
|
||||||
return `user-${id}`; // O(1) most of the time
|
|
||||||
}
|
|
||||||
|
|
||||||
async getAll() {
|
|
||||||
return { nodes: this.#nodes }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,13 +9,14 @@ import cors from 'cors'
|
|||||||
import http from 'http'
|
import http from 'http'
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
import moment from 'moment'
|
import moment from 'moment'
|
||||||
|
import * as z from "zod";
|
||||||
|
|
||||||
import { initWebSocket } from './ws.js'
|
import { initWebSocket } from './ws.js'
|
||||||
import Database from "./db/db.js"
|
import getAllDownloadsFiles from "./getDownloads.js"
|
||||||
import getAllDownloadsFiles from "./db/getDownloads.js"
|
import forms from "forms"
|
||||||
|
|
||||||
class Server {
|
export default class Server {
|
||||||
db;
|
store;
|
||||||
UIPath = path.join(__dirname, '../ui')
|
UIPath = path.join(__dirname, '../ui')
|
||||||
|
|
||||||
registerRoutes(router) {
|
registerRoutes(router) {
|
||||||
@@ -81,8 +82,25 @@ class Server {
|
|||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async connectToForms() {
|
||||||
|
await this.store.connect()
|
||||||
|
this.store.register("log", z.toJSONSchema(z.object({
|
||||||
|
time: z.string(),
|
||||||
|
host: z.string(),
|
||||||
|
ip: z.string(),
|
||||||
|
path: z.string(),
|
||||||
|
method: z.string()
|
||||||
|
})))
|
||||||
|
this.store.add("log", {
|
||||||
|
time: "0100",
|
||||||
|
host: "parchment.page",
|
||||||
|
ip: "0.0.0.1",
|
||||||
|
path: "/asd",
|
||||||
|
method: "GET"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.db = new Database()
|
|
||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors({ origin: '*' }));
|
app.use(cors({ origin: '*' }));
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
@@ -94,12 +112,15 @@ class Server {
|
|||||||
this.registerRoutes(router)
|
this.registerRoutes(router)
|
||||||
app.use('/', router);
|
app.use('/', router);
|
||||||
|
|
||||||
|
this.store = new forms.client()
|
||||||
|
this.connectToForms()
|
||||||
|
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
initWebSocket(server);
|
initWebSocket(server);
|
||||||
const PORT = 80;
|
const PORT = 80;
|
||||||
server.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
console.log("\n")
|
console.log("\n")
|
||||||
console.log(chalk.yellow("**************Downloads****************"))
|
console.log(chalk.yellow("**************Parchment****************"))
|
||||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||||
console.log(chalk.yellow("***************************************"))
|
console.log(chalk.yellow("***************************************"))
|
||||||
console.log("\n")
|
console.log("\n")
|
||||||
@@ -113,6 +134,4 @@ class Server {
|
|||||||
|
|
||||||
Object.preventExtensions(this);
|
Object.preventExtensions(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const server = new Server()
|
|
||||||
Reference in New Issue
Block a user