Skip to content

Commit

Permalink
feat: add dangerouslyRunInProduction flag to worker and server options
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelgawad1 committed Oct 23, 2023
1 parent a54138a commit 98c73f3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/browser/setupWorker/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ export interface StartOptions extends SharedOptions {
* of all registered Service Workers on the page.
*/
findWorker?: FindWorker

/**
* A flag to enable MSW in production (NODE_ENV === 'production'), otherwise it will throw an error
*/
dangerouslyRunInProduction?: boolean
}

export type StartReturnType = Promise<ServiceWorkerRegistration | undefined>
Expand Down
9 changes: 9 additions & 0 deletions src/browser/setupWorker/setupWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { mergeRight } from '~/core/utils/internal/mergeRight'
import { LifeCycleEventsMap } from '~/core/sharedOptions'
import { SetupWorker } from './glossary'
import { supportsReadableStreamTransfer } from '../utils/supportsReadableStreamTransfer'
import { isProduction } from '~/core/utils/internal/isProduction'

interface Listener {
target: EventTarget
Expand Down Expand Up @@ -177,6 +178,14 @@ export class SetupWorkerApi
options,
) as SetupWorkerInternalContext['startOptions']

invariant(
!this.context.startOptions.dangerouslyRunInProduction && isProduction(),
devUtils.formatMessage(
'The flag dangerouslyRunInProduction is false but you are in a production environment',
),
'https://github.com/mswjs/msw/issues/1703',
)

return await this.startHandler(this.context.startOptions, options)
}

Expand Down
1 change: 1 addition & 0 deletions src/browser/setupWorker/start/utils/prepareStartHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const DEFAULT_START_OPTIONS: RequiredDeep<StartOptions> = {
quiet: false,
waitUntilReady: true,
onUnhandledRequest: 'warn',
dangerouslyRunInProduction: false,
findWorker(scriptURL, mockServiceWorkerUrl) {
return scriptURL === mockServiceWorkerUrl
},
Expand Down
4 changes: 4 additions & 0 deletions src/core/sharedOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export interface SharedOptions {
* @example server.listen({ onUnhandledRequest: 'error' })
*/
onUnhandledRequest?: UnhandledRequestStrategy
/**
* A flag to enable MSW in production (NODE_ENV === 'production'), otherwise it will throw an error
*/
dangerouslyRunInProduction?: boolean
}

export type LifeCycleEventsMap = {
Expand Down
3 changes: 3 additions & 0 deletions src/core/utils/internal/isProduction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isProduction(): boolean {
return process && process.env.NODE_ENV === 'production'
}
9 changes: 9 additions & 0 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import { handleRequest } from '~/core/utils/handleRequest'
import { devUtils } from '~/core/utils/internal/devUtils'
import { SetupServer } from './glossary'
import { isNodeException } from './utils/isNodeException'
import { isProduction } from '~/core/utils/internal/isProduction'

const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
onUnhandledRequest: 'warn',
dangerouslyRunInProduction: false,
}

export class SetupServerApi
Expand Down Expand Up @@ -123,6 +125,13 @@ export class SetupServerApi
options,
) as RequiredDeep<SharedOptions>

invariant(
!this.resolvedOptions.dangerouslyRunInProduction && isProduction(),
devUtils.formatMessage(
'The flag dangerouslyRunInProduction is false but you are in a production environment',
),
'https://github.com/mswjs/msw/issues/1703',
)
// Apply the interceptor when starting the server.
this.interceptor.apply()

Expand Down

0 comments on commit 98c73f3

Please sign in to comment.