From 6f5af92769f69ac770976653a26b8e64e9550a03 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 6 Nov 2026 22:25:07 -0700 Subject: [PATCH 1/2] Add failing tests for an unreadable body and Cubot phones Co-Authored-By: Claude Opus 5.5 --- tests/trace-client.test.ts | 40 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/trace-client.test.ts b/tests/trace-client.test.ts index a569a0a..3d2d946 100644 --- a/tests/trace-client.test.ts +++ b/tests/trace-client.test.ts @@ -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") }), @@ -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", () => { From 4ca43995703ae326bcd7c98864f5e3f3bad15ac6 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 6 Nov 2026 22:26:05 -0700 Subject: [PATCH 2/2] Stop calling delivered reports dropped, and Cubot phones bots Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 7 +++++++ trace-client.ts | 8 +++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d83dc09..a70e03f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/trace-client.ts b/trace-client.ts index f6cfce6..946bf71 100644 --- a/trace-client.ts +++ b/trace-client.ts @@ -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); } @@ -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; + /(?