Skip to content

Commit

Permalink
fix: add a check if the res is destroyed before sending response
Browse files Browse the repository at this point in the history
In the case of using `curl` or a similar tool on the command line,
or if the client decides to end the request for a streamed file early
without the check we would end up causing a server crash. Now, we will
just not send the response as the client has already decided how to
move on.

fix: nestjs#10105
  • Loading branch information
jmcdo29 committed Aug 12, 2022
1 parent 7389070 commit 3fb6771
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/common/file-stream/streamable-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isFunction } from '../utils/shared.utils';
import { StreamableFileOptions } from './streamable-options.interface';

export interface StreamableHandlerResponse {
destroyed: boolean;
statusCode: number;
send: (msg: string) => void;
}
Expand All @@ -15,8 +16,10 @@ export class StreamableFile {
err: Error,
response: StreamableHandlerResponse,
) => void = (err: Error, res) => {
res.statusCode = 400;
res.send(err.message);
if (!res.destroyed) {
res.statusCode = 400;
res.send(err.message);
}
};

constructor(buffer: Uint8Array, options?: StreamableFileOptions);
Expand Down

0 comments on commit 3fb6771

Please sign in to comment.