scrapfly-sdk 0.10.6 → 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/browserconfig.d.ts +54 -0
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +106 -0
- package/esm/client.d.ts +104 -3
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +332 -14
- 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 +1 -0
- package/esm/main.d.ts.map +1 -1
- 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/package.json +1 -1
- package/script/browserconfig.d.ts +54 -0
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +106 -0
- package/script/client.d.ts +104 -3
- package/script/client.d.ts.map +1 -1
- package/script/client.js +332 -14
- 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 +1 -0
- package/script/main.d.ts.map +1 -1
- 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/src/browserconfig.ts +87 -0
- package/src/client.ts +402 -15
- package/src/crawlerconfig.ts +27 -6
- package/src/extractionconfig.ts +1 -1
- package/src/main.ts +1 -0
- package/src/scrapeconfig.ts +4 -5
- package/src/types.ts +115 -0
- package/src/utils.ts +6 -1
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 {
|
|
@@ -935,17 +935,52 @@ export class ScrapflyClient {
|
|
|
935
935
|
try {
|
|
936
936
|
const url = new URL(this.HOST + '/crawl');
|
|
937
937
|
url.search = new URLSearchParams({ key: this.key }).toString();
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
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
|
+
}
|
|
949
984
|
} catch (e) {
|
|
950
985
|
log.error('error', e);
|
|
951
986
|
throw e;
|
|
@@ -1309,12 +1344,25 @@ export class ScrapflyClient {
|
|
|
1309
1344
|
|
|
1310
1345
|
/**
|
|
1311
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
|
|
1312
1351
|
*/
|
|
1313
1352
|
cloudBrowser(config?: BrowserConfig): string {
|
|
1314
1353
|
const browserConfig = config || new BrowserConfig();
|
|
1315
1354
|
return browserConfig.websocketUrl(this.key, this.cloudBrowserHost);
|
|
1316
1355
|
}
|
|
1317
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
|
+
|
|
1318
1366
|
/**
|
|
1319
1367
|
* Call the Cloud Browser Unblock API.
|
|
1320
1368
|
*/
|
|
@@ -1323,13 +1371,14 @@ export class ScrapflyClient {
|
|
|
1323
1371
|
proxy_pool?: string;
|
|
1324
1372
|
country?: string;
|
|
1325
1373
|
os?: string;
|
|
1374
|
+
browser_brand?: string;
|
|
1326
1375
|
timeout?: number;
|
|
1327
1376
|
browser_timeout?: number;
|
|
1328
1377
|
headers?: Record<string, string>;
|
|
1329
1378
|
body?: string;
|
|
1330
1379
|
method?: string;
|
|
1331
1380
|
enable_mcp?: boolean;
|
|
1332
|
-
|
|
1381
|
+
debug?: boolean;
|
|
1333
1382
|
}): Promise<{ ws_url: string; session_id: string; run_id: string; mcp_endpoint?: string }> {
|
|
1334
1383
|
const proxyPoolMap: Record<string, string> = {
|
|
1335
1384
|
datacenter: 'public_datacenter_pool',
|
|
@@ -1340,13 +1389,14 @@ export class ScrapflyClient {
|
|
|
1340
1389
|
if (options.proxy_pool) jsonBody.proxy_pool = proxyPoolMap[options.proxy_pool] || options.proxy_pool;
|
|
1341
1390
|
if (options.country) jsonBody.country = options.country;
|
|
1342
1391
|
if (options.os) jsonBody.os = options.os;
|
|
1392
|
+
if (options.browser_brand) jsonBody.browser_brand = options.browser_brand;
|
|
1343
1393
|
if (options.timeout) jsonBody.timeout = options.timeout;
|
|
1344
1394
|
if (options.browser_timeout) jsonBody.browser_timeout = options.browser_timeout;
|
|
1345
1395
|
if (options.headers) jsonBody.headers = options.headers;
|
|
1346
1396
|
if (options.body) jsonBody.body = options.body;
|
|
1347
1397
|
if (options.method) jsonBody.method = options.method;
|
|
1348
1398
|
if (options.enable_mcp !== undefined) jsonBody.enable_mcp = options.enable_mcp;
|
|
1349
|
-
if (options.
|
|
1399
|
+
if (options.debug !== undefined) jsonBody.debug = options.debug;
|
|
1350
1400
|
|
|
1351
1401
|
const url = new URL(this.cloudBrowserApiHost + '/unblock');
|
|
1352
1402
|
url.searchParams.set('key', this.key);
|
|
@@ -1505,6 +1555,318 @@ export class ScrapflyClient {
|
|
|
1505
1555
|
return await response.json() as { extension: Record<string, any>; is_update: boolean };
|
|
1506
1556
|
}
|
|
1507
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
|
+
|
|
1508
1870
|
/**
|
|
1509
1871
|
* Download a debug session recording video.
|
|
1510
1872
|
* Returns the video as an ArrayBuffer (stream from server).
|
|
@@ -1528,7 +1890,9 @@ export class ScrapflyClient {
|
|
|
1528
1890
|
}
|
|
1529
1891
|
|
|
1530
1892
|
/**
|
|
1531
|
-
* 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`.
|
|
1532
1896
|
*/
|
|
1533
1897
|
async cloudBrowserPlayback(runId: string): Promise<Record<string, any>> {
|
|
1534
1898
|
const url = new URL(this.cloudBrowserApiHost + '/run/' + runId + '/playback');
|
|
@@ -1546,6 +1910,29 @@ export class ScrapflyClient {
|
|
|
1546
1910
|
return await response.json() as Record<string, any>;
|
|
1547
1911
|
}
|
|
1548
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
|
+
|
|
1549
1936
|
/**
|
|
1550
1937
|
* List all running Cloud Browser sessions.
|
|
1551
1938
|
*/
|
package/src/crawlerconfig.ts
CHANGED
|
@@ -65,7 +65,12 @@ const VALID_WEBHOOK_EVENTS: ReadonlySet<string> = new Set([
|
|
|
65
65
|
* which lets the server apply its documented defaults (e.g. `respect_robots_txt=true`).
|
|
66
66
|
*/
|
|
67
67
|
export type CrawlerConfigOptions = {
|
|
68
|
-
url
|
|
68
|
+
// URL source — exactly one of url, url_list, remote_url_list must be set.
|
|
69
|
+
// url enables discovery; url_list and remote_url_list crawl an explicit set
|
|
70
|
+
// with discovery off.
|
|
71
|
+
url?: string;
|
|
72
|
+
url_list?: string[];
|
|
73
|
+
remote_url_list?: string;
|
|
69
74
|
|
|
70
75
|
// Crawl limits
|
|
71
76
|
page_limit?: number;
|
|
@@ -131,7 +136,9 @@ export type CrawlerConfigOptions = {
|
|
|
131
136
|
* ```
|
|
132
137
|
*/
|
|
133
138
|
export class CrawlerConfig {
|
|
134
|
-
url
|
|
139
|
+
url?: string;
|
|
140
|
+
url_list?: string[];
|
|
141
|
+
remote_url_list?: string;
|
|
135
142
|
|
|
136
143
|
page_limit?: number;
|
|
137
144
|
max_depth?: number;
|
|
@@ -174,10 +181,19 @@ export class CrawlerConfig {
|
|
|
174
181
|
constructor(options: CrawlerConfigOptions) {
|
|
175
182
|
this.validateOptions(options);
|
|
176
183
|
|
|
177
|
-
|
|
178
|
-
|
|
184
|
+
const hasSeed = typeof options.url === 'string' && options.url.trim() !== '';
|
|
185
|
+
const hasList = Array.isArray(options.url_list) && options.url_list.length > 0;
|
|
186
|
+
const hasRemote = typeof options.remote_url_list === 'string' && options.remote_url_list.trim() !== '';
|
|
187
|
+
const sourceCount = (hasSeed ? 1 : 0) + (hasList ? 1 : 0) + (hasRemote ? 1 : 0);
|
|
188
|
+
if (sourceCount === 0) {
|
|
189
|
+
throw new CrawlerConfigError('Provide one of: url, url_list, remote_url_list');
|
|
179
190
|
}
|
|
180
|
-
|
|
191
|
+
if (sourceCount > 1) {
|
|
192
|
+
throw new CrawlerConfigError('Only one of url, url_list, remote_url_list can be set');
|
|
193
|
+
}
|
|
194
|
+
if (hasSeed) this.url = options.url;
|
|
195
|
+
if (hasList) this.url_list = options.url_list;
|
|
196
|
+
if (hasRemote) this.remote_url_list = options.remote_url_list;
|
|
181
197
|
|
|
182
198
|
// Mutually exclusive path filters
|
|
183
199
|
if (options.exclude_paths && options.include_only_paths) {
|
|
@@ -272,6 +288,8 @@ export class CrawlerConfig {
|
|
|
272
288
|
private validateOptions(options: Partial<CrawlerConfigOptions>) {
|
|
273
289
|
const validKeys = new Set<string>([
|
|
274
290
|
'url',
|
|
291
|
+
'url_list',
|
|
292
|
+
'remote_url_list',
|
|
275
293
|
'page_limit',
|
|
276
294
|
'max_depth',
|
|
277
295
|
'max_duration',
|
|
@@ -320,8 +338,11 @@ export class CrawlerConfig {
|
|
|
320
338
|
* defaults (e.g. `respect_robots_txt=true`).
|
|
321
339
|
*/
|
|
322
340
|
toApiParams(): Rec<any> {
|
|
323
|
-
const params: Rec<any> = {
|
|
341
|
+
const params: Rec<any> = {};
|
|
324
342
|
const fields: Array<keyof CrawlerConfig> = [
|
|
343
|
+
'url',
|
|
344
|
+
'url_list',
|
|
345
|
+
'remote_url_list',
|
|
325
346
|
'page_limit',
|
|
326
347
|
'max_depth',
|
|
327
348
|
'max_duration',
|
package/src/extractionconfig.ts
CHANGED
|
@@ -168,7 +168,7 @@ export class ExtractionConfig {
|
|
|
168
168
|
}
|
|
169
169
|
|
|
170
170
|
if (this.extraction_template) {
|
|
171
|
-
params.extraction_template = this.extraction_template;
|
|
171
|
+
params.extraction_template = 'persistent:' + this.extraction_template;
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
if (this.extraction_ephemeral_template) {
|
package/src/main.ts
CHANGED
|
@@ -40,6 +40,7 @@ export {
|
|
|
40
40
|
export { ExtractionConfig } from './extractionconfig.js';
|
|
41
41
|
export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
|
|
42
42
|
export type { BrowserConfigOptions } from './browserconfig.js';
|
|
43
|
+
export type { Vault, VaultItem, VaultItemCreate, VaultItemType, VaultSecret } from './types.js';
|
|
43
44
|
/** Namespace of every error class the SDK can throw. See {@link ScrapflyError}. */
|
|
44
45
|
export * as errors from './errors.js';
|
|
45
46
|
|
package/src/scrapeconfig.ts
CHANGED
|
@@ -113,6 +113,7 @@ type ScrapeConfigOptions = {
|
|
|
113
113
|
export class ScrapeConfig {
|
|
114
114
|
static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool';
|
|
115
115
|
static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool';
|
|
116
|
+
static PUBLIC_TOR_POOL = 'public_tor_pool';
|
|
116
117
|
|
|
117
118
|
url: string;
|
|
118
119
|
retry = true;
|
|
@@ -145,7 +146,7 @@ export class ScrapeConfig {
|
|
|
145
146
|
js?: string;
|
|
146
147
|
rendering_wait?: number;
|
|
147
148
|
wait_for_selector?: string;
|
|
148
|
-
session_sticky_proxy =
|
|
149
|
+
session_sticky_proxy = true;
|
|
149
150
|
screenshots?: Rec<any>;
|
|
150
151
|
screenshot_flags?:
|
|
151
152
|
('load_images' | 'dark_mode' | 'block_banners' | 'print_media_format' | 'high_quality' | ScreenshotFlags)[];
|
|
@@ -402,7 +403,7 @@ export class ScrapeConfig {
|
|
|
402
403
|
}
|
|
403
404
|
|
|
404
405
|
if (this.extraction_template) {
|
|
405
|
-
params.extraction_template = this.extraction_template;
|
|
406
|
+
params.extraction_template = 'persistent:' + this.extraction_template;
|
|
406
407
|
}
|
|
407
408
|
|
|
408
409
|
if (this.extraction_ephemeral_template) {
|
|
@@ -421,9 +422,7 @@ export class ScrapeConfig {
|
|
|
421
422
|
}
|
|
422
423
|
if (this.session) {
|
|
423
424
|
params.session = this.session;
|
|
424
|
-
|
|
425
|
-
params.session_sticky_proxy = true;
|
|
426
|
-
}
|
|
425
|
+
params.session_sticky_proxy = this.session_sticky_proxy;
|
|
427
426
|
} else {
|
|
428
427
|
if (this.session_sticky_proxy) {
|
|
429
428
|
log.warn('Params "session_sticky_proxy" is ignored. Works only if session is enabled');
|