ex0ns
ex0ns
Explore posts from servers
HHono
Created by ex0ns on 3/5/2025 in #help
Closing resources and streaming response
Thanks for clarifying, though I don't think that it would work in my case, as this is pretty specific to the simpler reproducer I created to illustrate the problem I had, in my case the real scenario is a pool of connection to postgres (using postgres.js) that are closed before I could stream all the data to the frontend, and I could indeed call postgres.end manually at the end of all the method that use a postgres connection, but I don't really like this. For now I think that I will go with the custom header to bypass the automatic cleanup + adding a cleanup function in c.var that some route would have to call whenever they are done to cleanup the resources (or maybe a custom ctx.set("stream", to replace the default .stream method from Hono and take care of closing the resources as well Thanks for all the ideas
34 replies
HHono
Created by ex0ns on 3/5/2025 in #help
Closing resources and streaming response
Thanks for the reply, I just don't really see what you mean when you say free the resources on end ? How would you implement this part ?
34 replies
HHono
Created by ex0ns on 3/5/2025 in #help
Closing resources and streaming response
This is the example for a single resource, but there might be more (in our case there is), I agree that I could somehow store this "cleanup" function in the context and make sure that all endpoint returning a stream call this cleanup function (either at the end of the streaming or in the onError callback), and continue closing it automatically for all other endpoints, but this is very error prone.
34 replies
HHono
Created by ex0ns on 3/5/2025 in #help
Closing resources and streaming response
An idea I had was to detect the headers on c.res.headers to check if [ "transfer-encoding", "chunked" ] was set or not and skip the resource cleanup in those case, but I really don't like this solution and this will in the long run still leak resources. (also the stream helper does not seems to set the transfer-encoding header, but the streamText does) Do you know how I could solve this issue ?
34 replies
HHono
Created by ex0ns on 3/5/2025 in #help
Closing resources and streaming response
import { Hono } from "hono";
import { createMiddleware } from "hono/factory";
import { streamText } from "hono/streaming";

const middleware = createMiddleware<{
Variables: {
resourceToClose: { use: () => AsyncGenerator<string, void, unknown> };
};
}>(async (c, next) => {
let isClosed = false;
const resourceToClose = {
use: async function* () {
for (let x of ["first", "second", "last"]) {
if (!isClosed) yield x;
else yield "I have been closed";
}
},
close: () => {
isClosed = true;
console.log("closing");
},
};
c.set("resourceToClose", resourceToClose);
await next();
resourceToClose.close();
});

const server = new Hono().get("/test", middleware, async (c) => {
c.var.resourceToClose.use();
return streamText(c, async (stream) => {
for await (const val of c.var.resourceToClose.use()) {
await stream.writeln(val);
await stream.sleep(200);
}
});
});

export default server;
import { Hono } from "hono";
import { createMiddleware } from "hono/factory";
import { streamText } from "hono/streaming";

const middleware = createMiddleware<{
Variables: {
resourceToClose: { use: () => AsyncGenerator<string, void, unknown> };
};
}>(async (c, next) => {
let isClosed = false;
const resourceToClose = {
use: async function* () {
for (let x of ["first", "second", "last"]) {
if (!isClosed) yield x;
else yield "I have been closed";
}
},
close: () => {
isClosed = true;
console.log("closing");
},
};
c.set("resourceToClose", resourceToClose);
await next();
resourceToClose.close();
});

const server = new Hono().get("/test", middleware, async (c) => {
c.var.resourceToClose.use();
return streamText(c, async (stream) => {
for await (const val of c.var.resourceToClose.use()) {
await stream.writeln(val);
await stream.sleep(200);
}
});
});

export default server;
34 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
Honestly, I just node.js because I'm more familiar with it and was easier for me to work with Node.js, however due to some monorepo, bundling and typescript setup running our server locally on our laptop became a pain in the a** (tsx, ts-run, you name it always failed at some point), I decided to try bun to run TS natively and it worked amazingly so far.
16 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
Yes it is. But I only use bun to dev, in prod we use node.js so this value is only use locally, but I agree that you should probably set it to another value if you use bun in production
16 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
export default {
port: process.env.PORT ?? 8787,
fetch: app.fetch,
idleTimeout: 0,
};
export default {
port: process.env.PORT ?? 8787,
fetch: app.fetch,
idleTimeout: 0,
};
16 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
I increased the value of idleTimeout
16 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
I had this issue since version 1.1.26 of bun.... this is a nasty setting to change in a patch
16 replies
HHono
Created by Chris on 8/30/2024 in #help
Hono SSE Stream Closing Unexpectedly After 10 Seconds
16 replies
WWindmill
Created by ex0ns on 5/23/2024 in #help
typescript client in cloudflare workers
By working I mean not crashing anymore. But I can't test other runtime right now
10 replies
WWindmill
Created by ex0ns on 5/23/2024 in #help
typescript client in cloudflare workers
I have tested locally (using wrangler, so it should be as close as possible to the real runtime), and globalThis?.process is indeed working
10 replies
HHono
Created by Marcel Overdijk on 4/15/2024 in #help
Validate response objects
You can do this directly in the body of the handler can't you ? Before returning c.json(content) you could call returnSchema.parse(content) (where returnSchema is a zod schema)
16 replies
HHono
Created by Marcel Overdijk on 4/9/2024 in #help
How to generate openapi.yaml spec with Zod OpenAPI Hono?
You can call
app.getOpenAPIDocument( {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
}))
app.getOpenAPIDocument( {
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'My API',
},
}))
On your hono to retrieve the document, from there you can probably write it to your filesystem or use it in your build system
4 replies
HHono
Created by Firu on 4/2/2024 in #help
Does Hono Cloudflare Pages work with `@supabase/supabase-js`?
Thanks !
19 replies
HHono
Created by Firu on 4/2/2024 in #help
Does Hono Cloudflare Pages work with `@supabase/supabase-js`?
so you bypassed wrangler as well ? going directly with esbuild to bundle the worker ?
19 replies
HHono
Created by Firu on 4/2/2024 in #help
Does Hono Cloudflare Pages work with `@supabase/supabase-js`?
Yup, see the comments in the issue I've linked, there is a workaround described, I hope this will get fixed soon, this sucks
19 replies
HHono
Created by Firu on 4/2/2024 in #help
Does Hono Cloudflare Pages work with `@supabase/supabase-js`?
Side note (about workers): the last release of supabase-js/supabase-auth seems to have some issues when running inside the workers: https://github.com/supabase/supabase-js/issues/1001#issuecomment-2029478541 Using a previous version "just works". This is not directly related to your question about Cloudflare Pages (I can't answer this part), but beware if you try to use cloudflare workers at the moment
19 replies