blaizejs 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-LI53MJIF.js → chunk-2LP25IUP.js} +3 -3
- package/dist/{chunk-N7F6OHDX.js → chunk-6A3MHG3V.js} +3 -3
- package/dist/{chunk-LMTJAVHX.js → chunk-6XBGCGAR.js} +3 -3
- package/dist/{chunk-ORFAFXHX.js → chunk-J7KS32ZT.js} +3 -3
- package/dist/chunk-QD45PRU4.js +11 -0
- package/dist/chunk-QD45PRU4.js.map +1 -0
- package/dist/index.cjs +22 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +932 -3
- package/dist/index.d.ts +932 -3
- package/dist/index.js +22 -9
- package/dist/index.js.map +1 -1
- package/dist/{internal-server-error-DAEFNRNG.js → internal-server-error-BDHJW7WB.js} +3 -3
- package/dist/{payload-too-large-error-YO3MSQJF.js → payload-too-large-error-PX6RP7T6.js} +3 -3
- package/dist/{unsupported-media-type-error-GW2UC37Q.js → unsupported-media-type-error-IXHPPRCO.js} +3 -3
- package/dist/{validation-error-SJTLEIZZ.js → validation-error-FNJKIDG6.js} +3 -3
- package/package.json +4 -4
- package/dist/chunk-GP5NAYQS.js +0 -11
- package/dist/chunk-GP5NAYQS.js.map +0 -1
- /package/dist/{chunk-LI53MJIF.js.map → chunk-2LP25IUP.js.map} +0 -0
- /package/dist/{chunk-N7F6OHDX.js.map → chunk-6A3MHG3V.js.map} +0 -0
- /package/dist/{chunk-LMTJAVHX.js.map → chunk-6XBGCGAR.js.map} +0 -0
- /package/dist/{chunk-ORFAFXHX.js.map → chunk-J7KS32ZT.js.map} +0 -0
- /package/dist/{internal-server-error-DAEFNRNG.js.map → internal-server-error-BDHJW7WB.js.map} +0 -0
- /package/dist/{payload-too-large-error-YO3MSQJF.js.map → payload-too-large-error-PX6RP7T6.js.map} +0 -0
- /package/dist/{unsupported-media-type-error-GW2UC37Q.js.map → unsupported-media-type-error-IXHPPRCO.js.map} +0 -0
- /package/dist/{validation-error-SJTLEIZZ.js.map → validation-error-FNJKIDG6.js.map} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -181,6 +181,8 @@ declare enum ErrorType {
|
|
|
181
181
|
UNAUTHORIZED = "UNAUTHORIZED",
|
|
182
182
|
/** Access forbidden (403) */
|
|
183
183
|
FORBIDDEN = "FORBIDDEN",
|
|
184
|
+
/** SSE Not Acceptable (406) */
|
|
185
|
+
SSE_NOT_ACCEPTABLE = "SSE_NOT_ACCEPTABLE",
|
|
184
186
|
/** Resource conflict (409) */
|
|
185
187
|
CONFLICT = "CONFLICT",
|
|
186
188
|
/** Rate limit exceeded (429) */
|
|
@@ -202,7 +204,13 @@ declare enum ErrorType {
|
|
|
202
204
|
/** Response parsing failure (0) */
|
|
203
205
|
PARSE_ERROR = "PARSE_ERROR",
|
|
204
206
|
/** Generic HTTP error (varies) */
|
|
205
|
-
HTTP_ERROR = "HTTP_ERROR"
|
|
207
|
+
HTTP_ERROR = "HTTP_ERROR",
|
|
208
|
+
/** SSE connection failed (502) */
|
|
209
|
+
SSE_CONNECTION_ERROR = "SSE_CONNECTION_ERROR",
|
|
210
|
+
/** SSE buffer overflow (503) */
|
|
211
|
+
SSE_BUFFER_OVERFLOW = "SSE_BUFFER_OVERFLOW",
|
|
212
|
+
/** SSE stream closed (410) */
|
|
213
|
+
SSE_STREAM_CLOSED = "SSE_STREAM_CLOSED"
|
|
206
214
|
}
|
|
207
215
|
/**
|
|
208
216
|
* Error severity levels for logging and monitoring
|
|
@@ -357,7 +365,7 @@ interface ForbiddenErrorDetails {
|
|
|
357
365
|
/** Action being attempted */
|
|
358
366
|
action?: string;
|
|
359
367
|
/** Reason for access denial */
|
|
360
|
-
reason?: 'insufficient_permissions' | 'account_suspended' | 'resource_locked' | string;
|
|
368
|
+
reason?: 'insufficient_permissions' | 'account_suspended' | 'resource_locked' | 'origin_not_allowed' | string;
|
|
361
369
|
/** Additional context */
|
|
362
370
|
[key: string]: unknown;
|
|
363
371
|
}
|
|
@@ -492,6 +500,115 @@ interface ErrorTransformContext {
|
|
|
492
500
|
responseSample?: string;
|
|
493
501
|
[key: string]: unknown;
|
|
494
502
|
}
|
|
503
|
+
/**
|
|
504
|
+
* SSE-specific error detail interfaces for BlaizeJS framework
|
|
505
|
+
*
|
|
506
|
+
* These interfaces define the structure of details for SSE errors.
|
|
507
|
+
* The actual error classes are implemented in blaize-core.
|
|
508
|
+
*/
|
|
509
|
+
/**
|
|
510
|
+
* Details for SSE connection errors
|
|
511
|
+
*/
|
|
512
|
+
interface SSEConnectionErrorDetails {
|
|
513
|
+
/** Client identifier if available */
|
|
514
|
+
clientId?: string;
|
|
515
|
+
/** Connection attempt number */
|
|
516
|
+
attemptNumber?: number;
|
|
517
|
+
/** Maximum retry attempts configured */
|
|
518
|
+
maxRetries?: number;
|
|
519
|
+
/** The underlying error that caused connection failure */
|
|
520
|
+
cause?: string;
|
|
521
|
+
/** Suggested resolution */
|
|
522
|
+
suggestion?: string;
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Details for SSE buffer overflow errors
|
|
526
|
+
*/
|
|
527
|
+
interface SSEBufferOverflowErrorDetails {
|
|
528
|
+
/** Client identifier */
|
|
529
|
+
clientId?: string;
|
|
530
|
+
/** Current buffer size when overflow occurred */
|
|
531
|
+
currentSize: number;
|
|
532
|
+
/** Maximum buffer size configured */
|
|
533
|
+
maxSize: number;
|
|
534
|
+
/** Number of events dropped */
|
|
535
|
+
eventsDropped?: number;
|
|
536
|
+
/** Buffer strategy that was applied */
|
|
537
|
+
strategy: 'drop-oldest' | 'drop-newest' | 'close';
|
|
538
|
+
/** Event that triggered the overflow */
|
|
539
|
+
triggeringEvent?: string;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Details for SSE stream closed errors
|
|
543
|
+
*/
|
|
544
|
+
interface SSEStreamClosedErrorDetails {
|
|
545
|
+
/** Client identifier */
|
|
546
|
+
clientId?: string;
|
|
547
|
+
/** When the stream was closed */
|
|
548
|
+
closedAt?: string;
|
|
549
|
+
/** Reason for closure */
|
|
550
|
+
closeReason?: 'client-disconnect' | 'server-close' | 'timeout' | 'error' | 'buffer-overflow';
|
|
551
|
+
/** Whether reconnection is possible */
|
|
552
|
+
canReconnect?: boolean;
|
|
553
|
+
/** Suggested retry interval in milliseconds */
|
|
554
|
+
retryAfter?: number;
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Context for SSE connection errors
|
|
558
|
+
*/
|
|
559
|
+
interface SSEConnectionErrorContext {
|
|
560
|
+
/** The SSE endpoint URL */
|
|
561
|
+
url: string;
|
|
562
|
+
/** Correlation ID for tracing */
|
|
563
|
+
correlationId: string;
|
|
564
|
+
/** Connection state when error occurred */
|
|
565
|
+
state: 'connecting' | 'connected' | 'disconnected' | 'closed';
|
|
566
|
+
/** Number of reconnection attempts made */
|
|
567
|
+
reconnectAttempts?: number;
|
|
568
|
+
/** The original error if available */
|
|
569
|
+
originalError?: Error;
|
|
570
|
+
/** Additional SSE-specific details */
|
|
571
|
+
sseDetails?: {
|
|
572
|
+
/** Whether credentials were included */
|
|
573
|
+
withCredentials?: boolean;
|
|
574
|
+
/** Last received event ID */
|
|
575
|
+
lastEventId?: string;
|
|
576
|
+
/** EventSource ready state */
|
|
577
|
+
readyState?: number;
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Context for SSE stream errors (server-sent errors)
|
|
582
|
+
*/
|
|
583
|
+
interface SSEStreamErrorContext {
|
|
584
|
+
/** The SSE endpoint URL */
|
|
585
|
+
url: string;
|
|
586
|
+
/** Correlation ID from server or client */
|
|
587
|
+
correlationId: string;
|
|
588
|
+
/** Error message from server */
|
|
589
|
+
message: string;
|
|
590
|
+
/** Error code if provided */
|
|
591
|
+
code?: string;
|
|
592
|
+
/** Error name/type from server */
|
|
593
|
+
name?: string;
|
|
594
|
+
/** Raw error data from server */
|
|
595
|
+
rawData?: any;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* Context for SSE heartbeat timeout errors
|
|
599
|
+
*/
|
|
600
|
+
interface SSEHeartbeatErrorContext {
|
|
601
|
+
/** The SSE endpoint URL */
|
|
602
|
+
url: string;
|
|
603
|
+
/** Correlation ID for tracing */
|
|
604
|
+
correlationId: string;
|
|
605
|
+
/** Configured heartbeat timeout in ms */
|
|
606
|
+
heartbeatTimeout: number;
|
|
607
|
+
/** Time since last event in ms */
|
|
608
|
+
timeSinceLastEvent?: number;
|
|
609
|
+
/** Last event ID received */
|
|
610
|
+
lastEventId?: string;
|
|
611
|
+
}
|
|
495
612
|
|
|
496
613
|
/**
|
|
497
614
|
* Represents an uploaded file in a multipart/form-data request
|
|
@@ -1276,6 +1393,438 @@ declare function stateMiddleware<T = {}>(handler: MiddlewareFunction): Middlewar
|
|
|
1276
1393
|
*/
|
|
1277
1394
|
declare function serviceMiddleware<T = {}>(handler: MiddlewareFunction): Middleware<{}, T>;
|
|
1278
1395
|
|
|
1396
|
+
/**
|
|
1397
|
+
* Represents a single Server-Sent Event
|
|
1398
|
+
* @template T - The type of data payload
|
|
1399
|
+
*
|
|
1400
|
+
* @example
|
|
1401
|
+
* ```typescript
|
|
1402
|
+
* const event: SSEEvent<{ message: string }> = {
|
|
1403
|
+
* id: '123',
|
|
1404
|
+
* event: 'message',
|
|
1405
|
+
* data: { message: 'Hello, world!' },
|
|
1406
|
+
* retry: 5000
|
|
1407
|
+
* };
|
|
1408
|
+
* ```
|
|
1409
|
+
*/
|
|
1410
|
+
interface SSEEvent<T = unknown> {
|
|
1411
|
+
/** Unique identifier for the event */
|
|
1412
|
+
id: string;
|
|
1413
|
+
/** Event type/name for client-side event listeners */
|
|
1414
|
+
event: string;
|
|
1415
|
+
/** The actual data payload of the event */
|
|
1416
|
+
data: T;
|
|
1417
|
+
/** Optional retry interval in milliseconds for reconnection */
|
|
1418
|
+
retry?: number;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Backpressure handling strategies for SSE streams
|
|
1422
|
+
*
|
|
1423
|
+
* - `drop-oldest`: Remove oldest events from buffer when full
|
|
1424
|
+
* - `drop-newest`: Reject new events when buffer is full
|
|
1425
|
+
* - `close`: Close the stream when buffer limit is reached
|
|
1426
|
+
*/
|
|
1427
|
+
type SSEBufferStrategy = 'drop-oldest' | 'drop-newest' | 'close';
|
|
1428
|
+
/**
|
|
1429
|
+
* Configuration options for SSE streams
|
|
1430
|
+
*
|
|
1431
|
+
* @example
|
|
1432
|
+
* ```typescript
|
|
1433
|
+
* const options: SSEOptions = {
|
|
1434
|
+
* autoClose: true,
|
|
1435
|
+
* maxBufferSize: 100,
|
|
1436
|
+
* bufferStrategy: 'drop-oldest'
|
|
1437
|
+
* };
|
|
1438
|
+
* ```
|
|
1439
|
+
*/
|
|
1440
|
+
interface SSEOptions {
|
|
1441
|
+
/** ms between heartbeat pings (0 to disable) */
|
|
1442
|
+
heartbeatInterval?: number;
|
|
1443
|
+
/** Maximum size in bytes for a single event */
|
|
1444
|
+
maxEventSize?: number;
|
|
1445
|
+
/** Automatically close stream when client disconnects */
|
|
1446
|
+
autoClose?: boolean;
|
|
1447
|
+
/** Maximum number of events to buffer before applying strategy */
|
|
1448
|
+
maxBufferSize?: number;
|
|
1449
|
+
/** Strategy to handle buffer overflow conditions */
|
|
1450
|
+
bufferStrategy?: SSEBufferStrategy;
|
|
1451
|
+
}
|
|
1452
|
+
/**
|
|
1453
|
+
* Connection states for SSE streams
|
|
1454
|
+
*/
|
|
1455
|
+
type SSEConnectionState = 'connecting' | 'connected' | 'disconnected' | 'closed';
|
|
1456
|
+
/**
|
|
1457
|
+
* SSE stream interface for managing server-sent events
|
|
1458
|
+
*
|
|
1459
|
+
* @example
|
|
1460
|
+
* ```typescript
|
|
1461
|
+
* const stream: SSEStream = createSSEStream(response);
|
|
1462
|
+
*
|
|
1463
|
+
* // Send typed event
|
|
1464
|
+
* stream.send('notification', { type: 'info', message: 'Update available' });
|
|
1465
|
+
*
|
|
1466
|
+
* // Send error event
|
|
1467
|
+
* stream.sendError(new Error('Processing failed'));
|
|
1468
|
+
*
|
|
1469
|
+
* // Clean up on close
|
|
1470
|
+
* stream.onClose(() => {
|
|
1471
|
+
* console.log('Client disconnected');
|
|
1472
|
+
* });
|
|
1473
|
+
*
|
|
1474
|
+
* // Close stream
|
|
1475
|
+
* stream.close();
|
|
1476
|
+
* ```
|
|
1477
|
+
*/
|
|
1478
|
+
interface SSEStream {
|
|
1479
|
+
/**
|
|
1480
|
+
* Send an event with typed data to the client
|
|
1481
|
+
* @template T - Type of the data payload
|
|
1482
|
+
* @param event - Event name/type
|
|
1483
|
+
* @param data - Event data payload
|
|
1484
|
+
*/
|
|
1485
|
+
send<T>(event: string, data: T): void;
|
|
1486
|
+
/**
|
|
1487
|
+
* Send an error event to the client
|
|
1488
|
+
* @param error - Error object to send
|
|
1489
|
+
*/
|
|
1490
|
+
sendError(error: Error): void;
|
|
1491
|
+
/**
|
|
1492
|
+
* Close the SSE stream connection
|
|
1493
|
+
*/
|
|
1494
|
+
close(): void;
|
|
1495
|
+
/**
|
|
1496
|
+
* Register a callback for stream closure
|
|
1497
|
+
* @param cb - Callback function to execute on close
|
|
1498
|
+
*/
|
|
1499
|
+
onClose(cb: () => void): void;
|
|
1500
|
+
}
|
|
1501
|
+
/**
|
|
1502
|
+
* Extended SSE stream with additional control methods
|
|
1503
|
+
*/
|
|
1504
|
+
interface SSEStreamExtended extends SSEStream {
|
|
1505
|
+
readonly id: string;
|
|
1506
|
+
[Symbol.asyncIterator](): AsyncGenerator<BufferedEvent, void, unknown>;
|
|
1507
|
+
/** Current connection state */
|
|
1508
|
+
readonly state: SSEConnectionState;
|
|
1509
|
+
/** Number of events in the buffer */
|
|
1510
|
+
readonly bufferSize: number;
|
|
1511
|
+
/** Check if stream is writable */
|
|
1512
|
+
readonly isWritable: boolean;
|
|
1513
|
+
/**
|
|
1514
|
+
* Ping the client to keep connection alive
|
|
1515
|
+
* @param comment - Optional comment to include in ping
|
|
1516
|
+
*/
|
|
1517
|
+
ping(comment?: string): void;
|
|
1518
|
+
/**
|
|
1519
|
+
* Set retry interval for client reconnection
|
|
1520
|
+
* @param milliseconds - Retry interval in milliseconds
|
|
1521
|
+
*/
|
|
1522
|
+
setRetry(milliseconds: number): void;
|
|
1523
|
+
/**
|
|
1524
|
+
* Flush any buffered events immediately
|
|
1525
|
+
*/
|
|
1526
|
+
flush(): void;
|
|
1527
|
+
getMetrics(): StreamMetrics;
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* SSE event serialization format
|
|
1531
|
+
*/
|
|
1532
|
+
interface SSESerializedEvent {
|
|
1533
|
+
/** Event ID field */
|
|
1534
|
+
id?: string;
|
|
1535
|
+
/** Event type field */
|
|
1536
|
+
event?: string;
|
|
1537
|
+
/** Data field (can be multi-line) */
|
|
1538
|
+
data: string;
|
|
1539
|
+
/** Retry field */
|
|
1540
|
+
retry?: number;
|
|
1541
|
+
/** Comment field for keep-alive */
|
|
1542
|
+
comment?: string;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* SSE event handler function type
|
|
1546
|
+
* @template T - Type of the event data
|
|
1547
|
+
*/
|
|
1548
|
+
type SSEEventHandler<T = unknown> = (event: SSEEvent<T>) => void | Promise<void>;
|
|
1549
|
+
/**
|
|
1550
|
+
* SSE event listener registration
|
|
1551
|
+
*/
|
|
1552
|
+
interface SSEEventListener {
|
|
1553
|
+
/** Event type to listen for (use '*' for all events) */
|
|
1554
|
+
event: string;
|
|
1555
|
+
/** Handler function for the event */
|
|
1556
|
+
handler: SSEEventHandler;
|
|
1557
|
+
/** Optional listener identifier for removal */
|
|
1558
|
+
id?: string;
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* SSE metrics for monitoring stream performance
|
|
1562
|
+
*/
|
|
1563
|
+
interface SSEMetrics {
|
|
1564
|
+
/** Total number of events sent */
|
|
1565
|
+
eventsSent: number;
|
|
1566
|
+
/** Total number of events dropped */
|
|
1567
|
+
eventsDropped: number;
|
|
1568
|
+
/** Current number of connected clients */
|
|
1569
|
+
activeConnections: number;
|
|
1570
|
+
/** Total bytes sent */
|
|
1571
|
+
bytesSent: number;
|
|
1572
|
+
/** Average event send latency in milliseconds */
|
|
1573
|
+
averageLatency: number;
|
|
1574
|
+
/** Connection duration in milliseconds */
|
|
1575
|
+
connectionDuration: number;
|
|
1576
|
+
}
|
|
1577
|
+
/**
|
|
1578
|
+
* SSE stream manager for handling multiple clients
|
|
1579
|
+
*/
|
|
1580
|
+
interface SSEStreamManager {
|
|
1581
|
+
/**
|
|
1582
|
+
* Create a new SSE stream for a client
|
|
1583
|
+
* @param clientId - Unique identifier for the client
|
|
1584
|
+
* @param options - Stream configuration options
|
|
1585
|
+
*/
|
|
1586
|
+
createStream(clientId: string, options?: SSEOptions): SSEStream;
|
|
1587
|
+
/**
|
|
1588
|
+
* Get an existing stream by client ID
|
|
1589
|
+
* @param clientId - Client identifier
|
|
1590
|
+
*/
|
|
1591
|
+
getStream(clientId: string): SSEStream | undefined;
|
|
1592
|
+
/**
|
|
1593
|
+
* Broadcast an event to all connected clients
|
|
1594
|
+
* @template T - Type of the event data
|
|
1595
|
+
* @param event - Event name
|
|
1596
|
+
* @param data - Event data
|
|
1597
|
+
*/
|
|
1598
|
+
broadcast<T>(event: string, data: T): void;
|
|
1599
|
+
/**
|
|
1600
|
+
* Broadcast to specific clients
|
|
1601
|
+
* @template T - Type of the event data
|
|
1602
|
+
* @param clientIds - Array of client IDs
|
|
1603
|
+
* @param event - Event name
|
|
1604
|
+
* @param data - Event data
|
|
1605
|
+
*/
|
|
1606
|
+
multicast<T>(clientIds: string[], event: string, data: T): void;
|
|
1607
|
+
/**
|
|
1608
|
+
* Close a specific client stream
|
|
1609
|
+
* @param clientId - Client identifier
|
|
1610
|
+
*/
|
|
1611
|
+
closeStream(clientId: string): void;
|
|
1612
|
+
/**
|
|
1613
|
+
* Close all active streams
|
|
1614
|
+
*/
|
|
1615
|
+
closeAll(): void;
|
|
1616
|
+
/**
|
|
1617
|
+
* Get metrics for all streams
|
|
1618
|
+
*/
|
|
1619
|
+
getMetrics(): SSEMetrics;
|
|
1620
|
+
}
|
|
1621
|
+
/**
|
|
1622
|
+
* Result type for operations that can fail
|
|
1623
|
+
*/
|
|
1624
|
+
type RegistryResult<T> = {
|
|
1625
|
+
success: true;
|
|
1626
|
+
value: T;
|
|
1627
|
+
} | {
|
|
1628
|
+
success: false;
|
|
1629
|
+
error: string;
|
|
1630
|
+
};
|
|
1631
|
+
/**
|
|
1632
|
+
* Connection metadata stored in the registry
|
|
1633
|
+
*/
|
|
1634
|
+
interface ConnectionEntry {
|
|
1635
|
+
stream: SSEStream;
|
|
1636
|
+
connectedAt: number;
|
|
1637
|
+
lastActivity: number;
|
|
1638
|
+
clientIp?: string;
|
|
1639
|
+
userAgent?: string;
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1642
|
+
* Internal connection registry interface
|
|
1643
|
+
*/
|
|
1644
|
+
interface ConnectionRegistry {
|
|
1645
|
+
/** Add a new connection to the registry */
|
|
1646
|
+
add: (id: string, stream: SSEStream, metadata?: {
|
|
1647
|
+
clientIp?: string;
|
|
1648
|
+
userAgent?: string;
|
|
1649
|
+
}) => void;
|
|
1650
|
+
/** Remove a connection from the registry */
|
|
1651
|
+
remove: (id: string) => void;
|
|
1652
|
+
/** Get current connection count */
|
|
1653
|
+
count: () => number;
|
|
1654
|
+
/** Clean up inactive or closed connections */
|
|
1655
|
+
cleanup: () => void;
|
|
1656
|
+
/** Get connection by ID (for internal use) */
|
|
1657
|
+
get: (id: string) => SSEStream | undefined;
|
|
1658
|
+
/** Check if a connection exists */
|
|
1659
|
+
has: (id: string) => boolean;
|
|
1660
|
+
/** Get all connection IDs */
|
|
1661
|
+
getIds: () => string[];
|
|
1662
|
+
/** Shutdown the registry and close all connections */
|
|
1663
|
+
shutdown: () => void;
|
|
1664
|
+
}
|
|
1665
|
+
/**
|
|
1666
|
+
* Extended stream interface for typed events
|
|
1667
|
+
*/
|
|
1668
|
+
interface TypedSSEStream<TEvents extends Record<string, z.ZodType>> extends SSEStreamExtended {
|
|
1669
|
+
send<K extends keyof TEvents>(event: K & string, data: z.infer<TEvents[K]>): void;
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Schema for SSE route validation with generic type parameters
|
|
1673
|
+
*/
|
|
1674
|
+
interface SSERouteSchema<P extends z.ZodType = z.ZodType<any>, Q extends z.ZodType = z.ZodType<any>, E = any> {
|
|
1675
|
+
/** Parameter schema for validation */
|
|
1676
|
+
params?: P;
|
|
1677
|
+
/** Query schema for validation */
|
|
1678
|
+
query?: Q;
|
|
1679
|
+
/** Events schema for validation (SSE-specific, replaces response) */
|
|
1680
|
+
events?: E;
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* SSE route handler function with stream as first parameter
|
|
1684
|
+
* This is the user-facing API - they write handlers with this signature
|
|
1685
|
+
*/
|
|
1686
|
+
type SSERouteHandler<TStream extends SSEStreamExtended = SSEStreamExtended, TParams = Record<string, string>, TQuery = Record<string, string | string[] | undefined>, TState extends State = State, TServices extends Services = Services> = (stream: TStream, ctx: Context<TState, TServices, never, TQuery>, // SSE never has body
|
|
1687
|
+
params: TParams) => Promise<void> | void;
|
|
1688
|
+
/**
|
|
1689
|
+
* SSE route creator with state and services support
|
|
1690
|
+
* Returns a higher-order function to handle generics properly
|
|
1691
|
+
*
|
|
1692
|
+
* The return type matches what the implementation actually returns:
|
|
1693
|
+
* - A route object with a GET property
|
|
1694
|
+
* - The GET property contains the wrapped handler and schemas
|
|
1695
|
+
* - The wrapped handler has the standard (ctx, params) signature expected by the router
|
|
1696
|
+
*/
|
|
1697
|
+
type CreateSSERoute = <TState extends State = State, TServices extends Services = Services>() => <P = never, Q = never, E = never>(config: {
|
|
1698
|
+
schema?: {
|
|
1699
|
+
params?: P extends never ? never : P;
|
|
1700
|
+
query?: Q extends never ? never : Q;
|
|
1701
|
+
events?: E extends never ? never : E;
|
|
1702
|
+
};
|
|
1703
|
+
handler: SSERouteHandler<E extends Record<string, z.ZodType> ? TypedSSEStream<E> : SSEStreamExtended, P extends z.ZodType ? Infer<P> : Record<string, string>, Q extends z.ZodType ? Infer<Q> : QueryParams, TState, TServices>;
|
|
1704
|
+
middleware?: Middleware[];
|
|
1705
|
+
options?: Record<string, unknown>;
|
|
1706
|
+
}) => {
|
|
1707
|
+
GET: {
|
|
1708
|
+
handler: (ctx: any, params: any) => Promise<void>;
|
|
1709
|
+
schema?: {
|
|
1710
|
+
params?: P extends never ? undefined : P;
|
|
1711
|
+
query?: Q extends never ? undefined : Q;
|
|
1712
|
+
};
|
|
1713
|
+
middleware?: Middleware[];
|
|
1714
|
+
options?: Record<string, unknown>;
|
|
1715
|
+
};
|
|
1716
|
+
path: string;
|
|
1717
|
+
};
|
|
1718
|
+
/**
|
|
1719
|
+
* Buffered event with metadata
|
|
1720
|
+
*/
|
|
1721
|
+
interface BufferedEvent {
|
|
1722
|
+
id: string;
|
|
1723
|
+
event: string;
|
|
1724
|
+
data: unknown;
|
|
1725
|
+
size: number;
|
|
1726
|
+
timestamp: number;
|
|
1727
|
+
correlationId: string;
|
|
1728
|
+
}
|
|
1729
|
+
/**
|
|
1730
|
+
* Stream metrics for monitoring
|
|
1731
|
+
*/
|
|
1732
|
+
interface StreamMetrics {
|
|
1733
|
+
eventsSent: number;
|
|
1734
|
+
eventsDropped: number;
|
|
1735
|
+
bytesWritten: number;
|
|
1736
|
+
bufferHighWatermark: number;
|
|
1737
|
+
lastEventTime: number;
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
/**
|
|
1741
|
+
* SSE Client Types for BlaizeJS
|
|
1742
|
+
* Location: packages/blaize-client/src/sse/types.ts
|
|
1743
|
+
*/
|
|
1744
|
+
|
|
1745
|
+
/**
|
|
1746
|
+
* Event handlers map
|
|
1747
|
+
*/
|
|
1748
|
+
interface EventHandlers {
|
|
1749
|
+
[event: string]: Set<(data: any) => void>;
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* SSE connection configuration options
|
|
1753
|
+
*/
|
|
1754
|
+
interface SSEClientOptions {
|
|
1755
|
+
headers?: Record<string, string>;
|
|
1756
|
+
withCredentials?: boolean;
|
|
1757
|
+
reconnect?: {
|
|
1758
|
+
enabled: boolean;
|
|
1759
|
+
maxAttempts?: number;
|
|
1760
|
+
strategy?: ReconnectStrategy;
|
|
1761
|
+
initialDelay?: number;
|
|
1762
|
+
};
|
|
1763
|
+
bufferMissedEvents?: boolean;
|
|
1764
|
+
maxMissedEvents?: number;
|
|
1765
|
+
heartbeatTimeout?: number;
|
|
1766
|
+
parseJSON?: boolean;
|
|
1767
|
+
/**
|
|
1768
|
+
* Whether to wait for connection before resolving the promise.
|
|
1769
|
+
* If false, returns the client immediately without waiting.
|
|
1770
|
+
* Default: true
|
|
1771
|
+
*/
|
|
1772
|
+
waitForConnection?: boolean;
|
|
1773
|
+
/**
|
|
1774
|
+
* Optional timeout for initial connection in milliseconds.
|
|
1775
|
+
* If not set, no timeout is applied (relies on EventSource native timeout).
|
|
1776
|
+
* Only applies if waitForConnection is true.
|
|
1777
|
+
*/
|
|
1778
|
+
connectionTimeout?: number;
|
|
1779
|
+
}
|
|
1780
|
+
/**
|
|
1781
|
+
* Metrics for SSE connection monitoring
|
|
1782
|
+
*/
|
|
1783
|
+
interface SSEClientMetrics {
|
|
1784
|
+
eventsReceived: number;
|
|
1785
|
+
bytesReceived: number;
|
|
1786
|
+
connectionDuration: number;
|
|
1787
|
+
reconnectAttempts: number;
|
|
1788
|
+
lastEventId?: string;
|
|
1789
|
+
}
|
|
1790
|
+
/**
|
|
1791
|
+
* Reconnection delay calculation strategy
|
|
1792
|
+
*/
|
|
1793
|
+
type ReconnectStrategy = (attempt: number) => number;
|
|
1794
|
+
/**
|
|
1795
|
+
* SSE Client interface with type-safe event handling
|
|
1796
|
+
*/
|
|
1797
|
+
interface SSEClient<TEvents extends Record<string, unknown> = Record<string, unknown>> {
|
|
1798
|
+
on<K extends keyof TEvents>(event: K & string, handler: (data: TEvents[K]) => void): void;
|
|
1799
|
+
on(event: 'error', handler: (error: BlaizeError) => void): void;
|
|
1800
|
+
on(event: 'open', handler: () => void): void;
|
|
1801
|
+
on(event: 'close', handler: (event: CloseEvent) => void): void;
|
|
1802
|
+
off<K extends keyof TEvents>(event: K & string, handler?: (data: TEvents[K]) => void): void;
|
|
1803
|
+
off(event: 'error', handler?: (error: BlaizeError) => void): void;
|
|
1804
|
+
off(event: 'open', handler?: () => void): void;
|
|
1805
|
+
off(event: 'close', handler?: (event: CloseEvent) => void): void;
|
|
1806
|
+
once<K extends keyof TEvents>(event: K & string, handler: (data: TEvents[K]) => void): void;
|
|
1807
|
+
once(event: 'error', handler: (error: BlaizeError) => void): void;
|
|
1808
|
+
once(event: 'open', handler: () => void): void;
|
|
1809
|
+
once(event: 'close', handler: (event: CloseEvent) => void): void;
|
|
1810
|
+
close(): void;
|
|
1811
|
+
readonly state: SSEConnectionState;
|
|
1812
|
+
readonly metrics: SSEClientMetrics;
|
|
1813
|
+
readonly lastEventId?: string;
|
|
1814
|
+
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Close event for SSE connections
|
|
1817
|
+
*/
|
|
1818
|
+
interface CloseEvent {
|
|
1819
|
+
reconnect: boolean;
|
|
1820
|
+
reason?: string;
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Internal SSE connection factory
|
|
1824
|
+
* Returns a Promise that resolves to an SSEClient instance
|
|
1825
|
+
*/
|
|
1826
|
+
type SSEConnectionFactory<TEvents extends Record<string, unknown> = Record<string, unknown>> = (options?: SSEClientOptions) => Promise<SSEClient<TEvents>>;
|
|
1827
|
+
|
|
1279
1828
|
type ExtractMethod<T> = T extends {
|
|
1280
1829
|
GET: any;
|
|
1281
1830
|
} ? 'GET' : T extends {
|
|
@@ -1331,6 +1880,7 @@ interface ClientConfig {
|
|
|
1331
1880
|
baseUrl: string;
|
|
1332
1881
|
defaultHeaders?: Record<string, string>;
|
|
1333
1882
|
timeout?: number;
|
|
1883
|
+
sse?: SSEClientOptions;
|
|
1334
1884
|
}
|
|
1335
1885
|
interface InternalRequestArgs {
|
|
1336
1886
|
params?: Record<string, any>;
|
|
@@ -1344,6 +1894,328 @@ interface RequestOptions {
|
|
|
1344
1894
|
body?: string;
|
|
1345
1895
|
timeout: number;
|
|
1346
1896
|
}
|
|
1897
|
+
/**
|
|
1898
|
+
* Detect if a route has SSE support
|
|
1899
|
+
* SSE routes have a special 'SSE' method key
|
|
1900
|
+
*/
|
|
1901
|
+
type HasSSEMethod<TRoute> = TRoute extends {
|
|
1902
|
+
SSE: any;
|
|
1903
|
+
} ? true : false;
|
|
1904
|
+
/**
|
|
1905
|
+
* Extract SSE event types from route schema
|
|
1906
|
+
*/
|
|
1907
|
+
type ExtractSSEEvents<TRoute> = TRoute extends {
|
|
1908
|
+
SSE: {
|
|
1909
|
+
events?: infer E;
|
|
1910
|
+
};
|
|
1911
|
+
} ? E extends z.ZodType ? z.infer<E> : Record<string, unknown> : Record<string, unknown>;
|
|
1912
|
+
/**
|
|
1913
|
+
* Extract SSE query parameters from route
|
|
1914
|
+
*/
|
|
1915
|
+
type ExtractSSEQuery<TRoute> = TRoute extends {
|
|
1916
|
+
SSE: {
|
|
1917
|
+
schema?: {
|
|
1918
|
+
query?: infer Q;
|
|
1919
|
+
};
|
|
1920
|
+
};
|
|
1921
|
+
} ? Q extends z.ZodType ? z.infer<Q> : Record<string, unknown> : never;
|
|
1922
|
+
/**
|
|
1923
|
+
* Extract SSE params from route
|
|
1924
|
+
*/
|
|
1925
|
+
type ExtractSSEParams<TRoute> = TRoute extends {
|
|
1926
|
+
SSE: {
|
|
1927
|
+
schema?: {
|
|
1928
|
+
params?: infer P;
|
|
1929
|
+
};
|
|
1930
|
+
};
|
|
1931
|
+
} ? P extends z.ZodType ? z.infer<P> : Record<string, string> : never;
|
|
1932
|
+
/**
|
|
1933
|
+
* Build SSE method arguments
|
|
1934
|
+
*/
|
|
1935
|
+
type BuildSSEArgs<TRoute> = ExtractSSEParams<TRoute> extends never ? ExtractSSEQuery<TRoute> extends never ? {
|
|
1936
|
+
options?: SSEClientOptions;
|
|
1937
|
+
} : {
|
|
1938
|
+
query: ExtractSSEQuery<TRoute>;
|
|
1939
|
+
options?: SSEClientOptions;
|
|
1940
|
+
} : ExtractSSEQuery<TRoute> extends never ? {
|
|
1941
|
+
params: ExtractSSEParams<TRoute>;
|
|
1942
|
+
options?: SSEClientOptions;
|
|
1943
|
+
} : {
|
|
1944
|
+
params: ExtractSSEParams<TRoute>;
|
|
1945
|
+
query: ExtractSSEQuery<TRoute>;
|
|
1946
|
+
options?: SSEClientOptions;
|
|
1947
|
+
};
|
|
1948
|
+
/**
|
|
1949
|
+
* Create SSE client method
|
|
1950
|
+
*/
|
|
1951
|
+
type CreateSSEMethod<TRoute> = HasSSEMethod<TRoute> extends true ? BuildSSEArgs<TRoute> extends {
|
|
1952
|
+
options?: SSEClientOptions;
|
|
1953
|
+
} ? (args?: BuildSSEArgs<TRoute>) => Promise<SSEClient<ExtractSSEEvents<TRoute>>> : (args: BuildSSEArgs<TRoute>) => Promise<SSEClient<ExtractSSEEvents<TRoute>>> : never;
|
|
1954
|
+
/**
|
|
1955
|
+
* Extract SSE routes from registry
|
|
1956
|
+
*/
|
|
1957
|
+
type ExtractSSERoutes<TRoutes extends Record<string, any>> = {
|
|
1958
|
+
[K in keyof TRoutes as HasSSEMethod<TRoutes[K]> extends true ? K : never]: TRoutes[K];
|
|
1959
|
+
};
|
|
1960
|
+
/**
|
|
1961
|
+
* Enhanced client with SSE support
|
|
1962
|
+
*/
|
|
1963
|
+
type CreateEnhancedClient<TRoutes extends Record<string, any>, TRegistry> = TRegistry & {
|
|
1964
|
+
$sse: {
|
|
1965
|
+
[K in keyof ExtractSSERoutes<TRoutes>]: CreateSSEMethod<TRoutes[K]>;
|
|
1966
|
+
};
|
|
1967
|
+
};
|
|
1968
|
+
|
|
1969
|
+
/**
|
|
1970
|
+
* CORS Types for BlaizeJS Framework
|
|
1971
|
+
*
|
|
1972
|
+
* Comprehensive type definitions for W3C-compliant CORS middleware
|
|
1973
|
+
* with support for string, regex, and async function origin validation.
|
|
1974
|
+
*
|
|
1975
|
+
* @module @blaizejs/types/cors
|
|
1976
|
+
*/
|
|
1977
|
+
|
|
1978
|
+
/**
|
|
1979
|
+
* Origin configuration type supporting multiple validation methods
|
|
1980
|
+
*
|
|
1981
|
+
* @example
|
|
1982
|
+
* ```typescript
|
|
1983
|
+
* // String origin (exact match)
|
|
1984
|
+
* const origin: CorsOrigin = 'https://example.com';
|
|
1985
|
+
*
|
|
1986
|
+
* // RegExp pattern
|
|
1987
|
+
* const origin: CorsOrigin = /^https:\/\/.*\.example\.com$/;
|
|
1988
|
+
*
|
|
1989
|
+
* // Dynamic validation function
|
|
1990
|
+
* const origin: CorsOrigin = async (origin, ctx) => {
|
|
1991
|
+
* return await checkOriginAllowed(origin, ctx?.state.user);
|
|
1992
|
+
* };
|
|
1993
|
+
*
|
|
1994
|
+
* // Array of mixed types
|
|
1995
|
+
* const origin: CorsOrigin = [
|
|
1996
|
+
* 'https://localhost:3000',
|
|
1997
|
+
* /^https:\/\/.*\.example\.com$/,
|
|
1998
|
+
* (origin) => origin.endsWith('.trusted.com')
|
|
1999
|
+
* ];
|
|
2000
|
+
* ```
|
|
2001
|
+
*/
|
|
2002
|
+
type CorsOrigin = string | RegExp | ((origin: string, ctx?: Context<any, any>) => boolean | Promise<boolean>) | Array<string | RegExp | ((origin: string, ctx?: Context<any, any>) => boolean | Promise<boolean>)>;
|
|
2003
|
+
/**
|
|
2004
|
+
* HTTP methods that can be allowed in CORS
|
|
2005
|
+
* Based on W3C CORS specification
|
|
2006
|
+
*/
|
|
2007
|
+
type CorsHttpMethod = HttpMethod | 'CONNECT' | 'TRACE';
|
|
2008
|
+
/**
|
|
2009
|
+
* Main CORS configuration options
|
|
2010
|
+
*
|
|
2011
|
+
* @example
|
|
2012
|
+
* ```typescript
|
|
2013
|
+
* const corsOptions: CorsOptions = {
|
|
2014
|
+
* origin: 'https://example.com',
|
|
2015
|
+
* methods: ['GET', 'POST'],
|
|
2016
|
+
* credentials: true,
|
|
2017
|
+
* maxAge: 86400
|
|
2018
|
+
* };
|
|
2019
|
+
* ```
|
|
2020
|
+
*/
|
|
2021
|
+
interface CorsOptions {
|
|
2022
|
+
/**
|
|
2023
|
+
* Configures the Access-Control-Allow-Origin header
|
|
2024
|
+
*
|
|
2025
|
+
* Possible values:
|
|
2026
|
+
* - `true`: Allow all origins (sets to '*' unless credentials is true, then reflects origin)
|
|
2027
|
+
* - `false`: Disable CORS (no headers set)
|
|
2028
|
+
* - `string`: Specific origin to allow
|
|
2029
|
+
* - `RegExp`: Pattern to match origins
|
|
2030
|
+
* - `function`: Custom validation logic
|
|
2031
|
+
* - `array`: Multiple origin configurations
|
|
2032
|
+
*
|
|
2033
|
+
* @default false
|
|
2034
|
+
*/
|
|
2035
|
+
origin?: boolean | CorsOrigin;
|
|
2036
|
+
/**
|
|
2037
|
+
* Configures the Access-Control-Allow-Methods header
|
|
2038
|
+
*
|
|
2039
|
+
* @default ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE']
|
|
2040
|
+
* @example ['GET', 'POST']
|
|
2041
|
+
*/
|
|
2042
|
+
methods?: CorsHttpMethod[] | string;
|
|
2043
|
+
/**
|
|
2044
|
+
* Configures the Access-Control-Allow-Headers header
|
|
2045
|
+
*
|
|
2046
|
+
* Pass an array of allowed headers or a comma-delimited string.
|
|
2047
|
+
*
|
|
2048
|
+
* @default Request's Access-Control-Request-Headers header value
|
|
2049
|
+
* @example ['Content-Type', 'Authorization']
|
|
2050
|
+
*/
|
|
2051
|
+
allowedHeaders?: string[] | string;
|
|
2052
|
+
/**
|
|
2053
|
+
* Configures the Access-Control-Expose-Headers header
|
|
2054
|
+
*
|
|
2055
|
+
* Headers that the browser is allowed to access.
|
|
2056
|
+
*
|
|
2057
|
+
* @default []
|
|
2058
|
+
* @example ['Content-Range', 'X-Content-Range']
|
|
2059
|
+
*/
|
|
2060
|
+
exposedHeaders?: string[] | string;
|
|
2061
|
+
/**
|
|
2062
|
+
* Configures the Access-Control-Allow-Credentials header
|
|
2063
|
+
*
|
|
2064
|
+
* Set to true to allow credentials (cookies, authorization headers, TLS client certificates).
|
|
2065
|
+
* Note: Cannot be used with origin: '*' for security reasons.
|
|
2066
|
+
*
|
|
2067
|
+
* @default false
|
|
2068
|
+
*/
|
|
2069
|
+
credentials?: boolean;
|
|
2070
|
+
/**
|
|
2071
|
+
* Configures the Access-Control-Max-Age header in seconds
|
|
2072
|
+
*
|
|
2073
|
+
* Indicates how long browsers can cache preflight response.
|
|
2074
|
+
* Set to -1 to disable caching.
|
|
2075
|
+
*
|
|
2076
|
+
* @default undefined (browser decides)
|
|
2077
|
+
* @example 86400 // 24 hours
|
|
2078
|
+
*/
|
|
2079
|
+
maxAge?: number;
|
|
2080
|
+
/**
|
|
2081
|
+
* Whether to pass the CORS preflight response to the next handler
|
|
2082
|
+
*
|
|
2083
|
+
* When false, the preflight response is sent immediately.
|
|
2084
|
+
* When true, control passes to the next middleware/handler.
|
|
2085
|
+
*
|
|
2086
|
+
* @default false
|
|
2087
|
+
*/
|
|
2088
|
+
preflightContinue?: boolean;
|
|
2089
|
+
/**
|
|
2090
|
+
* HTTP status code for successful OPTIONS requests
|
|
2091
|
+
*
|
|
2092
|
+
* Some legacy browsers require 200, while 204 is more correct.
|
|
2093
|
+
*
|
|
2094
|
+
* @default 204
|
|
2095
|
+
*/
|
|
2096
|
+
optionsSuccessStatus?: number;
|
|
2097
|
+
}
|
|
2098
|
+
/**
|
|
2099
|
+
* Internal CORS validation result
|
|
2100
|
+
* Used by middleware implementation
|
|
2101
|
+
*/
|
|
2102
|
+
interface CorsValidationResult {
|
|
2103
|
+
/**
|
|
2104
|
+
* Whether the origin is allowed
|
|
2105
|
+
*/
|
|
2106
|
+
allowed: boolean;
|
|
2107
|
+
/**
|
|
2108
|
+
* The origin value to set in the header
|
|
2109
|
+
* Can be '*', specific origin, or 'null'
|
|
2110
|
+
*/
|
|
2111
|
+
origin?: string;
|
|
2112
|
+
/**
|
|
2113
|
+
* Whether to add Vary: Origin header
|
|
2114
|
+
*/
|
|
2115
|
+
vary?: boolean;
|
|
2116
|
+
}
|
|
2117
|
+
/**
|
|
2118
|
+
* CORS preflight request information
|
|
2119
|
+
* Extracted from OPTIONS request headers
|
|
2120
|
+
*/
|
|
2121
|
+
interface CorsPreflightInfo {
|
|
2122
|
+
/**
|
|
2123
|
+
* The origin making the request
|
|
2124
|
+
*/
|
|
2125
|
+
origin?: string;
|
|
2126
|
+
/**
|
|
2127
|
+
* The method that will be used in the actual request
|
|
2128
|
+
* From Access-Control-Request-Method header
|
|
2129
|
+
*/
|
|
2130
|
+
requestedMethod?: string;
|
|
2131
|
+
/**
|
|
2132
|
+
* The headers that will be sent in the actual request
|
|
2133
|
+
* From Access-Control-Request-Headers header
|
|
2134
|
+
*/
|
|
2135
|
+
requestedHeaders?: string[];
|
|
2136
|
+
}
|
|
2137
|
+
/**
|
|
2138
|
+
* Cache entry for origin validation results
|
|
2139
|
+
* Used for performance optimization
|
|
2140
|
+
*/
|
|
2141
|
+
interface CorsOriginCacheEntry {
|
|
2142
|
+
/**
|
|
2143
|
+
* Whether the origin is allowed
|
|
2144
|
+
*/
|
|
2145
|
+
allowed: boolean;
|
|
2146
|
+
/**
|
|
2147
|
+
* When this cache entry expires (timestamp)
|
|
2148
|
+
*/
|
|
2149
|
+
expiresAt: number;
|
|
2150
|
+
/**
|
|
2151
|
+
* Optional user identifier for cache key
|
|
2152
|
+
*/
|
|
2153
|
+
userId?: string;
|
|
2154
|
+
}
|
|
2155
|
+
/**
|
|
2156
|
+
* Configuration for CORS origin validation cache
|
|
2157
|
+
*/
|
|
2158
|
+
interface CorsOriginCacheConfig {
|
|
2159
|
+
/**
|
|
2160
|
+
* Time-to-live for cache entries in milliseconds
|
|
2161
|
+
* @default 60000 (1 minute)
|
|
2162
|
+
*/
|
|
2163
|
+
ttl?: number;
|
|
2164
|
+
/**
|
|
2165
|
+
* Maximum number of entries in the cache
|
|
2166
|
+
* @default 1000
|
|
2167
|
+
*/
|
|
2168
|
+
maxSize?: number;
|
|
2169
|
+
/**
|
|
2170
|
+
* Whether to include user ID in cache key
|
|
2171
|
+
* @default true
|
|
2172
|
+
*/
|
|
2173
|
+
includeUserId?: boolean;
|
|
2174
|
+
}
|
|
2175
|
+
/**
|
|
2176
|
+
* Statistics for CORS middleware performance monitoring
|
|
2177
|
+
*/
|
|
2178
|
+
interface CorsStats {
|
|
2179
|
+
/**
|
|
2180
|
+
* Total number of CORS requests processed
|
|
2181
|
+
*/
|
|
2182
|
+
totalRequests: number;
|
|
2183
|
+
/**
|
|
2184
|
+
* Number of preflight requests handled
|
|
2185
|
+
*/
|
|
2186
|
+
preflightRequests: number;
|
|
2187
|
+
/**
|
|
2188
|
+
* Number of allowed origins
|
|
2189
|
+
*/
|
|
2190
|
+
allowedOrigins: number;
|
|
2191
|
+
/**
|
|
2192
|
+
* Number of denied origins
|
|
2193
|
+
*/
|
|
2194
|
+
deniedOrigins: number;
|
|
2195
|
+
/**
|
|
2196
|
+
* Cache hit rate for origin validation
|
|
2197
|
+
*/
|
|
2198
|
+
cacheHitRate: number;
|
|
2199
|
+
/**
|
|
2200
|
+
* Average origin validation time in milliseconds
|
|
2201
|
+
*/
|
|
2202
|
+
avgValidationTime: number;
|
|
2203
|
+
}
|
|
2204
|
+
/**
|
|
2205
|
+
* Cache entry type
|
|
2206
|
+
*/
|
|
2207
|
+
interface CacheEntry {
|
|
2208
|
+
allowed: boolean;
|
|
2209
|
+
expiresAt: number;
|
|
2210
|
+
lastAccessed: number;
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* Cache configuration
|
|
2214
|
+
*/
|
|
2215
|
+
interface CacheConfig {
|
|
2216
|
+
ttl: number;
|
|
2217
|
+
maxSize: number;
|
|
2218
|
+
}
|
|
1347
2219
|
|
|
1348
2220
|
/**
|
|
1349
2221
|
* BlaizeJS Server Module - Enhanced with Correlation Configuration
|
|
@@ -1411,6 +2283,35 @@ interface ServerOptionsInput {
|
|
|
1411
2283
|
* @since 0.4.0
|
|
1412
2284
|
*/
|
|
1413
2285
|
correlation?: CorrelationOptions;
|
|
2286
|
+
/**
|
|
2287
|
+
* CORS configuration
|
|
2288
|
+
*
|
|
2289
|
+
* - `true`: Enable CORS with development defaults (allow all origins)
|
|
2290
|
+
* - `false`: Disable CORS (no headers set)
|
|
2291
|
+
* - `CorsOptions`: Custom CORS configuration
|
|
2292
|
+
*
|
|
2293
|
+
* @default false (CORS disabled)
|
|
2294
|
+
* @since 0.5.0
|
|
2295
|
+
*
|
|
2296
|
+
* @example
|
|
2297
|
+
* ```typescript
|
|
2298
|
+
* // Enable with dev defaults
|
|
2299
|
+
* const server = createServer({ cors: true });
|
|
2300
|
+
*
|
|
2301
|
+
* // Custom configuration
|
|
2302
|
+
* const server = createServer({
|
|
2303
|
+
* cors: {
|
|
2304
|
+
* origin: 'https://example.com',
|
|
2305
|
+
* credentials: true,
|
|
2306
|
+
* maxAge: 86400
|
|
2307
|
+
* }
|
|
2308
|
+
* });
|
|
2309
|
+
*
|
|
2310
|
+
* // Disable CORS
|
|
2311
|
+
* const server = createServer({ cors: false });
|
|
2312
|
+
* ```
|
|
2313
|
+
*/
|
|
2314
|
+
cors?: CorsOptions | boolean;
|
|
1414
2315
|
}
|
|
1415
2316
|
/**
|
|
1416
2317
|
* Configuration for a BlaizeJS server
|
|
@@ -1440,6 +2341,11 @@ interface ServerOptions {
|
|
|
1440
2341
|
* @since 0.4.0
|
|
1441
2342
|
*/
|
|
1442
2343
|
correlation?: CorrelationOptions;
|
|
2344
|
+
/**
|
|
2345
|
+
* CORS configuration
|
|
2346
|
+
* @since 0.5.0
|
|
2347
|
+
*/
|
|
2348
|
+
cors?: CorsOptions | boolean;
|
|
1443
2349
|
}
|
|
1444
2350
|
/**
|
|
1445
2351
|
* BlaizeJS Server instance with generic type accumulation
|
|
@@ -1453,6 +2359,8 @@ interface Server<TState, TServices> {
|
|
|
1453
2359
|
server: http.Server | http2.Http2Server | undefined;
|
|
1454
2360
|
/** The port the server is configured to listen on */
|
|
1455
2361
|
port: number;
|
|
2362
|
+
/** CORS configuration for this server */
|
|
2363
|
+
corsOptions?: CorsOptions | boolean;
|
|
1456
2364
|
/** The host the server is bound to */
|
|
1457
2365
|
host: string;
|
|
1458
2366
|
events: EventEmitter;
|
|
@@ -1968,6 +2876,27 @@ declare function createRouteFactory<TState extends State = State, TServices exte
|
|
|
1968
2876
|
OPTIONS: RouteMethodOptions<P extends never ? never : P extends zod.ZodType<any, zod.ZodTypeDef, any> ? P : never, Q extends never ? never : Q extends zod.ZodType<any, zod.ZodTypeDef, any> ? Q : never, never, R extends never ? never : R extends zod.ZodType<any, zod.ZodTypeDef, any> ? R : never, zod.ZodType<any, zod.ZodTypeDef, any>>;
|
|
1969
2877
|
path: string;
|
|
1970
2878
|
};
|
|
2879
|
+
readonly sse: <P = never, Q = never, E = never>(config: {
|
|
2880
|
+
schema?: {
|
|
2881
|
+
params?: (P extends never ? never : P) | undefined;
|
|
2882
|
+
query?: (Q extends never ? never : Q) | undefined;
|
|
2883
|
+
events?: (E extends never ? never : E) | undefined;
|
|
2884
|
+
} | undefined;
|
|
2885
|
+
handler: SSERouteHandler<E extends Record<string, zod.ZodType<any, zod.ZodTypeDef, any>> ? TypedSSEStream<E> : SSEStreamExtended, P extends zod.ZodType<any, zod.ZodTypeDef, any> ? Infer<P> : Record<string, string>, Q extends zod.ZodType<any, zod.ZodTypeDef, any> ? Infer<Q> : QueryParams, TState, TServices>;
|
|
2886
|
+
middleware?: Middleware[];
|
|
2887
|
+
options?: Record<string, unknown>;
|
|
2888
|
+
}) => {
|
|
2889
|
+
GET: {
|
|
2890
|
+
handler: (ctx: any, params: any) => Promise<void>;
|
|
2891
|
+
schema?: {
|
|
2892
|
+
params?: (P extends never ? undefined : P) | undefined;
|
|
2893
|
+
query?: (Q extends never ? undefined : Q) | undefined;
|
|
2894
|
+
} | undefined;
|
|
2895
|
+
middleware?: Middleware[];
|
|
2896
|
+
options?: Record<string, unknown>;
|
|
2897
|
+
};
|
|
2898
|
+
path: string;
|
|
2899
|
+
};
|
|
1971
2900
|
};
|
|
1972
2901
|
|
|
1973
2902
|
/**
|
|
@@ -2381,4 +3310,4 @@ declare const Blaize: {
|
|
|
2381
3310
|
VERSION: string;
|
|
2382
3311
|
};
|
|
2383
3312
|
|
|
2384
|
-
export { Blaize, BlaizeError, type BlaizeErrorResponse, type BodyParseError, type BuildRoutesRegistry, type ClientConfig, type ComposeMiddlewareServices, type ComposeMiddlewareStates, type ComposeMiddlewareTypes, type ComposePluginServices, type ComposePluginStates, type ComposePluginTypes, ConflictError, type ConflictErrorDetails, type Context, type ContextOptions, type ContextRequest, type ContextResponse, type CorrelationOptions, type CreateClient, type CreateContextFn, type CreateDeleteRoute, type CreateGetRoute, type CreateHeadRoute, type CreateOptionsRoute, type CreatePatchRoute, type CreatePostRoute, type CreatePutRoute, type ErrorHandlerOptions, ErrorSeverity, type ErrorTransformContext, ErrorType, type ExtractMethod, type ExtractMiddlewareServices, type ExtractMiddlewareState, type ExtractMiddlewareTypes, type ExtractPluginServices, type ExtractPluginState, type ExtractPluginTypes, type FileCache, type FindRouteFilesOptions, ForbiddenError, type ForbiddenErrorDetails, type GetContextFn, type Http2Options, type HttpMethod, type Infer, type InferContext, type InternalRequestArgs, InternalServerError, type InternalServerErrorDetails, type Matcher, type MergeServices, type MergeStates, type Middleware, MiddlewareAPI, type MiddlewareFunction, type MiddlewareOptions, type MultipartData, type MultipartError, type MultipartLimits, type NetworkErrorContext, type NextFunction, NotFoundError, type NotFoundErrorDetails, type ParseErrorContext, type ParseOptions, type ParseResult, type ParsedRoute, type ParserState, PayloadTooLargeError, type PayloadTooLargeErrorDetails, type Plugin, type PluginFactory, type PluginHooks, type PluginLifecycleManager, type PluginLifecycleOptions, type PluginOptions, PluginsAPI, type ProcessResponseOptions, type ProcessingConfig, type QueryParams, RateLimitError, type RateLimitErrorDetails, type ReloadMetrics, type RequestHandler, type RequestOptions, type RequestParams, RequestTimeoutError, type Result, type Route, type RouteDefinition, type RouteEntry, type RouteHandler, type RouteMatch, type RouteMethodOptions, type RouteNode, type RouteOptions, type RouteRegistry, type RouteSchema, type Router, RouterAPI, type RouterOptions, type SafeComposeMiddlewareServices, type SafeComposeMiddlewareStates, type SafeComposePluginServices, type SafeComposePluginStates, type SafeExtractMiddlewareServices, type SafeExtractMiddlewareState, type SafeExtractPluginServices, type SafeExtractPluginState, type Server, ServerAPI, type ServerOptions, type ServerOptionsInput, type Services, type StandardErrorResponse, type StartOptions, type State, type StopOptions, type StreamOptions, type TimeoutErrorContext, UnauthorizedError, type UnauthorizedErrorDetails, type UnifiedRequest, type UnifiedResponse, type UnionToIntersection, type UnknownFunction, type UnknownServer, UnprocessableEntityError, UnsupportedMediaTypeError, type UnsupportedMediaTypeErrorDetails, type UploadProgress, type UploadedFile, VERSION, type ValidationConfig, ValidationError, type ValidationErrorDetails, type ValidationFieldError, type WatchOptions, asMiddlewareArray, asPluginArray, compose, createDeleteRoute, createGetRoute, createHeadRoute, create$2 as createMiddleware, createMiddlewareArray, createOptionsRoute, createPatchRoute, create$1 as createPlugin, createPluginArray, createPostRoute, createPutRoute, createRouteFactory, create as createServer, serviceMiddleware as createServiceMiddleware, stateMiddleware as createStateMiddleware, getCorrelationId, inferContext, isBodyParseError, isMiddleware, isPlugin };
|
|
3313
|
+
export { Blaize, BlaizeError, type BlaizeErrorResponse, type BodyParseError, type BufferedEvent, type BuildRoutesRegistry, type BuildSSEArgs, type CacheConfig, type CacheEntry, type ClientConfig, type CloseEvent, type ComposeMiddlewareServices, type ComposeMiddlewareStates, type ComposeMiddlewareTypes, type ComposePluginServices, type ComposePluginStates, type ComposePluginTypes, ConflictError, type ConflictErrorDetails, type ConnectionEntry, type ConnectionRegistry, type Context, type ContextOptions, type ContextRequest, type ContextResponse, type CorrelationOptions, type CorsHttpMethod, type CorsOptions, type CorsOrigin, type CorsOriginCacheConfig, type CorsOriginCacheEntry, type CorsPreflightInfo, type CorsStats, type CorsValidationResult, type CreateClient, type CreateContextFn, type CreateDeleteRoute, type CreateEnhancedClient, type CreateGetRoute, type CreateHeadRoute, type CreateOptionsRoute, type CreatePatchRoute, type CreatePostRoute, type CreatePutRoute, type CreateSSEMethod, type CreateSSERoute, type ErrorHandlerOptions, ErrorSeverity, type ErrorTransformContext, ErrorType, type EventHandlers, type ExtractMethod, type ExtractMiddlewareServices, type ExtractMiddlewareState, type ExtractMiddlewareTypes, type ExtractPluginServices, type ExtractPluginState, type ExtractPluginTypes, type ExtractSSEEvents, type ExtractSSEParams, type ExtractSSEQuery, type ExtractSSERoutes, type FileCache, type FindRouteFilesOptions, ForbiddenError, type ForbiddenErrorDetails, type GetContextFn, type HasSSEMethod, type Http2Options, type HttpMethod, type Infer, type InferContext, type InternalRequestArgs, InternalServerError, type InternalServerErrorDetails, type Matcher, type MergeServices, type MergeStates, type Middleware, MiddlewareAPI, type MiddlewareFunction, type MiddlewareOptions, type MultipartData, type MultipartError, type MultipartLimits, type NetworkErrorContext, type NextFunction, NotFoundError, type NotFoundErrorDetails, type ParseErrorContext, type ParseOptions, type ParseResult, type ParsedRoute, type ParserState, PayloadTooLargeError, type PayloadTooLargeErrorDetails, type Plugin, type PluginFactory, type PluginHooks, type PluginLifecycleManager, type PluginLifecycleOptions, type PluginOptions, PluginsAPI, type ProcessResponseOptions, type ProcessingConfig, type QueryParams, RateLimitError, type RateLimitErrorDetails, type ReconnectStrategy, type RegistryResult, type ReloadMetrics, type RequestHandler, type RequestOptions, type RequestParams, RequestTimeoutError, type Result, type Route, type RouteDefinition, type RouteEntry, type RouteHandler, type RouteMatch, type RouteMethodOptions, type RouteNode, type RouteOptions, type RouteRegistry, type RouteSchema, type Router, RouterAPI, type RouterOptions, type SSEBufferOverflowErrorDetails, type SSEBufferStrategy, type SSEClient, type SSEClientMetrics, type SSEClientOptions, type SSEConnectionErrorContext, type SSEConnectionErrorDetails, type SSEConnectionFactory, type SSEConnectionState, type SSEEvent, type SSEEventHandler, type SSEEventListener, type SSEHeartbeatErrorContext, type SSEMetrics, type SSEOptions, type SSERouteHandler, type SSERouteSchema, type SSESerializedEvent, type SSEStream, type SSEStreamClosedErrorDetails, type SSEStreamErrorContext, type SSEStreamExtended, type SSEStreamManager, type SafeComposeMiddlewareServices, type SafeComposeMiddlewareStates, type SafeComposePluginServices, type SafeComposePluginStates, type SafeExtractMiddlewareServices, type SafeExtractMiddlewareState, type SafeExtractPluginServices, type SafeExtractPluginState, type Server, ServerAPI, type ServerOptions, type ServerOptionsInput, type Services, type StandardErrorResponse, type StartOptions, type State, type StopOptions, type StreamMetrics, type StreamOptions, type TimeoutErrorContext, type TypedSSEStream, UnauthorizedError, type UnauthorizedErrorDetails, type UnifiedRequest, type UnifiedResponse, type UnionToIntersection, type UnknownFunction, type UnknownServer, UnprocessableEntityError, UnsupportedMediaTypeError, type UnsupportedMediaTypeErrorDetails, type UploadProgress, type UploadedFile, VERSION, type ValidationConfig, ValidationError, type ValidationErrorDetails, type ValidationFieldError, type WatchOptions, asMiddlewareArray, asPluginArray, compose, createDeleteRoute, createGetRoute, createHeadRoute, create$2 as createMiddleware, createMiddlewareArray, createOptionsRoute, createPatchRoute, create$1 as createPlugin, createPluginArray, createPostRoute, createPutRoute, createRouteFactory, create as createServer, serviceMiddleware as createServiceMiddleware, stateMiddleware as createStateMiddleware, getCorrelationId, inferContext, isBodyParseError, isMiddleware, isPlugin };
|