1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | Object.defineProperty(exports, "__esModule", { value: true });
|
7 | exports.writeResultToResponse = void 0;
|
8 | const stream_1 = require("stream");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function writeResultToResponse(
|
17 | // not needed and responsibility should be in the sequence.send
|
18 | response,
|
19 | // result returned back from invoking controller method
|
20 | result) {
|
21 |
|
22 |
|
23 | if (result === response || response.headersSent) {
|
24 | return;
|
25 | }
|
26 | if (result === undefined) {
|
27 | response.statusCode = 204;
|
28 | response.end();
|
29 | return;
|
30 | }
|
31 | const isStream = result instanceof stream_1.Readable || typeof (result === null || result === void 0 ? void 0 : result.pipe) === 'function';
|
32 | if (isStream) {
|
33 | response.setHeader('Content-Type', 'application/octet-stream');
|
34 |
|
35 | result.pipe(response);
|
36 | return;
|
37 | }
|
38 | switch (typeof result) {
|
39 | case 'object':
|
40 | case 'boolean':
|
41 | case 'number':
|
42 | if (Buffer.isBuffer(result)) {
|
43 |
|
44 | response.setHeader('Content-Type', 'application/octet-stream');
|
45 | }
|
46 | else {
|
47 |
|
48 |
|
49 | response.setHeader('Content-Type', 'application/json');
|
50 |
|
51 | result = JSON.stringify(result);
|
52 | }
|
53 | break;
|
54 | default:
|
55 | response.setHeader('Content-Type', 'text/plain');
|
56 | result = result.toString();
|
57 | break;
|
58 | }
|
59 | response.end(result);
|
60 | }
|
61 | exports.writeResultToResponse = writeResultToResponse;
|
62 |
|
\ | No newline at end of file |