-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathformat-response.js
36 lines (26 loc) · 1.1 KB
/
format-response.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const isBinary = require('./is-binary');
const Response = require('../../../response');
const sanitizeHeaders = require('./sanitize-headers');
module.exports = (event, response, options) => {
const { statusCode } = response;
const { headers, multiValueHeaders } = sanitizeHeaders(Response.headers(response));
if (headers['transfer-encoding'] === 'chunked' || response.chunkedEncoding) {
throw new Error('chunked encoding not supported');
}
let cookies = [];
if (multiValueHeaders['set-cookie']) {
cookies = multiValueHeaders['set-cookie'];
}
const isBase64Encoded = isBinary(headers, options);
const encoding = isBase64Encoded ? 'base64' : 'utf8';
const body = Response.body(response).toString(encoding);
let formattedResponse = { statusCode, headers, isBase64Encoded, body };
if (event.version === '2.0' && cookies.length) {
formattedResponse['cookies'] = cookies;
}
if ((!event.version || event.version === '1.0') && Object.keys(multiValueHeaders).length) {
formattedResponse['multiValueHeaders'] = multiValueHeaders;
}
return formattedResponse;
};