From 7afaff6cfb1108fcba93d608e33641ae1f75ddbc Mon Sep 17 00:00:00 2001 From: scr2em Date: Mon, 23 Oct 2023 22:23:01 +0300 Subject: [PATCH] feat: add dangerouslyRunInProduction flag to worker and server options #1703 --- src/browser/setupWorker/glossary.ts | 5 +++++ src/browser/setupWorker/setupWorker.ts | 9 +++++++++ .../setupWorker/start/utils/prepareStartHandler.ts | 1 + src/core/sharedOptions.ts | 4 ++++ src/core/utils/internal/isProduction.ts | 3 +++ src/node/SetupServerApi.ts | 9 +++++++++ 6 files changed, 31 insertions(+) create mode 100644 src/core/utils/internal/isProduction.ts diff --git a/src/browser/setupWorker/glossary.ts b/src/browser/setupWorker/glossary.ts index 23017c6e..f162da3f 100644 --- a/src/browser/setupWorker/glossary.ts +++ b/src/browser/setupWorker/glossary.ts @@ -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 diff --git a/src/browser/setupWorker/setupWorker.ts b/src/browser/setupWorker/setupWorker.ts index b8937664..3ad9519f 100644 --- a/src/browser/setupWorker/setupWorker.ts +++ b/src/browser/setupWorker/setupWorker.ts @@ -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 @@ -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) } diff --git a/src/browser/setupWorker/start/utils/prepareStartHandler.ts b/src/browser/setupWorker/start/utils/prepareStartHandler.ts index e98fe832..bb320d4f 100644 --- a/src/browser/setupWorker/start/utils/prepareStartHandler.ts +++ b/src/browser/setupWorker/start/utils/prepareStartHandler.ts @@ -15,6 +15,7 @@ export const DEFAULT_START_OPTIONS: RequiredDeep = { quiet: false, waitUntilReady: true, onUnhandledRequest: 'warn', + dangerouslyRunInProduction: false, findWorker(scriptURL, mockServiceWorkerUrl) { return scriptURL === mockServiceWorkerUrl }, diff --git a/src/core/sharedOptions.ts b/src/core/sharedOptions.ts index ad7f151a..23f5d58f 100644 --- a/src/core/sharedOptions.ts +++ b/src/core/sharedOptions.ts @@ -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 = { diff --git a/src/core/utils/internal/isProduction.ts b/src/core/utils/internal/isProduction.ts new file mode 100644 index 00000000..96836a10 --- /dev/null +++ b/src/core/utils/internal/isProduction.ts @@ -0,0 +1,3 @@ +export function isProduction(): boolean { + return process && process.env.NODE_ENV === 'production' +} diff --git a/src/node/SetupServerApi.ts b/src/node/SetupServerApi.ts index a451f521..713ef808 100644 --- a/src/node/SetupServerApi.ts +++ b/src/node/SetupServerApi.ts @@ -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 = { onUnhandledRequest: 'warn', + dangerouslyRunInProduction: false, } export class SetupServerApi @@ -123,6 +125,13 @@ export class SetupServerApi options, ) as RequiredDeep + 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()