msw 2.2.3 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/cli/init.js +1 -1
  2. package/lib/browser/index.js +33 -36
  3. package/lib/browser/index.js.map +1 -1
  4. package/lib/browser/index.mjs +33 -36
  5. package/lib/browser/index.mjs.map +1 -1
  6. package/lib/core/{GraphQLHandler-AenIUdwE.d.mts → GraphQLHandler-BumTfgY4.d.mts} +1 -1
  7. package/lib/core/{GraphQLHandler-jOzqbxSK.d.ts → GraphQLHandler-DqVT0Gbq.d.ts} +1 -1
  8. package/lib/core/HttpResponse.d.mts +1 -1
  9. package/lib/core/HttpResponse.d.ts +1 -1
  10. package/lib/core/SetupApi.d.mts +1 -1
  11. package/lib/core/SetupApi.d.ts +1 -1
  12. package/lib/core/getResponse.d.mts +1 -1
  13. package/lib/core/getResponse.d.ts +1 -1
  14. package/lib/core/graphql.d.mts +2 -2
  15. package/lib/core/graphql.d.ts +2 -2
  16. package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
  17. package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
  18. package/lib/core/handlers/HttpHandler.d.mts +1 -1
  19. package/lib/core/handlers/HttpHandler.d.ts +1 -1
  20. package/lib/core/handlers/RequestHandler.d.mts +1 -1
  21. package/lib/core/handlers/RequestHandler.d.ts +1 -1
  22. package/lib/core/http.d.mts +1 -1
  23. package/lib/core/http.d.ts +1 -1
  24. package/lib/core/index.d.mts +2 -2
  25. package/lib/core/index.d.ts +2 -2
  26. package/lib/core/passthrough.d.mts +1 -1
  27. package/lib/core/passthrough.d.ts +1 -1
  28. package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
  29. package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
  30. package/lib/core/utils/HttpResponse/decorators.js +4 -1
  31. package/lib/core/utils/HttpResponse/decorators.js.map +1 -1
  32. package/lib/core/utils/HttpResponse/decorators.mjs +4 -1
  33. package/lib/core/utils/HttpResponse/decorators.mjs.map +1 -1
  34. package/lib/core/utils/executeHandlers.d.mts +1 -1
  35. package/lib/core/utils/executeHandlers.d.ts +1 -1
  36. package/lib/core/utils/handleRequest.d.mts +1 -1
  37. package/lib/core/utils/handleRequest.d.ts +1 -1
  38. package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
  39. package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
  40. package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
  41. package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
  42. package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
  43. package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
  44. package/lib/iife/index.js +21 -23
  45. package/lib/iife/index.js.map +1 -1
  46. package/lib/mockServiceWorker.js +7 -3
  47. package/package.json +6 -9
  48. package/src/browser/setupWorker/glossary.ts +4 -1
  49. package/src/browser/setupWorker/start/createStartHandler.ts +9 -19
  50. package/src/browser/utils/checkWorkerIntegrity.ts +34 -0
  51. package/src/core/utils/HttpResponse/decorators.ts +8 -4
  52. package/src/mockServiceWorker.js +6 -2
  53. package/src/browser/utils/requestIntegrityCheck.ts +0 -23
  54. /package/lib/core/{HttpResponse-wcp03c7-.d.mts → HttpResponse-C94XLD-W.d.mts} +0 -0
  55. /package/lib/core/{HttpResponse-_514VQ9z.d.ts → HttpResponse-jHZ0p2dL.d.ts} +0 -0
@@ -2,13 +2,14 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (2.2.3).
5
+ * Mock Service Worker.
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const INTEGRITY_CHECKSUM = '223d191a56023cd36aa88c802961b911'
11
+ const PACKAGE_VERSION = '2.2.5'
12
+ const INTEGRITY_CHECKSUM = '5db7e6e8385dc04e017ac4823e0e9b29'
12
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
13
14
  const activeClientIds = new Set()
14
15
 
