0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-24 12:10:08 +01:00

http: expose websockets

PR-URL: https://github.com/nodejs/node/pull/53721
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Natalia Venditto 2024-07-08 17:55:43 +02:00 committed by GitHub
parent 1d4d76ff3f
commit a1869fa87e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 0 deletions

View File

@ -4228,6 +4228,15 @@ added:
Set the maximum number of idle HTTP parsers. 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 [RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt
[`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch [`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch
[`'checkContinue'`]: #event-checkcontinue [`'checkContinue'`]: #event-checkcontinue
@ -4244,6 +4253,7 @@ Set the maximum number of idle HTTP parsers.
[`Headers`]: globals.md#class-headers [`Headers`]: globals.md#class-headers
[`TypeError`]: errors.md#class-typeerror [`TypeError`]: errors.md#class-typeerror
[`URL`]: url.md#the-whatwg-url-api [`URL`]: url.md#the-whatwg-url-api
[`WebSocket`]: #websocket
[`agent.createConnection()`]: #agentcreateconnectionoptions-callback [`agent.createConnection()`]: #agentcreateconnectionoptions-callback
[`agent.getName()`]: #agentgetnameoptions [`agent.getName()`]: #agentgetnameoptions
[`destroy()`]: #agentdestroy [`destroy()`]: #agentdestroy

View File

@ -42,6 +42,7 @@ const {
ServerResponse, ServerResponse,
} = require('_http_server'); } = require('_http_server');
let maxHeaderSize; let maxHeaderSize;
let undici;
/** /**
* Returns a new instance of `http.Server`. * Returns a new instance of `http.Server`.
@ -114,6 +115,14 @@ function get(url, options, cb) {
return req; 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 = { module.exports = {
_connectionListener, _connectionListener,
METHODS: methods.toSorted(), METHODS: methods.toSorted(),
@ -160,3 +169,30 @@ ObjectDefineProperty(module.exports, 'globalAgent', {
httpAgent.globalAgent = value; 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;
},
});

View File

@ -0,0 +1,14 @@
'use strict';
require('../common');
const assert = require('assert');
const {
WebSocket: NodeHttpWebSocket,
CloseEvent: NodeHttpCloseEvent,
MessageEvent: NodeHttpMessageEvent
} = require('node:http');
// Compare with global objects
assert.strictEqual(NodeHttpWebSocket, WebSocket);
assert.strictEqual(NodeHttpCloseEvent, CloseEvent);
assert.strictEqual(NodeHttpMessageEvent, MessageEvent);