WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@BlankParticle
Copy link
Contributor

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Motivation

While writing a rpc method on a Durable Obeject, I wanted to return a SSE stream, so I reached for the streamSSE api from hono/streaming, but it turned out I needed to provide in a context, constructing a Context from request is easy, just the issue is that Context is not exported as a accessible class

I managed to work around it with this immediately invoked hono router setup

import { streamSSE } from "hono/streaming";
import { DurableObject } from "cloudflare:workers";
import { Hono } from "hono";

export class SSEStreamer extends DurableObject {
  async stream() {
    return new Hono()
      .get("*", (c) => {
        return streamSSE(c, async (api) => {
          await api.writeSSE({
            event: "message",
            data: "Begin!",
          });
          for (let i = 0; i < 10; i++) {
            await api.writeSSE({
              event: "message",
              data: `tick ${i}`,
            });
            await new Promise((resolve) => setTimeout(resolve, 500));
          }
          await api.writeSSE({
            event: "message",
            data: "End!",
          });
          await api.close();
        });
      })
      .fetch(new Request("http://internal"));
  }
}

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    const stub = env.SSE.getByName("test");
    const res = await stub.stream();
    return res;
  },
} satisfies ExportedHandler<Env>;

But this would be lot easier with the class, just creating the context as needed

const c = new Context(request, {/* */})

this opens up the bridge to use hono helpers like streaming, proxy, cookie utils with normal web standard requests without having to construct a full hono router

// example of using cookie utils in a fetch method
 fetch(request: Request): Response | Promise<Response> {
   const c = new Context(request);
   setCookie(c, "test", "test-cookie");
   return c.json({ message: "Hello, world!" });
 }

@codecov
Copy link

codecov bot commented Nov 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.52%. Comparing base (342ff3b) to head (3cd84a5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4543   +/-   ##
=======================================
  Coverage   91.52%   91.52%           
=======================================
  Files         172      172           
  Lines       11221    11222    +1     
  Branches     3255     3257    +2     
=======================================
+ Hits        10270    10271    +1     
  Misses        950      950           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant