msw 2.2.5 → 2.2.6

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.
@@ -8,8 +8,8 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.2.5'
12
- const INTEGRITY_CHECKSUM = '5db7e6e8385dc04e017ac4823e0e9b29'
11
+ const PACKAGE_VERSION = '2.2.6'
12
+ const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423'
13
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
14
14
  const activeClientIds = new Set()
15
15
 
@@ -206,13 +206,6 @@ async function getResponse(event, client, requestId) {
206
206
  return passthrough()
207
207
  }
208
208
 
209
- // Bypass requests with the explicit bypass header.
210
- // Such requests can be issued by "ctx.fetch()".
211
- const mswIntention = request.headers.get('x-msw-intention')
212
- if (['bypass', 'passthrough'].includes(mswIntention)) {
213
- return passthrough()
214
- }
215
-
216
209
  // Notify the client that a request has been intercepted.
217
210
  const requestBuffer = await request.arrayBuffer()
218
211
  const clientMessage = await sendToClient(
@@ -244,7 +237,7 @@ async function getResponse(event, client, requestId) {
244
237
  return respondWithMock(clientMessage.data)
245
238
  }
246
239
 
247
- case 'MOCK_NOT_FOUND': {
240
+ case 'PASSTHROUGH': {
248
241
  return passthrough()
249
242
  }
250
243
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.2.5",
3
+ "version": "2.2.6",
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",
@@ -48,7 +48,7 @@ export const createRequestListener = (
48
48
  context.emitter,
49
49
  {
50
50
  onPassthroughResponse() {
51
- messageChannel.postMessage('NOT_FOUND')
51
+ messageChannel.postMessage('PASSTHROUGH')
52
52
  },
53
53
  async onMockedResponse(response, { handler, parsedResult }) {
54
54
  // Clone the mocked response so its body could be read
@@ -21,6 +21,8 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
21
21
  const request = context.requests.get(requestId)!
22
22
  context.requests.delete(requestId)
23
23
 
24
+ console.log('RESPONSE LISTENER', responseJson, context.requests)
25
+
24
26
  /**
25
27
  * CORS requests with `mode: "no-cors"` result in "opaque" responses.
26
28
  * That kind of responses cannot be manipulated in JavaScript due
@@ -16,7 +16,7 @@ interface WorkerChannelEventsMap {
16
16
  data: StringifiedResponse,
17
17
  transfer?: [ReadableStream<Uint8Array>],
18
18
  ]
19
- NOT_FOUND: []
19
+ PASSTHROUGH: []
20
20
  }
21
21
 
22
22
  export class WorkerChannel {
@@ -45,3 +45,25 @@ it('returns bypassed request given request instance', async () => {
45
45
  'x-msw-intention': 'bypass',
46
46
  })
47
47
  })
48
+
49
+ it('allows modifying the bypassed request instance', async () => {
50
+ const original = new Request('http://localhost/resource', {
51
+ method: 'POST',
52
+ body: 'hello world',
53
+ })
54
+ const request = bypass(original, {
55
+ method: 'PUT',
56
+ headers: { 'x-modified-header': 'yes' },
57
+ })
58
+
59
+ expect(request.method).toBe('PUT')
60
+ expect(Object.fromEntries(request.headers.entries())).toEqual({
61
+ 'x-msw-intention': 'bypass',
62
+ 'x-modified-header': 'yes',
63
+ })
64
+ expect(original.bodyUsed).toBe(false)
65
+ expect(request.bodyUsed).toBe(false)
66
+
67
+ expect(await request.text()).toBe('hello world')
68
+ expect(original.bodyUsed).toBe(false)
69
+ })
@@ -15,7 +15,15 @@ export type BypassRequestInput = string | URL | Request
15
15
  * @see {@link https://mswjs.io/docs/api/bypass `bypass()` API reference}
16
16
  */
17
17
  export function bypass(input: BypassRequestInput, init?: RequestInit): Request {
18
- const request = input instanceof Request ? input : new Request(input, init)
18
+ // Always create a new Request instance.
19
+ // This way, the "init" modifications will propagate
20
+ // to the bypass request instance automatically.
21
+ const request = new Request(
22
+ // If given a Request instance, clone it not to exhaust
23
+ // the original request's body.
24
+ input instanceof Request ? input.clone() : input,
25
+ init,
26
+ )
19
27
 
20
28
  invariant(
21
29
  !request.bodyUsed,
@@ -52,7 +52,7 @@ export async function handleRequest(
52
52
  ): Promise<Response | undefined> {
53
53
  emitter.emit('request:start', { request, requestId })
54
54
 
55
- // Perform bypassed requests (i.e. issued via "ctx.fetch") as-is.
55
+ // Perform bypassed requests (i.e. wrapped in "bypass()") as-is.
56
56
  if (request.headers.get('x-msw-intention') === 'bypass') {
57
57
  emitter.emit('request:end', { request, requestId })
58
58
  handleRequestOptions?.onPassthroughResponse?.(request)
@@ -206,13 +206,6 @@ async function getResponse(event, client, requestId) {
206
206
  return passthrough()
207
207
  }
208
208
 
209
- // Bypass requests with the explicit bypass header.
210
- // Such requests can be issued by "ctx.fetch()".
211
- const mswIntention = request.headers.get('x-msw-intention')
212
- if (['bypass', 'passthrough'].includes(mswIntention)) {
213
- return passthrough()
214
- }
215
-
216
209
  // Notify the client that a request has been intercepted.
217
210
  const requestBuffer = await request.arrayBuffer()
218
211
  const clientMessage = await sendToClient(
@@ -244,7 +237,7 @@ async function getResponse(event, client, requestId) {
244
237
  return respondWithMock(clientMessage.data)
245
238
  }
246
239
 
247
- case 'MOCK_NOT_FOUND': {
240
+ case 'PASSTHROUGH': {
248
241
  return passthrough()
249
242
  }
250
243
  }