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

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-10'
11
+ const PACKAGE_VERSION = '2.3.0-ws.rc-11'
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-10",
3
+ "version": "2.3.0-ws.rc-11",
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.0",
140
+ "@mswjs/interceptors": "^0.36.1",
141
141
  "@open-draft/deferred-promise": "^2.2.0",
142
142
  "@open-draft/until": "^2.1.0",
143
143
  "@types/cookie": "^0.6.0",
@@ -10,10 +10,10 @@ import { getMessageLength } from './getMessageLength'
10
10
  import { getPublicData } from './getPublicData'
11
11
 
12
12
  const colors = {
13
- blue: '#3b82f6',
14
- green: '#22c55e',
15
- red: '#ef4444',
16
- orange: '#ff6a33',
13
+ system: '#3b82f6',
14
+ outgoing: '#22c55e',
15
+ incoming: '#ef4444',
16
+ mocked: '#ff6a33',
17
17
  }
18
18
 
19
19
  export function attachWebSocketLogger(
@@ -38,13 +38,6 @@ export function attachWebSocketLogger(
38
38
  logConnectionClose(event)
39
39
  })
40
40
 
41
- // Log the events received by the WebSocket client.
42
- // "client.socket" references the actual WebSocket instance
43
- // so these message events are incoming messages.
44
- client.socket.addEventListener('message', (event) => {
45
- logIncomingClientMessage(event)
46
- })
47
-
48
41
  // Log client errors (connection closures due to errors).
49
42
  client.socket.addEventListener('error', (event) => {
50
43
  logClientError(event)
@@ -66,7 +59,10 @@ export function attachWebSocketLogger(
66
59
  value: client.socket,
67
60
  },
68
61
  })
69
- logIncomingMockedClientMessage(messageEvent)
62
+
63
+ queueMicrotask(() => {
64
+ logIncomingMockedClientMessage(messageEvent)
65
+ })
70
66
 
71
67
  return Reflect.apply(target, thisArg, args)
72
68
  },
