Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ against this file to see what a re-vendor would bring.

## [Unreleased]

- A response whose body cannot be read no longer counts against the report,
however the read fails. A custom `fetch` whose response had no
`arrayBuffer`, or one that threw at once, used to log a delivered report
as `could not deliver`, and gave a non-`2xx` a second debug line.
- `isBot()` no longer treats Cubot phones as bots. Model names such as
`CUBOT_X19` and `CUBOT NOTE 20 PRO` matched its generic `bot` patterns,
so page views from those phones went uncounted.
- A request is now given up at `timeoutMs` whatever the `fetch` does. A
custom `fetch` that ignored its abort signal used to leave the `report()`
promise unsettled and keep its in-flight slot for good, so 256 of them
Expand Down
40 changes: 40 additions & 0 deletions tests/trace-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,43 @@ describe("TraceClient", () => {
assert.deepEqual(log, [], "a 2xx is delivered; an unreadable body is not a failure");
});

it("does not call a report dropped when reading the body throws, or cannot be done at all", async () => {
const responses = [
{ ok: true, status: 200 },
{
ok: true,
status: 200,
arrayBuffer(): never {
throw new Error("body gone");
},
},
];
for (const response of responses) {
const client = new TraceClient(baseUrl, "MyGame", {
version: "1.2.3",
key: "k",
debug,
fetch: (async () => response) as unknown as typeof fetch,
});
await client.report("startup"); // must resolve
await client.close();
}
assert.deepEqual(log, [], "the server has answered, so a body that cannot be read is no failure");
});

it("gives a rejected report one debug line when its body cannot be read either", async () => {
const client = new TraceClient(baseUrl, "MyGame", {
version: "1.2.3",
key: "k",
debug,
fetch: (async () => ({ ok: false, status: 500 })) as unknown as typeof fetch,
});
await client.report("startup"); // must resolve
await client.close();
assert.equal(log.length, 1, log.join("\n"));
assert.match(log[0], /answered 500/);
});

it("names the cause of a failed delivery in the debug line", async () => {
const failures: unknown[] = [
new Error("fetch failed", { cause: new Error("connect ECONNREFUSED") }),
Expand Down Expand Up @@ -1408,6 +1445,9 @@ describe("isBot", () => {
"Mozilla/5.0 (Android 14; Mobile; rv:125.0) Gecko/125.0 Firefox/125.0",
"Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/124.0.6367.111 Mobile/15E148 Safari/604.1",
"Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1",
// Cubot is a phone brand, so its model names end in or run on from "bot"
"Mozilla/5.0 (Linux; Android 9; CUBOT_X19) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 11; CUBOT NOTE 20 PRO) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36",
];

it("recognises crawlers, monitors and scripts", () => {
Expand Down
8 changes: 5 additions & 3 deletions trace-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,10 @@ export class TraceClient {
if (!response.ok) {
this.log(`trace server answered ${response.status} for ${body}`);
}
// Let the connection be reused; never fail on an unreadable body.
await Promise.race([response.arrayBuffer(), givenUp]).catch(() => undefined);
// Let the connection be reused; never fail on an unreadable body. Called
// from an async function, so a read that throws is caught here as well.
const drained = (async () => response.arrayBuffer())();
await Promise.race([drained, givenUp]).catch(() => undefined);
} finally {
clearTimeout(timer);
}
Expand Down Expand Up @@ -489,7 +491,7 @@ export function pagePath(urlOrPath: string): string {
}

const BOT_PATTERN =
/bot\b|bot[\/_-]|crawler|crawling|spider|slurp|curl\/|wget\/|python-requests|python-urllib|go-http-client|java\/|okhttp|libwww|httpclient|headlesschrome|lighthouse|pagespeed|uptimerobot|pingdom|statuscake|site24x7|monitor|facebookexternalhit|discordbot|twitterbot|telegrambot|whatsapp|slackbot|linkedinbot|embedly|quora link preview|bitlybot|skypeuripreview|nuzzel|vkshare|w3c_validator|redditbot|applebot|yandex|baiduspider|duckduckbot|bingpreview|semrush|ahrefs|mj12bot|dotbot|petalbot|bytespider|gptbot|chatgpt-user|claudebot|anthropic-ai|ccbot|perplexitybot|scrapy|axios\/|node-fetch|undici|postmanruntime|insomnia|httpie|phantomjs|selenium|puppeteer|playwright/i;
/(?<!cu)bot\b|(?<!cu)bot[\/_-]|crawler|crawling|spider|slurp|curl\/|wget\/|python-requests|python-urllib|go-http-client|java\/|okhttp|libwww|httpclient|headlesschrome|lighthouse|pagespeed|uptimerobot|pingdom|statuscake|site24x7|monitor|facebookexternalhit|discordbot|twitterbot|telegrambot|whatsapp|slackbot|linkedinbot|embedly|quora link preview|bitlybot|skypeuripreview|nuzzel|vkshare|w3c_validator|redditbot|applebot|yandex|baiduspider|duckduckbot|bingpreview|semrush|ahrefs|mj12bot|dotbot|petalbot|bytespider|gptbot|chatgpt-user|claudebot|anthropic-ai|ccbot|perplexitybot|scrapy|axios\/|node-fetch|undici|postmanruntime|insomnia|httpie|phantomjs|selenium|puppeteer|playwright/i;

/**
* Whether a User-Agent header belongs to a crawler, a monitor, or a script
Expand Down
Loading