UNPKG

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