Code Monkey home page Code Monkey logo

stream's People

Contributors

saltyaom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

stream's Issues

[Bug] Formatting error when sending multiple lines of data

According to MDN, when sending multiple lines of data via SSE, each line should be preceded by data: , and the current implementation is clearly doing it wrong.

If you try to send multiple lines of data like:

app.get('/live', () => {
    return new Stream(async (stream) => {
        stream.send("first line\nsecond line");
        stream.close();
    });
})

You will get:

id: <some id>
data: first line
second line  // Wrong here

Streaming chunks accumulating and/or being lost.

I am having this issue, since I've migrated from Node to Bun, writing streams by hand or using this plugin.

The problem consists of streams accumulating chunks and displaying all at once OR chunks simply being lost.

Here is an example:
PS: If I simply return the Stream object like it's said in the docs, Elysia returns a "jsonfied" version of new Stream(...), so... I had to tweak it a little.

const stream = new Stream(async (st) => {
  let count = 1;
  const interval = setInterval(() => {
    st.send({ count });
    count++;
    if(count > 50) {
      st.close();
      clearInterval(interval);
    }
  }, 10);
});

return new Response(stream.value, {
  headers: {
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'text/event-stream',
    'Transfer-Encoding': 'chunked',
  },
  status: 200,
});

The code above works with 250ms timeouts or higher, but won't with faster frequencies.

Since the project I am working leverages gen AI, this timeout won't deliver an acceptable UX.

Assuming the snippet is within a handler, what could be going wrong?

[bug] Using this with the CORS plugin causes streaming to break

When using elysia's streaming plugin with the CORS plugin, the streaming breaks. Below you can find a minimal reproducable example.

import { Elysia } from "elysia";
import cors from "@elysiajs/cors";
import Stream from "@elysiajs/stream";
import OpenAI from "openai";

const openai = new OpenAI({
  apiKey: Bun.env.OPENAI_API_KEY,
});

const app = new Elysia()
  .use(cors())
  .get(
    '/ai',
    ({ query: { prompt } }) =>
      new Stream(
        openai.chat.completions.create({
          model: 'gpt-3.5-turbo',
          stream: true,
          messages: [{
            role: 'user',
            content: prompt
          }]
        })
      )
  )
  .listen(PORT, () => {
    console.log(`šŸ¦Š Elysia is running at on port ${PORT}`)
  });

Doing this results in a content-type header of application/json along with a body of {}

[build] Fail due to imported Stream.

This is running in a bun workspace

../../node_modules/@elysiajs/stream/src/index.ts(56,25): error TS2552: Cannot find name 'ReadableStreamController'. Did you mean 'ReadableStreamDirectController'?

bun version 1.1.13
@elyisa/stream 1.0.2
elysia 1.0.23

[Bug] request.signal.onabort never getting fired

Unfortunately, adding an onabort-handler for my SSE-endpoints has only been possible with a workaround which you can see in this minimum reproducible example.

Creating an interval which prevents the request from getting garbage collected makes the onabort work properly. Stream.close() also doesn't call onabort (which makes sense but isn't really practical if you just want to handle connection loss, no matter if the client aborted it or the server), it would be nice if the Stream-API could get extended to allow developers to handle disconnects in an intuitive way.

import { Elysia } from "elysia";
import { Stream } from "@elysiajs/stream";

// [1] => Uncommenting this makes onabort work properly (page close/client-side connection loss calls onabort)
// [2] => stream.close() doesn't trigger onabort as well
// Running the code like this leads to "connection closed" never getting logged even if it should happen
new Elysia()
  .get("/", ({ request }) => {
    request.signal.onabort = () => {
      console.log("connection closed");
      // [1]
      // clearInterval(_);
    };
    // [1]
    // const _ = setInterval(() => request.signal.aborted, Math.pow(2, 31) - 1);
    return new Stream(async (stream) => {
      stream.send("test");
      // [2]
      // stream.close();
    });
  })
  .listen(3000);

handle closed connection

When the client closes the connection, the server maybe has to do something like clearing an interval.

app.get('/live', () => {
	return new Stream(async (stream) => {
		setInterval(() => {
			stream.send('hello')
		}, 1000)
	})
})

How am i supposed to achive that with the current version? IĀ“d expect to be able to return a callback that will be called when the connection is closed to handle such stuff.

[QUESTION] Example using CORS plugin

Hi, thanks for the great Stream plugin! Can you please add an example of how to use this together with the @elysiajs/cors plugin? It seems that when adding the CORS plugin, the SSE response is converted to json?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.