@shoru/kitten 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +7 -5
- package/src/auth/init-session.js +3 -1
- package/src/auth/lmdb-auth-state.js +63 -57
- package/src/client/client.js +240 -55
- package/src/client/getConnectionConfig.js +5 -2
- package/src/config/default.js +14 -1
- package/src/formatter/format-message.js +144 -0
- package/src/formatter/index.js +11 -0
- package/src/index.js +5 -4
- package/src/internals/config.js +7 -2
- package/src/internals/index.js +5 -4
- package/src/internals/lmdb-manager.js +3 -1
- package/src/internals/logger.js +1 -1
- package/src/internals/plugin-manager.js +477 -0
- package/src/utils/buffer-json.js +18 -3
- package/src/utils/get-pn.js +9 -0
- package/src/utils/index.js +5 -3
- package/src/utils/time-string.js +19 -0
- package/src/utils/type-conversions.js +5 -0
- package/src/utils/retry.js +0 -29
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aymane Shoru
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shoru/kitten",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "A powerful Node.js framework to simplify making WhatsApp Bots",
|
|
6
6
|
"main": "src/index.js",
|
|
7
|
-
|
|
7
|
+
"repository": {
|
|
8
8
|
"url": "git+https://github.com/Mangaka-bot/kitten-wa.git"
|
|
9
9
|
},
|
|
10
10
|
"scripts": {
|
|
@@ -13,13 +13,13 @@
|
|
|
13
13
|
},
|
|
14
14
|
"imports": {
|
|
15
15
|
"#utils.js": "./src/utils/index.js",
|
|
16
|
-
"#config.js": "./src/config/default.js",
|
|
17
16
|
"#auth.js": "./src/auth/index.js",
|
|
18
17
|
"#internals.js": "./src/internals/index.js",
|
|
19
|
-
"#client.js": "./src/client/index.js"
|
|
18
|
+
"#client.js": "./src/client/index.js",
|
|
19
|
+
"#formatter.js": "./src/formatter/index.js"
|
|
20
20
|
},
|
|
21
21
|
"exports": {
|
|
22
|
-
".":
|
|
22
|
+
".": "./src/index.js"
|
|
23
23
|
},
|
|
24
24
|
"author": "Aymane Shoru",
|
|
25
25
|
"license": "MIT",
|
|
@@ -27,8 +27,10 @@
|
|
|
27
27
|
"@hapi/boom": "^10.0.1",
|
|
28
28
|
"@inquirer/prompts": "^8.1.0",
|
|
29
29
|
"@shoru/spindle": "^1.0.6",
|
|
30
|
+
"async-mutex": "^0.5.0",
|
|
30
31
|
"baileys": "^7.0.0-rc.9",
|
|
31
32
|
"chalk": "^5.6.2",
|
|
33
|
+
"chokidar": "^5.0.0",
|
|
32
34
|
"cosmiconfig": "^9.0.0",
|
|
33
35
|
"defu": "^6.1.4",
|
|
34
36
|
"lmdb": "^3.4.4",
|
package/src/auth/init-session.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { makeWASocket } from "baileys";
|
|
2
2
|
import { useLMDBAuthState } from "./lmdb-auth-state.js";
|
|
3
|
-
import {
|
|
3
|
+
import { getConfig } from "#internals.js";
|
|
4
|
+
|
|
5
|
+
const config = await getConfig();
|
|
4
6
|
|
|
5
7
|
export const initSession = async ({ socketConfig, id } = {}) => {
|
|
6
8
|
try {
|
|
@@ -17,11 +17,9 @@ const keyBuilder = (sessionId) => {
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
const genID = async (db) => {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return id;
|
|
24
|
-
});
|
|
20
|
+
const id = (db.get(COUNTER_KEY) ?? 0) + 1;
|
|
21
|
+
await db.put(COUNTER_KEY, id);
|
|
22
|
+
return id;
|
|
25
23
|
};
|
|
26
24
|
|
|
27
25
|
const getSessionId = async (db, input) => {
|
|
@@ -37,6 +35,17 @@ export async function useLMDBAuthState(inputSessionId) {
|
|
|
37
35
|
const sessionId = await getSessionId(db, inputSessionId);
|
|
38
36
|
const keys = keyBuilder(sessionId);
|
|
39
37
|
|
|
38
|
+
const existingCreds = db.get(keys.creds);
|
|
39
|
+
let creds;
|
|
40
|
+
|
|
41
|
+
if (existingCreds != null) {
|
|
42
|
+
creds = deserialize(existingCreds);
|
|
43
|
+
} else {
|
|
44
|
+
creds = initAuthCreds();
|
|
45
|
+
await db.put(keys.creds, serialize(creds));
|
|
46
|
+
await db.put(`${SESSION_PREFIX}${sessionId}`, true);
|
|
47
|
+
}
|
|
48
|
+
|
|
40
49
|
const writeCreds = async (credsData) => {
|
|
41
50
|
await db.put(keys.creds, serialize(credsData));
|
|
42
51
|
};
|
|
@@ -44,90 +53,87 @@ export async function useLMDBAuthState(inputSessionId) {
|
|
|
44
53
|
const getKeys = (type, ids) => {
|
|
45
54
|
if (!ids.length) return {};
|
|
46
55
|
|
|
47
|
-
const keyList = ids.map((id) => keys.forKey(type, id));
|
|
48
|
-
const values = db.getMany(keyList);
|
|
49
|
-
|
|
50
56
|
const result = {};
|
|
51
|
-
for (
|
|
52
|
-
const
|
|
57
|
+
for (const id of ids) {
|
|
58
|
+
const dbKey = keys.forKey(type, id);
|
|
59
|
+
const rawValue = db.get(dbKey);
|
|
53
60
|
|
|
54
|
-
if (rawValue) {
|
|
61
|
+
if (rawValue != null) {
|
|
55
62
|
try {
|
|
56
63
|
let parsed = deserialize(rawValue);
|
|
57
64
|
if (type === "app-state-sync-key" && parsed) {
|
|
58
65
|
parsed = proto.Message.AppStateSyncKeyData.fromObject(parsed);
|
|
59
66
|
}
|
|
60
|
-
result[
|
|
67
|
+
result[id] = parsed;
|
|
61
68
|
} catch (err) {
|
|
62
69
|
logger.error(
|
|
63
70
|
err,
|
|
64
|
-
`[LMDBAuthState] Deserialize error: ${type}:${
|
|
71
|
+
`[LMDBAuthState] Deserialize error: ${type}:${id}`
|
|
65
72
|
);
|
|
66
|
-
|
|
73
|
+
result[id] = null;
|
|
67
74
|
}
|
|
75
|
+
} else {
|
|
76
|
+
result[id] = null;
|
|
68
77
|
}
|
|
69
78
|
}
|
|
70
79
|
return result;
|
|
71
80
|
};
|
|
72
81
|
|
|
73
82
|
const setKeys = async (data) => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
83
|
+
const writes = [];
|
|
84
|
+
|
|
85
|
+
for (const [category, categoryData] of Object.entries(data)) {
|
|
86
|
+
if (!categoryData) continue;
|
|
87
|
+
for (const [id, value] of Object.entries(categoryData)) {
|
|
88
|
+
const key = keys.forKey(category, id);
|
|
89
|
+
if (value != null) {
|
|
90
|
+
writes.push(db.put(key, serialize(value)));
|
|
91
|
+
} else {
|
|
92
|
+
writes.push(db.remove(key));
|
|
84
93
|
}
|
|
85
94
|
}
|
|
86
|
-
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (writes.length > 0) {
|
|
98
|
+
await Promise.all(writes);
|
|
99
|
+
}
|
|
87
100
|
};
|
|
88
101
|
|
|
89
102
|
const clearKeys = async () => {
|
|
90
103
|
let count = 0;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
104
|
+
const writes = [];
|
|
105
|
+
|
|
106
|
+
for (const { key } of db.getRange({
|
|
107
|
+
start: keys.sessionPrefix,
|
|
108
|
+
end: `${keys.sessionPrefix}\xFF`,
|
|
109
|
+
})) {
|
|
110
|
+
if (key !== keys.creds) {
|
|
111
|
+
writes.push(db.remove(key));
|
|
112
|
+
count++;
|
|
100
113
|
}
|
|
101
|
-
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (writes.length > 0) {
|
|
117
|
+
await Promise.all(writes);
|
|
118
|
+
}
|
|
102
119
|
logger.debug(`[LMDBAuthState] Cleared ${count} keys`);
|
|
103
120
|
};
|
|
104
121
|
|
|
105
122
|
const deleteSession = async () => {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
});
|
|
115
|
-
|
|
123
|
+
const writes = [];
|
|
124
|
+
|
|
125
|
+
for (const { key } of db.getRange({
|
|
126
|
+
start: keys.sessionPrefix,
|
|
127
|
+
end: `${keys.sessionPrefix}\xFF`,
|
|
128
|
+
})) {
|
|
129
|
+
writes.push(db.remove(key));
|
|
130
|
+
}
|
|
131
|
+
writes.push(db.remove(`${SESSION_PREFIX}${sessionId}`));
|
|
132
|
+
|
|
133
|
+
await Promise.all(writes);
|
|
116
134
|
logger.debug(`[LMDBAuthState] Deleted session ${sessionId}`);
|
|
117
135
|
};
|
|
118
136
|
|
|
119
|
-
const creds = await db.transaction(() => {
|
|
120
|
-
const existing = db.get(keys.creds);
|
|
121
|
-
if (existing != null) {
|
|
122
|
-
return deserialize(existing);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const newCreds = initAuthCreds();
|
|
126
|
-
db.put(keys.creds, serialize(newCreds));
|
|
127
|
-
db.put(`${SESSION_PREFIX}${sessionId}`, true);
|
|
128
|
-
return newCreds;
|
|
129
|
-
});
|
|
130
|
-
|
|
131
137
|
return {
|
|
132
138
|
state: {
|
|
133
139
|
creds,
|