-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
use-multi-file-auth-state.ts
90 lines (78 loc) · 2.54 KB
/
use-multi-file-auth-state.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises'
import { join } from 'path'
import { proto } from '../../WAProto'
import { AuthenticationCreds, AuthenticationState, SignalDataTypeMap } from '../Types'
import { initAuthCreds } from './auth-utils'
import { BufferJSON } from './generics'
/**
* stores the full authentication state in a single folder.
* Far more efficient than singlefileauthstate
*
* 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> }> => {
const writeData = (data: any, file: string) => {
return writeFile(join(folder, fixFileName(file)!), JSON.stringify(data, BufferJSON.replacer))
}
const readData = async(file: string) => {
try {
const data = await readFile(join(folder, fixFileName(file)!), { encoding: 'utf-8' })
return JSON.parse(data, BufferJSON.reviver)
} catch(error) {
return null
}
}
const removeData = async(file: string) => {
try {
await unlink(join(folder, fixFileName(file)!))
} 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`)
}
} else {
await mkdir(folder, { recursive: true })
}
const fixFileName = (file?: string) => file?.replace(/\//g, '__')?.replace(/:/g, '-')
const creds: AuthenticationCreds = await readData('creds.json') || initAuthCreds()
return {
state: {
creds,
keys: {
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
}
)
)
return data
},
set: async(data) => {
const tasks: Promise<void>[] = []
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))
}
}
await Promise.all(tasks)
}
}
},
saveCreds: () => {
return writeData(creds, 'creds.json')
}
}
}