@d0v3riz/baileys 7.0.0-rc.8 → 7.0.0-rc13

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 (213) hide show
  1. package/README.md +21 -15
  2. package/WAProto/fix-imports.js +74 -18
  3. package/WAProto/index.js +201 -160
  4. package/lib/Defaults/index.d.ts +19 -6
  5. package/lib/Defaults/index.d.ts.map +1 -1
  6. package/lib/Defaults/index.js +24 -10
  7. package/lib/Defaults/index.js.map +1 -1
  8. package/lib/Signal/libsignal.d.ts.map +1 -1
  9. package/lib/Signal/libsignal.js +105 -16
  10. package/lib/Signal/libsignal.js.map +1 -1
  11. package/lib/Signal/lid-mapping.d.ts +8 -8
  12. package/lib/Signal/lid-mapping.d.ts.map +1 -1
  13. package/lib/Signal/lid-mapping.js +176 -70
  14. package/lib/Signal/lid-mapping.js.map +1 -1
  15. package/lib/Socket/Client/websocket.d.ts +1 -1
  16. package/lib/Socket/Client/websocket.d.ts.map +1 -1
  17. package/lib/Socket/Client/websocket.js +5 -1
  18. package/lib/Socket/Client/websocket.js.map +1 -1
  19. package/lib/Socket/business.d.ts +36 -8
  20. package/lib/Socket/business.d.ts.map +1 -1
  21. package/lib/Socket/business.js +11 -8
  22. package/lib/Socket/business.js.map +1 -1
  23. package/lib/Socket/chats.d.ts +29 -5
  24. package/lib/Socket/chats.d.ts.map +1 -1
  25. package/lib/Socket/chats.js +292 -63
  26. package/lib/Socket/chats.js.map +1 -1
  27. package/lib/Socket/communities.d.ts +36 -8
  28. package/lib/Socket/communities.d.ts.map +1 -1
  29. package/lib/Socket/groups.d.ts +27 -5
  30. package/lib/Socket/groups.d.ts.map +1 -1
  31. package/lib/Socket/groups.js +20 -0
  32. package/lib/Socket/groups.js.map +1 -1
  33. package/lib/Socket/index.d.ts +36 -8
  34. package/lib/Socket/index.d.ts.map +1 -1
  35. package/lib/Socket/index.js +0 -6
  36. package/lib/Socket/index.js.map +1 -1
  37. package/lib/Socket/messages-recv.d.ts +40 -11
  38. package/lib/Socket/messages-recv.d.ts.map +1 -1
  39. package/lib/Socket/messages-recv.js +841 -354
  40. package/lib/Socket/messages-recv.js.map +1 -1
  41. package/lib/Socket/messages-send.d.ts +35 -6
  42. package/lib/Socket/messages-send.d.ts.map +1 -1
  43. package/lib/Socket/messages-send.js +263 -92
  44. package/lib/Socket/messages-send.js.map +1 -1
  45. package/lib/Socket/mex.d.ts.map +1 -1
  46. package/lib/Socket/mex.js +2 -0
  47. package/lib/Socket/mex.js.map +1 -1
  48. package/lib/Socket/newsletter.d.ts +27 -5
  49. package/lib/Socket/newsletter.d.ts.map +1 -1
  50. package/lib/Socket/newsletter.js +35 -3
  51. package/lib/Socket/newsletter.js.map +1 -1
  52. package/lib/Socket/socket.d.ts +9 -3
  53. package/lib/Socket/socket.d.ts.map +1 -1
  54. package/lib/Socket/socket.js +124 -39
  55. package/lib/Socket/socket.js.map +1 -1
  56. package/lib/Types/Auth.d.ts +2 -0
  57. package/lib/Types/Auth.d.ts.map +1 -1
  58. package/lib/Types/Call.d.ts +2 -1
  59. package/lib/Types/Call.d.ts.map +1 -1
  60. package/lib/Types/Chat.d.ts +1 -0
  61. package/lib/Types/Chat.d.ts.map +1 -1
  62. package/lib/Types/Contact.d.ts +2 -0
  63. package/lib/Types/Contact.d.ts.map +1 -1
  64. package/lib/Types/Events.d.ts +60 -6
  65. package/lib/Types/Events.d.ts.map +1 -1
  66. package/lib/Types/GroupMetadata.d.ts +4 -0
  67. package/lib/Types/GroupMetadata.d.ts.map +1 -1
  68. package/lib/Types/Message.d.ts +20 -5
  69. package/lib/Types/Message.d.ts.map +1 -1
  70. package/lib/Types/Message.js.map +1 -1
  71. package/lib/Types/{Newsletter.d.ts → Mex.d.ts} +13 -5
  72. package/lib/Types/Mex.d.ts.map +1 -0
  73. package/lib/Types/{Newsletter.js → Mex.js} +11 -3
  74. package/lib/Types/Mex.js.map +1 -0
  75. package/lib/Types/Signal.d.ts +12 -1
  76. package/lib/Types/Signal.d.ts.map +1 -1
  77. package/lib/Types/Socket.d.ts +3 -0
  78. package/lib/Types/Socket.d.ts.map +1 -1
  79. package/lib/Types/State.d.ts +58 -0
  80. package/lib/Types/State.d.ts.map +1 -1
  81. package/lib/Types/State.js +43 -0
  82. package/lib/Types/State.js.map +1 -1
  83. package/lib/Types/index.d.ts +1 -1
  84. package/lib/Types/index.d.ts.map +1 -1
  85. package/lib/Types/index.js +1 -1
  86. package/lib/Types/index.js.map +1 -1
  87. package/lib/Utils/auth-utils.d.ts +5 -0
  88. package/lib/Utils/auth-utils.d.ts.map +1 -1
  89. package/lib/Utils/auth-utils.js +65 -20
  90. package/lib/Utils/auth-utils.js.map +1 -1
  91. package/lib/Utils/chat-utils.d.ts +31 -1
  92. package/lib/Utils/chat-utils.d.ts.map +1 -1
  93. package/lib/Utils/chat-utils.js +180 -71
  94. package/lib/Utils/chat-utils.js.map +1 -1
  95. package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
  96. package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
  97. package/lib/Utils/companion-reg-client-utils.js +35 -0
  98. package/lib/Utils/companion-reg-client-utils.js.map +1 -0
  99. package/lib/Utils/crypto.d.ts +4 -8
  100. package/lib/Utils/crypto.d.ts.map +1 -1
  101. package/lib/Utils/crypto.js +2 -26
  102. package/lib/Utils/crypto.js.map +1 -1
  103. package/lib/Utils/decode-wa-message.d.ts +18 -0
  104. package/lib/Utils/decode-wa-message.d.ts.map +1 -1
  105. package/lib/Utils/decode-wa-message.js +34 -0
  106. package/lib/Utils/decode-wa-message.js.map +1 -1
  107. package/lib/Utils/event-buffer.d.ts +2 -0
  108. package/lib/Utils/event-buffer.d.ts.map +1 -1
  109. package/lib/Utils/event-buffer.js +82 -8
  110. package/lib/Utils/event-buffer.js.map +1 -1
  111. package/lib/Utils/generics.d.ts +2 -0
  112. package/lib/Utils/generics.d.ts.map +1 -1
  113. package/lib/Utils/generics.js +13 -1
  114. package/lib/Utils/generics.js.map +1 -1
  115. package/lib/Utils/history.d.ts +8 -3
  116. package/lib/Utils/history.d.ts.map +1 -1
  117. package/lib/Utils/history.js +58 -14
  118. package/lib/Utils/history.js.map +1 -1
  119. package/lib/Utils/identity-change-handler.d.ts +44 -0
  120. package/lib/Utils/identity-change-handler.d.ts.map +1 -0
  121. package/lib/Utils/identity-change-handler.js +50 -0
  122. package/lib/Utils/identity-change-handler.js.map +1 -0
  123. package/lib/Utils/index.d.ts +3 -0
  124. package/lib/Utils/index.d.ts.map +1 -1
  125. package/lib/Utils/index.js +3 -0
  126. package/lib/Utils/index.js.map +1 -1
  127. package/lib/Utils/link-preview.js +2 -2
  128. package/lib/Utils/link-preview.js.map +1 -1
  129. package/lib/Utils/lt-hash.d.ts +7 -12
  130. package/lib/Utils/lt-hash.d.ts.map +1 -1
  131. package/lib/Utils/lt-hash.js +2 -42
  132. package/lib/Utils/lt-hash.js.map +1 -1
  133. package/lib/Utils/make-mutex.d.ts +1 -0
  134. package/lib/Utils/make-mutex.d.ts.map +1 -1
  135. package/lib/Utils/make-mutex.js +20 -27
  136. package/lib/Utils/make-mutex.js.map +1 -1
  137. package/lib/Utils/message-retry-manager.d.ts +35 -2
  138. package/lib/Utils/message-retry-manager.d.ts.map +1 -1
  139. package/lib/Utils/message-retry-manager.js +98 -5
  140. package/lib/Utils/message-retry-manager.js.map +1 -1
  141. package/lib/Utils/messages-media.d.ts +22 -3
  142. package/lib/Utils/messages-media.d.ts.map +1 -1
  143. package/lib/Utils/messages-media.js +169 -44
  144. package/lib/Utils/messages-media.js.map +1 -1
  145. package/lib/Utils/messages.d.ts +2 -0
  146. package/lib/Utils/messages.d.ts.map +1 -1
  147. package/lib/Utils/messages.js +70 -27
  148. package/lib/Utils/messages.js.map +1 -1
  149. package/lib/Utils/noise-handler.d.ts +4 -4
  150. package/lib/Utils/noise-handler.d.ts.map +1 -1
  151. package/lib/Utils/noise-handler.js +139 -85
  152. package/lib/Utils/noise-handler.js.map +1 -1
  153. package/lib/Utils/offline-node-processor.d.ts +17 -0
  154. package/lib/Utils/offline-node-processor.d.ts.map +1 -0
  155. package/lib/Utils/offline-node-processor.js +40 -0
  156. package/lib/Utils/offline-node-processor.js.map +1 -0
  157. package/lib/Utils/process-message.d.ts.map +1 -1
  158. package/lib/Utils/process-message.js +165 -15
  159. package/lib/Utils/process-message.js.map +1 -1
  160. package/lib/Utils/reporting-utils.d.ts +11 -0
  161. package/lib/Utils/reporting-utils.d.ts.map +1 -0
  162. package/lib/Utils/reporting-utils.js +258 -0
  163. package/lib/Utils/reporting-utils.js.map +1 -0
  164. package/lib/Utils/signal.d.ts +13 -0
  165. package/lib/Utils/signal.d.ts.map +1 -1
  166. package/lib/Utils/signal.js +42 -0
  167. package/lib/Utils/signal.js.map +1 -1
  168. package/lib/Utils/stanza-ack.d.ts +11 -0
  169. package/lib/Utils/stanza-ack.d.ts.map +1 -0
  170. package/lib/Utils/stanza-ack.js +38 -0
  171. package/lib/Utils/stanza-ack.js.map +1 -0
  172. package/lib/Utils/sync-action-utils.d.ts +19 -0
  173. package/lib/Utils/sync-action-utils.d.ts.map +1 -0
  174. package/lib/Utils/sync-action-utils.js +49 -0
  175. package/lib/Utils/sync-action-utils.js.map +1 -0
  176. package/lib/Utils/tc-token-utils.d.ts +37 -0
  177. package/lib/Utils/tc-token-utils.d.ts.map +1 -0
  178. package/lib/Utils/tc-token-utils.js +163 -0
  179. package/lib/Utils/tc-token-utils.js.map +1 -0
  180. package/lib/Utils/validate-connection.d.ts.map +1 -1
  181. package/lib/Utils/validate-connection.js +4 -1
  182. package/lib/Utils/validate-connection.js.map +1 -1
  183. package/lib/WABinary/decode.d.ts.map +1 -1
  184. package/lib/WABinary/decode.js +24 -0
  185. package/lib/WABinary/decode.js.map +1 -1
  186. package/lib/WABinary/encode.js +5 -1
  187. package/lib/WABinary/encode.js.map +1 -1
  188. package/lib/WABinary/generic-utils.d.ts +1 -1
  189. package/lib/WABinary/generic-utils.d.ts.map +1 -1
  190. package/lib/WABinary/generic-utils.js +19 -8
  191. package/lib/WABinary/generic-utils.js.map +1 -1
  192. package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -1
  193. package/lib/WAUSync/Protocols/USyncContactProtocol.js +26 -3
  194. package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -1
  195. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
  196. package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
  197. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
  198. package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
  199. package/lib/WAUSync/Protocols/index.d.ts +1 -0
  200. package/lib/WAUSync/Protocols/index.d.ts.map +1 -1
  201. package/lib/WAUSync/Protocols/index.js +1 -0
  202. package/lib/WAUSync/Protocols/index.js.map +1 -1
  203. package/lib/WAUSync/USyncQuery.d.ts +1 -0
  204. package/lib/WAUSync/USyncQuery.d.ts.map +1 -1
  205. package/lib/WAUSync/USyncQuery.js +6 -2
  206. package/lib/WAUSync/USyncQuery.js.map +1 -1
  207. package/lib/WAUSync/USyncUser.d.ts +4 -0
  208. package/lib/WAUSync/USyncUser.d.ts.map +1 -1
  209. package/lib/WAUSync/USyncUser.js +8 -0
  210. package/lib/WAUSync/USyncUser.js.map +1 -1
  211. package/package.json +39 -8
  212. package/lib/Types/Newsletter.d.ts.map +0 -1
  213. package/lib/Types/Newsletter.js.map +0 -1
