ngx-sse-client icon indicating copy to clipboard operation
ngx-sse-client copied to clipboard

onStreamProgress not handling undefined data property

Open patrickmichalina opened this issue 1 year ago • 3 comments

I am pushing through a NestJS fastify client, it technically does work, but throws an error here every single request. Curios as to why this may be happening.

  private onStreamProgress(data: string, observer: Subscriber<string>): void {
    data = data.substring(this.progress); // right here, data is undefined
    this.progress += data.length;
    data.split(/(\r\n|\r|\n){2}/g).forEach((part) => this.parseEventData(part, observer));
  }

patrickmichalina avatar Oct 08 '24 21:10 patrickmichalina

same issue here any one found solutions?

aqi2l avatar Nov 29 '24 09:11 aqi2l

same issue here any one found solutions?

I have a small patch that just returns void. I do not track progress or anything so it seems to work fine for now, your mileage may vary. Use https://www.npmjs.com/package/patch-package or pnpm patch

diff --git a/esm2022/lib/sse-client-subscriber.mjs b/esm2022/lib/sse-client-subscriber.mjs
index f9cb513a79bf474f7856392c01c84a8a31d20ac1..48bfe6c5fcac5133acced2549f0ae743ceec0ba6 100644
--- a/esm2022/lib/sse-client-subscriber.mjs
+++ b/esm2022/lib/sse-client-subscriber.mjs
@@ -63,6 +63,7 @@ export class SseClientSubscriber {
         }
     }
     onStreamProgress(data, observer) {
+        if (!data) return;
         data = data.substring(this.progress);
         this.progress += data.length;
         data.split(/(\r\n|\r|\n){2}/g).forEach((part) => this.parseEventData(part, observer));
diff --git a/fesm2022/ngx-sse-client.mjs b/fesm2022/ngx-sse-client.mjs
index daba8326bb58940e8a1f1c4a6c664998160f3d2e..576a76bb6364a5b8912343d641e124d873579b02 100644
--- a/fesm2022/ngx-sse-client.mjs
+++ b/fesm2022/ngx-sse-client.mjs
@@ -71,6 +71,7 @@ class SseClientSubscriber {
         }
     }
     onStreamProgress(data, observer) {
+        if (!data) return;
         data = data.substring(this.progress);
         this.progress += data.length;
         data.split(/(\r\n|\r|\n){2}/g).forEach((part) => this.parseEventData(part, observer));

patrickmichalina avatar Nov 29 '24 16:11 patrickmichalina

I'm facing the same issue. NestJS will send an empty event right away (see https://github.com/nestjs/nest/issues/12670#issue-1966262054). This event is not properly handled in onStreamProgress, since data will be undefined.

@patrickmichalina there is a typo in the issue title: it should be data not date

homj avatar Mar 22 '25 18:03 homj

Thank you @patrickmichalina, your suggestion was included in PR #33, released in version 19.0.2 of ngx-sse-client.

marcospds avatar Jun 01 '25 03:06 marcospds