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

+3-5
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

+3-5
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

-2
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

+3-5
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

-2
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

-2
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

-2
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

+4-6
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

+4-11
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

+2-4
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
}

multipart/app/controller/buffer.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const fs = require('mz/fs');
1+
const fs = require('fs');
42
const path = require('path');
53
const Controller = require('egg').Controller;
64
const toArray = require('stream-to-array');

multipart/app/controller/form.js

+2-4
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 UploadFormController extends Controller {
97
async show() {
@@ -15,7 +13,7 @@ class UploadFormController 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
this.ctx.redirect('/public/' + filename);
2018
}
2119
}

multipart/app/controller/home.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
const Controller = require('egg').Controller;
42

53
class HomeController extends Controller {

multipart/app/controller/multiple.js

+2-4
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 UploadMultipleController extends Controller {
97
async show() {
@@ -19,7 +17,7 @@ class UploadMultipleController extends Controller {
1917
const filename = stream.filename.toLowerCase();
2018
const target = path.join(this.config.baseDir, 'app/public', filename);
2119
const writeStream = fs.createWriteStream(target);
22-
await pump(stream, writeStream);
20+
await pipeline(stream, writeStream);
2321
files.push(filename);
2422
}
2523

multipart/app/router.js

-2
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/config/config.default.js

-2
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/config/plugin.js

-2
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/package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
{
22
"name": "multipart-example",
33
"dependencies": {
4-
"egg": "^2.9.1",
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
"stream-to-array": "^2.3.0",
97
"stream-wormhole": "^1.0.4"
108
},
119
"devDependencies": {
12-
"egg-bin": "^4.3.5",
13-
"egg-mock": "^3.13.1"
10+
"egg-bin": "^5.9.0",
11+
"egg-mock": "^5.5.0"
1412
},
1513
"scripts": {
1614
"dev": "egg-bin dev",

multipart/test/index.test.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
'use strict';
2-
31
const path = require('path');
42
const assert = require('assert');
5-
const mm = require('egg-mock');
6-
const rimraf = require('mz-modules/rimraf');
3+
const fs = require('fs/promises');
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'), { recursive: true, force: true }));
169

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

0 commit comments

Comments
 (0)