msw 2.2.5 → 2.2.7
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.
- package/lib/browser/index.d.mts +3 -3
- package/lib/browser/index.js +3 -2
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +3 -2
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/bypass.js +6 -1
- package/lib/core/bypass.js.map +1 -1
- package/lib/core/bypass.mjs +6 -1
- package/lib/core/bypass.mjs.map +1 -1
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/iife/index.js +9 -3
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +3 -10
- package/lib/native/index.d.mts +3 -3
- package/lib/node/index.d.mts +3 -3
- package/package.json +1 -1
- package/src/browser/setupWorker/start/createRequestListener.ts +1 -1
- package/src/browser/setupWorker/start/createResponseListener.ts +2 -0
- package/src/browser/setupWorker/start/utils/createMessageChannel.ts +1 -1
- package/src/core/bypass.test.ts +22 -0
- package/src/core/bypass.ts +9 -1
- package/src/core/utils/handleRequest.ts +1 -1
- package/src/mockServiceWorker.js +1 -8
package/lib/mockServiceWorker.js
CHANGED
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
* - Please do NOT serve this file on production.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
const PACKAGE_VERSION = '2.2.
|
|
12
|
-
const INTEGRITY_CHECKSUM = '
|
|
11
|
+
const PACKAGE_VERSION = '2.2.7'
|
|
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 '
|
|
240
|
+
case 'PASSTHROUGH': {
|
|
248
241
|
return passthrough()
|
|
249
242
|
}
|
|
250
243
|
}
|
package/lib/native/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { RequestHandler, RequestHandlerDefaultInfo } from
|
|
1
|
+
import { RequestHandler, RequestHandlerDefaultInfo } from "../core/handlers/RequestHandler";
|
|
2
2
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
3
|
-
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from
|
|
4
|
-
import { SetupApi } from
|
|
3
|
+
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
|
|
4
|
+
import { SetupApi } from "../core/SetupApi";
|
|
5
5
|
import { PartialDeep } from 'type-fest';
|
|
6
6
|
|
|
7
7
|
interface SetupServerCommon {
|
package/lib/node/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { PartialDeep } from 'type-fest';
|
|
2
|
-
import { RequestHandler, RequestHandlerDefaultInfo } from
|
|
3
|
-
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from
|
|
2
|
+
import { RequestHandler, RequestHandlerDefaultInfo } from "../core/handlers/RequestHandler";
|
|
3
|
+
import { SharedOptions, LifeCycleEventEmitter, LifeCycleEventsMap } from "../core/sharedOptions";
|
|
4
4
|
import { BatchInterceptor, Interceptor, HttpRequestEventMap } from '@mswjs/interceptors';
|
|
5
|
-
import { SetupApi } from
|
|
5
|
+
import { SetupApi } from "../core/SetupApi";
|
|
6
6
|
|
|
7
7
|
interface SetupServerCommon {
|
|
8
8
|
/**
|
package/package.json
CHANGED
|
@@ -48,7 +48,7 @@ export const createRequestListener = (
|
|
|
48
48
|
context.emitter,
|
|
49
49
|
{
|
|
50
50
|
onPassthroughResponse() {
|
|
51
|
-
messageChannel.postMessage('
|
|
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
|
package/src/core/bypass.test.ts
CHANGED
|
@@ -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
|
+
})
|
package/src/core/bypass.ts
CHANGED
|
@@ -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
|
-
|
|
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.
|
|
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)
|
package/src/mockServiceWorker.js
CHANGED
|
@@ -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 '
|
|
240
|
+
case 'PASSTHROUGH': {
|
|
248
241
|
return passthrough()
|
|
249
242
|
}
|
|
250
243
|
}
|