@grabbit-labs/dynafetch 0.1.3 → 0.2.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.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,192 @@
1
+ export type DynafetchThirdPartyPolicy = "skip-noncritical" | "execute-all";
2
+
3
+ export type DynafetchFramework =
4
+ | "nextjs"
5
+ | "nuxt"
6
+ | "remix"
7
+ | "inertia"
8
+ | "astro"
9
+ | "sveltekit"
10
+ | "htmx"
11
+ | "generic-spa"
12
+ | "static";
13
+
14
+ export type DynafetchStrategy =
15
+ | "static-html"
16
+ | "framework-probe"
17
+ | "jsdom-fallback";
18
+
19
+ export type DynafetchProxyScope = "page" | "api" | "assets";
20
+
21
+ export type DynafetchProxyConfig = string | {
22
+ url: string;
23
+ only?: DynafetchProxyScope[];
24
+ };
25
+
26
+ export type DynafetchOptions = {
27
+ /** Target URL to fetch and render. */
28
+ url: string;
29
+
30
+ /**
31
+ * Extra request headers merged on top of the default Chrome 146 headers.
32
+ * Any header you set here overrides the built-in default for that key.
33
+ * Headers you don't set keep their Chrome-like defaults (User-Agent,
34
+ * Accept, Sec-Fetch-*, etc.).
35
+ */
36
+ headers?: Record<string, string>;
37
+
38
+ /**
39
+ * Cookies to include. Accepts:
40
+ * - `string` — raw `Cookie` header value (`"k1=v1; k2=v2"`)
41
+ * - `string[]` — individual `name=value` pairs
42
+ * - `Record<string, string>` — key/value map
43
+ */
44
+ cookies?: Record<string, string> | string | string[];
45
+
46
+ /**
47
+ * Proxy configuration. Routes requests through an HTTP/HTTPS proxy with
48
+ * TLS fingerprint preservation.
49
+ *
50
+ * ```ts
51
+ * // Proxy all requests
52
+ * proxy: "http://user:pass@ip:port"
53
+ *
54
+ * // Only proxy the page fetch and API calls (skip static assets)
55
+ * proxy: { url: "http://user:pass@ip:port", only: ["page", "api"] }
56
+ * ```
57
+ */
58
+ proxy?: DynafetchProxyConfig;
59
+
60
+ /** Overall timeout for the entire operation in milliseconds. */
61
+ timeoutMs?: number;
62
+
63
+ /** TLS client profile to impersonate. @default `"chrome_146"` */
64
+ browserProfile?: string;
65
+
66
+ /** Advisory limit on sub-requests the executor may issue. */
67
+ maxSubrequests?: number;
68
+
69
+ /** Allow JSDOM-based script execution for dynamic pages. @default `true` */
70
+ allowJsdomFallback?: boolean;
71
+
72
+ /** Pre-fetch external `<script src>` tags during harvest. @default `true` */
73
+ prefetchExternalScripts?: boolean;
74
+
75
+ /** Pre-fetch `<link rel="modulepreload">` assets during harvest. @default `true` */
76
+ prefetchModulePreloads?: boolean;
77
+
78
+ /**
79
+ * Controls whether non-critical third-party scripts (analytics, ads, chat
80
+ * widgets) are executed.
81
+ * @default `"skip-noncritical"`
82
+ */
83
+ thirdPartyPolicy?: DynafetchThirdPartyPolicy;
84
+
85
+ /**
86
+ * Minimum time (ms) to wait before checking if the page is idle.
87
+ * @default 75
88
+ */
89
+ minWaitMs?: number;
90
+
91
+ /**
92
+ * How long (ms) the page must be completely idle (zero pending requests)
93
+ * before we consider it settled.
94
+ * @default 100
95
+ */
96
+ idleWaitMs?: number;
97
+
98
+ /**
99
+ * Hard upper bound (ms) on how long to wait for the page to settle.
100
+ * @default 2000
101
+ */
102
+ maxWaitMs?: number;
103
+
104
+ /**
105
+ * Maximum time (ms) to wait for ES module bundling (esbuild) to complete.
106
+ * @default 6000
107
+ */
108
+ moduleWaitMs?: number;
109
+ };
110
+
111
+ export type DynafetchResult = {
112
+ url: string;
113
+ finalUrl: string;
114
+ status: number;
115
+ html: string;
116
+ framework: DynafetchFramework;
117
+ strategy: DynafetchStrategy;
118
+ confidence: number;
119
+ warnings: string[];
120
+ timings: {
121
+ total: number;
122
+ harvest: number;
123
+ execute: number;
124
+ quiescence: number;
125
+ scriptsTransformed: number;
126
+ };
127
+ requestCount: number;
128
+ };
129
+
130
+ export type DynafetchPlan = {
131
+ framework: DynafetchFramework;
132
+ strategy: DynafetchStrategy;
133
+ reason: string;
134
+ };
135
+
136
+ export declare class DynafetchInputError extends Error {
137
+ status: number;
138
+ constructor(message: string, status?: number);
139
+ }
140
+
141
+ /**
142
+ * Fetch a URL with full browser emulation — Chrome TLS fingerprinting,
143
+ * JavaScript execution, and network interception — then return the
144
+ * rendered HTML and metadata.
145
+ *
146
+ * Uses Chrome 146 headers by default. Any headers you provide are merged
147
+ * on top — your values override the defaults, everything else stays.
148
+ *
149
+ * @example
150
+ * ```ts
151
+ * // Minimal
152
+ * const page = await dynafetch("https://example.com");
153
+ *
154
+ * // With custom headers, cookies, and proxy
155
+ * const page = await dynafetch({
156
+ * url: "https://example.com",
157
+ * headers: { "Accept-Language": "fr-FR" },
158
+ * cookies: { session: "abc123" },
159
+ * proxy: "http://user:pass@ip:port",
160
+ * });
161
+ *
162
+ * // Smart proxy — only proxy the HTML page and API calls
163
+ * const page = await dynafetch({
164
+ * url: "https://example.com",
165
+ * proxy: { url: "http://user:pass@ip:port", only: ["page", "api"] },
166
+ * });
167
+ * ```
168
+ *
169
+ * **Options at a glance:**
170
+ *
171
+ * | Option | Default | Description |
172
+ * |---|---|---|
173
+ * | `headers` | Chrome 146 | Merged on top of defaults — yours override on conflict |
174
+ * | `cookies` | none | String, array, or `Record<string, string>` |
175
+ * | `proxy` | none | `"http://user:pass@ip:port"` or `{ url, only: ["page","api","assets"] }` |
176
+ * | `minWaitMs` | `75` | Min ms before checking if page is idle |
177
+ * | `idleWaitMs` | `100` | Ms of zero pending requests = settled |
178
+ * | `maxWaitMs` | `2000` | Hard cap on wait time regardless of activity |
179
+ * | `moduleWaitMs` | `6000` | Max ms for ES module bundling (esbuild) |
180
+ * | `timeoutMs` | none | Overall timeout for the entire operation |
181
+ * | `thirdPartyPolicy` | `"skip-noncritical"` | Skip analytics/ads/chat scripts |
182
+ *
183
+ * **Proxy scopes** — when using `{ url, only }`:
184
+ * - `"page"` — initial HTML document fetch
185
+ * - `"api"` — fetch() and XHR calls from page scripts
186
+ * - `"assets"` — JS scripts, ES modules, static resources
187
+ *
188
+ * **Speed presets:**
189
+ * - Fast: `{ maxWaitMs: 1000, idleWaitMs: 50 }`
190
+ * - Thorough: `{ maxWaitMs: 5000, idleWaitMs: 200 }`
191
+ */
192
+ export declare function dynafetch(input: string | DynafetchOptions): Promise<DynafetchResult>;