razshare / sveltekit-sse

Server Sent Events with SvelteKit
https://www.npmjs.com/package/sveltekit-sse
MIT License
307 stars 9 forks source link

Fails silently if request has unexpected URL search params #32

Closed oscarhermoso closed 7 months ago

oscarhermoso commented 7 months ago

Firstly, very cool project. I am combining this with https://github.com/grmkris/drizzle-pg-notify-audit-table/tree/main to build a notification system for my app. Thank you for maintaining this library!

I was migrating some SvelteKit endpoints that previously were GET endpoints with search params to sveltekit-sse POST endpoints.

Unfortunately, requests with search params were failing silently, and this took a lot of time to debug.

Steps to reproduce

// src/routes/custom-event/+page.ts
import { source } from 'sveltekit-sse';

export const load = async ({ url, parent }) => {
    const searchParams = new URLSearchParams(url.search);

    searchParams.set('status', 'waiting');
    const queue = source(`/queue?${searchParams.toString()}`)
        .select('message')
        .json();

    queue.subscribe((value) => console.log('aaaaaaaaaaa', value)); // value is empty

    return {
        ...(await parent()),
        fetching_tutor_queue_waiting,
    };
};
// src/routes/custom-event/+server.js
import { events } from 'sveltekit-sse'

function delay(milliseconds) {
  return new Promise(function run(resolve) {
    setTimeout(resolve, milliseconds)
  })
}

export function POST({ request, url }) {
  return events({
    request,
    async start({emit}) {
      console.log('now listening'); // never fires

      const status = url.searchParams.get('status'); //  was intending to do something with status

      while(true){
        emit('message', 'hello world')
        await delay(1000)
      }
    },
  })
}

Expected

Request succeeds, with URL search params usable. If this is not possible, then error should be made visible to user

Actual

No error reported, request fails silently with 200 response

razshare commented 7 months ago

Hello @oscarhermoso ,

I just pushed an update to fix the query string issue, please update to version 0.8.15.

Here's a repo with a working example https://github.com/tncrazvan/sveltekit-sse-issue-32 , it resembles your reproduction code.

Remember that the first value provided will always be null because of SSR.

Let me know if this solves your problem.

oscarhermoso commented 7 months ago

Updated and now it's working great! Thank you @tncrazvan for the crazy fast fix :heart: