-
-
Couldn't load subscription status.
- Fork 562
feat: support server-sent events (SSE) #2299
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
base: main
Are you sure you want to change the base?
Conversation
Augmenting actual server-sent eventssse(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 |
|
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 |
Test updateI've resolved the infinite request issue that failed the majority of SSE tests (was using legacy passthrough header; migrating to Two tests are still failing, both seem to be related to errors. There's an odd rogue |
| * Determines if the given value is an object. | ||
| */ | ||
| export function isObject(value: any): boolean { | ||
| export function isObject(value: any): value is Record<string, any> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fwiw, this isn't exactly type-safe in a way you'd assume: TS playground
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch.
Co-authored-by: Artem Zakharchenko <[email protected]>
commit: |
|
UpdateThe |
|
Just wanna prefix this by saying, thanks a lot for introducing this and we are enjoying using it!! Tip/note to other potential PNPM users for when you are using ERR_PNPM_UNEXPECTED_PKG_CONTENT_IN_STORE Package name mismatch found while reading {"tarball":"https://pkg.pr.new/msw@2299"} from the store.
This means that either the lockfile is broken or the package metadata (name and version) inside the package's package.json file doesn't match the metadata in the registry.
Expected package: msw@2.11.0.
Actual package in the store with the given integrity: msw@2.10.4.The workaround is that you can use the specific commit version like |
|
Thanks, @zeroregard. Let me know if you have some suggestions around this API or some use cases that you think it doesn't cover. The feature is complete and tested, and I plan on releasing it later this month. It's a great chance to share early feedback until then so we could address potentially breaking changes before they are locked up until the next major release. Thanks! |
Todo
OutgoingEventstype argument atsse()to make type-safeeventproperty on the mock payload.passthrough(). How do you intercept an EventSource, interact with it from the mock, and then passthrough after that?withCredentialsaffecting the request cookies propagation.passthrough()? What if I want to let theEventSourcereceive whichever events the actual server sends from now on? Still manual?.use()withsse().retrytypeof EventSource !== 'undefined'to let the developer know when they are usingsse()in the environment that doesn't support it.[Error [TypeError]: Failed to fetch]error duringtest/browser/sse-api/sse.server.connect.test.tstests. It seems to be originating from the actualfetch()in the Service Worker (the scenarios callserver.connect()on the SSE).sse.client.send.test.ts(the last test case is unfinished).ssefrommswroot. WhileEventSourcedoesn't exist in Node.js, it exists in Deno. It might also be added to Node.js in the future. Afaik, there's nothing specific to browsers in our implementation. Keep the global check, export from the root for consistency.client.from()?References