basic version of forms
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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 = ""
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
".": "./client/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": "^8.16.0"
|
||||
"ws": "^8.16.0",
|
||||
"zod": "^4.2.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"```
|
||||
Reference in New Issue
Block a user