scrapfly-sdk 0.11.1 → 0.12.1
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 +72 -5
- package/esm/browserconfig.d.ts +57 -9
- package/esm/browserconfig.d.ts.map +1 -1
- package/esm/browserconfig.js +63 -5
- 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 +224 -8
- package/esm/client.d.ts.map +1 -1
- package/esm/client.js +646 -62
- package/esm/crawl.d.ts +83 -1
- package/esm/crawl.d.ts.map +1 -1
- package/esm/crawl.js +73 -0
- package/esm/crawlerconfig.d.ts +54 -3
- package/esm/crawlerconfig.d.ts.map +1 -1
- package/esm/crawlerconfig.js +96 -2
- package/esm/crawlerresult.d.ts +374 -2
- package/esm/crawlerresult.d.ts.map +1 -1
- package/esm/crawlerresult.js +367 -1
- package/esm/errors.d.ts +12 -0
- package/esm/errors.d.ts.map +1 -1
- package/esm/errors.js +11 -0
- package/esm/main.d.ts +12 -7
- package/esm/main.d.ts.map +1 -1
- package/esm/main.js +10 -5
- 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 +65 -1
- package/esm/scrapeconfig.d.ts.map +1 -1
- package/esm/scrapeconfig.js +88 -4
- package/esm/screenshotconfig.d.ts +9 -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/esm/webhookserver.d.ts +33 -15
- package/esm/webhookserver.d.ts.map +1 -1
- package/esm/webhookserver.js +197 -38
- package/package.json +1 -1
- package/script/browserconfig.d.ts +57 -9
- package/script/browserconfig.d.ts.map +1 -1
- package/script/browserconfig.js +63 -5
- 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 +224 -8
- package/script/client.d.ts.map +1 -1
- package/script/client.js +645 -61
- package/script/crawl.d.ts +83 -1
- package/script/crawl.d.ts.map +1 -1
- package/script/crawl.js +73 -0
- package/script/crawlerconfig.d.ts +54 -3
- package/script/crawlerconfig.d.ts.map +1 -1
- package/script/crawlerconfig.js +97 -3
- package/script/crawlerresult.d.ts +374 -2
- package/script/crawlerresult.d.ts.map +1 -1
- package/script/crawlerresult.js +374 -2
- package/script/errors.d.ts +12 -0
- package/script/errors.d.ts.map +1 -1
- package/script/errors.js +12 -1
- package/script/main.d.ts +12 -7
- package/script/main.d.ts.map +1 -1
- package/script/main.js +20 -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 +65 -1
- package/script/scrapeconfig.d.ts.map +1 -1
- package/script/scrapeconfig.js +88 -4
- package/script/screenshotconfig.d.ts +9 -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/script/webhookserver.d.ts +33 -15
- package/script/webhookserver.d.ts.map +1 -1
- package/script/webhookserver.js +197 -38
- package/src/browserconfig.ts +74 -8
- package/src/browserdownloads.ts +266 -0
- package/src/client.ts +796 -65
- package/src/crawl.ts +106 -0
- package/src/crawlerconfig.ts +128 -5
- package/src/crawlerresult.ts +607 -3
- package/src/errors.ts +12 -0
- package/src/main.ts +55 -5
- package/src/monitoringconfig.ts +2 -2
- package/src/result.ts +17 -5
- package/src/scrapeconfig.ts +92 -3
- package/src/screenshotconfig.ts +14 -1
- package/src/types.ts +159 -1
- package/src/utils.ts +16 -2
- package/src/webhookserver.ts +235 -39
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieve the files a Cloud Browser session downloaded.
|
|
3
|
+
*
|
|
4
|
+
* Connect with `new BrowserConfig({ downloads_stream: true })`: `ScrapiumBrowser.getDownloads` then returns
|
|
5
|
+
* each small file inline and each large one as an IO stream handle, which these helpers read in chunks with
|
|
6
|
+
* `IO.read`, so no CDP message is larger than one chunk. Without the option every file comes back inline,
|
|
7
|
+
* and the helpers read that shape too.
|
|
8
|
+
*
|
|
9
|
+
* A session is anything with a `send(method, params)` method answering the CDP result: Puppeteer's and
|
|
10
|
+
* Playwright's `CDPSession`, a chrome-remote-interface client, or such a function.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const downloads = new CloudBrowserDownloads(await page.createCDPSession());
|
|
14
|
+
* await downloads.waitFor('report.pdf');
|
|
15
|
+
* const paths = await downloads.saveAll('downloads');
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import { mkdir, open, stat } from 'node:fs/promises';
|
|
19
|
+
import { basename, join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
/** Base64 characters per `IO.read`: an 8 MiB message carrying 6 MiB of the file. */
|
|
22
|
+
export const DEFAULT_CHUNK_SIZE = 8 * 1024 * 1024;
|
|
23
|
+
/** The largest chunk the Cloud Browser serves per `IO.read`. */
|
|
24
|
+
export const MAX_CHUNK_SIZE = 32 * 1024 * 1024;
|
|
25
|
+
|
|
26
|
+
const DEFAULT_WAIT_TIMEOUT_MS = 60_000;
|
|
27
|
+
const DEFAULT_WAIT_INTERVAL_MS = 500;
|
|
28
|
+
|
|
29
|
+
/** A CDP session: Puppeteer's or Playwright's `CDPSession`, or a chrome-remote-interface client. */
|
|
30
|
+
export interface CdpSession {
|
|
31
|
+
// deno-lint-ignore no-explicit-any
|
|
32
|
+
send(method: any, params?: any): Promise<any>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A function sending one CDP command and resolving to its result. */
|
|
36
|
+
// deno-lint-ignore no-explicit-any
|
|
37
|
+
export type CdpSend = (method: string, params?: Record<string, unknown>) => Promise<any>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One file of the session's download directory. A small file carries its base64 content in `data`. A large
|
|
41
|
+
* one, with `downloads_stream`, carries the IO handle to read in `stream` instead, and its size in bytes.
|
|
42
|
+
*/
|
|
43
|
+
export interface BrowserDownload {
|
|
44
|
+
name: string;
|
|
45
|
+
guid?: string;
|
|
46
|
+
size?: number;
|
|
47
|
+
data?: string;
|
|
48
|
+
stream?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A download could not be read whole. */
|
|
52
|
+
export class BrowserDownloadError extends Error {
|
|
53
|
+
constructor(message: string) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.name = 'BrowserDownloadError';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface StreamEntry {
|
|
60
|
+
stream: string;
|
|
61
|
+
size?: number;
|
|
62
|
+
name?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface GetDownloadsResult {
|
|
66
|
+
files?: Record<string, string>;
|
|
67
|
+
files_by_guid?: Record<string, string>;
|
|
68
|
+
guid_names?: Record<string, string>;
|
|
69
|
+
streams?: Record<string, StreamEntry>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The downloads of a `ScrapiumBrowser.getDownloads` result, in either of its shapes. */
|
|
73
|
+
export function parseDownloads(result: GetDownloadsResult): BrowserDownload[] {
|
|
74
|
+
const downloads: BrowserDownload[] = [];
|
|
75
|
+
if (result.guid_names !== undefined || result.streams !== undefined) {
|
|
76
|
+
const guidNames = result.guid_names ?? {};
|
|
77
|
+
for (const [guid, data] of Object.entries(result.files_by_guid ?? {})) {
|
|
78
|
+
downloads.push({ name: guidNames[guid] || guid, guid, size: decodedSize(data), data });
|
|
79
|
+
}
|
|
80
|
+
for (const [guid, entry] of Object.entries(result.streams ?? {})) {
|
|
81
|
+
downloads.push({ name: entry.name || guidNames[guid] || guid, guid, size: entry.size, stream: entry.stream });
|
|
82
|
+
}
|
|
83
|
+
// A file on disk with no download GUID, such as one finished before this session attached.
|
|
84
|
+
for (const [name, data] of Object.entries(result.files ?? {})) {
|
|
85
|
+
downloads.push({ name, size: decodedSize(data), data });
|
|
86
|
+
}
|
|
87
|
+
return downloads;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Without downloads_stream every file is inline under `files`, and again under `files_by_guid` with no
|
|
91
|
+
// name: `files` alone holds each file once.
|
|
92
|
+
for (const [name, data] of Object.entries(result.files ?? {})) {
|
|
93
|
+
downloads.push({ name, size: decodedSize(data), data });
|
|
94
|
+
}
|
|
95
|
+
return downloads;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function decodedSize(data: string): number {
|
|
99
|
+
let padding = 0;
|
|
100
|
+
if (data.endsWith('==')) {
|
|
101
|
+
padding = 2;
|
|
102
|
+
} else if (data.endsWith('=')) {
|
|
103
|
+
padding = 1;
|
|
104
|
+
}
|
|
105
|
+
return Math.floor(data.length / 4) * 3 - padding;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function decodeBase64(data: string): Uint8Array {
|
|
109
|
+
const binary = atob(data);
|
|
110
|
+
const bytes = new Uint8Array(binary.length);
|
|
111
|
+
for (let i = 0; i < binary.length; i++) {
|
|
112
|
+
bytes[i] = binary.charCodeAt(i);
|
|
113
|
+
}
|
|
114
|
+
return bytes;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function checkChunkSize(chunkSize: number): number {
|
|
118
|
+
if (!Number.isInteger(chunkSize) || chunkSize < 4 || chunkSize > MAX_CHUNK_SIZE) {
|
|
119
|
+
throw new RangeError(`chunkSize must be an integer between 4 and ${MAX_CHUNK_SIZE} base64 characters`);
|
|
120
|
+
}
|
|
121
|
+
return chunkSize - (chunkSize % 4);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function isDirectory(path: string): Promise<boolean> {
|
|
125
|
+
try {
|
|
126
|
+
return (await stat(path)).isDirectory();
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function targetPath(download: BrowserDownload, path?: string): Promise<string> {
|
|
133
|
+
// The name comes from the remote browser: keep its last component only.
|
|
134
|
+
const name = basename(download.name.replaceAll('\\', '/')) || download.guid || 'download';
|
|
135
|
+
if (path === undefined) {
|
|
136
|
+
return name;
|
|
137
|
+
}
|
|
138
|
+
if (await isDirectory(path)) {
|
|
139
|
+
return join(path, name);
|
|
140
|
+
}
|
|
141
|
+
return path;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface CloudBrowserDownloadsOptions {
|
|
145
|
+
/** Base64 characters per `IO.read`, rounded down to a multiple of 4. Defaults to 8 MiB, 32 MiB at most. */
|
|
146
|
+
chunkSize?: number;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface WaitForOptions {
|
|
150
|
+
/** Milliseconds before giving up. Defaults to 60000. */
|
|
151
|
+
timeout?: number;
|
|
152
|
+
/** Milliseconds between two checks. Defaults to 500. */
|
|
153
|
+
interval?: number;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Download helpers over a CDP session of a Cloud Browser. */
|
|
157
|
+
export class CloudBrowserDownloads {
|
|
158
|
+
readonly chunkSize: number;
|
|
159
|
+
private readonly sendCommand: CdpSend;
|
|
160
|
+
|
|
161
|
+
constructor(session: CdpSession | CdpSend, options: CloudBrowserDownloadsOptions = {}) {
|
|
162
|
+
if (typeof session === 'function') {
|
|
163
|
+
this.sendCommand = session;
|
|
164
|
+
} else {
|
|
165
|
+
this.sendCommand = (method, params) => session.send(method, params);
|
|
166
|
+
}
|
|
167
|
+
this.chunkSize = checkChunkSize(options.chunkSize ?? DEFAULT_CHUNK_SIZE);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The size in bytes of every completed download, by filename. */
|
|
171
|
+
async metadata(): Promise<Record<string, number>> {
|
|
172
|
+
const reply = await this.sendCommand('ScrapiumBrowser.getDownloadsMetadatas', {});
|
|
173
|
+
return reply?.metadata ?? {};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Wait until `filename` is listed with the same size twice in a row, and resolve to that size. */
|
|
177
|
+
async waitFor(filename: string, options: WaitForOptions = {}): Promise<number> {
|
|
178
|
+
const timeout = options.timeout ?? DEFAULT_WAIT_TIMEOUT_MS;
|
|
179
|
+
const interval = options.interval ?? DEFAULT_WAIT_INTERVAL_MS;
|
|
180
|
+
const deadline = Date.now() + timeout;
|
|
181
|
+
let last = -1;
|
|
182
|
+
while (Date.now() < deadline) {
|
|
183
|
+
const size = (await this.metadata())[filename] ?? -1;
|
|
184
|
+
if (size >= 0 && size === last) {
|
|
185
|
+
return size;
|
|
186
|
+
}
|
|
187
|
+
last = size;
|
|
188
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
189
|
+
}
|
|
190
|
+
throw new Error(`${filename} did not finish downloading in ${timeout}ms`);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Every download of the session: small ones with their content, large ones as handles. */
|
|
194
|
+
async list(): Promise<BrowserDownload[]> {
|
|
195
|
+
return parseDownloads(await this.sendCommand('ScrapiumBrowser.getDownloads', {}));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** The bytes of a download, one decoded chunk at a time. A stream handle is read once. */
|
|
199
|
+
async *chunks(download: BrowserDownload): AsyncGenerator<Uint8Array> {
|
|
200
|
+
if (download.stream === undefined) {
|
|
201
|
+
yield decodeBase64(download.data ?? '');
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
let written = 0;
|
|
205
|
+
try {
|
|
206
|
+
while (true) {
|
|
207
|
+
const reply = await this.sendCommand('IO.read', { handle: download.stream, size: this.chunkSize });
|
|
208
|
+
const data: string = reply?.data ?? '';
|
|
209
|
+
if (data.length > 0) {
|
|
210
|
+
const chunk = reply.base64Encoded === false ? new TextEncoder().encode(data) : decodeBase64(data);
|
|
211
|
+
written += chunk.length;
|
|
212
|
+
yield chunk;
|
|
213
|
+
}
|
|
214
|
+
if (reply?.eof) {
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
} finally {
|
|
219
|
+
await this.sendCommand('IO.close', { handle: download.stream });
|
|
220
|
+
}
|
|
221
|
+
if (download.size !== undefined && written !== download.size) {
|
|
222
|
+
throw new BrowserDownloadError(`${download.name}: read ${written} bytes, expected ${download.size}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The whole content of a download, in memory. */
|
|
227
|
+
async read(download: BrowserDownload): Promise<Uint8Array> {
|
|
228
|
+
const parts: Uint8Array[] = [];
|
|
229
|
+
let total = 0;
|
|
230
|
+
for await (const chunk of this.chunks(download)) {
|
|
231
|
+
parts.push(chunk);
|
|
232
|
+
total += chunk.length;
|
|
233
|
+
}
|
|
234
|
+
const bytes = new Uint8Array(total);
|
|
235
|
+
let offset = 0;
|
|
236
|
+
for (const part of parts) {
|
|
237
|
+
bytes.set(part, offset);
|
|
238
|
+
offset += part.length;
|
|
239
|
+
}
|
|
240
|
+
return bytes;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Write a download to `path` (a file or a directory, the current one by default) chunk by chunk. */
|
|
244
|
+
async save(download: BrowserDownload, path?: string): Promise<string> {
|
|
245
|
+
const target = await targetPath(download, path);
|
|
246
|
+
const file = await open(target, 'w');
|
|
247
|
+
try {
|
|
248
|
+
for await (const chunk of this.chunks(download)) {
|
|
249
|
+
await file.write(chunk);
|
|
250
|
+
}
|
|
251
|
+
} finally {
|
|
252
|
+
await file.close();
|
|
253
|
+
}
|
|
254
|
+
return target;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** Write every download of the session into `directory` and resolve to their paths. */
|
|
258
|
+
async saveAll(directory = '.'): Promise<string[]> {
|
|
259
|
+
await mkdir(directory, { recursive: true });
|
|
260
|
+
const paths: string[] = [];
|
|
261
|
+
for (const download of await this.list()) {
|
|
262
|
+
paths.push(await this.save(download, directory));
|
|
263
|
+
}
|
|
264
|
+
return paths;
|
|
265
|
+
}
|
|
266
|
+
}
|