msw 2.3.0-ws.rc-5 → 2.3.0
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/cli/init.js +0 -0
- package/config/scripts/postinstall.js +0 -0
- package/lib/browser/index.d.mts +6 -7
- package/lib/browser/index.d.ts +6 -7
- package/lib/browser/index.js +156 -81
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/index.mjs +156 -81
- package/lib/browser/index.mjs.map +1 -1
- package/lib/core/{GraphQLHandler-DGyzNtRQ.d.ts → GraphQLHandler-COiPfZ8k.d.ts} +1 -1
- package/lib/core/{GraphQLHandler-CzcXr7At.d.mts → GraphQLHandler-Dq_WRbKe.d.mts} +1 -1
- package/lib/core/{HttpResponse-BOzDwzB6.d.ts → HttpResponse-B07UKAkU.d.ts} +2 -2
- package/lib/core/{HttpResponse-CSyJ1cbO.d.mts → HttpResponse-C7niBMwb.d.mts} +2 -2
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/SetupApi.d.mts +12 -15
- package/lib/core/SetupApi.d.ts +12 -15
- package/lib/core/SetupApi.js +1 -3
- package/lib/core/SetupApi.js.map +1 -1
- package/lib/core/SetupApi.mjs +1 -3
- package/lib/core/SetupApi.mjs.map +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +2 -2
- package/lib/core/graphql.d.ts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.mts +2 -2
- package/lib/core/handlers/GraphQLHandler.d.ts +2 -2
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +2 -5
- package/lib/core/index.d.ts +2 -5
- package/lib/core/index.js +1 -5
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +1 -7
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/executeHandlers.js +0 -4
- package/lib/core/utils/executeHandlers.js.map +1 -1
- package/lib/core/utils/executeHandlers.mjs +0 -6
- package/lib/core/utils/executeHandlers.mjs.map +1 -1
- package/lib/core/utils/handleRequest.d.mts +2 -2
- package/lib/core/utils/handleRequest.d.ts +2 -2
- package/lib/core/utils/handleRequest.js.map +1 -1
- package/lib/core/utils/handleRequest.mjs.map +1 -1
- package/lib/core/utils/internal/Disposable.d.mts +2 -2
- package/lib/core/utils/internal/Disposable.d.ts +2 -2
- package/lib/core/utils/internal/Disposable.js +5 -2
- package/lib/core/utils/internal/Disposable.js.map +1 -1
- package/lib/core/utils/internal/Disposable.mjs +5 -2
- package/lib/core/utils/internal/Disposable.mjs.map +1 -1
- package/lib/core/utils/internal/devUtils.d.mts +10 -1
- package/lib/core/utils/internal/devUtils.d.ts +10 -1
- package/lib/core/utils/internal/devUtils.js +7 -0
- package/lib/core/utils/internal/devUtils.js.map +1 -1
- package/lib/core/utils/internal/devUtils.mjs +7 -0
- package/lib/core/utils/internal/devUtils.mjs.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +2 -2
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +2 -2
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/logging/getTimestamp.d.mts +1 -4
- package/lib/core/utils/logging/getTimestamp.d.ts +1 -4
- package/lib/core/utils/logging/getTimestamp.js +2 -6
- package/lib/core/utils/logging/getTimestamp.js.map +1 -1
- package/lib/core/utils/logging/getTimestamp.mjs +2 -6
- package/lib/core/utils/logging/getTimestamp.mjs.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.d.mts +1 -2
- package/lib/core/utils/matching/matchRequestUrl.d.ts +1 -2
- package/lib/core/utils/matching/matchRequestUrl.js +0 -4
- package/lib/core/utils/matching/matchRequestUrl.js.map +1 -1
- package/lib/core/utils/matching/matchRequestUrl.mjs +0 -4
- package/lib/core/utils/matching/matchRequestUrl.mjs.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.js +3 -3
- package/lib/core/utils/request/onUnhandledRequest.js.map +1 -1
- package/lib/core/utils/request/onUnhandledRequest.mjs +4 -4
- package/lib/core/utils/request/onUnhandledRequest.mjs.map +1 -1
- package/lib/iife/index.js +177 -1063
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/lib/native/index.d.mts +5 -6
- package/lib/native/index.d.ts +5 -6
- package/lib/native/index.js +5 -13
- package/lib/native/index.js.map +1 -1
- package/lib/native/index.mjs +6 -14
- package/lib/native/index.mjs.map +1 -1
- package/lib/node/index.d.mts +7 -8
- package/lib/node/index.d.ts +7 -8
- package/lib/node/index.js +5 -13
- package/lib/node/index.js.map +1 -1
- package/lib/node/index.mjs +6 -14
- package/lib/node/index.mjs.map +1 -1
- package/package.json +36 -29
- package/src/browser/setupWorker/glossary.ts +10 -10
- package/src/browser/setupWorker/setupWorker.ts +3 -34
- package/src/core/SetupApi.ts +20 -28
- package/src/core/index.ts +1 -8
- package/src/core/utils/executeHandlers.ts +2 -6
- package/src/core/utils/handleRequest.ts +2 -1
- package/src/core/utils/internal/Disposable.ts +6 -3
- package/src/core/utils/internal/devUtils.test.ts +21 -0
- package/src/core/utils/internal/devUtils.ts +13 -0
- package/src/core/utils/logging/getTimestamp.test.ts +6 -20
- package/src/core/utils/logging/getTimestamp.ts +6 -11
- package/src/core/utils/matching/matchRequestUrl.test.ts +0 -44
- package/src/core/utils/matching/matchRequestUrl.ts +0 -4
- package/src/core/utils/request/onUnhandledRequest.test.ts +30 -4
- package/src/core/utils/request/onUnhandledRequest.ts +4 -4
- package/src/node/SetupServerApi.ts +7 -8
- package/src/node/SetupServerCommonApi.ts +8 -15
- package/src/node/glossary.ts +7 -5
- package/src/node/setupServer.ts +1 -2
- package/lib/core/handlers/WebSocketHandler.d.mts +0 -32
- package/lib/core/handlers/WebSocketHandler.d.ts +0 -32
- package/lib/core/handlers/WebSocketHandler.js +0 -62
- package/lib/core/handlers/WebSocketHandler.js.map +0 -1
- package/lib/core/handlers/WebSocketHandler.mjs +0 -44
- package/lib/core/handlers/WebSocketHandler.mjs.map +0 -1
- package/lib/core/ws/WebSocketClientManager.d.mts +0 -64
- package/lib/core/ws/WebSocketClientManager.d.ts +0 -64
- package/lib/core/ws/WebSocketClientManager.js +0 -123
- package/lib/core/ws/WebSocketClientManager.js.map +0 -1
- package/lib/core/ws/WebSocketClientManager.mjs +0 -103
- package/lib/core/ws/WebSocketClientManager.mjs.map +0 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +0 -16
- package/lib/core/ws/handleWebSocketEvent.d.ts +0 -16
- package/lib/core/ws/handleWebSocketEvent.js +0 -56
- package/lib/core/ws/handleWebSocketEvent.js.map +0 -1
- package/lib/core/ws/handleWebSocketEvent.mjs +0 -36
- package/lib/core/ws/handleWebSocketEvent.mjs.map +0 -1
- package/lib/core/ws/utils/attachWebSocketLogger.d.mts +0 -34
- package/lib/core/ws/utils/attachWebSocketLogger.d.ts +0 -34
- package/lib/core/ws/utils/attachWebSocketLogger.js +0 -217
- package/lib/core/ws/utils/attachWebSocketLogger.js.map +0 -1
- package/lib/core/ws/utils/attachWebSocketLogger.mjs +0 -197
- package/lib/core/ws/utils/attachWebSocketLogger.mjs.map +0 -1
- package/lib/core/ws/utils/getMessageLength.d.mts +0 -11
- package/lib/core/ws/utils/getMessageLength.d.ts +0 -11
- package/lib/core/ws/utils/getMessageLength.js +0 -33
- package/lib/core/ws/utils/getMessageLength.js.map +0 -1
- package/lib/core/ws/utils/getMessageLength.mjs +0 -13
- package/lib/core/ws/utils/getMessageLength.mjs.map +0 -1
- package/lib/core/ws/utils/getPublicData.d.mts +0 -5
- package/lib/core/ws/utils/getPublicData.d.ts +0 -5
- package/lib/core/ws/utils/getPublicData.js +0 -36
- package/lib/core/ws/utils/getPublicData.js.map +0 -1
- package/lib/core/ws/utils/getPublicData.mjs +0 -16
- package/lib/core/ws/utils/getPublicData.mjs.map +0 -1
- package/lib/core/ws/utils/truncateMessage.d.mts +0 -3
- package/lib/core/ws/utils/truncateMessage.d.ts +0 -3
- package/lib/core/ws/utils/truncateMessage.js +0 -31
- package/lib/core/ws/utils/truncateMessage.js.map +0 -1
- package/lib/core/ws/utils/truncateMessage.mjs +0 -11
- package/lib/core/ws/utils/truncateMessage.mjs.map +0 -1
- package/lib/core/ws/webSocketInterceptor.d.mts +0 -5
- package/lib/core/ws/webSocketInterceptor.d.ts +0 -5
- package/lib/core/ws/webSocketInterceptor.js +0 -26
- package/lib/core/ws/webSocketInterceptor.js.map +0 -1
- package/lib/core/ws/webSocketInterceptor.mjs +0 -6
- package/lib/core/ws/webSocketInterceptor.mjs.map +0 -1
- package/lib/core/ws.d.mts +0 -49
- package/lib/core/ws.d.ts +0 -49
- package/lib/core/ws.js +0 -63
- package/lib/core/ws.js.map +0 -1
- package/lib/core/ws.mjs +0 -46
- package/lib/core/ws.mjs.map +0 -1
- package/src/core/handlers/WebSocketHandler.ts +0 -71
- package/src/core/ws/WebSocketClientManager.test.ts +0 -159
- package/src/core/ws/WebSocketClientManager.ts +0 -170
- package/src/core/ws/handleWebSocketEvent.ts +0 -56
- package/src/core/ws/utils/attachWebSocketLogger.ts +0 -269
- package/src/core/ws/utils/getMessageLength.test.ts +0 -16
- package/src/core/ws/utils/getMessageLength.ts +0 -19
- package/src/core/ws/utils/getPublicData.test.ts +0 -38
- package/src/core/ws/utils/getPublicData.ts +0 -17
- package/src/core/ws/utils/truncateMessage.test.ts +0 -12
- package/src/core/ws/utils/truncateMessage.ts +0 -9
- package/src/core/ws/webSocketInterceptor.ts +0 -3
- package/src/core/ws.test.ts +0 -23
- package/src/core/ws.ts +0 -122
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { toPublicUrl } from './toPublicUrl'
|
|
2
|
-
import { devUtils } from '../internal/devUtils'
|
|
2
|
+
import { InternalError, devUtils } from '../internal/devUtils'
|
|
3
3
|
|
|
4
4
|
export interface UnhandledRequestPrint {
|
|
5
5
|
warning(): void
|
|
@@ -22,7 +22,7 @@ export async function onUnhandledRequest(
|
|
|
22
22
|
strategy: UnhandledRequestStrategy = 'warn',
|
|
23
23
|
): Promise<void> {
|
|
24
24
|
const url = new URL(request.url)
|
|
25
|
-
const publicUrl = toPublicUrl(url)
|
|
25
|
+
const publicUrl = toPublicUrl(url) + url.search
|
|
26
26
|
|
|
27
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`
|
|
28
28
|
|
|
@@ -33,7 +33,7 @@ export async function onUnhandledRequest(
|
|
|
33
33
|
devUtils.error('Error: %s', unhandledRequestMessage)
|
|
34
34
|
|
|
35
35
|
// Throw an exception to halt request processing and not perform the original request.
|
|
36
|
-
throw new
|
|
36
|
+
throw new InternalError(
|
|
37
37
|
devUtils.formatMessage(
|
|
38
38
|
'Cannot bypass a request when using the "error" strategy for the "onUnhandledRequest" option.',
|
|
39
39
|
),
|
|
@@ -49,7 +49,7 @@ export async function onUnhandledRequest(
|
|
|
49
49
|
break
|
|
50
50
|
|
|
51
51
|
default:
|
|
52
|
-
throw new
|
|
52
|
+
throw new InternalError(
|
|
53
53
|
devUtils.formatMessage(
|
|
54
54
|
'Failed to react to an unhandled request: unknown strategy "%s". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.',
|
|
55
55
|
strategy,
|
|
@@ -4,15 +4,14 @@ import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/XMLHttpRequest'
|
|
|
4
4
|
import { FetchInterceptor } from '@mswjs/interceptors/fetch'
|
|
5
5
|
import { HandlersController } from '~/core/SetupApi'
|
|
6
6
|
import type { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
7
|
-
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
8
7
|
import type { SetupServer } from './glossary'
|
|
9
8
|
import { SetupServerCommonApi } from './SetupServerCommonApi'
|
|
10
9
|
|
|
11
10
|
const store = new AsyncLocalStorage<RequestHandlersContext>()
|
|
12
11
|
|
|
13
12
|
type RequestHandlersContext = {
|
|
14
|
-
initialHandlers: Array<RequestHandler
|
|
15
|
-
handlers: Array<RequestHandler
|
|
13
|
+
initialHandlers: Array<RequestHandler>
|
|
14
|
+
handlers: Array<RequestHandler>
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
/**
|
|
@@ -23,7 +22,7 @@ type RequestHandlersContext = {
|
|
|
23
22
|
class AsyncHandlersController implements HandlersController {
|
|
24
23
|
private rootContext: RequestHandlersContext
|
|
25
24
|
|
|
26
|
-
constructor(initialHandlers: Array<RequestHandler
|
|
25
|
+
constructor(initialHandlers: Array<RequestHandler>) {
|
|
27
26
|
this.rootContext = { initialHandlers, handlers: [] }
|
|
28
27
|
}
|
|
29
28
|
|
|
@@ -31,18 +30,18 @@ class AsyncHandlersController implements HandlersController {
|
|
|
31
30
|
return store.getStore() || this.rootContext
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
public prepend(runtimeHandlers: Array<RequestHandler
|
|
33
|
+
public prepend(runtimeHandlers: Array<RequestHandler>) {
|
|
35
34
|
this.context.handlers.unshift(...runtimeHandlers)
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
public reset(nextHandlers: Array<RequestHandler
|
|
37
|
+
public reset(nextHandlers: Array<RequestHandler>) {
|
|
39
38
|
const context = this.context
|
|
40
39
|
context.handlers = []
|
|
41
40
|
context.initialHandlers =
|
|
42
41
|
nextHandlers.length > 0 ? nextHandlers : context.initialHandlers
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
public currentHandlers(): Array<RequestHandler
|
|
44
|
+
public currentHandlers(): Array<RequestHandler> {
|
|
46
45
|
const { initialHandlers, handlers } = this.context
|
|
47
46
|
return handlers.concat(initialHandlers)
|
|
48
47
|
}
|
|
@@ -52,7 +51,7 @@ export class SetupServerApi
|
|
|
52
51
|
extends SetupServerCommonApi
|
|
53
52
|
implements SetupServer
|
|
54
53
|
{
|
|
55
|
-
constructor(handlers: Array<RequestHandler
|
|
54
|
+
constructor(handlers: Array<RequestHandler>) {
|
|
56
55
|
super(
|
|
57
56
|
[ClientRequestInterceptor, XMLHttpRequestInterceptor, FetchInterceptor],
|
|
58
57
|
handlers,
|
|
@@ -14,12 +14,9 @@ import type { LifeCycleEventsMap, SharedOptions } from '~/core/sharedOptions'
|
|
|
14
14
|
import { SetupApi } from '~/core/SetupApi'
|
|
15
15
|
import { handleRequest } from '~/core/utils/handleRequest'
|
|
16
16
|
import type { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
17
|
-
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
18
17
|
import { mergeRight } from '~/core/utils/internal/mergeRight'
|
|
19
|
-
import { devUtils } from '~/core/utils/internal/devUtils'
|
|
18
|
+
import { InternalError, devUtils } from '~/core/utils/internal/devUtils'
|
|
20
19
|
import type { SetupServerCommon } from './glossary'
|
|
21
|
-
import { handleWebSocketEvent } from '~/core/ws/handleWebSocketEvent'
|
|
22
|
-
import { webSocketInterceptor } from '~/core/ws/webSocketInterceptor'
|
|
23
20
|
|
|
24
21
|
export const DEFAULT_LISTEN_OPTIONS: RequiredDeep<SharedOptions> = {
|
|
25
22
|
onUnhandledRequest: 'warn',
|
|
@@ -37,7 +34,7 @@ export class SetupServerCommonApi
|
|
|
37
34
|
|
|
38
35
|
constructor(
|
|
39
36
|
interceptors: Array<{ new (): Interceptor<HttpRequestEventMap> }>,
|
|
40
|
-
handlers: Array<RequestHandler
|
|
37
|
+
handlers: Array<RequestHandler>,
|
|
41
38
|
) {
|
|
42
39
|
super(...handlers)
|
|
43
40
|
|
|
@@ -71,6 +68,12 @@ export class SetupServerCommonApi
|
|
|
71
68
|
return
|
|
72
69
|
})
|
|
73
70
|
|
|
71
|
+
this.interceptor.on('unhandledException', ({ error }) => {
|
|
72
|
+
if (error instanceof InternalError) {
|
|
73
|
+
throw error
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
|
|
74
77
|
this.interceptor.on(
|
|
75
78
|
'response',
|
|
76
79
|
({ response, isMockedResponse, request, requestId }) => {
|
|
@@ -84,14 +87,6 @@ export class SetupServerCommonApi
|
|
|
84
87
|
)
|
|
85
88
|
},
|
|
86
89
|
)
|
|
87
|
-
|
|
88
|
-
handleWebSocketEvent({
|
|
89
|
-
getHandlers: () => {
|
|
90
|
-
return this.handlersController.currentHandlers()
|
|
91
|
-
},
|
|
92
|
-
onMockedConnection: () => {},
|
|
93
|
-
onPassthroughConnection: () => {},
|
|
94
|
-
})
|
|
95
90
|
}
|
|
96
91
|
|
|
97
92
|
public listen(options: Partial<SharedOptions> = {}): void {
|
|
@@ -102,11 +97,9 @@ export class SetupServerCommonApi
|
|
|
102
97
|
|
|
103
98
|
// Apply the interceptor when starting the server.
|
|
104
99
|
this.interceptor.apply()
|
|
105
|
-
webSocketInterceptor.apply()
|
|
106
100
|
|
|
107
101
|
this.subscriptions.push(() => {
|
|
108
102
|
this.interceptor.dispose()
|
|
109
|
-
webSocketInterceptor.dispose()
|
|
110
103
|
})
|
|
111
104
|
|
|
112
105
|
// Assert that the interceptor has been applied successfully.
|
package/src/node/glossary.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { PartialDeep } from 'type-fest'
|
|
2
|
-
import type {
|
|
3
|
-
|
|
2
|
+
import type {
|
|
3
|
+
RequestHandler,
|
|
4
|
+
RequestHandlerDefaultInfo,
|
|
5
|
+
} from '~/core/handlers/RequestHandler'
|
|
4
6
|
import type {
|
|
5
7
|
LifeCycleEventEmitter,
|
|
6
8
|
LifeCycleEventsMap,
|
|
@@ -27,7 +29,7 @@ export interface SetupServerCommon {
|
|
|
27
29
|
*
|
|
28
30
|
* @see {@link https://mswjs.io/docs/api/setup-server/use `server.use()` API reference}
|
|
29
31
|
*/
|
|
30
|
-
use(...handlers: Array<RequestHandler
|
|
32
|
+
use(...handlers: Array<RequestHandler>): void
|
|
31
33
|
|
|
32
34
|
/**
|
|
33
35
|
* Marks all request handlers that respond using `res.once()` as unused.
|
|
@@ -41,14 +43,14 @@ export interface SetupServerCommon {
|
|
|
41
43
|
*
|
|
42
44
|
* @see {@link https://mswjs.io/docs/api/setup-server/reset-handlers `server.reset-handlers()` API reference}
|
|
43
45
|
*/
|
|
44
|
-
resetHandlers(...nextHandlers: Array<RequestHandler
|
|
46
|
+
resetHandlers(...nextHandlers: Array<RequestHandler>): void
|
|
45
47
|
|
|
46
48
|
/**
|
|
47
49
|
* Returns a readonly list of currently active request handlers.
|
|
48
50
|
*
|
|
49
51
|
* @see {@link https://mswjs.io/docs/api/setup-server/list-handlers `server.listHandlers()` API reference}
|
|
50
52
|
*/
|
|
51
|
-
listHandlers(): ReadonlyArray<RequestHandler
|
|
53
|
+
listHandlers(): ReadonlyArray<RequestHandler<RequestHandlerDefaultInfo, any>>
|
|
52
54
|
|
|
53
55
|
/**
|
|
54
56
|
* Life-cycle events.
|
package/src/node/setupServer.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { RequestHandler } from '~/core/handlers/RequestHandler'
|
|
2
|
-
import type { WebSocketHandler } from '~/core/handlers/WebSocketHandler'
|
|
3
2
|
import { SetupServerApi } from './SetupServerApi'
|
|
4
3
|
|
|
5
4
|
/**
|
|
@@ -9,7 +8,7 @@ import { SetupServerApi } from './SetupServerApi'
|
|
|
9
8
|
* @see {@link https://mswjs.io/docs/api/setup-server `setupServer()` API reference}
|
|
10
9
|
*/
|
|
11
10
|
export const setupServer = (
|
|
12
|
-
...handlers: Array<RequestHandler
|
|
11
|
+
...handlers: Array<RequestHandler>
|
|
13
12
|
): SetupServerApi => {
|
|
14
13
|
return new SetupServerApi(handlers)
|
|
15
14
|
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Emitter } from 'strict-event-emitter';
|
|
2
|
-
import { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket';
|
|
3
|
-
import { PathParams, Path, Match } from '../utils/matching/matchRequestUrl.mjs';
|
|
4
|
-
|
|
5
|
-
type WebSocketHandlerParsedResult = {
|
|
6
|
-
match: Match;
|
|
7
|
-
};
|
|
8
|
-
type WebSocketHandlerEventMap = {
|
|
9
|
-
connection: [args: WebSocketHandlerConnection];
|
|
10
|
-
};
|
|
11
|
-
interface WebSocketHandlerConnection extends WebSocketConnectionData {
|
|
12
|
-
params: PathParams;
|
|
13
|
-
}
|
|
14
|
-
declare const kEmitter: unique symbol;
|
|
15
|
-
declare const kDispatchEvent: unique symbol;
|
|
16
|
-
declare const kSender: unique symbol;
|
|
17
|
-
declare class WebSocketHandler {
|
|
18
|
-
private readonly url;
|
|
19
|
-
callFrame?: string;
|
|
20
|
-
protected [kEmitter]: Emitter<WebSocketHandlerEventMap>;
|
|
21
|
-
constructor(url: Path);
|
|
22
|
-
parse(args: {
|
|
23
|
-
event: MessageEvent<WebSocketConnectionData>;
|
|
24
|
-
}): WebSocketHandlerParsedResult;
|
|
25
|
-
predicate(args: {
|
|
26
|
-
event: MessageEvent<WebSocketConnectionData>;
|
|
27
|
-
parsedResult: WebSocketHandlerParsedResult;
|
|
28
|
-
}): boolean;
|
|
29
|
-
[kDispatchEvent](event: MessageEvent<WebSocketConnectionData>): Promise<void>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export { WebSocketHandler, type WebSocketHandlerEventMap, kDispatchEvent, kEmitter, kSender };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Emitter } from 'strict-event-emitter';
|
|
2
|
-
import { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket';
|
|
3
|
-
import { PathParams, Path, Match } from '../utils/matching/matchRequestUrl.js';
|
|
4
|
-
|
|
5
|
-
type WebSocketHandlerParsedResult = {
|
|
6
|
-
match: Match;
|
|
7
|
-
};
|
|
8
|
-
type WebSocketHandlerEventMap = {
|
|
9
|
-
connection: [args: WebSocketHandlerConnection];
|
|
10
|
-
};
|
|
11
|
-
interface WebSocketHandlerConnection extends WebSocketConnectionData {
|
|
12
|
-
params: PathParams;
|
|
13
|
-
}
|
|
14
|
-
declare const kEmitter: unique symbol;
|
|
15
|
-
declare const kDispatchEvent: unique symbol;
|
|
16
|
-
declare const kSender: unique symbol;
|
|
17
|
-
declare class WebSocketHandler {
|
|
18
|
-
private readonly url;
|
|
19
|
-
callFrame?: string;
|
|
20
|
-
protected [kEmitter]: Emitter<WebSocketHandlerEventMap>;
|
|
21
|
-
constructor(url: Path);
|
|
22
|
-
parse(args: {
|
|
23
|
-
event: MessageEvent<WebSocketConnectionData>;
|
|
24
|
-
}): WebSocketHandlerParsedResult;
|
|
25
|
-
predicate(args: {
|
|
26
|
-
event: MessageEvent<WebSocketConnectionData>;
|
|
27
|
-
parsedResult: WebSocketHandlerParsedResult;
|
|
28
|
-
}): boolean;
|
|
29
|
-
[kDispatchEvent](event: MessageEvent<WebSocketConnectionData>): Promise<void>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export { WebSocketHandler, type WebSocketHandlerEventMap, kDispatchEvent, kEmitter, kSender };
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var WebSocketHandler_exports = {};
|
|
20
|
-
__export(WebSocketHandler_exports, {
|
|
21
|
-
WebSocketHandler: () => WebSocketHandler,
|
|
22
|
-
kDispatchEvent: () => kDispatchEvent,
|
|
23
|
-
kEmitter: () => kEmitter,
|
|
24
|
-
kSender: () => kSender
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(WebSocketHandler_exports);
|
|
27
|
-
var import_strict_event_emitter = require("strict-event-emitter");
|
|
28
|
-
var import_matchRequestUrl = require("../utils/matching/matchRequestUrl.js");
|
|
29
|
-
var import_getCallFrame = require("../utils/internal/getCallFrame.js");
|
|
30
|
-
const kEmitter = Symbol("kEmitter");
|
|
31
|
-
const kDispatchEvent = Symbol("kDispatchEvent");
|
|
32
|
-
const kSender = Symbol("kSender");
|
|
33
|
-
class WebSocketHandler {
|
|
34
|
-
constructor(url) {
|
|
35
|
-
this.url = url;
|
|
36
|
-
this[kEmitter] = new import_strict_event_emitter.Emitter();
|
|
37
|
-
this.callFrame = (0, import_getCallFrame.getCallFrame)(new Error());
|
|
38
|
-
}
|
|
39
|
-
callFrame;
|
|
40
|
-
[kEmitter];
|
|
41
|
-
parse(args) {
|
|
42
|
-
const connection = args.event.data;
|
|
43
|
-
const match = (0, import_matchRequestUrl.matchRequestUrl)(connection.client.url, this.url);
|
|
44
|
-
return {
|
|
45
|
-
match
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
predicate(args) {
|
|
49
|
-
return args.parsedResult.match.matches;
|
|
50
|
-
}
|
|
51
|
-
async [kDispatchEvent](event) {
|
|
52
|
-
const parsedResult = this.parse({ event });
|
|
53
|
-
const connection = event.data;
|
|
54
|
-
const resolvedConnection = {
|
|
55
|
-
client: connection.client,
|
|
56
|
-
server: connection.server,
|
|
57
|
-
params: parsedResult.match.params || {}
|
|
58
|
-
};
|
|
59
|
-
this[kEmitter].emit("connection", resolvedConnection);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
//# sourceMappingURL=WebSocketHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/handlers/WebSocketHandler.ts"],"sourcesContent":["import { Emitter } from 'strict-event-emitter'\nimport type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'\nimport {\n type Match,\n type Path,\n type PathParams,\n matchRequestUrl,\n} from '../utils/matching/matchRequestUrl'\nimport { getCallFrame } from '../utils/internal/getCallFrame'\n\ntype WebSocketHandlerParsedResult = {\n match: Match\n}\n\nexport type WebSocketHandlerEventMap = {\n connection: [args: WebSocketHandlerConnection]\n}\n\ninterface WebSocketHandlerConnection extends WebSocketConnectionData {\n params: PathParams\n}\n\nexport const kEmitter = Symbol('kEmitter')\nexport const kDispatchEvent = Symbol('kDispatchEvent')\nexport const kSender = Symbol('kSender')\n\nexport class WebSocketHandler {\n public callFrame?: string\n\n protected [kEmitter]: Emitter<WebSocketHandlerEventMap>\n\n constructor(private readonly url: Path) {\n this[kEmitter] = new Emitter()\n this.callFrame = getCallFrame(new Error())\n }\n\n public parse(args: {\n event: MessageEvent<WebSocketConnectionData>\n }): WebSocketHandlerParsedResult {\n const connection = args.event.data\n const match = matchRequestUrl(connection.client.url, this.url)\n\n return {\n match,\n }\n }\n\n public predicate(args: {\n event: MessageEvent<WebSocketConnectionData>\n parsedResult: WebSocketHandlerParsedResult\n }): boolean {\n return args.parsedResult.match.matches\n }\n\n async [kDispatchEvent](\n event: MessageEvent<WebSocketConnectionData>,\n ): Promise<void> {\n const parsedResult = this.parse({ event })\n const connection = event.data\n\n const resolvedConnection: WebSocketHandlerConnection = {\n client: connection.client,\n server: connection.server,\n params: parsedResult.match.params || {},\n }\n\n // Emit the connection event on the handler.\n // This is what the developer adds listeners for.\n this[kEmitter].emit('connection', resolvedConnection)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kCAAwB;AAExB,6BAKO;AACP,0BAA6B;AActB,MAAM,WAAW,OAAO,UAAU;AAClC,MAAM,iBAAiB,OAAO,gBAAgB;AAC9C,MAAM,UAAU,OAAO,SAAS;AAEhC,MAAM,iBAAiB;AAAA,EAK5B,YAA6B,KAAW;AAAX;AAC3B,SAAK,QAAQ,IAAI,IAAI,oCAAQ;AAC7B,SAAK,gBAAY,kCAAa,IAAI,MAAM,CAAC;AAAA,EAC3C;AAAA,EAPO;AAAA,EAEP,CAAW,QAAQ;AAAA,EAOZ,MAAM,MAEoB;AAC/B,UAAM,aAAa,KAAK,MAAM;AAC9B,UAAM,YAAQ,wCAAgB,WAAW,OAAO,KAAK,KAAK,GAAG;AAE7D,WAAO;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,UAAU,MAGL;AACV,WAAO,KAAK,aAAa,MAAM;AAAA,EACjC;AAAA,EAEA,OAAO,cAAc,EACnB,OACe;AACf,UAAM,eAAe,KAAK,MAAM,EAAE,MAAM,CAAC;AACzC,UAAM,aAAa,MAAM;AAEzB,UAAM,qBAAiD;AAAA,MACrD,QAAQ,WAAW;AAAA,MACnB,QAAQ,WAAW;AAAA,MACnB,QAAQ,aAAa,MAAM,UAAU,CAAC;AAAA,IACxC;AAIA,SAAK,QAAQ,EAAE,KAAK,cAAc,kBAAkB;AAAA,EACtD;AACF;","names":[]}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { Emitter } from "strict-event-emitter";
|
|
2
|
-
import {
|
|
3
|
-
matchRequestUrl
|
|
4
|
-
} from '../utils/matching/matchRequestUrl.mjs';
|
|
5
|
-
import { getCallFrame } from '../utils/internal/getCallFrame.mjs';
|
|
6
|
-
const kEmitter = Symbol("kEmitter");
|
|
7
|
-
const kDispatchEvent = Symbol("kDispatchEvent");
|
|
8
|
-
const kSender = Symbol("kSender");
|
|
9
|
-
class WebSocketHandler {
|
|
10
|
-
constructor(url) {
|
|
11
|
-
this.url = url;
|
|
12
|
-
this[kEmitter] = new Emitter();
|
|
13
|
-
this.callFrame = getCallFrame(new Error());
|
|
14
|
-
}
|
|
15
|
-
callFrame;
|
|
16
|
-
[kEmitter];
|
|
17
|
-
parse(args) {
|
|
18
|
-
const connection = args.event.data;
|
|
19
|
-
const match = matchRequestUrl(connection.client.url, this.url);
|
|
20
|
-
return {
|
|
21
|
-
match
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
predicate(args) {
|
|
25
|
-
return args.parsedResult.match.matches;
|
|
26
|
-
}
|
|
27
|
-
async [kDispatchEvent](event) {
|
|
28
|
-
const parsedResult = this.parse({ event });
|
|
29
|
-
const connection = event.data;
|
|
30
|
-
const resolvedConnection = {
|
|
31
|
-
client: connection.client,
|
|
32
|
-
server: connection.server,
|
|
33
|
-
params: parsedResult.match.params || {}
|
|
34
|
-
};
|
|
35
|
-
this[kEmitter].emit("connection", resolvedConnection);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
export {
|
|
39
|
-
WebSocketHandler,
|
|
40
|
-
kDispatchEvent,
|
|
41
|
-
kEmitter,
|
|
42
|
-
kSender
|
|
43
|
-
};
|
|
44
|
-
//# sourceMappingURL=WebSocketHandler.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/handlers/WebSocketHandler.ts"],"sourcesContent":["import { Emitter } from 'strict-event-emitter'\nimport type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'\nimport {\n type Match,\n type Path,\n type PathParams,\n matchRequestUrl,\n} from '../utils/matching/matchRequestUrl'\nimport { getCallFrame } from '../utils/internal/getCallFrame'\n\ntype WebSocketHandlerParsedResult = {\n match: Match\n}\n\nexport type WebSocketHandlerEventMap = {\n connection: [args: WebSocketHandlerConnection]\n}\n\ninterface WebSocketHandlerConnection extends WebSocketConnectionData {\n params: PathParams\n}\n\nexport const kEmitter = Symbol('kEmitter')\nexport const kDispatchEvent = Symbol('kDispatchEvent')\nexport const kSender = Symbol('kSender')\n\nexport class WebSocketHandler {\n public callFrame?: string\n\n protected [kEmitter]: Emitter<WebSocketHandlerEventMap>\n\n constructor(private readonly url: Path) {\n this[kEmitter] = new Emitter()\n this.callFrame = getCallFrame(new Error())\n }\n\n public parse(args: {\n event: MessageEvent<WebSocketConnectionData>\n }): WebSocketHandlerParsedResult {\n const connection = args.event.data\n const match = matchRequestUrl(connection.client.url, this.url)\n\n return {\n match,\n }\n }\n\n public predicate(args: {\n event: MessageEvent<WebSocketConnectionData>\n parsedResult: WebSocketHandlerParsedResult\n }): boolean {\n return args.parsedResult.match.matches\n }\n\n async [kDispatchEvent](\n event: MessageEvent<WebSocketConnectionData>,\n ): Promise<void> {\n const parsedResult = this.parse({ event })\n const connection = event.data\n\n const resolvedConnection: WebSocketHandlerConnection = {\n client: connection.client,\n server: connection.server,\n params: parsedResult.match.params || {},\n }\n\n // Emit the connection event on the handler.\n // This is what the developer adds listeners for.\n this[kEmitter].emit('connection', resolvedConnection)\n }\n}\n"],"mappings":"AAAA,SAAS,eAAe;AAExB;AAAA,EAIE;AAAA,OACK;AACP,SAAS,oBAAoB;AActB,MAAM,WAAW,OAAO,UAAU;AAClC,MAAM,iBAAiB,OAAO,gBAAgB;AAC9C,MAAM,UAAU,OAAO,SAAS;AAEhC,MAAM,iBAAiB;AAAA,EAK5B,YAA6B,KAAW;AAAX;AAC3B,SAAK,QAAQ,IAAI,IAAI,QAAQ;AAC7B,SAAK,YAAY,aAAa,IAAI,MAAM,CAAC;AAAA,EAC3C;AAAA,EAPO;AAAA,EAEP,CAAW,QAAQ;AAAA,EAOZ,MAAM,MAEoB;AAC/B,UAAM,aAAa,KAAK,MAAM;AAC9B,UAAM,QAAQ,gBAAgB,WAAW,OAAO,KAAK,KAAK,GAAG;AAE7D,WAAO;AAAA,MACL;AAAA,IACF;AAAA,EACF;AAAA,EAEO,UAAU,MAGL;AACV,WAAO,KAAK,aAAa,MAAM;AAAA,EACjC;AAAA,EAEA,OAAO,cAAc,EACnB,OACe;AACf,UAAM,eAAe,KAAK,MAAM,EAAE,MAAM,CAAC;AACzC,UAAM,aAAa,MAAM;AAEzB,UAAM,qBAAiD;AAAA,MACrD,QAAQ,WAAW;AAAA,MACnB,QAAQ,WAAW;AAAA,MACnB,QAAQ,aAAa,MAAM,UAAU,CAAC;AAAA,IACxC;AAIA,SAAK,QAAQ,EAAE,KAAK,cAAc,kBAAkB;AAAA,EACtD;AACF;","names":[]}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { WebSocketData, WebSocketClientConnectionProtocol, WebSocketClientConnection } from '@mswjs/interceptors/WebSocket';
|
|
2
|
-
|
|
3
|
-
type WebSocketBroadcastChannelMessage = {
|
|
4
|
-
type: 'connection:open';
|
|
5
|
-
payload: {
|
|
6
|
-
clientId: string;
|
|
7
|
-
url: string;
|
|
8
|
-
};
|
|
9
|
-
} | {
|
|
10
|
-
type: 'extraneous:send';
|
|
11
|
-
payload: {
|
|
12
|
-
clientId: string;
|
|
13
|
-
data: WebSocketData;
|
|
14
|
-
};
|
|
15
|
-
} | {
|
|
16
|
-
type: 'extraneous:close';
|
|
17
|
-
payload: {
|
|
18
|
-
clientId: string;
|
|
19
|
-
code?: number;
|
|
20
|
-
reason?: string;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
declare const kAddByClientId: unique symbol;
|
|
24
|
-
/**
|
|
25
|
-
* A manager responsible for accumulating WebSocket client
|
|
26
|
-
* connections across different browser runtimes.
|
|
27
|
-
*/
|
|
28
|
-
declare class WebSocketClientManager {
|
|
29
|
-
private channel;
|
|
30
|
-
/**
|
|
31
|
-
* All active WebSocket client connections.
|
|
32
|
-
*/
|
|
33
|
-
clients: Set<WebSocketClientConnectionProtocol>;
|
|
34
|
-
constructor(channel: BroadcastChannel);
|
|
35
|
-
/**
|
|
36
|
-
* Adds the given `WebSocket` client connection to the set
|
|
37
|
-
* of all connections. The given connection is always the complete
|
|
38
|
-
* connection object because `addConnection()` is called only
|
|
39
|
-
* for the opened connections in the same runtime.
|
|
40
|
-
*/
|
|
41
|
-
addConnection(client: WebSocketClientConnection): void;
|
|
42
|
-
/**
|
|
43
|
-
* Adds a client connection wrapper to operate with
|
|
44
|
-
* WebSocket client connections in other runtimes.
|
|
45
|
-
*/
|
|
46
|
-
private onRemoteConnection;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* A wrapper class to operate with WebSocket client connections
|
|
50
|
-
* from other runtimes. This class maintains 1-1 public API
|
|
51
|
-
* compatibility to the `WebSocketClientConnection` but relies
|
|
52
|
-
* on the given `BroadcastChannel` to communicate instructions
|
|
53
|
-
* with the client connections from other runtimes.
|
|
54
|
-
*/
|
|
55
|
-
declare class WebSocketRemoteClientConnection implements WebSocketClientConnectionProtocol {
|
|
56
|
-
readonly id: string;
|
|
57
|
-
readonly url: URL;
|
|
58
|
-
private channel;
|
|
59
|
-
constructor(id: string, url: URL, channel: BroadcastChannel);
|
|
60
|
-
send(data: WebSocketData): void;
|
|
61
|
-
close(code?: number | undefined, reason?: string | undefined): void;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { type WebSocketBroadcastChannelMessage, WebSocketClientManager, WebSocketRemoteClientConnection, kAddByClientId };
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { WebSocketData, WebSocketClientConnectionProtocol, WebSocketClientConnection } from '@mswjs/interceptors/WebSocket';
|
|
2
|
-
|
|
3
|
-
type WebSocketBroadcastChannelMessage = {
|
|
4
|
-
type: 'connection:open';
|
|
5
|
-
payload: {
|
|
6
|
-
clientId: string;
|
|
7
|
-
url: string;
|
|
8
|
-
};
|
|
9
|
-
} | {
|
|
10
|
-
type: 'extraneous:send';
|
|
11
|
-
payload: {
|
|
12
|
-
clientId: string;
|
|
13
|
-
data: WebSocketData;
|
|
14
|
-
};
|
|
15
|
-
} | {
|
|
16
|
-
type: 'extraneous:close';
|
|
17
|
-
payload: {
|
|
18
|
-
clientId: string;
|
|
19
|
-
code?: number;
|
|
20
|
-
reason?: string;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
declare const kAddByClientId: unique symbol;
|
|
24
|
-
/**
|
|
25
|
-
* A manager responsible for accumulating WebSocket client
|
|
26
|
-
* connections across different browser runtimes.
|
|
27
|
-
*/
|
|
28
|
-
declare class WebSocketClientManager {
|
|
29
|
-
private channel;
|
|
30
|
-
/**
|
|
31
|
-
* All active WebSocket client connections.
|
|
32
|
-
*/
|
|
33
|
-
clients: Set<WebSocketClientConnectionProtocol>;
|
|
34
|
-
constructor(channel: BroadcastChannel);
|
|
35
|
-
/**
|
|
36
|
-
* Adds the given `WebSocket` client connection to the set
|
|
37
|
-
* of all connections. The given connection is always the complete
|
|
38
|
-
* connection object because `addConnection()` is called only
|
|
39
|
-
* for the opened connections in the same runtime.
|
|
40
|
-
*/
|
|
41
|
-
addConnection(client: WebSocketClientConnection): void;
|
|
42
|
-
/**
|
|
43
|
-
* Adds a client connection wrapper to operate with
|
|
44
|
-
* WebSocket client connections in other runtimes.
|
|
45
|
-
*/
|
|
46
|
-
private onRemoteConnection;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* A wrapper class to operate with WebSocket client connections
|
|
50
|
-
* from other runtimes. This class maintains 1-1 public API
|
|
51
|
-
* compatibility to the `WebSocketClientConnection` but relies
|
|
52
|
-
* on the given `BroadcastChannel` to communicate instructions
|
|
53
|
-
* with the client connections from other runtimes.
|
|
54
|
-
*/
|
|
55
|
-
declare class WebSocketRemoteClientConnection implements WebSocketClientConnectionProtocol {
|
|
56
|
-
readonly id: string;
|
|
57
|
-
readonly url: URL;
|
|
58
|
-
private channel;
|
|
59
|
-
constructor(id: string, url: URL, channel: BroadcastChannel);
|
|
60
|
-
send(data: WebSocketData): void;
|
|
61
|
-
close(code?: number | undefined, reason?: string | undefined): void;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { type WebSocketBroadcastChannelMessage, WebSocketClientManager, WebSocketRemoteClientConnection, kAddByClientId };
|
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var WebSocketClientManager_exports = {};
|
|
20
|
-
__export(WebSocketClientManager_exports, {
|
|
21
|
-
WebSocketClientManager: () => WebSocketClientManager,
|
|
22
|
-
WebSocketRemoteClientConnection: () => WebSocketRemoteClientConnection,
|
|
23
|
-
kAddByClientId: () => kAddByClientId
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(WebSocketClientManager_exports);
|
|
26
|
-
const kAddByClientId = Symbol("kAddByClientId");
|
|
27
|
-
class WebSocketClientManager {
|
|
28
|
-
constructor(channel) {
|
|
29
|
-
this.channel = channel;
|
|
30
|
-
this.clients = /* @__PURE__ */ new Set();
|
|
31
|
-
this.channel.addEventListener("message", (message) => {
|
|
32
|
-
const { type, payload } = message.data;
|
|
33
|
-
switch (type) {
|
|
34
|
-
case "connection:open": {
|
|
35
|
-
this.onRemoteConnection(payload.clientId, new URL(payload.url));
|
|
36
|
-
break;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* All active WebSocket client connections.
|
|
43
|
-
*/
|
|
44
|
-
clients;
|
|
45
|
-
/**
|
|
46
|
-
* Adds the given `WebSocket` client connection to the set
|
|
47
|
-
* of all connections. The given connection is always the complete
|
|
48
|
-
* connection object because `addConnection()` is called only
|
|
49
|
-
* for the opened connections in the same runtime.
|
|
50
|
-
*/
|
|
51
|
-
addConnection(client) {
|
|
52
|
-
this.clients.add(client);
|
|
53
|
-
this.channel.postMessage({
|
|
54
|
-
type: "connection:open",
|
|
55
|
-
payload: {
|
|
56
|
-
clientId: client.id,
|
|
57
|
-
url: client.url.toString()
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
const handleExtraneousMessage = (message) => {
|
|
61
|
-
const { type, payload } = message.data;
|
|
62
|
-
if (typeof payload === "object" && "clientId" in payload && payload.clientId !== client.id) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
switch (type) {
|
|
66
|
-
case "extraneous:send": {
|
|
67
|
-
client.send(payload.data);
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
case "extraneous:close": {
|
|
71
|
-
client.close(payload.code, payload.reason);
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
const abortController = new AbortController();
|
|
77
|
-
this.channel.addEventListener("message", handleExtraneousMessage, {
|
|
78
|
-
signal: abortController.signal
|
|
79
|
-
});
|
|
80
|
-
client.addEventListener("close", () => abortController.abort(), {
|
|
81
|
-
once: true
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Adds a client connection wrapper to operate with
|
|
86
|
-
* WebSocket client connections in other runtimes.
|
|
87
|
-
*/
|
|
88
|
-
onRemoteConnection(id, url) {
|
|
89
|
-
this.clients.add(
|
|
90
|
-
// Create a connection-compatible instance that can
|
|
91
|
-
// operate with this client from a different runtime
|
|
92
|
-
// using the BroadcastChannel messages.
|
|
93
|
-
new WebSocketRemoteClientConnection(id, url, this.channel)
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
class WebSocketRemoteClientConnection {
|
|
98
|
-
constructor(id, url, channel) {
|
|
99
|
-
this.id = id;
|
|
100
|
-
this.url = url;
|
|
101
|
-
this.channel = channel;
|
|
102
|
-
}
|
|
103
|
-
send(data) {
|
|
104
|
-
this.channel.postMessage({
|
|
105
|
-
type: "extraneous:send",
|
|
106
|
-
payload: {
|
|
107
|
-
clientId: this.id,
|
|
108
|
-
data
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
close(code, reason) {
|
|
113
|
-
this.channel.postMessage({
|
|
114
|
-
type: "extraneous:close",
|
|
115
|
-
payload: {
|
|
116
|
-
clientId: this.id,
|
|
117
|
-
code,
|
|
118
|
-
reason
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
//# sourceMappingURL=WebSocketClientManager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/ws/WebSocketClientManager.ts"],"sourcesContent":["import type {\n WebSocketData,\n WebSocketClientConnection,\n WebSocketClientConnectionProtocol,\n} from '@mswjs/interceptors/WebSocket'\n\nexport type WebSocketBroadcastChannelMessage =\n | {\n type: 'connection:open'\n payload: {\n clientId: string\n url: string\n }\n }\n | {\n type: 'extraneous:send'\n payload: {\n clientId: string\n data: WebSocketData\n }\n }\n | {\n type: 'extraneous:close'\n payload: {\n clientId: string\n code?: number\n reason?: string\n }\n }\n\nexport const kAddByClientId = Symbol('kAddByClientId')\n\n/**\n * A manager responsible for accumulating WebSocket client\n * connections across different browser runtimes.\n */\nexport class WebSocketClientManager {\n /**\n * All active WebSocket client connections.\n */\n public clients: Set<WebSocketClientConnectionProtocol>\n\n constructor(private channel: BroadcastChannel) {\n this.clients = new Set()\n\n this.channel.addEventListener('message', (message) => {\n const { type, payload } = message.data as WebSocketBroadcastChannelMessage\n\n switch (type) {\n case 'connection:open': {\n // When another runtime notifies about a new connection,\n // create a connection wrapper class and add it to the set.\n this.onRemoteConnection(payload.clientId, new URL(payload.url))\n break\n }\n }\n })\n }\n\n /**\n * Adds the given `WebSocket` client connection to the set\n * of all connections. The given connection is always the complete\n * connection object because `addConnection()` is called only\n * for the opened connections in the same runtime.\n */\n public addConnection(client: WebSocketClientConnection): void {\n this.clients.add(client)\n\n // Signal to other runtimes about this connection.\n this.channel.postMessage({\n type: 'connection:open',\n payload: {\n clientId: client.id,\n url: client.url.toString(),\n },\n } as WebSocketBroadcastChannelMessage)\n\n // Instruct the current client how to handle events\n // coming from other runtimes (e.g. when calling `.broadcast()`).\n const handleExtraneousMessage = (\n message: MessageEvent<WebSocketBroadcastChannelMessage>,\n ) => {\n const { type, payload } = message.data\n\n // Ignore broadcasted messages for other clients.\n if (\n typeof payload === 'object' &&\n 'clientId' in payload &&\n payload.clientId !== client.id\n ) {\n return\n }\n\n switch (type) {\n case 'extraneous:send': {\n client.send(payload.data)\n break\n }\n\n case 'extraneous:close': {\n client.close(payload.code, payload.reason)\n break\n }\n }\n }\n\n const abortController = new AbortController()\n\n this.channel.addEventListener('message', handleExtraneousMessage, {\n signal: abortController.signal,\n })\n\n // Once closed, this connection cannot be operated on.\n // This must include the extraneous runtimes as well.\n client.addEventListener('close', () => abortController.abort(), {\n once: true,\n })\n }\n\n /**\n * Adds a client connection wrapper to operate with\n * WebSocket client connections in other runtimes.\n */\n private onRemoteConnection(id: string, url: URL): void {\n this.clients.add(\n // Create a connection-compatible instance that can\n // operate with this client from a different runtime\n // using the BroadcastChannel messages.\n new WebSocketRemoteClientConnection(id, url, this.channel),\n )\n }\n}\n\n/**\n * A wrapper class to operate with WebSocket client connections\n * from other runtimes. This class maintains 1-1 public API\n * compatibility to the `WebSocketClientConnection` but relies\n * on the given `BroadcastChannel` to communicate instructions\n * with the client connections from other runtimes.\n */\nexport class WebSocketRemoteClientConnection\n implements WebSocketClientConnectionProtocol\n{\n constructor(\n public readonly id: string,\n public readonly url: URL,\n private channel: BroadcastChannel,\n ) {}\n\n send(data: WebSocketData): void {\n this.channel.postMessage({\n type: 'extraneous:send',\n payload: {\n clientId: this.id,\n data,\n },\n } as WebSocketBroadcastChannelMessage)\n }\n\n close(code?: number | undefined, reason?: string | undefined): void {\n this.channel.postMessage({\n type: 'extraneous:close',\n payload: {\n clientId: this.id,\n code,\n reason,\n },\n } as WebSocketBroadcastChannelMessage)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BO,MAAM,iBAAiB,OAAO,gBAAgB;AAM9C,MAAM,uBAAuB;AAAA,EAMlC,YAAoB,SAA2B;AAA3B;AAClB,SAAK,UAAU,oBAAI,IAAI;AAEvB,SAAK,QAAQ,iBAAiB,WAAW,CAAC,YAAY;AACpD,YAAM,EAAE,MAAM,QAAQ,IAAI,QAAQ;AAElC,cAAQ,MAAM;AAAA,QACZ,KAAK,mBAAmB;AAGtB,eAAK,mBAAmB,QAAQ,UAAU,IAAI,IAAI,QAAQ,GAAG,CAAC;AAC9D;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA,EAjBO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAyBA,cAAc,QAAyC;AAC5D,SAAK,QAAQ,IAAI,MAAM;AAGvB,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,OAAO;AAAA,QACjB,KAAK,OAAO,IAAI,SAAS;AAAA,MAC3B;AAAA,IACF,CAAqC;AAIrC,UAAM,0BAA0B,CAC9B,YACG;AACH,YAAM,EAAE,MAAM,QAAQ,IAAI,QAAQ;AAGlC,UACE,OAAO,YAAY,YACnB,cAAc,WACd,QAAQ,aAAa,OAAO,IAC5B;AACA;AAAA,MACF;AAEA,cAAQ,MAAM;AAAA,QACZ,KAAK,mBAAmB;AACtB,iBAAO,KAAK,QAAQ,IAAI;AACxB;AAAA,QACF;AAAA,QAEA,KAAK,oBAAoB;AACvB,iBAAO,MAAM,QAAQ,MAAM,QAAQ,MAAM;AACzC;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,UAAM,kBAAkB,IAAI,gBAAgB;AAE5C,SAAK,QAAQ,iBAAiB,WAAW,yBAAyB;AAAA,MAChE,QAAQ,gBAAgB;AAAA,IAC1B,CAAC;AAID,WAAO,iBAAiB,SAAS,MAAM,gBAAgB,MAAM,GAAG;AAAA,MAC9D,MAAM;AAAA,IACR,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,mBAAmB,IAAY,KAAgB;AACrD,SAAK,QAAQ;AAAA;AAAA;AAAA;AAAA,MAIX,IAAI,gCAAgC,IAAI,KAAK,KAAK,OAAO;AAAA,IAC3D;AAAA,EACF;AACF;AASO,MAAM,gCAEb;AAAA,EACE,YACkB,IACA,KACR,SACR;AAHgB;AACA;AACR;AAAA,EACP;AAAA,EAEH,KAAK,MAA2B;AAC9B,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AAAA,EAEA,MAAM,MAA2B,QAAmC;AAClE,SAAK,QAAQ,YAAY;AAAA,MACvB,MAAM;AAAA,MACN,SAAS;AAAA,QACP,UAAU,KAAK;AAAA,QACf;AAAA,QACA;AAAA,MACF;AAAA,IACF,CAAqC;AAAA,EACvC;AACF;","names":[]}
|