nanoexpress/nanoexpress

View on GitHub
src/response-proto/http/response-chunks/modify-end.js

Summary

Maintainability
A
3 hrs
Test Coverage
export default function modifyEnd() {
  if (!this._modifiedEnd) {
    const _oldEnd = this.end;

    this.end = function modifiedEnd(chunk, encoding) {
      // eslint-disable-next-line prefer-const
      let { _headers, statusCode, rawStatusCode } = this;

      // Polyfill for express-session and on-headers module
      if (!this.writeHead.notModified) {
        this.writeHead(statusCode || rawStatusCode, _headers);
        this.writeHead.notModified = true;
        _headers = this._headers;
      }

      if (typeof statusCode === 'number' && statusCode !== rawStatusCode) {
        this.status(statusCode);
        statusCode = this.statusCode;
      }
      if (_headers) {
        this.applyHeadersAndStatus();
      }
      if (statusCode && statusCode !== rawStatusCode) {
        this.writeStatus(statusCode);
      }

      return encoding
        ? _oldEnd.call(this, chunk, encoding)
        : _oldEnd.call(this, chunk);
    };

    this._modifiedEnd = true;
  }
  return this;
}