@@ -5,3 +5,3 @@ // src/adapter/bun/serve-static.ts | ||
| import { serveStatic as baseServeStatic } from "../../middleware/serve-static/index.js"; | ||
| var serveStatic = (options) => { | ||
| var serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -8,0 +8,0 @@ const getContent = async (path) => { |
| // src/adapter/cloudflare-workers/serve-static.ts | ||
| import { serveStatic as baseServeStatic } from "../../middleware/serve-static/index.js"; | ||
| import { getContentFromKVAsset } from "./utils.js"; | ||
| var serveStatic = (options) => { | ||
| var serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -6,0 +6,0 @@ const getContent = async (path) => { |
@@ -5,3 +5,3 @@ // src/adapter/deno/serve-static.ts | ||
| var { open, lstatSync, errors } = Deno; | ||
| var serveStatic = (options) => { | ||
| var serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -8,0 +8,0 @@ const getContent = async (path) => { |
@@ -26,3 +26,3 @@ var __defProp = Object.defineProperty; | ||
| var import_serve_static = require("../../middleware/serve-static"); | ||
| const serveStatic = (options) => { | ||
| const serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -29,0 +29,0 @@ const getContent = async (path) => { |
@@ -25,3 +25,3 @@ var __defProp = Object.defineProperty; | ||
| var import_utils = require("./utils"); | ||
| const serveStatic = (options) => { | ||
| const serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -28,0 +28,0 @@ const getContent = async (path) => { |
@@ -26,3 +26,3 @@ var __defProp = Object.defineProperty; | ||
| const { open, lstatSync, errors } = Deno; | ||
| const serveStatic = (options) => { | ||
| const serveStatic = (options = {}) => { | ||
| return async function serveStatic2(c, next) { | ||
@@ -29,0 +29,0 @@ const getContent = async (path) => { |
@@ -24,15 +24,10 @@ var __defProp = Object.defineProperty; | ||
| const defaultCacheableStatusCodes = [200]; | ||
| const shouldSkipCache = (res) => { | ||
| if (res.headers.has("Vary")) { | ||
| return true; | ||
| const shouldSkipCacheControl = (cacheControl) => !!cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl); | ||
| const parseVaryDirectives = (vary) => { | ||
| if (vary == null) { | ||
| return []; | ||
| } | ||
| const cacheControl = res.headers.get("Cache-Control"); | ||
| if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) { | ||
| return true; | ||
| } | ||
| if (res.headers.has("Set-Cookie")) { | ||
| return true; | ||
| } | ||
| return false; | ||
| return (Array.isArray(vary) ? vary : vary.split(",")).map((directive) => directive.trim().toLowerCase()).filter(Boolean); | ||
| }; | ||
| const shouldSkipCache = (res, optionsVaryDirectives, responseVary) => responseVary.length && (!optionsVaryDirectives || responseVary.some((name) => !optionsVaryDirectives.has(name))) || shouldSkipCacheControl(res.headers.get("Cache-Control")) || res.headers.has("Set-Cookie"); | ||
| const cache = (options) => { | ||
@@ -52,4 +47,5 @@ if (!globalThis.caches) { | ||
| const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase()); | ||
| const varyDirectives = Array.isArray(options.vary) ? options.vary : options.vary?.split(",").map((directive) => directive.trim()); | ||
| if (options.vary?.includes("*")) { | ||
| const optionsVaryList = parseVaryDirectives(options.vary); | ||
| const varyDirectives = optionsVaryList.length ? new Set(optionsVaryList) : void 0; | ||
| if (varyDirectives?.has("*")) { | ||
| throw new Error( | ||
@@ -62,3 +58,3 @@ 'Middleware vary configuration cannot include "*", as it disallows effective caching.' | ||
| ); | ||
| const addHeader = (c) => { | ||
| const addHeader = (c, responseVary) => { | ||
| if (cacheControlDirectives) { | ||
@@ -75,12 +71,14 @@ const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? []; | ||
| if (varyDirectives) { | ||
| const existingDirectives = c.res.headers.get("Vary")?.split(",").map((d) => d.trim()) ?? []; | ||
| const vary = Array.from( | ||
| new Set( | ||
| [...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase()) | ||
| ) | ||
| ).sort(); | ||
| if (vary.includes("*")) { | ||
| c.header("Vary", "*"); | ||
| if (responseVary.length === 0) { | ||
| c.header("Vary", Array.from(varyDirectives).join(", ")); | ||
| } else { | ||
| c.header("Vary", vary.join(", ")); | ||
| const merged = new Set(varyDirectives); | ||
| for (const directive of responseVary) { | ||
| merged.add(directive); | ||
| } | ||
| if (merged.has("*")) { | ||
| c.header("Vary", "*"); | ||
| } else { | ||
| c.header("Vary", Array.from(merged).join(", ")); | ||
| } | ||
| } | ||
@@ -98,2 +96,8 @@ } | ||
| } | ||
| if (varyDirectives) { | ||
| for (const directive of varyDirectives) { | ||
| const value = c.req.raw.headers.get(directive) ?? ""; | ||
| key += `::${directive}=${encodeURIComponent(value)}`; | ||
| } | ||
| } | ||
| const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName; | ||
@@ -109,4 +113,5 @@ const cache3 = await caches.open(cacheName); | ||
| } | ||
| addHeader(c); | ||
| if (shouldSkipCache(c.res)) { | ||
| const responseVary = parseVaryDirectives(c.res.headers.get("Vary")); | ||
| addHeader(c, responseVary); | ||
| if (shouldSkipCache(c.res, varyDirectives, responseVary)) { | ||
| return; | ||
@@ -113,0 +118,0 @@ } |
+15
-0
@@ -173,2 +173,17 @@ var __defProp = Object.defineProperty; | ||
| /** | ||
| * `.bytes()` parses the request body as a `Uint8Array`. | ||
| * | ||
| * @see {@link https://hono.dev/docs/api/request#bytes} | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * app.post('/entry', async (c) => { | ||
| * const body = await c.req.bytes() | ||
| * }) | ||
| * ``` | ||
| */ | ||
| bytes() { | ||
| return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer)); | ||
| } | ||
| /** | ||
| * Parses the request body as a `Blob`. | ||
@@ -175,0 +190,0 @@ * @example |
@@ -75,3 +75,3 @@ var __defProp = Object.defineProperty; | ||
| const pairs = cookie.split(";"); | ||
| const parsedCookie = {}; | ||
| const parsedCookie = /* @__PURE__ */ Object.create(null); | ||
| for (const pairStr of pairs) { | ||
@@ -83,3 +83,3 @@ const valueStartPos = pairStr.indexOf("="); | ||
| const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos)); | ||
| if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName)) { | ||
| if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName) || cookieName in parsedCookie) { | ||
| continue; | ||
@@ -101,3 +101,3 @@ } | ||
| const parseSigned = async (cookie, secret, name) => { | ||
| const parsedCookie = {}; | ||
| const parsedCookie = /* @__PURE__ */ Object.create(null); | ||
| const secretKey = await getCryptoKey(secret); | ||
@@ -104,0 +104,0 @@ for (const [key, value] of Object.entries(parse(cookie, name))) { |
@@ -51,3 +51,5 @@ var __defProp = Object.defineProperty; | ||
| cancel: () => { | ||
| this.abort(); | ||
| if (!this.closed) { | ||
| this.abort(); | ||
| } | ||
| } | ||
@@ -74,2 +76,3 @@ }); | ||
| async close() { | ||
| this.closed = true; | ||
| try { | ||
@@ -79,3 +82,2 @@ await this.writer.close(); | ||
| } | ||
| this.closed = true; | ||
| } | ||
@@ -82,0 +84,0 @@ async pipe(body) { |
| // src/middleware/cache/index.ts | ||
| var defaultCacheableStatusCodes = [200]; | ||
| var shouldSkipCache = (res) => { | ||
| if (res.headers.has("Vary")) { | ||
| return true; | ||
| var shouldSkipCacheControl = (cacheControl) => !!cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl); | ||
| var parseVaryDirectives = (vary) => { | ||
| if (vary == null) { | ||
| return []; | ||
| } | ||
| const cacheControl = res.headers.get("Cache-Control"); | ||
| if (cacheControl && /(?:^|,\s*)(?:private|no-(?:store|cache))(?:\s*(?:=|,|$))/i.test(cacheControl)) { | ||
| return true; | ||
| } | ||
| if (res.headers.has("Set-Cookie")) { | ||
| return true; | ||
| } | ||
| return false; | ||
| return (Array.isArray(vary) ? vary : vary.split(",")).map((directive) => directive.trim().toLowerCase()).filter(Boolean); | ||
| }; | ||
| var shouldSkipCache = (res, optionsVaryDirectives, responseVary) => responseVary.length && (!optionsVaryDirectives || responseVary.some((name) => !optionsVaryDirectives.has(name))) || shouldSkipCacheControl(res.headers.get("Cache-Control")) || res.headers.has("Set-Cookie"); | ||
| var cache = (options) => { | ||
@@ -30,4 +25,5 @@ if (!globalThis.caches) { | ||
| const cacheControlDirectives = options.cacheControl?.split(",").map((directive) => directive.toLowerCase()); | ||
| const varyDirectives = Array.isArray(options.vary) ? options.vary : options.vary?.split(",").map((directive) => directive.trim()); | ||
| if (options.vary?.includes("*")) { | ||
| const optionsVaryList = parseVaryDirectives(options.vary); | ||
| const varyDirectives = optionsVaryList.length ? new Set(optionsVaryList) : void 0; | ||
| if (varyDirectives?.has("*")) { | ||
| throw new Error( | ||
@@ -40,3 +36,3 @@ 'Middleware vary configuration cannot include "*", as it disallows effective caching.' | ||
| ); | ||
| const addHeader = (c) => { | ||
| const addHeader = (c, responseVary) => { | ||
| if (cacheControlDirectives) { | ||
@@ -53,12 +49,14 @@ const existingDirectives = c.res.headers.get("Cache-Control")?.split(",").map((d) => d.trim().split("=", 1)[0]) ?? []; | ||
| if (varyDirectives) { | ||
| const existingDirectives = c.res.headers.get("Vary")?.split(",").map((d) => d.trim()) ?? []; | ||
| const vary = Array.from( | ||
| new Set( | ||
| [...existingDirectives, ...varyDirectives].map((directive) => directive.toLowerCase()) | ||
| ) | ||
| ).sort(); | ||
| if (vary.includes("*")) { | ||
| c.header("Vary", "*"); | ||
| if (responseVary.length === 0) { | ||
| c.header("Vary", Array.from(varyDirectives).join(", ")); | ||
| } else { | ||
| c.header("Vary", vary.join(", ")); | ||
| const merged = new Set(varyDirectives); | ||
| for (const directive of responseVary) { | ||
| merged.add(directive); | ||
| } | ||
| if (merged.has("*")) { | ||
| c.header("Vary", "*"); | ||
| } else { | ||
| c.header("Vary", Array.from(merged).join(", ")); | ||
| } | ||
| } | ||
@@ -76,2 +74,8 @@ } | ||
| } | ||
| if (varyDirectives) { | ||
| for (const directive of varyDirectives) { | ||
| const value = c.req.raw.headers.get(directive) ?? ""; | ||
| key += `::${directive}=${encodeURIComponent(value)}`; | ||
| } | ||
| } | ||
| const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName; | ||
@@ -87,4 +91,5 @@ const cache3 = await caches.open(cacheName); | ||
| } | ||
| addHeader(c); | ||
| if (shouldSkipCache(c.res)) { | ||
| const responseVary = parseVaryDirectives(c.res.headers.get("Vary")); | ||
| addHeader(c, responseVary); | ||
| if (shouldSkipCache(c.res, varyDirectives, responseVary)) { | ||
| return; | ||
@@ -91,0 +96,0 @@ } |
+15
-0
@@ -151,2 +151,17 @@ // src/request.ts | ||
| /** | ||
| * `.bytes()` parses the request body as a `Uint8Array`. | ||
| * | ||
| * @see {@link https://hono.dev/docs/api/request#bytes} | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * app.post('/entry', async (c) => { | ||
| * const body = await c.req.bytes() | ||
| * }) | ||
| * ``` | ||
| */ | ||
| bytes() { | ||
| return this.#cachedBody("arrayBuffer").then((buffer) => new Uint8Array(buffer)); | ||
| } | ||
| /** | ||
| * Parses the request body as a `Blob`. | ||
@@ -153,0 +168,0 @@ * @example |
| import type { ServeStaticOptions } from '../../middleware/serve-static'; | ||
| import type { Env, MiddlewareHandler } from '../../types'; | ||
| export declare const serveStatic: <E extends Env = Env>(options: ServeStaticOptions<E>) => MiddlewareHandler; | ||
| export declare const serveStatic: <E extends Env = Env>(options?: ServeStaticOptions<E>) => MiddlewareHandler; |
@@ -5,3 +5,3 @@ import type { ServeStaticOptions as BaseServeStaticOptions } from '../../middleware/serve-static'; | ||
| namespace?: unknown; | ||
| manifest: object | string; | ||
| manifest?: object | string; | ||
| }; | ||
@@ -17,2 +17,2 @@ /** | ||
| */ | ||
| export declare const serveStatic: <E extends Env = Env>(options: ServeStaticOptions<E>) => MiddlewareHandler; | ||
| export declare const serveStatic: <E extends Env = Env>(options?: ServeStaticOptions<E>) => MiddlewareHandler; |
| import type { ServeStaticOptions } from '../../middleware/serve-static'; | ||
| import type { Env, MiddlewareHandler } from '../../types'; | ||
| export declare const serveStatic: <E extends Env = Env>(options: ServeStaticOptions<E>) => MiddlewareHandler; | ||
| export declare const serveStatic: <E extends Env = Env>(options?: ServeStaticOptions<E>) => MiddlewareHandler; |
@@ -6,3 +6,3 @@ /** | ||
| import type { Context } from '../../context'; | ||
| import type { MiddlewareHandler } from '../../types'; | ||
| import type { Env, MiddlewareHandler } from '../../types'; | ||
| type MessageFunction = (c: Context) => string | object | Promise<string | object>; | ||
@@ -13,3 +13,3 @@ type CustomizedErrorResponseOptions = { | ||
| }; | ||
| type BearerAuthOptions = { | ||
| type BearerAuthOptions<E extends Env = Env> = { | ||
| token: string | string[]; | ||
@@ -39,3 +39,3 @@ realm?: string; | ||
| headerName?: string; | ||
| verifyToken: (token: string, c: Context) => boolean | Promise<boolean>; | ||
| verifyToken: (token: string, c: Context<E>) => boolean | Promise<boolean>; | ||
| hashFunction?: Function; | ||
@@ -63,2 +63,3 @@ /** | ||
| * | ||
| * @template E - The environment type. | ||
| * @param {BearerAuthOptions} options - The options for the bearer authentication middleware. | ||
@@ -77,3 +78,3 @@ * @param {string | string[]} [options.token] - The string or array of strings to validate the incoming bearer token against. | ||
| * @param {string | object | MessageFunction} [options.invalidToken.wwwAuthenticateHeader="Bearer error=\"invalid_token\""] - The response header value for the WWW-Authenticate header when token is invalid. | ||
| * @returns {MiddlewareHandler} The middleware handler function. | ||
| * @returns {MiddlewareHandler<E>} The middleware handler function. | ||
| * @throws {Error} If neither "token" nor "verifyToken" options are provided. | ||
@@ -95,3 +96,3 @@ * @throws {HTTPException} If authentication fails, with 401 status code for missing or invalid token, or 400 status code for invalid request. | ||
| */ | ||
| export declare const bearerAuth: (options: BearerAuthOptions) => MiddlewareHandler; | ||
| export declare const bearerAuth: <E extends Env = Env>(options: BearerAuthOptions<E>) => MiddlewareHandler<E>; | ||
| export {}; |
@@ -17,3 +17,3 @@ /** | ||
| * @param {string} [options.cacheControl] - A string of directives for the `Cache-Control` header. | ||
| * @param {string | string[]} [options.vary] - Sets the `Vary` header in the response. If the original response header already contains a `Vary` header, the values are merged, removing any duplicates. | ||
| * @param {string | string[]} [options.vary] - Adds the configured request headers to the cache key variants and sets the `Vary` header in the response. If the original response header already contains a `Vary` header, the values are merged, removing any duplicates. | ||
| * @param {Function} [options.keyGenerator] - Generates keys for every request in the `cacheName` store. This can be used to cache data based on request parameters or context parameters. | ||
@@ -20,0 +20,0 @@ * @param {number[]} [options.cacheableStatusCodes=[200]] - An array of status codes that can be cached. |
@@ -168,2 +168,15 @@ import { GET_MATCH_RESULT } from './request/constants'; | ||
| /** | ||
| * `.bytes()` parses the request body as a `Uint8Array`. | ||
| * | ||
| * @see {@link https://hono.dev/docs/api/request#bytes} | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * app.post('/entry', async (c) => { | ||
| * const body = await c.req.bytes() | ||
| * }) | ||
| * ``` | ||
| */ | ||
| bytes(): Promise<Uint8Array>; | ||
| /** | ||
| * Parses the request body as a `Blob`. | ||
@@ -170,0 +183,0 @@ * @example |
@@ -51,3 +51,3 @@ // src/utils/cookie.ts | ||
| const pairs = cookie.split(";"); | ||
| const parsedCookie = {}; | ||
| const parsedCookie = /* @__PURE__ */ Object.create(null); | ||
| for (const pairStr of pairs) { | ||
@@ -59,3 +59,3 @@ const valueStartPos = pairStr.indexOf("="); | ||
| const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos)); | ||
| if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName)) { | ||
| if (name && name !== cookieName || !validCookieNameRegEx.test(cookieName) || cookieName in parsedCookie) { | ||
| continue; | ||
@@ -77,3 +77,3 @@ } | ||
| var parseSigned = async (cookie, secret, name) => { | ||
| const parsedCookie = {}; | ||
| const parsedCookie = /* @__PURE__ */ Object.create(null); | ||
| const secretKey = await getCryptoKey(secret); | ||
@@ -80,0 +80,0 @@ for (const [key, value] of Object.entries(parse(cookie, name))) { |
@@ -30,3 +30,5 @@ // src/utils/stream.ts | ||
| cancel: () => { | ||
| this.abort(); | ||
| if (!this.closed) { | ||
| this.abort(); | ||
| } | ||
| } | ||
@@ -53,2 +55,3 @@ }); | ||
| async close() { | ||
| this.closed = true; | ||
| try { | ||
@@ -58,3 +61,2 @@ await this.writer.close(); | ||
| } | ||
| this.closed = true; | ||
| } | ||
@@ -61,0 +63,0 @@ async pipe(body) { |
+2
-2
| { | ||
| "name": "hono", | ||
| "version": "4.12.18", | ||
| "version": "4.12.19", | ||
| "description": "Web framework built on Web Standards", | ||
@@ -660,3 +660,3 @@ "main": "dist/cjs/index.js", | ||
| "@hono/eslint-config": "^2.1.0", | ||
| "@hono/node-server": "^1.13.5", | ||
| "@hono/node-server": "^2.0.2", | ||
| "@types/glob": "^9.0.0", | ||
@@ -663,0 +663,0 @@ "@types/jsdom": "^21.1.7", |
Sorry, the diff of this file is not supported yet
1400290
0.2%36739
0.16%