mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-07-17 05:34:09 +00:00
feat: comprehensive audit + Tier 3 wiring (security/correctness/features)
Multi-agent audit + fix pass covering bugs, security, dead config wiring,
and missing features. All quality gates green: build/vet/test -race/govulncheck/
golangci-lint. Frontend tests 47/47.
SECURITY & CORRECTNESS
- Scanner pipeline fail-closed: cache.go scan timeout now 503 (was goto servePkg);
scanner.go all-scanners-fail saves ScanStatusError; CheckVulnerabilities blocks
on missing/error result instead of allowing.
- Scanner argv corrections: govulncheck source-mode + go.mod discovery;
npm-audit generates lockfile via npm install --package-lock-only --ignore-scripts;
pip-audit per-extension dispatch (wheel direct / sdist extract+pyproject).
- GHSA version-range filtering implemented (was always-include); url.QueryEscape
on package name.
- pypi SSRF closed via host allowlist on original_url; url.QueryEscape on
rewriteURL output.
- Path traversal: cache temp file uses os.CreateTemp; smb keyToPath sanitized;
filesystem keyToPath returns error + filepath.Abs prefix-verify.
- Goroutine leaks plugged: cache.cleanupWorker stop channel; auth.ValidationCache
Stop() with sync.Once; ws.unregister buffered with non-blocking send.
- WebSocket double-close panic fixed via sync.Once Client.closeSend().
- Race conditions: gormstore.registryCache sync.RWMutex (was concurrent map
panic); auth.LastUsedAt no longer mutated under RLock; analytics strict
lock-ordering invariant (statsMu before downloadsMu).
- Auth validator fails CLOSED on transport/unknown-status errors (was returning
true,err 'allow cache fallback').
- Credential cache hash full SHA256 (was 8-byte truncate; eliminates 2^32
collision risk).
- filesystem.fs.used incremented after successful rename (no quota inflation
race).
- gormstore aggregation events deleted in same tx as insert (no double-counting).
- gormstore.SavePackage uses Updates(map) so zero-value security flags
(RequiresAuth=false) can be cleared.
- partition_manager validates partition name regex before DROP TABLE.
- vcs/git: version regex validation rejects --option-injection; checkout uses
--detach -- separator; removed TrimPrefix(repo,'v') that corrupted vault/
vitess/vim-go.
- WebSocket CheckOrigin allowlist (was return true; CSWSH closed); same-origin
default + ServerConfig.AllowedOrigins.
- fiber CVE GO-2026-4543 patched (v2.52.10 -> v2.52.12).
FUNCTIONAL FEATURES
- API key DB persistence: APIKeyModel + migration 202604280002, full CRUD,
async LastUsedAt updates with WaitGroup-tracked goroutines drained on Close.
- Admin bootstrap via GOHOARDER_BOOTSTRAP_ADMIN_KEY env var; idempotent
(skipped when non-revoked admin already exists).
- Auth middleware factory in pkg/app: RequireAuth, RequireRole, RequirePermission,
OptionalAuth. Mounted on proxy + read APIs when Auth.Enabled=true.
DELETE /api/packages/* requires admin role. /health public for k8s probes.
- NFS storage backend: pkg/storage/nfs wraps filesystem with /proc/mounts
detection (Linux), post-write fsync, read-after-write health probe.
- WebSocket real-time pipeline: pkg/events Broadcaster interface; cache + scanner
managers emit EventPackageCached / EventPackageDownloaded / EventScanComplete;
app.go runs 30s EventStatsUpdate ticker. Frontend has full WS client (reconnect
with exponential backoff 1s->30s, 25s heartbeat, subscriber pattern), Pinia
realtime store, Dashboard live indicator + activity feed + reactive stats
override.
- TLS termination: fiber.ListenTLS when Server.TLS.Enabled.
- Pre-warming: Prewarming.Enabled flag wired (was hardcoded false); Interval,
MaxConcurrent, TopPackages plumbed.
- NetworkConfig wired (timeouts, retry, rate-limit, circuit-breaker).
- AuthConfig.BcryptCost wired.
- Security.Scanners.Static.MaxPackageSize plumbed to cache.io.LimitReader.
- Handlers.{Go,NPM,PyPI}.Enabled gates route mounting.
- Graceful shutdown: authManager.Close drains async writes.
LINT/HYGIENE
- 80 golangci-lint issues resolved: errcheck (Close/Write/RemoveAll wrapped),
gofmt, gosec G104/G306, govet shadow renames + fieldalignment reorders,
staticcheck ST1000 pkg comments + QF1003 tagged switches + QF1008 embedded
field selectors + QF1001 De Morgan's law.
BEHAVIOR CHANGES
- Scanner now fail-closed: deployments without scanner binaries
(trivy/govulncheck/npm-audit/pip-audit/grype) BLOCK packages instead of
serving unscanned. Add binaries or plan a future allow-on-scan-error flag.
- WS origin defaults to same-origin; cross-origin dashboards must set
server.allowed_origins.
- Validator no longer fails open; private packages won't serve from cache when
validation can't be performed.
- download_events retention dropped from 24h to ~5min (deleted in agg tx).
Migration 202604280001 purges pre-upgrade events.
- DELETE /api/packages/* requires admin role when auth enabled.
NEW PACKAGES
- pkg/events - Broadcaster interface
- pkg/storage/nfs - NFS-aware filesystem wrapper
DEFERRED (out of scope this pass)
- Static scanner implementation (config defines AllowedLicenses/MaxPackageSize/
BlockSuspicious but pkg/scanner/static/ does not exist).
- AuthConfig.AuditLog wiring (no audit log infra yet).
- CacheConfig.TTLOverrides passthrough (would touch cache.Config beyond
integrator scope).
This commit is contained in:
@@ -3,10 +3,12 @@ import { mount } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import Dashboard from './Dashboard.vue'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
import { useRealtimeStore, __resetRealtimeSingleton } from '../stores/realtime'
|
||||
|
||||
describe('Dashboard.vue', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
__resetRealtimeSingleton()
|
||||
// Mock the fetch functions to prevent actual API calls
|
||||
const store = usePackageStore()
|
||||
vi.spyOn(store, 'fetchStats').mockResolvedValue()
|
||||
@@ -213,4 +215,33 @@ describe('Dashboard.vue', () => {
|
||||
expect(wrapper.text()).toContain('0')
|
||||
expect(wrapper.text()).toContain('0 B')
|
||||
})
|
||||
|
||||
it('renders realtime lastStats over polled stats when present', async () => {
|
||||
const wrapper = mount(Dashboard)
|
||||
const store = usePackageStore()
|
||||
const rt = useRealtimeStore()
|
||||
|
||||
store.loading = false
|
||||
store.stats = {
|
||||
registry: '',
|
||||
total_packages: 10,
|
||||
total_size: 1024,
|
||||
max_cache_size: 10737418240,
|
||||
total_downloads: 5,
|
||||
scanned_packages: 0,
|
||||
vulnerable_packages: 0,
|
||||
blocked_packages: 0,
|
||||
}
|
||||
rt.lastStats = {
|
||||
total_packages: 999,
|
||||
total_size: 2048,
|
||||
total_downloads: 777,
|
||||
scanned_packages: 0,
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
// Realtime values should be visible (override polled values).
|
||||
expect(wrapper.text()).toContain('999')
|
||||
expect(wrapper.text()).toContain('777')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
<template>
|
||||
<div>
|
||||
<h2 class="text-3xl font-bold text-gray-900 mb-8">Dashboard</h2>
|
||||
<div class="flex items-center justify-between mb-8">
|
||||
<h2 class="text-3xl font-bold text-gray-900">Dashboard</h2>
|
||||
<div
|
||||
class="flex items-center gap-2 px-3 py-1 rounded-full border text-xs font-medium"
|
||||
:class="
|
||||
rt.connected
|
||||
? 'bg-emerald-50 border-emerald-200 text-emerald-700'
|
||||
: 'bg-gray-100 border-gray-200 text-gray-500'
|
||||
"
|
||||
data-testid="ws-live-indicator"
|
||||
>
|
||||
<span
|
||||
class="w-2 h-2 rounded-full"
|
||||
:class="rt.connected ? 'bg-emerald-500 animate-pulse' : 'bg-gray-400'"
|
||||
></span>
|
||||
{{ rt.connected ? 'Live' : 'Offline' }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Error Alert -->
|
||||
<Alert v-if="error" variant="destructive" class="mb-4">
|
||||
@@ -22,7 +39,7 @@
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Packages</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(stats?.total_packages || 0) }}
|
||||
{{ formatNumber(displayStats?.total_packages || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-slate-100 rounded-xl flex items-center justify-center">
|
||||
@@ -38,7 +55,7 @@
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Size</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatBytes(stats?.total_size || 0) }}
|
||||
{{ formatBytes(displayStats?.total_size || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-sky-50 rounded-xl flex items-center justify-center">
|
||||
@@ -54,7 +71,7 @@
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Total Downloads</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(stats?.total_downloads || 0) }}
|
||||
{{ formatNumber(displayStats?.total_downloads || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-emerald-50 rounded-xl flex items-center justify-center">
|
||||
@@ -70,7 +87,7 @@
|
||||
<div class="space-y-2">
|
||||
<p class="text-sm font-medium text-muted-foreground">Scanned Packages</p>
|
||||
<p class="text-3xl font-bold text-foreground tracking-tight">
|
||||
{{ formatNumber(stats?.scanned_packages || 0) }}
|
||||
{{ formatNumber(displayStats?.scanned_packages || 0) }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="w-12 h-12 bg-violet-50 rounded-xl flex items-center justify-center">
|
||||
@@ -81,6 +98,31 @@
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<!-- Realtime Feed -->
|
||||
<Card v-if="recentRealtimeEvents.length > 0" class="border-0 shadow-lg mb-10" data-testid="ws-event-feed">
|
||||
<CardContent class="p-6">
|
||||
<h3 class="text-xl font-semibold text-foreground mb-4">
|
||||
<i class="fas fa-bolt mr-2 text-amber-500"></i>Live Activity
|
||||
</h3>
|
||||
<ul class="space-y-2">
|
||||
<li
|
||||
v-for="(evt, idx) in recentRealtimeEvents"
|
||||
:key="`${evt.timestamp}-${idx}`"
|
||||
class="flex items-center gap-3 text-sm"
|
||||
>
|
||||
<component :is="iconForEvent(evt.type)" class="w-4 h-4 text-muted-foreground" />
|
||||
<span class="font-medium">{{ labelForEvent(evt.type) }}</span>
|
||||
<span class="text-muted-foreground truncate">
|
||||
{{ describeEvent(evt) }}
|
||||
</span>
|
||||
<span class="ml-auto text-xs text-muted-foreground">
|
||||
{{ formatTimestamp(evt.timestamp) }}
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- Downloads Chart -->
|
||||
<Card class="border-0 shadow-lg mb-10">
|
||||
<CardContent class="p-6">
|
||||
@@ -178,10 +220,19 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { computed, onMounted, ref, watch, type Component } from 'vue'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import axios from 'axios'
|
||||
import {
|
||||
Package as PackageIcon,
|
||||
Download as DownloadIcon,
|
||||
Trash2 as TrashIcon,
|
||||
ShieldCheck as ShieldIcon,
|
||||
Activity as ActivityIcon,
|
||||
} from 'lucide-vue-next'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
import { useRealtimeStore } from '../stores/realtime'
|
||||
import type { EventType, RealtimeEvent } from '@/lib/ws'
|
||||
import { Alert, AlertDescription } from '@/components/ui/alert'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
@@ -191,6 +242,56 @@ import { getRegistryBadgeClass } from '@/composables/useBadgeStyles'
|
||||
const store = usePackageStore()
|
||||
const { packages, stats, loading, error } = storeToRefs(store)
|
||||
|
||||
const rt = useRealtimeStore()
|
||||
const { lastStats, events: rtEvents } = storeToRefs(rt)
|
||||
|
||||
// Realtime stats override polled stats when available; otherwise fall back.
|
||||
const displayStats = computed(() => lastStats.value ?? stats.value)
|
||||
|
||||
const recentRealtimeEvents = computed<RealtimeEvent[]>(() => {
|
||||
const list = rtEvents.value
|
||||
return list.slice(Math.max(list.length - 5, 0)).reverse()
|
||||
})
|
||||
|
||||
const EVENT_LABELS: Record<EventType, string> = {
|
||||
package_cached: 'Cached',
|
||||
package_deleted: 'Deleted',
|
||||
package_downloaded: 'Downloaded',
|
||||
scan_complete: 'Scan complete',
|
||||
stats_update: 'Stats update',
|
||||
}
|
||||
|
||||
const EVENT_ICONS: Record<EventType, Component> = {
|
||||
package_cached: PackageIcon,
|
||||
package_deleted: TrashIcon,
|
||||
package_downloaded: DownloadIcon,
|
||||
scan_complete: ShieldIcon,
|
||||
stats_update: ActivityIcon,
|
||||
}
|
||||
|
||||
function iconForEvent(type: EventType): Component {
|
||||
return EVENT_ICONS[type] ?? ActivityIcon
|
||||
}
|
||||
|
||||
function labelForEvent(type: EventType): string {
|
||||
return EVENT_LABELS[type] ?? type
|
||||
}
|
||||
|
||||
function describeEvent(evt: RealtimeEvent): string {
|
||||
const p = evt.payload
|
||||
const name = typeof p.name === 'string' ? p.name : ''
|
||||
const version = typeof p.version === 'string' ? `@${p.version}` : ''
|
||||
const registry = typeof p.registry === 'string' ? `[${p.registry}] ` : ''
|
||||
if (evt.type === 'stats_update') return ''
|
||||
if (name) return `${registry}${name}${version}`
|
||||
return ''
|
||||
}
|
||||
|
||||
function formatTimestamp(ts: number): string {
|
||||
if (!ts) return ''
|
||||
return new Date(ts).toLocaleTimeString()
|
||||
}
|
||||
|
||||
// Chart periods and data
|
||||
const selectedPeriod = ref<string>('1day')
|
||||
const chartPeriods = [
|
||||
@@ -286,6 +387,10 @@ watch(selectedPeriod, () => {
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
// Connect to realtime stream first so we can pick up live events while
|
||||
// initial polling resolves. Singleton survives navigation, so we
|
||||
// intentionally do not disconnect on unmount.
|
||||
rt.connect()
|
||||
await store.fetchStats()
|
||||
await store.fetchPackages()
|
||||
await fetchTimeSeriesData()
|
||||
|
||||
@@ -1,22 +1,56 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { createRouter, createMemoryHistory, type Router } from 'vue-router'
|
||||
import { defineComponent, h } from 'vue'
|
||||
import PackageList from './PackageList.vue'
|
||||
import { usePackageStore } from '../stores/packages'
|
||||
|
||||
const Stub = defineComponent({ render: () => h('div') })
|
||||
|
||||
function createTestRouter(): Router {
|
||||
return createRouter({
|
||||
history: createMemoryHistory(),
|
||||
routes: [
|
||||
{ path: '/', name: 'dashboard', component: Stub },
|
||||
{ path: '/packages/:registry?', name: 'packages', component: Stub, props: true },
|
||||
{
|
||||
path: '/package/:registry/:name+/:version',
|
||||
name: 'package-details',
|
||||
component: Stub,
|
||||
props: true,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
async function mountWithRouter() {
|
||||
const router = createTestRouter()
|
||||
router.push('/packages')
|
||||
await router.isReady()
|
||||
return mount(PackageList, {
|
||||
global: {
|
||||
plugins: [router],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
describe('PackageList.vue', () => {
|
||||
beforeEach(() => {
|
||||
// Create a fresh pinia instance before each test
|
||||
setActivePinia(createPinia())
|
||||
// Prevent real network calls from store actions on mount
|
||||
const store = usePackageStore()
|
||||
vi.spyOn(store, 'fetchPackages').mockResolvedValue()
|
||||
})
|
||||
|
||||
it('renders package list component', () => {
|
||||
const wrapper = mount(PackageList)
|
||||
it('renders package list component', async () => {
|
||||
const wrapper = await mountWithRouter()
|
||||
expect(wrapper.find('h2').text()).toBe('Packages')
|
||||
})
|
||||
|
||||
it('displays loading state when loading', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = true
|
||||
@@ -26,7 +60,7 @@ describe('PackageList.vue', () => {
|
||||
})
|
||||
|
||||
it('displays error message when error occurs', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.error = 'Failed to fetch packages'
|
||||
@@ -36,7 +70,7 @@ describe('PackageList.vue', () => {
|
||||
})
|
||||
|
||||
it('displays empty state when no packages', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = false
|
||||
@@ -47,7 +81,7 @@ describe('PackageList.vue', () => {
|
||||
})
|
||||
|
||||
it('displays package accordion when packages exist', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = false
|
||||
@@ -69,17 +103,19 @@ describe('PackageList.vue', () => {
|
||||
expect(wrapper.text()).toContain('1 version')
|
||||
})
|
||||
|
||||
it('calls fetchPackages on mount', () => {
|
||||
it('calls fetchPackages on mount', async () => {
|
||||
const store = usePackageStore()
|
||||
const fetchSpy = vi.spyOn(store, 'fetchPackages')
|
||||
// beforeEach already spied with mockResolvedValue; reuse that spy
|
||||
const fetchSpy = store.fetchPackages as ReturnType<typeof vi.spyOn>
|
||||
fetchSpy.mockClear()
|
||||
|
||||
mount(PackageList)
|
||||
await mountWithRouter()
|
||||
|
||||
expect(fetchSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('groups packages and displays version counts', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = false
|
||||
@@ -112,7 +148,7 @@ describe('PackageList.vue', () => {
|
||||
})
|
||||
|
||||
it('formats bytes correctly', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = false
|
||||
@@ -134,7 +170,7 @@ describe('PackageList.vue', () => {
|
||||
})
|
||||
|
||||
it('applies correct registry badge classes', async () => {
|
||||
const wrapper = mount(PackageList)
|
||||
const wrapper = await mountWithRouter()
|
||||
const store = usePackageStore()
|
||||
|
||||
store.loading = false
|
||||
@@ -179,9 +215,10 @@ describe('PackageList.vue', () => {
|
||||
expect(wrapper.text()).toContain('go')
|
||||
|
||||
// Verify badge component is used with correct classes
|
||||
// (matches getRegistryBadgeClass in src/composables/useBadgeStyles.ts)
|
||||
const html = wrapper.html()
|
||||
expect(html).toContain('bg-blue-100') // npm badge
|
||||
expect(html).toContain('bg-green-100') // pypi badge
|
||||
expect(html).toContain('bg-yellow-100') // go badge
|
||||
expect(html).toContain('bg-red-100') // npm badge
|
||||
expect(html).toContain('bg-blue-100') // pypi badge
|
||||
expect(html).toContain('bg-cyan-100') // go badge
|
||||
})
|
||||
})
|
||||
|
||||
@@ -172,7 +172,10 @@ describe('Stats.vue', () => {
|
||||
}
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
const containers = wrapper.findAll('.rounded-full')
|
||||
// Scope to the registry icon containers (w-12 h-12 rounded-full),
|
||||
// excluding the storage progress bar which also uses rounded-full.
|
||||
const containers = wrapper.findAll('.w-12.h-12.rounded-full')
|
||||
expect(containers).toHaveLength(3)
|
||||
expect(containers[0].classes()).toContain('bg-red-100') // npm
|
||||
expect(containers[1].classes()).toContain('bg-blue-100') // pypi
|
||||
expect(containers[2].classes()).toContain('bg-cyan-100') // go
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { WSClient, __wsInternals, type RealtimeEvent } from './ws'
|
||||
|
||||
/**
|
||||
* Minimal WebSocket stub mimicking the parts of the browser API the WSClient
|
||||
* touches: readyState, onopen, onmessage, onclose, onerror, send, close.
|
||||
*/
|
||||
class StubWebSocket {
|
||||
static OPEN = 1
|
||||
static CLOSED = 3
|
||||
static instances: StubWebSocket[] = []
|
||||
|
||||
url: string
|
||||
readyState = 0
|
||||
sent: string[] = []
|
||||
onopen: ((ev: Event) => void) | null = null
|
||||
onmessage: ((ev: MessageEvent) => void) | null = null
|
||||
onclose: ((ev: CloseEvent) => void) | null = null
|
||||
onerror: ((ev: Event) => void) | null = null
|
||||
|
||||
constructor(url: string) {
|
||||
this.url = url
|
||||
StubWebSocket.instances.push(this)
|
||||
}
|
||||
|
||||
// Test helpers
|
||||
open(): void {
|
||||
this.readyState = StubWebSocket.OPEN
|
||||
this.onopen?.(new Event('open'))
|
||||
}
|
||||
|
||||
receive(data: unknown): void {
|
||||
const text = typeof data === 'string' ? data : JSON.stringify(data)
|
||||
this.onmessage?.({ data: text } as MessageEvent)
|
||||
}
|
||||
|
||||
triggerClose(): void {
|
||||
this.readyState = StubWebSocket.CLOSED
|
||||
this.onclose?.({} as CloseEvent)
|
||||
}
|
||||
|
||||
// Real API
|
||||
send(payload: string): void {
|
||||
this.sent.push(payload)
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.readyState = StubWebSocket.CLOSED
|
||||
this.onclose?.({} as CloseEvent)
|
||||
}
|
||||
}
|
||||
|
||||
const originalWebSocket = (globalThis as { WebSocket?: typeof WebSocket }).WebSocket
|
||||
|
||||
beforeEach(() => {
|
||||
StubWebSocket.instances = []
|
||||
;(globalThis as unknown as { WebSocket: unknown }).WebSocket = StubWebSocket
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
;(globalThis as unknown as { WebSocket: unknown }).WebSocket = originalWebSocket
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
describe('parseEvent', () => {
|
||||
it('normalizes backend envelope (data + RFC3339 timestamp)', () => {
|
||||
const evt = __wsInternals.parseEvent(
|
||||
JSON.stringify({
|
||||
type: 'package_cached',
|
||||
timestamp: '2026-04-28T12:00:00Z',
|
||||
data: { name: 'lodash', version: '4.17.21', registry: 'npm' },
|
||||
}),
|
||||
)
|
||||
expect(evt).not.toBeNull()
|
||||
expect(evt!.type).toBe('package_cached')
|
||||
expect(evt!.payload).toMatchObject({ name: 'lodash' })
|
||||
expect(evt!.timestamp).toBe(Date.parse('2026-04-28T12:00:00Z'))
|
||||
})
|
||||
|
||||
it('drops malformed JSON', () => {
|
||||
expect(__wsInternals.parseEvent('not-json{{')).toBeNull()
|
||||
})
|
||||
|
||||
it('drops unknown event types (e.g. control frames)', () => {
|
||||
expect(
|
||||
__wsInternals.parseEvent(JSON.stringify({ type: 'pong' })),
|
||||
).toBeNull()
|
||||
})
|
||||
|
||||
it('accepts numeric timestamp', () => {
|
||||
const evt = __wsInternals.parseEvent(
|
||||
JSON.stringify({ type: 'stats_update', timestamp: 1234567890, data: {} }),
|
||||
)
|
||||
expect(evt!.timestamp).toBe(1234567890)
|
||||
})
|
||||
})
|
||||
|
||||
describe('WSClient', () => {
|
||||
it('connects and dispatches typed events to subscribers', () => {
|
||||
const client = new WSClient('ws://test/ws')
|
||||
const received: RealtimeEvent[] = []
|
||||
client.on('package_cached', (e) => received.push(e))
|
||||
client.connect()
|
||||
|
||||
const sock = StubWebSocket.instances[0]
|
||||
expect(sock).toBeDefined()
|
||||
sock.open()
|
||||
sock.receive({
|
||||
type: 'package_cached',
|
||||
timestamp: '2026-04-28T00:00:00Z',
|
||||
data: { name: 'foo' },
|
||||
})
|
||||
|
||||
expect(received).toHaveLength(1)
|
||||
expect(received[0].type).toBe('package_cached')
|
||||
expect(received[0].payload.name).toBe('foo')
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('forwards events to wildcard subscribers', () => {
|
||||
const client = new WSClient('ws://test/ws')
|
||||
const seen: string[] = []
|
||||
client.on('*', (e) => seen.push(e.type))
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
sock.open()
|
||||
sock.receive({ type: 'scan_complete', data: {} })
|
||||
sock.receive({ type: 'package_deleted', data: {} })
|
||||
expect(seen).toEqual(['scan_complete', 'package_deleted'])
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('unsubscribe stops further dispatches', () => {
|
||||
const client = new WSClient('ws://test/ws')
|
||||
let count = 0
|
||||
const off = client.on('package_cached', () => {
|
||||
count += 1
|
||||
})
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
sock.open()
|
||||
sock.receive({ type: 'package_cached', data: {} })
|
||||
expect(count).toBe(1)
|
||||
off()
|
||||
sock.receive({ type: 'package_cached', data: {} })
|
||||
expect(count).toBe(1)
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('auto-reconnects with backoff after unexpected close', () => {
|
||||
vi.useFakeTimers()
|
||||
const client = new WSClient('ws://test/ws')
|
||||
client.connect()
|
||||
const first = StubWebSocket.instances[0]
|
||||
first.open()
|
||||
first.triggerClose()
|
||||
|
||||
// First reconnect happens after RECONNECT_BASE_MS (1s).
|
||||
vi.advanceTimersByTime(__wsInternals.RECONNECT_BASE_MS)
|
||||
expect(StubWebSocket.instances.length).toBe(2)
|
||||
|
||||
// Second drop -> next backoff is 2s.
|
||||
StubWebSocket.instances[1].open()
|
||||
StubWebSocket.instances[1].triggerClose()
|
||||
vi.advanceTimersByTime(__wsInternals.RECONNECT_BASE_MS * 2)
|
||||
expect(StubWebSocket.instances.length).toBe(3)
|
||||
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('does not reconnect after explicit close', () => {
|
||||
vi.useFakeTimers()
|
||||
const client = new WSClient('ws://test/ws')
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
sock.open()
|
||||
client.close()
|
||||
vi.advanceTimersByTime(60_000)
|
||||
expect(StubWebSocket.instances.length).toBe(1)
|
||||
})
|
||||
|
||||
it('emits heartbeat ping every 25s', () => {
|
||||
vi.useFakeTimers()
|
||||
const client = new WSClient('ws://test/ws')
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
sock.open()
|
||||
expect(sock.sent).toHaveLength(0)
|
||||
vi.advanceTimersByTime(__wsInternals.HEARTBEAT_INTERVAL_MS)
|
||||
expect(sock.sent).toHaveLength(1)
|
||||
expect(JSON.parse(sock.sent[0])).toEqual({ action: 'ping' })
|
||||
vi.advanceTimersByTime(__wsInternals.HEARTBEAT_INTERVAL_MS)
|
||||
expect(sock.sent).toHaveLength(2)
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('queues outbound messages while disconnected and flushes on open', () => {
|
||||
const client = new WSClient('ws://test/ws')
|
||||
client.send({ action: 'subscribe', data: ['package_cached'] })
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
expect(sock.sent).toHaveLength(0)
|
||||
sock.open()
|
||||
expect(sock.sent).toHaveLength(1)
|
||||
expect(JSON.parse(sock.sent[0])).toMatchObject({ action: 'subscribe' })
|
||||
client.close()
|
||||
})
|
||||
|
||||
it('drops malformed inbound JSON without throwing', () => {
|
||||
const client = new WSClient('ws://test/ws')
|
||||
let count = 0
|
||||
client.on('*', () => {
|
||||
count += 1
|
||||
})
|
||||
client.connect()
|
||||
const sock = StubWebSocket.instances[0]
|
||||
sock.open()
|
||||
sock.receive('not-json{{{')
|
||||
sock.receive({ type: 'unknown_type', data: {} })
|
||||
expect(count).toBe(0)
|
||||
client.close()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,301 @@
|
||||
/**
|
||||
* Lightweight WebSocket client with auto-reconnect, heartbeat, and pub/sub.
|
||||
*
|
||||
* Backend (pkg/websocket/server.go) emits envelopes shaped like:
|
||||
* { "type": "package_cached", "timestamp": "2026-04-28T12:34:56Z", "data": {...} }
|
||||
*
|
||||
* We normalize this into RealtimeEvent { type, payload, timestamp(number ms) }.
|
||||
*/
|
||||
|
||||
export type EventType =
|
||||
| 'package_deleted'
|
||||
| 'package_cached'
|
||||
| 'package_downloaded'
|
||||
| 'scan_complete'
|
||||
| 'stats_update'
|
||||
|
||||
export interface RealtimeEvent {
|
||||
type: EventType
|
||||
payload: Record<string, unknown>
|
||||
timestamp: number
|
||||
}
|
||||
|
||||
type WildcardListener = '*'
|
||||
type ListenerKey = EventType | WildcardListener
|
||||
type Listener = (e: RealtimeEvent) => void
|
||||
|
||||
const KNOWN_EVENT_TYPES: ReadonlySet<string> = new Set<EventType>([
|
||||
'package_deleted',
|
||||
'package_cached',
|
||||
'package_downloaded',
|
||||
'scan_complete',
|
||||
'stats_update',
|
||||
])
|
||||
|
||||
const RECONNECT_BASE_MS = 1_000
|
||||
const RECONNECT_MAX_MS = 30_000
|
||||
const HEARTBEAT_INTERVAL_MS = 25_000
|
||||
const RECONNECT_QUEUE_CAP = 50
|
||||
|
||||
interface OutboundMessage {
|
||||
action: string
|
||||
data?: unknown
|
||||
}
|
||||
|
||||
function defaultUrl(): string {
|
||||
if (typeof window === 'undefined' || !window.location) {
|
||||
return 'ws://localhost/ws'
|
||||
}
|
||||
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
return `${proto}//${window.location.host}/ws`
|
||||
}
|
||||
|
||||
function isKnownEventType(value: unknown): value is EventType {
|
||||
return typeof value === 'string' && KNOWN_EVENT_TYPES.has(value)
|
||||
}
|
||||
|
||||
function coerceTimestamp(raw: unknown): number {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw)) {
|
||||
return raw
|
||||
}
|
||||
if (typeof raw === 'string') {
|
||||
const parsed = Date.parse(raw)
|
||||
if (!Number.isNaN(parsed)) return parsed
|
||||
}
|
||||
return Date.now()
|
||||
}
|
||||
|
||||
function parseEvent(text: string): RealtimeEvent | null {
|
||||
let parsed: unknown
|
||||
try {
|
||||
parsed = JSON.parse(text)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
if (!parsed || typeof parsed !== 'object') return null
|
||||
const obj = parsed as Record<string, unknown>
|
||||
|
||||
// Server may also emit `{ type: "pong" }` style control frames; ignore those.
|
||||
if (!isKnownEventType(obj.type)) return null
|
||||
|
||||
const payloadSource = obj.payload ?? obj.data
|
||||
const payload =
|
||||
payloadSource && typeof payloadSource === 'object'
|
||||
? (payloadSource as Record<string, unknown>)
|
||||
: {}
|
||||
|
||||
return {
|
||||
type: obj.type,
|
||||
payload,
|
||||
timestamp: coerceTimestamp(obj.timestamp),
|
||||
}
|
||||
}
|
||||
|
||||
export class WSClient {
|
||||
readonly url: string
|
||||
private socket: WebSocket | null = null
|
||||
private listeners: Map<ListenerKey, Set<Listener>> = new Map()
|
||||
private heartbeatTimer: ReturnType<typeof setInterval> | null = null
|
||||
private reconnectTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private reconnectAttempts = 0
|
||||
private explicitlyClosed = false
|
||||
private outboundQueue: OutboundMessage[] = []
|
||||
private _connected = false
|
||||
|
||||
constructor(url?: string) {
|
||||
this.url = url ?? defaultUrl()
|
||||
}
|
||||
|
||||
get connected(): boolean {
|
||||
return this._connected
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
this.explicitlyClosed = false
|
||||
this.openSocket()
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.explicitlyClosed = true
|
||||
this.clearReconnect()
|
||||
this.stopHeartbeat()
|
||||
this._connected = false
|
||||
if (this.socket) {
|
||||
try {
|
||||
this.socket.close()
|
||||
} catch {
|
||||
/* swallow */
|
||||
}
|
||||
this.socket = null
|
||||
}
|
||||
}
|
||||
|
||||
on(type: ListenerKey, cb: Listener): () => void {
|
||||
let bucket = this.listeners.get(type)
|
||||
if (!bucket) {
|
||||
bucket = new Set()
|
||||
this.listeners.set(type, bucket)
|
||||
}
|
||||
bucket.add(cb)
|
||||
return () => {
|
||||
const current = this.listeners.get(type)
|
||||
if (!current) return
|
||||
current.delete(cb)
|
||||
if (current.size === 0) this.listeners.delete(type)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message. If not connected, queue (capped) and flush on open.
|
||||
*/
|
||||
send(msg: OutboundMessage): void {
|
||||
if (this.socket && this._connected && this.socket.readyState === 1) {
|
||||
try {
|
||||
this.socket.send(JSON.stringify(msg))
|
||||
return
|
||||
} catch {
|
||||
/* fall through to queue */
|
||||
}
|
||||
}
|
||||
if (this.outboundQueue.length >= RECONNECT_QUEUE_CAP) {
|
||||
this.outboundQueue.shift()
|
||||
}
|
||||
this.outboundQueue.push(msg)
|
||||
}
|
||||
|
||||
private openSocket(): void {
|
||||
const Ctor: typeof WebSocket | undefined =
|
||||
typeof WebSocket !== 'undefined'
|
||||
? WebSocket
|
||||
: (globalThis as unknown as { WebSocket?: typeof WebSocket }).WebSocket
|
||||
if (!Ctor) {
|
||||
// No WebSocket available (e.g. SSR); silently bail.
|
||||
return
|
||||
}
|
||||
|
||||
let socket: WebSocket
|
||||
try {
|
||||
socket = new Ctor(this.url)
|
||||
} catch {
|
||||
this.scheduleReconnect()
|
||||
return
|
||||
}
|
||||
this.socket = socket
|
||||
|
||||
socket.onopen = () => {
|
||||
this._connected = true
|
||||
this.reconnectAttempts = 0
|
||||
this.startHeartbeat()
|
||||
this.flushQueue()
|
||||
}
|
||||
|
||||
socket.onmessage = (ev: MessageEvent) => {
|
||||
const data = ev.data
|
||||
if (typeof data !== 'string') return
|
||||
const evt = parseEvent(data)
|
||||
if (!evt) return
|
||||
this.dispatch(evt)
|
||||
}
|
||||
|
||||
socket.onerror = () => {
|
||||
// onclose handles reconnect; nothing to do here.
|
||||
}
|
||||
|
||||
socket.onclose = () => {
|
||||
this._connected = false
|
||||
this.stopHeartbeat()
|
||||
this.socket = null
|
||||
if (!this.explicitlyClosed) {
|
||||
this.scheduleReconnect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private dispatch(event: RealtimeEvent): void {
|
||||
const exact = this.listeners.get(event.type)
|
||||
if (exact) {
|
||||
for (const cb of exact) {
|
||||
try {
|
||||
cb(event)
|
||||
} catch (err) {
|
||||
console.error('[ws] listener error', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
const wild = this.listeners.get('*')
|
||||
if (wild) {
|
||||
for (const cb of wild) {
|
||||
try {
|
||||
cb(event)
|
||||
} catch (err) {
|
||||
console.error('[ws] listener error', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private flushQueue(): void {
|
||||
if (!this.socket || !this._connected) return
|
||||
const queued = this.outboundQueue.splice(0)
|
||||
for (const msg of queued) {
|
||||
try {
|
||||
this.socket.send(JSON.stringify(msg))
|
||||
} catch {
|
||||
// requeue and bail
|
||||
this.outboundQueue.unshift(msg)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private startHeartbeat(): void {
|
||||
this.stopHeartbeat()
|
||||
this.heartbeatTimer = setInterval(() => {
|
||||
if (this.socket && this._connected) {
|
||||
try {
|
||||
this.socket.send(JSON.stringify({ action: 'ping' }))
|
||||
} catch {
|
||||
/* swallow; reconnect path will handle */
|
||||
}
|
||||
}
|
||||
}, HEARTBEAT_INTERVAL_MS)
|
||||
}
|
||||
|
||||
private stopHeartbeat(): void {
|
||||
if (this.heartbeatTimer !== null) {
|
||||
clearInterval(this.heartbeatTimer)
|
||||
this.heartbeatTimer = null
|
||||
}
|
||||
}
|
||||
|
||||
private scheduleReconnect(): void {
|
||||
if (this.explicitlyClosed) return
|
||||
this.clearReconnect()
|
||||
const delay = Math.min(
|
||||
RECONNECT_MAX_MS,
|
||||
RECONNECT_BASE_MS * 2 ** this.reconnectAttempts,
|
||||
)
|
||||
this.reconnectAttempts += 1
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.reconnectTimer = null
|
||||
this.openSocket()
|
||||
}, delay)
|
||||
}
|
||||
|
||||
private clearReconnect(): void {
|
||||
if (this.reconnectTimer !== null) {
|
||||
clearTimeout(this.reconnectTimer)
|
||||
this.reconnectTimer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Test-only helpers — exported under a namespace so production code does not
|
||||
// reach into reconnect math accidentally.
|
||||
export const __wsInternals = {
|
||||
RECONNECT_BASE_MS,
|
||||
RECONNECT_MAX_MS,
|
||||
HEARTBEAT_INTERVAL_MS,
|
||||
parseEvent,
|
||||
defaultUrl,
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { useRealtimeStore, __resetRealtimeSingleton } from './realtime'
|
||||
import type { RealtimeEvent } from '@/lib/ws'
|
||||
|
||||
function makeEvent(
|
||||
type: RealtimeEvent['type'],
|
||||
payload: Record<string, unknown> = {},
|
||||
timestamp = Date.now(),
|
||||
): RealtimeEvent {
|
||||
return { type, payload, timestamp }
|
||||
}
|
||||
|
||||
describe('useRealtimeStore', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
__resetRealtimeSingleton()
|
||||
})
|
||||
|
||||
it('appends events on ingest', () => {
|
||||
const store = useRealtimeStore()
|
||||
store.ingest(makeEvent('package_cached', { name: 'a' }))
|
||||
store.ingest(makeEvent('package_deleted', { name: 'b' }))
|
||||
expect(store.events).toHaveLength(2)
|
||||
expect(store.events[0].type).toBe('package_cached')
|
||||
})
|
||||
|
||||
it('caps events at 100 with FIFO eviction', () => {
|
||||
const store = useRealtimeStore()
|
||||
for (let i = 0; i < 150; i += 1) {
|
||||
store.ingest(makeEvent('package_cached', { i }))
|
||||
}
|
||||
expect(store.events).toHaveLength(100)
|
||||
// First event should be index 50 (0..49 dropped)
|
||||
expect((store.events[0].payload as { i: number }).i).toBe(50)
|
||||
expect((store.events[99].payload as { i: number }).i).toBe(149)
|
||||
})
|
||||
|
||||
it('updates lastStats only on stats_update events', () => {
|
||||
const store = useRealtimeStore()
|
||||
expect(store.lastStats).toBeNull()
|
||||
store.ingest(makeEvent('package_cached', { name: 'x' }))
|
||||
expect(store.lastStats).toBeNull()
|
||||
store.ingest(
|
||||
makeEvent('stats_update', {
|
||||
total_packages: 42,
|
||||
total_size: 1024,
|
||||
}),
|
||||
)
|
||||
expect(store.lastStats).toMatchObject({ total_packages: 42, total_size: 1024 })
|
||||
})
|
||||
|
||||
it('exposes filtered computed slices', () => {
|
||||
const store = useRealtimeStore()
|
||||
store.ingest(makeEvent('package_cached'))
|
||||
store.ingest(makeEvent('package_downloaded'))
|
||||
store.ingest(makeEvent('scan_complete'))
|
||||
store.ingest(makeEvent('stats_update'))
|
||||
expect(store.recentCacheActivity).toHaveLength(2)
|
||||
expect(store.recentScans).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,118 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
import { WSClient, type EventType, type RealtimeEvent } from '@/lib/ws'
|
||||
|
||||
const EVENTS_CAP = 100
|
||||
|
||||
export interface RealtimeStats {
|
||||
registry?: string
|
||||
total_packages?: number
|
||||
total_size?: number
|
||||
max_cache_size?: number
|
||||
total_downloads?: number
|
||||
scanned_packages?: number
|
||||
vulnerable_packages?: number
|
||||
blocked_packages?: number
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
let singletonClient: WSClient | null = null
|
||||
|
||||
function getClient(url?: string): WSClient {
|
||||
if (!singletonClient) {
|
||||
singletonClient = new WSClient(url)
|
||||
}
|
||||
return singletonClient
|
||||
}
|
||||
|
||||
// Test-only reset hook. Avoids leaking a singleton across tests.
|
||||
export function __resetRealtimeSingleton(): void {
|
||||
if (singletonClient) {
|
||||
singletonClient.close()
|
||||
}
|
||||
singletonClient = null
|
||||
}
|
||||
|
||||
export const useRealtimeStore = defineStore('realtime', () => {
|
||||
const connected = ref(false)
|
||||
const events = ref<RealtimeEvent[]>([])
|
||||
const lastStats = ref<RealtimeStats | null>(null)
|
||||
const unsubscribers = ref<Array<() => void>>([])
|
||||
|
||||
const recentCacheActivity = computed(() =>
|
||||
events.value.filter(
|
||||
(e) => e.type === 'package_cached' || e.type === 'package_downloaded',
|
||||
),
|
||||
)
|
||||
|
||||
const recentScans = computed(() =>
|
||||
events.value.filter((e) => e.type === 'scan_complete'),
|
||||
)
|
||||
|
||||
function appendEvent(event: RealtimeEvent): void {
|
||||
events.value.push(event)
|
||||
if (events.value.length > EVENTS_CAP) {
|
||||
events.value.splice(0, events.value.length - EVENTS_CAP)
|
||||
}
|
||||
if (event.type === 'stats_update') {
|
||||
lastStats.value = event.payload as RealtimeStats
|
||||
}
|
||||
}
|
||||
|
||||
function ingest(event: RealtimeEvent): void {
|
||||
appendEvent(event)
|
||||
}
|
||||
|
||||
function connect(url?: string): void {
|
||||
// Bail in SSR / non-browser test environments where there is no
|
||||
// WebSocket constructor. Keeps Dashboard.spec.ts (which mounts the
|
||||
// component without a WS stub) from leaking timers.
|
||||
const hasWS =
|
||||
typeof globalThis !== 'undefined' &&
|
||||
typeof (globalThis as { WebSocket?: unknown }).WebSocket !== 'undefined'
|
||||
if (!hasWS) return
|
||||
|
||||
const client = getClient(url)
|
||||
if (unsubscribers.value.length === 0) {
|
||||
const types: EventType[] = [
|
||||
'package_cached',
|
||||
'package_deleted',
|
||||
'package_downloaded',
|
||||
'scan_complete',
|
||||
'stats_update',
|
||||
]
|
||||
for (const t of types) {
|
||||
unsubscribers.value.push(client.on(t, ingest))
|
||||
}
|
||||
// Poll connected status — WSClient does not yet emit connection events;
|
||||
// a 1s tick is acceptable here and avoids tight coupling.
|
||||
const tick = setInterval(() => {
|
||||
connected.value = client.connected
|
||||
}, 1_000)
|
||||
unsubscribers.value.push(() => clearInterval(tick))
|
||||
}
|
||||
client.connect()
|
||||
// Reflect the current state immediately so tests/UX don't wait a tick.
|
||||
connected.value = client.connected
|
||||
}
|
||||
|
||||
function disconnect(): void {
|
||||
for (const off of unsubscribers.value) off()
|
||||
unsubscribers.value = []
|
||||
if (singletonClient) {
|
||||
singletonClient.close()
|
||||
}
|
||||
connected.value = false
|
||||
}
|
||||
|
||||
return {
|
||||
connected,
|
||||
events,
|
||||
lastStats,
|
||||
recentCacheActivity,
|
||||
recentScans,
|
||||
connect,
|
||||
disconnect,
|
||||
ingest,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user