@hiroosy/baileys-lite 0.0.3 → 0.3.0

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.
Files changed (116) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +660 -12
  3. package/WAProto/index.js +2 -2558
  4. package/engine-requirements.js +3 -4
  5. package/package.json +8 -3
  6. package/src/binary/wa-binary.js +3 -25
  7. package/src/constants.js +7 -15
  8. package/src/defaults.js +2 -5
  9. package/src/foundation/ai-rich.js +530 -0
  10. package/src/foundation/audio-duration.js +43 -103
  11. package/src/foundation/audio-samples.js +19 -37
  12. package/src/foundation/boom.js +0 -7
  13. package/src/foundation/cache.js +4 -26
  14. package/src/foundation/concurrency.js +1 -19
  15. package/src/foundation/curve25519.js +7 -39
  16. package/src/foundation/link-preview.js +15 -57
  17. package/src/foundation/logger.js +0 -11
  18. package/src/foundation/long.js +4 -14
  19. package/src/foundation/protobuf-minimal.js +21 -53
  20. package/src/foundation/qrcode-terminal.js +273 -0
  21. package/src/foundation/sql-database.js +702 -0
  22. package/src/foundation/wa-crypto.js +2 -31
  23. package/src/foundation/zip.js +25 -41
  24. package/src/index.js +8 -18
  25. package/src/signal/libsignal.js +23 -78
  26. package/src/signal/signal-group.js +0 -35
  27. package/src/signal/signal-repository.js +10 -29
  28. package/src/socket/business-communities.js +12 -31
  29. package/src/socket/chats.js +5 -8
  30. package/src/socket/groups-newsletter.js +18 -17
  31. package/src/socket/messages-recv.js +81 -127
  32. package/src/socket/messages-send.js +372 -139
  33. package/src/socket/socket-core.js +227 -157
  34. package/src/socket/usync.js +2 -6
  35. package/src/socket-client/native-websocket.js +344 -0
  36. package/src/socket-client/websocket-client.js +84 -38
  37. package/src/utils/auth-state-core.js +14 -56
  38. package/src/utils/auth-state-storage.js +7 -42
  39. package/src/utils/chat-sync.js +4 -26
  40. package/src/utils/media.js +15 -71
  41. package/src/utils/message-compose.js +208 -85
  42. package/src/utils/message-processing.js +10 -65
  43. package/src/utils/username.js +19 -0
  44. package/src/utils/wa-protocol-core.js +71 -127
  45. package/src/vendor/jpeg-js/decoder.js +75 -136
  46. package/src/vendor/jpeg-js/encoder.js +179 -264
  47. package/src/vendor/pngjs/bitmapper.js +5 -27
  48. package/src/vendor/pngjs/bitpacker.js +4 -12
  49. package/src/vendor/pngjs/constants.js +3 -11
  50. package/src/vendor/pngjs/crc.js +0 -6
  51. package/src/vendor/pngjs/filter-pack.js +1 -7
  52. package/src/vendor/pngjs/filter-parse-sync.js +0 -6
  53. package/src/vendor/pngjs/filter-parse.js +1 -11
  54. package/src/vendor/pngjs/format-normaliser.js +5 -11
  55. package/src/vendor/pngjs/interlace.js +7 -24
  56. package/src/vendor/pngjs/packer-sync.js +0 -11
  57. package/src/vendor/pngjs/packer.js +7 -15
  58. package/src/vendor/pngjs/paeth-predictor.js +0 -6
  59. package/src/vendor/pngjs/parser-sync.js +0 -8
  60. package/src/vendor/pngjs/parser.js +5 -29
  61. package/src/vendor/pngjs/png-sync.js +0 -7
  62. package/src/vendor/pngjs/sync-inflate.js +7 -16
  63. package/src/vendor/pngjs/sync-reader.js +4 -11
  64. package/src/voip/WaVoipCoordinator.js +158 -0
  65. package/src/voip/app-data/WaAppDataStream.js +193 -0
  66. package/src/voip/app-data/protocol.js +172 -0
  67. package/src/voip/bytes.js +63 -0
  68. package/src/voip/call/WaCallManager.js +442 -0
  69. package/src/voip/call/WaCallMediaSession.js +1705 -0
  70. package/src/voip/call/call-state.js +203 -0
  71. package/src/voip/crypto/encryption.js +13 -0
  72. package/src/voip/crypto/primitives.js +21 -0
  73. package/src/voip/crypto/rtcp.js +66 -0
  74. package/src/voip/crypto/srtp.js +180 -0
  75. package/src/voip/crypto/ssrc.js +15 -0
  76. package/src/voip/index.js +628 -0
  77. package/src/voip/media/WaAudioEngine.js +486 -0
  78. package/src/voip/media/WaVideoEngine.js +275 -0
  79. package/src/voip/media/audio-codec.js +0 -0
  80. package/src/voip/media/audio-reorder.js +107 -0
  81. package/src/voip/media/h264.js +246 -0
  82. package/src/voip/media/mlow-codec.js +465 -0
  83. package/src/voip/media/rtcp.js +68 -0
  84. package/src/voip/media/rtp.js +242 -0
  85. package/src/voip/protobuf.js +30 -0
  86. package/src/voip/relay/WaManualRelay.js +609 -0
  87. package/src/voip/relay/datachannel/pre-negotiated.js +25 -0
  88. package/src/voip/relay/dtls/aead.js +46 -0
  89. package/src/voip/relay/dtls/cert-builder.js +102 -0
  90. package/src/voip/relay/dtls/der.js +98 -0
  91. package/src/voip/relay/dtls/ecdhe.js +37 -0
  92. package/src/voip/relay/dtls/handshake-framing.js +100 -0
  93. package/src/voip/relay/dtls/handshake-messages.js +158 -0
  94. package/src/voip/relay/dtls/handshake.js +582 -0
  95. package/src/voip/relay/dtls/prf.js +39 -0
  96. package/src/voip/relay/dtls/record.js +56 -0
  97. package/src/voip/relay/relay-ack.js +141 -0
  98. package/src/voip/relay/sctp/association.js +434 -0
  99. package/src/voip/relay/sctp/crc32c.js +21 -0
  100. package/src/voip/relay/sctp/wire.js +226 -0
  101. package/src/voip/relay/stun.js +463 -0
  102. package/src/voip/shim/baileys-resolve.js +7 -0
  103. package/src/voip/shim/core.js +66 -0
  104. package/src/voip/shim/crypto.js +6 -0
  105. package/src/voip/shim/proto.js +10 -0
  106. package/src/voip/shim/protocol.js +130 -0
  107. package/src/voip/shim/transport.js +82 -0
  108. package/src/voip/shim/util.js +207 -0
  109. package/src/voip/signaling/bridge.js +102 -0
  110. package/src/voip/signaling/screen-share.js +60 -0
  111. package/src/voip/signaling/signaling.js +491 -0
  112. package/src/voip/types.js +69 -0
  113. package/src/voip/voip-deps.js +243 -0
  114. package/src/voip/voipClient.js +314 -0
  115. package/src/wam/wam-constants.js +0 -1
  116. package/src/wam/wam.js +2 -7
