Skip to content

Commit

Permalink
Merge 2a44e9f into f2a61b0
Browse files Browse the repository at this point in the history
  • Loading branch information
smcmurray committed Oct 24, 2015
2 parents f2a61b0 + 2a44e9f commit 84def63
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 44 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -24,17 +24,17 @@ It does not:

## API

### 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)`.

```js
var sendfile = require('koa-sendfile')

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)
})
```
Expand Down
55 changes: 28 additions & 27 deletions index.js
@@ -1,4 +1,3 @@

var extname = require('path').extname
var calculate = require('etag')
var fs = require('mz/fs')
Expand All @@ -9,35 +8,37 @@ var notfound = {
ENOTDIR: true,
}

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

function onstaterror(err) {
Expand Down
24 changes: 11 additions & 13 deletions test/index.js
Expand Up @@ -11,7 +11,7 @@ describe('when path is a directory', function () {
it('should 404', function (done) {
var app = koa()
app.use(function* (next) {
var stats = yield* sendfile.call(this, __dirname)
var stats = yield sendfile(this, __dirname)
assert.ok(stats)
})

Expand All @@ -25,7 +25,7 @@ describe('when path does not exist', function () {
it('should 404', function (done) {
var app = koa()
app.use(function* (next) {
var stats = yield* sendfile.call(this, __dirname + '/kljasdlkfjaklsdf')
var stats = yield sendfile(this, __dirname + '/kljasdlkfjaklsdf')
assert.ok(!stats)
})

Expand All @@ -42,7 +42,7 @@ describe('when path exists', function () {
var stats, tag
tag = fs.stat(process.cwd() + '/test/fixture.txt')
app.use(function* (next) {
stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
tag = calculate(stats, {
weak: true
Expand All @@ -66,14 +66,13 @@ describe('when path exists', function () {

it('should support weak etag 304', function (done) {
var app = koa()
var stats, tag
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
tag = calculate(stat, {
var tag = calculate(stat, {
weak: true
})

app.use(function* (next) {
stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
})

Expand All @@ -86,10 +85,9 @@ describe('when path exists', function () {

it('should support last-modified 304', function (done) {
var app = koa()
var stats
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
app.use(function* (next) {
stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
})

Expand All @@ -104,7 +102,7 @@ describe('when path exists', function () {
var app = koa()
var stream
app.use(function* (next) {
var stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
var stats = yield sendfile(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
stream = this.body
this.socket.emit('error', new Error('boom'))
Expand All @@ -124,8 +122,8 @@ describe('when path exists', function () {
it('should 200', function (done) {
var app = koa()
app.use(function* (next) {
var stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
yield sendfile(this, process.cwd() + '/test/fixture.txt')
.then(assert.ok)
})

request(app.listen())
Expand All @@ -139,8 +137,8 @@ describe('when path exists', function () {
var app = koa()
fs.stat(process.cwd() + '/test/fixture.txt').then(function(stat) {
app.use(function* (next) {
stats = yield* sendfile.call(this, process.cwd() + '/test/fixture.txt')
assert.ok(stats)
yield sendfile(this, process.cwd() + '/test/fixture.txt')
.then(assert.ok)
})

request(app.listen())
Expand Down

0 comments on commit 84def63

Please sign in to comment.