npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cookie-pot

v0.0.20

Published

Cookie jar style helper function for node HTTP requests to assist testing services

Downloads

33

Readme

cookie-pot

GitHub Super-Linter Tests Publish to npmjs

Cookie jar style helper function for Node.js HTTP requests to assist test and development of services.

Usage

const CookiePot = require('cookie-pot');

const pot = new CookiePot();

const cookieString = pot.deposit(response1);
pot.deposit(response2);
pot.deposit(response3);

const updatedCookieString = pot.cookieString;

CookiePot will

  • merge in new cookies from the response header strings
  • update the values of existing cookies
  • remove cookies if the value is set to the empty string

The cookie string string returned by the deposit() method and the cookieString property can be used for the Cookie request header verbatim.

To get a single cookie value from the cookiePot

const myCookie = pot.getCookie('myCookie');

If an exact match is not found, CookiePot will return the first cookie that includes the given name.

To clear the contents of the cookie pot

pot.clear();

A cookie pot can be built from an existing cookie string (will wipe the existing content)

const cookieString = 'id=2a9; X-TOKEN=pjb; .AspNetCore.Antiforgery.nUm79WDWtTU=xyz; LANG=de';
const pot = new CookiePot();
pot.buildPotFromCookieString(cookieString);

To set or overwrite a cookie value

pot.setCookie('myCookie', '123');

To remove a cookie, set it to the empty string

pot.setCookie('myCookie', '');

To build a cookie pot from request headers copied and pasted from the browser

const requestHeader = `Accept-Language
    en-GB,en;q=0.5
Cookie
    VIS_ID=123; _abck=234~0~YAAQ/0; SessionCookie=345; MY_COOKIE=PCY; amaze=34=34; last=1
    1
Host
    www.example.com`;
pot.addPossibleCookies(requestHeader);

Supported responses

CookiePot currently understands response header strings that look like

content-length: 29384
setcookie: mycookie=123; expires=Wed, 30 Nov 2022 00:00:00 GMT; path=/; secure
setcookie: LANG=en; expires=Wed, 30 Nov 2022 00:00:00 GMT;

CookiePot also understands request responses that includes a headers key that looks like (as returned by Axios)

    headers: {
        'date': 'Wed, 01 Dec 2021 10:23:43 GMT',
        'content-type': 'text/html;charset=UTF-8',
        'content-length': '362212',
        'set-cookie': [
            'id=2a9;Path=/;Expires=Tue, 21-Dec-2021 12:01:46 UTC;HttpOnly;Secure',
            'HASH=f5c8;Path=/;Expires=Thu, 01-Dec-2022 10:23:42 UTC',
        ],
    },

Axios example

const axios = require('axios');
const CookiePot = require('cookie-pot');

login();

async function login() {
    const url = `https://www.example.com/Account/SignIn`;
    const userAgent =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.4472.114 Safari/537.36';

    let options;
    let response;
    options = { headers: { 'User-Agent': userAgent } };
    response = await axios.get(url, options);
    let pot = new CookiePot();
    pot.deposit(response);

    const requestVerificationToken = pot.getCookie('Antiforgery');
    const signinPayload = `Form.Email=username%40example.com&Form.Password=pass123&Form.RememberMe=true&__RequestVerificationToken=${requestVerificationToken}&Form.RememberMe=true`;
    options = {
        data: signinPayload,
        method: 'POST',
        headers: {
            'User-Agent': userAgent,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': pot.cookieString,
        },
        maxRedirects: 0,
        validateStatus: (status) => {
            return status >= 200 && status < 400;
        },
    };
    response = await axios(url, options);
    pot.deposit(response);
}

With Axios in addition to setting maxRedirects: 0 you have to supply a validateStatus function that returns true for 3xx status codes otherwise Axios will throw an error.

SuperAgent example

const superagent = require('superagent');
const CookiePot = require('cookie-pot');

login();

