|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +const { firecrawlFetch } = await import("../../../open-sse/executors/firecrawl-fetch.ts"); |
| 5 | + |
| 6 | +// ── #2253 (decolua/9router): self-hosted Firecrawl support ──────────────────── |
| 7 | +// FIRECRAWL_BASE_URL lets operators point the executor at a self-hosted instance. |
| 8 | +// The API key stays required for the default cloud endpoint, but becomes optional |
| 9 | +// once a custom base URL is configured (self-hosted instances usually run with no |
| 10 | +// auth in front of them). |
| 11 | + |
| 12 | +function withEnv(vars: Record<string, string | undefined>, fn: () => Promise<void>) { |
| 13 | + const original: Record<string, string | undefined> = {}; |
| 14 | + for (const key of Object.keys(vars)) { |
| 15 | + original[key] = process.env[key]; |
| 16 | + } |
| 17 | + for (const [key, value] of Object.entries(vars)) { |
| 18 | + if (value === undefined) delete process.env[key]; |
| 19 | + else process.env[key] = value; |
| 20 | + } |
| 21 | + return fn().finally(() => { |
| 22 | + for (const [key, value] of Object.entries(original)) { |
| 23 | + if (value === undefined) delete process.env[key]; |
| 24 | + else process.env[key] = value; |
| 25 | + } |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +test("firecrawlFetch routes to FIRECRAWL_BASE_URL when set", async () => { |
| 30 | + await withEnv({ FIRECRAWL_BASE_URL: "http://127.0.0.1:3002" }, async () => { |
| 31 | + const originalFetch = globalThis.fetch; |
| 32 | + let capturedUrl = ""; |
| 33 | + |
| 34 | + globalThis.fetch = async (url) => { |
| 35 | + capturedUrl = String(url); |
| 36 | + return new Response(JSON.stringify({ data: { markdown: "# Self-hosted" } }), { |
| 37 | + status: 200, |
| 38 | + headers: { "content-type": "application/json" }, |
| 39 | + }); |
| 40 | + }; |
| 41 | + |
| 42 | + try { |
| 43 | + const result = await firecrawlFetch({ |
| 44 | + url: "https://example.com", |
| 45 | + format: "markdown", |
| 46 | + depth: 0, |
| 47 | + includeMetadata: false, |
| 48 | + credentials: { apiKey: "any-key" }, |
| 49 | + }); |
| 50 | + |
| 51 | + assert.equal(result.success, true); |
| 52 | + assert.equal(capturedUrl, "http://127.0.0.1:3002/v1/scrape"); |
| 53 | + } finally { |
| 54 | + globalThis.fetch = originalFetch; |
| 55 | + } |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +test("firecrawlFetch allows missing apiKey when FIRECRAWL_BASE_URL is custom", async () => { |
| 60 | + await withEnv({ FIRECRAWL_BASE_URL: "http://127.0.0.1:3002" }, async () => { |
| 61 | + const originalFetch = globalThis.fetch; |
| 62 | + let capturedHeaders: Record<string, string> = {}; |
| 63 | + |
| 64 | + globalThis.fetch = async (_url, init = {}) => { |
| 65 | + capturedHeaders = (init as RequestInit).headers as Record<string, string>; |
| 66 | + return new Response(JSON.stringify({ data: { markdown: "# Self-hosted" } }), { |
| 67 | + status: 200, |
| 68 | + headers: { "content-type": "application/json" }, |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + try { |
| 73 | + const result = await firecrawlFetch({ |
| 74 | + url: "https://example.com", |
| 75 | + format: "markdown", |
| 76 | + depth: 0, |
| 77 | + includeMetadata: false, |
| 78 | + credentials: {}, |
| 79 | + }); |
| 80 | + |
| 81 | + assert.equal(result.success, true, "custom base URL must not require an API key"); |
| 82 | + assert.equal( |
| 83 | + capturedHeaders["Authorization"], |
| 84 | + undefined, |
| 85 | + "no Authorization header should be sent without an apiKey" |
| 86 | + ); |
| 87 | + } finally { |
| 88 | + globalThis.fetch = originalFetch; |
| 89 | + } |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +test("firecrawlFetch still requires apiKey against the default cloud base URL", async () => { |
| 94 | + await withEnv({ FIRECRAWL_BASE_URL: undefined }, async () => { |
| 95 | + const result = await firecrawlFetch({ |
| 96 | + url: "https://example.com", |
| 97 | + format: "markdown", |
| 98 | + depth: 0, |
| 99 | + includeMetadata: false, |
| 100 | + credentials: {}, |
| 101 | + }); |
| 102 | + |
| 103 | + assert.equal(result.success, false); |
| 104 | + assert.equal(result.status, 401); |
| 105 | + assert.ok(!result.error?.includes("at /"), "error must not contain stack trace"); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +test("firecrawlFetch honors FIRECRAWL_TIMEOUT_MS override", async () => { |
| 110 | + await withEnv( |
| 111 | + { FIRECRAWL_BASE_URL: "http://127.0.0.1:3002", FIRECRAWL_TIMEOUT_MS: "5" }, |
| 112 | + async () => { |
| 113 | + const originalFetch = globalThis.fetch; |
| 114 | + |
| 115 | + globalThis.fetch = async (_url, init = {}) => { |
| 116 | + const signal = (init as RequestInit).signal; |
| 117 | + return new Promise((resolve, reject) => { |
| 118 | + const timer = setTimeout( |
| 119 | + () => |
| 120 | + resolve( |
| 121 | + new Response(JSON.stringify({ data: { markdown: "late" } }), { |
| 122 | + status: 200, |
| 123 | + headers: { "content-type": "application/json" }, |
| 124 | + }) |
| 125 | + ), |
| 126 | + 50 |
| 127 | + ); |
| 128 | + signal?.addEventListener("abort", () => { |
| 129 | + clearTimeout(timer); |
| 130 | + reject(new DOMException("Aborted", "AbortError")); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }; |
| 134 | + |
| 135 | + try { |
| 136 | + const result = await firecrawlFetch({ |
| 137 | + url: "https://example.com", |
| 138 | + format: "markdown", |
| 139 | + depth: 0, |
| 140 | + includeMetadata: false, |
| 141 | + credentials: {}, |
| 142 | + }); |
| 143 | + |
| 144 | + assert.equal(result.success, false); |
| 145 | + assert.equal(result.status, 504); |
| 146 | + } finally { |
| 147 | + globalThis.fetch = originalFetch; |
| 148 | + } |
| 149 | + } |
| 150 | + ); |
| 151 | +}); |
0 commit comments