msw 2.1.4 → 2.1.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 (47) hide show
  1. package/lib/browser/index.js +25 -12
  2. package/lib/browser/index.js.map +1 -1
  3. package/lib/browser/index.mjs +25 -12
  4. package/lib/browser/index.mjs.map +1 -1
  5. package/lib/core/handlers/GraphQLHandler.js +2 -2
  6. package/lib/core/handlers/GraphQLHandler.js.map +1 -1
  7. package/lib/core/handlers/GraphQLHandler.mjs +2 -2
  8. package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
  9. package/lib/core/handlers/HttpHandler.js +2 -2
  10. package/lib/core/handlers/HttpHandler.js.map +1 -1
  11. package/lib/core/handlers/HttpHandler.mjs +2 -2
  12. package/lib/core/handlers/HttpHandler.mjs.map +1 -1
  13. package/lib/core/utils/internal/parseGraphQLRequest.js +2 -2
  14. package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
  15. package/lib/core/utils/internal/parseGraphQLRequest.mjs +2 -2
  16. package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
  17. package/lib/core/utils/request/onUnhandledRequest.js +6 -2
  18. package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
  19. package/lib/core/utils/request/onUnhandledRequest.mjs +6 -2
  20. package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
  21. package/lib/core/utils/request/toPublicUrl.d.mts +7 -0
  22. package/lib/core/utils/request/toPublicUrl.d.ts +7 -0
  23. package/lib/core/utils/request/{getPublicUrlFromRequest.js → toPublicUrl.js} +9 -9
  24. package/lib/core/utils/request/toPublicUrl.js.map +1 -0
  25. package/lib/core/utils/request/toPublicUrl.mjs +11 -0
  26. package/lib/core/utils/request/toPublicUrl.mjs.map +1 -0
  27. package/lib/iife/index.js +37 -21
  28. package/lib/iife/index.js.map +1 -1
  29. package/lib/mockServiceWorker.js +1 -1
  30. package/package.json +5 -5
  31. package/src/browser/setupWorker/glossary.ts +1 -0
  32. package/src/browser/setupWorker/setupWorker.ts +1 -0
  33. package/src/browser/setupWorker/start/createRequestListener.ts +9 -0
  34. package/src/browser/setupWorker/start/createResponseListener.ts +9 -7
  35. package/src/core/handlers/GraphQLHandler.ts +2 -2
  36. package/src/core/handlers/HttpHandler.ts +2 -2
  37. package/src/core/utils/internal/parseGraphQLRequest.ts +2 -2
  38. package/src/core/utils/request/onUnhandledRequest.ts +14 -2
  39. package/src/core/utils/request/toPublicUrl.test.ts +18 -0
  40. package/src/core/utils/request/toPublicUrl.ts +15 -0
  41. package/lib/core/utils/request/getPublicUrlFromRequest.d.mts +0 -7
  42. package/lib/core/utils/request/getPublicUrlFromRequest.d.ts +0 -7
  43. package/lib/core/utils/request/getPublicUrlFromRequest.js.map +0 -1
  44. package/lib/core/utils/request/getPublicUrlFromRequest.mjs +0 -11
  45. package/lib/core/utils/request/getPublicUrlFromRequest.mjs.map +0 -1
  46. package/src/core/utils/request/getPublicUrlFromRequest.test.ts +0 -26
  47. package/src/core/utils/request/getPublicUrlFromRequest.ts +0 -15
@@ -2,7 +2,7 @@
2
2
  /* tslint:disable */
3
3
 
