hono 3.3.2 → 3.3.4

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.
@@ -17,6 +17,8 @@ var serveStatic = (options = { root: "" }) => {
17
17
  root: options.root,
18
18
  defaultDocument: DEFAULT_DOCUMENT
19
19
  });
20
+ if (!path)
21
+ return await next();
20
22
  path = `./${path}`;
21
23
  if (existsSync(path)) {
22
24
  const content = file(path);
@@ -11,6 +11,17 @@ var handle = (subApp, path) => (eventContext) => {
11
11
  }
12
12
  );
13
13
  };
14
+ var serveStatic = () => {
15
+ return async (c) => {
16
+ const env = c.env;
17
+ const res = await env.ASSETS.fetch(c.req.raw);
18
+ if (res.status === 404) {
19
+ return c.notFound();
20
+ }
21
+ return res;
22
+ };
23
+ };
14
24
  export {
15
- handle
25
+ handle,
26
+ serveStatic
16
27
  };
@@ -1,5 +1,6 @@
1
1
  // src/adapter/cloudflare-pages/index.ts
2
- import { handle } from "./handler.js";
2
+ import { handle, serveStatic } from "./handler.js";
3
3
  export {
4
- handle
4
+ handle,
5
+ serveStatic
5
6
  };
@@ -16,6 +16,8 @@ var serveStatic = (options = { root: "" }) => {
16
16
  root: options.root,
17
17
  defaultDocument: DEFAULT_DOCUMENT
18
18
  });
