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

Expose request headers #2073

Closed
1 task done
johnwchadwick opened this issue Aug 16, 2022 · 4 comments
Closed
1 task done

Expose request headers #2073

johnwchadwick opened this issue Aug 16, 2022 · 4 comments

Comments

@johnwchadwick
Copy link

Is there an existing issue for this?

  • I've searched for any related issues and avoided creating a duplicate issue.

Description

I'd like to be able to get the verbatim request headers for diagnostic purposes, but today I don't see anywhere in the API surface where this can be intercepted. It would be very useful to have an additional API or event for this.

ws version

No response

Node.js Version

v16.14.2

System

  System:
    OS: Linux 5.19 Arch Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
    Memory: 10.30 GB / 15.26 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh

Expected result

N/A

Actual result

N/A

Attachments

No response

@lpinca
Copy link
Member

lpinca commented Aug 16, 2022

import { WebSocketServer } from 'ws';

const wss = new WebSocketServer({ port: 8080 });

wss.on('connection', function (websocket, request) {
  console.log(request.headers);
});

or using server.handleUpgrade() directly:

import { createServer } from 'http';
import { WebSocketServer } from 'ws';

const server = createServer();
const wss = new WebSocketServer({ noServer: true });

wss.on('connection', function (websocket, request) {
  // ...
});

server.on('upgrade', function (request, socket, head) {
  console.log(request.headers);

  wss.handleUpgrade(request, socket, head, function done(websocket) {
    wss.emit('connection', websocket, request);
  });
});

server.listen(8080);

@johnwchadwick
Copy link
Author

Thanks for the quick response! Apologies, I realize now that I've made my feature request ambiguous. I meant with reference to the WebSocket client, not the server. That is, the ability to see the actual complete request headers that it sends.

@lpinca
Copy link
Member

lpinca commented Aug 16, 2022

You can use

const ws = new WebSocket(url);

ws._req.getHeaders();

but I would rather not turn it into an officially supported API.

@johnwchadwick
Copy link
Author

Fair enough. I appreciate the fast responses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants