# cookie-client

> Basic cookie-handling for outgoing requests

Latest version **0.0.3** (published 2013-10-09) · 0 weekly downloads

## Install

```sh
npm install cookie-client
pnpm add cookie-client
yarn add cookie-client
bun add cookie-client
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2013-10-09 |
| First published | 2013-09-27 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Geraint Luff |
| Maintainers | geraintluff |
| Keywords | cookie, client |

## Links

- npm: https://www.npmjs.com/package/cookie-client
- Repository: https://github.com/geraintluff/cookie-client
- npm.io page: https://npm.io/package/cookie-client

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.0.3 (latest) — 2013-10-09
- 0.0.2 — 2013-09-27
- 0.0.1 — 2013-09-27

## README

# Cookie client

This library emulates client cookie behaviour, allowing appropriate cookie behaviour when making outgoing requests from Node.

This library is **not particularly efficient** for large numbers of cookies.  The cookies are stored in a big list, and this entire list is searched for every request.  For small numbers of cookies, or cookies on a single domain, this should not be an issue - so if you're just automatically logging into and exploring a single API, this should be fine.

If you're looking to create a full-blown multi-domain scraper or something, then raise a GitHub issue or email the author about making it more efficient.

## Usage

### Create a cookie store

```javascript
var cookieClient = require('cookie-client');

var cookieStore = cookieClient();  // use of "new" is optional
```

### Adding cookies from incoming response headers

```javascript
cookieStore.addFromHeaders(response.headers); // full headers object
cookieStore.addFromHeaders(response.headers['set-cookie']); // just the cookie headers
```

### Cookie string for outgoing request

```javascript
request.headers['cookie'] = cookieStore.cookieStringForRequest(domain, path, isSecure);
```

### Inspect cookie objects for outgoing request

```javascript
request.headers['cookie'] = cookieStore.cookiesForRequest(domain, path, isSecure);
```

## Public Suffix List

To prevent "super-cookies" assigning themselves domains like `.com` (which is dangerous), this module attempts to download a Public Suffix List (see [publicsuffix.org](http://publicsuffix.org/)).  Any response other than a 200 will log an error to the console.

This is downloaded and stored in the module directory, so it happens once per installation.

Even if it is cached locally, it is loaded asynchronously.  You can query whether the PSL has loaded yet, or request a callback:

```javascript
cookieClient.pslLoaded;  // boolean flag

cookieClient,whenPslLoaded(function (error) {
	...
});
```

## License

The code is licensed as [Public Domain](http://geraintluff.github.io/tv4/LICENSE.txt) or [MIT](http://jsonary.com/LICENSE.txt) (your choice).

However, the file `public-suffix-list.txt` is taken from [publicsuffix.org](http://publicsuffix.org/), and has separate licensing terms (Mozilla Public License).  This package *used* to fetch the file from the web on first run, but that caused Node to crash if the connection was dropped by the remote server.  It is therefore included in the package, but is license separately from the rest of the code.

---
_Source: https://npm.io/package/cookie-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
