lib/cached-request.js
function makeRequest () {
    request = self.request.apply(null, args);
    requestMiddleware.use(request);
    request.on("response", function (response) {
      var contentEncoding, gzipper;

      //Only cache successful responses
      if (response.statusCode >= 200 && response.statusCode < 300) {
        response.on('error', function (error) {
          self.handleError(error);
        });
        fs.writeFile(self.cacheDirectory + key + ".json", JSON.stringify(response.headers), function (error) {
          if (error) self.handleError(error);
        });

        responseWriter = fs.createWriteStream(self.cacheDirectory + key);

        responseWriter.on('error', function (error) {
          self.handleError(error);
        });

        contentEncoding = response.headers['content-encoding'] || '';
        contentEncoding = contentEncoding.trim().toLowerCase();

        if (contentEncoding === 'gzip') {
          response.on('error', function (error) {
            responseWriter.end();
          });
          response.pipe(responseWriter);
        } else {
          gzipper = zlib.createGzip();
          response.on('error', function (error) {
            gzipper.end();
          });
          gzipper.on('error', function (error) {
            self.handleError(error);
            responseWriter.end();
          });
          responseWriter.on('error', function (error) {
            response.unpipe(gzipper);
            gzipper.end();
          });
          response.pipe(gzipper).pipe(responseWriter);
        }
      }
    });
    self.emit("request", args[0]);
  }
function makeRequest () {
    request = self.request.apply(null, args);
    requestMiddleware.use(request);
    request.on("response", function (response) {
      var contentEncoding, gzipper;

      //Only cache successful responses
      if (response.statusCode >= 200 && response.statusCode < 300) {
        response.on('error', function (error) {
          self.handleError(error);
        });
        fs.writeFile(self.cacheDirectory + key + ".json", JSON.stringify(response.headers), function (error) {
          if (error) self.handleError(error);
        });

        responseWriter = fs.createWriteStream(self.cacheDirectory + key);

        responseWriter.on('error', function (error) {
          self.handleError(error);
        });

        contentEncoding = response.headers['content-encoding'] || '';
        contentEncoding = contentEncoding.trim().toLowerCase();

        if (contentEncoding === 'gzip') {
          response.on('error', function (error) {
            responseWriter.end();
          });
          response.pipe(responseWriter);
        } else {
          gzipper = zlib.createGzip();
          response.on('error', function (error) {
            gzipper.end();
          });
          gzipper.on('error', function (error) {
            self.handleError(error);
            responseWriter.end();
          });
          responseWriter.on('error', function (error) {
            response.unpipe(gzipper);
            gzipper.end();
          });
          response.pipe(gzipper).pipe(responseWriter);
        }
      }
    });
    self.emit("request", args[0]);
  }
function makeRequest ( ) { request = self . request . apply ( null , args ) ; requestMiddleware . use ( request ) ; request . on ( "response" , function ( response ) { var contentEncoding , gzipper ; //Only cache successful responses if ( response . statusCode >= 200 && response . statusCode < 300 ) { response . on ( 'error' , function ( error ) { self . handleError ( error ) ; } ) ; fs . writeFile ( self . cacheDirectory + key + ".json" , JSON . stringify ( response . headers ) , function ( error ) { if ( error ) self . handleError ( error ) ; } ) ; responseWriter = fs . createWriteStream ( self . cacheDirectory + key ) ; responseWriter . on ( 'error' , function ( error ) { self . handleError ( error ) ; } ) ; contentEncoding = response . headers [ 'content-encoding' ] || '' ; contentEncoding = contentEncoding . trim ( ) . toLowerCase ( ) ; if ( contentEncoding === 'gzip' ) { response . on ( 'error' , function ( error ) { responseWriter . end ( ) ; } ) ; response . pipe ( responseWriter ) ; } else { gzipper = zlib . createGzip ( ) ; response . on ( 'error' , function ( error ) { gzipper . end ( ) ; } ) ; gzipper . on ( 'error' , function ( error ) { self . handleError ( error ) ; responseWriter . end ( ) ; } ) ; responseWriter . on ( 'error' , function ( error ) { response . unpipe ( gzipper ) ; gzipper . end ( ) ; } ) ; response . pipe ( gzipper ) . pipe ( responseWriter ) ; } } } ) ; self . emit ( "request" , args [ 0 ] ) ; }
Makes the actual request and caches the response
Makes the actual request and caches the response