koa-sendfile
Advanced tools
Comparing version 2.0.1 to 3.0.0
48
index.js
@@ -1,14 +0,18 @@ | ||
var extname = require('path').extname | ||
var calculate = require('etag') | ||
var fs = require('mz/fs') | ||
const promisify = require('util').promisify | ||
const extname = require('path').extname | ||
const fs = require('fs') | ||
const calculate = require('etag') | ||
var notfound = { | ||
const stat = promisify(fs.stat) | ||
const notfound = { | ||
ENOENT: true, | ||
ENAMETOOLONG: true, | ||
ENOTDIR: true, | ||
ENOTDIR: true | ||
} | ||
module.exports = function sendfile(ctx, path) { | ||
return fs.stat(path) | ||
.then(function(stats){ | ||
module.exports = async function sendfile (ctx, path) { | ||
try { | ||
const stats = await stat(path) | ||
if (!stats) return null | ||
@@ -21,15 +25,17 @@ if (!stats.isFile()) return stats | ||
ctx.response.type = extname(path) | ||
if (!ctx.response.etag) ctx.response.etag = calculate(stats, { | ||
weak: true | ||
}) | ||
if (!ctx.response.etag) { | ||
ctx.response.etag = calculate(stats, { | ||
weak: true | ||
}) | ||
} | ||
// fresh based solely on last-modified | ||
var fresh = ctx.request.fresh | ||
switch (ctx.request.method) { | ||
case 'HEAD': | ||
ctx.response.status = fresh ? 304 : 200 | ||
ctx.status = ctx.request.fresh ? 304 : 200 | ||
break | ||
case 'GET': | ||
if (fresh) { | ||
ctx.response.status = 304 | ||
if (ctx.request.fresh) { | ||
ctx.status = 304 | ||
} else { | ||
@@ -42,9 +48,7 @@ ctx.body = fs.createReadStream(path) | ||
return stats | ||
}, onstaterror); | ||
} catch (err) { | ||
if (notfound[err.code]) return | ||
err.status = 500 | ||
throw err | ||
} | ||
} | ||
function onstaterror(err) { | ||
if (notfound[err.code]) return | ||
err.status = 500 | ||
throw err | ||
} |
{ | ||
"name": "koa-sendfile", | ||
"version": "3.0.0", | ||
"description": "basic file-sending utility for koa", | ||
"version": "2.0.1", | ||
"files": [ | ||
"index.js" | ||
], | ||
"repository": "koajs/sendfile", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"pretest": "npm run lint", | ||
"test": "mocha --r should --exit", | ||
"precoverage": "rimraf .nyc_output coverage", | ||
"coverage": "nyc npm run test", | ||
"ci": "npm run coverage" | ||
}, | ||
"author": { | ||
@@ -11,27 +23,23 @@ "name": "Jonathan Ong", | ||
}, | ||
"repository": "koajs/sendfile", | ||
"license": "MIT", | ||
"dependencies": { | ||
"debug": "^3.2.5", | ||
"etag": "^1.5.1", | ||
"mz": "2" | ||
"debug": "^4.2.0", | ||
"etag": "^1.8.1" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0", | ||
"koa": "1", | ||
"should": "8", | ||
"mocha": "2", | ||
"supertest": "1" | ||
"eslint": "^6.8.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"koa": "^2.13.0", | ||
"mocha": "^8.1.3", | ||
"nyc": "^15.1.0", | ||
"should": "^13.2.3", | ||
"supertest": "^4.0.2" | ||
}, | ||
"scripts": { | ||
"test": "NODE_ENV=test mocha --harmony --require should --reporter spec", | ||
"test-cov": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --require should", | ||
"test-travis": "NODE_ENV=test node --harmony ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should" | ||
}, | ||
"engines": { | ||
"node": ">= 0.12" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"license": "MIT" | ||
"node": ">= 10" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
# koa sendfile | ||
@@ -34,7 +33,7 @@ | ||
```js | ||
var sendfile = require('koa-sendfile') | ||
const sendfile = require('koa-sendfile') | ||
app.use(function* (next) { | ||
var stats = yield sendfile(this, '/Users/jong/.bash_profile') | ||
if (!this.status) this.throw(404) | ||
app.use(async function (ctx, next) { | ||
const stats = await sendfile(ctx, '/Users/jong/.bash_profile') | ||
if (!ctx.status) ctx.throw(404) | ||
}) | ||
@@ -41,0 +40,0 @@ ``` |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
5697
2
44
11
55
2
+ Addeddebug@4.4.0(transitive)
- Removedmz@2
- Removedany-promise@1.3.0(transitive)
- Removeddebug@3.2.7(transitive)
- Removedmz@2.7.0(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedthenify@3.3.1(transitive)
- Removedthenify-all@1.6.0(transitive)
Updateddebug@^4.2.0
Updatedetag@^1.8.1