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

WebSocket data can be a primitive, causing exception #170

Closed
alberto-i opened this issue Jul 26, 2024 · 2 comments · Fixed by #172
Closed

WebSocket data can be a primitive, causing exception #170

alberto-i opened this issue Jul 26, 2024 · 2 comments · Fixed by #172
Labels
bug Something isn't working solved The question was answered or the problem was solved

Comments

@alberto-i
Copy link

Hello,

After upgrading from version 3.6.0 to 4.3.0, I started receiving the following error:

TypeError: Invalid value used as weak map key
    at WeakMap.set (<anonymous>)
    at ContextClsStoreMap.set (node_modules/nestjs-cls/dist/src/lib/cls-initializers/utils/context-cls-store-map.js:26:25)
    at ClsGuard.canActivate (node_modules/nestjs-cls/dist/src/lib/cls-initializers/cls.guard.js:34:56)
    at GuardsConsumer.tryActivate (node_modules/@nestjs/core/guards/guards-consumer.js:15:34)
....

I'm using "ClsGuard" in a WebSsocket context, and in this case, my WebSocket data is a string and not an object.

The ContextClsStoreMap.getContextByType function, with context.switchToWs().getData(), is leading to this error.

For now, I'm reverting to 3.6.0. And if I get more time, I'll check if I can fix it and do a pull request.

@Papooch Papooch added the bug Something isn't working label Jul 26, 2024
@Papooch
Copy link
Owner

Papooch commented Jul 26, 2024

Thanks for the bug report and also pin-pointing the exact issue!

The context map is there mainly as an escape hatch for some non-trivial GraphQL use-cases (see #88).

In the case of websockets, I think it's safe to use the entire WsArgumentsHost as the context map key, so context.switchToWs() should be enough for a fix (seeing as there is no other object that can be used instead of Data).

If you want, create a PR with that change, otherwise I'll get to it when I get some free time.

@Papooch
Copy link
Owner

Papooch commented Aug 6, 2024

Fixed in nestjs-cls@4.4.1

@Papooch Papooch added the solved The question was answered or the problem was solved label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working solved The question was answered or the problem was solved
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants