rhinobase / hono-rate-limiter

Rate Limit middleware for Hono Web Server
https://hono-rate-limiter.vercel.app
MIT License
219 stars 11 forks source link

Middleware @acpr/rate-limit-postgresql store not working in latest Bun or Node #11

Closed fzn0x closed 4 months ago

fzn0x commented 4 months ago

Step to reproduce: try to use the @acpr/rate-limit-postgresql store

const limiter = rateLimiter({
  windowMs: 15 * 60 * 1000, // 15 minutes
  limit: 1, // Limit each IP to 100 requests per `window` (here, per 15 minutes).
  standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers; draft-7: combined `RateLimit` header
  keyGenerator: (c) => crypto.randomUUID(), // Method to generate custom identifiers for clients.
  // @ts-ignore: unknown store config types
  store: new postgresStores.PostgresStore(
    {
      connectionString: DATABASE_URL,
    },
    "aggregated_store"
  ),
});

app.use(limiter);

Started server http://localhost:3000 2 | 3 | Object.defineProperty(exports, '__esModule', { value: true }); 4 | 5 | var factory = require('hono/factory'); 6 | 7 | const getResetSeconds=(resetTime,windowMs)=>{let resetSeconds;if(resetTime){const deltaSeconds=Math.ceil((resetTime.getTime()-Date.now())/1e3);resetSeconds=Math.max(0,deltaSeconds);}else if(windowMs){resetSeconds=Math.ceil(windowMs/1e3);}return resetSeconds};const setDraft6Headers=(context,info,windowMs)=>{if(context.finalized)return;const windowSeconds=Math.ceil(windowMs/1e3);const resetSeconds=getResetSeconds(info.resetTime);context.header("RateLimit-Policy",${info.limit};w=${windowSeconds});context.header("RateLimit-Limit",info.limit.toString());context.header("RateLimit-Remaining",info.remaining.toString());if(resetSeconds)context.header("RateLimit-Reset",resetSeconds.toString());};const setDraft7Headers=(context,info,windowMs)=>{if(context.finalized)return;const windowSeconds=Math.ceil(windowMs/1e3);const resetSeconds=getResetSeconds(info.resetTime,windowMs);context.header("RateLimit-Policy",${info.limit};w=${windowSeconds});context.header("RateLimit",`limit=${info.limit}, remaining=${info.remaining} | ... truncated

TypeError: resetTime.getTime is not a function. (In 'resetTime.getTime()', 'resetTime.getTime' is undefined) at getResetSeconds (D:\number-website\node_modules\hono-rate-limiter\index.cjs.js:7:107) at setDraft6Headers (D:\number-website\node_modules\hono-rate-limiter\index.cjs.js:7:400) at D:\number-website\node_modules\hono-rate-limiter\index.cjs.js:13:1650

MathurAditya724 commented 4 months ago

Were any migrations required in your DB to use @acpr/rate-limit-postgresql?

MathurAditya724 commented 4 months ago

I am not sure, why you are receiving this error. When I try it, it works as normal (other than the type error, which I believe you already know #10).

Seems to be working fine. If you are still getting any errors, can you share a reproduction?

fzn0x commented 4 months ago

Great answer but I decide to move to Redis instead and it's working fine, thanks for your attention @MathurAditya724.

This issue may be useful for someone later, but I close it as not planned for now.