Skip to content

Commit 8e8869a

Browse files
authored
refactor: upgrade egg-core@4 (#1631)
1 parent b586a23 commit 8e8869a

File tree

9 files changed

+42
-48
lines changed

9 files changed

+42
-48
lines changed

.autod.conf.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,5 @@ module.exports = {
2626
'@types/koa',
2727
'@types/koa-router',
2828
],
29-
semver: [
30-
'koa-bodyparser@2',
31-
],
3229
test: 'scripts',
3330
};

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- '6'
54
- '8'
65
- '9'
76
install:

app/extend/context.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const delegate = require('delegates');
4-
const co = require('co');
54
const { assign } = require('utility');
65

76
const HELPER = Symbol('Context#helper');
@@ -197,13 +196,15 @@ const proto = module.exports = {
197196
const start = Date.now();
198197
/* istanbul ignore next */
199198
const taskName = scope.name || '-';
200-
co(function* () {
201-
yield scope(ctx);
202-
ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
203-
}).catch(err => {
204-
ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);
205-
ctx.coreLogger.error(err);
206-
});
199+
// use app.toAsyncFunction to support both generator function and async function
200+
ctx.app.toAsyncFunction(scope)(ctx)
201+
.then(() => {
202+
ctx.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, Date.now() - start);
203+
})
204+
.catch(err => {
205+
ctx.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, Date.now() - start);
206+
ctx.coreLogger.error(err);
207+
});
207208
},
208209
};
209210

app/middleware/meta.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
'use strict';
66

77
module.exports = () => {
8-
return function* meta(next) {
9-
yield next;
8+
return async function meta(ctx, next) {
9+
await next();
1010
// total response time header
11-
this.set('x-readtime', Date.now() - this.starttime);
11+
ctx.set('x-readtime', Date.now() - ctx.starttime);
1212
};
1313
};

app/middleware/notfound.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
'use strict';
22

33
module.exports = options => {
4-
return function* notfound(next) {
5-
yield next;
4+
return async function notfound(ctx, next) {
5+
await next();
66

7-
if (this.status !== 404 || this.body) {
7+
if (ctx.status !== 404 || ctx.body) {
88
return;
99
}
1010

1111
// set status first, make sure set body not set status
12-
this.status = 404;
12+
ctx.status = 404;
1313

14-
if (this.acceptJSON) {
15-
this.body = {
14+
if (ctx.acceptJSON) {
15+
ctx.body = {
1616
message: 'Not Found',
1717
};
1818
return;
@@ -21,16 +21,16 @@ module.exports = options => {
2121
const notFoundHtml = '<h1>404 Not Found</h1>';
2222

2323
// notfound handler is unimplemented
24-
if (options.pageUrl && this.path === options.pageUrl) {
25-
this.body = `${notFoundHtml}<p><pre><code>config.notfound.pageUrl(${options.pageUrl})</code></pre> is unimplemented</p>`;
24+
if (options.pageUrl && ctx.path === options.pageUrl) {
25+
ctx.body = `${notFoundHtml}<p><pre><code>config.notfound.pageUrl(${options.pageUrl})</code></pre> is unimplemented</p>`;
2626
return;
2727
}
2828

2929
if (options.pageUrl) {
30-
this.realStatus = 404;
31-
this.redirect(options.pageUrl);
30+
ctx.realStatus = 404;
31+
ctx.redirect(options.pageUrl);
3232
return;
3333
}
34-
this.body = notFoundHtml;
34+
ctx.body = notFoundHtml;
3535
};
3636
};

app/middleware/site_file.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ const path = require('path');
44
const MAX_AGE = 'public, max-age=2592000'; // 30 days
55

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

12-
const content = options[this.path];
13-
if (!content) return yield next;
12+
const content = options[ctx.path];
13+
if (!content) return next();
1414

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

1919
// '/robots.txt': Buffer <xx..
2020
// content is buffer
2121
if (Buffer.isBuffer(content)) {
22-
this.set('cache-control', MAX_AGE);
23-
this.body = content;
24-
this.type = path.extname(this.path);
22+
ctx.set('cache-control', MAX_AGE);
23+
ctx.body = content;
24+
ctx.type = path.extname(ctx.path);
2525
return;
2626
}
2727

28-
yield next;
28+
return next();
2929
};
3030
};

appveyor.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
environment:
22
matrix:
3-
- nodejs_version: '6'
4-
- nodejs_version: '7'
53
- nodejs_version: '8'
4+
- nodejs_version: '9'
65

76
install:
87
- ps: Install-Product node $env:nodejs_version

lib/egg.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,11 +398,7 @@ class EggApplication extends EggCore {
398398
client.create = (...args) => {
399399
const realClient = create.apply(client, args);
400400
this[CLUSTER_CLIENTS].push(realClient);
401-
402-
this.beforeClose(function* () {
403-
yield cluster.close(realClient);
404-
});
405-
401+
this.beforeClose(() => cluster.close(realClient));
406402
return realClient;
407403
};
408404
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
"accepts": "^1.3.4",
2020
"agentkeepalive": "^3.3.0",
2121
"cluster-client": "^1.7.1",
22-
"co": "^4.6.0",
2322
"debug": "^3.1.0",
2423
"delegates": "^1.0.0",
2524
"egg-cluster": "^1.12.4",
2625
"egg-cookies": "^2.2.1",
27-
"egg-core": "^3.18.0",
26+
"egg-core": "^4.0.0",
2827
"egg-development": "^1.3.2",
2928
"egg-i18n": "^1.2.0",
3029
"egg-jsonp": "^1.2.1",
@@ -42,9 +41,9 @@
4241
"graceful": "^1.0.1",
4342
"humanize-ms": "^1.2.1",
4443
"is-type-of": "^1.2.0",
45-
"koa-bodyparser": "^2.5.0",
44+
"koa-bodyparser": "^4.2.0",
4645
"koa-is-json": "^1.0.0",
47-
"koa-override": "^2.0.0",
46+
"koa-override": "^3.0.0",
4847
"mime-types": "^2.1.17",
4948
"sendmessage": "^1.1.0",
5049
"urllib": "^2.25.1",
@@ -107,7 +106,10 @@
107106
"url": "https://github.com/eggjs/egg.git"
108107
},
109108
"engines": {
110-
"node": ">= 6.0.0"
109+
"node": ">= 8.0.0"
110+
},
111+
"publishConfig": {
112+
"tag": "next"
111113
},
112114
"license": "MIT"
113115
}

0 commit comments

Comments
 (0)