# tough-cookie
[RFC 6265](https://tools.ietf.org/html/rfc6265) Cookies and CookieJar for Node.js
[![npm package](https://nodei.co/npm/tough-cookie.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/tough-cookie/)
[![Build Status](https://travis-ci.org/salesforce/tough-cookie.svg?branch=master)](https://travis-ci.org/salesforce/tough-cookie)
## 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();
// Asynchronous!
var cookie = await cookiejar.setCookie(
cookie,
"https://currentdomain.example.com/path"
);
var cookies = await cookiejar.getCookies("https://example.com/otherpath");
// Or with callbacks!
cookiejar.setCookie(
cookie,
"https://currentdomain.example.com/path",
function (err, cookie) {
/* ... */
}
);
cookiejar.getCookies("http://example.com/otherpath", function (err, cookies) {
/* ... */
});
```
Why the name? NPM modules `cookie`, `cookies` and `cookiejar` were already taken.
## Installation
It's _so_ easy! Install with `npm` or your preferred package manager.
```sh
npm install tough-cookie
```
## Node.js Version Support
We follow the [node.js release schedule](https://github.com/nodejs/Release#release-schedule) and support all versions that are in Active LTS or Maintenance. We will always do a major release when dropping support for older versions of node, and we will do so in consultation with our community.
## API
### tough
The top-level exports from `require('tough-cookie')` can all be used as pure functions and don't need to be bound.
#### `parseDate(string)`
Parse a cookie date string into a `Date`. Parses according to [RFC 6265 Section 5.1.1](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.1), not `Date.parse()`.
#### `formatDate(date)`
Format a `Date` into an [RFC 822](https://datatracker.ietf.org/doc/html/rfc822#section-5) string (the RFC 6265 recommended format).
#### `canonicalDomain(str)`
Transforms a domain name into a canonical domain name. The canonical domain name is a domain name that has been trimmed, lowercased, stripped of leading dot, and optionally punycode-encoded ([Section 5.1.2 of RFC 6265](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.2)). For the most part, this function is idempotent (calling the function with the output from a previous call returns the same output).
#### `domainMatch(str, domStr[, canonicalize=true])`
Answers "does this real domain match the domain in a cookie?". The `str` is the "current" domain name and the `domStr` is the "cookie" domain name. Matches according to [RFC 6265 Section 5.1.3](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.3), but it helps to think of it as a "suffix match".
The `canonicalize` parameter toggles whether the domain parameters get normalized with `canonicalDomain` or not.
#### `defaultPath(path)`
Given a current request/response path, gives the path appropriate for storing in a cookie. This is basically the "directory" of a "file" in the path, but is specified by [Section 5.1.4 of the RFC](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4).
The `path` parameter MUST be _only_ the pathname part of a URI (excluding the hostname, query, fragment, and so on). This is the `.pathname` property of node's `uri.parse()` output.
#### `pathMatch(reqPath, cookiePath)`
Answers "does the request-path path-match a given cookie-path?" as per [RFC 6265 Section 5.1.4](https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4). Returns a boolean.
This is essentially a prefix-match where `cookiePath` is a prefix of `reqPath`.
#### `parse(cookieString[, options])`
Alias for [`Cookie.parse(cookieString[, options])`](#cookieparsecookiestring-options).
#### `fromJSON(string)`
Alias for [`Cookie.fromJSON(string)`](#cookiefromjsonstrorobj).
#### `getPublicSuffix(hostname)`
Returns the public suffix of this hostname. The public suffix is the shortest domain name upon which a cookie can be set. Returns `null` if the hostname cannot have cookies set for it.
For example: `www.example.com` and `www.subdomain.example.com` both have public suffix `example.com`.
For further information, see the [Public Suffix List](http://publicsuffix.org/). This module derives its list from that site. This call is a wrapper around [`psl`](https://www.npmjs.com/package/psl)'s [`get` method](https://www.npmjs.com/package/psl##pslgetdomain).
#### `cookieCompare(a, b)`
For use with `.sort()`, sorts a list of cookies into the recommended order given in step 2 of ([RFC 6265 Section 5.4](https://datatracker.ietf.org/doc/html/rfc6265#section-5.4)). The sort algorithm is, in order of precedence:
- Longest `.path`
- oldest `.creation` (which has a 1-ms precision, same as `Date`)
- lowest `.creationIndex` (to get beyond the 1-ms precision)
```javascript
var cookies = [
/* unsorted array of Cookie objects */
];
cookies = cookies.sort(cookieCompare);
```
> **Note**: Since the JavaScript `Date` is limited to a 1-ms precision, cookies within the same millisecond are entirely possible. This is especially true when using the `now` option to `.setCookie()`. The `.creationIndex` property is a per-process global counter, assigned during construction with `new Cookie()`, which preserves the spirit of the RFC sorting: older cookies go first. This works great for `MemoryCookieStore` since `Set-Cookie` headers are parsed in order, but is not so great for distributed systems. Sophisticated `Store`s may wish to set this to some other _logical clock_ so that if cookies A and B are created in the same millisecond, but cookie A is created before cookie B, then `A.creationIndex < B.creationIndex`. If you want to alter the global counter, which you probably _shouldn't_ do, it's stored in `Cookie.cookiesCreated`.
#### `permuteDomain(domain)`
Generates a list of all possible domains that `domainMatch()` the parameter. Can be handy for implementing cookie stores.
#### `permutePath(path)`
Generates a list of all possible paths that `pathMatch()` the parameter. Can be handy for implementing cookie stores.
### Cookie
Exported via `tough.Cookie`.
#### `Cookie.parse(cookieString[, options])`
Parses a single Cookie or Set-Cookie HTTP header into a `Cookie` object. Returns `undefined` if the string can't be parsed.
The options parameter is not required and currently has only one property:
- _loose_ - boolean - if `true` enable parsing of keyless cookies like `=abc` and `=`, which are not RFC-compliant.
If options is not an object it is ignored, which means it can be used with [`Array#map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
To process the Set-Cookie header(s) on a node HTTP/HTTPS response:
```javascript
if (Array.isArray(res.headers["set-cookie"]))
cookies = res.headers["set-cookie"].map(Cookie.parse);
else cookies = [Cookie.parse(res.headers["set-cookie"])];
```
_Note:_ In version 2.3.3, tough-cookie limited the number of spaces before the `=` to 256 characters. This limitation was removed in version 2.3.4.
For more details, see [issue #92](https://github.com/salesforce/tough-cookie/issues/92).
#### Properties
Cookie object properties:
- _key_ - string - the name or key of the cookie (default `""`)
- _value_ - string - the value of the cookie (default `""`)
- _expires_ - `Date` - if set, the `Expires=` attribute of the cookie (defaults to the string `"Infinity"`). See `setExpires()`
- _maxAge_ - seconds - if set, the `Max-Age=` attribute _in seconds_ of the cookie. Can also be set to strings `"Infinity"` and `"-Infinity"` for non-expiry and immediate-expiry, respectively. See `setMaxAge()`
- _domain_ - string - the `Domain=` attribute of the cookie
- _path_ - string - the `Path=` of the cookie
- _secure_ - boolean - the `Secure` cookie flag
- _httpOnly_ - boolean - the `HttpOnly` cookie
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
小红书爬虫源码,使用Python编写,下载即可运行,可做毕业设计 (1080个子文件)
README.md.bak 12KB
.env 0B
nwsapi.js.focus-visible 63KB
.gitignore 19B
qq.jpg 301KB
zfb_pay.jpg 267KB
psl.js 158KB
psl.min.js 134KB
Document.js 133KB
decimal.js 128KB
index.js 113KB
index.js 112KB
info.js 111KB
index.js 104KB
index.js 103KB
HTMLElement.js 96KB
SVGElement.js 86KB
saxes.js 72KB
xpath.js 69KB
regexes.js 66KB
nwsapi.js 63KB
HTMLInputElement.js 59KB
Element.js 57KB
properties.js 56KB
cookie.js 50KB
decode-data-html.js 47KB
decode-data-html.js 47KB
HTMLTextAreaElement.js 38KB
HTMLInputElement-impl.js 37KB
Node-impl.js 34KB
websocket.js 33KB
HTMLAnchorElement.js 32KB
XMLHttpRequest-impl.js 32KB
HTMLSelectElement.js 31KB
sbcs-data-generated.js 31KB
url-state-machine.js 30KB
Window.js 30KB
HTMLObjectElement.js 30KB
SymbolTree.js 29KB
HTMLMediaElement.js 28KB
HTMLBodyElement.js 28KB
HTMLImageElement.js 28KB
Document-impl.js 28KB
encode-html.js 26KB
encode-html.js 26KB
Range-impl.js 26KB
Node.js 26KB
HTMLTableElement.js 25KB
HTMLAreaElement.js 25KB
SVGSVGElement.js 23KB
HTMLFrameSetElement.js 23KB
dbcs-codec.js 23KB
HTMLTableCellElement.js 22KB
decode.js 22KB
Range.js 22KB
HTMLIFrameElement.js 22KB
CSSStyleDeclaration.test.js 21KB
XMLHttpRequest.js 21KB
url-parse.js 21KB
decode.js 19KB
parsers.js 19KB
Selection.js 18KB
HTMLMarqueeElement.js 18KB
html.js 18KB
DOMTokenList.js 17KB
html.js 17KB
HTMLLinkElement.js 17KB
NamedNodeMap.js 17KB
MouseEvent.js 17KB
HTMLButtonElement.js 17KB
index.js 16KB
HTMLOptionsCollection.js 16KB
Element-impl.js 16KB
SVGStringList.js 16KB
HTMLFrameElement.js 16KB
URLSearchParams.js 16KB
HTMLFormElement.js 16KB
websocket-server.js 15KB
tests.js 15KB
FormData.js 15KB
CharacterData.js 15KB
WebSocket.js 15KB
HTMLScriptElement.js 15KB
FileReader.js 14KB
receiver.js 14KB
default-stylesheet.js 14KB
permessage-deflate.js 14KB
xhr-utils.js 14KB
Headers.js 14KB
KeyboardEvent.js 13KB
form_data.js 13KB
Event.js 13KB
URL.js 13KB
Location.js 13KB
HTMLTableRowElement.js 13KB
punycode.es6.js 12KB
index.js 12KB
punycode.js 12KB
sender.js 12KB
HTMLOutputElement.js 12KB
共 1080 条
- 1
- 2
- 3
- 4
- 5
- 6
- 11
资源评论
萧鼎
- 粉丝: 2w+
- 资源: 155
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功