@rexxhayanasi/elaina-baileys 1.0.9 → 1.1.0-rc.2

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.
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.useSQLiteAuth = void 0;
7
+ const sqlite_1 = __importDefault(require("./sqlite"));
8
+ exports.useSQLiteAuth = sqlite_1.default;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.useSQLiteAuth = void 0;
5
+
6
+ const better_sqlite3_1 = require("better-sqlite3");
7
+ const baileys_1 = require("../index");
8
+
9
+ const BetterSqlite = better_sqlite3_1.default || better_sqlite3_1;
10
+ const db = new BetterSqlite("./session.db");
11
+
12
+ db.exec(`
13
+ CREATE TABLE IF NOT EXISTS auth (
14
+ id TEXT PRIMARY KEY,
15
+ data TEXT
16
+ )
17
+ `);
18
+
19
+ const useSQLiteAuth = () => {
20
+ const readData = (id) => {
21
+ const row = db.prepare("SELECT data FROM auth WHERE id=?").get(id);
22
+ return row ? JSON.parse(row.data, baileys_1.BufferJSON.reviver) : null;
23
+ };
24
+
25
+ const writeData = (id, data) => {
26
+ db.prepare("INSERT OR REPLACE INTO auth VALUES (?, ?)")
27
+ .run(id, JSON.stringify(data, baileys_1.BufferJSON.replacer));
28
+ };
29
+
30
+ const state = {
31
+ creds: readData("creds") || {},
32
+ keys: {
33
+ get(type, ids) {
34
+ const data = {};
35
+ for (const id of ids) {
36
+ const v = readData(`${type}-${id}`);
37
+ if (v)
38
+ data[id] = v;
39
+ }
40
+ return data;
41
+ },
42
+ set(data) {
43
+ for (const type in data) {
44
+ for (const id in data[type]) {
45
+ writeData(`${type}-${id}`, data[type][id]);
46
+ }
47
+ }
48
+ }
49
+ }
50
+ };
51
+
52
+ const saveCreds = () => writeData("creds", state.creds);
53
+
54
+ return { state, saveCreds };
55
+ };
56
+
57
+ exports.useSQLiteAuth = useSQLiteAuth;
58
+ exports.default = useSQLiteAuth;
@@ -6,7 +6,12 @@ const WABinary_1 = require("../WABinary");
6
6
  const generic_utils_1 = require("../WABinary/generic-utils");
7
7
  const messages_recv_1 = require("./messages-recv");
8
8
  const makeBusinessSocket = (config) => {
9
- const sock = (0, messages_recv_1.makeMessagesRecvSocket)(config);
9
+ const sock = (0, messages_recv_1.makeMessagesRecvSocket)({
10
+ ...config,
11
+ forceNewConnection: true,
12
+ syncFullHistory: false,
13
+ shouldIgnoreJid: () => false
14
+ });
10
15
  const { authState, query, waUploadToServer } = sock;
11
16
  const getCatalog = async ({ jid, limit, cursor }) => {
12
17
  var _a;
@@ -1,10 +1,36 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const Defaults_1 = require("../Defaults");
4
- const business_1 = require("./business");
5
- // export the last socket layer
6
- const makeWASocket = (config) => ((0, business_1.makeBusinessSocket)({
7
- ...Defaults_1.DEFAULT_CONNECTION_CONFIG,
1
+ "use strict"
2
+
3
+ Object.defineProperty(exports, "__esModule", { value: true })
4
+
5
+ const { DEFAULT_CONNECTION_CONFIG } = require("../Defaults")
6
+ const { makeBusinessSocket } = require("./business")
7
+
8
+ let __ACTIVE_SOCKET__ = null
9
+
10
+ const makeWASocket = (config) => {
11
+ try {
12
+ if (__ACTIVE_SOCKET__) {
13
+ try {
14
+ __ACTIVE_SOCKET__.ev?.removeAllListeners?.()
15
+ __ACTIVE_SOCKET__.ws?.removeAllListeners?.()
16
+ __ACTIVE_SOCKET__.ws?.terminate?.()
17
+ __ACTIVE_SOCKET__.ws?.close?.()
18
+ } catch {}
19
+
20
+ try {
21
+ __ACTIVE_SOCKET__ = null
22
+ } catch {}
23
+ }
24
+ } catch {}
25
+
26
+ const sock = makeBusinessSocket({
27
+ ...DEFAULT_CONNECTION_CONFIG,
8
28
  ...config
9
- }));
10
- exports.default = makeWASocket;
29
+ })
30
+
31
+ __ACTIVE_SOCKET__ = sock
32
+
33
+ return sock
34
+ }
35
+
36
+ exports.default = makeWASocket
@@ -261,34 +261,46 @@ const makeSocket = (config) => {
261
261
  }
262
262
  });
263
263
  };
