@weft-js/realtime-socketio 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-socketio
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 through a Socket.IO relay you run, with tickets the room signs.
6
+
7
+ ```bash
8
+ npm install @weft-js/realtime-socketio
9
+ ```
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@weft-js/realtime-socketio",
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-socketio: cursors through a Socket.IO relay you run, with tickets the room signs",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./src/index.ts",
9
+ "./relay": "./src/relay.ts",
10
+ "./plugin": "./plugin.mjs"
11
+ },
12
+ "dependencies": {
13
+ "socket.io-client": "4.8.4"
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
+ "socket.io": "^4.8.0"
20
+ },
21
+ "peerDependenciesMeta": {
22
+ "socket.io": {
23
+ "optional": true
24
+ }
25
+ },
26
+ "engines": {
27
+ "node": ">=26"
28
+ },
29
+ "keywords": [
30
+ "weft"
31
+ ],
32
+ "files": [
33
+ "src",
34
+ "plugin.mjs",
35
+ "plugin.d.mts",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
42
+ }
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
+ /** `socketio()` for `weft.config.ts`: puts the relay 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
+ // socketio() 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 socketio = definePlugin({
5
+ name: '@weft-js/realtime-socketio',
6
+ version: '0.1.0',
7
+ weft: '*',
8
+ permissions: { net: ['browser:socketio-relay'] },
9
+ contribute: () => ({
10
+ agents: { title: 'Peer transport (Socket.IO)', text: '`presence(conn, { transport: socketio({ url }) })` sends cursors through your relay. Run `relay(io, { secret })` from `@weft-js/realtime-socketio/relay` there, and pass `ticket: socketioTicket({ secret })`, from the same file, to `multiplayer()`. For migrations: with no relay already running, leave it out.' },
11
+ }),
12
+ })
13
+
14
+ export default socketio
package/src/index.ts ADDED
@@ -0,0 +1,37 @@
1
+ // Cursors through a Socket.IO relay you already run, for apps moving off one. The relay (`./relay`) checks the ticket the
2
+ // room signed, and stamps every frame with the ticket's subject, so a client can't speak as anyone else.
3
+
4
+ import { createRoot, derived, state, untrack } from '@weft-js/core'
5
+ import { io, type Socket } from 'socket.io-client'
6
+ import type { PeerLink, PeerRoom, PeerTransport } from '@weft-js/multiplayer'
7
+
8
+ /** Options of `socketio()`. */
9
+ export interface SocketIoOptions {
10
+ /** The relay. */
11
+ url: string
12
+ path?: string
13
+ }
14
+
15
+ /** A peer transport through a Socket.IO relay, for `presence(conn, { transport })`. */
16
+ export function socketio({ url, path }: SocketIoOptions): PeerTransport {
17
+ return {
18
+ name: 'socketio',
19
+ open(room: PeerRoom): PeerLink {
20
+ return createRoot(dispose => {
21
+ const joined = state(false)
22
+ const socket: Socket = io(url, { ...(path === undefined ? {} : { path }), auth: { ticket: room.ticket }, transports: ['websocket'] })
23
+ socket.on('weft:ok', () => { joined.value = true })
24
+ socket.on('disconnect', () => { joined.value = false })
25
+ socket.on('weft:f', (from: unknown, text: unknown) => { if (typeof from === 'string' && typeof text === 'string') room.receive(from, text) })
26
+ return {
27
+ up: derived(() => joined.value && room.peers.value.length > 0),
28
+ send: text => { if (untrack(() => joined.value)) socket.volatile.emit('weft:f', text) },
29
+ close() {
30
+ socket.disconnect()
31
+ dispose()
32
+ },
33
+ }
34
+ })
35
+ },
36
+ }
37
+ }
package/src/relay.ts ADDED
@@ -0,0 +1,37 @@
1
+ // The relay's half, for a Socket.IO server you run: `relay(io, { secret })`. A connection without a good ticket is refused,
2
+ // and a frame is fanned out with the ticket's subject as its sender.
3
+
4
+ import type { Server, Socket } from 'socket.io'
5
+ import { roomId, signTicket, verifyTicket } from '@weft-js/multiplayer/server'
6
+
7
+ const FRAME = 1024
8
+
9
+ /** Refuses a connection without a good ticket, and fans out each frame to its room under the ticket's subject. */
10
+ export function relay(io: Server, { secret }: { secret: string }) {
11
+ const admit = async (socket: Socket, next: (error?: Error) => void) => {
12
+ const claims = await verifyTicket(socket.handshake.auth?.ticket, secret)
13
+ if (!claims?.sub || !claims.room) return next(new Error('bad ticket'))
14
+ socket.data = { peer: claims.sub, room: claims.room }
15
+ next()
16
+ }
17
+ io.use((socket: Socket, next) => void admit(socket, next))
18
+ io.on('connection', async (socket: Socket) => {
19
+ await socket.join(socket.data.room)
20
+ socket.emit('weft:ok')
21
+ socket.on('weft:f', (text: unknown) => {
22
+ if (typeof text === 'string' && text.length <= FRAME) socket.to(socket.data.room).volatile.emit('weft:f', socket.data.peer, text)
23
+ })
24
+ })
25
+ }
26
+
27
+ /** Options of `socketioTicket()`. */
28
+ export interface TicketOptions {
29
+ /** Shared with the relay. */
30
+ secret: string
31
+ /** Seconds a ticket opens a connection. Default 3600. */
32
+ ttl?: number
33
+ }
34
+
35
+ /** For `multiplayer({ ticket })`: a signed claim of who this connection is and which room it may enter. */
36
+ export const socketioTicket = ({ secret, ttl = 3600 }: TicketOptions) => async ({ id, params }: { id: string; params: unknown }) =>
37
+ signTicket({ sub: id, room: await roomId(params), exp: Math.floor(Date.now() / 1000) + ttl }, secret)