@@ -120,7 +116,7 @@ export function logConnectionOpen(client: WebSocketClientConnection) {
120
116
  // eslint-disable-next-line no-console
121
117
  console.groupCollapsed(
122
118
  devUtils.formatMessage(`${getTimestamp()} %c▶%c ${publicUrl}`),
123
- `color:${colors.blue}`,
119
+ `color:${colors.system}`,
124
120
  'color:inherit',
125
121
  )
126
122
  // eslint-disable-next-line no-console
@@ -129,24 +125,17 @@ export function logConnectionOpen(client: WebSocketClientConnection) {
129
125
  console.groupEnd()
130
126
  }
131
127
 
132
- /**
133
- * Prints the outgoing client message.
134
- */
135
- export async function logOutgoingClientMessage(
136
- event: MessageEvent<WebSocketData>,
137
- ) {
138
- const byteLength = getMessageLength(event.data)
139
- const publicData = await getPublicData(event.data)
128
+ function logConnectionClose(event: CloseEvent) {
129
+ const target = event.target as WebSocket
130
+ const publicUrl = toPublicUrl(target.url)
140
131
 
141
132
  // eslint-disable-next-line no-console
142
133
  console.groupCollapsed(
143
134
  devUtils.formatMessage(
144
- `${getTimestamp({ milliseconds: true })} %c↑%c ${publicData} %c${byteLength}%c`,
135
+ `${getTimestamp({ milliseconds: true })} %c■%c ${publicUrl}`,
145
136
  ),
146
- `color:${colors.green}`,
137
+ `color:${colors.system}`,
147
138
  'color:inherit',
148
- 'color:gray;font-weight:normal',
149
- 'color:inherit;font-weight:inherit',
150
139
  )
151
140
  // eslint-disable-next-line no-console
152
141
  console.log(event)
@@ -154,25 +143,17 @@ export async function logOutgoingClientMessage(
154
143
  console.groupEnd()
155
144
  }
156
145
 
157
- /**
158
- * Prints the outgoing client message initiated
159
- * by `server.send()` in the event handler.
160
- */
161
- export async function logOutgoingMockedClientMessage(
162
- event: MessageEvent<WebSocketData>,
163
- ) {
164
- const byteLength = getMessageLength(event.data)
165
- const publicData = await getPublicData(event.data)
146
+ function logClientError(event: Event) {
147
+ const socket = event.target as WebSocket
148
+ const publicUrl = toPublicUrl(socket.url)
166
149
 
167
150
  // eslint-disable-next-line no-console
168
151
  console.groupCollapsed(
169
152
  devUtils.formatMessage(
170
- `${getTimestamp({ milliseconds: true })} %c⇡%c ${publicData} %c${byteLength}%c`,
153
+ `${getTimestamp({ milliseconds: true })} %c\u00D7%c ${publicUrl}`,
171
154
  ),
172
- `color:${colors.orange}`,
155
+ `color:${colors.system}`,
173
156
  'color:inherit',
174
- 'color:gray;font-weight:normal',
175
- 'color:inherit;font-weight:inherit',
176
157
  )
177
158
  // eslint-disable-next-line no-console
178
159
  console.log(event)
@@ -181,23 +162,19 @@ export async function logOutgoingMockedClientMessage(
181
162
  }
182
163
 
183
164
  /**
184
- * Prings the message received by the WebSocket client.
185
- * This is fired when the "message" event is dispatched
186
- * on the actual WebSocket client instance, and translates to
187
- * the client receiving a message from the server.
165
+ * Prints the outgoing client message.
188
166
  */
189
- export async function logIncomingClientMessage(
190
- event: MessageEvent<WebSocketData>,
191
- ) {
167
+ async function logOutgoingClientMessage(event: MessageEvent<WebSocketData>) {
192
168
  const byteLength = getMessageLength(event.data)
193
169
  const publicData = await getPublicData(event.data)
170
+ const arrow = event.defaultPrevented ? '⇡' : '⬆'
194
171
 
195
172
  // eslint-disable-next-line no-console
196
173
  console.groupCollapsed(
197
174
  devUtils.formatMessage(
198
- `${getTimestamp({ milliseconds: true })} %c↓%c ${publicData} %c${byteLength}%c`,
175
+ `${getTimestamp({ milliseconds: true })} %c${arrow}%c ${publicData} %c${byteLength}%c`,
199
176
  ),
200
- `color:${colors.red}`,
177
+ `color:${colors.outgoing}`,
201
178
  'color:inherit',
202
179
  'color:gray;font-weight:normal',
203
180
  'color:inherit;font-weight:inherit',
@@ -210,9 +187,9 @@ export async function logIncomingClientMessage(
210
187
 
211
188
  /**
212
189
  * Prints the outgoing client message initiated
213
- * by `client.send()` in the event handler.
190
+ * by `server.send()` in the event handler.
214
191
  */
215
- export async function logIncomingMockedClientMessage(
192
+ async function logOutgoingMockedClientMessage(
216
193
  event: MessageEvent<WebSocketData>,
217
194
  ) {
218
195
  const byteLength = getMessageLength(event.data)
@@ -221,9 +198,9 @@ export async function logIncomingMockedClientMessage(
221
198
  // eslint-disable-next-line no-console
222
199
  console.groupCollapsed(
223
200
  devUtils.formatMessage(
224
- `${getTimestamp({ milliseconds: true })} %c⇣%c ${publicData} %c${byteLength}%c`,
201
+ `${getTimestamp({ milliseconds: true })} %c⬆%c ${publicData} %c${byteLength}%c`,
225
202
  ),
226
- `color:${colors.orange}`,
203
+ `color:${colors.mocked}`,
227
204
  'color:inherit',
228
205
  'color:gray;font-weight:normal',
229
206
  'color:inherit;font-weight:inherit',
@@ -234,25 +211,11 @@ export async function logIncomingMockedClientMessage(
234
211
  console.groupEnd()
235
212
  }
236
213
 
237
- function logConnectionClose(event: CloseEvent) {
238
- const target = event.target as WebSocket
239
- const publicUrl = toPublicUrl(target.url)
240
-
241
- // eslint-disable-next-line no-console
242
- console.groupCollapsed(
243
- devUtils.formatMessage(
244
- `${getTimestamp({ milliseconds: true })} %c■%c ${publicUrl}`,
245
- ),
246
- `color:${colors.blue}`,
247
- 'color:inherit',
248
- )
249
- // eslint-disable-next-line no-console
250
- console.log(event)
251
- // eslint-disable-next-line no-console
252
- console.groupEnd()
253
- }
254
-
255
- export async function logIncomingServerMessage(
214
+ /**
215
+ * Prints the outgoing client message initiated
216
+ * by `client.send()` in the event handler.
217
+ */
218
+ async function logIncomingMockedClientMessage(
256
219
  event: MessageEvent<WebSocketData>,
257
220
  ) {
258
221
  const byteLength = getMessageLength(event.data)
@@ -261,9 +224,9 @@ export async function logIncomingServerMessage(
261
224
  // eslint-disable-next-line no-console
262
225
  console.groupCollapsed(
263
226
  devUtils.formatMessage(
264
- `${getTimestamp({ milliseconds: true })} %c⇣%c ${publicData} %c${byteLength}%c`,
227
+ `${getTimestamp({ milliseconds: true })} %c⬇%c ${publicData} %c${byteLength}%c`,
265
228
  ),
266
- `color:${colors.green}`,
229
+ `color:${colors.mocked}`,
267
230
  'color:inherit',
268
231
  'color:gray;font-weight:normal',
269
232
  'color:inherit;font-weight:inherit',
@@ -274,17 +237,20 @@ export async function logIncomingServerMessage(
274
237
  console.groupEnd()
275
238
  }
276
239
 
277
- function logClientError(event: Event) {
278
- const socket = event.target as WebSocket
279
- const publicUrl = toPublicUrl(socket.url)
240
+ async function logIncomingServerMessage(event: MessageEvent<WebSocketData>) {
241
+ const byteLength = getMessageLength(event.data)
242
+ const publicData = await getPublicData(event.data)
243
+ const arrow = event.defaultPrevented ? '⇣' : '⬇'
280
244
 
281
245
  // eslint-disable-next-line no-console
282
246
  console.groupCollapsed(
283
247
  devUtils.formatMessage(
284
- `${getTimestamp({ milliseconds: true })} %c\u00D7%c ${publicUrl}`,
248
+ `${getTimestamp({ milliseconds: true })} %c${arrow}%c ${publicData} %c${byteLength}%c`,
285
249
  ),
286
- `color:${colors.blue}`,
250
+ `color:${colors.incoming}`,
287
251
  'color:inherit',
252
+ 'color:gray;font-weight:normal',
253
+ 'color:inherit;font-weight:inherit',
288
254
  )
289
255
  // eslint-disable-next-line no-console
290
256
  console.log(event)