Skip to content

Commit

Permalink
http: expose websockets
Browse files Browse the repository at this point in the history
PR-URL: #53721
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Paolo Insogna <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
anfibiacreativa authored and aduh95 committed Jul 16, 2024
1 parent 87121a1 commit 1367c55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -4228,6 +4228,15 @@ added:
Set the maximum number of idle HTTP parsers.
## `WebSocket`
<!-- YAML
added:
- REPLACEME
-->
A browser-compatible implementation of [`WebSocket`][].
[RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt
[`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch
[`'checkContinue'`]: #event-checkcontinue
Expand All @@ -4244,6 +4253,7 @@ Set the maximum number of idle HTTP parsers.
[`Headers`]: globals.md#class-headers
[`TypeError`]: errors.md#class-typeerror
[`URL`]: url.md#the-whatwg-url-api
[`WebSocket`]: #websocket
[`agent.createConnection()`]: #agentcreateconnectionoptions-callback
[`agent.getName()`]: #agentgetnameoptions
[`destroy()`]: #agentdestroy
Expand Down
36 changes: 36 additions & 0 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
ServerResponse,
} = require('_http_server');
let maxHeaderSize;
let undici;

/**
* Returns a new instance of `http.Server`.
Expand Down Expand Up @@ -114,6 +115,14 @@ function get(url, options, cb) {
return req;
}

/**
* Lazy loads WebSocket, CloseEvent and MessageEvent classes from undici
* @returns {object} An object containing WebSocket, CloseEvent, and MessageEvent classes.
*/
function lazyUndici() {
return undici ??= require('internal/deps/undici/undici');
}

module.exports = {
_connectionListener,
METHODS: methods.toSorted(),
Expand Down Expand Up @@ -160,3 +169,30 @@ ObjectDefineProperty(module.exports, 'globalAgent', {
httpAgent.globalAgent = value;
},
});

ObjectDefineProperty(module.exports, 'WebSocket', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return lazyUndici().WebSocket;
},
});

ObjectDefineProperty(module.exports, 'CloseEvent', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return lazyUndici().CloseEvent;
},
});

ObjectDefineProperty(module.exports, 'MessageEvent', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return lazyUndici().MessageEvent;
},
});
12 changes: 12 additions & 0 deletions test/parallel/test-http-import-websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

require('../common');
const assert = require('assert');
const {
WebSocket: NodeHttpWebSocket,
MessageEvent: NodeHttpMessageEvent
} = require('node:http');

// Compare with global objects
assert.strictEqual(NodeHttpWebSocket, WebSocket);
assert.strictEqual(NodeHttpMessageEvent, MessageEvent);

0 comments on commit 1367c55

Please sign in to comment.