UNPKG

codelabs.y-socket.io

Version:

Socket IO Connector for Yjs (Inspired by y-websocket)

121 lines (119 loc) 4.86 kB
// src/server/document.ts import * as d from "yjs"; import * as l from "y-protocols/awareness"; var w = process.env.GC !== "false" && process.env.GC !== "0", c = class extends d.Doc { constructor(t, e, s) { super({ gc: w }); this.onUpdateDoc = (t) => { var e; if (((e = this.callbacks) == null ? void 0 : e.onUpdate) != null) try { this.callbacks.onUpdate(this, t); } catch (s) { console.warn(s); } this.namespace.emit("sync-update", t); }; this.onUpdateAwareness = ({ added: t, updated: e, removed: s }, n) => { var o; let r = t.concat(e, s), a = l.encodeAwarenessUpdate(this.awareness, r); if (((o = this.callbacks) == null ? void 0 : o.onChangeAwareness) != null) try { this.callbacks.onChangeAwareness(this, a); } catch (y) { console.warn(y); } this.namespace.emit("awareness-update", a); }; this.name = t, this.namespace = e, this.awareness = new l.Awareness(this), this.awareness.setLocalState(null), this.callbacks = s, this.awareness.on("update", this.onUpdateAwareness), this.on("update", this.onUpdateDoc); } async destroy() { var t; if (((t = this.callbacks) == null ? void 0 : t.onDestroy) != null) try { await this.callbacks.onDestroy(this); } catch (e) { console.warn(e); } this.awareness.off("update", this.onUpdateAwareness), this.off("update", this.onUpdateDoc), this.namespace.disconnectSockets(), super.destroy(); } }; // src/server/y-socket-io.ts import * as i from "yjs"; import * as p from "y-protocols/awareness"; import { LeveldbPersistence as v } from "y-leveldb"; import { Observable as A } from "lib0/observable"; var u = class extends A { constructor(t, e) { var s; super(); this._documents = /* @__PURE__ */ new Map(); this._levelPersistenceDir = null; this.persistence = null; this.initSyncListeners = (t, e) => { t.on("sync-step-1", (s, n) => { n(i.encodeStateAsUpdate(e, new Uint8Array(s))); }), t.on("sync-update", (s) => { i.applyUpdate(e, s, null); }); }; this.initAwarenessListeners = (t, e) => { t.on("awareness-update", (s) => { p.applyAwarenessUpdate(e.awareness, new Uint8Array(s), t); }); }; this.initSocketListeners = (t, e) => { t.on("disconnect", async () => { (await t.nsp.allSockets()).size === 0 && (this.emit("all-document-connections-closed", [e]), this.persistence != null && (await this.persistence.writeState(e.name, e), await e.destroy())); }); }; this.startSynchronization = (t, e) => { t.emit("sync-step-1", i.encodeStateVector(e), (s) => { i.applyUpdate(e, new Uint8Array(s), this); }), t.emit("awareness-update", p.encodeAwarenessUpdate(e.awareness, Array.from(e.awareness.getStates().keys()))); }; this.io = t, this._levelPersistenceDir = (s = e == null ? void 0 : e.levelPersistenceDir) != null ? s : process.env.YPERSISTENCE, this._levelPersistenceDir != null && this.initLevelDB(this._levelPersistenceDir), this.configuration = e; } initialize() { let t = this.io.of(/^\/yjs\|.*$/); t.use(async (e, s) => { var n; return ((n = this.configuration) == null ? void 0 : n.authenticate) == null || await this.configuration.authenticate(e.handshake) ? s() : s(new Error("Unauthorized")); }), t.on("connection", async (e) => { var r; let s = e.nsp.name.replace(/\/yjs\|/, ""), n = await this.initDocument(s, e.nsp, (r = this.configuration) == null ? void 0 : r.gcEnabled); this.initSyncListeners(e, n), this.initAwarenessListeners(e, n), this.initSocketListeners(e, n), this.startSynchronization(e, n); }); } get documents() { return this._documents; } async initDocument(t, e, s = !0) { var r; let n = (r = this._documents.get(t)) != null ? r : new c(t, e, { onUpdate: (a, o) => this.emit("document-update", [a, o]), onChangeAwareness: (a, o) => this.emit("awareness-update", [a, o]), onDestroy: async (a) => { this._documents.delete(a.name), this.emit("document-destroy", [a]); } }); return n.gc = s, this._documents.has(t) || (this.persistence != null && await this.persistence.bindState(t, n), this._documents.set(t, n), this.emit("document-loaded", [n])), n; } initLevelDB(t) { let e = new v(t); this.persistence = { provider: e, bindState: async (s, n) => { let r = await e.getYDoc(s), a = i.encodeStateAsUpdate(n); await e.storeUpdate(s, a), i.applyUpdate(n, i.encodeStateAsUpdate(r)), n.on("update", async (o) => await e.storeUpdate(s, o)); }, writeState: async (s, n) => { } }; } }; export { c as a, u as b }; //# sourceMappingURL=chunk-MBNSJQBI.mjs.map