19
+ if (!path)
20
+ return await next();
19
21
  const content = await getContentFromKVAsset(path, {
20
22
  manifest: options.manifest,
21
23
  namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : void 0
@@ -16,6 +16,8 @@ var serveStatic = (options = { root: "" }) => {
16
16
  root: options.root,
17
17
  defaultDocument: DEFAULT_DOCUMENT
18
18
  });
19
+ if (!path)
20
+ return await next();
19
21
  path = `./${path}`;
20
22
  let file;
21
23
  try {
@@ -2,14 +2,24 @@
2
2
  import crypto from "crypto";
3
3
  import { encodeBase64 } from "../../utils/encode.js";
4
4
  globalThis.crypto ?? (globalThis.crypto = crypto);
5
+ var convertHeaders = (headers) => {
6
+ const cfHeaders = {};
7
+ headers.forEach((value, key) => {
8
+ cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
9
+ });
10
+ return cfHeaders;
11
+ };
5
12
  var handle = (app) => {
6
13
  return async (event, context, callback) => {
7
- const req = createRequest(event);
8
- const res = await app.fetch(req, {
14
+ const res = await app.fetch(createRequest(event), {
9
15
  event,
10
16
  context,
11
- callback,
12
- request: event.Records[0].cf.request
17
+ callback: (err, result) => {
18
+ callback?.(err, result);
19
+ },
20
+ config: event.Records[0].cf.config,
21
+ request: event.Records[0].cf.request,
22
+ response: event.Records[0].cf.response
13
23
  });
14
24
  return createResult(res);
15
25
  };
@@ -17,35 +27,27 @@ var handle = (app) => {
17
27
  var createResult = async (res) => {
18
28
  const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
19
29
  const body = isBase64Encoded ? encodeBase64(await res.arrayBuffer()) : await res.text();
20
- const headers = {};
21
- res.headers.forEach((value, key) => {
22
- headers[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
23
- });
24
30
  return {
25
31
  status: res.status.toString(),
26
- headers,
32
+ headers: convertHeaders(res.headers),
27
33
  body
28
34
  };
29
35
  };
30
36
  var createRequest = (event) => {
31
- const queryString = extractQueryString(event);
37
+ const queryString = event.Records[0].cf.request.querystring;
32
38
  const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`;
33
39
  const url = queryString ? `${urlPath}?${queryString}` : urlPath;
34
40
  const headers = new Headers();
35
- for (const [k, v] of Object.entries(event.Records[0].cf.request.headers)) {
41
+ Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
36
42
  v.forEach((header) => headers.set(k, header.value));
37
- }
38
- const method = event.Records[0].cf.request.method;
39
- const requestInit = {
40
- headers,
41
- method
42
- };
43
+ });
43
44
  const requestBody = event.Records[0].cf.request.body;
44
- requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
45
- return new Request(url, requestInit);
46
- };
47
- var extractQueryString = (event) => {
48
- return event.Records[0].cf.request.querystring;
45
+ const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
46
+ return new Request(url, {
47
+ headers,
48
+ method: event.Records[0].cf.request.method,
49
+ body
50
+ });
49
51
  };
50
52
  var isContentTypeBinary = (contentType) => {
51
53
  return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(
@@ -39,6 +39,8 @@ const serveStatic = (options = { root: "" }) => {
39
39
  root: options.root,
40
40
  defaultDocument: DEFAULT_DOCUMENT
41
41
  });
42
+ if (!path)
43
+ return await next();
42
44
  path = `./${path}`;
43
45
  if ((0, import_fs.existsSync)(path)) {
44
46
  const content = file(path);
@@ -18,7 +18,8 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var handler_exports = {};
20
20
  __export(handler_exports, {
21
- handle: () => handle
21
+ handle: () => handle,
22
+ serveStatic: () => serveStatic
22
23
  });
23
24
  module.exports = __toCommonJS(handler_exports);
24
25
  var import_hono = require("../../hono");
@@ -33,7 +34,18 @@ const handle = (subApp, path) => (eventContext) => {
33
34
  }
34
35
  );
35
36
  };
37
+ const serveStatic = () => {
38
+ return async (c) => {
39
+ const env = c.env;
40
+ const res = await env.ASSETS.fetch(c.req.raw);
41
+ if (res.status === 404) {
42
+ return c.notFound();
43
+ }
44
+ return res;
45
+ };
46
+ };
36
47
  // Annotate the CommonJS export names for ESM import in node:
37
48
  0 && (module.exports = {
38
- handle
49
+ handle,
50
+ serveStatic
39
51
  });
@@ -18,11 +18,13 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var cloudflare_pages_exports = {};
20
20
  __export(cloudflare_pages_exports, {
21
- handle: () => import_handler.handle
21
+ handle: () => import_handler.handle,
22
+ serveStatic: () => import_handler.serveStatic
22
23
  });
23
24
  module.exports = __toCommonJS(cloudflare_pages_exports);
24
25
  var import_handler = require("./handler");
25
26
  // Annotate the CommonJS export names for ESM import in node:
26
27
  0 && (module.exports = {
27
- handle
28
+ handle,
29
+ serveStatic
28
30
  });
@@ -38,6 +38,8 @@ const serveStatic = (options = { root: "" }) => {
38
38
  root: options.root,
39
39
  defaultDocument: DEFAULT_DOCUMENT
40
40
  });
41
+ if (!path)
42
+ return await next();
41
43
  const content = await (0, import_cloudflare.getContentFromKVAsset)(path, {
42
44
  manifest: options.manifest,
43
45
  namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : void 0
@@ -38,6 +38,8 @@ const serveStatic = (options = { root: "" }) => {
38
38
  root: options.root,
39
39
  defaultDocument: DEFAULT_DOCUMENT
40
40
  });
41
+ if (!path)
42
+ return await next();
41
43
  path = `./${path}`;
42
44
  let file;
43
45
  try {
@@ -31,14 +31,24 @@ module.exports = __toCommonJS(handler_exports);
31
31
  var import_crypto = __toESM(require("crypto"), 1);
32
32
  var import_encode = require("../../utils/encode");
33
33
  globalThis.crypto ?? (globalThis.crypto = import_crypto.default);
34
+ const convertHeaders = (headers) => {
35
+ const cfHeaders = {};
36
+ headers.forEach((value, key) => {
37
+ cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
38
+ });
39
+ return cfHeaders;
40
+ };
34
41
  const handle = (app) => {
35
42
  return async (event, context, callback) => {
36
- const req = createRequest(event);
37
- const res = await app.fetch(req, {
43
+ const res = await app.fetch(createRequest(event), {
38
44
  event,
39
45
  context,
40
- callback,
41
- request: event.Records[0].cf.request
46
+ callback: (err, result) => {
47
+ callback?.(err, result);
48
+ },
49
+ config: event.Records[0].cf.config,
50
+ request: event.Records[0].cf.request,
51
+ response: event.Records[0].cf.response
42
52
  });
43
53
  return createResult(res);
44
54
  };
@@ -46,35 +56,27 @@ const handle = (app) => {
46
56
  const createResult = async (res) => {
47
57
  const isBase64Encoded = isContentTypeBinary(res.headers.get("content-type") || "");
48
58
  const body = isBase64Encoded ? (0, import_encode.encodeBase64)(await res.arrayBuffer()) : await res.text();
49
- const headers = {};
50
- res.headers.forEach((value, key) => {
51
- headers[key.toLowerCase()] = [{ key: key.toLowerCase(), value }];
52
- });
53
59
  return {
54
60
  status: res.status.toString(),
55
- headers,
61
+ headers: convertHeaders(res.headers),
56
62
  body
57
63
  };
58
64
  };
59
65
  const createRequest = (event) => {
60
- const queryString = extractQueryString(event);
66
+ const queryString = event.Records[0].cf.request.querystring;
61
67
  const urlPath = `https://${event.Records[0].cf.config.distributionDomainName}${event.Records[0].cf.request.uri}`;
62
68
  const url = queryString ? `${urlPath}?${queryString}` : urlPath;
63
69
  const headers = new Headers();
64
- for (const [k, v] of Object.entries(event.Records[0].cf.request.headers)) {
70
+ Object.entries(event.Records[0].cf.request.headers).forEach(([k, v]) => {
65
71
  v.forEach((header) => headers.set(k, header.value));
66
- }
67
- const method = event.Records[0].cf.request.method;
68
- const requestInit = {
69
- headers,
70
- method
71
- };
72
+ });
72
73
  const requestBody = event.Records[0].cf.request.body;
73
- requestInit.body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
74
- return new Request(url, requestInit);
75
- };
76
- const extractQueryString = (event) => {
77
- return event.Records[0].cf.request.querystring;
74
+ const body = requestBody?.encoding === "base64" && requestBody?.data ? Buffer.from(requestBody.data, "base64") : requestBody?.data;
75
+ return new Request(url, {
76
+ headers,
77
+ method: event.Records[0].cf.request.method,
78
+ body
79
+ });
78
80
  };
79
81
  const isContentTypeBinary = (contentType) => {
80
82
  return !/^(text\/(plain|html|css|javascript|csv).*|application\/(.*json|.*xml).*|image\/svg\+xml)$/.test(
@@ -117,7 +117,7 @@ class Hono extends defineDynamicClass() {
117
117
  const strict = init.strict ?? true;
118
118
  delete init.strict;
119
119
  Object.assign(this, init);
120
- this.getPath || (this.getPath = strict ? import_url.getPath : import_url.getPathNoStrict);
120
+ this.getPath = strict ? init.getPath ?? import_url.getPath : import_url.getPathNoStrict;
121
121
  }
122
122
  clone() {
123
123
  const clone = new Hono({
@@ -23,6 +23,8 @@ __export(filepath_exports, {
23
23
  module.exports = __toCommonJS(filepath_exports);
24
24
  const getFilePath = (options) => {
25
25
  let filename = options.filename;
26
+ if (/(?:^|\/)\.\.(?:$|\/)/.test(filename))
27
+ return;
26
28
  let root = options.root || "";
27
29
  const defaultDocument = options.defaultDocument || "index.html";
28
30
  if (filename.endsWith("/")) {
@@ -21,7 +21,7 @@ __export(html_exports, {
21
21
  escapeToBuffer: () => escapeToBuffer
22
22
  });
23
23
  module.exports = __toCommonJS(html_exports);
24
- const escapeRe = /[&<>"]/;
24
+ const escapeRe = /[&<>'"]/;
25
25
  const escapeToBuffer = (str, buffer) => {
26
26
  const match = str.search(escapeRe);
27
27
  if (match === -1) {
@@ -36,6 +36,9 @@ const escapeToBuffer = (str, buffer) => {
36
36
  case 34:
37
37
  escape = "&quot;";
38
38
  break;
39
+ case 39:
40
+ escape = "&#39;";
41
+ break;
39
42
  case 38:
40
43
  escape = "&amp;";
41
44
  break;
package/dist/hono-base.js CHANGED
@@ -95,7 +95,7 @@ var Hono = class extends defineDynamicClass() {
95
95
  const strict = init.strict ?? true;
96
96
  delete init.strict;
97
97
  Object.assign(this, init);
98
- this.getPath || (this.getPath = strict ? getPath : getPathNoStrict);
98
+ this.getPath = strict ? init.getPath ?? getPath : getPathNoStrict;
99
99
  }
100
100
  clone() {
101
101
  const clone = new Hono({
@@ -1,4 +1,5 @@
1
1
  import type { Hono } from '../../hono';
2
+ import type { Env } from '../../types';
2
3
  interface APIGatewayProxyEventV2 {
3
4
  httpMethod: string;
4
5
  headers: Record<string, string | undefined>;
@@ -43,6 +44,6 @@ interface APIGatewayProxyResult {
43
44
  /**
44
45
  * Accepts events from API Gateway/ELB(`APIGatewayProxyEvent`) and directly through Function Url(`APIGatewayProxyEventV2`)
45
46
  */
46
- export declare const handle: (app: Hono) => (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => Promise<APIGatewayProxyResult>;
47
+ export declare const handle: <E extends Env = Env, S = {}, BasePath extends string = "/">(app: Hono<E, S, BasePath>) => (event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | LambdaFunctionUrlEvent) => Promise<APIGatewayProxyResult>;
47
48
  export declare const isContentTypeBinary: (contentType: string) => boolean;
48
49
  export {};
@@ -5,4 +5,4 @@ export declare type ServeStaticOptions = {
5
5
  path?: string;
6
6
  rewriteRequestPath?: (path: string) => string;
7
7
  };
8
- export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<Response | undefined>;
8
+ export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<void | Response>;
@@ -1,5 +1,5 @@
1
1
  import { Hono } from '../../hono';
2
- import type { Env } from '../../types';
2
+ import type { Env, MiddlewareHandler } from '../../types';
3
3
  declare type Params<P extends string = any> = Record<P, string | string[]>;
4
4
  export declare type EventContext<Env = {}, P extends string = any, Data = {}> = {
5
5
  request: Request;
@@ -24,4 +24,11 @@ interface HandleInterface {
24
24
  <E extends Env, S extends {}, BasePath extends string>(app: Hono<E, S, BasePath>, path: string): (eventContext: EventContext) => Response | Promise<Response>;
25
25
  }
26
26
  export declare const handle: HandleInterface;
27
+ /**
28
+ *
29
+ * @description `serveStatic()` is for advanced mode:
30
+ * https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function
31
+ *
32
+ */
33
+ export declare const serveStatic: () => MiddlewareHandler;
27
34
  export {};
@@ -1,2 +1,2 @@
1
- export { handle } from './handler';
1
+ export { handle, serveStatic } from './handler';
2
2
  export type { EventContext } from './handler';
@@ -5,4 +5,4 @@ export declare type ServeStaticOptions = {
5
5
  path?: string;
6
6
  rewriteRequestPath?: (path: string) => string;
7
7
  };
8
- export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<Response | undefined>;
8
+ export declare const serveStatic: (options?: ServeStaticOptions) => (c: Context, next: Next) => Promise<void | Response>;
@@ -32,7 +32,12 @@ export interface CloudFrontRequest {
32
32
  custom: CloudFrontCustomOrigin;
33
33
  };
34
34
  }
35
- interface CloudFrontConfig {
35
+ export interface CloudFrontResponse {
36
+ headers: CloudFrontHeaders;
37
+ status: string;
38
+ statusDescription?: string;
39
+ }
40
+ export interface CloudFrontConfig {
36
41
  distributionDomainName: string;
37
42
  distributionId: string;
38
43
  eventType: string;
@@ -42,6 +47,7 @@ interface CloudFrontEvent {
42
47
  cf: {
43
48
  config: CloudFrontConfig;
44
49
  request: CloudFrontRequest;
50
+ response?: CloudFrontResponse;
45
51
  };
46
52
  }
47
53
  export interface CloudFrontEdgeEvent {
@@ -63,10 +69,6 @@ interface CloudFrontResult {
63
69
  body?: string;
64
70
  bodyEncoding?: 'text' | 'base64';
65
71
  }
66
- /**
67
- * Accepts events from 'Lambda@Edge' event
68
- * https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html
69
- */
70
72
  export declare const handle: (app: Hono<any>) => (event: CloudFrontEdgeEvent, context?: CloudFrontContext, callback?: Callback) => Promise<CloudFrontResult>;
71
73
  export declare const isContentTypeBinary: (contentType: string) => boolean;
72
74
  export {};
@@ -1,2 +1,2 @@
1
1
  export { handle } from './handler';
2
- export type { Callback, CloudFrontRequest, CloudFrontEdgeEvent } from './handler';
2
+ export type { Callback, CloudFrontConfig, CloudFrontRequest, CloudFrontResponse, CloudFrontEdgeEvent } from './handler';
@@ -3,5 +3,5 @@ declare type FilePathOptions = {
3
3
  root?: string;
4
4
  defaultDocument?: string;
5
5
  };
6
- export declare const getFilePath: (options: FilePathOptions) => string;
6
+ export declare const getFilePath: (options: FilePathOptions) => string | undefined;
7
7
  export {};
@@ -1,6 +1,8 @@
1
1
  // src/utils/filepath.ts
2
2
  var getFilePath = (options) => {
3
3
  let filename = options.filename;
4
+ if (/(?:^|\/)\.\.(?:$|\/)/.test(filename))
5
+ return;
4
6
  let root = options.root || "";
5
7
  const defaultDocument = options.defaultDocument || "index.html";
6
8
  if (filename.endsWith("/")) {
@@ -1,5 +1,5 @@
1
1
  // src/utils/html.ts
2
- var escapeRe = /[&<>"]/;
2
+ var escapeRe = /[&<>'"]/;
3
3
  var escapeToBuffer = (str, buffer) => {
4
4
  const match = str.search(escapeRe);
5
5
  if (match === -1) {
@@ -14,6 +14,9 @@ var escapeToBuffer = (str, buffer) => {
14
14
  case 34:
15
15
  escape = "&quot;";
16
16
  break;
17
+ case 39:
18
+ escape = "&#39;";
19
+ break;
17
20
  case 38:
18
21
  escape = "&amp;";
19
22
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono",
3
- "version": "3.3.2",
3
+ "version": "3.3.4",
4
4
  "description": "Ultrafast web framework for the Edges",
5
5
  "main": "dist/cjs/index.js",
6
6
  "type": "module",