package/README.md CHANGED
@@ -15,6 +15,12 @@ This is a temporary README.md, the new guide is in development and will this fil
15
15
 
16
16
  New guide link: https://baileys.wiki
17
17
 
18
+ # Get Support
19
+
20
+ If you'd like business to enterprise-level support from Rajeh, the current maintainer of Baileys, you can book a video chat. Book a 1 hour time slot by contacting him on Discord or pre-ordering [here](https://purpshell.dev/book). The earlier you pre-order the better, as his time slots usually fill up very quickly. He offers immense value per hour and will answer all your questions before the time runs out.
21
+
22
+ If you are a business, we encourage you to contribute back to the high development costs of the project and to feed the maintainers who dump tens of hours a week on this. You can do so by booking meetings or sponsoring below. All support, even in bona fide / contribution hours, is welcome by businesses of all sizes. This is not condoning or endorsing businesses to use the library. See the Disclaimer below.
23
+
18
24
  # Sponsor
19
25
  If you'd like to financially support this project, you can do so by supporting the current maintainer [here](https://purpshell.dev/sponsor).
20
26
 
@@ -66,7 +72,7 @@ import makeWASocket from '@whiskeysockets/baileys'
66
72
  # Links
67
73
 
68
74
  - [Discord](https://discord.gg/WeJM5FP9GG)
69
- - [Docs](https://guide.whiskeysockets.io/)
75
+ - [Docs](https://baileys.wiki/docs/intro/)
70
76
 
71
77
  # Index
72
78
 
@@ -181,12 +187,12 @@ WhatsApp provides a multi-device API that allows Baileys to be authenticated as
181
187
  > **[Here](#example-to-start) is a simple example of event handling**
182
188
 
183
189
  > [!TIP]
184
- > **You can see all supported socket configs [here](https://baileys.whiskeysockets.io/types/SocketConfig.html) (Recommended)**
190
+ > **You can see all supported socket configs in the [SocketConfig type alias](https://baileys.wiki/docs/api/type-aliases/SocketConfig/) (Recommended)**
185
191
 
186
192
  ### Starting socket with **QR-CODE**
187
193
 
188
194
  > [!TIP]
189
- > You can customize browser name if you connect with **QR-CODE**, with `Browser` constant, we have some browsers config, **see [here](https://baileys.whiskeysockets.io/types/BrowsersMap.html)**
195
+ > You can customize browser name if you connect with **QR-CODE**, with `Browser` constant, we have some browsers config, **see the [BrowsersMap type alias](https://baileys.wiki/docs/api/type-aliases/BrowsersMap/)**
190
196
 
191
197
  ```ts
192
198
  import makeWASocket from '@whiskeysockets/baileys'
@@ -306,7 +312,7 @@ sock.ev.on('creds.update', saveCreds)
306
312
  They're all nicely typed up, so you shouldn't have any issues with an Intellisense editor like VS Code.
307
313
 
308
314
  > [!IMPORTANT]
309
- > **The events are [these](https://baileys.whiskeysockets.io/types/BaileysEventMap.html)**, it's important you see all events
315
+ > **The events are in the [BaileysEventMap type alias](https://baileys.wiki/docs/api/type-aliases/BaileysEventMap/)**, it's important you see all events
310
316
 
311
317
  You can listen to these events like this:
312
318
  ```ts
@@ -456,8 +462,8 @@ The store also provides some simple functions such as `loadMessages` that utiliz
456
462
  ## Sending Messages
457
463
 
458
464
  - Send all types of messages with a single function
459
- - **[Here](https://baileys.whiskeysockets.io/types/AnyMessageContent.html) you can see all message contents supported, like text message**
460
- - **[Here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html) you can see all options supported, like quote message**
465
+ - **In the [AnyMessageContent type alias](https://baileys.wiki/docs/api/type-aliases/AnyMessageContent/) you can see all message contents supported, like text message**
466
+ - **In the [MiscMessageGenerationOptions type alias](https://baileys.wiki/docs/api/type-aliases/MiscMessageGenerationOptions/) you can see all options supported, like quote message**
461
467
 
462
468
  ```ts
463
469
  const jid: string
@@ -492,7 +498,7 @@ await sock.sendMessage(
492
498
  ```
493
499
 
494
500
  #### Forward Messages
495
- - You need to have message object, can be retrieved from [store](#implementing-a-data-store) or use a [message](https://baileys.whiskeysockets.io/types/WAMessage.html) object
501
+ - You need to have message object, can be retrieved from [store](#implementing-a-data-store) or use a [message](https://baileys.wiki/docs/api/type-aliases/WAMessage/) object
496
502
  ```ts
497
503
  const msg = getMessageFromStore() // implement this on your end
498
504
  await sock.sendMessage(jid, { forward: msg }) // WA forward the message!
@@ -531,7 +537,7 @@ await sock.sendMessage(
531
537
  ```
532
538
 
533
539
  #### Reaction Message
534
- - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.whiskeysockets.io/types/WAMessageKey.html) object
540
+ - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) object
535
541
  ```ts
536
542
  await sock.sendMessage(
537
543
  jid,
@@ -545,7 +551,7 @@ await sock.sendMessage(
545
551
  ```
546
552
 
547
553
  #### Pin Message
548
- - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.whiskeysockets.io/types/WAMessageKey.html) object
554
+ - You need to pass the key of message, you can retrieve from [store](#implementing-a-data-store) or use a [key](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) object
549
555
 
550
556
  - Time can be:
551
557
 
@@ -603,7 +609,7 @@ await sock.sendMessage(
603
609
  Sending media (video, stickers, images) is easier & more efficient than ever.
604
610
 
605
611
  > [!NOTE]
606
- > In media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
612
+ > In media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more in the [WAMediaUpload type alias](https://baileys.wiki/docs/api/type-aliases/WAMediaUpload/)
607
613
 
608
614
  - When specifying a media url, Baileys never loads the entire buffer into memory; it even encrypts the media as a readable stream.
609
615
 
@@ -768,7 +774,7 @@ await sock.rejectCall(callId, callFrom)
768
774
  ## Send States in Chat
769
775
 
770
776
  ### Reading Messages
771
- - A set of message [keys](https://baileys.whiskeysockets.io/types/WAMessageKey.html) must be explicitly marked read now.
777
+ - A set of message [keys](https://baileys.wiki/docs/api/type-aliases/WAMessageKey/) must be explicitly marked read now.
772
778
  - You cannot mark an entire 'chat' read as it were with Baileys Web.
773
779
  This means you have to keep track of unread messages.
774
780
 
@@ -783,7 +789,7 @@ On a `WAMessage`, the `messageID` can be accessed using ```messageID = message.k
783
789
 
784
790
  ### Update Presence
785
791
 
786
- - ``` presence ``` can be one of [these](https://baileys.whiskeysockets.io/types/WAPresence.html)
792
+ - ``` presence ``` can be one of the values in the [WAPresence type alias](https://baileys.wiki/docs/api/type-aliases/WAPresence/)
787
793
  - The presence expires after about 10 seconds.
788
794
  - This lets the person/group with `jid` know whether you're online, offline, typing etc.
789
795
 
@@ -985,7 +991,7 @@ await sock.updateProfileName('My name')
985
991
  - To change your display picture or a group's
986
992
 
987
993
  > [!NOTE]
988
- > Like media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more [here](https://baileys.whiskeysockets.io/types/WAMediaUpload.html)
994
+ > Like media messages, you can pass `{ stream: Stream }` or `{ url: Url }` or `Buffer` directly, you can see more in the [WAMediaUpload type alias](https://baileys.wiki/docs/api/type-aliases/WAMediaUpload/)
989
995
 
990
996
  ```ts
991
997
  await sock.updateProfilePicture(jid, { url: './new-profile-picture.jpeg' })
@@ -1197,8 +1203,8 @@ await sock.sendMessage(
1197
1203
  }
1198
1204
  )
1199
1205
  ```
1200
- - Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see [here](https://baileys.whiskeysockets.io/types/AnyRegularMessageContent.html)
1201
- - You can add `backgroundColor` and other options in the message options, see [here](https://baileys.whiskeysockets.io/types/MiscMessageGenerationOptions.html)
1206
+ - Message body can be a `extendedTextMessage` or `imageMessage` or `videoMessage` or `voiceMessage`, see the [AnyRegularMessageContent type alias](https://baileys.wiki/docs/api/type-aliases/AnyRegularMessageContent/)
1207
+ - You can add `backgroundColor` and other options in the message options, see the [MiscMessageGenerationOptions type alias](https://baileys.wiki/docs/api/type-aliases/MiscMessageGenerationOptions/)
1202
1208
  - `broadcast: true` enables broadcast mode
1203
1209
  - `statusJidList`: a list of people that you can get which you need to provide, which are the people who will get this status message.
1204
1210
 
@@ -1,29 +1,85 @@
1
1
  import { readFileSync, writeFileSync } from 'fs';
2
2
  import { exit } from 'process';
3
3
 
4
- const filePath = './index.js';
4
+ const filePath = './index.js'
5
5
 
6
6
  try {
7
- // Read the file
8
- let content = readFileSync(filePath, 'utf8');
7
+ let content = readFileSync(filePath, 'utf8')
9
8
 
10
- // Fix the import statement
11
- content = content.replace(
12
- /import \* as (\$protobuf) from/g,
13
- 'import $1 from'
14
- );
9
+ content = content.replace(/import \* as (\$protobuf) from/g, 'import $1 from')
10
+ content = content.replace(/(['"])protobufjs\/minimal(['"])/g, '$1protobufjs/minimal.js$2')
15
11
 
16
- // add missing extension to the import
17
- content = content.replace(
18
- /(['"])protobufjs\/minimal(['"])/g,
19
- '$1protobufjs/minimal.js$2'
20
- );
12
+ const marker = 'const $root = $protobuf.roots["default"] || ($protobuf.roots["default"] = {});\n\n'
13
+ const longToStringHelper =
14
+ 'function longToString(value, unsigned) {\n' +
15
+ '\tif (typeof value === "string") {\n' +
16
+ '\t\treturn value;\n' +
17
+ '\t}\n' +
18
+ '\tif (typeof value === "number") {\n' +
19
+ '\t\treturn String(value);\n' +
20
+ '\t}\n' +
21
+ '\t// Fast path: convert Long {low, high} directly via native BigInt\n' +
22
+ '\t// BigInt.toString() is a native C++ operation, much faster than Long\'s pure JS division loops\n' +
23
+ '\tif (value && typeof value.low === "number" && typeof value.high === "number") {\n' +
24
+ '\t\tconst lo = BigInt(value.low >>> 0);\n' +
25
+ '\t\tconst hi = BigInt(value.high >>> 0);\n' +
26
+ '\t\tconst combined = (hi << 32n) | lo;\n' +
27
+ '\t\tif (!unsigned && value.high < 0) {\n' +
28
+ '\t\t\treturn (combined - (1n << 64n)).toString();\n' +
29
+ '\t\t}\n' +
30
+ '\t\treturn combined.toString();\n' +
31
+ '\t}\n' +
32
+ '\treturn String(value);\n' +
33
+ '}\n\n'
34
+ const longToNumberHelper =
35
+ 'function longToNumber(value, unsigned) {\n' +
36
+ '\tif (typeof value === "number") {\n' +
37
+ '\t\treturn value;\n' +
38
+ '\t}\n' +
39
+ '\tif (typeof value === "string") {\n' +
40
+ '\t\treturn Number(value);\n' +
41
+ '\t}\n' +
42
+ '\t// Fast path: convert Long {low, high} directly via native BigInt\n' +
43
+ '\tif (value && typeof value.low === "number" && typeof value.high === "number") {\n' +
44
+ '\t\tconst lo = BigInt(value.low >>> 0);\n' +
45
+ '\t\tconst hi = BigInt(value.high >>> 0);\n' +
46
+ '\t\tconst combined = (hi << 32n) | lo;\n' +
47
+ '\t\tif (!unsigned && value.high < 0) {\n' +
48
+ '\t\t\treturn Number(combined - (1n << 64n));\n' +
49
+ '\t\t}\n' +
50
+ '\t\treturn Number(combined);\n' +
51
+ '\t}\n' +
52
+ '\treturn Number(value);\n' +
53
+ '}\n\n'
21
54
 
22
- // Write back
23
- writeFileSync(filePath, content, 'utf8');
55
+ if (!content.includes('function longToString(')) {
56
+ const markerIndex = content.indexOf(marker)
57
+ if (markerIndex === -1) {
58
+ throw new Error('Unable to inject Long helpers: marker not found in WAProto index output')
59
+ }
24
60
 
25
- console.log(`✅ Fixed imports in ${filePath}`);
61
+ content = content.replace(marker, `${marker}${longToStringHelper}${longToNumberHelper}`)
62
+ } else {
63
+ const longToStringRegex = /function longToString\(value, unsigned\) {\n[\s\S]*?\n}\n\n/
64
+ const longToNumberRegex = /function longToNumber\(value, unsigned\) {\n[\s\S]*?\n}\n\n/
65
+
66
+ if (!longToStringRegex.test(content) || !longToNumberRegex.test(content)) {
67
+ throw new Error('Unable to update Long helpers: existing definitions not found')
68
+ }
69
+
70
+ content = content.replace(longToStringRegex, longToStringHelper)
71
+ content = content.replace(longToNumberRegex, longToNumberHelper)
72
+ }
73
+
74
+ const longPattern = /([ \t]+d\.(\w+) = )o\.longs === String \? \$util\.Long\.prototype\.toString\.call\(m\.\2\) : o\.longs === Number \? new \$util\.LongBits\(m\.\2\.low >>> 0, m\.\2\.high >>> 0\)\.toNumber\((true)?\) : m\.\2;/g
75
+ content = content.replace(longPattern, (_match, prefix, field, unsignedFlag) => {
76
+ const unsignedArg = unsignedFlag ? ', true' : ''
77
+ return `${prefix}o.longs === String ? longToString(m.${field}${unsignedArg}) : o.longs === Number ? longToNumber(m.${field}${unsignedArg}) : m.${field};`
78
+ })
79
+
80
+ writeFileSync(filePath, content, 'utf8')
81
+ console.log(`✅ Fixed imports in ${filePath}`)
26
82
  } catch (error) {
27
- console.error(`❌ Error fixing imports: ${error.message}`);
28
- exit(1);
83
+ console.error(`❌ Error fixing imports: ${error.message}`)
84
+ exit(1)
29
85
  }