Skip to content

Conversation

@barjin
Copy link
Member

@barjin barjin commented Sep 12, 2025

Phasing out got-scraping-specific interfaces in favour of native fetch API.

Closes #3071

@barjin barjin self-assigned this Sep 12, 2025
@barjin barjin marked this pull request as draft September 12, 2025 15:09
@barjin barjin marked this pull request as ready for review September 17, 2025 11:43
Copy link
Contributor

@janbuchar janbuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't managed to read the whole thing yet, sorry

@@ -1,5 +1,5 @@
import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
import type { OptionsInit, Response as GotResponse } from 'got-scraping';
import type { OptionsInit } from 'got-scraping';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to see this go as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to do that as a part of a separate PR. Removing got-scraping (and all the type todos) is no small feat, which would make it hard to review, if done all-in-one.

* Perform an HTTP Request and return after the response headers are received. The body may be read from a stream contained in the response.
*/
stream(request: HttpRequest, onRedirect?: RedirectHandler): Promise<StreamingHttpResponse>;
stream(request: HttpRequest, onRedirect?: RedirectHandler): Promise<Response>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the stream method obsolete? The web Response class can be streamed using response.body when the caller chooses to do so

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it actually is 👍 I'd prefer to do this in a separate PR, for the same reasons as the total got-scraping phase-out.

@barjin barjin changed the base branch from v4 to master November 25, 2025 12:34
@barjin barjin changed the base branch from master to v4 November 25, 2025 12:34
@barjin barjin requested a review from janbuchar November 26, 2025 11:55
Copy link
Contributor

@janbuchar janbuchar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly good to me, it's a huge simplification! I'll leave it up to you which of the unresolved comments deserve immediate attention and which should be left for another PR.

pushData: this.pushData.bind(this),
useState: this.useState.bind(this),
sendRequest: createSendRequest(this.httpClient, request!, session),
sendRequest: createSendRequest(this.httpClient, request!, session) as CrawlingContext['sendRequest'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no problem with adding a cast, but I can't help myself asking... what is the issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

The got-accepted CookieJar class and the Crawlee-supplied CookieJar interface are not completely compatible. I'd prefer to deal with this in the got-scraping removal PR, as we'll likely touch this code again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, that's probably issue material then

Comment on lines -879 to -920
/**
* The stream object returned from got does not have the below properties.
* At the same time, you can't read data directly from the response stream,
* because they won't get emitted unless you also read from the primary
* got stream. To be able to work with only one stream, we move the expected props
* from the response stream to the got stream.
* @internal
*/
function addResponsePropertiesToStream(stream: Readable, response: StreamingHttpResponse) {
const properties: (keyof PlainResponse)[] = [
'statusCode',
'statusMessage',
'headers',
'complete',
'httpVersion',
'rawHeaders',
'rawTrailers',
'trailers',
'url',
'request',
];

stream.on('end', () => {
// @ts-expect-error
if (stream.rawTrailers) stream.rawTrailers = response.rawTrailers; // TODO BC with got - remove in 4.0

// @ts-expect-error
if (stream.trailers) stream.trailers = response.trailers;

// @ts-expect-error
stream.complete = response.complete;
});

for (const prop of properties) {
if (!(prop in stream)) {
(stream as any)[prop] = (response as any)[prop];
}
}

return stream as unknown as PlainResponse;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing this go is pure joy.

cleanup: this.cleanupDownload.bind(this),
action: async (context) => this.initiateDownload(context),
cleanup: async (context) => {
await (context.response.body ? finished(context.response.body as any) : Promise.resolve());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if the request handler doesn't even start reading the stream? Or if it crashes midway? We should make sure we prevent hanging in that case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, currently, this will hang if nobody touches the response stream (even reading it "by proxy" via .text() or .bytes() is fine).

Since this is the current behaviour in v3 as well, I'd keep this here and make a note somewhere to revisit this eventually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it an issue please 😄

@janbuchar
Copy link
Contributor

Also, can you mention breaking changes in the upgrading guide, change the PR type to feat! and update the custom HTTP client guide?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants