Comparing version 3.1.1 to 3.2.0
3.2.0 / 2016-03-23 | ||
================== | ||
* add setHeaders option | ||
3.1.1 / 2016-03-04 | ||
================== | ||
* bump deps | ||
* simplify scripts | ||
* bump deps | ||
* simplify scripts | ||
@@ -8,0 +13,0 @@ 3.1.0 / 2015-10-24 |
11
index.js
@@ -52,3 +52,8 @@ /** | ||
var gzip = opts.gzip === false ? false : true; | ||
var setHeaders = opts.setHeaders; | ||
if (setHeaders && typeof setHeaders !== 'function') { | ||
throw new TypeError('option setHeaders must be function') | ||
} | ||
var encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity'); | ||
@@ -98,6 +103,8 @@ | ||
if (setHeaders) setHeaders(ctx.res, path, stats); | ||
// stream | ||
ctx.set('Last-Modified', stats.mtime.toUTCString()); | ||
ctx.set('Content-Length', stats.size); | ||
ctx.set('Cache-Control', 'max-age=' + (maxage / 1000 | 0)); | ||
if (!ctx.response.get('Last-Modified')) ctx.set('Last-Modified', stats.mtime.toUTCString()); | ||
if (!ctx.response.get('Cache-Control')) ctx.set('Cache-Control', 'max-age=' + (maxage / 1000 | 0)); | ||
ctx.type = type(path); | ||
@@ -104,0 +111,0 @@ ctx.body = fs.createReadStream(path); |
@@ -5,3 +5,3 @@ { | ||
"repository": "koajs/send", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "koa", |
@@ -23,7 +23,8 @@ | ||
- `hidden` Allow transfer of hidden files. defaults to false | ||
- `root` Root directory to restrict file access | ||
- [`root`](#root-path) Root directory to restrict file access | ||
- `gzip` Try to serve the gzipped version of a file automatically when `gzip` is supported by a client and if the requested file with `.gz` extension exists. defaults to true. | ||
- `format` If not `false` (defaults to `true`), format the path to serve static file servers and not require a trailing slash for directories, so that you can do both `/directory` and `/directory/` | ||
- [`setHeaders`](#setheaders) Function to set custom headers on response. | ||
## Root path | ||
### Root path | ||
@@ -52,2 +53,13 @@ Note that `root` is required, defaults to `''` and will be resolved, | ||
### setHeaders | ||
The function is called as `fn(res, path, stats)`, where the arguments are: | ||
* `res`: the response object | ||
* `path`: the resolved file path that is being sent | ||
* `stats`: the stats object of the file that is being sent. | ||
You should only use the `setHeaders` option when you wish to edit the `Cache-Control` or `Last-Modified` headers, because doing it before is useless (it's overwritten by `send`), and doing it after is too late because the headers are already sent. | ||
If you want to edit any other header, simply set them before calling `send`. | ||
## Example (Koa@2) with async/await | ||
@@ -54,0 +66,0 @@ |
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
10011
119
121