@velajs/vela 0.3.0 → 0.4.0

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.
package/dist/index.d.ts CHANGED
@@ -18,6 +18,8 @@ export { EventEmitterModule, EventEmitter, EventEmitterSubscriber, OnEvent, ON_E
18
18
  export type { EventHandler, OnEventMetadata } from './event-emitter/index';
19
19
  export { ScheduleModule, ScheduleRegistry, ScheduleExecutor, Cron, Interval, SCHEDULE_MODULE_OPTIONS, CRON_METADATA, INTERVAL_METADATA, } from './schedule/index';
20
20
  export type { RegisteredCronJob, RegisteredIntervalJob, CronMetadata, IntervalMetadata, ScheduleModuleOptions, } from './schedule/index';
21
+ export { ThrottlerModule, ThrottlerGuard, ThrottlerStorage, Throttle, SkipThrottle, THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA, } from './throttler/index';
22
+ export type { ThrottlerModuleOptions, ThrottleConfig, ThrottlerStore, ThrottlerStorageRecord, RateLimitInfo, } from './throttler/index';
21
23
  export { Module } from './module/index';
22
24
  export type { ModuleOptions, DynamicModule } from './module/index';
23
25
  export { UseMiddleware, UseGuards, UsePipes, UseInterceptors, UseFilters, Catch, SetMetadata, Reflector, APP_GUARD, APP_PIPE, APP_INTERCEPTOR, APP_FILTER, APP_MIDDLEWARE, } from './pipeline/index';
package/dist/index.js CHANGED
@@ -21,6 +21,8 @@ export { CacheModule, CacheService, CacheInterceptor, MemoryCacheStore, CacheKey
21
21
  export { EventEmitterModule, EventEmitter, EventEmitterSubscriber, OnEvent, ON_EVENT_METADATA } from "./event-emitter/index.js";
22
22
  // Schedule
23
23
  export { ScheduleModule, ScheduleRegistry, ScheduleExecutor, Cron, Interval, SCHEDULE_MODULE_OPTIONS, CRON_METADATA, INTERVAL_METADATA } from "./schedule/index.js";
24
+ // Throttler
25
+ export { ThrottlerModule, ThrottlerGuard, ThrottlerStorage, Throttle, SkipThrottle, THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from "./throttler/index.js";
24
26
  // Module
25
27
  export { Module } from "./module/index.js";
26
28
  // Pipeline Decorators
@@ -0,0 +1,6 @@
1
+ export { ThrottlerModule } from './throttler.module';
2
+ export { ThrottlerGuard } from './throttler.guard';
3
+ export { ThrottlerStorage } from './throttler.storage';
4
+ export { Throttle, SkipThrottle } from './throttler.decorators';
5
+ export { THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from './throttler.tokens';
6
+ export type { ThrottlerModuleOptions, ThrottleConfig, ThrottlerStore, ThrottlerStorageRecord, RateLimitInfo } from './throttler.types';
@@ -0,0 +1,5 @@
1
+ export { ThrottlerModule } from "./throttler.module.js";
2
+ export { ThrottlerGuard } from "./throttler.guard.js";
3
+ export { ThrottlerStorage } from "./throttler.storage.js";
4
+ export { Throttle, SkipThrottle } from "./throttler.decorators.js";
5
+ export { THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from "./throttler.tokens.js";
@@ -0,0 +1,3 @@
1
+ import type { ThrottleConfig } from './throttler.types';
2
+ export declare const Throttle: (config: ThrottleConfig) => (target: Function | object, propertyKey?: string | symbol) => void;
3
+ export declare const SkipThrottle: () => (target: Function | object, propertyKey?: string | symbol) => void;
@@ -0,0 +1,4 @@
1
+ import { SetMetadata } from "../pipeline/reflector.js";
2
+ import { THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from "./throttler.tokens.js";
3
+ export const Throttle = (config)=>SetMetadata(THROTTLE_METADATA, config);
4
+ export const SkipThrottle = ()=>SetMetadata(SKIP_THROTTLE_METADATA, true);
@@ -0,0 +1,9 @@
1
+ import type { CanActivate, ExecutionContext } from '../pipeline/types';
2
+ import type { ThrottlerModuleOptions, ThrottlerStore } from './throttler.types';
3
+ export declare class ThrottlerGuard implements CanActivate {
4
+ private options;
5
+ private storage;
6
+ private reflector;
7
+ constructor(options: ThrottlerModuleOptions, storage: ThrottlerStore);
8
+ canActivate(context: ExecutionContext): Promise<boolean>;
9
+ }
@@ -0,0 +1,72 @@
1
+ function _ts_decorate(decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ }
7
+ function _ts_metadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+ function _ts_param(paramIndex, decorator) {
11
+ return function(target, key) {
12
+ decorator(target, key, paramIndex);
13
+ };
14
+ }
15
+ import { Injectable, Inject } from "../container/decorators.js";
16
+ import { Reflector } from "../pipeline/reflector.js";
17
+ import { TooManyRequestsException } from "../errors/http-exception.js";
18
+ import { THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from "./throttler.tokens.js";
19
+ export class ThrottlerGuard {
20
+ options;
21
+ storage;
22
+ reflector = new Reflector();
23
+ constructor(options, storage){
24
+ this.options = options;
25
+ this.storage = storage;
26
+ }
27
+ async canActivate(context) {
28
+ const skip = this.reflector.getAllAndOverride(SKIP_THROTTLE_METADATA, context);
29
+ if (skip) {
30
+ return true;
31
+ }
32
+ const override = this.reflector.getAllAndOverride(THROTTLE_METADATA, context);
33
+ const limit = override?.limit ?? this.options.limit;
34
+ const ttl = override?.ttl ?? this.options.ttl;
35
+ const request = context.getRequest();
36
+ const tracker = this.options.getTracker ? this.options.getTracker(request) : request.headers.get('x-forwarded-for') ?? 'anonymous';
37
+ const className = context.getClass().name;
38
+ const handlerName = String(context.getHandler());
39
+ const key = this.options.generateKey ? this.options.generateKey(tracker, {
40
+ className,
41
+ handlerName
42
+ }) : `throttler:${className}:${handlerName}:${tracker}`;
43
+ const { count, ttlMs } = await this.storage.increment(key, ttl);
44
+ const remaining = Math.max(0, limit - count);
45
+ const resetSeconds = Math.ceil(ttlMs / 1000);
46
+ const honoContext = context.getContext();
47
+ honoContext.header('X-RateLimit-Limit', String(limit));
48
+ honoContext.header('X-RateLimit-Remaining', String(remaining));
49
+ honoContext.header('X-RateLimit-Reset', String(resetSeconds));
50
+ const rateLimitInfo = {
51
+ limit,
52
+ remaining,
53
+ reset: resetSeconds
54
+ };
55
+ honoContext.set('rateLimit', rateLimitInfo);
56
+ if (count > limit) {
57
+ honoContext.header('Retry-After', String(resetSeconds));
58
+ throw new TooManyRequestsException();
59
+ }
60
+ return true;
61
+ }
62
+ }
63
+ ThrottlerGuard = _ts_decorate([
64
+ Injectable(),
65
+ _ts_param(0, Inject(THROTTLER_OPTIONS)),
66
+ _ts_param(1, Inject(THROTTLER_STORAGE)),
67
+ _ts_metadata("design:type", Function),
68
+ _ts_metadata("design:paramtypes", [
69
+ typeof ThrottlerModuleOptions === "undefined" ? Object : ThrottlerModuleOptions,
70
+ typeof ThrottlerStore === "undefined" ? Object : ThrottlerStore
71
+ ])
72
+ ], ThrottlerGuard);
@@ -0,0 +1,5 @@
1
+ import type { DynamicModule } from '../module/types';
2
+ import type { ThrottlerModuleOptions } from './throttler.types';
3
+ export declare class ThrottlerModule {
4
+ static forRoot(options: ThrottlerModuleOptions): DynamicModule;
5
+ }
@@ -0,0 +1,43 @@
1
+ import { METADATA_KEYS } from "../constants.js";
2
+ import { defineMetadata } from "../metadata.js";
3
+ import { MetadataRegistry } from "../registry/metadata.registry.js";
4
+ import { APP_GUARD } from "../pipeline/tokens.js";
5
+ import { ThrottlerGuard } from "./throttler.guard.js";
6
+ import { ThrottlerStorage } from "./throttler.storage.js";
7
+ import { THROTTLER_OPTIONS, THROTTLER_STORAGE } from "./throttler.tokens.js";
8
+ export class ThrottlerModule {
9
+ static forRoot(options) {
10
+ const moduleClass = class ThrottlerDynamicModule {
11
+ };
12
+ Object.defineProperty(moduleClass, 'name', {
13
+ value: 'ThrottlerModule'
14
+ });
15
+ defineMetadata(METADATA_KEYS.MODULE, true, moduleClass);
16
+ MetadataRegistry.setModuleOptions(moduleClass, {
17
+ exports: [
18
+ THROTTLER_OPTIONS,
19
+ THROTTLER_STORAGE,
20
+ ThrottlerGuard
21
+ ]
22
+ });
23
+ const providers = [
24
+ {
25
+ token: THROTTLER_OPTIONS,
26
+ useValue: options
27
+ },
28
+ {
29
+ token: THROTTLER_STORAGE,
30
+ useValue: options.storage ?? new ThrottlerStorage()
31
+ },
32
+ ThrottlerGuard,
33
+ {
34
+ token: APP_GUARD,
35
+ useExisting: ThrottlerGuard
36
+ }
37
+ ];
38
+ return {
39
+ module: moduleClass,
40
+ providers
41
+ };
42
+ }
43
+ }
@@ -0,0 +1,10 @@
1
+ import type { ThrottlerStore, ThrottlerStorageRecord } from './throttler.types';
2
+ export declare class ThrottlerStorage implements ThrottlerStore {
3
+ private current;
4
+ private previous;
5
+ private lastSwap;
6
+ private readonly swapIntervalMs;
7
+ increment(key: string, ttlMs: number): ThrottlerStorageRecord;
8
+ reset(key: string): void;
9
+ private maybeSwap;
10
+ }
@@ -0,0 +1,44 @@
1
+ function _ts_decorate(decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ }
7
+ import { Injectable } from "../container/decorators.js";
8
+ export class ThrottlerStorage {
9
+ current = new Map();
10
+ previous = new Map();
11
+ lastSwap = Date.now();
12
+ swapIntervalMs = 60_000;
13
+ increment(key, ttlMs) {
14
+ this.maybeSwap();
15
+ const now = Date.now();
16
+ let entry = this.current.get(key) ?? this.previous.get(key);
17
+ if (!entry || now >= entry.resetTime) {
18
+ entry = {
19
+ count: 0,
20
+ resetTime: now + ttlMs
21
+ };
22
+ }
23
+ entry.count++;
24
+ this.current.set(key, entry);
25
+ return {
26
+ count: entry.count,
27
+ ttlMs: entry.resetTime - now
28
+ };
29
+ }
30
+ reset(key) {
31
+ this.current.delete(key);
32
+ this.previous.delete(key);
33
+ }
34
+ maybeSwap() {
35
+ const now = Date.now();
36
+ if (now - this.lastSwap < this.swapIntervalMs) return;
37
+ this.previous = this.current;
38
+ this.current = new Map();
39
+ this.lastSwap = now;
40
+ }
41
+ }
42
+ ThrottlerStorage = _ts_decorate([
43
+ Injectable()
44
+ ], ThrottlerStorage);
@@ -0,0 +1,6 @@
1
+ import { InjectionToken } from '../container/types';
2
+ import type { ThrottlerModuleOptions, ThrottlerStore } from './throttler.types';
3
+ export declare const THROTTLER_OPTIONS: InjectionToken<ThrottlerModuleOptions>;
4
+ export declare const THROTTLER_STORAGE: InjectionToken<ThrottlerStore>;
5
+ export declare const THROTTLE_METADATA = "vela:throttle";
6
+ export declare const SKIP_THROTTLE_METADATA = "vela:skip-throttle";
@@ -0,0 +1,5 @@
1
+ import { InjectionToken } from "../container/types.js";
2
+ export const THROTTLER_OPTIONS = new InjectionToken('THROTTLER_OPTIONS');
3
+ export const THROTTLER_STORAGE = new InjectionToken('THROTTLER_STORAGE');
4
+ export const THROTTLE_METADATA = 'vela:throttle';
5
+ export const SKIP_THROTTLE_METADATA = 'vela:skip-throttle';
@@ -0,0 +1,25 @@
1
+ export interface ThrottleConfig {
2
+ limit: number;
3
+ ttl: number;
4
+ }
5
+ export interface ThrottlerStorageRecord {
6
+ count: number;
7
+ ttlMs: number;
8
+ }
9
+ export interface ThrottlerStore {
10
+ increment(key: string, ttlMs: number): ThrottlerStorageRecord | Promise<ThrottlerStorageRecord>;
11
+ reset(key: string): void | Promise<void>;
12
+ }
13
+ export interface RateLimitInfo {
14
+ limit: number;
15
+ remaining: number;
16
+ reset: number;
17
+ }
18
+ export interface ThrottlerModuleOptions extends ThrottleConfig {
19
+ storage?: ThrottlerStore;
20
+ getTracker?: (request: Request) => string;
21
+ generateKey?: (tracker: string, context: {
22
+ className: string;
23
+ handlerName: string;
24
+ }) => string;
25
+ }
@@ -0,0 +1 @@
1
+ export { };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velajs/vela",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "NestJS-compatible framework for edge runtimes, powered by Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",