mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: format everything
This commit is contained in:
@@ -15,7 +15,7 @@ const fileLocks = new Map<string, Mutex>()
|
||||
// Get or create a mutex for a specific file path
|
||||
const getFileLock = (path: string): Mutex => {
|
||||
let mutex = fileLocks.get(path)
|
||||
if(!mutex) {
|
||||
if (!mutex) {
|
||||
mutex = new Mutex()
|
||||
fileLocks.set(path, mutex)
|
||||
}
|
||||
@@ -30,13 +30,15 @@ const getFileLock = (path: string): Mutex => {
|
||||
* Again, I wouldn't endorse this for any production level use other than perhaps a bot.
|
||||
* Would recommend writing an auth state for use with a proper SQL or No-SQL DB
|
||||
* */
|
||||
export const useMultiFileAuthState = async(folder: string): Promise<{ state: AuthenticationState, saveCreds: () => Promise<void> }> => {
|
||||
export const useMultiFileAuthState = async (
|
||||
folder: string
|
||||
): Promise<{ state: AuthenticationState; saveCreds: () => Promise<void> }> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const writeData = async(data: any, file: string) => {
|
||||
const writeData = async (data: any, file: string) => {
|
||||
const filePath = join(folder, fixFileName(file)!)
|
||||
const mutex = getFileLock(filePath)
|
||||
|
||||
return mutex.acquire().then(async(release) => {
|
||||
return mutex.acquire().then(async release => {
|
||||
try {
|
||||
await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer))
|
||||
} finally {
|
||||
@@ -45,12 +47,12 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
|
||||
})
|
||||
}
|
||||
|
||||
const readData = async(file: string) => {
|
||||
const readData = async (file: string) => {
|
||||
try {
|
||||
const filePath = join(folder, fixFileName(file)!)
|
||||
const mutex = getFileLock(filePath)
|
||||
|
||||
return await mutex.acquire().then(async(release) => {
|
||||
return await mutex.acquire().then(async release => {
|
||||
try {
|
||||
const data = await readFile(filePath, { encoding: 'utf-8' })
|
||||
return JSON.parse(data, BufferJSON.reviver)
|
||||
@@ -58,32 +60,33 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
|
||||
release()
|
||||
}
|
||||
})
|
||||
} catch(error) {
|
||||
} catch (error) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
const removeData = async(file: string) => {
|
||||
const removeData = async (file: string) => {
|
||||
try {
|
||||
const filePath = join(folder, fixFileName(file)!)
|
||||
const mutex = getFileLock(filePath)
|
||||
|
||||
return mutex.acquire().then(async(release) => {
|
||||
return mutex.acquire().then(async release => {
|
||||
try {
|
||||
await unlink(filePath)
|
||||
} catch{
|
||||
} catch {
|
||||
} finally {
|
||||
release()
|
||||
}
|
||||
})
|
||||
} catch{
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const folderInfo = await stat(folder).catch(() => { })
|
||||
if(folderInfo) {
|
||||
if(!folderInfo.isDirectory()) {
|
||||
throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`)
|
||||
const folderInfo = await stat(folder).catch(() => {})
|
||||
if (folderInfo) {
|
||||
if (!folderInfo.isDirectory()) {
|
||||
throw new Error(
|
||||
`found something that is not a directory at ${folder}, either delete it or specify a different location`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
await mkdir(folder, { recursive: true })
|
||||
@@ -91,33 +94,31 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
|
||||
|
||||
const fixFileName = (file?: string) => file?.replace(/\//g, '__')?.replace(/:/g, '-')
|
||||
|
||||
const creds: AuthenticationCreds = await readData('creds.json') || initAuthCreds()
|
||||
const creds: AuthenticationCreds = (await readData('creds.json')) || initAuthCreds()
|
||||
|
||||
return {
|
||||
state: {
|
||||
creds,
|
||||
keys: {
|
||||
get: async(type, ids) => {
|
||||
const data: { [_: string]: SignalDataTypeMap[typeof type] } = { }
|
||||
get: async (type, ids) => {
|
||||
const data: { [_: string]: SignalDataTypeMap[typeof type] } = {}
|
||||
await Promise.all(
|
||||
ids.map(
|
||||
async id => {
|
||||
let value = await readData(`${type}-${id}.json`)
|
||||
if(type === 'app-state-sync-key' && value) {
|
||||
value = proto.Message.AppStateSyncKeyData.fromObject(value)
|
||||
}
|
||||
|
||||
data[id] = value
|
||||
ids.map(async id => {
|
||||
let value = await readData(`${type}-${id}.json`)
|
||||
if (type === 'app-state-sync-key' && value) {
|
||||
value = proto.Message.AppStateSyncKeyData.fromObject(value)
|
||||
}
|
||||
)
|
||||
|
||||
data[id] = value
|
||||
})
|
||||
)
|
||||
|
||||
return data
|
||||
},
|
||||
set: async(data) => {
|
||||
set: async data => {
|
||||
const tasks: Promise<void>[] = []
|
||||
for(const category in data) {
|
||||
for(const id in data[category]) {
|
||||
for (const category in data) {
|
||||
for (const id in data[category]) {
|
||||
const value = data[category][id]
|
||||
const file = `${category}-${id}.json`
|
||||
tasks.push(value ? writeData(value, file) : removeData(file))
|
||||
@@ -128,8 +129,8 @@ export const useMultiFileAuthState = async(folder: string): Promise<{ state: Aut
|
||||
}
|
||||
}
|
||||
},
|
||||
saveCreds: async() => {
|
||||
saveCreds: async () => {
|
||||
return writeData(creds, 'creds.json')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user