scrapfly-sdk 0.10.5 → 0.11.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/esm/batch.d.ts.map +1 -1
- package/esm/batch.js +6 -0
- package/esm/browserconfig.d.ts +64 -0
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +116 -0
- package/esm/client.d.ts +135 -2
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +454 -12
- package/esm/crawlerconfig.d.ts +6 -2
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +33 -4
- package/esm/extractionconfig.js +1 -1
- package/esm/main.d.ts +3 -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/scrapeconfig.d.ts +1 -0
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +9 -5
- package/esm/types.d.ts +94 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/utils.d.ts +1 -1
- package/esm/utils.d.ts.map +1 -1
- 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 +64 -0
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +116 -0
- package/script/client.d.ts +135 -2
- package/script/client.d.ts.map +1 -1
- package/script/client.js +454 -12
- package/script/crawlerconfig.d.ts +6 -2
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +33 -4
- package/script/extractionconfig.js +1 -1
- package/script/main.d.ts +3 -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/scrapeconfig.d.ts +1 -0
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +9 -5
- package/script/types.d.ts +94 -0
- package/script/types.d.ts.map +1 -1
- package/script/utils.d.ts +1 -1
- package/script/utils.d.ts.map +1 -1
- 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 +99 -0
- package/src/client.ts +592 -13
- package/src/crawlerconfig.ts +27 -6
- package/src/extractionconfig.ts +1 -1
- package/src/main.ts +11 -0
- package/src/schedule.ts +101 -0
- package/src/scrapeconfig.ts +4 -5
- package/src/types.ts +115 -0
- package/src/utils.ts +6 -1
- package/src/webhookserver.ts +13 -6
package/src/client.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type { ExtractionConfig } from './extractionconfig.js';
|
|
|
8
8
|
import { BrowserConfig } from './browserconfig.js';
|
|
9
9
|
import { type AccountData, type ClassifyOptions, type ClassifyResult, ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
10
10
|
import { log } from './logger.js';
|
|
11
|
-
import type { Rec } from './types.js';
|
|
11
|
+
import type { Rec, Vault, VaultItem, VaultItemCreate, VaultSecret } from './types.js';
|
|
12
12
|
import type { CrawlerConfig, CrawlerContentFormat } from './crawlerconfig.js';
|
|
13
13
|
import { CrawlerArtifact, type CrawlerArtifactType, CrawlerContents, CrawlerStatus, CrawlerUrls } from './crawlerresult.js';
|
|
14
14
|
import {
|
|
@@ -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.
|
|
@@ -928,17 +935,52 @@ export class ScrapflyClient {
|
|
|
928
935
|
try {
|
|
929
936
|
const url = new URL(this.HOST + '/crawl');
|
|
930
937
|
url.search = new URLSearchParams({ key: this.key }).toString();
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
938
|
+
// POST /crawl accepts two body formats:
|
|
939
|
+
// - application/json: the entire crawler configuration as JSON.
|
|
940
|
+
// Used for seed-URL crawls and remote_url_list crawls.
|
|
941
|
+
// - multipart/form-data: a 'config' JSON part and a 'urls' text
|
|
942
|
+
// part (one URL per line). Used only when the caller provides
|
|
943
|
+
// an in-memory url_list, so the URLs can be uploaded as a
|
|
944
|
+
// streamed file payload instead of inlined into the JSON body.
|
|
945
|
+
const params = config.toApiParams() as Rec<any>;
|
|
946
|
+
const urlList: string[] | undefined = Array.isArray(params.url_list) && params.url_list.length > 0
|
|
947
|
+
? (params.url_list as string[])
|
|
948
|
+
: undefined;
|
|
949
|
+
if (urlList !== undefined) {
|
|
950
|
+
// Strip url_list from the JSON config — the URLs go through the
|
|
951
|
+
// 'urls' multipart part instead, which the API gateway streams
|
|
952
|
+
// straight through to the scrape engine.
|
|
953
|
+
delete params.url_list;
|
|
954
|
+
const form = new FormData();
|
|
955
|
+
const configBlob = new Blob([JSON.stringify(params)], { type: 'application/json' });
|
|
956
|
+
form.append('config', configBlob, 'config.json');
|
|
957
|
+
const urlsBlob = new Blob([urlList.join('\n')], { type: 'text/plain' });
|
|
958
|
+
form.append('urls', urlsBlob, 'urls.txt');
|
|
959
|
+
response = await this.fetch({
|
|
960
|
+
url: url.toString(),
|
|
961
|
+
method: 'POST',
|
|
962
|
+
headers: {
|
|
963
|
+
'user-agent': this.ua,
|
|
964
|
+
// Intentionally omit content-type — fetch fills in the
|
|
965
|
+
// multipart boundary itself when given a FormData body.
|
|
966
|
+
'accept-encoding': 'gzip, deflate, br',
|
|
967
|
+
accept: 'application/json',
|
|
968
|
+
},
|
|
969
|
+
body: form,
|
|
970
|
+
});
|
|
971
|
+
} else {
|
|
972
|
+
response = await this.fetch({
|
|
973
|
+
url: url.toString(),
|
|
974
|
+
method: 'POST',
|
|
975
|
+
headers: {
|
|
976
|
+
'user-agent': this.ua,
|
|
977
|
+
'content-type': 'application/json',
|
|
978
|
+
'accept-encoding': 'gzip, deflate, br',
|
|
979
|
+
accept: 'application/json',
|
|
980
|
+
},
|
|
981
|
+
body: JSON.stringify(params),
|
|
982
|
+
});
|
|
983
|
+
}
|
|
942
984
|
} catch (e) {
|
|
943
985
|
log.error('error', e);
|
|
944
986
|
throw e;
|
|
@@ -1302,12 +1344,25 @@ export class ScrapflyClient {
|
|
|
1302
1344
|
|
|
1303
1345
|
/**
|
|
1304
1346
|
* Get the WebSocket URL for a Cloud Browser session.
|
|
1347
|
+
*
|
|
1348
|
+
* On rejection the server sends a JSON error frame then a close frame
|
|
1349
|
+
* with code 1008/1011/1013 and a "ERR::BROWSER::CODE: reason" string.
|
|
1350
|
+
* See https://scrapfly.io/docs/cloud-browser-api/errors#websocket-close-frame
|
|
1305
1351
|
*/
|
|
1306
1352
|
cloudBrowser(config?: BrowserConfig): string {
|
|
1307
1353
|
const browserConfig = config || new BrowserConfig();
|
|
1308
1354
|
return browserConfig.websocketUrl(this.key, this.cloudBrowserHost);
|
|
1309
1355
|
}
|
|
1310
1356
|
|
|
1357
|
+
/**
|
|
1358
|
+
* Return the deterministic project salt for this client's api_key. Matches
|
|
1359
|
+
* the `X-Browser-Project-Salt` response header returned on a successful
|
|
1360
|
+
* Cloud Browser WebSocket upgrade.
|
|
1361
|
+
*/
|
|
1362
|
+
async cloudBrowserProjectSalt(): Promise<string> {
|
|
1363
|
+
return BrowserConfig.projectSalt(this.key);
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1311
1366
|
/**
|
|
1312
1367
|
* Call the Cloud Browser Unblock API.
|
|
1313
1368
|
*/
|
|
@@ -1316,12 +1371,14 @@ export class ScrapflyClient {
|
|
|
1316
1371
|
proxy_pool?: string;
|
|
1317
1372
|
country?: string;
|
|
1318
1373
|
os?: string;
|
|
1374
|
+
browser_brand?: string;
|
|
1319
1375
|
timeout?: number;
|
|
1320
1376
|
browser_timeout?: number;
|
|
1321
1377
|
headers?: Record<string, string>;
|
|
1322
1378
|
body?: string;
|
|
1323
1379
|
method?: string;
|
|
1324
1380
|
enable_mcp?: boolean;
|
|
1381
|
+
debug?: boolean;
|
|
1325
1382
|
}): Promise<{ ws_url: string; session_id: string; run_id: string; mcp_endpoint?: string }> {
|
|
1326
1383
|
const proxyPoolMap: Record<string, string> = {
|
|
1327
1384
|
datacenter: 'public_datacenter_pool',
|
|
@@ -1332,12 +1389,14 @@ export class ScrapflyClient {
|
|
|
1332
1389
|
if (options.proxy_pool) jsonBody.proxy_pool = proxyPoolMap[options.proxy_pool] || options.proxy_pool;
|
|
1333
1390
|
if (options.country) jsonBody.country = options.country;
|
|
1334
1391
|
if (options.os) jsonBody.os = options.os;
|
|
1392
|
+
if (options.browser_brand) jsonBody.browser_brand = options.browser_brand;
|
|
1335
1393
|
if (options.timeout) jsonBody.timeout = options.timeout;
|
|
1336
1394
|
if (options.browser_timeout) jsonBody.browser_timeout = options.browser_timeout;
|
|
1337
1395
|
if (options.headers) jsonBody.headers = options.headers;
|
|
1338
1396
|
if (options.body) jsonBody.body = options.body;
|
|
1339
1397
|
if (options.method) jsonBody.method = options.method;
|
|
1340
1398
|
if (options.enable_mcp !== undefined) jsonBody.enable_mcp = options.enable_mcp;
|
|
1399
|
+
if (options.debug !== undefined) jsonBody.debug = options.debug;
|
|
1341
1400
|
|
|
1342
1401
|
const url = new URL(this.cloudBrowserApiHost + '/unblock');
|
|
1343
1402
|
url.searchParams.set('key', this.key);
|
|
@@ -1496,6 +1555,318 @@ export class ScrapflyClient {
|
|
|
1496
1555
|
return await response.json() as { extension: Record<string, any>; is_update: boolean };
|
|
1497
1556
|
}
|
|
1498
1557
|
|
|
1558
|
+
// ----- Cloud Browser Credential Vault -------------------------------------
|
|
1559
|
+
// Server contract documented at apps/scrapfly/api/scrapfly-api/pkg/vault/.
|
|
1560
|
+
//
|
|
1561
|
+
// SECURITY: the vault key is the customer-held secret that decrypts every
|
|
1562
|
+
// item in the vault. The server emits it ONCE on POST /vault and POST
|
|
1563
|
+
// /vault/{id}/rotate; never log it, never include it in error messages,
|
|
1564
|
+
// never put it in a breadcrumb. See agent_secret_tokenization_boundary.md.
|
|
1565
|
+
|
|
1566
|
+
/**
|
|
1567
|
+
* Create a new Cloud Browser Credential Vault. The response contains a
|
|
1568
|
+
* one-time `key` that the caller MUST save locally — the server cannot
|
|
1569
|
+
* recover it. Pair the key with the returned vault id when opening
|
|
1570
|
+
* sessions or creating items.
|
|
1571
|
+
*/
|
|
1572
|
+
async cloudBrowserVaultCreate(opts: {
|
|
1573
|
+
name: string;
|
|
1574
|
+
description?: string;
|
|
1575
|
+
}): Promise<{ vault: Vault; key: string; message: string }> {
|
|
1576
|
+
const url = new URL(this.cloudBrowserApiHost + '/vault');
|
|
1577
|
+
url.searchParams.set('key', this.key);
|
|
1578
|
+
|
|
1579
|
+
const body: Record<string, unknown> = { name: opts.name };
|
|
1580
|
+
if (opts.description !== undefined) body.description = opts.description;
|
|
1581
|
+
|
|
1582
|
+
const response = await this.fetch({
|
|
1583
|
+
url: url.toString(),
|
|
1584
|
+
method: 'POST',
|
|
1585
|
+
headers: {
|
|
1586
|
+
'content-type': 'application/json',
|
|
1587
|
+
'user-agent': this.ua,
|
|
1588
|
+
},
|
|
1589
|
+
body: JSON.stringify(body),
|
|
1590
|
+
});
|
|
1591
|
+
|
|
1592
|
+
if (!response.ok) {
|
|
1593
|
+
throw new Error(`Vault create failed: ${response.status} ${await response.text()}`);
|
|
1594
|
+
}
|
|
1595
|
+
return await response.json() as { vault: Vault; key: string; message: string };
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
/** List every vault on the account. */
|
|
1599
|
+
async cloudBrowserVaultList(): Promise<{ vaults: Vault[] }> {
|
|
1600
|
+
const url = new URL(this.cloudBrowserApiHost + '/vault');
|
|
1601
|
+
url.searchParams.set('key', this.key);
|
|
1602
|
+
|
|
1603
|
+
const response = await this.fetch({
|
|
1604
|
+
url: url.toString(),
|
|
1605
|
+
method: 'GET',
|
|
1606
|
+
headers: { 'user-agent': this.ua },
|
|
1607
|
+
});
|
|
1608
|
+
|
|
1609
|
+
if (!response.ok) {
|
|
1610
|
+
throw new Error(`Vault list failed: ${response.status} ${await response.text()}`);
|
|
1611
|
+
}
|
|
1612
|
+
const json = await response.json() as { vaults: Vault[] | null };
|
|
1613
|
+
return { vaults: json.vaults ?? [] };
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
/** Get a single vault by id. Does not return any secret material. */
|
|
1617
|
+
async cloudBrowserVaultGet(vaultId: string): Promise<{ vault: Vault }> {
|
|
1618
|
+
const url = new URL(this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId));
|
|
1619
|
+
url.searchParams.set('key', this.key);
|
|
1620
|
+
|
|
1621
|
+
const response = await this.fetch({
|
|
1622
|
+
url: url.toString(),
|
|
1623
|
+
method: 'GET',
|
|
1624
|
+
headers: { 'user-agent': this.ua },
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
if (!response.ok) {
|
|
1628
|
+
throw new Error(`Vault get failed: ${response.status} ${await response.text()}`);
|
|
1629
|
+
}
|
|
1630
|
+
return await response.json() as { vault: Vault };
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
/** Update vault metadata (name / description). Does not require the vault key. */
|
|
1634
|
+
async cloudBrowserVaultUpdate(
|
|
1635
|
+
vaultId: string,
|
|
1636
|
+
opts: { name?: string; description?: string },
|
|
1637
|
+
): Promise<{ vault: Vault; message?: string }> {
|
|
1638
|
+
const url = new URL(this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId));
|
|
1639
|
+
url.searchParams.set('key', this.key);
|
|
1640
|
+
|
|
1641
|
+
const body: Record<string, unknown> = {};
|
|
1642
|
+
if (opts.name !== undefined) body.name = opts.name;
|
|
1643
|
+
if (opts.description !== undefined) body.description = opts.description;
|
|
1644
|
+
|
|
1645
|
+
const response = await this.fetch({
|
|
1646
|
+
url: url.toString(),
|
|
1647
|
+
method: 'PATCH',
|
|
1648
|
+
headers: {
|
|
1649
|
+
'content-type': 'application/json',
|
|
1650
|
+
'user-agent': this.ua,
|
|
1651
|
+
},
|
|
1652
|
+
body: JSON.stringify(body),
|
|
1653
|
+
});
|
|
1654
|
+
|
|
1655
|
+
if (!response.ok) {
|
|
1656
|
+
throw new Error(`Vault update failed: ${response.status} ${await response.text()}`);
|
|
1657
|
+
}
|
|
1658
|
+
return await response.json() as { vault: Vault; message?: string };
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
/** Delete a vault and every item in it. Does not require the vault key. */
|
|
1662
|
+
async cloudBrowserVaultDelete(vaultId: string): Promise<{ success: boolean; message: string }> {
|
|
1663
|
+
const url = new URL(this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId));
|
|
1664
|
+
url.searchParams.set('key', this.key);
|
|
1665
|
+
|
|
1666
|
+
const response = await this.fetch({
|
|
1667
|
+
url: url.toString(),
|
|
1668
|
+
method: 'DELETE',
|
|
1669
|
+
headers: { 'user-agent': this.ua },
|
|
1670
|
+
});
|
|
1671
|
+
|
|
1672
|
+
if (!response.ok) {
|
|
1673
|
+
throw new Error(`Vault delete failed: ${response.status} ${await response.text()}`);
|
|
1674
|
+
}
|
|
1675
|
+
const body = await response.json() as Record<string, any>;
|
|
1676
|
+
return {
|
|
1677
|
+
success: body.success !== undefined ? Boolean(body.success) : true,
|
|
1678
|
+
message: typeof body.message === 'string' ? body.message : '',
|
|
1679
|
+
};
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/**
|
|
1683
|
+
* Rotate the vault key. The caller MUST pass the CURRENT key in
|
|
1684
|
+
* `currentVaultKey`; the server returns a fresh `key` exactly once. After
|
|
1685
|
+
* this call the old key cannot decrypt any row in the vault.
|
|
1686
|
+
*/
|
|
1687
|
+
async cloudBrowserVaultRotate(
|
|
1688
|
+
vaultId: string,
|
|
1689
|
+
currentVaultKey: string,
|
|
1690
|
+
): Promise<{ key: string; message: string }> {
|
|
1691
|
+
const url = new URL(
|
|
1692
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/rotate',
|
|
1693
|
+
);
|
|
1694
|
+
url.searchParams.set('key', this.key);
|
|
1695
|
+
|
|
1696
|
+
const response = await this.fetch({
|
|
1697
|
+
url: url.toString(),
|
|
1698
|
+
method: 'POST',
|
|
1699
|
+
headers: {
|
|
1700
|
+
'user-agent': this.ua,
|
|
1701
|
+
'X-Vault-Key': currentVaultKey,
|
|
1702
|
+
},
|
|
1703
|
+
});
|
|
1704
|
+
|
|
1705
|
+
if (!response.ok) {
|
|
1706
|
+
// Body may include the X-Vault-Key error message — safe to surface
|
|
1707
|
+
// because the server message names the header but not its value.
|
|
1708
|
+
throw new Error(`Vault rotate failed: ${response.status} ${await response.text()}`);
|
|
1709
|
+
}
|
|
1710
|
+
return await response.json() as { key: string; message: string };
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
/** List items in a vault. Items carry only opaque ciphertext, no plaintext. */
|
|
1714
|
+
async cloudBrowserVaultItemList(vaultId: string): Promise<{ items: VaultItem[] }> {
|
|
1715
|
+
const url = new URL(
|
|
1716
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/item',
|
|
1717
|
+
);
|
|
1718
|
+
url.searchParams.set('key', this.key);
|
|
1719
|
+
|
|
1720
|
+
const response = await this.fetch({
|
|
1721
|
+
url: url.toString(),
|
|
1722
|
+
method: 'GET',
|
|
1723
|
+
headers: { 'user-agent': this.ua },
|
|
1724
|
+
});
|
|
1725
|
+
|
|
1726
|
+
if (!response.ok) {
|
|
1727
|
+
throw new Error(`Vault item list failed: ${response.status} ${await response.text()}`);
|
|
1728
|
+
}
|
|
1729
|
+
const json = await response.json() as { items: VaultItem[] | null };
|
|
1730
|
+
return { items: json.items ?? [] };
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1733
|
+
/**
|
|
1734
|
+
* Add a credential to a vault. Requires the vault key in the X-Vault-Key
|
|
1735
|
+
* header so the server can encrypt the secret payload at rest.
|
|
1736
|
+
*/
|
|
1737
|
+
async cloudBrowserVaultItemCreate(
|
|
1738
|
+
vaultId: string,
|
|
1739
|
+
vaultKey: string,
|
|
1740
|
+
item: VaultItemCreate,
|
|
1741
|
+
): Promise<{ item: VaultItem; message: string }> {
|
|
1742
|
+
const url = new URL(
|
|
1743
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/item',
|
|
1744
|
+
);
|
|
1745
|
+
url.searchParams.set('key', this.key);
|
|
1746
|
+
|
|
1747
|
+
const body: Record<string, unknown> = {
|
|
1748
|
+
type: item.type,
|
|
1749
|
+
label: item.label,
|
|
1750
|
+
origin: item.origin,
|
|
1751
|
+
secret: item.secret,
|
|
1752
|
+
};
|
|
1753
|
+
if (item.username !== undefined) body.username = item.username;
|
|
1754
|
+
if (item.metadata !== undefined) body.metadata = item.metadata;
|
|
1755
|
+
|
|
1756
|
+
const response = await this.fetch({
|
|
1757
|
+
url: url.toString(),
|
|
1758
|
+
method: 'POST',
|
|
1759
|
+
headers: {
|
|
1760
|
+
'content-type': 'application/json',
|
|
1761
|
+
'user-agent': this.ua,
|
|
1762
|
+
'X-Vault-Key': vaultKey,
|
|
1763
|
+
},
|
|
1764
|
+
body: JSON.stringify(body),
|
|
1765
|
+
});
|
|
1766
|
+
|
|
1767
|
+
if (!response.ok) {
|
|
1768
|
+
throw new Error(`Vault item create failed: ${response.status} ${await response.text()}`);
|
|
1769
|
+
}
|
|
1770
|
+
return await response.json() as { item: VaultItem; message: string };
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
/**
|
|
1774
|
+
* Patch an item's metadata or rotate its secret. The server only requires
|
|
1775
|
+
* X-Vault-Key when `secret` is supplied (ciphertext rotation). Passing
|
|
1776
|
+
* `vaultKey` without `secret` is a no-op on the server side and is allowed
|
|
1777
|
+
* for caller convenience.
|
|
1778
|
+
*/
|
|
1779
|
+
async cloudBrowserVaultItemUpdate(
|
|
1780
|
+
vaultId: string,
|
|
1781
|
+
itemId: string,
|
|
1782
|
+
opts: {
|
|
1783
|
+
vaultKey?: string;
|
|
1784
|
+
type?: VaultItem['type'];
|
|
1785
|
+
label?: string;
|
|
1786
|
+
origin?: string;
|
|
1787
|
+
username?: string;
|
|
1788
|
+
secret?: VaultSecret;
|
|
1789
|
+
metadata?: Record<string, unknown>;
|
|
1790
|
+
},
|
|
1791
|
+
): Promise<{ item?: VaultItem; message: string }> {
|
|
1792
|
+
if (opts.secret !== undefined && (opts.vaultKey === undefined || opts.vaultKey === '')) {
|
|
1793
|
+
throw new Error(
|
|
1794
|
+
'cloudBrowserVaultItemUpdate: vaultKey is required when rotating the item secret',
|
|
1795
|
+
);
|
|
1796
|
+
}
|
|
1797
|
+
if (opts.secret !== undefined && opts.type === undefined) {
|
|
1798
|
+
throw new Error(
|
|
1799
|
+
'cloudBrowserVaultItemUpdate: type is required when rotating the item secret',
|
|
1800
|
+
);
|
|
1801
|
+
}
|
|
1802
|
+
const url = new URL(
|
|
1803
|
+
this.cloudBrowserApiHost +
|
|
1804
|
+
'/vault/' +
|
|
1805
|
+
encodeURIComponent(vaultId) +
|
|
1806
|
+
'/item/' +
|
|
1807
|
+
encodeURIComponent(itemId),
|
|
1808
|
+
);
|
|
1809
|
+
url.searchParams.set('key', this.key);
|
|
1810
|
+
|
|
1811
|
+
const body: Record<string, unknown> = {};
|
|
1812
|
+
if (opts.type !== undefined) body.type = opts.type;
|
|
1813
|
+
if (opts.label !== undefined) body.label = opts.label;
|
|
1814
|
+
if (opts.origin !== undefined) body.origin = opts.origin;
|
|
1815
|
+
if (opts.username !== undefined) body.username = opts.username;
|
|
1816
|
+
if (opts.metadata !== undefined) body.metadata = opts.metadata;
|
|
1817
|
+
if (opts.secret !== undefined) body.secret = opts.secret;
|
|
1818
|
+
|
|
1819
|
+
const headers: Record<string, string> = {
|
|
1820
|
+
'content-type': 'application/json',
|
|
1821
|
+
'user-agent': this.ua,
|
|
1822
|
+
};
|
|
1823
|
+
if (opts.vaultKey !== undefined && opts.vaultKey !== '') {
|
|
1824
|
+
headers['X-Vault-Key'] = opts.vaultKey;
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
const response = await this.fetch({
|
|
1828
|
+
url: url.toString(),
|
|
1829
|
+
method: 'PATCH',
|
|
1830
|
+
headers,
|
|
1831
|
+
body: JSON.stringify(body),
|
|
1832
|
+
});
|
|
1833
|
+
|
|
1834
|
+
if (!response.ok) {
|
|
1835
|
+
throw new Error(`Vault item update failed: ${response.status} ${await response.text()}`);
|
|
1836
|
+
}
|
|
1837
|
+
return await response.json() as { item?: VaultItem; message: string };
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
/** Delete a single vault item. Does not require the vault key. */
|
|
1841
|
+
async cloudBrowserVaultItemDelete(
|
|
1842
|
+
vaultId: string,
|
|
1843
|
+
itemId: string,
|
|
1844
|
+
): Promise<{ success: boolean; message: string }> {
|
|
1845
|
+
const url = new URL(
|
|
1846
|
+
this.cloudBrowserApiHost +
|
|
1847
|
+
'/vault/' +
|
|
1848
|
+
encodeURIComponent(vaultId) +
|
|
1849
|
+
'/item/' +
|
|
1850
|
+
encodeURIComponent(itemId),
|
|
1851
|
+
);
|
|
1852
|
+
url.searchParams.set('key', this.key);
|
|
1853
|
+
|
|
1854
|
+
const response = await this.fetch({
|
|
1855
|
+
url: url.toString(),
|
|
1856
|
+
method: 'DELETE',
|
|
1857
|
+
headers: { 'user-agent': this.ua },
|
|
1858
|
+
});
|
|
1859
|
+
|
|
1860
|
+
if (!response.ok) {
|
|
1861
|
+
throw new Error(`Vault item delete failed: ${response.status} ${await response.text()}`);
|
|
1862
|
+
}
|
|
1863
|
+
const body = await response.json() as Record<string, any>;
|
|
1864
|
+
return {
|
|
1865
|
+
success: body.success !== undefined ? Boolean(body.success) : true,
|
|
1866
|
+
message: typeof body.message === 'string' ? body.message : '',
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1499
1870
|
/**
|
|
1500
1871
|
* Download a debug session recording video.
|
|
1501
1872
|
* Returns the video as an ArrayBuffer (stream from server).
|
|
@@ -1519,7 +1890,9 @@ export class ScrapflyClient {
|
|
|
1519
1890
|
}
|
|
1520
1891
|
|
|
1521
1892
|
/**
|
|
1522
|
-
* Get playback info for a debug session recording.
|
|
1893
|
+
* Get playback info for a debug session recording. The returned object
|
|
1894
|
+
* carries `available`, `status` ('ready' | 'uploading' | 'unavailable' |
|
|
1895
|
+
* 'disabled'), `metadata`, `video_url`, and `retry_after_ms`.
|
|
1523
1896
|
*/
|
|
1524
1897
|
async cloudBrowserPlayback(runId: string): Promise<Record<string, any>> {
|
|
1525
1898
|
const url = new URL(this.cloudBrowserApiHost + '/run/' + runId + '/playback');
|
|
@@ -1537,6 +1910,29 @@ export class ScrapflyClient {
|
|
|
1537
1910
|
return await response.json() as Record<string, any>;
|
|
1538
1911
|
}
|
|
1539
1912
|
|
|
1913
|
+
/**
|
|
1914
|
+
* Poll the playback endpoint until the recording resolves to a terminal
|
|
1915
|
+
* state ('ready' or 'unavailable') or the timeout elapses. Honours the
|
|
1916
|
+
* server-side retry_after_ms hint when present.
|
|
1917
|
+
*/
|
|
1918
|
+
async cloudBrowserWaitForPlayback(
|
|
1919
|
+
runId: string,
|
|
1920
|
+
opts: { timeoutMs?: number; pollIntervalFallbackMs?: number } = {},
|
|
1921
|
+
): Promise<Record<string, any>> {
|
|
1922
|
+
const timeoutMs = opts.timeoutMs ?? 180_000;
|
|
1923
|
+
const fallbackMs = opts.pollIntervalFallbackMs ?? 3_000;
|
|
1924
|
+
const deadline = Date.now() + timeoutMs;
|
|
1925
|
+
while (true) {
|
|
1926
|
+
const playback = await this.cloudBrowserPlayback(runId);
|
|
1927
|
+
if (playback.status !== 'uploading') return playback;
|
|
1928
|
+
const remaining = deadline - Date.now();
|
|
1929
|
+
if (remaining <= 0) return playback;
|
|
1930
|
+
const retryAfter = Number(playback.retry_after_ms) || fallbackMs;
|
|
1931
|
+
const sleepFor = Math.min(retryAfter, remaining);
|
|
1932
|
+
await new Promise((resolve) => setTimeout(resolve, sleepFor));
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1540
1936
|
/**
|
|
1541
1937
|
* List all running Cloud Browser sessions.
|
|
1542
1938
|
*/
|
|
@@ -1555,6 +1951,189 @@ export class ScrapflyClient {
|
|
|
1555
1951
|
}
|
|
1556
1952
|
return await response.json() as { sessions: any[]; total: number };
|
|
1557
1953
|
}
|
|
1954
|
+
|
|
1955
|
+
// -------------------------------------------------------------------------
|
|
1956
|
+
// Public schedule endpoints — /scrape/schedules, /screenshot/schedules,
|
|
1957
|
+
// /crawl/schedules, /schedules. The kind-specific config is supplied to the
|
|
1958
|
+
// matching `create*Schedule` method; the cross-kind list/get/update/delete/
|
|
1959
|
+
// pause/resume/execute methods work regardless of kind.
|
|
1960
|
+
// -------------------------------------------------------------------------
|
|
1961
|
+
|
|
1962
|
+
/** Create a Web Scraping API schedule. `scrape_config` is the same shape
|
|
1963
|
+
* accepted by `/scrape` (e.g. `{ url, render_js, asp, ... }`). */
|
|
1964
|
+
async createScrapeSchedule(
|
|
1965
|
+
scrapeConfig: Record<string, unknown>,
|
|
1966
|
+
request: CreateScheduleRequest,
|
|
1967
|
+
): Promise<Schedule> {
|
|
1968
|
+
return await this.scheduleRequest(
|
|
1969
|
+
'POST',
|
|
1970
|
+
'/scrape/schedules',
|
|
1971
|
+
undefined,
|
|
1972
|
+
this.buildCreateBody('scrape_config', scrapeConfig, request),
|
|
1973
|
+
) as Schedule;
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
/** Create a Screenshot API schedule. */
|
|
1977
|
+
async createScreenshotSchedule(
|
|
1978
|
+
screenshotConfig: Record<string, unknown>,
|
|
1979
|
+
request: CreateScheduleRequest,
|
|
1980
|
+
): Promise<Schedule> {
|
|
1981
|
+
return await this.scheduleRequest(
|
|
1982
|
+
'POST',
|
|
1983
|
+
'/screenshot/schedules',
|
|
1984
|
+
undefined,
|
|
1985
|
+
this.buildCreateBody('screenshot_config', screenshotConfig, request),
|
|
1986
|
+
) as Schedule;
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
/** Create a Crawler API schedule. */
|
|
1990
|
+
async createCrawlerSchedule(
|
|
1991
|
+
crawlerConfig: Record<string, unknown>,
|
|
1992
|
+
request: CreateScheduleRequest,
|
|
1993
|
+
): Promise<Schedule> {
|
|
1994
|
+
return await this.scheduleRequest(
|
|
1995
|
+
'POST',
|
|
1996
|
+
'/crawl/schedules',
|
|
1997
|
+
undefined,
|
|
1998
|
+
this.buildCreateBody('crawler_config', crawlerConfig, request),
|
|
1999
|
+
) as Schedule;
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
/** Return a schedule by id. Works across all kinds. */
|
|
2003
|
+
async getSchedule(id: string): Promise<Schedule> {
|
|
2004
|
+
return await this.scheduleRequest('GET', `/schedules/${encodeURIComponent(id)}`) as Schedule;
|
|
2005
|
+
}
|
|
2006
|
+
|
|
2007
|
+
/** List every schedule on the account, optionally filtered by kind/status. */
|
|
2008
|
+
async listSchedules(opts?: ListSchedulesOptions): Promise<Schedule[]> {
|
|
2009
|
+
return await this.scheduleRequest(
|
|
2010
|
+
'GET',
|
|
2011
|
+
'/schedules',
|
|
2012
|
+
opts as Record<string, string | undefined> | undefined,
|
|
2013
|
+
) as Schedule[];
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
async listScrapeSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
2017
|
+
return await this.scheduleRequest('GET', '/scrape/schedules', opts) as Schedule[];
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
async listScreenshotSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
2021
|
+
return await this.scheduleRequest('GET', '/screenshot/schedules', opts) as Schedule[];
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
async listCrawlerSchedules(opts?: { status?: string }): Promise<Schedule[]> {
|
|
2025
|
+
return await this.scheduleRequest('GET', '/crawl/schedules', opts) as Schedule[];
|
|
2026
|
+
}
|
|
2027
|
+
|
|
2028
|
+
/** Patch an active schedule. Only fields set in `request` change. */
|
|
2029
|
+
async updateSchedule(id: string, request: UpdateScheduleRequest): Promise<Schedule> {
|
|
2030
|
+
return await this.scheduleRequest(
|
|
2031
|
+
'PATCH',
|
|
2032
|
+
`/schedules/${encodeURIComponent(id)}`,
|
|
2033
|
+
undefined,
|
|
2034
|
+
request as Record<string, unknown>,
|
|
2035
|
+
) as Schedule;
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/** Cancel a schedule. Cancellation is terminal (returns no body). */
|
|
2039
|
+
async cancelSchedule(id: string): Promise<void> {
|
|
2040
|
+
await this.scheduleRequest('DELETE', `/schedules/${encodeURIComponent(id)}`);
|
|
2041
|
+
}
|
|
2042
|
+
|
|
2043
|
+
async pauseSchedule(id: string): Promise<Schedule> {
|
|
2044
|
+
return await this.scheduleRequest(
|
|
2045
|
+
'POST',
|
|
2046
|
+
`/schedules/${encodeURIComponent(id)}/pause`,
|
|
2047
|
+
) as Schedule;
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
async resumeSchedule(id: string): Promise<Schedule> {
|
|
2051
|
+
return await this.scheduleRequest(
|
|
2052
|
+
'POST',
|
|
2053
|
+
`/schedules/${encodeURIComponent(id)}/resume`,
|
|
2054
|
+
) as Schedule;
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
/** Fire a schedule immediately, regardless of next_scheduled_date. */
|
|
2058
|
+
async executeSchedule(id: string): Promise<Schedule> {
|
|
2059
|
+
return await this.scheduleRequest(
|
|
2060
|
+
'POST',
|
|
2061
|
+
`/schedules/${encodeURIComponent(id)}/execute`,
|
|
2062
|
+
) as Schedule;
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
private buildCreateBody(
|
|
2066
|
+
configKey: string,
|
|
2067
|
+
config: Record<string, unknown>,
|
|
2068
|
+
request: CreateScheduleRequest,
|
|
2069
|
+
): Record<string, unknown> {
|
|
2070
|
+
const body: Record<string, unknown> = {
|
|
2071
|
+
[configKey]: config,
|
|
2072
|
+
webhook_name: request.webhook_name,
|
|
2073
|
+
allow_concurrency: request.allow_concurrency ?? false,
|
|
2074
|
+
retry_on_failure: request.retry_on_failure ?? false,
|
|
2075
|
+
};
|
|
2076
|
+
if (request.recurrence) body.recurrence = request.recurrence;
|
|
2077
|
+
if (request.scheduled_date) body.scheduled_date = request.scheduled_date;
|
|
2078
|
+
if (typeof request.max_retries === 'number') body.max_retries = request.max_retries;
|
|
2079
|
+
if (request.notes) body.notes = request.notes;
|
|
2080
|
+
return body;
|
|
2081
|
+
}
|
|
2082
|
+
|
|
2083
|
+
private async scheduleRequest(
|
|
2084
|
+
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
|
|
2085
|
+
path: string,
|
|
2086
|
+
query?: Record<string, string | undefined>,
|
|
2087
|
+
body?: Record<string, unknown>,
|
|
2088
|
+
): Promise<unknown> {
|
|
2089
|
+
const url = new URL(this.HOST + path);
|
|
2090
|
+
const params = new URLSearchParams();
|
|
2091
|
+
params.set('key', this.key);
|
|
2092
|
+
if (query) {
|
|
2093
|
+
for (const [k, v] of Object.entries(query)) {
|
|
2094
|
+
if (v !== undefined && v !== '') params.set(k, v);
|
|
2095
|
+
}
|
|
2096
|
+
}
|
|
2097
|
+
url.search = params.toString();
|
|
2098
|
+
|
|
2099
|
+
const init: { url: string; method: string; headers: Record<string, string>; body?: string } = {
|
|
2100
|
+
url: url.toString(),
|
|
2101
|
+
method,
|
|
2102
|
+
headers: {
|
|
2103
|
+
'user-agent': this.ua,
|
|
2104
|
+
accept: 'application/json',
|
|
2105
|
+
},
|
|
2106
|
+
};
|
|
2107
|
+
if (body !== undefined) {
|
|
2108
|
+
init.body = JSON.stringify(body);
|
|
2109
|
+
init.headers['content-type'] = 'application/json';
|
|
2110
|
+
}
|
|
2111
|
+
|
|
2112
|
+
const response = await this.fetch(init as any);
|
|
2113
|
+
|
|
2114
|
+
if (response.status === 204) return undefined;
|
|
2115
|
+
if (!response.ok) {
|
|
2116
|
+
let envelope: { error?: string; message?: string; reason?: string; details?: unknown } = {};
|
|
2117
|
+
try {
|
|
2118
|
+
envelope = await response.json() as typeof envelope;
|
|
2119
|
+
} catch {
|
|
2120
|
+
// body wasn't JSON; fall through with the empty envelope
|
|
2121
|
+
}
|
|
2122
|
+
const code = envelope.error ?? 'ERR::SCHEDULER::BACKEND_ERROR';
|
|
2123
|
+
let message = envelope.message ?? '';
|
|
2124
|
+
if (envelope.reason) {
|
|
2125
|
+
message = message ? `${message} (${envelope.reason})` : envelope.reason;
|
|
2126
|
+
}
|
|
2127
|
+
if (!message) message = `HTTP ${response.status}`;
|
|
2128
|
+
throw new ScheduleAPIError({
|
|
2129
|
+
code,
|
|
2130
|
+
message,
|
|
2131
|
+
httpStatusCode: response.status,
|
|
2132
|
+
details: envelope.details,
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
return await response.json();
|
|
2136
|
+
}
|
|
1558
2137
|
}
|
|
1559
2138
|
|
|
1560
2139
|
// ---------------------------------------------------------------------------
|