extract-webpage 1.2.35 → 1.2.36

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/src/utils/grab.ts CHANGED
@@ -1,51 +1,51 @@
1
- /**
2
- * Fetch wrapper for grabbing binary content
3
- * Replacement for grab-url package using standard fetch API
4
- */
5
- export interface GrabOptions {
6
- responseType?: "text" | "arraybuffer";
7
- /** Timeout in seconds */
8
- timeout?: number;
9
- method?: string;
10
- headers?: Record<string, string>;
11
- body?: string;
12
- }
13
-
14
- export default async function grab(
15
- url: string,
16
- options?: GrabOptions & { responseType?: "text" }
17
- ): Promise<string>;
18
- export default async function grab(
19
- url: string,
20
- options: GrabOptions & { responseType: "arraybuffer" }
21
- ): Promise<ArrayBuffer>;
22
- export default async function grab(
23
- url: string,
24
- options: GrabOptions = {}
25
- ): Promise<string | ArrayBuffer> {
26
- const timeoutMs = options.timeout ? options.timeout * 1000 : 10000;
27
- const controller = new AbortController();
28
- const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
29
-
30
- try {
31
- const response = await fetch(url, {
32
- method: options.method,
33
- headers: options.headers,
34
- body: options.body,
35
- signal: controller.signal,
36
- });
37
- clearTimeout(timeoutId);
38
-
39
- if (!response.ok) {
40
- throw new Error(`HTTP ${response.status}`);
41
- }
42
-
43
- if (options.responseType === "arraybuffer") {
44
- return await response.arrayBuffer();
45
- }
46
- return await response.text();
47
- } catch (error) {
48
- clearTimeout(timeoutId);
49
- throw error;
50
- }
51
- }
1
+ /**
2
+ * Fetch wrapper for grabbing binary content
3
+ * Replacement for grab-url package using standard fetch API
4
+ */
5
+ export interface GrabOptions {
6
+ responseType?: "text" | "arraybuffer";
7
+ /** Timeout in seconds */
8
+ timeout?: number;
9
+ method?: string;
10
+ headers?: Record<string, string>;
11
+ body?: string;
12
+ }
13
+
14
+ export default async function grab(
15
+ url: string,
16
+ options?: GrabOptions & { responseType?: "text" }
17
+ ): Promise<string>;
18
+ export default async function grab(
19
+ url: string,
20
+ options: GrabOptions & { responseType: "arraybuffer" }
21
+ ): Promise<ArrayBuffer>;
22
+ export default async function grab(
23
+ url: string,
24
+ options: GrabOptions = {}
25
+ ): Promise<string | ArrayBuffer> {
26
+ const timeoutMs = options.timeout ? options.timeout * 1000 : 10000;
27
+ const controller = new AbortController();
28
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
29
+
30
+ try {
31
+ const response = await fetch(url, {
32
+ method: options.method,
33
+ headers: options.headers,
34
+ body: options.body,
35
+ signal: controller.signal,
36
+ });
37
+ clearTimeout(timeoutId);
38
+
39
+ if (!response.ok) {
40
+ throw new Error(`HTTP ${response.status}`);
41
+ }
42
+
43
+ if (options.responseType === "arraybuffer") {
44
+ return await response.arrayBuffer();
45
+ }
46
+ return await response.text();
47
+ } catch (error) {
48
+ clearTimeout(timeoutId);
49
+ throw error;
50
+ }
51
+ }