Skip to content

Commit

Permalink
Merge pull request #337 from richardgarnier/richard/fix-stream-hang
Browse files Browse the repository at this point in the history
Avoid stream hanging on error
  • Loading branch information
icebob authored Oct 9, 2023
2 parents 4556386 + 211d116 commit c553ac5
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const _ = require("lodash");
const bodyParser = require("body-parser");
const serveStatic = require("serve-static");
const isReadableStream = require("isstream").isReadable;
const { pipeline } = require('stream');

const { MoleculerError, MoleculerServerError, ServiceNotFoundError } = require("moleculer").Errors;
const { ServiceUnavailableError, NotFoundError, ForbiddenError, RateLimitExceeded, ERR_ORIGIN_NOT_ALLOWED } = require("./errors");
Expand Down Expand Up @@ -818,7 +819,11 @@ module.exports = {
} else {
// respond
if (isReadableStream(data)) { //Stream response
data.pipe(res);
pipeline(data, res, err => {
if (err) {
this.logger.warn("Stream got an error.", { err, url: req.url, actionName: action.name })
}
})
} else {
res.end(chunk);
}
Expand Down

0 comments on commit c553ac5

Please sign in to comment.