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 }) }