@d0v3riz/baileys 7.0.0-rc.9 → 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.
- package/README.md +21 -15
- package/WAProto/fix-imports.js +74 -18
- package/WAProto/index.js +201 -160
- package/lib/Defaults/index.d.ts +19 -6
- package/lib/Defaults/index.d.ts.map +1 -1
- package/lib/Defaults/index.js +24 -10
- package/lib/Defaults/index.js.map +1 -1
- package/lib/Signal/libsignal.d.ts.map +1 -1
- package/lib/Signal/libsignal.js +105 -16
- package/lib/Signal/libsignal.js.map +1 -1
- package/lib/Signal/lid-mapping.d.ts +8 -8
- package/lib/Signal/lid-mapping.d.ts.map +1 -1
- package/lib/Signal/lid-mapping.js +176 -70
- package/lib/Signal/lid-mapping.js.map +1 -1
- package/lib/Socket/Client/websocket.d.ts +1 -1
- package/lib/Socket/Client/websocket.d.ts.map +1 -1
- package/lib/Socket/Client/websocket.js +5 -1
- package/lib/Socket/Client/websocket.js.map +1 -1
- package/lib/Socket/business.d.ts +36 -8
- package/lib/Socket/business.d.ts.map +1 -1
- package/lib/Socket/business.js +11 -8
- package/lib/Socket/business.js.map +1 -1
- package/lib/Socket/chats.d.ts +29 -5
- package/lib/Socket/chats.d.ts.map +1 -1
- package/lib/Socket/chats.js +292 -63
- package/lib/Socket/chats.js.map +1 -1
- package/lib/Socket/communities.d.ts +36 -8
- package/lib/Socket/communities.d.ts.map +1 -1
- package/lib/Socket/groups.d.ts +27 -5
- package/lib/Socket/groups.d.ts.map +1 -1
- package/lib/Socket/groups.js +20 -0
- package/lib/Socket/groups.js.map +1 -1
- package/lib/Socket/index.d.ts +36 -8
- package/lib/Socket/index.d.ts.map +1 -1
- package/lib/Socket/index.js +0 -6
- package/lib/Socket/index.js.map +1 -1
- package/lib/Socket/messages-recv.d.ts +40 -11
- package/lib/Socket/messages-recv.d.ts.map +1 -1
- package/lib/Socket/messages-recv.js +841 -342
- package/lib/Socket/messages-recv.js.map +1 -1
- package/lib/Socket/messages-send.d.ts +35 -6
- package/lib/Socket/messages-send.d.ts.map +1 -1
- package/lib/Socket/messages-send.js +263 -92
- package/lib/Socket/messages-send.js.map +1 -1
- package/lib/Socket/mex.d.ts.map +1 -1
- package/lib/Socket/mex.js +2 -0
- package/lib/Socket/mex.js.map +1 -1
- package/lib/Socket/newsletter.d.ts +27 -5
- package/lib/Socket/newsletter.d.ts.map +1 -1
- package/lib/Socket/newsletter.js +35 -3
- package/lib/Socket/newsletter.js.map +1 -1
- package/lib/Socket/socket.d.ts +9 -3
- package/lib/Socket/socket.d.ts.map +1 -1
- package/lib/Socket/socket.js +124 -39
- package/lib/Socket/socket.js.map +1 -1
- package/lib/Types/Auth.d.ts +2 -0
- package/lib/Types/Auth.d.ts.map +1 -1
- package/lib/Types/Call.d.ts +2 -1
- package/lib/Types/Call.d.ts.map +1 -1
- package/lib/Types/Chat.d.ts +1 -0
- package/lib/Types/Chat.d.ts.map +1 -1
- package/lib/Types/Contact.d.ts +2 -0
- package/lib/Types/Contact.d.ts.map +1 -1
- package/lib/Types/Events.d.ts +60 -6
- package/lib/Types/Events.d.ts.map +1 -1
- package/lib/Types/GroupMetadata.d.ts +4 -0
- package/lib/Types/GroupMetadata.d.ts.map +1 -1
- package/lib/Types/Message.d.ts +20 -5
- package/lib/Types/Message.d.ts.map +1 -1
- package/lib/Types/Message.js.map +1 -1
- package/lib/Types/{Newsletter.d.ts → Mex.d.ts} +13 -5
- package/lib/Types/Mex.d.ts.map +1 -0
- package/lib/Types/{Newsletter.js → Mex.js} +11 -3
- package/lib/Types/Mex.js.map +1 -0
- package/lib/Types/Signal.d.ts +12 -1
- package/lib/Types/Signal.d.ts.map +1 -1
- package/lib/Types/Socket.d.ts +3 -0
- package/lib/Types/Socket.d.ts.map +1 -1
- package/lib/Types/State.d.ts +58 -0
- package/lib/Types/State.d.ts.map +1 -1
- package/lib/Types/State.js +43 -0
- package/lib/Types/State.js.map +1 -1
- package/lib/Types/index.d.ts +1 -1
- package/lib/Types/index.d.ts.map +1 -1
- package/lib/Types/index.js +1 -1
- package/lib/Types/index.js.map +1 -1
- package/lib/Utils/auth-utils.d.ts +5 -0
- package/lib/Utils/auth-utils.d.ts.map +1 -1
- package/lib/Utils/auth-utils.js +65 -20
- package/lib/Utils/auth-utils.js.map +1 -1
- package/lib/Utils/chat-utils.d.ts +31 -1
- package/lib/Utils/chat-utils.d.ts.map +1 -1
- package/lib/Utils/chat-utils.js +180 -71
- package/lib/Utils/chat-utils.js.map +1 -1
- package/lib/Utils/companion-reg-client-utils.d.ts +17 -0
- package/lib/Utils/companion-reg-client-utils.d.ts.map +1 -0
- package/lib/Utils/companion-reg-client-utils.js +35 -0
- package/lib/Utils/companion-reg-client-utils.js.map +1 -0
- package/lib/Utils/crypto.d.ts +4 -8
- package/lib/Utils/crypto.d.ts.map +1 -1
- package/lib/Utils/crypto.js +2 -26
- package/lib/Utils/crypto.js.map +1 -1
- package/lib/Utils/decode-wa-message.d.ts +18 -0
- package/lib/Utils/decode-wa-message.d.ts.map +1 -1
- package/lib/Utils/decode-wa-message.js +34 -0
- package/lib/Utils/decode-wa-message.js.map +1 -1
- package/lib/Utils/event-buffer.d.ts +2 -0
- package/lib/Utils/event-buffer.d.ts.map +1 -1
- package/lib/Utils/event-buffer.js +82 -8
- package/lib/Utils/event-buffer.js.map +1 -1
- package/lib/Utils/generics.d.ts +2 -0
- package/lib/Utils/generics.d.ts.map +1 -1
- package/lib/Utils/generics.js +13 -1
- package/lib/Utils/generics.js.map +1 -1
- package/lib/Utils/history.d.ts +8 -3
- package/lib/Utils/history.d.ts.map +1 -1
- package/lib/Utils/history.js +58 -14
- package/lib/Utils/history.js.map +1 -1
- package/lib/Utils/identity-change-handler.d.ts +44 -0
- package/lib/Utils/identity-change-handler.d.ts.map +1 -0
- package/lib/Utils/identity-change-handler.js +50 -0
- package/lib/Utils/identity-change-handler.js.map +1 -0
- package/lib/Utils/index.d.ts +3 -0
- package/lib/Utils/index.d.ts.map +1 -1
- package/lib/Utils/index.js +3 -0
- package/lib/Utils/index.js.map +1 -1
- package/lib/Utils/link-preview.js +2 -2
- package/lib/Utils/link-preview.js.map +1 -1
- package/lib/Utils/lt-hash.d.ts +7 -12
- package/lib/Utils/lt-hash.d.ts.map +1 -1
- package/lib/Utils/lt-hash.js +2 -42
- package/lib/Utils/lt-hash.js.map +1 -1
- package/lib/Utils/make-mutex.d.ts +1 -0
- package/lib/Utils/make-mutex.d.ts.map +1 -1
- package/lib/Utils/make-mutex.js +20 -27
- package/lib/Utils/make-mutex.js.map +1 -1
- package/lib/Utils/message-retry-manager.d.ts +35 -2
- package/lib/Utils/message-retry-manager.d.ts.map +1 -1
- package/lib/Utils/message-retry-manager.js +98 -5
- package/lib/Utils/message-retry-manager.js.map +1 -1
- package/lib/Utils/messages-media.d.ts +22 -3
- package/lib/Utils/messages-media.d.ts.map +1 -1
- package/lib/Utils/messages-media.js +169 -44
- package/lib/Utils/messages-media.js.map +1 -1
- package/lib/Utils/messages.d.ts +2 -0
- package/lib/Utils/messages.d.ts.map +1 -1
- package/lib/Utils/messages.js +70 -27
- package/lib/Utils/messages.js.map +1 -1
- package/lib/Utils/noise-handler.d.ts +4 -4
- package/lib/Utils/noise-handler.d.ts.map +1 -1
- package/lib/Utils/noise-handler.js +139 -85
- package/lib/Utils/noise-handler.js.map +1 -1
- package/lib/Utils/offline-node-processor.d.ts +17 -0
- package/lib/Utils/offline-node-processor.d.ts.map +1 -0
- package/lib/Utils/offline-node-processor.js +40 -0
- package/lib/Utils/offline-node-processor.js.map +1 -0
- package/lib/Utils/process-message.d.ts.map +1 -1
- package/lib/Utils/process-message.js +165 -15
- package/lib/Utils/process-message.js.map +1 -1
- package/lib/Utils/reporting-utils.d.ts +11 -0
- package/lib/Utils/reporting-utils.d.ts.map +1 -0
- package/lib/Utils/reporting-utils.js +258 -0
- package/lib/Utils/reporting-utils.js.map +1 -0
- package/lib/Utils/signal.d.ts +13 -0
- package/lib/Utils/signal.d.ts.map +1 -1
- package/lib/Utils/signal.js +42 -0
- package/lib/Utils/signal.js.map +1 -1
- package/lib/Utils/stanza-ack.d.ts +11 -0
- package/lib/Utils/stanza-ack.d.ts.map +1 -0
- package/lib/Utils/stanza-ack.js +38 -0
- package/lib/Utils/stanza-ack.js.map +1 -0
- package/lib/Utils/sync-action-utils.d.ts +19 -0
- package/lib/Utils/sync-action-utils.d.ts.map +1 -0
- package/lib/Utils/sync-action-utils.js +49 -0
- package/lib/Utils/sync-action-utils.js.map +1 -0
- package/lib/Utils/tc-token-utils.d.ts +37 -0
- package/lib/Utils/tc-token-utils.d.ts.map +1 -0
- package/lib/Utils/tc-token-utils.js +163 -0
- package/lib/Utils/tc-token-utils.js.map +1 -0
- package/lib/Utils/validate-connection.d.ts.map +1 -1
- package/lib/Utils/validate-connection.js +4 -1
- package/lib/Utils/validate-connection.js.map +1 -1
- package/lib/WABinary/decode.d.ts.map +1 -1
- package/lib/WABinary/decode.js +24 -0
- package/lib/WABinary/decode.js.map +1 -1
- package/lib/WABinary/encode.js +5 -1
- package/lib/WABinary/encode.js.map +1 -1
- package/lib/WABinary/generic-utils.d.ts +1 -1
- package/lib/WABinary/generic-utils.d.ts.map +1 -1
- package/lib/WABinary/generic-utils.js +19 -8
- package/lib/WABinary/generic-utils.js.map +1 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.d.ts.map +1 -1
- package/lib/WAUSync/Protocols/USyncContactProtocol.js +26 -3
- package/lib/WAUSync/Protocols/USyncContactProtocol.js.map +1 -1
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts +10 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.d.ts.map +1 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js +25 -0
- package/lib/WAUSync/Protocols/USyncUsernameProtocol.js.map +1 -0
- package/lib/WAUSync/Protocols/index.d.ts +1 -0
- package/lib/WAUSync/Protocols/index.d.ts.map +1 -1
- package/lib/WAUSync/Protocols/index.js +1 -0
- package/lib/WAUSync/Protocols/index.js.map +1 -1
- package/lib/WAUSync/USyncQuery.d.ts +1 -0
- package/lib/WAUSync/USyncQuery.d.ts.map +1 -1
- package/lib/WAUSync/USyncQuery.js +6 -2
- package/lib/WAUSync/USyncQuery.js.map +1 -1
- package/lib/WAUSync/USyncUser.d.ts +4 -0
- package/lib/WAUSync/USyncUser.d.ts.map +1 -1
- package/lib/WAUSync/USyncUser.js +8 -0
- package/lib/WAUSync/USyncUser.js.map +1 -1
- package/package.json +39 -8
- package/lib/Types/Newsletter.d.ts.map +0 -1
- 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://
|
|
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 [
|
|
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 [
|
|
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 [
|
|
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
|
-
- **[
|
|
460
|
-
- **[
|
|
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.
|
|
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.
|
|
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.
|
|
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 [
|
|
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.
|
|
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 [
|
|
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 [
|
|
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 [
|
|
1201
|
-
- You can add `backgroundColor` and other options in the message options, see [
|
|
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
|
|
package/WAProto/fix-imports.js
CHANGED
|
@@ -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
|
-
|
|
8
|
-
let content = readFileSync(filePath, 'utf8');
|
|
7
|
+
let content = readFileSync(filePath, 'utf8')
|
|
9
8
|
|
|
10
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
'
|
|
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
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
}
|