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 🙏

© 2026 – Pkg Stats / Ryan Hefner

playwright-session-manager

v0.1.0

Published

cy.session for Playwright: cache, validate and auto re-login authenticated sessions across tests and workers.

Readme

playwright-session-manager

cy.session for Playwright — cache an authenticated session, validate it before reuse and automatically re-login when it's no longer valid. Handles the tricky case of tokens that expire or rotate mid-suite, shared safely across tests and parallel workers.

npm install playwright-session-manager

Requires @playwright/test (peer dependency) and Node >= 18.

Why

Playwright's storageState is static: it loads cookies/localStorage and that's it. If the session expired, your test just fails. Cypress's cy.session solves this with a validate callback that re-runs the login when the session is invalid. This package brings that behavior to Playwright, plus atomic, locked, shared-on-disk caching so parallel workers never corrupt the state or trigger redundant logins.

Quick start

import { test, expect } from '@playwright/test';
import { session } from 'playwright-session-manager';

async function login(page, context) {
  await session(
    context,
    '[email protected]',              // id: string | array | object (like cy.session)
    async () => {                    // setup: perform the login
      await page.goto('/login');
      await page.getByLabel('Email').fill('[email protected]');
      await page.getByLabel('Password').fill('s3cr3t');
      await page.getByRole('button', { name: 'Log In' }).click();
      await page.waitForURL('**/dashboard');
    },
    {
      validate: async () => {        // validate: throw or return false = invalid → re-login
        const res = await context.request.get('/whoami');
        expect(res.status()).toBe(200);
      },
    },
  );
}

test.beforeEach(async ({ page, context }) => {
  await login(page, context);
  await page.goto('/');              // navigate after restoring, like cy.session
});

test('is authenticated', async ({ page }) => {
  await expect(page.getByRole('link', { name: 'Account' })).toBeVisible();
});

API

function session(
  context: BrowserContext,
  id: SessionId,
  setup: () => Promise<void> | void,
  options?: SessionOptions,
): Promise<void>;

type SessionId = string | Array<string | number> | Record<string, unknown>;

interface SessionOptions {
  validate?: () => Promise<void> | Promise<boolean> | boolean | void;
  cacheDir?: string;          // default ".auth"
  cacheAcrossSpecs?: boolean; // default true (shared on disk)
  maxAgeMs?: number;          // invalidate cache by age (great for short-lived tokens)
  lock?: boolean;             // default true (file lock across workers)
  lockTimeoutMs?: number;     // default 30000
  staleLockMs?: number;       // default 60000
}

Behavior (faithful to cy.session)

| Situation | Behavior | |----------------------------------------|--------------------------------------------| | No cache for id | run setup → run validate | | validate fails right after setup | throws (no infinite loop) | | Cache exists for id | restore state → run validate | | validate fails after restoring | re-run setup (re-login) → validate | | validate invalid | throws OR resolves false | | id is array/object | deterministically serialized into the key |

The id is hashed to name the cache file (.auth/<hash>.json) — sensitive values never appear in the filename.

Rotating / short-lived tokens

  • Only session() writes the shared cache file (prevents "auth-state poisoning").
  • Writes are atomic (temp file + rename) and guarded by a file lock, so parallel workers never corrupt the cache or re-login redundantly.
  • After a successful validate, the freshest state is re-saved.
  • Use maxAgeMs to proactively invalidate a cache older than your token's lifetime.

If your refresh token is single-use and you run with high parallelism, re-logins may happen more often (correctness is always guaranteed; cache benefit degrades). Mitigate with workers: 1 for the authenticated suite, or one id per user.

Multiple users

// Different ids → independent cached sessions.
await session(context, ['admin', orgId], loginAsAdmin, { validate });
await session(context, ['viewer', orgId], loginAsViewer, { validate });

Notes / limitations

  • Applies cookies and localStorage to the given context. IndexedDB is not captured by Playwright's storageState (same limitation as the native feature).
  • Add .auth/ to your .gitignore — the cache contains live session data.

License

MIT