@skycodee/baileys 8.0.3 → 8.0.4

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 (114) hide show
  1. package/package.json +4 -4
  2. package/src/Defaults/baileys-version.json +1 -0
  3. package/src/Defaults/index.ts +149 -0
  4. package/src/Signal/Group/ciphertext-message.ts +9 -0
  5. package/src/Signal/Group/group-session-builder.ts +56 -0
  6. package/src/Signal/Group/group_cipher.ts +117 -0
  7. package/src/Signal/Group/index.ts +11 -0
  8. package/src/Signal/Group/keyhelper.ts +28 -0
  9. package/src/Signal/Group/sender-chain-key.ts +34 -0
  10. package/src/Signal/Group/sender-key-distribution-message.ts +95 -0
  11. package/src/Signal/Group/sender-key-message.ts +96 -0
  12. package/src/Signal/Group/sender-key-name.ts +66 -0
  13. package/src/Signal/Group/sender-key-record.ts +69 -0
  14. package/src/Signal/Group/sender-key-state.ts +134 -0
  15. package/src/Signal/Group/sender-message-key.ts +36 -0
  16. package/src/Signal/libsignal.ts +521 -0
  17. package/src/Signal/lid-mapping.ts +328 -0
  18. package/src/Socket/Client/index.ts +2 -0
  19. package/src/Socket/Client/types.ts +22 -0
  20. package/src/Socket/Client/websocket.ts +63 -0
  21. package/src/Socket/business.ts +424 -0
  22. package/src/Socket/chats.ts +1272 -0
  23. package/src/Socket/communities.ts +477 -0
  24. package/src/Socket/groups.ts +361 -0
  25. package/src/Socket/index.ts +15 -0
  26. package/src/Socket/messages-recv.ts +1668 -0
  27. package/src/Socket/messages-send.ts +1296 -0
  28. package/src/Socket/mex.ts +58 -0
  29. package/src/Socket/newsletter.ts +229 -0
  30. package/src/Socket/socket.ts +1184 -0
  31. package/src/Types/Auth.ts +116 -0
  32. package/src/Types/Bussines.ts +20 -0
  33. package/src/Types/Call.ts +15 -0
  34. package/src/Types/Chat.ts +138 -0
  35. package/src/Types/Contact.ts +24 -0
  36. package/src/Types/Events.ts +159 -0
  37. package/src/Types/GroupMetadata.ts +70 -0
  38. package/src/Types/Label.ts +48 -0
  39. package/src/Types/LabelAssociation.ts +35 -0
  40. package/src/Types/Message.ts +380 -0
  41. package/src/Types/Newsletter.ts +98 -0
  42. package/src/Types/Product.ts +85 -0
  43. package/src/Types/Signal.ts +76 -0
  44. package/src/Types/Socket.ts +150 -0
  45. package/src/Types/State.ts +43 -0
  46. package/src/Types/USync.ts +27 -0
  47. package/src/Types/globals.d.ts +8 -0
  48. package/src/Types/index.ts +67 -0
  49. package/src/Utils/auth-utils.ts +368 -0
  50. package/src/Utils/browser-utils.ts +31 -0
  51. package/src/Utils/business.ts +286 -0
  52. package/src/Utils/chat-utils.ts +974 -0
  53. package/src/Utils/crypto.ts +147 -0
  54. package/src/Utils/decode-wa-message.ts +355 -0
  55. package/src/Utils/event-buffer.ts +692 -0
  56. package/src/Utils/generics.ts +474 -0
  57. package/src/Utils/history.ts +161 -0
  58. package/src/Utils/identity-change-handler.ts +83 -0
  59. package/src/Utils/index.ts +19 -0
  60. package/src/Utils/link-preview.ts +111 -0
  61. package/src/Utils/logger.ts +13 -0
  62. package/src/Utils/lt-hash.ts +8 -0
  63. package/src/Utils/make-mutex.ts +42 -0
  64. package/src/Utils/message-retry-manager.ts +295 -0
  65. package/src/Utils/messages-media.ts +984 -0
  66. package/src/Utils/messages.ts +1097 -0
  67. package/src/Utils/noise-handler.ts +268 -0
  68. package/src/Utils/pre-key-manager.ts +126 -0
  69. package/src/Utils/process-message.ts +670 -0
  70. package/src/Utils/reporting-utils.ts +344 -0
  71. package/src/Utils/signal.ts +214 -0
  72. package/src/Utils/sync-action-utils.ts +74 -0
  73. package/src/Utils/tc-token-utils.ts +34 -0
  74. package/src/Utils/use-multi-file-auth-state.ts +136 -0
  75. package/src/Utils/validate-connection.ts +253 -0
  76. package/src/WABinary/constants.ts +1305 -0
  77. package/src/WABinary/decode.ts +308 -0
  78. package/src/WABinary/encode.ts +258 -0
  79. package/src/WABinary/generic-utils.ts +141 -0
  80. package/src/WABinary/index.ts +5 -0
  81. package/src/WABinary/jid-utils.ts +128 -0
  82. package/src/WABinary/types.ts +17 -0
  83. package/src/WAM/BinaryInfo.ts +12 -0
  84. package/src/WAM/constants.ts +22889 -0
  85. package/src/WAM/encode.ts +169 -0
  86. package/src/WAM/index.ts +3 -0
  87. package/src/WAUSync/Protocols/USyncContactProtocol.ts +32 -0
  88. package/src/WAUSync/Protocols/USyncDeviceProtocol.ts +78 -0
  89. package/src/WAUSync/Protocols/USyncDisappearingModeProtocol.ts +35 -0
  90. package/src/WAUSync/Protocols/USyncStatusProtocol.ts +44 -0
  91. package/src/WAUSync/Protocols/UsyncBotProfileProtocol.ts +76 -0
  92. package/src/WAUSync/Protocols/UsyncLIDProtocol.ts +33 -0
  93. package/src/WAUSync/Protocols/index.ts +4 -0
  94. package/src/WAUSync/USyncQuery.ts +133 -0
  95. package/src/WAUSync/USyncUser.ts +32 -0
  96. package/src/WAUSync/index.ts +3 -0
  97. package/src/__tests__/Signal/Group/sender-key-state-regression.test.ts +22 -0
  98. package/src/__tests__/Signal/lid-mapping.test.ts +47 -0
  99. package/src/__tests__/Socket/identity-change-handling.test.ts +207 -0
  100. package/src/__tests__/TestUtils/session.ts +47 -0
  101. package/src/__tests__/Utils/generics.test.ts +70 -0
  102. package/src/__tests__/Utils/history.test.ts +362 -0
  103. package/src/__tests__/Utils/messages-media.test.ts +405 -0
  104. package/src/__tests__/Utils/noise-handler.test.ts +339 -0
  105. package/src/__tests__/Utils/process-message.test.ts +138 -0
  106. package/src/__tests__/Utils/process-sync-action.test.ts +359 -0
  107. package/src/__tests__/Utils/reporting-utils.test.ts +160 -0
  108. package/src/__tests__/Utils/signal.test.ts +69 -0
  109. package/src/__tests__/Utils/sync-action-utils.test.ts +183 -0
  110. package/src/__tests__/binary/connection-deadlock.test.ts +54 -0
  111. package/src/__tests__/e2e/fetch-wa-web-version.test-e2e.ts +60 -0
  112. package/src/__tests__/e2e/send-receive-message.test-e2e.ts +644 -0
  113. package/src/__tests__/proto-tojson-long.test.ts +31 -0
  114. package/src/index.ts +13 -0
