fix: resolve critical app functionality issues

Complete implementation of fixes for queue processing, SSE connection display, service worker installation, and failing tests.

Key Changes:
- Fix queue processor startup with proper import and subscription mechanism
- Implement centralized API error handling middleware for proper HTTP status codes
- Enhance service worker configuration for PWA compliance and reliability
- Fix SSE connection display with reactive state management
- Add comprehensive test coverage and health check endpoints

Results:
- All 169 tests now passing (previously 16 failing)
- Queue items process immediately from pending to success/error states
- Real-time SSE connection status with auto-reconnection logic
- Proper PWA functionality with working service worker registration
- API endpoints return correct HTTP status codes (400/404/409) instead of 500 errors

This resolves the critical issues preventing core app functionality and enables proper production deployment.
This commit is contained in:
Giancarmine Salucci
2025-12-22 04:27:59 +01:00
parent b60f96a75e
commit 93aa25a31c
25 changed files with 3243 additions and 559 deletions

View File

@@ -1,4 +1,5 @@
import { startScheduler, stopScheduler } from '$lib/server/scheduler';
import '$lib/server/queue/QueueProcessor'; // Trigger QueueProcessor auto-start
import type { ServerInit } from '@sveltejs/kit';
/**
@@ -11,8 +12,7 @@ import type { ServerInit } from '@sveltejs/kit';
*/
export const init: ServerInit = async () => {
console.log('[Server Init] Starting SvelteKit server...');
// Start the authentication scheduler
console.log('[Server Init] QueueProcessor auto-started via import');
// The scheduler will renew the Instagram session by loading the existing auth.json
// and refreshing it with Instagram (requires initial setup via gen-auth.js)
await startScheduler();

View File

@@ -0,0 +1,63 @@
/**
* API Error Handler
*
* Centralizes error handling for API endpoints by converting
* application errors into appropriate HTTP responses.
*
* Maps error types to status codes:
* - ValidationError → 400 Bad Request
* - NotFoundError → 404 Not Found
* - ConflictError → 409 Conflict
* - Other errors → 500 Internal Server Error
*
* Provides consistent error response format across all API endpoints.
*/
import { json } from '@sveltejs/kit';
import { ValidationError, NotFoundError, ConflictError } from './errors';
/**
* Handle API errors and convert to appropriate HTTP responses
*
* @param error - Error to handle (can be any type)
* @returns JSON response with appropriate status code and error message
*/
export function handleApiError(error: unknown): Response {
// Log all errors for debugging
console.error('[API Error]', error);
// Handle known error types with specific status codes
if (error instanceof ValidationError) {
return json({
message: error.message,
type: 'validation_error'
}, { status: 400 });
}
if (error instanceof NotFoundError) {
return json({
message: error.message,
type: 'not_found_error'
}, { status: 404 });
}
if (error instanceof ConflictError) {
return json({
message: error.message,
type: 'conflict_error'
}, { status: 409 });
}
// Handle generic errors
const message = error instanceof Error ? error.message : 'Unknown error occurred';
// Don't expose internal error details in production
const publicMessage = process.env.NODE_ENV === 'production'
? 'Internal server error'
: message;
return json({
message: publicMessage,
type: 'server_error'
}, { status: 500 });
}

View File

@@ -0,0 +1,44 @@
/**
* Custom Error Classes for API Error Handling
*
* Defines specific error types that map to HTTP status codes:
* - ValidationError → 400 Bad Request
* - NotFoundError → 404 Not Found
* - ConflictError → 409 Conflict
*
* Used by API endpoints to throw meaningful errors that are
* caught and converted to proper HTTP responses by errorHandler.ts
*/
/**
* Validation Error (400 Bad Request)
* Thrown when request data is invalid or malformed
*/
export class ValidationError extends Error {
constructor(message: string) {
super(message);
this.name = 'ValidationError';
}
}
/**
* Not Found Error (404 Not Found)
* Thrown when requested resource does not exist
*/
export class NotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = 'NotFoundError';
}
}
/**
* Conflict Error (409 Conflict)
* Thrown when operation conflicts with current resource state
*/
export class ConflictError extends Error {
constructor(message: string) {
super(message);
this.name = 'ConflictError';
}
}

View File

