Skip to content

Commit

Permalink
refactor: upgrade egg-core@4 (#1631)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Nov 9, 2017
1 parent b586a23 commit 8e8869a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 48 deletions.
3 changes: 0 additions & 3 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ module.exports = {
'@types/koa',
'@types/koa-router',
],
semver: [
'koa-bodyparser@2',
],
test: 'scripts',
};
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
sudo: false
language: node_js
node_js:
- '6'
- '8'
- '9'
install:
Expand Down
17 changes: 9 additions & 8 deletions app/extend/context.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const delegate = require('delegates');
const co = require('co');
const { assign } = require('utility');

const HELPER = Symbol('Context#helper');
Expand Down Expand Up @@ -197,13 +196,15 @@ const proto = module.exports = {
const start = Date.now();
/* istanbul ignore next */
const taskName = scope.name || '-';
co(function* () {
yield scope(ctx);
ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
}).catch(err => {
ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);
ctx.coreLogger.error(err);
});
// use app.toAsyncFunction to support both generator function and async function
ctx.app.toAsyncFunction(scope)(ctx)
.then(() => {
ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
})
.catch(err => {
ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);
ctx.coreLogger.error(err);
});
},
};

Expand Down
6 changes: 3 additions & 3 deletions app/middleware/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
'use strict';

module.exports = () => {
return function* meta(next) {
yield next;
return async function meta(ctx, next) {
await next();
// total response time header
this.set('x-readtime', Date.now() - this.starttime);
ctx.set('x-readtime', Date.now() - ctx.starttime);
};
};
22 changes: 11 additions & 11 deletions app/middleware/notfound.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

module.exports = options => {
return function* notfound(next) {
yield next;
return async function notfound(ctx, next) {
await next();

if (this.status !== 404 || this.body) {
if (ctx.status !== 404 || ctx.body) {
return;
}

// set status first, make sure set body not set status
this.status = 404;
ctx.status = 404;

if (this.acceptJSON) {
this.body = {
if (ctx.acceptJSON) {
ctx.body = {
message: 'Not Found',
};
return;
Expand All @@ -21,16 +21,16 @@ module.exports = options => {
const notFoundHtml = '<h1>404 Not Found</h1>';

// notfound handler is unimplemented
if (options.pageUrl && this.path === options.pageUrl) {
this.body = `${notFoundHtml}<p><pre><code>config.notfound.pageUrl(${options.pageUrl})</code></pre> is unimplemented</p>`;
if (options.pageUrl && ctx.path === options.pageUrl) {
ctx.body = `${notFoundHtml}<p><pre><code>config.notfound.pageUrl(${options.pageUrl})</code></pre> is unimplemented</p>`;
return;
}

if (options.pageUrl) {
this.realStatus = 404;
this.redirect(options.pageUrl);
ctx.realStatus = 404;
ctx.redirect(options.pageUrl);
return;
}
this.body = notFoundHtml;
ctx.body = notFoundHtml;
};
};
20 changes: 10 additions & 10 deletions app/middleware/site_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ const path = require('path');
const MAX_AGE = 'public, max-age=2592000'; // 30 days

module.exports = options => {
return function* siteFile(next) {
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next;
return function siteFile(ctx, next) {
if (ctx.method !== 'HEAD' && ctx.method !== 'GET') return next();
/* istanbul ignore if */
if (this.path[0] !== '/') return yield next;
if (ctx.path[0] !== '/') return next();

const content = options[this.path];
if (!content) return yield next;
const content = options[ctx.path];
if (!content) return next();

// '/favicon.ico': 'https://eggjs.org/favicon.ico',
// content is url
if (typeof content === 'string') return this.redirect(content);
if (typeof content === 'string') return ctx.redirect(content);

// '/robots.txt': Buffer <xx..
// content is buffer
if (Buffer.isBuffer(content)) {
this.set('cache-control', MAX_AGE);
this.body = content;
this.type = path.extname(this.path);
ctx.set('cache-control', MAX_AGE);
ctx.body = content;
ctx.type = path.extname(ctx.path);
return;
}

yield next;
return next();
};
};
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
6 changes: 1 addition & 5 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,7 @@ class EggApplication extends EggCore {
client.create = (...args) => {
const realClient = create.apply(client, args);
this[CLUSTER_CLIENTS].push(realClient);

this.beforeClose(function* () {
yield cluster.close(realClient);
});

this.beforeClose(() => cluster.close(realClient));
return realClient;
};
}
Expand Down
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
"accepts": "^1.3.4",
"agentkeepalive": "^3.3.0",
"cluster-client": "^1.7.1",
"co": "^4.6.0",
"debug": "^3.1.0",
"delegates": "^1.0.0",
"egg-cluster": "^1.12.4",
"egg-cookies": "^2.2.1",
"egg-core": "^3.18.0",
"egg-core": "^4.0.0",
"egg-development": "^1.3.2",
"egg-i18n": "^1.2.0",
"egg-jsonp": "^1.2.1",
Expand All @@ -42,9 +41,9 @@
"graceful": "^1.0.1",
"humanize-ms": "^1.2.1",
"is-type-of": "^1.2.0",
"koa-bodyparser": "^2.5.0",
"koa-bodyparser": "^4.2.0",
"koa-is-json": "^1.0.0",
"koa-override": "^2.0.0",
"koa-override": "^3.0.0",
"mime-types": "^2.1.17",
"sendmessage": "^1.1.0",
"urllib": "^2.25.1",
Expand Down Expand Up @@ -107,7 +106,10 @@
"url": "https://github.com/eggjs/egg.git"
},
"engines": {
"node": ">= 6.0.0"
"node": ">= 8.0.0"
},
"publishConfig": {
"tag": "next"
},
"license": "MIT"
}

0 comments on commit 8e8869a

Please sign in to comment.