hono 4.1.0 → 4.1.1

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.
@@ -43,19 +43,33 @@ var createRequest = (event) => {
43
43
  v.forEach((header) => headers.set(k, header.value));
44
44
  });
45
45
  const requestBody = event.Records[0].cf.request.body;
46
- const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
46
+ const method = event.Records[0].cf.request.method;
47
+ const body = createBody(method, requestBody);
47
48
  return new Request(url, {
48
49
  headers,
49
- method: event.Records[0].cf.request.method,
50
+ method,
50
51
  body
51
52
  });
52
53
  };
54
+ var createBody = (method, requestBody) => {
55
+ if (!requestBody || !requestBody.data) {
56
+ return void 0;
57
+ }
58
+ if (method === "GET" || method === "HEAD") {
59
+ return void 0;
60
+ }
61
+ if (requestBody.encoding === "base64") {
62
+ return Buffer.from(requestBody.data, "base64");
63
+ }
64
+ return requestBody.data;
65
+ };
53
66
  var isContentTypeBinary = (contentType) => {
54
67
  return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml.*)$/.test(
55
68
  contentType
56
69
  );
57
70
  };
58
71
  export {
72
+ createBody,
59
73
  handle,
60
74
  isContentTypeBinary
61
75
  };
@@ -24,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
25
25
  var handler_exports = {};
26
26
  __export(handler_exports, {
27
+ createBody: () => createBody,
27
28
  handle: () => handle,
28
29
  isContentTypeBinary: () => isContentTypeBinary
29
30
  });
@@ -72,13 +73,26 @@ const createRequest = (event) => {
72
73
  v.forEach((header) => headers.set(k, header.value));
73
74
  });
74
75
  const requestBody = event.Records[0].cf.request.body;
75
- const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
76
+ const method = event.Records[0].cf.request.method;
77
+ const body = createBody(method, requestBody);
76
78
  return new Request(url, {
77
79
  headers,
78
- method: event.Records[0].cf.request.method,
80
+ method,
79
81
  body
80
82
  });
81
83
  };
84
+ const createBody = (method, requestBody) => {
85
+ if (!requestBody || !requestBody.data) {
86
+ return void 0;
87
+ }
88
+ if (method === "GET" || method === "HEAD") {
89
+ return void 0;
90
+ }
91
+ if (requestBody.encoding === "base64") {
92
+ return Buffer.from(requestBody.data, "base64");
93
+ }
94
+ return requestBody.data;
95
+ };
82
96
  const isContentTypeBinary = (contentType) => {
83
97
  return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml.*)$/.test(
84
98
  contentType
@@ -86,6 +100,7 @@ const isContentTypeBinary = (contentType) => {
86
100
  };
87
101
  // Annotate the CommonJS export names for ESM import in node:
88
102
  0 && (module.exports = {
103
+ createBody,
89
104
  handle,
90
105
  isContentTypeBinary
91
106
  });
@@ -38,7 +38,7 @@ const replaceUrlParam = (urlString, params) => {
38
38
  return urlString;
39
39
  };
40
40
  const removeIndexString = (urlSting) => {
41
- return urlSting.replace(/\/index$/, "/");
41
+ return urlSting.replace(/\/index$/, "");
42
42
  };
43
43
  function isObject(item) {
44
44
  return typeof item === "object" && item !== null && !Array.isArray(item);
@@ -152,7 +152,13 @@ class Context {
152
152
  });
153
153
  }
