Skip to content

Commit cbf0610

Browse files
author
haoxin
committedApr 26, 2017
lint
1 parent 737193c commit cbf0610

File tree

6 files changed

+423
-409
lines changed

6 files changed

+423
-409
lines changed
 

‎.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
extends: standard

‎example.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11

2-
const send = require('./');
3-
const Koa = require('koa');
4-
const app = new Koa();
2+
const send = require('./')
3+
const Koa = require('koa')
4+
const app = new Koa()
55

66
// $ GET /package.json
77
// $ GET /
88

99
app.use(async (ctx) => {
10-
if ('/' == ctx.path) return ctx.body = 'Try GET /package.json';
11-
await send(ctx, ctx.path, { root: __dirname });
10+
if (ctx.path === '/') {
11+
ctx.body = 'Try GET /package.json'
12+
return
13+
}
14+
await send(ctx, ctx.path, { root: __dirname })
1215
})
1316

14-
app.listen(3000);
15-
console.log('listening on port 3000');
17+
app.listen(3000)
18+
console.log('listening on port 3000')

‎index.js

+68-68
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Module dependencies.
33
*/
44

5-
const debug = require('debug')('koa-send');
6-
const resolvePath = require('resolve-path');
7-
const createError = require('http-errors');
8-
const assert = require('assert');
9-
const fs = require('mz/fs');
5+
const debug = require('debug')('koa-send')
6+
const resolvePath = require('resolve-path')
7+
const createError = require('http-errors')
8+
const assert = require('assert')
9+
const fs = require('mz/fs')
1010

1111
const {
1212
normalize,
@@ -15,13 +15,13 @@ const {
1515
resolve,
1616
parse,
1717
sep
18-
} = require('path');
18+
} = require('path')
1919

2020
/**
2121
* Expose `send()`.
2222
*/
2323

24-
module.exports = send;
24+
module.exports = send
2525

2626
/**
2727
* Send file at `path` with the
@@ -34,139 +34,139 @@ module.exports = send;
3434
* @api public
3535
*/
3636

