All files http-duplex.ts

89.54% Statements 257/287
100% Branches 19/19
53.57% Functions 15/28
89.54% Lines 257/287

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 2881x 1x 1x 1x 1x     1x     1x     1x     1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 29x 29x 29x 29x 29x 29x 116x 29x 29x 29x 29x 58x 29x 29x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 9x     9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 6x 6x 30x 1x 1x 1x 1x 1x 1x        
import http from 'http';
import EventEmitter from 'events';
 
export class HttpDuplex extends EventEmitter {
  setHeader(arg0: string, arg1: string) {
    throw new Error('Method not implemented.');
  }
  end(reason?: any) {
    throw new Error('Method not implemented.');
  }
  destroy() {
    throw new Error('Method not implemented.');
  }
  accept() {
    throw new Error('Method not implemented.');
  }
  reject(code: number, msg: string) {
    throw new Error('Method not implemented.');
  }
 
  /**
   * A IncomingMessage created by http.Server or http.ClientRequest usually passed as the
   * first parameter to the 'request' and 'response' events. Implements Readable Stream interface
   * but may not be a decendant thereof.
   * @see {@link https://nodejs.org/api/http.html#http_event_request|request}
   * @see {@link https://nodejs.org/api/http.html#http_class_http_incomingmessage|http.IncomingMessage}
   *
   */
  req: http.IncomingMessage;
 
  /**
   * Created http.server. Passed as the second parameter to the 'request' event.
   * The response implements Writable Stream interface but isn't a descendent thereof.
   * @see {@link https://nodejs.org/api/http.html#http_event_request|request}
   * @see {@link https://nodejs.org/api/http.html#http_class_http_serverresponse|http.ServerResponse}
   */
  res: http.ServerResponse;
  cwd: string | undefined;
  repo: string | undefined;
  exists: boolean | undefined;
 
  /**
   * Constructs a proxy object over input and output resulting in a unified stream.
   * Generally meant to combine request and response streams in the http.request event
   * @see {@link https://nodejs.org/api/http.html#http_event_request|request}
   * @see {@link https://nodejs.org/api/http.html#http_class_http_incomingmessage|http.IncomingMessage}
   * @see {@link https://nodejs.org/api/http.html#http_class_http_serverresponse|http.ServerResponse}
   * @example <caption> A simple example is shown below </caption>
 
    ```js
    http.createServer(function (req, res) {
        var dup = new HttpDuplex(req, res);
        res.end("Request: " + req.method + " " + req.url);
    }).listen(80);
    ```
   */
  constructor(input: http.IncomingMessage, output: http.ServerResponse) {
    super();
 
    this.req = input;
    this.res = output;
 
    // request / input proxy events
    ['data', 'end', 'error', 'close'].forEach((name) => {
      this.req.on(name, this.emit.bind(this, name));
    });
 
    // respone / output proxy events
    ['error', 'drain'].forEach((name) => {
      this.res.on(name, this.emit.bind(this, name));
    });
  }
 
  get complete() {
    return this.req.complete;
  }
 
  /**
   * Reference to the underlying socket for the request connection.
   * @readonly
   * @see {@link https://nodejs.org/api/http.html#http_request_socket|request.Socket}
   */
  get connection() {
    return this.req.connection;
  }
 
  /**
   * Request/response headers. Key-value pairs of header names and values. Header names are always lower-case.
   * @readonly
   * @see {@link https://nodejs.org/api/http.html#http_message_headers|message.headers}
   */
  get headers() {
    return this.req.headers;
  }
 
  /**
   * Requested HTTP Version sent by the client. Usually either '1.0' or '1.1'
   * @see {@link https://nodejs.org/api/http.html#http_message_httpversion|message.httpVersion}
   * @readonly
   */
  get httpVersion() {
    return this.req.httpVersion;
  }
 
  /**
   * First integer in the httpVersion string
   * @see httpVersion
   * @readonly
   */
  get httpVersionMajor() {
    return this.req.httpVersionMajor;
  }
 
  /**
   * Second integer ni the httpVersion string
   * @see httpVersion
   * @readonly
   */
  get httpVersionMinor() {
    return this.req.httpVersionMinor;
  }
 
  /**
   * Request method of the incoming request.
   * @see {@link https://nodejs.org/api/http.html#http_event_request|request}
   * @see {@link https://nodejs.org/api/http.html#http_class_http_serverresponse|http.ServerResponse}
   * @example 'GET', 'DELETE'
   * @readonly
   */
  get method() {
    return this.req.method;
  }
 
