@hono/node-server
Advanced tools
Comparing version 1.12.2 to 1.13.0
@@ -1,4 +0,4 @@ | ||
import { Context, MiddlewareHandler } from 'hono'; | ||
import { Env, Context, MiddlewareHandler } from 'hono'; | ||
type ServeStaticOptions = { | ||
type ServeStaticOptions<E extends Env = Env> = { | ||
/** | ||
@@ -10,4 +10,6 @@ * Root path, relative to current working directory from which the app was started. Absolute paths are not supported. | ||
index?: string; | ||
precompressed?: boolean; | ||
rewriteRequestPath?: (path: string) => string; | ||
onNotFound?: (path: string, c: Context) => void | Promise<void>; | ||
onFound?: (path: string, c: Context<E>) => void | Promise<void>; | ||
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>; | ||
}; | ||
@@ -14,0 +16,0 @@ declare const serveStatic: (options?: ServeStaticOptions) => MiddlewareHandler; |
@@ -26,5 +26,12 @@ "use strict"; | ||
module.exports = __toCommonJS(serve_static_exports); | ||
var import_fs = require("fs"); | ||
var import_filepath = require("hono/utils/filepath"); | ||
var import_mime = require("hono/utils/mime"); | ||
var import_fs = require("fs"); | ||
var COMPRESSIBLE_CONTENT_TYPE_REGEX = /^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i; | ||
var ENCODINGS = { | ||
br: ".br", | ||
zstd: ".zst", | ||
gzip: ".gz" | ||
}; | ||
var ENCODINGS_ORDERED_KEYS = Object.keys(ENCODINGS); | ||
var createStreamBody = (stream) => { | ||
@@ -90,2 +97,3 @@ const body = new ReadableStream({ | ||
} | ||
await options.onFound?.(path, c); | ||
const mimeType = (0, import_mime.getMimeType)(path); | ||
@@ -95,2 +103,20 @@ if (mimeType) { | ||
} | ||
if (options.precompressed && (!mimeType || COMPRESSIBLE_CONTENT_TYPE_REGEX.test(mimeType))) { | ||
const acceptEncodingSet = new Set( | ||
c.req.header("Accept-Encoding")?.split(",").map((encoding) => encoding.trim()) | ||
); | ||
for (const encoding of ENCODINGS_ORDERED_KEYS) { | ||
if (!acceptEncodingSet.has(encoding)) { | ||
continue; | ||
} | ||
const precompressedStats = getStats(path + ENCODINGS[encoding]); | ||
if (precompressedStats) { | ||
c.header("Content-Encoding", encoding); | ||
c.header("Vary", "Accept-Encoding", { append: true }); | ||
stats = precompressedStats; | ||
path = path + ENCODINGS[encoding]; | ||
break; | ||
} | ||
} | ||
} | ||
const size = stats.size; | ||
@@ -97,0 +123,0 @@ if (c.req.method == "HEAD" || c.req.method == "OPTIONS") { |
{ | ||
"name": "@hono/node-server", | ||
"version": "1.12.2", | ||
"version": "1.13.0", | ||
"description": "Node.js Adapter for Hono", | ||
@@ -63,4 +63,4 @@ "main": "dist/index.js", | ||
"release": "np", | ||
"lint": "eslint --ext js,ts src test", | ||
"lint:fix": "eslint --ext js,ts src test --fix", | ||
"lint": "eslint src test", | ||
"lint:fix": "eslint src test --fix", | ||
"format": "prettier --check \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"", | ||
@@ -84,3 +84,3 @@ "format:fix": "prettier --write \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\"" | ||
"devDependencies": { | ||
"@hono/eslint-config": "^0.0.4", | ||
"@hono/eslint-config": "^1.0.1", | ||
"@types/jest": "^29.5.3", | ||
@@ -90,3 +90,3 @@ "@types/node": "^20.10.0", | ||
"@whatwg-node/fetch": "^0.9.14", | ||
"eslint": "^8.55.0", | ||
"eslint": "^9.10.0", | ||
"hono": "^4.4.10", | ||
@@ -93,0 +93,0 @@ "jest": "^29.6.1", |
@@ -197,2 +197,18 @@ # Node.js Adapter for Hono | ||
#### `onFound` | ||
You can specify handling when the requested file is found with `onFound`. | ||
```ts | ||
app.use( | ||
'/static/*', | ||
serveStatic({ | ||
// ... | ||
onFound: (_path, c) => { | ||
c.header('Cache-Control', `public, immutable, max-age=31536000`) | ||
}, | ||
}) | ||
) | ||
``` | ||
#### `onNotFound` | ||
@@ -214,2 +230,15 @@ | ||
#### `precompressed` | ||
The `precompressed` option checks if files with extensions like `.br` or `.gz` are available and serves them based on the `Accept-Encoding` header. It prioritizes Brotli, then Zstd, and Gzip. If none are available, it serves the original file. | ||
```ts | ||
app.use( | ||
'/static/*', | ||
serveStatic({ | ||
precompressed: true, | ||
}) | ||
) | ||
``` | ||
## ConnInfo Helper | ||
@@ -216,0 +245,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
166157
4979
328