libzapitu 1.0.0-alpha.22 → 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.
- package/lib/worker/proxy.js +38 -9
- package/package.json +1 -1
package/lib/worker/proxy.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
-
|
|
121
|
+
changed = true;
|
|
110
122
|
return revived;
|
|
111
123
|
});
|
|
112
|
-
return
|
|
124
|
+
return changed ? result : value;
|
|
113
125
|
}
|
|
114
|
-
// Plain objects: only recurse if they might contain __error__ sentinels
|
|
115
|
-
//
|
|
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
|
// ---------------------------------------------------------------------------
|
|
@@ -255,7 +284,7 @@ function spawnWorker() {
|
|
|
255
284
|
}
|
|
256
285
|
else {
|
|
257
286
|
log === null || log === void 0 ? void 0 : log.trace({ rpcId: msg.id }, 'worker RPC result');
|
|
258
|
-
p.resolve(msg.result);
|
|
287
|
+
p.resolve(revivePostMessage(msg.result));
|
|
259
288
|
}
|
|
260
289
|
}
|
|
261
290
|
break;
|