Skip to content

Decouple internal data usage for Bun websockets #4271

@ClaudiuCeia

Description

@ClaudiuCeia

What is the feature you are proposing?

Right now, implementing websockets with Bun and Hono feels like a real downgrade (see #3230 as well).
As @manastunga787 rightfully pointed out in that thread, the Bun websocket object is still available under ws.raw but:

  • This is typed as optional. I'm not sure why, but of course it means peppering null checks wherever it's being used
  • More importantly, the data attribute is being leveraged for internal use leading to unexpected errors onClose:
// ....

    return {
      onOpen: (event, ws) => {
        const { raw } = ws;
        if (!raw) {
          // is there a chance for this to ever be the case?
          return;
        }

        // this leads to
        // TypeError: undefined is not an object (evaluating 'websocketListeners.onClose')
        // because the data initially attached is being overwritten
        raw.data = {
          foo: "bar",
        };
      },
      // ...
  };

I'm now copying the previously attached data as well and everything seems to work fine, but it was rather painful to debug.
I'm wondering if data could be monkey-patched such that Hono specific data is not stored alongside application specific data.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions