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

but this path is not defined in your AsyncAPI file - Caused by trailing slash #713

Closed
NaCl5alt opened this issue Feb 1, 2024 · 6 comments · Fixed by #769
Closed

but this path is not defined in your AsyncAPI file - Caused by trailing slash #713

NaCl5alt opened this issue Feb 1, 2024 · 6 comments · Fixed by #769
Labels

Comments

@NaCl5alt
Copy link
Contributor

NaCl5alt commented Feb 1, 2024

Overview

Currently, attempts to access the Websocket API, including trailing slashes, result in an error but this path is not defined in your AsyncAPI file.
I believe that resolving this will make the library easier to use.

Solution

Make the following changes to _extractPathname.
If it looks good, assign me!

private _extractPathname(req: IncomingMessage) {
  const serverUrl = new URL(this.serverUrlExpanded)
  const { pathname } = new URL(req.url, serverUrl)

  if (!pathname) return '/'

  if (pathname.endsWith('/')) {
    return pathname.substring(0, pathname.length - 1)
  }

  return pathname
}

private _extractPathname(req: IncomingMessage) {
const serverUrl = new URL(this.serverUrlExpanded)
const { pathname } = new URL(req.url, serverUrl)
return pathname || '/'
}

Append

I was careless.
That you can also set an address in asyncapi.yaml that includes a trailing slash.
Perhaps we need to remove the trailing slash in the channels method of @asyncapi/parser for unification, and also in the tilde method of utils.ts if we want to make it more correct.
https://github.com/asyncapi/parser-js/blob/9742b8145020f43db598fe394702fbab5a9c868e/src/models/v3/asyncapi.ts#L61-L67

https://github.com/asyncapi/parser-js/blob/9742b8145020f43db598fe394702fbab5a9c868e/src/utils.ts#L113-L121

If the impact is too large, I would suggest changing the _getChannel method as follows

private _getChannel(req: IncomingMessage) {
  const pathName = this._extractPathname(req)
  return this.parsedAsyncAPI.channels().all().filter(channel => {
    let address = channel.address()
    if(address.endsWith('/')) address = address.substring(0, address.length - 1)

    return address === pathName
  })[0]
}

private _getChannel(req: IncomingMessage) {
const pathName = this._extractPathname(req)
return this.parsedAsyncAPI.channels().all().filter(channel => channel.address() === pathName)[0]
}

Copy link
Contributor

github-actions bot commented Feb 1, 2024

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@Souvikns
Copy link
Member

@NaCl5alt sorry for the late reply, do you still wanna work on it

@NaCl5alt
Copy link
Contributor Author

@Souvikns
np, plz assign me if necessary

@KhudaDad414
Copy link
Member

@NaCl5alt we do not assign issues on GitHub. please feel free to open a PR.

@NaCl5alt
Copy link
Contributor Author

@KhudaDad414
OK, I'd like to tackle this issue.

@asyncapi-bot
Copy link
Contributor

🎉 This issue has been resolved in version 0.36.9 🎉

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
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants