Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: expose websocket in nodehttp #53721

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4083955
http: expose websockets
anfibiacreativa Jul 4, 2024
9dd678b
test: add test for exposing websocket in http
anfibiacreativa Jul 4, 2024
a2e4f84
http: add dynamic loading on first usage
anfibiacreativa Jul 4, 2024
0d5d120
test: extend test to compare strict with global
anfibiacreativa Jul 4, 2024
e4b6558
http: refactor to lazyload classes more effectively
anfibiacreativa Jul 4, 2024
576bdb0
test: update test/parallel/test-http-import-websocket.js
anfibiacreativa Jul 4, 2024
ecfd5ec
http: refactor lazy loading function
anfibiacreativa Jul 4, 2024
9b65b5b
docs: add websocket reference to http doc
anfibiacreativa Jul 5, 2024
804276c
fix: fix linting errors from ci
anfibiacreativa Jul 5, 2024
6e1cb1d
http: prevent global override
anfibiacreativa Jul 6, 2024
a9c915e
docs: put placeholder version
anfibiacreativa Jul 6, 2024
c80014b
http: add return in scope
anfibiacreativa Jul 6, 2024
b529947
docs: fix linter errors
anfibiacreativa Jul 6, 2024
29e1861
http: update with suggestion to rename var
anfibiacreativa Jul 6, 2024
067782d
http: update return accordingly
anfibiacreativa Jul 6, 2024
fc9a869
docs: remove escaping
anfibiacreativa Jul 6, 2024
9aa63b0
docs: add link at the bottom of the page
anfibiacreativa Jul 6, 2024
20789fb
fix: fix linter docs
anfibiacreativa Jul 6, 2024
b5e0931
http: revert suggestion to fix errors
anfibiacreativa Jul 6, 2024
d3df897
fix: revert broken link
anfibiacreativa Jul 6, 2024
4d5f180
http: import all unidici in the lazy function
anfibiacreativa Jul 7, 2024
0c2c82b
http: remove additional export
anfibiacreativa Jul 8, 2024
aeed701
http: remove unnecessary declaration that was used in module.exports
anfibiacreativa Jul 8, 2024
7209a89
http: replace ternary
anfibiacreativa Jul 8, 2024
c928a97
http: rename function
anfibiacreativa Jul 8, 2024
cf3e9a7
http: rename function invocation
anfibiacreativa Jul 8, 2024
5ca86a1
http: fix typo in name function
anfibiacreativa Jul 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -44,6 +44,7 @@ const {
ServerResponse,
} = require('_http_server');
let maxHeaderSize;
let undici;

/**
* Returns a new instance of `http.Server`.
Expand Down Expand Up @@ -116,6 +117,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.
*/
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
function lazyUndici() {
return undici ??= require('internal/deps/undici/undici');
}

module.exports = {
_connectionListener,
METHODS: ArrayPrototypeSort(ArrayPrototypeSlice(methods)),
Expand Down Expand Up @@ -162,3 +171,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;
},
});
14 changes: 14 additions & 0 deletions test/parallel/test-http-import-websocket.js
Original file line number Diff line number Diff line change
@@ -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);
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
Loading