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

puppeteer-tough-cookie-store

v1.1.0

Published

Puppeteer cookie store implementation for tough-cookie

Downloads

90

Readme

puppeteer-tough-cookie-store

Puppeteer store for cookie management library tough cookie.

[library types]

Tested in puppeteer/chromium only!

Installation

npm install puppeteer-tough-cookie-store

or

yarn add puppeteer-tough-cookie-store

Usage example

const puppeteer = require('puppeteer');
// const { getStoreByPage } = require('puppeteer-tough-cookie-store')
const { getStoreByPage } = require('../')
const { CookieJar, MemoryCookieStore } = require('tough-cookie');

// optional libraries, you need only what you are really using
const got = require('got'); // make sure its got@11, cuz got@12 forcing to using imports instead requires

async function getPage() {
    // launch some browser
    let browser = await puppeteer.launch({
        headless: false,
        args: [
            '--incognito',
            '--start-in-incognito',
        ],
    });

    const context = await browser.createIncognitoBrowserContext();
    const page = await context.newPage();

    return [browser, page];
}

async function runtime() {
    // test url
    const url = 'https://www.npmjs.com/package/puppeteer-tough-cookie-store';

    // launch browser
    let [browser, page] = await getPage();

    // init cookieJar
    let cookieJarBrowser = new CookieJar(await getStoreByPage(page));

    // do base puppeteer request
    await page.goto(url)
    console.log('browser 1: cookieJarBrowser.getCookies', await cookieJarBrowser.getCookies(url));

    // do requests with cookies by another libs
    await testLibraries(url, cookieJarBrowser);

    // detach cookies to memory storage
    const cookieJarMemory = await cookieJarBrowser.clone(new MemoryCookieStore);
    // now we can close the browser but we will have cookies to work
    await browser.close();

    // do requests with cookies by another libs but without browser
    await testLibraries(url, cookieJarMemory);

    // launch another browser
    [browser, page] = await getPage();
    // init cookieJar
    cookieJarBrowser = new CookieJar(await getStoreByPage(page));

    // check for no cookies cuz we are in incognito
    console.log('browser 2: cookieJarBrowser.getCookies empty', await cookieJarBrowser.getCookies(url));
    // restore cookies
    cookieJarBrowser = await cookieJarMemory.clone(await getStoreByPage(page));
    console.log('browser 2: cookieJarBrowser.getCookies restored', await cookieJarBrowser.getCookies(url));

    // cookies were restored, so we can go requests again!

    // do base puppeteer request
    await page.goto(url)
    console.log('browser 1: cookieJarBrowser.getCookies', await cookieJarBrowser.getCookies(url));

    // do requests with cookies by another libs
    await testLibraries(url, cookieJarBrowser);

    // close browser at the end of our work
    await browser.close();
}

async function testLibraries(url, cookieJar) {
    // got request library
    await doGotRequest(url, cookieJar);
}

// got request library
async function doGotRequest(url, cookieJar) {
    // do request with got
    const gotResponse = await got({
        cookieJar,
        url
    });
    // check cookies we have sent
    console.log('got request header', gotResponse.request.options.headers.cookie);
    // check cookies we have got
    console.log('got response set-cookie header', gotResponse.headers['set-cookie']);
    // already sync with jar object
    console.log('cookieJar.getCookies after got request', await cookieJar.getCookies(url));
}

runtime();

TODO:

  • add tests
  • documentation

License

Copyright © 2022, Utyfua. Released under the MIT License.