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