Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix(impit-client): honor redirect handler for session cookie parity (#3938)
Felix-Ayush9371301b21f7
diff --git a/packages/impit-client/src/index.ts b/packages/impit-client/src/index.tsindex ee9407319..da72c480d 100644--- a/packages/impit-client/src/index.ts+++ b/packages/impit-client/src/index.ts@@ -2,7 +2,14 @@ import { pipeline, Readable, Transform } from 'node:stream';import { type ReadableStream } from 'node:stream/web';import { isGeneratorObject } from 'node:util/types';-import type { BaseHttpClient, HttpRequest, HttpResponse, ResponseTypes, StreamingHttpResponse } from '@crawlee/core';+import type {+ BaseHttpClient,+ HttpRequest,+ HttpResponse,+ RedirectHandler,+ ResponseTypes,+ StreamingHttpResponse,+} from '@crawlee/core';import type { HttpMethod, ImpitOptions, ImpitResponse, RequestInit } from 'impit';import { Impit } from 'impit';import type { CookieJar as ToughCookieJar } from 'tough-cookie';@@ -19,6 +26,8 @@ interface ResponseWithRedirects {redirectUrls: URL[];}+type SimpleHeaders = Record<string, string | string[] | undefined>;+/*** A HTTP client implementation based on the `impit library.*/@@ -119,6 +128,27 @@ export class ImpitHttpClient implements BaseHttpClient {return false;}+ /**+ * Converts Fetch/Impit headers into a simple header map.+ * `Object.fromEntries` would keep only the last `set-cookie` value, so those are collected separately.+ */+ private intoSimpleHeaders(headers: Headers): SimpleHeaders {+ const result: SimpleHeaders = {};++ for (const [key, value] of headers.entries()) {+ if (key === 'set-cookie') continue;+ result[key] = value;+ }++ const setCookies = headers.getSetCookie();++ if (setCookies.length > 0) {+ result['set-cookie'] = setCookies.length === 1 ? setCookies[0] : setCookies;+ }++ return result;+ }+/*** Common implementation for `sendRequest` and `stream` methods.* @param request `HttpRequest` object@@ -130,6 +160,7 @@ export class ImpitHttpClient implements BaseHttpClient {redirectCount?: number;redirectUrls?: URL[];},+ onRedirect?: RedirectHandler,): Promise<ResponseWithRedirects> {if ((redirects?.redirectCount ?? 0) > this.maxRedirects) {throw new Error(`Too many redirects, maximum is ${this.maxRedirects}.`);@@ -159,16 +190,44 @@ export class ImpitHttpClient implements BaseHttpClient {throw new Error('Redirect response missing location header.');}+ const nextRedirectUrls = [...(redirects?.redirectUrls ?? []), redirectUrl];+ const updatedRequest: { url?: string | URL; headers: SimpleHeaders } = {+ url: redirectUrl.href,+ headers: { ...(request.headers ?? {}) },+ };++ // Match GotScrapingHttpClient: allow HttpCrawler to persist redirect cookies into the session+ // and mutate Cookie / URL for the next hop.+ onRedirect?.(+ {+ redirectUrls: nextRedirectUrls,+ url,+ statusCode: response.status,+ statusMessage: response.statusText,+ headers: this.intoSimpleHeaders(response.headers),+ trailers: {},+ complete: true,+ },+ updatedRequest,+ );++ const nextUrl =+ typeof updatedRequest.url === 'string'+ ? updatedRequest.url+ : (updatedRequest.url?.href ?? redirectUrl.href);+return this.getResponse({...request,method: this.shouldRewriteRedirectToGet(response.status, request.method) ? 'GET' : request.method,- url: redirectUrl.href,+ url: nextUrl,+ headers: updatedRequest.headers,},{redirectCount: (redirects?.redirectCount ?? 0) + 1,- redirectUrls: [...(redirects?.redirectUrls ?? []), redirectUrl],+ redirectUrls: nextRedirectUrls,},+ onRedirect,);}@@ -203,7 +262,7 @@ export class ImpitHttpClient implements BaseHttpClient {}return {- headers: Object.fromEntries(response.headers.entries()),+ headers: this.intoSimpleHeaders(response.headers),statusCode: response.status,url: response.url,request,@@ -243,8 +302,8 @@ export class ImpitHttpClient implements BaseHttpClient {/*** @inheritDoc*/- async stream(request: HttpRequest): Promise<StreamingHttpResponse> {- const { response, redirectUrls } = await this.getResponse(request);+ async stream(request: HttpRequest, onRedirect?: RedirectHandler): Promise<StreamingHttpResponse> {+ const { response, redirectUrls } = await this.getResponse(request, undefined, onRedirect);const [stream, getDownloadProgress] = this.getStreamWithProgress(response);return {@@ -258,7 +317,7 @@ export class ImpitHttpClient implements BaseHttpClient {},uploadProgress: { percent: 100, transferred: 0 },redirectUrls,- headers: Object.fromEntries(response.headers.entries()),+ headers: this.intoSimpleHeaders(response.headers),trailers: {},};}diff --git a/test/core/crawlers/http_crawler.test.ts b/test/core/crawlers/http_crawler.test.tsindex 0aa96976a..16d723df9 100644--- a/test/core/crawlers/http_crawler.test.ts+++ b/test/core/crawlers/http_crawler.test.ts@@ -39,6 +39,12 @@ router.set('/cookies', (req, res) => {res.end(JSON.stringify(req.headers.cookie));});+router.set('/setCookie', (req, res) => {+ res.setHeader('content-type', 'text/html');+ res.setHeader('set-cookie', 'first=1');+ res.end();+});+router.set('/redirectWithoutCookies', (req, res) => {res.setHeader('location', '/cookies');res.statusCode = 302;@@ -238,6 +244,27 @@ describe.each(expect(results).toStrictEqual(['foo=bar']);});+ test('handles cookies from redirects when the session already has cookies', async () => {+ const results: string[] = [];++ const crawler = new HttpCrawler({+ httpClient,+ sessionPoolOptions: {+ maxPoolSize: 1,+ // isolated so that cookies stored by the other tests / clients don't leak in+ persistStateKey: `SDK_SESSION_POOL_STATE_${httpClient.constructor.name}`,+ },+ maxConcurrency: 1,+ requestHandler: async ({ body }) => {+ results.push(body.toString());+ },+ });++ await crawler.run([`${url}/setCookie`, `${url}/redirectAndCookies`]);++ expect(results[1]).toBe('"first=1; foo=bar"');+ });+test('handles cookies from redirects - no empty cookie header', async () => {const results: string[] = [];diff --git a/test/core/impit_http_client.test.ts b/test/core/impit_http_client.test.tsindex d85c8b427..4f7542186 100644--- a/test/core/impit_http_client.test.ts+++ b/test/core/impit_http_client.test.ts@@ -9,6 +9,37 @@ vi.mock('impit', () => ({),}));+function createRedirectResponse(status: number, location: string, setCookie: string[] = []) {+ const headers = new Headers({ location });+ for (const cookie of setCookie) headers.append('set-cookie', cookie);++ return {+ status,+ statusText: 'Found',+ url: 'http://example.com/start',+ headers,+ body: undefined,+ };+}++function createFinalResponse(body = 'ok') {+ return {+ status: 200,+ statusText: 'OK',+ url: 'http://example.com/final',+ headers: new Headers({ 'content-type': 'text/plain', 'content-length': String(body.length) }),+ body: new ReadableStream({+ start(controller) {+ controller.enqueue(new TextEncoder().encode(body));+ controller.close();+ },+ }),+ text: async () => body,+ json: async () => ({ body }),+ bytes: async () => Buffer.from(body),+ };+}+describe('ImpitHttpClient', () => {beforeEach(() => {vi.mocked(Impit).mockClear();@@ -31,4 +62,70 @@ describe('ImpitHttpClient', () => {expect(Impit).toHaveBeenCalledTimes(2);});++ test('stream() invokes onRedirect and forwards mutated Cookie header to the next hop', async () => {+ const httpClient = new ImpitHttpClient({ cacheClients: false });+ const fetchMock = vi.fn();++ vi.mocked(Impit).mockImplementation(+ class {+ fetch = fetchMock;+ } as any,+ );++ fetchMock+ .mockResolvedValueOnce(createRedirectResponse(302, '/final', ['session=abc', 'other=def']))+ .mockResolvedValueOnce(createFinalResponse('done'));++ const onRedirect = vi.fn((_redirectResponse, updatedRequest) => {+ updatedRequest.headers.Cookie = 'session=abc';+ });++ const response = await httpClient.stream(+ {+ url: 'http://example.com/start',+ method: 'GET',+ headers: {},+ },+ onRedirect,+ );++ expect(onRedirect).toHaveBeenCalledTimes(1);+ expect(fetchMock).toHaveBeenCalledTimes(2);++ const [redirectResponse] = onRedirect.mock.calls[0];+ expect(redirectResponse.statusCode).toBe(302);+ expect(redirectResponse.headers['set-cookie']).toEqual(['session=abc', 'other=def']);++ const secondCallHeaders = fetchMock.mock.calls[1][1].headers as Headers;+ expect(secondCallHeaders.get('Cookie')).toBe('session=abc');+ expect(fetchMock.mock.calls[1][0]).toBe('http://example.com/final');+ expect(response.statusCode).toBe(200);+ expect(response.redirectUrls).toEqual([new URL('http://example.com/final')]);+ });++ test('stream() follows redirects without onRedirect for API compatibility', async () => {+ const httpClient = new ImpitHttpClient({ cacheClients: false });+ const fetchMock = vi.fn();++ vi.mocked(Impit).mockImplementation(+ class {+ fetch = fetchMock;+ } as any,+ );++ fetchMock+ .mockResolvedValueOnce(createRedirectResponse(302, 'http://example.com/final'))+ .mockResolvedValueOnce(createFinalResponse('done'));++ const response = await httpClient.stream({+ url: 'http://example.com/start',+ method: 'GET',+ headers: {},+ });++ expect(fetchMock).toHaveBeenCalledTimes(2);+ expect(response.statusCode).toBe(200);+ expect(response.redirectUrls).toHaveLength(1);+ });});