Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Refactor CRDT usage

Hakan Shehu4f77314c6bdd
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 83702018..4e0b241b 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -84,7 +84,6 @@
"form-data": "^4.0.1",
"fractional-indexing-jittered": "^0.9.1",
"is-hotkey": "^0.2.0",
- "js-base64": "^3.7.7",
"js-sha256": "^0.11.0",
"kysely": "^0.27.4",
"lodash": "^4.17.21",
diff --git a/apps/desktop/src/main/node-manager.ts b/apps/desktop/src/main/node-manager.ts
index a656b5d0..5e460053 100644
--- a/apps/desktop/src/main/node-manager.ts
+++ b/apps/desktop/src/main/node-manager.ts
@@ -1,12 +1,10 @@
-import * as Y from 'yjs';
import {
Node,
NodeAttributes,
NodeMutationContext,
registry,
} from '@colanode/core';
-import { applyCrdt } from '@colanode/crdt';
-import { fromUint8Array } from 'js-base64';
+import { YDoc } from '@colanode/crdt';
import {
LocalCreateNodeChangeData,
LocalDeleteNodeChangeData,
@@ -49,9 +47,6 @@ class NodeManager {
await workspaceDatabase.transaction().execute(async (transaction) => {
for (const input of inputs) {
- const doc = new Y.Doc({ guid: input.id });
- const attributesMap = doc.getMap('attributes');
-
const model = registry[input.attributes.type];
if (!model) {
throw new Error('Invalid node type');
@@ -81,8 +76,8 @@ class NodeManager {
throw new Error('Insufficient permissions');
}
- applyCrdt(model.schema, input.attributes, attributesMap);
- const state = Y.encodeStateAsUpdate(doc);
+ const ydoc = new YDoc(input.id);
+ ydoc.updateAttributes(model.schema, input.attributes);
const createdAt = new Date().toISOString();
const versionId = generateId(IdType.Version);
@@ -90,7 +85,7 @@ class NodeManager {
const changeData: LocalCreateNodeChangeData = {
type: 'node_create',
id: input.id,
- state: fromUint8Array(state),
+ state: ydoc.getEncodedState(),
createdAt: createdAt,
createdBy: context.userId,
versionId: versionId,
@@ -101,7 +96,7 @@ class NodeManager {
.values({
id: input.id,
attributes: JSON.stringify(input.attributes),
- state: state,
+ state: ydoc.getState(),
created_at: createdAt,
created_by: context.userId,
version_id: versionId,
@@ -191,17 +186,8 @@ class NodeManager {
ancestors
);
- const doc = new Y.Doc({ guid: nodeId });
- Y.applyUpdate(doc, nodeRow.state);
-
const versionId = generateId(IdType.Version);
const updatedAt = new Date().toISOString();
- const updates: string[] = [];
-
- doc.on('update', (update) => {
- updates.push(fromUint8Array(update));
- });
-
const updatedAttributes = updater(node.attributes);
const model = registry[node.type];
@@ -217,22 +203,17 @@ class NodeManager {
throw new Error('Insufficient permissions');
}
- const attributesMap = doc.getMap('attributes');
- doc.transact(() => {
- applyCrdt(
- registry[updatedAttributes.type].schema,
- updatedAttributes,
- attributesMap
- );
- });
+ const ydoc = new YDoc(nodeRow.id, nodeRow.state);
+ ydoc.updateAttributes(
+ registry[updatedAttributes.type].schema,
+ updatedAttributes
+ );
+ const updates = ydoc.getEncodedUpdates();
if (updates.length === 0) {
return true;
}
- const attributesJson = JSON.stringify(attributesMap.toJSON());
- const state = Y.encodeStateAsUpdate(doc);
-
const changeData: LocalUpdateNodeChangeData = {
type: 'node_update',
id: nodeId,
@@ -248,8 +229,8 @@ class NodeManager {
const result = await trx
.updateTable('nodes')
.set({
- attributes: attributesJson,
- state: state,
+ attributes: JSON.stringify(ydoc.getAttributes()),
+ state: ydoc.getState(),
updated_at: updatedAt,
updated_by: context.userId,
version_id: versionId,
diff --git a/apps/server/package.json b/apps/server/package.json
index df60c2c6..86a11257 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -30,17 +30,16 @@
"tsup": "^8.3.0"
},
"dependencies": {
- "@colanode/core": "workspace:*",
- "@colanode/crdt": "workspace:*",
"@aws-sdk/client-s3": "^3.675.0",
"@aws-sdk/s3-request-presigner": "^3.675.0",
+ "@colanode/core": "workspace:*",
+ "@colanode/crdt": "workspace:*",
"axios": "^1.7.7",
"bcrypt": "^5.1.1",
"bullmq": "^5.21.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.21.1",
- "js-base64": "^3.7.7",
"js-sha256": "^0.11.0",
"kafkajs": "^2.2.4",
"kysely": "^0.27.4",
@@ -50,7 +49,6 @@
"postgres": "^3.4.4",
"redis": "^4.7.0",
"sharp": "^0.33.5",
- "ws": "^8.18.0",
- "yjs": "^13.6.20"
+ "ws": "^8.18.0"
}
}
diff --git a/apps/server/src/routes/sync.ts b/apps/server/src/routes/sync.ts
index 66dc9b15..478d4e5c 100644
--- a/apps/server/src/routes/sync.ts
+++ b/apps/server/src/routes/sync.ts
@@ -1,4 +1,3 @@
-import * as Y from 'yjs';
import { ApiError, ColanodeRequest, ColanodeResponse } from '@/types/api';
import { database, hasUpdateChanges } from '@/data/database';
import { Router } from 'express';
@@ -14,7 +13,6 @@ import {
SyncLocalChangesInput,
} from '@/types/sync';
import { SelectWorkspaceUser } from '@/data/schema';
-import { toUint8Array } from 'js-base64';
import {
NodeCreatedEvent,
NodeDeletedEvent,
@@ -22,13 +20,9 @@ import {
} from '@/types/events';
import { enqueueEvent } from '@/queues/events';
import { synapse } from '@/services/synapse';
-import { fetchNodeAncestors, mapNode, mapServerNode } from '@/lib/nodes';
-import {
- Node,
- NodeAttributes,
- NodeMutationContext,
- registry,
-} from '@colanode/core';
+import { fetchNodeAncestors, mapNode } from '@/lib/nodes';
+import { NodeMutationContext, registry } from '@colanode/core';
+import { YDoc } from '@colanode/crdt';
export const syncRouter = Router();
@@ -121,11 +115,8 @@ const handleCreateNodeChange = async (
};
}
- const doc = new Y.Doc({ guid: changeData.id });
- Y.applyUpdate(doc, toUint8Array(changeData.state));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = attributesMap.toJSON() as NodeAttributes;
+ const ydoc = new YDoc(changeData.id, changeData.state);
+ const attributes = ydoc.getAttributes();
const model = registry[attributes.type];
if (!model) {
@@ -164,7 +155,7 @@ const handleCreateNodeChange = async (
id: changeData.id,
attributes: JSON.stringify(attributes),
workspace_id: workspaceUser.workspace_id,
- state: toUint8Array(changeData.state),
+ state: ydoc.getState(),
created_at: new Date(changeData.createdAt),
created_by: changeData.createdBy,
version_id: changeData.versionId,
@@ -211,17 +202,15 @@ const handleUpdateNodeChange = async (
};
}
- const doc = new Y.Doc({ guid: changeData.id });
- Y.applyUpdate(doc, existingNode.state);
+ const ydoc = new YDoc(changeData.id, existingNode.state);
for (const update of changeData.updates) {
- Y.applyUpdate(doc, toUint8Array(update));
+ ydoc.applyUpdate(update);
}
- const attributesMap = doc.getMap('attributes');
- const attributes = attributesMap.toJSON() as NodeAttributes;
+ const attributes = ydoc.getAttributes();
const attributesJson = JSON.stringify(attributes);
- const state = Y.encodeStateAsUpdate(doc);
+ const state = ydoc.getState();
const model = registry[attributes.type];
if (!model) {
diff --git a/packages/crdt/package.json b/packages/crdt/package.json
index 97eea733..80b41b74 100644
--- a/packages/crdt/package.json
+++ b/packages/crdt/package.json
@@ -13,7 +13,7 @@
"compile": "tsc --noEmit",
"test": "vitest",
"lint": "eslint . --max-warnings 0",
- "build?": "No build step required. This package links directly to its source files",
+ "build": "tsc",
"coverage": "vitest run --coverage "
},
"author": "Hakan Shehu",
@@ -26,6 +26,7 @@
"dependencies": {
"@colanode/core": "workspace:*",
"diff": "^7.0.0",
+ "js-base64": "^3.7.7",
"yjs": "^13.6.20",
"zod": "^3.23.8"
}
diff --git a/packages/crdt/src/index.ts b/packages/crdt/src/index.ts
index cf870e3c..10f16daa 100644
--- a/packages/crdt/src/index.ts
+++ b/packages/crdt/src/index.ts
@@ -1,299 +1,350 @@
import { z } from 'zod';
import * as Y from 'yjs';
-import { ZodText } from '@colanode/core';
+import { NodeAttributes, ZodText } from '@colanode/core';
import { isEqual } from 'lodash-es';
import { diffChars } from 'diff';
+import { fromUint8Array, toUint8Array } from 'js-base64';
+
+export class YDoc {
+ private doc: Y.Doc;
+ private updates: Uint8Array[] = [];
+
+ constructor(id: string, state?: Uint8Array | string) {
+ this.doc = new Y.Doc({ guid: id });
+ if (state) {
+ Y.applyUpdate(
+ this.doc,
+ typeof state === 'string' ? toUint8Array(state) : state
+ );
+ }
-export const applyCrdt = (
- schema: z.ZodSchema,
- attributes: z.infer<typeof schema>,
- attributesMap: Y.Map<any>
-) => {
- if (!(schema instanceof z.ZodObject)) {
- throw new Error('Schema must be a ZodObject');
+ this.doc.on('update', (update) => {
+ this.updates.push(update);
+ });
}
- applyObjectChanges(schema, attributes, attributesMap);
-
- const parseResult = schema.safeParse(attributesMap.toJSON());
- if (!parseResult.success) {
- throw new Error('Invalid attributes', parseResult.error);
- }
-};
-
-const applyObjectChanges = (
- schema: z.ZodObject<any, any, any, any>,
- attributes: any,
- yMap: Y.Map<any>
-) => {
- for (const [key, value] of Object.entries(attributes)) {
- if (value === null) {
- yMap.set(key, null);
- continue;
+ public updateAttributes(
+ schema: z.ZodSchema,
+ attributes: z.infer<typeof schema>
+ ) {
+ if (!(schema instanceof z.ZodObject)) {
+ throw new Error('Schema must be a ZodObject');
}
- const schemaField = extractType(schema.shape[key], value);
- if (schemaField instanceof z.ZodObject) {
- if (typeof value !== 'object') {
- throw new Error('Value must be an object');
- }
+ const attributesMap = this.doc.getMap('attributes');
+ this.doc.transact(() => {
+ this.applyObjectChanges(schema, attributes, attributesMap);
- let nestedMap = yMap.get(key);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
- yMap.set(key, nestedMap);
+ const parseResult = schema.safeParse(attributesMap.toJSON());
+ if (!parseResult.success) {
+ throw new Error('Invalid attributes', parseResult.error);
}
+ });
+ }
- applyObjectChanges(schemaField, value, nestedMap);
- } else if (schemaField instanceof z.ZodRecord) {
- if (typeof value !== 'object') {
- throw new Error('Value must be an object');
- }
+ public getAttributes(): NodeAttributes {
+ const attributesMap = this.doc.getMap('attributes');
+ const attributes = attributesMap.toJSON() as NodeAttributes;
+ return attributes;
+ }
- let nestedMap = yMap.get(key);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
- yMap.set(key, nestedMap);
- }
+ public applyUpdate(update: Uint8Array | string) {
+ Y.applyUpdate(
+ this.doc,
+ typeof update === 'string' ? toUint8Array(update) : update
+ );
+ }
- applyRecordChanges(schemaField, value, nestedMap);
- } else if (schemaField instanceof z.ZodArray) {
- if (!Array.isArray(value)) {
- throw new Error('Value must be an array');
- }
+ public getState(): Uint8Array {
+ return Y.encodeStateAsUpdate(this.doc);
+ }
- let yArray = yMap.get(key);
- if (!(yArray instanceof Y.Array)) {
- yArray = new Y.Array();
- yMap.set(key, yArray);
- }
+ public getEncodedState(): string {
+ return fromUint8Array(this.getState());
+ }
- applyArrayChanges(schemaField, value, yArray);
- } else if (schemaField instanceof ZodText) {
- if (typeof value !== 'string') {
- throw new Error('Value must be a string');
- }
+ public getUpdates(): Uint8Array[] {
+ return this.updates;
+ }
+
+ public getEncodedUpdates(): string[] {
+ return this.updates.map((update) => fromUint8Array(update));
+ }
- let yText = yMap.get(key);
- if (!(yText instanceof Y.Text)) {
- yText = new Y.Text();
- yMap.set(key, yText);
+ private applyObjectChanges(
+ schema: z.ZodObject<any, any, any, any>,
+ attributes: any,
+ yMap: Y.Map<any>
+ ) {
+ for (const [key, value] of Object.entries(attributes)) {
+ if (value === null) {
+ yMap.set(key, null);
+ continue;
}
- applyTextChanges(value, yText);
- } else {
- const currentValue = yMap.get(key);
+ const schemaField = this.extractType(schema.shape[key], value);
+ if (schemaField instanceof z.ZodObject) {
+ if (typeof value !== 'object') {
+ throw new Error('Value must be an object');
+ }
+
+ let nestedMap = yMap.get(key);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yMap.set(key, nestedMap);
+ }
+
+ this.applyObjectChanges(schemaField, value, nestedMap);
+ } else if (schemaField instanceof z.ZodRecord) {
+ if (typeof value !== 'object') {
+ throw new Error('Value must be an object');
+ }
+
+ let nestedMap = yMap.get(key);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yMap.set(key, nestedMap);
+ }
+
+ this.applyRecordChanges(schemaField, value, nestedMap);
+ } else if (schemaField instanceof z.ZodArray) {
+ if (!Array.isArray(value)) {
+ throw new Error('Value must be an array');
+ }
+
+ let yArray = yMap.get(key);
+ if (!(yArray instanceof Y.Array)) {
+ yArray = new Y.Array();
+ yMap.set(key, yArray);
+ }
+
+ this.applyArrayChanges(schemaField, value, yArray);
+ } else if (schemaField instanceof ZodText) {
+ if (typeof value !== 'string') {
+ throw new Error('Value must be a string');
+ }
+
+ let yText = yMap.get(key);
+ if (!(yText instanceof Y.Text)) {
+ yText = new Y.Text();
+ yMap.set(key, yText);
+ }
+
+ this.applyTextChanges(value, yText);
+ } else {
+ const currentValue = yMap.get(key);
- if (!isEqual(currentValue, value)) {
- yMap.set(key, value);
+ if (!isEqual(currentValue, value)) {
+ yMap.set(key, value);
+ }
}
}
- }
- const deletedKeys = Array.from(yMap.keys()).filter(
- (key) => !attributes.hasOwnProperty(key)
- );
+ const deletedKeys = Array.from(yMap.keys()).filter(
+ (key) => !attributes.hasOwnProperty(key)
+ );
- for (const key of deletedKeys) {
- yMap.delete(key);
- }
-};
-
-const applyArrayChanges = (
- schemaField: z.ZodArray<any>,
- value: Array<any>,
- yArray: Y.Array<any>
-) => {
- const itemSchema = extractType(schemaField.element, value);
- const length = value.length;
-
- for (let i = 0; i < length; i++) {
- const item = value[i];
-
- if (item === null) {
- yArray.delete(i, 1);
- yArray.insert(i, [null]);
- continue;
+ for (const key of deletedKeys) {
+ yMap.delete(key);
}
+ }
- if (itemSchema instanceof z.ZodObject) {
- if (yArray.length <= i) {
- const nestedMap = new Y.Map();
- yArray.insert(i, [nestedMap]);
- }
+ private applyArrayChanges(
+ schemaField: z.ZodArray<any>,
+ value: Array<any>,
+ yArray: Y.Array<any>
+ ) {
+ const itemSchema = this.extractType(schemaField.element, value);
+ const length = value.length;
- let nestedMap = yArray.get(i);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
+ for (let i = 0; i < length; i++) {
+ const item = value[i];
+
+ if (item === null) {
yArray.delete(i, 1);
- yArray.insert(i, [nestedMap]);
+ yArray.insert(i, [null]);
+ continue;
}
- applyObjectChanges(itemSchema, item, nestedMap);
- } else if (itemSchema instanceof z.ZodRecord) {
- if (yArray.length <= i) {
- const nestedMap = new Y.Map();
- yArray.insert(i, [nestedMap]);
- }
+ if (itemSchema instanceof z.ZodObject) {
+ if (yArray.length <= i) {
+ const nestedMap = new Y.Map();
+ yArray.insert(i, [nestedMap]);
+ }
- let nestedMap = yArray.get(i);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
- yArray.delete(i, 1);
- yArray.insert(i, [nestedMap]);
- }
+ let nestedMap = yArray.get(i);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yArray.delete(i, 1);
+ yArray.insert(i, [nestedMap]);
+ }
- applyRecordChanges(itemSchema, item, nestedMap);
- } else if (itemSchema instanceof ZodText) {
- if (yArray.length <= i) {
- const yText = new Y.Text();
- yArray.insert(i, [yText]);
- }
+ this.applyObjectChanges(itemSchema, item, nestedMap);
+ } else if (itemSchema instanceof z.ZodRecord) {
+ if (yArray.length <= i) {
+ const nestedMap = new Y.Map();
+ yArray.insert(i, [nestedMap]);
+ }
- let yText = yArray.get(i);
- if (!(yText instanceof Y.Text)) {
- yText = new Y.Text();
- yArray.delete(i, 1);
- yArray.insert(i, [yText]);
- }
+ let nestedMap = yArray.get(i);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yArray.delete(i, 1);
+ yArray.insert(i, [nestedMap]);
+ }
- applyTextChanges(item, yText);
- } else {
- if (yArray.length <= i) {
- yArray.insert(i, [item]);
+ this.applyRecordChanges(itemSchema, item, nestedMap);
+ } else if (itemSchema instanceof ZodText) {
+ if (yArray.length <= i) {
+ const yText = new Y.Text();
+ yArray.insert(i, [yText]);
+ }
+
+ let yText = yArray.get(i);
+ if (!(yText instanceof Y.Text)) {
+ yText = new Y.Text();
+ yArray.delete(i, 1);
+ yArray.insert(i, [yText]);
+ }
+
+ this.applyTextChanges(item, yText);
} else {
- const currentItem = yArray.get(i);
- if (!isEqual(currentItem, item)) {
- yArray.delete(i);
+ if (yArray.length <= i) {
yArray.insert(i, [item]);
+ } else {
+ const currentItem = yArray.get(i);
+ if (!isEqual(currentItem, item)) {
+ yArray.delete(i);
+ yArray.insert(i, [item]);
+ }
}
}
}
- }
- if (yArray.length > length) {
- yArray.delete(yArray.length - 1, yArray.length - length);
- }
-};
-
-const applyRecordChanges = (
- schemaField: z.ZodRecord<any, any>,
- record: Record<any, any>,
- yMap: Y.Map<any>
-) => {
- const valueSchema = extractType(schemaField.valueSchema, record);
- for (const [key, value] of Object.entries(record)) {
- if (value === null) {
- yMap.set(key, null);
- continue;
+ if (yArray.length > length) {
+ yArray.delete(yArray.length - 1, yArray.length - length);
}
+ }
- if (valueSchema instanceof z.ZodObject) {
- if (typeof value !== 'object') {
- throw new Error('Value must be an object');
+ private applyRecordChanges(
+ schemaField: z.ZodRecord<any, any>,
+ record: Record<any, any>,
+ yMap: Y.Map<any>
+ ) {
+ const valueSchema = this.extractType(schemaField.valueSchema, record);
+ for (const [key, value] of Object.entries(record)) {
+ if (value === null) {
+ yMap.set(key, null);
+ continue;
}
- let nestedMap = yMap.get(key);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
- yMap.set(key, nestedMap);
- }
+ if (valueSchema instanceof z.ZodObject) {
+ if (typeof value !== 'object') {
+ throw new Error('Value must be an object');
+ }
- applyObjectChanges(valueSchema, value, nestedMap);
- } else if (valueSchema instanceof z.ZodRecord) {
- if (typeof value !== 'object') {
- throw new Error('Value must be an object');
- }
+ let nestedMap = yMap.get(key);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yMap.set(key, nestedMap);
+ }
- let nestedMap = yMap.get(key);
- if (!(nestedMap instanceof Y.Map)) {
- nestedMap = new Y.Map();
- yMap.set(key, nestedMap);
- }
+ this.applyObjectChanges(valueSchema, value, nestedMap);
+ } else if (valueSchema instanceof z.ZodRecord) {
+ if (typeof value !== 'object') {
+ throw new Error('Value must be an object');
+ }
- applyRecordChanges(valueSchema, value, nestedMap);
- } else if (valueSchema instanceof z.ZodArray) {
- if (!Array.isArray(value)) {
- throw new Error('Value must be an array');
- }
+ let nestedMap = yMap.get(key);
+ if (!(nestedMap instanceof Y.Map)) {
+ nestedMap = new Y.Map();
+ yMap.set(key, nestedMap);
+ }
- let yArray = yMap.get(key);
- if (!(yArray instanceof Y.Array)) {
- yArray = new Y.Array();
- yMap.set(key, yArray);
- }
+ this.applyRecordChanges(valueSchema, value, nestedMap);
+ } else if (valueSchema instanceof z.ZodArray) {
+ if (!Array.isArray(value)) {
+ throw new Error('Value must be an array');
+ }
- applyArrayChanges(valueSchema, value, yArray);
- } else if (valueSchema instanceof ZodText) {
- if (typeof value !== 'string') {
- throw new Error('Value must be a string');
- }
+ let yArray = yMap.get(key);
+ if (!(yArray instanceof Y.Array)) {
+ yArray = new Y.Array();
+ yMap.set(key, yArray);
+ }
- let yText = yMap.get(key);
- if (!(yText instanceof Y.Text)) {
- yText = new Y.Text();
- yMap.set(key, yText);
- }
+ this.applyArrayChanges(valueSchema, value, yArray);
+ } else if (valueSchema instanceof ZodText) {
+ if (typeof value !== 'string') {
+ throw new Error('Value must be a string');
+ }
- applyTextChanges(value, yText);
- } else {
- const currentValue = yMap.get(key);
- if (!isEqual(currentValue, value)) {
- yMap.set(key, value);
+ let yText = yMap.get(key);
+ if (!(yText instanceof Y.Text)) {
+ yText = new Y.Text();
+ yMap.set(key, yText);
+ }
+
+ this.applyTextChanges(value, yText);
+ } else {
+ const currentValue = yMap.get(key);
+ if (!isEqual(currentValue, value)) {
+ yMap.set(key, value);
+ }
}
}
- }
- const deletedKeys = Array.from(yMap.keys()).filter(
- (key) => !record.hasOwnProperty(key)
- );
+ const deletedKeys = Array.from(yMap.keys()).filter(
+ (key) => !record.hasOwnProperty(key)
+ );
- for (const key of deletedKeys) {
- yMap.delete(key);
+ for (const key of deletedKeys) {
+ yMap.delete(key);
+ }
}
-};
-
-const applyTextChanges = (value: string, yText: Y.Text) => {
- const currentText = yText.toString();
- const newText = value ? value.toString() : '';
-
- if (!isEqual(currentText, newText)) {
- const diffs = diffChars(currentText, newText);
- let index = 0;
-
- for (const diff of diffs) {
- if (diff.added) {
- yText.insert(index, diff.value);
- index += diff.value.length;
- } else if (diff.removed) {
- yText.delete(index, diff.value.length);
- index -= diff.value.length;
- } else {
- index += diff.value.length;
+
+ private applyTextChanges(value: string, yText: Y.Text) {
+ const currentText = yText.toString();
+ const newText = value ? value.toString() : '';
+
+ if (!isEqual(currentText, newText)) {
+ const diffs = diffChars(currentText, newText);
+ let index = 0;
+
+ for (const diff of diffs) {
+ if (diff.added) {
+ yText.insert(index, diff.value);
+ index += diff.value.length;
+ } else if (diff.removed) {
+ yText.delete(index, diff.value.length);
+ index -= diff.value.length;
+ } else {
+ index += diff.value.length;
+ }
}
}
}
-};
-
-const extractType = (
- schema: z.ZodType<any, any, any>,
- value: any
-): z.ZodType<any, any, any> => {
- if (schema instanceof z.ZodOptional) {
- return extractType(schema.unwrap(), value);
- }
- if (schema instanceof z.ZodNullable) {
- return extractType(schema.unwrap(), value);
- }
+ private extractType(
+ schema: z.ZodType<any, any, any>,
+ value: any
+ ): z.ZodType<any, any, any> {
+ if (schema instanceof z.ZodOptional) {
+ return this.extractType(schema.unwrap(), value);
+ }
- if (schema instanceof z.ZodUnion) {
- for (const option of schema.options) {
- if (option.safeParse(value).success) {
- return extractType(option, value);
+ if (schema instanceof z.ZodNullable) {
+ return this.extractType(schema.unwrap(), value);
+ }
+
+ if (schema instanceof z.ZodUnion) {
+ for (const option of schema.options) {
+ if (option.safeParse(value).success) {
+ return this.extractType(option, value);
+ }
}
}
- }
- return schema;
-};
+ return schema;
+ }
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4bcc66f7..df8354c1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -219,9 +219,6 @@ importers:
is-hotkey:
specifier: ^0.2.0
version: 0.2.0
- js-base64:
- specifier: ^3.7.7
- version: 3.7.7
js-sha256:
specifier: ^0.11.0
version: 0.11.0
@@ -418,9 +415,6 @@ importers:
express:
specifier: ^4.21.1
version: 4.21.1
- js-base64:
- specifier: ^3.7.7
- version: 3.7.7
js-sha256:
specifier: ^0.11.0
version: 0.11.0
@@ -451,9 +445,6 @@ importers:
ws:
specifier: ^8.18.0
version: 8.18.0
- yjs:
- specifier: ^13.6.20
- version: 13.6.20
devDependencies:
'@types/bcrypt':
specifier: ^5.0.2
@@ -516,6 +507,9 @@ importers:
diff:
specifier: ^7.0.0
version: 7.0.0
+ js-base64:
+ specifier: ^3.7.7
+ version: 3.7.7
yjs:
specifier: ^13.6.20
version: 13.6.20
@@ -6598,7 +6592,7 @@ snapshots:
'@babel/traverse': 7.25.9
'@babel/types': 7.26.0
convert-source-map: 2.0.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
gensync: 1.0.0-beta.2
json5: 2.2.3
semver: 6.3.1
@@ -6686,7 +6680,7 @@ snapshots:
'@babel/parser': 7.26.2
'@babel/template': 7.25.9
'@babel/types': 7.26.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
globals: 11.12.0
transitivePeerDependencies:
- supports-color
@@ -6751,7 +6745,7 @@ snapshots:
'@electron/[email protected]':
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
env-paths: 2.2.1
fs-extra: 8.1.0
got: 11.8.6
@@ -6765,7 +6759,7 @@ snapshots:
'@electron/[email protected]':
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
fs-extra: 9.1.0
promise-retry: 2.0.1
transitivePeerDependencies:
@@ -6774,7 +6768,7 @@ snapshots:
'@electron/[email protected]':
dependencies:
compare-version: 0.1.2
- debug: 4.3.7([email protected])
+ debug: 4.3.7
fs-extra: 10.1.0
isbinaryfile: 4.0.10
minimist: 1.2.8
@@ -6786,7 +6780,7 @@ snapshots:
dependencies:
'@electron/asar': 3.2.16
'@malept/cross-spawn-promise': 1.1.1
- debug: 4.3.7([email protected])
+ debug: 4.3.7
dir-compare: 3.3.0
fs-extra: 9.1.0
minimatch: 3.1.2
@@ -6950,7 +6944,7 @@ snapshots:
'@eslint/[email protected]':
dependencies:
ajv: 6.12.6
- debug: 4.3.7([email protected])
+ debug: 4.3.7
espree: 9.6.1
globals: 13.24.0
ignore: 5.3.2
@@ -6987,7 +6981,7 @@ snapshots:
'@humanwhocodes/[email protected]':
dependencies:
'@humanwhocodes/object-schema': 2.0.3
- debug: 4.3.7([email protected])
+ debug: 4.3.7
minimatch: 3.1.2
transitivePeerDependencies:
- supports-color
@@ -7115,7 +7109,7 @@ snapshots:
'@malept/[email protected]':
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
fs-extra: 9.1.0
lodash: 4.17.21
tmp-promise: 3.0.3
@@ -8482,7 +8476,7 @@ snapshots:
'@typescript-eslint/scope-manager': 5.62.0
'@typescript-eslint/type-utils': 5.62.0([email protected])([email protected])
'@typescript-eslint/utils': 5.62.0([email protected])([email protected])
- debug: 4.3.7([email protected])
+ debug: 4.3.7
eslint: 8.57.1
graphemer: 1.4.0
ignore: 5.3.2
@@ -8517,7 +8511,7 @@ snapshots:
'@typescript-eslint/scope-manager': 5.62.0
'@typescript-eslint/types': 5.62.0
'@typescript-eslint/typescript-estree': 5.62.0([email protected])
- debug: 4.3.7([email protected])
+ debug: 4.3.7
eslint: 8.57.1
optionalDependencies:
typescript: 5.6.3
@@ -8530,7 +8524,7 @@ snapshots:
'@typescript-eslint/types': 7.18.0
'@typescript-eslint/typescript-estree': 7.18.0([email protected])
'@typescript-eslint/visitor-keys': 7.18.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
eslint: 8.57.1
optionalDependencies:
typescript: 5.6.3
@@ -8551,7 +8545,7 @@ snapshots:
dependencies:
'@typescript-eslint/typescript-estree': 5.62.0([email protected])
'@typescript-eslint/utils': 5.62.0([email protected])([email protected])
- debug: 4.3.7([email protected])
+ debug: 4.3.7
eslint: 8.57.1
tsutils: 3.21.0([email protected])
optionalDependencies:
@@ -8563,7 +8557,7 @@ snapshots:
dependencies:
'@typescript-eslint/typescript-estree': 7.18.0([email protected])
'@typescript-eslint/utils': 7.18.0([email protected])([email protected])
- debug: 4.3.7([email protected])
+ debug: 4.3.7
eslint: 8.57.1
ts-api-utils: 1.4.0([email protected])
optionalDependencies:
@@ -8579,7 +8573,7 @@ snapshots:
dependencies:
'@typescript-eslint/types': 5.62.0
'@typescript-eslint/visitor-keys': 5.62.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
globby: 11.1.0
is-glob: 4.0.3
semver: 7.6.3
@@ -8593,7 +8587,7 @@ snapshots:
dependencies:
'@typescript-eslint/types': 7.18.0
'@typescript-eslint/visitor-keys': 7.18.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
globby: 11.1.0
is-glob: 4.0.3
minimatch: 9.0.5
@@ -8752,7 +8746,7 @@ snapshots:
builder-util: 24.13.1
builder-util-runtime: 9.2.4
chromium-pickle-js: 0.2.0
- debug: 4.3.7([email protected])
+ debug: 4.3.7
dmg-builder: 24.13.3([email protected])
ejs: 3.1.10
electron-builder-squirrel-windows: 24.13.3([email protected])
@@ -9026,14 +9020,14 @@ snapshots:
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
sax: 1.4.1
transitivePeerDependencies:
- supports-color
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
sax: 1.4.1
transitivePeerDependencies:
- supports-color
@@ -9047,7 +9041,7 @@ snapshots:
builder-util-runtime: 9.2.4
chalk: 4.1.2
cross-spawn: 7.0.3
- debug: 4.3.7([email protected])
+ debug: 4.3.7
fs-extra: 10.1.0
http-proxy-agent: 5.0.0
https-proxy-agent: 5.0.1
@@ -9327,6 +9321,10 @@ snapshots:
dependencies:
ms: 2.1.3
+ dependencies:
+ ms: 2.1.3
+
dependencies:
ms: 2.1.3
@@ -9819,7 +9817,7 @@ snapshots:
ajv: 6.12.6
chalk: 4.1.2
cross-spawn: 7.0.3
- debug: 4.3.7([email protected])
+ debug: 4.3.7
doctrine: 3.0.0
escape-string-regexp: 4.0.0
eslint-scope: 7.2.2
@@ -9927,7 +9925,7 @@ snapshots:
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
get-stream: 5.2.0
yauzl: 2.10.0
optionalDependencies:
@@ -10251,7 +10249,7 @@ snapshots:
dependencies:
'@tootallnate/once': 2.0.0
agent-base: 6.0.2
- debug: 4.3.7([email protected])
+ debug: 4.3.7
transitivePeerDependencies:
- supports-color
@@ -11817,7 +11815,7 @@ snapshots:
dependencies:
- debug: 4.3.7([email protected])
+ debug: 4.3.7
transitivePeerDependencies:
- supports-color