You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
9.6 KiB

1 year ago
{
"name": "sirv",
"version": "2.0.2",
"description": "The optimized & lightweight middleware for serving requests to static assets",
"repository": "lukeed/sirv",
"module": "build.mjs",
"types": "sirv.d.ts",
"main": "build.js",
"license": "MIT",
"files": [
"build.*",
"sirv.d.ts"
],
"author": {
"name": "Luke Edwards",
"email": "luke@lukeed.com",
"url": "https://lukeed.com"
},
"engines": {
"node": ">= 10"
},
"dependencies": {
"@polka/url": "^1.0.0-next.20",
"mrmime": "^1.0.0",
"totalist": "^3.0.0"
},
"readme": "# sirv ![CI](https://github.com/lukeed/sirv/workflows/CI/badge.svg)\n\n> The optimized and lightweight middleware for serving requests to static assets\n\nYou may use `sirv` as a *very* fast and lightweight alternative to [`serve-static`](https://www.npmjs.com/package/serve-static).\n\nThe massive performance advantage over `serve-static` is explained by **not** relying on the file system for existence checks on every request. These are expensive interactions and must be avoided whenever possible! Instead, when not in \"dev\" mode, `sirv` performs all its file-system operations upfront and then relies on its cache for future operations.\n\nThis middleware will work out of the box for [Polka](https://github.com/lukeed/polka), Express, and other Express-like frameworks. It will also work with the native `http`, `https` and `http2` modules. It requires _very_ little effort to modify/wrap it for servers that don't accept the `(req, res, next)` signature.\n\n:bulb: For a feature-complete CLI application, check out the sibling [`sirv-cli`](https://github.com/lukeed/sirv/tree/master/packages/sirv-cli) package as an alternative to [`zeit/serve`](https://github.com/zeit/serve)~!\n\n## Install\n\n```\n$ npm install --save sirv\n```\n\n\n## Usage\n\n```js\nconst sirv = require('sirv');\nconst polka = require('polka');\nconst compress = require('compression')();\n\n// Init `sirv` handler\nconst assets = sirv('public', {\n maxAge: 31536000, // 1Y\n immutable: true\n});\n\npolka()\n .use(compress, assets)\n .use('/api', require('./api'))\n .listen(3000, err => {\n if (err) throw err;\n console.log('> Ready on localhost:3000~!');\n });\n```\n\n\n## API\n\n### sirv(dir, opts={})\n\nReturns: `Function`\n\nThe returned function is a middleware in the standard Express-like signature: `(req, res, next)`, where `req` is the [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_incomingmessage), `res` is the [`http.ServerResponse`](https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_class_http_serverresponse), and `next` (in this case) is the function to call if no file was found for the given path.\n\nWhen defined, a `next()` callback is always called _instead of_ the [`opts.onNoMatch`](#optsonnomatch) callback. However, unlike `onNoMatch`, your `next()` is given no arguments.\n\n#### dir\nType: `String`<br>\nDefault: `.`\n\nThe directory from which to read and serve assets. It is resolved to an absolute path &mdash; you must provide an absolute path yourself if `process.cwd()` is not the correct assumption.\n\n#### opts.dev\nType: `Boolean`<br>\nDefault: `false`\n\nEnable \"dev\" mode, which disables/skips caching. Instead, `sirv` will traverse the file system ***on every request***.\n\nAdditionally, `dev` mode will ignore `maxAge` and `immutable` as these options generate a production-oriented `Cache-Control` header value.\n\n> **Important:** Do not use `dev` mode in production!\n\n#### opts.etag\nType: `Boolean`<br>\nDefault: `false`\n\nGenerate and attach an `ETag` header to responses.\n\n> **Note:** If an incoming request's [`If-None-Match` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match) matches the `ETag` value, a `304` response is given.\n\n#### opts.dotfiles\nType: `Boolean`<br>\nDefault: `false`\n\nAllow requests to dotfiles (files or directories beginning with a `.`).\n\n> **Note:** Requests to [`/.well-known/*`](https://tools.ietf.org/html/rfc8615) are always allowed.\n\n#### opts.extensions\nType: `Array<String>`<br>\nDefault: `['html', 'htm']`\n\nThe file extension fallbacks to check for if a pathame is not initially found. For example, if a `/login` request cannot find a `login` filename, it will then look for `login.html` and `login.htm` before giving up~!\n\n> **Important:** Actually, `sirv` will **also** look for `login/index.html` and `login/index.htm` before giving up.\n\n#### opts.gzip\nType: `Boolean`<br>\nDefault: `false`\n\nDetermine if `sirv` look for **precompiled** `*.gz` files.<br>\nMust be enabled _and_ the incoming request's [`
}