264
- const end = (error) => {
265
- if (closed) {
266
- logger.trace({ trace: error === null || error === void 0 ? void 0 : error.stack }, 'connection already closed');
267
- return;
268
- }
269
- closed = true;
270
- logger.info({ trace: error === null || error === void 0 ? void 0 : error.stack }, error ? 'connection errored' : 'connection closed');
271
- clearInterval(keepAliveReq);
272
- clearTimeout(qrTimer);
273
- ws.removeAllListeners('close');
274
- ws.removeAllListeners('error');
275
- ws.removeAllListeners('open');
276
- ws.removeAllListeners('message');
277
- if (!ws.isClosed && !ws.isClosing) {
278
- try {
279
- ws.close();
280
- }
281
- catch (_a) { }
282
- }
264
+ const end = (error) => {
265
+ if (closed) return
266
+ closed = true
267
+
268
+ try { clearInterval(keepAliveReq) } catch {}
269
+ try { clearTimeout(qrTimer) } catch {}
270
+
271
+ try {
272
+ ws.off('message', onMessageReceived)
273
+ ws.removeAllListeners?.()
274
+ } catch {}
275
+
276
+ try {
277
+ if (ws.terminate) {
278
+ ws.terminate()
279
+ } else {
280
+ ws.close()
281
+ }
282
+ } catch {}
283
+
284
+ try {
283
285
  ev.emit('connection.update', {
284
286
  connection: 'close',
285
287
  lastDisconnect: {
286
288
  error,
287
289
  date: new Date()
288
290
  }
289
- });
290
- ev.removeAllListeners('connection.update');
291
- };
291
+ })
292
+ } catch {}
293
+
294
+ try { ev.flush?.() } catch {}
295
+ try { ev.removeAllListeners?.() } catch {}
296
+
297
+ try {
298
+ if (authState?.creds?.isConnecting) {
299
+ authState.creds.isConnecting = false
300
+ ev.emit?.('creds.update', authState.creds)
301
+ }
302
+ } catch {}
303
+ };
292
304
  const waitForSocketOpen = async () => {
293
305
  if (ws.isOpen) {
294
306
  return;
@@ -443,12 +455,31 @@ const makeSocket = (config) => {
443
455
  return authState.creds.pairingCode;
444
456
  };
445
457
  async function generatePairingKey() {
446
- const salt = (0, crypto_1.randomBytes)(32);
447
- const randomIv = (0, crypto_1.randomBytes)(16);
448
- const key = await (0, Utils_1.derivePairingCodeKey)(authState.creds.pairingCode, salt);
449
- const ciphered = (0, Utils_1.aesEncryptCTR)(authState.creds.pairingEphemeralKeyPair.public, key, randomIv);
450
- return Buffer.concat([salt, randomIv, ciphered]);
458
+ if (!authState?.creds?.pairingEphemeralKeyPair) {
459
+ authState.creds.pairingEphemeralKeyPair = require("../Utils").Curve.generateKeyPair()
460
+ ev.emit('creds.update', authState.creds)
451
461
  }
462
+
463
+ if (!authState?.creds?.pairingCode) {
464
+ throw new Error("Pairing code belum tersedia")
465
+ }
466
+
467
+ const salt = (0, crypto_1.randomBytes)(32)
468
+ const randomIv = (0, crypto_1.randomBytes)(16)
469
+
470
+ const key = await (0, Utils_1.derivePairingCodeKey)(
471
+ authState.creds.pairingCode,
472
+ salt
473
+ )
474
+
475
+ const ciphered = (0, Utils_1.aesEncryptCTR)(
476
+ authState.creds.pairingEphemeralKeyPair.public,
477
+ key,
478
+ randomIv
479
+ )
480
+
481
+ return Buffer.concat([salt, randomIv, ciphered])
482
+ }
452
483
  const sendWAMBuffer = (wamBuffer) => {
453
484
  return query({
454
485
  tag: 'iq',
package/lib/index.js CHANGED
@@ -53,5 +53,6 @@ __exportStar(require("./WABinary"), exports);
53
53
  __exportStar(require("./WAM"), exports);
54
54
  __exportStar(require("./WAUSync"), exports);
55
55
  __exportStar(require("./Api"), exports);
56
+ __exportStar(require("./Auth"), exports);
56
57
 
57
58
  exports.default = Socket_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexxhayanasi/elaina-baileys",
3
- "version": "1.0.9",
3
+ "version": "1.1.0-rc.2",
4
4
  "description": "Custom Baileys WhatsApp API",
5
5
  "keywords": [
6
6
  "baileys",
@@ -47,6 +47,7 @@
47
47
  "@hapi/boom": "^9.1.3",
48
48
  "async-mutex": "^0.5.0",
49
49
  "axios": "^1.6.0",
50
+ "better-sqlite3": "^12.5.0",
50
51
  "chalk": "^4.1.2",
51
52
  "gradient-string": "^2.0.2",
52
53
  "cache-manager": "^5.7.6",
@@ -70,7 +71,7 @@
70
71
  "conventional-changelog-cli": "^2.2.2",
71
72
  "eslint": "^8.0.0",
72
73
  "jest": "^27.0.6",
73
- "jimp": "^0.16.1",
74
+ "jimp": "^1.6.0",
74
75
  "json": "^11.0.0",
75
76
  "link-preview-js": "^3.0.0",
76
77
  "open": "^8.4.2",
@@ -84,7 +85,7 @@
84
85
  },
85
86
  "peerDependencies": {
86
87
  "audio-decode": "^2.1.3",
87
- "jimp": "^0.16.1",
88
+ "jimp": "^1.6.0",
88
89
  "link-preview-js": "^3.0.0",
89
90
  "qrcode-terminal": "^0.12.0",
90
91
  "sharp": "^0.34.1"