Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
824 additions
and
719 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
charset = utf-8 | ||
|
||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,11 @@ | ||
{ | ||
"extends": "eslint", | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 8, | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": ["error", 4, {"SwitchCase": 1}], | ||
"quotes": ["error", "single"], | ||
"no-unused-expressions": 0, | ||
"no-underscore-dangle": 0, | ||
"func-style": [ | ||
"error", | ||
"declaration", { "allowArrowFunctions": true } | ||
] | ||
} | ||
} | ||
{ | ||
"extends": "airbnb-base", | ||
"env": { | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"rules": { | ||
"no-unused-expressions": 0, | ||
"no-param-reassign": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ coverage | |
npm-debug.log | ||
node_modules | ||
.DS_Store | ||
logs/app.error.log | ||
logs/app.error.log | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
language: node_js | ||
node_js: | ||
- "7.6" | ||
- "7.7" | ||
- "7.8" | ||
- "7.9" | ||
- "7" | ||
- "8" | ||
script: | ||
- npm run lint | ||
- npm run test | ||
- npm run report | ||
- npm run report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
environment: | ||
matrix: | ||
- nodejs_version: "7.7" | ||
- nodejs_version: "8" | ||
install: | ||
- ps: Install-Product node $env:nodejs_version | ||
- npm install | ||
test_script: | ||
- node --version | ||
- npm --version | ||
- npm run lint | ||
- npm run test:lint | ||
- npm run test:security | ||
- npm run test:code | ||
build: off | ||
build: off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,74 @@ | ||
const debug = require('debug')('yeps:static:index'); | ||
|
||
const methodCheck = require('./lib/methodCheck'); | ||
const cacheCheck = require('./lib/cacheCheck'); | ||
const getPath = require('./lib/getPath'); | ||
const getStats = require('./lib/getStats'); | ||
const getType = require('./lib/getType'); | ||
const getEncoding = require('./lib/getEncoding'); | ||
const fileStream = require('./lib/fileStream'); | ||
const gzipFileStream = require('./lib/gzipFileStream'); | ||
|
||
module.exports = ({root = __dirname, index = 'index.html', etag = true, gzip = true, maxage = 0} = {}) => async context => { | ||
|
||
debug('YEPS Static'); | ||
debug('Headers: %O', context.req.headers); | ||
|
||
try { | ||
await methodCheck(context.req.method.toUpperCase()); | ||
await cacheCheck(etag, context); | ||
let path = await getPath(root, index, context.req.url); | ||
const type = getType(path); | ||
const stats = await getStats(index, path); | ||
path = stats.path; | ||
const encoding = getEncoding(context.req.headers['accept-encoding']); | ||
|
||
if (etag) { | ||
context.res.setHeader('ETag', context.req.url); | ||
debug('ETag: %s', context.req.url); | ||
} | ||
|
||
context.res.setHeader('Last-Modified', stats.mtime.toUTCString()); | ||
debug('Last-Modified: %s', stats.mtime.toUTCString()); | ||
|
||
if (maxage) { | ||
context.res.setHeader('Cache-Control', `max-age=${(maxage / 1000 | 0)}`); | ||
debug('Cache-Control: %s', `max-age=${(maxage / 1000 | 0)}`); | ||
} | ||
|
||
context.res.setHeader('Content-Type', type); | ||
debug('Content-Type: %s', type); | ||
|
||
if (gzip) { | ||
context.res.setHeader('Content-Encoding', encoding); | ||
debug('Content-Encoding: %s', encoding); | ||
} | ||
|
||
context.res.statusCode = 200; | ||
|
||
if (gzip) { | ||
debug('gzip enabled!'); | ||
debug('Encoding: %s', encoding); | ||
|
||
await gzipFileStream(path, context.res, encoding); | ||
} else { | ||
await fileStream(path, context.res); | ||
} | ||
} catch (error) { | ||
debug('Error: %O', error); | ||
|
||
if (error && !['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'].includes(error.code)) { | ||
return Promise.reject(error); | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
return Promise.reject(); | ||
}; | ||
const debug = require('debug')('yeps:static:index'); | ||
|
||
const methodCheck = require('./lib/methodCheck'); | ||
const cacheCheck = require('./lib/cacheCheck'); | ||
const getPath = require('./lib/getPath'); | ||
const getStats = require('./lib/getStats'); | ||
const getType = require('./lib/getType'); | ||
const getEncoding = require('./lib/getEncoding'); | ||
const fileStream = require('./lib/fileStream'); | ||
const gzipFileStream = require('./lib/gzipFileStream'); | ||
|
||
module.exports = ({ | ||
root = __dirname, | ||
index = 'index.html', | ||
etag = true, | ||
gzip = true, | ||
maxage = 0, | ||
} = {}) => async (context) => { | ||
debug('YEPS Static'); | ||
debug('Headers: %O', context.req.headers); | ||
|
||
try { | ||
await methodCheck(context.req.method.toUpperCase()); | ||
await cacheCheck(etag, context); | ||
const path = await getPath(root, index, context.req.url); | ||
const stats = await getStats(index, path); | ||
const newPath = stats.path; | ||
const type = getType(newPath); | ||
const encoding = getEncoding(context.req.headers['accept-encoding']); | ||
|
||
if (etag) { | ||
context.res.setHeader('ETag', context.req.url); | ||
debug('ETag: %s', context.req.url); | ||
} | ||
|
||
context.res.setHeader('Last-Modified', stats.mtime.toUTCString()); | ||
debug('Last-Modified: %s', stats.mtime.toUTCString()); | ||
|
||
if (maxage) { | ||
const age = maxage / 1000; | ||
context.res.setHeader('Cache-Control', `max-age=${age}`); | ||
debug('Cache-Control: %s', `max-age=${age}`); | ||
} | ||
|
||
context.res.setHeader('Content-Type', type); | ||
debug('Content-Type: %s', type); | ||
|
||
if (gzip) { | ||
context.res.setHeader('Content-Encoding', encoding); | ||
debug('Content-Encoding: %s', encoding); | ||
} | ||
|
||
context.res.statusCode = 200; | ||
|
||
if (gzip) { | ||
debug('gzip enabled!'); | ||
debug('Encoding: %s', encoding); | ||
|
||
await gzipFileStream(newPath, context.res, encoding); | ||
} else { | ||
await fileStream(newPath, context.res); | ||
} | ||
} catch (error) { | ||
debug('Error: %O', error); | ||
|
||
if (error && !['ENOENT', 'ENAMETOOLONG', 'ENOTDIR'].includes(error.code)) { | ||
return Promise.reject(error); | ||
} | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
return Promise.reject(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
const debug = require('debug')('yeps:static:cache'); | ||
|
||
module.exports = async (eTag, ctx) => { | ||
|
||
if (eTag && ctx.req.headers.etag) { | ||
|
||
ctx.res.statusCode = 304; | ||
debug('ETag: %s', ctx.req.headers.etag); | ||
|
||
ctx.res.end(); | ||
debug('End of response'); | ||
|
||
return Promise.reject(); | ||
} | ||
|
||
return Promise.resolve(); | ||
}; | ||
const debug = require('debug')('yeps:static:cache'); | ||
|
||
module.exports = async (eTag, ctx) => { | ||
if (eTag && ctx.req.headers.etag) { | ||
ctx.res.statusCode = 304; | ||
debug('ETag: %s', ctx.req.headers.etag); | ||
|
||
ctx.res.end(); | ||
debug('End of response'); | ||
|
||
return Promise.reject(); | ||
} | ||
|
||
return Promise.resolve(); | ||
}; |
Oops, something went wrong.