Ghost/core/server/api/shared/headers.js
root@andrea:~# 3f91a9e8a2 Corrected 'Content-Length' header by using Buffer.byteLength (#10055)
Closes #10041
1. Why is this change neccesary?
String.prototype.length returns the number of code units in the string (number
of characters) while Buffer.byteLength returns the actual byte length of a
string.

2. How does it address the issue?
Places that use String.prototype.length to calculate Content-Length
were switched to Buffer.byteLength instead.
2018-10-25 09:18:36 +07:00

53 lines
1.3 KiB
JavaScript

const debug = require('ghost-ignition').debug('api:shared:headers');
const INVALIDATE_ALL = '/*';
const cacheInvalidate = (result, options = {}) => {
let value = options.value;
return {
'X-Cache-Invalidate': value || INVALIDATE_ALL
};
};
const disposition = {
csv(result, options = {}) {
return {
'Content-Disposition': options.value,
'Content-Type': 'text/csv'
};
},
json(result, options = {}) {
return {
'Content-Disposition': options.value,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(JSON.stringify(result))
};
},
yaml(result, options = {}) {
return {
'Content-Disposition': options.value,
'Content-Type': 'application/yaml',
'Content-Length': Buffer.byteLength(JSON.stringify(result))
};
}
};
module.exports = {
get(result, apiConfig = {}) {
let headers = {};
if (apiConfig.disposition) {
Object.assign(headers, disposition[apiConfig.disposition.type](result, apiConfig.disposition));
}
if (apiConfig.cacheInvalidate) {
Object.assign(headers, cacheInvalidate(result, apiConfig.cacheInvalidate));
}
debug(headers);
return headers;
}
};