Skip to content

Commit

Permalink
chore: no need to consume stream on error throw (#18)
Browse files Browse the repository at this point in the history
The stream will be auto consume by koa-onerror
see koajs/onerror#33

closes eggjs/egg#2897
  • Loading branch information
fengmk2 committed Aug 20, 2018
1 parent 41e43c4 commit 0b4e118
Show file tree
Hide file tree
Showing 6 changed files with 45,928 additions and 25 deletions.
30 changes: 5 additions & 25 deletions README.md
Expand Up @@ -135,15 +135,8 @@ module.exports = Class UploadController extends Controller {
// file not exists will response 400 error
const stream = await ctx.getFileStream();
const name = 'egg-multipart-test/' + path.basename(stream.filename);
let result;
try {
// process file or upload to cloud storage
result = await ctx.oss.put(name, stream);
} catch (err) {
// must consume the stream, otherwise browser will be stuck.
await sendToWormhole(stream);
throw err;
}
// process file or upload to cloud storage
const result = await ctx.oss.put(name, stream);

ctx.body = {
url: result.url,
Expand All @@ -159,14 +152,8 @@ module.exports = Class UploadController extends Controller {
let result;
if (stream.filename) {
const name = 'egg-multipart-test/' + path.basename(stream.filename);
try {
// process file or upload to cloud storage
result = await ctx.oss.put(name, stream);
} catch (err) {
// must consume the stream, otherwise browser will be stuck.
await sendToWormhole(stream);
throw err;
}
// process file or upload to cloud storage
const result = await ctx.oss.put(name, stream);
} else {
// must consume the empty stream
await sendToWormhole(stream);
Expand Down Expand Up @@ -195,7 +182,6 @@ Controller which hanlder `POST /upload`:

```js
// app/controller/upload.js
const sendToWormhole = require('stream-wormhole');
const Controller = require('egg').Controller;

module.exports = Class UploadController extends Controller {
Expand All @@ -222,13 +208,7 @@ module.exports = Class UploadController extends Controller {
console.log('filename: ' + part.filename);
console.log('encoding: ' + part.encoding);
console.log('mime: ' + part.mime);
let result;
try {
result = await ctx.oss.put('egg-multipart-test/' + part.filename, part);
} catch (err) {
await sendToWormhole(part);
throw err;
}
const result = await ctx.oss.put('egg-multipart-test/' + part.filename, part);
console.log(result);
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -59,6 +59,7 @@
"is-type-of": "^1.0.0",
"mkdirp": "^0.5.1",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
"stream-wormhole": "^1.0.3",
"supertest": "^3.0.0",
"urllib": "^2.29.1",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/apps/multipart/app/controller/upload.js
Expand Up @@ -37,6 +37,10 @@ module.exports = async ctx => {
};
}

if (ctx.query.mock_undefined_error) {
part.foo();
}

const filepath = path.join(ctx.app.config.logger.dir, 'multipart-test-file');
await saveStream(part, filepath);
ctx.body = {
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/apps/upload-one-file/app/controller/async.js
Expand Up @@ -8,6 +8,10 @@ module.exports = app => {
async async() {
const ctx = this.ctx;
const stream = await ctx.getFileStream();
if (ctx.query.foo === 'error') {
// mock undefined error
stream.foo();
}
const name = 'egg-multipart-test/' + process.version + '-' + Date.now() + '-' + path.basename(stream.filename);
const result = await ctx.oss.put(name, stream);
ctx.body = {
Expand Down

0 comments on commit 0b4e118

Please sign in to comment.