Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ tagged `vX.Y.Z`.

### Fixed
- **Application icon access.** Widgets that subscribe to `dock` receive icons through the dashboard host; cross-site requests to the icon route are refused, so other widget frames cannot probe installed applications.
- **Fresh widget data after a provider retires.** Socket replay caches discard a retired channel snapshot for newly opened tiles and after a server disconnect. Already open tiles keep their last rendered value until the replacement provider publishes fresh data.
- **Widgets load on the native dashboard again.** WebKit can now load the bridge and a widget's
own script, style, image, font and media files inside the sandboxed frame.
- Shortcut buttons draw the real icon of an application that is not in the Dock again. The widget
Expand Down
7 changes: 6 additions & 1 deletion server/src/ws/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export class Hub {
this.contexts.set(socket, ctx)
socket.on('message', (raw: Buffer | string) => this.onMessage(socket, channels, raw))
socket.on('close', () => {
for (const ch of channels) this.registry.removeSubscriber(ch)
this.clients.delete(socket)
this.contexts.delete(socket)
for (const ch of channels) this.registry.removeSubscriber(ch)
})
}

Expand All @@ -58,9 +58,14 @@ export class Hub {
* channel last said belonged to a connection or a provider that is gone, and replaying it to
* the next subscriber would show data from credentials that no longer exist. A widget that
* subscribes after this waits for the first fresh poll instead, which the registry runs at once.
* Current socket clients also discard their replay cache; already open widgets keep their rendered value until the next publish.
*/
forget(channel: string): void {
this.lastValues.delete(channel)
const msg = JSON.stringify({ type: 'invalidate', channel })
for (const [socket, channels] of this.clients) {
if (channels.has(channel)) this.safeSend(socket, msg)
}
}

broadcast(channel: string, data: unknown): void {
Expand Down
19 changes: 19 additions & 0 deletions server/test/hub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ describe('the cached last value of a channel', () => {
expect(subscribe(hub, 'homey:old').sent).toEqual([])
})

it('invalidates only live subscribers when a provider is replaced', () => {
let hub!: Hub
const registry = new ProviderRegistry((c, d) => hub.broadcast(c, d), (c) => hub.forget(c))
hub = new Hub(registry)
registry.register({ channel: 'homey:x', intervalMs: 1000 })
const active = subscribe(hub, 'homey:x')
const unrelated = subscribe(hub, 'config')
hub.broadcast('homey:x', { devices: ['old host'] })

registry.register({ channel: 'homey:x', intervalMs: 1000 })

expect(active.sent).toEqual([
{ type: 'data', channel: 'homey:x', data: { devices: ['old host'] } },
{ type: 'invalidate', channel: 'homey:x' },
])
expect(unrelated.sent).toEqual([])
expect(subscribe(hub, 'homey:x').sent).toEqual([])
})

it('is dropped when the provider is replaced, so the old host is not shown', () => {
let hub!: Hub
const registry = new ProviderRegistry((c, d) => hub.broadcast(c, d), (c) => hub.forget(c))
Expand Down
4 changes: 4 additions & 0 deletions ui/src/shared/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export function useSocket(): Socket {
try { m = JSON.parse(ev.data) }
catch { console.warn('socket: invalid JSON message', ev.data); return }
if (m.type === 'data' && m.channel) { last.set(m.channel, m.data); subs.get(m.channel)?.forEach((cb) => cb(m.data)) }
// A provider can retire a channel while widgets remain subscribed. Do not replay its old snapshot.
else if (m.type === 'invalidate' && m.channel) last.delete(m.channel)
else if (m.type === 'result' || m.type === 'error') {
if (!m.id) return
const p = pending.get(m.id)
Expand All @@ -45,6 +47,8 @@ export function useSocket(): Socket {
}
ws.onclose = () => {
status.value = 'closed'
// Invalidation messages may have been missed; wait for fresh data after reconnect.
last.clear()
for (const p of pending.values()) p.reject(new Error(t('socket.lost')))
pending.clear()
setTimeout(connect, retryMs)
Expand Down
69 changes: 69 additions & 0 deletions ui/test/socket-cache-invalidation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @vitest-environment jsdom
import { afterEach, expect, it, vi } from 'vitest'

class FakeWebSocket {
static OPEN = 1
static latest: FakeWebSocket
readyState = 1
onopen: (() => void) | null = null
onmessage: ((event: { data: string }) => void) | null = null
onclose: (() => void) | null = null
onerror: (() => void) | null = null
sent: unknown[] = []

constructor() { FakeWebSocket.latest = this }
send(message: string) { this.sent.push(JSON.parse(message)) }
receive(message: unknown) { this.onmessage?.({ data: JSON.stringify(message) }) }
}

afterEach(() => {
vi.unstubAllGlobals()
vi.resetModules()
})

it('does not replay a retired snapshot to a later subscriber', async () => {
vi.stubGlobal('WebSocket', FakeWebSocket)
const { useSocket } = await import('../src/shared/socket')
const socket = useSocket()
const wire = FakeWebSocket.latest
wire.onopen?.()
const first: unknown[] = []
const second: unknown[] = []
const offFirst = socket.subscribe('service:account-a', (value) => first.push(value))

wire.receive({ type: 'data', channel: 'service:account-a', data: 'old snapshot' })
wire.receive({ type: 'invalidate', channel: 'service:account-a' })
const offSecond = socket.subscribe('service:account-a', (value) => second.push(value))
expect(second).toEqual([])
expect(wire.sent).toEqual([{ type: 'subscribe', channel: 'service:account-a' }])

wire.receive({ type: 'data', channel: 'service:account-a', data: 'fresh snapshot' })
expect(first).toEqual(['old snapshot', 'fresh snapshot'])
expect(second).toEqual(['fresh snapshot'])
offFirst()
offSecond()
})

it('does not replay a snapshot after a disconnect that may have missed invalidation', async () => {
vi.useFakeTimers()
try {
vi.stubGlobal('WebSocket', FakeWebSocket)
const { useSocket } = await import('../src/shared/socket')
const socket = useSocket()
const wire = FakeWebSocket.latest
wire.onopen?.()
const offFirst = socket.subscribe('service:account-a', () => {})
wire.receive({ type: 'data', channel: 'service:account-a', data: 'before disconnect' })
wire.readyState = 3
wire.onclose?.()

const second: unknown[] = []
const offSecond = socket.subscribe('service:account-a', (value) => second.push(value))
expect(second).toEqual([])
offFirst()
offSecond()
} finally {
vi.clearAllTimers()
vi.useRealTimers()
}
})
Loading