-
Notifications
You must be signed in to change notification settings - Fork 6
feat(transport): add rate limiting for messages and connections #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
45f474b
812d8b1
b3c635d
768eeea
76e30e2
6b90047
040914f
bf3b1d1
7092a0a
e9e725a
63ee982
621ecd6
9d29f68
61ca207
15dfc1e
bf458d0
c8a1768
c4950a0
0242e6e
f93baba
fe715d8
f215020
6be75fa
31427c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
|
|
||
| import { isResourceLimitError } from './isResourceLimitError.ts'; | ||
| import { ResourceLimitError } from '../errors/ResourceLimitError.ts'; | ||
|
|
||
| describe('isResourceLimitError', () => { | ||
| describe('without limitType parameter', () => { | ||
| it('returns true for ResourceLimitError', () => { | ||
| const error = new ResourceLimitError('limit exceeded'); | ||
| expect(isResourceLimitError(error)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for ResourceLimitError with any limitType', () => { | ||
| const connectionError = new ResourceLimitError('connection limit', { | ||
| data: { limitType: 'connection' }, | ||
| }); | ||
| const rateError = new ResourceLimitError('rate limit', { | ||
| data: { limitType: 'connectionRate' }, | ||
| }); | ||
|
|
||
| expect(isResourceLimitError(connectionError)).toBe(true); | ||
| expect(isResourceLimitError(rateError)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for regular Error', () => { | ||
| const error = new Error('some error'); | ||
| expect(isResourceLimitError(error)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for null', () => { | ||
| expect(isResourceLimitError(null)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for undefined', () => { | ||
| expect(isResourceLimitError(undefined)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for non-error objects', () => { | ||
| expect(isResourceLimitError({ message: 'fake error' })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with limitType parameter', () => { | ||
| it('returns true when limitType matches', () => { | ||
| const error = new ResourceLimitError('connection limit', { | ||
| data: { limitType: 'connection' }, | ||
| }); | ||
| expect(isResourceLimitError(error, 'connection')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false when limitType does not match', () => { | ||
| const error = new ResourceLimitError('connection limit', { | ||
| data: { limitType: 'connection' }, | ||
| }); | ||
| expect(isResourceLimitError(error, 'connectionRate')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false when error has no limitType', () => { | ||
| const error = new ResourceLimitError('limit exceeded'); | ||
| expect(isResourceLimitError(error, 'connection')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for non-ResourceLimitError even with matching-like data', () => { | ||
| const error = new Error('some error'); | ||
| expect(isResourceLimitError(error, 'connection')).toBe(false); | ||
| }); | ||
|
|
||
| it.each([ | ||
| 'connection', | ||
| 'connectionRate', | ||
| 'messageSize', | ||
| 'messageRate', | ||
| ] as const)('correctly identifies %s limitType', (limitType) => { | ||
| const error = new ResourceLimitError('limit exceeded', { | ||
| data: { limitType }, | ||
| }); | ||
| expect(isResourceLimitError(error, limitType)).toBe(true); | ||
| }); | ||
| }); | ||
| }); | ||
|
Comment on lines
+6
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LG in this PR. This whole suite tests
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think its ok, not that difficult to read |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { ResourceLimitError } from '../errors/ResourceLimitError.ts'; | ||
| import type { | ||
| ResourceLimitType, | ||
| ResourceLimitErrorData, | ||
| } from '../errors/ResourceLimitError.ts'; | ||
|
|
||
| /** | ||
| * Check if an error is a ResourceLimitError, optionally with a specific limit type. | ||
| * | ||
| * @param error - The error to check. | ||
| * @param limitType - Optional limit type to match against. | ||
| * @returns True if the error is a ResourceLimitError (with matching limitType if specified). | ||
| */ | ||
| export function isResourceLimitError( | ||
| error: unknown, | ||
| limitType?: ResourceLimitType, | ||
| ): error is ResourceLimitError { | ||
| if (!(error instanceof ResourceLimitError)) { | ||
| return false; | ||
| } | ||
|
|
||
| if (limitType === undefined) { | ||
| return true; | ||
| } | ||
|
|
||
| const data = error.data as ResourceLimitErrorData | undefined; | ||
| return data?.limitType === limitType; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.