From 5bb18da6bf214670a4fe7c390d3aef6511a94fce Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Thu, 19 Mar 2026 18:21:43 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20address=20PR=20#305=20review=20comments?= =?UTF-8?q?=20=E2=80=94=20Origin=20header,=20lowServerCount=20threshold,?= =?UTF-8?q?=20keepalive=20guard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - history.ts: add Origin: DEFAULT_ORIGIN to CDN DELETE request headers — the download path injects it internally but options does not carry it - socket.ts: fix lowServerCount comparison — was preKeyCount <= topUpAmount which triggered uploads even when above MIN_PREKEY_COUNT (e.g. 250 prekeys → topUp=550 → 250<=550=true → unnecessary upload). Now uses correct threshold: preKeyCount < MIN_PREKEY_COUNT - socket.ts: guard scheduleNextKeepAlive() with !closed check to prevent orphan timers when end() was called concurrently on a different path Co-Authored-By: Claude Sonnet 4.6 --- src/Socket/socket.ts | 12 ++++++++---- src/Utils/history.ts | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Socket/socket.ts b/src/Socket/socket.ts index f4080284..becfe657 100644 --- a/src/Socket/socket.ts +++ b/src/Socket/socket.ts @@ -786,14 +786,15 @@ export const makeSocket = (config: SocketConfig) => { try { let count = 0 const preKeyCount = await getAvailablePreKeysOnServer() - if (preKeyCount === 0) count = INITIAL_PREKEY_COUNT - else count = Math.max(0, INITIAL_PREKEY_COUNT - preKeyCount) + // How many to upload: top-up to INITIAL_PREKEY_COUNT from whatever remains on server + count = Math.max(0, INITIAL_PREKEY_COUNT - preKeyCount) const { exists: currentPreKeyExists, currentPreKeyId } = await verifyCurrentPreKeyExists() logger.info(`${preKeyCount} pre-keys found on server`) logger.info(`Current prekey ID: ${currentPreKeyId}, exists in storage: ${currentPreKeyExists}`) - const lowServerCount = preKeyCount <= count + // Trigger upload when below the replenishment threshold, not when count < topUp amount + const lowServerCount = preKeyCount < MIN_PREKEY_COUNT const missingCurrentPreKey = !currentPreKeyExists && currentPreKeyId > 0 const shouldUpload = lowServerCount || missingCurrentPreKey @@ -1309,7 +1310,10 @@ export const makeSocket = (config: SocketConfig) => { logger.warn('keep alive called when WS not open') } - scheduleNextKeepAlive() + // Do not reschedule once shutdown has started (closed set by end() on any concurrent path) + if (!closed) { + scheduleNextKeepAlive() + } } scheduleNextKeepAlive() diff --git a/src/Utils/history.ts b/src/Utils/history.ts index 61b20871..e58aa8b9 100644 --- a/src/Utils/history.ts +++ b/src/Utils/history.ts @@ -15,6 +15,7 @@ import { import { toNumber } from './generics' import type { ILogger } from './logger.js' import { normalizeMessageContent } from './messages' +import { DEFAULT_ORIGIN } from '../Defaults' import { downloadContentFromMessage, getUrlFromDirectPath } from './messages-media' const inflatePromise = promisify(inflate) @@ -430,7 +431,11 @@ export const downloadAndProcessHistorySyncNotification = async ( // processing throws — the server copy would be gone and retry after reconnect would fail. if (msg.directPath) { const cdnUrl = getUrlFromDirectPath(msg.directPath) - fetch(cdnUrl, { ...options, method: 'DELETE' }).catch(() => { + fetch(cdnUrl, { + ...options, + method: 'DELETE', + headers: { ...((options as RequestInit).headers ?? {}), Origin: DEFAULT_ORIGIN } + }).catch(() => { // non-fatal — server will expire it anyway }) }