How to feed a ReadableStream / async Generator into a Response object?

`` Bun.serve({ async fetch(req) { return new Response( async function* stream() { // ~~~~~~~^? yield "Hello, "; yield Buffer.from("world!"); }, ); }, }); it is possible to feed an async generator directly into the fetch Response with Bun. With deno this does not work. I also tried to turn it into a ReadableStream with ReadableStream.from(), but no success so far. Is this the correct way to produce a ReadableStream from an async generator, or do I have to do this differently? thanks!
No description
S
spirobel57d ago
another try using the node:stream Readable. works in bun does not work in deno
S
spirobel57d ago
No description
Z
zamfofex57d ago
If you want to be able to mix and match strings and buffers, you need a preprocessor function like this:
// mix strings and buffers

Deno.serve(request => new Response(ReadableStream.from(preprocess(request))))

let encoder = new TextEncoder()

async function * preprocess(request)
{
for await (let chunk of makeStream(request))
{
if (typeof chunk === "string") chunk = encoder.encode(chunk)
yield chunk
}
}

async function * makeStream(request)
{
yield "Hello, "
yield encoder.encode("world!")
}
// mix strings and buffers

Deno.serve(request => new Response(ReadableStream.from(preprocess(request))))

let encoder = new TextEncoder()

async function * preprocess(request)
{
for await (let chunk of makeStream(request))
{
if (typeof chunk === "string") chunk = encoder.encode(chunk)
yield chunk
}
}

async function * makeStream(request)
{
yield "Hello, "
yield encoder.encode("world!")
}
Otherwise, you can get away with only buffers or only strings more easily:
// only buffers
Deno.serve(request => new Response(ReadableStream.from(makeStream(request))))

let encoder = new TextEncoder()

async function * makeStream(request)
{
yield encoder.encode("Hello, ")
yield encoder.encode("world!")
}
// only buffers
Deno.serve(request => new Response(ReadableStream.from(makeStream(request))))

let encoder = new TextEncoder()

async function * makeStream(request)
{
yield encoder.encode("Hello, ")
yield encoder.encode("world!")
}
// only strings
Deno.serve(request => new Response(ReadableStream.from(makeStream(request)).pipeThrough(new TextEncoderStream())))

async function * makeStream(request)
{
yield "Hello, "
yield "world!"
}
// only strings
Deno.serve(request => new Response(ReadableStream.from(makeStream(request)).pipeThrough(new TextEncoderStream())))

async function * makeStream(request)
{
yield "Hello, "
yield "world!"
}
S
spirobel54d ago
thanks a lot for your reply! I didn't know yielding buffers or strings mattered. this is great info! is there something special I need to do to flush the buffers after every yield? I tried this:
javascript
Deno.serve(
(request) =>
new Response(
ReadableStream.from(makeStream(request)).pipeThrough(
new TextEncoderStream()
)
)
);
const wait = (n: number) => new Promise((resolve) => setTimeout(resolve, n));

async function* makeStream(request) {
yield "Hello,";
console.log("start wait ");
await wait(5000);
yield "world!";
}
javascript
Deno.serve(
(request) =>
new Response(
ReadableStream.from(makeStream(request)).pipeThrough(
new TextEncoderStream()
)
)
);
const wait = (n: number) => new Promise((resolve) => setTimeout(resolve, n));

async function* makeStream(request) {
yield "Hello,";
console.log("start wait ");
await wait(5000);
yield "world!";
}
it waited 5 seconds and then sent the whole Hello world. I thought it would send Hello, then wait for 5 seconds and then send the world! 😀 (while sending the Hello instantly) EDIT: with { headers: { "content-type": "text/html; charset=utf-8" } } it works
More Posts
deno_core manual tick/poll(Learning the ropes of deno_core) Two related questions: - If I create a JsRuntime, and call `polIs there any way to know currently running has been deno compiled?Can something like Deno.compiled be used to determine this?Testing API serverI'm trying to setup an API server using deno and am trying to figure out what the canonical way of tDebugging high (string) heap usageI was observing relatively high memory usage in my deno applications (web-applications). I haven't aVS Code integration is brokenHi all. Just opened my Deno project in VS Code and got: `Cannot set workspace settings: invalid typfollow up action after respond to the requestCurrently, i have this for my http interaction discord bot ```ts // ping.js execute(api: API, interaHow to apply @deno-types on a dynamic import```ts // @deno-types="npm:@types/react-dom@18/client" const { hydrateRoot: hydrate } = (await impCache fresh deps on docker problemI have a brand new fresh project with only the default deps and when i try to put my app in a dockerDeno fmt with verbatimModuleSyntax?Currently Deno fmt sorts *verbatim imports* alphabetically, this is conflicting with the *Organize IHow to use private remote modules hosted on GitHub on Deno Deploy?According to [Private Modules and Repositories](https://docs.deno.com/runtime/manual/basics/modules/Encryption using Deno KV and OAuth?I'm creating a note-taking app using most of Deno's tools. I was thinking about encrypting user noteDeno is not seeing a projectIt seems deno is not seeing a project i am using. Project link: https://github.com/Swifter1243/ReMa`arch` and `platform` are empty strings in `node:process`👋 hello! I'm trying to use a node project that uses `arch` and `platform` inside `node:process`, whNPM imports fail on JSON requireWhen importing modules via `npm:` protocol, the embedded esbuild will throw the following error if tuse-case: web component development in denoMy use-case is quite simple, really. But I have a hard time finding the tooling to work with it. - Starting project errorsI have error in url of DenoConditional callback with deno_kv_oauthHello I am using deno_kv_oauth for my application and am trying to do the following: I want users tClear deno kv dbHow can I clear out the database? using `list` requires the keys to be known, but I just want to cle