@pontalabs/baileys 1.2.9 → 1.2.11

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,300 @@
1
+ const VARINT = new Set(['int32', 'uint32', 'int64', 'uint64', 'sint32', 'sint64', 'bool']);
2
+ const FIXED64 = new Set(['double', 'fixed64', 'sfixed64']);
3
+ const FIXED32 = new Set(['float', 'fixed32', 'sfixed32']);
4
+
5
+ const tagOf = (num, wt) => ((num << 3) | wt) >>> 0;
6
+
7
+ const wireOf = (type) => {
8
+ if (type.startsWith('e:') || VARINT.has(type)) return 0;
9
+ if (FIXED64.has(type)) return 1;
10
+ if (FIXED32.has(type)) return 5;
11
+ return 2;
12
+ };
13
+
14
+ const isPackable = (type) => !(type === 'string' || type === 'bytes' || type.startsWith('m:'));
15
+
16
+ const readVarint = (buf, pos) => {
17
+ let result = 0;
18
+ let mul = 1;
19
+ let count = 0;
20
+ let byte;
21
+ do {
22
+ if (pos >= buf.length || ++count > 10) throw new RangeError('bad varint');
23
+ byte = buf[pos++];
24
+ result += (byte & 0x7f) * mul;
25
+ mul *= 128;
26
+ } while (byte & 0x80);
27
+ return [result, pos];
28
+ };
29
+
30
+ const readVarintBig = (buf, pos) => {
31
+ let result = 0n;
32
+ let shift = 0n;
33
+ let count = 0;
34
+ let byte;
35
+ do {
36
+ if (pos >= buf.length || ++count > 10) throw new RangeError('bad varint');
37
+ byte = buf[pos++];
38
+ result |= BigInt(byte & 0x7f) << shift;
39
+ shift += 7n;
40
+ } while (byte & 0x80);
41
+ return [result, pos];
42
+ };
43
+
44
+ const zigzag = (v) => (v >> 1n) ^ -(v & 1n);
45
+
46
+ export const createExtensions = (ns, data) => {
47
+ const classCache = new Map();
48
+
49
+ const oldClass = (path) => {
50
+ if (classCache.has(path)) return classCache.get(path);
51
+ let node = ns;
52
+ for (const part of path.split('.')) {
53
+ node = node?.[part];
54
+ if (node === undefined || node === null) break;
55
+ }
56
+ const found = node && typeof node.encode === 'function' && typeof node.decode === 'function' ? node : null;
57
+ classCache.set(path, found);
58
+ return found;
59
+ };
60
+
61
+ const enumNumber = (path, value) => {
62
+ if (typeof value === 'string') return data.enums[path]?.[value] ?? 0;
63
+ return value;
64
+ };
65
+
66
+ const writeScalar = (w, type, value) => {
67
+ switch (type) {
68
+ case 'string': w.string(String(value)); break;
69
+ case 'bytes': w.bytes(value); break;
70
+ case 'bool': w.bool(!!value); break;
71
+ case 'int32': w.int32(value); break;
72
+ case 'uint32': w.uint32(value); break;
73
+ case 'sint32': w.sint32(value); break;
74
+ case 'int64': w.int64(typeof value === 'bigint' ? value.toString() : value); break;
75
+ case 'uint64': w.uint64(typeof value === 'bigint' ? value.toString() : value); break;
76
+ case 'sint64': w.sint64(typeof value === 'bigint' ? value.toString() : value); break;
77
+ case 'fixed32': w.fixed32(value); break;
78
+ case 'sfixed32': w.sfixed32(value); break;
79
+ case 'fixed64': w.fixed64(typeof value === 'bigint' ? value.toString() : value); break;
80
+ case 'sfixed64': w.sfixed64(typeof value === 'bigint' ? value.toString() : value); break;
81
+ case 'float': w.float(value); break;
82
+ case 'double': w.double(value); break;
83
+ default: break;
84
+ }
85
+ };
86
+
87
+ const writeRaw = (w, type, value) => {
88
+ if (type.startsWith('e:')) w.int32(enumNumber(type.slice(2), value));
89
+ else writeScalar(w, type, value);
90
+ };
91
+
92
+ const encodeMessage = (w, path, value) => {
93
+ const old = oldClass(path);
94
+ if (old) {
95
+ old.encode(value, w);
96
+ return;
97
+ }
98
+ const fields = data.types[path];
99
+ if (!fields) return;
100
+ for (const field of fields) {
101
+ const v = value?.[field[0]];
102
+ if (v !== undefined && v !== null) writeField(w, field, v);
103
+ }
104
+ };
105
+
106
+ const writeOne = (w, num, type, value) => {
107
+ if (type.startsWith('m:')) {
108
+ w.uint32(tagOf(num, 2)).fork();
109
+ encodeMessage(w, type.slice(2), value);
110
+ w.ldelim();
111
+ } else {
112
+ w.uint32(tagOf(num, wireOf(type)));
113
+ writeRaw(w, type, value);
114
+ }
115
+ };
116
+
117
+ const writeField = (w, field, value) => {
118
+ const [, num, type, repeated] = field;
119
+ if (!repeated) {
120
+ writeOne(w, num, type, value);
121
+ return;
122
+ }
123
+ const list = Array.isArray(value) ? value : [value];
124
+ if (!list.length) return;
125
+ if (isPackable(type)) {
126
+ w.uint32(tagOf(num, 2)).fork();
127
+ for (const item of list) writeRaw(w, type, item);
128
+ w.ldelim();
129
+ } else {
130
+ for (const item of list) writeOne(w, num, type, item);
131
+ }
132
+ };
133
+
134
+ const decodeScalar = (buf, type, raw) => {
135
+ switch (type) {
136
+ case 'bool': return raw !== 0n;
137
+ case 'int32': return Number(BigInt.asIntN(32, raw));
138
+ case 'uint32': return Number(BigInt.asUintN(32, raw));
139
+ case 'int64': return Number(BigInt.asIntN(64, raw));
140
+ case 'uint64': return Number(BigInt.asUintN(64, raw));
141
+ case 'sint32': return Number(BigInt.asIntN(32, zigzag(raw)));
142
+ case 'sint64': return Number(BigInt.asIntN(64, zigzag(raw)));
143
+ default: return Number(raw);
144
+ }
145
+ };
146
+
147
+ const readFixed = (buf, type, pos) => {
148
+ const view = new DataView(buf.buffer, buf.byteOffset + pos, FIXED64.has(type) ? 8 : 4);
149
+ switch (type) {
150
+ case 'float': return view.getFloat32(0, true);
151
+ case 'fixed32': return view.getUint32(0, true);
152
+ case 'sfixed32': return view.getInt32(0, true);
153
+ case 'double': return view.getFloat64(0, true);
154
+ case 'fixed64': return Number(view.getBigUint64(0, true));
155
+ default: return Number(view.getBigInt64(0, true));
156
+ }
157
+ };
158
+
159
+ const decodeSlice = (buf, type, a, b) => {
160
+ if (type === 'string') return Buffer.from(buf.buffer, buf.byteOffset + a, b - a).toString('utf8');
161
+ if (type === 'bytes') return Buffer.from(buf.subarray(a, b));
162
+ const path = type.slice(2);
163
+ const old = oldClass(path);
164
+ if (old) return old.decode(buf.subarray(a, b));
165
+ return decodeInto(path, buf, a, b);
166
+ };
167
+
168
+ const readFieldValue = (buf, field, wt, pos) => {
169
+ const type = field[2];
170
+ if (wt === 0) {
171
+ const [raw, next] = readVarintBig(buf, pos);
172
+ if (type.startsWith('e:')) return [Number(BigInt.asIntN(32, raw)), next];
173
+ return [decodeScalar(buf, type, raw), next];
174
+ }
175
+ if (wt === 1) return [readFixed(buf, FIXED64.has(type) ? type : 'fixed64', pos), pos + 8];
176
+ if (wt === 5) return [readFixed(buf, FIXED32.has(type) ? type : 'fixed32', pos), pos + 4];
177
+ const [len, start] = readVarint(buf, pos);
178
+ return [decodeSlice(buf, type, start, start + len), start + len, start, start + len];
179
+ };
180
+
181
+ const skip = (buf, wt, pos) => {
182
+ if (wt === 0) return readVarint(buf, pos)[1];
183
+ if (wt === 1) return pos + 8;
184
+ if (wt === 5) return pos + 4;
185
+ if (wt === 2) {
186
+ const [len, start] = readVarint(buf, pos);
187
+ return start + len;
188
+ }
189
+ throw new RangeError('bad wire type');
190
+ };
191
+
192
+ const assign = (target, field, value) => {
193
+ const name = field[0];
194
+ if (!field[3]) {
195
+ target[name] = value;
196
+ return;
197
+ }
198
+ if (!Array.isArray(target[name])) target[name] = [];
199
+ if (Array.isArray(value)) target[name].push(...value);
200
+ else target[name].push(value);
201
+ };
202
+
203
+ const scan = (buf, start, end, byNumber, target) => {
204
+ let pos = start;
205
+ while (pos < end) {
206
+ const [tag, afterTag] = readVarint(buf, pos);
207
+ const num = tag >>> 3;
208
+ const wt = tag & 7;
209
+ const field = byNumber.get(num);
210
+ if (!field) {
211
+ pos = skip(buf, wt, afterTag);
212
+ continue;
213
+ }
214
+ const type = field[2];
215
+ if (field[3] && wt === 2 && isPackable(type)) {
216
+ const [len, s] = readVarint(buf, afterTag);
217
+ const packed = [];
218
+ let p = s;
219
+ const stop = s + len;
220
+ const elementWire = wireOf(type);
221
+ while (p < stop) {
222
+ const r = readFieldValue(buf, field, elementWire, p);
223
+ packed.push(r[0]);
224
+ p = r[1];
225
+ }
226
+ assign(target, field, packed);
227
+ pos = stop;
228
+ continue;
229
+ }
230
+ const result = readFieldValue(buf, field, wt, afterTag);
231
+ assign(target, field, result[0]);
232
+ pos = result[1];
233
+ }
234
+ };
235
+
236
+ const decodeInto = (path, buf, start, end) => {
237
+ const fields = data.types[path] || [];
238
+ const byNumber = new Map(fields.map((f) => [f[1], f]));
239
+ const out = {};
240
+ scan(buf, start, end, byNumber, out);
241
+ return out;
242
+ };
243
+
244
+ const patched = [];
245
+
246
+ const patchClass = (Cls, fields) => {
247
+ const byNumber = new Map(fields.map((f) => [f[1], f]));
248
+ const origEncode = Cls.encode;
249
+ const origFromObject = Cls.fromObject;
250
+ const origDecode = Cls.decode;
251
+
252
+ Cls.encode = function (message, writer) {
253
+ const w = origEncode.call(this, message, writer);
254
+ if (message) {
255
+ for (const field of fields) {
256
+ const v = message[field[0]];
257
+ if (v !== undefined && v !== null) writeField(w, field, v);
258
+ }
259
+ }
260
+ return w;
261
+ };
262
+
263
+ Cls.fromObject = function (object) {
264
+ const instance = origFromObject.call(this, object);
265
+ if (object && object !== instance) {
266
+ for (const field of fields) {
267
+ const v = object[field[0]];
268
+ if (v !== undefined && v !== null) instance[field[0]] = v;
269
+ }
270
+ }
271
+ return instance;
272
+ };
273
+
274
+ Cls.decode = function (reader, length, ...rest) {
275
+ const isReader = !!reader && typeof reader.pos === 'number' && !!reader.buf;
276
+ const buf = isReader ? reader.buf : reader;
277
+ const start = isReader ? reader.pos : 0;
278
+ const message = origDecode.call(this, reader, length, ...rest);
279
+ try {
280
+ if (buf && typeof buf.length === 'number') {
281
+ const end = isReader ? reader.pos : (length === undefined ? buf.length : start + length);
282
+ scan(buf, start, end, byNumber, message);
283
+ }
284
+ } catch (_) { /* keep the natively decoded message */ }
285
+ return message;
286
+ };
287
+ };
288
+
289
+ for (const [path, list] of Object.entries(data.ext)) {
290
+ const Cls = oldClass(path);
291
+ if (!Cls || Cls.__protoExtended) continue;
292
+ const fields = list.filter((f) => !(f[0] in Cls.prototype));
293
+ if (!fields.length) continue;
294
+ patchClass(Cls, fields);
295
+ Cls.__protoExtended = true;
296
+ patched.push(path);
297
+ }
298
+
299
+ return { patched, encodeMessage, decodeInto };
300
+ };
@@ -0,0 +1,5 @@
1
+ export declare const protoExtensions: {
2
+ patched: string[];
3
+ encodeMessage: (writer: any, path: string, value: any) => void;
4
+ decodeInto: (path: string, buf: Uint8Array, start: number, end: number) => any;
5
+ };
@@ -0,0 +1,5 @@
1
+ import { proto } from '../../WAProto/index.js';
2
+ import data from './proto-extension-data.js';
3
+ import { createExtensions } from './proto-extensions-core.js';
4
+
5
+ export const protoExtensions = createExtensions(proto, data);
@@ -48,3 +48,11 @@ export function wrapToBotForwardedMessage(richResponseMessage: any): {
48
48
  };
49
49
  //# sourceMappingURL=rich-message-utils.d.ts.map
50
50
  export declare function applyReadmeRichAliases(message: any): any;
51
+
52
+ export function packUnifiedResponse(unified: any, gzip?: boolean): {
53
+ data: Buffer;
54
+ gzipData?: undefined;
55
+ } | {
56
+ gzipData: Buffer;
57
+ data?: undefined;
58
+ };
@@ -1,4 +1,5 @@
1
1
  import { getRandomValues, randomUUID, randomBytes } from 'crypto';
2
+ import { gzipSync, gunzipSync } from 'zlib';
2
3
  import { DONATE_URL, LEXER_REGEX } from '../Defaults/index.js';
3
4
  import { LANGUAGE_KEYWORDS } from '../WABinary/constants.js';
4
5
  import { CodeHighlightType, RichSubMessageType } from '../Types/RichType.js';
@@ -7,6 +8,12 @@ import { unixTimestampSeconds, generateMessageIDV2 } from './generics.js';
7
8
 
8
9
  const NOOP = new Set([]);
9
10
 
11
+ /** Bungkus unified response JSON. gzip=true -> kirim lewat field gzipData, default tetap data. */
12
+ export const packUnifiedResponse = (unified, gzip = false) => {
13
+ const raw = Buffer.from(typeof unified === 'string' ? unified : JSON.stringify(unified), 'utf-8');
14
+ return gzip ? { gzipData: gzipSync(raw) } : { data: raw };
15
+ };
16
+
10
17
  /* ─────────────────────────────────────────────────────────────
11
18
  LATEX URL GENERATOR
12
19
  WhatsApp client membutuhkan URL gambar render untuk setiap
@@ -203,17 +210,27 @@ export const parseRichMessage = (richResponseMessage) => {
203
210
  .filter(Boolean);
204
211
 
205
212
  let unifiedData = null;
206
- if (richResponseMessage.unifiedResponse?.data) {
213
+ const readUnified = (ur) => {
214
+ if (!ur) return null;
207
215
  try {
208
- const buf = richResponseMessage.unifiedResponse.data;
209
- unifiedData = JSON.parse(Buffer.isBuffer(buf) ? buf.toString('utf-8') : Buffer.from(buf).toString('utf-8'));
216
+ if (ur.gzipData?.length) {
217
+ return JSON.parse(gunzipSync(Buffer.from(ur.gzipData)).toString('utf-8'));
218
+ }
219
+ if (ur.data?.length) {
220
+ const buf = ur.data;
221
+ return JSON.parse(Buffer.isBuffer(buf) ? buf.toString('utf-8') : Buffer.from(buf).toString('utf-8'));
222
+ }
210
223
  } catch (_) { /* ignore parse error */ }