154
154
  if (arg && typeof arg !== "number") {
155
- const headers2 = setHeaders(new Headers(arg.headers), this.#preparedHeaders);
155
+ const header = new Headers(arg.headers);
156
+ if (this.#headers) {
157
+ this.#headers.forEach((v, k) => {
158
+ header.set(k, v);
159
+ });
160
+ }
161
+ const headers2 = setHeaders(header, this.#preparedHeaders);
156
162
  return new Response(data, {
157
163
  headers: headers2,
158
164
  status: arg.status ?? this.#status
@@ -40,29 +40,29 @@ class SSEStreamingApi extends import_stream.StreamingApi {
40
40
  await this.write(sseData);
41
41
  }
42
42
  }
43
- const setSSEHeaders = (context) => {
44
- context.header("Transfer-Encoding", "chunked");
45
- context.header("Content-Type", "text/event-stream");
46
- context.header("Cache-Control", "no-cache");
47
- context.header("Connection", "keep-alive");
43
+ const run = async (stream, cb, onError) => {
44
+ try {
45
+ await cb(stream);
46
+ } catch (e) {
47
+ if (e instanceof Error && onError) {
48
+ await onError(e, stream);
49
+ await stream.writeSSE({
50
+ event: "error",
51
+ data: e.message
52
+ });
53
+ } else {
54
+ console.error(e);
55
+ }
56
+ }
48
57
  };
49
58
  const streamSSE = (c, cb, onError) => {
50
59
  const { readable, writable } = new TransformStream();
51
60
  const stream = new SSEStreamingApi(writable, readable);
52
- (async () => {
53
- try {
54
- await cb(stream);
55
- } catch (e) {
56
- if (e instanceof Error && onError) {
57
- await onError(e, stream);
58
- } else {
59
- console.error(e);
60
- }
61
- } finally {
62
- stream.close();
63
- }
64
- })();
65
- setSSEHeaders(c);
61
+ c.header("Transfer-Encoding", "chunked");
62
+ c.header("Content-Type", "text/event-stream");
63
+ c.header("Cache-Control", "no-cache");
64
+ c.header("Connection", "keep-alive");
65
+ run(stream, cb, onError);
66
66
  return c.newResponse(stream.responseReadable);
67
67
  };
68
68
  // Annotate the CommonJS export names for ESM import in node:
@@ -42,12 +42,15 @@ const createRenderer = (c, Layout, component, options) => (children, props) => {
42
42
  currentLayout
43
43
  )}`;
44
44
  if (options?.stream) {
45
- return c.body((0, import_streaming.renderToReadableStream)(body), {
46
- headers: options.stream === true ? {
47
- "Transfer-Encoding": "chunked",
48
- "Content-Type": "text/html; charset=UTF-8"
49
- } : options.stream
50
- });
45
+ if (options.stream === true) {
46
+ c.header("Transfer-Encoding", "chunked");
47
+ c.header("Content-Type", "text/html; charset=UTF-8");
48
+ } else {
49
+ for (const [key, value] of Object.entries(options.stream)) {
50
+ c.header(key, value);
51
+ }
52
+ }
53
+ return c.body((0, import_streaming.renderToReadableStream)(body));
51
54
  } else {
52
55
  return c.html(body);
53
56
  }
@@ -13,7 +13,7 @@ var replaceUrlParam = (urlString, params) => {
13
13
  return urlString;
14
14
  };
15
15
  var removeIndexString = (urlSting) => {
16
- return urlSting.replace(/\/index$/, "/");
16
+ return urlSting.replace(/\/index$/, "");
17
17
  };
18
18
  function isObject(item) {
19
19
  return typeof item === "object" && item !== null && !Array.isArray(item);
package/dist/context.js CHANGED
@@ -129,7 +129,13 @@ var Context = class {
129
129
  });
130
130
  }
131
131
  if (arg && typeof arg !== "number") {
132
- const headers2 = setHeaders(new Headers(arg.headers), this.#preparedHeaders);
132
+ const header = new Headers(arg.headers);
133
+ if (this.#headers) {
134
+ this.#headers.forEach((v, k) => {
135
+ header.set(k, v);
136
+ });
137
+ }
138
+ const headers2 = setHeaders(header, this.#preparedHeaders);
133
139
  return new Response(data, {
134
140
  headers: headers2,
135
141
  status: arg.status ?? this.#status
@@ -17,29 +17,29 @@ var SSEStreamingApi = class extends StreamingApi {
17
17
  await this.write(sseData);
18
18
  }
19
19
  };
20
- var setSSEHeaders = (context) => {
21
- context.header("Transfer-Encoding", "chunked");
22
- context.header("Content-Type", "text/event-stream");
23
- context.header("Cache-Control", "no-cache");
24
- context.header("Connection", "keep-alive");
20
+ var run = async (stream, cb, onError) => {
21
+ try {
22
+ await cb(stream);
23
+ } catch (e) {
24
+ if (e instanceof Error && onError) {
25
+ await onError(e, stream);
26
+ await stream.writeSSE({
27
+ event: "error",
28
+ data: e.message
29
+ });
30
+ } else {
31
+ console.error(e);
32
+ }
33
+ }
25
34
  };
26
35
  var streamSSE = (c, cb, onError) => {
27
36
  const { readable, writable } = new TransformStream();
28
37
  const stream = new SSEStreamingApi(writable, readable);
29
- (async () => {
30
- try {
31
- await cb(stream);
32
- } catch (e) {
33
- if (e instanceof Error && onError) {
34
- await onError(e, stream);
35
- } else {
36
- console.error(e);
37
- }
38
- } finally {
39
- stream.close();
40
- }
41
- })();
42
- setSSEHeaders(c);
38
+ c.header("Transfer-Encoding", "chunked");
39
+ c.header("Content-Type", "text/event-stream");
40
+ c.header("Cache-Control", "no-cache");
41
+ c.header("Connection", "keep-alive");
42
+ run(stream, cb, onError);
43
43
  return c.newResponse(stream.responseReadable);
44
44
  };
45
45
  export {
@@ -18,12 +18,15 @@ var createRenderer = (c, Layout, component, options) => (children, props) => {
18
18
  currentLayout
19
19
  )}`;
20
20
  if (options?.stream) {
21
- return c.body(renderToReadableStream(body), {
22
- headers: options.stream === true ? {
23
- "Transfer-Encoding": "chunked",
24
- "Content-Type": "text/html; charset=UTF-8"
25
- } : options.stream
26
- });
21
+ if (options.stream === true) {
22
+ c.header("Transfer-Encoding", "chunked");
23
+ c.header("Content-Type", "text/html; charset=UTF-8");
24
+ } else {
25
+ for (const [key, value] of Object.entries(options.stream)) {
26
+ c.header(key, value);
27
+ }
28
+ }
29
+ return c.body(renderToReadableStream(body));
27
30
  } else {
28
31
  return c.html(body);
29
32
  }
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import type { Hono } from '../../hono';
2
3
  interface CloudFrontHeader {
3
4
  key: string;
@@ -82,5 +83,6 @@ interface CloudFrontResult {
82
83
  bodyEncoding?: 'text' | 'base64';
83
84
  }
84
85
  export declare const handle: (app: Hono<any>) => (event: CloudFrontEdgeEvent, context?: CloudFrontContext, callback?: Callback) => Promise<CloudFrontResult>;
86
+ export declare const createBody: (method: string, requestBody: CloudFrontRequest['body']) => string | Buffer | undefined;
85
87
  export declare const isContentTypeBinary: (contentType: string) => boolean;
86
88
  export {};
@@ -1,6 +1,6 @@
1
1
  import { Hono } from './hono';
2
2
  export type { Env, ErrorHandler, Handler, MiddlewareHandler, Next, NotFoundHandler, ValidationTargets, Input, Schema, ToSchema, TypedResponse, } from './types';
3
- export type { Context, ContextVariableMap, ContextRenderer } from './context';
3
+ export type { Context, ContextVariableMap, ContextRenderer, ExecutionContext } from './context';
4
4
  export type { HonoRequest } from './request';
5
5
  export type { InferRequestType, InferResponseType, ClientRequestOptions } from './client';
6
6
  export { Hono };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "4.1.0",
3
+ "version": "4.1.1",
4
4
  "description": "Ultrafast web framework for the Edges",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",