scrapfly-sdk 0.12.0 → 0.12.1
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/README.md +47 -13
- package/esm/browserconfig.d.ts +20 -4
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +12 -2
- package/esm/browserdownloads.d.ts +70 -0
- package/esm/browserdownloads.d.ts.map +1 -0
- package/esm/browserdownloads.js +215 -0
- package/esm/client.d.ts +107 -4
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +307 -24
- package/esm/crawl.d.ts +26 -1
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +18 -0
- package/esm/crawlerconfig.d.ts +19 -6
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +5 -2
- package/esm/crawlerresult.d.ts +39 -0
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/main.d.ts +10 -5
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +8 -3
- package/esm/monitoringconfig.d.ts +2 -2
- package/esm/result.d.ts +11 -1
- package/esm/result.d.ts.map +1 -1
- package/esm/result.js +14 -5
- package/esm/scrapeconfig.d.ts +38 -10
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +48 -6
- package/esm/screenshotconfig.d.ts +9 -1
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/screenshotconfig.js +12 -1
- package/esm/types.d.ts +128 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +22 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -2
- package/package.json +1 -1
- package/script/browserconfig.d.ts +20 -4
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +12 -2
- package/script/browserdownloads.d.ts +70 -0
- package/script/browserdownloads.d.ts.map +1 -0
- package/script/browserdownloads.js +221 -0
- package/script/client.d.ts +107 -4
- package/script/client.d.ts.map +1 -1
- package/script/client.js +307 -24
- package/script/crawl.d.ts +26 -1
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +18 -0
- package/script/crawlerconfig.d.ts +19 -6
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +5 -2
- package/script/crawlerresult.d.ts +39 -0
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/main.d.ts +10 -5
- package/script/main.d.ts.map +1 -1
- package/script/main.js +13 -4
- package/script/monitoringconfig.d.ts +2 -2
- package/script/result.d.ts +11 -1
- package/script/result.d.ts.map +1 -1
- package/script/result.js +14 -5
- package/script/scrapeconfig.d.ts +38 -10
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +48 -6
- package/script/screenshotconfig.d.ts +9 -1
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/screenshotconfig.js +12 -1
- package/script/types.d.ts +128 -0
- package/script/types.d.ts.map +1 -1
- package/script/types.js +23 -0
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +16 -2
- package/src/browserconfig.ts +22 -4
- package/src/browserdownloads.ts +266 -0
- package/src/client.ts +390 -27
- package/src/crawl.ts +32 -0
- package/src/crawlerconfig.ts +19 -6
- package/src/crawlerresult.ts +41 -0
- package/src/main.ts +31 -4
- package/src/monitoringconfig.ts +2 -2
- package/src/result.ts +17 -5
- package/src/scrapeconfig.ts +47 -10
- package/src/screenshotconfig.ts +14 -1
- package/src/types.ts +159 -0
- package/src/utils.ts +16 -2
package/README.md
CHANGED
|
@@ -41,10 +41,8 @@ const apiResponse = await client.scrape(
|
|
|
41
41
|
render_js: true,
|
|
42
42
|
// set proxy country
|
|
43
43
|
country: 'us',
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
// set residential proxies
|
|
47
|
-
proxy_pool: 'public_residential_pool',
|
|
44
|
+
// proxy pool: basic, unblocker (anti-bot bypass), unblocker_ultra or auto
|
|
45
|
+
proxy_pool: ScrapeConfig.UNBLOCKER,
|
|
48
46
|
// etc.
|
|
49
47
|
}),
|
|
50
48
|
);
|
|
@@ -57,14 +55,47 @@ For more see [/examples](/examples/) directory.
|
|
|
57
55
|
For more on Scrapfly API see our [getting started documentation](https://scrapfly.io/docs/scrape-api/getting-started)
|
|
58
56
|
For Python see [Scrapfly Python SDK](https://github.com/scrapfly/python-scrapfly)
|
|
59
57
|
|
|
60
|
-
###
|
|
58
|
+
### Proxy pools
|
|
61
59
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
`proxy_pool` picks one of four proxy pools. The pool sets both the proxy
|
|
61
|
+
network and the anti-bot bypass (the Unblocker):
|
|
62
|
+
|
|
63
|
+
| Static | Value | What it runs |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `ScrapeConfig.BASIC` | `'basic'` | datacenter proxies, Unblocker off |
|
|
66
|
+
| `ScrapeConfig.UNBLOCKER` | `'unblocker'` | residential proxies and the Unblocker |
|
|
67
|
+
| `ScrapeConfig.UNBLOCKER_ULTRA` | `'unblocker_ultra'` | residential proxies and the Unblocker, the only pool accepted by the targets and anti-bot protections that have a price of their own |
|
|
68
|
+
| `ScrapeConfig.AUTO` | `'auto'` | the API picks the pool before the request, from the target's profile: the Unblocker on datacenter proxies, or the pool the target needs; accepted on every target, the ones that require `unblocker_ultra` included, where their price is added; priced as the pool it runs on |
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const result = await client.scrape(
|
|
72
|
+
new ScrapeConfig({ url: 'https://web-scraping.dev/product/1', proxy_pool: ScrapeConfig.UNBLOCKER }),
|
|
73
|
+
);
|
|
74
|
+
new CrawlerConfig({ url: 'https://web-scraping.dev/', proxy_pool: ScrapeConfig.UNBLOCKER });
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
A request on `basic` or `unblocker` against a target or protection that has a
|
|
78
|
+
price of its own fails with `ERR::PROXY::UNBLOCKER_ULTRA_REQUIRED` (HTTP 422,
|
|
79
|
+
not billed); retry it with `ScrapeConfig.UNBLOCKER_ULTRA`. `basic` is billed at
|
|
80
|
+
the datacenter price and both Unblocker pools at the residential price;
|
|
81
|
+
`unblocker_ultra` adds the target's or protection's own price where it has one.
|
|
82
|
+
The SDK sends no default `proxy_pool`.
|
|
83
|
+
|
|
84
|
+
`ScreenshotConfig` takes the same four pools, at the same price on every pool;
|
|
85
|
+
any other value is ignored there. The Cloud Browser (`BrowserConfig`)
|
|
86
|
+
keeps its own pools: `datacenter`, `residential`, `tor` and `byop`.
|
|
87
|
+
|
|
88
|
+
### Deprecated: `unblocker` and `asp`
|
|
89
|
+
|
|
90
|
+
`unblocker` and its alias `asp`, in both `ScrapeConfig` and `CrawlerConfig`, are
|
|
91
|
+
deprecated: use `proxy_pool` (`ScrapeConfig.UNBLOCKER_ULTRA` for the Unblocker).
|
|
92
|
+
The [migration guide](https://scrapfly.io/new-pricing#legacy-spellings) covers
|
|
93
|
+
the requests written before the proxy pools, the pool names
|
|
94
|
+
`ScrapeConfig.PUBLIC_DATACENTER_POOL` and `ScrapeConfig.PUBLIC_RESIDENTIAL_POOL`
|
|
95
|
+
included.
|
|
65
96
|
|
|
66
97
|
```javascript
|
|
67
|
-
new ScrapeConfig({ url: 'https://web-scraping.dev/product/1', unblocker: true });
|
|
98
|
+
new ScrapeConfig({ url: 'https://web-scraping.dev/product/1', unblocker: true }); // deprecated
|
|
68
99
|
new ScrapeConfig({ url: 'https://web-scraping.dev/product/1', asp: true }); // same thing
|
|
69
100
|
```
|
|
70
101
|
|
|
@@ -79,13 +110,16 @@ query and in the `POST /crawl` body, and only when the feature is ON - both
|
|
|
79
110
|
"off" and "not supplied" drop the key, so the request is byte-identical to the
|
|
80
111
|
one the Python, Go and Rust SDKs build for the same intent.
|
|
81
112
|
|
|
82
|
-
The matching error class is `ScrapflyUnblockerError`, the same class
|
|
83
|
-
`ScrapflyAspError`, so either name works in a `catch
|
|
113
|
+
The matching error class is `errors.ScrapflyUnblockerError`, the same class
|
|
114
|
+
object as `errors.ScrapflyAspError`, so either name works in a `catch`. The API
|
|
115
|
+
error classes (`ScrapflyError` and its subclasses) are exported under the
|
|
116
|
+
`errors` namespace, not at the package root; `BrowserDownloadError` and
|
|
117
|
+
`ScheduleAPIError` are root exports:
|
|
84
118
|
|
|
85
119
|
```javascript
|
|
86
|
-
import {
|
|
120
|
+
import { errors } from 'scrapfly-sdk';
|
|
87
121
|
|
|
88
|
-
if (err instanceof ScrapflyUnblockerError) {
|
|
122
|
+
if (err instanceof errors.ScrapflyUnblockerError) {
|
|
89
123
|
// the unblocker could not get through the target's protection
|
|
90
124
|
}
|
|
91
125
|
```
|
package/esm/browserconfig.d.ts
CHANGED
|
@@ -72,9 +72,17 @@ export type BrowserConfigOptions = {
|
|
|
72
72
|
cache?: boolean;
|
|
73
73
|
/** Apply Scrapfly's abuse blacklist. */
|
|
74
74
|
blacklist?: boolean;
|
|
75
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* @deprecated The Cloud Browser WebSocket endpoint does not read this
|
|
77
|
+
* parameter; it is sent but has no effect. Use
|
|
78
|
+
* {@link ScrapflyClient.cloudBrowserUnblock} (the `/unblock` endpoint).
|
|
79
|
+
*/
|
|
76
80
|
unblock?: boolean;
|
|
77
|
-
/**
|
|
81
|
+
/**
|
|
82
|
+
* @deprecated The Cloud Browser WebSocket endpoint does not read this
|
|
83
|
+
* parameter; it is sent but has no effect. Use
|
|
84
|
+
* {@link ScrapflyClient.cloudBrowserUnblock}, which takes its own timeouts.
|
|
85
|
+
*/
|
|
78
86
|
unblock_timeout?: number;
|
|
79
87
|
/** Browser brand to emulate (e.g. "chrome", "firefox"). */
|
|
80
88
|
browser_brand?: string;
|
|
@@ -117,6 +125,12 @@ export type BrowserConfigOptions = {
|
|
|
117
125
|
* comma-separated string or an array of bare IPs / CIDRs.
|
|
118
126
|
*/
|
|
119
127
|
hitl_allowed_networks?: string | string[];
|
|
128
|
+
/**
|
|
129
|
+
* Return large downloaded files from `ScrapiumBrowser.getDownloads` as IO stream handles instead of
|
|
130
|
+
* inline base64: read them with `CloudBrowserDownloads`.
|
|
131
|
+
* See https://scrapfly.io/docs/cloud-browser-api/file-downloads#large-downloads
|
|
132
|
+
*/
|
|
133
|
+
downloads_stream?: boolean;
|
|
120
134
|
};
|
|
121
135
|
/**
|
|
122
136
|
* Configuration for a Scrapfly Cloud Browser session. Builds the
|
|
@@ -162,9 +176,9 @@ export declare class BrowserConfig {
|
|
|
162
176
|
cache?: boolean;
|
|
163
177
|
/** See {@link BrowserConfigOptions.blacklist}. */
|
|
164
178
|
blacklist?: boolean;
|
|
165
|
-
/**
|
|
179
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock}. */
|
|
166
180
|
unblock?: boolean;
|
|
167
|
-
/**
|
|
181
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock_timeout}. */
|
|
168
182
|
unblock_timeout?: number;
|
|
169
183
|
/** See {@link BrowserConfigOptions.browser_brand}. */
|
|
170
184
|
browser_brand?: string;
|
|
@@ -188,6 +202,8 @@ export declare class BrowserConfig {
|
|
|
188
202
|
rtc_password?: string;
|
|
189
203
|
/** See {@link BrowserConfigOptions.hitl_allowed_networks}. */
|
|
190
204
|
hitl_allowed_networks?: string;
|
|
205
|
+
/** See {@link BrowserConfigOptions.downloads_stream}. */
|
|
206
|
+
downloads_stream?: boolean;
|
|
191
207
|
/**
|
|
192
208
|
* @param options Session options. See {@link BrowserConfigOptions}.
|
|
193
209
|
* @throws `Error` if `timeout` exceeds the upper bound of 1800 seconds.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"browserconfig.d.ts","sourceRoot":"","sources":["../src/browserconfig.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,oBAAY,SAAS;IACnB,2EAA2E;IAC3E,UAAU,eAAe;IACzB,iFAAiF;IACjF,WAAW,gBAAgB;CAC5B;AAED,6EAA6E;AAC7E,oBAAY,eAAe;IACzB,2CAA2C;IAC3C,KAAK,UAAU;IACf,6CAA6C;IAC7C,OAAO,YAAY;IACnB,2CAA2C;IAC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U,CAAC,EAAE,OAAO,CAAC;IACrB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,qDAAqD;IACrD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,8DAA8D;IAC9D,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,yDAAyD;IACzD,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;;OAGG;gBACS,OAAO,GAAE,oBAAyB;IAmD9C;;;;;;;;OAQG;WACU,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAWzD;;;;;;;OAOG;IACG,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAQxD;;;;;;;;OAQG;IACH,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM;CAyCpD"}
|
package/esm/browserconfig.js
CHANGED
|
@@ -166,14 +166,14 @@ export class BrowserConfig {
|
|
|
166
166
|
writable: true,
|
|
167
167
|
value: void 0
|
|
168
168
|
});
|
|
169
|
-
/**
|
|
169
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock}. */
|
|
170
170
|
Object.defineProperty(this, "unblock", {
|
|
171
171
|
enumerable: true,
|
|
172
172
|
configurable: true,
|
|
173
173
|
writable: true,
|
|
174
174
|
value: void 0
|
|
175
175
|
});
|
|
176
|
-
/**
|
|
176
|
+
/** @deprecated Not read by the API, see {@link BrowserConfigOptions.unblock_timeout}. */
|
|
177
177
|
Object.defineProperty(this, "unblock_timeout", {
|
|
178
178
|
enumerable: true,
|
|
179
179
|
configurable: true,
|
|
@@ -257,6 +257,13 @@ export class BrowserConfig {
|
|
|
257
257
|
writable: true,
|
|
258
258
|
value: void 0
|
|
259
259
|
});
|
|
260
|
+
/** See {@link BrowserConfigOptions.downloads_stream}. */
|
|
261
|
+
Object.defineProperty(this, "downloads_stream", {
|
|
262
|
+
enumerable: true,
|
|
263
|
+
configurable: true,
|
|
264
|
+
writable: true,
|
|
265
|
+
value: void 0
|
|
266
|
+
});
|
|
260
267
|
if (options.timeout !== undefined && options.timeout > 1800) {
|
|
261
268
|
throw new Error('timeout cannot exceed 1800 seconds (30 minutes)');
|
|
262
269
|
}
|
|
@@ -306,6 +313,7 @@ export class BrowserConfig {
|
|
|
306
313
|
else {
|
|
307
314
|
this.hitl_allowed_networks = options.hitl_allowed_networks || undefined;
|
|
308
315
|
}
|
|
316
|
+
this.downloads_stream = options.downloads_stream;
|
|
309
317
|
}
|
|
310
318
|
/**
|
|
311
319
|
* Return the deterministic project salt for an api_key (`sha256(api_key)[:8]`).
|
|
@@ -417,6 +425,8 @@ export class BrowserConfig {
|
|
|
417
425
|
params.set('rtc_password', this.rtc_password);
|
|
418
426
|
if (this.hitl_allowed_networks !== undefined)
|
|
419
427
|
params.set('hitl_allowed_networks', this.hitl_allowed_networks);
|
|
428
|
+
if (this.downloads_stream !== undefined)
|
|
429
|
+
params.set('downloads_stream', String(this.downloads_stream));
|
|
420
430
|
const baseHost = host || 'wss://browser.scrapfly.io';
|
|
421
431
|
return `${baseHost}?${params.toString()}`;
|
|
422
432
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** Base64 characters per `IO.read`: an 8 MiB message carrying 6 MiB of the file. */
|
|
2
|
+
export declare const DEFAULT_CHUNK_SIZE: number;
|
|
3
|
+
/** The largest chunk the Cloud Browser serves per `IO.read`. */
|
|
4
|
+
export declare const MAX_CHUNK_SIZE: number;
|
|
5
|
+
/** A CDP session: Puppeteer's or Playwright's `CDPSession`, or a chrome-remote-interface client. */
|
|
6
|
+
export interface CdpSession {
|
|
7
|
+
send(method: any, params?: any): Promise<any>;
|
|
8
|
+
}
|
|
9
|
+
/** A function sending one CDP command and resolving to its result. */
|
|
10
|
+
export type CdpSend = (method: string, params?: Record<string, unknown>) => Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* One file of the session's download directory. A small file carries its base64 content in `data`. A large
|
|
13
|
+
* one, with `downloads_stream`, carries the IO handle to read in `stream` instead, and its size in bytes.
|
|
14
|
+
*/
|
|
15
|
+
export interface BrowserDownload {
|
|
16
|
+
name: string;
|
|
17
|
+
guid?: string;
|
|
18
|
+
size?: number;
|
|
19
|
+
data?: string;
|
|
20
|
+
stream?: string;
|
|
21
|
+
}
|
|
22
|
+
/** A download could not be read whole. */
|
|
23
|
+
export declare class BrowserDownloadError extends Error {
|
|
24
|
+
constructor(message: string);
|
|
25
|
+
}
|
|
26
|
+
interface StreamEntry {
|
|
27
|
+
stream: string;
|
|
28
|
+
size?: number;
|
|
29
|
+
name?: string;
|
|
30
|
+
}
|
|
31
|
+
interface GetDownloadsResult {
|
|
32
|
+
files?: Record<string, string>;
|
|
33
|
+
files_by_guid?: Record<string, string>;
|
|
34
|
+
guid_names?: Record<string, string>;
|
|
35
|
+
streams?: Record<string, StreamEntry>;
|
|
36
|
+
}
|
|
37
|
+
/** The downloads of a `ScrapiumBrowser.getDownloads` result, in either of its shapes. */
|
|
38
|
+
export declare function parseDownloads(result: GetDownloadsResult): BrowserDownload[];
|
|
39
|
+
export interface CloudBrowserDownloadsOptions {
|
|
40
|
+
/** Base64 characters per `IO.read`, rounded down to a multiple of 4. Defaults to 8 MiB, 32 MiB at most. */
|
|
41
|
+
chunkSize?: number;
|
|
42
|
+
}
|
|
43
|
+
export interface WaitForOptions {
|
|
44
|
+
/** Milliseconds before giving up. Defaults to 60000. */
|
|
45
|
+
timeout?: number;
|
|
46
|
+
/** Milliseconds between two checks. Defaults to 500. */
|
|
47
|
+
interval?: number;
|
|
48
|
+
}
|
|
49
|
+
/** Download helpers over a CDP session of a Cloud Browser. */
|
|
50
|
+
export declare class CloudBrowserDownloads {
|
|
51
|
+
readonly chunkSize: number;
|
|
52
|
+
private readonly sendCommand;
|
|
53
|
+
constructor(session: CdpSession | CdpSend, options?: CloudBrowserDownloadsOptions);
|
|
54
|
+
/** The size in bytes of every completed download, by filename. */
|
|
55
|
+
metadata(): Promise<Record<string, number>>;
|
|
56
|
+
/** Wait until `filename` is listed with the same size twice in a row, and resolve to that size. */
|
|
57
|
+
waitFor(filename: string, options?: WaitForOptions): Promise<number>;
|
|
58
|
+
/** Every download of the session: small ones with their content, large ones as handles. */
|
|
59
|
+
list(): Promise<BrowserDownload[]>;
|
|
60
|
+
/** The bytes of a download, one decoded chunk at a time. A stream handle is read once. */
|
|
61
|
+
chunks(download: BrowserDownload): AsyncGenerator<Uint8Array>;
|
|
62
|
+
/** The whole content of a download, in memory. */
|
|
63
|
+
read(download: BrowserDownload): Promise<Uint8Array>;
|
|
64
|
+
/** Write a download to `path` (a file or a directory, the current one by default) chunk by chunk. */
|
|
65
|
+
save(download: BrowserDownload, path?: string): Promise<string>;
|
|
66
|
+
/** Write every download of the session into `directory` and resolve to their paths. */
|
|
67
|
+
saveAll(directory?: string): Promise<string[]>;
|
|
68
|
+
}
|
|
69
|
+
export {};
|
|
70
|
+
//# sourceMappingURL=browserdownloads.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browserdownloads.d.ts","sourceRoot":"","sources":["../src/browserdownloads.ts"],"names":[],"mappings":"AAoBA,oFAAoF;AACpF,eAAO,MAAM,kBAAkB,QAAkB,CAAC;AAClD,gEAAgE;AAChE,eAAO,MAAM,cAAc,QAAmB,CAAC;AAK/C,oGAAoG;AACpG,MAAM,WAAW,UAAU;IAEzB,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;CAC/C;AAED,sEAAsE;AAEtE,MAAM,MAAM,OAAO,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAEzF;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,I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G;IAC7F,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,MAAM,CAAC;IAgB9E,2FAA2F;IACrF,IAAI,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC;IAIxC,0FAA0F;IACnF,MAAM,CAAC,QAAQ,EAAE,eAAe,GAAG,cAAc,CAAC,UAAU,CAAC;IA2BpE,kDAAkD;IAC5C,IAAI,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,UAAU,CAAC;IAgB1D,qGAAqG;IAC/F,IAAI,CAAC,QAAQ,EAAE,eAAe,EAAE,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAarE,uFAAuF;IACjF,OAAO,CAAC,SAAS,SAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAQlD"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieve the files a Cloud Browser session downloaded.
|
|
3
|
+
*
|
|
4
|
+
* Connect with `new BrowserConfig({ downloads_stream: true })`: `ScrapiumBrowser.getDownloads` then returns
|
|
5
|
+
* each small file inline and each large one as an IO stream handle, which these helpers read in chunks with
|
|
6
|
+
* `IO.read`, so no CDP message is larger than one chunk. Without the option every file comes back inline,
|
|
7
|
+
* and the helpers read that shape too.
|
|
8
|
+
*
|
|
9
|
+
* A session is anything with a `send(method, params)` method answering the CDP result: Puppeteer's and
|
|
10
|
+
* Playwright's `CDPSession`, a chrome-remote-interface client, or such a function.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const downloads = new CloudBrowserDownloads(await page.createCDPSession());
|
|
14
|
+
* await downloads.waitFor('report.pdf');
|
|
15
|
+
* const paths = await downloads.saveAll('downloads');
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import { mkdir, open, stat } from 'node:fs/promises';
|
|
19
|
+
import { basename, join } from 'node:path';
|
|
20
|
+
/** Base64 characters per `IO.read`: an 8 MiB message carrying 6 MiB of the file. */
|
|
21
|
+
export const DEFAULT_CHUNK_SIZE = 8 * 1024 * 1024;
|
|
22
|
+
/** The largest chunk the Cloud Browser serves per `IO.read`. */
|
|
23
|
+
export const MAX_CHUNK_SIZE = 32 * 1024 * 1024;
|
|
24
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 60_000;
|
|
25
|
+
const DEFAULT_WAIT_INTERVAL_MS = 500;
|
|
26
|
+
/** A download could not be read whole. */
|
|
27
|
+
export class BrowserDownloadError extends Error {
|
|
28
|
+
constructor(message) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'BrowserDownloadError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** The downloads of a `ScrapiumBrowser.getDownloads` result, in either of its shapes. */
|
|
34
|
+
export function parseDownloads(result) {
|
|
35
|
+
const downloads = [];
|
|
36
|
+
if (result.guid_names !== undefined || result.streams !== undefined) {
|
|
37
|
+
const guidNames = result.guid_names ?? {};
|
|
38
|
+
for (const [guid, data] of Object.entries(result.files_by_guid ?? {})) {
|
|
39
|
+
downloads.push({ name: guidNames[guid] || guid, guid, size: decodedSize(data), data });
|
|
40
|
+
}
|
|
41
|
+
for (const [guid, entry] of Object.entries(result.streams ?? {})) {
|
|
42
|
+
downloads.push({ name: entry.name || guidNames[guid] || guid, guid, size: entry.size, stream: entry.stream });
|
|
43
|
+
}
|
|
44
|
+
// A file on disk with no download GUID, such as one finished before this session attached.
|
|
45
|
+
for (const [name, data] of Object.entries(result.files ?? {})) {
|
|
46
|
+
downloads.push({ name, size: decodedSize(data), data });
|
|
47
|
+
}
|
|
48
|
+
return downloads;
|
|
49
|
+
}
|
|
50
|
+
// Without downloads_stream every file is inline under `files`, and again under `files_by_guid` with no
|
|
51
|
+
// name: `files` alone holds each file once.
|
|
52
|
+
for (const [name, data] of Object.entries(result.files ?? {})) {
|
|
53
|
+
downloads.push({ name, size: decodedSize(data), data });
|
|
54
|
+
}
|
|
55
|
+
return downloads;
|
|
56
|
+
}
|
|
57
|
+
function decodedSize(data) {
|
|
58
|
+
let padding = 0;
|
|
59
|
+
if (data.endsWith('==')) {
|
|
60
|
+
padding = 2;
|
|
61
|
+
}
|
|
62
|
+
else if (data.endsWith('=')) {
|
|
63
|
+
padding = 1;
|
|
64
|
+
}
|
|
65
|
+
return Math.floor(data.length / 4) * 3 - padding;
|
|
66
|
+
}
|
|
67
|
+
function decodeBase64(data) {
|
|
68
|
+
const binary = atob(data);
|
|
69
|
+
const bytes = new Uint8Array(binary.length);
|
|
70
|
+
for (let i = 0; i < binary.length; i++) {
|
|
71
|
+
bytes[i] = binary.charCodeAt(i);
|
|
72
|
+
}
|
|
73
|
+
return bytes;
|
|
74
|
+
}
|
|
75
|
+
function checkChunkSize(chunkSize) {
|
|
76
|
+
if (!Number.isInteger(chunkSize) || chunkSize < 4 || chunkSize > MAX_CHUNK_SIZE) {
|
|
77
|
+
throw new RangeError(`chunkSize must be an integer between 4 and ${MAX_CHUNK_SIZE} base64 characters`);
|
|
78
|
+
}
|
|
79
|
+
return chunkSize - (chunkSize % 4);
|
|
80
|
+
}
|
|
81
|
+
async function isDirectory(path) {
|
|
82
|
+
try {
|
|
83
|
+
return (await stat(path)).isDirectory();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async function targetPath(download, path) {
|
|
90
|
+
// The name comes from the remote browser: keep its last component only.
|
|
91
|
+
const name = basename(download.name.replaceAll('\\', '/')) || download.guid || 'download';
|
|
92
|
+
if (path === undefined) {
|
|
93
|
+
return name;
|
|
94
|
+
}
|
|
95
|
+
if (await isDirectory(path)) {
|
|
96
|
+
return join(path, name);
|
|
97
|
+
}
|
|
98
|
+
return path;
|
|
99
|
+
}
|
|
100
|
+
/** Download helpers over a CDP session of a Cloud Browser. */
|
|
101
|
+
export class CloudBrowserDownloads {
|
|
102
|
+
constructor(session, options = {}) {
|
|
103
|
+
Object.defineProperty(this, "chunkSize", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(this, "sendCommand", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
115
|
+
if (typeof session === 'function') {
|
|
116
|
+
this.sendCommand = session;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this.sendCommand = (method, params) => session.send(method, params);
|
|
120
|
+
}
|
|
121
|
+
this.chunkSize = checkChunkSize(options.chunkSize ?? DEFAULT_CHUNK_SIZE);
|
|
122
|
+
}
|
|
123
|
+
/** The size in bytes of every completed download, by filename. */
|
|
124
|
+
async metadata() {
|
|
125
|
+
const reply = await this.sendCommand('ScrapiumBrowser.getDownloadsMetadatas', {});
|
|
126
|
+
return reply?.metadata ?? {};
|
|
127
|
+
}
|
|
128
|
+
/** Wait until `filename` is listed with the same size twice in a row, and resolve to that size. */
|
|
129
|
+
async waitFor(filename, options = {}) {
|
|
130
|
+
const timeout = options.timeout ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
131
|
+
const interval = options.interval ?? DEFAULT_WAIT_INTERVAL_MS;
|
|
132
|
+
const deadline = Date.now() + timeout;
|
|
133
|
+
let last = -1;
|
|
134
|
+
while (Date.now() < deadline) {
|
|
135
|
+
const size = (await this.metadata())[filename] ?? -1;
|
|
136
|
+
if (size >= 0 && size === last) {
|
|
137
|
+
return size;
|
|
138
|
+
}
|
|
139
|
+
last = size;
|
|
140
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
141
|
+
}
|
|
142
|
+
throw new Error(`${filename} did not finish downloading in ${timeout}ms`);
|
|
143
|
+
}
|
|
144
|
+
/** Every download of the session: small ones with their content, large ones as handles. */
|
|
145
|
+
async list() {
|
|
146
|
+
return parseDownloads(await this.sendCommand('ScrapiumBrowser.getDownloads', {}));
|
|
147
|
+
}
|
|
148
|
+
/** The bytes of a download, one decoded chunk at a time. A stream handle is read once. */
|
|
149
|
+
async *chunks(download) {
|
|
150
|
+
if (download.stream === undefined) {
|
|
151
|
+
yield decodeBase64(download.data ?? '');
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
let written = 0;
|
|
155
|
+
try {
|
|
156
|
+
while (true) {
|
|
157
|
+
const reply = await this.sendCommand('IO.read', { handle: download.stream, size: this.chunkSize });
|
|
158
|
+
const data = reply?.data ?? '';
|
|
159
|
+
if (data.length > 0) {
|
|
160
|
+
const chunk = reply.base64Encoded === false ? new TextEncoder().encode(data) : decodeBase64(data);
|
|
161
|
+
written += chunk.length;
|
|
162
|
+
yield chunk;
|
|
163
|
+
}
|
|
164
|
+
if (reply?.eof) {
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
await this.sendCommand('IO.close', { handle: download.stream });
|
|
171
|
+
}
|
|
172
|
+
if (download.size !== undefined && written !== download.size) {
|
|
173
|
+
throw new BrowserDownloadError(`${download.name}: read ${written} bytes, expected ${download.size}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** The whole content of a download, in memory. */
|
|
177
|
+
async read(download) {
|
|
178
|
+
const parts = [];
|
|
179
|
+
let total = 0;
|
|
180
|
+
for await (const chunk of this.chunks(download)) {
|
|
181
|
+
parts.push(chunk);
|
|
182
|
+
total += chunk.length;
|
|
183
|
+
}
|
|
184
|
+
const bytes = new Uint8Array(total);
|
|
185
|
+
let offset = 0;
|
|
186
|
+
for (const part of parts) {
|
|
187
|
+
bytes.set(part, offset);
|
|
188
|
+
offset += part.length;
|
|
189
|
+
}
|
|
190
|
+
return bytes;
|
|
191
|
+
}
|
|
192
|
+
/** Write a download to `path` (a file or a directory, the current one by default) chunk by chunk. */
|
|
193
|
+
async save(download, path) {
|
|
194
|
+
const target = await targetPath(download, path);
|
|
195
|
+
const file = await open(target, 'w');
|
|
196
|
+
try {
|
|
197
|
+
for await (const chunk of this.chunks(download)) {
|
|
198
|
+
await file.write(chunk);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
await file.close();
|
|
203
|
+
}
|
|
204
|
+
return target;
|
|
205
|
+
}
|
|
206
|
+
/** Write every download of the session into `directory` and resolve to their paths. */
|
|
207
|
+
async saveAll(directory = '.') {
|
|
208
|
+
await mkdir(directory, { recursive: true });
|
|
209
|
+
const paths = [];
|
|
210
|
+
for (const download of await this.list()) {
|
|
211
|
+
paths.push(await this.save(download, directory));
|
|
212
|
+
}
|
|
213
|
+
return paths;
|
|
214
|
+
}
|
|
215
|
+
}
|
package/esm/client.d.ts
CHANGED
|
@@ -5,9 +5,9 @@ import type { ScreenshotConfig } from './screenshotconfig.js';
|
|
|
5
5
|
import type { ExtractionConfig } from './extractionconfig.js';
|
|
6
6
|
import { BrowserConfig } from './browserconfig.js';
|
|
7
7
|
import { type AccountData, type ClassifyOptions, type ClassifyResult, ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
8
|
-
import type { Rec, Vault, VaultItem, VaultItemCreate, VaultSecret } from './types.js';
|
|
8
|
+
import type { Rec, Vault, VaultItem, VaultItemCreate, VaultLinkedServiceValue, VaultProbeResult, VaultSecret, VaultServiceLink, VaultServiceUpdate, VaultSyncReport } from './types.js';
|
|
9
9
|
import type { CrawlerConfig, CrawlerContentFormat } from './crawlerconfig.js';
|
|
10
|
-
import { CrawlerArtifact, type CrawlerArtifactType, CrawlerContents, type CrawlerPromptEvent, type CrawlerRefreshEntry, type CrawlerRefreshState, type CrawlerSearchMode, CrawlerSearchResponse, CrawlerStatus, CrawlerUrls } from './crawlerresult.js';
|
|
10
|
+
import { CrawlerArtifact, type CrawlerArtifactType, CrawlerContents, type CrawlerExtractResponse, type CrawlerPromptAnswer, type CrawlerPromptEvent, type CrawlerRefreshEntry, type CrawlerRefreshState, type CrawlerSearchMode, CrawlerSearchResponse, CrawlerStatus, CrawlerUrls } from './crawlerresult.js';
|
|
11
11
|
import { type CloudBrowserMonitoringOptions, type MonitoringMetricsOptions, type MonitoringTargetMetricsOptions } from './monitoringconfig.js';
|
|
12
12
|
import { type CreateScheduleRequest, type ListSchedulesOptions, type Schedule, type UpdateScheduleRequest } from './schedule.js';
|
|
13
13
|
/**
|
|
@@ -352,6 +352,42 @@ export declare class ScrapflyClient {
|
|
|
352
352
|
/** Generation model id. Unset uses the server default. */
|
|
353
353
|
model?: string;
|
|
354
354
|
}): AsyncGenerator<CrawlerPromptEvent, void, undefined>;
|
|
355
|
+
/**
|
|
356
|
+
* Ask a question answered from the content of one or more crawls, and get
|
|
357
|
+
* the whole answer in one object once generation completes.
|
|
358
|
+
*
|
|
359
|
+
* `POST /crawl/prompt` with `generation.stream: false`: the same facts as
|
|
360
|
+
* the frames of {@link crawlPrompt}. {@link crawl.Crawl.promptAnswer} is
|
|
361
|
+
* sugar over a one-element list.
|
|
362
|
+
*/
|
|
363
|
+
crawlPromptAnswer(crawlIds: string[], prompt: string, opts?: {
|
|
364
|
+
/** Retrieval overrides. Unset uses the server defaults. */
|
|
365
|
+
search?: {
|
|
366
|
+
limit?: number;
|
|
367
|
+
mode?: CrawlerSearchMode;
|
|
368
|
+
filters?: Rec<any>;
|
|
369
|
+
};
|
|
370
|
+
/** Generation model id. Unset uses the server default. */
|
|
371
|
+
model?: string;
|
|
372
|
+
}): Promise<CrawlerPromptAnswer>;
|
|
373
|
+
/**
|
|
374
|
+
* Extract a structured object from the content of one or more crawls.
|
|
375
|
+
*
|
|
376
|
+
* `POST /crawl/extract` retrieves like {@link crawlPromptAnswer} and answers
|
|
377
|
+
* with `data` shaped by `schema`, a JSON Schema handed to the model as its
|
|
378
|
+
* response schema. There is no streaming variant. {@link crawl.Crawl.extract}
|
|
379
|
+
* is sugar over a one-element list.
|
|
380
|
+
*/
|
|
381
|
+
crawlExtract<T = any>(crawlIds: string[], prompt: string, schema: Rec<any>, opts?: {
|
|
382
|
+
/** Retrieval overrides. Unset uses the server defaults. */
|
|
383
|
+
search?: {
|
|
384
|
+
limit?: number;
|
|
385
|
+
mode?: CrawlerSearchMode;
|
|
386
|
+
filters?: Rec<any>;
|
|
387
|
+
};
|
|
388
|
+
}): Promise<CrawlerExtractResponse<T>>;
|
|
389
|
+
/** POST a JSON body to a crawler endpoint that generates, once, and return its JSON answer. */
|
|
390
|
+
private postCrawlerJson;
|
|
355
391
|
/**
|
|
356
392
|
* Get the WebSocket URL for a Cloud Browser session.
|
|
357
393
|
*
|
|
@@ -380,7 +416,7 @@ export declare class ScrapflyClient {
|
|
|
380
416
|
*
|
|
381
417
|
* Unblock always uses a residential proxy (no proxy pool selection) and
|
|
382
418
|
* always performs a GET — the endpoint's purpose is to harvest cookies /
|
|
383
|
-
* clearance tokens via an
|
|
419
|
+
* clearance tokens via an Unblocker navigation, not to proxy arbitrary
|
|
384
420
|
* HTTP requests.
|
|
385
421
|
*/
|
|
386
422
|
cloudBrowserUnblock(options: {
|
|
@@ -519,6 +555,73 @@ export declare class ScrapflyClient {
|
|
|
519
555
|
success: boolean;
|
|
520
556
|
message: string;
|
|
521
557
|
}>;
|
|
558
|
+
/**
|
|
559
|
+
* Link a vault to a secret manager. The token is sealed under `vaultKey`, so
|
|
560
|
+
* the key is mandatory here. The vault must not already be linked and must
|
|
561
|
+
* hold no rows owned by a previous service (the server answers 409).
|
|
562
|
+
*/
|
|
563
|
+
cloudBrowserVaultServiceLink(vaultId: string, vaultKey: string, link: VaultServiceLink): Promise<{
|
|
564
|
+
vault: Vault;
|
|
565
|
+
message: string;
|
|
566
|
+
}>;
|
|
567
|
+
/**
|
|
568
|
+
* Update a link: edit the non-secret selection rules, rotate the token, or
|
|
569
|
+
* both. `vaultKey` is required only when `token` is supplied, mirroring
|
|
570
|
+
* `cloudBrowserVaultItemUpdate`; a config-only edit needs no key.
|
|
571
|
+
*
|
|
572
|
+
* A token rotation also clears the server's one-hour auth back-off, so a link
|
|
573
|
+
* broken by a dead token syncs again on the next round.
|
|
574
|
+
*/
|
|
575
|
+
cloudBrowserVaultServiceUpdate(vaultId: string, opts: VaultServiceUpdate & {
|
|
576
|
+
vaultKey?: string;
|
|
577
|
+
}): Promise<{
|
|
578
|
+
vault: Vault;
|
|
579
|
+
message: string;
|
|
580
|
+
}>;
|
|
581
|
+
/**
|
|
582
|
+
* Unlink the provider. `keepItems` governs only the mirrored rows — kept as
|
|
583
|
+
* plain manual items, or deleted — while the sealed token is removed either
|
|
584
|
+
* way, so no vault key is needed.
|
|
585
|
+
*
|
|
586
|
+
* Dropping the rows is irreversible, so the default is to keep them and the
|
|
587
|
+
* flag always goes on the wire explicitly rather than relying on a default at
|
|
588
|
+
* either end.
|
|
589
|
+
*/
|
|
590
|
+
cloudBrowserVaultServiceUnlink(vaultId: string, opts?: {
|
|
591
|
+
keepItems?: boolean;
|
|
592
|
+
}): Promise<{
|
|
593
|
+
success: boolean;
|
|
594
|
+
message: string;
|
|
595
|
+
}>;
|
|
596
|
+
/**
|
|
597
|
+
* Force a reconcile against the provider now, bypassing the sync TTL and the
|
|
598
|
+
* back-off a failed round leaves behind. Requires the vault key: the server
|
|
599
|
+
* opens the sealed token with it and re-seals every mirrored row.
|
|
600
|
+
*
|
|
601
|
+
* The server budget is 25s; the SDK pins no per-call timeout, so the runtime's
|
|
602
|
+
* own fetch default applies.
|
|
603
|
+
*/
|
|
604
|
+
cloudBrowserVaultServiceSync(vaultId: string, vaultKey: string): Promise<VaultSyncReport>;
|
|
605
|
+
/**
|
|
606
|
+
* Probe the provider: proves a token is live and lists the upstream vaults it
|
|
607
|
+
* can read. Writes nothing, so it never moves the vault's sync state.
|
|
608
|
+
*
|
|
609
|
+
* With no `token` the sealed token of an existing link is probed (the vault
|
|
610
|
+
* key opens it). Passing an unsaved `token` instead enumerates the upstream
|
|
611
|
+
* vaults BEFORE a link exists, which is how a caller picks `vault_id`.
|
|
612
|
+
*
|
|
613
|
+
* `linkedService` stays optional and undefaulted here: this is the one endpoint
|
|
614
|
+
* whose empty discriminator the server itself resolves to 1Password, and a
|
|
615
|
+
* client-side default would send a field the bodyless stored-token path must
|
|
616
|
+
* not carry.
|
|
617
|
+
*
|
|
618
|
+
* The server budget is 10s; the SDK pins no per-call timeout, so the runtime's
|
|
619
|
+
* own fetch default applies.
|
|
620
|
+
*/
|
|
621
|
+
cloudBrowserVaultServiceTest(vaultId: string, vaultKey: string, opts?: {
|
|
622
|
+
linkedService?: VaultLinkedServiceValue;
|
|
623
|
+
token?: string;
|
|
624
|
+
}): Promise<VaultProbeResult>;
|
|
522
625
|
/**
|
|
523
626
|
* Download a debug session recording video.
|
|
524
627
|
* Returns the video as an ArrayBuffer (stream from server).
|
|
@@ -567,7 +670,7 @@ export declare class ScrapflyClient {
|
|
|
567
670
|
createCrawlerSchedule(crawlerConfig: Record<string, unknown>, request: CreateScheduleRequest): Promise<Schedule>;
|
|
568
671
|
/** Return a schedule by id. Works across all kinds. */
|
|
569
672
|
getSchedule(id: string): Promise<Schedule>;
|
|
570
|
-
/** List
|
|
673
|
+
/** List the schedules of the API key's project and environment, optionally filtered by kind/status. */
|
|
571
674
|
listSchedules(opts?: ListSchedulesOptions): Promise<Schedule[]>;
|
|
572
675
|
listScrapeSchedules(opts?: {
|
|
573
676
|
status?: string;
|