website-api 1.1.9 → 1.1.10

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.
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Normalise a user-supplied proxy value into a URL string suitable for
3
+ * `HTTP_PROXY`/`HTTPS_PROXY` env vars. Mirrors chrome-cdp-manager's
4
+ * `parseProxy` logic so both the browser and HTTP transport see the same proxy.
5
+ *
6
+ * Accepted forms:
7
+ * - `true` / `"default"` → `socks5://127.0.0.1:1080`
8
+ * - `"1080"` → `socks5://127.0.0.1:1080`
9
+ * - `"host:port"` → `socks5://host:port`
10
+ * - full URL → as given
11
+ *
12
+ * Returns `null` on invalid input so callers can skip silently.
13
+ */
14
+ export declare function normalizeProxyUrl(value: string | boolean): string | null;
15
+ /**
16
+ * Configure Node's global `fetch` to route through a proxy. Call once before
17
+ * any HTTP capability requests are made.
18
+ *
19
+ * Uses `http.setGlobalProxyFromEnv()` (Node ≥ 22) which teaches the built-in
20
+ * undici-backed `fetch` to honour `HTTP_PROXY` / `HTTPS_PROXY` env vars.
21
+ *
22
+ * This complements the browser-transport proxy (chrome-cdp-manager passes
23
+ * `--proxy-server` to Chrome) by also routing direct HTTP capability requests.
24
+ *
25
+ * @returns The resolved proxy URL if applied, or `null` if skipped.
26
+ */
27
+ export declare function applyHttpProxy(proxyValue: string | boolean | undefined, debug?: boolean): string | null;
@@ -0,0 +1,71 @@
1
+ import { setGlobalProxyFromEnv } from "node:http";
2
+ // mark: Constants
3
+ const DEFAULT_PROXY_SCHEME = "socks5";
4
+ const DEFAULT_PROXY_HOST = "127.0.0.1";
5
+ const DEFAULT_PROXY_PORT = 1080;
6
+ const DEFAULT_PROXY = `${DEFAULT_PROXY_SCHEME}://${DEFAULT_PROXY_HOST}:${DEFAULT_PROXY_PORT}`;
7
+ // mark: normalizeProxyUrl
8
+ /**
9
+ * Normalise a user-supplied proxy value into a URL string suitable for
10
+ * `HTTP_PROXY`/`HTTPS_PROXY` env vars. Mirrors chrome-cdp-manager's
11
+ * `parseProxy` logic so both the browser and HTTP transport see the same proxy.
12
+ *
13
+ * Accepted forms:
14
+ * - `true` / `"default"` → `socks5://127.0.0.1:1080`
15
+ * - `"1080"` → `socks5://127.0.0.1:1080`
16
+ * - `"host:port"` → `socks5://host:port`
17
+ * - full URL → as given
18
+ *
19
+ * Returns `null` on invalid input so callers can skip silently.
20
+ */
21
+ export function normalizeProxyUrl(value) {
22
+ if (value === true)
23
+ return DEFAULT_PROXY;
24
+ let raw = String(value).trim();
25
+ if (raw === "" || raw.toLowerCase() === "default")
26
+ return DEFAULT_PROXY;
27
+ // Port-only shorthand: "1080"
28
+ if (/^\d+$/.test(raw)) {
29
+ return `${DEFAULT_PROXY_SCHEME}://${DEFAULT_PROXY_HOST}:${raw}`;
30
+ }
31
+ // host:port shorthand without scheme
32
+ if (!raw.includes("://")) {
33
+ raw = `${DEFAULT_PROXY_SCHEME}://${raw}`;
34
+ }
35
+ try {
36
+ const url = new URL(raw);
37
+ const scheme = url.protocol.replace(/:$/, "").toLowerCase();
38
+ const host = url.hostname || DEFAULT_PROXY_HOST;
39
+ const port = url.port ? Number(url.port) : scheme.startsWith("socks") ? DEFAULT_PROXY_PORT : 8080;
40
+ return `${scheme}://${host}:${port}`;
41
+ }
42
+ catch {
43
+ return null;
44
+ }
45
+ }
46
+ // mark: applyHttpProxy
47
+ /**
48
+ * Configure Node's global `fetch` to route through a proxy. Call once before
49
+ * any HTTP capability requests are made.
50
+ *
51
+ * Uses `http.setGlobalProxyFromEnv()` (Node ≥ 22) which teaches the built-in
52
+ * undici-backed `fetch` to honour `HTTP_PROXY` / `HTTPS_PROXY` env vars.
53
+ *
54
+ * This complements the browser-transport proxy (chrome-cdp-manager passes
55
+ * `--proxy-server` to Chrome) by also routing direct HTTP capability requests.
56
+ *
57
+ * @returns The resolved proxy URL if applied, or `null` if skipped.
58
+ */
59
+ export function applyHttpProxy(proxyValue, debug = false) {
60
+ if (!proxyValue)
61
+ return null;
62
+ const proxyUrl = normalizeProxyUrl(proxyValue);
63
+ if (!proxyUrl)
64
+ return null;
65
+ process.env.HTTP_PROXY = proxyUrl;
66
+ process.env.HTTPS_PROXY = proxyUrl;
67
+ setGlobalProxyFromEnv();
68
+ if (debug)
69
+ console.log(`[debug] HTTP proxy set to ${proxyUrl}`);
70
+ return proxyUrl;
71
+ }
@@ -1,9 +1,12 @@
1
+ // mark: Imports
1
2
  import { resolve } from "node:path";
2
3
  import { connectChrome } from "../capabilities/browser.js";
3
4
  import { buildCookieString, resolveCookies, resolveCredentials, resolveUserAgent, } from "../capabilities/cookies.js";
4
5
  import { createSaver } from "../capabilities/download.js";
5
6
  import { applyFingerprint } from "../capabilities/fingerprint.js";
6
7
  import { createHttp } from "../capabilities/http.js";
8
+ import { applyHttpProxy } from "../capabilities/proxy.js";
9
+ // mark: Helpers
7
10
  /** Treats `1`/`true`/`yes`/`on` (case-insensitive) as truthy for env flags. */
8
11
  function isTruthyEnv(value) {
9
12
  if (!value)
@@ -23,6 +26,7 @@ function resolveHeadless(headed, headlessEnv) {
23
26
  return isTruthyEnv(headlessEnv);
24
27
  return true;
25
28
  }
29
+ // mark: createContext
26
30
  /**
27
31
  * Builds the lazy capability context handed to `site.run(ctx)`. Nothing
28
32
  * expensive happens until a capability is touched: cookies are read on first
@@ -57,6 +61,8 @@ export function createContext(site, options = {}, providers = {}) {
57
61
  }
58
62
  return credentialsCache;
59
63
  };
64
+ // ── proxy for HTTP transport ──
65
+ applyHttpProxy(options.proxy ?? env.CDP_PROXY, debug);
60
66
  const http = createHttp({ fetchImpl: providers.fetchImpl, cookieString, userAgent, debug });
61
67
  // Host-provided cheerio: extensions can't resolve their own node_modules, so
62
68
  // they parse HTML through this instead of importing cheerio. Lazily imported
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "website-api",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "description": "CLI and library to query website private APIs with your real logged-in Chrome session",
5
5
  "main": "./dist/src/website-api.js",
6
6
  "types": "./dist/src/website-api.d.ts",