From dbfa0a370f5ab57f23ed44f5f56850a3f497a4e3 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Wed, 27 Jul 2022 23:19:34 +0700 Subject: [PATCH 1/2] fix: moleculer crash if file size is too large --- src/alias.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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); From 82922757d4eb2e45bbf5f9a85f73c91daeaa0b5d Mon Sep 17 00:00:00 2001 From: Icebob Date: Sat, 13 Aug 2022 15:51:12 +0200 Subject: [PATCH 2/2] add example --- examples/2k_bytes.log | 1 + examples/issue-313.js | 91 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 examples/2k_bytes.log create mode 100644 examples/issue-313.js diff --git a/examples/2k_bytes.log b/examples/2k_bytes.log new file mode 100644 index 00000000..29c29400 --- /dev/null +++ b/examples/2k_bytes.log @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 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); +});