4
4
  /**
5
- * Mock Service Worker (2.1.4).
5
+ * Mock Service Worker (2.1.5).
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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.1.4",
3
+ "version": "2.1.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",
@@ -95,7 +95,7 @@
95
95
  "@bundled-es-modules/cookie": "^2.0.0",
96
96
  "@bundled-es-modules/statuses": "^1.0.1",
97
97
  "@mswjs/cookies": "^1.1.0",
98
- "@mswjs/interceptors": "^0.25.14",
98
+ "@mswjs/interceptors": "^0.25.15",
99
99
  "@open-draft/until": "^2.1.0",
100
100
  "@types/cookie": "^0.6.0",
101
101
  "@types/statuses": "^2.0.4",
@@ -189,10 +189,10 @@
189
189
  "check:exports": "node \"./config/scripts/validate-esm.js\"",
190
190
  "test": "pnpm test:unit && pnpm test:node && pnpm test:browser && pnpm test:native",
191
191
  "test:unit": "vitest",
192
- "test:node": "vitest run --config=./test/node/vitest.config.ts",
193
- "test:native": "vitest run --config=./test/native/vitest.config.ts",
192
+ "test:node": "vitest --config=./test/node/vitest.config.ts",
193
+ "test:native": "vitest --config=./test/native/vitest.config.ts",
194
194
  "test:browser": "playwright test -c ./test/browser/playwright.config.ts",
195
- "test:modules:node": "vitest run --config=./test/modules/node/vitest.config.ts",
195
+ "test:modules:node": "vitest --config=./test/modules/node/vitest.config.ts",
196
196
  "test:modules:browser": "playwright test -c ./test/modules/browser/playwright.config.ts",
197
197
  "test:ts": "ts-node test/typings/run.ts",
198
198
  "release": "release publish",
@@ -103,6 +103,7 @@ export interface SetupWorkerInternalContext {
103
103
  worker: ServiceWorker | null
104
104
  registration: ServiceWorkerRegistration | null
105
105
  requestHandlers: Array<RequestHandler>
106
+ requests: Map<string, Request>
106
107
  emitter: Emitter<LifeCycleEventsMap>
107
108
  keepAliveInterval?: number
108
109
  workerChannel: {
@@ -60,6 +60,7 @@ export class SetupWorkerApi
60
60
  worker: null,
61
61
  registration: null,
62
62
  requestHandlers: this.currentHandlers,
63
+ requests: new Map(),
63
64
  emitter: this.emitter,
64
65
  workerChannel: {
65
66
  on: (eventType, callback) => {
@@ -8,6 +8,7 @@ import {
8
8
  WorkerChannel,
9
9
  } from './utils/createMessageChannel'
10
10
  import { parseWorkerRequest } from '../../utils/parseWorkerRequest'
11
+ import { RequestHandler } from '~/core/handlers/RequestHandler'
11
12
  import { handleRequest } from '~/core/utils/handleRequest'
12
13
  import { RequiredDeep } from '~/core/typeUtils'
13
14
  import { devUtils } from '~/core/utils/internal/devUtils'
@@ -30,6 +31,14 @@ export const createRequestListener = (
30
31
  const request = parseWorkerRequest(message.payload)
31
32
  const requestCloneForLogs = request.clone()
32
33
 
34
+ // Make this the first requets clone before the
35
+ // request resolution pipeline even starts.
36
+ // Store the clone in cache so the first matching
37
+ // request handler would skip the cloning phase.
38
+ const requestClone = request.clone()
39
+ RequestHandler.cache.set(request, requestClone)
40
+ context.requests.set(requestId, requestClone)
41
+
33
42
  try {
34
43
  await handleRequest(
35
44
  request,
@@ -1,8 +1,8 @@
1
- import {
1
+ import type {
2
2
  ServiceWorkerIncomingEventsMap,
3
3
  SetupWorkerInternalContext,
4
4
  } from '../glossary'
5
- import { ServiceWorkerMessage } from './utils/createMessageChannel'
5
+ import type { ServiceWorkerMessage } from './utils/createMessageChannel'
6
6
  import { isResponseWithoutBody } from '@mswjs/interceptors'
7
7
 
8
8
  export function createResponseListener(context: SetupWorkerInternalContext) {
@@ -15,6 +15,12 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
15
15
  ) => {
16
16
  const { payload: responseJson } = message
17
17
 
18
+ // Get the Request instance reference stored in the
19
+ // request listener.
20
+ const { requestId } = responseJson
21
+ const request = context.requests.get(requestId)!
22
+ context.requests.delete(requestId)
23
+
18
24
  /**
19
25
  * CORS requests with `mode: "no-cors"` result in "opaque" responses.
20
26
  * That kind of responses cannot be manipulated in JavaScript due
@@ -46,11 +52,7 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
46
52
  responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
47
53
  {
48
54
  response,
49
- /**
50
- * @todo @fixme In this context, we don't know anything about
51
- * the request.
52
- */
53
- request: null as any,
55
+ request,
54
56
  requestId: responseJson.requestId,
55
57
  },
56
58
  )
@@ -17,7 +17,7 @@ import {
17
17
  parseGraphQLRequest,
18
18
  parseDocumentNode,
19
19
  } from '../utils/internal/parseGraphQLRequest'
20
- import { getPublicUrlFromRequest } from '../utils/request/getPublicUrlFromRequest'
20
+ import { toPublicUrl } from '../utils/request/toPublicUrl'
21
21
  import { devUtils } from '../utils/internal/devUtils'
