koa-sendfile
Advanced tools
Comparing version 1.1.1 to 2.0.0
55
index.js
@@ -1,2 +0,1 @@ | ||
var extname = require('path').extname | ||
@@ -12,31 +11,33 @@ var calculate = require('etag') | ||
module.exports = function* sendfile(path) { | ||
var stats = yield fs.stat(path).catch(onstaterror) | ||
if (!stats) return null | ||
if (!stats.isFile()) return stats | ||
module.exports = function sendfile(ctx, path) { | ||
return fs.stat(path) | ||
.then(function(stats){ | ||
if (!stats) return null | ||
if (!stats.isFile()) return stats | ||
this.response.status = 200 | ||
this.response.lastModified = stats.mtime | ||
this.response.length = stats.size | ||
this.response.type = extname(path) | ||
if (!this.response.etag) this.response.etag = calculate(stats, { | ||
weak: true | ||
}) | ||
ctx.response.status = 200 | ||
ctx.response.lastModified = stats.mtime | ||
ctx.response.length = stats.size | ||
ctx.response.type = extname(path) | ||
if (!ctx.response.etag) ctx.response.etag = calculate(stats, { | ||
weak: true | ||
}) | ||
// fresh based solely on last-modified | ||
var fresh = this.request.fresh | ||
switch (this.request.method) { | ||
case 'HEAD': | ||
this.response.status = fresh ? 304 : 200 | ||
break | ||
case 'GET': | ||
if (fresh) { | ||
this.response.status = 304 | ||
} else { | ||
this.body = fs.createReadStream(path) | ||
} | ||
break | ||
} | ||
// fresh based solely on last-modified | ||
var fresh = ctx.request.fresh | ||
switch (ctx.request.method) { | ||
case 'HEAD': | ||
ctx.response.status = fresh ? 304 : 200 | ||
break | ||
case 'GET': | ||
if (fresh) { | ||
ctx.response.status = 304 | ||
} else { | ||
ctx.body = fs.createReadStream(path) | ||
} | ||
break | ||
} | ||
return stats | ||
return stats | ||
}, onstaterror); | ||
} | ||
@@ -43,0 +44,0 @@ |
{ | ||
"name": "koa-sendfile", | ||
"description": "basic file-sending utility for koa", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"author": { | ||
@@ -15,11 +15,10 @@ "name": "Jonathan Ong", | ||
"etag": "^1.5.1", | ||
"mz": "1" | ||
"mz": "2" | ||
}, | ||
"devDependencies": { | ||
"istanbul-harmony": "0", | ||
"koa": "*", | ||
"co": "^3.0.5", | ||
"should": "^3.0", | ||
"mocha": "^1.13", | ||
"supertest": "0" | ||
"istanbul": "0", | ||
"koa": "1", | ||
"should": "8", | ||
"mocha": "2", | ||
"supertest": "1" | ||
}, | ||
@@ -32,4 +31,3 @@ "scripts": { | ||
"engines": { | ||
"node": ">= 0.11.13", | ||
"iojs": ">= 1" | ||
"node": ">= 0.12" | ||
}, | ||
@@ -36,0 +34,0 @@ "files": [ |
@@ -27,7 +27,7 @@ | ||
### var stats = yield* sendfile.call(this, filename) | ||
### sendfile(context, filename) | ||
You must pass `this` as the context. `filename` is the filename of the file. | ||
You must pass the koa context. `filename` is the filename of the file. | ||
`stats` is the `fs.stat()` result of the filename. If `stats` exists, that doesn't mean that a response is set - the filename could be a directory. Instead, check `if (this.status)`. | ||
sendfile returns a promise that resolves to the `fs.stat()` result of the filename. If sendfile() resolves, that doesn't mean that a response is set - the filename could be a directory. Instead, check `if (context.status)`. | ||
@@ -38,3 +38,3 @@ ```js | ||
app.use(function* (next) { | ||
var stats = yield* sendfile.call(this, '/Users/jong/.bash_profile') | ||
var stats = yield sendfile(this, '/Users/jong/.bash_profile') | ||
if (!this.status) this.throw(404) | ||
@@ -41,0 +41,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
5203
5
42
1
+ Addedmz@2.7.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
- Removedmz@1.3.0(transitive)
- Removednative-or-bluebird@1.2.0(transitive)
Updatedmz@2