Skip to content

Commit

Permalink
fix(common): streamable file for headers already sent case
Browse files Browse the repository at this point in the history
  • Loading branch information
micalevisk committed Jan 20, 2023
1 parent 95e096f commit 9de14aa
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/common/file-stream/streamable-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { isFunction } from '../utils/shared.utils';
import { StreamableFileOptions } from './streamable-options.interface';

export interface StreamableHandlerResponse {
/** `true` if the connection is destroyed, `false` otherwise. */
destroyed: boolean;
/** `true` if headers were sent, `false` otherwise. */
headersSent: boolean;
/** The status code that will be sent to the client when the headers get flushed. */
statusCode: number;
send: (msg: string) => void;
/** Sends the HTTP response. */
send: (body: string) => void;
/** Signals to the server that all of the response headers and body have been sent. */
end: () => void;
}

export class StreamableFile {
Expand All @@ -16,10 +23,16 @@ export class StreamableFile {
err: Error,
response: StreamableHandlerResponse,
) => void = (err: Error, res) => {
if (!res.destroyed) {
res.statusCode = 400;
res.send(err.message);
if (res.destroyed) {
return;
}
if (res.headersSent) {
res.end();
return;
}

res.statusCode = 400;
res.send(err.message);
};

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

0 comments on commit 9de14aa

Please sign in to comment.