@@ -48,7 +49,10 @@ self.addEventListener('message', async function (event) {
48
49
  case 'INTEGRITY_CHECK_REQUEST': {
49
50
  sendToClient(client, {
50
51
  type: 'INTEGRITY_CHECK_RESPONSE',
51
- payload: INTEGRITY_CHECKSUM,
52
+ payload: {
53
+ packageVersion: PACKAGE_VERSION,
54
+ checksum: INTEGRITY_CHECKSUM,
55
+ },
52
56
  })
53
57
  break
54
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "Seamless REST/GraphQL API mocking library for browser and Node.js.",
5
5
  "main": "./lib/core/index.js",
6
6
  "module": "./lib/core/index.mjs",
@@ -73,10 +73,7 @@
73
73
  "url": "https://github.com/kettanaito"
74
74
  },
75
75
  "license": "MIT",
76
- "funding": {
77
- "type": "opencollective",
78
- "url": "https://opencollective.com/mswjs"
79
- },
76
+ "funding": "https://github.com/sponsors/mswjs",
80
77
  "files": [
81
78
  "config/constants.js",
82
79
  "config/scripts/postinstall.js",
@@ -126,7 +123,7 @@
126
123
  "@commitlint/cli": "^18.4.4",
127
124
  "@commitlint/config-conventional": "^18.4.4",
128
125
  "@open-draft/test-server": "^0.4.2",
129
- "@ossjs/release": "^0.8.0",
126
+ "@ossjs/release": "^0.8.1",
130
127
  "@playwright/test": "^1.40.1",
131
128
  "@swc/core": "^1.3.102",
132
129
  "@types/express": "^4.17.21",
@@ -134,8 +131,8 @@
134
131
  "@types/glob": "^8.1.0",
135
132
  "@types/json-bigint": "^1.0.4",
136
133
  "@types/node": "18.x",
137
- "@typescript-eslint/eslint-plugin": "^5.11.0",
138
- "@typescript-eslint/parser": "^5.11.0",
134
+ "@typescript-eslint/eslint-plugin": "^7.2.0",
135
+ "@typescript-eslint/parser": "^7.2.0",
139
136
  "@web/dev-server": "^0.1.38",
140
137
  "axios": "^1.6.5",
141
138
  "babel-minify": "^0.5.1",
@@ -145,7 +142,7 @@
145
142
  "cz-conventional-changelog": "3.3.0",
146
143
  "esbuild": "^0.19.11",
147
144
  "esbuild-loader": "^4.0.2",
148
- "eslint": "^8.56.0",
145
+ "eslint": "^8.57.0",
149
146
  "eslint-config-prettier": "^9.1.0",
150
147
  "eslint-plugin-prettier": "^5.1.3",
151
148
  "express": "^4.18.2",
@@ -54,7 +54,10 @@ export type ServiceWorkerIncomingResponse = Pick<
54
54
  */
55
55
  export interface ServiceWorkerIncomingEventsMap {
56
56
  MOCKING_ENABLED: boolean
57
- INTEGRITY_CHECK_RESPONSE: string
57
+ INTEGRITY_CHECK_RESPONSE: {
58
+ packageVersion: string
59
+ checksum: string
60
+ }
58
61
  KEEPALIVE_RESPONSE: never
59
62
  REQUEST: ServiceWorkerIncomingRequest
60
63
  RESPONSE: ServiceWorkerIncomingResponse
@@ -1,10 +1,9 @@
1
- import { until } from '@open-draft/until'
2
1
  import { devUtils } from '~/core/utils/internal/devUtils'
3
2
  import { getWorkerInstance } from './utils/getWorkerInstance'
4
3
  import { enableMocking } from './utils/enableMocking'
5
4
  import { SetupWorkerInternalContext, StartHandler } from '../glossary'
6
5
  import { createRequestListener } from './createRequestListener'
7
- import { requestIntegrityCheck } from '../../utils/requestIntegrityCheck'
6
+ import { checkWorkerIntegrity } from '../../utils/checkWorkerIntegrity'
8
7
  import { createResponseListener } from './createResponseListener'
9
8
  import { validateWorkerScope } from './utils/validateWorkerScope'
10
9
 
@@ -74,23 +73,14 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
74
73
  window.clearInterval(context.keepAliveInterval)
75
74
  })
76
75
 
77
- // Check if the active Service Worker is the latest published one
78
- const integrityCheckResult = await until(() =>
79
- requestIntegrityCheck(context, worker),
80
- )
81
-
82
- if (integrityCheckResult.error) {
83
- devUtils.error(`\
84
- Detected outdated Service Worker: ${integrityCheckResult.error.message}
85
-
86
- The mocking is still enabled, but it's highly recommended that you update your Service Worker by running:
87
-
88
- $ npx msw init <PUBLIC_DIR>
89
-
90
- This is necessary to ensure that the Service Worker is in sync with the library to guarantee its stability.
91
- If this message still persists after updating, please report an issue: https://github.com/open-draft/msw/issues\
92
- `)
93
- }
76
+ // Check if the active Service Worker has been generated
77
+ // by the currently installed version of MSW.
78
+ await checkWorkerIntegrity(context).catch((error) => {
79
+ devUtils.error(
80
+ 'Error while checking the worker script integrity. Please report this on GitHub (https://github.com/mswjs/msw/issues), including the original error below.',
81
+ )
82
+ console.error(error)
83
+ })
94
84
 
95
85
  context.keepAliveInterval = window.setInterval(
96
86
  () => context.workerChannel.send('KEEPALIVE_REQUEST'),
@@ -0,0 +1,34 @@
1
+ import { devUtils } from '~/core/utils/internal/devUtils'
2
+ import type { SetupWorkerInternalContext } from '../setupWorker/glossary'
3
+
4
+ /**
5
+ * Check whether the registered Service Worker has been
6
+ * generated by the installed version of the library.
7
+ * Prints a warning message if the worker scripts mismatch.
8
+ */
9
+ export async function checkWorkerIntegrity(
10
+ context: SetupWorkerInternalContext,
11
+ ): Promise<void> {
12
+ // Request the integrity checksum from the registered worker.
13
+ context.workerChannel.send('INTEGRITY_CHECK_REQUEST')
14
+
15
+ const { payload } = await context.events.once('INTEGRITY_CHECK_RESPONSE')
16
+
17
+ // Compare the response from the Service Worker and the
18
+ // global variable set during the build.
19
+
20
+ // The integrity is validated based on the worker script's checksum
21
+ // that's derived from its minified content during the build.
22
+ // The "SERVICE_WORKER_CHECKSUM" global variable is injected by the build.
23
+ if (payload.checksum !== SERVICE_WORKER_CHECKSUM) {
24
+ devUtils.warn(
25
+ `The currently registered Service Worker has been generated by a different version of MSW (${payload.packageVersion}) and may not be fully compatible with the installed version.
26
+
27
+ It's recommended you update your worker script by running this command:
28
+
29
+ \u2022 npx msw init <PUBLIC_DIR>
30
+
31
+ You can also automate this process and make the worker script update automatically upon the library installations. Read more: https://mswjs.io/docs/cli/init.`,
32
+ )
33
+ }
34
+ }
@@ -1,5 +1,6 @@
1
1
  import statuses from '@bundled-es-modules/statuses'
2
2
  import type { HttpResponseInit } from '../../HttpResponse'
3
+ import { Headers as HeadersPolyfill } from 'headers-polyfill'
3
4
 
4
5
  const { message } = statuses
5
6
 
@@ -40,10 +41,13 @@ export function decorateResponse(
40
41
  // Cookie forwarding is only relevant in the browser.
41
42
  if (typeof document !== 'undefined') {
42
43
  // Write the mocked response cookies to the document.
43
- // Note that Fetch API Headers will concatenate multiple "Set-Cookie"
44
- // headers into a single comma-separated string, just as it does
45
- // with any other multi-value headers.
46
- const responseCookies = init.headers.get('Set-Cookie')?.split(',') || []
44
+ // Use `headers-polyfill` to get the Set-Cookie header value correctly.
45
+ // This is an alternative until TypeScript 5.2
46
+ // and Node.js v20 become the minimum supported version
47
+ // and getSetCookie in Headers can be used directly.
48
+ const responseCookies = HeadersPolyfill.prototype.getSetCookie.call(
49
+ init.headers,
50
+ )
47
51
 
48
52
  for (const cookieString of responseCookies) {
49
53
  // No need to parse the cookie headers because it's defined
@@ -2,12 +2,13 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (<PACKAGE_VERSION>).
5
+ * Mock Service Worker.
6
6
  * @see https://github.com/mswjs/msw
7
7
  * - Please do NOT modify this file.
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
+ const PACKAGE_VERSION = '<PACKAGE_VERSION>'
11
12
  const INTEGRITY_CHECKSUM = '<INTEGRITY_CHECKSUM>'
12
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
13
14
  const activeClientIds = new Set()
@@ -48,7 +49,10 @@ self.addEventListener('message', async function (event) {
48
49
  case 'INTEGRITY_CHECK_REQUEST': {
49
50
  sendToClient(client, {
50
51
  type: 'INTEGRITY_CHECK_RESPONSE',
51
- payload: INTEGRITY_CHECKSUM,
52
+ payload: {
53
+ packageVersion: PACKAGE_VERSION,
54
+ checksum: INTEGRITY_CHECKSUM,
55
+ },
52
56
  })
53
57
  break
54
58
  }
@@ -1,23 +0,0 @@
1
- import type { SetupWorkerInternalContext } from '../setupWorker/glossary'
2
-
3
- export async function requestIntegrityCheck(
4
- context: SetupWorkerInternalContext,
5
- serviceWorker: ServiceWorker,
6
- ): Promise<ServiceWorker> {
7
- // Signal Service Worker to report back its integrity
8
- context.workerChannel.send('INTEGRITY_CHECK_REQUEST')
9
-
10
- const { payload: actualChecksum } = await context.events.once(
11
- 'INTEGRITY_CHECK_RESPONSE',
12
- )
13
-
14
- // Compare the response from the Service Worker and the
15
- // global variable set during the build.
16
- if (actualChecksum !== SERVICE_WORKER_CHECKSUM) {
17
- throw new Error(
18
- `Currently active Service Worker (${actualChecksum}) is behind the latest published one (${SERVICE_WORKER_CHECKSUM}).`,
19
- )
20
- }
21
-
22
- return serviceWorker
23
- }