37-
async function send(ctx, path, opts = {}) {
38-
assert(ctx, 'koa context required');
39-
assert(path, 'pathname required');
37+
async function send (ctx, path, opts = {}) {
38+
assert(ctx, 'koa context required')
39+
assert(path, 'pathname required')
4040

4141
// options
42-
debug('send "%s" %j', path, opts);
43-
const root = opts.root ? normalize(resolve(opts.root)) : '';
44-
const trailingSlash = '/' == path[path.length - 1];
45-
path = path.substr(parse(path).root.length);
46-
const index = opts.index;
47-
const maxage = opts.maxage || opts.maxAge || 0;
48-
const immutable = opts.immutable || false;
49-
const hidden = opts.hidden || false;
50-
const format = opts.format === false ? false : true;
51-
const extensions = Array.isArray(opts.extensions) ? opts.extensions : false;
52-
const brotli = opts.brotli === false ? false : true;
53-
const gzip = opts.gzip === false ? false : true;
54-
const setHeaders = opts.setHeaders;
42+
debug('send "%s" %j', path, opts)
43+
const root = opts.root ? normalize(resolve(opts.root)) : ''
44+
const trailingSlash = path[path.length - 1] === '/'
45+
path = path.substr(parse(path).root.length)
46+
const index = opts.index
47+
const maxage = opts.maxage || opts.maxAge || 0
48+
const immutable = opts.immutable || false
49+
const hidden = opts.hidden || false
50+
const format = opts.format !== false
51+
const extensions = Array.isArray(opts.extensions) ? opts.extensions : false
52+
const brotli = opts.brotli !== false
53+
const gzip = opts.gzip !== false
54+
const setHeaders = opts.setHeaders
5555

5656
if (setHeaders && typeof setHeaders !== 'function') {
5757
throw new TypeError('option setHeaders must be function')
5858
}
5959

6060
// normalize path
61-
path = decode(path);
61+
path = decode(path)
6262

63-
if (-1 == path) return ctx.throw(400, 'failed to decode');
63+
if (path === -1) return ctx.throw(400, 'failed to decode')
6464

6565
// index file support
66-
if (index && trailingSlash) path += index;
66+
if (index && trailingSlash) path += index
6767

68-
path = resolvePath(root, path);
68+
path = resolvePath(root, path)
6969

7070
// hidden file support, ignore
71-
if (!hidden && isHidden(root, path)) return;
71+
if (!hidden && isHidden(root, path)) return
7272

7373
// serve brotli file when possible otherwise gzipped file when possible
7474
if (ctx.acceptsEncodings('br', 'deflate', 'identity') === 'br' && brotli && (await fs.exists(path + '.br'))) {
75-
path = path + '.br';
76-
ctx.set('Content-Encoding', 'br');
77-
ctx.res.removeHeader('Content-Length');
75+
path = path + '.br'
76+
ctx.set('Content-Encoding', 'br')
77+
ctx.res.removeHeader('Content-Length')
7878
} else if (ctx.acceptsEncodings('gzip', 'deflate', 'identity') === 'gzip' && gzip && (await fs.exists(path + '.gz'))) {
79-
path = path + '.gz';
80-
ctx.set('Content-Encoding', 'gzip');
81-
ctx.res.removeHeader('Content-Length');
79+
path = path + '.gz'
80+
ctx.set('Content-Encoding', 'gzip')
81+
ctx.res.removeHeader('Content-Length')
8282
}
8383

8484
if (extensions && !/\..*$/.exec(path)) {
85-
const list = [].concat(extensions);
85+
const list = [].concat(extensions)
8686
for (let i = 0; i < list.length; i++) {
87-
let ext = list[i];
87+
let ext = list[i]
8888
if (typeof ext !== 'string') {
89-
throw new TypeError('option extensions must be array of strings or false');
89+
throw new TypeError('option extensions must be array of strings or false')
9090
}
91-
if (!/^\./.exec(ext)) ext = '.' + ext;
91+
if (!/^\./.exec(ext)) ext = '.' + ext
9292
if (await fs.exists(path + ext)) {
93-
path = path + ext;
94-
break;
93+
path = path + ext
94+
break
9595
}
9696
}
9797
}
9898

9999
// stat
100100
let stats
101101
try {
102-
stats = await fs.stat(path);
102+
stats = await fs.stat(path)
103103

104104
// Format the path to serve static file servers
105105
// and not require a trailing slash for directories,
106106
// so that you can do both `/directory` and `/directory/`
107107
if (stats.isDirectory()) {
108108
if (format && index) {
109-
path += '/' + index;
110-
stats = await fs.stat(path);
109+
path += '/' + index
110+
stats = await fs.stat(path)
111111
} else {
112-
return;
112+
return
113113
}
114114
}
115115
} catch (err) {
116-
const notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'];
116+
const notfound = ['ENOENT', 'ENAMETOOLONG', 'ENOTDIR']
117117
if (notfound.includes(err.code)) {
118-
throw createError(404, err);
118+
throw createError(404, err)
119119
}
120-
err.status = 500;
121-
throw err;
120+
err.status = 500
121+
throw err
122122
}
123123

124-
if (setHeaders) setHeaders(ctx.res, path, stats);
124+
if (setHeaders) setHeaders(ctx.res, path, stats)
125125

126126
// stream
127-
ctx.set('Content-Length', stats.size);
128-
if (!ctx.response.get('Last-Modified')) ctx.set('Last-Modified', stats.mtime.toUTCString());
127+
ctx.set('Content-Length', stats.size)
128+
if (!ctx.response.get('Last-Modified')) ctx.set('Last-Modified', stats.mtime.toUTCString())
129129
if (!ctx.response.get('Cache-Control')) {
130-
const directives = ['max-age=' + (maxage / 1000 | 0)];
131-
if (opts.immutable) {
132-
directives.push('immutable');
130+
const directives = ['max-age=' + (maxage / 1000 | 0)]
131+
if (immutable) {
132+
directives.push('immutable')
133133
}
134-
ctx.set('Cache-Control', directives.join(','));
134+
ctx.set('Cache-Control', directives.join(','))
135135
}
136-
ctx.type = type(path);
137-
ctx.body = fs.createReadStream(path);
136+
ctx.type = type(path)
137+
ctx.body = fs.createReadStream(path)
138138

139-
return path;
139+
return path
140140
}
141141

142142
/**
143143
* Check if it's hidden.
144144
*/
145145

146-
function isHidden(root, path) {
147-
path = path.substr(root.length).split(sep);
148-
for(let i = 0; i < path.length; i++) {
149-
if(path[i][0] === '.') return true;
146+
function isHidden (root, path) {
147+
path = path.substr(root.length).split(sep)
148+
for (let i = 0; i < path.length; i++) {
149+
if (path[i][0] === '.') return true
150150
}
151-
return false;
151+
return false
152152
}
153153

154154
/**
155155
* File type.
156156
*/
157157

158-
function type(file) {
159-
return extname(basename(file, '.gz'));
158+
function type (file) {
159+
return extname(basename(file, '.gz'))
160160
}
161161

162162
/**
163163
* Decode `path`.
164164
*/
165165

166-
function decode(path) {
166+
function decode (path) {
167167
try {
168-
return decodeURIComponent(path);
168+
return decodeURIComponent(path)
169169
} catch (err) {
170-
return -1;
170+
return -1
171171
}
172172
}

‎package.json

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
"index.js"
1414
],
1515
"devDependencies": {
16+
"eslint": "^3.19.0",
17+
"eslint-config-standard": "^10.2.1",
18+
"eslint-plugin-import": "^2.2.0",
19+
"eslint-plugin-node": "^4.2.2",
20+
"eslint-plugin-promise": "^3.5.0",
21+
"eslint-plugin-standard": "^3.0.1",
1622
"iltorb": "^1.2.1",
1723
"istanbul": "0",
1824
"koa": "2",
@@ -28,6 +34,7 @@
2834
"resolve-path": "^1.3.3"
2935
},
3036
"scripts": {
37+
"lint": "eslint --fix .",
3138
"test": "mocha --require should --reporter spec",
3239
"test-cov": "istanbul cover ./node_modules/.bin/_mocha -- --require should",
3340
"test-travis": "istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- --require should"

‎test/.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
env:
3+
mocha: true

‎test/index.js

+333-334
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.