Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
evheniy committed Oct 30, 2017
1 parent 6015ef4 commit 51c4437
Show file tree
Hide file tree
Showing 18 changed files with 824 additions and 719 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

charset = utf-8

indent_style = space
indent_size = 2
33 changes: 11 additions & 22 deletions .eslintrc
@@ -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
}
}
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@ coverage
npm-debug.log
node_modules
.DS_Store
logs/app.error.log
logs/app.error.log
package-lock.json
9 changes: 3 additions & 6 deletions .travis.yml
@@ -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
32 changes: 25 additions & 7 deletions README.md
Expand Up @@ -29,50 +29,66 @@ YEPS Static file serving
## How to use

const App = require('yeps');

const serve = require('yeps-static');

const error = require('yeps-error');
const logger = require('yeps-logger');
const server = require('yeps-server');

const app = new App();

app.all([
serve(),
error(),
logger(),
serve(),
]);

server.createHttpServer(app);

Or with options:
Or with **options**:

const { resolve } = require('path');

app.all([
error(),
logger(),
serve({
root: resolve(__dirname, 'public'),
index: 'index.html',
etag: true,
gzip: true,
maxage: 0,
}),
error(),
]);

#### With virtual host

const App = require('yeps');
const VirtualHost = require('yeps-virtual-host');
const Router = require('yeps-router');

const error = require('yeps-error');
const logger = require('yeps-logger');

const server = require('yeps-server');

const { resolve } = require('path');

const vhost = new VirtualHost();
const router = new Router();

const serve = require('yeps-static');

const app = new App();

app.then(error());
app.all([
error(),
logger(),
]);

router.get('/').then(async ctx => {
router.get('/').then(async (ctx) => {
ctx.res.statusCode = 200;
ctx.res.setHeader('Content-Type', 'application/json');
ctx.res.end('{"status":"OK"}');
Expand All @@ -85,9 +101,11 @@ Or with options:
vhost
.http('static.yeps.info')
.then(serve({
root: resolve(__dirname, 'files')
root: resolve(__dirname, 'files'),
}));

app.then(vhost.resolve());

#### [YEPS documentation](http://yeps.info/)
server.createHttpServer(app);

#### [YEPS documentation](http://yeps.info/)
6 changes: 3 additions & 3 deletions appveyor.yml
@@ -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
142 changes: 74 additions & 68 deletions index.js
@@ -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();
};
32 changes: 15 additions & 17 deletions lib/cacheCheck.js
@@ -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();
};

0 comments on commit 51c4437

Please sign in to comment.