@@ -0,0 +1,183 @@
1
+ import { jest } from '@jest/globals'
2
+ import type { ILogger } from '../../Utils/logger'
3
+ import { processContactAction } from '../../Utils/sync-action-utils'
4
+
5
+ describe('processContactAction', () => {
6
+ const mockLogger: ILogger = {
7
+ warn: jest.fn(),
8
+ info: jest.fn(),
9
+ debug: jest.fn(),
10
+ error: jest.fn(),
11
+ trace: jest.fn(),
12
+ child: jest.fn(() => mockLogger),
13
+ level: 'silent'
14
+ } as unknown as ILogger
15
+
16
+ beforeEach(() => {
17
+ jest.clearAllMocks()
18
+ })
19
+
20
+ describe('contacts.upsert', () => {
21
+ it('emits with phoneNumber from id when id is PN user', () => {
22
+ const action = { fullName: 'John Doe', lidJid: '123456789@lid', pnJid: null }
23
+ const id = '5511999999999@s.whatsapp.net'
24
+
25
+ const results = processContactAction(action, id)
26
+
27
+ expect(results).toContainEqual({
28
+ event: 'contacts.upsert',
29
+ data: [
30
+ {
31
+ id: '5511999999999@s.whatsapp.net',
32
+ name: 'John Doe',
33
+ lid: '123456789@lid',
34
+ phoneNumber: '5511999999999@s.whatsapp.net'
35
+ }
36
+ ]
37
+ })
38
+ })
39
+
40
+ it('uses pnJid as phoneNumber fallback when id is LID user', () => {
41
+ const action = { fullName: 'John Doe', lidJid: null, pnJid: '5511888888888@s.whatsapp.net' }
42
+ const id = '123456789@lid'
43
+
44
+ const results = processContactAction(action, id)
45
+
46
+ expect(results).toContainEqual({
47
+ event: 'contacts.upsert',
48
+ data: [
49
+ {
50
+ id: '123456789@lid',
51
+ name: 'John Doe',
52
+ lid: undefined,
53
+ phoneNumber: '5511888888888@s.whatsapp.net'
54
+ }
55
+ ]
56
+ })
57
+ })
58
+
59
+ it('handles undefined fullName', () => {
60
+ const action = { fullName: undefined, lidJid: '123456789@lid', pnJid: null }
61
+ const id = '5511999999999@s.whatsapp.net'
62
+
63
+ const results = processContactAction(action, id)
64
+
65
+ const contactData = results.find(r => r.event === 'contacts.upsert')!.data
66
+ expect(contactData[0]!.name).toBeUndefined()
67
+ })
68
+ })
69
+
70
+ describe('lid-mapping.update', () => {
71
+ it('emits when LID and PN are valid', () => {
72
+ const action = { fullName: 'John Doe', lidJid: '123456789@lid', pnJid: null }
73
+ const id = '5511999999999@s.whatsapp.net'
74
+
75
+ const results = processContactAction(action, id)
76
+
77
+ expect(results).toContainEqual({
78
+ event: 'lid-mapping.update',
79
+ data: { lid: '123456789@lid', pn: '5511999999999@s.whatsapp.net' }
80
+ })
81
+ })
82
+
83
+ it('handles LID with device ID suffix', () => {
84
+ const action = { fullName: 'Contact', lidJid: '173233882013816:99@lid', pnJid: null }
85
+ const id = '5511999999999@s.whatsapp.net'
86
+
87
+ const results = processContactAction(action, id)
88
+
89
+ expect(results).toContainEqual({
90
+ event: 'lid-mapping.update',
91
+ data: { lid: '173233882013816:99@lid', pn: '5511999999999@s.whatsapp.net' }
92
+ })
93
+ })
94
+
95
+ it('does NOT emit when lidJid is missing', () => {
96
+ const action = { fullName: 'John Doe', lidJid: null, pnJid: null }
97
+ const id = '5511999999999@s.whatsapp.net'
98
+
99
+ const results = processContactAction(action, id)
100
+
101
+ expect(results.find(r => r.event === 'lid-mapping.update')).toBeUndefined()
102
+ })
103
+
104
+ it('does NOT emit when id is LID user (not PN)', () => {
105
+ const action = { fullName: 'John Doe', lidJid: '123456789@lid', pnJid: null }
106
+ const id = '987654321@lid'
107
+
108
+ const results = processContactAction(action, id)
109
+
110
+ expect(results.find(r => r.event === 'lid-mapping.update')).toBeUndefined()
111
+ })
112
+
113
+ it('does NOT emit when lidJid is invalid format', () => {
114
+ const action = { fullName: 'John Doe', lidJid: 'invalid-lid-format', pnJid: null }
115
+ const id = '5511999999999@s.whatsapp.net'
116
+
117
+ const results = processContactAction(action, id)
118
+
119
+ expect(results.find(r => r.event === 'lid-mapping.update')).toBeUndefined()
120
+ })
121
+
122
+ it('does NOT emit for group JIDs', () => {
123
+ const action = { fullName: 'Group', lidJid: '123456789@lid', pnJid: null }
124
+ const id = '123456789012345678@g.us'
125
+
126
+ const results = processContactAction(action, id)
127
+
128
+ expect(results.find(r => r.event === 'lid-mapping.update')).toBeUndefined()
129
+ })
130
+ })
131
+
132
+ describe('missing id', () => {
133
+ it('returns empty array and logs warning when id is undefined', () => {
134
+ const action = { fullName: 'John Doe', lidJid: '123456789@lid', pnJid: null }
135
+
136
+ const results = processContactAction(action, undefined, mockLogger)
137
+
138
+ expect(results).toEqual([])
139
+ expect(mockLogger.warn).toHaveBeenCalledWith(
140
+ { hasFullName: true, hasLidJid: true, hasPnJid: false },
141
+ 'contactAction sync: missing id in index'
142
+ )
143
+ })
144
+
145
+ it('returns empty array when id is empty string', () => {
146
+ const action = { fullName: 'John Doe', lidJid: '123456789@lid', pnJid: null }
147
+
148
+ const results = processContactAction(action, '', mockLogger)
149
+
150
+ expect(results).toEqual([])
151
+ })
152
+ })
153
+
154
+ describe('PN extraction from index[1] (pnJid is always null)', () => {
155
+ it('extracts PN from id when pnJid is null', () => {
156
+ // In real WhatsApp data, pnJid is ALWAYS null - PN comes from index[1]
157
+ const action = { fullName: 'Test Contact', lidJid: '111222333@lid', pnJid: null }
158
+ const id = '5599887766@s.whatsapp.net'
159
+
160
+ const results = processContactAction(action, id)
161
+
162
+ const contactData = results.find(r => r.event === 'contacts.upsert')!.data
163
+ expect(contactData[0]!.phoneNumber).toBe('5599887766@s.whatsapp.net')
164
+ expect(contactData[0]!.lid).toBe('111222333@lid')
165
+
166
+ const mapping = results.find(r => r.event === 'lid-mapping.update')!.data
167
+ expect(mapping).toEqual({ lid: '111222333@lid', pn: '5599887766@s.whatsapp.net' })
168
+ })
169
+
170
+ it('prefers id over pnJid when id is PN user', () => {
171
+ const action = { fullName: 'Test', lidJid: '111222333@lid', pnJid: '1111111111@s.whatsapp.net' }
172
+ const id = '9999999999@s.whatsapp.net'
173
+
174
+ const results = processContactAction(action, id)
175
+
176
+ const contactData = results.find(r => r.event === 'contacts.upsert')!.data
177
+ expect(contactData[0]!.phoneNumber).toBe('9999999999@s.whatsapp.net')
178
+
179
+ const mapping = results.find(r => r.event === 'lid-mapping.update')!.data
180
+ expect(mapping.pn).toBe('9999999999@s.whatsapp.net')
181
+ })
182
+ })
183
+ })
@@ -0,0 +1,54 @@
1
+ import { jest } from '@jest/globals'
2
+ import { proto, type WAMessage } from '../..'
3
+ import { DEFAULT_CONNECTION_CONFIG } from '../../Defaults'
4
+ import makeWASocket from '../../Socket'
5
+ import { makeSession, mockWebSocket } from '../TestUtils/session'
6
+
7
+ mockWebSocket()
8
+
9
+ describe('Connection Deadlock Test', () => {
10
+ it('should not deadlock when history sync is disabled', async () => {
11
+ const { state, clear } = await makeSession()
12
+
13
+ state.creds.me = { id: '1234567890:1@s.whatsapp.net', name: 'Test User' }
14
+
15
+ const sock = makeWASocket({
16
+ ...DEFAULT_CONNECTION_CONFIG,
17
+ auth: state,
18
+ shouldSyncHistoryMessage: () => false
19
+ })
20
+
21
+ const regularMessageListener = jest.fn()
22
+ sock.ev.on('messages.upsert', regularMessageListener)
23
+
24
+ // 1. Simulate receiving pending notifications. This activates the buffer.
25
+ sock.ev.emit('connection.update', { receivedPendingNotifications: true })
26
+
27
+ // 2. Now, emit a regular message. Because the previous step should have
28
+ // flushed the buffer, this message should be processed immediately.
29
+ const regularMessage = proto.WebMessageInfo.fromObject({
30
+ key: { remoteJid: '1234567890@s.whatsapp.net', fromMe: false, id: 'REGULAR_MSG_1' },
31
+ messageTimestamp: Date.now() / 1000,
32
+ message: { conversation: 'Hello, world!' }
33
+ }) as WAMessage
34
+ sock.ev.emit('messages.upsert', { messages: [regularMessage], type: 'notify' })
35
+ // Wait for the event loop to process any final events.
36
+ await new Promise(resolve => setTimeout(resolve, 50))
37
+
38
+ // 3. Check if the regular message listener was called.
39
+ expect(regularMessageListener).toHaveBeenCalledTimes(1)
40
+ expect(regularMessageListener).toHaveBeenCalledWith(
41
+ expect.objectContaining({
42
+ messages: expect.arrayContaining([
43
+ expect.objectContaining({
44
+ key: regularMessage.key
45
+ })
46
+ ]),
47
+ type: 'notify'
48
+ })
49
+ )
50
+
51
+ await sock.end(new Error('Test completed'))
52
+ await clear()
53
+ })
54
+ })
@@ -0,0 +1,60 @@
1
+ import { jest } from '@jest/globals'
2
+ import { fetchLatestWaWebVersion } from '../../Utils/generics'
3
+
4
+ describe('fetchLatestWaWebVersion Integration Tests', () => {
5
+ jest.setTimeout(10000)
6
+
7
+ it('should successfully fetch the latest WhatsApp Web version from real API', async () => {
8
+ const result = await fetchLatestWaWebVersion()
9
+
10
+ expect(Array.isArray(result.version)).toBe(true)
11
+ expect(result.version).toHaveLength(3)
12
+ expect(typeof result.version[0]).toBe('number')
13
+ expect(typeof result.version[1]).toBe('number')
14
+ expect(typeof result.version[2]).toBe('number')
15
+
16
+ expect(typeof result.isLatest).toBe('boolean')
17
+
18
+ if (!result.isLatest) {
19
+ expect(result.error).toBeDefined()
20
+ }
21
+ })
22
+
23
+ it('should handle custom headers correctly', async () => {
24
+ const customHeaders = {
25
+ accept:
26
+ 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
27
+ 'accept-language': 'en-US,en;q=0.9',
28
+ 'cache-control': 'max-age=0',
29
+ 'sec-ch-prefers-color-scheme': 'dark',
30
+ 'sec-fetch-dest': 'document',
31
+ 'sec-fetch-mode': 'navigate',
32
+ 'sec-fetch-site': 'none',
33
+ 'upgrade-insecure-requests': '1'
34
+ }
35
+
36
+ const result = await fetchLatestWaWebVersion({
37
+ headers: customHeaders
38
+ })
39
+
40
+ expect(Array.isArray(result.version)).toBe(true)
41
+ expect(result.version).toHaveLength(3)
42
+ expect(result.isLatest).toBe(true)
43
+ })
44
+
45
+ it('should fallback gracefully when client_revision is not found', async () => {
46
+ const result = await fetchLatestWaWebVersion()
47
+
48
+ expect(result).toHaveProperty('version')
49
+ expect(result).toHaveProperty('isLatest')
50
+ expect(Array.isArray(result.version)).toBe(true)
51
+ })
52
+
53
+ it('should handle network timeouts gracefully', async () => {
54
+ const result = await fetchLatestWaWebVersion()
55
+
56
+ expect(result).toHaveProperty('version')
57
+ expect(result).toHaveProperty('isLatest')
58
+ expect(Array.isArray(result.version)).toBe(true)
59
+ })
60
+ })