JSPM

@yjsync/core

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 16
  • Score
    100M100P100Q64658F
  • License MIT

Runtime-agnostic Yjs room session: y-websocket-compatible sync, awareness, persistence hooks, and export helpers.

Package Exports

  • @yjsync/core

Readme

@yjsync/core

Runtime-agnostic Yjs room session: y-websocket-compatible binary protocol (MESSAGE_SYNC, MESSAGE_AWARENESS), optional persistence, compaction, and JSON export helpers.

Use this when you are building your own transport (custom Worker glue, tests) and want shared room logic without importing Cloudflare SDKs.

Install

bun add @yjsync/core yjs

Minimal in-memory room

import { MemoryPersistenceAdapter, RoomSession } from '@yjsync/core'

const room = new RoomSession('my-room-id', {
  persistence: new MemoryPersistenceAdapter(),
  compactAfterTailSize: 256,
})

await room.whenReady

await room.addConnection(myConnHandle, {
  send: (data) => ws.send(data),
  readyState: ws.readyState,
  close: () => ws.close(),
})

room.handleMessage(myConnHandle, incomingUint8Array)

RoomSession extends Y.Doc. Use room.awareness for collaboration cursors when your client supports it.

JSON export

import { exportJsonToApi } from '@yjsync/core'
import { serializeTiptap } from '@yjsync/serializers'

await exportJsonToApi({
  roomId: 'demo/doc-1',
  doc: roomSession,
  url: 'https://api.example.com/snapshots',
  revision: roomSession.revision,
  serialize: serializeTiptap,
})

JSON serializers (TipTap, root map, ProseMirror fragment, custom maps) live in @yjsync/serializers. The default in exportJsonToApi (when no serialize is passed) is doc.getMap('root').toJSON().

Optional auth hook

const room = new RoomSession('private-room', {
  persistence: adapter,
  auth: async ({ roomId, conn }) => {
    // return false to reject addConnection
    return true
  },
})

Example apps

Develop

bun run build
bun run typecheck