@sap/xsodata
Version:
Expose data from a HANA database as OData V2 service with help of .xsodata files.
69 lines (57 loc) • 1.71 kB
JavaScript
;
const _ = require('lodash');
const http = require('http');
module.exports.createResponse = function () {
return new SimpleResponse();
};
function SimpleResponse() {
this.statusCode = null;
this.headers = {};
this.data = '';
}
SimpleResponse.prototype._isBatchCRLFEnabled = function () {
return (
typeof process !== 'undefined' &&
process.env &&
process.env.XSODATA_ENABLE_BATCH_CRLF === 'true'
);
};
SimpleResponse.prototype.writeHead = function (code, headers) {
this.statusCode = code;
_.assign(this.headers, headers);
};
SimpleResponse.prototype.setHeader = function (name, value) {
this.headers[name] = value;
};
SimpleResponse.prototype.status = function (code) {
this.statusCode = code;
return this;
};
SimpleResponse.prototype.write = function (data) {
this.data += data;
return this;
};
SimpleResponse.prototype.end = function () {};
SimpleResponse.prototype.writeToBatchResponse = function (response, contentId) {
let value;
const headers = this.headers;
response.write(
'HTTP/1.1 ' +
this.statusCode +
' ' +
http.STATUS_CODES['' + this.statusCode] +
'\r\n'
);
Object.keys(headers).forEach(function (key) {
value = headers[key];
response.write(key + ': ' + value + '\r\n');
});
if (contentId) {
response.write('Content-ID: ' + contentId.id + '\r\n');
}
response.write('\r\n'); // end of header
response.write(this.data);
if (this._isBatchCRLFEnabled() && this.data.length > 0) {
response.write('\r\n'); //as used in OData_Specification_V2.0 batch samples
}
};