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