http: expose websockets · nodejs/node@1367c55 · GitHub
Skip to content

Commit 1367c55

Browse files
anfibiacreativaaduh95
authored andcommitted
http: expose websockets
PR-URL: #53721 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 87121a1 commit 1367c55

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

doc/api/http.md

Lines changed: 10 additions & 0 deletions

lib/http.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
ServerResponse,
4343
} = require('_http_server');
4444
let maxHeaderSize;
45+
let undici;
4546

4647
/**
4748
* Returns a new instance of `http.Server`.
@@ -114,6 +115,14 @@ function get(url, options, cb) {
114115
return req;
115116
}
116117

118+
/**
119+
* Lazy loads WebSocket, CloseEvent and MessageEvent classes from undici
120+
* @returns {object} An object containing WebSocket, CloseEvent, and MessageEvent classes.
121+
*/
122+
function lazyUndici() {
123+
return undici ??= require('internal/deps/undici/undici');
124+
}
125+
117126
module.exports = {
118127
_connectionListener,
119128
METHODS: methods.toSorted(),
@@ -160,3 +169,30 @@ ObjectDefineProperty(module.exports, 'globalAgent', {
160169
httpAgent.globalAgent = value;
161170
},
162171
});
172+
173+
ObjectDefineProperty(module.exports, 'WebSocket', {
174+
__proto__: null,
175+
configurable: true,
176+
enumerable: true,
177+
get() {
178+
return lazyUndici().WebSocket;
179+
},
180+
});
181+
182+
ObjectDefineProperty(module.exports, 'CloseEvent', {
183+
__proto__: null,
184+
configurable: true,
185+
enumerable: true,
186+
get() {
187+
return lazyUndici().CloseEvent;
188+
},
189+
});
190+
191+
ObjectDefineProperty(module.exports, 'MessageEvent', {
192+
__proto__: null,
193+
configurable: true,
194+
enumerable: true,
195+
get() {
196+
return lazyUndici().MessageEvent;
197+
},
198+
});
Lines changed: 12 additions & 0 deletions

0 commit comments

Comments
 (0)