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 ws client adapter #319

Merged
merged 28 commits into from
Dec 6, 2022
Merged

Conversation

Souvikns
Copy link
Member

Description
This PR is trying to implement a websocket client adapter for glee.

So the main idea is, if I have a WebSocket server, my glee application should be able to -

  • connect to that server
  • receive and send data
  • validate the data received or sent according to the specification file.

Related issue(s)

Fixes #259

@Souvikns Souvikns changed the title Ws client feat: add ws client adapter Jun 21, 2022
@coveralls
Copy link

coveralls commented Jun 21, 2022

Pull Request Test Coverage Report for Build 2533968711

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 90.773%

Totals Coverage Status
Change from base Build 2412938459: 0.0%
Covered Lines: 285
Relevant Lines: 299

💛 - Coveralls

src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/registerAdapters.ts Outdated Show resolved Hide resolved
@Souvikns Souvikns requested a review from fmvilas July 14, 2022 09:14
@Souvikns
Copy link
Member Author

@fmvilas need your review again

@jonaslagoni
Copy link
Member

@fmvilas as you started the review process, I am assuming you wish to finish it 🙂

examples/ws-server/.glee/functions/greet.js Outdated Show resolved Hide resolved
examples/dummy/asyncapi.yaml Outdated Show resolved Hide resolved
examples/dummy/asyncapi.yaml Outdated Show resolved Hide resolved
.eslintignore Outdated Show resolved Hide resolved
@Souvikns Souvikns requested a review from fmvilas August 24, 2022 11:36
@Souvikns
Copy link
Member Author

One weird issue I am running into while sending headers from the client side, is the headers are converted to lowercase.

{
  'sec-websocket-version': '13',
  'sec-websocket-key': 'nL+pNzTS+0ryHcEU/4ccPQ==',
  connection: 'Upgrade',
  upgrade: 'websocket',
  authorization: 'bearer arb-tokenValue',
  'sec-websocket-extensions': 'permessage-deflate; client_max_window_bits',
  host: 'localhost:3000'
}

Whereas I am sending the Authorization header and it is received as authorization

    bindings:
      ws:
        bindingVersion: 0.1.0
        headerValues:
          Authorization: 'bearer $TOKEN'

Found this https://stackoverflow.com/questions/50477233/issue-with-netty-websocket-headers-name-in-lowercase

- Supports ws client to pass in header and query values from asyncapi file.
- injects env to string
- makes header validation case insensitive
examples/crypto-websockets/README.md Outdated Show resolved Hide resolved
examples/crypto-websockets/client/asyncapi.yaml Outdated Show resolved Hide resolved
examples/crypto-websockets/client/functions/index.ts Outdated Show resolved Hide resolved
examples/crypto-websockets/server/asyncapi.yaml Outdated Show resolved Hide resolved
websocket:
url: ws://localhost:3000
protocol: ws
x-kind: local
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this property out of the server definition because that's how it will be done in v3.0.0. For now, we can just use our own extension like this:

Suggested change
x-kind: local
x-localServers:
- websocket

This will change the logic in the registerAdapters.ts file.

src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
@sonarcloud
Copy link

sonarcloud bot commented Oct 12, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 2 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there. Just left a suggestion to make remote/local server stuff more similar to what it would be in v3.

examples/crypto-websockets/client/asyncapi.yaml Outdated Show resolved Hide resolved
websocket:
url: ws://localhost:3000
protocol: ws
x-kind: local
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this property out of the server definition because that's how it will be done in v3.0.0. For now, we can just use our own extension like this:

Suggested change
x-kind: local
x-localServers:
- websocket

This will change the logic in the registerAdapters.ts file.

src/registerAdapters.ts Outdated Show resolved Hide resolved
src/adapters/ws/client.ts Outdated Show resolved Hide resolved
@@ -18,8 +18,9 @@ export type WebsocketAdapterConfig = {
port?: number,
},
client?: {
query?: any
authentication?: {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
authentication?: {
headers?: any

Instead of adding authentication should we just allow header and query config, with headers the user is able to set any type of authentication he desires?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the user can already do that once you merge this PR of yours, right? We only saw the need for authentication because it usually contains secrets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, just a suggestion, currently we have an authentication config and we are creating a header Authentication: bearer TOKEN, and it works but maybe we just allow headers instead of authentication, and then we have a broader control, but we only have the need for authentication. We can always change it in the future if there is a need.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd have broader control at Glee but we'd be breaking the purpose of the AsyncAPI specification. If the headers values are in the AsyncAPI document, other tools will be able to see them. If they're in the Glee config, only Glee will be able to see them. You see the problem?

For instance, in #261 we want to use Generator to generate docs automatically. If the info is not in the AsyncAPI file, it will not end up in the documentation. And the same will happen for all the tools that the user might want to apply to their AsyncAPI file.

@sonarcloud
Copy link

sonarcloud bot commented Dec 2, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link
Member

@fmvilas fmvilas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 🚀

@Souvikns
Copy link
Member Author

Souvikns commented Dec 6, 2022

@fmvilas sentiment analysis is failing, can you rerun the tests, I can't merge right now.

@fmvilas
Copy link
Member

fmvilas commented Dec 6, 2022

@Souvikns done. I hope your sentiments are fine now 😜

@Souvikns
Copy link
Member Author

Souvikns commented Dec 6, 2022

/rtm

@asyncapi-bot asyncapi-bot merged commit 63c9cc2 into asyncapi:master Dec 6, 2022
@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 0.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

Add support for WebSocket clients
5 participants