Skip to content

Commit f692222

Browse files
fix(ws): call BroadcastChannel.unref in Node.js environment (#2196)
1 parent 37f5d29 commit f692222

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/core/ws.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@ import {
1111
import { Path, isPath } from './utils/matching/matchRequestUrl'
1212
import { WebSocketClientManager } from './ws/WebSocketClientManager'
1313

14+
function isBroadcastChannelWithUnref(
15+
channel: BroadcastChannel,
16+
): channel is BroadcastChannel & NodeJS.RefCounted {
17+
return typeof Reflect.get(channel, 'unref') !== 'undefined'
18+
}
19+
1420
const wsBroadcastChannel = new BroadcastChannel('msw:ws-client-manager')
1521

22+
if (isBroadcastChannelWithUnref(wsBroadcastChannel)) {
23+
// Allows the Node.js thread to exit if it is the only active handle in the event system.
24+
// https://nodejs.org/api/worker_threads.html#broadcastchannelunref
25+
wsBroadcastChannel.unref()
26+
}
27+
1628
export type WebSocketLink = {
1729
/**
1830
* A set of all WebSocket clients connected

0 commit comments

Comments
 (0)