scrapfly-sdk 0.10.3 → 0.10.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/_dnt.shims.ts +64 -0
- package/src/batch.ts +272 -0
- package/src/browserconfig.ts +118 -0
- package/src/client.ts +1609 -0
- package/src/crawl.ts +251 -0
- package/src/crawlerconfig.ts +363 -0
- package/src/crawlerresult.ts +471 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/assert_path.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/basename.ts +53 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/common.ts +26 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/constants.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/dirname.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/format.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/from_file_url.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/glob_to_reg_exp.ts +290 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize.ts +9 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/normalize_string.ts +74 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/relative.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/strip_trailing_separators.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_common/to_file_url.ts +17 -0
- package/src/deps/jsr.io/@std/path/1.0.1/_os.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/basename.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/common.ts +35 -0
- package/src/deps/jsr.io/@std/path/1.0.1/constants.ts +18 -0
- package/src/deps/jsr.io/@std/path/1.0.1/dirname.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/extname.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/format.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/from_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_absolute.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/is_glob.ts +49 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/join_globs.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/mod.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/normalize_glob.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/parse.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/_util.ts +10 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/basename.ts +39 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/dirname.ts +55 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/extname.ts +73 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/from_file_url.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/glob_to_regexp.ts +94 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/is_absolute.ts +25 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join.ts +27 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/join_globs.ts +44 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize.ts +41 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/parse.ts +121 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/relative.ts +103 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/resolve.ts +69 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_file_url.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/posix/to_namespaced_path.ts +21 -0
- package/src/deps/jsr.io/@std/path/1.0.1/relative.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/resolve.ts +32 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_file_url.ts +30 -0
- package/src/deps/jsr.io/@std/path/1.0.1/to_namespaced_path.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/types.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/_util.ts +28 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/basename.ts +48 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/constants.ts +15 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/dirname.ts +114 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/extname.ts +86 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/format.ts +31 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/from_file_url.ts +34 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/glob_to_regexp.ts +92 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/is_absolute.ts +40 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join.ts +72 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/join_globs.ts +45 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize.ts +132 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/normalize_glob.ts +42 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/parse.ts +184 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/relative.ts +128 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/resolve.ts +174 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_file_url.ts +38 -0
- package/src/deps/jsr.io/@std/path/1.0.1/windows/to_namespaced_path.ts +60 -0
- package/src/deps.ts +4 -0
- package/src/errors.ts +55 -0
- package/src/extractionconfig.ts +175 -0
- package/src/logger.ts +51 -0
- package/src/main.ts +61 -0
- package/src/monitoringconfig.ts +50 -0
- package/src/polyfill.ts +37 -0
- package/src/result.ts +338 -0
- package/src/scrapeconfig.ts +448 -0
- package/src/screenshotconfig.ts +204 -0
- package/src/types.ts +2 -0
- package/src/utils.ts +86 -0
- package/src/webhookserver.ts +66 -0
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { urlsafe_b64encode } from './utils.js';
|
|
2
|
+
import { log } from './logger.js';
|
|
3
|
+
import type { HttpMethod, Rec } from './types.js';
|
|
4
|
+
import { ScrapeConfigError } from './errors.js';
|
|
5
|
+
|
|
6
|
+
export enum ScreenshotFlags {
|
|
7
|
+
/**
|
|
8
|
+
Options to customize the screenshot behavior
|
|
9
|
+
Attributes:
|
|
10
|
+
LOAD_IMAGES: Enable image rendering with the request, add extra usage for the bandwidth consumed.
|
|
11
|
+
DARK_MODE: Enable dark mode display.
|
|
12
|
+
BLOCK_BANNERS: Block cookies banners and overlay that cover the screen.
|
|
13
|
+
PRINT_MEDIA_FORMAT: Render the page in the print mode.
|
|
14
|
+
*/
|
|
15
|
+
LOAD_IMAGES = 'load_images',
|
|
16
|
+
DARK_MODE = 'dark_mode',
|
|
17
|
+
BLOCK_BANNERS = 'block_banners',
|
|
18
|
+
PRINT_MEDIA_FORMAT = 'print_media_format',
|
|
19
|
+
HIGH_QUALITY = 'high_quality',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export enum Format {
|
|
23
|
+
/**
|
|
24
|
+
Format of the scraped content.
|
|
25
|
+
Attributes:
|
|
26
|
+
JSON: JSON format.
|
|
27
|
+
TEXT: Text format.
|
|
28
|
+
MARKDOWN: Markdown format.
|
|
29
|
+
CLEAN_HTML: Clean HTML format.
|
|
30
|
+
*/
|
|
31
|
+
JSON = 'json',
|
|
32
|
+
TEXT = 'text',
|
|
33
|
+
MARKDOWN = 'markdown',
|
|
34
|
+
CLEAN_HTML = 'clean_html',
|
|
35
|
+
RAW = 'raw',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export enum FormatOption {
|
|
39
|
+
NO_LINKS = 'no_links',
|
|
40
|
+
NO_IMAGES = 'no_images',
|
|
41
|
+
ONLY_CONTENT = 'only_content',
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type ScrapeConfigOptions = {
|
|
45
|
+
url: string;
|
|
46
|
+
retry?: boolean;
|
|
47
|
+
method?: HttpMethod;
|
|
48
|
+
country?: string;
|
|
49
|
+
render_js?: boolean;
|
|
50
|
+
cache?: boolean;
|
|
51
|
+
cache_clear?: boolean;
|
|
52
|
+
cost_budget?: number;
|
|
53
|
+
ssl?: boolean;
|
|
54
|
+
dns?: boolean;
|
|
55
|
+
asp?: boolean;
|
|
56
|
+
debug?: boolean;
|
|
57
|
+
raise_on_upstream_error?: boolean;
|
|
58
|
+
cache_ttl?: number;
|
|
59
|
+
proxy_pool?: string;
|
|
60
|
+
session?: string;
|
|
61
|
+
tags?: string[];
|
|
62
|
+
format?: 'json' | 'text' | 'markdown' | 'clean_html' | 'raw' | Format;
|
|
63
|
+
format_options?: ('no_links' | 'no_images' | 'only_content' | FormatOption)[];
|
|
64
|
+
extraction_template?: string; // saved template name
|
|
65
|
+
extraction_ephemeral_template?: object; // ephemeraly declared json template
|
|
66
|
+
extraction_prompt?: string;
|
|
67
|
+
extraction_model?: string;
|
|
68
|
+
correlation_id?: string;
|
|
69
|
+
cookies?: Rec<string>;
|
|
70
|
+
body?: string;
|
|
71
|
+
data?: Rec<any>;
|
|
72
|
+
headers?: Rec<string>;
|
|
73
|
+
js?: string;
|
|
74
|
+
rendering_wait?: number;
|
|
75
|
+
wait_for_selector?: string;
|
|
76
|
+
screenshots?: Rec<any>;
|
|
77
|
+
screenshot_flags?:
|
|
78
|
+
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
|
|
79
|
+
session_sticky_proxy?: boolean;
|
|
80
|
+
webhook?: string;
|
|
81
|
+
timeout?: number;
|
|
82
|
+
js_scenario?: Rec<any>;
|
|
83
|
+
os?: string;
|
|
84
|
+
lang?: string[];
|
|
85
|
+
auto_scroll?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Chromium-based browser brand used for fingerprint generation.
|
|
88
|
+
* One of "chrome" | "edge" | "brave" | "opera". Defaults to "chrome"
|
|
89
|
+
* when omitted. Invalid values are silently dropped by the server.
|
|
90
|
+
*/
|
|
91
|
+
browser_brand?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Return the raw upstream response (target's status, headers, body)
|
|
94
|
+
* instead of the JSON envelope. When true, ScrapflyClient.scrape()
|
|
95
|
+
* returns the raw fetch Response so callers can drive it like any
|
|
96
|
+
* HTTP response. Scrapfly metadata is on the X-Scrapfly-* headers.
|
|
97
|
+
*/
|
|
98
|
+
proxified_response?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Controls when the browser considers the page loaded.
|
|
101
|
+
* "complete" (default) or "domcontentloaded" for faster scrapes.
|
|
102
|
+
*/
|
|
103
|
+
rendering_stage?: 'complete' | 'domcontentloaded';
|
|
104
|
+
/**
|
|
105
|
+
* Spoof browser geolocation. Format: "latitude,longitude"
|
|
106
|
+
* e.g. "48.856614,2.3522219" for Paris.
|
|
107
|
+
*/
|
|
108
|
+
geolocation?: string;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export class ScrapeConfig {
|
|
112
|
+
static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool';
|
|
113
|
+
static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool';
|
|
114
|
+
|
|
115
|
+
url: string;
|
|
116
|
+
retry = true;
|
|
117
|
+
method: HttpMethod = 'GET';
|
|
118
|
+
country?: string | null = null;
|
|
119
|
+
render_js = false;
|
|
120
|
+
cache = false;
|
|
121
|
+
cache_clear = false;
|
|
122
|
+
cost_budget?: number;
|
|
123
|
+
ssl = false;
|
|
124
|
+
dns = false;
|
|
125
|
+
asp = false;
|
|
126
|
+
debug = false;
|
|
127
|
+
raise_on_upstream_error = true;
|
|
128
|
+
cache_ttl?: number;
|
|
129
|
+
proxy_pool?: string;
|
|
130
|
+
session?: string;
|
|
131
|
+
tags: Set<string> = new Set<string>();
|
|
132
|
+
format?: 'json' | 'text' | 'markdown' | 'clean_html' | 'raw' | Format;
|
|
133
|
+
format_options?: ('no_links' | 'no_images' | 'only_content' | FormatOption)[];
|
|
134
|
+
extraction_template?: string; // saved template name
|
|
135
|
+
extraction_ephemeral_template?: object; // ephemeraly declared json template
|
|
136
|
+
extraction_prompt?: string;
|
|
137
|
+
extraction_model?: string;
|
|
138
|
+
correlation_id?: string;
|
|
139
|
+
cookies?: Rec<string>;
|
|
140
|
+
body?: string;
|
|
141
|
+
data?: Rec<any>;
|
|
142
|
+
headers?: Rec<string>;
|
|
143
|
+
js?: string;
|
|
144
|
+
rendering_wait?: number;
|
|
145
|
+
wait_for_selector?: string;
|
|
146
|
+
session_sticky_proxy = false;
|
|
147
|
+
screenshots?: Rec<any>;
|
|
148
|
+
screenshot_flags?:
|
|
149
|
+
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
|
|
150
|
+
webhook?: string;
|
|
151
|
+
timeout?: number; // in milliseconds
|
|
152
|
+
js_scenario?: Rec<any>;
|
|
153
|
+
lang?: string[];
|
|
154
|
+
os?: string;
|
|
155
|
+
auto_scroll?: boolean;
|
|
156
|
+
browser_brand?: string;
|
|
157
|
+
proxified_response?: boolean;
|
|
158
|
+
rendering_stage?: 'complete' | 'domcontentloaded';
|
|
159
|
+
geolocation?: string;
|
|
160
|
+
|
|
161
|
+
constructor(options: ScrapeConfigOptions) {
|
|
162
|
+
this.validateOptions(options);
|
|
163
|
+
if (options.format && !Object.values(Format).includes(options.format as Format)) {
|
|
164
|
+
throw new ScrapeConfigError(`Invalid Format param value: ${options.format}`);
|
|
165
|
+
}
|
|
166
|
+
this.format = options.format ?? this.format;
|
|
167
|
+
if (options.format_options) {
|
|
168
|
+
options.format_options.forEach((flag) => {
|
|
169
|
+
if (!Object.values(FormatOption).includes(flag as FormatOption)) {
|
|
170
|
+
throw new ScrapeConfigError(`Invalid FormatOption param value: ${flag}`);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (options.screenshot_flags) {
|
|
175
|
+
options.screenshot_flags.forEach((flag) => {
|
|
176
|
+
if (!Object.values(ScreenshotFlags).includes(flag as ScreenshotFlags)) {
|
|
177
|
+
throw new ScrapeConfigError(`Invalid ScreenshotFlags param value: ${flag}`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
this.url = options.url;
|
|
182
|
+
this.retry = options.retry ?? this.retry;
|
|
183
|
+
this.method = options.method ?? this.method;
|
|
184
|
+
this.country = options.country ?? this.country;
|
|
185
|
+
this.session_sticky_proxy = options.session_sticky_proxy ?? this.session_sticky_proxy;
|
|
186
|
+
this.render_js = options.render_js ?? this.render_js;
|
|
187
|
+
this.cache = options.cache ?? this.cache;
|
|
188
|
+
this.cache_clear = options.cache_clear ?? this.cache_clear;
|
|
189
|
+
this.cost_budget = options.cost_budget ?? this.cost_budget;
|
|
190
|
+
this.asp = options.asp ?? this.asp;
|
|
191
|
+
this.headers = options.headers
|
|
192
|
+
? Object.fromEntries(Object.entries(options.headers).map(([k, v]) => [k.toLowerCase(), v]))
|
|
193
|
+
: {};
|
|
194
|
+
this.raise_on_upstream_error = options.raise_on_upstream_error ?? this.raise_on_upstream_error;
|
|
195
|
+
this.cache_ttl = options.cache_ttl ?? this.cache_ttl;
|
|
196
|
+
this.proxy_pool = options.proxy_pool ?? this.proxy_pool;
|
|
197
|
+
this.session = options.session ?? this.session;
|
|
198
|
+
this.tags = options.tags ? new Set(options.tags) : this.tags;
|
|
199
|
+
this.format = options.format ?? this.format;
|
|
200
|
+
this.format_options = options.format_options ?? this.format_options;
|
|
201
|
+
this.extraction_template = options.extraction_template ?? this.extraction_template;
|
|
202
|
+
this.extraction_ephemeral_template = options.extraction_ephemeral_template ?? this.extraction_ephemeral_template;
|
|
203
|
+
this.extraction_prompt = options.extraction_prompt ?? this.extraction_prompt;
|
|
204
|
+
this.extraction_model = options.extraction_model ?? this.extraction_model;
|
|
205
|
+
this.correlation_id = options.correlation_id ?? this.correlation_id;
|
|
206
|
+
this.cookies = options.cookies
|
|
207
|
+
? Object.fromEntries(Object.entries(options.cookies).map(([k, v]) => [k.toLowerCase(), v]))
|
|
208
|
+
: {};
|
|
209
|
+
this.body = options.body ?? this.body;
|
|
210
|
+
this.data = options.data ?? this.data;
|
|
211
|
+
this.js = options.js ?? this.js;
|
|
212
|
+
this.rendering_wait = options.rendering_wait ?? this.rendering_wait;
|
|
213
|
+
this.wait_for_selector = options.wait_for_selector ?? this.wait_for_selector;
|
|
214
|
+
this.screenshots = options.screenshots ?? this.screenshots;
|
|
215
|
+
this.screenshot_flags = options.screenshot_flags ?? this.screenshot_flags;
|
|
216
|
+
this.webhook = options.webhook ?? this.webhook;
|
|
217
|
+
this.timeout = options.timeout ?? this.timeout;
|
|
218
|
+
this.js_scenario = options.js_scenario ?? this.js_scenario;
|
|
219
|
+
this.os = options.os ?? this.os;
|
|
220
|
+
this.lang = options.lang ?? this.lang;
|
|
221
|
+
this.auto_scroll = options.auto_scroll ?? this.auto_scroll;
|
|
222
|
+
this.browser_brand = options.browser_brand ?? this.browser_brand;
|
|
223
|
+
this.proxified_response = options.proxified_response ?? this.proxified_response;
|
|
224
|
+
this.rendering_stage = options.rendering_stage ?? this.rendering_stage;
|
|
225
|
+
this.geolocation = options.geolocation ?? this.geolocation;
|
|
226
|
+
this.dns = options.dns ?? this.dns;
|
|
227
|
+
this.ssl = options.ssl ?? this.ssl;
|
|
228
|
+
this.debug = options.debug ?? this.debug;
|
|
229
|
+
if (this.body && this.data) {
|
|
230
|
+
throw new ScrapeConfigError('Cannot set both body and data');
|
|
231
|
+
}
|
|
232
|
+
if (['POST', 'PUT', 'PATCH'].includes(this.method)) {
|
|
233
|
+
if (this.data && !this.body) {
|
|
234
|
+
if (!this.headers['content-type']) {
|
|
235
|
+
this.headers['content-type'] = 'application/x-www-form-urlencoded';
|
|
236
|
+
this.body = new URLSearchParams(this.data).toString();
|
|
237
|
+
} else {
|
|
238
|
+
if (this.headers['content-type']?.includes('application/json')) {
|
|
239
|
+
this.body = JSON.stringify(this.data);
|
|
240
|
+
} else if (this.headers['content-type']?.includes('application/x-www-form-urlencoded')) {
|
|
241
|
+
this.body = new URLSearchParams(this.data).toString();
|
|
242
|
+
} else {
|
|
243
|
+
throw new ScrapeConfigError(
|
|
244
|
+
`Content-Type "${
|
|
245
|
+
this.headers['content-type']
|
|
246
|
+
}" not supported, use body parameter to pass pre encoded body according to your content type`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
} else if (this.body && !this.data && !this.headers['content-type']) {
|
|
251
|
+
this.headers['content-type'] = 'text/plain';
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private validateOptions(options: Partial<ScrapeConfigOptions>) {
|
|
257
|
+
const validKeys = new Set(Object.keys(this) as Array<keyof ScrapeConfig>);
|
|
258
|
+
for (const key in options) {
|
|
259
|
+
if (!validKeys.has(key as keyof ScrapeConfig)) {
|
|
260
|
+
throw new ScrapeConfigError(`Invalid option provided: ${key}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
toApiParams(options: { key: string }): Record<string, any> {
|
|
266
|
+
const params: Record<string, any> = {
|
|
267
|
+
key: options.key,
|
|
268
|
+
};
|
|
269
|
+
params.url = this.url;
|
|
270
|
+
if (this.country) {
|
|
271
|
+
params.country = this.country;
|
|
272
|
+
}
|
|
273
|
+
if (this.headers !== undefined && Object.keys(this.headers).length > 0) {
|
|
274
|
+
Object.entries(this.headers).forEach(([key, value]) => {
|
|
275
|
+
params[`headers[${key}]`] = value;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (this.cookies !== undefined && Object.keys(this.cookies).length > 0) {
|
|
280
|
+
const cookiesAsHeader = [...Object.entries(this.cookies)]
|
|
281
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
282
|
+
.join('; ');
|
|
283
|
+
|
|
284
|
+
if (params['headers[cookie]']) {
|
|
285
|
+
// if current cookie value doesn't have a ';' at the end, add it.
|
|
286
|
+
if (params['headers[cookie]'][params['headers[cookie]'].length - 1] !== ';') {
|
|
287
|
+
params['headers[cookie]'] += ';';
|
|
288
|
+
}
|
|
289
|
+
params['headers[cookie]'] += ` ${cookiesAsHeader}`;
|
|
290
|
+
} else {
|
|
291
|
+
params['headers[cookie]'] = cookiesAsHeader;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
if (this.webhook) {
|
|
295
|
+
params.webhook_name = this.webhook;
|
|
296
|
+
}
|
|
297
|
+
if (this.timeout) {
|
|
298
|
+
params.timeout = this.timeout;
|
|
299
|
+
}
|
|
300
|
+
if (this.cost_budget !== undefined) {
|
|
301
|
+
params.cost_budget = this.cost_budget;
|
|
302
|
+
}
|
|
303
|
+
if (this.proxified_response !== undefined) {
|
|
304
|
+
params.proxified_response = this.proxified_response;
|
|
305
|
+
}
|
|
306
|
+
if (this.render_js === true) {
|
|
307
|
+
params.render_js = true;
|
|
308
|
+
if (this.wait_for_selector !== undefined) {
|
|
309
|
+
params.wait_for_selector = this.wait_for_selector;
|
|
310
|
+
}
|
|
311
|
+
if (this.js !== undefined) {
|
|
312
|
+
params.js = urlsafe_b64encode(this.js);
|
|
313
|
+
}
|
|
314
|
+
if (this.js_scenario !== undefined) {
|
|
315
|
+
params.js_scenario = urlsafe_b64encode(JSON.stringify(this.js_scenario));
|
|
316
|
+
}
|
|
317
|
+
if (this.rendering_wait !== undefined) {
|
|
318
|
+
params.rendering_wait = this.rendering_wait;
|
|
319
|
+
}
|
|
320
|
+
if (this.rendering_stage && this.rendering_stage !== 'complete') {
|
|
321
|
+
params.rendering_stage = this.rendering_stage;
|
|
322
|
+
}
|
|
323
|
+
if (this.geolocation) {
|
|
324
|
+
params.geolocation = this.geolocation;
|
|
325
|
+
}
|
|
326
|
+
if (this.screenshots !== undefined) {
|
|
327
|
+
Object.keys(this.screenshots).forEach((key) => {
|
|
328
|
+
params[`screenshots[${key}]`] = (this.screenshots || {})[key];
|
|
329
|
+
});
|
|
330
|
+
if (this.screenshot_flags) {
|
|
331
|
+
params.screenshot_flags = this.screenshot_flags.join(',');
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
if (this.screenshot_flags) {
|
|
335
|
+
log.warn('Params "screenshot_flags" is ignored. Works only if screenshots is enabled');
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (this.auto_scroll !== undefined) {
|
|
339
|
+
params.auto_scroll = this.auto_scroll;
|
|
340
|
+
}
|
|
341
|
+
} else {
|
|
342
|
+
if (this.wait_for_selector !== undefined) {
|
|
343
|
+
log.warn('Params "wait_for_selector" is ignored. Works only if render_js is enabled');
|
|
344
|
+
}
|
|
345
|
+
if (this.screenshots !== undefined) {
|
|
346
|
+
log.warn('Params "screenshots" is ignored. Works only if render_js is enabled');
|
|
347
|
+
}
|
|
348
|
+
if (this.js_scenario !== undefined) {
|
|
349
|
+
log.warn('Params "js_scenario" is ignored. Works only if render_js is enabled');
|
|
350
|
+
}
|
|
351
|
+
if (this.js !== undefined) {
|
|
352
|
+
log.warn('Params "js" is ignored. Works only if render_js is enabled');
|
|
353
|
+
}
|
|
354
|
+
if (this.rendering_wait !== undefined) {
|
|
355
|
+
log.warn('Params "rendering_wait" is ignored. Works only if render_js is enabled');
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (this.asp === true) {
|
|
360
|
+
params.asp = true;
|
|
361
|
+
}
|
|
362
|
+
if (this.retry === false) {
|
|
363
|
+
params.retry = false;
|
|
364
|
+
}
|
|
365
|
+
if (this.cache === true) {
|
|
366
|
+
params.cache = true;
|
|
367
|
+
if (this.cache_clear) {
|
|
368
|
+
params.cache_clear = true;
|
|
369
|
+
}
|
|
370
|
+
if (this.cache_ttl) {
|
|
371
|
+
params.cache_ttl = this.cache_ttl;
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
if (this.cache_clear) {
|
|
375
|
+
log.warn('Params "cache_clear" is ignored. Works only if cache is enabled');
|
|
376
|
+
}
|
|
377
|
+
if (this.cache_ttl) {
|
|
378
|
+
log.warn('Params "cache_ttl" is ignored. Works only if cache is enabled');
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (this.dns === true) {
|
|
382
|
+
params.dns = true;
|
|
383
|
+
}
|
|
384
|
+
if (this.ssl === true) {
|
|
385
|
+
params.ssl = true;
|
|
386
|
+
}
|
|
387
|
+
if (this.tags.size > 0) {
|
|
388
|
+
params.tags = Array.from(this.tags).join(',');
|
|
389
|
+
}
|
|
390
|
+
if (this.format) {
|
|
391
|
+
params.format = this.format.valueOf();
|
|
392
|
+
if (this.format_options) {
|
|
393
|
+
params.format += ':' + this.format_options.join(',');
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (this.extraction_template && this.extraction_ephemeral_template) {
|
|
397
|
+
throw new ScrapeConfigError(
|
|
398
|
+
'You cannot pass both parameters extraction_template and extraction_ephemeral_template. You must choose',
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (this.extraction_template) {
|
|
403
|
+
params.extraction_template = this.extraction_template;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (this.extraction_ephemeral_template) {
|
|
407
|
+
params.extraction_template = 'ephemeral:' + urlsafe_b64encode(JSON.stringify(this.extraction_ephemeral_template));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (this.extraction_prompt) {
|
|
411
|
+
params.extraction_prompt = this.extraction_prompt;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (this.extraction_model) {
|
|
415
|
+
params.extraction_model = this.extraction_model;
|
|
416
|
+
}
|
|
417
|
+
if (this.correlation_id) {
|
|
418
|
+
params.correlation_id = this.correlation_id;
|
|
419
|
+
}
|
|
420
|
+
if (this.session) {
|
|
421
|
+
params.session = this.session;
|
|
422
|
+
if (this.session_sticky_proxy) {
|
|
423
|
+
params.session_sticky_proxy = true;
|
|
424
|
+
}
|
|
425
|
+
} else {
|
|
426
|
+
if (this.session_sticky_proxy) {
|
|
427
|
+
log.warn('Params "session_sticky_proxy" is ignored. Works only if session is enabled');
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if (this.debug === true) {
|
|
431
|
+
params.debug = true;
|
|
432
|
+
}
|
|
433
|
+
if (this.proxy_pool) {
|
|
434
|
+
params.proxy_pool = this.proxy_pool;
|
|
435
|
+
}
|
|
436
|
+
if (this.lang !== undefined) {
|
|
437
|
+
params.lang = this.lang.join(',');
|
|
438
|
+
}
|
|
439
|
+
if (this.os !== undefined) {
|
|
440
|
+
params.os = this.os;
|
|
441
|
+
}
|
|
442
|
+
if (this.browser_brand !== undefined) {
|
|
443
|
+
params.browser_brand = this.browser_brand;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return params;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { ScreenshotConfigError } from './errors.js';
|
|
2
|
+
import { urlsafe_b64encode } from './utils.js';
|
|
3
|
+
import { log } from './logger.js';
|
|
4
|
+
|
|
5
|
+
export enum Options {
|
|
6
|
+
/**
|
|
7
|
+
Options to customize the screenshot behavior
|
|
8
|
+
Attributes:
|
|
9
|
+
LOAD_IMAGES: Enable image rendering with the request, add extra usage for the bandwidth consumed.
|
|
10
|
+
DARK_MODE: Enable dark mode display.
|
|
11
|
+
BLOCK_BANNERS: Block cookies banners and overlay that cover the screen.
|
|
12
|
+
PRINT_MEDIA_FORMAT: Render the page in the print mode.
|
|
13
|
+
*/
|
|
14
|
+
LOAD_IMAGES = 'load_images',
|
|
15
|
+
DARK_MODE = 'dark_mode',
|
|
16
|
+
BLOCK_BANNERS = 'block_banners',
|
|
17
|
+
PRINT_MEDIA_FORMAT = 'print_media_format',
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export enum Format {
|
|
21
|
+
/**
|
|
22
|
+
Format of the screenshot image.
|
|
23
|
+
Attributes:
|
|
24
|
+
JPG: JPG format.
|
|
25
|
+
PNG: PNG format.
|
|
26
|
+
WEBP: WEBP format.
|
|
27
|
+
GIF: GIF format.
|
|
28
|
+
*/
|
|
29
|
+
JPG = 'jpg',
|
|
30
|
+
PNG = 'png',
|
|
31
|
+
WEBP = 'webp',
|
|
32
|
+
GIF = 'gif',
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export enum VisionDeficiency {
|
|
36
|
+
/**
|
|
37
|
+
Simulate vision deficiency for accessibility testing (WCAG compliance)
|
|
38
|
+
Attributes:
|
|
39
|
+
DEUTERANOPIA: Difficulty distinguishing green from red; green appears beige/gray
|
|
40
|
+
PROTANOPIA: Reduced sensitivity to red light; red appears dark/black
|
|
41
|
+
TRITANOPIA: Difficulty distinguishing blue from yellow and violet from red
|
|
42
|
+
ACHROMATOPSIA: Complete inability to perceive color; sees only in grayscale
|
|
43
|
+
REDUCED_CONTRAST: Simulates reduced contrast due to aging, low light, or other factors
|
|
44
|
+
BLURRED_VISION: Simulates uncorrected refractive errors or age-related vision loss
|
|
45
|
+
*/
|
|
46
|
+
DEUTERANOPIA = 'deuteranopia',
|
|
47
|
+
PROTANOPIA = 'protanopia',
|
|
48
|
+
TRITANOPIA = 'tritanopia',
|
|
49
|
+
ACHROMATOPSIA = 'achromatopsia',
|
|
50
|
+
REDUCED_CONTRAST = 'reducedContrast',
|
|
51
|
+
BLURRED_VISION = 'blurredVision',
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
type ScreenshotConfigOptions = {
|
|
55
|
+
url: string;
|
|
56
|
+
format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
|
|
57
|
+
capture?: string;
|
|
58
|
+
resolution?: string;
|
|
59
|
+
country?: string;
|
|
60
|
+
timeout?: number;
|
|
61
|
+
rendering_wait?: number;
|
|
62
|
+
wait_for_selector?: string;
|
|
63
|
+
options?: ('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | Options)[];
|
|
64
|
+
auto_scroll?: boolean;
|
|
65
|
+
js?: string;
|
|
66
|
+
cache?: boolean;
|
|
67
|
+
cache_ttl?: number;
|
|
68
|
+
cache_clear?: boolean;
|
|
69
|
+
vision_deficiency?: 'deuteranopia' | 'protanopia' | 'tritanopia' | 'achromatopsia' | 'blurredVision' | 'reducedContrast' | VisionDeficiency;
|
|
70
|
+
webhook?: string;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export class ScreenshotConfig {
|
|
74
|
+
url: string;
|
|
75
|
+
format?: 'jpg' | 'png' | 'webp' | 'gif' | Format;
|
|
76
|
+
capture?: string;
|
|
77
|
+
resolution?: string;
|
|
78
|
+
country?: string = undefined;
|
|
79
|
+
timeout?: number;
|
|
80
|
+
rendering_wait?: number;
|
|
81
|
+
wait_for_selector?: string;
|
|
82
|
+
options?: ('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | Options)[];
|
|
83
|
+
auto_scroll?: boolean;
|
|
84
|
+
js?: string;
|
|
85
|
+
cache?: boolean;
|
|
86
|
+
cache_ttl?: number;
|
|
87
|
+
cache_clear?: boolean;
|
|
88
|
+
vision_deficiency?: 'deuteranopia' | 'protanopia' | 'tritanopia' | 'achromatopsia' | 'blurredVision' | 'reducedContrast' | VisionDeficiency;
|
|
89
|
+
webhook?: string;
|
|
90
|
+
|
|
91
|
+
constructor(options: ScreenshotConfigOptions) {
|
|
92
|
+
this.validateOptions(options);
|
|
93
|
+
if (options.format && !Object.values(Format).includes(options.format as Format)) {
|
|
94
|
+
throw new ScreenshotConfigError(`Invalid Format param value: ${options.format}`);
|
|
95
|
+
}
|
|
96
|
+
this.format = options.format ?? this.format;
|
|
97
|
+
if (options.options) {
|
|
98
|
+
options.options.forEach((opt) => {
|
|
99
|
+
if (!Object.values(Options).includes(opt as Options)) {
|
|
100
|
+
throw new ScreenshotConfigError(`Invalid Options param value: ${opt}`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
this.url = options.url;
|
|
105
|
+
this.format = options.format ?? this.format;
|
|
106
|
+
this.capture = options.capture ?? this.capture;
|
|
107
|
+
this.resolution = options.resolution ?? this.resolution;
|
|
108
|
+
this.country = options.country ?? this.country;
|
|
109
|
+
this.timeout = options.timeout ?? this.timeout;
|
|
110
|
+
this.rendering_wait = options.rendering_wait ?? this.rendering_wait;
|
|
111
|
+
this.wait_for_selector = options.wait_for_selector ?? this.wait_for_selector;
|
|
112
|
+
this.options = options.options ?? this.options;
|
|
113
|
+
this.auto_scroll = options.auto_scroll ?? this.auto_scroll;
|
|
114
|
+
this.js = options.js ?? this.js;
|
|
115
|
+
this.cache = options.cache ?? this.cache;
|
|
116
|
+
this.cache_ttl = options.cache_ttl ?? this.cache_ttl;
|
|
117
|
+
this.cache_clear = options.cache_clear ?? this.cache_clear;
|
|
118
|
+
this.vision_deficiency = options.vision_deficiency ?? this.vision_deficiency;
|
|
119
|
+
this.webhook = options.webhook;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private validateOptions(options: Partial<ScreenshotConfigOptions>) {
|
|
123
|
+
const validKeys = new Set(Object.keys(this) as Array<keyof ScreenshotConfig>);
|
|
124
|
+
for (const key in options) {
|
|
125
|
+
if (!validKeys.has(key as keyof ScreenshotConfig)) {
|
|
126
|
+
throw new ScreenshotConfigError(`Invalid option provided: ${key}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
toApiParams(options: { key: string }): Record<string, any> {
|
|
132
|
+
const params: Record<string, any> = {
|
|
133
|
+
key: options.key,
|
|
134
|
+
};
|
|
135
|
+
params.url = this.url;
|
|
136
|
+
|
|
137
|
+
if (this.format) {
|
|
138
|
+
params.format = this.format.valueOf();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (this.capture) {
|
|
142
|
+
params.capture = this.capture;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (this.resolution) {
|
|
146
|
+
params.resolution = this.resolution;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (this.country) {
|
|
150
|
+
params.country = this.country;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (this.timeout) {
|
|
154
|
+
params.timeout = this.timeout;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (this.rendering_wait) {
|
|
158
|
+
params.rendering_wait = this.rendering_wait;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (this.wait_for_selector) {
|
|
162
|
+
params.wait_for_selector = this.wait_for_selector;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
if (this.options) {
|
|
166
|
+
params.options = this.options.join(',');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (this.auto_scroll === true) {
|
|
170
|
+
params.auto_scroll = this.auto_scroll;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (this.js) {
|
|
174
|
+
params.js = urlsafe_b64encode(this.js);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (this.cache === true) {
|
|
178
|
+
params.cache = this.cache;
|
|
179
|
+
if (this.cache_ttl) {
|
|
180
|
+
params.cache_ttl = this.cache_ttl;
|
|
181
|
+
}
|
|
182
|
+
if (this.cache_clear === true) {
|
|
183
|
+
params.cache_clear = this.cache_clear;
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
if (this.cache_ttl) {
|
|
187
|
+
log.warn('Params "cache_ttl" is ignored. Works only if cache is enabled');
|
|
188
|
+
}
|
|
189
|
+
if (this.cache_clear) {
|
|
190
|
+
log.warn('Params "cache_clear" is ignored. Works only if cache is enabled');
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (this.vision_deficiency) {
|
|
195
|
+
params.vision_deficiency = this.vision_deficiency;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (this.webhook) {
|
|
199
|
+
params.webhook_name = this.webhook;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return params;
|
|
203
|
+
}
|
|
204
|
+
}
|
package/src/types.ts
ADDED