'use strict';

const { promisify } = require('util');

// Sends response
const send = function ({
  specific: { res } = {},
  content,
  contentType,
  contentLength,
  protocolstatus,
}) {
  // `specific` might be undefined, if initial input was wrong.
  if (!res) { return; }

  // However, we are not sure a response has already been sent or not,
  // so we must check to avoid double responses
  if (res.finished) { return; }

  setHeaders({ res, contentType, contentLength, protocolstatus });

  const sendResponse = promisify(res.end.bind(res));

  return sendResponse(content);
};

const setHeaders = function ({
  res,
  contentType,
  contentLength,
  protocolstatus,
}) {
  if (protocolstatus) {
    // eslint-disable-next-line no-param-reassign, fp/no-mutation
    res.statusCode = protocolstatus;
  }

  if (contentType) {
    res.setHeader('Content-Type', contentType);
  }

  res.setHeader('Content-Length', contentLength);
};

module.exports = {
  send,
};

@@ -78,16 +78,22 @@
 t send =
+ async
  functio
@@ -110,16 +110,21 @@
 cific: %7B
+ req,
  res %7D =
@@ -559,22 +559,21 @@
 s));%0A%0A  
-return
+await
  sendRes
@@ -587,16 +587,156 @@
 ontent);
+%0A%0A  // Otherwise, socket might not be freed, e.g. if an error was thrown before%0A  // the request body was fully read%0A  req.socket.destroy();
 %0A%7D;%0A%0Acon

Fix socket hanging