211
- }
224
+ return null;
225
+ };
226
+ unifiedData = readUnified(richResponseMessage.unifiedResponse);
227
+ const originalRecipientData = readUnified(richResponseMessage.originalRecipientMetadata);
212
228
 
213
229
  return {
214
230
  messageType: richResponseMessage.messageType ?? 0,
215
231
  submessages,
216
232
  unifiedData,
233
+ ...(originalRecipientData ? { originalRecipientData } : {}),
217
234
  };
218
235
  };
219
236
 
@@ -858,7 +875,7 @@ const makeRichCardSection = (data) => {
858
875
  MAIN BUILDER — prepareRichResponseMessage
859
876
  ───────────────────────────────────────────────────────────── */
860
877
 
861
- export const prepareRichResponseMessage = (content) => {
878
+ export const prepareRichResponseMessage = (content, runtime = {}) => {
862
879
  const {
863
880
  code, contentText, disclaimerText, footerText, headerText,
864
881
  language, links, noHeading, richResponse, table, title,
@@ -869,7 +886,7 @@ export const prepareRichResponseMessage = (content) => {
869
886
  richImage, richVideo, reels, source, richProduct, richPost, tip, suggest,
870
887
  richWidget, widget, richFooterAction, footerAction, richMetadata, metadata, richFOAText, foaText, richHtml, richCard,
871
888
  // opsi tambahan (belum ada di versi ponta.zip semula)
872
- aiForwarded
889
+ aiForwarded, unifiedGzip
873
890
  } = content;
874
891
 
875
892
  const submessages = [];
@@ -1037,9 +1054,7 @@ export const prepareRichResponseMessage = (content) => {
1037
1054
  const richResponseMessage = proto.AIRichResponseMessage.create({
1038
1055
  submessages,
1039
1056
  messageType: proto.AIRichResponseMessageType.AI_RICH_RESPONSE_TYPE_STANDARD,
1040
- unifiedResponse: {
1041
- data: Buffer.from(JSON.stringify(unified), 'utf-8')
1042
- },
1057
+ unifiedResponse: packUnifiedResponse(unified, typeof unifiedGzip === 'boolean' ? unifiedGzip : runtime.gzip === true),
1043
1058
  contextInfo: {
1044
1059
  isForwarded: true,
1045
1060
  forwardingScore: 1,
@@ -1252,8 +1267,9 @@ export const captureUnifiedResponse = (msg) => {
1252
1267
  const botFwd = msg?.botForwardedMessage?.message;
1253
1268
  if (!botFwd) return null;
1254
1269
  const rich = botFwd.richResponseMessage;
1255
- if (!rich?.unifiedResponse?.data) return null;
1256
- return { unifiedResponse: { data: rich.unifiedResponse.data }, submessages: rich.submessages || [], contextInfo: rich.contextInfo || {} };
1270
+ const ur = rich?.unifiedResponse;
1271
+ if (!ur?.data?.length && !ur?.gzipData?.length) return null;
1272
+ return { unifiedResponse: ur.data?.length ? { data: ur.data } : { gzipData: ur.gzipData }, submessages: rich.submessages || [], contextInfo: rich.contextInfo || {} };
1257
1273
  };
1258
1274
 
1259
1275
  // ── Link ──────────────────────────────────────────────────
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pontalabs/baileys",
3
3
  "type": "module",
4
- "version": "1.2.9",
4
+ "version": "1.2.11",
5
5
  "description": "PontaLabs Baileys is a lightweight, modern, and customizable WhatsApp Web API library built on Baileys.",
6
6
  "keywords": [
7
7
  "whatsapp",