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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa diff --git a/examples/issue-313.js b/examples/issue-313.js new file mode 100644 index 00000000..9ad047a8 --- /dev/null +++ b/examples/issue-313.js @@ -0,0 +1,91 @@ +const { ServiceBroker } = require("moleculer"); +const ApiGatewayService = require("../index"); +const http = require("http"); +const path = require("path"); +const fs = require("fs"); +const FormData = require("form-data"); + +const broker = new ServiceBroker({ + logger: false, +}); + +broker.createService({ + name: "file", + actions: { + save: { + handler(ctx) { + console.log("File received"); + return true; + }, + }, + }, +}); + +const gw = broker.createService(ApiGatewayService, { + name: "gateway", + settings: { + port: 0, + routes: [ + { + path: "/upload", + + aliases: { + "POST /": { + type: "multipart", + busboyConfig: { + limits: { + files: 1, + fileSize: 1e3, + }, + }, + action: "file.save", + }, + }, + }, + ], + }, +}); + +broker.start().then(() => { + return new Promise((resolve) => { + const form = new FormData(); + form.append( + "file", + fs.createReadStream(path.join(__dirname, "2k_bytes.log")) + ); + + const req = http.request( + { + host: "127.0.0.1", + port: gw.server.address().port, + path: "/upload", + method: "POST", + headers: form.getHeaders(), + }, + (res) => { + let data = ""; + res.on("error", console.error); + res.on("data", (chunk) => { + data += chunk.toString(); + }); + res.on("end", () => { + console.log(res.statusCode, data); + resolve(); + }); + } + ); + + req.on("error", (err) => { + console.log("req error", err); + // debugger + }); + form.pipe(req); + }); +}); + +// Event 'uncaughtException' +process.on("uncaughtException", (error, source) => { + console.log("uncaught hit"); + console.error(error, source); + process.exit(1); +}); diff --git a/src/alias.js b/src/alias.js index 932d3935..5b499b67 100644 --- a/src/alias.js +++ b/src/alias.js @@ -158,7 +158,11 @@ class Alias { if (_.isFunction(busboyOptions.onFileSizeLimit)) { busboyOptions.onFileSizeLimit.call(this.service, file, busboy); } - file.destroy(new PayloadTooLarge({ fieldname, filename, encoding, mimetype })); + file.destroy(); + busboy.emit("error", new PayloadTooLarge({ fieldname, filename, encoding, mimetype })); + }); + file.on("error", err => { + busboy.emit("error", err); }); numOfFiles++; promises.push(ctx.call(this.action, file, _.defaultsDeep({}, this.route.opts.callOptions, { meta: { @@ -209,7 +213,7 @@ class Alias { }); /* istanbul ignore next */ - busboy.on("error", err => { + busboy.once("error", err => { req.unpipe(req.busboy); req.resume(); this.service.sendError(req, res, err);