Skip to content

Commit

Permalink
fix: use private "__kind" property instead of instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 6, 2024
1 parent 468aa2f commit 09ef3ef
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
import { SetupWorkerInternalContext, StartOptions } from '../glossary'
import type { RequiredDeep } from '~/core/typeUtils'
import { handleRequest } from '~/core/utils/handleRequest'
import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'

export function createFallbackRequestListener(
context: SetupWorkerInternalContext,
Expand Down
2 changes: 1 addition & 1 deletion src/browser/setupWorker/start/createRequestListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { handleRequest } from '~/core/utils/handleRequest'
import { RequiredDeep } from '~/core/typeUtils'
import { devUtils } from '~/core/utils/internal/devUtils'
import { toResponseInit } from '~/core/utils/toResponseInit'
import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'

export const createRequestListener = (
context: SetupWorkerInternalContext,
Expand Down
3 changes: 3 additions & 0 deletions src/core/handlers/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export abstract class RequestHandler<
StrictRequest<DefaultBodyType>
>()

private readonly __kind: 'RequestHandler'

public info: HandlerInfo & RequestHandlerInternalInfo
/**
* Indicates whether this request handler has been used
Expand Down Expand Up @@ -151,6 +153,7 @@ export abstract class RequestHandler<
}

this.isUsed = false
this.__kind = 'RequestHandler'
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/core/handlers/WebSocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const kStopPropagationPatched = Symbol('kStopPropagationPatched')
const KOnStopPropagation = Symbol('KOnStopPropagation')

export class WebSocketHandler {
private readonly __kind: 'WebSocketHandler'

public id: string
public callFrame?: string

Expand All @@ -38,6 +40,7 @@ export class WebSocketHandler {

this[kEmitter] = new Emitter()
this.callFrame = getCallFrame(new Error())
this.__kind = 'WebSocketHandler'
}

public parse(args: {
Expand Down
40 changes: 40 additions & 0 deletions src/core/utils/internal/toRequestHandlersOnly.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { GraphQLHandler } from '../../handlers/GraphQLHandler'
import { HttpHandler } from '../../handlers/HttpHandler'
import { RequestHandler } from '../../handlers/RequestHandler'
import { WebSocketHandler } from '../../handlers/WebSocketHandler'
import { toRequestHandlersOnly } from './toRequestHandlersOnly'

it('returns true for HttpHandler', () => {
expect(toRequestHandlersOnly(new HttpHandler('*', '*', () => {}))).toBe(true)
})

it('returns true for GraphQLHandler', () => {
expect(
toRequestHandlersOnly(new GraphQLHandler('all', '*', '*', () => {})),
).toBe(true)
})

it('returns true for a custom RequestHandler', () => {
class MyHandler extends RequestHandler {
constructor() {
super({ info: { header: '*' }, resolver: () => {} })
}
predicate = () => false
log() {}
}

expect(toRequestHandlersOnly(new MyHandler())).toBe(true)
})

it('returns false for a WebSocketHandler', () => {
expect(toRequestHandlersOnly(new WebSocketHandler('*'))).toBe(false)
})

it('returns false for an arbitrary values', () => {
expect(toRequestHandlersOnly(undefined)).toBe(false)
expect(toRequestHandlersOnly(null)).toBe(false)
expect(toRequestHandlersOnly({})).toBe(false)
expect(toRequestHandlersOnly([])).toBe(false)
expect(toRequestHandlersOnly(123)).toBe(false)
expect(toRequestHandlersOnly('hello')).toBe(false)
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ import { RequestHandler } from '../../handlers/RequestHandler'
* out other handlers, like `WebSocketHandler`.
*/
export function toRequestHandlersOnly(input: unknown): input is RequestHandler {
return input instanceof RequestHandler
return (
input != null &&
typeof input === 'object' &&
'__kind' in input &&
input.__kind === 'RequestHandler'
)
}
2 changes: 1 addition & 1 deletion src/node/SetupServerCommonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
import type { SetupServerCommon } from './glossary'
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
import { toRequestHandlersOnly } from '~/core/utils/internal/filterRequestHandlers'
import { toRequestHandlersOnly } from '~/core/utils/internal/toRequestHandlersOnly'

export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
onUnhandledRequest: 'warn',
Expand Down

0 comments on commit 09ef3ef

Please sign in to comment.