libzapitu 1.0.0-alpha.21 → 1.0.0-alpha.23

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.
@@ -203,12 +203,15 @@ function createSocket(socketId, rawConfig) {
203
203
  /* best-effort */
204
204
  }
205
205
  };
206
- // Send immediately (may be empty) and also on connection.open
206
+ // Send immediately (may be empty) and also on connection.open.
207
+ // IMPORTANT: we send socket-info synchronously in the connection.update
208
+ // listener so it arrives at the parent BEFORE the forwarded event.
209
+ // Otherwise wsocket.user would be undefined when the parent's
210
+ // connection.update handler runs.
207
211
  sendSocketInfo();
208
212
  sock.ev.on('connection.update', (update) => {
209
213
  if ((update === null || update === void 0 ? void 0 : update.connection) === 'open') {
210
- // Delay slightly so sock.user is populated
211
- setTimeout(sendSocketInfo, 100);
214
+ sendSocketInfo();
212
215
  }
213
216
  // When the socket closes, clean up this socket entry so a new
214
217
  // init with a different socketId can reuse this worker.
@@ -1,4 +1,7 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.makeWASocket = void 0;
4
7
  exports.getStats = getStats;
@@ -28,6 +31,7 @@ const events_1 = require("events");
28
31
  const os_1 = require("os");
29
32
  const fs_1 = require("fs");
30
33
  const path_1 = require("path");
34
+ const long_1 = __importDefault(require("long"));
31
35
  // ---------------------------------------------------------------------------
32
36
  // Types
33
37
  // ---------------------------------------------------------------------------
@@ -86,11 +90,13 @@ function createNoopLogger() {
86
90
  }
87
91
  /**
88
92
  * Recursively revive objects that were sanitized by the child's
89
- * sanitizeForPostMessage (e.g. Error sentinels).
93
+ * sanitizeForPostMessage (e.g. Error sentinels) or decomposed by
94
+ * structured clone (e.g. Long → { low, high, unsigned }).
90
95
  *
91
96
  * IMPORTANT: structured clone already produces a perfect copy of the data.
92
97
  * We only need to recursively look for __error__ sentinels and convert
93
- * them back to Error instances. Everything else passes through unchanged.
98
+ * them back to Error instances, and revive Long-like plain objects back
99
+ * to real Long instances. Everything else passes through unchanged.
94
100
  */
95
101
  function revivePostMessage(value) {
96
102
  if (value !== null && typeof value === 'object') {
@@ -100,19 +106,25 @@ function revivePostMessage(value) {
100
106
  err.stack = value.stack;
101
107
  return err;
102
108
  }
109
+ // Revive Long-like objects that were decomposed by structured clone.
110
+ // protobufjs uses Long for 64-bit integers; after postMessage they
111
+ // become { low, high, unsigned } plain objects.
112
+ if (isLongLike(value)) {
113
+ return new long_1.default(value.low, value.high, value.unsigned);
114
+ }
103
115
  if (Array.isArray(value)) {
104
- // Arrays: recurse to revive any Error sentinels inside
105
- let hasError = false;
116
+ // Arrays: recurse to revive any Error sentinels / Longs inside
117
+ let changed = false;
106
118
  const result = value.map(v => {
107
119
  const revived = revivePostMessage(v);
108
120
  if (revived !== v)
109
- hasError = true;
121
+ changed = true;
110
122
  return revived;
111
123
  });
112
- return hasError ? result : value;
124
+ return changed ? result : value;
113
125
  }
114
- // Plain objects: only recurse if they might contain __error__ sentinels.
115
- // Check one level deep to avoid unnecessary cloning of large objects.
126
+ // Plain objects: only recurse if they might contain __error__ sentinels
127
+ // or Long-like objects.
116
128
  if (value.constructor === Object || value.constructor === undefined) {
117
129
  let changed = false;
118
130
  const result = {};
@@ -128,6 +140,23 @@ function revivePostMessage(value) {
128
140
  }
129
141
  return value;
130
142
  }
143
+ /**
144
+ * Check whether a value looks like a protobufjs Long that was decomposed
145
+ * by structured clone. Long instances have exactly { low, high, unsigned }
146
+ * after crossing postMessage.
147
+ */
148
+ function isLongLike(value) {
149
+ if (value === null || typeof value !== 'object')
150
+ return false;
151
+ const keys = Object.keys(value);
152
+ return (keys.length === 3 &&
153
+ keys.includes('low') &&
154
+ keys.includes('high') &&
155
+ keys.includes('unsigned') &&
156
+ typeof value.low === 'number' &&
157
+ typeof value.high === 'number' &&
158
+ typeof value.unsigned === 'boolean');
159
+ }
131
160
  // ---------------------------------------------------------------------------
132
161
  // Pool management
133
162
  // ---------------------------------------------------------------------------
@@ -180,7 +209,7 @@ function spawnWorker() {
180
209
  };
181
210
  // Global message handler — dispatches to the right socket's emitter
182
211
  worker.on('message', (msg) => {
183
- var _a, _b, _c;
212
+ var _a, _b, _c, _d;
184
213
  if (!msg || typeof msg !== 'object')
185
214
  return;
186
215
  const socketId = msg.socketId;
@@ -201,6 +230,17 @@ function spawnWorker() {
201
230
  Object.assign(cfg.auth.creds, map['creds.update']);
202
231
  log === null || log === void 0 ? void 0 : log.debug({ me: (_b = cfg.auth.creds.me) === null || _b === void 0 ? void 0 : _b.id }, 'synced creds.update to parent');
203
232
  }
233
+ // Also update localProps.user so wsocket.user and its
234
+ // nested properties (id, lid, name, verifiedName, etc.)
235
+ // stay in sync with the worker's authState.creds.me.
236
+ const credsUpdate = map['creds.update'];
237
+ if (credsUpdate === null || credsUpdate === void 0 ? void 0 : credsUpdate.me) {
238
+ const props = slot.props.get(socketId);
239
+ if (props) {
240
+ props.user = { ...props.user, ...credsUpdate.me };
241
+ log === null || log === void 0 ? void 0 : log.debug({ me: (_c = credsUpdate.me) === null || _c === void 0 ? void 0 : _c.id }, 'synced user to localProps');
242
+ }
243
+ }
204
244
  }
205
245
  // Emit the aggregated 'event' for ev.process() compatibility
206
246
  ev.emit('event', map);
@@ -211,7 +251,7 @@ function spawnWorker() {
211
251
  // When the socket closes, clean up proxy-side resources.
212
252
  // The worker already removed its entry; we remove the
213
253
  // emitter so a future startSock() can create a fresh one.
214
- if (((_c = map['connection.update']) === null || _c === void 0 ? void 0 : _c.connection) === 'close') {
254
+ if (((_d = map['connection.update']) === null || _d === void 0 ? void 0 : _d.connection) === 'close') {
215
255
  cleanupSocket(slot, socketId, log || createNoopLogger());
216
256
  }
217
257
  }
@@ -244,7 +284,7 @@ function spawnWorker() {
244
284
  }
245
285
  else {
246
286
  log === null || log === void 0 ? void 0 : log.trace({ rpcId: msg.id }, 'worker RPC result');
247
- p.resolve(msg.result);
287
+ p.resolve(revivePostMessage(msg.result));
248
288
  }
249
289
  }
250
290
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libzapitu",
3
- "version": "1.0.0-alpha.21",
3
+ "version": "1.0.0-alpha.23",
4
4
  "description": "A WebSockets library for interacting with WhatsApp Web",
5
5
  "keywords": [
6
6
  "whatsapp",