async function login() {
    const url = `https://www.example.com/Account/SignIn`;
    const userAgent =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.4472.114 Safari/537.36';

    let response;
    let pot = new CookiePot();
    response = await superagent.get(url).set('User-Agent', userAgent);
    pot.deposit(response);

    const requestVerificationToken = pot.getCookie('Antiforgery');
    const signinPayload = `Form.Email=username%40example.com&Form.Password=pass123&Form.RememberMe=true&__RequestVerificationToken=${requestVerificationToken}&Form.RememberMe=true`;
    try {
        response = await superagent
            .post(url)
            .redirects(0)
            .set('User-Agent', userAgent)
            .set('Content-Type', 'application/x-www-form-urlencoded')
            .set('Cookie', pot.cookieString)
            .send(signinPayload);
    } catch (error) {
        response = error.response;
    }
    pot.deposit(response);
}

With SuperAgent you set redirects(0) so you can see the 3xx response headers, but it will also throw an error. You have to catch the error and get the response from the error object.

node-fetch example

import CookiePot from 'cookie-pot';
import fetch from 'node-fetch';

login();

async function login() {
    const url = `https://www.example.com/Account/SignIn`;
    const userAgent =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.4472.114 Safari/537.36';

    let options;
    let response;
    options = { headers: { 'User-Agent': userAgent } };
    response = await fetch(url, options);
    let pot = new CookiePot();
    pot.deposit(response.headers.raw());

    const requestVerificationToken = pot.getCookie('Antiforgery');
    const signinPayload = `Form.Email=username%40example.com&Form.Password=pass123&Form.RememberMe=true&__RequestVerificationToken=${requestVerificationToken}&Form.RememberMe=true`;
    options = {
        method: 'POST',
        headers: {
            'User-Agent': userAgent,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': pot.cookieString,
        },
        body: signinPayload,
        redirect: 'manual',
    };
    response = await fetch(url, options);
    pot.deposit(response.headers.raw());
}

To install node-fetch, put "type": "module" in your package.json and run npm install node-fetch.

To stop node-fetch following redirects, set redirect: 'manual' in the options.

native node fetch (v18+) example

import CookiePot from 'cookie-pot';

login();

async function login() {
    const url = `https://www.example.com/Account/SignIn`;
    const userAgent =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';

    let options;
    let response;
    options = { headers: { 'User-Agent': userAgent } };
    response = await fetch(url, options);
    let pot = new CookiePot();
    pot.deposit(response);

    const requestVerificationToken = pot.getCookie('Antiforgery');
    const signinPayload = `Form.Email=example%40example.com&Form.Password=pass123&Form.RememberMe=true&__RequestVerificationToken=${requestVerificationToken}&Form.RememberMe=true`;
    options = {
        method: 'POST',
        headers: {
            'User-Agent': userAgent,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': pot.cookieString,
        },
        body: signinPayload,
        redirect: 'manual',
    };
    response = await fetch(url, options);
    if (response.status !== 302) {
        console.log(`Login failed: ${response.status} ${response.statusText}`);
        return;
    }
    console.log(`Login successful: ${response.status} ${response.statusText}`);
    pot.deposit(response);
    console.log(pot.cookieString);
}

got example

import CookiePot from 'cookie-pot';
import got from 'got';

login();

async function login() {
    const signinUrl = `https://www.example.com/Account/SignIn`;
    const userAgent =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.4472.114 Safari/537.36';

    let response;
    response = await got(signinUrl, { headers: { 'User-Agent': userAgent } });
    let pot = new CookiePot();
    pot.deposit(response);

    const requestVerificationToken = pot.getCookie('Antiforgery');
    const signinPayload = `Form.Email=username%40example.com&Form.Password=pass123&Form.RememberMe=true&__RequestVerificationToken=${requestVerificationToken}&Form.RememberMe=true`;
    response = await got.post(signinUrl, {
        headers: {
            'User-Agent': userAgent,
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cookie': pot.cookieString,
        },
        body: signinPayload,
        followRedirect: false,
    });
    pot.deposit(response.headers);
}

To install got, put "type": "module" in your package.json and run npm install got.

To stop got following redirects, set followRedirect: false in the options.