Example #1
0
 // intercept errors here in case we want to retry the request
 function handleError(error) {
     if (error && options.retriesRemaining > 0) {
         // options = {...options, retriesRemaining: options.retriesRemaining - 1}
         options.retriesRemaining--;
         if (error instanceof TwitterError) {
             loge_1.logger.warning(error.toString() + "... retrying");
             return request(options, callback);
         }
         else if (error['code'] == 'ETIMEDOUT') {
             loge_1.logger.warning("ETIMEDOUT: " + error.toString() + "... retrying");
             return request(options, callback);
         }
         else if (error['code'] == 'ECONNRESET') {
             loge_1.logger.warning("ECONNRESET: " + error.toString() + "... retrying");
             return request(options, callback);
         }
     }
     callback(error);
 }
Example #2
0
 streaming_1.readToEnd(response, function (error, chunks) {
     if (error)
         return callback(error);
     var body = chunks.join('');
     var statusLookup = JSON.parse(body).id;
     // statuses_map[id_str] will be null for all requested statuses that can't be found
     var statuses = id_strs.map(function (id_str) { return statusLookup[id_str] || { id_str: id_str, missing: true }; });
     loge_1.logger.debug("Fetched " + statuses.length + " statuses");
     callback(null, statuses);
 });
Example #3
0
 }, function (error, response) {
     if (error) {
         _this.emit('error', error);
         _this.push(null);
         return;
     }
     // Decompress if needed. This must run from within the response
     // listener since we need the response headers.
     var outputStream = response;
     var encoding = response.headers['content-encoding'];
     if (encoding == 'gzip') {
         loge_1.logger.debug('gunzipping HTTP response');
         var gunzip = zlib_1.createGunzip();
         outputStream = outputStream.pipe(gunzip);
     }
     else if (encoding == 'deflate') {
         loge_1.logger.debug('inflating HTTP response');
         var inflate = zlib_1.createInflate();
         outputStream = outputStream.pipe(gunzip);
     }
     outputStream.pipe(_this);
 });
Example #4
0
 credentials_1.getOAuth(function (error, oauth) {
     if (error)
         return callback(error);
     var httpRequestOptions = {
         url: url_1.format({
             protocol: 'https:',
             slashes: true,
             host: options.host,
             pathname: "/1.1/" + options.endpoint + ".json",
             query: options.query,
         }),
         headers: options.headers,
         form: options.form,
         method: options.method,
         timeout: 10000,
         oauth: oauth,
     };
     loge_1.logger.debug("httpRequest(" + JSON.stringify(httpRequestOptions) + ")");
     httpRequest(httpRequestOptions)
         .on('response', function (response) {
         // we don't want to treat all non-200 responses as errors; for example,
         // with /users/lookup, a 404 indicates that there were no matches at all.
         if (lodash_1.includes(exports.TwitterAPIFatalHTTPStatusCodes, response.statusCode)) {
             // Twitter error responses look like:
             // {"errors":[{"message":"Sorry, that page does not exist","code":34}]}
             readJSON(response, function (error, body) {
                 if (error)
                     return handleError(error);
                 var errorMessages = body.errors.map(function (_a) {
                     var message = _a.message;
                     return message;
                 });
                 handleError(new TwitterError(response.statusCode, errorMessages));
             });
         }
         else {
             callback(null, response);
         }
     })
         .on('error', handleError);
 });
Example #5
0
function request(options, callback) {
    lodash_1.defaults(options, { host: 'api.twitter.com', method: 'GET', retriesRemaining: 10, query: {} });
    // delete undefined query values
    options.query = compactObject(options.query);
    loge_1.logger.debug("request(" + JSON.stringify(options) + ")");
    // exit quickly if the endpoint is malformed
    var twitterAPIEndpoint = lodash_1.find(codes_1.TwitterAPIEndpoints, function (_a) {
        var regExp = _a.regExp;
        return regExp.test(options.endpoint);
    });
    if (twitterAPIEndpoint === undefined) {
        return callback(new Error("\"" + options.endpoint + "\" is not a valid Twitter API Endpoint"));
    }
    // intercept errors here in case we want to retry the request
    function handleError(error) {
        if (error && options.retriesRemaining > 0) {
            // options = {...options, retriesRemaining: options.retriesRemaining - 1}
            options.retriesRemaining--;
            if (error instanceof TwitterError) {
                loge_1.logger.warning(error.toString() + "... retrying");
                return request(options, callback);
            }
            else if (error['code'] == 'ETIMEDOUT') {
                loge_1.logger.warning("ETIMEDOUT: " + error.toString() + "... retrying");
                return request(options, callback);
            }
            else if (error['code'] == 'ECONNRESET') {
                loge_1.logger.warning("ECONNRESET: " + error.toString() + "... retrying");
                return request(options, callback);
            }
        }
        callback(error);
    }
    credentials_1.getOAuth(function (error, oauth) {
        if (error)
            return callback(error);
        var httpRequestOptions = {
            url: url_1.format({
                protocol: 'https:',
                slashes: true,
                host: options.host,
                pathname: "/1.1/" + options.endpoint + ".json",
                query: options.query,
            }),
            headers: options.headers,
            form: options.form,
            method: options.method,
            timeout: 10000,
            oauth: oauth,
        };
        loge_1.logger.debug("httpRequest(" + JSON.stringify(httpRequestOptions) + ")");
        httpRequest(httpRequestOptions)
            .on('response', function (response) {
            // we don't want to treat all non-200 responses as errors; for example,
            // with /users/lookup, a 404 indicates that there were no matches at all.
            if (lodash_1.includes(exports.TwitterAPIFatalHTTPStatusCodes, response.statusCode)) {
                // Twitter error responses look like:
                // {"errors":[{"message":"Sorry, that page does not exist","code":34}]}
                readJSON(response, function (error, body) {
                    if (error)
                        return handleError(error);
                    var errorMessages = body.errors.map(function (_a) {
                        var message = _a.message;
                        return message;
                    });
                    handleError(new TwitterError(response.statusCode, errorMessages));
                });
            }
            else {
                callback(null, response);
            }
        })
            .on('error', handleError);
    });
}
Example #6
0
.on('listening', function() {
  var address = this.address();
  logger.info('server listening on http://%s:%d', address.address, address.port);
});
Example #7
0
module.exports = http.createServer(function(req, res) {
  logger.debug('%s %s', req.method, req.url);
  controllers(req, res);
})