feat: narrower definition of cachestore

This commit is contained in:
Adhiraj Singh
2023-02-21 11:56:27 +05:30
parent 30328eebe1
commit 79aa2e5176
3 changed files with 22 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import type { URL } from 'url'
import { proto } from '../../WAProto' import { proto } from '../../WAProto'
import { MEDIA_HKDF_KEY_MAPPING } from '../Defaults' import { MEDIA_HKDF_KEY_MAPPING } from '../Defaults'
import type { GroupMetadata } from './GroupMetadata' import type { GroupMetadata } from './GroupMetadata'
import { CacheStore } from './Socket'
// export the WAMessage Prototypes // export the WAMessage Prototypes
export { proto as WAProto } export { proto as WAProto }
@@ -202,7 +203,7 @@ export type MediaGenerationOptions = {
mediaTypeOverride?: MediaType mediaTypeOverride?: MediaType
upload: WAMediaUploadFunction upload: WAMediaUploadFunction
/** cache media so it does not have to be uploaded again */ /** cache media so it does not have to be uploaded again */
mediaCache?: NodeCache mediaCache?: CacheStore
mediaUploadTimeoutMs?: number mediaUploadTimeoutMs?: number

View File

@@ -1,7 +1,6 @@
import { AxiosRequestConfig } from 'axios' import { AxiosRequestConfig } from 'axios'
import type { Agent } from 'https' import type { Agent } from 'https'
import type NodeCache from 'node-cache'
import type { Logger } from 'pino' import type { Logger } from 'pino'
import type { URL } from 'url' import type { URL } from 'url'
import { proto } from '../../WAProto' import { proto } from '../../WAProto'
@@ -11,6 +10,17 @@ import { MediaConnInfo } from './Message'
export type WAVersion = [number, number, number] export type WAVersion = [number, number, number]
export type WABrowserDescription = [string, string, string] export type WABrowserDescription = [string, string, string]
export type CacheStore = {
/** get a cached key and change the stats */
get<T>(key: string): T | undefined
/** set a key in the cache */
set<T>(key: string, value: T): void
/** delete a key from the cache */
del(key: string): void
/** flush all data */
flushAll(): void
}
export type SocketConfig = { export type SocketConfig = {
/** the WS url to connect to WA */ /** the WS url to connect to WA */
waWebSocketUrl: string | URL waWebSocketUrl: string | URL
@@ -34,8 +44,6 @@ export type SocketConfig = {
printQRInTerminal: boolean printQRInTerminal: boolean
/** should events be emitted for actions done by this socket connection */ /** should events be emitted for actions done by this socket connection */
emitOwnEvents: boolean emitOwnEvents: boolean
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: NodeCache
/** custom upload hosts to upload media to */ /** custom upload hosts to upload media to */
customUploadHosts: MediaConnInfo['hosts'] customUploadHosts: MediaConnInfo['hosts']
/** time to wait between sending new retry requests */ /** time to wait between sending new retry requests */
@@ -50,14 +58,17 @@ export type SocketConfig = {
transactionOpts: TransactionCapabilityOptions transactionOpts: TransactionCapabilityOptions
/** marks the client as online whenever the socket successfully connects */ /** marks the client as online whenever the socket successfully connects */
markOnlineOnConnect: boolean markOnlineOnConnect: boolean
/** provide a cache to store media, so does not have to be re-uploaded */
mediaCache?: CacheStore
/** /**
* map to store the retry counts for failed messages; * map to store the retry counts for failed messages;
* used to determine whether to retry a message or not */ * used to determine whether to retry a message or not */
msgRetryCounterCache?: NodeCache msgRetryCounterCache?: CacheStore
/** provide a cache to store a user's device list */ /** provide a cache to store a user's device list */
userDevicesCache?: NodeCache userDevicesCache?: CacheStore
/** cache to store call offers */ /** cache to store call offers */
callOfferCache?: NodeCache callOfferCache?: CacheStore
/** width for link preview images */ /** width for link preview images */
linkPreviewImageThumbnailWidth: number linkPreviewImageThumbnailWidth: number
/** Should Baileys ask the phone for full history, will be received async */ /** Should Baileys ask the phone for full history, will be received async */

View File

@@ -2,7 +2,7 @@ import { randomBytes } from 'crypto'
import NodeCache from 'node-cache' import NodeCache from 'node-cache'
import type { Logger } from 'pino' import type { Logger } from 'pino'
import { DEFAULT_CACHE_TTLS } from '../Defaults' import { DEFAULT_CACHE_TTLS } from '../Defaults'
import type { AuthenticationCreds, SignalDataSet, SignalDataTypeMap, SignalKeyStore, SignalKeyStoreWithTransaction, TransactionCapabilityOptions } from '../Types' import type { AuthenticationCreds, CacheStore, SignalDataSet, SignalDataTypeMap, SignalKeyStore, SignalKeyStoreWithTransaction, TransactionCapabilityOptions } from '../Types'
import { Curve, signedKeyPair } from './crypto' import { Curve, signedKeyPair } from './crypto'
import { delay, generateRegistrationId } from './generics' import { delay, generateRegistrationId } from './generics'
@@ -10,12 +10,12 @@ import { delay, generateRegistrationId } from './generics'
* Adds caching capability to a SignalKeyStore * Adds caching capability to a SignalKeyStore
* @param store the store to add caching to * @param store the store to add caching to
* @param logger to log trace events * @param logger to log trace events
* @param _cache NodeCache to use * @param _cache cache store to use
*/ */
export function makeCacheableSignalKeyStore( export function makeCacheableSignalKeyStore(
store: SignalKeyStore, store: SignalKeyStore,
logger: Logger, logger: Logger,
_cache?: NodeCache _cache?: CacheStore
): SignalKeyStore { ): SignalKeyStore {
const cache = _cache || new NodeCache({ const cache = _cache || new NodeCache({
stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes stdTTL: DEFAULT_CACHE_TTLS.SIGNAL_STORE, // 5 minutes