585 lines · 23.6 KB
import { DurableObject } from "cloudflare:workers";
import * as decoding from "lib0/decoding";
import * as encoding from "lib0/encoding";
import * as syncProtocol from "y-protocols/sync";
import * as Y from "yjs";
import { HttpError } from "../errors";
import { canWrite, resolveEditorToken } from "./session";
import {
  applyCommitted,
  conflicts,
  filesMap,
  getMeta,
  MAX_ROOM_FILES,
  MAX_TEXT_BYTES,
  pendingChanges,
  readAtCommit,
  rebaseRoom,
  resolveConflict,
  setBase,
  setMeta,
  textOf,
} from "./room-files";
import { agentClientId, awarenessFrame, base64, decodeAwareness, encodeAwareness, removalFrame, unbase64 } from "./room-awareness";
import {
  CLOSE_DOWNGRADED,
  CLOSE_REAUTH,
  EDITOR_WS_PROTOCOL,
  CLOSE_REVOKED,
  MESSAGE_AWARENESS,
  MESSAGE_SYNC,
} from "~/lib/editor-protocol";
import type { ClientControl, RoomControl } from "~/lib/editor-protocol";

const MAX_SOCKETS = 50;
const MAX_FRAME_BYTES = 1024 * 1024;
const MAX_CACHED_AWARENESS = 1200;
const RECHECK_MS = 60 * 1000;
const AGENT_PRESENCE_MS = 30 * 1000;
const STATUS_DEBOUNCE_MS = 300;
const COMPACT_AFTER = 300;