@@ -0,0 +1,242 @@
1
+ import { EMPTY_BYTES, readUInt16BE, readUInt32BE, writeUInt16BE, writeUInt32BE } from '../bytes.js';
2
+ import { randomInt } from '../crypto/primitives.js';
3
+ import { PayloadType } from '../types.js';
4
+ const RTP_VERSION = 2;
5
+ const MIN_HEADER_SIZE = 12;
6
+ export class RtpHeader {
7
+ version = RTP_VERSION;
8
+ padding = false;
9
+ extension = false;
10
+ marker = false;
11
+ payloadType;
12
+ sequenceNumber;
13
+ timestamp;
14
+ ssrc;
15
+ csrc = [];
16
+ extensionProfile = 0;
17
+ extensionData = EMPTY_BYTES;
18
+ get csrcCount() {
19
+ return this.csrc.length;
20
+ }
21
+ constructor(payloadType, sequenceNumber, timestamp, ssrc) {
22
+ this.payloadType = payloadType;
23
+ this.sequenceNumber = sequenceNumber;
24
+ this.timestamp = timestamp;
25
+ this.ssrc = ssrc;
26
+ }
27
+ size() {
28
+ let s = MIN_HEADER_SIZE + this.csrcCount * 4;
29
+ if (this.extension) {
30
+ s += 4 + this.extensionData.length;
31
+ }
32
+ return s;
33
+ }
34
+ encode(buf) {
35
+ if (buf.length < this.size()) {
36
+ throw new Error('buffer too small for RTP header');
37
+ }
38
+ buf[0] =
39
+ ((this.version & 0x03) << 6) |
40
+ ((this.padding ? 1 : 0) << 5) |
41
+ ((this.extension ? 1 : 0) << 4) |
42
+ (this.csrcCount & 0x0f);
43
+ buf[1] = ((this.marker ? 1 : 0) << 7) | (this.payloadType & 0x7f);
44
+ writeUInt16BE(buf, this.sequenceNumber, 2);
45
+ writeUInt32BE(buf, this.timestamp, 4);
46
+ writeUInt32BE(buf, this.ssrc, 8);
47
+ let offset = 12;
48
+ for (let i = 0; i < this.csrc.length; i++) {
49
+ writeUInt32BE(buf, this.csrc[i], offset);
50
+ offset += 4;
51
+ }
52
+ if (this.extension) {
53
+ if (this.extensionData.length % 4 !== 0) {
54
+ throw new Error('RTP extension data must be 32-bit aligned');
55
+ }
56
+ writeUInt16BE(buf, this.extensionProfile, offset);
57
+ writeUInt16BE(buf, this.extensionData.length / 4, offset + 2);
58
+ buf.set(this.extensionData, offset + 4);
59
+ }
60
+ return this.size();
61
+ }
62
+ static decode(buf) {
63
+ if (buf.length < MIN_HEADER_SIZE) {
64
+ throw new Error('buffer too small for RTP header');
65
+ }
66
+ const version = (buf[0] >> 6) & 0x03;
67
+ if (version !== RTP_VERSION) {
68
+ throw new Error(`invalid RTP version: ${version}`);
69
+ }
70
+ const padding = ((buf[0] >> 5) & 0x01) !== 0;
71
+ const extension = ((buf[0] >> 4) & 0x01) !== 0;
72
+ const csrcCount = buf[0] & 0x0f;
73
+ const marker = ((buf[1] >> 7) & 0x01) !== 0;
74
+ const payloadType = buf[1] & 0x7f;
75
+ const sequenceNumber = readUInt16BE(buf, 2);
76
+ const timestamp = readUInt32BE(buf, 4);
77
+ const ssrc = readUInt32BE(buf, 8);
78
+ const headerSize = MIN_HEADER_SIZE + csrcCount * 4;
79
+ if (buf.length < headerSize) {
80
+ throw new Error('buffer too small for CSRC list');
81
+ }
82
+ const csrc = [];
83
+ let offset = 12;
84
+ for (let i = 0; i < csrcCount; i++) {
85
+ csrc.push(readUInt32BE(buf, offset));
86
+ offset += 4;
87
+ }
88
+ const header = new RtpHeader(payloadType, sequenceNumber, timestamp, ssrc);
89
+ header.version = version;
90
+ header.padding = padding;
91
+ header.extension = extension;
92
+ header.marker = marker;
93
+ header.csrc = csrc;
94
+ if (extension) {
95
+ if (buf.length < offset + 4) {
96
+ throw new Error('buffer too small for RTP extension header');
97
+ }
98
+ header.extensionProfile = readUInt16BE(buf, offset);
99
+ const extWords = readUInt16BE(buf, offset + 2);
100
+ const extBytes = extWords * 4;
101
+ offset += 4;
102
+ if (buf.length < offset + extBytes) {
103
+ throw new Error('buffer too small for RTP extension data');
104
+ }
105
+ header.extensionData = buf.slice(offset, offset + extBytes);
106
+ }
107
+ return header;
108
+ }
109
+ }
110
+ export class RtpPacket {
111
+ header;
112
+ payload;
113
+ constructor(header, payload) {
114
+ this.header = header;
115
+ this.payload = payload;
116
+ }
117
+ size() {
118
+ return this.header.size() + this.payload.length;
119
+ }
120
+ encode() {
121
+ const buf = new Uint8Array(this.size());
122
+ const headerSize = this.header.encode(buf);
123
+ buf.set(this.payload, headerSize);
124
+ return buf;
125
+ }
126
+ static decode(buf) {
127
+ const header = RtpHeader.decode(buf);
128
+ let end = buf.length;
129
+ if (header.padding) {
130
+ const padLen = buf[buf.length - 1];
131
+ if (padLen > 0 && header.size() + padLen <= buf.length) {
132
+ end = buf.length - padLen;
133
+ }
134
+ }
135
+ const payload = buf.slice(header.size(), end);
136
+ return new RtpPacket(header, payload);
137
+ }
138
+ }
139
+
140
+ export const VideoMediaFrameInfo = { IDR: 0x08, Delta: 0x20 };
141
+ const DEFAULT_VIDEO_RTP_STEP_SAMPLES = Math.round(90000 / 30);
142
+
143
+ export function videoRtpDurationSamples(durationMs) {
144
+ if (!durationMs || durationMs <= 0) return DEFAULT_VIDEO_RTP_STEP_SAMPLES;
145
+ const samples = Math.round((durationMs * 90000) / 1000);
146
+ return samples || DEFAULT_VIDEO_RTP_STEP_SAMPLES;
147
+ }
148
+
149
+ function encodeWhatsappVideoExtension(ext) {
150
+ const hasFrameNumber = ext.frameNumber !== null && ext.frameNumber !== undefined;
151
+ const out = [];
152
+ out.push(0x30 | ((hasFrameNumber ? 3 : 1) - 1), ext.mediaFrameInfo & 0xff);
153
+ if (hasFrameNumber) {
154
+ out.push((ext.frameNumber >>> 8) & 0xff, ext.frameNumber & 0xff);
155
+ }
156
+ out.push(0x51, (ext.initialBandwidth >>> 8) & 0xff, ext.initialBandwidth & 0xff);
157
+ out.push(0x61, (ext.shortOffset >>> 8) & 0xff, ext.shortOffset & 0xff);
158
+ out.push(0x91, (ext.transportSequence >>> 8) & 0xff, ext.transportSequence & 0xff);
159
+ while (out.length % 4 !== 0) out.push(0);
160
+ return new Uint8Array(out);
161
+ }
162
+
163
+ export class VideoRtpStream {
164
+ ssrc;
165
+ sequenceNumber;
166
+ timestamp;
167
+ tsStride;
168
+ transportSequence = 0;
169
+ frameNumber = 1;
170
+ firstPacket = true;
171
+ constructor(ssrc, tsStride) {
172
+ this.ssrc = ssrc;
173
+ this.sequenceNumber = randomInt(0, 65536);
174
+ this.timestamp = randomInt(0, 0xffffffff);
175
+ this.tsStride = tsStride;
176
+ }
177
+ setTimestampStride(tsStride) {
178
+ if (!tsStride) return false;
179
+ this.tsStride = tsStride;
180
+ return true;
181
+ }
182
+
183
+ nextPacket(lastInAccessUnit, mediaFrameInfo) {
184
+ const frameNumber = this.firstPacket ? this.frameNumber : null;
185
+ const ext = {
186
+ mediaFrameInfo,
187
+ frameNumber,
188
+ initialBandwidth: 0,
189
+ shortOffset: 0,
190
+ transportSequence: this.transportSequence
191
+ };
192
+ const header = new RtpHeader(PayloadType.H264, this.sequenceNumber, this.timestamp, this.ssrc);
193
+ header.marker = lastInAccessUnit;
194
+ header.extension = true;
195
+ header.extensionProfile = 0xdebe;
196
+ header.extensionData = encodeWhatsappVideoExtension(ext);
197
+ this.sequenceNumber = (this.sequenceNumber + 1) & 0xffff;
198
+ this.transportSequence = (this.transportSequence + 1) & 0xffff;
199
+ if (lastInAccessUnit) {
200
+ this.timestamp = (this.timestamp + this.tsStride) >>> 0;
201
+ this.frameNumber = (this.frameNumber + 1) & 0xffff;
202
+ this.firstPacket = true;
203
+ } else {
204
+ this.firstPacket = false;
205
+ }
206
+ return header;
207
+ }
208
+ }
209
+
210
+ export class RtpSession {
211
+ ssrc;
212
+ payloadType;
213
+ sequenceNumber;
214
+ sampleRate;
215
+ timestamp;
216
+ samplesPerPacket;
217
+ constructor(ssrc, payloadType, sampleRate, samplesPerPacket) {
218
+ this.ssrc = ssrc;
219
+ this.payloadType = payloadType;
220
+ this.sequenceNumber = randomInt(0, 65536);
221
+ this.sampleRate = sampleRate;
222
+ this.timestamp = randomInt(0, 0xffffffff);
223
+ this.samplesPerPacket = samplesPerPacket;
224
+ }
225
+ static whatsappOpus(ssrc) {
226
+ return new RtpSession(ssrc, PayloadType.WhatsAppOpus, 16000, 960);
227
+ }
228
+ createPacket(payload, marker = false) {
229
+ const header = new RtpHeader(this.payloadType, this.sequenceNumber, this.timestamp, this.ssrc);
230
+ header.marker = marker;
231
+ this.sequenceNumber = (this.sequenceNumber + 1) & 0xffff;
232
+ this.timestamp = (this.timestamp + this.samplesPerPacket) >>> 0;
233
+ return new RtpPacket(header, payload);
234
+ }
235
+ createPacketWithDuration(payload, durationSamples, marker = false) {
236
+ const header = new RtpHeader(this.payloadType, this.sequenceNumber, this.timestamp, this.ssrc);
237
+ header.marker = marker;
238
+ this.sequenceNumber = (this.sequenceNumber + 1) & 0xffff;
239
+ this.timestamp = (this.timestamp + durationSamples) >>> 0;
240
+ return new RtpPacket(header, payload);
241
+ }
242
+ }
@@ -0,0 +1,30 @@
1
+ import { concatBytes } from './bytes.js';
2
+
3
+ const WIRE_TYPE_VARINT = 0;
4
+ const WIRE_TYPE_LENGTH_DELIMITED = 2;
5
+ const VARINT_CONTINUATION = 0x80;
6
+ const VARINT_PAYLOAD_MASK = 0x7fn;
7
+ const VARINT_SHIFT = 7n;
8
+ function encodeVarint(value) {
9
+ const bytes = [];
10
+ let remaining = value;
11
+ while (remaining > VARINT_PAYLOAD_MASK) {
12
+ bytes.push(Number(remaining & VARINT_PAYLOAD_MASK) | VARINT_CONTINUATION);
13
+ remaining >>= VARINT_SHIFT;
14
+ }
15
+ bytes.push(Number(remaining & VARINT_PAYLOAD_MASK));
16
+ return new Uint8Array(bytes);
17
+ }
18
+ function encodeTag(fieldNumber, wireType) {
19
+ return encodeVarint((BigInt(fieldNumber) << 3n) | BigInt(wireType));
20
+ }
21
+ export function encodeProtoVarintField(fieldNumber, value) {
22
+ return concatBytes([encodeTag(fieldNumber, WIRE_TYPE_VARINT), encodeVarint(value)]);
23
+ }
24
+ export function encodeProtoLengthDelimited(fieldNumber, data) {
25
+ return concatBytes([
26
+ encodeTag(fieldNumber, WIRE_TYPE_LENGTH_DELIMITED),
27
+ encodeVarint(BigInt(data.length)),
28
+ data
29
+ ]);
30
+ }