Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

koa-sendfile

Package Overview
Dependencies
Maintainers
9
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-sendfile - npm Package Compare versions

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 @@ })

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc