Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

fix(content): keep a webhook's 2xx as sent when the rest of its answer doesn't arrive in time (#1668)

Cihan2fdd12f882d6
diff --git a/docs/api-reference/webhooks.mdx b/docs/api-reference/webhooks.mdx
index 868d0c191..2c018a592 100644
--- a/docs/api-reference/webhooks.mdx
+++ b/docs/api-reference/webhooks.mdx
@@ -293,7 +293,7 @@ The test uses the URL and secret currently in the form, so you can check an endp
- Each event is sent **once**. There are no automatic retries.
- Every request carries `User-Agent: Ansvisor-Webhook/1.0 (+https://ansvisor.com)`, so a firewall or WAF in front of your endpoint can let it through.
- Ansvisor waits up to 15 seconds for a response (10 seconds for `webhook.test`).
-- Respond with a 2xx status quickly and do slow work after responding. Once the status has arrived, it counts even if the connection drops before the rest of the answer.
+- Respond with a 2xx status quickly and do slow work after responding. Once the status has arrived, it counts, even if the rest of the answer is cut off or doesn't arrive in time.
- Redirects aren't followed: a 3xx is the answer, and nothing reaches the address it points to. Use that address; the **Test** button reports a 3xx as a failure and names where it redirects to.
- When the **Test** button can't reach the webhook, it adds the network error, such as `ENOTFOUND` (the host name doesn't resolve), `ECONNREFUSED` (nothing listens on that port) or a TLS certificate error.
- Only admins and managers can test a brand's webhook, up to 10 tests a minute each.
diff --git a/server/src/lib/safe-post.js b/server/src/lib/safe-post.js
index 284d300ef..7e42f0e71 100644
--- a/server/src/lib/safe-post.js
+++ b/server/src/lib/safe-post.js
@@ -81,6 +81,8 @@ export async function postJson(address, body, opts = {}) {
const lookup = opts.lookup ?? (cloud ? publicOnlyLookup : undefined);
const signal = AbortSignal.timeout(timeoutMs);
return new Promise((resolve, reject) => {
+ // Set once the status arrives: the answer's end, with what was read.
+ let answered = null;
const client = url.protocol === 'https:' ? https : http;
const req = client.request(
url,
@@ -122,9 +124,13 @@ export async function postJson(address, body, opts = {}) {
// The status has arrived: an answer cut off after it is still that
// answer, with what was read of it.
res.on('error', done);
+ answered = done;
},
);
req.on('error', (err) => {
+ // The time limit, or the connection dropping, after the status
+ // arrived: the request hears of it first, and the answer still holds.
+ if (answered) return answered();
if (err instanceof PageFetchError) return reject(err);
if (signal.aborted) {
return reject(new PageFetchError('timeout', 'The webhook took too long to answer'));
diff --git a/server/src/lib/safe-post.test.js b/server/src/lib/safe-post.test.js
index 11dc84e5f..c87cb99c1 100644
--- a/server/src/lib/safe-post.test.js
+++ b/server/src/lib/safe-post.test.js
@@ -97,6 +97,12 @@ describe('postJson', () => {
res.end('x'.repeat(MAX_ANSWER_BYTES * 3));
},
'/slow': (_req, res) => setTimeout(() => res.end('late'), 300),
+ // The status and the start of the answer, then nothing until the time limit.
+ '/stalled': (_req, res) => {
+ res.writeHead(200, { 'content-length': '100' });
+ res.write('accepted');
+ setTimeout(() => res.end(), 300);
+ },
};
beforeAll(async () => {
@@ -166,6 +172,13 @@ describe('postJson', () => {
await expect(post('/slow', { timeoutMs: 50 })).rejects.toMatchObject({ code: 'timeout' });
});
+ it('keeps a status that arrived before the time limit, with what was read (#1605)', async () => {
+ await expect(post('/stalled', { timeoutMs: 100 })).resolves.toEqual({
+ status: 200,
+ answer: 'accepted',
+ });
+ });
+
afterEach(() => vi.unstubAllEnvs());
it('never reaches a name that resolves to a private address on Cloud', async () => {