Files
427e7578-d7bf-49c8-aee9-2dd…/dist/public/assets/repo-CjvObF6Z.js
yannickschuchmann b936095286 Add implementation
2026-03-08 18:01:28 +00:00

1255 lines
46 KiB
JavaScript

var __defProp = Object.defineProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
var __privateWrapper = (obj, member, setter, getter) => ({
set _(value) {
__privateSet(obj, member, value, setter);
},
get _() {
return __privateGet(obj, member, getter);
}
});
import { E as D, d as P, a as q, b as K, s as G, v as V, l as x, c as T, e as J, g as k, f as Y, h as B, j as Q, k as X, m as N, n as E, C as Z, t as z, D as _, o as $, p as A, q as C, r as L, u as ee, w as j, i as te, x as R, U as y, y as M, R as v, z as W, A as se, B as re, F as ne, G as oe, H as ie, __tla as __tla_0 } from "./fullfat_bundler-C8o4MXnP.js";
let Me, Re;
let __tla = Promise.all([
(() => {
try {
return __tla_0;
} catch {
}
})()
]).then(async () => {
var _e, _s, _o, _t, _n, _r, _ae_instances, i_fn, a_fn, _e2, _s2, _o2, _t2, _n2, _e3, _s3, _o3, _t3, _n3, _le_instances, r_fn, i_fn2, a_fn2, h_fn, _e4, _s4, _o4, _t4, _n4, _r2, _i, _a, _h, _u, _me_instances, d_fn, f_fn, _l, m_fn, c_fn, p_fn, y_fn, _e5, _s5, _o5, _t5, _n5, _r3, _i2, _pe_instances, a_fn3, _e6, _s6, _o6, _Pe_instances, t_fn, _n6, _r4;
class ae extends D {
constructor() {
super(...arguments);
__privateAdd(this, _ae_instances);
__privateAdd(this, _e, /* @__PURE__ */ new Map());
__privateAdd(this, _s, /* @__PURE__ */ new Set());
__privateAdd(this, _o, /* @__PURE__ */ new Map());
__privateAdd(this, _t, /* @__PURE__ */ new Set());
__privateAdd(this, _n, /* @__PURE__ */ new Map());
__privateAdd(this, _r, P("automerge-repo:remote-heads-subscriptions"));
}
subscribeToRemotes(e) {
__privateGet(this, _r).call(this, "subscribeToRemotes", e);
const t = [];
for (const s of e) __privateGet(this, _s).has(s) || (__privateGet(this, _s).add(s), t.push(s));
t.length > 0 && this.emit("change-remote-subs", {
add: t,
peers: Array.from(__privateGet(this, _t))
});
}
unsubscribeFromRemotes(e) {
__privateGet(this, _r).call(this, "subscribeToRemotes", e);
const t = [];
for (const s of e) __privateGet(this, _s).has(s) && (__privateGet(this, _s).delete(s), __privateGet(this, _o).has(s) || t.push(s));
t.length > 0 && this.emit("change-remote-subs", {
remove: t,
peers: Array.from(__privateGet(this, _t))
});
}
handleControlMessage(e) {
const t = [], s = [], r = [];
if (__privateGet(this, _r).call(this, "handleControlMessage", e), e.add) for (const n of e.add) {
let o = __privateGet(this, _o).get(n);
(__privateGet(this, _s).has(n) || o) && r.push(n), o || (o = /* @__PURE__ */ new Set(), __privateGet(this, _o).set(n, o), __privateGet(this, _s).has(n) || t.push(n)), o.add(e.senderId);
}
if (e.remove) for (const n of e.remove) {
const o = __privateGet(this, _o).get(n);
o && (o.delete(e.senderId), o.size == 0 && !__privateGet(this, _s).has(n) && s.push(n));
}
(t.length > 0 || s.length > 0) && this.emit("change-remote-subs", {
peers: Array.from(__privateGet(this, _t)),
add: t,
remove: s
});
for (const n of r) {
const o = __privateGet(this, _n).get(e.senderId);
if (o) for (const a of o) {
const u = __privateGet(this, _e).get(a);
if (!u) continue;
const h = u.get(n);
h && this.emit("notify-remote-heads", {
targetId: e.senderId,
documentId: a,
heads: h.lastHeads,
timestamp: h.lastSyncTimestamp,
storageId: n
});
}
}
}
handleRemoteHeads(e) {
__privateGet(this, _r).call(this, "handleRemoteHeads", e);
const t = __privateMethod(this, _ae_instances, a_fn).call(this, e);
for (const s of t) __privateGet(this, _s).has(s.storageId) && this.emit("remote-heads-changed", s);
for (const s of t) for (const r of __privateGet(this, _t)) r !== e.senderId && this.emit("notify-remote-heads", {
targetId: r,
documentId: s.documentId,
heads: s.remoteHeads,
timestamp: s.timestamp,
storageId: s.storageId
});
for (const s of t) {
const r = __privateGet(this, _o).get(s.storageId);
if (r) for (const n of r) __privateMethod(this, _ae_instances, i_fn).call(this, n, s.documentId) && this.emit("notify-remote-heads", {
targetId: n,
documentId: s.documentId,
heads: s.remoteHeads,
timestamp: s.timestamp,
storageId: s.storageId
});
}
}
handleImmediateRemoteHeadsChanged(e, t, s) {
__privateGet(this, _r).call(this, "handleLocalHeadsChanged", e, t, s);
const r = __privateGet(this, _e).get(e), n = Date.now();
if (!r) __privateGet(this, _e).set(e, /* @__PURE__ */ new Map([
[
t,
{
lastSyncTimestamp: n,
lastHeads: s
}
]
]));
else {
const a = r.get(t);
(!a || a.lastSyncTimestamp < Date.now()) && r.set(t, {
lastSyncTimestamp: Date.now(),
lastHeads: s
});
}
const o = __privateGet(this, _o).get(t);
if (o) for (const a of o) __privateMethod(this, _ae_instances, i_fn).call(this, a, e) && this.emit("notify-remote-heads", {
targetId: a,
documentId: e,
heads: s,
timestamp: n,
storageId: t
});
}
addGenerousPeer(e) {
__privateGet(this, _r).call(this, "addGenerousPeer", e), __privateGet(this, _t).add(e), __privateGet(this, _s).size > 0 && this.emit("change-remote-subs", {
add: Array.from(__privateGet(this, _s)),
peers: [
e
]
});
for (const [t, s] of __privateGet(this, _e)) for (const [r, { lastHeads: n, lastSyncTimestamp: o }] of s) this.emit("notify-remote-heads", {
targetId: e,
documentId: t,
heads: n,
timestamp: o,
storageId: r
});
}
removePeer(e) {
__privateGet(this, _r).call(this, "removePeer", e);
const t = [];
__privateGet(this, _t).delete(e), __privateGet(this, _n).delete(e);
for (const [s, r] of __privateGet(this, _o)) r.has(e) && (r.delete(e), r.size == 0 && (t.push(s), __privateGet(this, _o).delete(s)));
t.length > 0 && this.emit("change-remote-subs", {
remove: t,
peers: Array.from(__privateGet(this, _t))
});
}
subscribePeerToDoc(e, t) {
let s = __privateGet(this, _n).get(e);
s || (s = /* @__PURE__ */ new Set(), __privateGet(this, _n).set(e, s)), s.add(t);
const r = __privateGet(this, _e).get(t);
if (r) for (const [n, o] of r) {
const a = __privateGet(this, _o).get(n);
a && a.has(e) && this.emit("notify-remote-heads", {
targetId: e,
documentId: t,
heads: o.lastHeads,
timestamp: o.lastSyncTimestamp,
storageId: n
});
}
}
}
_e = new WeakMap();
_s = new WeakMap();
_o = new WeakMap();
_t = new WeakMap();
_n = new WeakMap();
_r = new WeakMap();
_ae_instances = new WeakSet();
i_fn = function(e, t) {
const s = __privateGet(this, _n).get(e);
return s && s.has(t);
};
a_fn = function(e) {
const t = [], { documentId: s, newHeads: r } = e;
for (const [n, { heads: o, timestamp: a }] of Object.entries(r)) {
if (!__privateGet(this, _s).has(n) && !__privateGet(this, _o).has(n)) continue;
let u = __privateGet(this, _e).get(s);
u || (u = /* @__PURE__ */ new Map(), __privateGet(this, _e).set(s, u));
const h = u.get(n);
h && h.lastSyncTimestamp >= a || (u.set(n, {
lastSyncTimestamp: a,
lastHeads: o
}), t.push({
documentId: s,
storageId: n,
remoteHeads: o,
timestamp: a
}));
}
return t;
};
const ce = (d) => `${d.senderId}:${d.sessionId}`;
class he extends D {
constructor(e, t, s) {
super();
__publicField(this, "peerId");
__publicField(this, "peerMetadata");
__privateAdd(this, _e2);
__privateAdd(this, _s2, {});
__privateAdd(this, _o2, 0);
__privateAdd(this, _t2, Math.random().toString(36).slice(2));
__privateAdd(this, _n2, {});
__publicField(this, "adapters", []);
__publicField(this, "isReady", () => this.adapters.every((e) => e.isReady()));
__publicField(this, "whenReady", async () => Promise.all(this.adapters.map((e) => e.whenReady())));
this.peerId = t, this.peerMetadata = s, __privateSet(this, _e2, P(`automerge-repo:network:${this.peerId}`)), e.forEach((r) => this.addNetworkAdapter(r));
}
disconnect() {
this.adapters.forEach((e) => e.disconnect());
}
reconnect() {
this.adapters.forEach((e) => e.connect(this.peerId));
}
addNetworkAdapter(e) {
this.adapters.push(e), e.on("peer-candidate", ({ peerId: t, peerMetadata: s }) => {
__privateGet(this, _e2).call(this, `peer candidate: ${t} `), __privateGet(this, _s2)[t] || (__privateGet(this, _s2)[t] = e), this.emit("peer", {
peerId: t,
peerMetadata: s
});
}), e.on("peer-disconnected", ({ peerId: t }) => {
__privateGet(this, _e2).call(this, `peer disconnected: ${t} `), delete __privateGet(this, _s2)[t], this.emit("peer-disconnected", {
peerId: t
});
}), e.on("message", (t) => {
if (!q(t)) {
__privateGet(this, _e2).call(this, `invalid message: ${JSON.stringify(t)}`);
return;
}
if (__privateGet(this, _e2).call(this, `message from ${t.senderId}`), K(t)) {
const s = ce(t);
(__privateGet(this, _n2)[s] === void 0 || t.count > __privateGet(this, _n2)[s]) && (__privateGet(this, _n2)[s] = t.count, this.emit("message", t));
return;
}
this.emit("message", t);
}), e.on("close", () => {
__privateGet(this, _e2).call(this, "adapter closed"), Object.entries(__privateGet(this, _s2)).forEach(([t, s]) => {
s === e && delete __privateGet(this, _s2)[t];
}), this.adapters = this.adapters.filter((t) => t !== e);
}), this.peerMetadata.then((t) => {
e.connect(this.peerId, t);
}).catch((t) => {
__privateGet(this, _e2).call(this, "error connecting to network", t);
});
}
removeNetworkAdapter(e) {
this.adapters = this.adapters.filter((t) => t !== e), e.disconnect();
}
send(e) {
const t = __privateGet(this, _s2)[e.targetId];
if (!t) {
__privateGet(this, _e2).call(this, `Tried to send message but peer not found: ${e.targetId}`);
return;
}
const r = ((n) => n.type === "ephemeral" ? "count" in n ? n : {
...n,
count: ++__privateWrapper(this, _o2)._,
sessionId: __privateGet(this, _t2),
senderId: this.peerId
} : {
...n,
senderId: this.peerId
})(e);
__privateGet(this, _e2).call(this, "sending message %o", r), t.send(r);
}
}
_e2 = new WeakMap();
_s2 = new WeakMap();
_o2 = new WeakMap();
_t2 = new WeakMap();
_n2 = new WeakMap();
function O(d) {
let e = 0;
d.forEach((r) => {
e += r.length;
});
const t = new Uint8Array(e);
let s = 0;
return d.forEach((r) => {
t.set(r, s), s += r.length;
}), t;
}
function F(d) {
const e = G.hash(d);
return ue(e);
}
function de(d) {
const e = new TextEncoder(), t = O(d.map((s) => e.encode(s)));
return F(t);
}
function ue(d) {
return Array.from(d, (e) => e.toString(16).padStart(2, "0")).join("");
}
class le extends D {
constructor(e) {
super();
__privateAdd(this, _le_instances);
__privateAdd(this, _e3);
__privateAdd(this, _s3, /* @__PURE__ */ new Map());
__privateAdd(this, _o3, /* @__PURE__ */ new Map());
__privateAdd(this, _t3, false);
__privateAdd(this, _n3, P("automerge-repo:storage-subsystem"));
__privateSet(this, _e3, e);
}
async id() {
const e = await __privateGet(this, _e3).load([
"storage-adapter-id"
]);
let t;
return e ? t = new TextDecoder().decode(e) : (t = V(), await __privateGet(this, _e3).save([
"storage-adapter-id"
], new TextEncoder().encode(t))), t;
}
async load(e, t) {
const s = [
e,
t
];
return await __privateGet(this, _e3).load(s);
}
async save(e, t, s) {
const r = [
e,
t
];
await __privateGet(this, _e3).save(r, s);
}
async remove(e, t) {
const s = [
e,
t
];
await __privateGet(this, _e3).remove(s);
}
async loadDocData(e) {
const t = await __privateGet(this, _e3).loadRange([
e,
"snapshot"
]), s = await __privateGet(this, _e3).loadRange([
e,
"incremental"
]), r = [], n = [];
for (const o of t) o.data !== void 0 && (n.push({
key: o.key,
type: "snapshot",
size: o.data.length
}), r.push(o.data));
for (const o of s) o.data !== void 0 && (n.push({
key: o.key,
type: "incremental",
size: o.data.length
}), r.push(o.data));
return __privateGet(this, _o3).set(e, n), r.length === 0 ? null : O(r);
}
async loadDoc(e) {
const t = await this.loadDocData(e);
if (!t) return null;
const s = performance.now(), r = x(T(), t), n = performance.now();
return this.emit("document-loaded", {
documentId: e,
durationMillis: n - s,
...J(r)
}), __privateGet(this, _s3).set(e, k(r)), r;
}
async saveDoc(e, t) {
if (!__privateMethod(this, _le_instances, a_fn2).call(this, e, t)) return;
const s = __privateGet(this, _o3).get(e) ?? [];
__privateMethod(this, _le_instances, h_fn).call(this, s) ? await __privateMethod(this, _le_instances, i_fn2).call(this, e, t, s) : await __privateMethod(this, _le_instances, r_fn).call(this, e, t), __privateGet(this, _s3).set(e, k(t));
}
async removeDoc(e) {
await __privateGet(this, _e3).removeRange([
e,
"snapshot"
]), await __privateGet(this, _e3).removeRange([
e,
"incremental"
]), await __privateGet(this, _e3).removeRange([
e,
"sync-state"
]);
}
async loadSyncState(e, t) {
const s = [
e,
"sync-state",
t
];
try {
const r = await __privateGet(this, _e3).load(s);
return r ? Q(r) : void 0;
} catch {
__privateGet(this, _n3).call(this, `Error loading sync state for ${e} from ${t}`);
return;
}
}
async saveSyncState(e, t, s) {
const r = [
e,
"sync-state",
t
];
await __privateGet(this, _e3).save(r, X(s));
}
}
_e3 = new WeakMap();
_s3 = new WeakMap();
_o3 = new WeakMap();
_t3 = new WeakMap();
_n3 = new WeakMap();
_le_instances = new WeakSet();
r_fn = async function(e, t) {
const s = __privateGet(this, _s3).get(e) ?? [], r = performance.now(), n = Y(t, s), o = performance.now();
if (this.emit("doc-saved", {
documentId: e,
durationMillis: o - r,
sinceHeads: s
}), n && n.length > 0) {
const a = [
e,
"incremental",
F(n)
];
__privateGet(this, _n3).call(this, `Saving incremental ${a} for document ${e}`), await __privateGet(this, _e3).save(a, n), __privateGet(this, _o3).has(e) || __privateGet(this, _o3).set(e, []), __privateGet(this, _o3).get(e).push({
key: a,
type: "incremental",
size: n.length
}), __privateGet(this, _s3).set(e, k(t));
} else return Promise.resolve();
};
i_fn2 = async function(e, t, s) {
var _a2;
__privateSet(this, _t3, true);
const r = performance.now(), n = B(t), o = performance.now();
this.emit("doc-compacted", {
documentId: e,
durationMillis: o - r
});
const a = de(k(t)), u = [
e,
"snapshot",
a
], h = new Set(s.map((c) => c.key).filter((c) => c[2] !== a));
__privateGet(this, _n3).call(this, `Saving snapshot ${u} for document ${e}`), __privateGet(this, _n3).call(this, `deleting old chunks ${Array.from(h)}`), await __privateGet(this, _e3).save(u, n);
for (const c of h) await __privateGet(this, _e3).remove(c);
const p = ((_a2 = __privateGet(this, _o3).get(e)) == null ? void 0 : _a2.filter((c) => !h.has(c.key))) ?? [];
p.push({
key: u,
type: "snapshot",
size: n.length
}), __privateGet(this, _o3).set(e, p), __privateSet(this, _t3, false);
};
a_fn2 = function(e, t) {
const s = __privateGet(this, _s3).get(e);
if (!s) return true;
const r = k(t);
return !N(E(r), E(s));
};
h_fn = function(e) {
if (__privateGet(this, _t3)) return false;
let t = 0, s = 0;
for (const r of e) r.type === "snapshot" ? t += r.size : s += r.size;
return t < 1024 || s >= t;
};
function fe() {
return "peer-" + Math.random().toString(36).slice(4);
}
class me extends D {
constructor({ storage: e, network: t = [], peerId: s = fe(), sharePolicy: r, shareConfig: n, isEphemeral: o = e === void 0, enableRemoteHeadsGossiping: a = false, denylist: u = [], saveDebounceRate: h = 100, idFactory: p } = {}) {
super();
__privateAdd(this, _me_instances);
__privateAdd(this, _e4);
__publicField(this, "networkSubsystem");
__publicField(this, "storageSubsystem");
__privateAdd(this, _s4);
__privateAdd(this, _o4);
__privateAdd(this, _t4, {});
__publicField(this, "synchronizer");
__privateAdd(this, _n4, {
announce: async () => true,
access: async () => true
});
__publicField(this, "peerMetadataByPeerId", {});
__privateAdd(this, _r2, new ae());
__privateAdd(this, _i, false);
__privateAdd(this, _a, {});
__privateAdd(this, _h, {});
__privateAdd(this, _u);
__privateAdd(this, _l, {});
__publicField(this, "subscribeToRemotes", (e) => {
__privateGet(this, _i) ? (__privateGet(this, _e4).call(this, "subscribeToRemotes", {
remotes: e
}), __privateGet(this, _r2).subscribeToRemotes(e)) : __privateGet(this, _e4).call(this, "WARN: subscribeToRemotes called but remote heads gossiping is not enabled");
});
__publicField(this, "storageId", async () => {
if (this.storageSubsystem) return this.storageSubsystem.id();
});
if (__privateSet(this, _i, a), __privateSet(this, _e4, P("automerge-repo:repo")), __privateSet(this, _u, p || null), r != null && n != null) throw new Error("cannot provide both sharePolicy and shareConfig at once");
r && __privateSet(this, _n4, {
announce: r,
access: async () => true
}), n && __privateSet(this, _n4, n), this.on("delete-document", ({ documentId: i }) => {
this.synchronizer.removeDocument(i), c && c.removeDoc(i).catch((f) => {
__privateGet(this, _e4).call(this, "error deleting document", {
documentId: i,
err: f
});
});
}), this.synchronizer = new Z(this, u), this.synchronizer.on("message", (i) => {
__privateGet(this, _e4).call(this, `sending ${i.type} message to ${i.targetId}`), m.send(i);
}), this.synchronizer.on("metrics", (i) => this.emit("doc-metrics", i)), __privateGet(this, _i) && this.synchronizer.on("open-doc", ({ peerId: i, documentId: f }) => {
__privateGet(this, _r2).subscribePeerToDoc(i, f);
});
const c = e ? new le(e) : void 0;
c && (c.on("document-loaded", (i) => this.emit("doc-metrics", {
type: "doc-loaded",
...i
})), c.on("doc-compacted", (i) => this.emit("doc-metrics", {
type: "doc-compacted",
...i
})), c.on("doc-saved", (i) => this.emit("doc-metrics", {
type: "doc-saved",
...i
}))), this.storageSubsystem = c, __privateSet(this, _s4, h), this.storageSubsystem ? __privateSet(this, _o4, ({ handle: i, doc: f }) => {
let l = __privateGet(this, _h)[i.documentId];
l || (l = z(({ doc: I, handle: H }) => {
this.storageSubsystem.saveDoc(H.documentId, I);
}, __privateGet(this, _s4)), __privateGet(this, _h)[i.documentId] = l), l({
handle: i,
doc: f
});
}) : __privateSet(this, _o4, () => {
});
const b = (async () => ({
storageId: await (c == null ? void 0 : c.id()),
isEphemeral: o
}))(), m = new he(t, s, b);
this.networkSubsystem = m, m.on("peer", async ({ peerId: i, peerMetadata: f }) => {
__privateGet(this, _e4).call(this, "peer connected", {
peerId: i
}), f && (this.peerMetadataByPeerId[i] = {
...f
}), __privateGet(this, _n4).announce(i).then((l) => {
l && __privateGet(this, _i) && __privateGet(this, _r2).addGenerousPeer(i);
}).catch((l) => {
console.log("error in share policy", {
err: l
});
}), this.synchronizer.addPeer(i);
}), m.on("peer-disconnected", ({ peerId: i }) => {
this.synchronizer.removePeer(i), __privateGet(this, _r2).removePeer(i);
}), m.on("message", async (i) => {
__privateMethod(this, _me_instances, f_fn).call(this, i);
}), this.synchronizer.on("sync-state", (i) => {
var _a2;
__privateMethod(this, _me_instances, m_fn).call(this, i);
const f = __privateGet(this, _t4)[i.documentId], { storageId: l } = this.peerMetadataByPeerId[i.peerId] || {};
if (!l) return;
const I = (_a2 = f.getSyncInfo(l)) == null ? void 0 : _a2.lastHeads;
i.syncState.theirHeads && (!I || !N(I, E(i.syncState.theirHeads))) && i.syncState.theirHeads && (f.setSyncInfo(l, {
lastHeads: E(i.syncState.theirHeads),
lastSyncTimestamp: Date.now()
}), l && __privateGet(this, _i) && __privateGet(this, _r2).handleImmediateRemoteHeadsChanged(i.documentId, l, E(i.syncState.theirHeads)));
}), __privateGet(this, _i) && (__privateGet(this, _r2).on("notify-remote-heads", (i) => {
this.networkSubsystem.send({
type: "remote-heads-changed",
targetId: i.targetId,
documentId: i.documentId,
newHeads: {
[i.storageId]: {
heads: i.heads,
timestamp: i.timestamp
}
}
});
}), __privateGet(this, _r2).on("change-remote-subs", (i) => {
__privateGet(this, _e4).call(this, "change-remote-subs", i);
for (const f of i.peers) this.networkSubsystem.send({
type: "remote-subscription-change",
targetId: f,
add: i.add,
remove: i.remove
});
}), __privateGet(this, _r2).on("remote-heads-changed", ({ documentId: i, storageId: f, remoteHeads: l, timestamp: I }) => {
__privateGet(this, _t4)[i].setSyncInfo(f, {
lastHeads: l,
lastSyncTimestamp: I
});
}));
}
get handles() {
return __privateGet(this, _t4);
}
get peers() {
return this.synchronizer.peers;
}
get peerId() {
return this.networkSubsystem.peerId;
}
get sharePolicy() {
return __privateGet(this, _n4).announce;
}
set sharePolicy(e) {
__privateGet(this, _n4).announce = e;
}
get shareConfig() {
return __privateGet(this, _n4);
}
set shareConfig(e) {
__privateSet(this, _n4, e);
}
getStorageIdOfPeer(e) {
var _a2;
return (_a2 = this.peerMetadataByPeerId[e]) == null ? void 0 : _a2.storageId;
}
create(e) {
let t;
e ? t = $(e) : t = A(T());
const { documentId: s } = C(L()), r = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: s
});
return __privateMethod(this, _me_instances, d_fn).call(this, r), r.update(() => t), r.doneLoading(), r;
}
async create2(e) {
let t;
e ? t = $(e) : t = A(T());
let { documentId: s } = C(L());
if (__privateGet(this, _u)) {
const n = await __privateGet(this, _u).call(this, k(t));
s = ee(n);
}
const r = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: s
});
return __privateMethod(this, _me_instances, d_fn).call(this, r), r.update(() => t), r.doneLoading(), r;
}
clone(e) {
if (!e.isReady()) throw new Error(`Cloned handle is not yet in ready state.
(Try await handle.whenReady() first.)`);
const t = e.doc(), s = this.create();
return s.update(() => j(t)), s;
}
findWithProgress(e, t = {}) {
const { signal: s } = t, { documentId: r, heads: n } = te(e) ? C(e) : {
documentId: R(e),
heads: void 0
};
if (__privateGet(this, _t4)[r]) {
const c = __privateGet(this, _t4)[r];
if (c.state === y) return {
state: "unavailable",
error: new Error(`Document ${e} is unavailable`),
handle: c
};
if (c.state === M) return {
state: "failed",
error: new Error(`Document ${e} was deleted`),
handle: c
};
if (c.state === v) return {
state: "ready",
handle: n ? c.view(n) : c
};
}
const o = __privateGet(this, _a)[r];
if (o) {
const c = __privateGet(this, _t4)[r];
if (c && (c.state === v || c.state === y || c.state === M || c.state === "loading")) return o;
}
const a = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: r
}), u = {
state: "loading",
progress: 0,
handle: a
}, h = {
subscribers: /* @__PURE__ */ new Set(),
currentProgress: void 0,
notify: (c) => {
h.currentProgress = c, h.subscribers.forEach((b) => b(c)), __privateGet(this, _a)[r] = c;
},
peek: () => h.currentProgress || u,
subscribe: (c) => (h.subscribers.add(c), () => h.subscribers.delete(c))
};
h.notify(u), __privateMethod(this, _me_instances, p_fn).call(this, e, r, a, h, s ? W(new Promise(() => {
}), s) : new Promise(() => {
}));
const p = {
...u,
peek: h.peek,
subscribe: h.subscribe
};
return __privateGet(this, _a)[r] = p, p;
}
async find(e, t = {}) {
const { allowableStates: s = [
"ready"
], signal: r } = t;
if (r == null ? void 0 : r.aborted) throw new se();
const n = this.findWithProgress(e, {
signal: r
});
if ("subscribe" in n) return __privateMethod(this, _me_instances, d_fn).call(this, n.handle), new Promise((o, a) => {
const u = n.subscribe((h) => {
s.includes(h.handle.state) ? (u(), o(h.handle)) : h.state === "unavailable" ? (u(), a(new Error(`Document ${e} is unavailable`))) : h.state === "failed" && (u(), a(h.error));
});
});
if (n.handle.state === v) return n.handle;
if (await n.handle.whenReady([
v,
y
]), n.handle.state === "unavailable" && !s.includes(y)) throw new Error(`Document ${e} is unavailable`);
return n.handle;
}
async findClassic(e, t = {}) {
const s = R(e), { allowableStates: r, signal: n } = t;
return W((async () => {
const o = await __privateMethod(this, _me_instances, y_fn).call(this, s);
if (!r && (await o.whenReady([
v,
y
]), o.state === y && !(n == null ? void 0 : n.aborted))) throw new Error(`Document ${e} is unavailable`);
return o;
})(), n);
}
delete(e) {
const t = R(e);
__privateMethod(this, _me_instances, c_fn).call(this, {
documentId: t
}).delete(), delete __privateGet(this, _t4)[t], delete __privateGet(this, _a)[t], delete __privateGet(this, _h)[t], this.emit("delete-document", {
documentId: t
});
}
async export(e) {
const t = R(e), r = (await this.find(t)).doc();
return B(r);
}
import(e, t) {
const s = t == null ? void 0 : t.docId;
if (s != null) {
const r = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: s
});
return r.update((n) => x(n, e)), __privateMethod(this, _me_instances, d_fn).call(this, r), r;
} else {
const r = re(e), n = this.create();
return n.update(() => j(r)), n;
}
}
async flush(e) {
if (!this.storageSubsystem) return;
const t = e ? e.map((s) => __privateGet(this, _t4)[s]) : Object.values(__privateGet(this, _t4));
await Promise.all(t.map(async (s) => this.storageSubsystem.saveDoc(s.documentId, s.doc())));
}
async removeFromCache(e) {
if (!__privateGet(this, _t4)[e]) {
__privateGet(this, _e4).call(this, `WARN: removeFromCache called but handle not found in handleCache for documentId: ${e}`);
return;
}
const t = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: e
});
await t.whenReady([
v,
ne,
M,
y
]), t.doc() ? (t.isReady() ? t.unload() : __privateGet(this, _e4).call(this, `WARN: removeFromCache called but handle for documentId: ${e} in unexpected state: ${t.state}`), delete __privateGet(this, _t4)[e], delete __privateGet(this, _a)[e], delete __privateGet(this, _h)[e], this.synchronizer.removeDocument(e)) : __privateGet(this, _e4).call(this, `WARN: removeFromCache called but doc undefined for documentId: ${e}`);
}
shutdown() {
return this.networkSubsystem.adapters.forEach((e) => {
e.disconnect();
}), this.flush();
}
metrics() {
return {
documents: this.synchronizer.metrics()
};
}
shareConfigChanged() {
this.synchronizer.reevaluateDocumentShare();
}
}
_e4 = new WeakMap();
_s4 = new WeakMap();
_o4 = new WeakMap();
_t4 = new WeakMap();
_n4 = new WeakMap();
_r2 = new WeakMap();
_i = new WeakMap();
_a = new WeakMap();
_h = new WeakMap();
_u = new WeakMap();
_me_instances = new WeakSet();
d_fn = function(e) {
this.storageSubsystem && (e.listeners("heads-changed").some((s) => s === __privateGet(this, _o4)) || e.on("heads-changed", __privateGet(this, _o4))), this.synchronizer.addDocument(e);
};
f_fn = function(e) {
switch (e.type) {
case "remote-subscription-change":
__privateGet(this, _i) && __privateGet(this, _r2).handleControlMessage(e);
break;
case "remote-heads-changed":
__privateGet(this, _i) && __privateGet(this, _r2).handleRemoteHeads(e);
break;
case "sync":
case "request":
case "ephemeral":
case "doc-unavailable":
this.synchronizer.receiveMessage(e).catch((t) => {
console.log("error receiving message", {
err: t,
message: e
});
});
}
};
_l = new WeakMap();
m_fn = function(e) {
if (!this.storageSubsystem) return;
const { storageId: t, isEphemeral: s } = this.peerMetadataByPeerId[e.peerId] || {};
if (!t || s) return;
let r = __privateGet(this, _l)[t];
r || (r = __privateGet(this, _l)[t] = z(({ documentId: n, syncState: o }) => {
this.storageSubsystem.saveSyncState(n, t, o);
}, __privateGet(this, _s4))), r(e);
};
c_fn = function({ documentId: e }) {
if (__privateGet(this, _t4)[e]) return __privateGet(this, _t4)[e];
if (!e) throw new Error(`Invalid documentId ${e}`);
const t = new _(e);
return __privateGet(this, _t4)[e] = t, t;
};
p_fn = async function(e, t, s, r, n) {
try {
r.notify({
state: "loading",
progress: 25,
handle: s
});
const o = await (this.storageSubsystem ? this.storageSubsystem.loadDoc(s.documentId) : Promise.resolve(null)), a = await Promise.race([
o,
n
]);
if (a ? (s.update(() => a), s.doneLoading(), r.notify({
state: "loading",
progress: 50,
handle: s
})) : (await Promise.race([
this.networkSubsystem.whenReady(),
n
]), s.request(), r.notify({
state: "loading",
progress: 75,
handle: s
})), __privateMethod(this, _me_instances, d_fn).call(this, s), await Promise.race([
s.whenReady([
v,
y
]),
n
]), s.state === y) {
const u = {
state: "unavailable",
handle: s
};
r.notify(u);
return;
}
if (s.state === M) throw new Error(`Document ${e} was deleted`);
r.notify({
state: "ready",
handle: s
});
} catch (o) {
r.notify({
state: "failed",
error: o instanceof Error || o instanceof DOMException ? o : new Error(String(o)),
handle: __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: t
})
});
}
};
y_fn = async function(e) {
if (__privateGet(this, _t4)[e]) return __privateGet(this, _t4)[e];
const t = __privateMethod(this, _me_instances, c_fn).call(this, {
documentId: e
}), s = await (this.storageSubsystem ? this.storageSubsystem.loadDoc(t.documentId) : Promise.resolve(null));
return s ? (t.update(() => s), t.doneLoading()) : (await this.networkSubsystem.whenReady(), t.request()), __privateMethod(this, _me_instances, d_fn).call(this, t), t;
};
class U extends D {
constructor() {
super(...arguments);
__publicField(this, "peerId");
__publicField(this, "peerMetadata");
}
}
class pe extends U {
constructor(e) {
super();
__privateAdd(this, _pe_instances);
__privateAdd(this, _e5);
__privateAdd(this, _s5, false);
__privateAdd(this, _o5, false);
__privateAdd(this, _t5, () => {
});
__privateAdd(this, _n5);
__privateAdd(this, _r3);
__privateAdd(this, _i2, []);
__privateSet(this, _r3, {
channelName: "broadcast",
peerWaitMs: 1e3,
...e ?? {}
}), __privateSet(this, _e5, new BroadcastChannel(__privateGet(this, _r3).channelName)), __privateSet(this, _n5, new Promise((t) => {
__privateSet(this, _t5, () => {
__privateSet(this, _o5, true), t();
}), setTimeout(() => __privateGet(this, _t5).call(this), __privateGet(this, _r3).peerWaitMs);
}));
}
isReady() {
return __privateGet(this, _o5);
}
whenReady() {
return __privateGet(this, _n5);
}
connect(e, t) {
this.peerId = e, this.peerMetadata = t, __privateSet(this, _s5, false), __privateGet(this, _e5).addEventListener("message", (s) => {
const r = s.data;
if ("targetId" in r && r.targetId !== this.peerId || __privateGet(this, _s5)) return;
const { senderId: n, type: o } = r;
switch (o) {
case "arrive":
{
const { peerMetadata: a } = r;
__privateGet(this, _e5).postMessage({
senderId: this.peerId,
targetId: n,
type: "welcome",
peerMetadata: this.peerMetadata
}), __privateMethod(this, _pe_instances, a_fn3).call(this, n, a);
}
break;
case "welcome":
{
const { peerMetadata: a } = r;
__privateMethod(this, _pe_instances, a_fn3).call(this, n, a);
}
break;
case "leave":
__privateSet(this, _i2, __privateGet(this, _i2).filter((a) => a !== n)), this.emit("peer-disconnected", {
peerId: n
});
break;
default:
if (!("data" in r)) this.emit("message", r);
else {
if (!r.data) throw new Error("We got a message without data, we can't send this.");
const a = r.data;
this.emit("message", {
...r,
data: new Uint8Array(a)
});
}
break;
}
}), __privateGet(this, _e5).postMessage({
senderId: this.peerId,
type: "arrive",
peerMetadata: t
});
}
send(e) {
if (__privateGet(this, _s5)) return false;
"data" in e ? __privateGet(this, _e5).postMessage({
...e,
data: e.data ? e.data.buffer.slice(e.data.byteOffset, e.data.byteOffset + e.data.byteLength) : void 0
}) : __privateGet(this, _e5).postMessage(e);
}
disconnect() {
__privateGet(this, _e5).postMessage({
senderId: this.peerId,
type: "leave"
});
for (const e of __privateGet(this, _i2)) this.emit("peer-disconnected", {
peerId: e
});
__privateSet(this, _s5, true);
}
}
_e5 = new WeakMap();
_s5 = new WeakMap();
_o5 = new WeakMap();
_t5 = new WeakMap();
_n5 = new WeakMap();
_r3 = new WeakMap();
_i2 = new WeakMap();
_pe_instances = new WeakSet();
a_fn3 = function(e, t) {
__privateGet(this, _t5).call(this), __privateGet(this, _i2).push(e), this.emit("peer-candidate", {
peerId: e,
peerMetadata: t
});
};
class ye {
constructor(e = "automerge", t = "documents") {
__publicField(this, "database");
__publicField(this, "store");
__publicField(this, "dbPromise");
this.database = e, this.store = t, this.dbPromise = this.createDatabasePromise();
}
createDatabasePromise() {
return new Promise((e, t) => {
const s = indexedDB.open(this.database, 1);
s.onerror = () => {
t(s.error);
}, s.onupgradeneeded = (r) => {
r.target.result.createObjectStore(this.store);
}, s.onsuccess = (r) => {
const n = r.target.result;
e(n);
};
});
}
async load(e) {
const s = (await this.dbPromise).transaction(this.store), n = s.objectStore(this.store).get(e);
return new Promise((o, a) => {
s.onerror = () => {
a(n.error);
}, n.onsuccess = (u) => {
const h = u.target.result;
h && typeof h == "object" && "binary" in h ? o(h.binary) : o(void 0);
};
});
}
async save(e, t) {
const r = (await this.dbPromise).transaction(this.store, "readwrite");
return r.objectStore(this.store).put({
key: e,
binary: t
}, e), new Promise((o, a) => {
r.onerror = () => {
a(r.error);
}, r.oncomplete = () => {
o();
};
});
}
async remove(e) {
const s = (await this.dbPromise).transaction(this.store, "readwrite");
return s.objectStore(this.store).delete(e), new Promise((n, o) => {
s.onerror = () => {
o(s.error);
}, s.oncomplete = () => {
n();
};
});
}
async loadRange(e) {
const t = await this.dbPromise, s = e, r = [
...e,
"\uFFFF"
], n = IDBKeyRange.bound(s, r), o = t.transaction(this.store), u = o.objectStore(this.store).openCursor(n), h = [];
return new Promise((p, c) => {
o.onerror = () => {
c(u.error);
}, u.onsuccess = (b) => {
const m = b.target.result;
m ? (h.push({
data: m.value.binary,
key: m.key
}), m.continue()) : p(h);
};
});
}
async removeRange(e) {
const t = await this.dbPromise, s = e, r = [
...e,
"\uFFFF"
], n = IDBKeyRange.bound(s, r), o = t.transaction(this.store, "readwrite");
return o.objectStore(this.store).delete(n), new Promise((u, h) => {
o.onerror = () => {
h(o.error);
}, o.oncomplete = () => {
u();
};
});
}
}
var w = null;
typeof WebSocket < "u" ? w = WebSocket : typeof MozWebSocket < "u" ? w = MozWebSocket : typeof global < "u" ? w = global.WebSocket || global.MozWebSocket : typeof window < "u" ? w = window.WebSocket || window.MozWebSocket : typeof self < "u" && (w = self.WebSocket || self.MozWebSocket);
const ge = (d) => d.type === "peer", be = (d) => d.type === "error", we = "1";
function g(d, e = "Assertion failed") {
if (d === false || d === null || d === void 0) {
const t = new Error(ve(e));
throw t.stack = Se(t.stack, "assert.ts"), t;
}
}
const ve = (d) => d.split(`
`).map((e) => e.trim()).join(`
`), Se = (d = "", e) => d.split(`
`).filter((t) => !t.includes(e)).join(`
`), Ie = (d) => {
const { buffer: e, byteOffset: t, byteLength: s } = d;
return e.slice(t, t + s);
};
class ke extends U {
constructor() {
super(...arguments);
__publicField(this, "socket");
}
}
class Pe extends ke {
constructor(e, t = 5e3) {
super();
__privateAdd(this, _Pe_instances);
__publicField(this, "url");
__publicField(this, "retryInterval");
__privateAdd(this, _e6, false);
__privateAdd(this, _s6);
__privateAdd(this, _o6, new Promise((e) => {
__privateSet(this, _s6, e);
}));
__privateAdd(this, _n6);
__privateAdd(this, _r4, P("automerge-repo:websocket:browser"));
__publicField(this, "remotePeerId");
__publicField(this, "onOpen", () => {
__privateGet(this, _r4).call(this, "open"), clearInterval(__privateGet(this, _n6)), __privateSet(this, _n6, void 0), this.join();
});
__publicField(this, "onClose", () => {
__privateGet(this, _r4).call(this, "close"), this.remotePeerId && this.emit("peer-disconnected", {
peerId: this.remotePeerId
}), this.retryInterval > 0 && !__privateGet(this, _n6) && setTimeout(() => (g(this.peerId), this.connect(this.peerId, this.peerMetadata)), this.retryInterval);
});
__publicField(this, "onMessage", (e) => {
this.receiveMessage(e.data);
});
__publicField(this, "onError", (e) => {
if ("error" in e && e.error.code !== "ECONNREFUSED") throw e.error;
__privateGet(this, _r4).call(this, "Connection failed, retrying...");
});
this.url = e, this.retryInterval = t, __privateSet(this, _r4, __privateGet(this, _r4).extend(e));
}
isReady() {
return __privateGet(this, _e6);
}
whenReady() {
return __privateGet(this, _o6);
}
connect(e, t) {
!this.socket || !this.peerId ? (__privateGet(this, _r4).call(this, "connecting"), this.peerId = e, this.peerMetadata = t ?? {}) : (__privateGet(this, _r4).call(this, "reconnecting"), g(e === this.peerId), this.socket.removeEventListener("open", this.onOpen), this.socket.removeEventListener("close", this.onClose), this.socket.removeEventListener("message", this.onMessage), this.socket.removeEventListener("error", this.onError)), __privateGet(this, _n6) || __privateSet(this, _n6, setInterval(() => {
this.connect(e, t);
}, this.retryInterval)), this.socket = new w(this.url), this.socket.binaryType = "arraybuffer", this.socket.addEventListener("open", this.onOpen), this.socket.addEventListener("close", this.onClose), this.socket.addEventListener("message", this.onMessage), this.socket.addEventListener("error", this.onError), setTimeout(() => __privateMethod(this, _Pe_instances, t_fn).call(this), 1e3), this.join();
}
join() {
g(this.peerId), g(this.socket), this.socket.readyState === w.OPEN && this.send(Ee(this.peerId, this.peerMetadata));
}
disconnect() {
g(this.peerId), g(this.socket);
const e = this.socket;
e && (e.removeEventListener("open", this.onOpen), e.removeEventListener("close", this.onClose), e.removeEventListener("message", this.onMessage), e.removeEventListener("error", this.onError), e.close()), clearInterval(__privateGet(this, _n6)), this.remotePeerId && this.emit("peer-disconnected", {
peerId: this.remotePeerId
}), this.socket = void 0;
}
send(e) {
var _a2;
if ("data" in e && ((_a2 = e.data) == null ? void 0 : _a2.byteLength) === 0) throw new Error("Tried to send a zero-length message");
if (g(this.peerId), !this.socket) {
__privateGet(this, _r4).call(this, "Tried to send on a disconnected socket.");
return;
}
if (this.socket.readyState !== w.OPEN) throw new Error(`Websocket not ready (${this.socket.readyState})`);
const t = oe(e);
this.socket.send(Ie(t));
}
peerCandidate(e, t) {
g(this.socket), __privateMethod(this, _Pe_instances, t_fn).call(this), this.remotePeerId = e, this.emit("peer-candidate", {
peerId: e,
peerMetadata: t
});
}
receiveMessage(e) {
let t;
try {
t = ie(new Uint8Array(e));
} catch (s) {
__privateGet(this, _r4).call(this, "error decoding message:", s);
return;
}
if (g(this.socket), e.byteLength === 0) throw new Error("received a zero-length message");
if (ge(t)) {
const { peerMetadata: s } = t;
__privateGet(this, _r4).call(this, `peer: ${t.senderId}`), this.peerCandidate(t.senderId, s);
} else be(t) ? __privateGet(this, _r4).call(this, `error: ${t.message}`) : this.emit("message", t);
}
}
_e6 = new WeakMap();
_s6 = new WeakMap();
_o6 = new WeakMap();
_Pe_instances = new WeakSet();
t_fn = function() {
var _a2;
__privateGet(this, _e6) || (__privateSet(this, _e6, true), (_a2 = __privateGet(this, _s6)) == null ? void 0 : _a2.call(this));
};
_n6 = new WeakMap();
_r4 = new WeakMap();
function Ee(d, e) {
return {
type: "join",
senderId: d,
peerMetadata: e,
supportedProtocolVersions: [
we
]
};
}
P("WebsocketServer");
let S = null;
Re = function() {
return S || (S = new me({
network: [
new Pe("wss://sync.automerge.org"),
new pe()
],
storage: new ye("p2p-poll-app")
}), S);
};
Me = function() {
S && (S.shutdown(), S = null);
};
});
export {
__tla,
Me as cleanupRepo,
Re as getRepo
};