UNPKG

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