basic version of forms

This commit is contained in:
metacryst
2025-12-22 04:53:00 -06:00
parent 80ceb48a62
commit 97bd02c59a
14 changed files with 153 additions and 1742 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules
package-lock.json
package-lock.json
master.forms

5
app.js
View File

@@ -1,6 +1,6 @@
import { app, BrowserWindow, globalShortcut } from 'electron';
import paths from 'path'
import "./server/index.js"
import Server from "./server/index.js"
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
@@ -68,7 +68,8 @@ function toggleDevTools(win) {
app.on("ready", async () => {
// await Forms.init();
createWindow({onTop: false});
// createWindow({onTop: false});
new Server()
globalShortcut.register('CommandOrControl+Shift+Space', () => {
createWindow({onTop: true});

View File

@@ -7,28 +7,37 @@ import { fileURLToPath } from "url"
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const KERNEL = path.join(__dirname, "../kernel/kernel.js")
const PID_FILE = "/tmp/forms.pid"
const LOG_FILE = path.join(__dirname, "forms.log") // Define your log file
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 (fs.existsSync(PID_FILE)) {
console.log("forms kernel already running")
logToFile("forms kernel already running")
process.exit(0)
}
const proc = spawn("node", [KERNEL], {
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()
fs.writeFileSync(PID_FILE, String(proc.pid))
console.log("forms kernel started")
logToFile("forms kernel started")
}
if (cmd === "stop") {
if (!fs.existsSync(PID_FILE)) {
console.log("forms kernel not running")
logToFile("forms kernel not running")
process.exit(0)
}
@@ -36,8 +45,10 @@ if (cmd === "stop") {
process.kill(pid)
fs.unlinkSync(PID_FILE)
console.log("forms kernel stopped")
logToFile("forms kernel stopped")
}
if (!cmd) {
console.log("usage: forms start | stop")
logToFile("usage: forms start | stop")
}

View File

@@ -1,25 +1,47 @@
import WebSocket from "ws"
export default class FormsClient {
constructor(url = "ws://localhost:4001") {
this.url = url
class client {
constructor() {
this.url = `ws://localhost:${10000}`
this.ws = null
this.seq = 0
this.handlers = new Map()
console.log(process.cwd())
}
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("open", () => console.log("[forms] connected"))
this.ws.on("close", () => console.log("[forms] disconnected"))
this.ws.on("message", msg => this.onMessage(msg))
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({
op: "append",
form,
op: "register",
type,
schema
}))
}
add(type, data) {
this.ws.send(JSON.stringify({
op: "add",
type,
data
}))
}
@@ -38,15 +60,16 @@ export default class FormsClient {
}))
}
_onMessage(raw) {
onMessage(raw) {
const msg = JSON.parse(raw.toString())
console.log("msg received: ", msg)
if (msg.type === "hello") {
this.seq = msg.seq
return
}
if (msg.type === "form:update") {
if (msg.type === "add") {
this.seq = msg.seq
const set = this.handlers.get(msg.form)
@@ -58,3 +81,7 @@ export default class FormsClient {
}
}
}
export default {
client: client
}

View File

@@ -1,8 +1,11 @@
import fs from "fs"
import path from "path"
import * as z from "zod"
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")
if (!fs.existsSync(DATA_DIR)) {
@@ -17,19 +20,20 @@ class Kernel {
port
seq = 0
clients = new Set()
schemas = new Map()
constructor(port = 10000) {
this.port = port
this._loadSeq()
this.loadData()
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}`)
}
_loadSeq() {
loadData() {
const data = fs.readFileSync(DATA_FILE, "utf8")
if (!data) return
@@ -41,10 +45,10 @@ class Kernel {
}
}
_onConnection(ws) {
onConnection(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.send(JSON.stringify({
@@ -53,7 +57,7 @@ class Kernel {
}))
}
_onMessage(ws, raw) {
onMessage(ws, raw) {
let msg
try {
msg = JSON.parse(raw.toString())
@@ -61,27 +65,60 @@ class Kernel {
return
}
if (msg.op === "append") {
this._append(msg.form, msg.data)
}
switch(msg.op) {
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") {
this._replay(ws, msg.form, msg.fromSeq)
default:
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 = {
seq: ++this.seq,
time: Date.now(),
form,
name,
data
}
fs.appendFileSync(DATA_FILE, JSON.stringify(entry) + "\n")
const payload = JSON.stringify({
type: "form:update",
type: "add",
...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" })
let buffer = ""

View File

View File

@@ -9,6 +9,7 @@
".": "./client/index.js"
},
"dependencies": {
"ws": "^8.16.0"
"ws": "^8.16.0",
"zod": "^4.2.1"
}
}
}

View File

@@ -1,8 +1,12 @@
cd forms
npm link
In app:
npm link forms
To Link Globally
```cd forms```
```npm link```
Start:
forms start
Running Forms:
```forms start```
```forms stop```
In app that is using forms:
```npm link forms```
```import FormsClient from "forms"```

View File

1615
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -12,8 +12,9 @@
"dependencies": {
"chalk": "^5.6.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"express": "^4.18.2",
"moment": "^2.30.1",
"ws": "^8.18.3"
"ws": "^8.18.3",
"zod": "^4.2.1"
}
}

View File

@@ -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 }
}
}

View File

@@ -9,13 +9,14 @@ import cors from 'cors'
import http from 'http'
import chalk from 'chalk'
import moment from 'moment'
import * as z from "zod";
import { initWebSocket } from './ws.js'
import Database from "./db/db.js"
import getAllDownloadsFiles from "./db/getDownloads.js"
import getAllDownloadsFiles from "./getDownloads.js"
import forms from "forms"
class Server {
db;
export default class Server {
store;
UIPath = path.join(__dirname, '../ui')
registerRoutes(router) {
@@ -81,8 +82,25 @@ class Server {
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() {
this.db = new Database()
const app = express();
app.use(cors({ origin: '*' }));
app.use(express.json());
@@ -94,12 +112,15 @@ class Server {
this.registerRoutes(router)
app.use('/', router);
this.store = new forms.client()
this.connectToForms()
const server = http.createServer(app);
initWebSocket(server);
const PORT = 80;
server.listen(PORT, () => {
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.yellow("***************************************"))
console.log("\n")
@@ -113,6 +134,4 @@ class Server {
Object.preventExtensions(this);
}
}
const server = new Server()
}