cookies.js 969 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict'
  2. var tough = require('tough-cookie')
  3. var Cookie = tough.Cookie
  4. , CookieJar = tough.CookieJar
  5. exports.parse = function(str) {
  6. if (str && str.uri) {
  7. str = str.uri
  8. }
  9. if (typeof str !== 'string') {
  10. throw new Error('The cookie function only accepts STRING as param')
  11. }
  12. return Cookie.parse(str, {loose: true})
  13. }
  14. // Adapt the sometimes-Async api of tough.CookieJar to our requirements
  15. function RequestJar(store) {
  16. var self = this
  17. self._jar = new CookieJar(store, {looseMode: true})
  18. }
  19. RequestJar.prototype.setCookie = function(cookieOrStr, uri, options) {
  20. var self = this
  21. return self._jar.setCookieSync(cookieOrStr, uri, options || {})
  22. }
  23. RequestJar.prototype.getCookieString = function(uri) {
  24. var self = this
  25. return self._jar.getCookieStringSync(uri)
  26. }
  27. RequestJar.prototype.getCookies = function(uri) {
  28. var self = this
  29. return self._jar.getCookiesSync(uri)
  30. }
  31. exports.jar = function(store) {
  32. return new RequestJar(store)
  33. }