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/main.ts
CHANGED
|
@@ -13,10 +13,9 @@
|
|
|
13
13
|
* const client = new ScrapflyClient({ key: 'your-api-key' });
|
|
14
14
|
* const result = await client.scrape(new ScrapeConfig({
|
|
15
15
|
* url: 'https://web-scraping.dev/products',
|
|
16
|
-
* render_js: true,
|
|
17
16
|
* unblocker: true,
|
|
18
17
|
* }));
|
|
19
|
-
* console.log(result.content);
|
|
18
|
+
* console.log(result.result.content);
|
|
20
19
|
* ```
|
|
21
20
|
*
|
|
22
21
|
* See the [Scrapfly docs](https://scrapfly.io/docs) for the full API
|
|
@@ -40,8 +39,33 @@ export {
|
|
|
40
39
|
export { ExtractionConfig } from './extractionconfig.js';
|
|
41
40
|
export { BrowserConfig, ProxyPool, OperatingSystem } from './browserconfig.js';
|
|
42
41
|
export type { BrowserConfigOptions } from './browserconfig.js';
|
|
43
|
-
export
|
|
44
|
-
|
|
42
|
+
export { BrowserDownloadError, CloudBrowserDownloads, parseDownloads } from './browserdownloads.js';
|
|
43
|
+
export type { BrowserDownload, CdpSend, CdpSession, CloudBrowserDownloadsOptions, WaitForOptions } from './browserdownloads.js';
|
|
44
|
+
// Enum, hence a value export: callers name VaultLinkedService.ONE_PASSWORD.
|
|
45
|
+
export { VaultLinkedService } from './types.js';
|
|
46
|
+
export type {
|
|
47
|
+
Vault,
|
|
48
|
+
VaultItem,
|
|
49
|
+
VaultItemCreate,
|
|
50
|
+
VaultLinkedServiceValue,
|
|
51
|
+
VaultItemSource,
|
|
52
|
+
VaultItemType,
|
|
53
|
+
VaultOnePasswordData,
|
|
54
|
+
VaultProbeResult,
|
|
55
|
+
VaultProbeVault,
|
|
56
|
+
VaultSecret,
|
|
57
|
+
VaultServiceLink,
|
|
58
|
+
VaultServiceUpdate,
|
|
59
|
+
VaultSyncCounts,
|
|
60
|
+
VaultSyncMode,
|
|
61
|
+
VaultSyncReport,
|
|
62
|
+
VaultSyncStatus,
|
|
63
|
+
VaultType,
|
|
64
|
+
} from './types.js';
|
|
65
|
+
/**
|
|
66
|
+
* Namespace of the API error classes, {@link ScrapflyError} and its subclasses: `errors.ScrapflyAspError`.
|
|
67
|
+
* `BrowserDownloadError` and `ScheduleAPIError` are exported at the root.
|
|
68
|
+
*/
|
|
45
69
|
export * as errors from './errors.js';
|
|
46
70
|
|
|
47
71
|
export { ExtractionResult, ScrapeResult, ScreenshotResult } from './result.js';
|
|
@@ -67,9 +91,12 @@ export {
|
|
|
67
91
|
} from './crawlerresult.js';
|
|
68
92
|
export type {
|
|
69
93
|
CrawlerArtifactType,
|
|
94
|
+
CrawlerExtractResponse,
|
|
70
95
|
CrawlerLifecycleWebhook,
|
|
96
|
+
CrawlerPromptAnswer,
|
|
71
97
|
CrawlerPromptDone,
|
|
72
98
|
CrawlerPromptEvent,
|
|
99
|
+
CrawlerPromptSearch,
|
|
73
100
|
CrawlerPromptSource,
|
|
74
101
|
CrawlerRefreshEntry,
|
|
75
102
|
CrawlerRefreshState,
|
package/src/monitoringconfig.ts
CHANGED
|
@@ -59,8 +59,8 @@ export interface CloudBrowserMonitoringOptions {
|
|
|
59
59
|
/** Time window to aggregate over. */
|
|
60
60
|
period?: MonitoringPeriod;
|
|
61
61
|
/**
|
|
62
|
-
* Optional filter to a single proxy pool
|
|
63
|
-
*
|
|
62
|
+
* Optional filter to a single proxy pool, as the runs record it:
|
|
63
|
+
* `datacenter`, `residential`, `tor` or `byop`.
|
|
64
64
|
*/
|
|
65
65
|
proxyPool?: string;
|
|
66
66
|
/** Window start. */
|
package/src/result.ts
CHANGED
|
@@ -17,6 +17,7 @@ export type ConfigData = {
|
|
|
17
17
|
debug: boolean;
|
|
18
18
|
raise_on_upstream_error: boolean;
|
|
19
19
|
cache_ttl: number;
|
|
20
|
+
/** The proxy pool the request ran on. */
|
|
20
21
|
proxy_pool: string;
|
|
21
22
|
session?: string;
|
|
22
23
|
tags: Set<string>;
|
|
@@ -74,6 +75,7 @@ export type ContextData = {
|
|
|
74
75
|
country: string;
|
|
75
76
|
identity: string;
|
|
76
77
|
network: string;
|
|
78
|
+
/** The proxy pool the request ran on. */
|
|
77
79
|
pool: string;
|
|
78
80
|
};
|
|
79
81
|
redirects: Array<string>;
|
|
@@ -158,6 +160,8 @@ export type ResultData = {
|
|
|
158
160
|
retryable: boolean;
|
|
159
161
|
doc_url?: string;
|
|
160
162
|
};
|
|
163
|
+
/** Present when the scrape ran an extraction (`extraction_model`, `extraction_prompt` or a template). */
|
|
164
|
+
extracted_data?: { data: any; content_type: string; data_quality?: any } | null;
|
|
161
165
|
format: string;
|
|
162
166
|
iframes: Array<{
|
|
163
167
|
url: string;
|
|
@@ -211,10 +215,10 @@ export class ScrapeResult {
|
|
|
211
215
|
|
|
212
216
|
get selector(): cheerio.CheerioAPI {
|
|
213
217
|
if (!this._selector) {
|
|
214
|
-
const
|
|
215
|
-
if (!
|
|
218
|
+
const contentType = normalizeHeaders(this.result.response_headers)['content-type'] ?? '';
|
|
219
|
+
if (!contentType.includes('text/html')) {
|
|
216
220
|
throw new errors.ContentTypeError(
|
|
217
|
-
`Cannot use selector on non-html content-type, received: ${
|
|
221
|
+
`Cannot use selector on non-html content-type, received: ${contentType}`,
|
|
218
222
|
);
|
|
219
223
|
}
|
|
220
224
|
this._selector = cheerio.load(this.result.content);
|
|
@@ -297,11 +301,15 @@ export class ScreenshotResult {
|
|
|
297
301
|
image: ArrayBuffer;
|
|
298
302
|
metadata: ScreenshotMetadata;
|
|
299
303
|
result: object | null;
|
|
304
|
+
/** The job of a screenshot with a webhook: the image is delivered to the webhook, `image` and `metadata` are empty. */
|
|
305
|
+
job_uuid: string | null;
|
|
300
306
|
|
|
301
|
-
constructor(response: Response, data: ArrayBuffer) {
|
|
307
|
+
constructor(response: Response, data: ArrayBuffer, job_uuid: string | null = null) {
|
|
302
308
|
this.image = data;
|
|
303
|
-
this.metadata =
|
|
309
|
+
this.metadata =
|
|
310
|
+
job_uuid === null ? this.defineMetadata(response) : { extension_name: '', upstream_status_code: 0, upstream_url: '' };
|
|
304
311
|
this.result = this.decodeResponse(response, data); // raw result
|
|
312
|
+
this.job_uuid = job_uuid;
|
|
305
313
|
}
|
|
306
314
|
|
|
307
315
|
private defineMetadata(response: Response): ScreenshotMetadata {
|
package/src/scrapeconfig.ts
CHANGED
|
@@ -55,21 +55,25 @@ type ScrapeConfigOptions = {
|
|
|
55
55
|
ssl?: boolean;
|
|
56
56
|
dns?: boolean;
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
59
|
-
* Sent to the API as the `asp` query parameter; only the
|
|
60
|
-
* changed.
|
|
58
|
+
* The Unblocker (anti-bot bypass), the current name for what used to be
|
|
59
|
+
* called `asp`. Sent to the API as the `asp` query parameter; only the
|
|
60
|
+
* SDK-facing name changed.
|
|
61
61
|
*/
|
|
62
62
|
unblocker?: boolean;
|
|
63
63
|
/**
|
|
64
64
|
* @deprecated Use {@link unblocker}. `asp` is a permanently supported alias
|
|
65
|
-
* and keeps working forever
|
|
66
|
-
*
|
|
67
|
-
* later `unblocker`.
|
|
65
|
+
* and keeps working forever. When both are supplied `asp` wins, so existing
|
|
66
|
+
* code cannot have its meaning changed by a later `unblocker`.
|
|
68
67
|
*/
|
|
69
68
|
asp?: boolean;
|
|
70
69
|
debug?: boolean;
|
|
71
70
|
raise_on_upstream_error?: boolean;
|
|
72
71
|
cache_ttl?: number;
|
|
72
|
+
/**
|
|
73
|
+
* Proxy pool: `ScrapeConfig.PUBLIC_DATACENTER_POOL` (the default),
|
|
74
|
+
* `ScrapeConfig.PUBLIC_RESIDENTIAL_POOL` or `ScrapeConfig.PUBLIC_TOR_POOL`.
|
|
75
|
+
* The anti-bot bypass is a parameter of its own: {@link unblocker}.
|
|
76
|
+
*/
|
|
73
77
|
proxy_pool?: string;
|
|
74
78
|
session?: string;
|
|
75
79
|
tags?: string[];
|
|
@@ -123,10 +127,19 @@ type ScrapeConfigOptions = {
|
|
|
123
127
|
};
|
|
124
128
|
|
|
125
129
|
export class ScrapeConfig {
|
|
130
|
+
// Proxy pools, sent as `proxy_pool`. Datacenter proxies are the default.
|
|
126
131
|
static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool';
|
|
127
132
|
static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool';
|
|
133
|
+
// The Tor network, the only pool that reaches .onion hidden services.
|
|
128
134
|
static PUBLIC_TOR_POOL = 'public_tor_pool';
|
|
129
135
|
|
|
136
|
+
// Pool names the API does not accept yet: a request naming one fails with
|
|
137
|
+
// ERR::PROXY::POOL_NOT_FOUND.
|
|
138
|
+
static BASIC = 'basic';
|
|
139
|
+
static UNBLOCKER = 'unblocker';
|
|
140
|
+
static UNBLOCKER_ULTRA = 'unblocker_ultra';
|
|
141
|
+
static AUTO = 'auto';
|
|
142
|
+
|
|
130
143
|
url: string;
|
|
131
144
|
retry = true;
|
|
132
145
|
method: HttpMethod = 'GET';
|
|
@@ -147,6 +160,10 @@ export class ScrapeConfig {
|
|
|
147
160
|
debug = false;
|
|
148
161
|
raise_on_upstream_error = true;
|
|
149
162
|
cache_ttl?: number;
|
|
163
|
+
/**
|
|
164
|
+
* Proxy pool: `ScrapeConfig.PUBLIC_DATACENTER_POOL` (the default),
|
|
165
|
+
* `ScrapeConfig.PUBLIC_RESIDENTIAL_POOL` or `ScrapeConfig.PUBLIC_TOR_POOL`.
|
|
166
|
+
*/
|
|
150
167
|
proxy_pool?: string;
|
|
151
168
|
session?: string;
|
|
152
169
|
tags: Set<string> = new Set<string>();
|
|
@@ -279,7 +296,7 @@ export class ScrapeConfig {
|
|
|
279
296
|
}
|
|
280
297
|
|
|
281
298
|
/**
|
|
282
|
-
*
|
|
299
|
+
* The Unblocker toggle, the current name for what used to be `asp`.
|
|
283
300
|
*
|
|
284
301
|
* An accessor over the single `asp` field rather than a second stored
|
|
285
302
|
* property: one storage slot means the two names can never disagree, so
|
package/src/screenshotconfig.ts
CHANGED
|
@@ -22,7 +22,7 @@ export enum Format {
|
|
|
22
22
|
PNG = 'png',
|
|
23
23
|
/** WebP encoding. */
|
|
24
24
|
WEBP = 'webp',
|
|
25
|
-
/**
|
|
25
|
+
/** GIF encoding, single frame. */
|
|
26
26
|
GIF = 'gif',
|
|
27
27
|
}
|
|
28
28
|
|
|
@@ -83,6 +83,11 @@ type ScreenshotConfigOptions = {
|
|
|
83
83
|
vision_deficiency?: 'deuteranopia' | 'protanopia' | 'tritanopia' | 'achromatopsia' | 'blurredVision' | 'reducedContrast' | VisionDeficiency;
|
|
84
84
|
/** Webhook name to notify when the screenshot completes. */
|
|
85
85
|
webhook?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Sent as `proxy_pool`. The Screenshot API picks its pool itself and ignores
|
|
88
|
+
* this value.
|
|
89
|
+
*/
|
|
90
|
+
proxy_pool?: string;
|
|
86
91
|
};
|
|
87
92
|
|
|
88
93
|
/**
|
|
@@ -113,6 +118,8 @@ export class ScreenshotConfig {
|
|
|
113
118
|
cache_clear?: boolean;
|
|
114
119
|
vision_deficiency?: 'deuteranopia' | 'protanopia' | 'tritanopia' | 'achromatopsia' | 'blurredVision' | 'reducedContrast' | VisionDeficiency;
|
|
115
120
|
webhook?: string;
|
|
121
|
+
/** Sent as `proxy_pool`; the Screenshot API picks its pool itself and ignores it. */
|
|
122
|
+
proxy_pool?: string;
|
|
116
123
|
|
|
117
124
|
constructor(options: ScreenshotConfigOptions) {
|
|
118
125
|
this.validateOptions(options);
|
|
@@ -143,6 +150,7 @@ export class ScreenshotConfig {
|
|
|
143
150
|
this.cache_clear = options.cache_clear ?? this.cache_clear;
|
|
144
151
|
this.vision_deficiency = options.vision_deficiency ?? this.vision_deficiency;
|
|
145
152
|
this.webhook = options.webhook;
|
|
153
|
+
this.proxy_pool = options.proxy_pool;
|
|
146
154
|
}
|
|
147
155
|
|
|
148
156
|
private validateOptions(options: Partial<ScreenshotConfigOptions>) {
|
|
@@ -232,6 +240,10 @@ export class ScreenshotConfig {
|
|
|
232
240
|
params.webhook_name = this.webhook;
|
|
233
241
|
}
|
|
234
242
|
|
|
243
|
+
if (this.proxy_pool) {
|
|
244
|
+
params.proxy_pool = this.proxy_pool;
|
|
245
|
+
}
|
|
246
|
+
|
|
235
247
|
return params;
|
|
236
248
|
}
|
|
237
249
|
}
|
package/src/types.ts
CHANGED
|
@@ -19,12 +19,28 @@ export type Vault = {
|
|
|
19
19
|
name: string;
|
|
20
20
|
description?: string;
|
|
21
21
|
item_count?: number;
|
|
22
|
+
/** Number of mirrored rows. Populated by `cloudBrowserVaultGet` only. */
|
|
23
|
+
linked_item_count?: number;
|
|
24
|
+
// Link metadata. Non-secret by construction: the provider service-account
|
|
25
|
+
// token is a sealed item, never one of these fields.
|
|
26
|
+
vault_type?: VaultType;
|
|
27
|
+
linked_service?: VaultLinkedService;
|
|
28
|
+
/** Stored `linked_service_data`, which carries the server-owned `token_item_id`. */
|
|
29
|
+
linked_service_data?: VaultOnePasswordData & { token_item_id?: string };
|
|
30
|
+
linked_sync_at?: string;
|
|
31
|
+
linked_sync_started_at?: string;
|
|
32
|
+
linked_sync_status?: VaultSyncStatus;
|
|
33
|
+
linked_sync_error?: string;
|
|
34
|
+
linked_sync_counts?: VaultSyncCounts;
|
|
22
35
|
created_at?: string;
|
|
23
36
|
updated_at?: string;
|
|
24
37
|
// Allow forward-compat fields the server may add.
|
|
25
38
|
[k: string]: unknown;
|
|
26
39
|
};
|
|
27
40
|
|
|
41
|
+
/** Whether a vault's items are operator-entered or mirrored from a provider. */
|
|
42
|
+
export type VaultType = 'manual' | 'linked_service';
|
|
43
|
+
|
|
28
44
|
/** Discriminator for the secret payload of a {@link VaultItem}. */
|
|
29
45
|
export type VaultItemType = 'password' | 'passkey' | 'cookie' | 'totp';
|
|
30
46
|
|
|
@@ -100,11 +116,32 @@ export type VaultItem = {
|
|
|
100
116
|
secret_blob?: string;
|
|
101
117
|
master_key_version?: number;
|
|
102
118
|
metadata?: Record<string, unknown>;
|
|
119
|
+
/**
|
|
120
|
+
* Provenance. Anything other than `manual` is owned by the linked-service
|
|
121
|
+
* syncer: the item endpoints refuse to mutate such a row, and the next sync
|
|
122
|
+
* overwrites or deletes it.
|
|
123
|
+
*/
|
|
124
|
+
source?: VaultItemSource;
|
|
125
|
+
/** Upstream diff key; set on mirrored rows only. */
|
|
126
|
+
source_ref?: string;
|
|
127
|
+
/** Opaque upstream revision the sync diff compares without decrypting. */
|
|
128
|
+
source_version?: string;
|
|
129
|
+
source_synced_at?: string;
|
|
103
130
|
created_at?: string;
|
|
104
131
|
updated_at?: string;
|
|
105
132
|
[k: string]: unknown;
|
|
106
133
|
};
|
|
107
134
|
|
|
135
|
+
/**
|
|
136
|
+
* `source` of a {@link VaultItem}. The sealed provider token is never listed.
|
|
137
|
+
*
|
|
138
|
+
* Server-written. The server's own domain is wider, reserving `service_token`,
|
|
139
|
+
* but that row is never returned to a caller so it is not listed here. Kept as
|
|
140
|
+
* literals so a mirrored row reads as plain data; comparing it against
|
|
141
|
+
* `VaultLinkedService.ONE_PASSWORD` still type-checks.
|
|
142
|
+
*/
|
|
143
|
+
export type VaultItemSource = 'manual' | `${VaultLinkedService}`;
|
|
144
|
+
|
|
108
145
|
/** Payload accepted by `cloudBrowserVaultItemCreate`. */
|
|
109
146
|
export type VaultItemCreate = {
|
|
110
147
|
type: VaultItemType;
|
|
@@ -114,3 +151,125 @@ export type VaultItemCreate = {
|
|
|
114
151
|
secret: VaultSecret;
|
|
115
152
|
metadata?: Record<string, unknown>;
|
|
116
153
|
};
|
|
154
|
+
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
// Linked secret managers (1Password).
|
|
157
|
+
//
|
|
158
|
+
// A linked vault mirrors an upstream secret manager: the provider's
|
|
159
|
+
// service-account token is sealed under the vault key as a reserved item, and
|
|
160
|
+
// the syncer owns every row it imports. The token travels in the request body
|
|
161
|
+
// of link / update / test and is NEVER stored by the caller, logged, or echoed
|
|
162
|
+
// back by the server.
|
|
163
|
+
// ---------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Linked-service discriminator, and the only closed value set a caller has to
|
|
167
|
+
* name when linking. 1Password is the sole entry in the server's linked-service
|
|
168
|
+
* registry; anything else is a 400, not a typed provider error.
|
|
169
|
+
*/
|
|
170
|
+
export enum VaultLinkedService {
|
|
171
|
+
/**
|
|
172
|
+
* 1Password service account. Also the `source` written on every row its
|
|
173
|
+
* syncer mirrors.
|
|
174
|
+
*/
|
|
175
|
+
ONE_PASSWORD = '1password',
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Accepted on the wire: the enum member or its own string value. Derived from
|
|
180
|
+
* {@link VaultLinkedService} so the literal is declared once; the server
|
|
181
|
+
* registry, not this type, is the authority on what links.
|
|
182
|
+
*/
|
|
183
|
+
export type VaultLinkedServiceValue = `${VaultLinkedService}`;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* When the mirror is refreshed. `on_session` refreshes at most once per
|
|
187
|
+
* `sync_ttl_s` when a Cloud Browser session opens the vault; `manual` only
|
|
188
|
+
* syncs on `cloudBrowserVaultServiceSync`. (`live` is rejected by the server.)
|
|
189
|
+
*/
|
|
190
|
+
export type VaultSyncMode = 'manual' | 'on_session';
|
|
191
|
+
|
|
192
|
+
/** Outcome of the last sync round, as stored on the vault row. */
|
|
193
|
+
export type VaultSyncStatus =
|
|
194
|
+
| 'running'
|
|
195
|
+
| 'ok'
|
|
196
|
+
| 'partial'
|
|
197
|
+
| 'auth_failed'
|
|
198
|
+
| 'rate_limited'
|
|
199
|
+
| 'scope_error'
|
|
200
|
+
| 'unavailable'
|
|
201
|
+
| 'error';
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Non-secret selection rules of a 1Password link (`linked_service_data`).
|
|
205
|
+
*
|
|
206
|
+
* `vault_id` or `vault_name` is required — the server rejects a document with
|
|
207
|
+
* neither. `token_item_id` is deliberately absent: the server binds it when the
|
|
208
|
+
* token is sealed, and a PATCH body cannot redirect the token lookup because
|
|
209
|
+
* the stored id always wins.
|
|
210
|
+
*/
|
|
211
|
+
export type VaultOnePasswordData = {
|
|
212
|
+
/** Upstream vault id. Survives an upstream rename, unlike `vault_name`. */
|
|
213
|
+
vault_id?: string;
|
|
214
|
+
vault_name?: string;
|
|
215
|
+
/** Glob over upstream item titles, 128 characters max. */
|
|
216
|
+
title_filter?: string;
|
|
217
|
+
/** At most 16 tags. */
|
|
218
|
+
tags?: string[];
|
|
219
|
+
sync_mode?: VaultSyncMode;
|
|
220
|
+
/** Refresh TTL in seconds, 60 .. 86400. Defaults to 900 server-side. */
|
|
221
|
+
sync_ttl_s?: number;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/** Payload accepted by `cloudBrowserVaultServiceLink`. */
|
|
225
|
+
export type VaultServiceLink = {
|
|
226
|
+
/**
|
|
227
|
+
* Required: `POST /vault/{id}/service` has no server-side default. The raw
|
|
228
|
+
* wire value stays assignable so untyped JavaScript callers keep working.
|
|
229
|
+
*/
|
|
230
|
+
linked_service: VaultLinkedServiceValue;
|
|
231
|
+
/** Provider service-account token. Sealed under the vault key on arrival. */
|
|
232
|
+
token: string;
|
|
233
|
+
linked_service_data: VaultOnePasswordData;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Payload accepted by `cloudBrowserVaultServiceUpdate`. `linked_service` is not
|
|
238
|
+
* settable: the stored discriminator is what the server updates against.
|
|
239
|
+
*/
|
|
240
|
+
export type VaultServiceUpdate = {
|
|
241
|
+
/** Rotates the sealed token. Requires the vault key; omit for a config-only edit. */
|
|
242
|
+
token?: string;
|
|
243
|
+
linked_service_data?: VaultOnePasswordData;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/** Per-round reconcile counts, also stored as `linked_sync_counts`. */
|
|
247
|
+
export type VaultSyncCounts = {
|
|
248
|
+
imported: number;
|
|
249
|
+
updated: number;
|
|
250
|
+
deleted: number;
|
|
251
|
+
/** Rows the reconcile refused, which is what makes a round `partial`. */
|
|
252
|
+
skipped: number;
|
|
253
|
+
/** Upstream items the provider could not map. Does not make a round partial. */
|
|
254
|
+
unmirrored: number;
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/** Body of `cloudBrowserVaultServiceSync`. */
|
|
258
|
+
export type VaultSyncReport = VaultSyncCounts & {
|
|
259
|
+
status: VaultSyncStatus;
|
|
260
|
+
warnings?: string[];
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
/** One upstream vault the service account can read. */
|
|
264
|
+
export type VaultProbeVault = {
|
|
265
|
+
id: string;
|
|
266
|
+
title: string;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/** Body of `cloudBrowserVaultServiceTest`. Reads nothing and writes nothing. */
|
|
270
|
+
export type VaultProbeResult = {
|
|
271
|
+
vaults_visible: VaultProbeVault[];
|
|
272
|
+
/** Active items in the selected upstream vault; 0 when none is selected yet. */
|
|
273
|
+
item_count: number;
|
|
274
|
+
warnings?: string[];
|
|
275
|
+
};
|
package/src/utils.ts
CHANGED
|
@@ -23,6 +23,18 @@ export type RequestOptions = {
|
|
|
23
23
|
body?: string | Uint8Array | any;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
+
const KEY_IN_URL = /(?<=[?&]key=)[^&\s)'"]+/g;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Mask the API key in a fetch error, in place: Deno prints the request URL,
|
|
30
|
+
* query and key included, in the message and in the stack it captured.
|
|
31
|
+
*/
|
|
32
|
+
function maskKey(error: unknown): void {
|
|
33
|
+
if (!(error instanceof Error)) return;
|
|
34
|
+
error.message = error.message.replace(KEY_IN_URL, 'REDACTED');
|
|
35
|
+
if (error.stack !== undefined) error.stack = error.stack.replace(KEY_IN_URL, 'REDACTED');
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
export async function fetchRetry(
|
|
27
39
|
config: RequestOptions,
|
|
28
40
|
retries: number = 3,
|
|
@@ -46,8 +58,9 @@ export async function fetchRetry(
|
|
|
46
58
|
body: reqInit.body as any,
|
|
47
59
|
};
|
|
48
60
|
const response = await fetch(new Request(url, init));
|
|
49
|
-
//
|
|
50
|
-
|
|
61
|
+
// Retry 5xx status codes. A single-attempt caller reads the 5xx body
|
|
62
|
+
// itself: it carries the typed error.
|
|
63
|
+
if (response.status >= 500 && response.status < 600 && retries > 1) {
|
|
51
64
|
const _text = await response.text(); // consume response to prevent leak
|
|
52
65
|
lastError = new Error(`Fetch failed with status: ${response.status}`);
|
|
53
66
|
if (attempt < retries) {
|
|
@@ -58,6 +71,7 @@ export async function fetchRetry(
|
|
|
58
71
|
return response;
|
|
59
72
|
}
|
|
60
73
|
} catch (error) {
|
|
74
|
+
maskKey(error);
|
|
61
75
|
lastError = error;
|
|
62
76
|
|
|
63
77
|
// AbortError should never be retried: it means the caller explicitly
|