scrapfly-sdk 0.12.2 → 2.0.0
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 +6 -0
- package/esm/browserconfig.d.ts +14 -2
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +24 -0
- package/esm/client.d.ts +5 -3
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +14 -7
- package/esm/crawlerresult.d.ts +16 -3
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/main.d.ts +3 -2
- package/esm/main.d.ts.map +1 -1
- package/esm/scrapeconfig.d.ts +31 -3
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +5 -0
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +7 -1
- package/package.json +1 -1
- package/script/browserconfig.d.ts +14 -2
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +24 -0
- package/script/client.d.ts +5 -3
- package/script/client.d.ts.map +1 -1
- package/script/client.js +14 -7
- package/script/crawlerresult.d.ts +16 -3
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/main.d.ts +3 -2
- package/script/main.d.ts.map +1 -1
- package/script/scrapeconfig.d.ts +31 -3
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +5 -0
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +7 -1
- package/src/browserconfig.ts +25 -2
- package/src/client.ts +17 -9
- package/src/crawlerresult.ts +27 -5
- package/src/main.ts +3 -1
- package/src/scrapeconfig.ts +45 -3
- package/src/utils.ts +10 -1
package/src/crawlerresult.ts
CHANGED
|
@@ -62,18 +62,40 @@ function optionalField<T>(
|
|
|
62
62
|
export type CrawlerStatusValue = 'PENDING' | 'RUNNING' | 'DONE' | 'CANCELLED';
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
65
|
+
* Why a crawler stopped, as `state.stop_reason` reports it.
|
|
66
|
+
*
|
|
67
|
+
* `backlog_empty` and `retry_backlog_only` are a normal completion: every URL found was crawled, or only URLs waiting
|
|
68
|
+
* for a retry were left. A stop on a limit (`page_limit`, `max_duration_reached`, `max_api_credit`) or on
|
|
69
|
+
* `crawler_error` after at least one visited page keeps its pages and ends with `is_success: true`: a stop on
|
|
70
|
+
* `max_api_credit` is a success.
|
|
66
71
|
*/
|
|
67
72
|
export type CrawlerStopReason =
|
|
73
|
+
| 'backlog_empty'
|
|
74
|
+
| 'retry_backlog_only'
|
|
68
75
|
| 'no_more_urls'
|
|
69
76
|
| 'page_limit'
|
|
70
|
-
| '
|
|
77
|
+
| 'max_duration_reached'
|
|
71
78
|
| 'max_api_credit'
|
|
72
79
|
| 'seed_url_failed'
|
|
73
80
|
| 'user_cancelled'
|
|
74
81
|
| 'crawler_error'
|
|
75
|
-
| '
|
|
76
|
-
| '
|
|
82
|
+
| 'target_not_allowed'
|
|
83
|
+
| 'webhook_not_found'
|
|
84
|
+
| 'storage_error'
|
|
85
|
+
| 'remote_list_fetch_failed'
|
|
86
|
+
| 'remote_list_too_large'
|
|
87
|
+
| 'worker_interrupted'
|
|
88
|
+
| 'internal_error'
|
|
89
|
+
| LegacyCrawlerStopReason;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Values the API no longer sets, kept so code that names them still compiles: a stop on `max_duration` is
|
|
93
|
+
* `max_duration_reached`, `no_api_credit_left` is never returned, and `worker_interupted` is the misspelled value
|
|
94
|
+
* that crawls stored before its correction still report.
|
|
95
|
+
*
|
|
96
|
+
* @deprecated Compare with `worker_interrupted` and `max_duration_reached`; the API never returns the others.
|
|
97
|
+
*/
|
|
98
|
+
export type LegacyCrawlerStopReason = 'max_duration' | 'no_api_credit_left' | 'worker_interupted';
|
|
77
99
|
|
|
78
100
|
/**
|
|
79
101
|
* Per-job state metrics returned in the status response and webhook payloads.
|
|
@@ -98,7 +120,7 @@ export interface CrawlerState {
|
|
|
98
120
|
api_credit_used: number;
|
|
99
121
|
/** Elapsed time in seconds since the crawl started (0 while PENDING). */
|
|
100
122
|
duration: number;
|
|
101
|
-
/** `null` until
|
|
123
|
+
/** Why the crawler stopped, `null` until it does. */
|
|
102
124
|
stop_reason: CrawlerStopReason | null;
|
|
103
125
|
/** `null` until the crawler actually starts processing (still PENDING). */
|
|
104
126
|
start_time: number | null;
|
package/src/main.ts
CHANGED
|
@@ -30,6 +30,7 @@ export {
|
|
|
30
30
|
ScrapeConfig,
|
|
31
31
|
ScreenshotFlags,
|
|
32
32
|
} from './scrapeconfig.js';
|
|
33
|
+
export type { ScrapeAnswer, ScrapeConfigOptions } from './scrapeconfig.js';
|
|
33
34
|
export {
|
|
34
35
|
Format as ScreenshotFormat,
|
|
35
36
|
Options as ScreenshotOptions,
|
|
@@ -38,7 +39,7 @@ export {
|
|
|
38
39
|
} from './screenshotconfig.js';
|
|
39
40
|
export { ExtractionConfig } from './extractionconfig.js';
|
|
40
41
|
export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
|
|
41
|
-
export type { BrowserConfigOptions } from './browserconfig.js';
|
|
42
|
+
export type { BrowserConfigOptions, NetworkPolicy } from './browserconfig.js';
|
|
42
43
|
export { BrowserDownloadError, CloudBrowserDownloads, parseDownloads } from './browserdownloads.js';
|
|
43
44
|
export type { BrowserDownload, CdpSend, CdpSession, CloudBrowserDownloadsOptions, WaitForOptions } from './browserdownloads.js';
|
|
44
45
|
// Enum, hence a value export: callers name VaultLinkedService.ONE_PASSWORD.
|
|
@@ -114,6 +115,7 @@ export type {
|
|
|
114
115
|
CrawlerState,
|
|
115
116
|
CrawlerStatusValue,
|
|
116
117
|
CrawlerStopReason,
|
|
118
|
+
LegacyCrawlerStopReason,
|
|
117
119
|
CrawlerUpdatedWebhook,
|
|
118
120
|
CrawlerUrlDiscoveredWebhook,
|
|
119
121
|
CrawlerUrlEntry,
|
package/src/scrapeconfig.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { urlsafe_b64encode } from './utils.js';
|
|
|
2
2
|
import { log } from './logger.js';
|
|
3
3
|
import type { HttpMethod, Rec } from './types.js';
|
|
4
4
|
import { ScrapeConfigError } from './errors.js';
|
|
5
|
+
import type { ScrapeResult } from './result.js';
|
|
5
6
|
|
|
6
7
|
export enum ScreenshotFlags {
|
|
7
8
|
/**
|
|
@@ -43,7 +44,7 @@ export enum FormatOption {
|
|
|
43
44
|
ONLY_CONTENT = 'only_content',
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
type ScrapeConfigOptions = {
|
|
47
|
+
export type ScrapeConfigOptions = {
|
|
47
48
|
url: string;
|
|
48
49
|
retry?: boolean;
|
|
49
50
|
method?: HttpMethod;
|
|
@@ -126,7 +127,45 @@ type ScrapeConfigOptions = {
|
|
|
126
127
|
geolocation?: string;
|
|
127
128
|
};
|
|
128
129
|
|
|
129
|
-
|
|
130
|
+
/**
|
|
131
|
+
* An option the constructor does not take: a typo fails to compile, as the constructor refuses it when it
|
|
132
|
+
* runs. Options built with an index signature (computed keys) are left to that runtime check.
|
|
133
|
+
*/
|
|
134
|
+
type NoExtraOptions<O> = string extends keyof O ? unknown
|
|
135
|
+
: { [K in Exclude<keyof O, keyof ScrapeConfigOptions>]: never };
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* `proxified_response` as options `O` set it: `false` when absent, `boolean` when unknown (`any`, a variable, an
|
|
139
|
+
* optional key, options that are a union of literals from a ternary or a conditional spread).
|
|
140
|
+
*/
|
|
141
|
+
type ProxifiedOf<O> = 0 extends (1 & O) ? boolean
|
|
142
|
+
: O extends unknown ? (
|
|
143
|
+
'proxified_response' extends keyof O
|
|
144
|
+
? ({} extends Pick<O, 'proxified_response' & keyof O> ? false : never)
|
|
145
|
+
| KnownBoolean<Exclude<O['proxified_response' & keyof O], undefined>>
|
|
146
|
+
: false
|
|
147
|
+
)
|
|
148
|
+
: never;
|
|
149
|
+
|
|
150
|
+
/** `any` (an `undefined` value without strict null checks widens to it) says nothing: `boolean`. */
|
|
151
|
+
type KnownBoolean<V> = 0 extends (1 & V) ? boolean : V;
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* What `ScrapflyClient.scrape` answers for a config built from options `O`: the raw `Response` with
|
|
155
|
+
* `proxified_response: true`, a `ScrapeResult` without it or with `false`, either one when the options
|
|
156
|
+
* do not say which (a `boolean` variable, `any`, or a config typed as plain `ScrapeConfig`).
|
|
157
|
+
*/
|
|
158
|
+
export type ScrapeAnswer<O> = [ProxifiedOf<O>] extends [never] ? ScrapeResult
|
|
159
|
+
: [ProxifiedOf<O>] extends [true] ? Response
|
|
160
|
+
: [ProxifiedOf<O>] extends [false] ? ScrapeResult
|
|
161
|
+
: ScrapeResult | Response;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* `O` is the options the config was built from, inferred by `new ScrapeConfig({...})`: it only types what
|
|
165
|
+
* `ScrapflyClient.scrape` answers. A plain `ScrapeConfig` takes any of them and answers either one; type a
|
|
166
|
+
* variable that holds both proxified and plain configs as plain `ScrapeConfig`.
|
|
167
|
+
*/
|
|
168
|
+
export class ScrapeConfig<O extends ScrapeConfigOptions = ScrapeConfigOptions> {
|
|
130
169
|
// Proxy pools, sent as `proxy_pool`. Datacenter proxies are the default.
|
|
131
170
|
static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool';
|
|
132
171
|
static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool';
|
|
@@ -196,7 +235,10 @@ export class ScrapeConfig {
|
|
|
196
235
|
rendering_stage?: 'complete' | 'domcontentloaded';
|
|
197
236
|
geolocation?: string;
|
|
198
237
|
|
|
199
|
-
|
|
238
|
+
/** Type-level only, never defined: carries `O` to `ScrapflyClient.scrape`. A method, so configs of any options stay assignable to each other. */
|
|
239
|
+
__options?(options: O): void;
|
|
240
|
+
|
|
241
|
+
constructor(options: O & NoExtraOptions<O>) {
|
|
200
242
|
this.validateOptions(options);
|
|
201
243
|
if (options.format && !Object.values(Format).includes(options.format as Format)) {
|
|
202
244
|
throw new ScrapeConfigError(`Invalid Format param value: ${options.format}`);
|
package/src/utils.ts
CHANGED
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { log } from './logger.js';
|
|
2
2
|
import type { Rec } from './types.js';
|
|
3
3
|
|
|
4
|
+
const BASE64_CHUNK_BYTES = 0x8000;
|
|
5
|
+
|
|
4
6
|
export function urlsafe_b64encode(data: string): string {
|
|
5
7
|
const encoder = new TextEncoder();
|
|
6
8
|
const encoded = encoder.encode(data);
|
|
7
|
-
|
|
9
|
+
let binary = '';
|
|
10
|
+
|
|
11
|
+
// Encoded in chunks of BASE64_CHUNK_BYTES bytes.
|
|
12
|
+
for (let offset = 0; offset < encoded.length; offset += BASE64_CHUNK_BYTES) {
|
|
13
|
+
binary += String.fromCharCode(...encoded.subarray(offset, offset + BASE64_CHUNK_BYTES));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const base64 = btoa(binary)
|
|
8
17
|
.replace(/\+/g, '-')
|
|
9
18
|
.replace(/\//g, '_')
|
|
10
19
|
.replace(/=+$/, '');
|