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: support server-sent events #2299

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft

feat: support server-sent events #2299

wants to merge 19 commits into from

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Sep 30, 2024

Todo

  • Accept an OutgoingEvents type argument at sse() to make type-safe event property on the mock payload.
  • Support passthrough(). How do you intercept an EventSource, interact with it from the mock, and then passthrough after that?
  • Support event augmentation. How do you listen to the actual server's events, modify them, and send them back to the client?
  • Add test for withCredentials affecting the request cookies propagation.
  • How to handle passthrough()? What if I want to let the EventSource receive whichever events the actual server sends from now on? Still manual?
  • Add tests for .use() with sse().
  • Support retry
  • Add an invariant for typeof EventSource !== 'undefined' to let the developer know when they are using sse() in the environment that doesn't support it.
  • Documentation

References

@kettanaito
Copy link
Member Author

Augmenting actual server-sent events

sse(url, ({ source, server }) => {
  // This creates a new EventSource request
  // and propagates all the events from here
  // to the underlying pending stream for `source`.
  server.connect()
})

Modifying the event is similar to that in WebSockets: prevent its default and send a new event:

sse(url, ({ source, server }) => {
  server.connect()

  server.addEventListener('message', (event) => {
    // Prevent this server event from reaching the client.
    event.preventDefault()
    const newEvent = modify(event.data)
    source.send(newEvent)
  })
})

May be a good idea to rename source to client and have a consistent SSE/WebSocket experience.

@kettanaito
Copy link
Member Author

kettanaito commented Oct 2, 2024

The tests are failing likely due to Node.js bump to v20 in CI.

Edit: Looks like an issue specific to a particular version of Node.js. Reported here: nodejs/undici#3676

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

Successfully merging this pull request may close these issues.

First-class Server-Sent Events (SSE) API
1 participant