Skip to content

Commit

Permalink
feat(common): Add Buffer support has returned value from an endpoint
Browse files Browse the repository at this point in the history
Closes: #755
  • Loading branch information
Romakita committed Feb 6, 2020
1 parent 165ca9d commit 406224d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/mvc/builders/HandlerBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class HandlerBuilder {
process = process.toPromise();
}

if (isStream(process)) {
if (isStream(process) || Buffer.isBuffer(process)) {
return done(null, process);
}

Expand Down
12 changes: 10 additions & 2 deletions packages/common/src/mvc/components/SendResponseMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,24 @@ export class SendResponseMiddleware implements IMiddleware {
return response.send();
}

if (isStream(data)) {
if (this.shouldStream(data)) {
data.pipe(response);

return response;
}

if (isBoolean(data) || isNumber(data) || isString(data) || data === null) {
if (this.shouldSend(data)) {
return response.send(data);
}

return response.json(this.converterService.serialize(data, {type: endpoint.type}));
}

protected shouldSend(data: any) {
return Buffer.isBuffer(data) || isBoolean(data) || isNumber(data) || isString(data) || data === null;
}

protected shouldStream(data: any) {
return isStream(data);
}
}

0 comments on commit 406224d

Please sign in to comment.