Skip to content

Commit 1367c55

Browse files
anfibiacreativaaduh95
authored andcommittedJul 16, 2024
http: expose websockets
PR-URL: #53721 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 87121a1 commit 1367c55

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
 

‎doc/api/http.md

+10
Original file line numberDiff line numberDiff line change
@@ -4228,6 +4228,15 @@ added:
42284228
42294229
Set the maximum number of idle HTTP parsers.
42304230
4231+
## `WebSocket`
4232+
4233+
<!-- YAML
4234+
added:
4235+
- REPLACEME
4236+
-->
4237+
4238+
A browser-compatible implementation of [`WebSocket`][].
4239+
42314240
[RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt
42324241
[`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch
42334242
[`'checkContinue'`]: #event-checkcontinue
@@ -4244,6 +4253,7 @@ Set the maximum number of idle HTTP parsers.
42444253
[`Headers`]: globals.md#class-headers
42454254
[`TypeError`]: errors.md#class-typeerror
42464255
[`URL`]: url.md#the-whatwg-url-api
4256+
[`WebSocket`]: #websocket
42474257
[`agent.createConnection()`]: #agentcreateconnectionoptions-callback
42484258
[`agent.getName()`]: #agentgetnameoptions
42494259
[`destroy()`]: #agentdestroy

‎lib/http.js

+36
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const {
6+
WebSocket: NodeHttpWebSocket,
7+
MessageEvent: NodeHttpMessageEvent
8+
} = require('node:http');
9+
10+
// Compare with global objects
11+
assert.strictEqual(NodeHttpWebSocket, WebSocket);
12+
assert.strictEqual(NodeHttpMessageEvent, MessageEvent);

0 commit comments

Comments
 (0)