scrapfly-sdk 0.10.5 → 0.10.6
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/esm/batch.d.ts.map +1 -1
- package/esm/batch.js +6 -0
- package/esm/browserconfig.d.ts +10 -0
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +10 -0
- package/esm/client.d.ts +32 -0
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +124 -0
- package/esm/main.d.ts +2 -0
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +1 -0
- package/esm/schedule.d.ts +93 -0
- package/esm/schedule.d.ts.map +1 -0
- package/esm/schedule.js +40 -0
- package/esm/webhookserver.d.ts +7 -3
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +13 -6
- package/package.json +1 -1
- package/script/batch.d.ts.map +1 -1
- package/script/batch.js +6 -0
- package/script/browserconfig.d.ts +10 -0
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +10 -0
- package/script/client.d.ts +32 -0
- package/script/client.d.ts.map +1 -1
- package/script/client.js +124 -0
- package/script/main.d.ts +2 -0
- package/script/main.d.ts.map +1 -1
- package/script/main.js +3 -1
- package/script/schedule.d.ts +93 -0
- package/script/schedule.d.ts.map +1 -0
- package/script/schedule.js +44 -0
- package/script/webhookserver.d.ts +7 -3
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +13 -6
- package/src/batch.ts +7 -1
- package/src/browserconfig.ts +12 -0
- package/src/client.ts +192 -0
- package/src/main.ts +10 -0
- package/src/schedule.ts +101 -0
- package/src/webhookserver.ts +13 -6
package/src/client.ts
CHANGED
|
@@ -17,6 +17,13 @@ import {
|
|
|
17
17
|
type MonitoringMetricsOptions,
|
|
18
18
|
type MonitoringTargetMetricsOptions,
|
|
19
19
|
} from './monitoringconfig.js';
|
|
20
|
+
import {
|
|
21
|
+
ScheduleAPIError,
|
|
22
|
+
type CreateScheduleRequest,
|
|
23
|
+
type ListSchedulesOptions,
|
|
24
|
+
type Schedule,
|
|
25
|
+
type UpdateScheduleRequest,
|
|
26
|
+
} from './schedule.js';
|
|
20
27
|
|
|
21
28
|
/**
|
|
22
29
|
* Main entry point for the Scrapfly SDK.
|
|
@@ -1322,6 +1329,7 @@ export class ScrapflyClient {
|
|
|
1322
1329
|
body?: string;
|
|
1323
1330
|
method?: string;
|
|
1324
1331
|
enable_mcp?: boolean;
|
|
1332
|
+
solve_captcha?: boolean;
|
|
1325
1333
|
}): Promise<{ ws_url: string; session_id: string; run_id: string; mcp_endpoint?: string }> {
|
|
1326
1334
|
const proxyPoolMap: Record<string, string> = {
|
|
1327
1335
|
datacenter: 'public_datacenter_pool',
|
|
@@ -1338,6 +1346,7 @@ export class ScrapflyClient {
|
|
|
1338
1346
|
if (options.body) jsonBody.body = options.body;
|
|
1339
1347
|
if (options.method) jsonBody.method = options.method;
|
|
1340
1348
|
if (options.enable_mcp !== undefined) jsonBody.enable_mcp = options.enable_mcp;
|
|
1349
|
+
if (options.solve_captcha !== undefined) jsonBody.solve_captcha = options.solve_captcha;
|
|
1341
1350
|
|
|
1342
1351
|
const url = new URL(this.cloudBrowserApiHost + '/unblock');
|
|
1343
1352
|
url.searchParams.set('key', this.key);
|
|
@@ -1555,6 +1564,189 @@ export class ScrapflyClient {
|
|
|
1555
1564
|
}
|
|
1556
1565
|
return await response.json() as { sessions: any[]; total: number };
|
|
1557
1566
|
}
|
|
1567
|
+
|
|
1568
|
+
// -------------------------------------------------------------------------
|
|
1569
|
+
// Public schedule endpoints — /scrape/schedules, /screenshot/schedules,
|
|
1570
|
+
// /crawl/schedules, /schedules. The kind-specific config is supplied to the
|
|
1571
|
+
// matching `create*Schedule` method; the cross-kind list/get/update/delete/
|
|
1572
|
+
// pause/resume/execute methods work regardless of kind.
|
|
1573
|
+
// -------------------------------------------------------------------------
|
|
1574
|
+
|
|
1575
|
+
/** Create a Web Scraping API schedule. `scrape_config` is the same shape
|
|
1576
|
+
* accepted by `/scrape` (e.g. `{ url, render_js, asp, ... }`). */
|
|
1577
|
+
async createScrapeSchedule(
|
|
1578
|
+
scrapeConfig: Record<string, unknown>,
|
|
1579
|
+
request: CreateScheduleRequest,
|
|
1580
|
+
): Promise<Schedule> {
|
|
1581
|
+
return await this.scheduleRequest(
|
|
1582
|
+
'POST',
|
|
1583
|
+
'/scrape/schedules',
|
|
1584
|
+
undefined,
|
|
1585
|
+
this.buildCreateBody('scrape_config', scrapeConfig, request),
|
|
1586
|
+
) as Schedule;
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
/** Create a Screenshot API schedule. */
|
|
1590
|
+
async createScreenshotSchedule(
|
|
1591
|
+
screenshotConfig: Record<string, unknown>,
|
|
1592
|
+
request: CreateScheduleRequest,
|
|
1593
|
+
): Promise<Schedule> {
|
|
1594
|
+
return await this.scheduleRequest(
|
|
1595
|
+
'POST',
|
|
1596
|
+
'/screenshot/schedules',
|
|
1597
|
+
undefined,
|
|
1598
|
+
this.buildCreateBody('screenshot_config', screenshotConfig, request),
|
|
1599
|
+
) as Schedule;
|
|
1600
|
+
}
|
|
1601
|
+
|
|
1602
|
+
/** Create a Crawler API schedule. */
|
|
1603
|
+
async createCrawlerSchedule(
|
|
1604
|
+
crawlerConfig: Record<string, unknown>,
|
|
1605
|
+
request: CreateScheduleRequest,
|
|
1606
|
+
): Promise<Schedule> {
|
|
1607
|
+
return await this.scheduleRequest(
|
|
1608
|
+
'POST',
|
|
1609
|
+
'/crawl/schedules',
|
|
1610
|
+
undefined,
|
|
1611
|
+
this.buildCreateBody('crawler_config', crawlerConfig, request),
|
|
1612
|
+
) as Schedule;
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
/** Return a schedule by id. Works across all kinds. */
|
|
1616
|
+
async getSchedule(id: string): Promise<Schedule> {
|
|
1617
|
+
return await this.scheduleRequest('GET', `/schedules/${encodeURIComponent(id)}`) as Schedule;
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
/** List every schedule on the account, optionally filtered by kind/status. */
|
|
1621
|
+
async listSchedules(opts?: ListSchedulesOptions): Promise<Schedule[]> {
|
|
1622
|
+
return await this.scheduleRequest(
|
|
1623
|
+
'GET',
|
|
1624
|
+
'/schedules',
|
|
1625
|
+
opts as Record<string, string | undefined> | undefined,
|
|
1626
|
+
) as Schedule[];
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
async listScrapeSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
1630
|
+
return await this.scheduleRequest('GET', '/scrape/schedules', opts) as Schedule[];
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
async listScreenshotSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
1634
|
+
return await this.scheduleRequest('GET', '/screenshot/schedules', opts) as Schedule[];
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
async listCrawlerSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
1638
|
+
return await this.scheduleRequest('GET', '/crawl/schedules', opts) as Schedule[];
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
/** Patch an active schedule. Only fields set in `request` change. */
|
|
1642
|
+
async updateSchedule(id: string, request: UpdateScheduleRequest): Promise<Schedule> {
|
|
1643
|
+
return await this.scheduleRequest(
|
|
1644
|
+
'PATCH',
|
|
1645
|
+
`/schedules/${encodeURIComponent(id)}`,
|
|
1646
|
+
undefined,
|
|
1647
|
+
request as Record<string, unknown>,
|
|
1648
|
+
) as Schedule;
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
/** Cancel a schedule. Cancellation is terminal (returns no body). */
|
|
1652
|
+
async cancelSchedule(id: string): Promise<void> {
|
|
1653
|
+
await this.scheduleRequest('DELETE', `/schedules/${encodeURIComponent(id)}`);
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
async pauseSchedule(id: string): Promise<Schedule> {
|
|
1657
|
+
return await this.scheduleRequest(
|
|
1658
|
+
'POST',
|
|
1659
|
+
`/schedules/${encodeURIComponent(id)}/pause`,
|
|
1660
|
+
) as Schedule;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
async resumeSchedule(id: string): Promise<Schedule> {
|
|
1664
|
+
return await this.scheduleRequest(
|
|
1665
|
+
'POST',
|
|
1666
|
+
`/schedules/${encodeURIComponent(id)}/resume`,
|
|
1667
|
+
) as Schedule;
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
/** Fire a schedule immediately, regardless of next_scheduled_date. */
|
|
1671
|
+
async executeSchedule(id: string): Promise<Schedule> {
|
|
1672
|
+
return await this.scheduleRequest(
|
|
1673
|
+
'POST',
|
|
1674
|
+
`/schedules/${encodeURIComponent(id)}/execute`,
|
|
1675
|
+
) as Schedule;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
private buildCreateBody(
|
|
1679
|
+
configKey: string,
|
|
1680
|
+
config: Record<string, unknown>,
|
|
1681
|
+
request: CreateScheduleRequest,
|
|
1682
|
+
): Record<string, unknown> {
|
|
1683
|
+
const body: Record<string, unknown> = {
|
|
1684
|
+
[configKey]: config,
|
|
1685
|
+
webhook_name: request.webhook_name,
|
|
1686
|
+
allow_concurrency: request.allow_concurrency ?? false,
|
|
1687
|
+
retry_on_failure: request.retry_on_failure ?? false,
|
|
1688
|
+
};
|
|
1689
|
+
if (request.recurrence) body.recurrence = request.recurrence;
|
|
1690
|
+
if (request.scheduled_date) body.scheduled_date = request.scheduled_date;
|
|
1691
|
+
if (typeof request.max_retries === 'number') body.max_retries = request.max_retries;
|
|
1692
|
+
if (request.notes) body.notes = request.notes;
|
|
1693
|
+
return body;
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
private async scheduleRequest(
|
|
1697
|
+
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
|
|
1698
|
+
path: string,
|
|
1699
|
+
query?: Record<string, string | undefined>,
|
|
1700
|
+
body?: Record<string, unknown>,
|
|
1701
|
+
): Promise<unknown> {
|
|
1702
|
+
const url = new URL(this.HOST + path);
|
|
1703
|
+
const params = new URLSearchParams();
|
|
1704
|
+
params.set('key', this.key);
|
|
1705
|
+
if (query) {
|
|
1706
|
+
for (const [k, v] of Object.entries(query)) {
|
|
1707
|
+
if (v !== undefined && v !== '') params.set(k, v);
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
url.search = params.toString();
|
|
1711
|
+
|
|
1712
|
+
const init: { url: string; method: string; headers: Record<string, string>; body?: string } = {
|
|
1713
|
+
url: url.toString(),
|
|
1714
|
+
method,
|
|
1715
|
+
headers: {
|
|
1716
|
+
'user-agent': this.ua,
|
|
1717
|
+
accept: 'application/json',
|
|
1718
|
+
},
|
|
1719
|
+
};
|
|
1720
|
+
if (body !== undefined) {
|
|
1721
|
+
init.body = JSON.stringify(body);
|
|
1722
|
+
init.headers['content-type'] = 'application/json';
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
const response = await this.fetch(init as any);
|
|
1726
|
+
|
|
1727
|
+
if (response.status === 204) return undefined;
|
|
1728
|
+
if (!response.ok) {
|
|
1729
|
+
let envelope: { error?: string; message?: string; reason?: string; details?: unknown } = {};
|
|
1730
|
+
try {
|
|
1731
|
+
envelope = await response.json() as typeof envelope;
|
|
1732
|
+
} catch {
|
|
1733
|
+
// body wasn't JSON; fall through with the empty envelope
|
|
1734
|
+
}
|
|
1735
|
+
const code = envelope.error ?? 'ERR::SCHEDULER::BACKEND_ERROR';
|
|
1736
|
+
let message = envelope.message ?? '';
|
|
1737
|
+
if (envelope.reason) {
|
|
1738
|
+
message = message ? `${message} (${envelope.reason})` : envelope.reason;
|
|
1739
|
+
}
|
|
1740
|
+
if (!message) message = `HTTP ${response.status}`;
|
|
1741
|
+
throw new ScheduleAPIError({
|
|
1742
|
+
code,
|
|
1743
|
+
message,
|
|
1744
|
+
httpStatusCode: response.status,
|
|
1745
|
+
details: envelope.details,
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
return await response.json();
|
|
1749
|
+
}
|
|
1558
1750
|
}
|
|
1559
1751
|
|
|
1560
1752
|
// ---------------------------------------------------------------------------
|
package/src/main.ts
CHANGED
|
@@ -83,6 +83,16 @@ export type {
|
|
|
83
83
|
MonitoringTargetMetricsOptions,
|
|
84
84
|
} from './monitoringconfig.js';
|
|
85
85
|
|
|
86
|
+
export { ScheduleAPIError } from './schedule.js';
|
|
87
|
+
export type {
|
|
88
|
+
CreateScheduleRequest,
|
|
89
|
+
ListSchedulesOptions,
|
|
90
|
+
Schedule,
|
|
91
|
+
ScheduleEnd,
|
|
92
|
+
ScheduleRecurrence,
|
|
93
|
+
UpdateScheduleRequest,
|
|
94
|
+
} from './schedule.js';
|
|
95
|
+
|
|
86
96
|
export { log } from './logger.js';
|
|
87
97
|
|
|
88
98
|
export { createServer, verifySignature } from './webhookserver.js';
|
package/src/schedule.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public schedule client for the Scrapfly API.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `/scrape/schedules`, `/screenshot/schedules`, `/crawl/schedules`
|
|
5
|
+
* and the cross-kind `/schedules` endpoints. The server returns fully-formed
|
|
6
|
+
* Schedule objects; we surface them as-is so callers always see the live
|
|
7
|
+
* server shape.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Bounds a recurring schedule by either a date or a fire count. */
|
|
11
|
+
export interface ScheduleEnd {
|
|
12
|
+
type: 'date' | 'count';
|
|
13
|
+
date?: string;
|
|
14
|
+
count?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* When a schedule fires next. Cron mode wins when `cron` is set; otherwise
|
|
19
|
+
* `interval` + `unit` drive the cadence.
|
|
20
|
+
*/
|
|
21
|
+
export interface ScheduleRecurrence {
|
|
22
|
+
cron?: string;
|
|
23
|
+
interval?: number;
|
|
24
|
+
unit?: 'minute' | 'hour' | 'day' | 'week' | 'month';
|
|
25
|
+
days?: string[];
|
|
26
|
+
ends?: ScheduleEnd;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Public-facing request envelope for creating a schedule. The kind-specific
|
|
31
|
+
* config (`scrape_config` / `screenshot_config` / `crawler_config`) is
|
|
32
|
+
* supplied as a separate argument by the matching `create*Schedule` method.
|
|
33
|
+
*/
|
|
34
|
+
export interface CreateScheduleRequest {
|
|
35
|
+
webhook_name: string;
|
|
36
|
+
recurrence?: ScheduleRecurrence;
|
|
37
|
+
scheduled_date?: string;
|
|
38
|
+
allow_concurrency?: boolean;
|
|
39
|
+
retry_on_failure?: boolean;
|
|
40
|
+
max_retries?: number;
|
|
41
|
+
notes?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** PATCH payload. Only fields explicitly set are forwarded. */
|
|
45
|
+
export interface UpdateScheduleRequest {
|
|
46
|
+
recurrence?: ScheduleRecurrence;
|
|
47
|
+
scheduled_date?: string;
|
|
48
|
+
allow_concurrency?: boolean;
|
|
49
|
+
retry_on_failure?: boolean;
|
|
50
|
+
max_retries?: number;
|
|
51
|
+
notes?: string;
|
|
52
|
+
scrape_config?: Record<string, unknown>;
|
|
53
|
+
screenshot_config?: Record<string, unknown>;
|
|
54
|
+
crawler_config?: Record<string, unknown>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Server-side schedule record. Returned by every read or mutation endpoint. */
|
|
58
|
+
export interface Schedule {
|
|
59
|
+
id: string;
|
|
60
|
+
kind: string;
|
|
61
|
+
status: string;
|
|
62
|
+
next_scheduled_date?: string;
|
|
63
|
+
scheduled_date?: string;
|
|
64
|
+
recurrence?: ScheduleRecurrence;
|
|
65
|
+
metadata?: Record<string, unknown>;
|
|
66
|
+
notes?: string | null;
|
|
67
|
+
created_by?: string | null;
|
|
68
|
+
created_at: string;
|
|
69
|
+
updated_at: string;
|
|
70
|
+
cancelled_at?: string | null;
|
|
71
|
+
allow_concurrency: boolean;
|
|
72
|
+
retry_on_failure: boolean;
|
|
73
|
+
max_retries: number;
|
|
74
|
+
webhook_uuid?: string | null;
|
|
75
|
+
user_uuid?: string | null;
|
|
76
|
+
consecutive_failures?: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ListSchedulesOptions {
|
|
80
|
+
status?: string;
|
|
81
|
+
kind?: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Raised on any non-2xx response from a `/schedules/*` endpoint. The
|
|
86
|
+
* `code` property carries the public `ERR::SCHEDULER::*` identifier so
|
|
87
|
+
* callers can branch on it without parsing the message string.
|
|
88
|
+
*/
|
|
89
|
+
export class ScheduleAPIError extends Error {
|
|
90
|
+
code: string;
|
|
91
|
+
httpStatusCode: number;
|
|
92
|
+
details: unknown;
|
|
93
|
+
|
|
94
|
+
constructor(args: { code: string; message: string; httpStatusCode: number; details?: unknown }) {
|
|
95
|
+
super(args.message);
|
|
96
|
+
this.name = 'ScheduleAPIError';
|
|
97
|
+
this.code = args.code;
|
|
98
|
+
this.httpStatusCode = args.httpStatusCode;
|
|
99
|
+
this.details = args.details;
|
|
100
|
+
}
|
|
101
|
+
}
|
package/src/webhookserver.ts
CHANGED
|
@@ -12,17 +12,24 @@ export enum ResourceType {
|
|
|
12
12
|
* HMAC-SHA256 signature on the request body against the configured
|
|
13
13
|
* signing secret.
|
|
14
14
|
*
|
|
15
|
-
* @param body Raw request body as received (
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* @param body Raw request body as received (verify BEFORE any decompression
|
|
16
|
+
* or JSON parsing — the signature is computed on the bytes
|
|
17
|
+
* Scrapfly sent on the wire).
|
|
18
|
+
* @param signature Value of the `X-Scrapfly-Webhook-Signature` header
|
|
19
|
+
* (uppercase hex digest).
|
|
20
|
+
* @param signingSecret Webhook signing secret from the dashboard, passed as-is
|
|
21
|
+
* (UTF-8 string; not hex-encoded).
|
|
18
22
|
* @returns `true` if the signature matches.
|
|
19
23
|
*/
|
|
20
24
|
export function verifySignature(body: Buffer, signature: string, signingSecret: string): boolean {
|
|
21
|
-
const
|
|
22
|
-
const hmac = crypto.createHmac('sha256', secret);
|
|
25
|
+
const hmac = crypto.createHmac('sha256', Buffer.from(signingSecret, 'utf8'));
|
|
23
26
|
hmac.update(body);
|
|
24
27
|
const computed = hmac.digest('hex').toUpperCase();
|
|
25
|
-
|
|
28
|
+
const received = signature.toUpperCase();
|
|
29
|
+
if (computed.length !== received.length) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return crypto.timingSafeEqual(Buffer.from(computed), Buffer.from(received));
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
|