@orpc/experimental-ratelimit 0.0.0 → 0.0.1

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.
@@ -25,7 +25,8 @@ class MemoryRatelimiter {
25
25
  this.lastCleanupTime = now;
26
26
  const windowStart = now - this.window;
27
27
  for (const [key, timestamps] of this.store) {
28
- timestamps.splice(0, timestamps.findIndex((timestamp) => timestamp < windowStart) + 1);
28
+ const idx = timestamps.findIndex((timestamp) => timestamp >= windowStart);
29
+ timestamps.splice(0, idx === -1 ? timestamps.length : idx);
29
30
  if (timestamps.length === 0) {
30
31
  this.store.delete(key);
31
32
  }
@@ -36,7 +37,8 @@ class MemoryRatelimiter {
36
37
  const windowStart = now - this.window;
37
38
  let timestamps = this.store.get(key);
38
39
  if (timestamps) {
39
- timestamps.splice(0, timestamps.findIndex((timestamp) => timestamp < windowStart) + 1);
40
+ const idx = timestamps.findIndex((timestamp) => timestamp >= windowStart);
41
+ timestamps.splice(0, idx === -1 ? timestamps.length : idx);
40
42
  } else {
41
43
  this.store.set(key, timestamps = []);
42
44
  }
@@ -1,7 +1,7 @@
1
1
  import { a as Ratelimiter, R as RatelimiterLimitResult } from '../shared/experimental-ratelimit.C8zlaHwR.mjs';
2
2
 
3
3
  interface RedisRatelimiterOptions {
4
- eval: (script: string, numKeys: number, ...args: string[]) => Promise<unknown>;
4
+ eval: (script: string, numKeys: number, ...rest: string[]) => Promise<unknown>;
5
5
  /**
6
6
  * Block until the request may pass or timeout is reached.
7
7
  */
@@ -1,7 +1,7 @@
1
1
  import { a as Ratelimiter, R as RatelimiterLimitResult } from '../shared/experimental-ratelimit.C8zlaHwR.js';
2
2
 
3
3
  interface RedisRatelimiterOptions {
4
- eval: (script: string, numKeys: number, ...args: string[]) => Promise<unknown>;
4
+ eval: (script: string, numKeys: number, ...rest: string[]) => Promise<unknown>;
5
5
  /**
6
6
  * Block until the request may pass or timeout is reached.
7
7
  */
@@ -65,7 +65,14 @@ class RedisRatelimiter {
65
65
  if (!Array.isArray(result) || result.length !== 4) {
66
66
  throw new TypeError("Invalid response from rate limit script");
67
67
  }
68
- const [success, limit, remaining, reset] = result;
68
+ const numbers = result.map((item) => {
69
+ const num = Number(item);
70
+ if (!Number.isInteger(num)) {
71
+ throw new TypeError("Invalid response from rate limit script");
72
+ }
73
+ return num;
74
+ });
75
+ const [success, limit, remaining, reset] = numbers;
69
76
  return {
70
77
  success: success === 1,
71
78
  limit,
@@ -17,7 +17,7 @@ interface UpstashRatelimiterOptions {
17
17
  * On Vercel Edge or Cloudflare workers, you might need `.bind` before assign:
18
18
  * ```ts
19
19
  * const ratelimiter = new UpstashRatelimiter(ratelimit, {
20
- * waitUtil: ctx.waitUntil.bind(ctx),
20
+ * waitUntil: ctx.waitUntil.bind(ctx),
21
21
  * })
22
22
  * ```
23
23
  */
@@ -17,7 +17,7 @@ interface UpstashRatelimiterOptions {
17
17
  * On Vercel Edge or Cloudflare workers, you might need `.bind` before assign:
18
18
  * ```ts
19
19
  * const ratelimiter = new UpstashRatelimiter(ratelimit, {
20
- * waitUtil: ctx.waitUntil.bind(ctx),
20
+ * waitUntil: ctx.waitUntil.bind(ctx),
21
21
  * })
22
22
  * ```
23
23
  */
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ import { R as RatelimiterLimitResult, a as Ratelimiter } from './shared/experime
4
4
  import { Value, Promisable } from '@orpc/shared';
5
5
 
6
6
  declare const RATELIMIT_HANDLER_CONTEXT_SYMBOL: unique symbol;
7
- interface RatelimitHandlerPluginContext extends Context {
7
+ interface RatelimitHandlerPluginContext {
8
8
  [RATELIMIT_HANDLER_CONTEXT_SYMBOL]?: {
9
9
  /**
10
10
  * The result of the ratelimiter after applying limits
@@ -12,12 +12,7 @@ interface RatelimitHandlerPluginContext extends Context {
12
12
  ratelimitResult?: RatelimiterLimitResult;
13
13
  };
14
14
  }
15
- declare class RatelimitHandlerPlugin<T extends RatelimitHandlerPluginContext> implements StandardHandlerPlugin<T> {
16
- /**
17
- * this plugin should lower priority than response headers plugin,
18
- * if user want override rate limit headers
19
- */
20
- order: number;
15
+ declare class RatelimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
21
16
  init(options: StandardHandlerOptions<T>): void;
22
17
  }
23
18
 
@@ -43,7 +38,7 @@ interface CreateRatelimitMiddlewareOptions<TInContext extends Context, TInput, T
43
38
  */
44
39
  key: Value<Promisable<string>, [middlewareOptions: MiddlewareOptions<TInContext, unknown, Record<never, never>, TMeta>, input: TInput]>;
45
40
  /**
46
- * If you ratelimit middleware is used multiple times
41
+ * If your ratelimit middleware is used multiple times
47
42
  * or you invoke a procedure inside another procedure (shared the same context) that also has
48
43
  * ratelimit middleware **with the same limiter and key**, this option
49
44
  * will ensure that the limit is only applied once per request.
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { R as RatelimiterLimitResult, a as Ratelimiter } from './shared/experime
4
4
  import { Value, Promisable } from '@orpc/shared';
5
5
 
6
6
  declare const RATELIMIT_HANDLER_CONTEXT_SYMBOL: unique symbol;
7
- interface RatelimitHandlerPluginContext extends Context {
7
+ interface RatelimitHandlerPluginContext {
8
8
  [RATELIMIT_HANDLER_CONTEXT_SYMBOL]?: {
9
9
  /**
10
10
  * The result of the ratelimiter after applying limits
@@ -12,12 +12,7 @@ interface RatelimitHandlerPluginContext extends Context {
12
12
  ratelimitResult?: RatelimiterLimitResult;
13
13
  };
14
14
  }
15
- declare class RatelimitHandlerPlugin<T extends RatelimitHandlerPluginContext> implements StandardHandlerPlugin<T> {
16
- /**
17
- * this plugin should lower priority than response headers plugin,
18
- * if user want override rate limit headers
19
- */
20
- order: number;
15
+ declare class RatelimitHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
21
16
  init(options: StandardHandlerOptions<T>): void;
22
17
  }
23
18
 
@@ -43,7 +38,7 @@ interface CreateRatelimitMiddlewareOptions<TInContext extends Context, TInput, T
43
38
  */
44
39
  key: Value<Promisable<string>, [middlewareOptions: MiddlewareOptions<TInContext, unknown, Record<never, never>, TMeta>, input: TInput]>;
45
40
  /**
46
- * If you ratelimit middleware is used multiple times
41
+ * If your ratelimit middleware is used multiple times
47
42
  * or you invoke a procedure inside another procedure (shared the same context) that also has
48
43
  * ratelimit middleware **with the same limiter and key**, this option
49
44
  * will ensure that the limit is only applied once per request.
package/dist/index.mjs CHANGED
@@ -3,14 +3,9 @@ import { value, toArray } from '@orpc/shared';
3
3
 
4
4
  const RATELIMIT_HANDLER_CONTEXT_SYMBOL = Symbol("ORPC_RATE_LIMIT_HANDLER_CONTEXT");
5
5
  class RatelimitHandlerPlugin {
6
- /**
7
- * this plugin should lower priority than response headers plugin,
8
- * if user want override rate limit headers
9
- */
10
- order = 1e5;
11
6
  init(options) {
12
7
  options.rootInterceptors ??= [];
13
- options.rootInterceptors.push(async (interceptorOptions) => {
8
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
14
9
  const handlerContext = {};
15
10
  const result = await interceptorOptions.next({
16
11
  ...interceptorOptions,
@@ -67,7 +62,7 @@ function createRatelimitMiddleware({ dedupe = true, ...options }) {
67
62
  return middlewareOptions.next({
68
63
  context: {
69
64
  [RATELIMIT_MIDDLEWARE_CONTEXT_SYMBOL]: {
70
- ...middlewareOptions,
65
+ ...middlewareContext,
71
66
  limits: [
72
67
  ...toArray(middlewareContext?.limits),
73
68
  { limiter, key }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/experimental-ratelimit",
3
3
  "type": "module",
4
- "version": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -49,8 +49,8 @@
49
49
  "dependencies": {
50
50
  "@orpc/client": "1.10.3",
51
51
  "@orpc/server": "1.10.3",
52
- "@orpc/shared": "1.10.3",
53
- "@orpc/standard-server": "1.10.3"
52
+ "@orpc/standard-server": "1.10.3",
53
+ "@orpc/shared": "1.10.3"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@upstash/ratelimit": "^2.0.7",