1 | 'use strict'
|
2 |
|
3 | var tough = require('tough-cookie')
|
4 |
|
5 | var Cookie = tough.Cookie
|
6 | var CookieJar = tough.CookieJar
|
7 |
|
8 | exports.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 |
|
19 | function RequestJar (store) {
|
20 | var self = this
|
21 | self._jar = new CookieJar(store, {looseMode: true})
|
22 | }
|
23 | RequestJar.prototype.setCookie = function (cookieOrStr, uri, options) {
|
24 | var self = this
|
25 | return self._jar.setCookieSync(cookieOrStr, uri, options || {})
|
26 | }
|
27 | RequestJar.prototype.getCookieString = function (uri) {
|
28 | var self = this
|
29 | return self._jar.getCookieStringSync(uri)
|
30 | }
|
31 | RequestJar.prototype.getCookies = function (uri) {
|
32 | var self = this
|
33 | return self._jar.getCookiesSync(uri)
|
34 | }
|
35 |
|
36 | exports.jar = function (store) {
|
37 | return new RequestJar(store)
|
38 | }
|