Skip to content

Commit 7114606

Browse files
committed
support clob and blob object handling
1 parent 5afe212 commit 7114606

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/client.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ export class ScrapflyClient {
9292
return new errors.ScrapflyError(message, args);
9393
}
9494

95+
/**
96+
* Handle clob and blob large objects
97+
*/
98+
async handleLargeObjects(result: any): Promise<ScrapeResult> {
99+
const format = result.format
100+
if (format === 'clob' || format === 'blob') {
101+
let response: Response;
102+
try {
103+
const url = new URL(result.content);
104+
const params = { key: this.key };
105+
url.search = new URLSearchParams(params).toString();
106+
response = await this.fetch({
107+
url: url.toString(),
108+
method: 'GET',
109+
headers: {
110+
'user-agent': this.ua,
111+
'accept-encoding': 'gzip, deflate, br',
112+
accept: 'application/json',
113+
},
114+
});
115+
} catch (e) {
116+
log.error('error', e);
117+
throw e;
118+
}
119+
const content: string = await response.text();
120+
result.content = content
121+
if (format === 'clob') {
122+
result.format = 'text'
123+
}
124+
if (format === 'blob') {
125+
result.format = 'binary'
126+
}
127+
}
128+
return result
129+
}
130+
95131
/**
96132
* Turn scrapfly API response to ScrapeResult or raise one of ScrapflyError
97133
*/
@@ -173,6 +209,10 @@ export class ScrapflyClient {
173209
}
174210
throw new errors.ApiHttpClientError(JSON.stringify(data));
175211
}
212+
213+
const content = await this.handleLargeObjects(data.result)
214+
data.result = content
215+
176216
const result = this.handleResponse(
177217
response,
178218
new ScrapeResult({

0 commit comments

Comments
 (0)