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 4 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
64 changes: 64 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 WebSocket, MessageEvent, CloseEvent;
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* Load WebSocket class from undici
* @returns {void}
*/
function loadWebSocket() {
if (!WebSocket) {
({ WebSocket } = require('internal/deps/undici/undici'));
}
}

/**
* Load MessageEvent class from undici
* @returns {void}
*/
function loadMessageEvent() {
if (!MessageEvent) {
({ MessageEvent } = require('internal/deps/undici/undici'));
}
}

/**
* Load CloseEvent class from undici
* @returns {void}
*/
function loadCloseEvent() {
if (!CloseEvent) {
({ CloseEvent } = require('internal/deps/undici/undici'));
}
}

anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
module.exports = {
_connectionListener,
METHODS: ArrayPrototypeSort(ArrayPrototypeSlice(methods)),
Expand All @@ -126,6 +157,9 @@ module.exports = {
OutgoingMessage,
Server,
ServerResponse,
WebSocket,
MessageEvent,
CloseEvent,
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
createServer,
validateHeaderName,
validateHeaderValue,
Expand Down Expand Up @@ -162,3 +196,33 @@ ObjectDefineProperty(module.exports, 'globalAgent', {
httpAgent.globalAgent = value;
},
});

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

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

ObjectDefineProperty(module.exports, 'MessageEvent', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
loadMessageEvent();
return 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');

assert.strictEqual(typeof NodeHttpWebSocket, 'function');
assert.strictEqual(typeof NodeHttpCloseEvent, 'function');
assert.strictEqual(typeof NodeHttpMessageEvent, 'function');
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved

// compare with global objects
assert.strictEqual(NodeHttpWebSocket, WebSocket);
assert.strictEqual(NodeHttpCloseEvent, CloseEvent);
assert.strictEqual(NodeHttpMessageEvent, MessageEvent);