Popsicle Cookie Jar Middleware

1.0.1 · active · verified Wed Apr 22

popsicle-cookie-jar is a middleware designed for the Popsicle HTTP client library, enabling cookie management in Node.js environments. It integrates with Popsicle's request lifecycle to automatically handle `Set-Cookie` headers from responses and attach `Cookie` headers to outgoing requests, mimicking browser-like cookie behavior. The current stable version is 1.0.1, which primarily features an update to the underlying `tough-cookie` library for security patches. Given its single-purpose nature and recent release history, its release cadence appears to be driven by bug fixes or critical dependency updates rather than frequent feature additions. Key differentiators include its tight integration with the Popsicle ecosystem and its reliance on `tough-cookie` for robust cookie specification adherence, providing a reliable, in-memory cookie store by default, with options for custom `CookieJar` instances.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply `popsicle-cookie-jar` middleware to a Popsicle client to enable automatic cookie handling, showing both default in-memory and custom `CookieJar` usage.

import { cookies, CookieJar } from "popsicle-cookie-jar";
import { compose } from 'servie';
// Assume 'transport' is another middleware, e.g., from 'popsicle-transport-http'
// For demonstration, we'll create a dummy transport and Popsicle client.

interface Context {
  request: { url: string; headers?: Record<string, string>; };
  response?: { status: number; headers: Record<string, string>; body?: any; };
}

async function dummyTransport(ctx: Context, next: () => Promise<void>) {
  console.log(`Sending request to: ${ctx.request.url}`);
  ctx.response = {
    status: 200,
    headers: { 'Set-Cookie': 'session=abc; Path=/; HttpOnly' },
    body: 'Hello World'
  };
  await next();
}

// Create an in-memory cookie jar
const myCookieJar = new CookieJar();

// Compose the middleware with a custom cookie jar
const middlewareWithJar = compose([cookies(myCookieJar), dummyTransport]);

// Or let it create a default in-memory jar
const middlewareDefault = compose([cookies(), dummyTransport]);

async function makeRequest(middleware: (ctx: Context, next: () => Promise<void>) => Promise<void>) {
  const ctx: Context = { request: { url: 'http://example.com/login' } };
  await middleware(ctx, async () => {}); // No further middleware after transport
  console.log('Response headers:', ctx.response?.headers);
  console.log('Cookies in jar (after request):', myCookieJar.getCookiesSync('http://example.com/login'));
}

console.log('--- Using custom CookieJar ---');
await makeRequest(middlewareWithJar);

console.log('\n--- Using default (new) CookieJar instance for separate calls ---');
// Each call to cookies() without an argument creates a new jar
await makeRequest(compose([cookies(), dummyTransport]));

view raw JSON →