@sobree/collab-providers 0.1.9 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/websocket.ts","../src/indexeddb.ts","../src/webrtc.ts","../src/loopback.ts"],"sourcesContent":["import type { CollabHandle, IdentityOptions } from \"./types\";\nimport type * as Y from \"yjs\";\n\nexport interface WebsocketProviderOptions extends IdentityOptions {\n /** WebSocket server URL (e.g. `wss://collab.example.com`). The room\n * name is appended as a path. */\n url: string;\n /** Room name. Each room is one Y.Doc shared across peers. */\n room: string;\n /** Auth params appended to the WebSocket URL as query string. */\n params?: Record<string, string>;\n /** Connect immediately (default true). Set to false to call\n * `provider.connect()` manually. */\n connect?: boolean;\n}\n\n/**\n * Attach a `y-websocket` provider to a Y.Doc.\n *\n * Requires `y-websocket` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-websocket\n * ```\n *\n * Returns a `CollabHandle` with the connected provider, awareness\n * (cursor / presence channel), and a `synced` promise that resolves\n * when the initial sync completes.\n *\n * Usage:\n *\n * ```ts\n * import * as Y from \"yjs\";\n * import { createSobree } from \"@sobree/core\";\n * import { attachWebsocketProvider } from \"@sobree/collab-providers\";\n *\n * const ydoc = new Y.Doc();\n * const handle = await attachWebsocketProvider(ydoc, {\n * url: \"wss://collab.example.com\",\n * room: \"doc-q2-brief\",\n * name: \"Alice\",\n * color: \"#f59e0b\",\n * });\n * const editor = createSobree(\"#editor\", { ydoc });\n *\n * // Later:\n * editor.destroy();\n * handle.destroy();\n * ydoc.destroy();\n * ```\n */\nexport async function attachWebsocketProvider(\n ydoc: Y.Doc,\n opts: WebsocketProviderOptions,\n): Promise<CollabHandle> {\n const yws = await loadYWebsocket();\n const params = opts.params ?? {};\n const provider = new yws.WebsocketProvider(opts.url, opts.room, ydoc, {\n params,\n connect: opts.connect ?? true,\n });\n\n if (opts.name || opts.color || opts.userId) {\n provider.awareness.setLocalStateField(\"user\", {\n id: opts.userId ?? randomId(),\n name: opts.name ?? \"Anonymous\",\n color: opts.color ?? \"#888\",\n });\n }\n\n const synced = new Promise<void>((resolve) => {\n if (provider.synced) {\n resolve();\n return;\n }\n const handler = (isSynced: boolean) => {\n if (isSynced) {\n provider.off(\"sync\", handler);\n resolve();\n }\n };\n provider.on(\"sync\", handler);\n });\n\n return {\n provider,\n awareness: provider.awareness,\n synced,\n destroy(): void {\n provider.disconnect();\n provider.destroy();\n },\n };\n}\n\nasync function loadYWebsocket(): Promise<typeof import(\"y-websocket\")> {\n try {\n return await import(\"y-websocket\");\n } catch (err) {\n throw new Error(\n \"y-websocket is not installed. Run `pnpm add y-websocket` (or `npm install y-websocket`) \" +\n \"and import this module again. See https://github.com/yjs/y-websocket.\",\n { cause: err },\n );\n }\n}\n\nfunction randomId(): string {\n // Sufficient for awareness peer ids; not crypto.\n return Math.random().toString(36).slice(2, 10);\n}\n","import type { CollabHandle } from \"./types\";\nimport type * as Y from \"yjs\";\n\nexport interface IndexedDBProviderOptions {\n /** Database name. Each name is a separate IndexedDB store. Use one\n * per document if you want isolated persistence. */\n dbName: string;\n}\n\n/**\n * Attach a `y-indexeddb` provider for local persistence. The Y.Doc's\n * state is stored in the browser's IndexedDB; reloading the page\n * restores the document.\n *\n * Requires `y-indexeddb` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-indexeddb\n * ```\n *\n * No awareness — local persistence only. The `synced` promise resolves\n * when the existing snapshot has been loaded into the Y.Doc.\n *\n * Usage:\n *\n * ```ts\n * import * as Y from \"yjs\";\n * import { createSobree } from \"@sobree/core\";\n * import { attachIndexedDBProvider } from \"@sobree/collab-providers\";\n *\n * const ydoc = new Y.Doc();\n * const handle = await attachIndexedDBProvider(ydoc, {\n * dbName: \"doc-q2-brief\",\n * });\n * await handle.synced; // optional — wait for initial load\n * const editor = createSobree(\"#editor\", { ydoc });\n * ```\n */\nexport async function attachIndexedDBProvider(\n ydoc: Y.Doc,\n opts: IndexedDBProviderOptions,\n): Promise<CollabHandle> {\n const yidb = await loadYIndexedDB();\n const provider = new yidb.IndexeddbPersistence(opts.dbName, ydoc);\n\n const synced = new Promise<void>((resolve) => {\n provider.once(\"synced\", () => resolve());\n });\n\n return {\n provider,\n awareness: null,\n synced,\n destroy(): void {\n provider.destroy();\n },\n };\n}\n\nasync function loadYIndexedDB(): Promise<typeof import(\"y-indexeddb\")> {\n try {\n return await import(\"y-indexeddb\");\n } catch (err) {\n throw new Error(\n \"y-indexeddb is not installed. Run `pnpm add y-indexeddb` (or `npm install y-indexeddb`) \" +\n \"and import this module again. See https://github.com/yjs/y-indexeddb.\",\n { cause: err },\n );\n }\n}\n","import type { CollabHandle, IdentityOptions } from \"./types\";\nimport type * as Y from \"yjs\";\n\nexport interface WebRTCProviderOptions extends IdentityOptions {\n /** Room name. Each room is one Y.Doc shared across peers. */\n room: string;\n /** Pre-shared room password. Optional but recommended for any\n * non-public collaboration. */\n password?: string;\n /** Custom signaling servers. Defaults to the public servers shipped\n * with `y-webrtc`; for production set up your own. */\n signaling?: string[];\n}\n\n/**\n * Attach a `y-webrtc` provider — peer-to-peer collaboration, with a\n * tiny signaling server (or shared public ones) used only for initial\n * peer discovery.\n *\n * Requires `y-webrtc` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-webrtc\n * ```\n *\n * Best for small (≤4 peer) ad-hoc collaboration where you don't want\n * to host a relay server. For more peers / persistence /\n * authoritative state, use `attachWebsocketProvider` against\n * `@sobree/collab-server` (Phase 3).\n */\nexport async function attachWebRTCProvider(\n ydoc: Y.Doc,\n opts: WebRTCProviderOptions,\n): Promise<CollabHandle> {\n const ywebrtc = await loadYWebRTC();\n const providerOpts: Record<string, unknown> = {};\n if (opts.password) providerOpts.password = opts.password;\n if (opts.signaling) providerOpts.signaling = opts.signaling;\n\n const provider = new ywebrtc.WebrtcProvider(opts.room, ydoc, providerOpts);\n\n if (opts.name || opts.color || opts.userId) {\n provider.awareness.setLocalStateField(\"user\", {\n id: opts.userId ?? randomId(),\n name: opts.name ?? \"Anonymous\",\n color: opts.color ?? \"#888\",\n });\n }\n\n // y-webrtc has no canonical 'synced' event (peer-to-peer; no\n // single source of truth). Resolve once the first peer connects,\n // or after a short timeout if we're alone in the room.\n const synced = new Promise<void>((resolve) => {\n let resolved = false;\n const finish = () => {\n if (!resolved) {\n resolved = true;\n resolve();\n }\n };\n provider.on(\"peers\", ({ added }: { added: unknown[] }) => {\n if (added.length > 0) finish();\n });\n setTimeout(finish, 1000);\n });\n\n return {\n provider,\n awareness: provider.awareness,\n synced,\n destroy(): void {\n provider.disconnect();\n provider.destroy();\n },\n };\n}\n\nasync function loadYWebRTC(): Promise<typeof import(\"y-webrtc\")> {\n try {\n return await import(\"y-webrtc\");\n } catch (err) {\n throw new Error(\n \"y-webrtc is not installed. Run `pnpm add y-webrtc` (or `npm install y-webrtc`) \" +\n \"and import this module again. See https://github.com/yjs/y-webrtc.\",\n { cause: err },\n );\n }\n}\n\nfunction randomId(): string {\n return Math.random().toString(36).slice(2, 10);\n}\n","import * as Y from \"yjs\";\n\n/**\n * Two Y.Docs wired together in-memory — useful for tests and demos.\n *\n * Constructs a pair: `{ a: Y.Doc, b: Y.Doc }`. Updates applied to `a`\n * automatically replicate to `b` and vice versa. No network involved.\n *\n * The returned `destroy()` removes both observers — the Y.Docs survive\n * for the caller to inspect / use. `.destroy()` on the docs themselves\n * still works the usual way.\n */\nexport function loopback(): {\n a: Y.Doc;\n b: Y.Doc;\n destroy(): void;\n} {\n const a = new Y.Doc();\n const b = new Y.Doc();\n\n const ab = (update: Uint8Array, origin: unknown) => {\n if (origin === \"remote\") return;\n Y.applyUpdate(b, update, \"remote\");\n };\n const ba = (update: Uint8Array, origin: unknown) => {\n if (origin === \"remote\") return;\n Y.applyUpdate(a, update, \"remote\");\n };\n a.on(\"update\", ab);\n b.on(\"update\", ba);\n\n return {\n a,\n b,\n destroy(): void {\n a.off(\"update\", ab);\n b.off(\"update\", ba);\n },\n };\n}\n"],"names":["attachWebsocketProvider","ydoc","opts","yws","loadYWebsocket","params","provider","randomId","synced","resolve","handler","isSynced","err","attachIndexedDBProvider","yidb","loadYIndexedDB","attachWebRTCProvider","ywebrtc","loadYWebRTC","providerOpts","resolved","finish","added","loopback","a","Y","b","ab","update","origin","ba"],"mappings":";AAmDA,eAAsBA,EACpBC,GACAC,GACuB;AACvB,QAAMC,IAAM,MAAMC,EAAA,GACZC,IAASH,EAAK,UAAU,CAAA,GACxBI,IAAW,IAAIH,EAAI,kBAAkBD,EAAK,KAAKA,EAAK,MAAMD,GAAM;AAAA,IACpE,QAAAI;AAAA,IACA,SAASH,EAAK,WAAW;AAAA,EAAA,CAC1B;AAED,GAAIA,EAAK,QAAQA,EAAK,SAASA,EAAK,WAClCI,EAAS,UAAU,mBAAmB,QAAQ;AAAA,IAC5C,IAAIJ,EAAK,UAAUK,EAAA;AAAA,IACnB,MAAML,EAAK,QAAQ;AAAA,IACnB,OAAOA,EAAK,SAAS;AAAA,EAAA,CACtB;AAGH,QAAMM,IAAS,IAAI,QAAc,CAACC,MAAY;AAC5C,QAAIH,EAAS,QAAQ;AACnB,MAAAG,EAAA;AACA;AAAA,IACF;AACA,UAAMC,IAAU,CAACC,MAAsB;AACrC,MAAIA,MACFL,EAAS,IAAI,QAAQI,CAAO,GAC5BD,EAAA;AAAA,IAEJ;AACA,IAAAH,EAAS,GAAG,QAAQI,CAAO;AAAA,EAC7B,CAAC;AAED,SAAO;AAAA,IACL,UAAAJ;AAAA,IACA,WAAWA,EAAS;AAAA,IACpB,QAAAE;AAAA,IACA,UAAgB;AACd,MAAAF,EAAS,WAAA,GACTA,EAAS,QAAA;AAAA,IACX;AAAA,EAAA;AAEJ;AAEA,eAAeF,IAAwD;AACrE,MAAI;AACF,WAAO,MAAM,OAAO,aAAa;AAAA,EACnC,SAASQ,GAAK;AACZ,UAAM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ket.ts","../src/indexeddb.ts","../src/webrtc.ts","../src/loopback.ts"],"sourcesContent":["import type * as Y from \"yjs\";\nimport type { CollabHandle, IdentityOptions } from \"./types\";\n\nexport interface WebsocketProviderOptions extends IdentityOptions {\n /** WebSocket server URL (e.g. `wss://collab.example.com`). The room\n * name is appended as a path. */\n url: string;\n /** Room name. Each room is one Y.Doc shared across peers. */\n room: string;\n /** Auth params appended to the WebSocket URL as query string. */\n params?: Record<string, string>;\n /** Connect immediately (default true). Set to false to call\n * `provider.connect()` manually. */\n connect?: boolean;\n}\n\n/**\n * Attach a `y-websocket` provider to a Y.Doc.\n *\n * Requires `y-websocket` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-websocket\n * ```\n *\n * Returns a `CollabHandle` with the connected provider, awareness\n * (cursor / presence channel), and a `synced` promise that resolves\n * when the initial sync completes.\n *\n * Usage:\n *\n * ```ts\n * import * as Y from \"yjs\";\n * import { createSobree } from \"@sobree/core\";\n * import { attachWebsocketProvider } from \"@sobree/collab-providers\";\n *\n * const ydoc = new Y.Doc();\n * const handle = await attachWebsocketProvider(ydoc, {\n * url: \"wss://collab.example.com\",\n * room: \"doc-q2-brief\",\n * name: \"Alice\",\n * color: \"#f59e0b\",\n * });\n * const editor = createSobree(\"#editor\", { ydoc });\n *\n * // Later:\n * editor.destroy();\n * handle.destroy();\n * ydoc.destroy();\n * ```\n */\nexport async function attachWebsocketProvider(\n ydoc: Y.Doc,\n opts: WebsocketProviderOptions,\n): Promise<CollabHandle> {\n const yws = await loadYWebsocket();\n const params = opts.params ?? {};\n const provider = new yws.WebsocketProvider(opts.url, opts.room, ydoc, {\n params,\n connect: opts.connect ?? true,\n });\n\n if (opts.name || opts.color || opts.userId) {\n provider.awareness.setLocalStateField(\"user\", {\n id: opts.userId ?? randomId(),\n name: opts.name ?? \"Anonymous\",\n color: opts.color ?? \"#888\",\n });\n }\n\n const synced = new Promise<void>((resolve) => {\n if (provider.synced) {\n resolve();\n return;\n }\n const handler = (isSynced: boolean) => {\n if (isSynced) {\n provider.off(\"sync\", handler);\n resolve();\n }\n };\n provider.on(\"sync\", handler);\n });\n\n return {\n provider,\n awareness: provider.awareness,\n synced,\n destroy(): void {\n provider.disconnect();\n provider.destroy();\n },\n };\n}\n\nasync function loadYWebsocket(): Promise<typeof import(\"y-websocket\")> {\n try {\n return await import(\"y-websocket\");\n } catch (err) {\n throw new Error(\n \"y-websocket is not installed. Run `pnpm add y-websocket` (or `npm install y-websocket`) \" +\n \"and import this module again. See https://github.com/yjs/y-websocket.\",\n { cause: err },\n );\n }\n}\n\nfunction randomId(): string {\n // Sufficient for awareness peer ids; not crypto.\n return Math.random().toString(36).slice(2, 10);\n}\n","import type * as Y from \"yjs\";\nimport type { CollabHandle } from \"./types\";\n\nexport interface IndexedDBProviderOptions {\n /** Database name. Each name is a separate IndexedDB store. Use one\n * per document if you want isolated persistence. */\n dbName: string;\n}\n\n/**\n * Attach a `y-indexeddb` provider for local persistence. The Y.Doc's\n * state is stored in the browser's IndexedDB; reloading the page\n * restores the document.\n *\n * Requires `y-indexeddb` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-indexeddb\n * ```\n *\n * No awareness — local persistence only. The `synced` promise resolves\n * when the existing snapshot has been loaded into the Y.Doc.\n *\n * Usage:\n *\n * ```ts\n * import * as Y from \"yjs\";\n * import { createSobree } from \"@sobree/core\";\n * import { attachIndexedDBProvider } from \"@sobree/collab-providers\";\n *\n * const ydoc = new Y.Doc();\n * const handle = await attachIndexedDBProvider(ydoc, {\n * dbName: \"doc-q2-brief\",\n * });\n * await handle.synced; // optional — wait for initial load\n * const editor = createSobree(\"#editor\", { ydoc });\n * ```\n */\nexport async function attachIndexedDBProvider(\n ydoc: Y.Doc,\n opts: IndexedDBProviderOptions,\n): Promise<CollabHandle> {\n const yidb = await loadYIndexedDB();\n const provider = new yidb.IndexeddbPersistence(opts.dbName, ydoc);\n\n const synced = new Promise<void>((resolve) => {\n provider.once(\"synced\", () => resolve());\n });\n\n return {\n provider,\n awareness: null,\n synced,\n destroy(): void {\n provider.destroy();\n },\n };\n}\n\nasync function loadYIndexedDB(): Promise<typeof import(\"y-indexeddb\")> {\n try {\n return await import(\"y-indexeddb\");\n } catch (err) {\n throw new Error(\n \"y-indexeddb is not installed. Run `pnpm add y-indexeddb` (or `npm install y-indexeddb`) \" +\n \"and import this module again. See https://github.com/yjs/y-indexeddb.\",\n { cause: err },\n );\n }\n}\n","import type * as Y from \"yjs\";\nimport type { CollabHandle, IdentityOptions } from \"./types\";\n\nexport interface WebRTCProviderOptions extends IdentityOptions {\n /** Room name. Each room is one Y.Doc shared across peers. */\n room: string;\n /** Pre-shared room password. Optional but recommended for any\n * non-public collaboration. */\n password?: string;\n /** Custom signaling servers. Defaults to the public servers shipped\n * with `y-webrtc`; for production set up your own. */\n signaling?: string[];\n}\n\n/**\n * Attach a `y-webrtc` provider — peer-to-peer collaboration, with a\n * tiny signaling server (or shared public ones) used only for initial\n * peer discovery.\n *\n * Requires `y-webrtc` as a peer dep — install it explicitly:\n *\n * ```sh\n * pnpm add y-webrtc\n * ```\n *\n * Best for small (≤4 peer) ad-hoc collaboration where you don't want\n * to host a relay server. For more peers / persistence /\n * authoritative state, use `attachWebsocketProvider` against\n * `@sobree/collab-server` (Phase 3).\n */\nexport async function attachWebRTCProvider(\n ydoc: Y.Doc,\n opts: WebRTCProviderOptions,\n): Promise<CollabHandle> {\n const ywebrtc = await loadYWebRTC();\n const providerOpts: Record<string, unknown> = {};\n if (opts.password) providerOpts.password = opts.password;\n if (opts.signaling) providerOpts.signaling = opts.signaling;\n\n const provider = new ywebrtc.WebrtcProvider(opts.room, ydoc, providerOpts);\n\n if (opts.name || opts.color || opts.userId) {\n provider.awareness.setLocalStateField(\"user\", {\n id: opts.userId ?? randomId(),\n name: opts.name ?? \"Anonymous\",\n color: opts.color ?? \"#888\",\n });\n }\n\n // y-webrtc has no canonical 'synced' event (peer-to-peer; no\n // single source of truth). Resolve once the first peer connects,\n // or after a short timeout if we're alone in the room.\n const synced = new Promise<void>((resolve) => {\n let resolved = false;\n const finish = () => {\n if (!resolved) {\n resolved = true;\n resolve();\n }\n };\n provider.on(\"peers\", ({ added }: { added: unknown[] }) => {\n if (added.length > 0) finish();\n });\n setTimeout(finish, 1000);\n });\n\n return {\n provider,\n awareness: provider.awareness,\n synced,\n destroy(): void {\n provider.disconnect();\n provider.destroy();\n },\n };\n}\n\nasync function loadYWebRTC(): Promise<typeof import(\"y-webrtc\")> {\n try {\n return await import(\"y-webrtc\");\n } catch (err) {\n throw new Error(\n \"y-webrtc is not installed. Run `pnpm add y-webrtc` (or `npm install y-webrtc`) \" +\n \"and import this module again. See https://github.com/yjs/y-webrtc.\",\n { cause: err },\n );\n }\n}\n\nfunction randomId(): string {\n return Math.random().toString(36).slice(2, 10);\n}\n","import * as Y from \"yjs\";\n\n/**\n * Two Y.Docs wired together in-memory — useful for tests and demos.\n *\n * Constructs a pair: `{ a: Y.Doc, b: Y.Doc }`. Updates applied to `a`\n * automatically replicate to `b` and vice versa. No network involved.\n *\n * The returned `destroy()` removes both observers — the Y.Docs survive\n * for the caller to inspect / use. `.destroy()` on the docs themselves\n * still works the usual way.\n */\nexport function loopback(): {\n a: Y.Doc;\n b: Y.Doc;\n destroy(): void;\n} {\n const a = new Y.Doc();\n const b = new Y.Doc();\n\n const ab = (update: Uint8Array, origin: unknown) => {\n if (origin === \"remote\") return;\n Y.applyUpdate(b, update, \"remote\");\n };\n const ba = (update: Uint8Array, origin: unknown) => {\n if (origin === \"remote\") return;\n Y.applyUpdate(a, update, \"remote\");\n };\n a.on(\"update\", ab);\n b.on(\"update\", ba);\n\n return {\n a,\n b,\n destroy(): void {\n a.off(\"update\", ab);\n b.off(\"update\", ba);\n },\n };\n}\n"],"names":["attachWebsocketProvider","ydoc","opts","yws","loadYWebsocket","params","provider","randomId","synced","resolve","handler","isSynced","err","attachIndexedDBProvider","yidb","loadYIndexedDB","attachWebRTCProvider","ywebrtc","loadYWebRTC","providerOpts","res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;AAED,SAAO;AAAA,IACL,UAAAf;AAAA,IACA,WAAWA,EAAS;AAAA,IACpB,QAAAE;AAAA,IACA,UAAgB;AACd,MAAAF,EAAS,WAAA,GACTA,EAAS,QAAA;AAAA,IACX;AAAA,EAAA;AAEJ;AAEA,eAAeY,IAAkD;AAC/D,MAAI;AACF,WAAO,MAAM,OAAO,UAAU;AAAA,EAChC,SAASN,GAAK;AACZ,UAAM,IAAI;AAAA,MACR;AAAA,MAEA,EAAE,OAAOA,EAAA;AAAA,IAAI;AAAA,EAEjB;AACF;AAEA,SAASL,IAAmB;AAC1B,SAAO,KAAK,SAAS,SAAS,EAAE,EAAE,MAAM,GAAG,EAAE;AAC/C;AC/EO,SAASgB,IAId;AACA,QAAMC,IAAI,IAAIC,EAAE,IAAA,GACVC,IAAI,IAAID,EAAE,IAAA,GAEVE,IAAK,CAACC,GAAoBC,MAAoB;AAClD,IAAIA,MAAW,YACfJ,EAAE,YAAYC,GAAGE,GAAQ,QAAQ;AAAA,EACnC,GACME,IAAK,CAACF,GAAoBC,MAAoB;AAClD,IAAIA,MAAW,YACfJ,EAAE,YAAYD,GAAGI,GAAQ,QAAQ;AAAA,EACnC;AACA,SAAAJ,EAAE,GAAG,UAAUG,CAAE,GACjBD,EAAE,GAAG,UAAUI,CAAE,GAEV;AAAA,IACL,GAAAN;AAAA,IACA,GAAAE;AAAA,IACA,UAAgB;AACd,MAAAF,EAAE,IAAI,UAAUG,CAAE,GAClBD,EAAE,IAAI,UAAUI,CAAE;AAAA,IACpB;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sobree/collab-providers",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
],
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"yjs": "^13.6.0",
|
|
47
|
-
"@sobree/core": "0.1.
|
|
47
|
+
"@sobree/core": "0.1.10"
|
|
48
48
|
},
|
|
49
49
|
"peerDependenciesMeta": {
|
|
50
50
|
"y-websocket": {
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"y-protocols": "^1.0.7",
|
|
63
63
|
"y-webrtc": "^10.3.0",
|
|
64
64
|
"y-websocket": "^3.0.0",
|
|
65
|
-
"@sobree/core": "0.1.
|
|
65
|
+
"@sobree/core": "0.1.10"
|
|
66
66
|
},
|
|
67
67
|
"scripts": {
|
|
68
68
|
"typecheck": "tsc --noEmit",
|