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: create method on client to get websocket pattern #10545

Merged
merged 5 commits into from Jun 12, 2023

Conversation

jmcdo29
Copy link
Member

@jmcdo29 jmcdo29 commented Nov 9, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

The only way to get the websocket pattern is through reflection, which is unavailable inside of filters as the ArgumentsHost doesn't have a getClass or getHandler method.

Issue Number: #10520

What is the new behavior?

ExecutionContext#switchToWs().getClient() now has a getPattern() method attached to it so that the pattern can be retrieved in filters.

This pattern is still a reflected pattern, and not retrieved from the direct request, as we don't, to my knowledge, bind the exception filter for when invalid patterns are called (if I'm wrong about that please point to where it is)

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@coveralls
Copy link

coveralls commented Nov 9, 2022

Pull Request Test Coverage Report for Build 687f5680-c7ea-440a-8484-35693509459c

  • 3 of 4 (75.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 92.951%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/helpers/execution-context-host.ts 0 1 0.0%
Totals Coverage Status
Change from base Build 13b747f0-1270-494c-96aa-f7166e331f9e: -0.01%
Covered Lines: 6382
Relevant Lines: 6866

💛 - Coveralls

@jmcdo29 jmcdo29 changed the title feat: create on client to get websocket pattern feat: create method on client to get websocket pattern Nov 9, 2022
@kamilmysliwiec
Copy link
Member

ExecutionContext#switchToWs().getClient() returns the Socket instance. What if someone used this type (as socket.io now ships TS declarations out-of-the-box) as a type argument, as follows:

ExecutionContext#switchToWs().getClient<Socket>()

?

@jmcdo29
Copy link
Member Author

jmcdo29 commented Nov 9, 2022

ExecutionContext#switchToWs().getClient() returns the Socket instance. What if someone used this type (as socket.io now ships TS declarations out-of-the-box) as a type argument, as follows:

ExecutionContext#switchToWs().getClient<Socket>()

?

We could update the generic of getClient() to be getClient<T>(): T & { getPattern: () => string }. Might not be the most ideal, but with the current approach I think it's the best we've got

Comment on lines 111 to 113
Object.assign(args[0] ?? {}, {
getPattern: () => this.reflectCallbackPattern(callback),
});
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Object.assign(args[0] ?? {}, {
getPattern: () => this.reflectCallbackPattern(callback),
});
Object.assign(args[0] ?? {}, {
getPattern: () => this.reflectCallbackPattern(callback),
});

Hmm.. I'm wondering if this won't cause some unexpected issues 🤔 If socket represents a single connection (and so its instance object is shared?), there's a probability that 2 messages from that socket might be processed asynchronously (independently).

Example:

  • Connection to server is established (socket instance is created)
  • Message A is emitted
    • we mutate the "socket instance" enhancing it with the "getPattern()" method
    • before this message is processed, some asynchronous operation is triggered in (guard/interceptor/wherever)
  • In the meantime, message B is emitted
    • we mutate the same "socket instance" again replacing the previous "getPattern()" method
  • Async operation (mentioned above) completes and we're back to processing message A.
  • In another interceptor/guard we call client.getPattern() when processing message A but it's already overridden with the message B's "getPattern" implementation

Copy link
Member Author

Choose a reason for hiding this comment

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

Rather than mutating the client, could I add a new args entry and let getPattern() retrieve args[2]? Would that be more "stable"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Great call by the way! I moved getPattern to be a method on WsArgumentHost instead of under the getClient() so that it should be unique per request as the WsArgumentHost already is. Tests are still passing and the interfaces are updated. Let me know if you think of any other issues with this approach 😸

@@ -35,7 +35,7 @@ jobs:
- checkout
- run:
name: Update NPM version
command: 'sudo npm install -g npm@latest'
command: 'sudo npm install -g npm@^8'
Copy link
Member Author

Choose a reason for hiding this comment

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

npm v9 was released on 2022-10-24 is is not compatible with node 12. Today (2022-11-09) it was set to the @latest tag for npm so our CircleCI builds started breaking.

Wednesday Nov. 9th (General Availability)
To ensure npm@9.x is considered "non-breaking" for Node.js LTS we will codify a set of exit criteria in collaboration with the Release WG
npm@9.x will be set to the latest dist-tag (becoming the latest, maintained version of npm)
A PR will be opened to land npm@9.x in nodejs/node's main branch (exposing experimental/nightly users to this latest version)

https://github.blog/changelog/2022-10-24-npm-v9-0-0-released/

@jmcdo29
Copy link
Member Author

jmcdo29 commented Dec 17, 2022

Hey @kamilmysliwiec anything else you'd like me to work with on this feature?

@jmcdo29
Copy link
Member Author

jmcdo29 commented Apr 11, 2023

Hey @kamilmysliwiec any chance this can make it in before v10?

@jmcdo29
Copy link
Member Author

jmcdo29 commented May 31, 2023

@kamilmysliwiec just making sure there's visibility on this. I know you've got your hands full with a lot.

I'd like to get this merged in so I can make a minor release with a new feature for ogma before I release a major upgrade with breaking changes, as this would be a super helpful feature when it comes to logging errors

@kamilmysliwiec kamilmysliwiec merged commit 5481a01 into nestjs:master Jun 12, 2023
@kamilmysliwiec
Copy link
Member

LGTM

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.

None yet

4 participants