fix: address PR #305 review comments — Origin header, lowServerCount threshold, keepalive guard
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -786,14 +786,15 @@ export const makeSocket = (config: SocketConfig) => {
|
|||||||
try {
|
try {
|
||||||
let count = 0
|
let count = 0
|
||||||
const preKeyCount = await getAvailablePreKeysOnServer()
|
const preKeyCount = await getAvailablePreKeysOnServer()
|
||||||
if (preKeyCount === 0) count = INITIAL_PREKEY_COUNT
|
// How many to upload: top-up to INITIAL_PREKEY_COUNT from whatever remains on server
|
||||||
else count = Math.max(0, INITIAL_PREKEY_COUNT - preKeyCount)
|
count = Math.max(0, INITIAL_PREKEY_COUNT - preKeyCount)
|
||||||
const { exists: currentPreKeyExists, currentPreKeyId } = await verifyCurrentPreKeyExists()
|
const { exists: currentPreKeyExists, currentPreKeyId } = await verifyCurrentPreKeyExists()
|
||||||
|
|
||||||
logger.info(`${preKeyCount} pre-keys found on server`)
|
logger.info(`${preKeyCount} pre-keys found on server`)
|
||||||
logger.info(`Current prekey ID: ${currentPreKeyId}, exists in storage: ${currentPreKeyExists}`)
|
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 missingCurrentPreKey = !currentPreKeyExists && currentPreKeyId > 0
|
||||||
|
|
||||||
const shouldUpload = lowServerCount || missingCurrentPreKey
|
const shouldUpload = lowServerCount || missingCurrentPreKey
|
||||||
@@ -1309,7 +1310,10 @@ export const makeSocket = (config: SocketConfig) => {
|
|||||||
logger.warn('keep alive called when WS not open')
|
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()
|
scheduleNextKeepAlive()
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
import { toNumber } from './generics'
|
import { toNumber } from './generics'
|
||||||
import type { ILogger } from './logger.js'
|
import type { ILogger } from './logger.js'
|
||||||
import { normalizeMessageContent } from './messages'
|
import { normalizeMessageContent } from './messages'
|
||||||
|
import { DEFAULT_ORIGIN } from '../Defaults'
|
||||||
import { downloadContentFromMessage, getUrlFromDirectPath } from './messages-media'
|
import { downloadContentFromMessage, getUrlFromDirectPath } from './messages-media'
|
||||||
|
|
||||||
const inflatePromise = promisify(inflate)
|
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.
|
// processing throws — the server copy would be gone and retry after reconnect would fail.
|
||||||
if (msg.directPath) {
|
if (msg.directPath) {
|
||||||
const cdnUrl = getUrlFromDirectPath(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
|
// non-fatal — server will expire it anyway
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user