Merge branch 'refactor/move-interface-in-file-stream' of github.com:Tony133/nest into Tony133-refactor/move-interface-in-file-stream

This commit is contained in:
Kamil Myśliwiec
2023-02-01 12:27:16 +01:00
4 changed files with 25 additions and 10 deletions

View File

@@ -0,0 +1,2 @@
export * from './streamable-options.interface';
export * from './streamable-handler-response.interface';

View File

@@ -0,0 +1,12 @@
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;
/** 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;
}

View File

@@ -1,13 +1,8 @@
import { Readable } from 'stream';
import { types } from 'util';
import { HttpStatus } from '../enums';
import { isFunction } from '../utils/shared.utils';
import { StreamableFileOptions } from './streamable-options.interface';
export interface StreamableHandlerResponse {
destroyed: boolean;
statusCode: number;
send: (msg: string) => void;
}
import { StreamableFileOptions, StreamableHandlerResponse } from './interfaces';
/**
* @see [Streaming files](https://docs.nestjs.com/techniques/streaming-files)
@@ -21,10 +16,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 = HttpStatus.BAD_REQUEST;
res.send(err.message);
};
constructor(buffer: Uint8Array, options?: StreamableFileOptions);