Skip to content

support clob and blob object handling #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,41 @@ export class ScrapflyClient {
return new errors.ScrapflyError(message, args);
}

/**
* Handle clob and blob large objects
*/
async handleLargeObjects(result: any, format: "clob" | "blob"): Promise<ScrapeResult> {
let response: Response;

try {
const url = new URL(result.content);
const params = { key: this.key };
url.search = new URLSearchParams(params).toString();
response = await this.fetch({
url: url.toString(),
method: 'GET',
headers: {
'user-agent': this.ua,
'accept-encoding': 'gzip, deflate, br',
accept: 'application/json',
},
});
} catch (e) {
log.error('error', e);
throw e;
}

const content: string = await response.text();
result.content = content
if (format === 'clob') {
result.format = 'text'
}
if (format === 'blob') {
result.format = 'binary'
}
return result
}

/**
* Turn scrapfly API response to ScrapeResult or raise one of ScrapflyError
*/
Expand Down Expand Up @@ -173,6 +208,13 @@ export class ScrapflyClient {
}
throw new errors.ApiHttpClientError(JSON.stringify(data));
}

const content_format = data.result.format
if (content_format === 'clob' || content_format === 'blob') {
const content = await this.handleLargeObjects(data.result, content_format)
Copy link
Member

Choose a reason for hiding this comment

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

this can be simplified

Suggested change
const content = await this.handleLargeObjects(data.result, content_format)
data.result = await this.handleLargeObjects(data.result, content_format)

data.result = content
}

const result = this.handleResponse(
response,
new ScrapeResult({
Expand Down
Loading