@rizzxdc/baileys 8.0.10 → 8.1.0

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.
Files changed (220) hide show
  1. package/LICENSE +4 -2
  2. package/README.md +107 -245
  3. package/WAProto/index.d.ts +15795 -0
  4. package/WAProto/index.js +25587 -4867
  5. package/lib/Defaults/index.d.ts +147 -0
  6. package/lib/Defaults/index.js +134 -104
  7. package/lib/Signal/Group/ciphertext-message.d.ts +10 -0
  8. package/lib/Signal/Group/ciphertext-message.js +9 -11
  9. package/lib/Signal/Group/group-session-builder.d.ts +8 -0
  10. package/lib/Signal/Group/group-session-builder.js +5 -8
  11. package/lib/Signal/Group/group_cipher.d.ts +11 -0
  12. package/lib/Signal/Group/group_cipher.js +72 -75
  13. package/lib/Signal/Group/index.d.ts +12 -0
  14. package/lib/Signal/Group/index.js +11 -13
  15. package/lib/Signal/Group/keyhelper.d.ts +7 -0
  16. package/lib/Signal/Group/keyhelper.js +11 -14
  17. package/lib/Signal/Group/sender-chain-key.d.ts +14 -0
  18. package/lib/Signal/Group/sender-chain-key.js +23 -26
  19. package/lib/Signal/Group/sender-key-distribution-message.d.ts +17 -0
  20. package/lib/Signal/Group/sender-key-distribution-message.js +58 -61
  21. package/lib/Signal/Group/sender-key-message.d.ts +19 -0
  22. package/lib/Signal/Group/sender-key-message.js +61 -64
  23. package/lib/Signal/Group/sender-key-name.d.ts +12 -0
  24. package/lib/Signal/Group/sender-key-name.js +39 -44
  25. package/lib/Signal/Group/sender-key-record.d.ts +13 -0
  26. package/lib/Signal/Group/sender-key-record.js +35 -38
  27. package/lib/Signal/Group/sender-key-state.d.ts +17 -0
  28. package/lib/Signal/Group/sender-key-state.js +76 -79
  29. package/lib/Signal/Group/sender-message-key.d.ts +12 -0
  30. package/lib/Signal/Group/sender-message-key.js +23 -26
  31. package/lib/Signal/libsignal.d.ts +55 -0
  32. package/lib/Signal/libsignal.js +414 -308
  33. package/lib/Signal/lid-mapping.d.ts +21 -0
  34. package/lib/Signal/lid-mapping.js +270 -149
  35. package/lib/Socket/Client/index.d.ts +3 -0
  36. package/lib/Socket/Client/index.js +2 -4
  37. package/lib/Socket/Client/types.d.ts +6 -0
  38. package/lib/Socket/Client/types.js +8 -11
  39. package/lib/Socket/Client/websocket.d.ts +13 -0
  40. package/lib/Socket/Client/websocket.js +51 -50
  41. package/lib/Socket/business.d.ts +395 -0
  42. package/lib/Socket/business.js +357 -355
  43. package/lib/Socket/chats.d.ts +137 -0
  44. package/lib/Socket/chats.js +1195 -867
  45. package/lib/Socket/communities.d.ts +562 -0
  46. package/lib/Socket/communities.js +416 -398
  47. package/lib/Socket/dugong.js +8 -1
  48. package/lib/Socket/groups.d.ts +286 -0
  49. package/lib/Socket/groups.js +336 -301
  50. package/lib/Socket/index.d.ts +530 -0
  51. package/lib/Socket/index.js +8 -13
  52. package/lib/Socket/messages-recv.d.ts +334 -0
  53. package/lib/Socket/messages-recv.js +1729 -1121
  54. package/lib/Socket/messages-send.d.ts +330 -0
  55. package/lib/Socket/messages-send.js +684 -462
  56. package/lib/Socket/mex.d.ts +2 -0
  57. package/lib/Socket/mex.js +36 -40
  58. package/lib/Socket/newsletter.d.ts +284 -0
  59. package/lib/Socket/newsletter.js +247 -237
  60. package/lib/Socket/socket.d.ts +61 -0
  61. package/lib/Socket/socket.js +968 -790
  62. package/lib/Store/index.d.ts +4 -0
  63. package/lib/Store/index.js +4 -6
  64. package/lib/Store/make-cache-manager-store.js +2 -2
  65. package/lib/Store/make-in-memory-store.d.ts +63 -0
  66. package/lib/Store/make-in-memory-store.js +414 -284
  67. package/lib/Store/make-ordered-dictionary.d.ts +13 -0
  68. package/lib/Store/make-ordered-dictionary.js +75 -76
  69. package/lib/Store/object-repository.d.ts +11 -0
  70. package/lib/Store/object-repository.js +21 -23
  71. package/lib/Types/Auth.d.ts +2 -0
  72. package/lib/Types/Auth.js +0 -2
  73. package/lib/Types/Bussines.d.ts +2 -0
  74. package/lib/Types/Bussines.js +0 -2
  75. package/lib/Types/Call.d.ts +2 -0
  76. package/lib/Types/Call.js +0 -2
  77. package/lib/Types/Chat.d.ts +2 -0
  78. package/lib/Types/Chat.js +5 -7
  79. package/lib/Types/Contact.d.ts +2 -0
  80. package/lib/Types/Contact.js +0 -2
  81. package/lib/Types/Events.d.ts +2 -0
  82. package/lib/Types/Events.js +1 -3
  83. package/lib/Types/GroupMetadata.d.ts +2 -0
  84. package/lib/Types/GroupMetadata.js +0 -2
  85. package/lib/Types/Label.d.ts +3 -0
  86. package/lib/Types/Label.js +21 -22
  87. package/lib/Types/LabelAssociation.d.ts +3 -0
  88. package/lib/Types/LabelAssociation.js +3 -4
  89. package/lib/Types/Message.d.ts +12 -0
  90. package/lib/Types/Message.js +11 -6
  91. package/lib/Types/Mex.d.ts +3 -0
  92. package/lib/Types/Mex.js +38 -0
  93. package/lib/Types/Product.d.ts +2 -0
  94. package/lib/Types/Product.js +0 -2
  95. package/lib/Types/RichType.d.ts +3 -0
  96. package/lib/Types/RichType.js +22 -0
  97. package/lib/Types/Signal.d.ts +2 -0
  98. package/lib/Types/Signal.js +1 -3
  99. package/lib/Types/Socket.d.ts +2 -0
  100. package/lib/Types/Socket.js +1 -4
  101. package/lib/Types/State.d.ts +6 -0
  102. package/lib/Types/State.js +52 -8
  103. package/lib/Types/USync.d.ts +2 -0
  104. package/lib/Types/USync.js +1 -3
  105. package/lib/Types/index.d.ts +14 -0
  106. package/lib/Types/index.js +22 -25
  107. package/lib/Utils/auth-utils.d.ts +58 -0
  108. package/lib/Utils/auth-utils.js +285 -203
  109. package/lib/Utils/browser-utils.d.ts +9 -0
  110. package/lib/Utils/browser-utils.js +21 -19
  111. package/lib/Utils/business.d.ts +50 -0
  112. package/lib/Utils/business.js +206 -209
  113. package/lib/Utils/chat-utils.d.ts +409 -0
  114. package/lib/Utils/chat-utils.js +825 -706
  115. package/lib/Utils/companion-reg-client-utils.d.ts +5 -0
  116. package/lib/Utils/companion-reg-client-utils.js +39 -0
  117. package/lib/Utils/crypto.d.ts +40 -0
  118. package/lib/Utils/crypto.js +88 -101
  119. package/lib/Utils/decode-wa-message.d.ts +84 -0
  120. package/lib/Utils/decode-wa-message.js +287 -238
  121. package/lib/Utils/event-buffer.d.ts +13 -0
  122. package/lib/Utils/event-buffer.js +602 -509
  123. package/lib/Utils/generics.d.ts +64 -0
  124. package/lib/Utils/generics.js +334 -295
  125. package/lib/Utils/history.d.ts +76 -0
  126. package/lib/Utils/history.js +125 -79
  127. package/lib/Utils/identity-change-handler.d.ts +14 -0
  128. package/lib/Utils/identity-change-handler.js +49 -0
  129. package/lib/Utils/index.d.ts +25 -0
  130. package/lib/Utils/index.js +25 -21
  131. package/lib/Utils/link-preview.d.ts +13 -0
  132. package/lib/Utils/link-preview.js +73 -70
  133. package/lib/Utils/logger.d.ts +3 -0
  134. package/lib/Utils/logger.js +1 -4
  135. package/lib/Utils/lt-hash.d.ts +7 -0
  136. package/lib/Utils/lt-hash.js +7 -45
  137. package/lib/Utils/make-mutex.d.ts +7 -0
  138. package/lib/Utils/make-mutex.js +27 -31
  139. package/lib/Utils/message-retry-manager.d.ts +80 -0
  140. package/lib/Utils/message-retry-manager.js +165 -14
  141. package/lib/Utils/messages-media.d.ts +137 -0
  142. package/lib/Utils/messages-media.js +768 -572
  143. package/lib/Utils/messages.d.ts +46 -0
  144. package/lib/Utils/messages.js +1082 -130
  145. package/lib/Utils/noise-handler.d.ts +21 -0
  146. package/lib/Utils/noise-handler.js +191 -135
  147. package/lib/Utils/offline-node-processor.d.ts +10 -0
  148. package/lib/Utils/offline-node-processor.js +39 -0
  149. package/lib/Utils/pre-key-manager.d.ts +26 -0
  150. package/lib/Utils/pre-key-manager.js +96 -76
  151. package/lib/Utils/process-message.d.ts +84 -0
  152. package/lib/Utils/process-message.js +718 -311
  153. package/lib/Utils/reporting-utils.d.ts +13 -0
  154. package/lib/Utils/reporting-utils.js +257 -0
  155. package/lib/Utils/rich-message-utils.d.ts +49 -0
  156. package/lib/Utils/rich-message-utils.js +257 -0
  157. package/lib/Utils/signal.d.ts +83 -0
  158. package/lib/Utils/signal.js +174 -135
  159. package/lib/Utils/stanza-ack.d.ts +17 -0
  160. package/lib/Utils/stanza-ack.js +37 -0
  161. package/lib/Utils/sync-action-utils.d.ts +3 -0
  162. package/lib/Utils/sync-action-utils.js +48 -0
  163. package/lib/Utils/tc-token-utils.d.ts +30 -0
  164. package/lib/Utils/tc-token-utils.js +162 -0
  165. package/lib/Utils/use-multi-file-auth-state.d.ts +11 -0
  166. package/lib/Utils/use-multi-file-auth-state.js +104 -95
  167. package/lib/Utils/use-single-file-auth-state.d.ts +11 -0
  168. package/lib/Utils/use-single-file-auth-state.js +108 -0
  169. package/lib/Utils/use-sqlite-auth-state.d.ts +11 -0
  170. package/lib/Utils/use-sqlite-auth-state.js +108 -0
  171. package/lib/Utils/validate-connection.d.ts +44 -0
  172. package/lib/Utils/validate-connection.js +182 -180
  173. package/lib/WABinary/constants.d.ts +62 -0
  174. package/lib/WABinary/constants.js +1455 -1292
  175. package/lib/WABinary/decode.d.ts +10 -0
  176. package/lib/WABinary/decode.js +249 -228
  177. package/lib/WABinary/encode.d.ts +3 -0
  178. package/lib/WABinary/encode.js +207 -206
  179. package/lib/WABinary/generic-utils.d.ts +77 -0
  180. package/lib/WABinary/generic-utils.js +135 -99
  181. package/lib/WABinary/index.d.ts +6 -0
  182. package/lib/WABinary/index.js +5 -7
  183. package/lib/WABinary/jid-utils.d.ts +29 -0
  184. package/lib/WABinary/jid-utils.js +80 -78
  185. package/lib/WABinary/types.d.ts +2 -0
  186. package/lib/WABinary/types.js +1 -3
  187. package/lib/WAM/BinaryInfo.d.ts +8 -0
  188. package/lib/WAM/BinaryInfo.js +7 -9
  189. package/lib/WAM/constants.d.ts +34927 -0
  190. package/lib/WAM/constants.js +22847 -22848
  191. package/lib/WAM/encode.d.ts +2 -0
  192. package/lib/WAM/encode.js +129 -134
  193. package/lib/WAM/index.d.ts +4 -0
  194. package/lib/WAM/index.js +3 -5
  195. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts +37 -0
  196. package/lib/WAUSync/Protocols/USyncContactProtocol.js +48 -27
  197. package/lib/WAUSync/Protocols/USyncDeviceProtocol.d.ts +23 -0
  198. package/lib/WAUSync/Protocols/USyncDeviceProtocol.js +48 -48
  199. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.d.ts +13 -0
  200. package/lib/WAUSync/Protocols/USyncDisappearingModeProtocol.js +23 -26
  201. package/lib/WAUSync/Protocols/USyncStatusProtocol.d.ts +13 -0
  202. package/lib/WAUSync/Protocols/USyncStatusProtocol.js +33 -35
  203. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  204. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +24 -0
  205. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.d.ts +39 -0
  206. package/lib/WAUSync/Protocols/UsyncBotProfileProtocol.js +46 -49
  207. package/lib/WAUSync/Protocols/UsyncLIDProtocol.d.ts +15 -0
  208. package/lib/WAUSync/Protocols/UsyncLIDProtocol.js +23 -25
  209. package/lib/WAUSync/Protocols/index.d.ts +6 -0
  210. package/lib/WAUSync/Protocols/index.js +5 -6
  211. package/lib/WAUSync/USyncQuery.d.ts +21 -0
  212. package/lib/WAUSync/USyncQuery.js +92 -85
  213. package/lib/WAUSync/USyncUser.d.ts +17 -0
  214. package/lib/WAUSync/USyncUser.js +28 -22
  215. package/lib/WAUSync/index.d.ts +4 -0
  216. package/lib/WAUSync/index.js +3 -5
  217. package/lib/index.d.ts +12 -0
  218. package/lib/index.js +31 -19
  219. package/package.json +12 -6
  220. package/lib/Types/Newsletter.js +0 -33
@@ -1,111 +1,120 @@
1
- //=======================================================//
2
- import { mkdir, readFile, stat, unlink, writeFile } from "fs/promises";
3
- import { initAuthCreds } from "./auth-utils.js";
4
- import { proto } from "../../WAProto/index.js";
5
- import { BufferJSON } from "./generics.js";
6
- import { Mutex } from "async-mutex";
7
- import { join } from "path";
8
- //=======================================================//
1
+ import { Mutex } from 'async-mutex';
2
+ import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises';
3
+ import { join } from 'path';
4
+ import { proto } from '../../WAProto/index.js';
5
+ import { initAuthCreds } from './auth-utils.js';
6
+ import { BufferJSON } from './generics.js';
7
+ // We need to lock files due to the fact that we are using async functions to read and write files
8
+ // https://github.com/WhiskeySockets/Baileys/issues/794
9
+ // https://github.com/nodejs/node/issues/26338
10
+ // Use a Map to store mutexes for each file path
9
11
  const fileLocks = new Map();
12
+ // Get or create a mutex for a specific file path
10
13
  const getFileLock = (path) => {
11
- let mutex = fileLocks.get(path);
12
- if (!mutex) {
13
- mutex = new Mutex();
14
- fileLocks.set(path, mutex);
15
- }
16
- return mutex;
14
+ let mutex = fileLocks.get(path);
15
+ if (!mutex) {
16
+ mutex = new Mutex();
17
+ fileLocks.set(path, mutex);
18
+ }
19
+ return mutex;
17
20
  };
18
- //=======================================================//
21
+ /**
22
+ * stores the full authentication state in a single folder.
23
+ * Far more efficient than singlefileauthstate
24
+ *
25
+ * Again, I wouldn't endorse this for any production level use other than perhaps a bot.
26
+ * Would recommend writing an auth state for use with a proper SQL or No-SQL DB
27
+ * */
19
28
  export const useMultiFileAuthState = async (folder) => {
20
- const writeData = async (data, file) => {
21
- const filePath = join(folder, fixFileName(file));
22
- const mutex = getFileLock(filePath);
23
- return mutex.acquire().then(async (release) => {
24
- try {
25
- await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
26
- }
27
- finally {
28
- release();
29
- }
30
- });
31
- };
32
- const readData = async (file) => {
33
- try {
34
- const filePath = join(folder, fixFileName(file));
35
- const mutex = getFileLock(filePath);
36
- return await mutex.acquire().then(async (release) => {
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ const writeData = async (data, file) => {
31
+ const filePath = join(folder, fixFileName(file));
32
+ const mutex = getFileLock(filePath);
33
+ return mutex.acquire().then(async (release) => {
34
+ try {
35
+ await writeFile(filePath, JSON.stringify(data, BufferJSON.replacer));
36
+ }
37
+ finally {
38
+ release();
39
+ }
40
+ });
41
+ };
42
+ const readData = async (file) => {
37
43
  try {
38
- const data = await readFile(filePath, { encoding: "utf-8" });
39
- return JSON.parse(data, BufferJSON.reviver);
44
+ const filePath = join(folder, fixFileName(file));
45
+ const mutex = getFileLock(filePath);
46
+ return await mutex.acquire().then(async (release) => {
47
+ try {
48
+ const data = await readFile(filePath, { encoding: 'utf-8' });
49
+ return JSON.parse(data, BufferJSON.reviver);
50
+ }
51
+ finally {
52
+ release();
53
+ }
54
+ });
40
55
  }
41
- finally {
42
- release();
56
+ catch (error) {
57
+ return null;
43
58
  }
44
- });
45
- }
46
- catch (error) {
47
- return null;
48
- }
49
- };
50
- const removeData = async (file) => {
51
- try {
52
- const filePath = join(folder, fixFileName(file));
53
- const mutex = getFileLock(filePath);
54
- return mutex.acquire().then(async (release) => {
59
+ };
60
+ const removeData = async (file) => {
55
61
  try {
56
- await unlink(filePath);
57
- }
58
- catch {
62
+ const filePath = join(folder, fixFileName(file));
63
+ const mutex = getFileLock(filePath);
64
+ return mutex.acquire().then(async (release) => {
65
+ try {
66
+ await unlink(filePath);
67
+ }
68
+ catch {
69
+ }
70
+ finally {
71
+ release();
72
+ }
73
+ });
59
74
  }
60
- finally {
61
- release();
75
+ catch { }
76
+ };
77
+ const folderInfo = await stat(folder).catch(() => { });
78
+ if (folderInfo) {
79
+ if (!folderInfo.isDirectory()) {
80
+ throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
62
81
  }
63
- });
64
82
  }
65
- catch { }
66
- };
67
- const folderInfo = await stat(folder).catch(() => { });
68
- if (folderInfo) {
69
- if (!folderInfo.isDirectory()) {
70
- throw new Error(`found something that is not a directory at ${folder}, either delete it or specify a different location`);
83
+ else {
84
+ await mkdir(folder, { recursive: true });
71
85
  }
72
- }
73
- else {
74
- await mkdir(folder, { recursive: true });
75
- }
76
- const fixFileName = (file) => file?.replace(/\//g, "__")?.replace(/:/g, "-");
77
- const creds = (await readData("creds.json")) || initAuthCreds();
78
- return {
79
- state: {
80
- creds,
81
- keys: {
82
- get: async (type, ids) => {
83
- const data = {};
84
- await Promise.all(ids.map(async (id) => {
85
- let value = await readData(`${type}-${id}.json`);
86
- if (type === "app-state-sync-key" && value) {
87
- value = proto.Message.AppStateSyncKeyData.fromObject(value);
86
+ const fixFileName = (file) => file?.replace(/\//g, '__')?.replace(/:/g, '-');
87
+ const creds = (await readData('creds.json')) || initAuthCreds();
88
+ return {
89
+ state: {
90
+ creds,
91
+ keys: {
92
+ get: async (type, ids) => {
93
+ const data = {};
94
+ await Promise.all(ids.map(async (id) => {
95
+ let value = await readData(`${type}-${id}.json`);
96
+ if (type === 'app-state-sync-key' && value) {
97
+ value = proto.Message.AppStateSyncKeyData.fromObject(value);
98
+ }
99
+ data[id] = value;
100
+ }));
101
+ return data;
102
+ },
103
+ set: async (data) => {
104
+ const tasks = [];
105
+ for (const category in data) {
106
+ for (const id in data[category]) {
107
+ const value = data[category][id];
108
+ const file = `${category}-${id}.json`;
109
+ tasks.push(value ? writeData(value, file) : removeData(file));
110
+ }
111
+ }
112
+ await Promise.all(tasks);
113
+ }
88
114
  }
89
- data[id] = value;
90
- }));
91
- return data;
92
115
  },
93
- set: async (data) => {
94
- const tasks = [];
95
- for (const category in data) {
96
- for (const id in data[category]) {
97
- const value = data[category][id];
98
- const file = `${category}-${id}.json`;
99
- tasks.push(value ? writeData(value, file) : removeData(file));
100
- }
101
- }
102
- await Promise.all(tasks);
116
+ saveCreds: async () => {
117
+ return writeData(creds, 'creds.json');
103
118
  }
104
- }
105
- },
106
- saveCreds: async () => {
107
- return writeData(creds, "creds.json");
108
- }
109
- };
119
+ };
110
120
  };
111
- //=======================================================//
@@ -0,0 +1,11 @@
1
+ export function useSingleFileAuthState(fileName: any): Promise<{
2
+ state: {
3
+ creds: any;
4
+ keys: {
5
+ get: (type: any, ids: any) => {};
6
+ set: (data: any) => void;
7
+ };
8
+ };
9
+ saveCreds: () => void;
10
+ }>;
11
+ //# sourceMappingURL=use-single-file-auth-state.d.ts.map
@@ -0,0 +1,108 @@
1
+ import { readFile, rename, stat, writeFile } from 'fs/promises';
2
+ import { DEFAULT_CACHE_TTLS } from '../Defaults/index.js';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { initAuthCreds } from './auth-utils.js';
5
+ import { BufferJSON } from './generics.js';
6
+ import { LRUCache } from 'lru-cache';
7
+ import { Mutex } from 'async-mutex';
8
+ // Lia@Changes 25-03-26 --- Add useSingleFileAuthState with integrated cache
9
+ const FLUSH_TIMEOUT_MS = 3000;
10
+ // Lia@Changes 22-04-26 --- Enhanced useSingleFileAuthState with LRUCache
11
+ export const useSingleFileAuthState = async (fileName) => {
12
+ const cache = new LRUCache({
13
+ max: 15000,
14
+ ttl: 1000 * DEFAULT_CACHE_TTLS.SIGNAL_STORE,
15
+ updateAgeOnGet: false,
16
+ updateAgeOnHas: false,
17
+ ttlAutopurge: true
18
+ });
19
+ // Lia@Changes 26-04-26 --- Add mutex for prevent race condition
20
+ const mutex = new Mutex();
21
+ let fileData = {};
22
+ let isLoaded = false;
23
+ let flushTimeout = null;
24
+ const loadKey = async () => {
25
+ return await mutex.runExclusive(async () => {
26
+ if (isLoaded)
27
+ return;
28
+ try {
29
+ const data = JSON.parse(await readFile(fileName, 'utf-8'), BufferJSON.reviver);
30
+ fileData = data || {};
31
+ for (const [keyName, value] of Object.entries(fileData)) {
32
+ cache.set(keyName, value);
33
+ }
34
+ }
35
+ catch {
36
+ fileData = {};
37
+ }
38
+ isLoaded = true;
39
+ });
40
+ };
41
+ const flushKey = () => {
42
+ if (flushTimeout)
43
+ return;
44
+ flushTimeout = setTimeout(async () => {
45
+ flushTimeout = null;
46
+ await mutex.runExclusive(async () => {
47
+ try {
48
+ const tempFile = fileName + '.temp';
49
+ await writeFile(tempFile, JSON.stringify(fileData, BufferJSON.replacer));
50
+ await rename(tempFile, fileName);
51
+ }
52
+ catch { }
53
+ });
54
+ }, FLUSH_TIMEOUT_MS);
55
+ };
56
+ const writeKey = (keyName, value) => {
57
+ cache.set(keyName, value);
58
+ fileData[keyName] = value;
59
+ flushKey();
60
+ };
61
+ const removeKey = (keyName) => {
62
+ cache.delete(keyName);
63
+ delete fileData[keyName];
64
+ flushKey();
65
+ };
66
+ const fileInfo = await stat(fileName).catch(() => null);
67
+ if (!fileInfo) {
68
+ await writeFile(fileName, '{}');
69
+ }
70
+ else if (!fileInfo.isFile()) {
71
+ throw new Error(`found something that is not a file at ${fileName}, either delete it or specify a different location`);
72
+ }
73
+ await loadKey();
74
+ const creds = fileData['creds'] || initAuthCreds();
75
+ return {
76
+ state: {
77
+ creds,
78
+ keys: {
79
+ get: (type, ids) => {
80
+ const data = {};
81
+ for (const id of ids) {
82
+ const keyName = type + id;
83
+ let value = cache.get(keyName);
84
+ if (value === undefined && fileData[keyName] !== undefined) {
85
+ value = fileData[keyName];
86
+ cache.set(keyName, value);
87
+ }
88
+ if (type === 'app-state-sync-key' && value) {
89
+ value = proto.Message.AppStateSyncKeyData.fromObject(value);
90
+ }
91
+ data[id] = value;
92
+ }
93
+ return data;
94
+ },
95
+ set: (data) => {
96
+ for (const category in data) {
97
+ for (const id in data[category]) {
98
+ const keyName = category + id;
99
+ const value = data[category][id];
100
+ value ? writeKey(keyName, value) : removeKey(keyName);
101
+ }
102
+ }
103
+ }
104
+ }
105
+ },
106
+ saveCreds: () => writeKey('creds', creds)
107
+ };
108
+ };
@@ -0,0 +1,11 @@
1
+ export function useSqliteAuthState(opts: any): Promise<{
2
+ state: {
3
+ creds: any;
4
+ keys: {
5
+ get: (type: any, ids: any) => Promise<{}>;
6
+ set: (data: any) => Promise<void>;
7
+ };
8
+ };
9
+ saveCreds: () => Promise<void>;
10
+ }>;
11
+ //# sourceMappingURL=use-sqlite-auth-state.d.ts.map
@@ -0,0 +1,108 @@
1
+ import { mkdir, readFile, stat, unlink, writeFile } from 'fs/promises';
2
+ import { join } from 'path';
3
+ import { proto } from '../../WAProto/index.js';
4
+ import { initAuthCreds } from './auth-utils.js';
5
+ import { BufferJSON } from './generics.js';
6
+ async function loadBetterSqlite3() {
7
+ try {
8
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
+ const mod = (await import('better-sqlite3'));
10
+ return mod.default ?? mod;
11
+ }
12
+ catch (err) {
13
+ const helpful = new Error('`better-sqlite3` is required for `useSqliteAuthState`. Install it as a peer dependency: `npm install better-sqlite3` (or `yarn add better-sqlite3`).');
14
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
+ helpful.cause = err;
16
+ throw helpful;
17
+ }
18
+ }
19
+ const CREDS_ROW_KEY = '__creds__';
20
+ const CREATE_SCHEMA_SQL = `
21
+ CREATE TABLE IF NOT EXISTS creds (
22
+ key TEXT PRIMARY KEY,
23
+ value TEXT NOT NULL
24
+ );
25
+ CREATE TABLE IF NOT EXISTS signal_keys (
26
+ type TEXT NOT NULL,
27
+ id TEXT NOT NULL,
28
+ value TEXT NOT NULL,
29
+ PRIMARY KEY (type, id)
30
+ );
31
+ CREATE INDEX IF NOT EXISTS signal_keys_type_idx ON signal_keys(type);
32
+ `;
33
+ export async function useSqliteAuthState(opts) {
34
+ let db;
35
+ if (opts.database) {
36
+ db = opts.database;
37
+ }
38
+ else {
39
+ const Database = await loadBetterSqlite3();
40
+ db = new Database(opts.dbPath);
41
+ }
42
+ // WAL mode allows concurrent reads alongside a single writer; matches
43
+ // what SQLite recommends for read-heavy workloads with sporadic writes.
44
+ db.pragma('journal_mode = WAL');
45
+ db.pragma('synchronous = NORMAL');
46
+ db.exec(CREATE_SCHEMA_SQL);
47
+ const stmts = {
48
+ credsSelect: db.prepare('SELECT value FROM creds WHERE key = ?'),
49
+ credsUpsert: db.prepare('INSERT INTO creds (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value'),
50
+ keySelect: db.prepare('SELECT value FROM signal_keys WHERE type = ? AND id = ?'),
51
+ keyUpsert: db.prepare('INSERT INTO signal_keys (type, id, value) VALUES (?, ?, ?) ON CONFLICT(type, id) DO UPDATE SET value = excluded.value'),
52
+ keyDelete: db.prepare('DELETE FROM signal_keys WHERE type = ? AND id = ?'),
53
+ keyListIds: db.prepare('SELECT id FROM signal_keys WHERE type = ?'),
54
+ keyList: db.prepare('SELECT id, value FROM signal_keys WHERE type = ?'),
55
+ clearKeys: db.prepare('DELETE FROM signal_keys')
56
+ };
57
+ const loadCreds = () => {
58
+ const row = stmts.credsSelect.get(CREDS_ROW_KEY);
59
+ if (!row)
60
+ return initAuthCreds();
61
+ return JSON.parse(row.value, BufferJSON.reviver);
62
+ };
63
+ const persistCreds = (creds) => {
64
+ stmts.credsUpsert.run(CREDS_ROW_KEY, JSON.stringify(creds, BufferJSON.replacer));
65
+ };
66
+ const creds = loadCreds();
67
+ return {
68
+ state: {
69
+ creds,
70
+ keys: {
71
+ get: async (type, ids) => {
72
+ const data = {};
73
+ for (const id of ids) {
74
+ const row = stmts.keySelect.get(type, id);
75
+ if (row) {
76
+ let value = JSON.parse(row.value, BufferJSON.reviver);
77
+ if (type === 'app-state-sync-key' && value) {
78
+ value = proto.Message.AppStateSyncKeyData.fromObject(value);
79
+ }
80
+ data[id] = value;
81
+ }
82
+ }
83
+ return data;
84
+ },
85
+ set: async (data) => {
86
+ const writeTx = db.transaction(() => {
87
+ for (const category in data) {
88
+ for (const id in data[category]) {
89
+ const value = data[category][id];
90
+ if (value) {
91
+ const stringified = JSON.stringify(value, BufferJSON.replacer);
92
+ stmts.keyUpsert.run(category, id, stringified);
93
+ }
94
+ else {
95
+ stmts.keyDelete.run(category, id);
96
+ }
97
+ }
98
+ }
99
+ });
100
+ writeTx();
101
+ }
102
+ }
103
+ },
104
+ saveCreds: async () => {
105
+ persistCreds(creds);
106
+ }
107
+ };
108
+ }
@@ -0,0 +1,44 @@
1
+ export function generateLoginNode(userJid: any, config: any): proto.ClientPayload;
2
+ export function generateRegistrationNode({ registrationId, signedPreKey, signedIdentityKey }: {
3
+ registrationId: any;
4
+ signedPreKey: any;
5
+ signedIdentityKey: any;
6
+ }, config: any): proto.ClientPayload;
7
+ export function configureSuccessfulPairing(stanza: any, { advSecretKey, signedIdentityKey, signalIdentities }: {
8
+ advSecretKey: any;
9
+ signedIdentityKey: any;
10
+ signalIdentities: any;
11
+ }): {
12
+ creds: {
13
+ account: proto.ADVSignedDeviceIdentity;
14
+ me: {
15
+ id: any;
16
+ name: any;
17
+ lid: any;
18
+ };
19
+ signalIdentities: any[];
20
+ platform: any;
21
+ };
22
+ reply: {
23
+ tag: string;
24
+ attrs: {
25
+ to: string;
26
+ type: string;
27
+ id: any;
28
+ };
29
+ content: {
30
+ tag: string;
31
+ attrs: {};
32
+ content: {
33
+ tag: string;
34
+ attrs: {
35
+ 'key-index': string;
36
+ };
37
+ content: any;
38
+ }[];
39
+ }[];
40
+ };
41
+ };
42
+ export function encodeSignedDeviceIdentity(account: any, includeSignatureKey: any): any;
43
+ import { proto } from '../../WAProto/index.js';
44
+ //# sourceMappingURL=validate-connection.d.ts.map