Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@toeverything/y-indexeddb

Package Overview
Dependencies
Maintainers
3
Versions
154
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@toeverything/y-indexeddb - npm Package Compare versions

Comparing version 0.10.0-canary.1 to 0.10.0-canary.2

494

dist/index.js

@@ -1,218 +0,46 @@

import { openDB as S } from "idb";
import { applyUpdate as D, encodeStateAsUpdate as U, encodeStateVector as T, Doc as z, diffUpdate as G, encodeStateVectorFromUpdate as J, UndoManager as K } from "yjs";
const A = 1, v = "affine-local";
function P(t) {
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" });
import { openDB as p } from "idb";
import { Doc as A, applyUpdate as S, encodeStateAsUpdate as D, diffUpdate as U, encodeStateVectorFromUpdate as B, encodeStateVector as P, UndoManager as M } from "yjs";
import { writeOperation as C, createLazyProvider as V } from "y-provider";
const m = 1, l = "affine-local";
function f(e) {
e.createObjectStore("workspace", { keyPath: "id" }), e.createObjectStore("milestone", { keyPath: "id" });
}
function M(t, a = "val does not exist") {
if (t == null)
throw a instanceof Error ? a : new Error(a);
function E(e, n = "val does not exist") {
if (e == null)
throw n instanceof Error ? n : new Error(n);
}
function F(t, a) {
if (t.guid === a)
return t;
for (const s of t.subdocs) {
const e = F(s, a);
if (e)
return e;
}
let y;
function g(e) {
const n = new A();
return e.forEach((a) => {
S(n, a);
}), D(n);
}
const Q = (t, a, s = {}) => {
let e = !1;
const d = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Set();
let u = null;
const { origin: i = "lazy-provider" } = s;
let p = {
type: "idle"
}, f = 0;
const b = /* @__PURE__ */ new Set(), l = (n) => {
n.type === "syncing" ? f++ : (n.type === "synced" || n.type === "error") && f--, f < 0 && console.error(
"syncingStatus < 0, this should not happen",
s.origin
), f === 0 && (p = n), n.type !== "synced" && (p = n), f === 0 && (e ? p = {
type: "synced"
} : p = {
type: "idle"
}), b.forEach((c) => c());
};
async function k(n) {
const c = n.guid;
{
const y = c.startsWith("space:") ? c.slice(6) : c, m = `${t.guid}:space:${y}`, h = `space:${y}`, g = await a.queryDocState(m), E = await a.queryDocState(h);
let B = !1;
g && g.missing.length !== 2 && g.missing[0] !== 0 && g.missing[1] !== 0 && (D(n, g.missing, i), B = !0), E && E.missing.length !== 2 && E.missing[0] !== 0 && E.missing[1] !== 0 && (D(n, E.missing, i), B = !0), B && await a.sendDocUpdate(
c,
U(
n,
g ? g.state : E ? E.state : void 0
)
);
}
if (!e)
return;
l({
type: "syncing"
});
const w = await a.queryDocState(c, {
stateVector: T(n)
}).then((y) => (l({
type: "synced"
}), y)).catch((y) => {
throw l({
type: "error",
error: y
}), y;
});
d.set(c, []), w && D(n, w.missing, i), e && (await a.sendDocUpdate(
c,
U(n, w ? w.state : void 0)
), n.emit("sync", []));
}
function L(n) {
const c = /* @__PURE__ */ new Set();
r.set(n.guid, c);
const w = async (m, h) => {
i !== h && (l({
type: "syncing"
}), a.sendDocUpdate(n.guid, m).then(() => {
l({
type: "synced"
});
}).catch((g) => {
l({
type: "error",
error: g
}), console.error(g);
}));
}, y = (m) => {
m.loaded.forEach((h) => {
V(h).catch(console.error);
}), m.removed.forEach((h) => {
$(h);
});
async function O(e) {
return new Promise((n) => {
const a = indexedDB.open(e);
let t = !0;
a.onsuccess = function() {
a.result.close(), t || indexedDB.deleteDatabase(e), n(t);
}, a.onupgradeneeded = function() {
t = !1;
};
n.on("update", w), n.on("subdocs", y), c.add(() => {
n.off("update", w), n.off("subdocs", y);
});
}
function N() {
var c;
M(u, "abortController should be defined");
const n = (c = a.onDocUpdate) == null ? void 0 : c.call(a, (w, y) => {
var h;
l({
type: "syncing"
});
const m = F(t, w);
m ? (D(m, y, i), d.has(w) && ((h = d.get(w)) == null || h.forEach((g) => D(m, g, i)), d.delete(w))) : (console.warn("doc not found", w), d.set(w, (d.get(w) ?? []).concat(y))), l({
type: "synced"
});
});
u.signal.addEventListener("abort", () => {
n == null || n();
});
}
async function V(n) {
o.has(n.guid) || (o.add(n.guid), L(n), await k(n), await Promise.all(
[...n.subdocs].filter((c) => c.shouldLoad).map((c) => V(c))
));
}
function $(n) {
o.delete(n.guid);
const c = r.get(n.guid);
c && (c.forEach((w) => w()), r.delete(n.guid)), n.subdocs.forEach($);
}
function W() {
r.forEach((n) => {
n.forEach((c) => c());
}), r.clear(), o.clear();
}
function _() {
e = !0, u = new AbortController(), l({
type: "syncing"
}), V(t).then(() => {
l({
type: "synced"
});
}).catch((n) => {
l({
type: "error",
error: n
}), console.error(n);
}), N();
}
async function R() {
e = !1, W(), M(u, "abortController should be defined"), u.abort(), u = null;
}
const O = async (n) => {
await k(n), await Promise.all(
[...n.subdocs.values()].map((c) => O(c))
);
};
return {
sync: async (n) => {
e = !0;
try {
n ? await k(t) : await O(t);
} finally {
e = !1;
}
},
get status() {
return p;
},
subscribeStatusChange(n) {
return b.add(n), () => {
b.delete(n);
};
},
get connected() {
return e;
},
passive: !0,
connect: _,
disconnect: R,
datasource: a
};
}, q = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), X = async (t) => {
window.addEventListener("beforeunload", q, {
capture: !0
}), await t, window.removeEventListener("beforeunload", q, {
capture: !0
});
};
let x;
function j(t) {
const a = new z();
return t.forEach((s) => {
D(a, s);
}), U(a);
}
async function Y(t) {
return new Promise((a) => {
const s = indexedDB.open(t);
let e = !0;
s.onsuccess = function() {
s.result.close(), e || indexedDB.deleteDatabase(t), a(e);
}, s.onupgradeneeded = function() {
e = !1;
};
});
}
async function ne(t, a, s = v) {
async function q(e, n, a = l) {
do
if (!x || localStorage.getItem(`${s}-migration`) !== "true") {
if (!y || localStorage.getItem(`${a}-migration`) !== "true") {
try {
x = await indexedDB.databases();
y = await indexedDB.databases();
} catch {
if (await Y(a)) {
await S(a, 1).then(async (e) => {
if (!e.objectStoreNames.contains("updates"))
if (await O(n)) {
await p(n, 1).then(async (t) => {
if (!t.objectStoreNames.contains("updates"))
return;
const r = await e.transaction("updates", "readonly").objectStore("updates").getAll();
if (!Array.isArray(r) || !r.every((p) => p instanceof Uint8Array))
const r = await t.transaction("updates", "readonly").objectStore("updates").getAll();
if (!Array.isArray(r) || !r.every((d) => d instanceof Uint8Array))
return;
const o = j(r), u = t.transaction("workspace", "readwrite").objectStore("workspace");
await u.get(a) || (console.log("upgrading the database"), await u.put({
id: a,
const o = g(r), i = e.transaction("workspace", "readwrite").objectStore("workspace");
await i.get(n) || (console.log("upgrading the database"), await i.put({
id: n,
updates: [

@@ -230,19 +58,19 @@ {

await Promise.all(
x && x.map((e) => {
if (e.name && e.version === 1) {
const d = e.name, r = e.version;
return S(d, r).then(
y && y.map((t) => {
if (t.name && t.version === 1) {
const c = t.name, r = t.version;
return p(c, r).then(
async (o) => {
if (!o.objectStoreNames.contains("updates"))
return;
const i = await o.transaction("updates", "readonly").objectStore("updates").getAll();
if (!Array.isArray(i) || !i.every((l) => l instanceof Uint8Array))
const s = await o.transaction("updates", "readonly").objectStore("updates").getAll();
if (!Array.isArray(s) || !s.every((b) => b instanceof Uint8Array))
return;
const p = j(i), f = t.transaction("workspace", "readwrite").objectStore("workspace");
await f.get(d) || (console.log("upgrading the database"), await f.put({
id: d,
const d = g(s), u = e.transaction("workspace", "readwrite").objectStore("workspace");
await u.get(c) || (console.log("upgrading the database"), await u.put({
id: c,
updates: [
{
timestamp: Date.now(),
update: p
update: d
}

@@ -255,3 +83,3 @@ ]

})
), localStorage.setItem(`${s}-migration`, "true");
), localStorage.setItem(`${a}-migration`, "true");
break;

@@ -261,17 +89,17 @@ }

}
async function ae(t, a = v) {
const r = await (await S(a, A, {
upgrade: P
})).transaction("workspace", "readonly").objectStore("workspace").get(t);
return r ? j(r.updates.map(({ update: o }) => o)) : !1;
async function F(e, n = l) {
const r = await (await p(n, m, {
upgrade: f
})).transaction("workspace", "readonly").objectStore("workspace").get(e);
return r ? g(r.updates.map(({ update: o }) => o)) : !1;
}
async function se(t, a, s = v) {
await (await S(s, A, {
upgrade: P
async function N(e, n, a = l) {
await (await p(a, m, {
upgrade: f
})).transaction("workspace", "readwrite").objectStore("workspace").put({
id: t,
id: e,
updates: [
{
timestamp: Date.now(),
update: a
update: n
}

@@ -281,15 +109,15 @@ ]

}
async function re(t, a, s = v) {
const r = (await S(s, A, {
upgrade: P
})).transaction("workspace", "readwrite").objectStore("workspace"), o = await r.get(t);
async function _(e, n, a = l) {
const r = (await p(a, m, {
upgrade: f
})).transaction("workspace", "readwrite").objectStore("workspace"), o = await r.get(e);
o ? (o.updates.push({
timestamp: Date.now(),
update: a
update: n
}), await r.put(o)) : await r.put({
id: t,
id: e,
updates: [
{
timestamp: Date.now(),
update: a
update: n
}

@@ -299,27 +127,27 @@ ]

}
let H = 500;
function oe(t) {
H = t;
let v = 500;
function z(e) {
v = e;
}
const Z = ({
dbName: t = v,
mergeCount: a
const T = ({
dbName: e = l,
mergeCount: n
}) => {
let s = null;
const e = async () => (s === null && (s = S(t, A, {
upgrade: P
})), s);
let a = null;
const t = async () => (a === null && (a = p(e, m, {
upgrade: f
})), a);
return {
...{
queryDocState: async (r, o) => {
var u;
var i;
try {
const f = await (await e()).transaction("workspace", "readonly").objectStore("workspace").get(r);
if (!f)
const u = await (await t()).transaction("workspace", "readonly").objectStore("workspace").get(r);
if (!u)
return !1;
const { updates: b } = f, l = j(b.map(({ update: L }) => L));
return { missing: o != null && o.stateVector ? G(l, o == null ? void 0 : o.stateVector) : l, state: J(l) };
} catch (i) {
if (!((u = i.message) != null && u.includes("The database connection is closing.")))
throw i;
const { updates: w } = u, b = g(w.map(({ update: x }) => x));
return { missing: o != null && o.stateVector ? U(b, o == null ? void 0 : o.stateVector) : b, state: B(b) };
} catch (s) {
if (!((i = s.message) != null && i.includes("The database connection is closing.")))
throw s;
return !1;

@@ -329,22 +157,22 @@ }

sendDocUpdate: async (r, o) => {
var u;
var i;
try {
const p = (await e()).transaction("workspace", "readwrite").objectStore("workspace"), { updates: f } = await p.get(r) ?? { updates: [] };
let b = [
...f,
const d = (await t()).transaction("workspace", "readwrite").objectStore("workspace"), { updates: u } = await d.get(r) ?? { updates: [] };
let w = [
...u,
{ timestamp: Date.now(), update: o }
];
if (a && b.length >= a) {
const l = j(b.map(({ update: k }) => k));
b = [{ timestamp: Date.now(), update: l }];
if (n && w.length >= n) {
const b = g(w.map(({ update: k }) => k));
w = [{ timestamp: Date.now(), update: b }];
}
await X(
p.put({
await C(
d.put({
id: r,
updates: b
updates: w
})
);
} catch (i) {
if (!((u = i.message) != null && u.includes("The database connection is closing.")))
throw i;
} catch (s) {
if (!((i = s.message) != null && i.includes("The database connection is closing.")))
throw s;
}

@@ -354,71 +182,71 @@ }

disconnect: () => {
e().then((r) => r.close()).then(() => {
s = null;
t().then((r) => r.close()).then(() => {
a = null;
}).catch(console.error);
},
cleanup: async () => {
await (await e()).clear("workspace");
await (await t()).clear("workspace");
}
};
}, ce = (t, a = v) => {
const s = Z({ dbName: a, mergeCount: H });
let e = null;
const d = {
}, W = (e, n = l) => {
const a = T({ dbName: n, mergeCount: v });
let t = null;
const c = {
get status() {
return M(e), e.status;
return E(t), t.status;
},
subscribeStatusChange(r) {
return M(e), e.subscribeStatusChange(r);
return E(t), t.subscribeStatusChange(r);
},
connect: () => {
d.connected && d.disconnect(), e = Q(t, s, { origin: "idb" }), e.connect();
c.connected && c.disconnect(), t = V(e, a, { origin: "idb" }), t.connect();
},
disconnect: () => {
s == null || s.disconnect(), e == null || e.disconnect(), e = null;
a == null || a.disconnect(), t == null || t.disconnect(), t = null;
},
cleanup: async () => {
await (s == null ? void 0 : s.cleanup());
await (a == null ? void 0 : a.cleanup());
},
get connected() {
return (e == null ? void 0 : e.connected) || !1;
return (t == null ? void 0 : t.connected) || !1;
},
datasource: s
datasource: a
};
return d;
}, C = "snapshot-origin", I = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), ie = async (t) => {
window.addEventListener("beforeunload", I, {
return c;
}, h = "snapshot-origin", j = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), G = async (e) => {
window.addEventListener("beforeunload", j, {
capture: !0
}), await t, window.removeEventListener("beforeunload", I, {
}), await e, window.removeEventListener("beforeunload", j, {
capture: !0
});
};
function de(t, a, s) {
const e = new z();
D(e, a, C);
const d = T(t), r = T(e), o = U(
t,
function H(e, n, a) {
const t = new A();
S(t, n, h);
const c = P(e), r = P(t), o = D(
e,
r
), u = new K(
[...e.share.keys()].map((p) => {
const f = s(p);
if (f === "Text")
return e.getText(p);
if (f === "Map")
return e.getMap(p);
if (f === "Array")
return e.getArray(p);
), i = new M(
[...t.share.keys()].map((d) => {
const u = a(d);
if (u === "Text")
return t.getText(d);
if (u === "Map")
return t.getMap(d);
if (u === "Array")
return t.getArray(d);
throw new Error("Unknown type");
}),
{
trackedOrigins: /* @__PURE__ */ new Set([C])
trackedOrigins: /* @__PURE__ */ new Set([h])
}
);
D(e, o, C), u.undo();
const i = U(
e,
d
S(t, o, h), i.undo();
const s = D(
t,
c
);
D(t, i, C);
S(e, s, h);
}
class ue extends Error {
class J extends Error {
constructor() {

@@ -428,3 +256,3 @@ super("Early disconnect");

}
class le extends Error {
class K extends Error {
constructor() {

@@ -434,37 +262,37 @@ super("Cleanup when connecting");

}
const pe = async (t, a, s, e = v) => {
const o = (await S(e, A, {
upgrade: P
})).transaction("milestone", "readwrite").objectStore("milestone"), u = await o.get("id"), i = U(a);
u ? (u.milestone[s] = i, await o.put(u)) : await o.put({
id: t,
const Q = async (e, n, a, t = l) => {
const o = (await p(t, m, {
upgrade: f
})).transaction("milestone", "readwrite").objectStore("milestone"), i = await o.get("id"), s = D(n);
i ? (i.milestone[a] = s, await o.put(i)) : await o.put({
id: e,
milestone: {
[s]: i
[a]: s
}
});
}, fe = async (t, a = v) => {
const r = await (await S(a, A, {
upgrade: P
})).transaction("milestone", "readonly").objectStore("milestone").get(t);
}, R = async (e, n = l) => {
const r = await (await p(n, m, {
upgrade: f
})).transaction("milestone", "readonly").objectStore("milestone").get(e);
return r ? r.milestone : null;
};
export {
le as CleanupWhenConnectingError,
v as DEFAULT_DB_NAME,
ue as EarlyDisconnectError,
Z as createIndexedDBDatasource,
ce as createIndexedDBProvider,
A as dbVersion,
ae as downloadBinary,
fe as getMilestones,
pe as markMilestone,
j as mergeUpdates,
se as overwriteBinary,
re as pushBinary,
de as revertUpdate,
oe as setMergeCount,
ne as tryMigrate,
P as upgradeDB,
ie as writeOperation
K as CleanupWhenConnectingError,
l as DEFAULT_DB_NAME,
J as EarlyDisconnectError,
T as createIndexedDBDatasource,
W as createIndexedDBProvider,
m as dbVersion,
F as downloadBinary,
R as getMilestones,
Q as markMilestone,
g as mergeUpdates,
N as overwriteBinary,
_ as pushBinary,
H as revertUpdate,
z as setMergeCount,
q as tryMigrate,
f as upgradeDB,
G as writeOperation
};
//# sourceMappingURL=index.js.map
{
"name": "@toeverything/y-indexeddb",
"type": "module",
"version": "0.10.0-canary.1",
"version": "0.10.0-canary.2",
"description": "IndexedDB database adapter for Yjs",

@@ -36,8 +36,8 @@ "repository": "toeverything/AFFiNE",

"idb": "^7.1.1",
"nanoid": "^5.0.1"
"nanoid": "^5.0.1",
"y-provider": "workspace:*"
},
"devDependencies": {
"@affine/y-provider": "workspace:*",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/blocks": "0.0.0-20231018100009-361737d3-nightly",
"@blocksuite/store": "0.0.0-20231018100009-361737d3-nightly",
"fake-indexeddb": "^5.0.0",

@@ -47,3 +47,4 @@ "vite": "^4.4.11",

"vitest": "0.34.6",
"y-indexeddb": "^9.0.11"
"y-indexeddb": "^9.0.11",
"yjs": "^13.6.8"
},

@@ -50,0 +51,0 @@ "peerDependencies": {

@@ -1,3 +0,3 @@

import type { DataSourceAdapter } from '@affine/y-provider';
import type { DBSchema, IDBPDatabase } from 'idb';
import type { DataSourceAdapter } from 'y-provider';
export declare const dbVersion = 1;

@@ -4,0 +4,0 @@ export declare const DEFAULT_DB_NAME = "affine-local";

{
"name": "@toeverything/y-indexeddb",
"type": "module",
"version": "0.10.0-canary.1",
"version": "0.10.0-canary.2",
"description": "IndexedDB database adapter for Yjs",

@@ -41,8 +41,8 @@ "repository": "toeverything/AFFiNE",

"idb": "^7.1.1",
"nanoid": "^5.0.1"
"nanoid": "^5.0.1",
"y-provider": "0.10.0-canary.2"
},
"devDependencies": {
"@affine/y-provider": "0.10.0-canary.1",
"@blocksuite/blocks": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/store": "0.0.0-20230926212737-6d4b1569-nightly",
"@blocksuite/blocks": "0.0.0-20231018100009-361737d3-nightly",
"@blocksuite/store": "0.0.0-20231018100009-361737d3-nightly",
"fake-indexeddb": "^5.0.0",

@@ -52,3 +52,4 @@ "vite": "^4.4.11",

"vitest": "0.34.6",
"y-indexeddb": "^9.0.11"
"y-indexeddb": "^9.0.11",
"yjs": "^13.6.8"
},

@@ -55,0 +56,0 @@ "peerDependencies": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc