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

Feat: Add ability to set headers for WebSocket client #4436

Merged
merged 4 commits into from Mar 13, 2024

Conversation

marcus-j-davies
Copy link

@marcus-j-davies marcus-j-davies commented Nov 14, 2023

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Proposed changes

This PR adds the ability to set Headers for the WebSocket client.
a lot of the editor code was lifted from the HTTP Request node, with some small differences.

  • Values are iether typed or can be taken from env, global or a static string
  • Similarly, the header names can only be set from a typed value, or a static string

The hint text has been updated, to state that headers are only used during the Upgrade routine, and this message has been translated accordingly.

A few checks are in place, to ensure users who have not provided a headers object in the node (i.e they are in receipt of this change without making changes) does not cause an undefined error

Screenshot 2023-11-14 at 21 21 23

Discussion here:
https://discourse.nodered.org/t/allow-adding-headers-to-websocket-client

Checklist

  • I have read the contribution guidelines
  • For non-bugfix PRs, I have discussed this change on the forum/slack team.
  • I have run npm run test to verify the unit tests pass
  • I have added suitable unit tests to cover the new/changed functionality

@marcus-j-davies
Copy link
Author

marcus-j-davies commented Nov 20, 2023

@knolleary, @dceejay

Is there a reason why this PR isn't triggering other checks? where other PR's have?
This is my first PR on the project - maybe its that?

I can't think what I would have missed

@marcus-j-davies
Copy link
Author

marcus-j-davies commented Nov 27, 2023

@Steve-Mcl, @knolleary

I have just noticed the dev branch is tagged for V4.

This PR checks for uninitialised properties of headers - as I assumed we will have hit another feature update before V4, therefore checking for uninitialised headers to stop it causing a break for implementations of the websocket client created in 3.1.x

if(node.upgradeHeaders !== undefined && node.upgradeHeaders.length > 0){

With the dev branch targeting V4, would you prefer I remove this check, and render it as a breaking change?

@Steve-Mcl
Copy link
Contributor

Is there a reason why this PR isn't triggering other checks?

Not certain what you mean? As far as I can see, tests have ran?

image

With the dev branch targeting V4, would you prefer I remove this check, and render it as a breaking change?

We will evaluate the situation after 3.1.1 (when we prepare for v4)


Thanks for your efforts thus far.

@marcus-j-davies
Copy link
Author

Not certain what you mean? As far as I can see, tests have ran?

it was addressed by @dceejay shortly after😄

@knolleary knolleary added this to the 4.0 milestone Jan 19, 2024
@knolleary
Copy link
Member

Thanks for this @marcus-j-davies - finally getting to reviewing this for 4.0

I'm not sure we need so many predefined headers in the list as many of them have no meaning in a WebSocket upgrade request. For example content-type will do nothing.

I suspect the main use-case is to be able to set the Authorization header - so maybe that should be the only predefined header provided. We should do some digging to see if there are any other commonly required headers.

As you note, you allow the value to be pulled from global and env. We would normally have both flow and global if we have any of them. In the backend, the call to evaluateNodeProperty then also must use an async callback as looking up a global value may require async work.

Given the time passed since you worked on this, I'm happy to do some of this rework as part of the 4.0 prep - but do let me know if you want to dust it off and make some of these updates yourself.

@marcus-j-davies
Copy link
Author

marcus-j-davies commented Mar 7, 2024

Hi @knolleary

I'm not sure we need so many predefined headers in the list as many of them have no meaning in a WebSocket upgrade request. For example content-type will do nothing.

Agreed!
I'll reduce it down to the supported headers (I'll review the spec)

As you note, you allow the value to be pulled from global and env. We would normally have both flow and global if we have any of them. In the backend, the call to evaluateNodeProperty then also must use an async callback as looking up a global value may require async work.

The reason I have not allowed flow, msg or J; - is that this code block is part of the config node, so flow and msg are actually undefined here, this allowed me to escape the need for async, given JSONata is not used 😄

So IMO - please do correct me if wrong, is to reduce down the list of headers + any tweaks you feel are necessary?

Tweaks such as - should we really check for an undefined object for the headers array?

@knolleary
Copy link
Member

There is absolutely no reason this should be a breaking change; it is adding a new feature, not changing an existing one. So the checks you have in place are entirely appropriate.

If we want to allow global context access, then it must use a callback in evaluateNodeProperty as global context could be backed by an async store.

It might be easier to just remove that option for now - nothing says we can't add more options later.

@marcus-j-davies
Copy link
Author

Cool!

I'll push the edits a bit later.

@marcus-j-davies
Copy link
Author

@knolleary

For now : I have commented out some of the header list items (I couldn't find any definitive, to determine what are valid headers for an upgrade operation).

I have also removed access to global content

@knolleary
Copy link
Member

Looks good - have applied one small change to use the built-in env type, rather than redefining it. Will get this merged and into beta.0 this week!

@knolleary knolleary merged commit 208dd2a into node-red:dev Mar 13, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants