UNPKG

@api.global/typedserver

Version:

A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.

441 lines (440 loc) 15.1 kB
import * as plugins from './plugins.js'; export interface CacheStorage { keys: () => Promise<string[]>; match: any; open: any; delete: any; } export declare var caches: CacheStorage; export interface IMessage_Serviceworker_Client_UpdateInfo extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_Client_UpdateInfo> { method: 'serviceworker_newVersion'; request: { appVersion: string; appHash: string; }; response: {}; } export interface IMessage_Serviceworker_Client_RequestReload extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_Client_RequestReload> { method: 'serviceworker_requestReload'; request: {}; response: {}; } export interface IRequest_Serviceworker_Backend_VersionInfo extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_Backend_VersionInfo> { method: 'serviceworker_versionInfo'; request: {}; response: { appHash: string; appSemVer: string; }; } /** * purges the service workers cache */ export interface IRequest_PurgeServiceWorkerCache extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_PurgeServiceWorkerCache> { method: 'purgeServiceWorkerCache'; request: {}; response: {}; } /** * updates the info in all connected tabs */ export interface IMessage_Serviceworker_Client_UpdateInfo extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_Client_UpdateInfo> { method: 'serviceworker_newVersion'; request: { appVersion: string; appHash: string; }; response: {}; } /** * requests all clients to reload */ export interface IMessage_Serviceworker_Client_RequestReload extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_Client_RequestReload> { method: 'serviceworker_requestReload'; request: {}; response: {}; } /** * updates version infos */ export interface IRequest_Serviceworker_Backend_VersionInfo extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_Backend_VersionInfo> { method: 'serviceworker_versionInfo'; request: {}; response: { appHash: string; appSemVer: string; }; } /** * ensures a stable connection between clients and the serviceworker */ export interface IRequest_Client_Serviceworker_ConnectionPolling extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Client_Serviceworker_ConnectionPolling> { method: 'broadcastConnectionPolling'; request: { tabId: string; }; response: { serviceworkerId: string; }; } /** * Request to get service worker metrics */ export interface IRequest_Serviceworker_Metrics extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_Metrics> { method: 'serviceworker_metrics'; request: {}; response: { cache: { hits: number; misses: number; errors: number; bytesServedFromCache: number; bytesFetched: number; averageResponseTime: number; }; network: { totalRequests: number; successfulRequests: number; failedRequests: number; timeouts: number; averageLatency: number; totalBytesTransferred: number; }; update: { totalChecks: number; successfulChecks: number; failedChecks: number; updatesFound: number; updatesApplied: number; lastCheckTimestamp: number; lastUpdateTimestamp: number; }; connection: { connectedClients: number; totalConnectionAttempts: number; successfulConnections: number; failedConnections: number; }; startTime: number; uptime: number; }; } /** * Result of a service worker connection attempt */ export interface IConnectionResult { connected: boolean; error?: string; attempts?: number; duration?: number; } /** * Cache invalidation request from server to service worker */ export interface IRequest_Serviceworker_CacheInvalidate extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_CacheInvalidate> { method: 'serviceworker_cacheInvalidate'; request: { reason: string; timestamp: number; }; response: { success: boolean; }; } /** * Speedtest request between service worker and backend * * Types: * - 'latency': Simple ping to measure round-trip time * - 'download_chunk': Request a chunk of data (64KB default) * - 'upload_chunk': Send a chunk of data to server * * The client runs a loop calling download_chunk or upload_chunk * until the desired test duration (e.g., 5 seconds) elapses. */ export interface IRequest_Serviceworker_Speedtest extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_Speedtest> { method: 'serviceworker_speedtest'; request: { type: 'latency' | 'download_chunk' | 'upload_chunk'; chunkSizeKB?: number; payload?: string; }; response: { bytesTransferred: number; timestamp: number; payload?: string; }; } /** * Status update source types */ export type TStatusSource = 'backend' | 'serviceworker' | 'network'; /** * Status update event types */ export type TStatusType = 'connected' | 'disconnected' | 'reconnecting' | 'update' | 'cache' | 'error' | 'offline' | 'online'; /** * Status update details */ export interface IStatusDetails { version?: string; cacheHitRate?: number; resourceCount?: number; connectionType?: string; latencyMs?: number; message?: string; } /** * Status update payload sent from SW to clients */ export interface IStatusUpdate { source: TStatusSource; type: TStatusType; message: string; details?: IStatusDetails; persist?: boolean; timestamp: number; } /** * Message for status updates from service worker to clients */ export interface IMessage_Serviceworker_StatusUpdate extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_StatusUpdate> { method: 'serviceworker_statusUpdate'; request: IStatusUpdate; response: {}; } /** * Request to get current service worker status */ export interface IRequest_Serviceworker_GetStatus extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetStatus> { method: 'serviceworker_getStatus'; request: {}; response: { isActive: boolean; isOnline: boolean; version?: string; cacheHitRate: number; resourceCount: number; connectionType?: string; connectedClients: number; lastUpdateCheck: number; }; } /** * Event types for the persistent event log */ export type TEventType = 'sw_installed' | 'sw_activated' | 'sw_updated' | 'sw_stopped' | 'speedtest_started' | 'speedtest_completed' | 'speedtest_failed' | 'backend_connected' | 'backend_disconnected' | 'cache_invalidated' | 'network_online' | 'network_offline' | 'update_check' | 'error'; /** * Event log entry structure * Survives both SW restarts AND cache invalidation */ export interface IEventLogEntry { id: string; timestamp: number; type: TEventType; message: string; details?: Record<string, any>; } /** * Cumulative metrics that persist across SW restarts * Reset on cache invalidation */ export interface ICumulativeMetrics { firstSeenTimestamp: number; totalCacheHits: number; totalCacheMisses: number; totalCacheErrors: number; totalBytesServedFromCache: number; totalBytesFetched: number; totalNetworkRequests: number; totalNetworkSuccesses: number; totalNetworkFailures: number; totalNetworkTimeouts: number; totalBytesTransferred: number; totalUpdateChecks: number; totalUpdatesApplied: number; totalSpeedtests: number; swRestartCount: number; lastUpdatedTimestamp: number; } /** * Request to get event log from service worker */ export interface IRequest_Serviceworker_GetEventLog extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetEventLog> { method: 'serviceworker_getEventLog'; request: { limit?: number; type?: TEventType; since?: number; before?: number; }; response: { events: IEventLogEntry[]; totalCount: number; }; } /** * Request to get cumulative metrics from service worker */ export interface IRequest_Serviceworker_GetCumulativeMetrics extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetCumulativeMetrics> { method: 'serviceworker_getCumulativeMetrics'; request: {}; response: ICumulativeMetrics; } /** * Request to clear event log */ export interface IRequest_Serviceworker_ClearEventLog extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_ClearEventLog> { method: 'serviceworker_clearEventLog'; request: {}; response: { success: boolean; }; } /** * Request to get event count since a timestamp */ export interface IRequest_Serviceworker_GetEventCount extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetEventCount> { method: 'serviceworker_getEventCount'; request: { since: number; }; response: { count: number; }; } /** * Push notification when a new event is logged * Sent via DeesComms BroadcastChannel */ export interface IMessage_Serviceworker_EventLogged extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_EventLogged> { method: 'serviceworker_eventLogged'; request: IEventLogEntry; response: {}; } /** * Metrics snapshot for push updates */ export interface IMetricsSnapshot { cache: { hits: number; misses: number; errors: number; bytesServedFromCache: number; bytesFetched: number; }; network: { totalRequests: number; successfulRequests: number; failedRequests: number; }; cacheHitRate: number; networkSuccessRate: number; resourceCount: number; uptime: number; timestamp: number; } /** * Push notification for metrics updates * Sent via DeesComms BroadcastChannel (throttled) */ export interface IMessage_Serviceworker_MetricsUpdate extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_MetricsUpdate> { method: 'serviceworker_metricsUpdate'; request: IMetricsSnapshot; response: {}; } /** * Push notification when a new resource is cached */ export interface IMessage_Serviceworker_ResourceCached extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_ResourceCached> { method: 'serviceworker_resourceCached'; request: { url: string; contentType: string; size: number; cached: boolean; }; response: {}; } /** * Log entry for TypedRequest traffic monitoring */ export interface ITypedRequestLogEntry { correlationId: string; method: string; direction: 'outgoing' | 'incoming'; phase: 'request' | 'response'; timestamp: number; durationMs?: number; payload: any; error?: string; } /** * Statistics for TypedRequest traffic */ export interface ITypedRequestStats { totalRequests: number; totalResponses: number; methodCounts: Record<string, { requests: number; responses: number; errors: number; avgDurationMs: number; }>; errorCount: number; avgDurationMs: number; } /** * Message for logging TypedRequest traffic from client to SW */ export interface IMessage_Serviceworker_TypedRequestLog extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_TypedRequestLog> { method: 'serviceworker_typedRequestLog'; request: ITypedRequestLogEntry; response: {}; } /** * Push notification when a TypedRequest is logged */ export interface IMessage_Serviceworker_TypedRequestLogged extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IMessage_Serviceworker_TypedRequestLogged> { method: 'serviceworker_typedRequestLogged'; request: ITypedRequestLogEntry; response: {}; } /** * Request to get TypedRequest logs */ export interface IRequest_Serviceworker_GetTypedRequestLogs extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetTypedRequestLogs> { method: 'serviceworker_getTypedRequestLogs'; request: { limit?: number; method?: string; since?: number; before?: number; }; response: { logs: ITypedRequestLogEntry[]; totalCount: number; }; } /** * Request to get TypedRequest traffic statistics */ export interface IRequest_Serviceworker_GetTypedRequestStats extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_GetTypedRequestStats> { method: 'serviceworker_getTypedRequestStats'; request: {}; response: ITypedRequestStats; } /** * Request to clear TypedRequest logs */ export interface IRequest_Serviceworker_ClearTypedRequestLogs extends plugins.typedrequestInterfaces.implementsTR<plugins.typedrequestInterfaces.ITypedRequest, IRequest_Serviceworker_ClearTypedRequestLogs> { method: 'serviceworker_clearTypedRequestLogs'; request: {}; response: { success: boolean; }; } /** * HTML shell for the SW Dashboard - shared between server and service worker */ export declare const SW_DASH_HTML = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>SW Dashboard</title>\n <style>\n * { margin: 0; padding: 0; box-sizing: border-box; }\n body { background: #0a0a0a; min-height: 100vh; }\n </style>\n</head>\n<body>\n <sw-dash-app></sw-dash-app>\n <script type=\"module\" src=\"/sw-dash/bundle.js\"></script>\n</body>\n</html>";