scrapfly-sdk 0.12.0 → 0.12.2
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 +38 -9
- package/esm/browserconfig.d.ts +20 -4
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +12 -2
- package/esm/browserdownloads.d.ts +70 -0
- package/esm/browserdownloads.d.ts.map +1 -0
- package/esm/browserdownloads.js +215 -0
- package/esm/client.d.ts +107 -4
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +307 -24
- package/esm/crawl.d.ts +26 -1
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +18 -0
- package/esm/crawlerconfig.d.ts +8 -3
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +1 -1
- package/esm/crawlerresult.d.ts +39 -0
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/main.d.ts +10 -5
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +8 -3
- package/esm/monitoringconfig.d.ts +2 -2
- package/esm/result.d.ts +11 -1
- package/esm/result.d.ts.map +1 -1
- package/esm/result.js +14 -5
- package/esm/scrapeconfig.d.ts +19 -7
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +33 -1
- package/esm/screenshotconfig.d.ts +8 -1
- package/esm/screenshotconfig.d.ts.map +1 -1
- package/esm/screenshotconfig.js +12 -1
- package/esm/types.d.ts +128 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/types.js +22 -1
- package/esm/utils.d.ts.map +1 -1
- package/esm/utils.js +16 -2
- package/package.json +1 -1
- package/script/browserconfig.d.ts +20 -4
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +12 -2
- package/script/browserdownloads.d.ts +70 -0
- package/script/browserdownloads.d.ts.map +1 -0
- package/script/browserdownloads.js +221 -0
- package/script/client.d.ts +107 -4
- package/script/client.d.ts.map +1 -1
- package/script/client.js +307 -24
- package/script/crawl.d.ts +26 -1
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +18 -0
- package/script/crawlerconfig.d.ts +8 -3
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +1 -1
- package/script/crawlerresult.d.ts +39 -0
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/main.d.ts +10 -5
- package/script/main.d.ts.map +1 -1
- package/script/main.js +13 -4
- package/script/monitoringconfig.d.ts +2 -2
- package/script/result.d.ts +11 -1
- package/script/result.d.ts.map +1 -1
- package/script/result.js +14 -5
- package/script/scrapeconfig.d.ts +19 -7
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +33 -1
- package/script/screenshotconfig.d.ts +8 -1
- package/script/screenshotconfig.d.ts.map +1 -1
- package/script/screenshotconfig.js +12 -1
- package/script/types.d.ts +128 -0
- package/script/types.d.ts.map +1 -1
- package/script/types.js +23 -0
- package/script/utils.d.ts.map +1 -1
- package/script/utils.js +16 -2
- package/src/browserconfig.ts +22 -4
- package/src/browserdownloads.ts +266 -0
- package/src/client.ts +390 -27
- package/src/crawl.ts +32 -0
- package/src/crawlerconfig.ts +8 -3
- package/src/crawlerresult.ts +41 -0
- package/src/main.ts +31 -4
- package/src/monitoringconfig.ts +2 -2
- package/src/result.ts +13 -5
- package/src/scrapeconfig.ts +24 -7
- package/src/screenshotconfig.ts +13 -1
- package/src/types.ts +159 -0
- package/src/utils.ts +16 -2
package/src/client.ts
CHANGED
|
@@ -8,13 +8,28 @@ 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 {
|
|
11
|
+
import type {
|
|
12
|
+
Rec,
|
|
13
|
+
Vault,
|
|
14
|
+
VaultItem,
|
|
15
|
+
VaultItemCreate,
|
|
16
|
+
VaultLinkedService,
|
|
17
|
+
VaultLinkedServiceValue,
|
|
18
|
+
VaultProbeResult,
|
|
19
|
+
VaultProbeVault,
|
|
20
|
+
VaultSecret,
|
|
21
|
+
VaultServiceLink,
|
|
22
|
+
VaultServiceUpdate,
|
|
23
|
+
VaultSyncReport,
|
|
24
|
+
} from './types.js';
|
|
12
25
|
import type { CrawlerConfig, CrawlerContentFormat } from './crawlerconfig.js';
|
|
13
26
|
import { REFRESH_MAX_INTERVAL, REFRESH_MIN_INTERVAL } from './crawlerconfig.js';
|
|
14
27
|
import {
|
|
15
28
|
CrawlerArtifact,
|
|
16
29
|
type CrawlerArtifactType,
|
|
17
30
|
CrawlerContents,
|
|
31
|
+
type CrawlerExtractResponse,
|
|
32
|
+
type CrawlerPromptAnswer,
|
|
18
33
|
type CrawlerPromptEvent,
|
|
19
34
|
type CrawlerRefreshEntry,
|
|
20
35
|
type CrawlerRefreshState,
|
|
@@ -203,8 +218,7 @@ export class ScrapflyClient {
|
|
|
203
218
|
|
|
204
219
|
try {
|
|
205
220
|
const url = new URL(result.content);
|
|
206
|
-
|
|
207
|
-
url.search = new URLSearchParams(params).toString();
|
|
221
|
+
url.searchParams.set('key', this.key);
|
|
208
222
|
response = await this.fetch({
|
|
209
223
|
url: url.toString(),
|
|
210
224
|
method: 'GET',
|
|
@@ -219,6 +233,20 @@ export class ScrapflyClient {
|
|
|
219
233
|
throw e;
|
|
220
234
|
}
|
|
221
235
|
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
const body = await response.text();
|
|
238
|
+
let parsed: unknown = null;
|
|
239
|
+
try {
|
|
240
|
+
parsed = JSON.parse(body);
|
|
241
|
+
} catch {
|
|
242
|
+
// Not an API error: a proxy's page, say.
|
|
243
|
+
}
|
|
244
|
+
const envelope = parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)
|
|
245
|
+
? parsed as Record<string, unknown>
|
|
246
|
+
: { message: body.slice(0, 500) || response.statusText };
|
|
247
|
+
throw this.batchApiErrorPartToError(envelope, { 'x-scrapfly-scrape-status': String(response.status) });
|
|
248
|
+
}
|
|
249
|
+
|
|
222
250
|
let content;
|
|
223
251
|
|
|
224
252
|
if (format === 'clob') {
|
|
@@ -241,7 +269,8 @@ export class ScrapflyClient {
|
|
|
241
269
|
handleResponse(response: Response, result: ScrapeResult): ScrapeResult {
|
|
242
270
|
// success
|
|
243
271
|
log.debug('scrape log url: ', result.result.log_url);
|
|
244
|
-
|
|
272
|
+
// QUEUED: a scrape with a webhook, delivered to it later.
|
|
273
|
+
if (result.result.status === 'QUEUED' || (result.result.status === 'DONE' && result.result.success === true)) {
|
|
245
274
|
return result;
|
|
246
275
|
}
|
|
247
276
|
// something went wrong
|
|
@@ -716,6 +745,12 @@ export class ScrapflyClient {
|
|
|
716
745
|
// Drop `key` (batch key is in the URL).
|
|
717
746
|
const params = cfg.toApiParams({ key: this.key }) as Record<string, unknown>;
|
|
718
747
|
delete params.key;
|
|
748
|
+
// What a single scrape sends as the request itself: the API reads
|
|
749
|
+
// both keys of a batch entry and runs a GET without them.
|
|
750
|
+
params.method = cfg.method;
|
|
751
|
+
if (cfg.body !== undefined) {
|
|
752
|
+
params.body = cfg.body;
|
|
753
|
+
}
|
|
719
754
|
bodyConfigs.push(params);
|
|
720
755
|
}
|
|
721
756
|
|
|
@@ -801,11 +836,13 @@ export class ScrapflyClient {
|
|
|
801
836
|
}
|
|
802
837
|
|
|
803
838
|
try {
|
|
839
|
+
// A config with a webhook is answered by its enqueue ack, the scrape's answer under `result`.
|
|
840
|
+
const envelope = (parsed as any).enqueued === true && (parsed as any).result ? (parsed as any).result : parsed;
|
|
804
841
|
const result = new ScrapeResult({
|
|
805
|
-
config:
|
|
806
|
-
context:
|
|
807
|
-
result:
|
|
808
|
-
uuid:
|
|
842
|
+
config: envelope.config,
|
|
843
|
+
context: envelope.context,
|
|
844
|
+
result: envelope.result,
|
|
845
|
+
uuid: envelope.uuid,
|
|
809
846
|
});
|
|
810
847
|
yield [correlationId, result];
|
|
811
848
|
} catch (resultErr) {
|
|
@@ -823,7 +860,7 @@ export class ScrapflyClient {
|
|
|
823
860
|
* Save screenshot response to a file
|
|
824
861
|
*/
|
|
825
862
|
async saveScreenshot(result: ScreenshotResult, name: string, savePath?: string): Promise<any> {
|
|
826
|
-
if (!result.image) {
|
|
863
|
+
if (!result.image || result.image.byteLength === 0) {
|
|
827
864
|
throw new Error('Screenshot binary does not exist');
|
|
828
865
|
}
|
|
829
866
|
|
|
@@ -847,25 +884,38 @@ export class ScrapflyClient {
|
|
|
847
884
|
* Turn scrapfly screenshot API response to ScreenshotResult or raise one of ScrapflyError
|
|
848
885
|
*/
|
|
849
886
|
async handleScreenshotResponse(response: Response): Promise<ScreenshotResult> {
|
|
850
|
-
// A
|
|
851
|
-
//
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
887
|
+
// A JSON answer is never an image: an error, or the 201 of a screenshot with a webhook.
|
|
888
|
+
// A Response body can only be consumed once, so it is read as text and parsed from that.
|
|
889
|
+
const isJson = (response.headers.get('content-type') ?? '').startsWith('application/json');
|
|
890
|
+
if (isJson || response.status === 201) {
|
|
891
|
+
const text = await response.text();
|
|
892
|
+
let jsonData: Rec<any> | null = null;
|
|
893
|
+
try {
|
|
894
|
+
const parsed = JSON.parse(text);
|
|
895
|
+
if (parsed !== null && typeof parsed === 'object') {
|
|
896
|
+
jsonData = parsed as Rec<any>;
|
|
897
|
+
}
|
|
898
|
+
} catch {
|
|
899
|
+
jsonData = null;
|
|
859
900
|
}
|
|
860
|
-
|
|
861
|
-
|
|
901
|
+
const message = jsonData ? JSON.stringify(jsonData) : text || `API returned status ${response.status}`;
|
|
902
|
+
if (response.status == 401 || jsonData?.http_code == 401) {
|
|
903
|
+
throw new errors.BadApiKeyError(message);
|
|
862
904
|
}
|
|
905
|
+
if (jsonData) {
|
|
906
|
+
if ('error_id' in jsonData) {
|
|
907
|
+
throw new errors.ScreenshotApiError(JSON.stringify(jsonData));
|
|
908
|
+
}
|
|
909
|
+
// 201: a screenshot with a webhook, delivered to it later.
|
|
910
|
+
const job = jsonData.job_uuid;
|
|
911
|
+
if (response.status === 201 && jsonData.webhook_enqueued === true && typeof job === 'string' && job !== '') {
|
|
912
|
+
return new ScreenshotResult(response, new ArrayBuffer(0), job);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
throw new errors.ScreenshotApiError(message);
|
|
863
916
|
}
|
|
864
917
|
if (!response.ok) {
|
|
865
|
-
|
|
866
|
-
throw new errors.ScreenshotApiError(
|
|
867
|
-
typeof body === 'string' ? body : JSON.stringify(body),
|
|
868
|
-
);
|
|
918
|
+
throw new errors.ScreenshotApiError((await response.text()) || `API returned status ${response.status}`);
|
|
869
919
|
}
|
|
870
920
|
const data = await response.arrayBuffer();
|
|
871
921
|
const result = new ScreenshotResult(response, data);
|
|
@@ -970,7 +1020,8 @@ export class ScrapflyClient {
|
|
|
970
1020
|
private throwCrawlerError(response: Response, data: Rec<any>): never {
|
|
971
1021
|
const httpCode = data.http_code ?? response.status;
|
|
972
1022
|
const code = (data.code ?? '') as string;
|
|
973
|
-
|
|
1023
|
+
// The engine relays its own failures as `{error, code}`.
|
|
1024
|
+
const message = data.message ?? (typeof data.error === 'string' ? data.error : JSON.stringify(data));
|
|
974
1025
|
if (httpCode === 401) {
|
|
975
1026
|
throw new errors.BadApiKeyError(message, { http_status_code: httpCode, code, api_response: data });
|
|
976
1027
|
}
|
|
@@ -1687,6 +1738,98 @@ export class ScrapflyClient {
|
|
|
1687
1738
|
yield* parseCrawlerPromptStream(response.body);
|
|
1688
1739
|
}
|
|
1689
1740
|
|
|
1741
|
+
/**
|
|
1742
|
+
* Ask a question answered from the content of one or more crawls, and get
|
|
1743
|
+
* the whole answer in one object once generation completes.
|
|
1744
|
+
*
|
|
1745
|
+
* `POST /crawl/prompt` with `generation.stream: false`: the same facts as
|
|
1746
|
+
* the frames of {@link crawlPrompt}. {@link crawl.Crawl.promptAnswer} is
|
|
1747
|
+
* sugar over a one-element list.
|
|
1748
|
+
*/
|
|
1749
|
+
async crawlPromptAnswer(
|
|
1750
|
+
crawlIds: string[],
|
|
1751
|
+
prompt: string,
|
|
1752
|
+
opts?: {
|
|
1753
|
+
/** Retrieval overrides. Unset uses the server defaults. */
|
|
1754
|
+
search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Rec<any> };
|
|
1755
|
+
/** Generation model id. Unset uses the server default. */
|
|
1756
|
+
model?: string;
|
|
1757
|
+
},
|
|
1758
|
+
): Promise<CrawlerPromptAnswer> {
|
|
1759
|
+
if (!Array.isArray(crawlIds) || crawlIds.length === 0) {
|
|
1760
|
+
throw new errors.ScrapflyCrawlerError('crawl_ids must contain at least one crawler UUID');
|
|
1761
|
+
}
|
|
1762
|
+
if (!prompt) {
|
|
1763
|
+
throw new errors.ScrapflyCrawlerError('prompt must be a non-empty string');
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
const generation: Rec<any> = { stream: false };
|
|
1767
|
+
if (opts?.model !== undefined) generation.model = opts.model;
|
|
1768
|
+
const payload: Rec<any> = { prompt, crawl_ids: crawlIds, generation };
|
|
1769
|
+
if (opts?.search !== undefined) payload.search = opts.search;
|
|
1770
|
+
|
|
1771
|
+
return (await this.postCrawlerJson('/crawl/prompt', payload)) as CrawlerPromptAnswer;
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
/**
|
|
1775
|
+
* Extract a structured object from the content of one or more crawls.
|
|
1776
|
+
*
|
|
1777
|
+
* `POST /crawl/extract` retrieves like {@link crawlPromptAnswer} and answers
|
|
1778
|
+
* with `data` shaped by `schema`, a JSON Schema handed to the model as its
|
|
1779
|
+
* response schema. There is no streaming variant. {@link crawl.Crawl.extract}
|
|
1780
|
+
* is sugar over a one-element list.
|
|
1781
|
+
*/
|
|
1782
|
+
async crawlExtract<T = any>(
|
|
1783
|
+
crawlIds: string[],
|
|
1784
|
+
prompt: string,
|
|
1785
|
+
schema: Rec<any>,
|
|
1786
|
+
opts?: {
|
|
1787
|
+
/** Retrieval overrides. Unset uses the server defaults. */
|
|
1788
|
+
search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Rec<any> };
|
|
1789
|
+
},
|
|
1790
|
+
): Promise<CrawlerExtractResponse<T>> {
|
|
1791
|
+
if (!Array.isArray(crawlIds) || crawlIds.length === 0) {
|
|
1792
|
+
throw new errors.ScrapflyCrawlerError('crawl_ids must contain at least one crawler UUID');
|
|
1793
|
+
}
|
|
1794
|
+
if (!prompt) {
|
|
1795
|
+
throw new errors.ScrapflyCrawlerError('prompt must be a non-empty string');
|
|
1796
|
+
}
|
|
1797
|
+
if (typeof schema !== 'object' || schema === null || Array.isArray(schema) || Object.keys(schema).length === 0) {
|
|
1798
|
+
throw new errors.ScrapflyCrawlerError('schema must be a non-empty JSON Schema object');
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
const payload: Rec<any> = { prompt, crawl_ids: crawlIds, schema };
|
|
1802
|
+
if (opts?.search !== undefined) payload.search = opts.search;
|
|
1803
|
+
|
|
1804
|
+
return (await this.postCrawlerJson('/crawl/extract', payload)) as CrawlerExtractResponse<T>;
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
/** POST a JSON body to a crawler endpoint that generates, once, and return its JSON answer. */
|
|
1808
|
+
private async postCrawlerJson(path: string, payload: Rec<any>): Promise<Rec<any>> {
|
|
1809
|
+
const url = new URL(`${this.HOST}${path}`);
|
|
1810
|
+
url.search = new URLSearchParams({ key: this.key }).toString();
|
|
1811
|
+
// One attempt only: a retry re-runs the fan-out and the generation.
|
|
1812
|
+
const response = await this.fetch(
|
|
1813
|
+
{
|
|
1814
|
+
url: url.toString(),
|
|
1815
|
+
method: 'POST',
|
|
1816
|
+
headers: {
|
|
1817
|
+
'user-agent': this.ua,
|
|
1818
|
+
'content-type': 'application/json',
|
|
1819
|
+
accept: 'application/json',
|
|
1820
|
+
},
|
|
1821
|
+
body: JSON.stringify(payload),
|
|
1822
|
+
},
|
|
1823
|
+
1,
|
|
1824
|
+
);
|
|
1825
|
+
|
|
1826
|
+
if (!response.ok) {
|
|
1827
|
+
const data: Rec<any> = (await response.json().catch(() => ({}))) as Rec<any>;
|
|
1828
|
+
this.throwCrawlerError(response, data);
|
|
1829
|
+
}
|
|
1830
|
+
return (await response.json()) as Rec<any>;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1690
1833
|
// --- Cloud Browser ---
|
|
1691
1834
|
|
|
1692
1835
|
/**
|
|
@@ -1727,7 +1870,7 @@ export class ScrapflyClient {
|
|
|
1727
1870
|
*
|
|
1728
1871
|
* Unblock always uses a residential proxy (no proxy pool selection) and
|
|
1729
1872
|
* always performs a GET — the endpoint's purpose is to harvest cookies /
|
|
1730
|
-
* clearance tokens via an
|
|
1873
|
+
* clearance tokens via an Unblocker navigation, not to proxy arbitrary
|
|
1731
1874
|
* HTTP requests.
|
|
1732
1875
|
*/
|
|
1733
1876
|
async cloudBrowserUnblock(options: {
|
|
@@ -2220,6 +2363,226 @@ export class ScrapflyClient {
|
|
|
2220
2363
|
};
|
|
2221
2364
|
}
|
|
2222
2365
|
|
|
2366
|
+
// ----- Vault ↔ linked secret manager (1Password) ---------------------------
|
|
2367
|
+
// A linked vault mirrors an upstream secret manager instead of holding
|
|
2368
|
+
// operator-entered items. The provider's service-account token is sealed
|
|
2369
|
+
// under the vault key like any other secret, so every endpoint that carries
|
|
2370
|
+
// a token also carries X-Vault-Key: the server verifies the key BEFORE
|
|
2371
|
+
// sealing, because a well-formed wrong key would link the vault and leave a
|
|
2372
|
+
// token every later sync fails to open.
|
|
2373
|
+
|
|
2374
|
+
/**
|
|
2375
|
+
* Link a vault to a secret manager. The token is sealed under `vaultKey`, so
|
|
2376
|
+
* the key is mandatory here. The vault must not already be linked and must
|
|
2377
|
+
* hold no rows owned by a previous service (the server answers 409).
|
|
2378
|
+
*/
|
|
2379
|
+
async cloudBrowserVaultServiceLink(
|
|
2380
|
+
vaultId: string,
|
|
2381
|
+
vaultKey: string,
|
|
2382
|
+
link: VaultServiceLink,
|
|
2383
|
+
): Promise<{ vault: Vault; message: string }> {
|
|
2384
|
+
if (!link.token) {
|
|
2385
|
+
throw new Error('cloudBrowserVaultServiceLink: token is required when linking a service');
|
|
2386
|
+
}
|
|
2387
|
+
// Same rule as the server's linked_service_data validation; caught here so a
|
|
2388
|
+
// malformed link does not cost a round trip.
|
|
2389
|
+
if (!link.linked_service_data?.vault_id && !link.linked_service_data?.vault_name) {
|
|
2390
|
+
throw new Error(
|
|
2391
|
+
'cloudBrowserVaultServiceLink: linked_service_data requires vault_id or vault_name',
|
|
2392
|
+
);
|
|
2393
|
+
}
|
|
2394
|
+
const url = new URL(
|
|
2395
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/service',
|
|
2396
|
+
);
|
|
2397
|
+
url.searchParams.set('key', this.key);
|
|
2398
|
+
|
|
2399
|
+
const response = await this.fetch({
|
|
2400
|
+
url: url.toString(),
|
|
2401
|
+
method: 'POST',
|
|
2402
|
+
headers: {
|
|
2403
|
+
'content-type': 'application/json',
|
|
2404
|
+
'user-agent': this.ua,
|
|
2405
|
+
'X-Vault-Key': vaultKey,
|
|
2406
|
+
},
|
|
2407
|
+
body: JSON.stringify({
|
|
2408
|
+
linked_service: link.linked_service,
|
|
2409
|
+
token: link.token,
|
|
2410
|
+
linked_service_data: link.linked_service_data,
|
|
2411
|
+
}),
|
|
2412
|
+
});
|
|
2413
|
+
|
|
2414
|
+
if (!response.ok) {
|
|
2415
|
+
throw new Error(`Vault service link failed: ${response.status} ${await response.text()}`);
|
|
2416
|
+
}
|
|
2417
|
+
return await response.json() as { vault: Vault; message: string };
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
/**
|
|
2421
|
+
* Update a link: edit the non-secret selection rules, rotate the token, or
|
|
2422
|
+
* both. `vaultKey` is required only when `token` is supplied, mirroring
|
|
2423
|
+
* `cloudBrowserVaultItemUpdate`; a config-only edit needs no key.
|
|
2424
|
+
*
|
|
2425
|
+
* A token rotation also clears the server's one-hour auth back-off, so a link
|
|
2426
|
+
* broken by a dead token syncs again on the next round.
|
|
2427
|
+
*/
|
|
2428
|
+
async cloudBrowserVaultServiceUpdate(
|
|
2429
|
+
vaultId: string,
|
|
2430
|
+
opts: VaultServiceUpdate & { vaultKey?: string },
|
|
2431
|
+
): Promise<{ vault: Vault; message: string }> {
|
|
2432
|
+
if (opts.token !== undefined && opts.token !== '' && (opts.vaultKey === undefined || opts.vaultKey === '')) {
|
|
2433
|
+
throw new Error(
|
|
2434
|
+
'cloudBrowserVaultServiceUpdate: vaultKey is required when rotating the service token',
|
|
2435
|
+
);
|
|
2436
|
+
}
|
|
2437
|
+
const url = new URL(
|
|
2438
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/service',
|
|
2439
|
+
);
|
|
2440
|
+
url.searchParams.set('key', this.key);
|
|
2441
|
+
|
|
2442
|
+
const body: Record<string, unknown> = {};
|
|
2443
|
+
if (opts.token !== undefined) body.token = opts.token;
|
|
2444
|
+
if (opts.linked_service_data !== undefined) body.linked_service_data = opts.linked_service_data;
|
|
2445
|
+
|
|
2446
|
+
const headers: Record<string, string> = {
|
|
2447
|
+
'content-type': 'application/json',
|
|
2448
|
+
'user-agent': this.ua,
|
|
2449
|
+
};
|
|
2450
|
+
if (opts.vaultKey !== undefined && opts.vaultKey !== '') {
|
|
2451
|
+
headers['X-Vault-Key'] = opts.vaultKey;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
const response = await this.fetch({
|
|
2455
|
+
url: url.toString(),
|
|
2456
|
+
method: 'PATCH',
|
|
2457
|
+
headers,
|
|
2458
|
+
body: JSON.stringify(body),
|
|
2459
|
+
});
|
|
2460
|
+
|
|
2461
|
+
if (!response.ok) {
|
|
2462
|
+
throw new Error(`Vault service update failed: ${response.status} ${await response.text()}`);
|
|
2463
|
+
}
|
|
2464
|
+
return await response.json() as { vault: Vault; message: string };
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
/**
|
|
2468
|
+
* Unlink the provider. `keepItems` governs only the mirrored rows — kept as
|
|
2469
|
+
* plain manual items, or deleted — while the sealed token is removed either
|
|
2470
|
+
* way, so no vault key is needed.
|
|
2471
|
+
*
|
|
2472
|
+
* Dropping the rows is irreversible, so the default is to keep them and the
|
|
2473
|
+
* flag always goes on the wire explicitly rather than relying on a default at
|
|
2474
|
+
* either end.
|
|
2475
|
+
*/
|
|
2476
|
+
async cloudBrowserVaultServiceUnlink(
|
|
2477
|
+
vaultId: string,
|
|
2478
|
+
opts: { keepItems?: boolean } = {},
|
|
2479
|
+
): Promise<{ success: boolean; message: string }> {
|
|
2480
|
+
const url = new URL(
|
|
2481
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/service',
|
|
2482
|
+
);
|
|
2483
|
+
url.searchParams.set('key', this.key);
|
|
2484
|
+
url.searchParams.set('keep_items', (opts.keepItems ?? true) ? 'true' : 'false');
|
|
2485
|
+
|
|
2486
|
+
const response = await this.fetch({
|
|
2487
|
+
url: url.toString(),
|
|
2488
|
+
method: 'DELETE',
|
|
2489
|
+
headers: { 'user-agent': this.ua },
|
|
2490
|
+
});
|
|
2491
|
+
|
|
2492
|
+
if (!response.ok) {
|
|
2493
|
+
throw new Error(`Vault service unlink failed: ${response.status} ${await response.text()}`);
|
|
2494
|
+
}
|
|
2495
|
+
const body = await response.json() as Record<string, any>;
|
|
2496
|
+
return {
|
|
2497
|
+
success: body.success !== undefined ? Boolean(body.success) : true,
|
|
2498
|
+
message: typeof body.message === 'string' ? body.message : '',
|
|
2499
|
+
};
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
/**
|
|
2503
|
+
* Force a reconcile against the provider now, bypassing the sync TTL and the
|
|
2504
|
+
* back-off a failed round leaves behind. Requires the vault key: the server
|
|
2505
|
+
* opens the sealed token with it and re-seals every mirrored row.
|
|
2506
|
+
*
|
|
2507
|
+
* The server budget is 25s; the SDK pins no per-call timeout, so the runtime's
|
|
2508
|
+
* own fetch default applies.
|
|
2509
|
+
*/
|
|
2510
|
+
async cloudBrowserVaultServiceSync(vaultId: string, vaultKey: string): Promise<VaultSyncReport> {
|
|
2511
|
+
const url = new URL(
|
|
2512
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/service/sync',
|
|
2513
|
+
);
|
|
2514
|
+
url.searchParams.set('key', this.key);
|
|
2515
|
+
|
|
2516
|
+
const response = await this.fetch({
|
|
2517
|
+
url: url.toString(),
|
|
2518
|
+
method: 'POST',
|
|
2519
|
+
headers: {
|
|
2520
|
+
'user-agent': this.ua,
|
|
2521
|
+
'X-Vault-Key': vaultKey,
|
|
2522
|
+
},
|
|
2523
|
+
});
|
|
2524
|
+
|
|
2525
|
+
if (!response.ok) {
|
|
2526
|
+
throw new Error(`Vault service sync failed: ${response.status} ${await response.text()}`);
|
|
2527
|
+
}
|
|
2528
|
+
return await response.json() as VaultSyncReport;
|
|
2529
|
+
}
|
|
2530
|
+
|
|
2531
|
+
/**
|
|
2532
|
+
* Probe the provider: proves a token is live and lists the upstream vaults it
|
|
2533
|
+
* can read. Writes nothing, so it never moves the vault's sync state.
|
|
2534
|
+
*
|
|
2535
|
+
* With no `token` the sealed token of an existing link is probed (the vault
|
|
2536
|
+
* key opens it). Passing an unsaved `token` instead enumerates the upstream
|
|
2537
|
+
* vaults BEFORE a link exists, which is how a caller picks `vault_id`.
|
|
2538
|
+
*
|
|
2539
|
+
* `linkedService` stays optional and undefaulted here: this is the one endpoint
|
|
2540
|
+
* whose empty discriminator the server itself resolves to 1Password, and a
|
|
2541
|
+
* client-side default would send a field the bodyless stored-token path must
|
|
2542
|
+
* not carry.
|
|
2543
|
+
*
|
|
2544
|
+
* The server budget is 10s; the SDK pins no per-call timeout, so the runtime's
|
|
2545
|
+
* own fetch default applies.
|
|
2546
|
+
*/
|
|
2547
|
+
async cloudBrowserVaultServiceTest(
|
|
2548
|
+
vaultId: string,
|
|
2549
|
+
vaultKey: string,
|
|
2550
|
+
opts: { linkedService?: VaultLinkedServiceValue; token?: string } = {},
|
|
2551
|
+
): Promise<VaultProbeResult> {
|
|
2552
|
+
const url = new URL(
|
|
2553
|
+
this.cloudBrowserApiHost + '/vault/' + encodeURIComponent(vaultId) + '/service/test',
|
|
2554
|
+
);
|
|
2555
|
+
url.searchParams.set('key', this.key);
|
|
2556
|
+
|
|
2557
|
+
const headers: Record<string, string> = {
|
|
2558
|
+
'user-agent': this.ua,
|
|
2559
|
+
'X-Vault-Key': vaultKey,
|
|
2560
|
+
};
|
|
2561
|
+
// A bodyless request is what selects the stored-token path server-side, so
|
|
2562
|
+
// send a body only when the caller supplied something to probe with.
|
|
2563
|
+
let body: string | undefined;
|
|
2564
|
+
if (opts.token !== undefined || opts.linkedService !== undefined) {
|
|
2565
|
+
const payload: Record<string, unknown> = {};
|
|
2566
|
+
if (opts.linkedService !== undefined) payload.linked_service = opts.linkedService;
|
|
2567
|
+
if (opts.token !== undefined) payload.token = opts.token;
|
|
2568
|
+
body = JSON.stringify(payload);
|
|
2569
|
+
headers['content-type'] = 'application/json';
|
|
2570
|
+
}
|
|
2571
|
+
|
|
2572
|
+
const response = await this.fetch({
|
|
2573
|
+
url: url.toString(),
|
|
2574
|
+
method: 'POST',
|
|
2575
|
+
headers,
|
|
2576
|
+
body,
|
|
2577
|
+
});
|
|
2578
|
+
|
|
2579
|
+
if (!response.ok) {
|
|
2580
|
+
throw new Error(`Vault service test failed: ${response.status} ${await response.text()}`);
|
|
2581
|
+
}
|
|
2582
|
+
const json = await response.json() as VaultProbeResult & { vaults_visible: VaultProbeVault[] | null };
|
|
2583
|
+
return { ...json, vaults_visible: json.vaults_visible ?? [] };
|
|
2584
|
+
}
|
|
2585
|
+
|
|
2223
2586
|
/**
|
|
2224
2587
|
* Download a debug session recording video.
|
|
2225
2588
|
* Returns the video as an ArrayBuffer (stream from server).
|
|
@@ -2366,7 +2729,7 @@ export class ScrapflyClient {
|
|
|
2366
2729
|
return await this.scheduleRequest('GET', `/schedules/${encodeURIComponent(id)}`) as Schedule;
|
|
2367
2730
|
}
|
|
2368
2731
|
|
|
2369
|
-
/** List
|
|
2732
|
+
/** List the schedules of the API key's project and environment, optionally filtered by kind/status. */
|
|
2370
2733
|
async listSchedules(opts?: ListSchedulesOptions): Promise<Schedule[]> {
|
|
2371
2734
|
return await this.scheduleRequest(
|
|
2372
2735
|
'GET',
|
package/src/crawl.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type {
|
|
|
6
6
|
CrawlerArtifact,
|
|
7
7
|
CrawlerArtifactType,
|
|
8
8
|
CrawlerContents,
|
|
9
|
+
CrawlerExtractResponse,
|
|
10
|
+
CrawlerPromptAnswer,
|
|
9
11
|
CrawlerPromptEvent,
|
|
10
12
|
CrawlerRefreshEntry,
|
|
11
13
|
CrawlerRefreshState,
|
|
@@ -235,6 +237,36 @@ export class Crawl {
|
|
|
235
237
|
return this.client.crawlPrompt([uuid], prompt, opts);
|
|
236
238
|
}
|
|
237
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Ask a question answered from this crawl's content, the whole answer at once.
|
|
242
|
+
*
|
|
243
|
+
* Sugar over `client.crawlPromptAnswer([uuid], ...)`.
|
|
244
|
+
*/
|
|
245
|
+
async promptAnswer(
|
|
246
|
+
prompt: string,
|
|
247
|
+
opts?: {
|
|
248
|
+
search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any> };
|
|
249
|
+
model?: string;
|
|
250
|
+
},
|
|
251
|
+
): Promise<CrawlerPromptAnswer> {
|
|
252
|
+
const uuid = this.requireUuid();
|
|
253
|
+
return this.client.crawlPromptAnswer([uuid], prompt, opts);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Extract a structured object from this crawl's content, shaped by `schema`.
|
|
258
|
+
*
|
|
259
|
+
* Sugar over `client.crawlExtract([uuid], ...)`.
|
|
260
|
+
*/
|
|
261
|
+
async extract<T = any>(
|
|
262
|
+
prompt: string,
|
|
263
|
+
schema: Record<string, any>,
|
|
264
|
+
opts?: { search?: { limit?: number; mode?: CrawlerSearchMode; filters?: Record<string, any> } },
|
|
265
|
+
): Promise<CrawlerExtractResponse<T>> {
|
|
266
|
+
const uuid = this.requireUuid();
|
|
267
|
+
return this.client.crawlExtract<T>([uuid], prompt, schema, opts);
|
|
268
|
+
}
|
|
269
|
+
|
|
238
270
|
/**
|
|
239
271
|
* Re-scrape this crawl's URLs in place, right now.
|
|
240
272
|
*
|
package/src/crawlerconfig.ts
CHANGED
|
@@ -133,8 +133,9 @@ export type CrawlerConfigOptions = {
|
|
|
133
133
|
|
|
134
134
|
// Web scraping features
|
|
135
135
|
/**
|
|
136
|
-
*
|
|
137
|
-
* Sent in the `POST /crawl` body as `asp`; only the SDK-facing
|
|
136
|
+
* The Unblocker (anti-bot bypass), the current name for what used to be
|
|
137
|
+
* called `asp`. Sent in the `POST /crawl` body as `asp`; only the SDK-facing
|
|
138
|
+
* name changed.
|
|
138
139
|
*/
|
|
139
140
|
unblocker?: boolean;
|
|
140
141
|
/**
|
|
@@ -144,6 +145,10 @@ export type CrawlerConfigOptions = {
|
|
|
144
145
|
* later `unblocker`.
|
|
145
146
|
*/
|
|
146
147
|
asp?: boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Proxy pool for every crawled page: `public_datacenter_pool` (the default),
|
|
150
|
+
* `public_residential_pool` or `public_tor_pool`.
|
|
151
|
+
*/
|
|
147
152
|
proxy_pool?: string;
|
|
148
153
|
country?: string;
|
|
149
154
|
|
|
@@ -351,7 +356,7 @@ export class CrawlerConfig {
|
|
|
351
356
|
}
|
|
352
357
|
|
|
353
358
|
/**
|
|
354
|
-
*
|
|
359
|
+
* The Unblocker toggle, the current name for what used to be `asp`.
|
|
355
360
|
*
|
|
356
361
|
* An accessor over the single `asp` field rather than a second stored
|
|
357
362
|
* property: one storage slot means the two names can never disagree, and
|
package/src/crawlerresult.ts
CHANGED
|
@@ -553,6 +553,47 @@ export interface CrawlerPromptSource {
|
|
|
553
553
|
score?: number;
|
|
554
554
|
}
|
|
555
555
|
|
|
556
|
+
/** How much of the corpus a prompt answer or an extraction covers. */
|
|
557
|
+
export interface CrawlerPromptSearch {
|
|
558
|
+
completeness: 'exact' | 'partial';
|
|
559
|
+
crawls_requested?: number;
|
|
560
|
+
crawls_searched?: number;
|
|
561
|
+
crawls_pruned_exact?: number;
|
|
562
|
+
/** Requested crawls that contributed nothing, with a reason. Never fatal. */
|
|
563
|
+
skipped: CrawlerSearchSkipped[];
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/** The answer of `POST /crawl/prompt` with `generation.stream: false`: the facts of the stream, in one object. */
|
|
567
|
+
export interface CrawlerPromptAnswer {
|
|
568
|
+
prompt: string;
|
|
569
|
+
answer: string;
|
|
570
|
+
sources: CrawlerPromptSource[];
|
|
571
|
+
sources_used: number[];
|
|
572
|
+
/** Retrieved chunks that did not fit the context and never reached the model. */
|
|
573
|
+
sources_dropped: number;
|
|
574
|
+
/** True when the model hit its output cap. The answer is still delivered. */
|
|
575
|
+
truncated: boolean;
|
|
576
|
+
search: CrawlerPromptSearch;
|
|
577
|
+
/** Actual charge, including zero. Undefined when the server does not report it. */
|
|
578
|
+
api_credit?: number;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* The response of `POST /crawl/extract`. `data` is shaped by the request's JSON
|
|
583
|
+
* Schema, which constrains the model as it generates; it is not validated again
|
|
584
|
+
* afterwards, so validate on your side anything you act on.
|
|
585
|
+
*/
|
|
586
|
+
export interface CrawlerExtractResponse<T = any> {
|
|
587
|
+
prompt: string;
|
|
588
|
+
data: T;
|
|
589
|
+
/** The retrieved chunks `data` was built from. */
|
|
590
|
+
sources: CrawlerPromptSource[];
|
|
591
|
+
/** Retrieved chunks that did not fit the context and never reached the model. */
|
|
592
|
+
sources_dropped: number;
|
|
593
|
+
/** True when the model hit its output cap. */
|
|
594
|
+
truncated: boolean;
|
|
595
|
+
search: CrawlerPromptSearch;
|
|
596
|
+
}
|
|
556
597
|
|
|
557
598
|
/** Payload of the terminal `done` frame. */
|
|
558
599
|
export interface CrawlerPromptDone {
|