How to use tough-cookie

Comprehensive tough-cookie code examples:

How to use tough-cookie.sort:

95
96
97
98
99
100
101
102
103
104

For use with `.sort()`, sorts a list of cookies into the recommended order given in the RFC (Section 5.4 step 2).  Longest `.path`s go first, then sorted oldest to youngest.

``` javascript
var cookies = [ /* unsorted array of Cookie objects */ ];
cookies = cookies.sort(cookieCompare);
```

permuteDomain(domain)
---------------------

How to use tough-cookie.join:

12
13
14
15
16
17
18
19
20
21

var cookiejar = new cookies.CookieJar();
cookiejar.setCookie(cookie, 'http://currentdomain.example.com/path', cb);
// ...
cookiejar.getCookies('http://example.com/otherpath',function(err,cookies) {
  res.headers['cookie'] = cookies.join('; ');
});
```

# Installation

How to use tough-cookie.defaultPath:

17
18
19
20
21
22
23
24
25
26
MyRequest.prototype.clearCookie = function (url) {
  if (this.j.store) {
    //memstore
    var context = urlParse(url);
    var host = tough.canonicalDomain(context.hostname);
    this.j.store.removeCookies(host, tough.defaultPath(context.pathname), function () {});
  }
};

MyRequest.prototype.setCookie = function (str_cookie, url) {

How to use tough-cookie.canonicalDomain:

16
17
18
19
20
21
22
23
24

MyRequest.prototype.clearCookie = function (url) {
  if (this.j.store) {
    //memstore
    var context = urlParse(url);
    var host = tough.canonicalDomain(context.hostname);
    this.j.store.removeCookies(host, tough.defaultPath(context.pathname), function () {});
  }
};

How to use tough-cookie.fromJSON:

149
150
151
152
153
154
155
156
157
158
var data = fs.readFileSync(filePath, 'utf8');
var dataJson = data ? JSON.parse(data) : null;
for(var domainName in dataJson) {
    for(var pathName in dataJson[domainName]) {
        for(var cookieName in dataJson[domainName][pathName]) {
            dataJson[domainName][pathName][cookieName] = tough.fromJSON(JSON.stringify(dataJson[domainName][pathName][cookieName]));
        }
    }
}
cb(dataJson);

How to use tough-cookie.Store:

How to use tough-cookie.permutePath:

3
4
5
6
7
8
9
10
11
12
var UTIL = require('util');
var Q = require('q');
var TOUGH = require('tough-cookie');
var canonicalDomain = TOUGH.canonicalDomain;
var permuteDomain = TOUGH.permuteDomain;
var permutePath = TOUGH.permutePath;
var LOCKFILE = require('lockfile');

function isString(str) {
    return typeof str === 'string' || str instanceof String;

How to use tough-cookie.permuteDomain:

2
3
4
5
6
7
8
9
10
11
var fs = require('fs');
var UTIL = require('util');
var Q = require('q');
var TOUGH = require('tough-cookie');
var canonicalDomain = TOUGH.canonicalDomain;
var permuteDomain = TOUGH.permuteDomain;
var permutePath = TOUGH.permutePath;
var LOCKFILE = require('lockfile');

function isString(str) {

How to use tough-cookie.Cookie:

7
8
9
10
11
12
13
14
15
16

## Synopsis

```javascript
var tough = require("tough-cookie");
var Cookie = tough.Cookie;
var cookie = Cookie.parse(header);
cookie.value = "somethingdifferent";
header = cookie.toString();
var cookiejar = new tough.CookieJar();

How to use tough-cookie.CookieJar:

11
12
13
14
15
16
17
18
19
20
var tough = require("tough-cookie");
var Cookie = tough.Cookie;
var cookie = Cookie.parse(header);
cookie.value = "somethingdifferent";
header = cookie.toString();
var cookiejar = new tough.CookieJar();

// Asynchronous!
var cookie = await cookiejar.setCookie(
  cookie,