@weft-js/realtime-livekit 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ravonus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,9 @@
1
- # Temporary Holding Version
1
+ # @weft-js/realtime-livekit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ > **Early release (0.1.0).** APIs will change between minor versions.
4
+
5
+ Cursors over a LiveKit room, with the token signed by the room.
6
+
7
+ ```bash
8
+ npm install @weft-js/realtime-livekit
9
+ ```
package/package.json CHANGED
@@ -1,6 +1,36 @@
1
1
  {
2
2
  "name": "@weft-js/realtime-livekit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "@weft-js/realtime-livekit: cursors over a LiveKit room, with the token signed by the room",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./src/index.ts",
9
+ "./server": "./src/server.ts",
10
+ "./plugin": "./plugin.mjs"
11
+ },
12
+ "dependencies": {
13
+ "livekit-client": "2.23.0"
14
+ },
15
+ "peerDependencies": {
16
+ "@weft-js/start": "^0.1.0",
17
+ "@weft-js/core": "^0.1.0",
18
+ "@weft-js/multiplayer": "^0.1.0"
19
+ },
20
+ "engines": {
21
+ "node": ">=26"
22
+ },
23
+ "keywords": [
24
+ "weft"
25
+ ],
26
+ "files": [
27
+ "src",
28
+ "plugin.mjs",
29
+ "plugin.d.mts",
30
+ "README.md",
31
+ "LICENSE"
32
+ ],
33
+ "publishConfig": {
34
+ "access": "public"
35
+ }
36
+ }
package/plugin.d.mts ADDED
@@ -0,0 +1,5 @@
1
+ import type { PluginInstance } from '@weft-js/core/plugin'
2
+
3
+ declare const plugin: (options?: Record<string, unknown>) => PluginInstance<unknown>
4
+ /** `livekit()` for `weft.config.ts`: puts LiveKit on the record of what the app reaches. A page passes the transport to `presence`. */
5
+ export default plugin
package/plugin.mjs ADDED
@@ -0,0 +1,14 @@
1
+ // livekit() for weft.config.ts: no slot, since a page passes the transport to `presence`.
2
+ import { definePlugin } from '@weft-js/core/plugin'
3
+
4
+ export const livekit = definePlugin({
5
+ name: '@weft-js/realtime-livekit',
6
+ version: '0.1.0',
7
+ weft: '*',
8
+ permissions: { net: ['browser:livekit'], env: ['LIVEKIT_API_KEY', 'LIVEKIT_API_SECRET'] },
9
+ contribute: () => ({
10
+ agents: { title: 'Peer transport (LiveKit)', text: '`presence(conn, { transport: livekit({ url }) })` sends cursors through LiveKit, and `link.room` is the room for audio and video. Pass `ticket: livekitTicket({ media })` from `@weft-js/realtime-livekit/server` to `multiplayer()`, with `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET` on the server.' },
11
+ }),
12
+ })
13
+
14
+ export default livekit
package/src/index.ts ADDED
@@ -0,0 +1,41 @@
1
+ // Cursors over a LiveKit room. `ticket` from the server is the token, and LiveKit proves a frame's sender: a participant's
2
+ // identity is the connection the room signed it for. The room is there for audio and video too: `link.room`.
3
+
4
+ import { createRoot, derived, state, untrack } from '@weft-js/core'
5
+ import { Room, RoomEvent } from 'livekit-client'
6
+ import type { PeerLink, PeerRoom, PeerTransport } from '@weft-js/multiplayer'
7
+
8
+ const TOPIC = 'weft'
9
+
10
+ /** Options of `livekit()`. */
11
+ export interface LivekitOptions {
12
+ /** `wss://…` */
13
+ url: string
14
+ }
15
+
16
+ /** A peer transport over a LiveKit room, for `presence(conn, { transport })`. The link's `room` carries audio and video too. */
17
+ export function livekit({ url }: LivekitOptions): PeerTransport {
18
+ return {
19
+ name: 'livekit',
20
+ open(room: PeerRoom): PeerLink & { room: Room } {
21
+ return createRoot(dispose => {
22
+ const live = new Room()
23
+ const here = state<string[]>([])
24
+ const sync = () => { here.value = [...live.remoteParticipants.values()].map(p => p.identity) }
25
+ const decoder = new TextDecoder(), encoder = new TextEncoder()
26
+ live.on(RoomEvent.DataReceived, (bytes, participant, _kind, topic) => { if (topic === TOPIC && participant) room.receive(participant.identity, decoder.decode(bytes)) })
27
+ for (const e of [RoomEvent.ParticipantConnected, RoomEvent.ParticipantDisconnected, RoomEvent.Connected, RoomEvent.Disconnected]) live.on(e, sync)
28
+ if (room.ticket) void live.connect(url, room.ticket as string).then(sync, () => {})
29
+ return {
30
+ room: live,
31
+ up: derived(() => { const ids = room.peers.value; return ids.length > 0 && ids.every(id => here.value.includes(id)) }),
32
+ send(text) { if (untrack(() => here.value.length)) void live.localParticipant.publishData(encoder.encode(text), { reliable: false, topic: TOPIC }).catch(() => {}) },
33
+ close() {
34
+ void live.disconnect()
35
+ dispose()
36
+ },
37
+ }
38
+ })
39
+ },
40
+ }
41
+ }
package/src/server.ts ADDED
@@ -0,0 +1,21 @@
1
+ // The token, for `multiplayer({ ticket })`: signed with the room's identity, so no client can name itself to LiveKit.
2
+ import { roomId, signTicket } from '@weft-js/multiplayer/server'
3
+
4
+ /** Options of `livekitTicket()`. */
5
+ export interface LivekitTicketOptions {
6
+ /** Default `LIVEKIT_API_KEY`. */
7
+ key?: string
8
+ /** Default `LIVEKIT_API_SECRET`. */
9
+ secret?: string
10
+ /** Seconds. Default 3600. */
11
+ ttl?: number
12
+ /** Audio and video too. Default false: data only, which is what cursors need. */
13
+ media?: boolean
14
+ }
15
+
16
+ /** For `multiplayer({ ticket })`: a LiveKit token that names the connection and its room. Refuses to run without a key and secret. */
17
+ export const livekitTicket = ({ key = process.env.LIVEKIT_API_KEY, secret = process.env.LIVEKIT_API_SECRET, ttl = 3600, media = false }: LivekitTicketOptions = {}) => async ({ id, user, params }: { id: string; user: { name?: string }; params: unknown }) => {
18
+ if (!key || !secret) throw new Error('`livekitTicket` needs `LIVEKIT_API_KEY` and `LIVEKIT_API_SECRET`')
19
+ const now = Math.floor(Date.now() / 1000)
20
+ return signTicket({ iss: key, sub: id, name: user.name, nbf: now, exp: now + ttl, video: { room: await roomId(params), roomJoin: true, canPublish: media, canSubscribe: true, canPublishData: true } }, secret)
21
+ }