Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add random timeout wrapper. #26

Open
wants to merge 1 commit into
base: v0.2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 39 additions & 35 deletions lib/requestify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var Requestify = (function() {
/**
* Module API
*/
api;
api,

randomTimeout;

/**
* Returns http|s instance according to the given protocol
Expand Down Expand Up @@ -93,47 +95,48 @@ var Requestify = (function() {
/**
* Handle request callback
*/
httpRequest = http.request(options, function(res) {
clearTimeout(timeout);
var response = new Response(res.statusCode, res.headers);

res.setEncoding(responseEncoding);
res.on('data', function(chunk) {
response.setChunk(chunk);
setTimeout(function() {
httpRequest = http.request(options, function (res) {
clearTimeout(timeout);
var response = new Response(res.statusCode, res.headers);

res.setEncoding(responseEncoding);
res.on('data', function (chunk) {
response.setChunk(chunk);
});

res.on('end', function () {
if (isSuccessful(response.code)) {
storeCache(request.getFullUrl(), response.getCode(), response.getHeaders(), response.body, request.cache);
defer.resolve(response);
return;
}

defer.reject(response);
});
});

res.on('end', function() {
if (isSuccessful(response.code)) {
storeCache(request.getFullUrl(), response.getCode(), response.getHeaders(), response.body, request.cache);
defer.resolve(response);
return;
}

defer.reject(response);
/**
* Abort and reject on timeout
*/
timeout = setTimeout(function() {
httpRequest.abort();
defer.reject(new Response(405, {}, 'timeout exceeded'));
}, request.timeout);

/**
* Reject on error and pass the given error object
*/
httpRequest.on('error', function(error) {
defer.reject(error);
});
});

/**
* Abort and reject on timeout
*/
timeout = setTimeout(function() {
httpRequest.abort();
defer.reject(new Response(405, {}, 'timeout exceeded'));
}, request.timeout);

/**
* Reject on error and pass the given error object
*/
httpRequest.on('error', function(error) {
defer.reject(error);
});

httpRequest.end(request.getBody());
httpRequest.end(request.getBody());
}, randomTimeout);

return defer.promise;
}


/**
* Request router, handles caching
* @param {Request} request
Expand Down Expand Up @@ -177,7 +180,8 @@ var Requestify = (function() {
* @param {{ method: string, dataType: string, headers: object, body: object, cookies: object, auth: object }} options
* @returns {Q.promise} - Returns a promise, once resolved || rejected, Response object is given
*/
request: function(url, options) {
request: function(url, options, timeout) {
randomTimeout = timeout;
return callRouter(new Request(url, options));
},

Expand Down