@@ -74,6 +74,29 @@ function createRateLimit(options = {}) {
7474 skip,
7575 } = options
7676
77+ /**
78+ * Helper function to add rate limit headers to a response
79+ * @param {Response } response - Response object to add headers to
80+ * @param {number } totalHits - Current hit count
81+ * @param {Date } resetTime - When the rate limit resets
82+ * @returns {Response } Response with headers added
83+ */
84+ const addRateLimitHeaders = ( response , totalHits , resetTime ) => {
85+ if ( standardHeaders && response && response . headers ) {
86+ response . headers . set ( 'X-RateLimit-Limit' , max . toString ( ) )
87+ response . headers . set (
88+ 'X-RateLimit-Remaining' ,
89+ Math . max ( 0 , max - totalHits ) . toString ( ) ,
90+ )
91+ response . headers . set (
92+ 'X-RateLimit-Reset' ,
93+ Math . ceil ( resetTime . getTime ( ) / 1000 ) . toString ( ) ,
94+ )
95+ response . headers . set ( 'X-RateLimit-Used' , totalHits . toString ( ) )
96+ }
97+ return response
98+ }
99+
77100 return async function rateLimitMiddleware ( req , next ) {
78101 // Allow test to inject a fresh store for isolation
79102 const activeStore = req && req . rateLimitStore ? req . rateLimitStore : store
@@ -92,22 +115,6 @@ function createRateLimit(options = {}) {
92115 const key = await keyGenerator ( req )
93116 const { totalHits, resetTime} = await activeStore . increment ( key , windowMs )
94117
95- const createResponse = ( response ) => {
96- if ( standardHeaders && response && response . headers ) {
97- response . headers . set ( 'X-RateLimit-Limit' , max . toString ( ) )
98- response . headers . set (
99- 'X-RateLimit-Remaining' ,
100- Math . max ( 0 , max - totalHits ) . toString ( ) ,
101- )
102- response . headers . set (
103- 'X-RateLimit-Reset' ,
104- Math . ceil ( resetTime . getTime ( ) / 1000 ) . toString ( ) ,
105- )
106- response . headers . set ( 'X-RateLimit-Used' , totalHits . toString ( ) )
107- }
108- return response
109- }
110-
111118 if ( totalHits > max ) {
112119 let response
113120
@@ -121,7 +128,7 @@ function createRateLimit(options = {}) {
121128 }
122129 }
123130
124- return createResponse ( response )
131+ return addRateLimitHeaders ( response , totalHits , resetTime )
125132 }
126133
127134 // Set rate limit context
@@ -143,7 +150,7 @@ function createRateLimit(options = {}) {
143150
144151 const response = await next ( )
145152 if ( response instanceof Response ) {
146- return createResponse ( response )
153+ return addRateLimitHeaders ( response , totalHits , resetTime )
147154 }
148155 return response
149156 } catch ( error ) {
@@ -173,7 +180,7 @@ function createRateLimit(options = {}) {
173180
174181 const response = await next ( )
175182 if ( response instanceof Response ) {
176- return response
183+ return addRateLimitHeaders ( response , totalHits , resetTime )
177184 }
178185 return response
179186 } catch ( e ) {
0 commit comments