forked from request/request
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request request#767 from stash/stash/sync-cookie-api
Use tough-cookie CookieJar sync API
- Loading branch information
Showing
7 changed files
with
59 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var optional = require('./optional') | ||
, tough = optional('tough-cookie') | ||
, Cookie = tough && tough.Cookie | ||
, CookieJar = tough && tough.CookieJar | ||
; | ||
|
||
exports.parse = function(str) { | ||
if (str && str.uri) str = str.uri | ||
if (typeof str !== 'string') throw new Error("The cookie function only accepts STRING as param") | ||
if (!Cookie) { | ||
return null; | ||
} | ||
return Cookie.parse(str) | ||
}; | ||
|
||
// Adapt the sometimes-Async api of tough.CookieJar to our requirements | ||
function RequestJar() { | ||
this._jar = new CookieJar(); | ||
} | ||
RequestJar.prototype.setCookie = function(cookieOrStr, uri) { | ||
return this._jar.setCookieSync(cookieOrStr, uri); | ||
}; | ||
RequestJar.prototype.getCookieString = function(uri) { | ||
return this._jar.getCookieStringSync(uri); | ||
}; | ||
|
||
exports.jar = function() { | ||
if (!CookieJar) { | ||
// tough-cookie not loaded, return a stub object: | ||
return { | ||
setCookie: function(){}, | ||
getCookieString: function(){} | ||
}; | ||
} | ||
return new RequestJar(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters