@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 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.1",
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
- "repository": {
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
- ".": "./src/index.js"
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",
@@ -1,6 +1,8 @@
1
1
  import { makeWASocket } from "baileys";
2
2
  import { useLMDBAuthState } from "./lmdb-auth-state.js";
3
- import { config } from "#internals.js";
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
- return db.transaction(() => {
21
- const id = (db.get(COUNTER_KEY) ?? 0) + 1;
22
- db.put(COUNTER_KEY, id);
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 (let i = 0; i < ids.length; i++) {
52
- const rawValue = values[i];
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[ids[i]] = parsed;
67
+ result[id] = parsed;
61
68
  } catch (err) {
62
69
  logger.error(
63
70
  err,
64
- `[LMDBAuthState] Deserialize error: ${type}:${ids[i]}`
71
+ `[LMDBAuthState] Deserialize error: ${type}:${id}`
65
72
  );
66
- db.remove(keys.forKey(type, ids[i]));
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
- await db.batch(() => {
75
- for (const [category, categoryData] of Object.entries(data)) {
76
- if (!categoryData) continue;
77
- for (const [id, value] of Object.entries(categoryData)) {
78
- const key = keys.forKey(category, id);
79
- if (value != null) {
80
- db.put(key, serialize(value));
81
- } else {
82
- db.remove(key);
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
- await db.batch(() => {
92
- for (const { key } of db.getRange({
93
- start: keys.sessionPrefix,
94
- end: `${keys.sessionPrefix}\xFF`,
95
- })) {
96
- if (key !== keys.creds) {
97
- db.remove(key);
98
- count++;
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
- await db.batch(() => {
107
- for (const { key } of db.getRange({
108
- start: keys.sessionPrefix,
109
- end: `${keys.sessionPrefix}\xFF`,
110
- })) {
111
- db.remove(key);
112
- }
113
- db.remove(`${SESSION_PREFIX}${sessionId}`);
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,