title: Interrupted Server Side Events stream
body: <p>I'm connecting to an SSE stream using the below code:</p>
  <pre class="lang-js prettyprint-override"><code>const getNonTradingEventsUsingHttp = async (onData: (data: string) =&gt; void): Promise&lt;any | void&gt; =&gt; {
      return new Promise((resolve, reject) =&gt; {
        https.get(new URL(`https://some.com/events`), { headers: { Authorization: this.authHeader(apiKey, apiSecret) } }, res =&gt; {
          res.on('data', data =&gt; processSSEDataframe(data.toString()).forEach(onData))
          res.on('end', resolve)
          res.on('error', (err: any) =&gt; reject(err))
        })
      })
    }
  
  processSSEDataframe = (dataframe: string): string[] =&gt; {
    /**
     * Takes in a raw dataframe from a Server Sent Events stream,
     * parses the message and returns an array of string objects
     * representing JSON data that can be used upstream.
     *
     * We return an array because the dataframe can include no
     * data objects (e.g. the welcome message), or it cane include
     * multiple objects (separated by \n\n)
     */
    const messages = dataframe.split('\n\n')
    const data: string[] = []
  
    messages.forEach(msg =&gt; {
      if (msg.startsWith(': welcome')) {
        console.log('Connected to events stream')
        return // skip
      }
  
      const dataFramePrefix = 'data:'
      if (msg.startsWith(dataFramePrefix)) {
        const contents = msg.slice(dataFramePrefix.length).trim()
        data.push(contents)
        return // done
      }
    })
  
    return data
  }
  </code></pre>
  <p>Now, using it is a piece of cake. I just call it like so:</p>
  <pre class="lang-js prettyprint-override"><code>const onData = (data: string) =&gt; {
    console.log(JSON.parse(data))
  }
  
  await getNonTradingEventsUsingHttp(onData);
  </code></pre>
  <h2>The problem</h2>
  <p>When I run the script, I get a JSON parser error:</p>
  <pre><code>SyntaxError: Unexpected end of JSON input
      at JSON.parse (&lt;anonymous&gt;)
  </code></pre>
  <p>When I inspect the <code>data</code> string, I find that it's been truncated:</p>
  <pre class="lang-json prettyprint-override"><code>{...,&quot;net_amount&quot;:31467.62,&quot;per_share_amount&quot;:n // &lt;-- the rest is missing!
  </code></pre>
  <p>I've tried running this command in CURL, and I don't have this issue.</p>
  <p>I also added debug breakpoints at <code>res.on('end')</code> and <code>res.on('error')</code> to see if they're causing the stream to end abruptly, but neither are triggered.</p>
  <p>Can you help me figure out why the output gets truncated in my Node program?</p>