Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright CookieStore

From Leeroopedia
Knowledge Sources
Domains Server, Cookies
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for managing browser cookies in-memory with domain/path matching and expiration handling provided by the Playwright library.

Description

The module defines two classes: `Cookie` and `CookieStore`. The `Cookie` class wraps a `channels.NetworkCookie` and provides URL matching (domain, path, secure checks per RFC 6265), equality comparison, and expiration checking. The `CookieStore` class maintains a `Map<string, Set<Cookie>>` keyed by cookie name, implementing `addCookies` (with deduplication per RFC 6265 Section 5.3), `cookies(url)` for URL-filtered retrieval, `allCookies()` for listing, and `removeCookies`. Expired cookies are lazily pruned during iteration. The `domainMatches` and `pathMatches` helper functions implement standard cookie matching rules.

Usage

Use CookieStore when Playwright needs to maintain an in-memory cookie jar, such as for route-based cookie management or when browser-level cookie APIs are insufficient.

Code Reference

Source Location

Signature

export class Cookie {
  constructor(data: channels.NetworkCookie);
  name(): string;
  matches(url: URL): boolean;
  equals(other: Cookie): boolean;
  networkCookie(): channels.NetworkCookie;
  expired(): boolean;
}

export class CookieStore {
  addCookies(cookies: channels.NetworkCookie[]): void;
  cookies(url: URL): channels.NetworkCookie[];
  allCookies(): channels.NetworkCookie[];
}

Import

import { Cookie, CookieStore } from '../server/cookieStore';

I/O Contract

Inputs

Name Type Required Description
cookies channels.NetworkCookie[] Yes Array of cookie data to add to the store
url URL Yes (cookies) URL to filter cookies by domain/path matching

Outputs

Name Type Description
cookies channels.NetworkCookie[] Array of matching cookies for the given URL
allCookies channels.NetworkCookie[] All non-expired cookies in the store

Usage Examples

import { CookieStore } from '../server/cookieStore';

const store = new CookieStore();
store.addCookies([{
  name: 'session',
  value: 'abc123',
  domain: '.example.com',
  path: '/',
  expires: -1,
  httpOnly: false,
  secure: false,
  sameSite: 'Lax',
}]);

const cookies = store.cookies(new URL('https://example.com/page'));
// Returns the 'session' cookie

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment