Skip to content
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

feat: add dangerouslyRunInProduction flag to worker and server options #1791

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/browser/setupWorker/glossary.ts
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
scr2em marked this conversation as resolved.
Show resolved Hide resolved
}

export type StartReturnType = Promise<ServiceWorkerRegistration | undefined>
Expand Down
9 changes: 9 additions & 0 deletions src/browser/setupWorker/setupWorker.ts
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think if we phrase this error a bit better? Perhaps something like this?

  • Failed to call "setupWorker" in a production environment. Please make sure you enable API mocking conditionally so it doesn't leak to production.

Don't even mention the flag that controls it to emphasize the importance of this behavior. Still include a link to the docs about the flag for those who know what they are doing and want to suppress this behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kettanaito let me know the link when you finish it

),
'https://github.com/mswjs/msw/issues/1703',
scr2em marked this conversation as resolved.
Show resolved Hide resolved
)

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

Expand Down
1 change: 1 addition & 0 deletions src/browser/setupWorker/start/utils/prepareStartHandler.ts
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
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
@@ -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
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