How to use the statuses.empty function in statuses

To help you get started, we’ve selected a few statuses examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github metrue / fx / images / node / node_modules / koa / lib / response.js View on Github external
set body(val) {
    const original = this._body;
    this._body = val;

    if (this.res.headersSent) return;

    // no content
    if (null == val) {
      if (!statuses.empty[this.status]) this.status = 204;
      this.remove('Content-Type');
      this.remove('Content-Length');
      this.remove('Transfer-Encoding');
      return;
    }

    // set the status
    if (!this._explicitStatus) this.status = 200;

    // set the content-type only if not yet set
    const setType = !this.header['content-type'];

    // string
    if ('string' == typeof val) {
      if (setType) this.type = /^\s*
github koajs / compress / index.js View on Github external
return async (ctx, next) => {
    ctx.vary('Accept-Encoding')

    await next()

    let { body } = ctx
    if (!body) return
    if (ctx.res.headersSent || !ctx.writable) return
    if (ctx.compress === false) return
    if (ctx.request.method === 'HEAD') return
    if (status.empty[ctx.response.status]) return
    if (ctx.response.get('Content-Encoding')) return

    // forced compression or implied
    if (!(ctx.compress === true || filter(ctx.response.type))) return

    // identity
    const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
    if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
    if (encoding === 'identity') return

    // json
    if (isJSON(body)) body = ctx.body = JSON.stringify(body)

    // threshold
    if (threshold && ctx.response.length < threshold) return
github polixjs / polix / extends / application.js View on Github external
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) {
github andywer / srv / src / core / response.ts View on Github external
const headers = response.headers || {}

  res.statusCode = response.status
  res.statusMessage = statuses[response.status] || res.statusMessage

  // Using for..in here, since it's faster than Object.keys()
  for (const headerName in headers) {
    const headerValue = headers[headerName]
    if (typeof headerValue !== "undefined") {
      res.setHeader(headerName, headerValue)
    }
  }

  if (body && statuses.empty[response.status]) {
    debug(Error(`Warning: Response body defined on a ${response.status} response.`))
  } else if (!body && !statuses.empty[response.status]) {
    debug(Error(`Warning: No response body defined on a ${response.status} response.`))
  }

  if (Buffer.isBuffer(body)) {
    return res.end(body)
  } else if (isStream(body)) {
    return body.pipe(res)
  } else if (body) {
    throw Error(`Unexpected Response.body: ${body}`)
  } else {
    return res.end()
  }
}
github andywer / srv / src / core / response.ts View on Github external
export function applyResponseTo(response: Response, res: http.ServerResponse) {
  const body = response.body
  const headers = response.headers || {}

  res.statusCode = response.status
  res.statusMessage = statuses[response.status] || res.statusMessage

  // Using for..in here, since it's faster than Object.keys()
  for (const headerName in headers) {
    const headerValue = headers[headerName]
    if (typeof headerValue !== "undefined") {
      res.setHeader(headerName, headerValue)
    }
  }

  if (body && statuses.empty[response.status]) {
    debug(Error(`Warning: Response body defined on a ${response.status} response.`))
  } else if (!body && !statuses.empty[response.status]) {
    debug(Error(`Warning: No response body defined on a ${response.status} response.`))
  }

  if (Buffer.isBuffer(body)) {
    return res.end(body)
  } else if (isStream(body)) {
    return body.pipe(res)
  } else if (body) {
    throw Error(`Unexpected Response.body: ${body}`)
  } else {
    return res.end()
  }
}
github rill-js / rill / src / respond.js View on Github external
// Skip request ended externally.
  if (original.headersSent) return

  // Apply default statuses.
  if (Number(res.status) === 404) {
    // Ensure redirect status.
    if (res.get('Location')) res.status = 302
    // Default the status to 200 if there is substance to the response.
    else if (body) res.status = 200
  }

  // Default status message based on status code.
  res.message = res.message || statuses[res.status]

  // Ensure no content-type for empty responses.
  if (req.method === 'HEAD' || statuses.empty[res.status] || !body) {
    body = null
    res.remove('Content-Type')
    res.remove('Content-Length')
  } else {
    // Attempt to guess content type.
    if (!res.get('Content-Type')) res.set('Content-Type', checkType(body))
    // Stringify objects that are not buffers.
    if (typeof body === 'object' && !isStream && !isBuffer) body = JSON.stringify(body)
    // Attempt to guess content-length.
    if (!res.get('Content-Length') && !isStream) res.set('Content-Length', byteLength(body))
  }

  // Send off headers.
  original.writeHead(res.status, res.message, removeEmptyHeaders(res.headers))
  // Allow for requests to stay open.
  if (res.end === false) return
github koajs / koa / lib / response.js View on Github external
set status(code) {
    if (this.headerSent) return;

    assert(Number.isInteger(code), 'status code must be a number');
    assert(code >= 100 && code <= 999, `invalid status code: ${code}`);
    this._explicitStatus = true;
    this.res.statusCode = code;
    if (this.req.httpVersionMajor < 2) this.res.statusMessage = statuses[code];
    if (this.body && statuses.empty[code]) this.body = null;
  },
github metrue / fx / images / node / node_modules / koa / lib / response.js View on Github external
set status(code) {
    assert('number' == typeof code, 'status code must be a number');
    assert(statuses[code], `invalid status code: ${code}`);
    assert(!this.res.headersSent, 'headers have already been sent');
    this._explicitStatus = true;
    this.res.statusCode = code;
    this.res.statusMessage = statuses[code];
    if (this.body && statuses.empty[code]) this.body = null;
  },
github slashhuang / cuty / src / response.js View on Github external
set status(code) {
    assert('number' == typeof code, 'status code must be a number');
    assert(statuses[code], `invalid status code: ${code}`);
    assert(!this.res.headersSent, 'headers have already been sent');
    this._explicitStatus = true;
    this.res.statusCode = code;
    this.res.statusMessage = statuses[code];
    if (this.body && statuses.empty[code]) this.body = null;
  },
github koajs / compressor / index.js View on Github external
return function* compressor(next) {
    yield* next

    var body = this.response.body
    if (!body) return
    if (this.request.method === 'HEAD') return
    if (status.empty[this.response.status]) return
    if (this.response.get('Content-Encoding')) return
    if (!filter(this.response.type)) return
    if (isJSON(body)) body = this.body = JSON.stringify(body)
    if (this.response.length < threshold) return

    this.response.set('Content-Encoding', 'gzip')
    this.response.remove('Content-Length')

    this.body = typeof body.pipe === 'function'
      ? body.pipe(zlib.Gzip(options))
      : (yield zlib.gzip(body))
  }
}

statuses

HTTP status utility

MIT
Latest version published 4 years ago

Package Health Score

71 / 100
Full package analysis