@metamask-previews/rate-limit-controller 7.0.0-preview-ee982ebe → 7.0.0-preview-1a8571ae

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.
@@ -1 +1 @@
1
- {"version":3,"file":"RateLimitController.cjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":";;;AAAA,+DAImC;AAEnC,qDAAiD;AACjD,2CAAwD;AAsCxD,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwC7C,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;QACzB,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,KAAK;QAC7B,QAAQ,EAAE,KAAK;KAChB;CACF,CAAC;AAEF;;GAEG;AACH,MAAa,mBAEX,SAAQ,gCAIT;IAOC;;;;;;;;;OASG;IACH,YAAY,EACV,gBAAgB,GAAG,IAAI,EACvB,cAAc,GAAG,CAAC,EAClB,SAAS,EACT,KAAK,EACL,eAAe,GAOhB;QACC,MAAM,YAAY,GAAG;YACnB,QAAQ,EAAE,IAAA,6BAAqB,EAAC,eAAe,CAAC,CAAC,MAAM,CAErD,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAW,CAAC;SACtD,CAAC;QACF,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ;YACR,SAAS;YACT,KAAK,EAAE,EAAE,GAAG,YAAY,EAAE,GAAG,KAAK,EAAE;SACrC,CAAC,CAAC;QACH,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;QACzC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QAErC,IAAI,CAAC,SAAS,CAAC,qBAAqB,CAClC,GAAG,cAAc,OAAO,EACxB,CACE,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AAC,CAAC;QACvD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;aACzE;YACD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlJD,kDAkJC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: ActionConstraint['handler'];\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['method']>\n ) => this.call(origin, type, ...args),\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n * @returns The result of the API call.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['method']>;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args);\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\n });\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
1
+ {"version":3,"file":"RateLimitController.cjs","sour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CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC;YAC1E,CAAC;YACD,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAlJD,kDAkJC","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: ActionConstraint['handler'];\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['method']>\n ) => this.call(origin, type, ...args),\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n * @returns The result of the API call.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['method']>;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args);\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\n });\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"RateLimitController.mjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAGf,kCAAkC;AAEnC,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,q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AAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,gFAAgF;oBAChF,qEAAqE;oBACrE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: ActionConstraint['handler'];\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['method']>\n ) => this.call(origin, type, ...args),\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n * @returns The result of the API call.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['method']>;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args);\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\n });\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
1
+ {"version":3,"file":"RateLimitController.mjs","sourceRoot":"","sources":["../src/RateLimitController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAGf,kCAAkC;AAEnC,OAAO,EAAE,SAAS,EAAE,6BAA6B;AACjD,OAAO,EAAE,qBAAqB,EAAE,wBAAwB;AAsCxD,MAAM,cAAc,GAAG,qBAAqB,CAAC;AAwC7C,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE;QACR,kBAAkB,EAAE,KAAK;Q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AC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE;iBAClC;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;;;;OAKG;IACK,iBAAiB,CAAC,GAA0B,EAAE,MAAc;QAClE,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;gBACnB,QAAQ,EAAE;oBACR,GAAI,KAAK,CAAC,QAAiD;oBAC3D,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE;iBACvB;aACF,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["import {\n BaseController,\n type ControllerGetStateAction,\n type ControllerStateChangeEvent,\n} from '@metamask/base-controller';\nimport type { Messenger, ActionConstraint } from '@metamask/messenger';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { getKnownPropertyNames } from '@metamask/utils';\n\n/**\n * A rate-limited API endpoint.\n *\n * @property method - The method that is rate-limited.\n * @property rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @property rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\nexport type RateLimitedApi = {\n method: ActionConstraint['handler'];\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n};\n\n/**\n * A map of rate-limited API types to APIs.\n */\nexport type RateLimitedApiMap = Record<string, RateLimitedApi>;\n\n/**\n * A map of rate-limited API types to the number of requests made in a given interval for each origin and api type combination.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n */\nexport type RateLimitedRequests<RateLimitedApis extends RateLimitedApiMap> =\n Record<keyof RateLimitedApis, Record<string, number>>;\n\n/**\n * The state of the {@link RateLimitController}.\n *\n * @template RateLimitedApis - A {@link RateLimitedApiMap} containing the rate-limited API endpoints that is used by the {@link RateLimitController}.\n * @property requests - An object containing the number of requests made in a given interval for each origin and api type combination.\n */\nexport type RateLimitState<RateLimitedApis extends RateLimitedApiMap> = {\n requests: RateLimitedRequests<RateLimitedApis>;\n};\n\nconst controllerName = 'RateLimitController';\n\nexport type RateLimitControllerStateChangeEvent<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerStateChangeEvent<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerGetStateAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = ControllerGetStateAction<\n typeof controllerName,\n RateLimitState<RateLimitedApis>\n>;\n\nexport type RateLimitControllerCallApiAction<\n RateLimitedApis extends RateLimitedApiMap,\n> = {\n type: `${typeof controllerName}:call`;\n handler: RateLimitController<RateLimitedApis>['call'];\n};\n\nexport type RateLimitControllerActions<\n RateLimitedApis extends RateLimitedApiMap,\n> =\n | RateLimitControllerGetStateAction<RateLimitedApis>\n | RateLimitControllerCallApiAction<RateLimitedApis>;\n\nexport type RateLimitControllerEvents<\n RateLimitedApis extends RateLimitedApiMap,\n> = RateLimitControllerStateChangeEvent<RateLimitedApis>;\n\nexport type RateLimitMessenger<RateLimitedApis extends RateLimitedApiMap> =\n Messenger<\n typeof controllerName,\n RateLimitControllerActions<RateLimitedApis>,\n RateLimitControllerEvents<RateLimitedApis>\n >;\n\nconst metadata = {\n requests: {\n includeInStateLogs: false,\n persist: false,\n includeInDebugSnapshot: false,\n usedInUi: false,\n },\n};\n\n/**\n * Controller with logic for rate-limiting API endpoints per requesting origin.\n */\nexport class RateLimitController<\n RateLimitedApis extends RateLimitedApiMap,\n> extends BaseController<\n typeof controllerName,\n RateLimitState<RateLimitedApis>,\n RateLimitMessenger<RateLimitedApis>\n> {\n private readonly implementations;\n\n private readonly rateLimitTimeout;\n\n private readonly rateLimitCount;\n\n /**\n * Creates a RateLimitController instance.\n *\n * @param options - Constructor options.\n * @param options.messenger - A reference to the messaging system.\n * @param options.state - Initial state to set on this controller.\n * @param options.implementations - Mapping from API type to API implementation.\n * @param options.rateLimitTimeout - The time window in which the rate limit is applied (in ms).\n * @param options.rateLimitCount - The amount of calls an origin can make in the rate limit time window.\n */\n constructor({\n rateLimitTimeout = 5000,\n rateLimitCount = 1,\n messenger,\n state,\n implementations,\n }: {\n rateLimitTimeout?: number;\n rateLimitCount?: number;\n messenger: RateLimitMessenger<RateLimitedApis>;\n state?: Partial<RateLimitState<RateLimitedApis>>;\n implementations: RateLimitedApis;\n }) {\n const defaultState = {\n requests: getKnownPropertyNames(implementations).reduce<\n RateLimitedRequests<RateLimitedApis>\n >((acc, key) => ({ ...acc, [key]: {} }), {} as never),\n };\n super({\n name: controllerName,\n metadata,\n messenger,\n state: { ...defaultState, ...state },\n });\n this.implementations = implementations;\n this.rateLimitTimeout = rateLimitTimeout;\n this.rateLimitCount = rateLimitCount;\n\n this.messenger.registerActionHandler(\n `${controllerName}:call`,\n (\n origin: string,\n type: keyof RateLimitedApis,\n ...args: Parameters<RateLimitedApis[typeof type]['method']>\n ) => this.call(origin, type, ...args),\n );\n }\n\n /**\n * Calls an API if the requesting origin is not rate-limited.\n *\n * @param origin - The requesting origin.\n * @param type - The type of API call to make.\n * @param args - Arguments for the API call.\n * @returns The result of the API call.\n */\n async call<ApiType extends keyof RateLimitedApis>(\n origin: string,\n type: ApiType,\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ): Promise<ReturnType<RateLimitedApis[ApiType]['method']>> {\n if (this.isRateLimited(type, origin)) {\n throw rpcErrors.limitExceeded({\n message: `\"${type.toString()}\" is currently rate-limited. Please try again later.`,\n });\n }\n this.recordRequest(type, origin);\n\n const implementation = this.implementations[type].method as (\n ...args: Parameters<RateLimitedApis[ApiType]['method']>\n ) => ReturnType<RateLimitedApis[ApiType]['method']>;\n\n if (!implementation) {\n throw new Error('Invalid api type');\n }\n\n return implementation(...args);\n }\n\n /**\n * Checks whether an origin is rate limited for the a specific API.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n * @returns `true` if rate-limited, and `false` otherwise.\n */\n private isRateLimited(api: keyof RateLimitedApis, origin: string) {\n const rateLimitCount =\n this.implementations[api].rateLimitCount ?? this.rateLimitCount;\n return this.state.requests[api][origin] >= rateLimitCount;\n }\n\n /**\n * Records that an origin has made a request to call an API, for rate-limiting purposes.\n *\n * @param api - The API the origin is trying to access.\n * @param origin - The origin trying to access the API.\n */\n private recordRequest(api: keyof RateLimitedApis, origin: string) {\n const rateLimitTimeout =\n this.implementations[api].rateLimitTimeout ?? this.rateLimitTimeout;\n const previous = this.state.requests[api][origin] ?? 0;\n this.update((state) => {\n if (previous === 0) {\n setTimeout(() => this.resetRequestCount(api, origin), rateLimitTimeout);\n }\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n [api]: { [origin]: previous + 1 },\n },\n });\n });\n }\n\n /**\n * Resets the request count for a given origin and API combination, for rate-limiting purposes.\n *\n * @param api - The API in question.\n * @param origin - The origin in question.\n */\n private resetRequestCount(api: keyof RateLimitedApis, origin: string) {\n this.update((state) => {\n Object.assign(state, {\n requests: {\n ...(state.requests as RateLimitedRequests<RateLimitedApis>),\n [api]: { [origin]: 0 },\n },\n });\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/rate-limit-controller",
3
- "version": "7.0.0-preview-ee982ebe",
3
+ "version": "7.0.0-preview-1a8571ae",
4
4
  "description": "Contains logic for rate-limiting API endpoints by requesting origin",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -62,7 +62,7 @@
62
62
  "ts-jest": "^27.1.4",
63
63
  "typedoc": "^0.24.8",
64
64
  "typedoc-plugin-missing-exports": "^2.0.0",
65
- "typescript": "~5.2.2"
65
+ "typescript": "~5.3.3"
66
66
  },
67
67
  "engines": {
68
68
  "node": "^18.18 || >=20"