22
22
  import { getAllRequestCookies } from '../utils/request/getRequestCookies'
23
23
 
@@ -200,7 +200,7 @@ export class GraphQLHandler extends RequestHandler<
200
200
  }
201
201
 
202
202
  if (!args.parsedResult.operationName && this.info.operationType !== 'all') {
203
- const publicUrl = getPublicUrlFromRequest(args.request)
203
+ const publicUrl = toPublicUrl(args.request.url)
204
204
 
205
205
  devUtils.warn(`\
206
206
  Failed to intercept a GraphQL request at "${args.request.method} ${publicUrl}": anonymous GraphQL operations are not supported.
@@ -11,7 +11,7 @@ import {
11
11
  Path,
12
12
  PathParams,
13
13
  } from '../utils/matching/matchRequestUrl'
14
- import { getPublicUrlFromRequest } from '../utils/request/getPublicUrlFromRequest'
14
+ import { toPublicUrl } from '../utils/request/toPublicUrl'
15
15
  import { getAllRequestCookies } from '../utils/request/getRequestCookies'
16
16
  import { cleanUrl, getSearchParams } from '../utils/url/cleanUrl'
17
17
  import {
@@ -147,7 +147,7 @@ export class HttpHandler extends RequestHandler<
147
147
  }
148
148
 
149
149
  async log(args: { request: Request; response: Response }) {
150
- const publicUrl = getPublicUrlFromRequest(args.request)
150
+ const publicUrl = toPublicUrl(args.request.url)
151
151
  const loggedRequest = await serializeRequest(args.request)
152
152
  const loggedResponse = await serializeResponse(args.response)
153
153
  const statusColor = getStatusCodeColor(loggedResponse.status)
@@ -5,7 +5,7 @@ import type {
5
5
  } from 'graphql'
6
6
  import { parse } from 'graphql'
7
7
  import type { GraphQLVariables } from '../../handlers/GraphQLHandler'
8
- import { getPublicUrlFromRequest } from '../request/getPublicUrlFromRequest'
8
+ import { toPublicUrl } from '../request/toPublicUrl'
9
9
  import { devUtils } from './devUtils'
10
10
  import { jsonParse } from './jsonParse'
11
11
  import { parseMultipartData } from './parseMultipartData'
@@ -184,7 +184,7 @@ export async function parseGraphQLRequest(
184
184
  const parsedResult = parseQuery(query)
185
185
 
186
186
  if (parsedResult instanceof Error) {
187
- const requestPublicUrl = getPublicUrlFromRequest(request)
187
+ const requestPublicUrl = toPublicUrl(request.url)
188
188
 
189
189
  throw new Error(
190
190
  devUtils.formatMessage(
@@ -1,4 +1,4 @@
1
- import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
1
+ import { toPublicUrl } from './toPublicUrl'
2
2
  import { devUtils } from '../internal/devUtils'
3
3
 
4
4
  export interface UnhandledRequestPrint {
@@ -21,7 +21,9 @@ export async function onUnhandledRequest(
21
21
  request: Request,
22
22
  strategy: UnhandledRequestStrategy = 'warn',
23
23
  ): Promise<void> {
24
- const publicUrl = getPublicUrlFromRequest(request)
24
+ const url = new URL(request.url)
25
+ const publicUrl = toPublicUrl(url)
26
+
25
27
  const unhandledRequestMessage = `intercepted a request without a matching request handler:\n\n \u2022 ${request.method} ${publicUrl}\n\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks`
26
28
 
27
29
  function applyStrategy(strategy: UnhandledRequestStrategy) {
@@ -64,5 +66,15 @@ export async function onUnhandledRequest(
64
66
  return
65
67
  }
66
68
 
69
+ /**
70
+ * @note Ignore "file://" requests.
71
+ * Those often are an implementation detail of modern tooling
72
+ * that fetches modules via HTTP. Developers don't issue those
73
+ * requests and so they mustn't be warned about them.
74
+ */
75
+ if (url.protocol === 'file:') {
76
+ return
77
+ }
78
+
67
79
  applyStrategy(strategy)
68
80
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @vitest-environment jsdom
3
+ */
4
+ import { toPublicUrl } from './toPublicUrl'
5
+
6
+ test('returns an absolute request URL withouth search params', () => {
7
+ expect(toPublicUrl(new URL('https://test.mswjs.io/path'))).toBe(
8
+ 'https://test.mswjs.io/path',
9
+ )
10
+
11
+ expect(toPublicUrl(new URL('http://localhost/path'))).toBe('/path')
12
+
13
+ expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe('/path')
14
+ })
15
+
16
+ it('returns a relative URL given the request to the same origin', () => {
17
+ expect(toPublicUrl('http://localhost/user')).toBe('/user')
18
+ })
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Returns a relative URL if the given request URL is relative
3
+ * to the current origin. Otherwise returns an absolute URL.
4
+ */
5
+ export function toPublicUrl(url: string | URL): string {
6
+ if (typeof location === 'undefined') {
7
+ return url.toString()
8
+ }
9
+
10
+ const urlInstance = url instanceof URL ? url : new URL(url)
11
+
12
+ return urlInstance.origin === location.origin
13
+ ? urlInstance.pathname
14
+ : urlInstance.origin + urlInstance.pathname
15
+ }
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- declare function getPublicUrlFromRequest(request: Request): string;
6
-
7
- export { getPublicUrlFromRequest };
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- declare function getPublicUrlFromRequest(request: Request): string;
6
-
7
- export { getPublicUrlFromRequest };
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/getPublicUrlFromRequest.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative to the current origin.\n * Otherwise returns an absolute URL.\n */\nexport function getPublicUrlFromRequest(request: Request): string {\n if (typeof location === 'undefined') {\n return request.url\n }\n\n const url = new URL(request.url)\n\n return url.origin === location.origin\n ? url.pathname\n : url.origin + url.pathname\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIO,SAAS,wBAAwB,SAA0B;AAChE,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,QAAQ;AAAA,EACjB;AAEA,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAE/B,SAAO,IAAI,WAAW,SAAS,SAC3B,IAAI,WACJ,IAAI,SAAS,IAAI;AACvB;","names":[]}
@@ -1,11 +0,0 @@
1
- function getPublicUrlFromRequest(request) {
2
- if (typeof location === "undefined") {
3
- return request.url;
4
- }
5
- const url = new URL(request.url);
6
- return url.origin === location.origin ? url.pathname : url.origin + url.pathname;
7
- }
8
- export {
9
- getPublicUrlFromRequest
10
- };
11
- //# sourceMappingURL=getPublicUrlFromRequest.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../../src/core/utils/request/getPublicUrlFromRequest.ts"],"sourcesContent":["/**\n * Returns a relative URL if the given request URL is relative to the current origin.\n * Otherwise returns an absolute URL.\n */\nexport function getPublicUrlFromRequest(request: Request): string {\n if (typeof location === 'undefined') {\n return request.url\n }\n\n const url = new URL(request.url)\n\n return url.origin === location.origin\n ? url.pathname\n : url.origin + url.pathname\n}\n"],"mappings":"AAIO,SAAS,wBAAwB,SAA0B;AAChE,MAAI,OAAO,aAAa,aAAa;AACnC,WAAO,QAAQ;AAAA,EACjB;AAEA,QAAM,MAAM,IAAI,IAAI,QAAQ,GAAG;AAE/B,SAAO,IAAI,WAAW,SAAS,SAC3B,IAAI,WACJ,IAAI,SAAS,IAAI;AACvB;","names":[]}
@@ -1,26 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
- import { getPublicUrlFromRequest } from './getPublicUrlFromRequest'
5
-
6
- test('returns an absolute request URL withouth search params', () => {
7
- expect(
8
- getPublicUrlFromRequest(new Request(new URL('https://test.mswjs.io/path'))),
9
- ).toBe('https://test.mswjs.io/path')
10
-
11
- expect(
12
- getPublicUrlFromRequest(new Request(new URL('http://localhost/path'))),
13
- ).toBe('/path')
14
-
15
- expect(
16
- getPublicUrlFromRequest(
17
- new Request(new URL('http://localhost/path?foo=bar')),
18
- ),
19
- ).toBe('/path')
20
- })
21
-
22
- it('returns a relative URL given the request to the same origin', () => {
23
- expect(getPublicUrlFromRequest(new Request('http://localhost/user'))).toBe(
24
- '/user',
25
- )
26
- })
@@ -1,15 +0,0 @@
1
- /**
2
- * Returns a relative URL if the given request URL is relative to the current origin.
3
- * Otherwise returns an absolute URL.
4
- */
5
- export function getPublicUrlFromRequest(request: Request): string {
6
- if (typeof location === 'undefined') {
7
- return request.url
8
- }
9
-
10
- const url = new URL(request.url)
11
-
12
- return url.origin === location.origin
13
- ? url.pathname
14
- : url.origin + url.pathname
15
- }