msw 2.3.0-ws.rc-11 → 2.3.0-ws.rc-12

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,7 +8,7 @@
8
8
  * - Please do NOT serve this file on production.
9
9
  */
10
10
 
11
- const PACKAGE_VERSION = '2.3.0-ws.rc-11'
11
+ const PACKAGE_VERSION = '2.3.0-ws.rc-12'
12
12
  const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423'
13
13
  const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
14
14
  const activeClientIds = new Set()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "msw",
3
- "version": "2.3.0-ws.rc-11",
3
+ "version": "2.3.0-ws.rc-12",
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",
@@ -137,7 +137,7 @@
137
137
  "@bundled-es-modules/statuses": "^1.0.1",
138
138
  "@bundled-es-modules/tough-cookie": "^0.1.6",
139
139
  "@inquirer/confirm": "^3.0.0",
140
- "@mswjs/interceptors": "^0.36.1",
140
+ "@mswjs/interceptors": "^0.36.4",
141
141
  "@open-draft/deferred-promise": "^2.2.0",
142
142
  "@open-draft/until": "^2.1.0",
143
143
  "@types/cookie": "^0.6.0",
@@ -1,4 +1,5 @@
1
1
  import { Emitter } from 'strict-event-emitter'
2
+ import { createRequestId } from '@mswjs/interceptors'
2
3
  import type { WebSocketConnectionData } from '@mswjs/interceptors/WebSocket'
3
4
  import {
4
5
  type Match,
@@ -23,13 +24,18 @@ interface WebSocketHandlerConnection extends WebSocketConnectionData {
23
24
  export const kEmitter = Symbol('kEmitter')
24
25
  export const kDispatchEvent = Symbol('kDispatchEvent')
25
26
  export const kSender = Symbol('kSender')
27
+ const kStopPropagationPatched = Symbol('kStopPropagationPatched')
28
+ const KOnStopPropagation = Symbol('KOnStopPropagation')
26
29
 
27
30
  export class WebSocketHandler {
31
+ public id: string
28
32
  public callFrame?: string
29
33
 
30
34
  protected [kEmitter]: Emitter<WebSocketHandlerEventMap>
31
35
 
32
36
  constructor(private readonly url: Path) {
37
+ this.id = createRequestId()
38
+
33
39
  this[kEmitter] = new Emitter()
34
40
  this.callFrame = getCallFrame(new Error())
35
41
  }
@@ -63,8 +69,74 @@ export class WebSocketHandler {
63
69
  params: parsedResult.match.params || {},
64
70
  }
65
71
 
72
+ // Support `event.stopPropagation()` for various client/server events.
73
+ connection.client.addEventListener(
74
+ 'message',
75
+ createStopPropagationListener(this),
76
+ )
77
+ connection.client.addEventListener(
78
+ 'close',
79
+ createStopPropagationListener(this),
80
+ )
81
+
82
+ connection.server.addEventListener(
83
+ 'open',
84
+ createStopPropagationListener(this),
85
+ )
86
+ connection.server.addEventListener(
87
+ 'message',
88
+ createStopPropagationListener(this),
89
+ )
90
+ connection.server.addEventListener(
91
+ 'error',
92
+ createStopPropagationListener(this),
93
+ )
94
+ connection.server.addEventListener(
95
+ 'close',
96
+ createStopPropagationListener(this),
97
+ )
98
+
66
99
  // Emit the connection event on the handler.
67
100
  // This is what the developer adds listeners for.
68
101
  this[kEmitter].emit('connection', resolvedConnection)
69
102
  }
70
103
  }
104
+
105
+ function createStopPropagationListener(handler: WebSocketHandler) {
106
+ return function stopPropagationListener(event: Event) {
107
+ const propagationStoppedAt = Reflect.get(event, 'kPropagationStoppedAt') as
108
+ | string
109
+ | undefined
110
+
111
+ if (propagationStoppedAt && handler.id !== propagationStoppedAt) {
112
+ event.stopImmediatePropagation()
113
+ return
114
+ }
115
+
116
+ Object.defineProperty(event, KOnStopPropagation, {
117
+ value(this: WebSocketHandler) {
118
+ Object.defineProperty(event, 'kPropagationStoppedAt', {
119
+ value: handler.id,
120
+ })
121
+ },
122
+ configurable: true,
123
+ })
124
+
125
+ // Since the same event instance is shared between all client/server objects,
126
+ // make sure to patch its `stopPropagation` method only once.
127
+ if (!Reflect.get(event, kStopPropagationPatched)) {
128
+ event.stopPropagation = new Proxy(event.stopPropagation, {
129
+ apply: (target, thisArg, args) => {
130
+ Reflect.get(event, KOnStopPropagation)?.call(handler)
131
+ return Reflect.apply(target, thisArg, args)
132
+ },
133
+ })
134
+
135
+ Object.defineProperty(event, kStopPropagationPatched, {
136
+ value: true,
137
+ // If something else attempts to redefine this, throw.
138
+ configurable: false,
139
+ })
140
+ }
141
+ }
142
+ }