-
-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent
instanceof
handler check failures between different MS…
…W versions (#2349)
- Loading branch information
1 parent
3135575
commit 28d26bd
Showing
10 changed files
with
105 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type HandlerKind = 'RequestHandler' | 'EventHandler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { GraphQLHandler } from '../../handlers/GraphQLHandler' | ||
import { HttpHandler } from '../../handlers/HttpHandler' | ||
import { RequestHandler } from '../../handlers/RequestHandler' | ||
import { WebSocketHandler } from '../../handlers/WebSocketHandler' | ||
import { isHandlerKind } from './isHandlerKind' | ||
|
||
it('returns true if expected a request handler and given a request handler', () => { | ||
expect( | ||
isHandlerKind('RequestHandler')(new HttpHandler('*', '*', () => {})), | ||
).toBe(true) | ||
|
||
expect( | ||
isHandlerKind('RequestHandler')( | ||
new GraphQLHandler('all', '*', '*', () => {}), | ||
), | ||
).toBe(true) | ||
}) | ||
|
||
it('returns true if expected a request handler and given a custom request handler', () => { | ||
class MyHandler extends RequestHandler { | ||
constructor() { | ||
super({ info: { header: '*' }, resolver: () => {} }) | ||
} | ||
predicate = () => false | ||
log() {} | ||
} | ||
|
||
expect(isHandlerKind('RequestHandler')(new MyHandler())).toBe(true) | ||
}) | ||
|
||
it('returns false if expected a request handler but given event handler', () => { | ||
expect(isHandlerKind('RequestHandler')(new WebSocketHandler('*'))).toBe(false) | ||
}) | ||
|
||
it('returns false if expected a request handler but given arbitrary object', () => { | ||
expect(isHandlerKind('RequestHandler')(undefined)).toBe(false) | ||
expect(isHandlerKind('RequestHandler')(null)).toBe(false) | ||
expect(isHandlerKind('RequestHandler')({})).toBe(false) | ||
expect(isHandlerKind('RequestHandler')([])).toBe(false) | ||
expect(isHandlerKind('RequestHandler')(123)).toBe(false) | ||
expect(isHandlerKind('RequestHandler')('hello')).toBe(false) | ||
}) | ||
|
||
it('returns true if expected an event handler and given an event handler', () => { | ||
expect(isHandlerKind('EventHandler')(new WebSocketHandler('*'))).toBe(true) | ||
}) | ||
|
||
it('returns true if expected an event handler and given a custom event handler', () => { | ||
class MyEventHandler extends WebSocketHandler { | ||
constructor() { | ||
super('*') | ||
} | ||
} | ||
expect(isHandlerKind('EventHandler')(new MyEventHandler())).toBe(true) | ||
}) | ||
|
||
it('returns false if expected an event handler but given arbitrary object', () => { | ||
expect(isHandlerKind('EventHandler')(undefined)).toBe(false) | ||
expect(isHandlerKind('EventHandler')(null)).toBe(false) | ||
expect(isHandlerKind('EventHandler')({})).toBe(false) | ||
expect(isHandlerKind('EventHandler')([])).toBe(false) | ||
expect(isHandlerKind('EventHandler')(123)).toBe(false) | ||
expect(isHandlerKind('EventHandler')('hello')).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { HandlerKind } from '../../handlers/common' | ||
import type { RequestHandler } from '../../handlers/RequestHandler' | ||
import type { WebSocketHandler } from '../../handlers/WebSocketHandler' | ||
|
||
/** | ||
* A filter function that ensures that the provided argument | ||
* is a handler of the given kind. This helps differentiate | ||
* between different kinds of handlers, e.g. request and event handlers. | ||
*/ | ||
export function isHandlerKind<K extends HandlerKind>(kind: K) { | ||
return ( | ||
input: unknown, | ||
): input is K extends 'EventHandler' ? WebSocketHandler : RequestHandler => { | ||
return ( | ||
input != null && | ||
typeof input === 'object' && | ||
'__kind' in input && | ||
input.__kind === kind | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters