src/kronos-koa.js
function respond(ctx) {
  // allow bypassing koa
  if (false === ctx.respond) return;

  const res = ctx.res;
  if (!ctx.writable) return;

  let body = ctx.body;
  const code = ctx.status;

  // ignore body
  if (statuses.empty[code]) {
    // strip headers
    ctx.body = null;
    return res.end();
  }

  if ("HEAD" == ctx.method) {
    if (!res.headersSent && isJSON(body)) {
      ctx.length = Buffer.byteLength(JSON.stringify(body));
    }
    return res.end();
  }

  // status body
  if (null == body) {
    body = ctx.message || String(code);
    if (!res.headersSent) {
      ctx.type = "text";
      ctx.length = Buffer.byteLength(body);
    }
    return res.end(body);
  }

  // responses
  if (Buffer.isBuffer(body)) return res.end(body);
  if ("string" == typeof body) return res.end(body);
  if (body instanceof Stream) return body.pipe(res);

  // body: json
  body = JSON.stringify(body);
  if (!res.headersSent) {
    ctx.length = Buffer.byteLength(body);
  }
  res.end(body);
}
function respond(ctx) {
  // allow bypassing koa
  if (false === ctx.respond) return;

  const res = ctx.res;
  if (!ctx.writable) return;

  let body = ctx.body;
  const code = ctx.status;

  // ignore body
  if (statuses.empty[code]) {
    // strip headers
    ctx.body = null;
    return res.end();
  }

  if ("HEAD" == ctx.method) {
    if (!res.headersSent && isJSON(body)) {
      ctx.length = Buffer.byteLength(JSON.stringify(body));
    }
    return res.end();
  }

  // status body
  if (null == body) {
    body = ctx.message || String(code);
    if (!res.headersSent) {
      ctx.type = "text";
      ctx.length = Buffer.byteLength(body);
    }
    return res.end(body);
  }

  // responses
  if (Buffer.isBuffer(body)) return res.end(body);
  if ("string" == typeof body) return res.end(body);
  if (body instanceof Stream) return body.pipe(res);

  // body: json
  body = JSON.stringify(body);
  if (!res.headersSent) {
    ctx.length = Buffer.byteLength(body);
  }
  res.end(body);
}
function respond ( ctx ) { // allow bypassing koa if ( false === ctx . respond ) return ; const res = ctx . res ; if ( ! ctx . writable ) return ; let body = ctx . body ; const code = ctx . status ; // ignore body if ( statuses . empty [ code ] ) { // strip headers ctx . body = null ; return res . end ( ) ; } if ( "HEAD" == ctx . method ) { if ( ! res . headersSent && isJSON ( body ) ) { ctx . length = Buffer . byteLength ( JSON . stringify ( body ) ) ; } return res . end ( ) ; } // status body if ( null == body ) { body = ctx . message || String ( code ) ; if ( ! res . headersSent ) { ctx . type = "text" ; ctx . length = Buffer . byteLength ( body ) ; } return res . end ( body ) ; } // responses if ( Buffer . isBuffer ( body ) ) return res . end ( body ) ; if ( "string" == typeof body ) return res . end ( body ) ; if ( body instanceof Stream ) return body . pipe ( res ) ; // body: json body = JSON . stringify ( body ) ; if ( ! res . headersSent ) { ctx . length = Buffer . byteLength ( body ) ; } res . end ( body ) ; }
Response helper.
copied from the original
Response helper . copied from the original