Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use pipeline over stream.pipe #9819

Merged
merged 4 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions integration/send-files/e2e/express.spec.ts
Expand Up @@ -65,4 +65,7 @@ describe('Express FileSend', () => {
expect(res.text).to.be.eq(readmeString);
});
});
it('should return an error if the file does not exist', async () => {
return request(app.getHttpServer()).get('/file/not/exist').expect(400);
});
});
5 changes: 5 additions & 0 deletions integration/send-files/src/app.controller.ts
Expand Up @@ -31,4 +31,9 @@ export class AppController {
getFileWithHeaders(): StreamableFile {
return this.appService.getFileWithHeaders();
}

@Get('file/not/exist')
getNonExistantFile(): StreamableFile {
return this.appService.getFileThatDoesNotExist();
}
}
4 changes: 4 additions & 0 deletions integration/send-files/src/app.service.ts
Expand Up @@ -35,4 +35,8 @@ export class AppService {
},
);
}

getFileThatDoesNotExist(): StreamableFile {
return new StreamableFile(createReadStream('does-not-exist.txt'));
}
}
25 changes: 25 additions & 0 deletions packages/common/file-stream/streamable-file.ts
Expand Up @@ -3,9 +3,20 @@ import { types } from 'util';
import { isFunction } from '../utils/shared.utils';
import { StreamableFileOptions } from './streamable-options.interface';

export interface StreamableHandlerResponse {
statusCode: number;
send: (msg: string) => void;
}

export class StreamableFile {
private readonly stream: Readable;

protected handler: (err: Error, response: StreamableHandlerResponse) => void =
jmcdo29 marked this conversation as resolved.
Show resolved Hide resolved
(err: Error, res) => {
res.statusCode = 400;
res.send(err.message);
};

constructor(buffer: Uint8Array, options?: StreamableFileOptions);
constructor(readable: Readable, options?: StreamableFileOptions);
constructor(
Expand Down Expand Up @@ -38,4 +49,18 @@ export class StreamableFile {
length,
};
}

get errorHandler(): (
err: Error,
response: StreamableHandlerResponse,
) => void {
return this.handler;
}

setErrorHandler(
handler: (err: Error, response: StreamableHandlerResponse) => void,
) {
this.handler = handler;
return this;
}
}
14 changes: 13 additions & 1 deletion packages/platform-express/adapters/express-adapter.ts
@@ -1,5 +1,6 @@
import {
InternalServerErrorException,
Logger,
RawBodyRequest,
RequestMethod,
StreamableFile,
Expand Down Expand Up @@ -33,6 +34,7 @@ import * as cors from 'cors';
import * as express from 'express';
import * as http from 'http';
import * as https from 'https';
import { PassThrough, pipeline } from 'stream';
import { ServeStaticOptions } from '../interfaces/serve-static-options.interface';

type VersionedRoute = <
Expand Down Expand Up @@ -78,7 +80,17 @@ export class ExpressAdapter extends AbstractHttpAdapter {
) {
response.setHeader('Content-Length', streamHeaders.length);
}
return body.getStream().pipe(response);
return pipeline(
body.getStream().once('error', (err: Error) => {
body.errorHandler(err, response);
}),
response,
(err: Error) => {
if (err) {
new Logger('ExpressAdapter').error(err.message, err.stack);
jmcdo29 marked this conversation as resolved.
Show resolved Hide resolved
}
},
);
}
return isObject(body) ? response.json(body) : response.send(String(body));
}
Expand Down