Skip to content

Commit 081dcf5

Browse files
authored
feat: multipart upgrade to egg v3 (#123)
1 parent ef2b32d commit 081dcf5

File tree

19 files changed

+32
-80
lines changed

19 files changed

+32
-80
lines changed

multipart-file-mode/app/controller/ajax.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
3-
const fs = require('mz/fs');
41
const path = require('path');
2+
const fs = require('fs');
3+
const { pipeline } = require('stream/promises');
54
const Controller = require('egg').Controller;
6-
const pump = require('mz-modules/pump');
75

86
module.exports = class extends Controller {
97
async show() {
@@ -21,7 +19,7 @@ module.exports = class extends Controller {
2119
const target = fs.createWriteStream(targetPath);
2220

2321
try {
24-
await pump(source, target);
22+
await pipeline(source, target);
2523
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
2624
} finally {
2725
// delete those request tmp files

multipart-file-mode/app/controller/form.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict';
2-
3-
const fs = require('mz/fs');
41
const path = require('path');
5-
const pump = require('mz-modules/pump');
2+
const fs = require('fs');
3+
const { pipeline } = require('stream/promises');
64
const Controller = require('egg').Controller;
75

86
module.exports = class extends Controller {
@@ -21,7 +19,7 @@ module.exports = class extends Controller {
2119
const target = fs.createWriteStream(targetPath);
2220

2321
try {
24-
await pump(source, target);
22+
await pipeline(source, target);
2523
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
2624
} finally {
2725
// delete those request tmp files

multipart-file-mode/app/controller/home.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const Controller = require('egg').Controller;
42

53
module.exports = class extends Controller {

multipart-file-mode/app/controller/multiple.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
'use strict';
2-
3-
const fs = require('mz/fs');
41
const path = require('path');
5-
const pump = require('mz-modules/pump');
2+
const fs = require('fs');
3+
const { pipeline } = require('stream/promises');
64
const Controller = require('egg').Controller;
75

86
module.exports = class extends Controller {
@@ -21,7 +19,7 @@ module.exports = class extends Controller {
2119
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
2220
const source = fs.createReadStream(file.filepath);
2321
const target = fs.createWriteStream(targetPath);
24-
await pump(source, target);
22+
await pipeline(source, target);
2523
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
2624
}
2725
} finally {

multipart-file-mode/app/router.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
module.exports = app => {
42
app.router.get('/', 'home.render');
53

multipart-file-mode/config/config.default.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
exports.keys = 'my keys';
42

53
exports.view = {

multipart-file-mode/config/plugin.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
exports.nunjucks = {
42
enable: true,
53
package: 'egg-view-nunjucks',

multipart-file-mode/package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
{
22
"name": "multipart-file-mode-example",
33
"dependencies": {
4-
"egg": "^2.11.2",
5-
"egg-view-nunjucks": "^2.1.4",
6-
"mz": "^2.7.0",
7-
"mz-modules": "^2.1.0"
4+
"egg": "^3.11.0",
5+
"egg-view-nunjucks": "^2.3.0"
86
},
97
"devDependencies": {
10-
"egg-bin": "^4.3.5",
11-
"egg-mock": "^3.13.1"
8+
"egg-bin": "^5.9.0",
9+
"egg-mock": "^5.5.0"
1210
},
1311
"scripts": {
1412
"dev": "egg-bin dev",

multipart-file-mode/test/index.test.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
'use strict';
2-
31
const path = require('path');
2+
const fs = require('fs/promises');
43
const assert = require('assert');
5-
const mm = require('egg-mock');
6-
const rimraf = require('mz-modules/rimraf');
4+
const { app, mock } = require('egg-mock/bootstrap');
75

86
describe('example multipart test', () => {
9-
let app;
10-
before(async () => {
11-
app = mm.app();
12-
await app.ready();
13-
});
14-
after(() => app.close());
15-
after(() => rimraf(path.join(app.config.baseDir, 'app/public')));
7+
afterEach(mock.restore);
8+
after(() => fs.rm(path.join(app.config.baseDir, 'app/public'), { force: true, recursive: true }));
169

1710
describe('form', () => {
1811
it('should upload', async () => {

multipart/app/controller/ajax.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
31
const fs = require('fs');
42
const path = require('path');
3+
const { pipeline } = require('stream/promises');
54
const Controller = require('egg').Controller;
6-
const pump = require('mz-modules/pump');
75

86
class UploadAjaxController extends Controller {
97
async show() {
@@ -15,7 +13,7 @@ class UploadAjaxController extends Controller {
1513
const filename = encodeURIComponent(stream.fields.name) + path.extname(stream.filename).toLowerCase();
1614
const target = path.join(this.config.baseDir, 'app/public', filename);
1715
const writeStream = fs.createWriteStream(target);
18-
await pump(stream, writeStream);
16+
await pipeline(stream, writeStream);
1917

2018
this.ctx.body = { url: '/public/' + filename };
2119
}

0 commit comments

Comments
 (0)