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 7 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
42 changes: 42 additions & 0 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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,17 @@
return req;
}

/**
* Lazy loads WebSocket, CloseEvent and MessageEvent classes from undici
* @returns {Object} An object containing WebSocket, CloseEvent, and MessageEvent classes.

Check warning on line 122 in lib/http.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Invalid JSDoc @returns type "Object"; prefer: "object"
*/
function lazyWebSocket() {
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
if (!WebSocket) {
({ WebSocket, CloseEvent, MessageEvent } = require('internal/deps/undici/undici'));

Check failure on line 126 in lib/http.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Read-only global 'WebSocket' should not be modified
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
}
return { WebSocket, CloseEvent, MessageEvent };
anfibiacreativa marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = {
_connectionListener,
METHODS: ArrayPrototypeSort(ArrayPrototypeSlice(methods)),
Expand All @@ -126,6 +138,9 @@
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 +177,30 @@
httpAgent.globalAgent = value;
},
});

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

Check failure on line 187 in lib/http.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
});

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

Check failure on line 196 in lib/http.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
});

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

Check failure on line 205 in lib/http.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing trailing comma
});
11 changes: 11 additions & 0 deletions test/parallel/test-http-import-websocket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

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

Check failure on line 5 in test/parallel/test-http-import-websocket.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

This line has a length of 130. Maximum allowed is 120


// compare with global objects

Check failure on line 8 in test/parallel/test-http-import-websocket.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Comments should not begin with a lowercase character
assert.strictEqual(NodeHttpWebSocket, WebSocket);
assert.strictEqual(NodeHttpCloseEvent, CloseEvent);
assert.strictEqual(NodeHttpMessageEvent, MessageEvent);
Loading