graphql-helix
Version:
A highly evolved GraphQL HTTP Server 🧬
87 lines (86 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendResult = exports.sendPushResult = exports.sendMultipartResponseResult = exports.sendResponseResult = void 0;
const errors_1 = require("../errors");
const calculate_byte_length_1 = require("../util/calculate-byte-length");
const utils_1 = require("./utils");
async function sendResponseResult(responseResult, rawResponse, transformResult = utils_1.DEFAULT_TRANSFORM_RESULT_FN) {
for (const { name, value } of responseResult.headers) {
rawResponse.setHeader(name, value);
}
const data = JSON.stringify(transformResult(responseResult.payload));
rawResponse.writeHead(responseResult.status, {
"content-type": "application/json",
"content-length": (0, calculate_byte_length_1.calculateByteLength)(data),
});
rawResponse.end(data);
}
exports.sendResponseResult = sendResponseResult;
async function sendMultipartResponseResult(multipartResult, rawResponse, transformResult = utils_1.DEFAULT_TRANSFORM_RESULT_FN) {
/**
* TypeScript complains because of function call signature mismatches.
* The signatures, however, are identical, thus we cas this here to suppress warnings,
*/
const response = rawResponse;
response.writeHead(200, {
// prettier-ignore
"Connection": "keep-alive",
"Content-Type": 'multipart/mixed; boundary="-"',
"Transfer-Encoding": "chunked",
});
response.on("close", () => {
multipartResult.unsubscribe();
});
response.write("---");
await multipartResult.subscribe((result) => {
const chunk = JSON.stringify(transformResult(result));
const data = [
"",
"Content-Type: application/json; charset=utf-8",
"Content-Length: " + (0, calculate_byte_length_1.calculateByteLength)(chunk),
"",
chunk,
];
if (result.hasNext) {
data.push("---");
}
response.write(data.join("\r\n"));
});
response.write("\r\n-----\r\n");
response.end();
}
exports.sendMultipartResponseResult = sendMultipartResponseResult;
async function sendPushResult(pushResult, rawResponse, transformResult = utils_1.DEFAULT_TRANSFORM_RESULT_FN) {
/**
* TypeScript complains because of function call signature mismatches.
* The signatures, however, are identical, thus we cas this here to suppress warnings,
*/
const response = rawResponse;
response.writeHead(200, {
"Content-Type": "text/event-stream",
// prettier-ignore
"Connection": "keep-alive",
"Cache-Control": "no-cache",
});
response.on("close", () => {
pushResult.unsubscribe();
});
await pushResult.subscribe((result) => {
response.write(`data: ${JSON.stringify(transformResult(result))}\n\n`);
});
response.end();
}
exports.sendPushResult = sendPushResult;
async function sendResult(result, rawResponse, transformResult = utils_1.DEFAULT_TRANSFORM_RESULT_FN) {
switch (result.type) {
case "RESPONSE":
return sendResponseResult(result, rawResponse, transformResult);
case "MULTIPART_RESPONSE":
return sendMultipartResponseResult(result, rawResponse, transformResult);
case "PUSH":
return sendPushResult(result, rawResponse, transformResult);
default:
throw new errors_1.HttpError(500, "Cannot process result.");
}
}
exports.sendResult = sendResult;