-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.js
27 lines (25 loc) · 968 Bytes
/
websocket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'use strict'
// This file is structured this way to allow webpack to optimize out
// the require calls based on hardcoded process.type per bundle.
// Do not refactor or deduplicate.
// Try to use global browser APIs (e.g. if in Electron), otherwise require impls
if (typeof process !== 'undefined' && process) {
// Node.js or Electron with Node.js integration
if (process.type === 'renderer' || process.type === 'worker') {
// Electron renderer with Node.js integration
module.exports = globalThis.WebSocket
} else {
// Node.js or Electron browser process
// eslint-disable-next-line unicorn/no-typeof-undefined
if (typeof globalThis.WebSocket === 'undefined') {
// Fall back to ws
module.exports = require('ws')
} else {
// Prefer Node.js WebSocket if exists
module.exports = globalThis.WebSocket
}
}
} else {
// Browser or Electron without Node.js integration
module.exports = globalThis.WebSocket
}