basic version of forms
This commit is contained in:
@@ -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 = ""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user