cookie-parser
A JavaScript cookie parser for HTTP clients. Works in the browser as ES6 module or in NodeJS.
import { Cookies } from '@advanced-rest-client/cookie-parser';
const parser = new Cookies('rememberme=1; domain=foo.com; path=/; ssid=Hy1t5e#oj21.876aak;', 'http://bar.com/');
console.log(parser.cookies);
In Node:
const {Cookies} = require('@advanced-rest-client/cookie-parser');
const parser = new Cookies('rememberme=1; domain=foo.com; path=/; ssid=Hy1t5e#oj21.876aak;', 'http://bar.com/');
console.log(parser.cookies);
Usage
Installation
npm install --save @advanced-rest-client/cookie-parser
It parses set-cookie
header received from the server and produces a Cookie object.
import { Cookies } from '@advanced-rest-client/cookie-parser';
const parser = new Cookies('rememberme=1; domain=foo.com; path=/; ssid=Hy1t5e#oj21.876aak;', 'http://bar.com/');
console.log(parser.cookies);
which is equivalent to:
const cookies = Cookies.parse('...');
Manipulating cookies
The filter function filters out cookies that should not be considered for given URL. This is defined in https://tools.ietf.org/html/rfc6265.
const instance = new Cookies('a=b; domain=foo.com; path=/;', 'http://sub.foo.com/');
const removed = instance.filter();
console.log(removed);
To clear expired cookies just call clearExpired()
function.
const instance = new Cookies('a=b; expires=0;', 'http://sub.foo.com/');
const removed = instance.filter();
console.log(removed);
Development
git clone https://github.com/advanced-rest-client/cookie-parser
cd cookie-parser
npm install
Running the demo locally
npm start
Running the tests
npm test