  /**
   * Is this stream readable.
   * @readonly
   */
  get readable() {
    return this.req.readable;
  }
 
  /**
   * net.Socket object associated with the connection.
   * @see {@link https://nodejs.org/api/net.html#net_class_net_socket|net.Socket}
   * @readonly
   */
  get socket() {
    return this.req.socket;
  }
 
  /**
   * The HTTP status code. Generally assigned before sending headers for a response to a client.
   * @see {@link https://nodejs.org/api/http.html#http_response_statuscode|response.statusCode}
   * @example request.statusCode = 404;
   */
  get statusCode() {
    return this.res.statusCode;
  }
 
  set statusCode(val) {
    this.res.statusCode = val;
  }
 
  /**
   * Controls the status message sent to the client as long as an explicit call to response.writeHead() isn't made
   * If ignored or the value is undefined, the default message corresponding to the status code will be used.
   * @see {@link https://nodejs.org/api/http.html#http_response_statusmessage|response.statusMessage}
   * @example request.statusMessage = 'Document Not found';
   */
  get statusMessage() {
    return this.res.statusMessage;
  }
 
  set statusMessage(val) {
    this.res.statusMessage = val;
  }
 
  /**
   * Request/response trailer headers. Just like {@link headers} except these are only written
   * after the initial response to the client.
   * This object is only populated at the 'end' event and only work if a 'transfer-encoding: chunked'
   * header is sent in the initial response.
   * @readonly
   * @see headers
   * @see addTrailers
   * @see {@link https://nodejs.org/api/http.html#http_message_trailers|message.trailers}
   * @see {@link https://nodejs.org/api/http.html#http_response_addtrailers_headers|response.addTrailers}
   */
  get trailers() {
    return this.req.trailers;
  }
 
  /**
   * Request URL string.
   * @example <caption>A request made as:</caption>
   * GET /info?check=none HTTP/1.1
   * @example <caption>Will return the string</caption>
   * '/info?check=none'
   * @readonly
   */
  get url() {
    return this.req.url;
  }
 
  // output / response wrapping
  get writable() {
    return this.res.writable;
  }
 
  /**
   * Sends a response header to the client request. Must only be called one time and before calling response.end().
   * @param statusCode 3-digit HTTP status code, like 404
   * @param statusMessage - An optional human readable status message to send with the status code
   * @param headers - An object containing the response headers to send
   * @see {@link https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers|response.writeHead}
   * @example var content = 'Under Construction...';
   * response.writeHead(200, {
   *     'Content-Length': Buffer.byteLength(content),
   *     'Content-Type': 'text/plain'
   * });
   * response.end(content);
   */
  writeHead(statusCode: number, statusMessage: string, headers: string[]) {
    this.res.writeHead(statusCode, statusMessage, headers);
    return this;
  }
 
  /**
   * Buffers written data in memory. This data will be flushed when either the uncork or end methods are called.
   * @see uncork
   * @see {@link https://nodejs.org/api/stream.html#stream_writable_cork|stream.Writeable.cork}
   * @example
   * request.cork();
   * request.write('buffer data ');
   * request.write('before sending ');
   * request.uncork();
   */
  cork() {
    this.res.socket?.cork();
    return this;
  }
 
  /**
   * Flushes all data buffered since cork() was called.
   * @see cork
   * @see {@link https://nodejs.org/api/stream.html#stream_writable_uncork|stream.Writeable.uncork}
   */
  uncork() {
    this.res.socket?.uncork();
    return this;
  }
}
 
// proxy request methods
['pause', 'resume', 'setEncoding'].forEach(function (name) {
  (HttpDuplex.prototype as any)[name] = function () {
    // eslint-disable-next-line prefer-rest-params
    return (this.req as any)[name].apply(this.req, Array.from(arguments));
  };
});
 
// proxy respone methods
[
  'setDefaultEncoding',
  'write',
  'end',
  'flush',
  'writeHeader',
  'writeContinue',
  'setHeader',
  'getHeader',
  'removeHeader',
  'addTrailers',
].forEach(function (name) {
  (HttpDuplex.prototype as any)[name] = function () {
    // eslint-disable-next-line prefer-rest-params
    return (this.res as any)[name].apply(this.res, Array.from(arguments));
  };
});
 
/**
 * Destroys object and it's bound streams
 */
HttpDuplex.prototype.destroy = function () {
  this.req.destroy();
  this.res.destroy();
};