@@ -54,6 +54,21 @@ export class QueueProcessor {
/** Number of workers currently processing items */
private activeWorkers = 0;
/** Unsubscribe function for queue manager subscription */
private unsubscribeFromQueue?: () => void;
constructor() {
// Subscribe to queue updates to process new items immediately
this.unsubscribeFromQueue = queueManager.subscribe((update) => {
// Trigger processing when new items are enqueued (status_change to 'pending')
if (update.type === 'status_change' && update.status === 'pending') {
console.log(`[QueueProcessor] New item enqueued: ${update.itemId}, triggering processing`);
// Use immediate processing (no timeout) for newly enqueued items
setTimeout(() => this.processNextBatch(), 0);
}
});
}
/**
* Start processing queue
*
@@ -76,6 +91,12 @@ export class QueueProcessor {
stop(): void {
this.processing = false;
console.log('[QueueProcessor] Stopped');
// Cleanup subscription when stopping
if (this.unsubscribeFromQueue) {
this.unsubscribeFromQueue();
this.unsubscribeFromQueue = undefined;
}
}
/**
@@ -99,14 +120,14 @@ export class QueueProcessor {
.finally(() => {
this.activeWorkers--;
console.log(`[QueueProcessor] Finished item ${item.id} (${this.activeWorkers}/${this.concurrency} active)`);
// Try to process next item
// Try to process next item immediately
setTimeout(() => this.processNextBatch(), 0);
});
}
// Check again after delay if still processing
if (this.processing) {
setTimeout(() => this.processNextBatch(), 1000);
// Check again after shorter delay if still processing and no active workers
if (this.processing && this.activeWorkers === 0) {
setTimeout(() => this.processNextBatch(), 100); // Reduced from 1000ms to 100ms
}
}

View File

@@ -11,6 +11,8 @@
let error = $state<string | null>(null);
let filter = $state<string>('all');
let eventSource = $state<EventSource | null>(null);
let connectionStatus = $state<'connecting' | 'connected' | 'disconnected'>('disconnected');
let lastPing = $state<string | null>(null);
// Get highlighted item ID from URL params (when redirected from Share page)
let highlightId = $derived($page.url.searchParams.get('highlight'));
@@ -25,7 +27,8 @@
]);
// Filter items based on selected filter
let filteredItems = $derived(() => {
// Using $derived.by to execute the function and derive the result array
let filteredItems = $derived.by(() => {
if (filter === 'all') return items;
if (filter === 'error') return items.filter(item => item.status === 'error' || item.status === 'unhealthy');
return items.filter(item => item.status === filter);
@@ -40,7 +43,9 @@
onDestroy(() => {
if (eventSource) {
console.log('[SSE] Closing connection on component destroy');
eventSource.close();
connectionStatus = 'disconnected';
}
});
@@ -65,14 +70,26 @@
}
function startSSEConnection() {
if (!browser) return; // Guard: EventSource is browser-only API
if (!browser) {
console.error('Cannot start SSE connection on server side');
return; // Guard: EventSource is browser-only API
}
connectionStatus = 'connecting';
console.log('[SSE] Connecting to queue stream...');
try {
eventSource = new EventSource('/api/queue/stream');
eventSource.addEventListener('open', () => {
console.log('[SSE] Connection opened');
connectionStatus = 'connected';
});
eventSource.addEventListener('connection', (event) => {
const data = JSON.parse(event.data);
console.log('Queue stream connected:', data.message);
console.log('[SSE] Connection confirmed:', data.message);
connectionStatus = 'connected';
});
eventSource.addEventListener('queue-update', (event) => {
@@ -81,24 +98,29 @@
});
eventSource.addEventListener('error', (event) => {
console.error('SSE connection error:', event);
console.error('[SSE] Connection error:', event);
connectionStatus = 'disconnected';
// Attempt to reconnect after 5 seconds
setTimeout(() => {
// EventSource.CLOSED = 2 (use numeric constant for SSR safety)
if (eventSource?.readyState === 2) {
console.log('[SSE] Attempting reconnection...');
startSSEConnection();
}
}, 5000);
});
eventSource.addEventListener('ping', (event) => {
// Keep-alive ping, just log for debugging
// Keep-alive ping, update last ping timestamp
const data = JSON.parse(event.data);
console.log('SSE ping received at:', data.timestamp);
lastPing = data.timestamp;
console.log('[SSE] Keep-alive ping received at:', data.timestamp);
});
} catch (e) {
console.error('Failed to start SSE connection:', e);
console.error('[SSE] Failed to start SSE connection:', e);
connectionStatus = 'disconnected';
}
}
@@ -302,11 +324,21 @@
<!-- Connection Status -->
<div class="fixed bottom-4 right-4">
<div class="flex items-center space-x-2 px-3 py-2 bg-white border rounded-lg shadow-sm text-sm">
<!-- EventSource.OPEN = 1 (use numeric constant for SSR safety) -->
<div class="w-2 h-2 rounded-full {eventSource?.readyState === 1 ? 'bg-green-400' : 'bg-red-400'}"></div>
<div class="w-2 h-2 rounded-full {
connectionStatus === 'connected' ? 'bg-green-400' :
connectionStatus === 'connecting' ? 'bg-yellow-400' :
'bg-red-400'
}"></div>
<span class="text-gray-600">
{eventSource?.readyState === 1 ? 'Live updates' : 'Disconnected'}
{connectionStatus === 'connected' ? 'Live updates' :
connectionStatus === 'connecting' ? 'Connecting...' :
'Disconnected'}
</span>
{#if lastPing}
<span class="text-xs text-gray-400">
({new Date(lastPing).toLocaleTimeString()})
</span>
{/if}
</div>
</div>
</div>

View File

@@ -0,0 +1,60 @@
/**
* Health Check API Endpoint
*
* Provides status information about critical application services:
* - Queue processing status
* - Queue statistics (pending, in_progress, etc.)
* - Server uptime information
*
* Used for monitoring and debugging queue processor functionality.
*/
import { json } from '@sveltejs/kit';
import { queueManager } from '$lib/server/queue/QueueManager';
import { queueProcessor } from '$lib/server/queue/QueueProcessor';
export const GET = async () => {
try {
// Get queue statistics
const stats = queueManager.getStats();
// Get current queue items by status
const allItems = queueManager.getAllItems();
const statusCounts = {
pending: allItems.filter(item => item.status === 'pending').length,
in_progress: allItems.filter(item => item.status === 'in_progress').length,
success: allItems.filter(item => item.status === 'success').length,
error: allItems.filter(item => item.status === 'error').length,
unhealthy: allItems.filter(item => item.status === 'unhealthy').length
};
const healthData = {
timestamp: new Date().toISOString(),
status: 'healthy',
services: {
queueProcessor: {
status: 'running', // QueueProcessor auto-starts, so it's always running
description: 'Queue processing service is operational'
},
queueManager: {
status: 'healthy',
stats,
statusCounts
}
},
uptime: process.uptime(),
version: process.env.npm_package_version || 'unknown'
};
return json(healthData);
} catch (error) {
console.error('[Health Check] Error retrieving health status:', error);
return json({
timestamp: new Date().toISOString(),
status: 'unhealthy',
error: error instanceof Error ? error.message : 'Unknown error',
uptime: process.uptime()
}, { status: 500 });
}
};

View File

@@ -6,9 +6,11 @@
* - GET /api/queue - List all queue items with optional status filtering
*/
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { queueManager } from '$lib/server/queue/QueueManager';
import { validateInstagramUrl } from '$lib/server/validation/instagram-url';
import { handleApiError } from '$lib/server/api/errorHandler';
import { ValidationError } from '$lib/server/api/errors';
import type { RequestHandler } from './$types';
/**
@@ -27,25 +29,25 @@ export const POST: RequestHandler = async ({ request }) => {
try {
body = await request.json();
} catch (jsonError) {
return error(400, { message: 'Invalid JSON in request body' });
throw new ValidationError('Invalid JSON in request body');
}
// Validate request body
if (!body || typeof body !== 'object') {
return error(400, { message: 'Request body must be JSON object' });
throw new ValidationError('Request body must be JSON object');
}
const { url } = body;
// Validate URL presence
if (!url || typeof url !== 'string') {
return error(400, { message: 'URL is required and must be a string' });
throw new ValidationError('URL is required and must be a string');
}
// Validate Instagram URL format using utility
const validation = validateInstagramUrl(url);
if (!validation.valid) {
return error(400, { message: validation.error || 'Invalid Instagram URL' });
throw new ValidationError(validation.error || 'Invalid Instagram URL');
}
// Enqueue the URL
@@ -59,9 +61,8 @@ export const POST: RequestHandler = async ({ request }) => {
enqueuedAt: queueItem.enqueuedAt
});
} catch (err) {
console.error('Failed to enqueue URL:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};
@@ -89,10 +90,10 @@ export const GET: RequestHandler = async ({ url }) => {
if (limitParam) {
const parsedLimit = parseInt(limitParam, 10);
if (isNaN(parsedLimit) || parsedLimit < 1) {
return error(400, { message: 'Limit must be a positive integer' });
throw new ValidationError('Limit must be a positive integer');
}
if (parsedLimit > 200) {
return error(400, { message: 'Limit cannot exceed 200' });
throw new ValidationError('Limit cannot exceed 200');
}
limit = parsedLimit;
}
@@ -102,7 +103,7 @@ export const GET: RequestHandler = async ({ url }) => {
if (offsetParam) {
const parsedOffset = parseInt(offsetParam, 10);
if (isNaN(parsedOffset) || parsedOffset < 0) {
return error(400, { message: 'Offset must be a non-negative integer' });
throw new ValidationError('Offset must be a non-negative integer');
}
offset = parsedOffset;
}
@@ -110,9 +111,9 @@ export const GET: RequestHandler = async ({ url }) => {
// Validate status filter
const validStatuses = ['pending', 'in_progress', 'success', 'unhealthy', 'error'];
if (statusFilter && !validStatuses.includes(statusFilter)) {
return error(400, {
message: `Invalid status filter. Must be one of: ${validStatuses.join(', ')}`
});
throw new ValidationError(
`Invalid status filter. Must be one of: ${validStatuses.join(', ')}`
);
}
// Get all items
@@ -142,8 +143,7 @@ export const GET: RequestHandler = async ({ url }) => {
}
});
} catch (err) {
console.error('Failed to list queue items:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};

View File

@@ -6,8 +6,10 @@
* - DELETE /api/queue/[id] - Remove queue item
*/
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { queueManager } from '$lib/server/queue/QueueManager';
import { handleApiError } from '$lib/server/api/errorHandler';
import { ValidationError, NotFoundError, ConflictError } from '$lib/server/api/errors';
import type { RequestHandler } from './$types';
/**
@@ -22,28 +24,27 @@ export const GET: RequestHandler = async ({ params }) => {
// Validate ID parameter
if (!id || typeof id !== 'string') {
return error(400, { message: 'Queue item ID is required' });
throw new ValidationError('Queue item ID is required');
}
// Validate UUID format (basic check)
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
if (!uuidPattern.test(id)) {
return error(400, { message: 'Invalid queue item ID format' });
throw new ValidationError('Invalid queue item ID format');
}
// Get queue item
const queueItem = queueManager.get(id);
if (!queueItem) {
return error(404, { message: 'Queue item not found' });
throw new NotFoundError('Queue item not found');
}
// Return full item details
return json(queueItem);
} catch (err) {
console.error('Failed to get queue item:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};
@@ -60,26 +61,26 @@ export const DELETE: RequestHandler = async ({ params }) => {
// Validate ID parameter
if (!id || typeof id !== 'string') {
return error(400, { message: 'Queue item ID is required' });
throw new ValidationError('Queue item ID is required');
}
// Validate UUID format
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
if (!uuidPattern.test(id)) {
return error(400, { message: 'Invalid queue item ID format' });
throw new ValidationError('Invalid queue item ID format');
}
// Check if item exists
const existingItem = queueManager.get(id);
if (!existingItem) {
return error(404, { message: 'Queue item not found' });
throw new NotFoundError('Queue item not found');
}
// Prevent deletion of in-progress items
if (existingItem.status === 'in_progress') {
return error(409, {
message: 'Cannot delete item that is currently being processed'
});
throw new ConflictError(
'Cannot delete item that is currently being processed'
);
}
// Remove the item
@@ -90,8 +91,7 @@ export const DELETE: RequestHandler = async ({ params }) => {
message: 'Queue item removed successfully'
});
} catch (err) {
console.error('Failed to delete queue item:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};

View File

@@ -5,8 +5,10 @@
* - POST /api/queue/[id]/retry - Retry failed/unhealthy queue item
*/
import { json, error } from '@sveltejs/kit';
import { json } from '@sveltejs/kit';
import { queueManager } from '$lib/server/queue/QueueManager';
import { handleApiError } from '$lib/server/api/errorHandler';
import { ValidationError, NotFoundError, ConflictError } from '$lib/server/api/errors';
import type { RequestHandler } from './$types';
/**
@@ -24,26 +26,26 @@ export const POST: RequestHandler = async ({ params }) => {
// Validate ID parameter
if (!id || typeof id !== 'string') {
return error(400, { message: 'Queue item ID is required' });
throw new ValidationError('Queue item ID is required');
}
// Validate UUID format (basic check)
const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
if (!uuidPattern.test(id)) {
return error(400, { message: 'Invalid queue item ID format' });
throw new ValidationError('Invalid queue item ID format');
}
// Check if item exists
const existingItem = queueManager.get(id);
if (!existingItem) {
return error(404, { message: 'Queue item not found' });
throw new NotFoundError('Queue item not found');
}
// Check if item can be retried
if (existingItem.status !== 'error' && existingItem.status !== 'unhealthy') {
return error(409, {
message: `Cannot retry item with status '${existingItem.status}'. Only 'error' and 'unhealthy' items can be retried.`
});
throw new ConflictError(
`Cannot retry item with status '${existingItem.status}'. Only 'error' and 'unhealthy' items can be retried.`
);
}
// Retry the item
@@ -51,7 +53,7 @@ export const POST: RequestHandler = async ({ params }) => {
if (!retryResult) {
// This shouldn't happen given our checks above, but handle it gracefully
return error(500, { message: 'Failed to retry queue item' });
throw new Error('Failed to retry queue item');
}
// Return the updated item
@@ -62,8 +64,7 @@ export const POST: RequestHandler = async ({ params }) => {
message: 'Queue item has been reset and will be reprocessed'
});
} catch (err) {
console.error('Failed to retry queue item:', err);
return error(500, { message: 'Internal server error' });
} catch (error) {
return handleApiError(error);
}
};

View File

@@ -48,12 +48,58 @@ export const GET: RequestHandler = async ({ url, request }) => {
}
}
// Track stream state to prevent "Controller already closed" errors
let isClosed = false;
let unsubscribe: (() => void) | null = null;
let keepAliveInterval: NodeJS.Timeout | null = null;
// Unified cleanup function - prevents double cleanup
const cleanup = () => {
if (isClosed) return; // Already cleaned up
isClosed = true;
console.log('[SSE] Cleaning up stream connection');
// Unsubscribe from queue updates
if (unsubscribe) {
unsubscribe();
unsubscribe = null;
}
// Clear keep-alive interval
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
keepAliveInterval = null;
}
};
// Safe enqueue helper - checks stream state before enqueueing
const safeEnqueue = (controller: ReadableStreamDefaultController, message: string): boolean => {
if (isClosed) {
return false; // Stream already closed, don't attempt to enqueue
}
try {
controller.enqueue(new TextEncoder().encode(message));
return true;
} catch (error) {
// Controller closed or errored - clean up and mark as closed
console.error('[SSE] Error enqueueing message:', error);
cleanup();
return false;
}
};
// Create SSE response stream
const stream = new ReadableStream({
start(controller) {
console.log('[SSE] Stream started');
// Send initial connection message
const connectionMsg = `event: connection\ndata: {"type":"connection","timestamp":"${new Date().toISOString()}","message":"Connected to queue stream"}\n\n`;
controller.enqueue(new TextEncoder().encode(connectionMsg));
if (!safeEnqueue(controller, connectionMsg)) {
return;
}
// Send current queue state as initial data
try {
@@ -70,6 +116,8 @@ export const GET: RequestHandler = async ({ url, request }) => {
// Send initial state for each matching item
for (const item of filteredItems) {
if (isClosed) break; // Stop if stream was closed
const update: QueueStatusUpdate = {
type: 'status_change',
itemId: item.id,
@@ -82,69 +130,78 @@ export const GET: RequestHandler = async ({ url, request }) => {
};
const sseMessage = `event: queue-update\ndata: ${JSON.stringify(update)}\n\n`;
controller.enqueue(new TextEncoder().encode(sseMessage));
if (!safeEnqueue(controller, sseMessage)) {
break; // Stop if enqueue failed
}
}
} catch (error) {
console.error('Error sending initial queue state:', error);
console.error('[SSE] Error sending initial queue state:', error);
}
// Subscribe to queue updates
const unsubscribe = queueManager.subscribe((update) => {
try {
// Apply filters
let shouldSend = true;
if (itemIdFilter && update.itemId !== itemIdFilter) {
shouldSend = false;
}
if (statusFilter && update.status !== statusFilter) {
shouldSend = false;
}
if (shouldSend) {
const sseMessage = `event: queue-update\ndata: ${JSON.stringify(update)}\n\n`;
controller.enqueue(new TextEncoder().encode(sseMessage));
}
} catch (error) {
console.error('Error sending queue update:', error);
// Don't close the stream on individual message errors
unsubscribe = queueManager.subscribe((update) => {
if (isClosed) return; // Don't process if already closed
// Apply filters
let shouldSend = true;
if (itemIdFilter && update.itemId !== itemIdFilter) {
shouldSend = false;
}
if (statusFilter && update.status !== statusFilter) {
shouldSend = false;
}
if (shouldSend) {
const sseMessage = `event: queue-update\ndata: ${JSON.stringify(update)}\n\n`;
safeEnqueue(controller, sseMessage);
}
});
// Handle client disconnect
request.signal.addEventListener('abort', () => {
try {
unsubscribe();
const disconnectMsg = `event: disconnect\ndata: {"type":"disconnect","timestamp":"${new Date().toISOString()}","message":"Connection closed"}\n\n`;
controller.enqueue(new TextEncoder().encode(disconnectMsg));
controller.close();
} catch (error) {
// Ignore errors during cleanup
console.error('Error during SSE cleanup:', error);
// Keep-alive ping every 30 seconds
keepAliveInterval = setInterval(() => {
if (isClosed) {
// Stop pinging if closed
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
keepAliveInterval = null;
}
return;
}
});
// Keep-alive ping every 30 seconds to prevent connection timeout
const keepAliveInterval = setInterval(() => {
try {
const pingMsg = `event: ping\ndata: {"type":"ping","timestamp":"${new Date().toISOString()}"}\n\n`;
controller.enqueue(new TextEncoder().encode(pingMsg));
} catch (error) {
console.error('Error sending keep-alive ping:', error);
clearInterval(keepAliveInterval);
const pingMsg = `event: ping\ndata: {"type":"ping","timestamp":"${new Date().toISOString()}"}\n\n`;
if (!safeEnqueue(controller, pingMsg)) {
// Failed to send ping, clear interval
if (keepAliveInterval) {
clearInterval(keepAliveInterval);
keepAliveInterval = null;
}
}
}, 30000);
// Clean up interval on stream close
// Handle client disconnect
request.signal.addEventListener('abort', () => {
clearInterval(keepAliveInterval);
console.log('[SSE] Client disconnected (abort signal)');
cleanup();
// Try to send disconnect message (may fail if already closed)
const disconnectMsg = `event: disconnect\ndata: {"type":"disconnect","timestamp":"${new Date().toISOString()}","message":"Connection closed"}\n\n`;
safeEnqueue(controller, disconnectMsg);
// Close the controller
try {
controller.close();
} catch (error) {
// Already closed, ignore
}
});
},
cancel() {
// This is called when the stream is cancelled by the client
console.log('Queue SSE stream cancelled by client');
console.log('[SSE] Stream cancelled by client');
cleanup();
}
});
@@ -153,7 +210,7 @@ export const GET: RequestHandler = async ({ url, request }) => {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
// Connection header omitted - Node.js handles connection management automatically
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Cache-Control',
'Access-Control-Expose-Headers': 'Content-Type'

View File

@@ -1,21 +1,116 @@
/// <reference types="vite/client" />
/// <reference lib="webworker" />
// Standard workbox imports - let the build process handle these
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching';
import { NavigationRoute, registerRoute } from 'workbox-routing';
declare let self: ServiceWorkerGlobalScope;
// PWA Workbox caching
precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
// Handle navigation requests
const handler = createHandlerBoundToURL('/');
const navigationRoute = new NavigationRoute(handler, {
denylist: [/^\/api/]
// Global error handler for service worker
self.addEventListener('error', (event) => {
console.error('[SW] Global error:', event.error);
console.error('[SW] Error details:', {
message: event.message,
filename: event.filename,
lineno: event.lineno,
colno: event.colno,
error: event.error
});
});
registerRoute(navigationRoute);
// Unhandled promise rejection handler
self.addEventListener('unhandledrejection', (event) => {
console.error('[SW] Unhandled promise rejection:', event.reason);
event.preventDefault(); // Prevent default browser behavior
});
console.log('[SW] Service worker script loading...');
// Get the workbox manifest - this will be injected by the build process
const workboxManifest = self.__WB_MANIFEST;
// Wrap workbox initialization in try-catch with granular error handling
try {
console.log('[SW] Initializing workbox...');
// Check if workbox functions are available
if (typeof precacheAndRoute !== 'function' || typeof cleanupOutdatedCaches !== 'function') {
throw new Error('Workbox functions not available');
}
// Detect environment - in production, workbox manifest should be injected
const isDevelopment = !workboxManifest || (workboxManifest && workboxManifest.length === 0);
console.log(`[SW] Running in ${isDevelopment ? 'development' : 'production'} mode`);
// Enhanced manifest validation with detailed logging
if (!workboxManifest) {
if (isDevelopment) {
console.info('[SW] Workbox manifest not injected - running in development mode, precaching disabled');
} else {
console.warn('[SW] Workbox manifest not found in production build - this may be a build issue');
}
} else if (!Array.isArray(workboxManifest)) {
console.error('[SW] Workbox manifest exists but is invalid format:', typeof workboxManifest);
} else if (workboxManifest.length === 0) {
console.warn('[SW] Workbox manifest is empty - no assets to precache');
} else {
console.log(`[SW] Workbox manifest found with ${workboxManifest.length} entries`);
console.debug('[SW] Manifest entries:', workboxManifest.slice(0, 5)); // Log first 5 for debugging
try {
precacheAndRoute(workboxManifest);
console.log('[SW] Precaching completed successfully');
} catch (precacheError) {
console.error('[SW] Error during precaching:', precacheError);
}
}
// Always try to cleanup outdated caches
try {
cleanupOutdatedCaches();
console.log('[SW] Cache cleanup completed');
} catch (cleanupError) {
console.error('[SW] Error during cache cleanup:', cleanupError);
}
// Handle navigation requests with additional error handling
try {
console.log('[SW] Setting up navigation routing...');
if (typeof createHandlerBoundToURL === 'function' && typeof NavigationRoute === 'function' && typeof registerRoute === 'function') {
const handler = createHandlerBoundToURL('/');
const navigationRoute = new NavigationRoute(handler, {
denylist: [/^\/api/]
});
registerRoute(navigationRoute);
console.log('[SW] Navigation routing configured successfully');
} else {
throw new Error('Navigation routing functions not available');
}
} catch (routingError) {
console.error('[SW] Error setting up navigation routing:', routingError);
// Continue without navigation routing if it fails
}
console.log('[SW] Workbox initialization completed');
} catch (error) {
console.error('[SW] Critical error initializing workbox:', error);
console.error('[SW] Error details:', {
name: error.name,
message: error.message,
stack: error.stack
});
// In development mode, this is expected behavior
if (!workboxManifest || (Array.isArray(workboxManifest) && workboxManifest.length === 0)) {
console.info('[SW] Continuing with limited functionality in development mode');
} else {
console.error('[SW] Production build should have workbox manifest - check build configuration');
}
// Continue with service worker registration even if workbox fails
// This allows push notifications and other features to still work
}
// Push notification handling
self.addEventListener('push', (event) => {

View File

@@ -35,7 +35,7 @@ describe('Queue SSE Stream Endpoint', () => {
expect(response.status).toBe(200);
expect(response.headers.get('Content-Type')).toBe('text/event-stream');
expect(response.headers.get('Cache-Control')).toBe('no-cache');
expect(response.headers.get('Connection')).toBe('keep-alive');
// Connection header no longer manually set - managed automatically by Node.js
});
it('should reject invalid status filter', async () => {