@wppconnect-team/wppconnect 2.0.0 → 2.0.1
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/CHANGELOG.md +6 -0
- package/dist/api/helpers/base64-mimetype.js +2 -2
- package/dist/api/helpers/decrypt.js +65 -72
- package/dist/api/helpers/download-file.js +31 -81
- package/dist/api/helpers/evaluate-and-return.js +98 -93
- package/dist/api/helpers/file-to-base64.js +30 -81
- package/dist/api/helpers/filename-from-mimetype.js +4 -4
- package/dist/api/helpers/scrape-img-qr.js +37 -86
- package/dist/api/helpers/scrape-login.js +12 -57
- package/dist/api/helpers/select-sticker.js +35 -100
- package/dist/api/layers/business.layer.js +29 -134
- package/dist/api/layers/catalog.layer.js +59 -210
- package/dist/api/layers/community.layer.js +29 -120
- package/dist/api/layers/controls.layer.js +62 -262
- package/dist/api/layers/group.layer.js +126 -391
- package/dist/api/layers/host.layer.js +348 -702
- package/dist/api/layers/labels.layer.js +35 -131
- package/dist/api/layers/listener.layer.js +386 -504
- package/dist/api/layers/newsletter.layer.js +20 -89
- package/dist/api/layers/profile.layer.js +73 -184
- package/dist/api/layers/retriever.layer.js +161 -468
- package/dist/api/layers/sender.layer.js +554 -1094
- package/dist/api/layers/status.layer.js +80 -179
- package/dist/api/layers/ui.layer.js +32 -129
- package/dist/api/model/enum/definitions.js +1 -1
- package/dist/api/whatsapp.js +200 -437
- package/dist/config/WAuserAgente.js +1 -1
- package/dist/config/create-config.js +1 -1
- package/dist/config/puppeteer.config.js +1 -1
- package/dist/controllers/auth.js +42 -110
- package/dist/controllers/browser.js +251 -411
- package/dist/controllers/initializer.js +171 -265
- package/dist/controllers/websocket.js +29 -28
- package/dist/controllers/welcome.js +38 -88
- package/dist/token-store/fileTokenStore.js +83 -173
- package/dist/token-store/isValidSessionToken.js +2 -2
- package/dist/token-store/isValidTokenStore.js +2 -2
- package/dist/token-store/memoryTokenStore.js +11 -14
- package/dist/utils/ffmpeg.js +75 -131
- package/dist/utils/logger.js +5 -5
- package/dist/utils/semver.js +6 -6
- package/dist/utils/sleep.js +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
## 2.0.1 (2026-05-04)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
- tsconfig version to support spread operators natively and also fix the backendEvents ([#2764](https://github.com/wppconnect-team/wppconnect/issues/2764)) ([415926e](https://github.com/wppconnect-team/wppconnect/commit/415926e848c1717b91123debe9f6ce829b33280a))
|
|
6
|
+
|
|
1
7
|
# 2.0.0 (2026-04-30)
|
|
2
8
|
|
|
3
9
|
### Features
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
19
|
exports.base64MimeType = base64MimeType;
|
|
20
20
|
function base64MimeType(encoded) {
|
|
21
|
-
|
|
21
|
+
let result = null;
|
|
22
22
|
if (typeof encoded !== 'string') {
|
|
23
23
|
return result;
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
const mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);
|
|
26
26
|
if (mime && mime.length) {
|
|
27
27
|
result = mime[1];
|
|
28
28
|
}
|
|
@@ -53,27 +53,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
53
53
|
};
|
|
54
54
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
55
|
exports.newMagix = exports.magix = exports.mediaTypes = exports.timeout = exports.makeOptions = void 0;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
};
|
|
56
|
+
const crypto = __importStar(require("crypto"));
|
|
57
|
+
const futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
|
|
58
|
+
const atob_1 = __importDefault(require("atob"));
|
|
59
|
+
const stream_1 = require("stream");
|
|
60
|
+
const makeOptions = (useragentOverride, responseType = 'arraybuffer') => ({
|
|
61
|
+
responseType: responseType,
|
|
62
|
+
headers: {
|
|
63
|
+
'User-Agent': processUA(useragentOverride),
|
|
64
|
+
DNT: '1',
|
|
65
|
+
'Upgrade-Insecure-Requests': '1',
|
|
66
|
+
origin: 'https://web.whatsapp.com/',
|
|
67
|
+
referer: 'https://web.whatsapp.com/',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
73
70
|
exports.makeOptions = makeOptions;
|
|
74
|
-
|
|
75
|
-
return new Promise(function (res) { return setTimeout(res, ms); });
|
|
76
|
-
};
|
|
71
|
+
const timeout = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
77
72
|
exports.timeout = timeout;
|
|
78
73
|
exports.mediaTypes = {
|
|
79
74
|
IMAGE: 'Image',
|
|
@@ -83,38 +78,38 @@ exports.mediaTypes = {
|
|
|
83
78
|
DOCUMENT: 'Document',
|
|
84
79
|
STICKER: 'Image',
|
|
85
80
|
};
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
const processUA = (userAgent) => {
|
|
82
|
+
let ua = userAgent ||
|
|
88
83
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36';
|
|
89
84
|
if (!ua.includes('WhatsApp'))
|
|
90
85
|
ua = 'WhatsApp/2.16.352 ' + ua;
|
|
91
86
|
return ua;
|
|
92
87
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
salt
|
|
103
|
-
info
|
|
104
|
-
hash
|
|
88
|
+
const magix = (fileData, mediaKeyBase64, mediaType, expectedSize) => {
|
|
89
|
+
const encodedHex = fileData.toString('hex');
|
|
90
|
+
const encodedBytes = hexToBytes(encodedHex);
|
|
91
|
+
const mediaKeyBytes = base64ToBytes(mediaKeyBase64);
|
|
92
|
+
const info = `WhatsApp ${exports.mediaTypes[mediaType.toUpperCase()]} Keys`;
|
|
93
|
+
const hash = 'sha256';
|
|
94
|
+
const salt = new Uint8Array(32);
|
|
95
|
+
const expandedSize = 112;
|
|
96
|
+
const mediaKeyExpanded = (0, futoin_hkdf_1.default)(mediaKeyBytes, expandedSize, {
|
|
97
|
+
salt,
|
|
98
|
+
info,
|
|
99
|
+
hash,
|
|
105
100
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
101
|
+
const iv = mediaKeyExpanded.slice(0, 16);
|
|
102
|
+
const cipherKey = mediaKeyExpanded.slice(16, 48);
|
|
103
|
+
const decipher = crypto.createDecipheriv('aes-256-cbc', cipherKey, iv);
|
|
104
|
+
const decoded = decipher.update(encodedBytes);
|
|
105
|
+
const mediaDataBuffer = expectedSize
|
|
111
106
|
? fixPadding(decoded, expectedSize)
|
|
112
107
|
: decoded;
|
|
113
108
|
return mediaDataBuffer;
|
|
114
109
|
};
|
|
115
110
|
exports.magix = magix;
|
|
116
|
-
|
|
117
|
-
|
|
111
|
+
const fixPadding = (data, expectedSize) => {
|
|
112
|
+
let padding = (16 - (expectedSize % 16)) & 0xf;
|
|
118
113
|
if (padding > 0) {
|
|
119
114
|
if (expectedSize + padding == data.length) {
|
|
120
115
|
// console.log(`trimmed: ${padding} bytes`);
|
|
@@ -122,51 +117,51 @@ var fixPadding = function (data, expectedSize) {
|
|
|
122
117
|
}
|
|
123
118
|
else if (data.length + padding == expectedSize) {
|
|
124
119
|
// console.log(`adding: ${padding} bytes`);
|
|
125
|
-
|
|
120
|
+
let arr = new Uint16Array(padding).map((b) => padding);
|
|
126
121
|
data = Buffer.concat([data, Buffer.from(arr)]);
|
|
127
122
|
}
|
|
128
123
|
}
|
|
129
124
|
//@ts-ignore
|
|
130
125
|
return Buffer.from(data, 'utf-8');
|
|
131
126
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
for (
|
|
127
|
+
const hexToBytes = (hexStr) => {
|
|
128
|
+
const intArray = [];
|
|
129
|
+
for (let i = 0; i < hexStr.length; i += 2) {
|
|
135
130
|
intArray.push(parseInt(hexStr.substr(i, 2), 16));
|
|
136
131
|
}
|
|
137
132
|
return new Uint8Array(intArray);
|
|
138
133
|
};
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
for (
|
|
134
|
+
const base64ToBytes = (base64Str) => {
|
|
135
|
+
const binaryStr = (0, atob_1.default)(base64Str);
|
|
136
|
+
const byteArray = new Uint8Array(binaryStr.length);
|
|
137
|
+
for (let i = 0; i < binaryStr.length; i++) {
|
|
143
138
|
byteArray[i] = binaryStr.charCodeAt(i);
|
|
144
139
|
}
|
|
145
140
|
return byteArray;
|
|
146
141
|
};
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
salt
|
|
155
|
-
info
|
|
156
|
-
hash
|
|
142
|
+
const newMagix = (mediaKeyBase64, mediaType, expectedSize) => {
|
|
143
|
+
const mediaKeyBytes = newBase64ToBytes(mediaKeyBase64);
|
|
144
|
+
const info = `WhatsApp ${exports.mediaTypes[mediaType.toUpperCase()]} Keys`;
|
|
145
|
+
const hash = 'sha256';
|
|
146
|
+
const salt = Buffer.alloc(32);
|
|
147
|
+
const expandedSize = 112;
|
|
148
|
+
const mediaKeyExpanded = (0, futoin_hkdf_1.default)(mediaKeyBytes, expandedSize, {
|
|
149
|
+
salt,
|
|
150
|
+
info,
|
|
151
|
+
hash,
|
|
157
152
|
});
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
transform
|
|
153
|
+
const iv = mediaKeyExpanded.slice(0, 16);
|
|
154
|
+
const cipherKey = mediaKeyExpanded.slice(16, 48);
|
|
155
|
+
const decipher = crypto.createDecipheriv('aes-256-cbc', cipherKey, iv);
|
|
156
|
+
let processedBytes = 0;
|
|
157
|
+
let buffer = Buffer.alloc(0);
|
|
158
|
+
const transformStream = new stream_1.Transform({
|
|
159
|
+
transform(chunk, encoding, callback) {
|
|
165
160
|
try {
|
|
166
|
-
|
|
161
|
+
const decryptedChunk = decipher.update(chunk);
|
|
167
162
|
processedBytes += decryptedChunk.length;
|
|
168
163
|
if (processedBytes > expectedSize) {
|
|
169
|
-
|
|
164
|
+
const paddedChunk = Buffer.from(decryptedChunk).slice(0, buffer.length - (processedBytes - expectedSize));
|
|
170
165
|
callback(null, paddedChunk);
|
|
171
166
|
}
|
|
172
167
|
else {
|
|
@@ -178,12 +173,10 @@ var newMagix = function (mediaKeyBase64, mediaType, expectedSize) {
|
|
|
178
173
|
}
|
|
179
174
|
},
|
|
180
175
|
});
|
|
181
|
-
transformStream.on('error',
|
|
176
|
+
transformStream.on('error', (error) => {
|
|
182
177
|
console.error('Error during decryption:', error);
|
|
183
178
|
});
|
|
184
179
|
return transformStream;
|
|
185
180
|
};
|
|
186
181
|
exports.newMagix = newMagix;
|
|
187
|
-
|
|
188
|
-
return Buffer.from(base64Str, 'base64');
|
|
189
|
-
};
|
|
182
|
+
const newBase64ToBytes = (base64Str) => Buffer.from(base64Str, 'base64');
|
|
@@ -15,91 +15,41 @@
|
|
|
15
15
|
* You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
19
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
21
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
22
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
23
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
28
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
29
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
30
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
31
|
-
function step(op) {
|
|
32
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
33
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
34
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
35
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
36
|
-
switch (op[0]) {
|
|
37
|
-
case 0: case 1: t = op; break;
|
|
38
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
39
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
40
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
41
|
-
default:
|
|
42
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
43
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
44
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
45
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
46
|
-
if (t[2]) _.ops.pop();
|
|
47
|
-
_.trys.pop(); continue;
|
|
48
|
-
}
|
|
49
|
-
op = body.call(thisArg, _);
|
|
50
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
51
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
18
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
55
19
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
56
20
|
};
|
|
57
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
22
|
exports.downloadFileToBase64 = downloadFileToBase64;
|
|
59
|
-
|
|
60
|
-
function downloadFileToBase64(
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!reHttp.test(_path)) {
|
|
73
|
-
return [2 /*return*/, false];
|
|
74
|
-
}
|
|
75
|
-
_a.label = 1;
|
|
76
|
-
case 1:
|
|
77
|
-
_a.trys.push([1, 3, , 4]);
|
|
78
|
-
return [4 /*yield*/, axios_1.default.get(_path, {
|
|
79
|
-
responseType: 'arraybuffer',
|
|
80
|
-
})];
|
|
81
|
-
case 2:
|
|
82
|
-
response = _a.sent();
|
|
83
|
-
mimeType_1 = response.headers['content-type'];
|
|
84
|
-
if (_mines.length) {
|
|
85
|
-
isValidMime = _mines.some(function (m) {
|
|
86
|
-
if (typeof m === 'string') {
|
|
87
|
-
return m === mimeType_1;
|
|
88
|
-
}
|
|
89
|
-
return m.exec(mimeType_1);
|
|
90
|
-
});
|
|
91
|
-
if (!isValidMime) {
|
|
92
|
-
console.error("Content-Type \"".concat(mimeType_1, "\" of ").concat(_path, " is not allowed"));
|
|
93
|
-
return [2 /*return*/, false];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
content = Buffer.from(response.data, 'binary').toString('base64');
|
|
97
|
-
return [2 /*return*/, "data:".concat(mimeType_1, ";base64,").concat(content)];
|
|
98
|
-
case 3:
|
|
99
|
-
error_1 = _a.sent();
|
|
100
|
-
return [3 /*break*/, 4];
|
|
101
|
-
case 4: return [2 /*return*/, false];
|
|
102
|
-
}
|
|
23
|
+
const axios_1 = __importDefault(require("axios"));
|
|
24
|
+
async function downloadFileToBase64(_path, _mines = []) {
|
|
25
|
+
if (!Array.isArray(_mines)) {
|
|
26
|
+
console.error(`set mines string array, not "${typeof _mines}" `);
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
const reHttp = /^https?:/;
|
|
30
|
+
if (!reHttp.test(_path)) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
try {
|
|
34
|
+
const response = await axios_1.default.get(_path, {
|
|
35
|
+
responseType: 'arraybuffer',
|
|
103
36
|
});
|
|
104
|
-
|
|
37
|
+
const mimeType = response.headers['content-type'];
|
|
38
|
+
if (_mines.length) {
|
|
39
|
+
const isValidMime = _mines.some((m) => {
|
|
40
|
+
if (typeof m === 'string') {
|
|
41
|
+
return m === mimeType;
|
|
42
|
+
}
|
|
43
|
+
return m.exec(mimeType);
|
|
44
|
+
});
|
|
45
|
+
if (!isValidMime) {
|
|
46
|
+
console.error(`Content-Type "${mimeType}" of ${_path} is not allowed`);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const content = Buffer.from(response.data, 'binary').toString('base64');
|
|
51
|
+
return `data:${mimeType};base64,${content}`;
|
|
52
|
+
}
|
|
53
|
+
catch (error) { }
|
|
54
|
+
return false;
|
|
105
55
|
}
|
|
@@ -15,103 +15,108 @@
|
|
|
15
15
|
* You should have received a copy of the GNU Lesser General Public License
|
|
16
16
|
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
19
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
20
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
21
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
22
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
23
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
24
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
28
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
29
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
30
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
31
|
-
function step(op) {
|
|
32
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
33
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
34
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
35
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
36
|
-
switch (op[0]) {
|
|
37
|
-
case 0: case 1: t = op; break;
|
|
38
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
39
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
40
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
41
|
-
default:
|
|
42
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
43
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
44
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
45
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
46
|
-
if (t[2]) _.ops.pop();
|
|
47
|
-
_.trys.pop(); continue;
|
|
48
|
-
}
|
|
49
|
-
op = body.call(thisArg, _);
|
|
50
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
51
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
55
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
56
|
-
if (ar || !(i in from)) {
|
|
57
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
58
|
-
ar[i] = from[i];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
62
|
-
};
|
|
63
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
19
|
exports.evaluateAndReturn = evaluateAndReturn;
|
|
65
20
|
//EvaluateFn, EvaluateFnReturnType, SerializableOrJSHandle //
|
|
66
|
-
function evaluateAndReturn(page, pageFunction) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
21
|
+
async function evaluateAndReturn(page, pageFunction, ...args) {
|
|
22
|
+
// See https://github.com/puppeteer/puppeteer/blob/41f23beb0da2433cf9103e5d8fc22a03b1820336/src/common/ExecutionContext.ts#L196
|
|
23
|
+
let functionText = pageFunction.toString();
|
|
24
|
+
try {
|
|
25
|
+
new Function('(' + functionText + ')');
|
|
70
26
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
case 1:
|
|
101
|
-
result = (_a.sent());
|
|
102
|
-
if (result !== null && typeof result === 'object' && '__error' in result) {
|
|
103
|
-
errorMessage = result.__error.message || JSON.stringify(result.__error);
|
|
104
|
-
error = new Error(errorMessage);
|
|
105
|
-
Object.assign(error, result.__error);
|
|
106
|
-
jsStack = error.stack;
|
|
107
|
-
Error.captureStackTrace(error);
|
|
108
|
-
if (jsStack) {
|
|
109
|
-
error.stack = "".concat(error.stack || '', "\nJS Stack: ").concat(jsStack, "\nFunction: ").concat(functionText);
|
|
110
|
-
}
|
|
111
|
-
throw error;
|
|
112
|
-
}
|
|
113
|
-
return [2 /*return*/, result];
|
|
27
|
+
catch (error) {
|
|
28
|
+
// This means we might have a function shorthand. Try another
|
|
29
|
+
// time prefixing 'function '.
|
|
30
|
+
if (functionText.startsWith('async ')) {
|
|
31
|
+
functionText =
|
|
32
|
+
'async function ' + functionText.substring('async '.length);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
functionText = 'function ' + functionText;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
new Function('(' + functionText + ')');
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// We tried hard to serialize, but there's a weird beast here.
|
|
42
|
+
throw new Error('Passed function is not well-serializable!');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Polyfill async/await and promise converter
|
|
47
|
+
* See https://github.com/basarat/typescript-book/blob/master/docs/async-await.md
|
|
48
|
+
*/
|
|
49
|
+
const func = new Function(`
|
|
50
|
+
var __assign = (this && this.__assign) || function () {
|
|
51
|
+
__assign = Object.assign || function(t) {
|
|
52
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
53
|
+
s = arguments[i];
|
|
54
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
55
|
+
t[p] = s[p];
|
|
114
56
|
}
|
|
57
|
+
return t;
|
|
58
|
+
};
|
|
59
|
+
return __assign.apply(this, arguments);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
63
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
64
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
65
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
66
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
67
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
68
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
115
69
|
});
|
|
116
|
-
|
|
70
|
+
};
|
|
71
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
72
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
73
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
74
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
75
|
+
function step(op) {
|
|
76
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
77
|
+
while (_) try {
|
|
78
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
79
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
80
|
+
switch (op[0]) {
|
|
81
|
+
case 0: case 1: t = op; break;
|
|
82
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
83
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
84
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
85
|
+
default:
|
|
86
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
87
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
88
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
89
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
90
|
+
if (t[2]) _.ops.pop();
|
|
91
|
+
_.trys.pop(); continue;
|
|
92
|
+
}
|
|
93
|
+
op = body.call(thisArg, _);
|
|
94
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
95
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var _this; // for arrow function
|
|
99
|
+
|
|
100
|
+
return new Promise(async (resolve) => {
|
|
101
|
+
try {
|
|
102
|
+
return resolve(await (${functionText}).apply(this, arguments));
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return resolve({
|
|
105
|
+
__error: JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
});`);
|
|
109
|
+
const result = (await page.evaluate(func, ...args));
|
|
110
|
+
if (result !== null && typeof result === 'object' && '__error' in result) {
|
|
111
|
+
const errorMessage = result.__error.message || JSON.stringify(result.__error);
|
|
112
|
+
const error = new Error(errorMessage);
|
|
113
|
+
Object.assign(error, result.__error);
|
|
114
|
+
let jsStack = error.stack;
|
|
115
|
+
Error.captureStackTrace(error);
|
|
116
|
+
if (jsStack) {
|
|
117
|
+
error.stack = `${error.stack || ''}\nJS Stack: ${jsStack}\nFunction: ${functionText}`;
|
|
118
|
+
}
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
117
122
|
}
|