-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfa4cea
commit 5429368
Showing
6 changed files
with
124 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,24 @@ | ||
{ | ||
"name": "Example devcontainer for add-on repositories", | ||
"image": "ghcr.io/home-assistant/devcontainer:addons", | ||
"appPort": [ | ||
"7123:8123", | ||
"7357:4357" | ||
], | ||
"appPort": ["7123:8123", "7357:4357"], | ||
"postStartCommand": "bash devcontainer_bootstrap", | ||
"runArgs": [ | ||
"-e", | ||
"GIT_EDITOR=code --wait", | ||
"--privileged" | ||
], | ||
"runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"], | ||
"containerEnv": { | ||
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}" | ||
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}" | ||
}, | ||
"extensions": [ | ||
"timonwong.shellcheck", | ||
"esbenp.prettier-vscode" | ||
], | ||
"mounts": [ | ||
"type=volume,target=/var/lib/docker" | ||
], | ||
"extensions": ["timonwong.shellcheck", "esbenp.prettier-vscode"], | ||
"mounts": [ "type=volume,target=/var/lib/docker" ], | ||
"settings": { | ||
"terminal.integrated.profiles.linux": { | ||
"zsh": { | ||
"path": "/usr/bin/zsh" | ||
} | ||
}, | ||
"terminal.integrated.defaultProfile.linux": "zsh", | ||
"editor.formatOnPaste": false, | ||
"editor.formatOnSave": true, | ||
"editor.formatOnType": true, | ||
"files.trimTrailingWhitespace": true | ||
"terminal.integrated.profiles.linux": { | ||
"zsh": { | ||
"path": "/usr/bin/zsh" | ||
} | ||
}, | ||
"terminal.integrated.defaultProfile.linux": "zsh", | ||
"editor.formatOnPaste": false, | ||
"editor.formatOnSave": true, | ||
"editor.formatOnType": true, | ||
"files.trimTrailingWhitespace": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { | ||
proto, | ||
initAuthCreds, | ||
BufferJSON | ||
} = require("@adiwajshing/baileys"); | ||
const fs = require('fs'); | ||
|
||
const KEY_MAP = { | ||
'pre-key': 'preKeys', | ||
'session': 'sessions', | ||
'sender-key': 'senderKeys', | ||
'app-state-sync-key': 'appStateSyncKeys', | ||
'app-state-sync-version': 'appStateVersions', | ||
'sender-key-memory': 'senderKeyMemory' | ||
}; | ||
|
||
async function readFileAsync(filename) { | ||
try { | ||
return await fs.promises.readFile(filename, 'utf8'); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
async function writeToFileAsync(filename, text) { | ||
await fs.promises.writeFile(filename, text, 'utf8'); | ||
} | ||
|
||
const useFileAuthState = async (path) => { | ||
const saved_data = await readFileAsync(path) | ||
|
||
let creds = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).creds : initAuthCreds(); | ||
let keys = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).keys : {}; | ||
|
||
const saveState = () => { | ||
writeToFileAsync(path, JSON.stringify({ creds, keys }, BufferJSON.replacer, 2)); | ||
}; | ||
|
||
return { | ||
state: { | ||
creds, | ||
keys: { | ||
get: (type, ids) => { | ||
const key = KEY_MAP[type]; | ||
return ids.reduce((dict, id) => { | ||
var _a; | ||
let value = (_a = keys[key]) === null || _a === void 0 ? void 0 : _a[id]; | ||
if (value) { | ||
if (type === 'app-state-sync-key') { | ||
value = proto.Message.AppStateSyncKeyData.fromObject(value); | ||
} | ||
dict[id] = value; | ||
} | ||
return dict; | ||
}, {}); | ||
}, | ||
set: (data) => { | ||
for (const _key in data) { | ||
const key = KEY_MAP[_key]; | ||
keys[key] = keys[key] || {}; | ||
Object.assign(keys[key], data[_key]); | ||
} | ||
saveState(); | ||
} | ||
} | ||
}, | ||
saveState | ||
}; | ||
}; | ||
|
||
module.exports = useFileAuthState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters