Skip to content

Commit 25dd131

Browse files
committed
reduce ifs in auth state
1 parent 13b13b9 commit 25dd131

7 files changed

Lines changed: 22 additions & 20 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unoapi-cloud",
3-
"version": "2.10.2",
3+
"version": "2.11-0-dev",
44
"description": "Unoapi Cloud",
55
"exports": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export const UNOAPI_RETRY_REQUEST_DELAY_MS = parseInt(process.env.UNOAPI_RETRY_R
7979
export const DATA_TTL: number = parseInt(process.env.DATA_TTL || `${60 * 60 * 24 * 30}`) // a month
8080
export const DATA_URL_TTL: number = parseInt(process.env.DATA_URL_TTL || `${60 * 60 * 24 * 3}`) // tree days
8181
export const SESSION_TTL: number = parseInt(process.env.SESSION_TTL || '-1')
82+
export const SESSION_KEY_TTL: number = parseInt(process.env.SESSION_KEY_TTL || '-1') // a day
8283
export const UNOAPI_X_COUNT_RETRIES: string = process.env.UNOAPI_X_COUNT_RETRIES || 'x-unoapi-count-retries'
8384
export const UNOAPI_X_MAX_RETRIES: string = process.env.UNOAPI_X_MAX_RETRIES || 'x-unoapi-max-retries'
8485
export const UNOAPI_EXCHANGE_NAME = process.env.UNOAPI_EXCHANGE_NAME || 'unoapi'

src/services/auth_state.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { initAuthCreds, proto, AuthenticationState, AuthenticationCreds, makeCacheableSignalKeyStore } from 'baileys'
22
import { session } from './session'
33
import logger from './logger'
4+
import { SESSION_KEY_TTL } from '../defaults'
45

56
export const authState = async (session: session, phone: string) => {
6-
const { readData, writeData, removeData, getKey } = await session(phone)
7+
const { readData, writeData, removeData, getKey, extension } = await session(phone)
78

8-
const creds: AuthenticationCreds = ((await readData('')) || initAuthCreds()) as AuthenticationCreds
9+
const creds: AuthenticationCreds = ((await readData(`creds${extension}`)) || initAuthCreds()) as AuthenticationCreds
910

1011
const keys = {
1112
get: async (type: string, ids: string[]) => {
@@ -32,7 +33,7 @@ export const authState = async (session: session, phone: string) => {
3233
for (const id in data[category]) {
3334
const value = data[category][id]
3435
const key = getKey(category, id)
35-
tasks.push(value ? writeData(key, value) : removeData(key))
36+
tasks.push(value ? writeData(key, value, SESSION_KEY_TTL) : removeData(key || `creds${extension}`))
3637
}
3738
}
3839
await Promise.all(tasks)
@@ -46,7 +47,7 @@ export const authState = async (session: session, phone: string) => {
4647

4748
const saveCreds: () => Promise<void> = async () => {
4849
logger.debug('save creds %s', phone)
49-
return await writeData('', creds)
50+
return await writeData(`creds${extension}`, creds, -1)
5051
}
5152

5253
return {

src/services/redis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ export const getAuth = async (phone: string, parse = (value: string) => JSON.par
362362
}
363363

364364
// eslint-disable-next-line @typescript-eslint/no-explicit-any
365-
export const setAuth = async (phone: string, value: any, stringify = (value: string) => JSON.stringify(value, null, '\t')) => {
365+
export const setAuth = async (phone: string, value: any, ttl, stringify = (value: string) => JSON.stringify(value, null, '\t')) => {
366366
const key = authKey(phone)
367367
const authValue = stringify(value)
368-
return redisSetAndExpire(key, authValue, SESSION_TTL)
368+
return redisSetAndExpire(key, authValue, ttl)
369369
}
370370

371371
export const setbulkMessage = async (phone: string, bulkId: string, messageId: string, phoneNumber) => {

src/services/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface writeData {
2-
(key: string, data: object): Promise<void>
2+
(key: string, data: object, ttl: number): Promise<void>
33
}
44

55
export interface readData {
@@ -15,5 +15,5 @@ export interface getKey {
1515
}
1616

1717
export interface session {
18-
(phone: string): Promise<{ writeData: writeData; readData: readData; removeData: removeData; getKey: getKey }>
18+
(phone: string): Promise<{ writeData: writeData; readData: readData; removeData: removeData; getKey: getKey, extension }>
1919
}

src/services/session_file.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { session, writeData, readData, removeData, getKey } from './session'
44
import logger from './logger'
55

66
export const sessionFile: session = async (phone: string) => {
7-
const getKey: getKey = (type: string, id: string) => `/${type}-${id}.json`
8-
const getFile = (key: string) => `${phone}${key ? key : '/creds.json'}`.replace('/.', '')
7+
const extension = '.json'
8+
const getKey: getKey = (type: string, id: string) => `${type}-${id}${extension}`
9+
const getFile = (key: string) => `${phone}/${key}`.replace('/.', '')
910

1011
if (!existsSync(phone)) {
1112
mkdirSync(phone, { recursive: true })
@@ -46,7 +47,7 @@ export const sessionFile: session = async (phone: string) => {
4647
}
4748
}
4849

49-
const writeData: writeData = async (key: string, data: object) => {
50+
const writeData: writeData = async (key: string, data: object, _ttl: number) => {
5051
const file = getFile(key)
5152
logger.debug('write data', file)
5253
try {
@@ -88,5 +89,5 @@ export const sessionFile: session = async (phone: string) => {
8889
}
8990
}
9091

91-
return { writeData, readData, removeData, getKey }
92+
return { writeData, readData, removeData, getKey, extension }
9293
}

src/services/session_redis.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import { session, writeData, readData, removeData, getKey } from './session'
44
import logger from './logger'
55

66
export const sessionRedis: session = async (phone: string) => {
7-
const getKey: getKey = (type: string, id: string) => `:${type}-${id}`
8-
const getBase = (key: string) => `${phone}${key ? key : ':creds'}`
7+
const getKey: getKey = (type: string, id: string) => `${phone}:${type}-${id}`
98

10-
const writeData: writeData = async (key: string, data: object) => {
9+
const writeData: writeData = async (key: string, data: object, ttl: number) => {
1110
try {
1211
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13-
return setAuth(getBase(key), data, (value: any) => JSON.stringify(value, BufferJSON.replacer))
12+
return setAuth(key, data, ttl, (value: any) => JSON.stringify(value, BufferJSON.replacer))
1413
} catch (error) {
1514
logger.error(error, 'Error on write auth')
1615
throw error
@@ -19,7 +18,7 @@ export const sessionRedis: session = async (phone: string) => {
1918

2019
const readData: readData = async (key: string) => {
2120
try {
22-
return getAuth(getBase(key), (value: string) => {
21+
return getAuth(key, (value: string) => {
2322
try {
2423
return value ? JSON.parse(value, BufferJSON.reviver) : null
2524
} catch (error) {
@@ -35,12 +34,12 @@ export const sessionRedis: session = async (phone: string) => {
3534

3635
const removeData: removeData = async (key: string) => {
3736
try {
38-
await delAuth(getBase(key))
37+
await delAuth(key)
3938
} catch (error) {
4039
logger.error(error, 'Error on remove auth %s')
4140
throw error
4241
}
4342
}
4443

45-
return { writeData, getKey, removeData, readData }
44+
return { writeData, getKey, removeData, readData, extension: '' }
4645
}

0 commit comments

Comments
 (0)