Skip to content

Commit 5733b82

Browse files
authored
fix(scrapeURL/fire-engine): default to separate US-generic proxy list if no location is specified (FIR-728) (#1104)
* feat(location/country): default to us-generic * add tests + fix mock
1 parent 5c1b675 commit 5733b82

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

apps/api/src/__tests__/snips/mocks/mocking-works-properly.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"time": 1735911273239,
44
"options": {
5-
"url": "http://default-fire-engine-api-service:8080/scrape",
5+
"url": "<fire-engine>/scrape",
66
"method": "POST",
77
"body": {
88
"url": "http://firecrawl.dev",
@@ -27,7 +27,7 @@
2727
{
2828
"time": 1735911273354,
2929
"options": {
30-
"url": "http://default-fire-engine-api-service:8080/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
30+
"url": "<fire-engine>/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
3131
"method": "GET",
3232
"headers": {},
3333
"ignoreResponse": false,
@@ -43,7 +43,7 @@
4343
{
4444
"time": 1735911273720,
4545
"options": {
46-
"url": "http://default-fire-engine-api-service:8080/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
46+
"url": "<fire-engine>/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
4747
"method": "GET",
4848
"headers": {},
4949
"ignoreResponse": false,
@@ -59,7 +59,7 @@
5959
{
6060
"time": 1735911274092,
6161
"options": {
62-
"url": "http://default-fire-engine-api-service:8080/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
62+
"url": "<fire-engine>/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
6363
"method": "GET",
6464
"headers": {},
6565
"ignoreResponse": false,
@@ -75,7 +75,7 @@
7575
{
7676
"time": 1735911274467,
7777
"options": {
78-
"url": "http://default-fire-engine-api-service:8080/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
78+
"url": "<fire-engine>/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
7979
"method": "GET",
8080
"headers": {},
8181
"ignoreResponse": false,
@@ -91,7 +91,7 @@
9191
{
9292
"time": 1735911274947,
9393
"options": {
94-
"url": "http://default-fire-engine-api-service:8080/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
94+
"url": "<fire-engine>/scrape/ede37286-90db-4f60-8efb-76217dfcfa35!chrome-cdp",
9595
"method": "GET",
9696
"headers": {},
9797
"ignoreResponse": false,

apps/api/src/__tests__/snips/scrape.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,24 @@ describe("Scrape tests", () => {
3535
"this is fake data coming from the mocking system!",
3636
);
3737
});
38+
39+
describe("Location API", () => {
40+
it.concurrent("works without specifying an explicit location", async () => {
41+
const response = await scrape({
42+
url: "https://iplocation.com",
43+
});
44+
45+
expectScrapeToSucceed(response);
46+
});
47+
48+
it.concurrent("works with country US", async () => {
49+
const response = await scrape({
50+
url: "https://iplocation.com",
51+
location: { country: "US" },
52+
});
53+
54+
expectScrapeToSucceed(response);
55+
expect(response.body.data.markdown).toContain("| Country | United States |");
56+
});
57+
})
3858
});

apps/api/src/controllers/v1/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ export const scrapeOptions = z
154154
.string()
155155
.optional()
156156
.refine(
157-
(val) => !val || Object.keys(countries).includes(val.toUpperCase()),
157+
(val) => !val || Object.keys(countries).includes(val.toUpperCase()) || val === "US-generic",
158158
{
159159
message:
160160
"Invalid country code. Please use a valid ISO 3166-1 alpha-2 country code.",
161161
},
162162
)
163-
.transform((val) => (val ? val.toUpperCase() : "US")),
163+
.transform((val) => (val ? val.toUpperCase() : "US-generic")),
164164
languages: z.string().array().optional(),
165165
})
166166
.optional(),
@@ -178,7 +178,7 @@ export const scrapeOptions = z
178178
"Invalid country code. Please use a valid ISO 3166-1 alpha-2 country code.",
179179
},
180180
)
181-
.transform((val) => (val ? val.toUpperCase() : "US")),
181+
.transform((val) => (val ? val.toUpperCase() : "US-generic")),
182182
languages: z.string().array().optional(),
183183
})
184184
.optional(),

apps/api/src/scraper/scrapeURL/lib/fetch.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,14 @@ export async function robustFetch<
126126
const makeRequestTypeId = (
127127
request: (typeof mock)["requests"][number]["options"],
128128
) => {
129-
let out = request.url + ";" + request.method;
129+
let trueUrl = (process.env.FIRE_ENGINE_BETA_URL && request.url.startsWith(process.env.FIRE_ENGINE_BETA_URL))
130+
? request.url.replace(process.env.FIRE_ENGINE_BETA_URL, "<fire-engine>")
131+
: request.url;
132+
133+
let out = trueUrl + ";" + request.method;
130134
if (
131135
process.env.FIRE_ENGINE_BETA_URL &&
132-
url.startsWith(process.env.FIRE_ENGINE_BETA_URL) &&
136+
(trueUrl.startsWith("<fire-engine>")) &&
133137
request.method === "POST"
134138
) {
135139
out += "f-e;" + request.body?.engine + ";" + request.body?.url;

apps/api/src/scraper/scrapeURL/lib/mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path";
33
import { logger as _logger } from "../../../lib/logger";
44
import { Logger } from "winston";
55
const saveMocksDirPath = path.join(__dirname, "../mocks/").replace("dist/", "");
6-
const loadMocksDirPath = path.join(__dirname, "../../../__tests__/snips/mocks");
6+
const loadMocksDirPath = path.join(__dirname, "../../../__tests__/snips/mocks").replace("dist/", "");
77

88
export async function saveMock(options: unknown, result: unknown) {
99
if (process.env.FIRECRAWL_SAVE_MOCKS !== "true") return;

0 commit comments

Comments
 (0)