From 7114606e5972f7009e6c5c5afcf8e7f1a1ad18e2 Mon Sep 17 00:00:00 2001 From: mazen-r Date: Mon, 12 Aug 2024 14:43:49 +0300 Subject: [PATCH 1/3] support clob and blob object handling --- src/client.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/client.ts b/src/client.ts index 3ae2880..365910e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -92,6 +92,42 @@ export class ScrapflyClient { return new errors.ScrapflyError(message, args); } + /** + * Handle clob and blob large objects + */ + async handleLargeObjects(result: any): Promise { + const format = result.format + if (format === 'clob' || format === 'blob') { + 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 */ @@ -173,6 +209,10 @@ export class ScrapflyClient { } throw new errors.ApiHttpClientError(JSON.stringify(data)); } + + const content = await this.handleLargeObjects(data.result) + data.result = content + const result = this.handleResponse( response, new ScrapeResult({ From 041b17767387df29f78766a836d0d28de428809e Mon Sep 17 00:00:00 2001 From: mazen-r Date: Mon, 12 Aug 2024 18:02:23 +0300 Subject: [PATCH 2/3] move format checking to outer statement --- src/client.ts | 64 ++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/client.ts b/src/client.ts index 365910e..7f7f2c4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -95,35 +95,34 @@ export class ScrapflyClient { /** * Handle clob and blob large objects */ - async handleLargeObjects(result: any): Promise { - const format = result.format - if (format === 'clob' || format === 'blob') { - 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' - } + async handleLargeObjects(result: any, format: "clob" | "blob"): Promise { + 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 } @@ -210,8 +209,11 @@ export class ScrapflyClient { throw new errors.ApiHttpClientError(JSON.stringify(data)); } - const content = await this.handleLargeObjects(data.result) - data.result = content + const content_format = data.result.format + if (content_format === 'clob' || content_format === 'blob') { + const content = await this.handleLargeObjects(data.result, content_format) + data.result = content + } const result = this.handleResponse( response, From e41a93db583c41c756db4b4aa2cb68717925bc73 Mon Sep 17 00:00:00 2001 From: mazen-r Date: Tue, 13 Aug 2024 10:33:06 +0300 Subject: [PATCH 3/3] simplify object assignment --- src/client.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 7f7f2c4..37dc83c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -211,8 +211,7 @@ export class ScrapflyClient { const content_format = data.result.format if (content_format === 'clob' || content_format === 'blob') { - const content = await this.handleLargeObjects(data.result, content_format) - data.result = content + data.result = await this.handleLargeObjects(data.result, content_format) } const result = this.handleResponse(