codelabs.y-socket.io
Version:
Socket IO Connector for Yjs (Inspired by y-websocket)
144 lines (141 loc) • 5.74 kB
JavaScript
"use strict";
var g = Object.create;
var d = Object.defineProperty;
var D = Object.getOwnPropertyDescriptor;
var S = Object.getOwnPropertyNames;
var b = Object.getPrototypeOf, P = Object.prototype.hasOwnProperty;
var k = (a, r) => {
for (var t in r)
d(a, t, { get: r[t], enumerable: !0 });
}, w = (a, r, t, e) => {
if (r && typeof r == "object" || typeof r == "function")
for (let s of S(r))
!P.call(a, s) && s !== t && d(a, s, { get: () => r[s], enumerable: !(e = D(r, s)) || e.enumerable });
return a;
};
var u = (a, r, t) => (t = a != null ? g(b(a)) : {}, w(
r || !a || !a.__esModule ? d(t, "default", { value: a, enumerable: !0 }) : t,
a
)), C = (a) => w(d({}, "__esModule", { value: !0 }), a);
// src/server/index.ts
var _ = {};
k(_, {
Document: () => p,
YSocketIO: () => y
});
module.exports = C(_);
// src/server/document.ts
var v = u(require("yjs")), m = u(require("y-protocols/awareness")), Y = process.env.GC !== "false" && process.env.GC !== "0", p = class extends v.Doc {
constructor(t, e, s) {
super({ gc: Y });
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 l;
let c = t.concat(e, s), i = m.encodeAwarenessUpdate(this.awareness, c);
if (((l = this.callbacks) == null ? void 0 : l.onChangeAwareness) != null)
try {
this.callbacks.onChangeAwareness(this, i);
} catch (U) {
console.warn(U);
}
this.namespace.emit("awareness-update", i);
};
this.name = t, this.namespace = e, this.awareness = new m.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
var o = u(require("yjs")), h = u(require("y-protocols/awareness")), A = require("y-leveldb");
var f = require("lib0/observable"), y = class extends f.Observable {
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(o.encodeStateAsUpdate(e, new Uint8Array(s)));
}), t.on("sync-update", (s) => {
o.applyUpdate(e, s, null);
});
};
this.initAwarenessListeners = (t, e) => {
t.on("awareness-update", (s) => {
h.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", o.encodeStateVector(e), (s) => {
o.applyUpdate(e, new Uint8Array(s), this);
}), t.emit("awareness-update", h.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 c;
let s = e.nsp.name.replace(/\/yjs\|/, ""), n = await this.initDocument(s, e.nsp, (c = this.configuration) == null ? void 0 : c.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 c;
let n = (c = this._documents.get(t)) != null ? c : new p(t, e, {
onUpdate: (i, l) => this.emit("document-update", [i, l]),
onChangeAwareness: (i, l) => this.emit("awareness-update", [i, l]),
onDestroy: async (i) => {
this._documents.delete(i.name), this.emit("document-destroy", [i]);
}
});
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 A.LeveldbPersistence(t);
this.persistence = {
provider: e,
bindState: async (s, n) => {
let c = await e.getYDoc(s), i = o.encodeStateAsUpdate(n);
await e.storeUpdate(s, i), o.applyUpdate(n, o.encodeStateAsUpdate(c)), n.on("update", async (l) => await e.storeUpdate(s, l));
},
writeState: async (s, n) => {
}
};
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Document,
YSocketIO
});
//# sourceMappingURL=index.js.map