@basaltkit/hono 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +25 -1
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -4,7 +4,11 @@ import {
4
4
  HttpServerCollector,
5
5
  HTTP_SERVER,
6
6
  runRoute,
7
- toErrorResponse
7
+ toErrorResponse,
8
+ isSseResponse,
9
+ sseProducerOf,
10
+ driveSse,
11
+ SSE_HEADERS
8
12
  } from "@basaltkit/http";
9
13
  import { Hono } from "hono";
10
14
  var HONO = createToken("hono");
@@ -34,6 +38,25 @@ async function toNeutralRequest(context) {
34
38
  raw: context
35
39
  };
36
40
  }
41
+ function sseResponse(context, producer) {
42
+ const encoder = new TextEncoder();
43
+ const stream = new ReadableStream({
44
+ start(controller) {
45
+ void driveSse(producer, {
46
+ write: (frame) => controller.enqueue(encoder.encode(frame)),
47
+ end: () => {
48
+ try {
49
+ controller.close();
50
+ } catch {
51
+ }
52
+ },
53
+ onClose: (listener) => context.req.raw.signal.addEventListener("abort", listener)
54
+ });
55
+ }
56
+ });
57
+ const { connection: _connection, ...headers } = SSE_HEADERS;
58
+ return new Response(stream, { headers });
59
+ }
37
60
  var HonoReply = class {
38
61
  constructor(context) {
39
62
  this.context = context;
@@ -91,6 +114,7 @@ function handlerFor(definition, container, enrichers, guards) {
91
114
  enrichers,
92
115
  guards
93
116
  });
117
+ if (isSseResponse(result)) return sseResponse(context, sseProducerOf(result));
94
118
  return toResponse(reply, reply.sent ? reply.payload : result);
95
119
  } catch (error) {
96
120
  const { status, body } = toErrorResponse(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basaltkit/hono",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Hono adapter for Basalt: run the same typed routes, enrichers and guards on Hono (Node, Bun, Deno, edge) as on Fastify or Express.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,8 +14,8 @@
14
14
  "dist"
15
15
  ],
16
16
  "dependencies": {
17
- "@basaltkit/core": "^1.0.0",
18
- "@basaltkit/http": "^1.3.0"
17
+ "@basaltkit/core": "^1.1.0",
18
+ "@basaltkit/http": "^1.7.0"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "hono": "^4.0.0"