( isFastModeActive?: boolean, )
| 845 | } |
| 846 | |
| 847 | export function checkMockFastModeRateLimit( |
| 848 | isFastModeActive?: boolean, |
| 849 | ): MockHeaders | null { |
| 850 | if (mockFastModeRateLimitDurationMs === null) { |
| 851 | return null |
| 852 | } |
| 853 | |
| 854 | // Only throw when fast mode is active |
| 855 | if (!isFastModeActive) { |
| 856 | return null |
| 857 | } |
| 858 | |
| 859 | // Check if the rate limit has expired |
| 860 | if ( |
| 861 | mockFastModeRateLimitExpiresAt !== null && |
| 862 | Date.now() >= mockFastModeRateLimitExpiresAt |
| 863 | ) { |
| 864 | clearMockHeaders() |
| 865 | return null |
| 866 | } |
| 867 | |
| 868 | // Set expiry on first error (not when scenario is configured) |
| 869 | if (mockFastModeRateLimitExpiresAt === null) { |
| 870 | mockFastModeRateLimitExpiresAt = |
| 871 | Date.now() + mockFastModeRateLimitDurationMs |
| 872 | } |
| 873 | |
| 874 | // Compute dynamic retry-after based on remaining time |
| 875 | const remainingMs = mockFastModeRateLimitExpiresAt - Date.now() |
| 876 | const headersToSend = { ...mockHeaders } |
| 877 | headersToSend['retry-after'] = String( |
| 878 | Math.max(1, Math.ceil(remainingMs / 1000)), |
| 879 | ) |
| 880 | |
| 881 | return headersToSend |
| 882 | } |
| 883 |
no test coverage detected